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