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