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