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