xref: /linux/drivers/net/phy/phy_device.c (revision f4ae8420f6ebdabf97e0c4f5f99412768687985f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for finding and configuring PHYs.
3  * Also contains generic PHY driver
4  *
5  * Author: Andy Fleming
6  *
7  * Copyright (c) 2004 Freescale Semiconductor, Inc.
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/acpi.h>
13 #include <linux/bitmap.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mdio.h>
24 #include <linux/mii.h>
25 #include <linux/mm.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/netdevice.h>
29 #include <linux/phy.h>
30 #include <linux/phylib_stubs.h>
31 #include <linux/phy_led_triggers.h>
32 #include <linux/pse-pd/pse.h>
33 #include <linux/property.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/sfp.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/uaccess.h>
40 #include <linux/unistd.h>
41 
42 MODULE_DESCRIPTION("PHY library");
43 MODULE_AUTHOR("Andy Fleming");
44 MODULE_LICENSE("GPL");
45 
46 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
47 EXPORT_SYMBOL_GPL(phy_basic_features);
48 
49 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
50 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
51 
52 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init;
53 EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features);
54 
55 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
56 EXPORT_SYMBOL_GPL(phy_gbit_features);
57 
58 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
59 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
60 
61 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
62 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
63 
64 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
65 EXPORT_SYMBOL_GPL(phy_10gbit_features);
66 
67 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
68 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
69 
70 const int phy_basic_ports_array[3] = {
71 	ETHTOOL_LINK_MODE_Autoneg_BIT,
72 	ETHTOOL_LINK_MODE_TP_BIT,
73 	ETHTOOL_LINK_MODE_MII_BIT,
74 };
75 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
76 
77 const int phy_fibre_port_array[1] = {
78 	ETHTOOL_LINK_MODE_FIBRE_BIT,
79 };
80 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
81 
82 const int phy_all_ports_features_array[7] = {
83 	ETHTOOL_LINK_MODE_Autoneg_BIT,
84 	ETHTOOL_LINK_MODE_TP_BIT,
85 	ETHTOOL_LINK_MODE_MII_BIT,
86 	ETHTOOL_LINK_MODE_FIBRE_BIT,
87 	ETHTOOL_LINK_MODE_AUI_BIT,
88 	ETHTOOL_LINK_MODE_BNC_BIT,
89 	ETHTOOL_LINK_MODE_Backplane_BIT,
90 };
91 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
92 
93 const int phy_10_100_features_array[4] = {
94 	ETHTOOL_LINK_MODE_10baseT_Half_BIT,
95 	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
96 	ETHTOOL_LINK_MODE_100baseT_Half_BIT,
97 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
98 };
99 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
100 
101 const int phy_basic_t1_features_array[3] = {
102 	ETHTOOL_LINK_MODE_TP_BIT,
103 	ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
104 	ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
105 };
106 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
107 
108 const int phy_basic_t1s_p2mp_features_array[2] = {
109 	ETHTOOL_LINK_MODE_TP_BIT,
110 	ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
111 };
112 EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features_array);
113 
114 const int phy_gbit_features_array[2] = {
115 	ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
116 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
117 };
118 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
119 
120 const int phy_10gbit_features_array[1] = {
121 	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
122 };
123 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
124 
125 static const int phy_10gbit_fec_features_array[1] = {
126 	ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
127 };
128 
129 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
130 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
131 
132 static const int phy_10gbit_full_features_array[] = {
133 	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
134 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
135 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
136 	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
137 };
138 
139 static const int phy_eee_cap1_features_array[] = {
140 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
141 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
142 	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
143 	ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
144 	ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
145 	ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
146 };
147 
148 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init;
149 EXPORT_SYMBOL_GPL(phy_eee_cap1_features);
150 
151 static const int phy_eee_cap2_features_array[] = {
152 	ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
153 	ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
154 };
155 
156 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init;
157 EXPORT_SYMBOL_GPL(phy_eee_cap2_features);
158 
159 static void features_init(void)
160 {
161 	/* 10/100 half/full*/
162 	linkmode_set_bit_array(phy_basic_ports_array,
163 			       ARRAY_SIZE(phy_basic_ports_array),
164 			       phy_basic_features);
165 	linkmode_set_bit_array(phy_10_100_features_array,
166 			       ARRAY_SIZE(phy_10_100_features_array),
167 			       phy_basic_features);
168 
169 	/* 100 full, TP */
170 	linkmode_set_bit_array(phy_basic_t1_features_array,
171 			       ARRAY_SIZE(phy_basic_t1_features_array),
172 			       phy_basic_t1_features);
173 
174 	/* 10 half, P2MP, TP */
175 	linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array,
176 			       ARRAY_SIZE(phy_basic_t1s_p2mp_features_array),
177 			       phy_basic_t1s_p2mp_features);
178 
179 	/* 10/100 half/full + 1000 half/full */
180 	linkmode_set_bit_array(phy_basic_ports_array,
181 			       ARRAY_SIZE(phy_basic_ports_array),
182 			       phy_gbit_features);
183 	linkmode_set_bit_array(phy_10_100_features_array,
184 			       ARRAY_SIZE(phy_10_100_features_array),
185 			       phy_gbit_features);
186 	linkmode_set_bit_array(phy_gbit_features_array,
187 			       ARRAY_SIZE(phy_gbit_features_array),
188 			       phy_gbit_features);
189 
190 	/* 10/100 half/full + 1000 half/full + fibre*/
191 	linkmode_set_bit_array(phy_basic_ports_array,
192 			       ARRAY_SIZE(phy_basic_ports_array),
193 			       phy_gbit_fibre_features);
194 	linkmode_set_bit_array(phy_10_100_features_array,
195 			       ARRAY_SIZE(phy_10_100_features_array),
196 			       phy_gbit_fibre_features);
197 	linkmode_set_bit_array(phy_gbit_features_array,
198 			       ARRAY_SIZE(phy_gbit_features_array),
199 			       phy_gbit_fibre_features);
200 	linkmode_set_bit_array(phy_fibre_port_array,
201 			       ARRAY_SIZE(phy_fibre_port_array),
202 			       phy_gbit_fibre_features);
203 
204 	/* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
205 	linkmode_set_bit_array(phy_all_ports_features_array,
206 			       ARRAY_SIZE(phy_all_ports_features_array),
207 			       phy_gbit_all_ports_features);
208 	linkmode_set_bit_array(phy_10_100_features_array,
209 			       ARRAY_SIZE(phy_10_100_features_array),
210 			       phy_gbit_all_ports_features);
211 	linkmode_set_bit_array(phy_gbit_features_array,
212 			       ARRAY_SIZE(phy_gbit_features_array),
213 			       phy_gbit_all_ports_features);
214 
215 	/* 10/100 half/full + 1000 half/full + 10G full*/
216 	linkmode_set_bit_array(phy_all_ports_features_array,
217 			       ARRAY_SIZE(phy_all_ports_features_array),
218 			       phy_10gbit_features);
219 	linkmode_set_bit_array(phy_10_100_features_array,
220 			       ARRAY_SIZE(phy_10_100_features_array),
221 			       phy_10gbit_features);
222 	linkmode_set_bit_array(phy_gbit_features_array,
223 			       ARRAY_SIZE(phy_gbit_features_array),
224 			       phy_10gbit_features);
225 	linkmode_set_bit_array(phy_10gbit_features_array,
226 			       ARRAY_SIZE(phy_10gbit_features_array),
227 			       phy_10gbit_features);
228 
229 	/* 10/100/1000/10G full */
230 	linkmode_set_bit_array(phy_all_ports_features_array,
231 			       ARRAY_SIZE(phy_all_ports_features_array),
232 			       phy_10gbit_full_features);
233 	linkmode_set_bit_array(phy_10gbit_full_features_array,
234 			       ARRAY_SIZE(phy_10gbit_full_features_array),
235 			       phy_10gbit_full_features);
236 	/* 10G FEC only */
237 	linkmode_set_bit_array(phy_10gbit_fec_features_array,
238 			       ARRAY_SIZE(phy_10gbit_fec_features_array),
239 			       phy_10gbit_fec_features);
240 	linkmode_set_bit_array(phy_eee_cap1_features_array,
241 			       ARRAY_SIZE(phy_eee_cap1_features_array),
242 			       phy_eee_cap1_features);
243 	linkmode_set_bit_array(phy_eee_cap2_features_array,
244 			       ARRAY_SIZE(phy_eee_cap2_features_array),
245 			       phy_eee_cap2_features);
246 
247 }
248 
249 void phy_device_free(struct phy_device *phydev)
250 {
251 	put_device(&phydev->mdio.dev);
252 }
253 EXPORT_SYMBOL(phy_device_free);
254 
255 static void phy_mdio_device_free(struct mdio_device *mdiodev)
256 {
257 	struct phy_device *phydev;
258 
259 	phydev = container_of(mdiodev, struct phy_device, mdio);
260 	phy_device_free(phydev);
261 }
262 
263 static void phy_device_release(struct device *dev)
264 {
265 	fwnode_handle_put(dev->fwnode);
266 	kfree(to_phy_device(dev));
267 }
268 
269 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
270 {
271 	struct phy_device *phydev;
272 
273 	phydev = container_of(mdiodev, struct phy_device, mdio);
274 	phy_device_remove(phydev);
275 }
276 
277 static struct phy_driver genphy_driver;
278 
279 static LIST_HEAD(phy_fixup_list);
280 static DEFINE_MUTEX(phy_fixup_lock);
281 
282 static bool phy_drv_wol_enabled(struct phy_device *phydev)
283 {
284 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
285 
286 	phy_ethtool_get_wol(phydev, &wol);
287 
288 	return wol.wolopts != 0;
289 }
290 
291 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
292 {
293 	struct device_driver *drv = phydev->mdio.dev.driver;
294 	struct phy_driver *phydrv = to_phy_driver(drv);
295 	struct net_device *netdev = phydev->attached_dev;
296 
297 	if (!drv || !phydrv->suspend)
298 		return false;
299 
300 	/* If the PHY on the mido bus is not attached but has WOL enabled
301 	 * we cannot suspend the PHY.
302 	 */
303 	if (!netdev && phy_drv_wol_enabled(phydev))
304 		return false;
305 
306 	/* PHY not attached? May suspend if the PHY has not already been
307 	 * suspended as part of a prior call to phy_disconnect() ->
308 	 * phy_detach() -> phy_suspend() because the parent netdev might be the
309 	 * MDIO bus driver and clock gated at this point.
310 	 */
311 	if (!netdev)
312 		goto out;
313 
314 	if (netdev->ethtool->wol_enabled)
315 		return false;
316 
317 	/* As long as not all affected network drivers support the
318 	 * wol_enabled flag, let's check for hints that WoL is enabled.
319 	 * Don't suspend PHY if the attached netdev parent may wake up.
320 	 * The parent may point to a PCI device, as in tg3 driver.
321 	 */
322 	if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
323 		return false;
324 
325 	/* Also don't suspend PHY if the netdev itself may wakeup. This
326 	 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
327 	 * e.g. SoC devices.
328 	 */
329 	if (device_may_wakeup(&netdev->dev))
330 		return false;
331 
332 out:
333 	return !phydev->suspended;
334 }
335 
336 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
337 {
338 	struct phy_device *phydev = to_phy_device(dev);
339 
340 	if (phydev->mac_managed_pm)
341 		return 0;
342 
343 	/* Wakeup interrupts may occur during the system sleep transition when
344 	 * the PHY is inaccessible. Set flag to postpone handling until the PHY
345 	 * has resumed. Wait for concurrent interrupt handler to complete.
346 	 */
347 	if (phy_interrupt_is_valid(phydev)) {
348 		phydev->irq_suspended = 1;
349 		synchronize_irq(phydev->irq);
350 	}
351 
352 	/* We must stop the state machine manually, otherwise it stops out of
353 	 * control, possibly with the phydev->lock held. Upon resume, netdev
354 	 * may call phy routines that try to grab the same lock, and that may
355 	 * lead to a deadlock.
356 	 */
357 	if (phydev->attached_dev && phydev->adjust_link)
358 		phy_stop_machine(phydev);
359 
360 	if (!mdio_bus_phy_may_suspend(phydev))
361 		return 0;
362 
363 	phydev->suspended_by_mdio_bus = 1;
364 
365 	return phy_suspend(phydev);
366 }
367 
368 static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
369 {
370 	struct phy_device *phydev = to_phy_device(dev);
371 	int ret;
372 
373 	if (phydev->mac_managed_pm)
374 		return 0;
375 
376 	if (!phydev->suspended_by_mdio_bus)
377 		goto no_resume;
378 
379 	phydev->suspended_by_mdio_bus = 0;
380 
381 	/* If we managed to get here with the PHY state machine in a state
382 	 * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication
383 	 * that something went wrong and we should most likely be using
384 	 * MAC managed PM, but we are not.
385 	 */
386 	WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY &&
387 		phydev->state != PHY_UP);
388 
389 	ret = phy_init_hw(phydev);
390 	if (ret < 0)
391 		return ret;
392 
393 	ret = phy_resume(phydev);
394 	if (ret < 0)
395 		return ret;
396 no_resume:
397 	if (phy_interrupt_is_valid(phydev)) {
398 		phydev->irq_suspended = 0;
399 		synchronize_irq(phydev->irq);
400 
401 		/* Rerun interrupts which were postponed by phy_interrupt()
402 		 * because they occurred during the system sleep transition.
403 		 */
404 		if (phydev->irq_rerun) {
405 			phydev->irq_rerun = 0;
406 			enable_irq(phydev->irq);
407 			irq_wake_thread(phydev->irq, phydev);
408 		}
409 	}
410 
411 	if (phydev->attached_dev && phydev->adjust_link)
412 		phy_start_machine(phydev);
413 
414 	return 0;
415 }
416 
417 static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
418 			 mdio_bus_phy_resume);
419 
420 /**
421  * phy_register_fixup - creates a new phy_fixup and adds it to the list
422  * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
423  * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
424  *	It can also be PHY_ANY_UID
425  * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
426  *	comparison
427  * @run: The actual code to be run when a matching PHY is found
428  */
429 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
430 		       int (*run)(struct phy_device *))
431 {
432 	struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
433 
434 	if (!fixup)
435 		return -ENOMEM;
436 
437 	strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
438 	fixup->phy_uid = phy_uid;
439 	fixup->phy_uid_mask = phy_uid_mask;
440 	fixup->run = run;
441 
442 	mutex_lock(&phy_fixup_lock);
443 	list_add_tail(&fixup->list, &phy_fixup_list);
444 	mutex_unlock(&phy_fixup_lock);
445 
446 	return 0;
447 }
448 EXPORT_SYMBOL(phy_register_fixup);
449 
450 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
451 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
452 			       int (*run)(struct phy_device *))
453 {
454 	return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
455 }
456 EXPORT_SYMBOL(phy_register_fixup_for_uid);
457 
458 /* Registers a fixup to be run on the PHY with id string bus_id */
459 int phy_register_fixup_for_id(const char *bus_id,
460 			      int (*run)(struct phy_device *))
461 {
462 	return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
463 }
464 EXPORT_SYMBOL(phy_register_fixup_for_id);
465 
466 /**
467  * phy_unregister_fixup - remove a phy_fixup from the list
468  * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
469  * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
470  * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
471  */
472 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
473 {
474 	struct list_head *pos, *n;
475 	struct phy_fixup *fixup;
476 	int ret;
477 
478 	ret = -ENODEV;
479 
480 	mutex_lock(&phy_fixup_lock);
481 	list_for_each_safe(pos, n, &phy_fixup_list) {
482 		fixup = list_entry(pos, struct phy_fixup, list);
483 
484 		if ((!strcmp(fixup->bus_id, bus_id)) &&
485 		    phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
486 			list_del(&fixup->list);
487 			kfree(fixup);
488 			ret = 0;
489 			break;
490 		}
491 	}
492 	mutex_unlock(&phy_fixup_lock);
493 
494 	return ret;
495 }
496 EXPORT_SYMBOL(phy_unregister_fixup);
497 
498 /* Unregisters a fixup of any PHY with the UID in phy_uid */
499 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
500 {
501 	return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
502 }
503 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
504 
505 /* Unregisters a fixup of the PHY with id string bus_id */
506 int phy_unregister_fixup_for_id(const char *bus_id)
507 {
508 	return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
509 }
510 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
511 
512 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
513  * Fixups can be set to match any in one or more fields.
514  */
515 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
516 {
517 	if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
518 		if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
519 			return 0;
520 
521 	if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
522 			    fixup->phy_uid_mask))
523 		if (fixup->phy_uid != PHY_ANY_UID)
524 			return 0;
525 
526 	return 1;
527 }
528 
529 /* Runs any matching fixups for this phydev */
530 static int phy_scan_fixups(struct phy_device *phydev)
531 {
532 	struct phy_fixup *fixup;
533 
534 	mutex_lock(&phy_fixup_lock);
535 	list_for_each_entry(fixup, &phy_fixup_list, list) {
536 		if (phy_needs_fixup(phydev, fixup)) {
537 			int err = fixup->run(phydev);
538 
539 			if (err < 0) {
540 				mutex_unlock(&phy_fixup_lock);
541 				return err;
542 			}
543 			phydev->has_fixups = true;
544 		}
545 	}
546 	mutex_unlock(&phy_fixup_lock);
547 
548 	return 0;
549 }
550 
551 static int phy_bus_match(struct device *dev, const struct device_driver *drv)
552 {
553 	struct phy_device *phydev = to_phy_device(dev);
554 	const struct phy_driver *phydrv = to_phy_driver(drv);
555 	const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
556 	int i;
557 
558 	if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
559 		return 0;
560 
561 	if (phydrv->match_phy_device)
562 		return phydrv->match_phy_device(phydev);
563 
564 	if (phydev->is_c45) {
565 		for (i = 1; i < num_ids; i++) {
566 			if (phydev->c45_ids.device_ids[i] == 0xffffffff)
567 				continue;
568 
569 			if (phy_id_compare(phydev->c45_ids.device_ids[i],
570 					   phydrv->phy_id, phydrv->phy_id_mask))
571 				return 1;
572 		}
573 		return 0;
574 	} else {
575 		return phy_id_compare(phydev->phy_id, phydrv->phy_id,
576 				      phydrv->phy_id_mask);
577 	}
578 }
579 
580 static ssize_t
581 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
582 {
583 	struct phy_device *phydev = to_phy_device(dev);
584 
585 	return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
586 }
587 static DEVICE_ATTR_RO(phy_id);
588 
589 static ssize_t
590 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
591 {
592 	struct phy_device *phydev = to_phy_device(dev);
593 	const char *mode = NULL;
594 
595 	if (phy_is_internal(phydev))
596 		mode = "internal";
597 	else
598 		mode = phy_modes(phydev->interface);
599 
600 	return sysfs_emit(buf, "%s\n", mode);
601 }
602 static DEVICE_ATTR_RO(phy_interface);
603 
604 static ssize_t
605 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
606 		    char *buf)
607 {
608 	struct phy_device *phydev = to_phy_device(dev);
609 
610 	return sysfs_emit(buf, "%d\n", phydev->has_fixups);
611 }
612 static DEVICE_ATTR_RO(phy_has_fixups);
613 
614 static ssize_t phy_dev_flags_show(struct device *dev,
615 				  struct device_attribute *attr,
616 				  char *buf)
617 {
618 	struct phy_device *phydev = to_phy_device(dev);
619 
620 	return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags);
621 }
622 static DEVICE_ATTR_RO(phy_dev_flags);
623 
624 static struct attribute *phy_dev_attrs[] = {
625 	&dev_attr_phy_id.attr,
626 	&dev_attr_phy_interface.attr,
627 	&dev_attr_phy_has_fixups.attr,
628 	&dev_attr_phy_dev_flags.attr,
629 	NULL,
630 };
631 ATTRIBUTE_GROUPS(phy_dev);
632 
633 static const struct device_type mdio_bus_phy_type = {
634 	.name = "PHY",
635 	.groups = phy_dev_groups,
636 	.release = phy_device_release,
637 	.pm = pm_ptr(&mdio_bus_phy_pm_ops),
638 };
639 
640 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
641 {
642 	int ret;
643 
644 	ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
645 			     MDIO_ID_ARGS(phy_id));
646 	/* We only check for failures in executing the usermode binary,
647 	 * not whether a PHY driver module exists for the PHY ID.
648 	 * Accept -ENOENT because this may occur in case no initramfs exists,
649 	 * then modprobe isn't available.
650 	 */
651 	if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
652 		phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
653 			   ret, (unsigned long)phy_id);
654 		return ret;
655 	}
656 
657 	return 0;
658 }
659 
660 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
661 				     bool is_c45,
662 				     struct phy_c45_device_ids *c45_ids)
663 {
664 	struct phy_device *dev;
665 	struct mdio_device *mdiodev;
666 	int ret = 0;
667 
668 	/* We allocate the device, and initialize the default values */
669 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
670 	if (!dev)
671 		return ERR_PTR(-ENOMEM);
672 
673 	mdiodev = &dev->mdio;
674 	mdiodev->dev.parent = &bus->dev;
675 	mdiodev->dev.bus = &mdio_bus_type;
676 	mdiodev->dev.type = &mdio_bus_phy_type;
677 	mdiodev->bus = bus;
678 	mdiodev->bus_match = phy_bus_match;
679 	mdiodev->addr = addr;
680 	mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
681 	mdiodev->device_free = phy_mdio_device_free;
682 	mdiodev->device_remove = phy_mdio_device_remove;
683 	mdiodev->reset_state = -1;
684 
685 	dev->speed = SPEED_UNKNOWN;
686 	dev->duplex = DUPLEX_UNKNOWN;
687 	dev->pause = 0;
688 	dev->asym_pause = 0;
689 	dev->link = 0;
690 	dev->port = PORT_TP;
691 	dev->interface = PHY_INTERFACE_MODE_GMII;
692 
693 	dev->autoneg = AUTONEG_ENABLE;
694 
695 	dev->pma_extable = -ENODATA;
696 	dev->is_c45 = is_c45;
697 	dev->phy_id = phy_id;
698 	if (c45_ids)
699 		dev->c45_ids = *c45_ids;
700 	dev->irq = bus->irq[addr];
701 
702 	dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
703 	device_initialize(&mdiodev->dev);
704 
705 	dev->state = PHY_DOWN;
706 	INIT_LIST_HEAD(&dev->leds);
707 
708 	mutex_init(&dev->lock);
709 	INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
710 
711 	/* Request the appropriate module unconditionally; don't
712 	 * bother trying to do so only if it isn't already loaded,
713 	 * because that gets complicated. A hotplug event would have
714 	 * done an unconditional modprobe anyway.
715 	 * We don't do normal hotplug because it won't work for MDIO
716 	 * -- because it relies on the device staying around for long
717 	 * enough for the driver to get loaded. With MDIO, the NIC
718 	 * driver will get bored and give up as soon as it finds that
719 	 * there's no driver _already_ loaded.
720 	 */
721 	if (is_c45 && c45_ids) {
722 		const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
723 		int i;
724 
725 		for (i = 1; i < num_ids; i++) {
726 			if (c45_ids->device_ids[i] == 0xffffffff)
727 				continue;
728 
729 			ret = phy_request_driver_module(dev,
730 						c45_ids->device_ids[i]);
731 			if (ret)
732 				break;
733 		}
734 	} else {
735 		ret = phy_request_driver_module(dev, phy_id);
736 	}
737 
738 	if (ret) {
739 		put_device(&mdiodev->dev);
740 		dev = ERR_PTR(ret);
741 	}
742 
743 	return dev;
744 }
745 EXPORT_SYMBOL(phy_device_create);
746 
747 /* phy_c45_probe_present - checks to see if a MMD is present in the package
748  * @bus: the target MII bus
749  * @prtad: PHY package address on the MII bus
750  * @devad: PHY device (MMD) address
751  *
752  * Read the MDIO_STAT2 register, and check whether a device is responding
753  * at this address.
754  *
755  * Returns: negative error number on bus access error, zero if no device
756  * is responding, or positive if a device is present.
757  */
758 static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
759 {
760 	int stat2;
761 
762 	stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
763 	if (stat2 < 0)
764 		return stat2;
765 
766 	return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
767 }
768 
769 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
770  * @bus: the target MII bus
771  * @addr: PHY address on the MII bus
772  * @dev_addr: MMD address in the PHY.
773  * @devices_in_package: where to store the devices in package information.
774  *
775  * Description: reads devices in package registers of a MMD at @dev_addr
776  * from PHY at @addr on @bus.
777  *
778  * Returns: 0 on success, -EIO on failure.
779  */
780 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
781 				   u32 *devices_in_package)
782 {
783 	int phy_reg;
784 
785 	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
786 	if (phy_reg < 0)
787 		return -EIO;
788 	*devices_in_package = phy_reg << 16;
789 
790 	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
791 	if (phy_reg < 0)
792 		return -EIO;
793 	*devices_in_package |= phy_reg;
794 
795 	return 0;
796 }
797 
798 /**
799  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
800  * @bus: the target MII bus
801  * @addr: PHY address on the MII bus
802  * @c45_ids: where to store the c45 ID information.
803  *
804  * Read the PHY "devices in package". If this appears to be valid, read
805  * the PHY identifiers for each device. Return the "devices in package"
806  * and identifiers in @c45_ids.
807  *
808  * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
809  * the "devices in package" is invalid or no device responds.
810  */
811 static int get_phy_c45_ids(struct mii_bus *bus, int addr,
812 			   struct phy_c45_device_ids *c45_ids)
813 {
814 	const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
815 	u32 devs_in_pkg = 0;
816 	int i, ret, phy_reg;
817 
818 	/* Find first non-zero Devices In package. Device zero is reserved
819 	 * for 802.3 c45 complied PHYs, so don't probe it at first.
820 	 */
821 	for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
822 	     (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
823 		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
824 			/* Check that there is a device present at this
825 			 * address before reading the devices-in-package
826 			 * register to avoid reading garbage from the PHY.
827 			 * Some PHYs (88x3310) vendor space is not IEEE802.3
828 			 * compliant.
829 			 */
830 			ret = phy_c45_probe_present(bus, addr, i);
831 			if (ret < 0)
832 				/* returning -ENODEV doesn't stop bus
833 				 * scanning
834 				 */
835 				return (phy_reg == -EIO ||
836 					phy_reg == -ENODEV) ? -ENODEV : -EIO;
837 
838 			if (!ret)
839 				continue;
840 		}
841 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
842 		if (phy_reg < 0)
843 			return -EIO;
844 	}
845 
846 	if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
847 		/* If mostly Fs, there is no device there, then let's probe
848 		 * MMD 0, as some 10G PHYs have zero Devices In package,
849 		 * e.g. Cortina CS4315/CS4340 PHY.
850 		 */
851 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
852 		if (phy_reg < 0)
853 			return -EIO;
854 
855 		/* no device there, let's get out of here */
856 		if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
857 			return -ENODEV;
858 	}
859 
860 	/* Now probe Device Identifiers for each device present. */
861 	for (i = 1; i < num_ids; i++) {
862 		if (!(devs_in_pkg & (1 << i)))
863 			continue;
864 
865 		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
866 			/* Probe the "Device Present" bits for the vendor MMDs
867 			 * to ignore these if they do not contain IEEE 802.3
868 			 * registers.
869 			 */
870 			ret = phy_c45_probe_present(bus, addr, i);
871 			if (ret < 0)
872 				return ret;
873 
874 			if (!ret)
875 				continue;
876 		}
877 
878 		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
879 		if (phy_reg < 0)
880 			return -EIO;
881 		c45_ids->device_ids[i] = phy_reg << 16;
882 
883 		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
884 		if (phy_reg < 0)
885 			return -EIO;
886 		c45_ids->device_ids[i] |= phy_reg;
887 	}
888 
889 	c45_ids->devices_in_package = devs_in_pkg;
890 	/* Bit 0 doesn't represent a device, it indicates c22 regs presence */
891 	c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
892 
893 	return 0;
894 }
895 
896 /**
897  * get_phy_c22_id - reads the specified addr for its clause 22 ID.
898  * @bus: the target MII bus
899  * @addr: PHY address on the MII bus
900  * @phy_id: where to store the ID retrieved.
901  *
902  * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
903  * placing it in @phy_id. Return zero on successful read and the ID is
904  * valid, %-EIO on bus access error, or %-ENODEV if no device responds
905  * or invalid ID.
906  */
907 static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
908 {
909 	int phy_reg;
910 
911 	/* Grab the bits from PHYIR1, and put them in the upper half */
912 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
913 	if (phy_reg < 0) {
914 		/* returning -ENODEV doesn't stop bus scanning */
915 		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
916 	}
917 
918 	*phy_id = phy_reg << 16;
919 
920 	/* Grab the bits from PHYIR2, and put them in the lower half */
921 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
922 	if (phy_reg < 0) {
923 		/* returning -ENODEV doesn't stop bus scanning */
924 		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
925 	}
926 
927 	*phy_id |= phy_reg;
928 
929 	/* If the phy_id is mostly Fs, there is no device there */
930 	if ((*phy_id & 0x1fffffff) == 0x1fffffff)
931 		return -ENODEV;
932 
933 	return 0;
934 }
935 
936 /* Extract the phy ID from the compatible string of the form
937  * ethernet-phy-idAAAA.BBBB.
938  */
939 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
940 {
941 	unsigned int upper, lower;
942 	const char *cp;
943 	int ret;
944 
945 	ret = fwnode_property_read_string(fwnode, "compatible", &cp);
946 	if (ret)
947 		return ret;
948 
949 	if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2)
950 		return -EINVAL;
951 
952 	*phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0));
953 	return 0;
954 }
955 EXPORT_SYMBOL(fwnode_get_phy_id);
956 
957 /**
958  * get_phy_device - reads the specified PHY device and returns its @phy_device
959  *		    struct
960  * @bus: the target MII bus
961  * @addr: PHY address on the MII bus
962  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
963  *
964  * Probe for a PHY at @addr on @bus.
965  *
966  * When probing for a clause 22 PHY, then read the ID registers. If we find
967  * a valid ID, allocate and return a &struct phy_device.
968  *
969  * When probing for a clause 45 PHY, read the "devices in package" registers.
970  * If the "devices in package" appears valid, read the ID registers for each
971  * MMD, allocate and return a &struct phy_device.
972  *
973  * Returns an allocated &struct phy_device on success, %-ENODEV if there is
974  * no PHY present, or %-EIO on bus access error.
975  */
976 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
977 {
978 	struct phy_c45_device_ids c45_ids;
979 	u32 phy_id = 0;
980 	int r;
981 
982 	c45_ids.devices_in_package = 0;
983 	c45_ids.mmds_present = 0;
984 	memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
985 
986 	if (is_c45)
987 		r = get_phy_c45_ids(bus, addr, &c45_ids);
988 	else
989 		r = get_phy_c22_id(bus, addr, &phy_id);
990 
991 	if (r)
992 		return ERR_PTR(r);
993 
994 	/* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID
995 	 * of 0 when probed using get_phy_c22_id() with no error. Proceed to
996 	 * probe with C45 to see if we're able to get a valid PHY ID in the C45
997 	 * space, if successful, create the C45 PHY device.
998 	 */
999 	if (!is_c45 && phy_id == 0 && bus->read_c45) {
1000 		r = get_phy_c45_ids(bus, addr, &c45_ids);
1001 		if (!r)
1002 			return phy_device_create(bus, addr, phy_id,
1003 						 true, &c45_ids);
1004 	}
1005 
1006 	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
1007 }
1008 EXPORT_SYMBOL(get_phy_device);
1009 
1010 /**
1011  * phy_device_register - Register the phy device on the MDIO bus
1012  * @phydev: phy_device structure to be added to the MDIO bus
1013  */
1014 int phy_device_register(struct phy_device *phydev)
1015 {
1016 	int err;
1017 
1018 	err = mdiobus_register_device(&phydev->mdio);
1019 	if (err)
1020 		return err;
1021 
1022 	/* Deassert the reset signal */
1023 	phy_device_reset(phydev, 0);
1024 
1025 	/* Run all of the fixups for this PHY */
1026 	err = phy_scan_fixups(phydev);
1027 	if (err) {
1028 		phydev_err(phydev, "failed to initialize\n");
1029 		goto out;
1030 	}
1031 
1032 	err = device_add(&phydev->mdio.dev);
1033 	if (err) {
1034 		phydev_err(phydev, "failed to add\n");
1035 		goto out;
1036 	}
1037 
1038 	return 0;
1039 
1040  out:
1041 	/* Assert the reset signal */
1042 	phy_device_reset(phydev, 1);
1043 
1044 	mdiobus_unregister_device(&phydev->mdio);
1045 	return err;
1046 }
1047 EXPORT_SYMBOL(phy_device_register);
1048 
1049 /**
1050  * phy_device_remove - Remove a previously registered phy device from the MDIO bus
1051  * @phydev: phy_device structure to remove
1052  *
1053  * This doesn't free the phy_device itself, it merely reverses the effects
1054  * of phy_device_register(). Use phy_device_free() to free the device
1055  * after calling this function.
1056  */
1057 void phy_device_remove(struct phy_device *phydev)
1058 {
1059 	unregister_mii_timestamper(phydev->mii_ts);
1060 	pse_control_put(phydev->psec);
1061 
1062 	device_del(&phydev->mdio.dev);
1063 
1064 	/* Assert the reset signal */
1065 	phy_device_reset(phydev, 1);
1066 
1067 	mdiobus_unregister_device(&phydev->mdio);
1068 }
1069 EXPORT_SYMBOL(phy_device_remove);
1070 
1071 /**
1072  * phy_get_c45_ids - Read 802.3-c45 IDs for phy device.
1073  * @phydev: phy_device structure to read 802.3-c45 IDs
1074  *
1075  * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
1076  * the "devices in package" is invalid.
1077  */
1078 int phy_get_c45_ids(struct phy_device *phydev)
1079 {
1080 	return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr,
1081 			       &phydev->c45_ids);
1082 }
1083 EXPORT_SYMBOL(phy_get_c45_ids);
1084 
1085 /**
1086  * phy_find_first - finds the first PHY device on the bus
1087  * @bus: the target MII bus
1088  */
1089 struct phy_device *phy_find_first(struct mii_bus *bus)
1090 {
1091 	struct phy_device *phydev;
1092 	int addr;
1093 
1094 	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
1095 		phydev = mdiobus_get_phy(bus, addr);
1096 		if (phydev)
1097 			return phydev;
1098 	}
1099 	return NULL;
1100 }
1101 EXPORT_SYMBOL(phy_find_first);
1102 
1103 static void phy_link_change(struct phy_device *phydev, bool up)
1104 {
1105 	struct net_device *netdev = phydev->attached_dev;
1106 
1107 	if (up)
1108 		netif_carrier_on(netdev);
1109 	else
1110 		netif_carrier_off(netdev);
1111 	phydev->adjust_link(netdev);
1112 	if (phydev->mii_ts && phydev->mii_ts->link_state)
1113 		phydev->mii_ts->link_state(phydev->mii_ts, phydev);
1114 }
1115 
1116 /**
1117  * phy_prepare_link - prepares the PHY layer to monitor link status
1118  * @phydev: target phy_device struct
1119  * @handler: callback function for link status change notifications
1120  *
1121  * Description: Tells the PHY infrastructure to handle the
1122  *   gory details on monitoring link status (whether through
1123  *   polling or an interrupt), and to call back to the
1124  *   connected device driver when the link status changes.
1125  *   If you want to monitor your own link state, don't call
1126  *   this function.
1127  */
1128 static void phy_prepare_link(struct phy_device *phydev,
1129 			     void (*handler)(struct net_device *))
1130 {
1131 	phydev->adjust_link = handler;
1132 }
1133 
1134 /**
1135  * phy_connect_direct - connect an ethernet device to a specific phy_device
1136  * @dev: the network device to connect
1137  * @phydev: the pointer to the phy device
1138  * @handler: callback function for state change notifications
1139  * @interface: PHY device's interface
1140  */
1141 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1142 		       void (*handler)(struct net_device *),
1143 		       phy_interface_t interface)
1144 {
1145 	int rc;
1146 
1147 	if (!dev)
1148 		return -EINVAL;
1149 
1150 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1151 	if (rc)
1152 		return rc;
1153 
1154 	phy_prepare_link(phydev, handler);
1155 	if (phy_interrupt_is_valid(phydev))
1156 		phy_request_interrupt(phydev);
1157 
1158 	return 0;
1159 }
1160 EXPORT_SYMBOL(phy_connect_direct);
1161 
1162 /**
1163  * phy_connect - connect an ethernet device to a PHY device
1164  * @dev: the network device to connect
1165  * @bus_id: the id string of the PHY device to connect
1166  * @handler: callback function for state change notifications
1167  * @interface: PHY device's interface
1168  *
1169  * Description: Convenience function for connecting ethernet
1170  *   devices to PHY devices.  The default behavior is for
1171  *   the PHY infrastructure to handle everything, and only notify
1172  *   the connected driver when the link status changes.  If you
1173  *   don't want, or can't use the provided functionality, you may
1174  *   choose to call only the subset of functions which provide
1175  *   the desired functionality.
1176  */
1177 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1178 			       void (*handler)(struct net_device *),
1179 			       phy_interface_t interface)
1180 {
1181 	struct phy_device *phydev;
1182 	struct device *d;
1183 	int rc;
1184 
1185 	/* Search the list of PHY devices on the mdio bus for the
1186 	 * PHY with the requested name
1187 	 */
1188 	d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1189 	if (!d) {
1190 		pr_err("PHY %s not found\n", bus_id);
1191 		return ERR_PTR(-ENODEV);
1192 	}
1193 	phydev = to_phy_device(d);
1194 
1195 	rc = phy_connect_direct(dev, phydev, handler, interface);
1196 	put_device(d);
1197 	if (rc)
1198 		return ERR_PTR(rc);
1199 
1200 	return phydev;
1201 }
1202 EXPORT_SYMBOL(phy_connect);
1203 
1204 /**
1205  * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1206  *		    device
1207  * @phydev: target phy_device struct
1208  */
1209 void phy_disconnect(struct phy_device *phydev)
1210 {
1211 	if (phy_is_started(phydev))
1212 		phy_stop(phydev);
1213 
1214 	if (phy_interrupt_is_valid(phydev))
1215 		phy_free_interrupt(phydev);
1216 
1217 	phydev->adjust_link = NULL;
1218 
1219 	phy_detach(phydev);
1220 }
1221 EXPORT_SYMBOL(phy_disconnect);
1222 
1223 /**
1224  * phy_poll_reset - Safely wait until a PHY reset has properly completed
1225  * @phydev: The PHY device to poll
1226  *
1227  * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1228  *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
1229  *   register must be polled until the BMCR_RESET bit clears.
1230  *
1231  *   Furthermore, any attempts to write to PHY registers may have no effect
1232  *   or even generate MDIO bus errors until this is complete.
1233  *
1234  *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1235  *   standard and do not fully reset after the BMCR_RESET bit is set, and may
1236  *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
1237  *   effort to support such broken PHYs, this function is separate from the
1238  *   standard phy_init_hw() which will zero all the other bits in the BMCR
1239  *   and reapply all driver-specific and board-specific fixups.
1240  */
1241 static int phy_poll_reset(struct phy_device *phydev)
1242 {
1243 	/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1244 	int ret, val;
1245 
1246 	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1247 				    50000, 600000, true);
1248 	if (ret)
1249 		return ret;
1250 	/* Some chips (smsc911x) may still need up to another 1ms after the
1251 	 * BMCR_RESET bit is cleared before they are usable.
1252 	 */
1253 	msleep(1);
1254 	return 0;
1255 }
1256 
1257 int phy_init_hw(struct phy_device *phydev)
1258 {
1259 	int ret = 0;
1260 
1261 	/* Deassert the reset signal */
1262 	phy_device_reset(phydev, 0);
1263 
1264 	if (!phydev->drv)
1265 		return 0;
1266 
1267 	if (phydev->drv->soft_reset) {
1268 		ret = phydev->drv->soft_reset(phydev);
1269 		if (ret < 0)
1270 			return ret;
1271 
1272 		/* see comment in genphy_soft_reset for an explanation */
1273 		phydev->suspended = 0;
1274 	}
1275 
1276 	ret = phy_scan_fixups(phydev);
1277 	if (ret < 0)
1278 		return ret;
1279 
1280 	phy_interface_zero(phydev->possible_interfaces);
1281 
1282 	if (phydev->drv->config_init) {
1283 		ret = phydev->drv->config_init(phydev);
1284 		if (ret < 0)
1285 			return ret;
1286 	}
1287 
1288 	if (phydev->drv->config_intr) {
1289 		ret = phydev->drv->config_intr(phydev);
1290 		if (ret < 0)
1291 			return ret;
1292 	}
1293 
1294 	return 0;
1295 }
1296 EXPORT_SYMBOL(phy_init_hw);
1297 
1298 void phy_attached_info(struct phy_device *phydev)
1299 {
1300 	phy_attached_print(phydev, NULL);
1301 }
1302 EXPORT_SYMBOL(phy_attached_info);
1303 
1304 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)"
1305 char *phy_attached_info_irq(struct phy_device *phydev)
1306 {
1307 	char *irq_str;
1308 	char irq_num[8];
1309 
1310 	switch(phydev->irq) {
1311 	case PHY_POLL:
1312 		irq_str = "POLL";
1313 		break;
1314 	case PHY_MAC_INTERRUPT:
1315 		irq_str = "MAC";
1316 		break;
1317 	default:
1318 		snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1319 		irq_str = irq_num;
1320 		break;
1321 	}
1322 
1323 	return kasprintf(GFP_KERNEL, "%s", irq_str);
1324 }
1325 EXPORT_SYMBOL(phy_attached_info_irq);
1326 
1327 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1328 {
1329 	const char *unbound = phydev->drv ? "" : "[unbound] ";
1330 	char *irq_str = phy_attached_info_irq(phydev);
1331 
1332 	if (!fmt) {
1333 		phydev_info(phydev, ATTACHED_FMT "\n", unbound,
1334 			    phydev_name(phydev), irq_str);
1335 	} else {
1336 		va_list ap;
1337 
1338 		phydev_info(phydev, ATTACHED_FMT, unbound,
1339 			    phydev_name(phydev), irq_str);
1340 
1341 		va_start(ap, fmt);
1342 		vprintk(fmt, ap);
1343 		va_end(ap);
1344 	}
1345 	kfree(irq_str);
1346 }
1347 EXPORT_SYMBOL(phy_attached_print);
1348 
1349 static void phy_sysfs_create_links(struct phy_device *phydev)
1350 {
1351 	struct net_device *dev = phydev->attached_dev;
1352 	int err;
1353 
1354 	if (!dev)
1355 		return;
1356 
1357 	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1358 				"attached_dev");
1359 	if (err)
1360 		return;
1361 
1362 	err = sysfs_create_link_nowarn(&dev->dev.kobj,
1363 				       &phydev->mdio.dev.kobj,
1364 				       "phydev");
1365 	if (err) {
1366 		dev_err(&dev->dev, "could not add device link to %s err %d\n",
1367 			kobject_name(&phydev->mdio.dev.kobj),
1368 			err);
1369 		/* non-fatal - some net drivers can use one netdevice
1370 		 * with more then one phy
1371 		 */
1372 	}
1373 
1374 	phydev->sysfs_links = true;
1375 }
1376 
1377 static ssize_t
1378 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1379 		    char *buf)
1380 {
1381 	struct phy_device *phydev = to_phy_device(dev);
1382 
1383 	return sysfs_emit(buf, "%d\n", !phydev->attached_dev);
1384 }
1385 static DEVICE_ATTR_RO(phy_standalone);
1386 
1387 /**
1388  * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1389  * @upstream: pointer to the phy device
1390  * @bus: sfp bus representing cage being attached
1391  *
1392  * This is used to fill in the sfp_upstream_ops .attach member.
1393  */
1394 void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1395 {
1396 	struct phy_device *phydev = upstream;
1397 
1398 	if (phydev->attached_dev)
1399 		phydev->attached_dev->sfp_bus = bus;
1400 	phydev->sfp_bus_attached = true;
1401 }
1402 EXPORT_SYMBOL(phy_sfp_attach);
1403 
1404 /**
1405  * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1406  * @upstream: pointer to the phy device
1407  * @bus: sfp bus representing cage being attached
1408  *
1409  * This is used to fill in the sfp_upstream_ops .detach member.
1410  */
1411 void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1412 {
1413 	struct phy_device *phydev = upstream;
1414 
1415 	if (phydev->attached_dev)
1416 		phydev->attached_dev->sfp_bus = NULL;
1417 	phydev->sfp_bus_attached = false;
1418 }
1419 EXPORT_SYMBOL(phy_sfp_detach);
1420 
1421 /**
1422  * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1423  * @phydev: Pointer to phy_device
1424  * @ops: SFP's upstream operations
1425  */
1426 int phy_sfp_probe(struct phy_device *phydev,
1427 		  const struct sfp_upstream_ops *ops)
1428 {
1429 	struct sfp_bus *bus;
1430 	int ret = 0;
1431 
1432 	if (phydev->mdio.dev.fwnode) {
1433 		bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1434 		if (IS_ERR(bus))
1435 			return PTR_ERR(bus);
1436 
1437 		phydev->sfp_bus = bus;
1438 
1439 		ret = sfp_bus_add_upstream(bus, phydev, ops);
1440 		sfp_bus_put(bus);
1441 	}
1442 	return ret;
1443 }
1444 EXPORT_SYMBOL(phy_sfp_probe);
1445 
1446 static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
1447 {
1448 	return phydrv->config_intr && phydrv->handle_interrupt;
1449 }
1450 
1451 /**
1452  * phy_attach_direct - attach a network device to a given PHY device pointer
1453  * @dev: network device to attach
1454  * @phydev: Pointer to phy_device to attach
1455  * @flags: PHY device's dev_flags
1456  * @interface: PHY device's interface
1457  *
1458  * Description: Called by drivers to attach to a particular PHY
1459  *     device. The phy_device is found, and properly hooked up
1460  *     to the phy_driver.  If no driver is attached, then a
1461  *     generic driver is used.  The phy_device is given a ptr to
1462  *     the attaching device, and given a callback for link status
1463  *     change.  The phy_device is returned to the attaching driver.
1464  *     This function takes a reference on the phy device.
1465  */
1466 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1467 		      u32 flags, phy_interface_t interface)
1468 {
1469 	struct mii_bus *bus = phydev->mdio.bus;
1470 	struct device *d = &phydev->mdio.dev;
1471 	struct module *ndev_owner = NULL;
1472 	bool using_genphy = false;
1473 	int err;
1474 
1475 	/* For Ethernet device drivers that register their own MDIO bus, we
1476 	 * will have bus->owner match ndev_mod, so we do not want to increment
1477 	 * our own module->refcnt here, otherwise we would not be able to
1478 	 * unload later on.
1479 	 */
1480 	if (dev)
1481 		ndev_owner = dev->dev.parent->driver->owner;
1482 	if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1483 		phydev_err(phydev, "failed to get the bus module\n");
1484 		return -EIO;
1485 	}
1486 
1487 	get_device(d);
1488 
1489 	/* Assume that if there is no driver, that it doesn't
1490 	 * exist, and we should use the genphy driver.
1491 	 */
1492 	if (!d->driver) {
1493 		if (phydev->is_c45)
1494 			d->driver = &genphy_c45_driver.mdiodrv.driver;
1495 		else
1496 			d->driver = &genphy_driver.mdiodrv.driver;
1497 
1498 		using_genphy = true;
1499 	}
1500 
1501 	if (!try_module_get(d->driver->owner)) {
1502 		phydev_err(phydev, "failed to get the device driver module\n");
1503 		err = -EIO;
1504 		goto error_put_device;
1505 	}
1506 
1507 	if (using_genphy) {
1508 		err = d->driver->probe(d);
1509 		if (err >= 0)
1510 			err = device_bind_driver(d);
1511 
1512 		if (err)
1513 			goto error_module_put;
1514 	}
1515 
1516 	if (phydev->attached_dev) {
1517 		dev_err(&dev->dev, "PHY already attached\n");
1518 		err = -EBUSY;
1519 		goto error;
1520 	}
1521 
1522 	phydev->phy_link_change = phy_link_change;
1523 	if (dev) {
1524 		phydev->attached_dev = dev;
1525 		dev->phydev = phydev;
1526 
1527 		if (phydev->sfp_bus_attached)
1528 			dev->sfp_bus = phydev->sfp_bus;
1529 	}
1530 
1531 	/* Some Ethernet drivers try to connect to a PHY device before
1532 	 * calling register_netdevice() -> netdev_register_kobject() and
1533 	 * does the dev->dev.kobj initialization. Here we only check for
1534 	 * success which indicates that the network device kobject is
1535 	 * ready. Once we do that we still need to keep track of whether
1536 	 * links were successfully set up or not for phy_detach() to
1537 	 * remove them accordingly.
1538 	 */
1539 	phydev->sysfs_links = false;
1540 
1541 	phy_sysfs_create_links(phydev);
1542 
1543 	if (!phydev->attached_dev) {
1544 		err = sysfs_create_file(&phydev->mdio.dev.kobj,
1545 					&dev_attr_phy_standalone.attr);
1546 		if (err)
1547 			phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1548 	}
1549 
1550 	phydev->dev_flags |= flags;
1551 
1552 	phydev->interface = interface;
1553 
1554 	phydev->state = PHY_READY;
1555 
1556 	phydev->interrupts = PHY_INTERRUPT_DISABLED;
1557 
1558 	/* PHYs can request to use poll mode even though they have an
1559 	 * associated interrupt line. This could be the case if they
1560 	 * detect a broken interrupt handling.
1561 	 */
1562 	if (phydev->dev_flags & PHY_F_NO_IRQ)
1563 		phydev->irq = PHY_POLL;
1564 
1565 	if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev))
1566 		phydev->irq = PHY_POLL;
1567 
1568 	/* Port is set to PORT_TP by default and the actual PHY driver will set
1569 	 * it to different value depending on the PHY configuration. If we have
1570 	 * the generic PHY driver we can't figure it out, thus set the old
1571 	 * legacy PORT_MII value.
1572 	 */
1573 	if (using_genphy)
1574 		phydev->port = PORT_MII;
1575 
1576 	/* Initial carrier state is off as the phy is about to be
1577 	 * (re)initialized.
1578 	 */
1579 	if (dev)
1580 		netif_carrier_off(phydev->attached_dev);
1581 
1582 	/* Do initial configuration here, now that
1583 	 * we have certain key parameters
1584 	 * (dev_flags and interface)
1585 	 */
1586 	err = phy_init_hw(phydev);
1587 	if (err)
1588 		goto error;
1589 
1590 	phy_resume(phydev);
1591 	if (!phydev->is_on_sfp_module)
1592 		phy_led_triggers_register(phydev);
1593 
1594 	/**
1595 	 * If the external phy used by current mac interface is managed by
1596 	 * another mac interface, so we should create a device link between
1597 	 * phy dev and mac dev.
1598 	 */
1599 	if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
1600 		phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
1601 						  DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
1602 
1603 	return err;
1604 
1605 error:
1606 	/* phy_detach() does all of the cleanup below */
1607 	phy_detach(phydev);
1608 	return err;
1609 
1610 error_module_put:
1611 	module_put(d->driver->owner);
1612 	d->driver = NULL;
1613 error_put_device:
1614 	put_device(d);
1615 	if (ndev_owner != bus->owner)
1616 		module_put(bus->owner);
1617 	return err;
1618 }
1619 EXPORT_SYMBOL(phy_attach_direct);
1620 
1621 /**
1622  * phy_attach - attach a network device to a particular PHY device
1623  * @dev: network device to attach
1624  * @bus_id: Bus ID of PHY device to attach
1625  * @interface: PHY device's interface
1626  *
1627  * Description: Same as phy_attach_direct() except that a PHY bus_id
1628  *     string is passed instead of a pointer to a struct phy_device.
1629  */
1630 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1631 			      phy_interface_t interface)
1632 {
1633 	struct phy_device *phydev;
1634 	struct device *d;
1635 	int rc;
1636 
1637 	if (!dev)
1638 		return ERR_PTR(-EINVAL);
1639 
1640 	/* Search the list of PHY devices on the mdio bus for the
1641 	 * PHY with the requested name
1642 	 */
1643 	d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1644 	if (!d) {
1645 		pr_err("PHY %s not found\n", bus_id);
1646 		return ERR_PTR(-ENODEV);
1647 	}
1648 	phydev = to_phy_device(d);
1649 
1650 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1651 	put_device(d);
1652 	if (rc)
1653 		return ERR_PTR(rc);
1654 
1655 	return phydev;
1656 }
1657 EXPORT_SYMBOL(phy_attach);
1658 
1659 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1660 				      struct device_driver *driver)
1661 {
1662 	struct device *d = &phydev->mdio.dev;
1663 	bool ret = false;
1664 
1665 	if (!phydev->drv)
1666 		return ret;
1667 
1668 	get_device(d);
1669 	ret = d->driver == driver;
1670 	put_device(d);
1671 
1672 	return ret;
1673 }
1674 
1675 bool phy_driver_is_genphy(struct phy_device *phydev)
1676 {
1677 	return phy_driver_is_genphy_kind(phydev,
1678 					 &genphy_driver.mdiodrv.driver);
1679 }
1680 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1681 
1682 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1683 {
1684 	return phy_driver_is_genphy_kind(phydev,
1685 					 &genphy_c45_driver.mdiodrv.driver);
1686 }
1687 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1688 
1689 /**
1690  * phy_package_join - join a common PHY group
1691  * @phydev: target phy_device struct
1692  * @base_addr: cookie and base PHY address of PHY package for offset
1693  *   calculation of global register access
1694  * @priv_size: if non-zero allocate this amount of bytes for private data
1695  *
1696  * This joins a PHY group and provides a shared storage for all phydevs in
1697  * this group. This is intended to be used for packages which contain
1698  * more than one PHY, for example a quad PHY transceiver.
1699  *
1700  * The base_addr parameter serves as cookie which has to have the same values
1701  * for all members of one group and as the base PHY address of the PHY package
1702  * for offset calculation to access generic registers of a PHY package.
1703  * Usually, one of the PHY addresses of the different PHYs in the package
1704  * provides access to these global registers.
1705  * The address which is given here, will be used in the phy_package_read()
1706  * and phy_package_write() convenience functions as base and added to the
1707  * passed offset in those functions.
1708  *
1709  * This will set the shared pointer of the phydev to the shared storage.
1710  * If this is the first call for a this cookie the shared storage will be
1711  * allocated. If priv_size is non-zero, the given amount of bytes are
1712  * allocated for the priv member.
1713  *
1714  * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
1715  * with the same cookie but a different priv_size is an error.
1716  */
1717 int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
1718 {
1719 	struct mii_bus *bus = phydev->mdio.bus;
1720 	struct phy_package_shared *shared;
1721 	int ret;
1722 
1723 	if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
1724 		return -EINVAL;
1725 
1726 	mutex_lock(&bus->shared_lock);
1727 	shared = bus->shared[base_addr];
1728 	if (!shared) {
1729 		ret = -ENOMEM;
1730 		shared = kzalloc(sizeof(*shared), GFP_KERNEL);
1731 		if (!shared)
1732 			goto err_unlock;
1733 		if (priv_size) {
1734 			shared->priv = kzalloc(priv_size, GFP_KERNEL);
1735 			if (!shared->priv)
1736 				goto err_free;
1737 			shared->priv_size = priv_size;
1738 		}
1739 		shared->base_addr = base_addr;
1740 		shared->np = NULL;
1741 		refcount_set(&shared->refcnt, 1);
1742 		bus->shared[base_addr] = shared;
1743 	} else {
1744 		ret = -EINVAL;
1745 		if (priv_size && priv_size != shared->priv_size)
1746 			goto err_unlock;
1747 		refcount_inc(&shared->refcnt);
1748 	}
1749 	mutex_unlock(&bus->shared_lock);
1750 
1751 	phydev->shared = shared;
1752 
1753 	return 0;
1754 
1755 err_free:
1756 	kfree(shared);
1757 err_unlock:
1758 	mutex_unlock(&bus->shared_lock);
1759 	return ret;
1760 }
1761 EXPORT_SYMBOL_GPL(phy_package_join);
1762 
1763 /**
1764  * of_phy_package_join - join a common PHY group in PHY package
1765  * @phydev: target phy_device struct
1766  * @priv_size: if non-zero allocate this amount of bytes for private data
1767  *
1768  * This is a variant of phy_package_join for PHY package defined in DT.
1769  *
1770  * The parent node of the @phydev is checked as a valid PHY package node
1771  * structure (by matching the node name "ethernet-phy-package") and the
1772  * base_addr for the PHY package is passed to phy_package_join.
1773  *
1774  * With this configuration the shared struct will also have the np value
1775  * filled to use additional DT defined properties in PHY specific
1776  * probe_once and config_init_once PHY package OPs.
1777  *
1778  * Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
1779  * with the same cookie but a different priv_size is an error. Or a parent
1780  * node is not detected or is not valid or doesn't match the expected node
1781  * name for PHY package.
1782  */
1783 int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
1784 {
1785 	struct device_node *node = phydev->mdio.dev.of_node;
1786 	struct device_node *package_node;
1787 	u32 base_addr;
1788 	int ret;
1789 
1790 	if (!node)
1791 		return -EINVAL;
1792 
1793 	package_node = of_get_parent(node);
1794 	if (!package_node)
1795 		return -EINVAL;
1796 
1797 	if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
1798 		ret = -EINVAL;
1799 		goto exit;
1800 	}
1801 
1802 	if (of_property_read_u32(package_node, "reg", &base_addr)) {
1803 		ret = -EINVAL;
1804 		goto exit;
1805 	}
1806 
1807 	ret = phy_package_join(phydev, base_addr, priv_size);
1808 	if (ret)
1809 		goto exit;
1810 
1811 	phydev->shared->np = package_node;
1812 
1813 	return 0;
1814 exit:
1815 	of_node_put(package_node);
1816 	return ret;
1817 }
1818 EXPORT_SYMBOL_GPL(of_phy_package_join);
1819 
1820 /**
1821  * phy_package_leave - leave a common PHY group
1822  * @phydev: target phy_device struct
1823  *
1824  * This leaves a PHY group created by phy_package_join(). If this phydev
1825  * was the last user of the shared data between the group, this data is
1826  * freed. Resets the phydev->shared pointer to NULL.
1827  */
1828 void phy_package_leave(struct phy_device *phydev)
1829 {
1830 	struct phy_package_shared *shared = phydev->shared;
1831 	struct mii_bus *bus = phydev->mdio.bus;
1832 
1833 	if (!shared)
1834 		return;
1835 
1836 	/* Decrease the node refcount on leave if present */
1837 	if (shared->np)
1838 		of_node_put(shared->np);
1839 
1840 	if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
1841 		bus->shared[shared->base_addr] = NULL;
1842 		mutex_unlock(&bus->shared_lock);
1843 		kfree(shared->priv);
1844 		kfree(shared);
1845 	}
1846 
1847 	phydev->shared = NULL;
1848 }
1849 EXPORT_SYMBOL_GPL(phy_package_leave);
1850 
1851 static void devm_phy_package_leave(struct device *dev, void *res)
1852 {
1853 	phy_package_leave(*(struct phy_device **)res);
1854 }
1855 
1856 /**
1857  * devm_phy_package_join - resource managed phy_package_join()
1858  * @dev: device that is registering this PHY package
1859  * @phydev: target phy_device struct
1860  * @base_addr: cookie and base PHY address of PHY package for offset
1861  *   calculation of global register access
1862  * @priv_size: if non-zero allocate this amount of bytes for private data
1863  *
1864  * Managed phy_package_join(). Shared storage fetched by this function,
1865  * phy_package_leave() is automatically called on driver detach. See
1866  * phy_package_join() for more information.
1867  */
1868 int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1869 			  int base_addr, size_t priv_size)
1870 {
1871 	struct phy_device **ptr;
1872 	int ret;
1873 
1874 	ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
1875 			   GFP_KERNEL);
1876 	if (!ptr)
1877 		return -ENOMEM;
1878 
1879 	ret = phy_package_join(phydev, base_addr, priv_size);
1880 
1881 	if (!ret) {
1882 		*ptr = phydev;
1883 		devres_add(dev, ptr);
1884 	} else {
1885 		devres_free(ptr);
1886 	}
1887 
1888 	return ret;
1889 }
1890 EXPORT_SYMBOL_GPL(devm_phy_package_join);
1891 
1892 /**
1893  * devm_of_phy_package_join - resource managed of_phy_package_join()
1894  * @dev: device that is registering this PHY package
1895  * @phydev: target phy_device struct
1896  * @priv_size: if non-zero allocate this amount of bytes for private data
1897  *
1898  * Managed of_phy_package_join(). Shared storage fetched by this function,
1899  * phy_package_leave() is automatically called on driver detach. See
1900  * of_phy_package_join() for more information.
1901  */
1902 int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
1903 			     size_t priv_size)
1904 {
1905 	struct phy_device **ptr;
1906 	int ret;
1907 
1908 	ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
1909 			   GFP_KERNEL);
1910 	if (!ptr)
1911 		return -ENOMEM;
1912 
1913 	ret = of_phy_package_join(phydev, priv_size);
1914 
1915 	if (!ret) {
1916 		*ptr = phydev;
1917 		devres_add(dev, ptr);
1918 	} else {
1919 		devres_free(ptr);
1920 	}
1921 
1922 	return ret;
1923 }
1924 EXPORT_SYMBOL_GPL(devm_of_phy_package_join);
1925 
1926 /**
1927  * phy_detach - detach a PHY device from its network device
1928  * @phydev: target phy_device struct
1929  *
1930  * This detaches the phy device from its network device and the phy
1931  * driver, and drops the reference count taken in phy_attach_direct().
1932  */
1933 void phy_detach(struct phy_device *phydev)
1934 {
1935 	struct net_device *dev = phydev->attached_dev;
1936 	struct module *ndev_owner = NULL;
1937 	struct mii_bus *bus;
1938 
1939 	if (phydev->devlink)
1940 		device_link_del(phydev->devlink);
1941 
1942 	if (phydev->sysfs_links) {
1943 		if (dev)
1944 			sysfs_remove_link(&dev->dev.kobj, "phydev");
1945 		sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1946 	}
1947 
1948 	if (!phydev->attached_dev)
1949 		sysfs_remove_file(&phydev->mdio.dev.kobj,
1950 				  &dev_attr_phy_standalone.attr);
1951 
1952 	phy_suspend(phydev);
1953 	if (dev) {
1954 		phydev->attached_dev->phydev = NULL;
1955 		phydev->attached_dev = NULL;
1956 	}
1957 	phydev->phylink = NULL;
1958 
1959 	if (!phydev->is_on_sfp_module)
1960 		phy_led_triggers_unregister(phydev);
1961 
1962 	if (phydev->mdio.dev.driver)
1963 		module_put(phydev->mdio.dev.driver->owner);
1964 
1965 	/* If the device had no specific driver before (i.e. - it
1966 	 * was using the generic driver), we unbind the device
1967 	 * from the generic driver so that there's a chance a
1968 	 * real driver could be loaded
1969 	 */
1970 	if (phy_driver_is_genphy(phydev) ||
1971 	    phy_driver_is_genphy_10g(phydev))
1972 		device_release_driver(&phydev->mdio.dev);
1973 
1974 	/* Assert the reset signal */
1975 	phy_device_reset(phydev, 1);
1976 
1977 	/*
1978 	 * The phydev might go away on the put_device() below, so avoid
1979 	 * a use-after-free bug by reading the underlying bus first.
1980 	 */
1981 	bus = phydev->mdio.bus;
1982 
1983 	put_device(&phydev->mdio.dev);
1984 	if (dev)
1985 		ndev_owner = dev->dev.parent->driver->owner;
1986 	if (ndev_owner != bus->owner)
1987 		module_put(bus->owner);
1988 }
1989 EXPORT_SYMBOL(phy_detach);
1990 
1991 int phy_suspend(struct phy_device *phydev)
1992 {
1993 	struct net_device *netdev = phydev->attached_dev;
1994 	const struct phy_driver *phydrv = phydev->drv;
1995 	int ret;
1996 
1997 	if (phydev->suspended || !phydrv)
1998 		return 0;
1999 
2000 	phydev->wol_enabled = phy_drv_wol_enabled(phydev) ||
2001 			      (netdev && netdev->ethtool->wol_enabled);
2002 	/* If the device has WOL enabled, we cannot suspend the PHY */
2003 	if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND))
2004 		return -EBUSY;
2005 
2006 	if (!phydrv->suspend)
2007 		return 0;
2008 
2009 	ret = phydrv->suspend(phydev);
2010 	if (!ret)
2011 		phydev->suspended = true;
2012 
2013 	return ret;
2014 }
2015 EXPORT_SYMBOL(phy_suspend);
2016 
2017 int __phy_resume(struct phy_device *phydev)
2018 {
2019 	const struct phy_driver *phydrv = phydev->drv;
2020 	int ret;
2021 
2022 	lockdep_assert_held(&phydev->lock);
2023 
2024 	if (!phydrv || !phydrv->resume)
2025 		return 0;
2026 
2027 	ret = phydrv->resume(phydev);
2028 	if (!ret)
2029 		phydev->suspended = false;
2030 
2031 	return ret;
2032 }
2033 EXPORT_SYMBOL(__phy_resume);
2034 
2035 int phy_resume(struct phy_device *phydev)
2036 {
2037 	int ret;
2038 
2039 	mutex_lock(&phydev->lock);
2040 	ret = __phy_resume(phydev);
2041 	mutex_unlock(&phydev->lock);
2042 
2043 	return ret;
2044 }
2045 EXPORT_SYMBOL(phy_resume);
2046 
2047 int phy_loopback(struct phy_device *phydev, bool enable)
2048 {
2049 	int ret = 0;
2050 
2051 	if (!phydev->drv)
2052 		return -EIO;
2053 
2054 	mutex_lock(&phydev->lock);
2055 
2056 	if (enable && phydev->loopback_enabled) {
2057 		ret = -EBUSY;
2058 		goto out;
2059 	}
2060 
2061 	if (!enable && !phydev->loopback_enabled) {
2062 		ret = -EINVAL;
2063 		goto out;
2064 	}
2065 
2066 	if (phydev->drv->set_loopback)
2067 		ret = phydev->drv->set_loopback(phydev, enable);
2068 	else
2069 		ret = genphy_loopback(phydev, enable);
2070 
2071 	if (ret)
2072 		goto out;
2073 
2074 	phydev->loopback_enabled = enable;
2075 
2076 out:
2077 	mutex_unlock(&phydev->lock);
2078 	return ret;
2079 }
2080 EXPORT_SYMBOL(phy_loopback);
2081 
2082 /**
2083  * phy_reset_after_clk_enable - perform a PHY reset if needed
2084  * @phydev: target phy_device struct
2085  *
2086  * Description: Some PHYs are known to need a reset after their refclk was
2087  *   enabled. This function evaluates the flags and perform the reset if it's
2088  *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
2089  *   was reset.
2090  */
2091 int phy_reset_after_clk_enable(struct phy_device *phydev)
2092 {
2093 	if (!phydev || !phydev->drv)
2094 		return -ENODEV;
2095 
2096 	if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
2097 		phy_device_reset(phydev, 1);
2098 		phy_device_reset(phydev, 0);
2099 		return 1;
2100 	}
2101 
2102 	return 0;
2103 }
2104 EXPORT_SYMBOL(phy_reset_after_clk_enable);
2105 
2106 /* Generic PHY support and helper functions */
2107 
2108 /**
2109  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
2110  * @phydev: target phy_device struct
2111  * @advert: auto-negotiation parameters to advertise
2112  *
2113  * Description: Writes MII_ADVERTISE with the appropriate values,
2114  *   after sanitizing the values to make sure we only advertise
2115  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
2116  *   hasn't changed, and > 0 if it has changed.
2117  */
2118 static int genphy_config_advert(struct phy_device *phydev,
2119 				const unsigned long *advert)
2120 {
2121 	int err, bmsr, changed = 0;
2122 	u32 adv;
2123 
2124 	adv = linkmode_adv_to_mii_adv_t(advert);
2125 
2126 	/* Setup standard advertisement */
2127 	err = phy_modify_changed(phydev, MII_ADVERTISE,
2128 				 ADVERTISE_ALL | ADVERTISE_100BASE4 |
2129 				 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
2130 				 adv);
2131 	if (err < 0)
2132 		return err;
2133 	if (err > 0)
2134 		changed = 1;
2135 
2136 	bmsr = phy_read(phydev, MII_BMSR);
2137 	if (bmsr < 0)
2138 		return bmsr;
2139 
2140 	/* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
2141 	 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
2142 	 * logical 1.
2143 	 */
2144 	if (!(bmsr & BMSR_ESTATEN))
2145 		return changed;
2146 
2147 	adv = linkmode_adv_to_mii_ctrl1000_t(advert);
2148 
2149 	err = phy_modify_changed(phydev, MII_CTRL1000,
2150 				 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
2151 				 adv);
2152 	if (err < 0)
2153 		return err;
2154 	if (err > 0)
2155 		changed = 1;
2156 
2157 	return changed;
2158 }
2159 
2160 /**
2161  * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
2162  * @phydev: target phy_device struct
2163  *
2164  * Description: Writes MII_ADVERTISE with the appropriate values,
2165  *   after sanitizing the values to make sure we only advertise
2166  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
2167  *   hasn't changed, and > 0 if it has changed. This function is intended
2168  *   for Clause 37 1000Base-X mode.
2169  */
2170 static int genphy_c37_config_advert(struct phy_device *phydev)
2171 {
2172 	u16 adv = 0;
2173 
2174 	/* Only allow advertising what this PHY supports */
2175 	linkmode_and(phydev->advertising, phydev->advertising,
2176 		     phydev->supported);
2177 
2178 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2179 			      phydev->advertising))
2180 		adv |= ADVERTISE_1000XFULL;
2181 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2182 			      phydev->advertising))
2183 		adv |= ADVERTISE_1000XPAUSE;
2184 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2185 			      phydev->advertising))
2186 		adv |= ADVERTISE_1000XPSE_ASYM;
2187 
2188 	return phy_modify_changed(phydev, MII_ADVERTISE,
2189 				  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
2190 				  ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
2191 				  adv);
2192 }
2193 
2194 /**
2195  * genphy_config_eee_advert - disable unwanted eee mode advertisement
2196  * @phydev: target phy_device struct
2197  *
2198  * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
2199  *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
2200  *   changed, and 1 if it has changed.
2201  */
2202 int genphy_config_eee_advert(struct phy_device *phydev)
2203 {
2204 	int err;
2205 
2206 	/* Nothing to disable */
2207 	if (!phydev->eee_broken_modes)
2208 		return 0;
2209 
2210 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
2211 				     phydev->eee_broken_modes, 0);
2212 	/* If the call failed, we assume that EEE is not supported */
2213 	return err < 0 ? 0 : err;
2214 }
2215 EXPORT_SYMBOL(genphy_config_eee_advert);
2216 
2217 /**
2218  * genphy_setup_forced - configures/forces speed/duplex from @phydev
2219  * @phydev: target phy_device struct
2220  *
2221  * Description: Configures MII_BMCR to force speed/duplex
2222  *   to the values in phydev. Assumes that the values are valid.
2223  *   Please see phy_sanitize_settings().
2224  */
2225 int genphy_setup_forced(struct phy_device *phydev)
2226 {
2227 	u16 ctl;
2228 
2229 	phydev->pause = 0;
2230 	phydev->asym_pause = 0;
2231 
2232 	ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2233 
2234 	return phy_modify(phydev, MII_BMCR,
2235 			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
2236 }
2237 EXPORT_SYMBOL(genphy_setup_forced);
2238 
2239 static int genphy_setup_master_slave(struct phy_device *phydev)
2240 {
2241 	u16 ctl = 0;
2242 
2243 	if (!phydev->is_gigabit_capable)
2244 		return 0;
2245 
2246 	switch (phydev->master_slave_set) {
2247 	case MASTER_SLAVE_CFG_MASTER_PREFERRED:
2248 		ctl |= CTL1000_PREFER_MASTER;
2249 		break;
2250 	case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2251 		break;
2252 	case MASTER_SLAVE_CFG_MASTER_FORCE:
2253 		ctl |= CTL1000_AS_MASTER;
2254 		fallthrough;
2255 	case MASTER_SLAVE_CFG_SLAVE_FORCE:
2256 		ctl |= CTL1000_ENABLE_MASTER;
2257 		break;
2258 	case MASTER_SLAVE_CFG_UNKNOWN:
2259 	case MASTER_SLAVE_CFG_UNSUPPORTED:
2260 		return 0;
2261 	default:
2262 		phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2263 		return -EOPNOTSUPP;
2264 	}
2265 
2266 	return phy_modify_changed(phydev, MII_CTRL1000,
2267 				  (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2268 				   CTL1000_PREFER_MASTER), ctl);
2269 }
2270 
2271 int genphy_read_master_slave(struct phy_device *phydev)
2272 {
2273 	int cfg, state;
2274 	int val;
2275 
2276 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2277 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2278 
2279 	val = phy_read(phydev, MII_CTRL1000);
2280 	if (val < 0)
2281 		return val;
2282 
2283 	if (val & CTL1000_ENABLE_MASTER) {
2284 		if (val & CTL1000_AS_MASTER)
2285 			cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2286 		else
2287 			cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2288 	} else {
2289 		if (val & CTL1000_PREFER_MASTER)
2290 			cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2291 		else
2292 			cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2293 	}
2294 
2295 	val = phy_read(phydev, MII_STAT1000);
2296 	if (val < 0)
2297 		return val;
2298 
2299 	if (val & LPA_1000MSFAIL) {
2300 		state = MASTER_SLAVE_STATE_ERR;
2301 	} else if (phydev->link) {
2302 		/* this bits are valid only for active link */
2303 		if (val & LPA_1000MSRES)
2304 			state = MASTER_SLAVE_STATE_MASTER;
2305 		else
2306 			state = MASTER_SLAVE_STATE_SLAVE;
2307 	} else {
2308 		state = MASTER_SLAVE_STATE_UNKNOWN;
2309 	}
2310 
2311 	phydev->master_slave_get = cfg;
2312 	phydev->master_slave_state = state;
2313 
2314 	return 0;
2315 }
2316 EXPORT_SYMBOL(genphy_read_master_slave);
2317 
2318 /**
2319  * genphy_restart_aneg - Enable and Restart Autonegotiation
2320  * @phydev: target phy_device struct
2321  */
2322 int genphy_restart_aneg(struct phy_device *phydev)
2323 {
2324 	/* Don't isolate the PHY if we're negotiating */
2325 	return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2326 			  BMCR_ANENABLE | BMCR_ANRESTART);
2327 }
2328 EXPORT_SYMBOL(genphy_restart_aneg);
2329 
2330 /**
2331  * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2332  * @phydev: target phy_device struct
2333  * @restart: whether aneg restart is requested
2334  *
2335  * Check, and restart auto-negotiation if needed.
2336  */
2337 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2338 {
2339 	int ret;
2340 
2341 	if (!restart) {
2342 		/* Advertisement hasn't changed, but maybe aneg was never on to
2343 		 * begin with?  Or maybe phy was isolated?
2344 		 */
2345 		ret = phy_read(phydev, MII_BMCR);
2346 		if (ret < 0)
2347 			return ret;
2348 
2349 		if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2350 			restart = true;
2351 	}
2352 
2353 	if (restart)
2354 		return genphy_restart_aneg(phydev);
2355 
2356 	return 0;
2357 }
2358 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2359 
2360 /**
2361  * __genphy_config_aneg - restart auto-negotiation or write BMCR
2362  * @phydev: target phy_device struct
2363  * @changed: whether autoneg is requested
2364  *
2365  * Description: If auto-negotiation is enabled, we configure the
2366  *   advertising, and then restart auto-negotiation.  If it is not
2367  *   enabled, then we write the BMCR.
2368  */
2369 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2370 {
2371 	__ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert);
2372 	const struct phy_setting *set;
2373 	unsigned long *advert;
2374 	int err;
2375 
2376 	err = genphy_c45_an_config_eee_aneg(phydev);
2377 	if (err < 0)
2378 		return err;
2379 	else if (err)
2380 		changed = true;
2381 
2382 	err = genphy_setup_master_slave(phydev);
2383 	if (err < 0)
2384 		return err;
2385 	else if (err)
2386 		changed = true;
2387 
2388 	if (phydev->autoneg == AUTONEG_ENABLE) {
2389 		/* Only allow advertising what this PHY supports */
2390 		linkmode_and(phydev->advertising, phydev->advertising,
2391 			     phydev->supported);
2392 		advert = phydev->advertising;
2393 	} else if (phydev->speed < SPEED_1000) {
2394 		return genphy_setup_forced(phydev);
2395 	} else {
2396 		linkmode_zero(fixed_advert);
2397 
2398 		set = phy_lookup_setting(phydev->speed, phydev->duplex,
2399 					 phydev->supported, true);
2400 		if (set)
2401 			linkmode_set_bit(set->bit, fixed_advert);
2402 
2403 		advert = fixed_advert;
2404 	}
2405 
2406 	err = genphy_config_advert(phydev, advert);
2407 	if (err < 0) /* error */
2408 		return err;
2409 	else if (err)
2410 		changed = true;
2411 
2412 	return genphy_check_and_restart_aneg(phydev, changed);
2413 }
2414 EXPORT_SYMBOL(__genphy_config_aneg);
2415 
2416 /**
2417  * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2418  * @phydev: target phy_device struct
2419  *
2420  * Description: If auto-negotiation is enabled, we configure the
2421  *   advertising, and then restart auto-negotiation.  If it is not
2422  *   enabled, then we write the BMCR. This function is intended
2423  *   for use with Clause 37 1000Base-X mode.
2424  */
2425 int genphy_c37_config_aneg(struct phy_device *phydev)
2426 {
2427 	int err, changed;
2428 
2429 	if (phydev->autoneg != AUTONEG_ENABLE)
2430 		return genphy_setup_forced(phydev);
2431 
2432 	err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2433 			 BMCR_SPEED1000);
2434 	if (err)
2435 		return err;
2436 
2437 	changed = genphy_c37_config_advert(phydev);
2438 	if (changed < 0) /* error */
2439 		return changed;
2440 
2441 	if (!changed) {
2442 		/* Advertisement hasn't changed, but maybe aneg was never on to
2443 		 * begin with?  Or maybe phy was isolated?
2444 		 */
2445 		int ctl = phy_read(phydev, MII_BMCR);
2446 
2447 		if (ctl < 0)
2448 			return ctl;
2449 
2450 		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2451 			changed = 1; /* do restart aneg */
2452 	}
2453 
2454 	/* Only restart aneg if we are advertising something different
2455 	 * than we were before.
2456 	 */
2457 	if (changed > 0)
2458 		return genphy_restart_aneg(phydev);
2459 
2460 	return 0;
2461 }
2462 EXPORT_SYMBOL(genphy_c37_config_aneg);
2463 
2464 /**
2465  * genphy_aneg_done - return auto-negotiation status
2466  * @phydev: target phy_device struct
2467  *
2468  * Description: Reads the status register and returns 0 either if
2469  *   auto-negotiation is incomplete, or if there was an error.
2470  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2471  */
2472 int genphy_aneg_done(struct phy_device *phydev)
2473 {
2474 	int retval = phy_read(phydev, MII_BMSR);
2475 
2476 	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2477 }
2478 EXPORT_SYMBOL(genphy_aneg_done);
2479 
2480 /**
2481  * genphy_update_link - update link status in @phydev
2482  * @phydev: target phy_device struct
2483  *
2484  * Description: Update the value in phydev->link to reflect the
2485  *   current link value.  In order to do this, we need to read
2486  *   the status register twice, keeping the second value.
2487  */
2488 int genphy_update_link(struct phy_device *phydev)
2489 {
2490 	int status = 0, bmcr;
2491 
2492 	bmcr = phy_read(phydev, MII_BMCR);
2493 	if (bmcr < 0)
2494 		return bmcr;
2495 
2496 	/* Autoneg is being started, therefore disregard BMSR value and
2497 	 * report link as down.
2498 	 */
2499 	if (bmcr & BMCR_ANRESTART)
2500 		goto done;
2501 
2502 	/* The link state is latched low so that momentary link
2503 	 * drops can be detected. Do not double-read the status
2504 	 * in polling mode to detect such short link drops except
2505 	 * the link was already down.
2506 	 */
2507 	if (!phy_polling_mode(phydev) || !phydev->link) {
2508 		status = phy_read(phydev, MII_BMSR);
2509 		if (status < 0)
2510 			return status;
2511 		else if (status & BMSR_LSTATUS)
2512 			goto done;
2513 	}
2514 
2515 	/* Read link and autonegotiation status */
2516 	status = phy_read(phydev, MII_BMSR);
2517 	if (status < 0)
2518 		return status;
2519 done:
2520 	phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2521 	phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2522 
2523 	/* Consider the case that autoneg was started and "aneg complete"
2524 	 * bit has been reset, but "link up" bit not yet.
2525 	 */
2526 	if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2527 		phydev->link = 0;
2528 
2529 	return 0;
2530 }
2531 EXPORT_SYMBOL(genphy_update_link);
2532 
2533 int genphy_read_lpa(struct phy_device *phydev)
2534 {
2535 	int lpa, lpagb;
2536 
2537 	if (phydev->autoneg == AUTONEG_ENABLE) {
2538 		if (!phydev->autoneg_complete) {
2539 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2540 							0);
2541 			mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2542 			return 0;
2543 		}
2544 
2545 		if (phydev->is_gigabit_capable) {
2546 			lpagb = phy_read(phydev, MII_STAT1000);
2547 			if (lpagb < 0)
2548 				return lpagb;
2549 
2550 			if (lpagb & LPA_1000MSFAIL) {
2551 				int adv = phy_read(phydev, MII_CTRL1000);
2552 
2553 				if (adv < 0)
2554 					return adv;
2555 
2556 				if (adv & CTL1000_ENABLE_MASTER)
2557 					phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2558 				else
2559 					phydev_err(phydev, "Master/Slave resolution failed\n");
2560 				return -ENOLINK;
2561 			}
2562 
2563 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2564 							lpagb);
2565 		}
2566 
2567 		lpa = phy_read(phydev, MII_LPA);
2568 		if (lpa < 0)
2569 			return lpa;
2570 
2571 		mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2572 	} else {
2573 		linkmode_zero(phydev->lp_advertising);
2574 	}
2575 
2576 	return 0;
2577 }
2578 EXPORT_SYMBOL(genphy_read_lpa);
2579 
2580 /**
2581  * genphy_read_status_fixed - read the link parameters for !aneg mode
2582  * @phydev: target phy_device struct
2583  *
2584  * Read the current duplex and speed state for a PHY operating with
2585  * autonegotiation disabled.
2586  */
2587 int genphy_read_status_fixed(struct phy_device *phydev)
2588 {
2589 	int bmcr = phy_read(phydev, MII_BMCR);
2590 
2591 	if (bmcr < 0)
2592 		return bmcr;
2593 
2594 	if (bmcr & BMCR_FULLDPLX)
2595 		phydev->duplex = DUPLEX_FULL;
2596 	else
2597 		phydev->duplex = DUPLEX_HALF;
2598 
2599 	if (bmcr & BMCR_SPEED1000)
2600 		phydev->speed = SPEED_1000;
2601 	else if (bmcr & BMCR_SPEED100)
2602 		phydev->speed = SPEED_100;
2603 	else
2604 		phydev->speed = SPEED_10;
2605 
2606 	return 0;
2607 }
2608 EXPORT_SYMBOL(genphy_read_status_fixed);
2609 
2610 /**
2611  * genphy_read_status - check the link status and update current link state
2612  * @phydev: target phy_device struct
2613  *
2614  * Description: Check the link, then figure out the current state
2615  *   by comparing what we advertise with what the link partner
2616  *   advertises.  Start by checking the gigabit possibilities,
2617  *   then move on to 10/100.
2618  */
2619 int genphy_read_status(struct phy_device *phydev)
2620 {
2621 	int err, old_link = phydev->link;
2622 
2623 	/* Update the link, but return if there was an error */
2624 	err = genphy_update_link(phydev);
2625 	if (err)
2626 		return err;
2627 
2628 	/* why bother the PHY if nothing can have changed */
2629 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2630 		return 0;
2631 
2632 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2633 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2634 	phydev->speed = SPEED_UNKNOWN;
2635 	phydev->duplex = DUPLEX_UNKNOWN;
2636 	phydev->pause = 0;
2637 	phydev->asym_pause = 0;
2638 
2639 	if (phydev->is_gigabit_capable) {
2640 		err = genphy_read_master_slave(phydev);
2641 		if (err < 0)
2642 			return err;
2643 	}
2644 
2645 	err = genphy_read_lpa(phydev);
2646 	if (err < 0)
2647 		return err;
2648 
2649 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2650 		phy_resolve_aneg_linkmode(phydev);
2651 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2652 		err = genphy_read_status_fixed(phydev);
2653 		if (err < 0)
2654 			return err;
2655 	}
2656 
2657 	return 0;
2658 }
2659 EXPORT_SYMBOL(genphy_read_status);
2660 
2661 /**
2662  * genphy_c37_read_status - check the link status and update current link state
2663  * @phydev: target phy_device struct
2664  * @changed: pointer where to store if link changed
2665  *
2666  * Description: Check the link, then figure out the current state
2667  *   by comparing what we advertise with what the link partner
2668  *   advertises. This function is for Clause 37 1000Base-X mode.
2669  *
2670  *   If link has changed, @changed is set to true, false otherwise.
2671  */
2672 int genphy_c37_read_status(struct phy_device *phydev, bool *changed)
2673 {
2674 	int lpa, err, old_link = phydev->link;
2675 
2676 	/* Update the link, but return if there was an error */
2677 	err = genphy_update_link(phydev);
2678 	if (err)
2679 		return err;
2680 
2681 	/* why bother the PHY if nothing can have changed */
2682 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
2683 		*changed = false;
2684 		return 0;
2685 	}
2686 
2687 	/* Signal link has changed */
2688 	*changed = true;
2689 	phydev->duplex = DUPLEX_UNKNOWN;
2690 	phydev->pause = 0;
2691 	phydev->asym_pause = 0;
2692 
2693 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2694 		lpa = phy_read(phydev, MII_LPA);
2695 		if (lpa < 0)
2696 			return lpa;
2697 
2698 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2699 				 phydev->lp_advertising, lpa & LPA_LPACK);
2700 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2701 				 phydev->lp_advertising, lpa & LPA_1000XFULL);
2702 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2703 				 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2704 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2705 				 phydev->lp_advertising,
2706 				 lpa & LPA_1000XPAUSE_ASYM);
2707 
2708 		phy_resolve_aneg_linkmode(phydev);
2709 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2710 		int bmcr = phy_read(phydev, MII_BMCR);
2711 
2712 		if (bmcr < 0)
2713 			return bmcr;
2714 
2715 		if (bmcr & BMCR_FULLDPLX)
2716 			phydev->duplex = DUPLEX_FULL;
2717 		else
2718 			phydev->duplex = DUPLEX_HALF;
2719 	}
2720 
2721 	return 0;
2722 }
2723 EXPORT_SYMBOL(genphy_c37_read_status);
2724 
2725 /**
2726  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2727  * @phydev: target phy_device struct
2728  *
2729  * Description: Perform a software PHY reset using the standard
2730  * BMCR_RESET bit and poll for the reset bit to be cleared.
2731  *
2732  * Returns: 0 on success, < 0 on failure
2733  */
2734 int genphy_soft_reset(struct phy_device *phydev)
2735 {
2736 	u16 res = BMCR_RESET;
2737 	int ret;
2738 
2739 	if (phydev->autoneg == AUTONEG_ENABLE)
2740 		res |= BMCR_ANRESTART;
2741 
2742 	ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2743 	if (ret < 0)
2744 		return ret;
2745 
2746 	/* Clause 22 states that setting bit BMCR_RESET sets control registers
2747 	 * to their default value. Therefore the POWER DOWN bit is supposed to
2748 	 * be cleared after soft reset.
2749 	 */
2750 	phydev->suspended = 0;
2751 
2752 	ret = phy_poll_reset(phydev);
2753 	if (ret)
2754 		return ret;
2755 
2756 	/* BMCR may be reset to defaults */
2757 	if (phydev->autoneg == AUTONEG_DISABLE)
2758 		ret = genphy_setup_forced(phydev);
2759 
2760 	return ret;
2761 }
2762 EXPORT_SYMBOL(genphy_soft_reset);
2763 
2764 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
2765 {
2766 	/* It seems there are cases where the interrupts are handled by another
2767 	 * entity (ie an IRQ controller embedded inside the PHY) and do not
2768 	 * need any other interraction from phylib. In this case, just trigger
2769 	 * the state machine directly.
2770 	 */
2771 	phy_trigger_machine(phydev);
2772 
2773 	return 0;
2774 }
2775 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
2776 
2777 /**
2778  * genphy_read_abilities - read PHY abilities from Clause 22 registers
2779  * @phydev: target phy_device struct
2780  *
2781  * Description: Reads the PHY's abilities and populates
2782  * phydev->supported accordingly.
2783  *
2784  * Returns: 0 on success, < 0 on failure
2785  */
2786 int genphy_read_abilities(struct phy_device *phydev)
2787 {
2788 	int val;
2789 
2790 	linkmode_set_bit_array(phy_basic_ports_array,
2791 			       ARRAY_SIZE(phy_basic_ports_array),
2792 			       phydev->supported);
2793 
2794 	val = phy_read(phydev, MII_BMSR);
2795 	if (val < 0)
2796 		return val;
2797 
2798 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2799 			 val & BMSR_ANEGCAPABLE);
2800 
2801 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2802 			 val & BMSR_100FULL);
2803 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2804 			 val & BMSR_100HALF);
2805 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2806 			 val & BMSR_10FULL);
2807 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2808 			 val & BMSR_10HALF);
2809 
2810 	if (val & BMSR_ESTATEN) {
2811 		val = phy_read(phydev, MII_ESTATUS);
2812 		if (val < 0)
2813 			return val;
2814 
2815 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2816 				 phydev->supported, val & ESTATUS_1000_TFULL);
2817 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2818 				 phydev->supported, val & ESTATUS_1000_THALF);
2819 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2820 				 phydev->supported, val & ESTATUS_1000_XFULL);
2821 	}
2822 
2823 	/* This is optional functionality. If not supported, we may get an error
2824 	 * which should be ignored.
2825 	 */
2826 	genphy_c45_read_eee_abilities(phydev);
2827 
2828 	return 0;
2829 }
2830 EXPORT_SYMBOL(genphy_read_abilities);
2831 
2832 /* This is used for the phy device which doesn't support the MMD extended
2833  * register access, but it does have side effect when we are trying to access
2834  * the MMD register via indirect method.
2835  */
2836 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2837 {
2838 	return -EOPNOTSUPP;
2839 }
2840 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2841 
2842 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2843 				 u16 regnum, u16 val)
2844 {
2845 	return -EOPNOTSUPP;
2846 }
2847 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2848 
2849 int genphy_suspend(struct phy_device *phydev)
2850 {
2851 	return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2852 }
2853 EXPORT_SYMBOL(genphy_suspend);
2854 
2855 int genphy_resume(struct phy_device *phydev)
2856 {
2857 	return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2858 }
2859 EXPORT_SYMBOL(genphy_resume);
2860 
2861 int genphy_loopback(struct phy_device *phydev, bool enable)
2862 {
2863 	if (enable) {
2864 		u16 ctl = BMCR_LOOPBACK;
2865 		int ret, val;
2866 
2867 		ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2868 
2869 		phy_modify(phydev, MII_BMCR, ~0, ctl);
2870 
2871 		ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
2872 					    val & BMSR_LSTATUS,
2873 				    5000, 500000, true);
2874 		if (ret)
2875 			return ret;
2876 	} else {
2877 		phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
2878 
2879 		phy_config_aneg(phydev);
2880 	}
2881 
2882 	return 0;
2883 }
2884 EXPORT_SYMBOL(genphy_loopback);
2885 
2886 /**
2887  * phy_remove_link_mode - Remove a supported link mode
2888  * @phydev: phy_device structure to remove link mode from
2889  * @link_mode: Link mode to be removed
2890  *
2891  * Description: Some MACs don't support all link modes which the PHY
2892  * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
2893  * to remove a link mode.
2894  */
2895 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2896 {
2897 	linkmode_clear_bit(link_mode, phydev->supported);
2898 	phy_advertise_supported(phydev);
2899 }
2900 EXPORT_SYMBOL(phy_remove_link_mode);
2901 
2902 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2903 {
2904 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2905 		linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2906 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2907 		linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2908 }
2909 
2910 /**
2911  * phy_advertise_supported - Advertise all supported modes
2912  * @phydev: target phy_device struct
2913  *
2914  * Description: Called to advertise all supported modes, doesn't touch
2915  * pause mode advertising.
2916  */
2917 void phy_advertise_supported(struct phy_device *phydev)
2918 {
2919 	__ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2920 
2921 	linkmode_copy(new, phydev->supported);
2922 	phy_copy_pause_bits(new, phydev->advertising);
2923 	linkmode_copy(phydev->advertising, new);
2924 }
2925 EXPORT_SYMBOL(phy_advertise_supported);
2926 
2927 /**
2928  * phy_advertise_eee_all - Advertise all supported EEE modes
2929  * @phydev: target phy_device struct
2930  *
2931  * Description: Per default phylib preserves the EEE advertising at the time of
2932  * phy probing, which might be a subset of the supported EEE modes. Use this
2933  * function when all supported EEE modes should be advertised. This does not
2934  * trigger auto-negotiation, so must be called before phy_start()/
2935  * phylink_start() which will start auto-negotiation.
2936  */
2937 void phy_advertise_eee_all(struct phy_device *phydev)
2938 {
2939 	linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2940 }
2941 EXPORT_SYMBOL_GPL(phy_advertise_eee_all);
2942 
2943 /**
2944  * phy_support_eee - Set initial EEE policy configuration
2945  * @phydev: Target phy_device struct
2946  *
2947  * This function configures the initial policy for Energy Efficient Ethernet
2948  * (EEE) on the specified PHY device, influencing that EEE capabilities are
2949  * advertised before the link is established. It should be called during PHY
2950  * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
2951  * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
2952  * of the MAC.
2953  *
2954  * The function sets default EEE policy parameters, including preparing the PHY
2955  * to advertise EEE capabilities based on hardware support.
2956  *
2957  * It also sets the expected configuration for Low Power Idle (LPI) in the MAC
2958  * driver. If the PHY framework determines that both local and remote
2959  * advertisements support EEE, and the negotiated link mode is compatible with
2960  * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on
2961  * this setting by enabling the LPI timer if enable_tx_lpi is set.
2962  */
2963 void phy_support_eee(struct phy_device *phydev)
2964 {
2965 	linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2966 	phydev->eee_cfg.tx_lpi_enabled = true;
2967 	phydev->eee_cfg.eee_enabled = true;
2968 }
2969 EXPORT_SYMBOL(phy_support_eee);
2970 
2971 /**
2972  * phy_support_sym_pause - Enable support of symmetrical pause
2973  * @phydev: target phy_device struct
2974  *
2975  * Description: Called by the MAC to indicate is supports symmetrical
2976  * Pause, but not asym pause.
2977  */
2978 void phy_support_sym_pause(struct phy_device *phydev)
2979 {
2980 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2981 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2982 }
2983 EXPORT_SYMBOL(phy_support_sym_pause);
2984 
2985 /**
2986  * phy_support_asym_pause - Enable support of asym pause
2987  * @phydev: target phy_device struct
2988  *
2989  * Description: Called by the MAC to indicate is supports Asym Pause.
2990  */
2991 void phy_support_asym_pause(struct phy_device *phydev)
2992 {
2993 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2994 }
2995 EXPORT_SYMBOL(phy_support_asym_pause);
2996 
2997 /**
2998  * phy_set_sym_pause - Configure symmetric Pause
2999  * @phydev: target phy_device struct
3000  * @rx: Receiver Pause is supported
3001  * @tx: Transmit Pause is supported
3002  * @autoneg: Auto neg should be used
3003  *
3004  * Description: Configure advertised Pause support depending on if
3005  * receiver pause and pause auto neg is supported. Generally called
3006  * from the set_pauseparam .ndo.
3007  */
3008 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
3009 		       bool autoneg)
3010 {
3011 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
3012 
3013 	if (rx && tx && autoneg)
3014 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3015 				 phydev->supported);
3016 
3017 	linkmode_copy(phydev->advertising, phydev->supported);
3018 }
3019 EXPORT_SYMBOL(phy_set_sym_pause);
3020 
3021 /**
3022  * phy_set_asym_pause - Configure Pause and Asym Pause
3023  * @phydev: target phy_device struct
3024  * @rx: Receiver Pause is supported
3025  * @tx: Transmit Pause is supported
3026  *
3027  * Description: Configure advertised Pause support depending on if
3028  * transmit and receiver pause is supported. If there has been a
3029  * change in adverting, trigger a new autoneg. Generally called from
3030  * the set_pauseparam .ndo.
3031  */
3032 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
3033 {
3034 	__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
3035 
3036 	linkmode_copy(oldadv, phydev->advertising);
3037 	linkmode_set_pause(phydev->advertising, tx, rx);
3038 
3039 	if (!linkmode_equal(oldadv, phydev->advertising) &&
3040 	    phydev->autoneg)
3041 		phy_start_aneg(phydev);
3042 }
3043 EXPORT_SYMBOL(phy_set_asym_pause);
3044 
3045 /**
3046  * phy_validate_pause - Test if the PHY/MAC support the pause configuration
3047  * @phydev: phy_device struct
3048  * @pp: requested pause configuration
3049  *
3050  * Description: Test if the PHY/MAC combination supports the Pause
3051  * configuration the user is requesting. Returns True if it is
3052  * supported, false otherwise.
3053  */
3054 bool phy_validate_pause(struct phy_device *phydev,
3055 			struct ethtool_pauseparam *pp)
3056 {
3057 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3058 			       phydev->supported) && pp->rx_pause)
3059 		return false;
3060 
3061 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3062 			       phydev->supported) &&
3063 	    pp->rx_pause != pp->tx_pause)
3064 		return false;
3065 
3066 	return true;
3067 }
3068 EXPORT_SYMBOL(phy_validate_pause);
3069 
3070 /**
3071  * phy_get_pause - resolve negotiated pause modes
3072  * @phydev: phy_device struct
3073  * @tx_pause: pointer to bool to indicate whether transmit pause should be
3074  * enabled.
3075  * @rx_pause: pointer to bool to indicate whether receive pause should be
3076  * enabled.
3077  *
3078  * Resolve and return the flow control modes according to the negotiation
3079  * result. This includes checking that we are operating in full duplex mode.
3080  * See linkmode_resolve_pause() for further details.
3081  */
3082 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
3083 {
3084 	if (phydev->duplex != DUPLEX_FULL) {
3085 		*tx_pause = false;
3086 		*rx_pause = false;
3087 		return;
3088 	}
3089 
3090 	return linkmode_resolve_pause(phydev->advertising,
3091 				      phydev->lp_advertising,
3092 				      tx_pause, rx_pause);
3093 }
3094 EXPORT_SYMBOL(phy_get_pause);
3095 
3096 #if IS_ENABLED(CONFIG_OF_MDIO)
3097 static int phy_get_int_delay_property(struct device *dev, const char *name)
3098 {
3099 	s32 int_delay;
3100 	int ret;
3101 
3102 	ret = device_property_read_u32(dev, name, &int_delay);
3103 	if (ret)
3104 		return ret;
3105 
3106 	return int_delay;
3107 }
3108 #else
3109 static int phy_get_int_delay_property(struct device *dev, const char *name)
3110 {
3111 	return -EINVAL;
3112 }
3113 #endif
3114 
3115 /**
3116  * phy_get_internal_delay - returns the index of the internal delay
3117  * @phydev: phy_device struct
3118  * @dev: pointer to the devices device struct
3119  * @delay_values: array of delays the PHY supports
3120  * @size: the size of the delay array
3121  * @is_rx: boolean to indicate to get the rx internal delay
3122  *
3123  * Returns the index within the array of internal delay passed in.
3124  * If the device property is not present then the interface type is checked
3125  * if the interface defines use of internal delay then a 1 is returned otherwise
3126  * a 0 is returned.
3127  * The array must be in ascending order. If PHY does not have an ascending order
3128  * array then size = 0 and the value of the delay property is returned.
3129  * Return -EINVAL if the delay is invalid or cannot be found.
3130  */
3131 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
3132 			   const int *delay_values, int size, bool is_rx)
3133 {
3134 	s32 delay;
3135 	int i;
3136 
3137 	if (is_rx) {
3138 		delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
3139 		if (delay < 0 && size == 0) {
3140 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3141 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
3142 				return 1;
3143 			else
3144 				return 0;
3145 		}
3146 
3147 	} else {
3148 		delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
3149 		if (delay < 0 && size == 0) {
3150 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3151 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
3152 				return 1;
3153 			else
3154 				return 0;
3155 		}
3156 	}
3157 
3158 	if (delay < 0)
3159 		return delay;
3160 
3161 	if (size == 0)
3162 		return delay;
3163 
3164 	if (delay < delay_values[0] || delay > delay_values[size - 1]) {
3165 		phydev_err(phydev, "Delay %d is out of range\n", delay);
3166 		return -EINVAL;
3167 	}
3168 
3169 	if (delay == delay_values[0])
3170 		return 0;
3171 
3172 	for (i = 1; i < size; i++) {
3173 		if (delay == delay_values[i])
3174 			return i;
3175 
3176 		/* Find an approximate index by looking up the table */
3177 		if (delay > delay_values[i - 1] &&
3178 		    delay < delay_values[i]) {
3179 			if (delay - delay_values[i - 1] <
3180 			    delay_values[i] - delay)
3181 				return i - 1;
3182 			else
3183 				return i;
3184 		}
3185 	}
3186 
3187 	phydev_err(phydev, "error finding internal delay index for %d\n",
3188 		   delay);
3189 
3190 	return -EINVAL;
3191 }
3192 EXPORT_SYMBOL(phy_get_internal_delay);
3193 
3194 static int phy_led_set_brightness(struct led_classdev *led_cdev,
3195 				  enum led_brightness value)
3196 {
3197 	struct phy_led *phyled = to_phy_led(led_cdev);
3198 	struct phy_device *phydev = phyled->phydev;
3199 	int err;
3200 
3201 	mutex_lock(&phydev->lock);
3202 	err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
3203 	mutex_unlock(&phydev->lock);
3204 
3205 	return err;
3206 }
3207 
3208 static int phy_led_blink_set(struct led_classdev *led_cdev,
3209 			     unsigned long *delay_on,
3210 			     unsigned long *delay_off)
3211 {
3212 	struct phy_led *phyled = to_phy_led(led_cdev);
3213 	struct phy_device *phydev = phyled->phydev;
3214 	int err;
3215 
3216 	mutex_lock(&phydev->lock);
3217 	err = phydev->drv->led_blink_set(phydev, phyled->index,
3218 					 delay_on, delay_off);
3219 	mutex_unlock(&phydev->lock);
3220 
3221 	return err;
3222 }
3223 
3224 static __maybe_unused struct device *
3225 phy_led_hw_control_get_device(struct led_classdev *led_cdev)
3226 {
3227 	struct phy_led *phyled = to_phy_led(led_cdev);
3228 	struct phy_device *phydev = phyled->phydev;
3229 
3230 	if (phydev->attached_dev)
3231 		return &phydev->attached_dev->dev;
3232 	return NULL;
3233 }
3234 
3235 static int __maybe_unused
3236 phy_led_hw_control_get(struct led_classdev *led_cdev,
3237 		       unsigned long *rules)
3238 {
3239 	struct phy_led *phyled = to_phy_led(led_cdev);
3240 	struct phy_device *phydev = phyled->phydev;
3241 	int err;
3242 
3243 	mutex_lock(&phydev->lock);
3244 	err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
3245 	mutex_unlock(&phydev->lock);
3246 
3247 	return err;
3248 }
3249 
3250 static int __maybe_unused
3251 phy_led_hw_control_set(struct led_classdev *led_cdev,
3252 		       unsigned long rules)
3253 {
3254 	struct phy_led *phyled = to_phy_led(led_cdev);
3255 	struct phy_device *phydev = phyled->phydev;
3256 	int err;
3257 
3258 	mutex_lock(&phydev->lock);
3259 	err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
3260 	mutex_unlock(&phydev->lock);
3261 
3262 	return err;
3263 }
3264 
3265 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev,
3266 						  unsigned long rules)
3267 {
3268 	struct phy_led *phyled = to_phy_led(led_cdev);
3269 	struct phy_device *phydev = phyled->phydev;
3270 	int err;
3271 
3272 	mutex_lock(&phydev->lock);
3273 	err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
3274 	mutex_unlock(&phydev->lock);
3275 
3276 	return err;
3277 }
3278 
3279 static void phy_leds_unregister(struct phy_device *phydev)
3280 {
3281 	struct phy_led *phyled;
3282 
3283 	list_for_each_entry(phyled, &phydev->leds, list) {
3284 		led_classdev_unregister(&phyled->led_cdev);
3285 	}
3286 }
3287 
3288 static int of_phy_led(struct phy_device *phydev,
3289 		      struct device_node *led)
3290 {
3291 	struct device *dev = &phydev->mdio.dev;
3292 	struct led_init_data init_data = {};
3293 	struct led_classdev *cdev;
3294 	unsigned long modes = 0;
3295 	struct phy_led *phyled;
3296 	u32 index;
3297 	int err;
3298 
3299 	phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
3300 	if (!phyled)
3301 		return -ENOMEM;
3302 
3303 	cdev = &phyled->led_cdev;
3304 	phyled->phydev = phydev;
3305 
3306 	err = of_property_read_u32(led, "reg", &index);
3307 	if (err)
3308 		return err;
3309 	if (index > U8_MAX)
3310 		return -EINVAL;
3311 
3312 	if (of_property_read_bool(led, "active-low"))
3313 		set_bit(PHY_LED_ACTIVE_LOW, &modes);
3314 	if (of_property_read_bool(led, "inactive-high-impedance"))
3315 		set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
3316 
3317 	if (modes) {
3318 		/* Return error if asked to set polarity modes but not supported */
3319 		if (!phydev->drv->led_polarity_set)
3320 			return -EINVAL;
3321 
3322 		err = phydev->drv->led_polarity_set(phydev, index, modes);
3323 		if (err)
3324 			return err;
3325 	}
3326 
3327 	phyled->index = index;
3328 	if (phydev->drv->led_brightness_set)
3329 		cdev->brightness_set_blocking = phy_led_set_brightness;
3330 	if (phydev->drv->led_blink_set)
3331 		cdev->blink_set = phy_led_blink_set;
3332 
3333 #ifdef CONFIG_LEDS_TRIGGERS
3334 	if (phydev->drv->led_hw_is_supported &&
3335 	    phydev->drv->led_hw_control_set &&
3336 	    phydev->drv->led_hw_control_get) {
3337 		cdev->hw_control_is_supported = phy_led_hw_is_supported;
3338 		cdev->hw_control_set = phy_led_hw_control_set;
3339 		cdev->hw_control_get = phy_led_hw_control_get;
3340 		cdev->hw_control_trigger = "netdev";
3341 	}
3342 
3343 	cdev->hw_control_get_device = phy_led_hw_control_get_device;
3344 #endif
3345 	cdev->max_brightness = 1;
3346 	init_data.devicename = dev_name(&phydev->mdio.dev);
3347 	init_data.fwnode = of_fwnode_handle(led);
3348 	init_data.devname_mandatory = true;
3349 
3350 	err = led_classdev_register_ext(dev, cdev, &init_data);
3351 	if (err)
3352 		return err;
3353 
3354 	list_add(&phyled->list, &phydev->leds);
3355 
3356 	return 0;
3357 }
3358 
3359 static int of_phy_leds(struct phy_device *phydev)
3360 {
3361 	struct device_node *node = phydev->mdio.dev.of_node;
3362 	struct device_node *leds, *led;
3363 	int err;
3364 
3365 	if (!IS_ENABLED(CONFIG_OF_MDIO))
3366 		return 0;
3367 
3368 	if (!node)
3369 		return 0;
3370 
3371 	leds = of_get_child_by_name(node, "leds");
3372 	if (!leds)
3373 		return 0;
3374 
3375 	for_each_available_child_of_node(leds, led) {
3376 		err = of_phy_led(phydev, led);
3377 		if (err) {
3378 			of_node_put(led);
3379 			phy_leds_unregister(phydev);
3380 			return err;
3381 		}
3382 	}
3383 
3384 	return 0;
3385 }
3386 
3387 /**
3388  * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
3389  * @fwnode: pointer to the mdio_device's fwnode
3390  *
3391  * If successful, returns a pointer to the mdio_device with the embedded
3392  * struct device refcount incremented by one, or NULL on failure.
3393  * The caller should call put_device() on the mdio_device after its use.
3394  */
3395 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
3396 {
3397 	struct device *d;
3398 
3399 	if (!fwnode)
3400 		return NULL;
3401 
3402 	d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode);
3403 	if (!d)
3404 		return NULL;
3405 
3406 	return to_mdio_device(d);
3407 }
3408 EXPORT_SYMBOL(fwnode_mdio_find_device);
3409 
3410 /**
3411  * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
3412  *
3413  * @phy_fwnode: Pointer to the phy's fwnode.
3414  *
3415  * If successful, returns a pointer to the phy_device with the embedded
3416  * struct device refcount incremented by one, or NULL on failure.
3417  */
3418 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
3419 {
3420 	struct mdio_device *mdiodev;
3421 
3422 	mdiodev = fwnode_mdio_find_device(phy_fwnode);
3423 	if (!mdiodev)
3424 		return NULL;
3425 
3426 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
3427 		return to_phy_device(&mdiodev->dev);
3428 
3429 	put_device(&mdiodev->dev);
3430 
3431 	return NULL;
3432 }
3433 EXPORT_SYMBOL(fwnode_phy_find_device);
3434 
3435 /**
3436  * device_phy_find_device - For the given device, get the phy_device
3437  * @dev: Pointer to the given device
3438  *
3439  * Refer return conditions of fwnode_phy_find_device().
3440  */
3441 struct phy_device *device_phy_find_device(struct device *dev)
3442 {
3443 	return fwnode_phy_find_device(dev_fwnode(dev));
3444 }
3445 EXPORT_SYMBOL_GPL(device_phy_find_device);
3446 
3447 /**
3448  * fwnode_get_phy_node - Get the phy_node using the named reference.
3449  * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
3450  *
3451  * Refer return conditions of fwnode_find_reference().
3452  * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
3453  * and "phy-device" are not supported in ACPI. DT supports all the three
3454  * named references to the phy node.
3455  */
3456 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode)
3457 {
3458 	struct fwnode_handle *phy_node;
3459 
3460 	/* Only phy-handle is used for ACPI */
3461 	phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
3462 	if (is_acpi_node(fwnode) || !IS_ERR(phy_node))
3463 		return phy_node;
3464 	phy_node = fwnode_find_reference(fwnode, "phy", 0);
3465 	if (IS_ERR(phy_node))
3466 		phy_node = fwnode_find_reference(fwnode, "phy-device", 0);
3467 	return phy_node;
3468 }
3469 EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
3470 
3471 /**
3472  * phy_probe - probe and init a PHY device
3473  * @dev: device to probe and init
3474  *
3475  * Take care of setting up the phy_device structure, set the state to READY.
3476  */
3477 static int phy_probe(struct device *dev)
3478 {
3479 	struct phy_device *phydev = to_phy_device(dev);
3480 	struct device_driver *drv = phydev->mdio.dev.driver;
3481 	struct phy_driver *phydrv = to_phy_driver(drv);
3482 	int err = 0;
3483 
3484 	phydev->drv = phydrv;
3485 
3486 	/* Disable the interrupt if the PHY doesn't support it
3487 	 * but the interrupt is still a valid one
3488 	 */
3489 	if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
3490 		phydev->irq = PHY_POLL;
3491 
3492 	if (phydrv->flags & PHY_IS_INTERNAL)
3493 		phydev->is_internal = true;
3494 
3495 	/* Deassert the reset signal */
3496 	phy_device_reset(phydev, 0);
3497 
3498 	if (phydev->drv->probe) {
3499 		err = phydev->drv->probe(phydev);
3500 		if (err)
3501 			goto out;
3502 	}
3503 
3504 	phy_disable_interrupts(phydev);
3505 
3506 	/* Start out supporting everything. Eventually,
3507 	 * a controller will attach, and may modify one
3508 	 * or both of these values
3509 	 */
3510 	if (phydrv->features) {
3511 		linkmode_copy(phydev->supported, phydrv->features);
3512 		genphy_c45_read_eee_abilities(phydev);
3513 	}
3514 	else if (phydrv->get_features)
3515 		err = phydrv->get_features(phydev);
3516 	else if (phydev->is_c45)
3517 		err = genphy_c45_pma_read_abilities(phydev);
3518 	else
3519 		err = genphy_read_abilities(phydev);
3520 
3521 	if (err)
3522 		goto out;
3523 
3524 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3525 			       phydev->supported))
3526 		phydev->autoneg = 0;
3527 
3528 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
3529 			      phydev->supported))
3530 		phydev->is_gigabit_capable = 1;
3531 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
3532 			      phydev->supported))
3533 		phydev->is_gigabit_capable = 1;
3534 
3535 	of_set_phy_supported(phydev);
3536 	phy_advertise_supported(phydev);
3537 
3538 	/* Get PHY default EEE advertising modes and handle them as potentially
3539 	 * safe initial configuration.
3540 	 */
3541 	err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
3542 	if (err)
3543 		goto out;
3544 
3545 	/* There is no "enabled" flag. If PHY is advertising, assume it is
3546 	 * kind of enabled.
3547 	 */
3548 	phydev->eee_enabled = !linkmode_empty(phydev->advertising_eee);
3549 
3550 	/* Some PHYs may advertise, by default, not support EEE modes. So,
3551 	 * we need to clean them.
3552 	 */
3553 	if (phydev->eee_enabled)
3554 		linkmode_and(phydev->advertising_eee, phydev->supported_eee,
3555 			     phydev->advertising_eee);
3556 
3557 	/* Get the EEE modes we want to prohibit. We will ask
3558 	 * the PHY stop advertising these mode later on
3559 	 */
3560 	of_set_phy_eee_broken(phydev);
3561 
3562 	/* The Pause Frame bits indicate that the PHY can support passing
3563 	 * pause frames. During autonegotiation, the PHYs will determine if
3564 	 * they should allow pause frames to pass.  The MAC driver should then
3565 	 * use that result to determine whether to enable flow control via
3566 	 * pause frames.
3567 	 *
3568 	 * Normally, PHY drivers should not set the Pause bits, and instead
3569 	 * allow phylib to do that.  However, there may be some situations
3570 	 * (e.g. hardware erratum) where the driver wants to set only one
3571 	 * of these bits.
3572 	 */
3573 	if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
3574 	    !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
3575 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3576 				 phydev->supported);
3577 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3578 				 phydev->supported);
3579 	}
3580 
3581 	/* Set the state to READY by default */
3582 	phydev->state = PHY_READY;
3583 
3584 	/* Get the LEDs from the device tree, and instantiate standard
3585 	 * LEDs for them.
3586 	 */
3587 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3588 		err = of_phy_leds(phydev);
3589 
3590 out:
3591 	/* Re-assert the reset signal on error */
3592 	if (err)
3593 		phy_device_reset(phydev, 1);
3594 
3595 	return err;
3596 }
3597 
3598 static int phy_remove(struct device *dev)
3599 {
3600 	struct phy_device *phydev = to_phy_device(dev);
3601 
3602 	cancel_delayed_work_sync(&phydev->state_queue);
3603 
3604 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3605 		phy_leds_unregister(phydev);
3606 
3607 	phydev->state = PHY_DOWN;
3608 
3609 	sfp_bus_del_upstream(phydev->sfp_bus);
3610 	phydev->sfp_bus = NULL;
3611 
3612 	if (phydev->drv && phydev->drv->remove)
3613 		phydev->drv->remove(phydev);
3614 
3615 	/* Assert the reset signal */
3616 	phy_device_reset(phydev, 1);
3617 
3618 	phydev->drv = NULL;
3619 
3620 	return 0;
3621 }
3622 
3623 /**
3624  * phy_driver_register - register a phy_driver with the PHY layer
3625  * @new_driver: new phy_driver to register
3626  * @owner: module owning this PHY
3627  */
3628 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
3629 {
3630 	int retval;
3631 
3632 	/* Either the features are hard coded, or dynamically
3633 	 * determined. It cannot be both.
3634 	 */
3635 	if (WARN_ON(new_driver->features && new_driver->get_features)) {
3636 		pr_err("%s: features and get_features must not both be set\n",
3637 		       new_driver->name);
3638 		return -EINVAL;
3639 	}
3640 
3641 	/* PHYLIB device drivers must not match using a DT compatible table
3642 	 * as this bypasses our checks that the mdiodev that is being matched
3643 	 * is backed by a struct phy_device. If such a case happens, we will
3644 	 * make out-of-bounds accesses and lockup in phydev->lock.
3645 	 */
3646 	if (WARN(new_driver->mdiodrv.driver.of_match_table,
3647 		 "%s: driver must not provide a DT match table\n",
3648 		 new_driver->name))
3649 		return -EINVAL;
3650 
3651 	new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3652 	new_driver->mdiodrv.driver.name = new_driver->name;
3653 	new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3654 	new_driver->mdiodrv.driver.probe = phy_probe;
3655 	new_driver->mdiodrv.driver.remove = phy_remove;
3656 	new_driver->mdiodrv.driver.owner = owner;
3657 	new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3658 
3659 	retval = driver_register(&new_driver->mdiodrv.driver);
3660 	if (retval) {
3661 		pr_err("%s: Error %d in registering driver\n",
3662 		       new_driver->name, retval);
3663 
3664 		return retval;
3665 	}
3666 
3667 	pr_debug("%s: Registered new driver\n", new_driver->name);
3668 
3669 	return 0;
3670 }
3671 EXPORT_SYMBOL(phy_driver_register);
3672 
3673 int phy_drivers_register(struct phy_driver *new_driver, int n,
3674 			 struct module *owner)
3675 {
3676 	int i, ret = 0;
3677 
3678 	for (i = 0; i < n; i++) {
3679 		ret = phy_driver_register(new_driver + i, owner);
3680 		if (ret) {
3681 			while (i-- > 0)
3682 				phy_driver_unregister(new_driver + i);
3683 			break;
3684 		}
3685 	}
3686 	return ret;
3687 }
3688 EXPORT_SYMBOL(phy_drivers_register);
3689 
3690 void phy_driver_unregister(struct phy_driver *drv)
3691 {
3692 	driver_unregister(&drv->mdiodrv.driver);
3693 }
3694 EXPORT_SYMBOL(phy_driver_unregister);
3695 
3696 void phy_drivers_unregister(struct phy_driver *drv, int n)
3697 {
3698 	int i;
3699 
3700 	for (i = 0; i < n; i++)
3701 		phy_driver_unregister(drv + i);
3702 }
3703 EXPORT_SYMBOL(phy_drivers_unregister);
3704 
3705 static struct phy_driver genphy_driver = {
3706 	.phy_id		= 0xffffffff,
3707 	.phy_id_mask	= 0xffffffff,
3708 	.name		= "Generic PHY",
3709 	.get_features	= genphy_read_abilities,
3710 	.suspend	= genphy_suspend,
3711 	.resume		= genphy_resume,
3712 	.set_loopback   = genphy_loopback,
3713 };
3714 
3715 static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
3716 	.get_sset_count		= phy_ethtool_get_sset_count,
3717 	.get_strings		= phy_ethtool_get_strings,
3718 	.get_stats		= phy_ethtool_get_stats,
3719 	.get_plca_cfg		= phy_ethtool_get_plca_cfg,
3720 	.set_plca_cfg		= phy_ethtool_set_plca_cfg,
3721 	.get_plca_status	= phy_ethtool_get_plca_status,
3722 	.start_cable_test	= phy_start_cable_test,
3723 	.start_cable_test_tdr	= phy_start_cable_test_tdr,
3724 };
3725 
3726 static const struct phylib_stubs __phylib_stubs = {
3727 	.hwtstamp_get = __phy_hwtstamp_get,
3728 	.hwtstamp_set = __phy_hwtstamp_set,
3729 };
3730 
3731 static void phylib_register_stubs(void)
3732 {
3733 	phylib_stubs = &__phylib_stubs;
3734 }
3735 
3736 static void phylib_unregister_stubs(void)
3737 {
3738 	phylib_stubs = NULL;
3739 }
3740 
3741 static int __init phy_init(void)
3742 {
3743 	int rc;
3744 
3745 	rtnl_lock();
3746 	ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
3747 	phylib_register_stubs();
3748 	rtnl_unlock();
3749 
3750 	rc = mdio_bus_init();
3751 	if (rc)
3752 		goto err_ethtool_phy_ops;
3753 
3754 	features_init();
3755 
3756 	rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
3757 	if (rc)
3758 		goto err_mdio_bus;
3759 
3760 	rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3761 	if (rc)
3762 		goto err_c45;
3763 
3764 	return 0;
3765 
3766 err_c45:
3767 	phy_driver_unregister(&genphy_c45_driver);
3768 err_mdio_bus:
3769 	mdio_bus_exit();
3770 err_ethtool_phy_ops:
3771 	rtnl_lock();
3772 	phylib_unregister_stubs();
3773 	ethtool_set_ethtool_phy_ops(NULL);
3774 	rtnl_unlock();
3775 
3776 	return rc;
3777 }
3778 
3779 static void __exit phy_exit(void)
3780 {
3781 	phy_driver_unregister(&genphy_c45_driver);
3782 	phy_driver_unregister(&genphy_driver);
3783 	mdio_bus_exit();
3784 	rtnl_lock();
3785 	phylib_unregister_stubs();
3786 	ethtool_set_ethtool_phy_ops(NULL);
3787 	rtnl_unlock();
3788 }
3789 
3790 subsys_initcall(phy_init);
3791 module_exit(phy_exit);
3792