xref: /linux/drivers/net/phy/phy-core.c (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
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 #include "phylib.h"
10 #include "phylib-internal.h"
11 #include "phy-caps.h"
12 
13 /**
14  * phy_speed_to_str - Return a string representing the PHY link speed
15  *
16  * @speed: Speed of the link
17  */
18 const char *phy_speed_to_str(int speed)
19 {
20 	BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 121,
21 		"Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
22 		"If a speed or mode has been added please update phy_speed_to_str "
23 		"and the PHY settings array.\n");
24 
25 	switch (speed) {
26 	case SPEED_10:
27 		return "10Mbps";
28 	case SPEED_100:
29 		return "100Mbps";
30 	case SPEED_1000:
31 		return "1Gbps";
32 	case SPEED_2500:
33 		return "2.5Gbps";
34 	case SPEED_5000:
35 		return "5Gbps";
36 	case SPEED_10000:
37 		return "10Gbps";
38 	case SPEED_14000:
39 		return "14Gbps";
40 	case SPEED_20000:
41 		return "20Gbps";
42 	case SPEED_25000:
43 		return "25Gbps";
44 	case SPEED_40000:
45 		return "40Gbps";
46 	case SPEED_50000:
47 		return "50Gbps";
48 	case SPEED_56000:
49 		return "56Gbps";
50 	case SPEED_100000:
51 		return "100Gbps";
52 	case SPEED_200000:
53 		return "200Gbps";
54 	case SPEED_400000:
55 		return "400Gbps";
56 	case SPEED_800000:
57 		return "800Gbps";
58 	case SPEED_UNKNOWN:
59 		return "Unknown";
60 	default:
61 		return "Unsupported (update phy-core.c)";
62 	}
63 }
64 EXPORT_SYMBOL_GPL(phy_speed_to_str);
65 
66 /**
67  * phy_duplex_to_str - Return string describing the duplex
68  *
69  * @duplex: Duplex setting to describe
70  */
71 const char *phy_duplex_to_str(unsigned int duplex)
72 {
73 	if (duplex == DUPLEX_HALF)
74 		return "Half";
75 	if (duplex == DUPLEX_FULL)
76 		return "Full";
77 	if (duplex == DUPLEX_UNKNOWN)
78 		return "Unknown";
79 	return "Unsupported (update phy-core.c)";
80 }
81 EXPORT_SYMBOL_GPL(phy_duplex_to_str);
82 
83 /**
84  * phy_rate_matching_to_str - Return a string describing the rate matching
85  *
86  * @rate_matching: Type of rate matching to describe
87  */
88 const char *phy_rate_matching_to_str(int rate_matching)
89 {
90 	switch (rate_matching) {
91 	case RATE_MATCH_NONE:
92 		return "none";
93 	case RATE_MATCH_PAUSE:
94 		return "pause";
95 	case RATE_MATCH_CRS:
96 		return "crs";
97 	case RATE_MATCH_OPEN_LOOP:
98 		return "open-loop";
99 	}
100 	return "Unsupported (update phy-core.c)";
101 }
102 EXPORT_SYMBOL_GPL(phy_rate_matching_to_str);
103 
104 /**
105  * phy_interface_num_ports - Return the number of links that can be carried by
106  *			     a given MAC-PHY physical link. Returns 0 if this is
107  *			     unknown, the number of links else.
108  *
109  * @interface: The interface mode we want to get the number of ports
110  */
111 int phy_interface_num_ports(phy_interface_t interface)
112 {
113 	switch (interface) {
114 	case PHY_INTERFACE_MODE_NA:
115 		return 0;
116 	case PHY_INTERFACE_MODE_INTERNAL:
117 	case PHY_INTERFACE_MODE_MII:
118 	case PHY_INTERFACE_MODE_GMII:
119 	case PHY_INTERFACE_MODE_TBI:
120 	case PHY_INTERFACE_MODE_REVMII:
121 	case PHY_INTERFACE_MODE_RMII:
122 	case PHY_INTERFACE_MODE_REVRMII:
123 	case PHY_INTERFACE_MODE_RGMII:
124 	case PHY_INTERFACE_MODE_RGMII_ID:
125 	case PHY_INTERFACE_MODE_RGMII_RXID:
126 	case PHY_INTERFACE_MODE_RGMII_TXID:
127 	case PHY_INTERFACE_MODE_RTBI:
128 	case PHY_INTERFACE_MODE_XGMII:
129 	case PHY_INTERFACE_MODE_XLGMII:
130 	case PHY_INTERFACE_MODE_MOCA:
131 	case PHY_INTERFACE_MODE_TRGMII:
132 	case PHY_INTERFACE_MODE_USXGMII:
133 	case PHY_INTERFACE_MODE_SGMII:
134 	case PHY_INTERFACE_MODE_SMII:
135 	case PHY_INTERFACE_MODE_1000BASEX:
136 	case PHY_INTERFACE_MODE_2500BASEX:
137 	case PHY_INTERFACE_MODE_5GBASER:
138 	case PHY_INTERFACE_MODE_10GBASER:
139 	case PHY_INTERFACE_MODE_25GBASER:
140 	case PHY_INTERFACE_MODE_10GKR:
141 	case PHY_INTERFACE_MODE_100BASEX:
142 	case PHY_INTERFACE_MODE_RXAUI:
143 	case PHY_INTERFACE_MODE_XAUI:
144 	case PHY_INTERFACE_MODE_1000BASEKX:
145 	case PHY_INTERFACE_MODE_50GBASER:
146 	case PHY_INTERFACE_MODE_LAUI:
147 	case PHY_INTERFACE_MODE_100GBASEP:
148 		return 1;
149 	case PHY_INTERFACE_MODE_QSGMII:
150 	case PHY_INTERFACE_MODE_QUSGMII:
151 	case PHY_INTERFACE_MODE_10G_QXGMII:
152 		return 4;
153 	case PHY_INTERFACE_MODE_PSGMII:
154 		return 5;
155 	case PHY_INTERFACE_MODE_MAX:
156 		WARN_ONCE(1, "PHY_INTERFACE_MODE_MAX isn't a valid interface mode");
157 		return 0;
158 	}
159 	return 0;
160 }
161 EXPORT_SYMBOL_GPL(phy_interface_num_ports);
162 
163 static void __set_phy_supported(struct phy_device *phydev, u32 max_speed)
164 {
165 	phy_caps_linkmode_max_speed(max_speed, phydev->supported);
166 }
167 
168 /**
169  * phy_set_max_speed - Set the maximum speed the PHY should support
170  *
171  * @phydev: The phy_device struct
172  * @max_speed: Maximum speed
173  *
174  * The PHY might be more capable than the MAC. For example a Fast Ethernet
175  * is connected to a 1G PHY. This function allows the MAC to indicate its
176  * maximum speed, and so limit what the PHY will advertise.
177  */
178 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
179 {
180 	__set_phy_supported(phydev, max_speed);
181 
182 	phy_advertise_supported(phydev);
183 }
184 EXPORT_SYMBOL(phy_set_max_speed);
185 
186 void of_set_phy_supported(struct phy_device *phydev)
187 {
188 	struct device_node *node = phydev->mdio.dev.of_node;
189 	u32 max_speed;
190 
191 	if (!IS_ENABLED(CONFIG_OF_MDIO))
192 		return;
193 
194 	if (!node)
195 		return;
196 
197 	if (!of_property_read_u32(node, "max-speed", &max_speed))
198 		__set_phy_supported(phydev, max_speed);
199 }
200 
201 void of_set_phy_eee_broken(struct phy_device *phydev)
202 {
203 	struct device_node *node = phydev->mdio.dev.of_node;
204 	unsigned long *modes = phydev->eee_disabled_modes;
205 
206 	if (!IS_ENABLED(CONFIG_OF_MDIO) || !node)
207 		return;
208 
209 	linkmode_zero(modes);
210 
211 	if (of_property_read_bool(node, "eee-broken-100tx"))
212 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, modes);
213 	if (of_property_read_bool(node, "eee-broken-1000t"))
214 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, modes);
215 	if (of_property_read_bool(node, "eee-broken-10gt"))
216 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, modes);
217 	if (of_property_read_bool(node, "eee-broken-1000kx"))
218 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, modes);
219 	if (of_property_read_bool(node, "eee-broken-10gkx4"))
220 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, modes);
221 	if (of_property_read_bool(node, "eee-broken-10gkr"))
222 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, modes);
223 }
224 
225 /**
226  * of_set_phy_timing_role - Set the master/slave mode of the PHY
227  *
228  * @phydev: The phy_device struct
229  *
230  * Set master/slave configuration of the PHY based on the device tree.
231  */
232 void of_set_phy_timing_role(struct phy_device *phydev)
233 {
234 	struct device_node *node = phydev->mdio.dev.of_node;
235 	const char *master;
236 
237 	if (!IS_ENABLED(CONFIG_OF_MDIO))
238 		return;
239 
240 	if (!node)
241 		return;
242 
243 	if (of_property_read_string(node, "timing-role", &master))
244 		return;
245 
246 	if (strcmp(master, "forced-master") == 0)
247 		phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_FORCE;
248 	else if (strcmp(master, "forced-slave") == 0)
249 		phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_FORCE;
250 	else if (strcmp(master, "preferred-master") == 0)
251 		phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_PREFERRED;
252 	else if (strcmp(master, "preferred-slave") == 0)
253 		phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
254 	else
255 		phydev_warn(phydev, "Unknown master-slave mode %s\n", master);
256 }
257 
258 /**
259  * phy_resolve_aneg_pause - Determine pause autoneg results
260  *
261  * @phydev: The phy_device struct
262  *
263  * Once autoneg has completed the local pause settings can be
264  * resolved.  Determine if pause and asymmetric pause should be used
265  * by the MAC.
266  */
267 
268 void phy_resolve_aneg_pause(struct phy_device *phydev)
269 {
270 	if (phydev->duplex == DUPLEX_FULL) {
271 		phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
272 						  phydev->lp_advertising);
273 		phydev->asym_pause = linkmode_test_bit(
274 			ETHTOOL_LINK_MODE_Asym_Pause_BIT,
275 			phydev->lp_advertising);
276 	}
277 }
278 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
279 
280 /**
281  * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings
282  * @phydev: The phy_device struct
283  *
284  * Resolve our and the link partner advertisements into their corresponding
285  * speed and duplex. If full duplex was negotiated, extract the pause mode
286  * from the link partner mask.
287  */
288 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
289 {
290 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
291 	const struct link_capabilities *c;
292 
293 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
294 
295 	c = phy_caps_lookup_by_linkmode(common);
296 	if (c) {
297 		phydev->speed = c->speed;
298 		phydev->duplex = c->duplex;
299 	}
300 
301 	phy_resolve_aneg_pause(phydev);
302 }
303 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
304 
305 /**
306  * phy_check_downshift - check whether downshift occurred
307  * @phydev: The phy_device struct
308  *
309  * Check whether a downshift to a lower speed occurred. If this should be the
310  * case warn the user.
311  * Prerequisite for detecting downshift is that PHY driver implements the
312  * read_status callback and sets phydev->speed to the actual link speed.
313  */
314 void phy_check_downshift(struct phy_device *phydev)
315 {
316 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
317 	const struct link_capabilities *c;
318 	int speed = SPEED_UNKNOWN;
319 
320 	phydev->downshifted_rate = 0;
321 
322 	if (phydev->autoneg == AUTONEG_DISABLE ||
323 	    phydev->speed == SPEED_UNKNOWN)
324 		return;
325 
326 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
327 
328 	c = phy_caps_lookup_by_linkmode(common);
329 	if (c)
330 		speed = c->speed;
331 
332 	if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
333 		return;
334 
335 	phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
336 		    phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
337 
338 	phydev->downshifted_rate = 1;
339 }
340 
341 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
342 {
343 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
344 	const struct link_capabilities *c;
345 
346 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
347 
348 	c = phy_caps_lookup_by_linkmode_rev(common, fdx_only);
349 	if (c)
350 		return c->speed;
351 
352 	return SPEED_UNKNOWN;
353 }
354 
355 int phy_speed_down_core(struct phy_device *phydev)
356 {
357 	int min_common_speed = phy_resolve_min_speed(phydev, true);
358 
359 	if (min_common_speed == SPEED_UNKNOWN)
360 		return -EINVAL;
361 
362 	phy_caps_linkmode_max_speed(min_common_speed, phydev->advertising);
363 
364 	return 0;
365 }
366 
367 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
368 			     u16 regnum)
369 {
370 	/* Write the desired MMD Devad */
371 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
372 
373 	/* Write the desired MMD register address */
374 	__mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
375 
376 	/* Select the Function : DATA with no post increment */
377 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
378 			devad | MII_MMD_CTRL_NOINCR);
379 }
380 
381 int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45,
382 		 int devad, u32 regnum)
383 {
384 	if (is_c45)
385 		return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
386 
387 	mmd_phy_indirect(bus, phy_addr, devad, regnum);
388 	/* Read the content of the MMD's selected register */
389 	return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
390 }
391 EXPORT_SYMBOL_GPL(mmd_phy_read);
392 
393 int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45,
394 		  int devad, u32 regnum, u16 val)
395 {
396 	if (is_c45)
397 		return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
398 
399 	mmd_phy_indirect(bus, phy_addr, devad, regnum);
400 	/* Write the data into MMD's selected register */
401 	return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
402 }
403 EXPORT_SYMBOL_GPL(mmd_phy_write);
404 
405 /**
406  * __phy_read_mmd - Convenience function for reading a register
407  * from an MMD on a given PHY.
408  * @phydev: The phy_device struct
409  * @devad: The MMD to read from (0..31)
410  * @regnum: The register on the MMD to read (0..65535)
411  *
412  * Same rules as for __phy_read();
413  */
414 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
415 {
416 	if (regnum > (u16)~0 || devad > 32)
417 		return -EINVAL;
418 
419 	if (phydev->drv && phydev->drv->read_mmd)
420 		return phydev->drv->read_mmd(phydev, devad, regnum);
421 
422 	return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr,
423 			    phydev->is_c45, devad, regnum);
424 }
425 EXPORT_SYMBOL(__phy_read_mmd);
426 
427 /**
428  * phy_read_mmd - Convenience function for reading a register
429  * from an MMD on a given PHY.
430  * @phydev: The phy_device struct
431  * @devad: The MMD to read from
432  * @regnum: The register on the MMD to read
433  *
434  * Same rules as for phy_read();
435  */
436 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
437 {
438 	int ret;
439 
440 	phy_lock_mdio_bus(phydev);
441 	ret = __phy_read_mmd(phydev, devad, regnum);
442 	phy_unlock_mdio_bus(phydev);
443 
444 	return ret;
445 }
446 EXPORT_SYMBOL(phy_read_mmd);
447 
448 /**
449  * __phy_write_mmd - Convenience function for writing a register
450  * on an MMD on a given PHY.
451  * @phydev: The phy_device struct
452  * @devad: The MMD to read from
453  * @regnum: The register on the MMD to read
454  * @val: value to write to @regnum
455  *
456  * Same rules as for __phy_write();
457  */
458 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
459 {
460 	if (regnum > (u16)~0 || devad > 32)
461 		return -EINVAL;
462 
463 	if (phydev->drv && phydev->drv->write_mmd)
464 		return phydev->drv->write_mmd(phydev, devad, regnum, val);
465 
466 	return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr,
467 			     phydev->is_c45, devad, regnum, val);
468 }
469 EXPORT_SYMBOL(__phy_write_mmd);
470 
471 /**
472  * phy_write_mmd - Convenience function for writing a register
473  * on an MMD on a given PHY.
474  * @phydev: The phy_device struct
475  * @devad: The MMD to read from
476  * @regnum: The register on the MMD to read
477  * @val: value to write to @regnum
478  *
479  * Same rules as for phy_write();
480  */
481 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
482 {
483 	int ret;
484 
485 	phy_lock_mdio_bus(phydev);
486 	ret = __phy_write_mmd(phydev, devad, regnum, val);
487 	phy_unlock_mdio_bus(phydev);
488 
489 	return ret;
490 }
491 EXPORT_SYMBOL(phy_write_mmd);
492 
493 /**
494  * phy_modify_changed - Function for modifying a PHY register
495  * @phydev: the phy_device struct
496  * @regnum: register number to modify
497  * @mask: bit mask of bits to clear
498  * @set: new value of bits set in mask to write to @regnum
499  *
500  * NOTE: MUST NOT be called from interrupt context,
501  * because the bus read/write functions may wait for an interrupt
502  * to conclude the operation.
503  *
504  * Returns negative errno, 0 if there was no change, and 1 in case of change
505  */
506 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
507 {
508 	int ret;
509 
510 	phy_lock_mdio_bus(phydev);
511 	ret = __phy_modify_changed(phydev, regnum, mask, set);
512 	phy_unlock_mdio_bus(phydev);
513 
514 	return ret;
515 }
516 EXPORT_SYMBOL_GPL(phy_modify_changed);
517 
518 /**
519  * __phy_modify - Convenience function for modifying a PHY register
520  * @phydev: the phy_device struct
521  * @regnum: register number to modify
522  * @mask: bit mask of bits to clear
523  * @set: new value of bits set in mask to write to @regnum
524  *
525  * NOTE: MUST NOT be called from interrupt context,
526  * because the bus read/write functions may wait for an interrupt
527  * to conclude the operation.
528  */
529 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
530 {
531 	int ret;
532 
533 	ret = __phy_modify_changed(phydev, regnum, mask, set);
534 
535 	return ret < 0 ? ret : 0;
536 }
537 EXPORT_SYMBOL_GPL(__phy_modify);
538 
539 /**
540  * phy_modify - Convenience function for modifying a given PHY register
541  * @phydev: the phy_device struct
542  * @regnum: register number to write
543  * @mask: bit mask of bits to clear
544  * @set: new value of bits set in mask to write to @regnum
545  *
546  * NOTE: MUST NOT be called from interrupt context,
547  * because the bus read/write functions may wait for an interrupt
548  * to conclude the operation.
549  */
550 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
551 {
552 	int ret;
553 
554 	phy_lock_mdio_bus(phydev);
555 	ret = __phy_modify(phydev, regnum, mask, set);
556 	phy_unlock_mdio_bus(phydev);
557 
558 	return ret;
559 }
560 EXPORT_SYMBOL_GPL(phy_modify);
561 
562 /**
563  * __phy_modify_mmd_changed - Function for modifying a register on MMD
564  * @phydev: the phy_device struct
565  * @devad: the MMD containing register to modify
566  * @regnum: register number to modify
567  * @mask: bit mask of bits to clear
568  * @set: new value of bits set in mask to write to @regnum
569  *
570  * Unlocked helper function which allows a MMD register to be modified as
571  * new register value = (old register value & ~mask) | set
572  *
573  * Returns negative errno, 0 if there was no change, and 1 in case of change
574  */
575 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
576 			     u16 mask, u16 set)
577 {
578 	int new, ret;
579 
580 	ret = __phy_read_mmd(phydev, devad, regnum);
581 	if (ret < 0)
582 		return ret;
583 
584 	new = (ret & ~mask) | set;
585 	if (new == ret)
586 		return 0;
587 
588 	ret = __phy_write_mmd(phydev, devad, regnum, new);
589 
590 	return ret < 0 ? ret : 1;
591 }
592 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
593 
594 /**
595  * phy_modify_mmd_changed - Function for modifying a register on MMD
596  * @phydev: the phy_device struct
597  * @devad: the MMD containing register to modify
598  * @regnum: register number to modify
599  * @mask: bit mask of bits to clear
600  * @set: new value of bits set in mask to write to @regnum
601  *
602  * NOTE: MUST NOT be called from interrupt context,
603  * because the bus read/write functions may wait for an interrupt
604  * to conclude the operation.
605  *
606  * Returns negative errno, 0 if there was no change, and 1 in case of change
607  */
608 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
609 			   u16 mask, u16 set)
610 {
611 	int ret;
612 
613 	phy_lock_mdio_bus(phydev);
614 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
615 	phy_unlock_mdio_bus(phydev);
616 
617 	return ret;
618 }
619 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
620 
621 /**
622  * __phy_modify_mmd - Convenience function for modifying a register on MMD
623  * @phydev: the phy_device struct
624  * @devad: the MMD containing register to modify
625  * @regnum: register number to modify
626  * @mask: bit mask of bits to clear
627  * @set: new value of bits set in mask to write to @regnum
628  *
629  * NOTE: MUST NOT be called from interrupt context,
630  * because the bus read/write functions may wait for an interrupt
631  * to conclude the operation.
632  */
633 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
634 		     u16 mask, u16 set)
635 {
636 	int ret;
637 
638 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
639 
640 	return ret < 0 ? ret : 0;
641 }
642 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
643 
644 /**
645  * phy_modify_mmd - Convenience function for modifying a register on MMD
646  * @phydev: the phy_device struct
647  * @devad: the MMD containing register to modify
648  * @regnum: register number to modify
649  * @mask: bit mask of bits to clear
650  * @set: new value of bits set in mask to write to @regnum
651  *
652  * NOTE: MUST NOT be called from interrupt context,
653  * because the bus read/write functions may wait for an interrupt
654  * to conclude the operation.
655  */
656 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
657 		   u16 mask, u16 set)
658 {
659 	int ret;
660 
661 	phy_lock_mdio_bus(phydev);
662 	ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
663 	phy_unlock_mdio_bus(phydev);
664 
665 	return ret;
666 }
667 EXPORT_SYMBOL_GPL(phy_modify_mmd);
668 
669 static int __phy_read_page(struct phy_device *phydev)
670 {
671 	if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
672 		return -EOPNOTSUPP;
673 
674 	return phydev->drv->read_page(phydev);
675 }
676 
677 static int __phy_write_page(struct phy_device *phydev, int page)
678 {
679 	if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
680 		return -EOPNOTSUPP;
681 
682 	return phydev->drv->write_page(phydev, page);
683 }
684 
685 /**
686  * phy_save_page() - take the bus lock and save the current page
687  * @phydev: a pointer to a &struct phy_device
688  *
689  * Take the MDIO bus lock, and return the current page number. On error,
690  * returns a negative errno. phy_restore_page() must always be called
691  * after this, irrespective of success or failure of this call.
692  */
693 int phy_save_page(struct phy_device *phydev)
694 {
695 	phy_lock_mdio_bus(phydev);
696 	return __phy_read_page(phydev);
697 }
698 EXPORT_SYMBOL_GPL(phy_save_page);
699 
700 /**
701  * phy_select_page() - take the bus lock, save the current page, and set a page
702  * @phydev: a pointer to a &struct phy_device
703  * @page: desired page
704  *
705  * Take the MDIO bus lock to protect against concurrent access, save the
706  * current PHY page, and set the current page.  On error, returns a
707  * negative errno, otherwise returns the previous page number.
708  * phy_restore_page() must always be called after this, irrespective
709  * of success or failure of this call.
710  */
711 int phy_select_page(struct phy_device *phydev, int page)
712 {
713 	int ret, oldpage;
714 
715 	oldpage = ret = phy_save_page(phydev);
716 	if (ret < 0)
717 		return ret;
718 
719 	if (oldpage != page) {
720 		ret = __phy_write_page(phydev, page);
721 		if (ret < 0)
722 			return ret;
723 	}
724 
725 	return oldpage;
726 }
727 EXPORT_SYMBOL_GPL(phy_select_page);
728 
729 /**
730  * phy_restore_page() - restore the page register and release the bus lock
731  * @phydev: a pointer to a &struct phy_device
732  * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
733  * @ret: operation's return code
734  *
735  * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
736  * This function propagates the earliest error code from the group of
737  * operations.
738  *
739  * Returns:
740  *   @oldpage if it was a negative value, otherwise
741  *   @ret if it was a negative errno value, otherwise
742  *   phy_write_page()'s negative value if it were in error, otherwise
743  *   @ret.
744  */
745 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
746 {
747 	int r;
748 
749 	if (oldpage >= 0) {
750 		r = __phy_write_page(phydev, oldpage);
751 
752 		/* Propagate the operation return code if the page write
753 		 * was successful.
754 		 */
755 		if (ret >= 0 && r < 0)
756 			ret = r;
757 	} else {
758 		/* Propagate the phy page selection error code */
759 		ret = oldpage;
760 	}
761 
762 	phy_unlock_mdio_bus(phydev);
763 
764 	return ret;
765 }
766 EXPORT_SYMBOL_GPL(phy_restore_page);
767 
768 /**
769  * phy_read_paged() - Convenience function for reading a paged register
770  * @phydev: a pointer to a &struct phy_device
771  * @page: the page for the phy
772  * @regnum: register number
773  *
774  * Same rules as for phy_read().
775  */
776 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
777 {
778 	int ret = 0, oldpage;
779 
780 	oldpage = phy_select_page(phydev, page);
781 	if (oldpage >= 0)
782 		ret = __phy_read(phydev, regnum);
783 
784 	return phy_restore_page(phydev, oldpage, ret);
785 }
786 EXPORT_SYMBOL(phy_read_paged);
787 
788 /**
789  * phy_write_paged() - Convenience function for writing a paged register
790  * @phydev: a pointer to a &struct phy_device
791  * @page: the page for the phy
792  * @regnum: register number
793  * @val: value to write
794  *
795  * Same rules as for phy_write().
796  */
797 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
798 {
799 	int ret = 0, oldpage;
800 
801 	oldpage = phy_select_page(phydev, page);
802 	if (oldpage >= 0)
803 		ret = __phy_write(phydev, regnum, val);
804 
805 	return phy_restore_page(phydev, oldpage, ret);
806 }
807 EXPORT_SYMBOL(phy_write_paged);
808 
809 /**
810  * phy_modify_paged_changed() - Function for modifying a paged register
811  * @phydev: a pointer to a &struct phy_device
812  * @page: the page for the phy
813  * @regnum: register number
814  * @mask: bit mask of bits to clear
815  * @set: bit mask of bits to set
816  *
817  * Returns negative errno, 0 if there was no change, and 1 in case of change
818  */
819 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
820 			     u16 mask, u16 set)
821 {
822 	int ret = 0, oldpage;
823 
824 	oldpage = phy_select_page(phydev, page);
825 	if (oldpage >= 0)
826 		ret = __phy_modify_changed(phydev, regnum, mask, set);
827 
828 	return phy_restore_page(phydev, oldpage, ret);
829 }
830 EXPORT_SYMBOL(phy_modify_paged_changed);
831 
832 /**
833  * phy_modify_paged() - Convenience function for modifying a paged register
834  * @phydev: a pointer to a &struct phy_device
835  * @page: the page for the phy
836  * @regnum: register number
837  * @mask: bit mask of bits to clear
838  * @set: bit mask of bits to set
839  *
840  * Same rules as for phy_read() and phy_write().
841  */
842 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
843 		     u16 mask, u16 set)
844 {
845 	int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
846 
847 	return ret < 0 ? ret : 0;
848 }
849 EXPORT_SYMBOL(phy_modify_paged);
850