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