xref: /linux/drivers/net/phy/phy_device.c (revision 6a35ddc5445a8291ced6247a67977e110275acde)
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/kernel.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/unistd.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/mii.h>
26 #include <linux/ethtool.h>
27 #include <linux/bitmap.h>
28 #include <linux/phy.h>
29 #include <linux/phy_led_triggers.h>
30 #include <linux/sfp.h>
31 #include <linux/mdio.h>
32 #include <linux/io.h>
33 #include <linux/uaccess.h>
34 
35 MODULE_DESCRIPTION("PHY library");
36 MODULE_AUTHOR("Andy Fleming");
37 MODULE_LICENSE("GPL");
38 
39 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
40 EXPORT_SYMBOL_GPL(phy_basic_features);
41 
42 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
43 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
44 
45 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
46 EXPORT_SYMBOL_GPL(phy_gbit_features);
47 
48 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
49 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
50 
51 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
52 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
53 
54 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
55 EXPORT_SYMBOL_GPL(phy_10gbit_features);
56 
57 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
58 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
59 
60 const int phy_basic_ports_array[3] = {
61 	ETHTOOL_LINK_MODE_Autoneg_BIT,
62 	ETHTOOL_LINK_MODE_TP_BIT,
63 	ETHTOOL_LINK_MODE_MII_BIT,
64 };
65 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
66 
67 const int phy_fibre_port_array[1] = {
68 	ETHTOOL_LINK_MODE_FIBRE_BIT,
69 };
70 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
71 
72 const int phy_all_ports_features_array[7] = {
73 	ETHTOOL_LINK_MODE_Autoneg_BIT,
74 	ETHTOOL_LINK_MODE_TP_BIT,
75 	ETHTOOL_LINK_MODE_MII_BIT,
76 	ETHTOOL_LINK_MODE_FIBRE_BIT,
77 	ETHTOOL_LINK_MODE_AUI_BIT,
78 	ETHTOOL_LINK_MODE_BNC_BIT,
79 	ETHTOOL_LINK_MODE_Backplane_BIT,
80 };
81 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
82 
83 const int phy_10_100_features_array[4] = {
84 	ETHTOOL_LINK_MODE_10baseT_Half_BIT,
85 	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
86 	ETHTOOL_LINK_MODE_100baseT_Half_BIT,
87 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
88 };
89 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
90 
91 const int phy_basic_t1_features_array[2] = {
92 	ETHTOOL_LINK_MODE_TP_BIT,
93 	ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
94 };
95 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
96 
97 const int phy_gbit_features_array[2] = {
98 	ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
99 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
100 };
101 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
102 
103 const int phy_10gbit_features_array[1] = {
104 	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
105 };
106 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
107 
108 const int phy_10gbit_fec_features_array[1] = {
109 	ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
110 };
111 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
112 
113 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
114 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
115 
116 static const int phy_10gbit_full_features_array[] = {
117 	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
118 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
119 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
120 	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
121 };
122 
123 static void features_init(void)
124 {
125 	/* 10/100 half/full*/
126 	linkmode_set_bit_array(phy_basic_ports_array,
127 			       ARRAY_SIZE(phy_basic_ports_array),
128 			       phy_basic_features);
129 	linkmode_set_bit_array(phy_10_100_features_array,
130 			       ARRAY_SIZE(phy_10_100_features_array),
131 			       phy_basic_features);
132 
133 	/* 100 full, TP */
134 	linkmode_set_bit_array(phy_basic_t1_features_array,
135 			       ARRAY_SIZE(phy_basic_t1_features_array),
136 			       phy_basic_t1_features);
137 
138 	/* 10/100 half/full + 1000 half/full */
139 	linkmode_set_bit_array(phy_basic_ports_array,
140 			       ARRAY_SIZE(phy_basic_ports_array),
141 			       phy_gbit_features);
142 	linkmode_set_bit_array(phy_10_100_features_array,
143 			       ARRAY_SIZE(phy_10_100_features_array),
144 			       phy_gbit_features);
145 	linkmode_set_bit_array(phy_gbit_features_array,
146 			       ARRAY_SIZE(phy_gbit_features_array),
147 			       phy_gbit_features);
148 
149 	/* 10/100 half/full + 1000 half/full + fibre*/
150 	linkmode_set_bit_array(phy_basic_ports_array,
151 			       ARRAY_SIZE(phy_basic_ports_array),
152 			       phy_gbit_fibre_features);
153 	linkmode_set_bit_array(phy_10_100_features_array,
154 			       ARRAY_SIZE(phy_10_100_features_array),
155 			       phy_gbit_fibre_features);
156 	linkmode_set_bit_array(phy_gbit_features_array,
157 			       ARRAY_SIZE(phy_gbit_features_array),
158 			       phy_gbit_fibre_features);
159 	linkmode_set_bit_array(phy_fibre_port_array,
160 			       ARRAY_SIZE(phy_fibre_port_array),
161 			       phy_gbit_fibre_features);
162 
163 	/* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
164 	linkmode_set_bit_array(phy_all_ports_features_array,
165 			       ARRAY_SIZE(phy_all_ports_features_array),
166 			       phy_gbit_all_ports_features);
167 	linkmode_set_bit_array(phy_10_100_features_array,
168 			       ARRAY_SIZE(phy_10_100_features_array),
169 			       phy_gbit_all_ports_features);
170 	linkmode_set_bit_array(phy_gbit_features_array,
171 			       ARRAY_SIZE(phy_gbit_features_array),
172 			       phy_gbit_all_ports_features);
173 
174 	/* 10/100 half/full + 1000 half/full + 10G full*/
175 	linkmode_set_bit_array(phy_all_ports_features_array,
176 			       ARRAY_SIZE(phy_all_ports_features_array),
177 			       phy_10gbit_features);
178 	linkmode_set_bit_array(phy_10_100_features_array,
179 			       ARRAY_SIZE(phy_10_100_features_array),
180 			       phy_10gbit_features);
181 	linkmode_set_bit_array(phy_gbit_features_array,
182 			       ARRAY_SIZE(phy_gbit_features_array),
183 			       phy_10gbit_features);
184 	linkmode_set_bit_array(phy_10gbit_features_array,
185 			       ARRAY_SIZE(phy_10gbit_features_array),
186 			       phy_10gbit_features);
187 
188 	/* 10/100/1000/10G full */
189 	linkmode_set_bit_array(phy_all_ports_features_array,
190 			       ARRAY_SIZE(phy_all_ports_features_array),
191 			       phy_10gbit_full_features);
192 	linkmode_set_bit_array(phy_10gbit_full_features_array,
193 			       ARRAY_SIZE(phy_10gbit_full_features_array),
194 			       phy_10gbit_full_features);
195 	/* 10G FEC only */
196 	linkmode_set_bit_array(phy_10gbit_fec_features_array,
197 			       ARRAY_SIZE(phy_10gbit_fec_features_array),
198 			       phy_10gbit_fec_features);
199 }
200 
201 void phy_device_free(struct phy_device *phydev)
202 {
203 	put_device(&phydev->mdio.dev);
204 }
205 EXPORT_SYMBOL(phy_device_free);
206 
207 static void phy_mdio_device_free(struct mdio_device *mdiodev)
208 {
209 	struct phy_device *phydev;
210 
211 	phydev = container_of(mdiodev, struct phy_device, mdio);
212 	phy_device_free(phydev);
213 }
214 
215 static void phy_device_release(struct device *dev)
216 {
217 	kfree(to_phy_device(dev));
218 }
219 
220 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
221 {
222 	struct phy_device *phydev;
223 
224 	phydev = container_of(mdiodev, struct phy_device, mdio);
225 	phy_device_remove(phydev);
226 }
227 
228 static struct phy_driver genphy_driver;
229 extern struct phy_driver genphy_c45_driver;
230 
231 static LIST_HEAD(phy_fixup_list);
232 static DEFINE_MUTEX(phy_fixup_lock);
233 
234 #ifdef CONFIG_PM
235 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
236 {
237 	struct device_driver *drv = phydev->mdio.dev.driver;
238 	struct phy_driver *phydrv = to_phy_driver(drv);
239 	struct net_device *netdev = phydev->attached_dev;
240 
241 	if (!drv || !phydrv->suspend)
242 		return false;
243 
244 	/* PHY not attached? May suspend if the PHY has not already been
245 	 * suspended as part of a prior call to phy_disconnect() ->
246 	 * phy_detach() -> phy_suspend() because the parent netdev might be the
247 	 * MDIO bus driver and clock gated at this point.
248 	 */
249 	if (!netdev)
250 		return !phydev->suspended;
251 
252 	if (netdev->wol_enabled)
253 		return false;
254 
255 	/* As long as not all affected network drivers support the
256 	 * wol_enabled flag, let's check for hints that WoL is enabled.
257 	 * Don't suspend PHY if the attached netdev parent may wake up.
258 	 * The parent may point to a PCI device, as in tg3 driver.
259 	 */
260 	if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
261 		return false;
262 
263 	/* Also don't suspend PHY if the netdev itself may wakeup. This
264 	 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
265 	 * e.g. SoC devices.
266 	 */
267 	if (device_may_wakeup(&netdev->dev))
268 		return false;
269 
270 	return true;
271 }
272 
273 static int mdio_bus_phy_suspend(struct device *dev)
274 {
275 	struct phy_device *phydev = to_phy_device(dev);
276 
277 	/* We must stop the state machine manually, otherwise it stops out of
278 	 * control, possibly with the phydev->lock held. Upon resume, netdev
279 	 * may call phy routines that try to grab the same lock, and that may
280 	 * lead to a deadlock.
281 	 */
282 	if (phydev->attached_dev && phydev->adjust_link)
283 		phy_stop_machine(phydev);
284 
285 	if (!mdio_bus_phy_may_suspend(phydev))
286 		return 0;
287 
288 	return phy_suspend(phydev);
289 }
290 
291 static int mdio_bus_phy_resume(struct device *dev)
292 {
293 	struct phy_device *phydev = to_phy_device(dev);
294 	int ret;
295 
296 	if (!mdio_bus_phy_may_suspend(phydev))
297 		goto no_resume;
298 
299 	ret = phy_resume(phydev);
300 	if (ret < 0)
301 		return ret;
302 
303 no_resume:
304 	if (phydev->attached_dev && phydev->adjust_link)
305 		phy_start_machine(phydev);
306 
307 	return 0;
308 }
309 
310 static int mdio_bus_phy_restore(struct device *dev)
311 {
312 	struct phy_device *phydev = to_phy_device(dev);
313 	struct net_device *netdev = phydev->attached_dev;
314 	int ret;
315 
316 	if (!netdev)
317 		return 0;
318 
319 	ret = phy_init_hw(phydev);
320 	if (ret < 0)
321 		return ret;
322 
323 	if (phydev->attached_dev && phydev->adjust_link)
324 		phy_start_machine(phydev);
325 
326 	return 0;
327 }
328 
329 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
330 	.suspend = mdio_bus_phy_suspend,
331 	.resume = mdio_bus_phy_resume,
332 	.freeze = mdio_bus_phy_suspend,
333 	.thaw = mdio_bus_phy_resume,
334 	.restore = mdio_bus_phy_restore,
335 };
336 
337 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
338 
339 #else
340 
341 #define MDIO_BUS_PHY_PM_OPS NULL
342 
343 #endif /* CONFIG_PM */
344 
345 /**
346  * phy_register_fixup - creates a new phy_fixup and adds it to the list
347  * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
348  * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
349  *	It can also be PHY_ANY_UID
350  * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
351  *	comparison
352  * @run: The actual code to be run when a matching PHY is found
353  */
354 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
355 		       int (*run)(struct phy_device *))
356 {
357 	struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
358 
359 	if (!fixup)
360 		return -ENOMEM;
361 
362 	strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
363 	fixup->phy_uid = phy_uid;
364 	fixup->phy_uid_mask = phy_uid_mask;
365 	fixup->run = run;
366 
367 	mutex_lock(&phy_fixup_lock);
368 	list_add_tail(&fixup->list, &phy_fixup_list);
369 	mutex_unlock(&phy_fixup_lock);
370 
371 	return 0;
372 }
373 EXPORT_SYMBOL(phy_register_fixup);
374 
375 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
376 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
377 			       int (*run)(struct phy_device *))
378 {
379 	return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
380 }
381 EXPORT_SYMBOL(phy_register_fixup_for_uid);
382 
383 /* Registers a fixup to be run on the PHY with id string bus_id */
384 int phy_register_fixup_for_id(const char *bus_id,
385 			      int (*run)(struct phy_device *))
386 {
387 	return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
388 }
389 EXPORT_SYMBOL(phy_register_fixup_for_id);
390 
391 /**
392  * phy_unregister_fixup - remove a phy_fixup from the list
393  * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
394  * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
395  * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
396  */
397 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
398 {
399 	struct list_head *pos, *n;
400 	struct phy_fixup *fixup;
401 	int ret;
402 
403 	ret = -ENODEV;
404 
405 	mutex_lock(&phy_fixup_lock);
406 	list_for_each_safe(pos, n, &phy_fixup_list) {
407 		fixup = list_entry(pos, struct phy_fixup, list);
408 
409 		if ((!strcmp(fixup->bus_id, bus_id)) &&
410 		    ((fixup->phy_uid & phy_uid_mask) ==
411 		     (phy_uid & phy_uid_mask))) {
412 			list_del(&fixup->list);
413 			kfree(fixup);
414 			ret = 0;
415 			break;
416 		}
417 	}
418 	mutex_unlock(&phy_fixup_lock);
419 
420 	return ret;
421 }
422 EXPORT_SYMBOL(phy_unregister_fixup);
423 
424 /* Unregisters a fixup of any PHY with the UID in phy_uid */
425 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
426 {
427 	return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
428 }
429 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
430 
431 /* Unregisters a fixup of the PHY with id string bus_id */
432 int phy_unregister_fixup_for_id(const char *bus_id)
433 {
434 	return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
435 }
436 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
437 
438 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
439  * Fixups can be set to match any in one or more fields.
440  */
441 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
442 {
443 	if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
444 		if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
445 			return 0;
446 
447 	if ((fixup->phy_uid & fixup->phy_uid_mask) !=
448 	    (phydev->phy_id & fixup->phy_uid_mask))
449 		if (fixup->phy_uid != PHY_ANY_UID)
450 			return 0;
451 
452 	return 1;
453 }
454 
455 /* Runs any matching fixups for this phydev */
456 static int phy_scan_fixups(struct phy_device *phydev)
457 {
458 	struct phy_fixup *fixup;
459 
460 	mutex_lock(&phy_fixup_lock);
461 	list_for_each_entry(fixup, &phy_fixup_list, list) {
462 		if (phy_needs_fixup(phydev, fixup)) {
463 			int err = fixup->run(phydev);
464 
465 			if (err < 0) {
466 				mutex_unlock(&phy_fixup_lock);
467 				return err;
468 			}
469 			phydev->has_fixups = true;
470 		}
471 	}
472 	mutex_unlock(&phy_fixup_lock);
473 
474 	return 0;
475 }
476 
477 static int phy_bus_match(struct device *dev, struct device_driver *drv)
478 {
479 	struct phy_device *phydev = to_phy_device(dev);
480 	struct phy_driver *phydrv = to_phy_driver(drv);
481 	const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
482 	int i;
483 
484 	if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
485 		return 0;
486 
487 	if (phydrv->match_phy_device)
488 		return phydrv->match_phy_device(phydev);
489 
490 	if (phydev->is_c45) {
491 		for (i = 1; i < num_ids; i++) {
492 			if (phydev->c45_ids.device_ids[i] == 0xffffffff)
493 				continue;
494 
495 			if ((phydrv->phy_id & phydrv->phy_id_mask) ==
496 			    (phydev->c45_ids.device_ids[i] &
497 			     phydrv->phy_id_mask))
498 				return 1;
499 		}
500 		return 0;
501 	} else {
502 		return (phydrv->phy_id & phydrv->phy_id_mask) ==
503 			(phydev->phy_id & phydrv->phy_id_mask);
504 	}
505 }
506 
507 static ssize_t
508 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
509 {
510 	struct phy_device *phydev = to_phy_device(dev);
511 
512 	return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
513 }
514 static DEVICE_ATTR_RO(phy_id);
515 
516 static ssize_t
517 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
518 {
519 	struct phy_device *phydev = to_phy_device(dev);
520 	const char *mode = NULL;
521 
522 	if (phy_is_internal(phydev))
523 		mode = "internal";
524 	else
525 		mode = phy_modes(phydev->interface);
526 
527 	return sprintf(buf, "%s\n", mode);
528 }
529 static DEVICE_ATTR_RO(phy_interface);
530 
531 static ssize_t
532 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
533 		    char *buf)
534 {
535 	struct phy_device *phydev = to_phy_device(dev);
536 
537 	return sprintf(buf, "%d\n", phydev->has_fixups);
538 }
539 static DEVICE_ATTR_RO(phy_has_fixups);
540 
541 static struct attribute *phy_dev_attrs[] = {
542 	&dev_attr_phy_id.attr,
543 	&dev_attr_phy_interface.attr,
544 	&dev_attr_phy_has_fixups.attr,
545 	NULL,
546 };
547 ATTRIBUTE_GROUPS(phy_dev);
548 
549 static const struct device_type mdio_bus_phy_type = {
550 	.name = "PHY",
551 	.groups = phy_dev_groups,
552 	.release = phy_device_release,
553 	.pm = MDIO_BUS_PHY_PM_OPS,
554 };
555 
556 static int phy_request_driver_module(struct phy_device *dev, int phy_id)
557 {
558 	int ret;
559 
560 	ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
561 			     MDIO_ID_ARGS(phy_id));
562 	/* We only check for failures in executing the usermode binary,
563 	 * not whether a PHY driver module exists for the PHY ID.
564 	 * Accept -ENOENT because this may occur in case no initramfs exists,
565 	 * then modprobe isn't available.
566 	 */
567 	if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
568 		phydev_err(dev, "error %d loading PHY driver module for ID 0x%08x\n",
569 			   ret, phy_id);
570 		return ret;
571 	}
572 
573 	return 0;
574 }
575 
576 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
577 				     bool is_c45,
578 				     struct phy_c45_device_ids *c45_ids)
579 {
580 	struct phy_device *dev;
581 	struct mdio_device *mdiodev;
582 	int ret = 0;
583 
584 	/* We allocate the device, and initialize the default values */
585 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
586 	if (!dev)
587 		return ERR_PTR(-ENOMEM);
588 
589 	mdiodev = &dev->mdio;
590 	mdiodev->dev.parent = &bus->dev;
591 	mdiodev->dev.bus = &mdio_bus_type;
592 	mdiodev->dev.type = &mdio_bus_phy_type;
593 	mdiodev->bus = bus;
594 	mdiodev->bus_match = phy_bus_match;
595 	mdiodev->addr = addr;
596 	mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
597 	mdiodev->device_free = phy_mdio_device_free;
598 	mdiodev->device_remove = phy_mdio_device_remove;
599 
600 	dev->speed = SPEED_UNKNOWN;
601 	dev->duplex = DUPLEX_UNKNOWN;
602 	dev->pause = 0;
603 	dev->asym_pause = 0;
604 	dev->link = 0;
605 	dev->interface = PHY_INTERFACE_MODE_GMII;
606 
607 	dev->autoneg = AUTONEG_ENABLE;
608 
609 	dev->is_c45 = is_c45;
610 	dev->phy_id = phy_id;
611 	if (c45_ids)
612 		dev->c45_ids = *c45_ids;
613 	dev->irq = bus->irq[addr];
614 	dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
615 
616 	dev->state = PHY_DOWN;
617 
618 	mutex_init(&dev->lock);
619 	INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
620 
621 	/* Request the appropriate module unconditionally; don't
622 	 * bother trying to do so only if it isn't already loaded,
623 	 * because that gets complicated. A hotplug event would have
624 	 * done an unconditional modprobe anyway.
625 	 * We don't do normal hotplug because it won't work for MDIO
626 	 * -- because it relies on the device staying around for long
627 	 * enough for the driver to get loaded. With MDIO, the NIC
628 	 * driver will get bored and give up as soon as it finds that
629 	 * there's no driver _already_ loaded.
630 	 */
631 	if (is_c45 && c45_ids) {
632 		const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
633 		int i;
634 
635 		for (i = 1; i < num_ids; i++) {
636 			if (c45_ids->device_ids[i] == 0xffffffff)
637 				continue;
638 
639 			ret = phy_request_driver_module(dev,
640 						c45_ids->device_ids[i]);
641 			if (ret)
642 				break;
643 		}
644 	} else {
645 		ret = phy_request_driver_module(dev, phy_id);
646 	}
647 
648 	if (!ret) {
649 		device_initialize(&mdiodev->dev);
650 	} else {
651 		kfree(dev);
652 		dev = ERR_PTR(ret);
653 	}
654 
655 	return dev;
656 }
657 EXPORT_SYMBOL(phy_device_create);
658 
659 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
660  * @bus: the target MII bus
661  * @addr: PHY address on the MII bus
662  * @dev_addr: MMD address in the PHY.
663  * @devices_in_package: where to store the devices in package information.
664  *
665  * Description: reads devices in package registers of a MMD at @dev_addr
666  * from PHY at @addr on @bus.
667  *
668  * Returns: 0 on success, -EIO on failure.
669  */
670 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
671 				   u32 *devices_in_package)
672 {
673 	int phy_reg, reg_addr;
674 
675 	reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
676 	phy_reg = mdiobus_read(bus, addr, reg_addr);
677 	if (phy_reg < 0)
678 		return -EIO;
679 	*devices_in_package = phy_reg << 16;
680 
681 	reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
682 	phy_reg = mdiobus_read(bus, addr, reg_addr);
683 	if (phy_reg < 0)
684 		return -EIO;
685 	*devices_in_package |= phy_reg;
686 
687 	/* Bit 0 doesn't represent a device, it indicates c22 regs presence */
688 	*devices_in_package &= ~BIT(0);
689 
690 	return 0;
691 }
692 
693 /**
694  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
695  * @bus: the target MII bus
696  * @addr: PHY address on the MII bus
697  * @phy_id: where to store the ID retrieved.
698  * @c45_ids: where to store the c45 ID information.
699  *
700  *   If the PHY devices-in-package appears to be valid, it and the
701  *   corresponding identifiers are stored in @c45_ids, zero is stored
702  *   in @phy_id.  Otherwise 0xffffffff is stored in @phy_id.  Returns
703  *   zero on success.
704  *
705  */
706 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
707 			   struct phy_c45_device_ids *c45_ids) {
708 	int phy_reg;
709 	int i, reg_addr;
710 	const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
711 	u32 *devs = &c45_ids->devices_in_package;
712 
713 	/* Find first non-zero Devices In package. Device zero is reserved
714 	 * for 802.3 c45 complied PHYs, so don't probe it at first.
715 	 */
716 	for (i = 1; i < num_ids && *devs == 0; i++) {
717 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
718 		if (phy_reg < 0)
719 			return -EIO;
720 
721 		if ((*devs & 0x1fffffff) == 0x1fffffff) {
722 			/*  If mostly Fs, there is no device there,
723 			 *  then let's continue to probe more, as some
724 			 *  10G PHYs have zero Devices In package,
725 			 *  e.g. Cortina CS4315/CS4340 PHY.
726 			 */
727 			phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
728 			if (phy_reg < 0)
729 				return -EIO;
730 			/* no device there, let's get out of here */
731 			if ((*devs & 0x1fffffff) == 0x1fffffff) {
732 				*phy_id = 0xffffffff;
733 				return 0;
734 			} else {
735 				break;
736 			}
737 		}
738 	}
739 
740 	/* Now probe Device Identifiers for each device present. */
741 	for (i = 1; i < num_ids; i++) {
742 		if (!(c45_ids->devices_in_package & (1 << i)))
743 			continue;
744 
745 		reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
746 		phy_reg = mdiobus_read(bus, addr, reg_addr);
747 		if (phy_reg < 0)
748 			return -EIO;
749 		c45_ids->device_ids[i] = phy_reg << 16;
750 
751 		reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
752 		phy_reg = mdiobus_read(bus, addr, reg_addr);
753 		if (phy_reg < 0)
754 			return -EIO;
755 		c45_ids->device_ids[i] |= phy_reg;
756 	}
757 	*phy_id = 0;
758 	return 0;
759 }
760 
761 /**
762  * get_phy_id - reads the specified addr for its ID.
763  * @bus: the target MII bus
764  * @addr: PHY address on the MII bus
765  * @phy_id: where to store the ID retrieved.
766  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
767  * @c45_ids: where to store the c45 ID information.
768  *
769  * Description: In the case of a 802.3-c22 PHY, reads the ID registers
770  *   of the PHY at @addr on the @bus, stores it in @phy_id and returns
771  *   zero on success.
772  *
773  *   In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
774  *   its return value is in turn returned.
775  *
776  */
777 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
778 		      bool is_c45, struct phy_c45_device_ids *c45_ids)
779 {
780 	int phy_reg;
781 
782 	if (is_c45)
783 		return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
784 
785 	/* Grab the bits from PHYIR1, and put them in the upper half */
786 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
787 	if (phy_reg < 0) {
788 		/* returning -ENODEV doesn't stop bus scanning */
789 		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
790 	}
791 
792 	*phy_id = phy_reg << 16;
793 
794 	/* Grab the bits from PHYIR2, and put them in the lower half */
795 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
796 	if (phy_reg < 0)
797 		return -EIO;
798 
799 	*phy_id |= phy_reg;
800 
801 	return 0;
802 }
803 
804 /**
805  * get_phy_device - reads the specified PHY device and returns its @phy_device
806  *		    struct
807  * @bus: the target MII bus
808  * @addr: PHY address on the MII bus
809  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
810  *
811  * Description: Reads the ID registers of the PHY at @addr on the
812  *   @bus, then allocates and returns the phy_device to represent it.
813  */
814 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
815 {
816 	struct phy_c45_device_ids c45_ids;
817 	u32 phy_id = 0;
818 	int r;
819 
820 	c45_ids.devices_in_package = 0;
821 	memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
822 
823 	r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
824 	if (r)
825 		return ERR_PTR(r);
826 
827 	/* If the phy_id is mostly Fs, there is no device there */
828 	if ((phy_id & 0x1fffffff) == 0x1fffffff)
829 		return ERR_PTR(-ENODEV);
830 
831 	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
832 }
833 EXPORT_SYMBOL(get_phy_device);
834 
835 /**
836  * phy_device_register - Register the phy device on the MDIO bus
837  * @phydev: phy_device structure to be added to the MDIO bus
838  */
839 int phy_device_register(struct phy_device *phydev)
840 {
841 	int err;
842 
843 	err = mdiobus_register_device(&phydev->mdio);
844 	if (err)
845 		return err;
846 
847 	/* Deassert the reset signal */
848 	phy_device_reset(phydev, 0);
849 
850 	/* Run all of the fixups for this PHY */
851 	err = phy_scan_fixups(phydev);
852 	if (err) {
853 		phydev_err(phydev, "failed to initialize\n");
854 		goto out;
855 	}
856 
857 	err = device_add(&phydev->mdio.dev);
858 	if (err) {
859 		phydev_err(phydev, "failed to add\n");
860 		goto out;
861 	}
862 
863 	return 0;
864 
865  out:
866 	/* Assert the reset signal */
867 	phy_device_reset(phydev, 1);
868 
869 	mdiobus_unregister_device(&phydev->mdio);
870 	return err;
871 }
872 EXPORT_SYMBOL(phy_device_register);
873 
874 /**
875  * phy_device_remove - Remove a previously registered phy device from the MDIO bus
876  * @phydev: phy_device structure to remove
877  *
878  * This doesn't free the phy_device itself, it merely reverses the effects
879  * of phy_device_register(). Use phy_device_free() to free the device
880  * after calling this function.
881  */
882 void phy_device_remove(struct phy_device *phydev)
883 {
884 	device_del(&phydev->mdio.dev);
885 
886 	/* Assert the reset signal */
887 	phy_device_reset(phydev, 1);
888 
889 	mdiobus_unregister_device(&phydev->mdio);
890 }
891 EXPORT_SYMBOL(phy_device_remove);
892 
893 /**
894  * phy_find_first - finds the first PHY device on the bus
895  * @bus: the target MII bus
896  */
897 struct phy_device *phy_find_first(struct mii_bus *bus)
898 {
899 	struct phy_device *phydev;
900 	int addr;
901 
902 	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
903 		phydev = mdiobus_get_phy(bus, addr);
904 		if (phydev)
905 			return phydev;
906 	}
907 	return NULL;
908 }
909 EXPORT_SYMBOL(phy_find_first);
910 
911 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
912 {
913 	struct net_device *netdev = phydev->attached_dev;
914 
915 	if (do_carrier) {
916 		if (up)
917 			netif_carrier_on(netdev);
918 		else
919 			netif_carrier_off(netdev);
920 	}
921 	phydev->adjust_link(netdev);
922 }
923 
924 /**
925  * phy_prepare_link - prepares the PHY layer to monitor link status
926  * @phydev: target phy_device struct
927  * @handler: callback function for link status change notifications
928  *
929  * Description: Tells the PHY infrastructure to handle the
930  *   gory details on monitoring link status (whether through
931  *   polling or an interrupt), and to call back to the
932  *   connected device driver when the link status changes.
933  *   If you want to monitor your own link state, don't call
934  *   this function.
935  */
936 static void phy_prepare_link(struct phy_device *phydev,
937 			     void (*handler)(struct net_device *))
938 {
939 	phydev->adjust_link = handler;
940 }
941 
942 /**
943  * phy_connect_direct - connect an ethernet device to a specific phy_device
944  * @dev: the network device to connect
945  * @phydev: the pointer to the phy device
946  * @handler: callback function for state change notifications
947  * @interface: PHY device's interface
948  */
949 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
950 		       void (*handler)(struct net_device *),
951 		       phy_interface_t interface)
952 {
953 	int rc;
954 
955 	if (!dev)
956 		return -EINVAL;
957 
958 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
959 	if (rc)
960 		return rc;
961 
962 	phy_prepare_link(phydev, handler);
963 	if (phy_interrupt_is_valid(phydev))
964 		phy_request_interrupt(phydev);
965 
966 	return 0;
967 }
968 EXPORT_SYMBOL(phy_connect_direct);
969 
970 /**
971  * phy_connect - connect an ethernet device to a PHY device
972  * @dev: the network device to connect
973  * @bus_id: the id string of the PHY device to connect
974  * @handler: callback function for state change notifications
975  * @interface: PHY device's interface
976  *
977  * Description: Convenience function for connecting ethernet
978  *   devices to PHY devices.  The default behavior is for
979  *   the PHY infrastructure to handle everything, and only notify
980  *   the connected driver when the link status changes.  If you
981  *   don't want, or can't use the provided functionality, you may
982  *   choose to call only the subset of functions which provide
983  *   the desired functionality.
984  */
985 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
986 			       void (*handler)(struct net_device *),
987 			       phy_interface_t interface)
988 {
989 	struct phy_device *phydev;
990 	struct device *d;
991 	int rc;
992 
993 	/* Search the list of PHY devices on the mdio bus for the
994 	 * PHY with the requested name
995 	 */
996 	d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
997 	if (!d) {
998 		pr_err("PHY %s not found\n", bus_id);
999 		return ERR_PTR(-ENODEV);
1000 	}
1001 	phydev = to_phy_device(d);
1002 
1003 	rc = phy_connect_direct(dev, phydev, handler, interface);
1004 	put_device(d);
1005 	if (rc)
1006 		return ERR_PTR(rc);
1007 
1008 	return phydev;
1009 }
1010 EXPORT_SYMBOL(phy_connect);
1011 
1012 /**
1013  * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1014  *		    device
1015  * @phydev: target phy_device struct
1016  */
1017 void phy_disconnect(struct phy_device *phydev)
1018 {
1019 	if (phy_is_started(phydev))
1020 		phy_stop(phydev);
1021 
1022 	if (phy_interrupt_is_valid(phydev))
1023 		phy_free_interrupt(phydev);
1024 
1025 	phydev->adjust_link = NULL;
1026 
1027 	phy_detach(phydev);
1028 }
1029 EXPORT_SYMBOL(phy_disconnect);
1030 
1031 /**
1032  * phy_poll_reset - Safely wait until a PHY reset has properly completed
1033  * @phydev: The PHY device to poll
1034  *
1035  * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1036  *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
1037  *   register must be polled until the BMCR_RESET bit clears.
1038  *
1039  *   Furthermore, any attempts to write to PHY registers may have no effect
1040  *   or even generate MDIO bus errors until this is complete.
1041  *
1042  *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1043  *   standard and do not fully reset after the BMCR_RESET bit is set, and may
1044  *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
1045  *   effort to support such broken PHYs, this function is separate from the
1046  *   standard phy_init_hw() which will zero all the other bits in the BMCR
1047  *   and reapply all driver-specific and board-specific fixups.
1048  */
1049 static int phy_poll_reset(struct phy_device *phydev)
1050 {
1051 	/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1052 	unsigned int retries = 12;
1053 	int ret;
1054 
1055 	do {
1056 		msleep(50);
1057 		ret = phy_read(phydev, MII_BMCR);
1058 		if (ret < 0)
1059 			return ret;
1060 	} while (ret & BMCR_RESET && --retries);
1061 	if (ret & BMCR_RESET)
1062 		return -ETIMEDOUT;
1063 
1064 	/* Some chips (smsc911x) may still need up to another 1ms after the
1065 	 * BMCR_RESET bit is cleared before they are usable.
1066 	 */
1067 	msleep(1);
1068 	return 0;
1069 }
1070 
1071 int phy_init_hw(struct phy_device *phydev)
1072 {
1073 	int ret = 0;
1074 
1075 	/* Deassert the reset signal */
1076 	phy_device_reset(phydev, 0);
1077 
1078 	if (!phydev->drv)
1079 		return 0;
1080 
1081 	if (phydev->drv->soft_reset)
1082 		ret = phydev->drv->soft_reset(phydev);
1083 
1084 	if (ret < 0)
1085 		return ret;
1086 
1087 	ret = phy_scan_fixups(phydev);
1088 	if (ret < 0)
1089 		return ret;
1090 
1091 	if (phydev->drv->config_init)
1092 		ret = phydev->drv->config_init(phydev);
1093 
1094 	return ret;
1095 }
1096 EXPORT_SYMBOL(phy_init_hw);
1097 
1098 void phy_attached_info(struct phy_device *phydev)
1099 {
1100 	phy_attached_print(phydev, NULL);
1101 }
1102 EXPORT_SYMBOL(phy_attached_info);
1103 
1104 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1105 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1106 {
1107 	const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1108 	char *irq_str;
1109 	char irq_num[8];
1110 
1111 	switch(phydev->irq) {
1112 	case PHY_POLL:
1113 		irq_str = "POLL";
1114 		break;
1115 	case PHY_IGNORE_INTERRUPT:
1116 		irq_str = "IGNORE";
1117 		break;
1118 	default:
1119 		snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1120 		irq_str = irq_num;
1121 		break;
1122 	}
1123 
1124 
1125 	if (!fmt) {
1126 		phydev_info(phydev, ATTACHED_FMT "\n",
1127 			 drv_name, phydev_name(phydev),
1128 			 irq_str);
1129 	} else {
1130 		va_list ap;
1131 
1132 		phydev_info(phydev, ATTACHED_FMT,
1133 			 drv_name, phydev_name(phydev),
1134 			 irq_str);
1135 
1136 		va_start(ap, fmt);
1137 		vprintk(fmt, ap);
1138 		va_end(ap);
1139 	}
1140 }
1141 EXPORT_SYMBOL(phy_attached_print);
1142 
1143 static void phy_sysfs_create_links(struct phy_device *phydev)
1144 {
1145 	struct net_device *dev = phydev->attached_dev;
1146 	int err;
1147 
1148 	if (!dev)
1149 		return;
1150 
1151 	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1152 				"attached_dev");
1153 	if (err)
1154 		return;
1155 
1156 	err = sysfs_create_link_nowarn(&dev->dev.kobj,
1157 				       &phydev->mdio.dev.kobj,
1158 				       "phydev");
1159 	if (err) {
1160 		dev_err(&dev->dev, "could not add device link to %s err %d\n",
1161 			kobject_name(&phydev->mdio.dev.kobj),
1162 			err);
1163 		/* non-fatal - some net drivers can use one netdevice
1164 		 * with more then one phy
1165 		 */
1166 	}
1167 
1168 	phydev->sysfs_links = true;
1169 }
1170 
1171 static ssize_t
1172 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1173 		    char *buf)
1174 {
1175 	struct phy_device *phydev = to_phy_device(dev);
1176 
1177 	return sprintf(buf, "%d\n", !phydev->attached_dev);
1178 }
1179 static DEVICE_ATTR_RO(phy_standalone);
1180 
1181 /**
1182  * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1183  * @upstream: pointer to the phy device
1184  * @bus: sfp bus representing cage being attached
1185  *
1186  * This is used to fill in the sfp_upstream_ops .attach member.
1187  */
1188 void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1189 {
1190 	struct phy_device *phydev = upstream;
1191 
1192 	if (phydev->attached_dev)
1193 		phydev->attached_dev->sfp_bus = bus;
1194 	phydev->sfp_bus_attached = true;
1195 }
1196 EXPORT_SYMBOL(phy_sfp_attach);
1197 
1198 /**
1199  * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1200  * @upstream: pointer to the phy device
1201  * @bus: sfp bus representing cage being attached
1202  *
1203  * This is used to fill in the sfp_upstream_ops .detach member.
1204  */
1205 void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1206 {
1207 	struct phy_device *phydev = upstream;
1208 
1209 	if (phydev->attached_dev)
1210 		phydev->attached_dev->sfp_bus = NULL;
1211 	phydev->sfp_bus_attached = false;
1212 }
1213 EXPORT_SYMBOL(phy_sfp_detach);
1214 
1215 /**
1216  * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1217  * @phydev: Pointer to phy_device
1218  * @ops: SFP's upstream operations
1219  */
1220 int phy_sfp_probe(struct phy_device *phydev,
1221 		  const struct sfp_upstream_ops *ops)
1222 {
1223 	struct sfp_bus *bus;
1224 	int ret;
1225 
1226 	if (phydev->mdio.dev.fwnode) {
1227 		bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1228 		if (IS_ERR(bus))
1229 			return PTR_ERR(bus);
1230 
1231 		phydev->sfp_bus = bus;
1232 
1233 		ret = sfp_bus_add_upstream(bus, phydev, ops);
1234 		sfp_bus_put(bus);
1235 	}
1236 	return 0;
1237 }
1238 EXPORT_SYMBOL(phy_sfp_probe);
1239 
1240 /**
1241  * phy_attach_direct - attach a network device to a given PHY device pointer
1242  * @dev: network device to attach
1243  * @phydev: Pointer to phy_device to attach
1244  * @flags: PHY device's dev_flags
1245  * @interface: PHY device's interface
1246  *
1247  * Description: Called by drivers to attach to a particular PHY
1248  *     device. The phy_device is found, and properly hooked up
1249  *     to the phy_driver.  If no driver is attached, then a
1250  *     generic driver is used.  The phy_device is given a ptr to
1251  *     the attaching device, and given a callback for link status
1252  *     change.  The phy_device is returned to the attaching driver.
1253  *     This function takes a reference on the phy device.
1254  */
1255 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1256 		      u32 flags, phy_interface_t interface)
1257 {
1258 	struct mii_bus *bus = phydev->mdio.bus;
1259 	struct device *d = &phydev->mdio.dev;
1260 	struct module *ndev_owner = NULL;
1261 	bool using_genphy = false;
1262 	int err;
1263 
1264 	/* For Ethernet device drivers that register their own MDIO bus, we
1265 	 * will have bus->owner match ndev_mod, so we do not want to increment
1266 	 * our own module->refcnt here, otherwise we would not be able to
1267 	 * unload later on.
1268 	 */
1269 	if (dev)
1270 		ndev_owner = dev->dev.parent->driver->owner;
1271 	if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1272 		phydev_err(phydev, "failed to get the bus module\n");
1273 		return -EIO;
1274 	}
1275 
1276 	get_device(d);
1277 
1278 	/* Assume that if there is no driver, that it doesn't
1279 	 * exist, and we should use the genphy driver.
1280 	 */
1281 	if (!d->driver) {
1282 		if (phydev->is_c45)
1283 			d->driver = &genphy_c45_driver.mdiodrv.driver;
1284 		else
1285 			d->driver = &genphy_driver.mdiodrv.driver;
1286 
1287 		using_genphy = true;
1288 	}
1289 
1290 	if (!try_module_get(d->driver->owner)) {
1291 		phydev_err(phydev, "failed to get the device driver module\n");
1292 		err = -EIO;
1293 		goto error_put_device;
1294 	}
1295 
1296 	if (using_genphy) {
1297 		err = d->driver->probe(d);
1298 		if (err >= 0)
1299 			err = device_bind_driver(d);
1300 
1301 		if (err)
1302 			goto error_module_put;
1303 	}
1304 
1305 	if (phydev->attached_dev) {
1306 		dev_err(&dev->dev, "PHY already attached\n");
1307 		err = -EBUSY;
1308 		goto error;
1309 	}
1310 
1311 	phydev->phy_link_change = phy_link_change;
1312 	if (dev) {
1313 		phydev->attached_dev = dev;
1314 		dev->phydev = phydev;
1315 
1316 		if (phydev->sfp_bus_attached)
1317 			dev->sfp_bus = phydev->sfp_bus;
1318 	}
1319 
1320 	/* Some Ethernet drivers try to connect to a PHY device before
1321 	 * calling register_netdevice() -> netdev_register_kobject() and
1322 	 * does the dev->dev.kobj initialization. Here we only check for
1323 	 * success which indicates that the network device kobject is
1324 	 * ready. Once we do that we still need to keep track of whether
1325 	 * links were successfully set up or not for phy_detach() to
1326 	 * remove them accordingly.
1327 	 */
1328 	phydev->sysfs_links = false;
1329 
1330 	phy_sysfs_create_links(phydev);
1331 
1332 	if (!phydev->attached_dev) {
1333 		err = sysfs_create_file(&phydev->mdio.dev.kobj,
1334 					&dev_attr_phy_standalone.attr);
1335 		if (err)
1336 			phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1337 	}
1338 
1339 	phydev->dev_flags |= flags;
1340 
1341 	phydev->interface = interface;
1342 
1343 	phydev->state = PHY_READY;
1344 
1345 	/* Initial carrier state is off as the phy is about to be
1346 	 * (re)initialized.
1347 	 */
1348 	if (dev)
1349 		netif_carrier_off(phydev->attached_dev);
1350 
1351 	/* Do initial configuration here, now that
1352 	 * we have certain key parameters
1353 	 * (dev_flags and interface)
1354 	 */
1355 	err = phy_init_hw(phydev);
1356 	if (err)
1357 		goto error;
1358 
1359 	phy_resume(phydev);
1360 	phy_led_triggers_register(phydev);
1361 
1362 	return err;
1363 
1364 error:
1365 	/* phy_detach() does all of the cleanup below */
1366 	phy_detach(phydev);
1367 	return err;
1368 
1369 error_module_put:
1370 	module_put(d->driver->owner);
1371 error_put_device:
1372 	put_device(d);
1373 	if (ndev_owner != bus->owner)
1374 		module_put(bus->owner);
1375 	return err;
1376 }
1377 EXPORT_SYMBOL(phy_attach_direct);
1378 
1379 /**
1380  * phy_attach - attach a network device to a particular PHY device
1381  * @dev: network device to attach
1382  * @bus_id: Bus ID of PHY device to attach
1383  * @interface: PHY device's interface
1384  *
1385  * Description: Same as phy_attach_direct() except that a PHY bus_id
1386  *     string is passed instead of a pointer to a struct phy_device.
1387  */
1388 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1389 			      phy_interface_t interface)
1390 {
1391 	struct bus_type *bus = &mdio_bus_type;
1392 	struct phy_device *phydev;
1393 	struct device *d;
1394 	int rc;
1395 
1396 	if (!dev)
1397 		return ERR_PTR(-EINVAL);
1398 
1399 	/* Search the list of PHY devices on the mdio bus for the
1400 	 * PHY with the requested name
1401 	 */
1402 	d = bus_find_device_by_name(bus, NULL, bus_id);
1403 	if (!d) {
1404 		pr_err("PHY %s not found\n", bus_id);
1405 		return ERR_PTR(-ENODEV);
1406 	}
1407 	phydev = to_phy_device(d);
1408 
1409 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1410 	put_device(d);
1411 	if (rc)
1412 		return ERR_PTR(rc);
1413 
1414 	return phydev;
1415 }
1416 EXPORT_SYMBOL(phy_attach);
1417 
1418 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1419 				      struct device_driver *driver)
1420 {
1421 	struct device *d = &phydev->mdio.dev;
1422 	bool ret = false;
1423 
1424 	if (!phydev->drv)
1425 		return ret;
1426 
1427 	get_device(d);
1428 	ret = d->driver == driver;
1429 	put_device(d);
1430 
1431 	return ret;
1432 }
1433 
1434 bool phy_driver_is_genphy(struct phy_device *phydev)
1435 {
1436 	return phy_driver_is_genphy_kind(phydev,
1437 					 &genphy_driver.mdiodrv.driver);
1438 }
1439 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1440 
1441 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1442 {
1443 	return phy_driver_is_genphy_kind(phydev,
1444 					 &genphy_c45_driver.mdiodrv.driver);
1445 }
1446 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1447 
1448 /**
1449  * phy_detach - detach a PHY device from its network device
1450  * @phydev: target phy_device struct
1451  *
1452  * This detaches the phy device from its network device and the phy
1453  * driver, and drops the reference count taken in phy_attach_direct().
1454  */
1455 void phy_detach(struct phy_device *phydev)
1456 {
1457 	struct net_device *dev = phydev->attached_dev;
1458 	struct module *ndev_owner = NULL;
1459 	struct mii_bus *bus;
1460 
1461 	if (phydev->sysfs_links) {
1462 		if (dev)
1463 			sysfs_remove_link(&dev->dev.kobj, "phydev");
1464 		sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1465 	}
1466 
1467 	if (!phydev->attached_dev)
1468 		sysfs_remove_file(&phydev->mdio.dev.kobj,
1469 				  &dev_attr_phy_standalone.attr);
1470 
1471 	phy_suspend(phydev);
1472 	if (dev) {
1473 		phydev->attached_dev->phydev = NULL;
1474 		phydev->attached_dev = NULL;
1475 	}
1476 	phydev->phylink = NULL;
1477 
1478 	phy_led_triggers_unregister(phydev);
1479 
1480 	module_put(phydev->mdio.dev.driver->owner);
1481 
1482 	/* If the device had no specific driver before (i.e. - it
1483 	 * was using the generic driver), we unbind the device
1484 	 * from the generic driver so that there's a chance a
1485 	 * real driver could be loaded
1486 	 */
1487 	if (phy_driver_is_genphy(phydev) ||
1488 	    phy_driver_is_genphy_10g(phydev))
1489 		device_release_driver(&phydev->mdio.dev);
1490 
1491 	/*
1492 	 * The phydev might go away on the put_device() below, so avoid
1493 	 * a use-after-free bug by reading the underlying bus first.
1494 	 */
1495 	bus = phydev->mdio.bus;
1496 
1497 	put_device(&phydev->mdio.dev);
1498 	if (dev)
1499 		ndev_owner = dev->dev.parent->driver->owner;
1500 	if (ndev_owner != bus->owner)
1501 		module_put(bus->owner);
1502 
1503 	/* Assert the reset signal */
1504 	phy_device_reset(phydev, 1);
1505 }
1506 EXPORT_SYMBOL(phy_detach);
1507 
1508 int phy_suspend(struct phy_device *phydev)
1509 {
1510 	struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1511 	struct net_device *netdev = phydev->attached_dev;
1512 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1513 	int ret = 0;
1514 
1515 	/* If the device has WOL enabled, we cannot suspend the PHY */
1516 	phy_ethtool_get_wol(phydev, &wol);
1517 	if (wol.wolopts || (netdev && netdev->wol_enabled))
1518 		return -EBUSY;
1519 
1520 	if (phydev->drv && phydrv->suspend)
1521 		ret = phydrv->suspend(phydev);
1522 
1523 	if (ret)
1524 		return ret;
1525 
1526 	phydev->suspended = true;
1527 
1528 	return ret;
1529 }
1530 EXPORT_SYMBOL(phy_suspend);
1531 
1532 int __phy_resume(struct phy_device *phydev)
1533 {
1534 	struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1535 	int ret = 0;
1536 
1537 	WARN_ON(!mutex_is_locked(&phydev->lock));
1538 
1539 	if (phydev->drv && phydrv->resume)
1540 		ret = phydrv->resume(phydev);
1541 
1542 	if (ret)
1543 		return ret;
1544 
1545 	phydev->suspended = false;
1546 
1547 	return ret;
1548 }
1549 EXPORT_SYMBOL(__phy_resume);
1550 
1551 int phy_resume(struct phy_device *phydev)
1552 {
1553 	int ret;
1554 
1555 	mutex_lock(&phydev->lock);
1556 	ret = __phy_resume(phydev);
1557 	mutex_unlock(&phydev->lock);
1558 
1559 	return ret;
1560 }
1561 EXPORT_SYMBOL(phy_resume);
1562 
1563 int phy_loopback(struct phy_device *phydev, bool enable)
1564 {
1565 	struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1566 	int ret = 0;
1567 
1568 	mutex_lock(&phydev->lock);
1569 
1570 	if (enable && phydev->loopback_enabled) {
1571 		ret = -EBUSY;
1572 		goto out;
1573 	}
1574 
1575 	if (!enable && !phydev->loopback_enabled) {
1576 		ret = -EINVAL;
1577 		goto out;
1578 	}
1579 
1580 	if (phydev->drv && phydrv->set_loopback)
1581 		ret = phydrv->set_loopback(phydev, enable);
1582 	else
1583 		ret = -EOPNOTSUPP;
1584 
1585 	if (ret)
1586 		goto out;
1587 
1588 	phydev->loopback_enabled = enable;
1589 
1590 out:
1591 	mutex_unlock(&phydev->lock);
1592 	return ret;
1593 }
1594 EXPORT_SYMBOL(phy_loopback);
1595 
1596 /**
1597  * phy_reset_after_clk_enable - perform a PHY reset if needed
1598  * @phydev: target phy_device struct
1599  *
1600  * Description: Some PHYs are known to need a reset after their refclk was
1601  *   enabled. This function evaluates the flags and perform the reset if it's
1602  *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1603  *   was reset.
1604  */
1605 int phy_reset_after_clk_enable(struct phy_device *phydev)
1606 {
1607 	if (!phydev || !phydev->drv)
1608 		return -ENODEV;
1609 
1610 	if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1611 		phy_device_reset(phydev, 1);
1612 		phy_device_reset(phydev, 0);
1613 		return 1;
1614 	}
1615 
1616 	return 0;
1617 }
1618 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1619 
1620 /* Generic PHY support and helper functions */
1621 
1622 /**
1623  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1624  * @phydev: target phy_device struct
1625  *
1626  * Description: Writes MII_ADVERTISE with the appropriate values,
1627  *   after sanitizing the values to make sure we only advertise
1628  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
1629  *   hasn't changed, and > 0 if it has changed.
1630  */
1631 static int genphy_config_advert(struct phy_device *phydev)
1632 {
1633 	int err, bmsr, changed = 0;
1634 	u32 adv;
1635 
1636 	/* Only allow advertising what this PHY supports */
1637 	linkmode_and(phydev->advertising, phydev->advertising,
1638 		     phydev->supported);
1639 
1640 	adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1641 
1642 	/* Setup standard advertisement */
1643 	err = phy_modify_changed(phydev, MII_ADVERTISE,
1644 				 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1645 				 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1646 				 adv);
1647 	if (err < 0)
1648 		return err;
1649 	if (err > 0)
1650 		changed = 1;
1651 
1652 	bmsr = phy_read(phydev, MII_BMSR);
1653 	if (bmsr < 0)
1654 		return bmsr;
1655 
1656 	/* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1657 	 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1658 	 * logical 1.
1659 	 */
1660 	if (!(bmsr & BMSR_ESTATEN))
1661 		return changed;
1662 
1663 	adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1664 
1665 	err = phy_modify_changed(phydev, MII_CTRL1000,
1666 				 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1667 				 adv);
1668 	if (err < 0)
1669 		return err;
1670 	if (err > 0)
1671 		changed = 1;
1672 
1673 	return changed;
1674 }
1675 
1676 /**
1677  * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1678  * @phydev: target phy_device struct
1679  *
1680  * Description: Writes MII_ADVERTISE with the appropriate values,
1681  *   after sanitizing the values to make sure we only advertise
1682  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
1683  *   hasn't changed, and > 0 if it has changed. This function is intended
1684  *   for Clause 37 1000Base-X mode.
1685  */
1686 static int genphy_c37_config_advert(struct phy_device *phydev)
1687 {
1688 	u16 adv = 0;
1689 
1690 	/* Only allow advertising what this PHY supports */
1691 	linkmode_and(phydev->advertising, phydev->advertising,
1692 		     phydev->supported);
1693 
1694 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1695 			      phydev->advertising))
1696 		adv |= ADVERTISE_1000XFULL;
1697 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1698 			      phydev->advertising))
1699 		adv |= ADVERTISE_1000XPAUSE;
1700 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1701 			      phydev->advertising))
1702 		adv |= ADVERTISE_1000XPSE_ASYM;
1703 
1704 	return phy_modify_changed(phydev, MII_ADVERTISE,
1705 				  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1706 				  ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1707 				  adv);
1708 }
1709 
1710 /**
1711  * genphy_config_eee_advert - disable unwanted eee mode advertisement
1712  * @phydev: target phy_device struct
1713  *
1714  * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1715  *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1716  *   changed, and 1 if it has changed.
1717  */
1718 int genphy_config_eee_advert(struct phy_device *phydev)
1719 {
1720 	int err;
1721 
1722 	/* Nothing to disable */
1723 	if (!phydev->eee_broken_modes)
1724 		return 0;
1725 
1726 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1727 				     phydev->eee_broken_modes, 0);
1728 	/* If the call failed, we assume that EEE is not supported */
1729 	return err < 0 ? 0 : err;
1730 }
1731 EXPORT_SYMBOL(genphy_config_eee_advert);
1732 
1733 /**
1734  * genphy_setup_forced - configures/forces speed/duplex from @phydev
1735  * @phydev: target phy_device struct
1736  *
1737  * Description: Configures MII_BMCR to force speed/duplex
1738  *   to the values in phydev. Assumes that the values are valid.
1739  *   Please see phy_sanitize_settings().
1740  */
1741 int genphy_setup_forced(struct phy_device *phydev)
1742 {
1743 	u16 ctl = 0;
1744 
1745 	phydev->pause = 0;
1746 	phydev->asym_pause = 0;
1747 
1748 	if (SPEED_1000 == phydev->speed)
1749 		ctl |= BMCR_SPEED1000;
1750 	else if (SPEED_100 == phydev->speed)
1751 		ctl |= BMCR_SPEED100;
1752 
1753 	if (DUPLEX_FULL == phydev->duplex)
1754 		ctl |= BMCR_FULLDPLX;
1755 
1756 	return phy_modify(phydev, MII_BMCR,
1757 			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1758 }
1759 EXPORT_SYMBOL(genphy_setup_forced);
1760 
1761 /**
1762  * genphy_restart_aneg - Enable and Restart Autonegotiation
1763  * @phydev: target phy_device struct
1764  */
1765 int genphy_restart_aneg(struct phy_device *phydev)
1766 {
1767 	/* Don't isolate the PHY if we're negotiating */
1768 	return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1769 			  BMCR_ANENABLE | BMCR_ANRESTART);
1770 }
1771 EXPORT_SYMBOL(genphy_restart_aneg);
1772 
1773 /**
1774  * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
1775  * @phydev: target phy_device struct
1776  * @restart: whether aneg restart is requested
1777  *
1778  * Check, and restart auto-negotiation if needed.
1779  */
1780 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
1781 {
1782 	int ret = 0;
1783 
1784 	if (!restart) {
1785 		/* Advertisement hasn't changed, but maybe aneg was never on to
1786 		 * begin with?  Or maybe phy was isolated?
1787 		 */
1788 		ret = phy_read(phydev, MII_BMCR);
1789 		if (ret < 0)
1790 			return ret;
1791 
1792 		if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
1793 			restart = true;
1794 	}
1795 
1796 	if (restart)
1797 		ret = genphy_restart_aneg(phydev);
1798 
1799 	return ret;
1800 }
1801 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
1802 
1803 /**
1804  * __genphy_config_aneg - restart auto-negotiation or write BMCR
1805  * @phydev: target phy_device struct
1806  * @changed: whether autoneg is requested
1807  *
1808  * Description: If auto-negotiation is enabled, we configure the
1809  *   advertising, and then restart auto-negotiation.  If it is not
1810  *   enabled, then we write the BMCR.
1811  */
1812 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
1813 {
1814 	int err;
1815 
1816 	if (genphy_config_eee_advert(phydev))
1817 		changed = true;
1818 
1819 	if (AUTONEG_ENABLE != phydev->autoneg)
1820 		return genphy_setup_forced(phydev);
1821 
1822 	err = genphy_config_advert(phydev);
1823 	if (err < 0) /* error */
1824 		return err;
1825 	else if (err)
1826 		changed = true;
1827 
1828 	return genphy_check_and_restart_aneg(phydev, changed);
1829 }
1830 EXPORT_SYMBOL(__genphy_config_aneg);
1831 
1832 /**
1833  * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
1834  * @phydev: target phy_device struct
1835  *
1836  * Description: If auto-negotiation is enabled, we configure the
1837  *   advertising, and then restart auto-negotiation.  If it is not
1838  *   enabled, then we write the BMCR. This function is intended
1839  *   for use with Clause 37 1000Base-X mode.
1840  */
1841 int genphy_c37_config_aneg(struct phy_device *phydev)
1842 {
1843 	int err, changed;
1844 
1845 	if (phydev->autoneg != AUTONEG_ENABLE)
1846 		return genphy_setup_forced(phydev);
1847 
1848 	err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
1849 			 BMCR_SPEED1000);
1850 	if (err)
1851 		return err;
1852 
1853 	changed = genphy_c37_config_advert(phydev);
1854 	if (changed < 0) /* error */
1855 		return changed;
1856 
1857 	if (!changed) {
1858 		/* Advertisement hasn't changed, but maybe aneg was never on to
1859 		 * begin with?  Or maybe phy was isolated?
1860 		 */
1861 		int ctl = phy_read(phydev, MII_BMCR);
1862 
1863 		if (ctl < 0)
1864 			return ctl;
1865 
1866 		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1867 			changed = 1; /* do restart aneg */
1868 	}
1869 
1870 	/* Only restart aneg if we are advertising something different
1871 	 * than we were before.
1872 	 */
1873 	if (changed > 0)
1874 		return genphy_restart_aneg(phydev);
1875 
1876 	return 0;
1877 }
1878 EXPORT_SYMBOL(genphy_c37_config_aneg);
1879 
1880 /**
1881  * genphy_aneg_done - return auto-negotiation status
1882  * @phydev: target phy_device struct
1883  *
1884  * Description: Reads the status register and returns 0 either if
1885  *   auto-negotiation is incomplete, or if there was an error.
1886  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1887  */
1888 int genphy_aneg_done(struct phy_device *phydev)
1889 {
1890 	int retval = phy_read(phydev, MII_BMSR);
1891 
1892 	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1893 }
1894 EXPORT_SYMBOL(genphy_aneg_done);
1895 
1896 /**
1897  * genphy_update_link - update link status in @phydev
1898  * @phydev: target phy_device struct
1899  *
1900  * Description: Update the value in phydev->link to reflect the
1901  *   current link value.  In order to do this, we need to read
1902  *   the status register twice, keeping the second value.
1903  */
1904 int genphy_update_link(struct phy_device *phydev)
1905 {
1906 	int status = 0, bmcr;
1907 
1908 	bmcr = phy_read(phydev, MII_BMCR);
1909 	if (bmcr < 0)
1910 		return bmcr;
1911 
1912 	/* Autoneg is being started, therefore disregard BMSR value and
1913 	 * report link as down.
1914 	 */
1915 	if (bmcr & BMCR_ANRESTART)
1916 		goto done;
1917 
1918 	/* The link state is latched low so that momentary link
1919 	 * drops can be detected. Do not double-read the status
1920 	 * in polling mode to detect such short link drops.
1921 	 */
1922 	if (!phy_polling_mode(phydev)) {
1923 		status = phy_read(phydev, MII_BMSR);
1924 		if (status < 0)
1925 			return status;
1926 		else if (status & BMSR_LSTATUS)
1927 			goto done;
1928 	}
1929 
1930 	/* Read link and autonegotiation status */
1931 	status = phy_read(phydev, MII_BMSR);
1932 	if (status < 0)
1933 		return status;
1934 done:
1935 	phydev->link = status & BMSR_LSTATUS ? 1 : 0;
1936 	phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
1937 
1938 	/* Consider the case that autoneg was started and "aneg complete"
1939 	 * bit has been reset, but "link up" bit not yet.
1940 	 */
1941 	if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
1942 		phydev->link = 0;
1943 
1944 	return 0;
1945 }
1946 EXPORT_SYMBOL(genphy_update_link);
1947 
1948 int genphy_read_lpa(struct phy_device *phydev)
1949 {
1950 	int lpa, lpagb;
1951 
1952 	if (phydev->autoneg == AUTONEG_ENABLE) {
1953 		if (!phydev->autoneg_complete) {
1954 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1955 							0);
1956 			mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
1957 			return 0;
1958 		}
1959 
1960 		if (phydev->is_gigabit_capable) {
1961 			lpagb = phy_read(phydev, MII_STAT1000);
1962 			if (lpagb < 0)
1963 				return lpagb;
1964 
1965 			if (lpagb & LPA_1000MSFAIL) {
1966 				int adv = phy_read(phydev, MII_CTRL1000);
1967 
1968 				if (adv < 0)
1969 					return adv;
1970 
1971 				if (adv & CTL1000_ENABLE_MASTER)
1972 					phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1973 				else
1974 					phydev_err(phydev, "Master/Slave resolution failed\n");
1975 				return -ENOLINK;
1976 			}
1977 
1978 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1979 							lpagb);
1980 		}
1981 
1982 		lpa = phy_read(phydev, MII_LPA);
1983 		if (lpa < 0)
1984 			return lpa;
1985 
1986 		mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
1987 	} else {
1988 		linkmode_zero(phydev->lp_advertising);
1989 	}
1990 
1991 	return 0;
1992 }
1993 EXPORT_SYMBOL(genphy_read_lpa);
1994 
1995 /**
1996  * genphy_read_status_fixed - read the link parameters for !aneg mode
1997  * @phydev: target phy_device struct
1998  *
1999  * Read the current duplex and speed state for a PHY operating with
2000  * autonegotiation disabled.
2001  */
2002 int genphy_read_status_fixed(struct phy_device *phydev)
2003 {
2004 	int bmcr = phy_read(phydev, MII_BMCR);
2005 
2006 	if (bmcr < 0)
2007 		return bmcr;
2008 
2009 	if (bmcr & BMCR_FULLDPLX)
2010 		phydev->duplex = DUPLEX_FULL;
2011 	else
2012 		phydev->duplex = DUPLEX_HALF;
2013 
2014 	if (bmcr & BMCR_SPEED1000)
2015 		phydev->speed = SPEED_1000;
2016 	else if (bmcr & BMCR_SPEED100)
2017 		phydev->speed = SPEED_100;
2018 	else
2019 		phydev->speed = SPEED_10;
2020 
2021 	return 0;
2022 }
2023 EXPORT_SYMBOL(genphy_read_status_fixed);
2024 
2025 /**
2026  * genphy_read_status - check the link status and update current link state
2027  * @phydev: target phy_device struct
2028  *
2029  * Description: Check the link, then figure out the current state
2030  *   by comparing what we advertise with what the link partner
2031  *   advertises.  Start by checking the gigabit possibilities,
2032  *   then move on to 10/100.
2033  */
2034 int genphy_read_status(struct phy_device *phydev)
2035 {
2036 	int err, old_link = phydev->link;
2037 
2038 	/* Update the link, but return if there was an error */
2039 	err = genphy_update_link(phydev);
2040 	if (err)
2041 		return err;
2042 
2043 	/* why bother the PHY if nothing can have changed */
2044 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2045 		return 0;
2046 
2047 	phydev->speed = SPEED_UNKNOWN;
2048 	phydev->duplex = DUPLEX_UNKNOWN;
2049 	phydev->pause = 0;
2050 	phydev->asym_pause = 0;
2051 
2052 	err = genphy_read_lpa(phydev);
2053 	if (err < 0)
2054 		return err;
2055 
2056 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2057 		phy_resolve_aneg_linkmode(phydev);
2058 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2059 		err = genphy_read_status_fixed(phydev);
2060 		if (err < 0)
2061 			return err;
2062 	}
2063 
2064 	return 0;
2065 }
2066 EXPORT_SYMBOL(genphy_read_status);
2067 
2068 /**
2069  * genphy_c37_read_status - check the link status and update current link state
2070  * @phydev: target phy_device struct
2071  *
2072  * Description: Check the link, then figure out the current state
2073  *   by comparing what we advertise with what the link partner
2074  *   advertises. This function is for Clause 37 1000Base-X mode.
2075  */
2076 int genphy_c37_read_status(struct phy_device *phydev)
2077 {
2078 	int lpa, err, old_link = phydev->link;
2079 
2080 	/* Update the link, but return if there was an error */
2081 	err = genphy_update_link(phydev);
2082 	if (err)
2083 		return err;
2084 
2085 	/* why bother the PHY if nothing can have changed */
2086 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2087 		return 0;
2088 
2089 	phydev->duplex = DUPLEX_UNKNOWN;
2090 	phydev->pause = 0;
2091 	phydev->asym_pause = 0;
2092 
2093 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2094 		lpa = phy_read(phydev, MII_LPA);
2095 		if (lpa < 0)
2096 			return lpa;
2097 
2098 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2099 				 phydev->lp_advertising, lpa & LPA_LPACK);
2100 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2101 				 phydev->lp_advertising, lpa & LPA_1000XFULL);
2102 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2103 				 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2104 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2105 				 phydev->lp_advertising,
2106 				 lpa & LPA_1000XPAUSE_ASYM);
2107 
2108 		phy_resolve_aneg_linkmode(phydev);
2109 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2110 		int bmcr = phy_read(phydev, MII_BMCR);
2111 
2112 		if (bmcr < 0)
2113 			return bmcr;
2114 
2115 		if (bmcr & BMCR_FULLDPLX)
2116 			phydev->duplex = DUPLEX_FULL;
2117 		else
2118 			phydev->duplex = DUPLEX_HALF;
2119 	}
2120 
2121 	return 0;
2122 }
2123 EXPORT_SYMBOL(genphy_c37_read_status);
2124 
2125 /**
2126  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2127  * @phydev: target phy_device struct
2128  *
2129  * Description: Perform a software PHY reset using the standard
2130  * BMCR_RESET bit and poll for the reset bit to be cleared.
2131  *
2132  * Returns: 0 on success, < 0 on failure
2133  */
2134 int genphy_soft_reset(struct phy_device *phydev)
2135 {
2136 	u16 res = BMCR_RESET;
2137 	int ret;
2138 
2139 	if (phydev->autoneg == AUTONEG_ENABLE)
2140 		res |= BMCR_ANRESTART;
2141 
2142 	ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2143 	if (ret < 0)
2144 		return ret;
2145 
2146 	ret = phy_poll_reset(phydev);
2147 	if (ret)
2148 		return ret;
2149 
2150 	/* BMCR may be reset to defaults */
2151 	if (phydev->autoneg == AUTONEG_DISABLE)
2152 		ret = genphy_setup_forced(phydev);
2153 
2154 	return ret;
2155 }
2156 EXPORT_SYMBOL(genphy_soft_reset);
2157 
2158 /**
2159  * genphy_read_abilities - read PHY abilities from Clause 22 registers
2160  * @phydev: target phy_device struct
2161  *
2162  * Description: Reads the PHY's abilities and populates
2163  * phydev->supported accordingly.
2164  *
2165  * Returns: 0 on success, < 0 on failure
2166  */
2167 int genphy_read_abilities(struct phy_device *phydev)
2168 {
2169 	int val;
2170 
2171 	linkmode_set_bit_array(phy_basic_ports_array,
2172 			       ARRAY_SIZE(phy_basic_ports_array),
2173 			       phydev->supported);
2174 
2175 	val = phy_read(phydev, MII_BMSR);
2176 	if (val < 0)
2177 		return val;
2178 
2179 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2180 			 val & BMSR_ANEGCAPABLE);
2181 
2182 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2183 			 val & BMSR_100FULL);
2184 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2185 			 val & BMSR_100HALF);
2186 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2187 			 val & BMSR_10FULL);
2188 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2189 			 val & BMSR_10HALF);
2190 
2191 	if (val & BMSR_ESTATEN) {
2192 		val = phy_read(phydev, MII_ESTATUS);
2193 		if (val < 0)
2194 			return val;
2195 
2196 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2197 				 phydev->supported, val & ESTATUS_1000_TFULL);
2198 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2199 				 phydev->supported, val & ESTATUS_1000_THALF);
2200 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2201 				 phydev->supported, val & ESTATUS_1000_XFULL);
2202 	}
2203 
2204 	return 0;
2205 }
2206 EXPORT_SYMBOL(genphy_read_abilities);
2207 
2208 /* This is used for the phy device which doesn't support the MMD extended
2209  * register access, but it does have side effect when we are trying to access
2210  * the MMD register via indirect method.
2211  */
2212 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2213 {
2214 	return -EOPNOTSUPP;
2215 }
2216 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2217 
2218 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2219 				 u16 regnum, u16 val)
2220 {
2221 	return -EOPNOTSUPP;
2222 }
2223 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2224 
2225 int genphy_suspend(struct phy_device *phydev)
2226 {
2227 	return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2228 }
2229 EXPORT_SYMBOL(genphy_suspend);
2230 
2231 int genphy_resume(struct phy_device *phydev)
2232 {
2233 	return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2234 }
2235 EXPORT_SYMBOL(genphy_resume);
2236 
2237 int genphy_loopback(struct phy_device *phydev, bool enable)
2238 {
2239 	return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2240 			  enable ? BMCR_LOOPBACK : 0);
2241 }
2242 EXPORT_SYMBOL(genphy_loopback);
2243 
2244 /**
2245  * phy_remove_link_mode - Remove a supported link mode
2246  * @phydev: phy_device structure to remove link mode from
2247  * @link_mode: Link mode to be removed
2248  *
2249  * Description: Some MACs don't support all link modes which the PHY
2250  * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
2251  * to remove a link mode.
2252  */
2253 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2254 {
2255 	linkmode_clear_bit(link_mode, phydev->supported);
2256 	phy_advertise_supported(phydev);
2257 }
2258 EXPORT_SYMBOL(phy_remove_link_mode);
2259 
2260 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2261 {
2262 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2263 		linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2264 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2265 		linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2266 }
2267 
2268 /**
2269  * phy_advertise_supported - Advertise all supported modes
2270  * @phydev: target phy_device struct
2271  *
2272  * Description: Called to advertise all supported modes, doesn't touch
2273  * pause mode advertising.
2274  */
2275 void phy_advertise_supported(struct phy_device *phydev)
2276 {
2277 	__ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2278 
2279 	linkmode_copy(new, phydev->supported);
2280 	phy_copy_pause_bits(new, phydev->advertising);
2281 	linkmode_copy(phydev->advertising, new);
2282 }
2283 EXPORT_SYMBOL(phy_advertise_supported);
2284 
2285 /**
2286  * phy_support_sym_pause - Enable support of symmetrical pause
2287  * @phydev: target phy_device struct
2288  *
2289  * Description: Called by the MAC to indicate is supports symmetrical
2290  * Pause, but not asym pause.
2291  */
2292 void phy_support_sym_pause(struct phy_device *phydev)
2293 {
2294 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2295 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2296 }
2297 EXPORT_SYMBOL(phy_support_sym_pause);
2298 
2299 /**
2300  * phy_support_asym_pause - Enable support of asym pause
2301  * @phydev: target phy_device struct
2302  *
2303  * Description: Called by the MAC to indicate is supports Asym Pause.
2304  */
2305 void phy_support_asym_pause(struct phy_device *phydev)
2306 {
2307 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2308 }
2309 EXPORT_SYMBOL(phy_support_asym_pause);
2310 
2311 /**
2312  * phy_set_sym_pause - Configure symmetric Pause
2313  * @phydev: target phy_device struct
2314  * @rx: Receiver Pause is supported
2315  * @tx: Transmit Pause is supported
2316  * @autoneg: Auto neg should be used
2317  *
2318  * Description: Configure advertised Pause support depending on if
2319  * receiver pause and pause auto neg is supported. Generally called
2320  * from the set_pauseparam .ndo.
2321  */
2322 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2323 		       bool autoneg)
2324 {
2325 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2326 
2327 	if (rx && tx && autoneg)
2328 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2329 				 phydev->supported);
2330 
2331 	linkmode_copy(phydev->advertising, phydev->supported);
2332 }
2333 EXPORT_SYMBOL(phy_set_sym_pause);
2334 
2335 /**
2336  * phy_set_asym_pause - Configure Pause and Asym Pause
2337  * @phydev: target phy_device struct
2338  * @rx: Receiver Pause is supported
2339  * @tx: Transmit Pause is supported
2340  *
2341  * Description: Configure advertised Pause support depending on if
2342  * transmit and receiver pause is supported. If there has been a
2343  * change in adverting, trigger a new autoneg. Generally called from
2344  * the set_pauseparam .ndo.
2345  */
2346 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2347 {
2348 	__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2349 
2350 	linkmode_copy(oldadv, phydev->advertising);
2351 
2352 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2353 			   phydev->advertising);
2354 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2355 			   phydev->advertising);
2356 
2357 	if (rx) {
2358 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2359 				 phydev->advertising);
2360 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2361 				 phydev->advertising);
2362 	}
2363 
2364 	if (tx)
2365 		linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2366 				    phydev->advertising);
2367 
2368 	if (!linkmode_equal(oldadv, phydev->advertising) &&
2369 	    phydev->autoneg)
2370 		phy_start_aneg(phydev);
2371 }
2372 EXPORT_SYMBOL(phy_set_asym_pause);
2373 
2374 /**
2375  * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2376  * @phydev: phy_device struct
2377  * @pp: requested pause configuration
2378  *
2379  * Description: Test if the PHY/MAC combination supports the Pause
2380  * configuration the user is requesting. Returns True if it is
2381  * supported, false otherwise.
2382  */
2383 bool phy_validate_pause(struct phy_device *phydev,
2384 			struct ethtool_pauseparam *pp)
2385 {
2386 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2387 			       phydev->supported) && pp->rx_pause)
2388 		return false;
2389 
2390 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2391 			       phydev->supported) &&
2392 	    pp->rx_pause != pp->tx_pause)
2393 		return false;
2394 
2395 	return true;
2396 }
2397 EXPORT_SYMBOL(phy_validate_pause);
2398 
2399 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2400 {
2401 	return phydrv->config_intr && phydrv->ack_interrupt;
2402 }
2403 
2404 /**
2405  * phy_probe - probe and init a PHY device
2406  * @dev: device to probe and init
2407  *
2408  * Description: Take care of setting up the phy_device structure,
2409  *   set the state to READY (the driver's init function should
2410  *   set it to STARTING if needed).
2411  */
2412 static int phy_probe(struct device *dev)
2413 {
2414 	struct phy_device *phydev = to_phy_device(dev);
2415 	struct device_driver *drv = phydev->mdio.dev.driver;
2416 	struct phy_driver *phydrv = to_phy_driver(drv);
2417 	int err = 0;
2418 
2419 	phydev->drv = phydrv;
2420 
2421 	/* Disable the interrupt if the PHY doesn't support it
2422 	 * but the interrupt is still a valid one
2423 	 */
2424 	 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2425 		phydev->irq = PHY_POLL;
2426 
2427 	if (phydrv->flags & PHY_IS_INTERNAL)
2428 		phydev->is_internal = true;
2429 
2430 	mutex_lock(&phydev->lock);
2431 
2432 	if (phydev->drv->probe) {
2433 		/* Deassert the reset signal */
2434 		phy_device_reset(phydev, 0);
2435 
2436 		err = phydev->drv->probe(phydev);
2437 		if (err) {
2438 			/* Assert the reset signal */
2439 			phy_device_reset(phydev, 1);
2440 			goto out;
2441 		}
2442 	}
2443 
2444 	/* Start out supporting everything. Eventually,
2445 	 * a controller will attach, and may modify one
2446 	 * or both of these values
2447 	 */
2448 	if (phydrv->features) {
2449 		linkmode_copy(phydev->supported, phydrv->features);
2450 	} else if (phydrv->get_features) {
2451 		err = phydrv->get_features(phydev);
2452 	} else if (phydev->is_c45) {
2453 		err = genphy_c45_pma_read_abilities(phydev);
2454 	} else {
2455 		err = genphy_read_abilities(phydev);
2456 	}
2457 
2458 	if (err)
2459 		goto out;
2460 
2461 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2462 			       phydev->supported))
2463 		phydev->autoneg = 0;
2464 
2465 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2466 			      phydev->supported))
2467 		phydev->is_gigabit_capable = 1;
2468 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2469 			      phydev->supported))
2470 		phydev->is_gigabit_capable = 1;
2471 
2472 	of_set_phy_supported(phydev);
2473 	phy_advertise_supported(phydev);
2474 
2475 	/* Get the EEE modes we want to prohibit. We will ask
2476 	 * the PHY stop advertising these mode later on
2477 	 */
2478 	of_set_phy_eee_broken(phydev);
2479 
2480 	/* The Pause Frame bits indicate that the PHY can support passing
2481 	 * pause frames. During autonegotiation, the PHYs will determine if
2482 	 * they should allow pause frames to pass.  The MAC driver should then
2483 	 * use that result to determine whether to enable flow control via
2484 	 * pause frames.
2485 	 *
2486 	 * Normally, PHY drivers should not set the Pause bits, and instead
2487 	 * allow phylib to do that.  However, there may be some situations
2488 	 * (e.g. hardware erratum) where the driver wants to set only one
2489 	 * of these bits.
2490 	 */
2491 	if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2492 	    !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2493 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2494 				 phydev->supported);
2495 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2496 				 phydev->supported);
2497 	}
2498 
2499 	/* Set the state to READY by default */
2500 	phydev->state = PHY_READY;
2501 
2502 out:
2503 	mutex_unlock(&phydev->lock);
2504 
2505 	return err;
2506 }
2507 
2508 static int phy_remove(struct device *dev)
2509 {
2510 	struct phy_device *phydev = to_phy_device(dev);
2511 
2512 	cancel_delayed_work_sync(&phydev->state_queue);
2513 
2514 	mutex_lock(&phydev->lock);
2515 	phydev->state = PHY_DOWN;
2516 	mutex_unlock(&phydev->lock);
2517 
2518 	sfp_bus_del_upstream(phydev->sfp_bus);
2519 	phydev->sfp_bus = NULL;
2520 
2521 	if (phydev->drv && phydev->drv->remove) {
2522 		phydev->drv->remove(phydev);
2523 
2524 		/* Assert the reset signal */
2525 		phy_device_reset(phydev, 1);
2526 	}
2527 	phydev->drv = NULL;
2528 
2529 	return 0;
2530 }
2531 
2532 /**
2533  * phy_driver_register - register a phy_driver with the PHY layer
2534  * @new_driver: new phy_driver to register
2535  * @owner: module owning this PHY
2536  */
2537 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2538 {
2539 	int retval;
2540 
2541 	/* Either the features are hard coded, or dynamically
2542 	 * determined. It cannot be both.
2543 	 */
2544 	if (WARN_ON(new_driver->features && new_driver->get_features)) {
2545 		pr_err("%s: features and get_features must not both be set\n",
2546 		       new_driver->name);
2547 		return -EINVAL;
2548 	}
2549 
2550 	new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2551 	new_driver->mdiodrv.driver.name = new_driver->name;
2552 	new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2553 	new_driver->mdiodrv.driver.probe = phy_probe;
2554 	new_driver->mdiodrv.driver.remove = phy_remove;
2555 	new_driver->mdiodrv.driver.owner = owner;
2556 
2557 	retval = driver_register(&new_driver->mdiodrv.driver);
2558 	if (retval) {
2559 		pr_err("%s: Error %d in registering driver\n",
2560 		       new_driver->name, retval);
2561 
2562 		return retval;
2563 	}
2564 
2565 	pr_debug("%s: Registered new driver\n", new_driver->name);
2566 
2567 	return 0;
2568 }
2569 EXPORT_SYMBOL(phy_driver_register);
2570 
2571 int phy_drivers_register(struct phy_driver *new_driver, int n,
2572 			 struct module *owner)
2573 {
2574 	int i, ret = 0;
2575 
2576 	for (i = 0; i < n; i++) {
2577 		ret = phy_driver_register(new_driver + i, owner);
2578 		if (ret) {
2579 			while (i-- > 0)
2580 				phy_driver_unregister(new_driver + i);
2581 			break;
2582 		}
2583 	}
2584 	return ret;
2585 }
2586 EXPORT_SYMBOL(phy_drivers_register);
2587 
2588 void phy_driver_unregister(struct phy_driver *drv)
2589 {
2590 	driver_unregister(&drv->mdiodrv.driver);
2591 }
2592 EXPORT_SYMBOL(phy_driver_unregister);
2593 
2594 void phy_drivers_unregister(struct phy_driver *drv, int n)
2595 {
2596 	int i;
2597 
2598 	for (i = 0; i < n; i++)
2599 		phy_driver_unregister(drv + i);
2600 }
2601 EXPORT_SYMBOL(phy_drivers_unregister);
2602 
2603 static struct phy_driver genphy_driver = {
2604 	.phy_id		= 0xffffffff,
2605 	.phy_id_mask	= 0xffffffff,
2606 	.name		= "Generic PHY",
2607 	.soft_reset	= genphy_no_soft_reset,
2608 	.get_features	= genphy_read_abilities,
2609 	.suspend	= genphy_suspend,
2610 	.resume		= genphy_resume,
2611 	.set_loopback   = genphy_loopback,
2612 };
2613 
2614 static int __init phy_init(void)
2615 {
2616 	int rc;
2617 
2618 	rc = mdio_bus_init();
2619 	if (rc)
2620 		return rc;
2621 
2622 	features_init();
2623 
2624 	rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
2625 	if (rc)
2626 		goto err_c45;
2627 
2628 	rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2629 	if (rc) {
2630 		phy_driver_unregister(&genphy_c45_driver);
2631 err_c45:
2632 		mdio_bus_exit();
2633 	}
2634 
2635 	return rc;
2636 }
2637 
2638 static void __exit phy_exit(void)
2639 {
2640 	phy_driver_unregister(&genphy_c45_driver);
2641 	phy_driver_unregister(&genphy_driver);
2642 	mdio_bus_exit();
2643 }
2644 
2645 subsys_initcall(phy_init);
2646 module_exit(phy_exit);
2647