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