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