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