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