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