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