xref: /linux/drivers/net/phy/phy_device.c (revision eb3ab13d997a2f12ec9d557b6ae2aea4e28e2bc3)
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  *
2112  * Description: Writes MII_ADVERTISE with the appropriate values,
2113  *   after sanitizing the values to make sure we only advertise
2114  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
2115  *   hasn't changed, and > 0 if it has changed.
2116  */
2117 static int genphy_config_advert(struct phy_device *phydev)
2118 {
2119 	int err, bmsr, changed = 0;
2120 	u32 adv;
2121 
2122 	/* Only allow advertising what this PHY supports */
2123 	linkmode_and(phydev->advertising, phydev->advertising,
2124 		     phydev->supported);
2125 
2126 	adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
2127 
2128 	/* Setup standard advertisement */
2129 	err = phy_modify_changed(phydev, MII_ADVERTISE,
2130 				 ADVERTISE_ALL | ADVERTISE_100BASE4 |
2131 				 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
2132 				 adv);
2133 	if (err < 0)
2134 		return err;
2135 	if (err > 0)
2136 		changed = 1;
2137 
2138 	bmsr = phy_read(phydev, MII_BMSR);
2139 	if (bmsr < 0)
2140 		return bmsr;
2141 
2142 	/* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
2143 	 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
2144 	 * logical 1.
2145 	 */
2146 	if (!(bmsr & BMSR_ESTATEN))
2147 		return changed;
2148 
2149 	adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
2150 
2151 	err = phy_modify_changed(phydev, MII_CTRL1000,
2152 				 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
2153 				 adv);
2154 	if (err < 0)
2155 		return err;
2156 	if (err > 0)
2157 		changed = 1;
2158 
2159 	return changed;
2160 }
2161 
2162 /**
2163  * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
2164  * @phydev: target phy_device struct
2165  *
2166  * Description: Writes MII_ADVERTISE with the appropriate values,
2167  *   after sanitizing the values to make sure we only advertise
2168  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
2169  *   hasn't changed, and > 0 if it has changed. This function is intended
2170  *   for Clause 37 1000Base-X mode.
2171  */
2172 static int genphy_c37_config_advert(struct phy_device *phydev)
2173 {
2174 	u16 adv = 0;
2175 
2176 	/* Only allow advertising what this PHY supports */
2177 	linkmode_and(phydev->advertising, phydev->advertising,
2178 		     phydev->supported);
2179 
2180 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2181 			      phydev->advertising))
2182 		adv |= ADVERTISE_1000XFULL;
2183 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2184 			      phydev->advertising))
2185 		adv |= ADVERTISE_1000XPAUSE;
2186 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2187 			      phydev->advertising))
2188 		adv |= ADVERTISE_1000XPSE_ASYM;
2189 
2190 	return phy_modify_changed(phydev, MII_ADVERTISE,
2191 				  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
2192 				  ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
2193 				  adv);
2194 }
2195 
2196 /**
2197  * genphy_config_eee_advert - disable unwanted eee mode advertisement
2198  * @phydev: target phy_device struct
2199  *
2200  * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
2201  *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
2202  *   changed, and 1 if it has changed.
2203  */
2204 int genphy_config_eee_advert(struct phy_device *phydev)
2205 {
2206 	int err;
2207 
2208 	/* Nothing to disable */
2209 	if (!phydev->eee_broken_modes)
2210 		return 0;
2211 
2212 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
2213 				     phydev->eee_broken_modes, 0);
2214 	/* If the call failed, we assume that EEE is not supported */
2215 	return err < 0 ? 0 : err;
2216 }
2217 EXPORT_SYMBOL(genphy_config_eee_advert);
2218 
2219 /**
2220  * genphy_setup_forced - configures/forces speed/duplex from @phydev
2221  * @phydev: target phy_device struct
2222  *
2223  * Description: Configures MII_BMCR to force speed/duplex
2224  *   to the values in phydev. Assumes that the values are valid.
2225  *   Please see phy_sanitize_settings().
2226  */
2227 int genphy_setup_forced(struct phy_device *phydev)
2228 {
2229 	u16 ctl;
2230 
2231 	phydev->pause = 0;
2232 	phydev->asym_pause = 0;
2233 
2234 	ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2235 
2236 	return phy_modify(phydev, MII_BMCR,
2237 			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
2238 }
2239 EXPORT_SYMBOL(genphy_setup_forced);
2240 
2241 static int genphy_setup_master_slave(struct phy_device *phydev)
2242 {
2243 	u16 ctl = 0;
2244 
2245 	if (!phydev->is_gigabit_capable)
2246 		return 0;
2247 
2248 	switch (phydev->master_slave_set) {
2249 	case MASTER_SLAVE_CFG_MASTER_PREFERRED:
2250 		ctl |= CTL1000_PREFER_MASTER;
2251 		break;
2252 	case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2253 		break;
2254 	case MASTER_SLAVE_CFG_MASTER_FORCE:
2255 		ctl |= CTL1000_AS_MASTER;
2256 		fallthrough;
2257 	case MASTER_SLAVE_CFG_SLAVE_FORCE:
2258 		ctl |= CTL1000_ENABLE_MASTER;
2259 		break;
2260 	case MASTER_SLAVE_CFG_UNKNOWN:
2261 	case MASTER_SLAVE_CFG_UNSUPPORTED:
2262 		return 0;
2263 	default:
2264 		phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2265 		return -EOPNOTSUPP;
2266 	}
2267 
2268 	return phy_modify_changed(phydev, MII_CTRL1000,
2269 				  (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2270 				   CTL1000_PREFER_MASTER), ctl);
2271 }
2272 
2273 int genphy_read_master_slave(struct phy_device *phydev)
2274 {
2275 	int cfg, state;
2276 	int val;
2277 
2278 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2279 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2280 
2281 	val = phy_read(phydev, MII_CTRL1000);
2282 	if (val < 0)
2283 		return val;
2284 
2285 	if (val & CTL1000_ENABLE_MASTER) {
2286 		if (val & CTL1000_AS_MASTER)
2287 			cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2288 		else
2289 			cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2290 	} else {
2291 		if (val & CTL1000_PREFER_MASTER)
2292 			cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2293 		else
2294 			cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2295 	}
2296 
2297 	val = phy_read(phydev, MII_STAT1000);
2298 	if (val < 0)
2299 		return val;
2300 
2301 	if (val & LPA_1000MSFAIL) {
2302 		state = MASTER_SLAVE_STATE_ERR;
2303 	} else if (phydev->link) {
2304 		/* this bits are valid only for active link */
2305 		if (val & LPA_1000MSRES)
2306 			state = MASTER_SLAVE_STATE_MASTER;
2307 		else
2308 			state = MASTER_SLAVE_STATE_SLAVE;
2309 	} else {
2310 		state = MASTER_SLAVE_STATE_UNKNOWN;
2311 	}
2312 
2313 	phydev->master_slave_get = cfg;
2314 	phydev->master_slave_state = state;
2315 
2316 	return 0;
2317 }
2318 EXPORT_SYMBOL(genphy_read_master_slave);
2319 
2320 /**
2321  * genphy_restart_aneg - Enable and Restart Autonegotiation
2322  * @phydev: target phy_device struct
2323  */
2324 int genphy_restart_aneg(struct phy_device *phydev)
2325 {
2326 	/* Don't isolate the PHY if we're negotiating */
2327 	return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2328 			  BMCR_ANENABLE | BMCR_ANRESTART);
2329 }
2330 EXPORT_SYMBOL(genphy_restart_aneg);
2331 
2332 /**
2333  * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2334  * @phydev: target phy_device struct
2335  * @restart: whether aneg restart is requested
2336  *
2337  * Check, and restart auto-negotiation if needed.
2338  */
2339 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2340 {
2341 	int ret;
2342 
2343 	if (!restart) {
2344 		/* Advertisement hasn't changed, but maybe aneg was never on to
2345 		 * begin with?  Or maybe phy was isolated?
2346 		 */
2347 		ret = phy_read(phydev, MII_BMCR);
2348 		if (ret < 0)
2349 			return ret;
2350 
2351 		if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2352 			restart = true;
2353 	}
2354 
2355 	if (restart)
2356 		return genphy_restart_aneg(phydev);
2357 
2358 	return 0;
2359 }
2360 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2361 
2362 /**
2363  * __genphy_config_aneg - restart auto-negotiation or write BMCR
2364  * @phydev: target phy_device struct
2365  * @changed: whether autoneg is requested
2366  *
2367  * Description: If auto-negotiation is enabled, we configure the
2368  *   advertising, and then restart auto-negotiation.  If it is not
2369  *   enabled, then we write the BMCR.
2370  */
2371 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2372 {
2373 	int err;
2374 
2375 	err = genphy_c45_an_config_eee_aneg(phydev);
2376 	if (err < 0)
2377 		return err;
2378 	else if (err)
2379 		changed = true;
2380 
2381 	err = genphy_setup_master_slave(phydev);
2382 	if (err < 0)
2383 		return err;
2384 	else if (err)
2385 		changed = true;
2386 
2387 	if (AUTONEG_ENABLE != phydev->autoneg)
2388 		return genphy_setup_forced(phydev);
2389 
2390 	err = genphy_config_advert(phydev);
2391 	if (err < 0) /* error */
2392 		return err;
2393 	else if (err)
2394 		changed = true;
2395 
2396 	return genphy_check_and_restart_aneg(phydev, changed);
2397 }
2398 EXPORT_SYMBOL(__genphy_config_aneg);
2399 
2400 /**
2401  * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2402  * @phydev: target phy_device struct
2403  *
2404  * Description: If auto-negotiation is enabled, we configure the
2405  *   advertising, and then restart auto-negotiation.  If it is not
2406  *   enabled, then we write the BMCR. This function is intended
2407  *   for use with Clause 37 1000Base-X mode.
2408  */
2409 int genphy_c37_config_aneg(struct phy_device *phydev)
2410 {
2411 	int err, changed;
2412 
2413 	if (phydev->autoneg != AUTONEG_ENABLE)
2414 		return genphy_setup_forced(phydev);
2415 
2416 	err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2417 			 BMCR_SPEED1000);
2418 	if (err)
2419 		return err;
2420 
2421 	changed = genphy_c37_config_advert(phydev);
2422 	if (changed < 0) /* error */
2423 		return changed;
2424 
2425 	if (!changed) {
2426 		/* Advertisement hasn't changed, but maybe aneg was never on to
2427 		 * begin with?  Or maybe phy was isolated?
2428 		 */
2429 		int ctl = phy_read(phydev, MII_BMCR);
2430 
2431 		if (ctl < 0)
2432 			return ctl;
2433 
2434 		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2435 			changed = 1; /* do restart aneg */
2436 	}
2437 
2438 	/* Only restart aneg if we are advertising something different
2439 	 * than we were before.
2440 	 */
2441 	if (changed > 0)
2442 		return genphy_restart_aneg(phydev);
2443 
2444 	return 0;
2445 }
2446 EXPORT_SYMBOL(genphy_c37_config_aneg);
2447 
2448 /**
2449  * genphy_aneg_done - return auto-negotiation status
2450  * @phydev: target phy_device struct
2451  *
2452  * Description: Reads the status register and returns 0 either if
2453  *   auto-negotiation is incomplete, or if there was an error.
2454  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2455  */
2456 int genphy_aneg_done(struct phy_device *phydev)
2457 {
2458 	int retval = phy_read(phydev, MII_BMSR);
2459 
2460 	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2461 }
2462 EXPORT_SYMBOL(genphy_aneg_done);
2463 
2464 /**
2465  * genphy_update_link - update link status in @phydev
2466  * @phydev: target phy_device struct
2467  *
2468  * Description: Update the value in phydev->link to reflect the
2469  *   current link value.  In order to do this, we need to read
2470  *   the status register twice, keeping the second value.
2471  */
2472 int genphy_update_link(struct phy_device *phydev)
2473 {
2474 	int status = 0, bmcr;
2475 
2476 	bmcr = phy_read(phydev, MII_BMCR);
2477 	if (bmcr < 0)
2478 		return bmcr;
2479 
2480 	/* Autoneg is being started, therefore disregard BMSR value and
2481 	 * report link as down.
2482 	 */
2483 	if (bmcr & BMCR_ANRESTART)
2484 		goto done;
2485 
2486 	/* The link state is latched low so that momentary link
2487 	 * drops can be detected. Do not double-read the status
2488 	 * in polling mode to detect such short link drops except
2489 	 * the link was already down.
2490 	 */
2491 	if (!phy_polling_mode(phydev) || !phydev->link) {
2492 		status = phy_read(phydev, MII_BMSR);
2493 		if (status < 0)
2494 			return status;
2495 		else if (status & BMSR_LSTATUS)
2496 			goto done;
2497 	}
2498 
2499 	/* Read link and autonegotiation status */
2500 	status = phy_read(phydev, MII_BMSR);
2501 	if (status < 0)
2502 		return status;
2503 done:
2504 	phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2505 	phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2506 
2507 	/* Consider the case that autoneg was started and "aneg complete"
2508 	 * bit has been reset, but "link up" bit not yet.
2509 	 */
2510 	if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2511 		phydev->link = 0;
2512 
2513 	return 0;
2514 }
2515 EXPORT_SYMBOL(genphy_update_link);
2516 
2517 int genphy_read_lpa(struct phy_device *phydev)
2518 {
2519 	int lpa, lpagb;
2520 
2521 	if (phydev->autoneg == AUTONEG_ENABLE) {
2522 		if (!phydev->autoneg_complete) {
2523 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2524 							0);
2525 			mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2526 			return 0;
2527 		}
2528 
2529 		if (phydev->is_gigabit_capable) {
2530 			lpagb = phy_read(phydev, MII_STAT1000);
2531 			if (lpagb < 0)
2532 				return lpagb;
2533 
2534 			if (lpagb & LPA_1000MSFAIL) {
2535 				int adv = phy_read(phydev, MII_CTRL1000);
2536 
2537 				if (adv < 0)
2538 					return adv;
2539 
2540 				if (adv & CTL1000_ENABLE_MASTER)
2541 					phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2542 				else
2543 					phydev_err(phydev, "Master/Slave resolution failed\n");
2544 				return -ENOLINK;
2545 			}
2546 
2547 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2548 							lpagb);
2549 		}
2550 
2551 		lpa = phy_read(phydev, MII_LPA);
2552 		if (lpa < 0)
2553 			return lpa;
2554 
2555 		mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2556 	} else {
2557 		linkmode_zero(phydev->lp_advertising);
2558 	}
2559 
2560 	return 0;
2561 }
2562 EXPORT_SYMBOL(genphy_read_lpa);
2563 
2564 /**
2565  * genphy_read_status_fixed - read the link parameters for !aneg mode
2566  * @phydev: target phy_device struct
2567  *
2568  * Read the current duplex and speed state for a PHY operating with
2569  * autonegotiation disabled.
2570  */
2571 int genphy_read_status_fixed(struct phy_device *phydev)
2572 {
2573 	int bmcr = phy_read(phydev, MII_BMCR);
2574 
2575 	if (bmcr < 0)
2576 		return bmcr;
2577 
2578 	if (bmcr & BMCR_FULLDPLX)
2579 		phydev->duplex = DUPLEX_FULL;
2580 	else
2581 		phydev->duplex = DUPLEX_HALF;
2582 
2583 	if (bmcr & BMCR_SPEED1000)
2584 		phydev->speed = SPEED_1000;
2585 	else if (bmcr & BMCR_SPEED100)
2586 		phydev->speed = SPEED_100;
2587 	else
2588 		phydev->speed = SPEED_10;
2589 
2590 	return 0;
2591 }
2592 EXPORT_SYMBOL(genphy_read_status_fixed);
2593 
2594 /**
2595  * genphy_read_status - check the link status and update current link state
2596  * @phydev: target phy_device struct
2597  *
2598  * Description: Check the link, then figure out the current state
2599  *   by comparing what we advertise with what the link partner
2600  *   advertises.  Start by checking the gigabit possibilities,
2601  *   then move on to 10/100.
2602  */
2603 int genphy_read_status(struct phy_device *phydev)
2604 {
2605 	int err, old_link = phydev->link;
2606 
2607 	/* Update the link, but return if there was an error */
2608 	err = genphy_update_link(phydev);
2609 	if (err)
2610 		return err;
2611 
2612 	/* why bother the PHY if nothing can have changed */
2613 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2614 		return 0;
2615 
2616 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2617 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2618 	phydev->speed = SPEED_UNKNOWN;
2619 	phydev->duplex = DUPLEX_UNKNOWN;
2620 	phydev->pause = 0;
2621 	phydev->asym_pause = 0;
2622 
2623 	if (phydev->is_gigabit_capable) {
2624 		err = genphy_read_master_slave(phydev);
2625 		if (err < 0)
2626 			return err;
2627 	}
2628 
2629 	err = genphy_read_lpa(phydev);
2630 	if (err < 0)
2631 		return err;
2632 
2633 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2634 		phy_resolve_aneg_linkmode(phydev);
2635 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2636 		err = genphy_read_status_fixed(phydev);
2637 		if (err < 0)
2638 			return err;
2639 	}
2640 
2641 	return 0;
2642 }
2643 EXPORT_SYMBOL(genphy_read_status);
2644 
2645 /**
2646  * genphy_c37_read_status - check the link status and update current link state
2647  * @phydev: target phy_device struct
2648  * @changed: pointer where to store if link changed
2649  *
2650  * Description: Check the link, then figure out the current state
2651  *   by comparing what we advertise with what the link partner
2652  *   advertises. This function is for Clause 37 1000Base-X mode.
2653  *
2654  *   If link has changed, @changed is set to true, false otherwise.
2655  */
2656 int genphy_c37_read_status(struct phy_device *phydev, bool *changed)
2657 {
2658 	int lpa, err, old_link = phydev->link;
2659 
2660 	/* Update the link, but return if there was an error */
2661 	err = genphy_update_link(phydev);
2662 	if (err)
2663 		return err;
2664 
2665 	/* why bother the PHY if nothing can have changed */
2666 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
2667 		*changed = false;
2668 		return 0;
2669 	}
2670 
2671 	/* Signal link has changed */
2672 	*changed = true;
2673 	phydev->duplex = DUPLEX_UNKNOWN;
2674 	phydev->pause = 0;
2675 	phydev->asym_pause = 0;
2676 
2677 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2678 		lpa = phy_read(phydev, MII_LPA);
2679 		if (lpa < 0)
2680 			return lpa;
2681 
2682 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2683 				 phydev->lp_advertising, lpa & LPA_LPACK);
2684 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2685 				 phydev->lp_advertising, lpa & LPA_1000XFULL);
2686 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2687 				 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2688 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2689 				 phydev->lp_advertising,
2690 				 lpa & LPA_1000XPAUSE_ASYM);
2691 
2692 		phy_resolve_aneg_linkmode(phydev);
2693 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2694 		int bmcr = phy_read(phydev, MII_BMCR);
2695 
2696 		if (bmcr < 0)
2697 			return bmcr;
2698 
2699 		if (bmcr & BMCR_FULLDPLX)
2700 			phydev->duplex = DUPLEX_FULL;
2701 		else
2702 			phydev->duplex = DUPLEX_HALF;
2703 	}
2704 
2705 	return 0;
2706 }
2707 EXPORT_SYMBOL(genphy_c37_read_status);
2708 
2709 /**
2710  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2711  * @phydev: target phy_device struct
2712  *
2713  * Description: Perform a software PHY reset using the standard
2714  * BMCR_RESET bit and poll for the reset bit to be cleared.
2715  *
2716  * Returns: 0 on success, < 0 on failure
2717  */
2718 int genphy_soft_reset(struct phy_device *phydev)
2719 {
2720 	u16 res = BMCR_RESET;
2721 	int ret;
2722 
2723 	if (phydev->autoneg == AUTONEG_ENABLE)
2724 		res |= BMCR_ANRESTART;
2725 
2726 	ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2727 	if (ret < 0)
2728 		return ret;
2729 
2730 	/* Clause 22 states that setting bit BMCR_RESET sets control registers
2731 	 * to their default value. Therefore the POWER DOWN bit is supposed to
2732 	 * be cleared after soft reset.
2733 	 */
2734 	phydev->suspended = 0;
2735 
2736 	ret = phy_poll_reset(phydev);
2737 	if (ret)
2738 		return ret;
2739 
2740 	/* BMCR may be reset to defaults */
2741 	if (phydev->autoneg == AUTONEG_DISABLE)
2742 		ret = genphy_setup_forced(phydev);
2743 
2744 	return ret;
2745 }
2746 EXPORT_SYMBOL(genphy_soft_reset);
2747 
2748 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
2749 {
2750 	/* It seems there are cases where the interrupts are handled by another
2751 	 * entity (ie an IRQ controller embedded inside the PHY) and do not
2752 	 * need any other interraction from phylib. In this case, just trigger
2753 	 * the state machine directly.
2754 	 */
2755 	phy_trigger_machine(phydev);
2756 
2757 	return 0;
2758 }
2759 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
2760 
2761 /**
2762  * genphy_read_abilities - read PHY abilities from Clause 22 registers
2763  * @phydev: target phy_device struct
2764  *
2765  * Description: Reads the PHY's abilities and populates
2766  * phydev->supported accordingly.
2767  *
2768  * Returns: 0 on success, < 0 on failure
2769  */
2770 int genphy_read_abilities(struct phy_device *phydev)
2771 {
2772 	int val;
2773 
2774 	linkmode_set_bit_array(phy_basic_ports_array,
2775 			       ARRAY_SIZE(phy_basic_ports_array),
2776 			       phydev->supported);
2777 
2778 	val = phy_read(phydev, MII_BMSR);
2779 	if (val < 0)
2780 		return val;
2781 
2782 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2783 			 val & BMSR_ANEGCAPABLE);
2784 
2785 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2786 			 val & BMSR_100FULL);
2787 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2788 			 val & BMSR_100HALF);
2789 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2790 			 val & BMSR_10FULL);
2791 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2792 			 val & BMSR_10HALF);
2793 
2794 	if (val & BMSR_ESTATEN) {
2795 		val = phy_read(phydev, MII_ESTATUS);
2796 		if (val < 0)
2797 			return val;
2798 
2799 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2800 				 phydev->supported, val & ESTATUS_1000_TFULL);
2801 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2802 				 phydev->supported, val & ESTATUS_1000_THALF);
2803 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2804 				 phydev->supported, val & ESTATUS_1000_XFULL);
2805 	}
2806 
2807 	/* This is optional functionality. If not supported, we may get an error
2808 	 * which should be ignored.
2809 	 */
2810 	genphy_c45_read_eee_abilities(phydev);
2811 
2812 	return 0;
2813 }
2814 EXPORT_SYMBOL(genphy_read_abilities);
2815 
2816 /* This is used for the phy device which doesn't support the MMD extended
2817  * register access, but it does have side effect when we are trying to access
2818  * the MMD register via indirect method.
2819  */
2820 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2821 {
2822 	return -EOPNOTSUPP;
2823 }
2824 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2825 
2826 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2827 				 u16 regnum, u16 val)
2828 {
2829 	return -EOPNOTSUPP;
2830 }
2831 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2832 
2833 int genphy_suspend(struct phy_device *phydev)
2834 {
2835 	return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2836 }
2837 EXPORT_SYMBOL(genphy_suspend);
2838 
2839 int genphy_resume(struct phy_device *phydev)
2840 {
2841 	return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2842 }
2843 EXPORT_SYMBOL(genphy_resume);
2844 
2845 int genphy_loopback(struct phy_device *phydev, bool enable)
2846 {
2847 	if (enable) {
2848 		u16 ctl = BMCR_LOOPBACK;
2849 		int ret, val;
2850 
2851 		ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2852 
2853 		phy_modify(phydev, MII_BMCR, ~0, ctl);
2854 
2855 		ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
2856 					    val & BMSR_LSTATUS,
2857 				    5000, 500000, true);
2858 		if (ret)
2859 			return ret;
2860 	} else {
2861 		phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
2862 
2863 		phy_config_aneg(phydev);
2864 	}
2865 
2866 	return 0;
2867 }
2868 EXPORT_SYMBOL(genphy_loopback);
2869 
2870 /**
2871  * phy_remove_link_mode - Remove a supported link mode
2872  * @phydev: phy_device structure to remove link mode from
2873  * @link_mode: Link mode to be removed
2874  *
2875  * Description: Some MACs don't support all link modes which the PHY
2876  * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
2877  * to remove a link mode.
2878  */
2879 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2880 {
2881 	linkmode_clear_bit(link_mode, phydev->supported);
2882 	phy_advertise_supported(phydev);
2883 }
2884 EXPORT_SYMBOL(phy_remove_link_mode);
2885 
2886 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2887 {
2888 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2889 		linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2890 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2891 		linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2892 }
2893 
2894 /**
2895  * phy_advertise_supported - Advertise all supported modes
2896  * @phydev: target phy_device struct
2897  *
2898  * Description: Called to advertise all supported modes, doesn't touch
2899  * pause mode advertising.
2900  */
2901 void phy_advertise_supported(struct phy_device *phydev)
2902 {
2903 	__ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2904 
2905 	linkmode_copy(new, phydev->supported);
2906 	phy_copy_pause_bits(new, phydev->advertising);
2907 	linkmode_copy(phydev->advertising, new);
2908 }
2909 EXPORT_SYMBOL(phy_advertise_supported);
2910 
2911 /**
2912  * phy_advertise_eee_all - Advertise all supported EEE modes
2913  * @phydev: target phy_device struct
2914  *
2915  * Description: Per default phylib preserves the EEE advertising at the time of
2916  * phy probing, which might be a subset of the supported EEE modes. Use this
2917  * function when all supported EEE modes should be advertised. This does not
2918  * trigger auto-negotiation, so must be called before phy_start()/
2919  * phylink_start() which will start auto-negotiation.
2920  */
2921 void phy_advertise_eee_all(struct phy_device *phydev)
2922 {
2923 	linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2924 }
2925 EXPORT_SYMBOL_GPL(phy_advertise_eee_all);
2926 
2927 /**
2928  * phy_support_eee - Set initial EEE policy configuration
2929  * @phydev: Target phy_device struct
2930  *
2931  * This function configures the initial policy for Energy Efficient Ethernet
2932  * (EEE) on the specified PHY device, influencing that EEE capabilities are
2933  * advertised before the link is established. It should be called during PHY
2934  * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
2935  * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
2936  * of the MAC.
2937  *
2938  * The function sets default EEE policy parameters, including preparing the PHY
2939  * to advertise EEE capabilities based on hardware support.
2940  *
2941  * It also sets the expected configuration for Low Power Idle (LPI) in the MAC
2942  * driver. If the PHY framework determines that both local and remote
2943  * advertisements support EEE, and the negotiated link mode is compatible with
2944  * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on
2945  * this setting by enabling the LPI timer if enable_tx_lpi is set.
2946  */
2947 void phy_support_eee(struct phy_device *phydev)
2948 {
2949 	linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2950 	phydev->eee_cfg.tx_lpi_enabled = true;
2951 	phydev->eee_cfg.eee_enabled = true;
2952 }
2953 EXPORT_SYMBOL(phy_support_eee);
2954 
2955 /**
2956  * phy_support_sym_pause - Enable support of symmetrical pause
2957  * @phydev: target phy_device struct
2958  *
2959  * Description: Called by the MAC to indicate is supports symmetrical
2960  * Pause, but not asym pause.
2961  */
2962 void phy_support_sym_pause(struct phy_device *phydev)
2963 {
2964 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2965 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2966 }
2967 EXPORT_SYMBOL(phy_support_sym_pause);
2968 
2969 /**
2970  * phy_support_asym_pause - Enable support of asym pause
2971  * @phydev: target phy_device struct
2972  *
2973  * Description: Called by the MAC to indicate is supports Asym Pause.
2974  */
2975 void phy_support_asym_pause(struct phy_device *phydev)
2976 {
2977 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2978 }
2979 EXPORT_SYMBOL(phy_support_asym_pause);
2980 
2981 /**
2982  * phy_set_sym_pause - Configure symmetric Pause
2983  * @phydev: target phy_device struct
2984  * @rx: Receiver Pause is supported
2985  * @tx: Transmit Pause is supported
2986  * @autoneg: Auto neg should be used
2987  *
2988  * Description: Configure advertised Pause support depending on if
2989  * receiver pause and pause auto neg is supported. Generally called
2990  * from the set_pauseparam .ndo.
2991  */
2992 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2993 		       bool autoneg)
2994 {
2995 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2996 
2997 	if (rx && tx && autoneg)
2998 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2999 				 phydev->supported);
3000 
3001 	linkmode_copy(phydev->advertising, phydev->supported);
3002 }
3003 EXPORT_SYMBOL(phy_set_sym_pause);
3004 
3005 /**
3006  * phy_set_asym_pause - Configure Pause and Asym Pause
3007  * @phydev: target phy_device struct
3008  * @rx: Receiver Pause is supported
3009  * @tx: Transmit Pause is supported
3010  *
3011  * Description: Configure advertised Pause support depending on if
3012  * transmit and receiver pause is supported. If there has been a
3013  * change in adverting, trigger a new autoneg. Generally called from
3014  * the set_pauseparam .ndo.
3015  */
3016 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
3017 {
3018 	__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
3019 
3020 	linkmode_copy(oldadv, phydev->advertising);
3021 	linkmode_set_pause(phydev->advertising, tx, rx);
3022 
3023 	if (!linkmode_equal(oldadv, phydev->advertising) &&
3024 	    phydev->autoneg)
3025 		phy_start_aneg(phydev);
3026 }
3027 EXPORT_SYMBOL(phy_set_asym_pause);
3028 
3029 /**
3030  * phy_validate_pause - Test if the PHY/MAC support the pause configuration
3031  * @phydev: phy_device struct
3032  * @pp: requested pause configuration
3033  *
3034  * Description: Test if the PHY/MAC combination supports the Pause
3035  * configuration the user is requesting. Returns True if it is
3036  * supported, false otherwise.
3037  */
3038 bool phy_validate_pause(struct phy_device *phydev,
3039 			struct ethtool_pauseparam *pp)
3040 {
3041 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3042 			       phydev->supported) && pp->rx_pause)
3043 		return false;
3044 
3045 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3046 			       phydev->supported) &&
3047 	    pp->rx_pause != pp->tx_pause)
3048 		return false;
3049 
3050 	return true;
3051 }
3052 EXPORT_SYMBOL(phy_validate_pause);
3053 
3054 /**
3055  * phy_get_pause - resolve negotiated pause modes
3056  * @phydev: phy_device struct
3057  * @tx_pause: pointer to bool to indicate whether transmit pause should be
3058  * enabled.
3059  * @rx_pause: pointer to bool to indicate whether receive pause should be
3060  * enabled.
3061  *
3062  * Resolve and return the flow control modes according to the negotiation
3063  * result. This includes checking that we are operating in full duplex mode.
3064  * See linkmode_resolve_pause() for further details.
3065  */
3066 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
3067 {
3068 	if (phydev->duplex != DUPLEX_FULL) {
3069 		*tx_pause = false;
3070 		*rx_pause = false;
3071 		return;
3072 	}
3073 
3074 	return linkmode_resolve_pause(phydev->advertising,
3075 				      phydev->lp_advertising,
3076 				      tx_pause, rx_pause);
3077 }
3078 EXPORT_SYMBOL(phy_get_pause);
3079 
3080 #if IS_ENABLED(CONFIG_OF_MDIO)
3081 static int phy_get_int_delay_property(struct device *dev, const char *name)
3082 {
3083 	s32 int_delay;
3084 	int ret;
3085 
3086 	ret = device_property_read_u32(dev, name, &int_delay);
3087 	if (ret)
3088 		return ret;
3089 
3090 	return int_delay;
3091 }
3092 #else
3093 static int phy_get_int_delay_property(struct device *dev, const char *name)
3094 {
3095 	return -EINVAL;
3096 }
3097 #endif
3098 
3099 /**
3100  * phy_get_internal_delay - returns the index of the internal delay
3101  * @phydev: phy_device struct
3102  * @dev: pointer to the devices device struct
3103  * @delay_values: array of delays the PHY supports
3104  * @size: the size of the delay array
3105  * @is_rx: boolean to indicate to get the rx internal delay
3106  *
3107  * Returns the index within the array of internal delay passed in.
3108  * If the device property is not present then the interface type is checked
3109  * if the interface defines use of internal delay then a 1 is returned otherwise
3110  * a 0 is returned.
3111  * The array must be in ascending order. If PHY does not have an ascending order
3112  * array then size = 0 and the value of the delay property is returned.
3113  * Return -EINVAL if the delay is invalid or cannot be found.
3114  */
3115 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
3116 			   const int *delay_values, int size, bool is_rx)
3117 {
3118 	s32 delay;
3119 	int i;
3120 
3121 	if (is_rx) {
3122 		delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
3123 		if (delay < 0 && size == 0) {
3124 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3125 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
3126 				return 1;
3127 			else
3128 				return 0;
3129 		}
3130 
3131 	} else {
3132 		delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
3133 		if (delay < 0 && size == 0) {
3134 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3135 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
3136 				return 1;
3137 			else
3138 				return 0;
3139 		}
3140 	}
3141 
3142 	if (delay < 0)
3143 		return delay;
3144 
3145 	if (size == 0)
3146 		return delay;
3147 
3148 	if (delay < delay_values[0] || delay > delay_values[size - 1]) {
3149 		phydev_err(phydev, "Delay %d is out of range\n", delay);
3150 		return -EINVAL;
3151 	}
3152 
3153 	if (delay == delay_values[0])
3154 		return 0;
3155 
3156 	for (i = 1; i < size; i++) {
3157 		if (delay == delay_values[i])
3158 			return i;
3159 
3160 		/* Find an approximate index by looking up the table */
3161 		if (delay > delay_values[i - 1] &&
3162 		    delay < delay_values[i]) {
3163 			if (delay - delay_values[i - 1] <
3164 			    delay_values[i] - delay)
3165 				return i - 1;
3166 			else
3167 				return i;
3168 		}
3169 	}
3170 
3171 	phydev_err(phydev, "error finding internal delay index for %d\n",
3172 		   delay);
3173 
3174 	return -EINVAL;
3175 }
3176 EXPORT_SYMBOL(phy_get_internal_delay);
3177 
3178 static int phy_led_set_brightness(struct led_classdev *led_cdev,
3179 				  enum led_brightness value)
3180 {
3181 	struct phy_led *phyled = to_phy_led(led_cdev);
3182 	struct phy_device *phydev = phyled->phydev;
3183 	int err;
3184 
3185 	mutex_lock(&phydev->lock);
3186 	err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
3187 	mutex_unlock(&phydev->lock);
3188 
3189 	return err;
3190 }
3191 
3192 static int phy_led_blink_set(struct led_classdev *led_cdev,
3193 			     unsigned long *delay_on,
3194 			     unsigned long *delay_off)
3195 {
3196 	struct phy_led *phyled = to_phy_led(led_cdev);
3197 	struct phy_device *phydev = phyled->phydev;
3198 	int err;
3199 
3200 	mutex_lock(&phydev->lock);
3201 	err = phydev->drv->led_blink_set(phydev, phyled->index,
3202 					 delay_on, delay_off);
3203 	mutex_unlock(&phydev->lock);
3204 
3205 	return err;
3206 }
3207 
3208 static __maybe_unused struct device *
3209 phy_led_hw_control_get_device(struct led_classdev *led_cdev)
3210 {
3211 	struct phy_led *phyled = to_phy_led(led_cdev);
3212 	struct phy_device *phydev = phyled->phydev;
3213 
3214 	if (phydev->attached_dev)
3215 		return &phydev->attached_dev->dev;
3216 	return NULL;
3217 }
3218 
3219 static int __maybe_unused
3220 phy_led_hw_control_get(struct led_classdev *led_cdev,
3221 		       unsigned long *rules)
3222 {
3223 	struct phy_led *phyled = to_phy_led(led_cdev);
3224 	struct phy_device *phydev = phyled->phydev;
3225 	int err;
3226 
3227 	mutex_lock(&phydev->lock);
3228 	err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
3229 	mutex_unlock(&phydev->lock);
3230 
3231 	return err;
3232 }
3233 
3234 static int __maybe_unused
3235 phy_led_hw_control_set(struct led_classdev *led_cdev,
3236 		       unsigned long rules)
3237 {
3238 	struct phy_led *phyled = to_phy_led(led_cdev);
3239 	struct phy_device *phydev = phyled->phydev;
3240 	int err;
3241 
3242 	mutex_lock(&phydev->lock);
3243 	err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
3244 	mutex_unlock(&phydev->lock);
3245 
3246 	return err;
3247 }
3248 
3249 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev,
3250 						  unsigned long rules)
3251 {
3252 	struct phy_led *phyled = to_phy_led(led_cdev);
3253 	struct phy_device *phydev = phyled->phydev;
3254 	int err;
3255 
3256 	mutex_lock(&phydev->lock);
3257 	err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
3258 	mutex_unlock(&phydev->lock);
3259 
3260 	return err;
3261 }
3262 
3263 static void phy_leds_unregister(struct phy_device *phydev)
3264 {
3265 	struct phy_led *phyled;
3266 
3267 	list_for_each_entry(phyled, &phydev->leds, list) {
3268 		led_classdev_unregister(&phyled->led_cdev);
3269 	}
3270 }
3271 
3272 static int of_phy_led(struct phy_device *phydev,
3273 		      struct device_node *led)
3274 {
3275 	struct device *dev = &phydev->mdio.dev;
3276 	struct led_init_data init_data = {};
3277 	struct led_classdev *cdev;
3278 	unsigned long modes = 0;
3279 	struct phy_led *phyled;
3280 	u32 index;
3281 	int err;
3282 
3283 	phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
3284 	if (!phyled)
3285 		return -ENOMEM;
3286 
3287 	cdev = &phyled->led_cdev;
3288 	phyled->phydev = phydev;
3289 
3290 	err = of_property_read_u32(led, "reg", &index);
3291 	if (err)
3292 		return err;
3293 	if (index > U8_MAX)
3294 		return -EINVAL;
3295 
3296 	if (of_property_read_bool(led, "active-low"))
3297 		set_bit(PHY_LED_ACTIVE_LOW, &modes);
3298 	if (of_property_read_bool(led, "inactive-high-impedance"))
3299 		set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
3300 
3301 	if (modes) {
3302 		/* Return error if asked to set polarity modes but not supported */
3303 		if (!phydev->drv->led_polarity_set)
3304 			return -EINVAL;
3305 
3306 		err = phydev->drv->led_polarity_set(phydev, index, modes);
3307 		if (err)
3308 			return err;
3309 	}
3310 
3311 	phyled->index = index;
3312 	if (phydev->drv->led_brightness_set)
3313 		cdev->brightness_set_blocking = phy_led_set_brightness;
3314 	if (phydev->drv->led_blink_set)
3315 		cdev->blink_set = phy_led_blink_set;
3316 
3317 #ifdef CONFIG_LEDS_TRIGGERS
3318 	if (phydev->drv->led_hw_is_supported &&
3319 	    phydev->drv->led_hw_control_set &&
3320 	    phydev->drv->led_hw_control_get) {
3321 		cdev->hw_control_is_supported = phy_led_hw_is_supported;
3322 		cdev->hw_control_set = phy_led_hw_control_set;
3323 		cdev->hw_control_get = phy_led_hw_control_get;
3324 		cdev->hw_control_trigger = "netdev";
3325 	}
3326 
3327 	cdev->hw_control_get_device = phy_led_hw_control_get_device;
3328 #endif
3329 	cdev->max_brightness = 1;
3330 	init_data.devicename = dev_name(&phydev->mdio.dev);
3331 	init_data.fwnode = of_fwnode_handle(led);
3332 	init_data.devname_mandatory = true;
3333 
3334 	err = led_classdev_register_ext(dev, cdev, &init_data);
3335 	if (err)
3336 		return err;
3337 
3338 	list_add(&phyled->list, &phydev->leds);
3339 
3340 	return 0;
3341 }
3342 
3343 static int of_phy_leds(struct phy_device *phydev)
3344 {
3345 	struct device_node *node = phydev->mdio.dev.of_node;
3346 	struct device_node *leds, *led;
3347 	int err;
3348 
3349 	if (!IS_ENABLED(CONFIG_OF_MDIO))
3350 		return 0;
3351 
3352 	if (!node)
3353 		return 0;
3354 
3355 	leds = of_get_child_by_name(node, "leds");
3356 	if (!leds)
3357 		return 0;
3358 
3359 	for_each_available_child_of_node(leds, led) {
3360 		err = of_phy_led(phydev, led);
3361 		if (err) {
3362 			of_node_put(led);
3363 			phy_leds_unregister(phydev);
3364 			return err;
3365 		}
3366 	}
3367 
3368 	return 0;
3369 }
3370 
3371 /**
3372  * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
3373  * @fwnode: pointer to the mdio_device's fwnode
3374  *
3375  * If successful, returns a pointer to the mdio_device with the embedded
3376  * struct device refcount incremented by one, or NULL on failure.
3377  * The caller should call put_device() on the mdio_device after its use.
3378  */
3379 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
3380 {
3381 	struct device *d;
3382 
3383 	if (!fwnode)
3384 		return NULL;
3385 
3386 	d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode);
3387 	if (!d)
3388 		return NULL;
3389 
3390 	return to_mdio_device(d);
3391 }
3392 EXPORT_SYMBOL(fwnode_mdio_find_device);
3393 
3394 /**
3395  * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
3396  *
3397  * @phy_fwnode: Pointer to the phy's fwnode.
3398  *
3399  * If successful, returns a pointer to the phy_device with the embedded
3400  * struct device refcount incremented by one, or NULL on failure.
3401  */
3402 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
3403 {
3404 	struct mdio_device *mdiodev;
3405 
3406 	mdiodev = fwnode_mdio_find_device(phy_fwnode);
3407 	if (!mdiodev)
3408 		return NULL;
3409 
3410 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
3411 		return to_phy_device(&mdiodev->dev);
3412 
3413 	put_device(&mdiodev->dev);
3414 
3415 	return NULL;
3416 }
3417 EXPORT_SYMBOL(fwnode_phy_find_device);
3418 
3419 /**
3420  * device_phy_find_device - For the given device, get the phy_device
3421  * @dev: Pointer to the given device
3422  *
3423  * Refer return conditions of fwnode_phy_find_device().
3424  */
3425 struct phy_device *device_phy_find_device(struct device *dev)
3426 {
3427 	return fwnode_phy_find_device(dev_fwnode(dev));
3428 }
3429 EXPORT_SYMBOL_GPL(device_phy_find_device);
3430 
3431 /**
3432  * fwnode_get_phy_node - Get the phy_node using the named reference.
3433  * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
3434  *
3435  * Refer return conditions of fwnode_find_reference().
3436  * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
3437  * and "phy-device" are not supported in ACPI. DT supports all the three
3438  * named references to the phy node.
3439  */
3440 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode)
3441 {
3442 	struct fwnode_handle *phy_node;
3443 
3444 	/* Only phy-handle is used for ACPI */
3445 	phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
3446 	if (is_acpi_node(fwnode) || !IS_ERR(phy_node))
3447 		return phy_node;
3448 	phy_node = fwnode_find_reference(fwnode, "phy", 0);
3449 	if (IS_ERR(phy_node))
3450 		phy_node = fwnode_find_reference(fwnode, "phy-device", 0);
3451 	return phy_node;
3452 }
3453 EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
3454 
3455 /**
3456  * phy_probe - probe and init a PHY device
3457  * @dev: device to probe and init
3458  *
3459  * Take care of setting up the phy_device structure, set the state to READY.
3460  */
3461 static int phy_probe(struct device *dev)
3462 {
3463 	struct phy_device *phydev = to_phy_device(dev);
3464 	struct device_driver *drv = phydev->mdio.dev.driver;
3465 	struct phy_driver *phydrv = to_phy_driver(drv);
3466 	int err = 0;
3467 
3468 	phydev->drv = phydrv;
3469 
3470 	/* Disable the interrupt if the PHY doesn't support it
3471 	 * but the interrupt is still a valid one
3472 	 */
3473 	if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
3474 		phydev->irq = PHY_POLL;
3475 
3476 	if (phydrv->flags & PHY_IS_INTERNAL)
3477 		phydev->is_internal = true;
3478 
3479 	/* Deassert the reset signal */
3480 	phy_device_reset(phydev, 0);
3481 
3482 	if (phydev->drv->probe) {
3483 		err = phydev->drv->probe(phydev);
3484 		if (err)
3485 			goto out;
3486 	}
3487 
3488 	phy_disable_interrupts(phydev);
3489 
3490 	/* Start out supporting everything. Eventually,
3491 	 * a controller will attach, and may modify one
3492 	 * or both of these values
3493 	 */
3494 	if (phydrv->features) {
3495 		linkmode_copy(phydev->supported, phydrv->features);
3496 		genphy_c45_read_eee_abilities(phydev);
3497 	}
3498 	else if (phydrv->get_features)
3499 		err = phydrv->get_features(phydev);
3500 	else if (phydev->is_c45)
3501 		err = genphy_c45_pma_read_abilities(phydev);
3502 	else
3503 		err = genphy_read_abilities(phydev);
3504 
3505 	if (err)
3506 		goto out;
3507 
3508 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3509 			       phydev->supported))
3510 		phydev->autoneg = 0;
3511 
3512 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
3513 			      phydev->supported))
3514 		phydev->is_gigabit_capable = 1;
3515 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
3516 			      phydev->supported))
3517 		phydev->is_gigabit_capable = 1;
3518 
3519 	of_set_phy_supported(phydev);
3520 	phy_advertise_supported(phydev);
3521 
3522 	/* Get PHY default EEE advertising modes and handle them as potentially
3523 	 * safe initial configuration.
3524 	 */
3525 	err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
3526 	if (err)
3527 		goto out;
3528 
3529 	/* There is no "enabled" flag. If PHY is advertising, assume it is
3530 	 * kind of enabled.
3531 	 */
3532 	phydev->eee_enabled = !linkmode_empty(phydev->advertising_eee);
3533 
3534 	/* Some PHYs may advertise, by default, not support EEE modes. So,
3535 	 * we need to clean them.
3536 	 */
3537 	if (phydev->eee_enabled)
3538 		linkmode_and(phydev->advertising_eee, phydev->supported_eee,
3539 			     phydev->advertising_eee);
3540 
3541 	/* Get the EEE modes we want to prohibit. We will ask
3542 	 * the PHY stop advertising these mode later on
3543 	 */
3544 	of_set_phy_eee_broken(phydev);
3545 
3546 	/* The Pause Frame bits indicate that the PHY can support passing
3547 	 * pause frames. During autonegotiation, the PHYs will determine if
3548 	 * they should allow pause frames to pass.  The MAC driver should then
3549 	 * use that result to determine whether to enable flow control via
3550 	 * pause frames.
3551 	 *
3552 	 * Normally, PHY drivers should not set the Pause bits, and instead
3553 	 * allow phylib to do that.  However, there may be some situations
3554 	 * (e.g. hardware erratum) where the driver wants to set only one
3555 	 * of these bits.
3556 	 */
3557 	if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
3558 	    !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
3559 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3560 				 phydev->supported);
3561 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3562 				 phydev->supported);
3563 	}
3564 
3565 	/* Set the state to READY by default */
3566 	phydev->state = PHY_READY;
3567 
3568 	/* Get the LEDs from the device tree, and instantiate standard
3569 	 * LEDs for them.
3570 	 */
3571 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3572 		err = of_phy_leds(phydev);
3573 
3574 out:
3575 	/* Re-assert the reset signal on error */
3576 	if (err)
3577 		phy_device_reset(phydev, 1);
3578 
3579 	return err;
3580 }
3581 
3582 static int phy_remove(struct device *dev)
3583 {
3584 	struct phy_device *phydev = to_phy_device(dev);
3585 
3586 	cancel_delayed_work_sync(&phydev->state_queue);
3587 
3588 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3589 		phy_leds_unregister(phydev);
3590 
3591 	phydev->state = PHY_DOWN;
3592 
3593 	sfp_bus_del_upstream(phydev->sfp_bus);
3594 	phydev->sfp_bus = NULL;
3595 
3596 	if (phydev->drv && phydev->drv->remove)
3597 		phydev->drv->remove(phydev);
3598 
3599 	/* Assert the reset signal */
3600 	phy_device_reset(phydev, 1);
3601 
3602 	phydev->drv = NULL;
3603 
3604 	return 0;
3605 }
3606 
3607 /**
3608  * phy_driver_register - register a phy_driver with the PHY layer
3609  * @new_driver: new phy_driver to register
3610  * @owner: module owning this PHY
3611  */
3612 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
3613 {
3614 	int retval;
3615 
3616 	/* Either the features are hard coded, or dynamically
3617 	 * determined. It cannot be both.
3618 	 */
3619 	if (WARN_ON(new_driver->features && new_driver->get_features)) {
3620 		pr_err("%s: features and get_features must not both be set\n",
3621 		       new_driver->name);
3622 		return -EINVAL;
3623 	}
3624 
3625 	/* PHYLIB device drivers must not match using a DT compatible table
3626 	 * as this bypasses our checks that the mdiodev that is being matched
3627 	 * is backed by a struct phy_device. If such a case happens, we will
3628 	 * make out-of-bounds accesses and lockup in phydev->lock.
3629 	 */
3630 	if (WARN(new_driver->mdiodrv.driver.of_match_table,
3631 		 "%s: driver must not provide a DT match table\n",
3632 		 new_driver->name))
3633 		return -EINVAL;
3634 
3635 	new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3636 	new_driver->mdiodrv.driver.name = new_driver->name;
3637 	new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3638 	new_driver->mdiodrv.driver.probe = phy_probe;
3639 	new_driver->mdiodrv.driver.remove = phy_remove;
3640 	new_driver->mdiodrv.driver.owner = owner;
3641 	new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3642 
3643 	retval = driver_register(&new_driver->mdiodrv.driver);
3644 	if (retval) {
3645 		pr_err("%s: Error %d in registering driver\n",
3646 		       new_driver->name, retval);
3647 
3648 		return retval;
3649 	}
3650 
3651 	pr_debug("%s: Registered new driver\n", new_driver->name);
3652 
3653 	return 0;
3654 }
3655 EXPORT_SYMBOL(phy_driver_register);
3656 
3657 int phy_drivers_register(struct phy_driver *new_driver, int n,
3658 			 struct module *owner)
3659 {
3660 	int i, ret = 0;
3661 
3662 	for (i = 0; i < n; i++) {
3663 		ret = phy_driver_register(new_driver + i, owner);
3664 		if (ret) {
3665 			while (i-- > 0)
3666 				phy_driver_unregister(new_driver + i);
3667 			break;
3668 		}
3669 	}
3670 	return ret;
3671 }
3672 EXPORT_SYMBOL(phy_drivers_register);
3673 
3674 void phy_driver_unregister(struct phy_driver *drv)
3675 {
3676 	driver_unregister(&drv->mdiodrv.driver);
3677 }
3678 EXPORT_SYMBOL(phy_driver_unregister);
3679 
3680 void phy_drivers_unregister(struct phy_driver *drv, int n)
3681 {
3682 	int i;
3683 
3684 	for (i = 0; i < n; i++)
3685 		phy_driver_unregister(drv + i);
3686 }
3687 EXPORT_SYMBOL(phy_drivers_unregister);
3688 
3689 static struct phy_driver genphy_driver = {
3690 	.phy_id		= 0xffffffff,
3691 	.phy_id_mask	= 0xffffffff,
3692 	.name		= "Generic PHY",
3693 	.get_features	= genphy_read_abilities,
3694 	.suspend	= genphy_suspend,
3695 	.resume		= genphy_resume,
3696 	.set_loopback   = genphy_loopback,
3697 };
3698 
3699 static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
3700 	.get_sset_count		= phy_ethtool_get_sset_count,
3701 	.get_strings		= phy_ethtool_get_strings,
3702 	.get_stats		= phy_ethtool_get_stats,
3703 	.get_plca_cfg		= phy_ethtool_get_plca_cfg,
3704 	.set_plca_cfg		= phy_ethtool_set_plca_cfg,
3705 	.get_plca_status	= phy_ethtool_get_plca_status,
3706 	.start_cable_test	= phy_start_cable_test,
3707 	.start_cable_test_tdr	= phy_start_cable_test_tdr,
3708 };
3709 
3710 static const struct phylib_stubs __phylib_stubs = {
3711 	.hwtstamp_get = __phy_hwtstamp_get,
3712 	.hwtstamp_set = __phy_hwtstamp_set,
3713 };
3714 
3715 static void phylib_register_stubs(void)
3716 {
3717 	phylib_stubs = &__phylib_stubs;
3718 }
3719 
3720 static void phylib_unregister_stubs(void)
3721 {
3722 	phylib_stubs = NULL;
3723 }
3724 
3725 static int __init phy_init(void)
3726 {
3727 	int rc;
3728 
3729 	rtnl_lock();
3730 	ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
3731 	phylib_register_stubs();
3732 	rtnl_unlock();
3733 
3734 	rc = mdio_bus_init();
3735 	if (rc)
3736 		goto err_ethtool_phy_ops;
3737 
3738 	features_init();
3739 
3740 	rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
3741 	if (rc)
3742 		goto err_mdio_bus;
3743 
3744 	rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3745 	if (rc)
3746 		goto err_c45;
3747 
3748 	return 0;
3749 
3750 err_c45:
3751 	phy_driver_unregister(&genphy_c45_driver);
3752 err_mdio_bus:
3753 	mdio_bus_exit();
3754 err_ethtool_phy_ops:
3755 	rtnl_lock();
3756 	phylib_unregister_stubs();
3757 	ethtool_set_ethtool_phy_ops(NULL);
3758 	rtnl_unlock();
3759 
3760 	return rc;
3761 }
3762 
3763 static void __exit phy_exit(void)
3764 {
3765 	phy_driver_unregister(&genphy_c45_driver);
3766 	phy_driver_unregister(&genphy_driver);
3767 	mdio_bus_exit();
3768 	rtnl_lock();
3769 	phylib_unregister_stubs();
3770 	ethtool_set_ethtool_phy_ops(NULL);
3771 	rtnl_unlock();
3772 }
3773 
3774 subsys_initcall(phy_init);
3775 module_exit(phy_exit);
3776