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