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