xref: /linux/drivers/net/phy/phy-core.c (revision 44f57d78)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Core PHY library, taken from phy.c
4  */
5 #include <linux/export.h>
6 #include <linux/phy.h>
7 #include <linux/of.h>
8 
9 const char *phy_speed_to_str(int speed)
10 {
11 	BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 67,
12 		"Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
13 		"If a speed or mode has been added please update phy_speed_to_str "
14 		"and the PHY settings array.\n");
15 
16 	switch (speed) {
17 	case SPEED_10:
18 		return "10Mbps";
19 	case SPEED_100:
20 		return "100Mbps";
21 	case SPEED_1000:
22 		return "1Gbps";
23 	case SPEED_2500:
24 		return "2.5Gbps";
25 	case SPEED_5000:
26 		return "5Gbps";
27 	case SPEED_10000:
28 		return "10Gbps";
29 	case SPEED_14000:
30 		return "14Gbps";
31 	case SPEED_20000:
32 		return "20Gbps";
33 	case SPEED_25000:
34 		return "25Gbps";
35 	case SPEED_40000:
36 		return "40Gbps";
37 	case SPEED_50000:
38 		return "50Gbps";
39 	case SPEED_56000:
40 		return "56Gbps";
41 	case SPEED_100000:
42 		return "100Gbps";
43 	case SPEED_200000:
44 		return "200Gbps";
45 	case SPEED_UNKNOWN:
46 		return "Unknown";
47 	default:
48 		return "Unsupported (update phy-core.c)";
49 	}
50 }
51 EXPORT_SYMBOL_GPL(phy_speed_to_str);
52 
53 const char *phy_duplex_to_str(unsigned int duplex)
54 {
55 	if (duplex == DUPLEX_HALF)
56 		return "Half";
57 	if (duplex == DUPLEX_FULL)
58 		return "Full";
59 	if (duplex == DUPLEX_UNKNOWN)
60 		return "Unknown";
61 	return "Unsupported (update phy-core.c)";
62 }
63 EXPORT_SYMBOL_GPL(phy_duplex_to_str);
64 
65 /* A mapping of all SUPPORTED settings to speed/duplex.  This table
66  * must be grouped by speed and sorted in descending match priority
67  * - iow, descending speed. */
68 
69 #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
70 			       .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
71 
72 static const struct phy_setting settings[] = {
73 	/* 200G */
74 	PHY_SETTING( 200000, FULL, 200000baseCR4_Full		),
75 	PHY_SETTING( 200000, FULL, 200000baseKR4_Full		),
76 	PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full	),
77 	PHY_SETTING( 200000, FULL, 200000baseDR4_Full		),
78 	PHY_SETTING( 200000, FULL, 200000baseSR4_Full		),
79 	/* 100G */
80 	PHY_SETTING( 100000, FULL, 100000baseCR4_Full		),
81 	PHY_SETTING( 100000, FULL, 100000baseKR4_Full		),
82 	PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full	),
83 	PHY_SETTING( 100000, FULL, 100000baseSR4_Full		),
84 	PHY_SETTING( 100000, FULL, 100000baseCR2_Full		),
85 	PHY_SETTING( 100000, FULL, 100000baseKR2_Full		),
86 	PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full	),
87 	PHY_SETTING( 100000, FULL, 100000baseDR2_Full		),
88 	PHY_SETTING( 100000, FULL, 100000baseSR2_Full		),
89 	/* 56G */
90 	PHY_SETTING(  56000, FULL,  56000baseCR4_Full	  	),
91 	PHY_SETTING(  56000, FULL,  56000baseKR4_Full	  	),
92 	PHY_SETTING(  56000, FULL,  56000baseLR4_Full	  	),
93 	PHY_SETTING(  56000, FULL,  56000baseSR4_Full	  	),
94 	/* 50G */
95 	PHY_SETTING(  50000, FULL,  50000baseCR2_Full		),
96 	PHY_SETTING(  50000, FULL,  50000baseKR2_Full		),
97 	PHY_SETTING(  50000, FULL,  50000baseSR2_Full		),
98 	PHY_SETTING(  50000, FULL,  50000baseCR_Full		),
99 	PHY_SETTING(  50000, FULL,  50000baseKR_Full		),
100 	PHY_SETTING(  50000, FULL,  50000baseLR_ER_FR_Full	),
101 	PHY_SETTING(  50000, FULL,  50000baseDR_Full		),
102 	PHY_SETTING(  50000, FULL,  50000baseSR_Full		),
103 	/* 40G */
104 	PHY_SETTING(  40000, FULL,  40000baseCR4_Full		),
105 	PHY_SETTING(  40000, FULL,  40000baseKR4_Full		),
106 	PHY_SETTING(  40000, FULL,  40000baseLR4_Full		),
107 	PHY_SETTING(  40000, FULL,  40000baseSR4_Full		),
108 	/* 25G */
109 	PHY_SETTING(  25000, FULL,  25000baseCR_Full		),
110 	PHY_SETTING(  25000, FULL,  25000baseKR_Full		),
111 	PHY_SETTING(  25000, FULL,  25000baseSR_Full		),
112 	/* 20G */
113 	PHY_SETTING(  20000, FULL,  20000baseKR2_Full		),
114 	PHY_SETTING(  20000, FULL,  20000baseMLD2_Full		),
115 	/* 10G */
116 	PHY_SETTING(  10000, FULL,  10000baseCR_Full		),
117 	PHY_SETTING(  10000, FULL,  10000baseER_Full		),
118 	PHY_SETTING(  10000, FULL,  10000baseKR_Full		),
119 	PHY_SETTING(  10000, FULL,  10000baseKX4_Full		),
120 	PHY_SETTING(  10000, FULL,  10000baseLR_Full		),
121 	PHY_SETTING(  10000, FULL,  10000baseLRM_Full		),
122 	PHY_SETTING(  10000, FULL,  10000baseR_FEC		),
123 	PHY_SETTING(  10000, FULL,  10000baseSR_Full		),
124 	PHY_SETTING(  10000, FULL,  10000baseT_Full		),
125 	/* 5G */
126 	PHY_SETTING(   5000, FULL,   5000baseT_Full		),
127 	/* 2.5G */
128 	PHY_SETTING(   2500, FULL,   2500baseT_Full		),
129 	PHY_SETTING(   2500, FULL,   2500baseX_Full		),
130 	/* 1G */
131 	PHY_SETTING(   1000, FULL,   1000baseKX_Full		),
132 	PHY_SETTING(   1000, FULL,   1000baseT_Full		),
133 	PHY_SETTING(   1000, HALF,   1000baseT_Half		),
134 	PHY_SETTING(   1000, FULL,   1000baseX_Full		),
135 	/* 100M */
136 	PHY_SETTING(    100, FULL,    100baseT_Full		),
137 	PHY_SETTING(    100, HALF,    100baseT_Half		),
138 	/* 10M */
139 	PHY_SETTING(     10, FULL,     10baseT_Full		),
140 	PHY_SETTING(     10, HALF,     10baseT_Half		),
141 };
142 #undef PHY_SETTING
143 
144 /**
145  * phy_lookup_setting - lookup a PHY setting
146  * @speed: speed to match
147  * @duplex: duplex to match
148  * @mask: allowed link modes
149  * @exact: an exact match is required
150  *
151  * Search the settings array for a setting that matches the speed and
152  * duplex, and which is supported.
153  *
154  * If @exact is unset, either an exact match or %NULL for no match will
155  * be returned.
156  *
157  * If @exact is set, an exact match, the fastest supported setting at
158  * or below the specified speed, the slowest supported setting, or if
159  * they all fail, %NULL will be returned.
160  */
161 const struct phy_setting *
162 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
163 {
164 	const struct phy_setting *p, *match = NULL, *last = NULL;
165 	int i;
166 
167 	for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
168 		if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
169 		    test_bit(p->bit, mask)) {
170 			last = p;
171 			if (p->speed == speed && p->duplex == duplex) {
172 				/* Exact match for speed and duplex */
173 				match = p;
174 				break;
175 			} else if (!exact) {
176 				if (!match && p->speed <= speed)
177 					/* Candidate */
178 					match = p;
179 
180 				if (p->speed < speed)
181 					break;
182 			}
183 		}
184 	}
185 
186 	if (!match && !exact)
187 		match = last;
188 
189 	return match;
190 }
191 EXPORT_SYMBOL_GPL(phy_lookup_setting);
192 
193 size_t phy_speeds(unsigned int *speeds, size_t size,
194 		  unsigned long *mask)
195 {
196 	size_t count;
197 	int i;
198 
199 	for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
200 		if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
201 		    test_bit(settings[i].bit, mask) &&
202 		    (count == 0 || speeds[count - 1] != settings[i].speed))
203 			speeds[count++] = settings[i].speed;
204 
205 	return count;
206 }
207 
208 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
209 {
210 	const struct phy_setting *p;
211 	int i;
212 
213 	for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
214 		if (p->speed > max_speed)
215 			linkmode_clear_bit(p->bit, phydev->supported);
216 		else
217 			break;
218 	}
219 
220 	return 0;
221 }
222 
223 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
224 {
225 	int err;
226 
227 	err = __set_phy_supported(phydev, max_speed);
228 	if (err)
229 		return err;
230 
231 	phy_advertise_supported(phydev);
232 
233 	return 0;
234 }
235 EXPORT_SYMBOL(phy_set_max_speed);
236 
237 void of_set_phy_supported(struct phy_device *phydev)
238 {
239 	struct device_node *node = phydev->mdio.dev.of_node;
240 	u32 max_speed;
241 
242 	if (!IS_ENABLED(CONFIG_OF_MDIO))
243 		return;
244 
245 	if (!node)
246 		return;
247 
248 	if (!of_property_read_u32(node, "max-speed", &max_speed))
249 		__set_phy_supported(phydev, max_speed);
250 }
251 
252 void of_set_phy_eee_broken(struct phy_device *phydev)
253 {
254 	struct device_node *node = phydev->mdio.dev.of_node;
255 	u32 broken = 0;
256 
257 	if (!IS_ENABLED(CONFIG_OF_MDIO))
258 		return;
259 
260 	if (!node)
261 		return;
262 
263 	if (of_property_read_bool(node, "eee-broken-100tx"))
264 		broken |= MDIO_EEE_100TX;
265 	if (of_property_read_bool(node, "eee-broken-1000t"))
266 		broken |= MDIO_EEE_1000T;
267 	if (of_property_read_bool(node, "eee-broken-10gt"))
268 		broken |= MDIO_EEE_10GT;
269 	if (of_property_read_bool(node, "eee-broken-1000kx"))
270 		broken |= MDIO_EEE_1000KX;
271 	if (of_property_read_bool(node, "eee-broken-10gkx4"))
272 		broken |= MDIO_EEE_10GKX4;
273 	if (of_property_read_bool(node, "eee-broken-10gkr"))
274 		broken |= MDIO_EEE_10GKR;
275 
276 	phydev->eee_broken_modes = broken;
277 }
278 
279 /**
280  * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
281  * @phydev: The phy_device struct
282  *
283  * Resolve our and the link partner advertisements into their corresponding
284  * speed and duplex. If full duplex was negotiated, extract the pause mode
285  * from the link partner mask.
286  */
287 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
288 {
289 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
290 	int i;
291 
292 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
293 
294 	for (i = 0; i < ARRAY_SIZE(settings); i++)
295 		if (test_bit(settings[i].bit, common)) {
296 			phydev->speed = settings[i].speed;
297 			phydev->duplex = settings[i].duplex;
298 			break;
299 		}
300 
301 	if (phydev->duplex == DUPLEX_FULL) {
302 		phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
303 						  phydev->lp_advertising);
304 		phydev->asym_pause = linkmode_test_bit(
305 			ETHTOOL_LINK_MODE_Asym_Pause_BIT,
306 			phydev->lp_advertising);
307 	}
308 }
309 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
310 
311 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
312 			     u16 regnum)
313 {
314 	/* Write the desired MMD Devad */
315 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
316 
317 	/* Write the desired MMD register address */
318 	__mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
319 
320 	/* Select the Function : DATA with no post increment */
321 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
322 			devad | MII_MMD_CTRL_NOINCR);
323 }
324 
325 /**
326  * __phy_read_mmd - Convenience function for reading a register
327  * from an MMD on a given PHY.
328  * @phydev: The phy_device struct
329  * @devad: The MMD to read from (0..31)
330  * @regnum: The register on the MMD to read (0..65535)
331  *
332  * Same rules as for __phy_read();
333  */
334 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
335 {
336 	int val;
337 
338 	if (regnum > (u16)~0 || devad > 32)
339 		return -EINVAL;
340 
341 	if (phydev->drv->read_mmd) {
342 		val = phydev->drv->read_mmd(phydev, devad, regnum);
343 	} else if (phydev->is_c45) {
344 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
345 
346 		val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
347 	} else {
348 		struct mii_bus *bus = phydev->mdio.bus;
349 		int phy_addr = phydev->mdio.addr;
350 
351 		mmd_phy_indirect(bus, phy_addr, devad, regnum);
352 
353 		/* Read the content of the MMD's selected register */
354 		val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
355 	}
356 	return val;
357 }
358 EXPORT_SYMBOL(__phy_read_mmd);
359 
360 /**
361  * phy_read_mmd - Convenience function for reading a register
362  * from an MMD on a given PHY.
363  * @phydev: The phy_device struct
364  * @devad: The MMD to read from
365  * @regnum: The register on the MMD to read
366  *
367  * Same rules as for phy_read();
368  */
369 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
370 {
371 	int ret;
372 
373 	mutex_lock(&phydev->mdio.bus->mdio_lock);
374 	ret = __phy_read_mmd(phydev, devad, regnum);
375 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
376 
377 	return ret;
378 }
379 EXPORT_SYMBOL(phy_read_mmd);
380 
381 /**
382  * __phy_write_mmd - Convenience function for writing a register
383  * on an MMD on a given PHY.
384  * @phydev: The phy_device struct
385  * @devad: The MMD to read from
386  * @regnum: The register on the MMD to read
387  * @val: value to write to @regnum
388  *
389  * Same rules as for __phy_write();
390  */
391 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
392 {
393 	int ret;
394 
395 	if (regnum > (u16)~0 || devad > 32)
396 		return -EINVAL;
397 
398 	if (phydev->drv->write_mmd) {
399 		ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
400 	} else if (phydev->is_c45) {
401 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
402 
403 		ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
404 				      addr, val);
405 	} else {
406 		struct mii_bus *bus = phydev->mdio.bus;
407 		int phy_addr = phydev->mdio.addr;
408 
409 		mmd_phy_indirect(bus, phy_addr, devad, regnum);
410 
411 		/* Write the data into MMD's selected register */
412 		__mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
413 
414 		ret = 0;
415 	}
416 	return ret;
417 }
418 EXPORT_SYMBOL(__phy_write_mmd);
419 
420 /**
421  * phy_write_mmd - Convenience function for writing a register
422  * on an MMD on a given PHY.
423  * @phydev: The phy_device struct
424  * @devad: The MMD to read from
425  * @regnum: The register on the MMD to read
426  * @val: value to write to @regnum
427  *
428  * Same rules as for phy_write();
429  */
430 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
431 {
432 	int ret;
433 
434 	mutex_lock(&phydev->mdio.bus->mdio_lock);
435 	ret = __phy_write_mmd(phydev, devad, regnum, val);
436 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
437 
438 	return ret;
439 }
440 EXPORT_SYMBOL(phy_write_mmd);
441 
442 /**
443  * __phy_modify_changed() - Convenience function for modifying a PHY register
444  * @phydev: a pointer to a &struct phy_device
445  * @regnum: register number
446  * @mask: bit mask of bits to clear
447  * @set: bit mask of bits to set
448  *
449  * Unlocked helper function which allows a PHY register to be modified as
450  * new register value = (old register value & ~mask) | set
451  *
452  * Returns negative errno, 0 if there was no change, and 1 in case of change
453  */
454 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
455 			 u16 set)
456 {
457 	int new, ret;
458 
459 	ret = __phy_read(phydev, regnum);
460 	if (ret < 0)
461 		return ret;
462 
463 	new = (ret & ~mask) | set;
464 	if (new == ret)
465 		return 0;
466 
467 	ret = __phy_write(phydev, regnum, new);
468 
469 	return ret < 0 ? ret : 1;
470 }
471 EXPORT_SYMBOL_GPL(__phy_modify_changed);
472 
473 /**
474  * phy_modify_changed - Function for modifying a PHY register
475  * @phydev: the phy_device struct
476  * @regnum: register number to modify
477  * @mask: bit mask of bits to clear
478  * @set: new value of bits set in mask to write to @regnum
479  *
480  * NOTE: MUST NOT be called from interrupt context,
481  * because the bus read/write functions may wait for an interrupt
482  * to conclude the operation.
483  *
484  * Returns negative errno, 0 if there was no change, and 1 in case of change
485  */
486 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
487 {
488 	int ret;
489 
490 	mutex_lock(&phydev->mdio.bus->mdio_lock);
491 	ret = __phy_modify_changed(phydev, regnum, mask, set);
492 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
493 
494 	return ret;
495 }
496 EXPORT_SYMBOL_GPL(phy_modify_changed);
497 
498 /**
499  * __phy_modify - Convenience function for modifying a PHY register
500  * @phydev: the phy_device struct
501  * @regnum: register number to modify
502  * @mask: bit mask of bits to clear
503  * @set: new value of bits set in mask to write to @regnum
504  *
505  * NOTE: MUST NOT be called from interrupt context,
506  * because the bus read/write functions may wait for an interrupt
507  * to conclude the operation.
508  */
509 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
510 {
511 	int ret;
512 
513 	ret = __phy_modify_changed(phydev, regnum, mask, set);
514 
515 	return ret < 0 ? ret : 0;
516 }
517 EXPORT_SYMBOL_GPL(__phy_modify);
518 
519 /**
520  * phy_modify - Convenience function for modifying a given PHY register
521  * @phydev: the phy_device struct
522  * @regnum: register number to write
523  * @mask: bit mask of bits to clear
524  * @set: new value of bits set in mask to write to @regnum
525  *
526  * NOTE: MUST NOT be called from interrupt context,
527  * because the bus read/write functions may wait for an interrupt
528  * to conclude the operation.
529  */
530 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
531 {
532 	int ret;
533 
534 	mutex_lock(&phydev->mdio.bus->mdio_lock);
535 	ret = __phy_modify(phydev, regnum, mask, set);
536 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
537 
538 	return ret;
539 }
540 EXPORT_SYMBOL_GPL(phy_modify);
541 
542 /**
543  * __phy_modify_mmd_changed - Function for modifying a register on MMD
544  * @phydev: the phy_device struct
545  * @devad: the MMD containing register to modify
546  * @regnum: register number to modify
547  * @mask: bit mask of bits to clear
548  * @set: new value of bits set in mask to write to @regnum
549  *
550  * Unlocked helper function which allows a MMD register to be modified as
551  * new register value = (old register value & ~mask) | set
552  *
553  * Returns negative errno, 0 if there was no change, and 1 in case of change
554  */
555 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
556 			     u16 mask, u16 set)
557 {
558 	int new, ret;
559 
560 	ret = __phy_read_mmd(phydev, devad, regnum);
561 	if (ret < 0)
562 		return ret;
563 
564 	new = (ret & ~mask) | set;
565 	if (new == ret)
566 		return 0;
567 
568 	ret = __phy_write_mmd(phydev, devad, regnum, new);
569 
570 	return ret < 0 ? ret : 1;
571 }
572 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
573 
574 /**
575  * phy_modify_mmd_changed - Function for modifying a register on MMD
576  * @phydev: the phy_device struct
577  * @devad: the MMD containing register to modify
578  * @regnum: register number to modify
579  * @mask: bit mask of bits to clear
580  * @set: new value of bits set in mask to write to @regnum
581  *
582  * NOTE: MUST NOT be called from interrupt context,
583  * because the bus read/write functions may wait for an interrupt
584  * to conclude the operation.
585  *
586  * Returns negative errno, 0 if there was no change, and 1 in case of change
587  */
588 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
589 			   u16 mask, u16 set)
590 {
591 	int ret;
592 
593 	mutex_lock(&phydev->mdio.bus->mdio_lock);
594 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
595 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
596 
597 	return ret;
598 }
599 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
600 
601 /**
602  * __phy_modify_mmd - Convenience function for modifying a register on MMD
603  * @phydev: the phy_device struct
604  * @devad: the MMD containing register to modify
605  * @regnum: register number to modify
606  * @mask: bit mask of bits to clear
607  * @set: new value of bits set in mask to write to @regnum
608  *
609  * NOTE: MUST NOT be called from interrupt context,
610  * because the bus read/write functions may wait for an interrupt
611  * to conclude the operation.
612  */
613 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
614 		     u16 mask, u16 set)
615 {
616 	int ret;
617 
618 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
619 
620 	return ret < 0 ? ret : 0;
621 }
622 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
623 
624 /**
625  * phy_modify_mmd - Convenience function for modifying a register on MMD
626  * @phydev: the phy_device struct
627  * @devad: the MMD containing register to modify
628  * @regnum: register number to modify
629  * @mask: bit mask of bits to clear
630  * @set: new value of bits set in mask to write to @regnum
631  *
632  * NOTE: MUST NOT be called from interrupt context,
633  * because the bus read/write functions may wait for an interrupt
634  * to conclude the operation.
635  */
636 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
637 		   u16 mask, u16 set)
638 {
639 	int ret;
640 
641 	mutex_lock(&phydev->mdio.bus->mdio_lock);
642 	ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
643 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
644 
645 	return ret;
646 }
647 EXPORT_SYMBOL_GPL(phy_modify_mmd);
648 
649 static int __phy_read_page(struct phy_device *phydev)
650 {
651 	return phydev->drv->read_page(phydev);
652 }
653 
654 static int __phy_write_page(struct phy_device *phydev, int page)
655 {
656 	return phydev->drv->write_page(phydev, page);
657 }
658 
659 /**
660  * phy_save_page() - take the bus lock and save the current page
661  * @phydev: a pointer to a &struct phy_device
662  *
663  * Take the MDIO bus lock, and return the current page number. On error,
664  * returns a negative errno. phy_restore_page() must always be called
665  * after this, irrespective of success or failure of this call.
666  */
667 int phy_save_page(struct phy_device *phydev)
668 {
669 	mutex_lock(&phydev->mdio.bus->mdio_lock);
670 	return __phy_read_page(phydev);
671 }
672 EXPORT_SYMBOL_GPL(phy_save_page);
673 
674 /**
675  * phy_select_page() - take the bus lock, save the current page, and set a page
676  * @phydev: a pointer to a &struct phy_device
677  * @page: desired page
678  *
679  * Take the MDIO bus lock to protect against concurrent access, save the
680  * current PHY page, and set the current page.  On error, returns a
681  * negative errno, otherwise returns the previous page number.
682  * phy_restore_page() must always be called after this, irrespective
683  * of success or failure of this call.
684  */
685 int phy_select_page(struct phy_device *phydev, int page)
686 {
687 	int ret, oldpage;
688 
689 	oldpage = ret = phy_save_page(phydev);
690 	if (ret < 0)
691 		return ret;
692 
693 	if (oldpage != page) {
694 		ret = __phy_write_page(phydev, page);
695 		if (ret < 0)
696 			return ret;
697 	}
698 
699 	return oldpage;
700 }
701 EXPORT_SYMBOL_GPL(phy_select_page);
702 
703 /**
704  * phy_restore_page() - restore the page register and release the bus lock
705  * @phydev: a pointer to a &struct phy_device
706  * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
707  * @ret: operation's return code
708  *
709  * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
710  * This function propagates the earliest error code from the group of
711  * operations.
712  *
713  * Returns:
714  *   @oldpage if it was a negative value, otherwise
715  *   @ret if it was a negative errno value, otherwise
716  *   phy_write_page()'s negative value if it were in error, otherwise
717  *   @ret.
718  */
719 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
720 {
721 	int r;
722 
723 	if (oldpage >= 0) {
724 		r = __phy_write_page(phydev, oldpage);
725 
726 		/* Propagate the operation return code if the page write
727 		 * was successful.
728 		 */
729 		if (ret >= 0 && r < 0)
730 			ret = r;
731 	} else {
732 		/* Propagate the phy page selection error code */
733 		ret = oldpage;
734 	}
735 
736 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
737 
738 	return ret;
739 }
740 EXPORT_SYMBOL_GPL(phy_restore_page);
741 
742 /**
743  * phy_read_paged() - Convenience function for reading a paged register
744  * @phydev: a pointer to a &struct phy_device
745  * @page: the page for the phy
746  * @regnum: register number
747  *
748  * Same rules as for phy_read().
749  */
750 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
751 {
752 	int ret = 0, oldpage;
753 
754 	oldpage = phy_select_page(phydev, page);
755 	if (oldpage >= 0)
756 		ret = __phy_read(phydev, regnum);
757 
758 	return phy_restore_page(phydev, oldpage, ret);
759 }
760 EXPORT_SYMBOL(phy_read_paged);
761 
762 /**
763  * phy_write_paged() - Convenience function for writing a paged register
764  * @phydev: a pointer to a &struct phy_device
765  * @page: the page for the phy
766  * @regnum: register number
767  * @val: value to write
768  *
769  * Same rules as for phy_write().
770  */
771 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
772 {
773 	int ret = 0, oldpage;
774 
775 	oldpage = phy_select_page(phydev, page);
776 	if (oldpage >= 0)
777 		ret = __phy_write(phydev, regnum, val);
778 
779 	return phy_restore_page(phydev, oldpage, ret);
780 }
781 EXPORT_SYMBOL(phy_write_paged);
782 
783 /**
784  * phy_modify_paged() - Convenience function for modifying a paged register
785  * @phydev: a pointer to a &struct phy_device
786  * @page: the page for the phy
787  * @regnum: register number
788  * @mask: bit mask of bits to clear
789  * @set: bit mask of bits to set
790  *
791  * Same rules as for phy_read() and phy_write().
792  */
793 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
794 		     u16 mask, u16 set)
795 {
796 	int ret = 0, oldpage;
797 
798 	oldpage = phy_select_page(phydev, page);
799 	if (oldpage >= 0)
800 		ret = __phy_modify(phydev, regnum, mask, set);
801 
802 	return phy_restore_page(phydev, oldpage, ret);
803 }
804 EXPORT_SYMBOL(phy_modify_paged);
805