xref: /linux/drivers/net/phy/phy_device.c (revision 9e7cd33fafb2ce1fd6cf19fd20f486ea341e6c0c)
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 		 */
1831 		hwprov = rtnl_dereference(dev->hwprov);
1832 		/* Disable timestamp if it is the one selected */
1833 		if (hwprov && hwprov->phydev == phydev) {
1834 			rcu_assign_pointer(dev->hwprov, NULL);
1835 			kfree_rcu(hwprov, rcu_head);
1836 		}
1837 
1838 		phydev->attached_dev->phydev = NULL;
1839 		phydev->attached_dev = NULL;
1840 		phy_link_topo_del_phy(dev, phydev);
1841 		if (phydev->mod_port)
1842 			phy_link_topo_del_port(dev, phydev->mod_port);
1843 	}
1844 
1845 	phydev->phy_link_change = NULL;
1846 	phydev->phylink = NULL;
1847 
1848 	module_put(phydev->drv_owner);
1849 	phydev->drv_owner = NULL;
1850 
1851 	/* If the device had no specific driver before (i.e. - it
1852 	 * was using the generic driver), we unbind the device
1853 	 * from the generic driver so that there's a chance a
1854 	 * real driver could be loaded
1855 	 */
1856 	if (phydev->is_genphy_driven) {
1857 		/* The release below lets phy_probe() write this field. */
1858 		phydev->irq = phydev->mdio.bus->irq[phydev->mdio.addr];
1859 		device_release_driver(&phydev->mdio.dev);
1860 		phydev->is_genphy_driven = 0;
1861 	}
1862 
1863 	/* Assert the reset signal */
1864 	phy_device_reset(phydev, 1);
1865 
1866 	/*
1867 	 * The phydev might go away on the put_device() below, so avoid
1868 	 * a use-after-free bug by reading the underlying bus first.
1869 	 */
1870 	bus = phydev->mdio.bus;
1871 
1872 	put_device(&phydev->mdio.dev);
1873 	if (dev)
1874 		ndev_owner = dev->dev.parent->driver->owner;
1875 	if (ndev_owner != bus->owner)
1876 		module_put(bus->owner);
1877 }
1878 
1879 /**
1880  * phy_detach - detach a PHY device from its network device
1881  * @phydev: target phy_device struct
1882  *
1883  * This detaches the phy device from its network device and the phy
1884  * driver, and drops the reference count taken in phy_attach_direct().
1885  */
1886 void phy_detach(struct phy_device *phydev)
1887 {
1888 	/* cleanup including bus notification */
1889 	phy_detach_internal(phydev, true);
1890 }
1891 EXPORT_SYMBOL(phy_detach);
1892 
1893 /**
1894  * phy_attach_direct - attach a network device to a given PHY device pointer
1895  * @dev: network device to attach
1896  * @phydev: Pointer to phy_device to attach
1897  * @flags: PHY device's dev_flags
1898  * @interface: PHY device's interface
1899  *
1900  * Description: Called by drivers to attach to a particular PHY
1901  *     device. The phy_device is found, and properly hooked up
1902  *     to the phy_driver.  If no driver is attached, then a
1903  *     generic driver is used.  The phy_device is given a ptr to
1904  *     the attaching device, and given a callback for link status
1905  *     change.  The phy_device is returned to the attaching driver.
1906  *     This function takes a reference on the phy device.
1907  */
1908 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1909 		      u32 flags, phy_interface_t interface)
1910 {
1911 	struct mii_bus *bus = phydev->mdio.bus;
1912 	struct device *d = &phydev->mdio.dev;
1913 	struct module *ndev_owner = NULL;
1914 	struct module *drv_owner;
1915 	int irq = phydev->irq;
1916 	int err;
1917 
1918 	/* For Ethernet device drivers that register their own MDIO bus, we
1919 	 * will have bus->owner match ndev_mod, so we do not want to increment
1920 	 * our own module->refcnt here, otherwise we would not be able to
1921 	 * unload later on.
1922 	 */
1923 	if (dev)
1924 		ndev_owner = dev->dev.parent->driver->owner;
1925 	if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1926 		phydev_err(phydev, "failed to get the bus module\n");
1927 		return -EIO;
1928 	}
1929 
1930 	get_device(d);
1931 
1932 	/* Set by every attach, with or without a netdev */
1933 	if (phydev->phy_link_change) {
1934 		phydev_err(phydev, "PHY already attached\n");
1935 		err = -EBUSY;
1936 		goto error_put_device;
1937 	}
1938 
1939 	/* Assume that if there is no driver, that it doesn't
1940 	 * exist, and we should use the genphy driver.
1941 	 */
1942 	if (!d->driver) {
1943 		if (phydev->is_c45)
1944 			d->driver = &genphy_c45_driver.mdiodrv.driver;
1945 		else
1946 			d->driver = &genphy_driver.mdiodrv.driver;
1947 
1948 		phydev->is_genphy_driven = 1;
1949 	}
1950 
1951 	drv_owner = d->driver->owner;
1952 	if (!try_module_get(drv_owner)) {
1953 		phydev_err(phydev, "failed to get the device driver module\n");
1954 		err = -EIO;
1955 		goto error_put_device;
1956 	}
1957 	phydev->drv_owner = drv_owner;
1958 
1959 	if (phydev->is_genphy_driven) {
1960 		err = d->driver->probe(d);
1961 		if (err >= 0)
1962 			err = device_bind_driver(d);
1963 
1964 		if (err)
1965 			goto error_module_put;
1966 	}
1967 
1968 	phydev->phy_link_change = phy_link_change;
1969 	if (dev) {
1970 		phydev->attached_dev = dev;
1971 		dev->phydev = phydev;
1972 
1973 		if (phydev->sfp_bus_attached)
1974 			dev->sfp_bus = phydev->sfp_bus;
1975 
1976 		err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev);
1977 		if (err)
1978 			goto error;
1979 
1980 		if (phydev->mod_port) {
1981 			err = phy_link_topo_add_port(dev, phydev->mod_port);
1982 			if (err)
1983 				goto error;
1984 		}
1985 	}
1986 
1987 	/* Some Ethernet drivers try to connect to a PHY device before
1988 	 * calling register_netdevice() -> netdev_register_kobject() and
1989 	 * does the dev->dev.kobj initialization. Here we only check for
1990 	 * success which indicates that the network device kobject is
1991 	 * ready. Once we do that we still need to keep track of whether
1992 	 * links were successfully set up or not for phy_detach() to
1993 	 * remove them accordingly.
1994 	 */
1995 	phydev->sysfs_links = false;
1996 
1997 	phy_sysfs_create_links(phydev);
1998 
1999 	if (!phydev->attached_dev) {
2000 		err = sysfs_create_file(&phydev->mdio.dev.kobj,
2001 					&dev_attr_phy_standalone.attr);
2002 		if (err)
2003 			phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
2004 	}
2005 
2006 	phydev->dev_flags |= flags;
2007 
2008 	phydev->interface = interface;
2009 
2010 	phydev->state = PHY_READY;
2011 
2012 	phydev->interrupts = PHY_INTERRUPT_DISABLED;
2013 
2014 	/* PHYs can request to use poll mode even though they have an
2015 	 * associated interrupt line. This could be the case if they
2016 	 * detect a broken interrupt handling.
2017 	 */
2018 	if (phydev->dev_flags & PHY_F_NO_IRQ)
2019 		phydev->irq = PHY_POLL;
2020 
2021 	if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev))
2022 		phydev->irq = PHY_POLL;
2023 
2024 	/* Port is set to PORT_TP by default and the actual PHY driver will set
2025 	 * it to different value depending on the PHY configuration. If we have
2026 	 * the generic PHY driver we can't figure it out, thus set the old
2027 	 * legacy PORT_MII value.
2028 	 */
2029 	if (phydev->is_genphy_driven)
2030 		phydev->port = PORT_MII;
2031 
2032 	/* Initial carrier state is off as the phy is about to be
2033 	 * (re)initialized.
2034 	 */
2035 	if (dev)
2036 		netif_carrier_off(phydev->attached_dev);
2037 
2038 	/* Do initial configuration here, now that
2039 	 * we have certain key parameters
2040 	 * (dev_flags and interface)
2041 	 */
2042 	err = phy_init_hw(phydev);
2043 	if (err)
2044 		goto error;
2045 
2046 	if (phydev->mdio.bus->notify_phy_attach) {
2047 		err = phydev->mdio.bus->notify_phy_attach(phydev);
2048 		if (err)
2049 			goto error;
2050 	}
2051 
2052 	phy_resume(phydev);
2053 
2054 	/**
2055 	 * If the external phy used by current mac interface is managed by
2056 	 * another mac interface, so we should create a device link between
2057 	 * phy dev and mac dev.
2058 	 */
2059 	if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
2060 		phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
2061 						  DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
2062 
2063 	return err;
2064 
2065 error:
2066 	/* phy_detach_internal() does all of the cleanup below */
2067 	phy_detach_internal(phydev, false);
2068 	return err;
2069 
2070 error_module_put:
2071 	module_put(phydev->drv_owner);
2072 	phydev->drv_owner = NULL;
2073 	phydev->irq = irq;
2074 	phydev->is_genphy_driven = 0;
2075 	d->driver = NULL;
2076 error_put_device:
2077 	put_device(d);
2078 	if (ndev_owner != bus->owner)
2079 		module_put(bus->owner);
2080 	return err;
2081 }
2082 EXPORT_SYMBOL(phy_attach_direct);
2083 
2084 int phy_suspend(struct phy_device *phydev)
2085 {
2086 	struct net_device *netdev = phydev->attached_dev;
2087 	const struct phy_driver *phydrv = phydev->drv;
2088 	int ret;
2089 
2090 	if (phydev->suspended || !phydrv)
2091 		return 0;
2092 
2093 	phydev->wol_enabled = phy_may_wakeup(phydev) ||
2094 			      (netdev && netdev->ethtool->wol_enabled);
2095 	/* If the device has WOL enabled, we cannot suspend the PHY */
2096 	if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND))
2097 		return -EBUSY;
2098 
2099 	if (!phydrv->suspend)
2100 		return 0;
2101 
2102 	ret = phydrv->suspend(phydev);
2103 	if (!ret)
2104 		phydev->suspended = true;
2105 
2106 	return ret;
2107 }
2108 EXPORT_SYMBOL(phy_suspend);
2109 
2110 int __phy_resume(struct phy_device *phydev)
2111 {
2112 	const struct phy_driver *phydrv = phydev->drv;
2113 	int ret;
2114 
2115 	lockdep_assert_held(&phydev->lock);
2116 
2117 	if (!phydrv || !phydrv->resume)
2118 		return 0;
2119 
2120 	ret = phydrv->resume(phydev);
2121 	if (!ret)
2122 		phydev->suspended = false;
2123 
2124 	return ret;
2125 }
2126 EXPORT_SYMBOL(__phy_resume);
2127 
2128 int phy_resume(struct phy_device *phydev)
2129 {
2130 	int ret;
2131 
2132 	mutex_lock(&phydev->lock);
2133 	ret = __phy_resume(phydev);
2134 	mutex_unlock(&phydev->lock);
2135 
2136 	return ret;
2137 }
2138 EXPORT_SYMBOL(phy_resume);
2139 
2140 /**
2141  * phy_reset_after_clk_enable - perform a PHY reset if needed
2142  * @phydev: target phy_device struct
2143  *
2144  * Description: Some PHYs are known to need a reset after their refclk was
2145  *   enabled. This function evaluates the flags and perform the reset if it's
2146  *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
2147  *   was reset.
2148  */
2149 int phy_reset_after_clk_enable(struct phy_device *phydev)
2150 {
2151 	if (!phydev || !phydev->drv)
2152 		return -ENODEV;
2153 
2154 	if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
2155 		phy_device_reset(phydev, 1);
2156 		phy_device_reset(phydev, 0);
2157 		return 1;
2158 	}
2159 
2160 	return 0;
2161 }
2162 EXPORT_SYMBOL(phy_reset_after_clk_enable);
2163 
2164 /* Generic PHY support and helper functions */
2165 
2166 /**
2167  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
2168  * @phydev: target phy_device struct
2169  * @advert: auto-negotiation parameters to advertise
2170  *
2171  * Description: Writes MII_ADVERTISE with the appropriate values,
2172  *   after sanitizing the values to make sure we only advertise
2173  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
2174  *   hasn't changed, and > 0 if it has changed.
2175  */
2176 static int genphy_config_advert(struct phy_device *phydev,
2177 				const unsigned long *advert)
2178 {
2179 	int err, bmsr, changed = 0;
2180 	u32 adv;
2181 
2182 	adv = linkmode_adv_to_mii_adv_t(advert);
2183 
2184 	/* Setup standard advertisement */
2185 	err = phy_modify_changed(phydev, MII_ADVERTISE,
2186 				 ADVERTISE_ALL | ADVERTISE_100BASE4 |
2187 				 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
2188 				 adv);
2189 	if (err < 0)
2190 		return err;
2191 	if (err > 0)
2192 		changed = 1;
2193 
2194 	bmsr = phy_read(phydev, MII_BMSR);
2195 	if (bmsr < 0)
2196 		return bmsr;
2197 
2198 	/* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
2199 	 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
2200 	 * logical 1.
2201 	 */
2202 	if (!(bmsr & BMSR_ESTATEN))
2203 		return changed;
2204 
2205 	adv = linkmode_adv_to_mii_ctrl1000_t(advert);
2206 
2207 	err = phy_modify_changed(phydev, MII_CTRL1000,
2208 				 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
2209 				 adv);
2210 	if (err < 0)
2211 		return err;
2212 	if (err > 0)
2213 		changed = 1;
2214 
2215 	return changed;
2216 }
2217 
2218 /**
2219  * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
2220  * @phydev: target phy_device struct
2221  *
2222  * Description: Writes MII_ADVERTISE with the appropriate values,
2223  *   after sanitizing the values to make sure we only advertise
2224  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
2225  *   hasn't changed, and > 0 if it has changed. This function is intended
2226  *   for Clause 37 1000Base-X mode.
2227  */
2228 static int genphy_c37_config_advert(struct phy_device *phydev)
2229 {
2230 	u16 adv = 0;
2231 
2232 	/* Only allow advertising what this PHY supports */
2233 	linkmode_and(phydev->advertising, phydev->advertising,
2234 		     phydev->supported);
2235 
2236 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2237 			      phydev->advertising))
2238 		adv |= ADVERTISE_1000XFULL;
2239 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2240 			      phydev->advertising))
2241 		adv |= ADVERTISE_1000XPAUSE;
2242 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2243 			      phydev->advertising))
2244 		adv |= ADVERTISE_1000XPSE_ASYM;
2245 
2246 	return phy_modify_changed(phydev, MII_ADVERTISE,
2247 				  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
2248 				  ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
2249 				  adv);
2250 }
2251 
2252 /**
2253  * genphy_setup_forced - configures/forces speed/duplex from @phydev
2254  * @phydev: target phy_device struct
2255  *
2256  * Description: Configures MII_BMCR to force speed/duplex
2257  *   to the values in phydev. Assumes that the values are valid.
2258  *   Please see phy_sanitize_settings().
2259  */
2260 int genphy_setup_forced(struct phy_device *phydev)
2261 {
2262 	u16 ctl;
2263 
2264 	phydev->pause = false;
2265 	phydev->asym_pause = false;
2266 
2267 	ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2268 
2269 	return phy_modify(phydev, MII_BMCR,
2270 			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
2271 }
2272 EXPORT_SYMBOL(genphy_setup_forced);
2273 
2274 static int genphy_setup_master_slave(struct phy_device *phydev)
2275 {
2276 	u16 ctl = 0;
2277 
2278 	if (!phydev->is_gigabit_capable)
2279 		return 0;
2280 
2281 	switch (phydev->master_slave_set) {
2282 	case MASTER_SLAVE_CFG_MASTER_PREFERRED:
2283 		ctl |= CTL1000_PREFER_MASTER;
2284 		break;
2285 	case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2286 		break;
2287 	case MASTER_SLAVE_CFG_MASTER_FORCE:
2288 		ctl |= CTL1000_AS_MASTER;
2289 		fallthrough;
2290 	case MASTER_SLAVE_CFG_SLAVE_FORCE:
2291 		ctl |= CTL1000_ENABLE_MASTER;
2292 		break;
2293 	case MASTER_SLAVE_CFG_UNKNOWN:
2294 	case MASTER_SLAVE_CFG_UNSUPPORTED:
2295 		return 0;
2296 	default:
2297 		phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2298 		return -EOPNOTSUPP;
2299 	}
2300 
2301 	return phy_modify_changed(phydev, MII_CTRL1000,
2302 				  (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2303 				   CTL1000_PREFER_MASTER), ctl);
2304 }
2305 
2306 int genphy_read_master_slave(struct phy_device *phydev)
2307 {
2308 	int cfg, state;
2309 	int val;
2310 
2311 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2312 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2313 
2314 	val = phy_read(phydev, MII_CTRL1000);
2315 	if (val < 0)
2316 		return val;
2317 
2318 	if (val & CTL1000_ENABLE_MASTER) {
2319 		if (val & CTL1000_AS_MASTER)
2320 			cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2321 		else
2322 			cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2323 	} else {
2324 		if (val & CTL1000_PREFER_MASTER)
2325 			cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2326 		else
2327 			cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2328 	}
2329 
2330 	val = phy_read(phydev, MII_STAT1000);
2331 	if (val < 0)
2332 		return val;
2333 
2334 	if (val & LPA_1000MSFAIL) {
2335 		state = MASTER_SLAVE_STATE_ERR;
2336 	} else if (phydev->link) {
2337 		/* this bits are valid only for active link */
2338 		if (val & LPA_1000MSRES)
2339 			state = MASTER_SLAVE_STATE_MASTER;
2340 		else
2341 			state = MASTER_SLAVE_STATE_SLAVE;
2342 	} else {
2343 		state = MASTER_SLAVE_STATE_UNKNOWN;
2344 	}
2345 
2346 	phydev->master_slave_get = cfg;
2347 	phydev->master_slave_state = state;
2348 
2349 	return 0;
2350 }
2351 EXPORT_SYMBOL(genphy_read_master_slave);
2352 
2353 /**
2354  * genphy_restart_aneg - Enable and Restart Autonegotiation
2355  * @phydev: target phy_device struct
2356  */
2357 int genphy_restart_aneg(struct phy_device *phydev)
2358 {
2359 	/* Don't isolate the PHY if we're negotiating */
2360 	return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2361 			  BMCR_ANENABLE | BMCR_ANRESTART);
2362 }
2363 EXPORT_SYMBOL(genphy_restart_aneg);
2364 
2365 /**
2366  * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2367  * @phydev: target phy_device struct
2368  * @restart: whether aneg restart is requested
2369  *
2370  * Check, and restart auto-negotiation if needed.
2371  */
2372 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2373 {
2374 	int ret;
2375 
2376 	if (!restart) {
2377 		/* Advertisement hasn't changed, but maybe aneg was never on to
2378 		 * begin with?  Or maybe phy was isolated?
2379 		 */
2380 		ret = phy_read(phydev, MII_BMCR);
2381 		if (ret < 0)
2382 			return ret;
2383 
2384 		if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2385 			restart = true;
2386 	}
2387 
2388 	if (restart)
2389 		return genphy_restart_aneg(phydev);
2390 
2391 	return 0;
2392 }
2393 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2394 
2395 /**
2396  * __genphy_config_aneg - restart auto-negotiation or write BMCR
2397  * @phydev: target phy_device struct
2398  * @changed: whether autoneg is requested
2399  *
2400  * Description: If auto-negotiation is enabled, we configure the
2401  *   advertising, and then restart auto-negotiation.  If it is not
2402  *   enabled, then we write the BMCR.
2403  */
2404 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2405 {
2406 	__ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert);
2407 	const struct link_capabilities *c;
2408 	unsigned long *advert;
2409 	int err;
2410 
2411 	err = genphy_c45_an_config_eee_aneg(phydev);
2412 	if (err < 0)
2413 		return err;
2414 	else if (err)
2415 		changed = true;
2416 
2417 	err = genphy_setup_master_slave(phydev);
2418 	if (err < 0)
2419 		return err;
2420 	else if (err)
2421 		changed = true;
2422 
2423 	if (phydev->autoneg == AUTONEG_ENABLE) {
2424 		/* Only allow advertising what this PHY supports */
2425 		linkmode_and(phydev->advertising, phydev->advertising,
2426 			     phydev->supported);
2427 		advert = phydev->advertising;
2428 	} else if (phydev->speed < SPEED_1000) {
2429 		return genphy_setup_forced(phydev);
2430 	} else {
2431 		linkmode_zero(fixed_advert);
2432 
2433 		c = phy_caps_lookup(phydev->speed, phydev->duplex,
2434 				    phydev->supported, true);
2435 		if (c)
2436 			linkmode_and(fixed_advert, phydev->supported,
2437 				     c->linkmodes);
2438 
2439 		advert = fixed_advert;
2440 	}
2441 
2442 	err = genphy_config_advert(phydev, advert);
2443 	if (err < 0) /* error */
2444 		return err;
2445 	else if (err)
2446 		changed = true;
2447 
2448 	return genphy_check_and_restart_aneg(phydev, changed);
2449 }
2450 EXPORT_SYMBOL(__genphy_config_aneg);
2451 
2452 /**
2453  * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2454  * @phydev: target phy_device struct
2455  *
2456  * Description: If auto-negotiation is enabled, we configure the
2457  *   advertising, and then restart auto-negotiation.  If it is not
2458  *   enabled, then we write the BMCR. This function is intended
2459  *   for use with Clause 37 1000Base-X mode.
2460  */
2461 int genphy_c37_config_aneg(struct phy_device *phydev)
2462 {
2463 	int err, changed;
2464 
2465 	if (phydev->autoneg != AUTONEG_ENABLE)
2466 		return genphy_setup_forced(phydev);
2467 
2468 	err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2469 			 BMCR_SPEED1000);
2470 	if (err)
2471 		return err;
2472 
2473 	changed = genphy_c37_config_advert(phydev);
2474 	if (changed < 0) /* error */
2475 		return changed;
2476 
2477 	if (!changed) {
2478 		/* Advertisement hasn't changed, but maybe aneg was never on to
2479 		 * begin with?  Or maybe phy was isolated?
2480 		 */
2481 		int ctl = phy_read(phydev, MII_BMCR);
2482 
2483 		if (ctl < 0)
2484 			return ctl;
2485 
2486 		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2487 			changed = 1; /* do restart aneg */
2488 	}
2489 
2490 	/* Only restart aneg if we are advertising something different
2491 	 * than we were before.
2492 	 */
2493 	if (changed > 0)
2494 		return genphy_restart_aneg(phydev);
2495 
2496 	return 0;
2497 }
2498 EXPORT_SYMBOL(genphy_c37_config_aneg);
2499 
2500 /**
2501  * genphy_aneg_done - return auto-negotiation status
2502  * @phydev: target phy_device struct
2503  *
2504  * Description: Reads the status register and returns 0 either if
2505  *   auto-negotiation is incomplete, or if there was an error.
2506  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2507  */
2508 int genphy_aneg_done(struct phy_device *phydev)
2509 {
2510 	int retval = phy_read(phydev, MII_BMSR);
2511 
2512 	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2513 }
2514 EXPORT_SYMBOL(genphy_aneg_done);
2515 
2516 /**
2517  * genphy_update_link - update link status in @phydev
2518  * @phydev: target phy_device struct
2519  *
2520  * Description: Update the value in phydev->link to reflect the
2521  *   current link value.  In order to do this, we need to read
2522  *   the status register twice, keeping the second value.
2523  */
2524 int genphy_update_link(struct phy_device *phydev)
2525 {
2526 	int status = 0, bmcr;
2527 
2528 	bmcr = phy_read(phydev, MII_BMCR);
2529 	if (bmcr < 0)
2530 		return bmcr;
2531 
2532 	/* Autoneg is being started, therefore disregard BMSR value and
2533 	 * report link as down.
2534 	 */
2535 	if (bmcr & BMCR_ANRESTART)
2536 		goto done;
2537 
2538 	/* The link state is latched low so that momentary link
2539 	 * drops can be detected. Do not double-read the status
2540 	 * in polling mode to detect such short link drops except
2541 	 * if the link was already down.
2542 	 */
2543 	if (!phy_polling_mode(phydev) || !phydev->link) {
2544 		status = phy_read(phydev, MII_BMSR);
2545 		if (status < 0)
2546 			return status;
2547 		else if (status & BMSR_LSTATUS)
2548 			goto done;
2549 	}
2550 
2551 	/* Read link and autonegotiation status */
2552 	status = phy_read(phydev, MII_BMSR);
2553 	if (status < 0)
2554 		return status;
2555 done:
2556 	phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2557 	phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2558 
2559 	/* Consider the case that autoneg was started and "aneg complete"
2560 	 * bit has been reset, but "link up" bit not yet.
2561 	 */
2562 	if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2563 		phydev->link = 0;
2564 
2565 	return 0;
2566 }
2567 EXPORT_SYMBOL(genphy_update_link);
2568 
2569 int genphy_read_lpa(struct phy_device *phydev)
2570 {
2571 	int lpa, lpagb;
2572 
2573 	if (phydev->autoneg == AUTONEG_ENABLE) {
2574 		if (!phydev->autoneg_complete) {
2575 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2576 							0);
2577 			mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2578 			return 0;
2579 		}
2580 
2581 		if (phydev->is_gigabit_capable) {
2582 			lpagb = phy_read(phydev, MII_STAT1000);
2583 			if (lpagb < 0)
2584 				return lpagb;
2585 
2586 			if (lpagb & LPA_1000MSFAIL) {
2587 				int adv = phy_read(phydev, MII_CTRL1000);
2588 
2589 				if (adv < 0)
2590 					return adv;
2591 
2592 				if (adv & CTL1000_ENABLE_MASTER)
2593 					phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2594 				else
2595 					phydev_err(phydev, "Master/Slave resolution failed\n");
2596 				return -ENOLINK;
2597 			}
2598 
2599 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2600 							lpagb);
2601 		}
2602 
2603 		lpa = phy_read(phydev, MII_LPA);
2604 		if (lpa < 0)
2605 			return lpa;
2606 
2607 		mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2608 	} else {
2609 		linkmode_zero(phydev->lp_advertising);
2610 	}
2611 
2612 	return 0;
2613 }
2614 EXPORT_SYMBOL(genphy_read_lpa);
2615 
2616 /**
2617  * genphy_read_status_fixed - read the link parameters for !aneg mode
2618  * @phydev: target phy_device struct
2619  *
2620  * Read the current duplex and speed state for a PHY operating with
2621  * autonegotiation disabled.
2622  */
2623 int genphy_read_status_fixed(struct phy_device *phydev)
2624 {
2625 	int bmcr = phy_read(phydev, MII_BMCR);
2626 
2627 	if (bmcr < 0)
2628 		return bmcr;
2629 
2630 	if (bmcr & BMCR_FULLDPLX)
2631 		phydev->duplex = DUPLEX_FULL;
2632 	else
2633 		phydev->duplex = DUPLEX_HALF;
2634 
2635 	if (bmcr & BMCR_SPEED1000)
2636 		phydev->speed = SPEED_1000;
2637 	else if (bmcr & BMCR_SPEED100)
2638 		phydev->speed = SPEED_100;
2639 	else
2640 		phydev->speed = SPEED_10;
2641 
2642 	return 0;
2643 }
2644 EXPORT_SYMBOL(genphy_read_status_fixed);
2645 
2646 /**
2647  * genphy_read_status - check the link status and update current link state
2648  * @phydev: target phy_device struct
2649  *
2650  * Description: Check the link, then figure out the current state
2651  *   by comparing what we advertise with what the link partner
2652  *   advertises.  Start by checking the gigabit possibilities,
2653  *   then move on to 10/100.
2654  */
2655 int genphy_read_status(struct phy_device *phydev)
2656 {
2657 	int err, old_link = phydev->link;
2658 
2659 	/* Update the link, but return if there was an error */
2660 	err = genphy_update_link(phydev);
2661 	if (err)
2662 		return err;
2663 
2664 	/* why bother the PHY if nothing can have changed */
2665 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2666 		return 0;
2667 
2668 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2669 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2670 	phydev->speed = SPEED_UNKNOWN;
2671 	phydev->duplex = DUPLEX_UNKNOWN;
2672 	phydev->pause = false;
2673 	phydev->asym_pause = false;
2674 
2675 	if (phydev->is_gigabit_capable) {
2676 		err = genphy_read_master_slave(phydev);
2677 		if (err < 0)
2678 			return err;
2679 	}
2680 
2681 	err = genphy_read_lpa(phydev);
2682 	if (err < 0)
2683 		return err;
2684 
2685 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2686 		phy_resolve_aneg_linkmode(phydev);
2687 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2688 		err = genphy_read_status_fixed(phydev);
2689 		if (err < 0)
2690 			return err;
2691 	}
2692 
2693 	return 0;
2694 }
2695 EXPORT_SYMBOL(genphy_read_status);
2696 
2697 /**
2698  * genphy_c37_read_status - check the link status and update current link state
2699  * @phydev: target phy_device struct
2700  * @changed: pointer where to store if link changed
2701  *
2702  * Description: Check the link, then figure out the current state
2703  *   by comparing what we advertise with what the link partner
2704  *   advertises. This function is for Clause 37 1000Base-X mode.
2705  *
2706  *   If link has changed, @changed is set to true, false otherwise.
2707  */
2708 int genphy_c37_read_status(struct phy_device *phydev, bool *changed)
2709 {
2710 	int lpa, err, old_link = phydev->link;
2711 
2712 	/* Update the link, but return if there was an error */
2713 	err = genphy_update_link(phydev);
2714 	if (err)
2715 		return err;
2716 
2717 	/* why bother the PHY if nothing can have changed */
2718 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
2719 		*changed = false;
2720 		return 0;
2721 	}
2722 
2723 	/* Signal link has changed */
2724 	*changed = true;
2725 	phydev->duplex = DUPLEX_UNKNOWN;
2726 	phydev->pause = false;
2727 	phydev->asym_pause = false;
2728 
2729 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2730 		lpa = phy_read(phydev, MII_LPA);
2731 		if (lpa < 0)
2732 			return lpa;
2733 
2734 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2735 				 phydev->lp_advertising, lpa & LPA_LPACK);
2736 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2737 				 phydev->lp_advertising, lpa & LPA_1000XFULL);
2738 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2739 				 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2740 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2741 				 phydev->lp_advertising,
2742 				 lpa & LPA_1000XPAUSE_ASYM);
2743 
2744 		phy_resolve_aneg_linkmode(phydev);
2745 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2746 		int bmcr = phy_read(phydev, MII_BMCR);
2747 
2748 		if (bmcr < 0)
2749 			return bmcr;
2750 
2751 		if (bmcr & BMCR_FULLDPLX)
2752 			phydev->duplex = DUPLEX_FULL;
2753 		else
2754 			phydev->duplex = DUPLEX_HALF;
2755 	}
2756 
2757 	return 0;
2758 }
2759 EXPORT_SYMBOL(genphy_c37_read_status);
2760 
2761 /**
2762  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2763  * @phydev: target phy_device struct
2764  *
2765  * Description: Perform a software PHY reset using the standard
2766  * BMCR_RESET bit and poll for the reset bit to be cleared.
2767  *
2768  * Returns: 0 on success, < 0 on failure
2769  */
2770 int genphy_soft_reset(struct phy_device *phydev)
2771 {
2772 	u16 res = BMCR_RESET;
2773 	int ret;
2774 
2775 	if (phydev->autoneg == AUTONEG_ENABLE)
2776 		res |= BMCR_ANRESTART;
2777 
2778 	ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2779 	if (ret < 0)
2780 		return ret;
2781 
2782 	/* Clause 22 states that setting bit BMCR_RESET sets control registers
2783 	 * to their default value. Therefore the POWER DOWN bit is supposed to
2784 	 * be cleared after soft reset.
2785 	 */
2786 	phydev->suspended = 0;
2787 
2788 	ret = phy_poll_reset(phydev);
2789 	if (ret)
2790 		return ret;
2791 
2792 	/* BMCR may be reset to defaults */
2793 	if (phydev->autoneg == AUTONEG_DISABLE)
2794 		ret = genphy_setup_forced(phydev);
2795 
2796 	return ret;
2797 }
2798 EXPORT_SYMBOL(genphy_soft_reset);
2799 
2800 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
2801 {
2802 	/* It seems there are cases where the interrupts are handled by another
2803 	 * entity (ie an IRQ controller embedded inside the PHY) and do not
2804 	 * need any other interraction from phylib. In this case, just trigger
2805 	 * the state machine directly.
2806 	 */
2807 	phy_trigger_machine(phydev);
2808 
2809 	return 0;
2810 }
2811 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
2812 
2813 /**
2814  * genphy_read_abilities - read PHY abilities from Clause 22 registers
2815  * @phydev: target phy_device struct
2816  *
2817  * Description: Reads the PHY's abilities and populates
2818  * phydev->supported accordingly.
2819  *
2820  * Returns: 0 on success, < 0 on failure
2821  */
2822 int genphy_read_abilities(struct phy_device *phydev)
2823 {
2824 	int val;
2825 
2826 	linkmode_set_bit_array(phy_basic_ports_array,
2827 			       ARRAY_SIZE(phy_basic_ports_array),
2828 			       phydev->supported);
2829 
2830 	val = phy_read(phydev, MII_BMSR);
2831 	if (val < 0)
2832 		return val;
2833 
2834 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2835 			 val & BMSR_ANEGCAPABLE);
2836 
2837 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2838 			 val & BMSR_100FULL);
2839 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2840 			 val & BMSR_100HALF);
2841 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2842 			 val & BMSR_10FULL);
2843 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2844 			 val & BMSR_10HALF);
2845 
2846 	if (val & BMSR_ESTATEN) {
2847 		val = phy_read(phydev, MII_ESTATUS);
2848 		if (val < 0)
2849 			return val;
2850 
2851 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2852 				 phydev->supported, val & ESTATUS_1000_TFULL);
2853 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2854 				 phydev->supported, val & ESTATUS_1000_THALF);
2855 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2856 				 phydev->supported, val & ESTATUS_1000_XFULL);
2857 	}
2858 
2859 	/* This is optional functionality. If not supported, we may get an error
2860 	 * which should be ignored.
2861 	 */
2862 	genphy_c45_read_eee_abilities(phydev);
2863 
2864 	return 0;
2865 }
2866 EXPORT_SYMBOL(genphy_read_abilities);
2867 
2868 /* Some PHYs support direct C45 register access but not C22 indirect
2869  * MMD access (registers 13 and 14). When discovered via C22, phylib
2870  * routes MMD access through the indirect path, which won't work on
2871  * these devices. These helpers bypass indirect access and use the bus
2872  * C45 accessors directly.
2873  */
2874 int genphy_read_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum)
2875 {
2876 	struct mii_bus *bus = phydev->mdio.bus;
2877 	int addr = phydev->mdio.addr;
2878 
2879 	return __mdiobus_c45_read(bus, addr, devnum, regnum);
2880 }
2881 EXPORT_SYMBOL(genphy_read_mmd_c45);
2882 
2883 int genphy_write_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum,
2884 			 u16 val)
2885 {
2886 	struct mii_bus *bus = phydev->mdio.bus;
2887 	int addr = phydev->mdio.addr;
2888 
2889 	return __mdiobus_c45_write(bus, addr, devnum, regnum, val);
2890 }
2891 EXPORT_SYMBOL(genphy_write_mmd_c45);
2892 
2893 /* This is used for the phy device which doesn't support the MMD extended
2894  * register access, but it does have side effect when we are trying to access
2895  * the MMD register via indirect method.
2896  */
2897 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2898 {
2899 	return -EOPNOTSUPP;
2900 }
2901 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2902 
2903 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2904 				 u16 regnum, u16 val)
2905 {
2906 	return -EOPNOTSUPP;
2907 }
2908 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2909 
2910 int genphy_suspend(struct phy_device *phydev)
2911 {
2912 	return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2913 }
2914 EXPORT_SYMBOL(genphy_suspend);
2915 
2916 int genphy_resume(struct phy_device *phydev)
2917 {
2918 	return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2919 }
2920 EXPORT_SYMBOL(genphy_resume);
2921 
2922 int genphy_loopback(struct phy_device *phydev, bool enable, int speed)
2923 {
2924 	if (enable) {
2925 		u16 ctl = BMCR_LOOPBACK;
2926 		int ret, val;
2927 
2928 		if (speed == SPEED_10 || speed == SPEED_100 ||
2929 		    speed == SPEED_1000)
2930 			phydev->speed = speed;
2931 		else if (speed)
2932 			return -EINVAL;
2933 
2934 		ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2935 
2936 		phy_modify(phydev, MII_BMCR, ~0, ctl);
2937 
2938 		ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
2939 					    val & BMSR_LSTATUS,
2940 				    5000, 500000, true);
2941 		if (ret)
2942 			return ret;
2943 	} else {
2944 		phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
2945 
2946 		phy_config_aneg(phydev);
2947 	}
2948 
2949 	return 0;
2950 }
2951 EXPORT_SYMBOL(genphy_loopback);
2952 
2953 /**
2954  * phy_remove_link_mode - Remove a supported link mode
2955  * @phydev: phy_device structure to remove link mode from
2956  * @link_mode: Link mode to be removed
2957  *
2958  * Description: Some MACs don't support all link modes which the PHY
2959  * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
2960  * to remove a link mode.
2961  */
2962 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2963 {
2964 	linkmode_clear_bit(link_mode, phydev->supported);
2965 	phy_advertise_supported(phydev);
2966 }
2967 EXPORT_SYMBOL(phy_remove_link_mode);
2968 
2969 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2970 {
2971 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2972 		linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2973 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2974 		linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2975 }
2976 
2977 /**
2978  * phy_advertise_supported - Advertise all supported modes
2979  * @phydev: target phy_device struct
2980  *
2981  * Description: Called to advertise all supported modes, doesn't touch
2982  * pause mode advertising.
2983  */
2984 void phy_advertise_supported(struct phy_device *phydev)
2985 {
2986 	__ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2987 
2988 	linkmode_copy(new, phydev->supported);
2989 	phy_copy_pause_bits(new, phydev->advertising);
2990 	linkmode_copy(phydev->advertising, new);
2991 }
2992 EXPORT_SYMBOL(phy_advertise_supported);
2993 
2994 /**
2995  * phy_advertise_eee_all - Advertise all supported EEE modes
2996  * @phydev: target phy_device struct
2997  *
2998  * Description: Per default phylib preserves the EEE advertising at the time of
2999  * phy probing, which might be a subset of the supported EEE modes. Use this
3000  * function when all supported EEE modes should be advertised. This does not
3001  * trigger auto-negotiation, so must be called before phy_start()/
3002  * phylink_start() which will start auto-negotiation.
3003  */
3004 void phy_advertise_eee_all(struct phy_device *phydev)
3005 {
3006 	linkmode_andnot(phydev->advertising_eee, phydev->supported_eee,
3007 			phydev->eee_disabled_modes);
3008 }
3009 EXPORT_SYMBOL_GPL(phy_advertise_eee_all);
3010 
3011 /**
3012  * phy_support_eee - Set initial EEE policy configuration
3013  * @phydev: Target phy_device struct
3014  *
3015  * This function configures the initial policy for Energy Efficient Ethernet
3016  * (EEE) on the specified PHY device, influencing that EEE capabilities are
3017  * advertised before the link is established. It should be called during PHY
3018  * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
3019  * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
3020  * of the MAC.
3021  *
3022  * The function sets default EEE policy parameters, including preparing the PHY
3023  * to advertise EEE capabilities based on hardware support.
3024  *
3025  * It also sets the expected configuration for Low Power Idle (LPI) in the MAC
3026  * driver. If the PHY framework determines that both local and remote
3027  * advertisements support EEE, and the negotiated link mode is compatible with
3028  * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on
3029  * this setting by enabling the LPI timer if enable_tx_lpi is set.
3030  */
3031 void phy_support_eee(struct phy_device *phydev)
3032 {
3033 	linkmode_andnot(phydev->advertising_eee, phydev->supported_eee,
3034 			phydev->eee_disabled_modes);
3035 	phydev->eee_cfg.tx_lpi_enabled = true;
3036 	phydev->eee_cfg.eee_enabled = true;
3037 
3038 	/* If the PHY supports autonomous EEE, disable it so the MAC can
3039 	 * manage LPI signaling instead. The flag is stored so it can be
3040 	 * re-applied after a PHY soft reset (e.g. suspend/resume).
3041 	 */
3042 	if (phydev->drv && phydev->drv->disable_autonomous_eee) {
3043 		int ret = phydev->drv->disable_autonomous_eee(phydev);
3044 
3045 		if (ret)
3046 			phydev_warn(phydev, "Failed to disable autonomous EEE: %pe\n",
3047 				    ERR_PTR(ret));
3048 		else
3049 			phydev->autonomous_eee_disabled = true;
3050 	}
3051 }
3052 EXPORT_SYMBOL(phy_support_eee);
3053 
3054 /**
3055  * phy_disable_eee - Disable EEE for the PHY
3056  * @phydev: Target phy_device struct
3057  *
3058  * This function is used by MAC drivers for MAC's which don't support EEE.
3059  * It disables EEE on the PHY layer.
3060  */
3061 void phy_disable_eee(struct phy_device *phydev)
3062 {
3063 	linkmode_zero(phydev->advertising_eee);
3064 	phydev->eee_cfg.tx_lpi_enabled = false;
3065 	phydev->eee_cfg.eee_enabled = false;
3066 	/* don't let userspace re-enable EEE advertisement */
3067 	linkmode_fill(phydev->eee_disabled_modes);
3068 }
3069 EXPORT_SYMBOL_GPL(phy_disable_eee);
3070 
3071 /**
3072  * phy_support_sym_pause - Enable support of symmetrical pause
3073  * @phydev: target phy_device struct
3074  *
3075  * Description: Called by the MAC to indicate is supports symmetrical
3076  * Pause, but not asym pause.
3077  */
3078 void phy_support_sym_pause(struct phy_device *phydev)
3079 {
3080 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
3081 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
3082 }
3083 EXPORT_SYMBOL(phy_support_sym_pause);
3084 
3085 /**
3086  * phy_support_asym_pause - Enable support of asym pause
3087  * @phydev: target phy_device struct
3088  *
3089  * Description: Called by the MAC to indicate is supports Asym Pause.
3090  */
3091 void phy_support_asym_pause(struct phy_device *phydev)
3092 {
3093 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
3094 }
3095 EXPORT_SYMBOL(phy_support_asym_pause);
3096 
3097 /**
3098  * phy_set_sym_pause - Configure symmetric Pause
3099  * @phydev: target phy_device struct
3100  * @rx: Receiver Pause is supported
3101  * @tx: Transmit Pause is supported
3102  * @autoneg: Auto neg should be used
3103  *
3104  * Description: Configure advertised Pause support depending on if
3105  * receiver pause and pause auto neg is supported. Generally called
3106  * from the set_pauseparam .ndo.
3107  */
3108 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
3109 		       bool autoneg)
3110 {
3111 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
3112 
3113 	if (rx && tx && autoneg)
3114 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3115 				 phydev->supported);
3116 
3117 	linkmode_copy(phydev->advertising, phydev->supported);
3118 }
3119 EXPORT_SYMBOL(phy_set_sym_pause);
3120 
3121 /**
3122  * phy_set_asym_pause - Configure Pause and Asym Pause
3123  * @phydev: target phy_device struct
3124  * @rx: Receiver Pause is supported
3125  * @tx: Transmit Pause is supported
3126  *
3127  * Description: Configure advertised Pause support depending on if
3128  * transmit and receiver pause is supported. If there has been a
3129  * change in adverting, trigger a new autoneg. Generally called from
3130  * the set_pauseparam .ndo.
3131  */
3132 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
3133 {
3134 	__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
3135 
3136 	linkmode_copy(oldadv, phydev->advertising);
3137 	linkmode_set_pause(phydev->advertising, tx, rx);
3138 
3139 	if (!linkmode_equal(oldadv, phydev->advertising) &&
3140 	    phydev->autoneg)
3141 		phy_start_aneg(phydev);
3142 }
3143 EXPORT_SYMBOL(phy_set_asym_pause);
3144 
3145 /**
3146  * phy_validate_pause - Test if the PHY/MAC support the pause configuration
3147  * @phydev: phy_device struct
3148  * @pp: requested pause configuration
3149  *
3150  * Description: Test if the PHY/MAC combination supports the Pause
3151  * configuration the user is requesting. Returns True if it is
3152  * supported, false otherwise.
3153  */
3154 bool phy_validate_pause(struct phy_device *phydev,
3155 			struct ethtool_pauseparam *pp)
3156 {
3157 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3158 			       phydev->supported) && pp->rx_pause)
3159 		return false;
3160 
3161 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3162 			       phydev->supported) &&
3163 	    pp->rx_pause != pp->tx_pause)
3164 		return false;
3165 
3166 	return true;
3167 }
3168 EXPORT_SYMBOL(phy_validate_pause);
3169 
3170 /**
3171  * phy_get_pause - resolve negotiated pause modes
3172  * @phydev: phy_device struct
3173  * @tx_pause: pointer to bool to indicate whether transmit pause should be
3174  * enabled.
3175  * @rx_pause: pointer to bool to indicate whether receive pause should be
3176  * enabled.
3177  *
3178  * Resolve and return the flow control modes according to the negotiation
3179  * result. This includes checking that we are operating in full duplex mode.
3180  * See linkmode_resolve_pause() for further details.
3181  */
3182 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
3183 {
3184 	if (phydev->duplex != DUPLEX_FULL) {
3185 		*tx_pause = false;
3186 		*rx_pause = false;
3187 		return;
3188 	}
3189 
3190 	return linkmode_resolve_pause(phydev->advertising,
3191 				      phydev->lp_advertising,
3192 				      tx_pause, rx_pause);
3193 }
3194 EXPORT_SYMBOL(phy_get_pause);
3195 
3196 #if IS_ENABLED(CONFIG_OF_MDIO)
3197 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val)
3198 {
3199 	return device_property_read_u32(dev, name, val);
3200 }
3201 #else
3202 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val)
3203 {
3204 	return -EINVAL;
3205 }
3206 #endif
3207 
3208 /**
3209  * phy_get_internal_delay - returns the index of the internal delay
3210  * @phydev: phy_device struct
3211  * @delay_values: array of delays the PHY supports
3212  * @size: the size of the delay array
3213  * @is_rx: boolean to indicate to get the rx internal delay
3214  *
3215  * Returns the index within the array of internal delay passed in.
3216  * If the device property is not present then the interface type is checked
3217  * if the interface defines use of internal delay then a 1 is returned otherwise
3218  * a 0 is returned.
3219  * The array must be in ascending order. If PHY does not have an ascending order
3220  * array then size = 0 and the value of the delay property is returned.
3221  * Return -EINVAL if the delay is invalid or cannot be found.
3222  */
3223 s32 phy_get_internal_delay(struct phy_device *phydev, const int *delay_values,
3224 			   int size, bool is_rx)
3225 {
3226 	struct device *dev = &phydev->mdio.dev;
3227 	int i, ret;
3228 	u32 delay;
3229 
3230 	if (is_rx) {
3231 		ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay);
3232 		if (ret < 0 && size == 0) {
3233 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3234 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
3235 				return 1;
3236 			else
3237 				return 0;
3238 		}
3239 
3240 	} else {
3241 		ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay);
3242 		if (ret < 0 && size == 0) {
3243 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3244 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
3245 				return 1;
3246 			else
3247 				return 0;
3248 		}
3249 	}
3250 
3251 	if (ret < 0)
3252 		return ret;
3253 
3254 	if (size == 0)
3255 		return delay;
3256 
3257 	if (delay < delay_values[0] || delay > delay_values[size - 1]) {
3258 		phydev_err(phydev, "Delay %d is out of range\n", delay);
3259 		return -EINVAL;
3260 	}
3261 
3262 	if (delay == delay_values[0])
3263 		return 0;
3264 
3265 	for (i = 1; i < size; i++) {
3266 		if (delay == delay_values[i])
3267 			return i;
3268 
3269 		/* Find an approximate index by looking up the table */
3270 		if (delay > delay_values[i - 1] &&
3271 		    delay < delay_values[i]) {
3272 			if (delay - delay_values[i - 1] <
3273 			    delay_values[i] - delay)
3274 				return i - 1;
3275 			else
3276 				return i;
3277 		}
3278 	}
3279 
3280 	phydev_err(phydev, "error finding internal delay index for %d\n",
3281 		   delay);
3282 
3283 	return -EINVAL;
3284 }
3285 EXPORT_SYMBOL(phy_get_internal_delay);
3286 
3287 /**
3288  * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val
3289  * @phydev: phy_device struct
3290  * @dev: pointer to the devices device struct
3291  * @linkmode: linkmode for which the tx amplitude gain should be retrieved
3292  * @val: tx amplitude gain
3293  *
3294  * Returns: 0 on success, < 0 on failure
3295  */
3296 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev,
3297 			      enum ethtool_link_mode_bit_indices linkmode,
3298 			      u32 *val)
3299 {
3300 	switch (linkmode) {
3301 	case ETHTOOL_LINK_MODE_100baseT_Full_BIT:
3302 		return phy_get_u32_property(dev,
3303 					    "tx-amplitude-100base-tx-percent",
3304 					    val);
3305 	default:
3306 		return -EINVAL;
3307 	}
3308 }
3309 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain);
3310 
3311 /**
3312  * phy_get_mac_termination - stores MAC termination in @val
3313  * @phydev: phy_device struct
3314  * @dev: pointer to the devices device struct
3315  * @val: MAC termination
3316  *
3317  * Returns: 0 on success, < 0 on failure
3318  */
3319 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev,
3320 			    u32 *val)
3321 {
3322 	return phy_get_u32_property(dev, "mac-termination-ohms", val);
3323 }
3324 EXPORT_SYMBOL_GPL(phy_get_mac_termination);
3325 
3326 static int phy_led_set_brightness(struct led_classdev *led_cdev,
3327 				  enum led_brightness value)
3328 {
3329 	struct phy_led *phyled = to_phy_led(led_cdev);
3330 	struct phy_device *phydev = phyled->phydev;
3331 	int err;
3332 
3333 	mutex_lock(&phydev->lock);
3334 	err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
3335 	mutex_unlock(&phydev->lock);
3336 
3337 	return err;
3338 }
3339 
3340 static int phy_led_blink_set(struct led_classdev *led_cdev,
3341 			     unsigned long *delay_on,
3342 			     unsigned long *delay_off)
3343 {
3344 	struct phy_led *phyled = to_phy_led(led_cdev);
3345 	struct phy_device *phydev = phyled->phydev;
3346 	int err;
3347 
3348 	mutex_lock(&phydev->lock);
3349 	err = phydev->drv->led_blink_set(phydev, phyled->index,
3350 					 delay_on, delay_off);
3351 	mutex_unlock(&phydev->lock);
3352 
3353 	return err;
3354 }
3355 
3356 static __maybe_unused struct device *
3357 phy_led_hw_control_get_device(struct led_classdev *led_cdev)
3358 {
3359 	struct phy_led *phyled = to_phy_led(led_cdev);
3360 	struct phy_device *phydev = phyled->phydev;
3361 
3362 	if (phydev->attached_dev)
3363 		return &phydev->attached_dev->dev;
3364 	return NULL;
3365 }
3366 
3367 static int __maybe_unused
3368 phy_led_hw_control_get(struct led_classdev *led_cdev,
3369 		       unsigned long *rules)
3370 {
3371 	struct phy_led *phyled = to_phy_led(led_cdev);
3372 	struct phy_device *phydev = phyled->phydev;
3373 	int err;
3374 
3375 	mutex_lock(&phydev->lock);
3376 	err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
3377 	mutex_unlock(&phydev->lock);
3378 
3379 	return err;
3380 }
3381 
3382 static int __maybe_unused
3383 phy_led_hw_control_set(struct led_classdev *led_cdev,
3384 		       unsigned long rules)
3385 {
3386 	struct phy_led *phyled = to_phy_led(led_cdev);
3387 	struct phy_device *phydev = phyled->phydev;
3388 	int err;
3389 
3390 	mutex_lock(&phydev->lock);
3391 	err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
3392 	mutex_unlock(&phydev->lock);
3393 
3394 	return err;
3395 }
3396 
3397 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev,
3398 						  unsigned long rules)
3399 {
3400 	struct phy_led *phyled = to_phy_led(led_cdev);
3401 	struct phy_device *phydev = phyled->phydev;
3402 	int err;
3403 
3404 	mutex_lock(&phydev->lock);
3405 	err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
3406 	mutex_unlock(&phydev->lock);
3407 
3408 	return err;
3409 }
3410 
3411 static void phy_leds_unregister(struct phy_device *phydev)
3412 {
3413 	struct phy_led *phyled, *tmp;
3414 
3415 	list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) {
3416 		led_classdev_unregister(&phyled->led_cdev);
3417 		list_del(&phyled->list);
3418 	}
3419 }
3420 
3421 static int of_phy_led(struct phy_device *phydev,
3422 		      struct device_node *led)
3423 {
3424 	struct device *dev = &phydev->mdio.dev;
3425 	struct led_init_data init_data = {};
3426 	struct led_classdev *cdev;
3427 	unsigned long modes = 0;
3428 	struct phy_led *phyled;
3429 	u32 index;
3430 	int err;
3431 
3432 	phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
3433 	if (!phyled)
3434 		return -ENOMEM;
3435 
3436 	cdev = &phyled->led_cdev;
3437 	phyled->phydev = phydev;
3438 
3439 	err = of_property_read_u32(led, "reg", &index);
3440 	if (err)
3441 		return err;
3442 	if (index > U8_MAX)
3443 		return -EINVAL;
3444 
3445 	if (of_property_read_bool(led, "active-high"))
3446 		set_bit(PHY_LED_ACTIVE_HIGH, &modes);
3447 	if (of_property_read_bool(led, "active-low"))
3448 		set_bit(PHY_LED_ACTIVE_LOW, &modes);
3449 	if (of_property_read_bool(led, "inactive-high-impedance"))
3450 		set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
3451 
3452 	if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) &&
3453 		    modes & BIT(PHY_LED_ACTIVE_HIGH)))
3454 		return -EINVAL;
3455 
3456 	if (modes) {
3457 		/* Return error if asked to set polarity modes but not supported */
3458 		if (!phydev->drv->led_polarity_set)
3459 			return -EINVAL;
3460 
3461 		err = phydev->drv->led_polarity_set(phydev, index, modes);
3462 		if (err)
3463 			return err;
3464 	}
3465 
3466 	phyled->index = index;
3467 	if (phydev->drv->led_brightness_set)
3468 		cdev->brightness_set_blocking = phy_led_set_brightness;
3469 	if (phydev->drv->led_blink_set)
3470 		cdev->blink_set = phy_led_blink_set;
3471 
3472 #ifdef CONFIG_LEDS_TRIGGERS
3473 	if (phydev->drv->led_hw_is_supported &&
3474 	    phydev->drv->led_hw_control_set &&
3475 	    phydev->drv->led_hw_control_get) {
3476 		cdev->hw_control_is_supported = phy_led_hw_is_supported;
3477 		cdev->hw_control_set = phy_led_hw_control_set;
3478 		cdev->hw_control_get = phy_led_hw_control_get;
3479 		cdev->hw_control_trigger = "netdev";
3480 	}
3481 
3482 	cdev->hw_control_get_device = phy_led_hw_control_get_device;
3483 #endif
3484 	cdev->max_brightness = 1;
3485 	init_data.devicename = dev_name(&phydev->mdio.dev);
3486 	init_data.fwnode = of_fwnode_handle(led);
3487 	init_data.devname_mandatory = true;
3488 
3489 	err = led_classdev_register_ext(dev, cdev, &init_data);
3490 	if (err)
3491 		return err;
3492 
3493 	list_add(&phyled->list, &phydev->leds);
3494 
3495 	return 0;
3496 }
3497 
3498 static int of_phy_leds(struct phy_device *phydev)
3499 {
3500 	struct device_node *node = phydev->mdio.dev.of_node;
3501 	struct device_node *leds;
3502 	int err;
3503 
3504 	if (!IS_ENABLED(CONFIG_OF_MDIO))
3505 		return 0;
3506 
3507 	if (!node)
3508 		return 0;
3509 
3510 	leds = of_get_child_by_name(node, "leds");
3511 	if (!leds)
3512 		return 0;
3513 
3514 	/* Check if the PHY driver have at least an OP to
3515 	 * set the LEDs.
3516 	 */
3517 	if (!(phydev->drv->led_brightness_set ||
3518 	      phydev->drv->led_blink_set ||
3519 	      phydev->drv->led_hw_control_set)) {
3520 		phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n");
3521 		goto exit;
3522 	}
3523 
3524 	for_each_available_child_of_node_scoped(leds, led) {
3525 		err = of_phy_led(phydev, led);
3526 		if (err) {
3527 			of_node_put(leds);
3528 			phy_leds_unregister(phydev);
3529 			return err;
3530 		}
3531 	}
3532 
3533 exit:
3534 	of_node_put(leds);
3535 	return 0;
3536 }
3537 
3538 static void phy_cleanup_ports(struct phy_device *phydev)
3539 {
3540 	struct phy_port *tmp, *port;
3541 
3542 	list_for_each_entry_safe(port, tmp, &phydev->ports, head) {
3543 		phy_del_port(phydev, port);
3544 		phy_port_destroy(port);
3545 	}
3546 }
3547 
3548 static int phy_default_setup_single_port(struct phy_device *phydev)
3549 {
3550 	struct phy_port *port = phy_port_alloc();
3551 	unsigned long mode;
3552 	int ret;
3553 
3554 	if (!port)
3555 		return -ENOMEM;
3556 
3557 	port->parent_type = PHY_PORT_PHY;
3558 	port->phy = phydev;
3559 
3560 	/* Let the PHY driver know that this port was never described anywhere.
3561 	 * This is the usual case, where we assume single-port PHY devices with
3562 	 * no SFP. In that case, the port supports exactly the same thing as
3563 	 * the PHY itself.
3564 	 *
3565 	 * However, this can also be because we have a combo-port PHY, with
3566 	 * only one port described in DT, through SFP for example.
3567 	 *
3568 	 * In that case, the PHY driver will be in charge of saying what we can
3569 	 * do on that non-represented port.
3570 	 */
3571 	port->not_described = true;
3572 	linkmode_copy(port->supported, phydev->supported);
3573 	port->mediums = phy_caps_mediums_from_linkmodes(port->supported);
3574 
3575 	for_each_set_bit(mode, port->supported, __ETHTOOL_LINK_MODE_MASK_NBITS)
3576 		port->pairs = max_t(int, port->pairs,
3577 				    ethtool_linkmode_n_pairs(mode));
3578 
3579 	ret = phy_add_port(phydev, port);
3580 	if (ret)
3581 		phy_port_destroy(port);
3582 
3583 	return ret;
3584 }
3585 
3586 static int of_phy_ports(struct phy_device *phydev)
3587 {
3588 	struct device_node *node = phydev->mdio.dev.of_node;
3589 	struct device_node *mdi;
3590 	struct phy_port *port;
3591 	int err;
3592 
3593 	if (!IS_ENABLED(CONFIG_OF_MDIO))
3594 		return 0;
3595 
3596 	if (!node)
3597 		return 0;
3598 
3599 	mdi = of_get_child_by_name(node, "mdi");
3600 	if (!mdi)
3601 		return 0;
3602 
3603 	for_each_available_child_of_node_scoped(mdi, port_node) {
3604 		port = phy_of_parse_port(port_node);
3605 		if (IS_ERR(port)) {
3606 			err = PTR_ERR(port);
3607 			goto out_err;
3608 		}
3609 
3610 		port->parent_type = PHY_PORT_PHY;
3611 		port->phy = phydev;
3612 
3613 		linkmode_copy(port->supported, phydev->supported);
3614 
3615 		err = phy_add_port(phydev, port);
3616 		if (err) {
3617 			phy_port_destroy(port);
3618 			goto out_err;
3619 		}
3620 	}
3621 	of_node_put(mdi);
3622 
3623 	return 0;
3624 
3625 out_err:
3626 	phy_cleanup_ports(phydev);
3627 	of_node_put(mdi);
3628 	return err;
3629 }
3630 
3631 static int phy_setup_ports(struct phy_device *phydev)
3632 {
3633 	__ETHTOOL_DECLARE_LINK_MODE_MASK(ports_supported);
3634 	struct phy_port *port;
3635 	int ret;
3636 
3637 	ret = of_phy_ports(phydev);
3638 	if (ret)
3639 		return ret;
3640 
3641 	/* We don't support SFP with genphy drivers. Also, genphy driver
3642 	 * binding occurs with RTNL help, which will deadlock the call to
3643 	 * sfp_bus_add_upstream().
3644 	 */
3645 	if (!phydev->is_genphy_driven) {
3646 		ret = phy_sfp_probe(phydev);
3647 		if (ret)
3648 			goto err_ports;
3649 	}
3650 
3651 	if (phydev->n_ports < phydev->max_n_ports) {
3652 		ret = phy_default_setup_single_port(phydev);
3653 		if (ret)
3654 			goto err_sfp;
3655 	}
3656 
3657 	linkmode_zero(ports_supported);
3658 
3659 	/* Aggregate the supported modes, which are made-up of :
3660 	 *  - What the PHY itself supports
3661 	 *  - What the sum of all ports support
3662 	 */
3663 	list_for_each_entry(port, &phydev->ports, head)
3664 		if (port->active)
3665 			linkmode_or(ports_supported, ports_supported,
3666 				    port->supported);
3667 
3668 	if (!linkmode_empty(ports_supported))
3669 		linkmode_and(phydev->supported, phydev->supported,
3670 			     ports_supported);
3671 
3672 	/* For now, the phy->port field is set as the first active port's type */
3673 	list_for_each_entry(port, &phydev->ports, head)
3674 		if (port->active) {
3675 			phydev->port = phy_port_get_type(port);
3676 			break;
3677 		}
3678 
3679 	ret = phy_sfp_add_upstream(phydev);
3680 	if (ret)
3681 		goto err_sfp;
3682 
3683 	return 0;
3684 
3685 err_sfp:
3686 	phy_sfp_release(phydev);
3687 err_ports:
3688 	phy_cleanup_ports(phydev);
3689 	return ret;
3690 }
3691 
3692 /**
3693  * phy_get_sfp_port() - Returns the first valid SFP port of a PHY
3694  * @phydev: pointer to the PHY device to get the SFP port from
3695  *
3696  * Returns: The first active SFP (serdes) port of a PHY device, NULL if none
3697  * exist.
3698  */
3699 struct phy_port *phy_get_sfp_port(struct phy_device *phydev)
3700 {
3701 	struct phy_port *port;
3702 
3703 	list_for_each_entry(port, &phydev->ports, head)
3704 		if (port->active && port->is_sfp)
3705 			return port;
3706 
3707 	return NULL;
3708 }
3709 EXPORT_SYMBOL_GPL(phy_get_sfp_port);
3710 
3711 /**
3712  * phy_set_upstream_port() - Sets the phy_port controlling the MII this PHY is
3713  *			     attached to.
3714  * @phydev: pointer to the PHY device we set the upstream of.
3715  * @port: The phy_port upstream of this PHY, can be NULL.
3716  */
3717 void phy_set_upstream_port(struct phy_device *phydev, struct phy_port *port)
3718 {
3719 	struct phy_port *local_port;
3720 
3721 	ASSERT_RTNL();
3722 
3723 	phydev->upstream_port = port;
3724 
3725 	phy_for_each_port(phydev, local_port)
3726 		local_port->upstream_port = port;
3727 }
3728 EXPORT_SYMBOL_GPL(phy_set_upstream_port);
3729 
3730 /**
3731  * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
3732  * @fwnode: pointer to the mdio_device's fwnode
3733  *
3734  * If successful, returns a pointer to the mdio_device with the embedded
3735  * struct device refcount incremented by one, or NULL on failure.
3736  * The caller should call put_device() on the mdio_device after its use.
3737  */
3738 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
3739 {
3740 	struct device *d;
3741 
3742 	if (!fwnode)
3743 		return NULL;
3744 
3745 	d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode);
3746 	if (!d)
3747 		return NULL;
3748 
3749 	return to_mdio_device(d);
3750 }
3751 EXPORT_SYMBOL(fwnode_mdio_find_device);
3752 
3753 /**
3754  * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
3755  *
3756  * @phy_fwnode: Pointer to the phy's fwnode.
3757  *
3758  * If successful, returns a pointer to the phy_device with the embedded
3759  * struct device refcount incremented by one, or NULL on failure.
3760  */
3761 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
3762 {
3763 	struct mdio_device *mdiodev;
3764 
3765 	mdiodev = fwnode_mdio_find_device(phy_fwnode);
3766 	if (!mdiodev)
3767 		return NULL;
3768 
3769 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
3770 		return to_phy_device(&mdiodev->dev);
3771 
3772 	put_device(&mdiodev->dev);
3773 
3774 	return NULL;
3775 }
3776 EXPORT_SYMBOL(fwnode_phy_find_device);
3777 
3778 /**
3779  * fwnode_get_phy_node - Get the phy_node using the named reference.
3780  * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
3781  *
3782  * Refer return conditions of fwnode_find_reference().
3783  * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
3784  * and "phy-device" are not supported in ACPI. DT supports all the three
3785  * named references to the phy node.
3786  */
3787 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode)
3788 {
3789 	struct fwnode_handle *phy_node;
3790 
3791 	/* Only phy-handle is used for ACPI */
3792 	phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
3793 	if (!IS_ERR(phy_node) || is_acpi_node(fwnode))
3794 		return phy_node;
3795 	phy_node = fwnode_find_reference(fwnode, "phy", 0);
3796 	if (!IS_ERR(phy_node))
3797 		return phy_node;
3798 	return fwnode_find_reference(fwnode, "phy-device", 0);
3799 }
3800 EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
3801 
3802 /**
3803  * phy_probe - probe and init a PHY device
3804  * @dev: device to probe and init
3805  *
3806  * Take care of setting up the phy_device structure, set the state to READY.
3807  */
3808 static int phy_probe(struct device *dev)
3809 {
3810 	struct phy_device *phydev = to_phy_device(dev);
3811 	struct device_driver *drv = phydev->mdio.dev.driver;
3812 	struct phy_driver *phydrv = to_phy_driver(drv);
3813 	int err = 0;
3814 
3815 	phydev->drv = phydrv;
3816 
3817 	/* Disable the interrupt if the PHY doesn't support it
3818 	 * but the interrupt is still a valid one
3819 	 */
3820 	if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
3821 		phydev->irq = PHY_POLL;
3822 
3823 	if (phydrv->flags & PHY_IS_INTERNAL)
3824 		phydev->is_internal = true;
3825 
3826 	/* Deassert the reset signal */
3827 	phy_device_reset(phydev, 0);
3828 
3829 	if (phydev->drv->probe) {
3830 		err = phydev->drv->probe(phydev);
3831 		if (err)
3832 			goto out_reset;
3833 	}
3834 
3835 	phy_disable_interrupts(phydev);
3836 
3837 	/* Start out supporting everything. Eventually,
3838 	 * a controller will attach, and may modify one
3839 	 * or both of these values
3840 	 */
3841 	if (phydrv->features) {
3842 		linkmode_copy(phydev->supported, phydrv->features);
3843 		genphy_c45_read_eee_abilities(phydev);
3844 	}
3845 	else if (phydrv->get_features)
3846 		err = phydrv->get_features(phydev);
3847 	else if (phydev->is_c45)
3848 		err = genphy_c45_pma_read_abilities(phydev);
3849 	else
3850 		err = genphy_read_abilities(phydev);
3851 
3852 	if (err)
3853 		goto out_remove;
3854 
3855 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3856 			       phydev->supported))
3857 		phydev->autoneg = 0;
3858 
3859 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
3860 			      phydev->supported))
3861 		phydev->is_gigabit_capable = 1;
3862 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
3863 			      phydev->supported))
3864 		phydev->is_gigabit_capable = 1;
3865 
3866 	of_set_phy_supported(phydev);
3867 
3868 	err = phy_setup_ports(phydev);
3869 	if (err)
3870 		goto out_remove;
3871 
3872 	phy_advertise_supported(phydev);
3873 
3874 	/* Get PHY default EEE advertising modes and handle them as potentially
3875 	 * safe initial configuration.
3876 	 */
3877 	err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
3878 	if (err)
3879 		goto out_sfp_release;
3880 
3881 	/* Get the EEE modes we want to prohibit. */
3882 	of_set_phy_eee_broken(phydev);
3883 
3884 	/* Some PHYs may advertise, by default, not support EEE modes. So,
3885 	 * we need to clean them. In addition remove all disabled EEE modes.
3886 	 */
3887 	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
3888 		     phydev->advertising_eee);
3889 	linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
3890 			phydev->eee_disabled_modes);
3891 
3892 	/* There is no "enabled" flag. If PHY is advertising, assume it is
3893 	 * kind of enabled.
3894 	 */
3895 	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
3896 
3897 	/* Get master/slave strap overrides */
3898 	of_set_phy_timing_role(phydev);
3899 
3900 	/* The Pause Frame bits indicate that the PHY can support passing
3901 	 * pause frames. During autonegotiation, the PHYs will determine if
3902 	 * they should allow pause frames to pass.  The MAC driver should then
3903 	 * use that result to determine whether to enable flow control via
3904 	 * pause frames.
3905 	 *
3906 	 * Normally, PHY drivers should not set the Pause bits, and instead
3907 	 * allow phylib to do that.  However, there may be some situations
3908 	 * (e.g. hardware erratum) where the driver wants to set only one
3909 	 * of these bits.
3910 	 */
3911 	if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
3912 	    !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
3913 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3914 				 phydev->supported);
3915 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3916 				 phydev->supported);
3917 	}
3918 
3919 	/* Register the PHY LED triggers */
3920 	if (!phydev->is_on_sfp_module)
3921 		phy_led_triggers_register(phydev);
3922 
3923 	/* Get the LEDs from the device tree, and instantiate standard
3924 	 * LEDs for them.
3925 	 */
3926 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) {
3927 		err = of_phy_leds(phydev);
3928 		if (err)
3929 			goto out_unreg_led_triggers;
3930 	}
3931 
3932 	/* Set the state to READY by default */
3933 	phydev->state = PHY_READY;
3934 
3935 	return 0;
3936 
3937 out_unreg_led_triggers:
3938 	if (!phydev->is_on_sfp_module)
3939 		phy_led_triggers_unregister(phydev);
3940 
3941 out_sfp_release:
3942 	phy_sfp_release(phydev);
3943 	phy_cleanup_ports(phydev);
3944 
3945 out_remove:
3946 	if (phydev->drv->remove)
3947 		phydev->drv->remove(phydev);
3948 
3949 out_reset:
3950 	/* Re-assert the reset signal on error */
3951 	phy_device_reset(phydev, 1);
3952 
3953 	return err;
3954 }
3955 
3956 static int phy_remove(struct device *dev)
3957 {
3958 	struct phy_device *phydev = to_phy_device(dev);
3959 
3960 	cancel_delayed_work_sync(&phydev->state_queue);
3961 
3962 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev))
3963 		phy_leds_unregister(phydev);
3964 
3965 	if (!phydev->is_on_sfp_module)
3966 		phy_led_triggers_unregister(phydev);
3967 
3968 	phydev->state = PHY_DOWN;
3969 
3970 	phy_sfp_release(phydev);
3971 	phy_cleanup_ports(phydev);
3972 
3973 	if (phydev->drv && phydev->drv->remove)
3974 		phydev->drv->remove(phydev);
3975 
3976 	/* Assert the reset signal */
3977 	phy_device_reset(phydev, 1);
3978 
3979 	phydev->drv = NULL;
3980 
3981 	return 0;
3982 }
3983 
3984 /**
3985  * phy_driver_register - register a phy_driver with the PHY layer
3986  * @new_driver: new phy_driver to register
3987  * @owner: module owning this PHY
3988  */
3989 static int phy_driver_register(struct phy_driver *new_driver,
3990 			       struct module *owner)
3991 {
3992 	int retval;
3993 
3994 	/* Either the features are hard coded, or dynamically
3995 	 * determined. It cannot be both.
3996 	 */
3997 	if (WARN_ON(new_driver->features && new_driver->get_features)) {
3998 		pr_err("%s: features and get_features must not both be set\n",
3999 		       new_driver->name);
4000 		return -EINVAL;
4001 	}
4002 
4003 	/* PHYLIB device drivers must not match using a DT compatible table
4004 	 * as this bypasses our checks that the mdiodev that is being matched
4005 	 * is backed by a struct phy_device. If such a case happens, we will
4006 	 * make out-of-bounds accesses and lockup in phydev->lock.
4007 	 */
4008 	if (WARN(new_driver->mdiodrv.driver.of_match_table,
4009 		 "%s: driver must not provide a DT match table\n",
4010 		 new_driver->name))
4011 		return -EINVAL;
4012 
4013 	new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
4014 	new_driver->mdiodrv.driver.name = new_driver->name;
4015 	new_driver->mdiodrv.driver.bus = &mdio_bus_type;
4016 	new_driver->mdiodrv.driver.probe = phy_probe;
4017 	new_driver->mdiodrv.driver.remove = phy_remove;
4018 	new_driver->mdiodrv.driver.owner = owner;
4019 	new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
4020 
4021 	retval = driver_register(&new_driver->mdiodrv.driver);
4022 	if (retval) {
4023 		pr_err("%s: Error %d in registering driver\n",
4024 		       new_driver->name, retval);
4025 
4026 		return retval;
4027 	}
4028 
4029 	pr_debug("%s: Registered new driver\n", new_driver->name);
4030 
4031 	return 0;
4032 }
4033 
4034 static void phy_driver_unregister(struct phy_driver *drv)
4035 {
4036 	driver_unregister(&drv->mdiodrv.driver);
4037 }
4038 
4039 int phy_drivers_register(struct phy_driver *new_driver, int n,
4040 			 struct module *owner)
4041 {
4042 	int i, ret = 0;
4043 
4044 	for (i = 0; i < n; i++) {
4045 		ret = phy_driver_register(new_driver + i, owner);
4046 		if (ret) {
4047 			while (i-- > 0)
4048 				phy_driver_unregister(new_driver + i);
4049 			break;
4050 		}
4051 	}
4052 	return ret;
4053 }
4054 EXPORT_SYMBOL(phy_drivers_register);
4055 
4056 void phy_drivers_unregister(struct phy_driver *drv, int n)
4057 {
4058 	int i;
4059 
4060 	for (i = 0; i < n; i++)
4061 		phy_driver_unregister(drv + i);
4062 }
4063 EXPORT_SYMBOL(phy_drivers_unregister);
4064 
4065 static struct phy_driver genphy_driver = {
4066 	.phy_id		= 0xffffffff,
4067 	.phy_id_mask	= 0xffffffff,
4068 	.name		= "Generic PHY",
4069 	.get_features	= genphy_read_abilities,
4070 	.suspend	= genphy_suspend,
4071 	.resume		= genphy_resume,
4072 	.set_loopback   = genphy_loopback,
4073 };
4074 
4075 static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
4076 	.get_sset_count		= phy_ethtool_get_sset_count,
4077 	.get_strings		= phy_ethtool_get_strings,
4078 	.get_stats		= phy_ethtool_get_stats,
4079 	.get_plca_cfg		= phy_ethtool_get_plca_cfg,
4080 	.set_plca_cfg		= phy_ethtool_set_plca_cfg,
4081 	.get_plca_status	= phy_ethtool_get_plca_status,
4082 	.start_cable_test	= phy_start_cable_test,
4083 	.start_cable_test_tdr	= phy_start_cable_test_tdr,
4084 };
4085 
4086 static const struct phylib_stubs __phylib_stubs = {
4087 	.hwtstamp_get = __phy_hwtstamp_get,
4088 	.hwtstamp_set = __phy_hwtstamp_set,
4089 	.get_phy_stats = __phy_ethtool_get_phy_stats,
4090 	.get_link_ext_stats = __phy_ethtool_get_link_ext_stats,
4091 };
4092 
4093 static void phylib_register_stubs(void)
4094 {
4095 	phylib_stubs = &__phylib_stubs;
4096 }
4097 
4098 static void phylib_unregister_stubs(void)
4099 {
4100 	phylib_stubs = NULL;
4101 }
4102 
4103 static int __init phy_init(void)
4104 {
4105 	int rc;
4106 
4107 	rc = class_register(&mdio_bus_class);
4108 	if (rc)
4109 		return rc;
4110 
4111 	rc = bus_register(&mdio_bus_type);
4112 	if (rc)
4113 		goto err_class;
4114 
4115 	rtnl_lock();
4116 	ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
4117 	phylib_register_stubs();
4118 	rtnl_unlock();
4119 
4120 	rc = phy_caps_init();
4121 	if (rc)
4122 		goto err_ethtool_phy_ops;
4123 
4124 	features_init();
4125 
4126 	rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
4127 	if (rc)
4128 		goto err_ethtool_phy_ops;
4129 
4130 	rc = phy_driver_register(&genphy_driver, THIS_MODULE);
4131 	if (rc)
4132 		goto err_c45;
4133 
4134 	return 0;
4135 
4136 err_c45:
4137 	phy_driver_unregister(&genphy_c45_driver);
4138 err_ethtool_phy_ops:
4139 	rtnl_lock();
4140 	phylib_unregister_stubs();
4141 	ethtool_set_ethtool_phy_ops(NULL);
4142 	rtnl_unlock();
4143 	bus_unregister(&mdio_bus_type);
4144 err_class:
4145 	class_unregister(&mdio_bus_class);
4146 
4147 	return rc;
4148 }
4149 
4150 static void __exit phy_exit(void)
4151 {
4152 	phy_driver_unregister(&genphy_c45_driver);
4153 	phy_driver_unregister(&genphy_driver);
4154 	rtnl_lock();
4155 	phylib_unregister_stubs();
4156 	ethtool_set_ethtool_phy_ops(NULL);
4157 	rtnl_unlock();
4158 	bus_unregister(&mdio_bus_type);
4159 	class_unregister(&mdio_bus_class);
4160 }
4161 
4162 subsys_initcall(phy_init);
4163 module_exit(phy_exit);
4164