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