treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / phy / phy_device.c
blob6a5056e0ae77578cc44006eaec2fef72cf646717
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for finding and configuring PHYs.
3 * Also contains generic PHY driver
5 * Author: Andy Fleming
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/unistd.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/mii.h>
26 #include <linux/ethtool.h>
27 #include <linux/bitmap.h>
28 #include <linux/phy.h>
29 #include <linux/phy_led_triggers.h>
30 #include <linux/sfp.h>
31 #include <linux/mdio.h>
32 #include <linux/io.h>
33 #include <linux/uaccess.h>
35 MODULE_DESCRIPTION("PHY library");
36 MODULE_AUTHOR("Andy Fleming");
37 MODULE_LICENSE("GPL");
39 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
40 EXPORT_SYMBOL_GPL(phy_basic_features);
42 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
43 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
45 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
46 EXPORT_SYMBOL_GPL(phy_gbit_features);
48 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
49 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
51 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
52 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
54 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
55 EXPORT_SYMBOL_GPL(phy_10gbit_features);
57 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
58 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
60 const int phy_basic_ports_array[3] = {
61 ETHTOOL_LINK_MODE_Autoneg_BIT,
62 ETHTOOL_LINK_MODE_TP_BIT,
63 ETHTOOL_LINK_MODE_MII_BIT,
65 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
67 const int phy_fibre_port_array[1] = {
68 ETHTOOL_LINK_MODE_FIBRE_BIT,
70 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
72 const int phy_all_ports_features_array[7] = {
73 ETHTOOL_LINK_MODE_Autoneg_BIT,
74 ETHTOOL_LINK_MODE_TP_BIT,
75 ETHTOOL_LINK_MODE_MII_BIT,
76 ETHTOOL_LINK_MODE_FIBRE_BIT,
77 ETHTOOL_LINK_MODE_AUI_BIT,
78 ETHTOOL_LINK_MODE_BNC_BIT,
79 ETHTOOL_LINK_MODE_Backplane_BIT,
81 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
83 const int phy_10_100_features_array[4] = {
84 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
85 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
86 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
87 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
89 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
91 const int phy_basic_t1_features_array[2] = {
92 ETHTOOL_LINK_MODE_TP_BIT,
93 ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
95 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
97 const int phy_gbit_features_array[2] = {
98 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
99 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
101 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
103 const int phy_10gbit_features_array[1] = {
104 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
106 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
108 const int phy_10gbit_fec_features_array[1] = {
109 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
111 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
113 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
114 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
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,
123 static void features_init(void)
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);
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);
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);
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);
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);
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);
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);
201 void phy_device_free(struct phy_device *phydev)
203 put_device(&phydev->mdio.dev);
205 EXPORT_SYMBOL(phy_device_free);
207 static void phy_mdio_device_free(struct mdio_device *mdiodev)
209 struct phy_device *phydev;
211 phydev = container_of(mdiodev, struct phy_device, mdio);
212 phy_device_free(phydev);
215 static void phy_device_release(struct device *dev)
217 kfree(to_phy_device(dev));
220 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
222 struct phy_device *phydev;
224 phydev = container_of(mdiodev, struct phy_device, mdio);
225 phy_device_remove(phydev);
228 static struct phy_driver genphy_driver;
229 extern struct phy_driver genphy_c45_driver;
231 static LIST_HEAD(phy_fixup_list);
232 static DEFINE_MUTEX(phy_fixup_lock);
234 #ifdef CONFIG_PM
235 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
237 struct device_driver *drv = phydev->mdio.dev.driver;
238 struct phy_driver *phydrv = to_phy_driver(drv);
239 struct net_device *netdev = phydev->attached_dev;
241 if (!drv || !phydrv->suspend)
242 return false;
244 /* PHY not attached? May suspend if the PHY has not already been
245 * suspended as part of a prior call to phy_disconnect() ->
246 * phy_detach() -> phy_suspend() because the parent netdev might be the
247 * MDIO bus driver and clock gated at this point.
249 if (!netdev)
250 return !phydev->suspended;
252 if (netdev->wol_enabled)
253 return false;
255 /* As long as not all affected network drivers support the
256 * wol_enabled flag, let's check for hints that WoL is enabled.
257 * Don't suspend PHY if the attached netdev parent may wake up.
258 * The parent may point to a PCI device, as in tg3 driver.
260 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
261 return false;
263 /* Also don't suspend PHY if the netdev itself may wakeup. This
264 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
265 * e.g. SoC devices.
267 if (device_may_wakeup(&netdev->dev))
268 return false;
270 return true;
273 static int mdio_bus_phy_suspend(struct device *dev)
275 struct phy_device *phydev = to_phy_device(dev);
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.
282 if (phydev->attached_dev && phydev->adjust_link)
283 phy_stop_machine(phydev);
285 if (!mdio_bus_phy_may_suspend(phydev))
286 return 0;
288 return phy_suspend(phydev);
291 static int mdio_bus_phy_resume(struct device *dev)
293 struct phy_device *phydev = to_phy_device(dev);
294 int ret;
296 if (!mdio_bus_phy_may_suspend(phydev))
297 goto no_resume;
299 ret = phy_resume(phydev);
300 if (ret < 0)
301 return ret;
303 no_resume:
304 if (phydev->attached_dev && phydev->adjust_link)
305 phy_start_machine(phydev);
307 return 0;
310 static int mdio_bus_phy_restore(struct device *dev)
312 struct phy_device *phydev = to_phy_device(dev);
313 struct net_device *netdev = phydev->attached_dev;
314 int ret;
316 if (!netdev)
317 return 0;
319 ret = phy_init_hw(phydev);
320 if (ret < 0)
321 return ret;
323 if (phydev->attached_dev && phydev->adjust_link)
324 phy_start_machine(phydev);
326 return 0;
329 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
330 .suspend = mdio_bus_phy_suspend,
331 .resume = mdio_bus_phy_resume,
332 .freeze = mdio_bus_phy_suspend,
333 .thaw = mdio_bus_phy_resume,
334 .restore = mdio_bus_phy_restore,
337 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
339 #else
341 #define MDIO_BUS_PHY_PM_OPS NULL
343 #endif /* CONFIG_PM */
346 * phy_register_fixup - creates a new phy_fixup and adds it to the list
347 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
348 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
349 * It can also be PHY_ANY_UID
350 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
351 * comparison
352 * @run: The actual code to be run when a matching PHY is found
354 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
355 int (*run)(struct phy_device *))
357 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
359 if (!fixup)
360 return -ENOMEM;
362 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
363 fixup->phy_uid = phy_uid;
364 fixup->phy_uid_mask = phy_uid_mask;
365 fixup->run = run;
367 mutex_lock(&phy_fixup_lock);
368 list_add_tail(&fixup->list, &phy_fixup_list);
369 mutex_unlock(&phy_fixup_lock);
371 return 0;
373 EXPORT_SYMBOL(phy_register_fixup);
375 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
376 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
377 int (*run)(struct phy_device *))
379 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
381 EXPORT_SYMBOL(phy_register_fixup_for_uid);
383 /* Registers a fixup to be run on the PHY with id string bus_id */
384 int phy_register_fixup_for_id(const char *bus_id,
385 int (*run)(struct phy_device *))
387 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
389 EXPORT_SYMBOL(phy_register_fixup_for_id);
392 * phy_unregister_fixup - remove a phy_fixup from the list
393 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
394 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
395 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
397 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
399 struct list_head *pos, *n;
400 struct phy_fixup *fixup;
401 int ret;
403 ret = -ENODEV;
405 mutex_lock(&phy_fixup_lock);
406 list_for_each_safe(pos, n, &phy_fixup_list) {
407 fixup = list_entry(pos, struct phy_fixup, list);
409 if ((!strcmp(fixup->bus_id, bus_id)) &&
410 ((fixup->phy_uid & phy_uid_mask) ==
411 (phy_uid & phy_uid_mask))) {
412 list_del(&fixup->list);
413 kfree(fixup);
414 ret = 0;
415 break;
418 mutex_unlock(&phy_fixup_lock);
420 return ret;
422 EXPORT_SYMBOL(phy_unregister_fixup);
424 /* Unregisters a fixup of any PHY with the UID in phy_uid */
425 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
427 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
429 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
431 /* Unregisters a fixup of the PHY with id string bus_id */
432 int phy_unregister_fixup_for_id(const char *bus_id)
434 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
436 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
438 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
439 * Fixups can be set to match any in one or more fields.
441 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
443 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
444 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
445 return 0;
447 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
448 (phydev->phy_id & fixup->phy_uid_mask))
449 if (fixup->phy_uid != PHY_ANY_UID)
450 return 0;
452 return 1;
455 /* Runs any matching fixups for this phydev */
456 static int phy_scan_fixups(struct phy_device *phydev)
458 struct phy_fixup *fixup;
460 mutex_lock(&phy_fixup_lock);
461 list_for_each_entry(fixup, &phy_fixup_list, list) {
462 if (phy_needs_fixup(phydev, fixup)) {
463 int err = fixup->run(phydev);
465 if (err < 0) {
466 mutex_unlock(&phy_fixup_lock);
467 return err;
469 phydev->has_fixups = true;
472 mutex_unlock(&phy_fixup_lock);
474 return 0;
477 static int phy_bus_match(struct device *dev, struct device_driver *drv)
479 struct phy_device *phydev = to_phy_device(dev);
480 struct phy_driver *phydrv = to_phy_driver(drv);
481 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
482 int i;
484 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
485 return 0;
487 if (phydrv->match_phy_device)
488 return phydrv->match_phy_device(phydev);
490 if (phydev->is_c45) {
491 for (i = 1; i < num_ids; i++) {
492 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
493 continue;
495 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
496 (phydev->c45_ids.device_ids[i] &
497 phydrv->phy_id_mask))
498 return 1;
500 return 0;
501 } else {
502 return (phydrv->phy_id & phydrv->phy_id_mask) ==
503 (phydev->phy_id & phydrv->phy_id_mask);
507 static ssize_t
508 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
510 struct phy_device *phydev = to_phy_device(dev);
512 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
514 static DEVICE_ATTR_RO(phy_id);
516 static ssize_t
517 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
519 struct phy_device *phydev = to_phy_device(dev);
520 const char *mode = NULL;
522 if (phy_is_internal(phydev))
523 mode = "internal";
524 else
525 mode = phy_modes(phydev->interface);
527 return sprintf(buf, "%s\n", mode);
529 static DEVICE_ATTR_RO(phy_interface);
531 static ssize_t
532 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
533 char *buf)
535 struct phy_device *phydev = to_phy_device(dev);
537 return sprintf(buf, "%d\n", phydev->has_fixups);
539 static DEVICE_ATTR_RO(phy_has_fixups);
541 static struct attribute *phy_dev_attrs[] = {
542 &dev_attr_phy_id.attr,
543 &dev_attr_phy_interface.attr,
544 &dev_attr_phy_has_fixups.attr,
545 NULL,
547 ATTRIBUTE_GROUPS(phy_dev);
549 static const struct device_type mdio_bus_phy_type = {
550 .name = "PHY",
551 .groups = phy_dev_groups,
552 .release = phy_device_release,
553 .pm = MDIO_BUS_PHY_PM_OPS,
556 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
558 int ret;
560 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
561 MDIO_ID_ARGS(phy_id));
562 /* We only check for failures in executing the usermode binary,
563 * not whether a PHY driver module exists for the PHY ID.
564 * Accept -ENOENT because this may occur in case no initramfs exists,
565 * then modprobe isn't available.
567 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
568 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
569 ret, (unsigned long)phy_id);
570 return ret;
573 return 0;
576 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
577 bool is_c45,
578 struct phy_c45_device_ids *c45_ids)
580 struct phy_device *dev;
581 struct mdio_device *mdiodev;
582 int ret = 0;
584 /* We allocate the device, and initialize the default values */
585 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
586 if (!dev)
587 return ERR_PTR(-ENOMEM);
589 mdiodev = &dev->mdio;
590 mdiodev->dev.parent = &bus->dev;
591 mdiodev->dev.bus = &mdio_bus_type;
592 mdiodev->dev.type = &mdio_bus_phy_type;
593 mdiodev->bus = bus;
594 mdiodev->bus_match = phy_bus_match;
595 mdiodev->addr = addr;
596 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
597 mdiodev->device_free = phy_mdio_device_free;
598 mdiodev->device_remove = phy_mdio_device_remove;
600 dev->speed = SPEED_UNKNOWN;
601 dev->duplex = DUPLEX_UNKNOWN;
602 dev->pause = 0;
603 dev->asym_pause = 0;
604 dev->link = 0;
605 dev->interface = PHY_INTERFACE_MODE_GMII;
607 dev->autoneg = AUTONEG_ENABLE;
609 dev->is_c45 = is_c45;
610 dev->phy_id = phy_id;
611 if (c45_ids)
612 dev->c45_ids = *c45_ids;
613 dev->irq = bus->irq[addr];
614 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
616 dev->state = PHY_DOWN;
618 mutex_init(&dev->lock);
619 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
621 /* Request the appropriate module unconditionally; don't
622 * bother trying to do so only if it isn't already loaded,
623 * because that gets complicated. A hotplug event would have
624 * done an unconditional modprobe anyway.
625 * We don't do normal hotplug because it won't work for MDIO
626 * -- because it relies on the device staying around for long
627 * enough for the driver to get loaded. With MDIO, the NIC
628 * driver will get bored and give up as soon as it finds that
629 * there's no driver _already_ loaded.
631 if (is_c45 && c45_ids) {
632 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
633 int i;
635 for (i = 1; i < num_ids; i++) {
636 if (c45_ids->device_ids[i] == 0xffffffff)
637 continue;
639 ret = phy_request_driver_module(dev,
640 c45_ids->device_ids[i]);
641 if (ret)
642 break;
644 } else {
645 ret = phy_request_driver_module(dev, phy_id);
648 if (!ret) {
649 device_initialize(&mdiodev->dev);
650 } else {
651 kfree(dev);
652 dev = ERR_PTR(ret);
655 return dev;
657 EXPORT_SYMBOL(phy_device_create);
659 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
660 * @bus: the target MII bus
661 * @addr: PHY address on the MII bus
662 * @dev_addr: MMD address in the PHY.
663 * @devices_in_package: where to store the devices in package information.
665 * Description: reads devices in package registers of a MMD at @dev_addr
666 * from PHY at @addr on @bus.
668 * Returns: 0 on success, -EIO on failure.
670 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
671 u32 *devices_in_package)
673 int phy_reg, reg_addr;
675 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
676 phy_reg = mdiobus_read(bus, addr, reg_addr);
677 if (phy_reg < 0)
678 return -EIO;
679 *devices_in_package = phy_reg << 16;
681 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
682 phy_reg = mdiobus_read(bus, addr, reg_addr);
683 if (phy_reg < 0)
684 return -EIO;
685 *devices_in_package |= phy_reg;
687 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
688 *devices_in_package &= ~BIT(0);
690 return 0;
694 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
695 * @bus: the target MII bus
696 * @addr: PHY address on the MII bus
697 * @phy_id: where to store the ID retrieved.
698 * @c45_ids: where to store the c45 ID information.
700 * If the PHY devices-in-package appears to be valid, it and the
701 * corresponding identifiers are stored in @c45_ids, zero is stored
702 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
703 * zero on success.
706 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
707 struct phy_c45_device_ids *c45_ids) {
708 int phy_reg;
709 int i, reg_addr;
710 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
711 u32 *devs = &c45_ids->devices_in_package;
713 /* Find first non-zero Devices In package. Device zero is reserved
714 * for 802.3 c45 complied PHYs, so don't probe it at first.
716 for (i = 1; i < num_ids && *devs == 0; i++) {
717 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
718 if (phy_reg < 0)
719 return -EIO;
721 if ((*devs & 0x1fffffff) == 0x1fffffff) {
722 /* If mostly Fs, there is no device there,
723 * then let's continue to probe more, as some
724 * 10G PHYs have zero Devices In package,
725 * e.g. Cortina CS4315/CS4340 PHY.
727 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
728 if (phy_reg < 0)
729 return -EIO;
730 /* no device there, let's get out of here */
731 if ((*devs & 0x1fffffff) == 0x1fffffff) {
732 *phy_id = 0xffffffff;
733 return 0;
734 } else {
735 break;
740 /* Now probe Device Identifiers for each device present. */
741 for (i = 1; i < num_ids; i++) {
742 if (!(c45_ids->devices_in_package & (1 << i)))
743 continue;
745 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
746 phy_reg = mdiobus_read(bus, addr, reg_addr);
747 if (phy_reg < 0)
748 return -EIO;
749 c45_ids->device_ids[i] = phy_reg << 16;
751 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
752 phy_reg = mdiobus_read(bus, addr, reg_addr);
753 if (phy_reg < 0)
754 return -EIO;
755 c45_ids->device_ids[i] |= phy_reg;
757 *phy_id = 0;
758 return 0;
762 * get_phy_id - reads the specified addr for its ID.
763 * @bus: the target MII bus
764 * @addr: PHY address on the MII bus
765 * @phy_id: where to store the ID retrieved.
766 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
767 * @c45_ids: where to store the c45 ID information.
769 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
770 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
771 * zero on success.
773 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
774 * its return value is in turn returned.
777 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
778 bool is_c45, struct phy_c45_device_ids *c45_ids)
780 int phy_reg;
782 if (is_c45)
783 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
785 /* Grab the bits from PHYIR1, and put them in the upper half */
786 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
787 if (phy_reg < 0) {
788 /* returning -ENODEV doesn't stop bus scanning */
789 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
792 *phy_id = phy_reg << 16;
794 /* Grab the bits from PHYIR2, and put them in the lower half */
795 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
796 if (phy_reg < 0)
797 return -EIO;
799 *phy_id |= phy_reg;
801 return 0;
805 * get_phy_device - reads the specified PHY device and returns its @phy_device
806 * struct
807 * @bus: the target MII bus
808 * @addr: PHY address on the MII bus
809 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
811 * Description: Reads the ID registers of the PHY at @addr on the
812 * @bus, then allocates and returns the phy_device to represent it.
814 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
816 struct phy_c45_device_ids c45_ids;
817 u32 phy_id = 0;
818 int r;
820 c45_ids.devices_in_package = 0;
821 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
823 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
824 if (r)
825 return ERR_PTR(r);
827 /* If the phy_id is mostly Fs, there is no device there */
828 if ((phy_id & 0x1fffffff) == 0x1fffffff)
829 return ERR_PTR(-ENODEV);
831 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
833 EXPORT_SYMBOL(get_phy_device);
836 * phy_device_register - Register the phy device on the MDIO bus
837 * @phydev: phy_device structure to be added to the MDIO bus
839 int phy_device_register(struct phy_device *phydev)
841 int err;
843 err = mdiobus_register_device(&phydev->mdio);
844 if (err)
845 return err;
847 /* Deassert the reset signal */
848 phy_device_reset(phydev, 0);
850 /* Run all of the fixups for this PHY */
851 err = phy_scan_fixups(phydev);
852 if (err) {
853 phydev_err(phydev, "failed to initialize\n");
854 goto out;
857 err = device_add(&phydev->mdio.dev);
858 if (err) {
859 phydev_err(phydev, "failed to add\n");
860 goto out;
863 return 0;
865 out:
866 /* Assert the reset signal */
867 phy_device_reset(phydev, 1);
869 mdiobus_unregister_device(&phydev->mdio);
870 return err;
872 EXPORT_SYMBOL(phy_device_register);
875 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
876 * @phydev: phy_device structure to remove
878 * This doesn't free the phy_device itself, it merely reverses the effects
879 * of phy_device_register(). Use phy_device_free() to free the device
880 * after calling this function.
882 void phy_device_remove(struct phy_device *phydev)
884 if (phydev->mii_ts)
885 unregister_mii_timestamper(phydev->mii_ts);
887 device_del(&phydev->mdio.dev);
889 /* Assert the reset signal */
890 phy_device_reset(phydev, 1);
892 mdiobus_unregister_device(&phydev->mdio);
894 EXPORT_SYMBOL(phy_device_remove);
897 * phy_find_first - finds the first PHY device on the bus
898 * @bus: the target MII bus
900 struct phy_device *phy_find_first(struct mii_bus *bus)
902 struct phy_device *phydev;
903 int addr;
905 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
906 phydev = mdiobus_get_phy(bus, addr);
907 if (phydev)
908 return phydev;
910 return NULL;
912 EXPORT_SYMBOL(phy_find_first);
914 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
916 struct net_device *netdev = phydev->attached_dev;
918 if (do_carrier) {
919 if (up)
920 netif_carrier_on(netdev);
921 else
922 netif_carrier_off(netdev);
924 phydev->adjust_link(netdev);
925 if (phydev->mii_ts && phydev->mii_ts->link_state)
926 phydev->mii_ts->link_state(phydev->mii_ts, phydev);
930 * phy_prepare_link - prepares the PHY layer to monitor link status
931 * @phydev: target phy_device struct
932 * @handler: callback function for link status change notifications
934 * Description: Tells the PHY infrastructure to handle the
935 * gory details on monitoring link status (whether through
936 * polling or an interrupt), and to call back to the
937 * connected device driver when the link status changes.
938 * If you want to monitor your own link state, don't call
939 * this function.
941 static void phy_prepare_link(struct phy_device *phydev,
942 void (*handler)(struct net_device *))
944 phydev->adjust_link = handler;
948 * phy_connect_direct - connect an ethernet device to a specific phy_device
949 * @dev: the network device to connect
950 * @phydev: the pointer to the phy device
951 * @handler: callback function for state change notifications
952 * @interface: PHY device's interface
954 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
955 void (*handler)(struct net_device *),
956 phy_interface_t interface)
958 int rc;
960 if (!dev)
961 return -EINVAL;
963 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
964 if (rc)
965 return rc;
967 phy_prepare_link(phydev, handler);
968 if (phy_interrupt_is_valid(phydev))
969 phy_request_interrupt(phydev);
971 return 0;
973 EXPORT_SYMBOL(phy_connect_direct);
976 * phy_connect - connect an ethernet device to a PHY device
977 * @dev: the network device to connect
978 * @bus_id: the id string of the PHY device to connect
979 * @handler: callback function for state change notifications
980 * @interface: PHY device's interface
982 * Description: Convenience function for connecting ethernet
983 * devices to PHY devices. The default behavior is for
984 * the PHY infrastructure to handle everything, and only notify
985 * the connected driver when the link status changes. If you
986 * don't want, or can't use the provided functionality, you may
987 * choose to call only the subset of functions which provide
988 * the desired functionality.
990 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
991 void (*handler)(struct net_device *),
992 phy_interface_t interface)
994 struct phy_device *phydev;
995 struct device *d;
996 int rc;
998 /* Search the list of PHY devices on the mdio bus for the
999 * PHY with the requested name
1001 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1002 if (!d) {
1003 pr_err("PHY %s not found\n", bus_id);
1004 return ERR_PTR(-ENODEV);
1006 phydev = to_phy_device(d);
1008 rc = phy_connect_direct(dev, phydev, handler, interface);
1009 put_device(d);
1010 if (rc)
1011 return ERR_PTR(rc);
1013 return phydev;
1015 EXPORT_SYMBOL(phy_connect);
1018 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1019 * device
1020 * @phydev: target phy_device struct
1022 void phy_disconnect(struct phy_device *phydev)
1024 if (phy_is_started(phydev))
1025 phy_stop(phydev);
1027 if (phy_interrupt_is_valid(phydev))
1028 phy_free_interrupt(phydev);
1030 phydev->adjust_link = NULL;
1032 phy_detach(phydev);
1034 EXPORT_SYMBOL(phy_disconnect);
1037 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1038 * @phydev: The PHY device to poll
1040 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1041 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1042 * register must be polled until the BMCR_RESET bit clears.
1044 * Furthermore, any attempts to write to PHY registers may have no effect
1045 * or even generate MDIO bus errors until this is complete.
1047 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1048 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1049 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1050 * effort to support such broken PHYs, this function is separate from the
1051 * standard phy_init_hw() which will zero all the other bits in the BMCR
1052 * and reapply all driver-specific and board-specific fixups.
1054 static int phy_poll_reset(struct phy_device *phydev)
1056 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1057 unsigned int retries = 12;
1058 int ret;
1060 do {
1061 msleep(50);
1062 ret = phy_read(phydev, MII_BMCR);
1063 if (ret < 0)
1064 return ret;
1065 } while (ret & BMCR_RESET && --retries);
1066 if (ret & BMCR_RESET)
1067 return -ETIMEDOUT;
1069 /* Some chips (smsc911x) may still need up to another 1ms after the
1070 * BMCR_RESET bit is cleared before they are usable.
1072 msleep(1);
1073 return 0;
1076 int phy_init_hw(struct phy_device *phydev)
1078 int ret = 0;
1080 /* Deassert the reset signal */
1081 phy_device_reset(phydev, 0);
1083 if (!phydev->drv)
1084 return 0;
1086 if (phydev->drv->soft_reset)
1087 ret = phydev->drv->soft_reset(phydev);
1089 if (ret < 0)
1090 return ret;
1092 ret = phy_scan_fixups(phydev);
1093 if (ret < 0)
1094 return ret;
1096 if (phydev->drv->config_init)
1097 ret = phydev->drv->config_init(phydev);
1099 return ret;
1101 EXPORT_SYMBOL(phy_init_hw);
1103 void phy_attached_info(struct phy_device *phydev)
1105 phy_attached_print(phydev, NULL);
1107 EXPORT_SYMBOL(phy_attached_info);
1109 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1110 char *phy_attached_info_irq(struct phy_device *phydev)
1112 char *irq_str;
1113 char irq_num[8];
1115 switch(phydev->irq) {
1116 case PHY_POLL:
1117 irq_str = "POLL";
1118 break;
1119 case PHY_IGNORE_INTERRUPT:
1120 irq_str = "IGNORE";
1121 break;
1122 default:
1123 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1124 irq_str = irq_num;
1125 break;
1128 return kasprintf(GFP_KERNEL, "%s", irq_str);
1130 EXPORT_SYMBOL(phy_attached_info_irq);
1132 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1134 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1135 char *irq_str = phy_attached_info_irq(phydev);
1137 if (!fmt) {
1138 phydev_info(phydev, ATTACHED_FMT "\n",
1139 drv_name, phydev_name(phydev),
1140 irq_str);
1141 } else {
1142 va_list ap;
1144 phydev_info(phydev, ATTACHED_FMT,
1145 drv_name, phydev_name(phydev),
1146 irq_str);
1148 va_start(ap, fmt);
1149 vprintk(fmt, ap);
1150 va_end(ap);
1152 kfree(irq_str);
1154 EXPORT_SYMBOL(phy_attached_print);
1156 static void phy_sysfs_create_links(struct phy_device *phydev)
1158 struct net_device *dev = phydev->attached_dev;
1159 int err;
1161 if (!dev)
1162 return;
1164 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1165 "attached_dev");
1166 if (err)
1167 return;
1169 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1170 &phydev->mdio.dev.kobj,
1171 "phydev");
1172 if (err) {
1173 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1174 kobject_name(&phydev->mdio.dev.kobj),
1175 err);
1176 /* non-fatal - some net drivers can use one netdevice
1177 * with more then one phy
1181 phydev->sysfs_links = true;
1184 static ssize_t
1185 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1186 char *buf)
1188 struct phy_device *phydev = to_phy_device(dev);
1190 return sprintf(buf, "%d\n", !phydev->attached_dev);
1192 static DEVICE_ATTR_RO(phy_standalone);
1195 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1196 * @upstream: pointer to the phy device
1197 * @bus: sfp bus representing cage being attached
1199 * This is used to fill in the sfp_upstream_ops .attach member.
1201 void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1203 struct phy_device *phydev = upstream;
1205 if (phydev->attached_dev)
1206 phydev->attached_dev->sfp_bus = bus;
1207 phydev->sfp_bus_attached = true;
1209 EXPORT_SYMBOL(phy_sfp_attach);
1212 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1213 * @upstream: pointer to the phy device
1214 * @bus: sfp bus representing cage being attached
1216 * This is used to fill in the sfp_upstream_ops .detach member.
1218 void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1220 struct phy_device *phydev = upstream;
1222 if (phydev->attached_dev)
1223 phydev->attached_dev->sfp_bus = NULL;
1224 phydev->sfp_bus_attached = false;
1226 EXPORT_SYMBOL(phy_sfp_detach);
1229 * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1230 * @phydev: Pointer to phy_device
1231 * @ops: SFP's upstream operations
1233 int phy_sfp_probe(struct phy_device *phydev,
1234 const struct sfp_upstream_ops *ops)
1236 struct sfp_bus *bus;
1237 int ret;
1239 if (phydev->mdio.dev.fwnode) {
1240 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1241 if (IS_ERR(bus))
1242 return PTR_ERR(bus);
1244 phydev->sfp_bus = bus;
1246 ret = sfp_bus_add_upstream(bus, phydev, ops);
1247 sfp_bus_put(bus);
1249 return 0;
1251 EXPORT_SYMBOL(phy_sfp_probe);
1254 * phy_attach_direct - attach a network device to a given PHY device pointer
1255 * @dev: network device to attach
1256 * @phydev: Pointer to phy_device to attach
1257 * @flags: PHY device's dev_flags
1258 * @interface: PHY device's interface
1260 * Description: Called by drivers to attach to a particular PHY
1261 * device. The phy_device is found, and properly hooked up
1262 * to the phy_driver. If no driver is attached, then a
1263 * generic driver is used. The phy_device is given a ptr to
1264 * the attaching device, and given a callback for link status
1265 * change. The phy_device is returned to the attaching driver.
1266 * This function takes a reference on the phy device.
1268 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1269 u32 flags, phy_interface_t interface)
1271 struct mii_bus *bus = phydev->mdio.bus;
1272 struct device *d = &phydev->mdio.dev;
1273 struct module *ndev_owner = NULL;
1274 bool using_genphy = false;
1275 int err;
1277 /* For Ethernet device drivers that register their own MDIO bus, we
1278 * will have bus->owner match ndev_mod, so we do not want to increment
1279 * our own module->refcnt here, otherwise we would not be able to
1280 * unload later on.
1282 if (dev)
1283 ndev_owner = dev->dev.parent->driver->owner;
1284 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1285 phydev_err(phydev, "failed to get the bus module\n");
1286 return -EIO;
1289 get_device(d);
1291 /* Assume that if there is no driver, that it doesn't
1292 * exist, and we should use the genphy driver.
1294 if (!d->driver) {
1295 if (phydev->is_c45)
1296 d->driver = &genphy_c45_driver.mdiodrv.driver;
1297 else
1298 d->driver = &genphy_driver.mdiodrv.driver;
1300 using_genphy = true;
1303 if (!try_module_get(d->driver->owner)) {
1304 phydev_err(phydev, "failed to get the device driver module\n");
1305 err = -EIO;
1306 goto error_put_device;
1309 if (using_genphy) {
1310 err = d->driver->probe(d);
1311 if (err >= 0)
1312 err = device_bind_driver(d);
1314 if (err)
1315 goto error_module_put;
1318 if (phydev->attached_dev) {
1319 dev_err(&dev->dev, "PHY already attached\n");
1320 err = -EBUSY;
1321 goto error;
1324 phydev->phy_link_change = phy_link_change;
1325 if (dev) {
1326 phydev->attached_dev = dev;
1327 dev->phydev = phydev;
1329 if (phydev->sfp_bus_attached)
1330 dev->sfp_bus = phydev->sfp_bus;
1333 /* Some Ethernet drivers try to connect to a PHY device before
1334 * calling register_netdevice() -> netdev_register_kobject() and
1335 * does the dev->dev.kobj initialization. Here we only check for
1336 * success which indicates that the network device kobject is
1337 * ready. Once we do that we still need to keep track of whether
1338 * links were successfully set up or not for phy_detach() to
1339 * remove them accordingly.
1341 phydev->sysfs_links = false;
1343 phy_sysfs_create_links(phydev);
1345 if (!phydev->attached_dev) {
1346 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1347 &dev_attr_phy_standalone.attr);
1348 if (err)
1349 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1352 phydev->dev_flags |= flags;
1354 phydev->interface = interface;
1356 phydev->state = PHY_READY;
1358 /* Initial carrier state is off as the phy is about to be
1359 * (re)initialized.
1361 if (dev)
1362 netif_carrier_off(phydev->attached_dev);
1364 /* Do initial configuration here, now that
1365 * we have certain key parameters
1366 * (dev_flags and interface)
1368 err = phy_init_hw(phydev);
1369 if (err)
1370 goto error;
1372 phy_resume(phydev);
1373 phy_led_triggers_register(phydev);
1375 return err;
1377 error:
1378 /* phy_detach() does all of the cleanup below */
1379 phy_detach(phydev);
1380 return err;
1382 error_module_put:
1383 module_put(d->driver->owner);
1384 error_put_device:
1385 put_device(d);
1386 if (ndev_owner != bus->owner)
1387 module_put(bus->owner);
1388 return err;
1390 EXPORT_SYMBOL(phy_attach_direct);
1393 * phy_attach - attach a network device to a particular PHY device
1394 * @dev: network device to attach
1395 * @bus_id: Bus ID of PHY device to attach
1396 * @interface: PHY device's interface
1398 * Description: Same as phy_attach_direct() except that a PHY bus_id
1399 * string is passed instead of a pointer to a struct phy_device.
1401 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1402 phy_interface_t interface)
1404 struct bus_type *bus = &mdio_bus_type;
1405 struct phy_device *phydev;
1406 struct device *d;
1407 int rc;
1409 if (!dev)
1410 return ERR_PTR(-EINVAL);
1412 /* Search the list of PHY devices on the mdio bus for the
1413 * PHY with the requested name
1415 d = bus_find_device_by_name(bus, NULL, bus_id);
1416 if (!d) {
1417 pr_err("PHY %s not found\n", bus_id);
1418 return ERR_PTR(-ENODEV);
1420 phydev = to_phy_device(d);
1422 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1423 put_device(d);
1424 if (rc)
1425 return ERR_PTR(rc);
1427 return phydev;
1429 EXPORT_SYMBOL(phy_attach);
1431 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1432 struct device_driver *driver)
1434 struct device *d = &phydev->mdio.dev;
1435 bool ret = false;
1437 if (!phydev->drv)
1438 return ret;
1440 get_device(d);
1441 ret = d->driver == driver;
1442 put_device(d);
1444 return ret;
1447 bool phy_driver_is_genphy(struct phy_device *phydev)
1449 return phy_driver_is_genphy_kind(phydev,
1450 &genphy_driver.mdiodrv.driver);
1452 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1454 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1456 return phy_driver_is_genphy_kind(phydev,
1457 &genphy_c45_driver.mdiodrv.driver);
1459 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1462 * phy_detach - detach a PHY device from its network device
1463 * @phydev: target phy_device struct
1465 * This detaches the phy device from its network device and the phy
1466 * driver, and drops the reference count taken in phy_attach_direct().
1468 void phy_detach(struct phy_device *phydev)
1470 struct net_device *dev = phydev->attached_dev;
1471 struct module *ndev_owner = NULL;
1472 struct mii_bus *bus;
1474 if (phydev->sysfs_links) {
1475 if (dev)
1476 sysfs_remove_link(&dev->dev.kobj, "phydev");
1477 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1480 if (!phydev->attached_dev)
1481 sysfs_remove_file(&phydev->mdio.dev.kobj,
1482 &dev_attr_phy_standalone.attr);
1484 phy_suspend(phydev);
1485 if (dev) {
1486 phydev->attached_dev->phydev = NULL;
1487 phydev->attached_dev = NULL;
1489 phydev->phylink = NULL;
1491 phy_led_triggers_unregister(phydev);
1493 module_put(phydev->mdio.dev.driver->owner);
1495 /* If the device had no specific driver before (i.e. - it
1496 * was using the generic driver), we unbind the device
1497 * from the generic driver so that there's a chance a
1498 * real driver could be loaded
1500 if (phy_driver_is_genphy(phydev) ||
1501 phy_driver_is_genphy_10g(phydev))
1502 device_release_driver(&phydev->mdio.dev);
1505 * The phydev might go away on the put_device() below, so avoid
1506 * a use-after-free bug by reading the underlying bus first.
1508 bus = phydev->mdio.bus;
1510 put_device(&phydev->mdio.dev);
1511 if (dev)
1512 ndev_owner = dev->dev.parent->driver->owner;
1513 if (ndev_owner != bus->owner)
1514 module_put(bus->owner);
1516 /* Assert the reset signal */
1517 phy_device_reset(phydev, 1);
1519 EXPORT_SYMBOL(phy_detach);
1521 int phy_suspend(struct phy_device *phydev)
1523 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1524 struct net_device *netdev = phydev->attached_dev;
1525 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1526 int ret = 0;
1528 /* If the device has WOL enabled, we cannot suspend the PHY */
1529 phy_ethtool_get_wol(phydev, &wol);
1530 if (wol.wolopts || (netdev && netdev->wol_enabled))
1531 return -EBUSY;
1533 if (phydev->drv && phydrv->suspend)
1534 ret = phydrv->suspend(phydev);
1536 if (ret)
1537 return ret;
1539 phydev->suspended = true;
1541 return ret;
1543 EXPORT_SYMBOL(phy_suspend);
1545 int __phy_resume(struct phy_device *phydev)
1547 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1548 int ret = 0;
1550 WARN_ON(!mutex_is_locked(&phydev->lock));
1552 if (phydev->drv && phydrv->resume)
1553 ret = phydrv->resume(phydev);
1555 if (ret)
1556 return ret;
1558 phydev->suspended = false;
1560 return ret;
1562 EXPORT_SYMBOL(__phy_resume);
1564 int phy_resume(struct phy_device *phydev)
1566 int ret;
1568 mutex_lock(&phydev->lock);
1569 ret = __phy_resume(phydev);
1570 mutex_unlock(&phydev->lock);
1572 return ret;
1574 EXPORT_SYMBOL(phy_resume);
1576 int phy_loopback(struct phy_device *phydev, bool enable)
1578 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1579 int ret = 0;
1581 mutex_lock(&phydev->lock);
1583 if (enable && phydev->loopback_enabled) {
1584 ret = -EBUSY;
1585 goto out;
1588 if (!enable && !phydev->loopback_enabled) {
1589 ret = -EINVAL;
1590 goto out;
1593 if (phydev->drv && phydrv->set_loopback)
1594 ret = phydrv->set_loopback(phydev, enable);
1595 else
1596 ret = -EOPNOTSUPP;
1598 if (ret)
1599 goto out;
1601 phydev->loopback_enabled = enable;
1603 out:
1604 mutex_unlock(&phydev->lock);
1605 return ret;
1607 EXPORT_SYMBOL(phy_loopback);
1610 * phy_reset_after_clk_enable - perform a PHY reset if needed
1611 * @phydev: target phy_device struct
1613 * Description: Some PHYs are known to need a reset after their refclk was
1614 * enabled. This function evaluates the flags and perform the reset if it's
1615 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1616 * was reset.
1618 int phy_reset_after_clk_enable(struct phy_device *phydev)
1620 if (!phydev || !phydev->drv)
1621 return -ENODEV;
1623 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1624 phy_device_reset(phydev, 1);
1625 phy_device_reset(phydev, 0);
1626 return 1;
1629 return 0;
1631 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1633 /* Generic PHY support and helper functions */
1636 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1637 * @phydev: target phy_device struct
1639 * Description: Writes MII_ADVERTISE with the appropriate values,
1640 * after sanitizing the values to make sure we only advertise
1641 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1642 * hasn't changed, and > 0 if it has changed.
1644 static int genphy_config_advert(struct phy_device *phydev)
1646 int err, bmsr, changed = 0;
1647 u32 adv;
1649 /* Only allow advertising what this PHY supports */
1650 linkmode_and(phydev->advertising, phydev->advertising,
1651 phydev->supported);
1653 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1655 /* Setup standard advertisement */
1656 err = phy_modify_changed(phydev, MII_ADVERTISE,
1657 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1658 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1659 adv);
1660 if (err < 0)
1661 return err;
1662 if (err > 0)
1663 changed = 1;
1665 bmsr = phy_read(phydev, MII_BMSR);
1666 if (bmsr < 0)
1667 return bmsr;
1669 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1670 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1671 * logical 1.
1673 if (!(bmsr & BMSR_ESTATEN))
1674 return changed;
1676 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1678 err = phy_modify_changed(phydev, MII_CTRL1000,
1679 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1680 adv);
1681 if (err < 0)
1682 return err;
1683 if (err > 0)
1684 changed = 1;
1686 return changed;
1690 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1691 * @phydev: target phy_device struct
1693 * Description: Writes MII_ADVERTISE with the appropriate values,
1694 * after sanitizing the values to make sure we only advertise
1695 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1696 * hasn't changed, and > 0 if it has changed. This function is intended
1697 * for Clause 37 1000Base-X mode.
1699 static int genphy_c37_config_advert(struct phy_device *phydev)
1701 u16 adv = 0;
1703 /* Only allow advertising what this PHY supports */
1704 linkmode_and(phydev->advertising, phydev->advertising,
1705 phydev->supported);
1707 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1708 phydev->advertising))
1709 adv |= ADVERTISE_1000XFULL;
1710 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1711 phydev->advertising))
1712 adv |= ADVERTISE_1000XPAUSE;
1713 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1714 phydev->advertising))
1715 adv |= ADVERTISE_1000XPSE_ASYM;
1717 return phy_modify_changed(phydev, MII_ADVERTISE,
1718 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1719 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1720 adv);
1724 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1725 * @phydev: target phy_device struct
1727 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1728 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1729 * changed, and 1 if it has changed.
1731 int genphy_config_eee_advert(struct phy_device *phydev)
1733 int err;
1735 /* Nothing to disable */
1736 if (!phydev->eee_broken_modes)
1737 return 0;
1739 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1740 phydev->eee_broken_modes, 0);
1741 /* If the call failed, we assume that EEE is not supported */
1742 return err < 0 ? 0 : err;
1744 EXPORT_SYMBOL(genphy_config_eee_advert);
1747 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1748 * @phydev: target phy_device struct
1750 * Description: Configures MII_BMCR to force speed/duplex
1751 * to the values in phydev. Assumes that the values are valid.
1752 * Please see phy_sanitize_settings().
1754 int genphy_setup_forced(struct phy_device *phydev)
1756 u16 ctl = 0;
1758 phydev->pause = 0;
1759 phydev->asym_pause = 0;
1761 if (SPEED_1000 == phydev->speed)
1762 ctl |= BMCR_SPEED1000;
1763 else if (SPEED_100 == phydev->speed)
1764 ctl |= BMCR_SPEED100;
1766 if (DUPLEX_FULL == phydev->duplex)
1767 ctl |= BMCR_FULLDPLX;
1769 return phy_modify(phydev, MII_BMCR,
1770 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1772 EXPORT_SYMBOL(genphy_setup_forced);
1775 * genphy_restart_aneg - Enable and Restart Autonegotiation
1776 * @phydev: target phy_device struct
1778 int genphy_restart_aneg(struct phy_device *phydev)
1780 /* Don't isolate the PHY if we're negotiating */
1781 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1782 BMCR_ANENABLE | BMCR_ANRESTART);
1784 EXPORT_SYMBOL(genphy_restart_aneg);
1787 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
1788 * @phydev: target phy_device struct
1789 * @restart: whether aneg restart is requested
1791 * Check, and restart auto-negotiation if needed.
1793 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
1795 int ret = 0;
1797 if (!restart) {
1798 /* Advertisement hasn't changed, but maybe aneg was never on to
1799 * begin with? Or maybe phy was isolated?
1801 ret = phy_read(phydev, MII_BMCR);
1802 if (ret < 0)
1803 return ret;
1805 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
1806 restart = true;
1809 if (restart)
1810 ret = genphy_restart_aneg(phydev);
1812 return ret;
1814 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
1817 * __genphy_config_aneg - restart auto-negotiation or write BMCR
1818 * @phydev: target phy_device struct
1819 * @changed: whether autoneg is requested
1821 * Description: If auto-negotiation is enabled, we configure the
1822 * advertising, and then restart auto-negotiation. If it is not
1823 * enabled, then we write the BMCR.
1825 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
1827 int err;
1829 if (genphy_config_eee_advert(phydev))
1830 changed = true;
1832 if (AUTONEG_ENABLE != phydev->autoneg)
1833 return genphy_setup_forced(phydev);
1835 err = genphy_config_advert(phydev);
1836 if (err < 0) /* error */
1837 return err;
1838 else if (err)
1839 changed = true;
1841 return genphy_check_and_restart_aneg(phydev, changed);
1843 EXPORT_SYMBOL(__genphy_config_aneg);
1846 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
1847 * @phydev: target phy_device struct
1849 * Description: If auto-negotiation is enabled, we configure the
1850 * advertising, and then restart auto-negotiation. If it is not
1851 * enabled, then we write the BMCR. This function is intended
1852 * for use with Clause 37 1000Base-X mode.
1854 int genphy_c37_config_aneg(struct phy_device *phydev)
1856 int err, changed;
1858 if (phydev->autoneg != AUTONEG_ENABLE)
1859 return genphy_setup_forced(phydev);
1861 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
1862 BMCR_SPEED1000);
1863 if (err)
1864 return err;
1866 changed = genphy_c37_config_advert(phydev);
1867 if (changed < 0) /* error */
1868 return changed;
1870 if (!changed) {
1871 /* Advertisement hasn't changed, but maybe aneg was never on to
1872 * begin with? Or maybe phy was isolated?
1874 int ctl = phy_read(phydev, MII_BMCR);
1876 if (ctl < 0)
1877 return ctl;
1879 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1880 changed = 1; /* do restart aneg */
1883 /* Only restart aneg if we are advertising something different
1884 * than we were before.
1886 if (changed > 0)
1887 return genphy_restart_aneg(phydev);
1889 return 0;
1891 EXPORT_SYMBOL(genphy_c37_config_aneg);
1894 * genphy_aneg_done - return auto-negotiation status
1895 * @phydev: target phy_device struct
1897 * Description: Reads the status register and returns 0 either if
1898 * auto-negotiation is incomplete, or if there was an error.
1899 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1901 int genphy_aneg_done(struct phy_device *phydev)
1903 int retval = phy_read(phydev, MII_BMSR);
1905 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1907 EXPORT_SYMBOL(genphy_aneg_done);
1910 * genphy_update_link - update link status in @phydev
1911 * @phydev: target phy_device struct
1913 * Description: Update the value in phydev->link to reflect the
1914 * current link value. In order to do this, we need to read
1915 * the status register twice, keeping the second value.
1917 int genphy_update_link(struct phy_device *phydev)
1919 int status = 0, bmcr;
1921 bmcr = phy_read(phydev, MII_BMCR);
1922 if (bmcr < 0)
1923 return bmcr;
1925 /* Autoneg is being started, therefore disregard BMSR value and
1926 * report link as down.
1928 if (bmcr & BMCR_ANRESTART)
1929 goto done;
1931 /* The link state is latched low so that momentary link
1932 * drops can be detected. Do not double-read the status
1933 * in polling mode to detect such short link drops.
1935 if (!phy_polling_mode(phydev)) {
1936 status = phy_read(phydev, MII_BMSR);
1937 if (status < 0)
1938 return status;
1939 else if (status & BMSR_LSTATUS)
1940 goto done;
1943 /* Read link and autonegotiation status */
1944 status = phy_read(phydev, MII_BMSR);
1945 if (status < 0)
1946 return status;
1947 done:
1948 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
1949 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
1951 /* Consider the case that autoneg was started and "aneg complete"
1952 * bit has been reset, but "link up" bit not yet.
1954 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
1955 phydev->link = 0;
1957 return 0;
1959 EXPORT_SYMBOL(genphy_update_link);
1961 int genphy_read_lpa(struct phy_device *phydev)
1963 int lpa, lpagb;
1965 if (phydev->autoneg == AUTONEG_ENABLE) {
1966 if (!phydev->autoneg_complete) {
1967 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1969 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
1970 return 0;
1973 if (phydev->is_gigabit_capable) {
1974 lpagb = phy_read(phydev, MII_STAT1000);
1975 if (lpagb < 0)
1976 return lpagb;
1978 if (lpagb & LPA_1000MSFAIL) {
1979 int adv = phy_read(phydev, MII_CTRL1000);
1981 if (adv < 0)
1982 return adv;
1984 if (adv & CTL1000_ENABLE_MASTER)
1985 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1986 else
1987 phydev_err(phydev, "Master/Slave resolution failed\n");
1988 return -ENOLINK;
1991 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1992 lpagb);
1995 lpa = phy_read(phydev, MII_LPA);
1996 if (lpa < 0)
1997 return lpa;
1999 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2000 } else {
2001 linkmode_zero(phydev->lp_advertising);
2004 return 0;
2006 EXPORT_SYMBOL(genphy_read_lpa);
2009 * genphy_read_status_fixed - read the link parameters for !aneg mode
2010 * @phydev: target phy_device struct
2012 * Read the current duplex and speed state for a PHY operating with
2013 * autonegotiation disabled.
2015 int genphy_read_status_fixed(struct phy_device *phydev)
2017 int bmcr = phy_read(phydev, MII_BMCR);
2019 if (bmcr < 0)
2020 return bmcr;
2022 if (bmcr & BMCR_FULLDPLX)
2023 phydev->duplex = DUPLEX_FULL;
2024 else
2025 phydev->duplex = DUPLEX_HALF;
2027 if (bmcr & BMCR_SPEED1000)
2028 phydev->speed = SPEED_1000;
2029 else if (bmcr & BMCR_SPEED100)
2030 phydev->speed = SPEED_100;
2031 else
2032 phydev->speed = SPEED_10;
2034 return 0;
2036 EXPORT_SYMBOL(genphy_read_status_fixed);
2039 * genphy_read_status - check the link status and update current link state
2040 * @phydev: target phy_device struct
2042 * Description: Check the link, then figure out the current state
2043 * by comparing what we advertise with what the link partner
2044 * advertises. Start by checking the gigabit possibilities,
2045 * then move on to 10/100.
2047 int genphy_read_status(struct phy_device *phydev)
2049 int err, old_link = phydev->link;
2051 /* Update the link, but return if there was an error */
2052 err = genphy_update_link(phydev);
2053 if (err)
2054 return err;
2056 /* why bother the PHY if nothing can have changed */
2057 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2058 return 0;
2060 phydev->speed = SPEED_UNKNOWN;
2061 phydev->duplex = DUPLEX_UNKNOWN;
2062 phydev->pause = 0;
2063 phydev->asym_pause = 0;
2065 err = genphy_read_lpa(phydev);
2066 if (err < 0)
2067 return err;
2069 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2070 phy_resolve_aneg_linkmode(phydev);
2071 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2072 err = genphy_read_status_fixed(phydev);
2073 if (err < 0)
2074 return err;
2077 return 0;
2079 EXPORT_SYMBOL(genphy_read_status);
2082 * genphy_c37_read_status - check the link status and update current link state
2083 * @phydev: target phy_device struct
2085 * Description: Check the link, then figure out the current state
2086 * by comparing what we advertise with what the link partner
2087 * advertises. This function is for Clause 37 1000Base-X mode.
2089 int genphy_c37_read_status(struct phy_device *phydev)
2091 int lpa, err, old_link = phydev->link;
2093 /* Update the link, but return if there was an error */
2094 err = genphy_update_link(phydev);
2095 if (err)
2096 return err;
2098 /* why bother the PHY if nothing can have changed */
2099 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2100 return 0;
2102 phydev->duplex = DUPLEX_UNKNOWN;
2103 phydev->pause = 0;
2104 phydev->asym_pause = 0;
2106 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2107 lpa = phy_read(phydev, MII_LPA);
2108 if (lpa < 0)
2109 return lpa;
2111 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2112 phydev->lp_advertising, lpa & LPA_LPACK);
2113 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2114 phydev->lp_advertising, lpa & LPA_1000XFULL);
2115 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2116 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2117 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2118 phydev->lp_advertising,
2119 lpa & LPA_1000XPAUSE_ASYM);
2121 phy_resolve_aneg_linkmode(phydev);
2122 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2123 int bmcr = phy_read(phydev, MII_BMCR);
2125 if (bmcr < 0)
2126 return bmcr;
2128 if (bmcr & BMCR_FULLDPLX)
2129 phydev->duplex = DUPLEX_FULL;
2130 else
2131 phydev->duplex = DUPLEX_HALF;
2134 return 0;
2136 EXPORT_SYMBOL(genphy_c37_read_status);
2139 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2140 * @phydev: target phy_device struct
2142 * Description: Perform a software PHY reset using the standard
2143 * BMCR_RESET bit and poll for the reset bit to be cleared.
2145 * Returns: 0 on success, < 0 on failure
2147 int genphy_soft_reset(struct phy_device *phydev)
2149 u16 res = BMCR_RESET;
2150 int ret;
2152 if (phydev->autoneg == AUTONEG_ENABLE)
2153 res |= BMCR_ANRESTART;
2155 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2156 if (ret < 0)
2157 return ret;
2159 ret = phy_poll_reset(phydev);
2160 if (ret)
2161 return ret;
2163 /* BMCR may be reset to defaults */
2164 if (phydev->autoneg == AUTONEG_DISABLE)
2165 ret = genphy_setup_forced(phydev);
2167 return ret;
2169 EXPORT_SYMBOL(genphy_soft_reset);
2172 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2173 * @phydev: target phy_device struct
2175 * Description: Reads the PHY's abilities and populates
2176 * phydev->supported accordingly.
2178 * Returns: 0 on success, < 0 on failure
2180 int genphy_read_abilities(struct phy_device *phydev)
2182 int val;
2184 linkmode_set_bit_array(phy_basic_ports_array,
2185 ARRAY_SIZE(phy_basic_ports_array),
2186 phydev->supported);
2188 val = phy_read(phydev, MII_BMSR);
2189 if (val < 0)
2190 return val;
2192 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2193 val & BMSR_ANEGCAPABLE);
2195 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2196 val & BMSR_100FULL);
2197 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2198 val & BMSR_100HALF);
2199 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2200 val & BMSR_10FULL);
2201 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2202 val & BMSR_10HALF);
2204 if (val & BMSR_ESTATEN) {
2205 val = phy_read(phydev, MII_ESTATUS);
2206 if (val < 0)
2207 return val;
2209 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2210 phydev->supported, val & ESTATUS_1000_TFULL);
2211 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2212 phydev->supported, val & ESTATUS_1000_THALF);
2213 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2214 phydev->supported, val & ESTATUS_1000_XFULL);
2217 return 0;
2219 EXPORT_SYMBOL(genphy_read_abilities);
2221 /* This is used for the phy device which doesn't support the MMD extended
2222 * register access, but it does have side effect when we are trying to access
2223 * the MMD register via indirect method.
2225 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2227 return -EOPNOTSUPP;
2229 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2231 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2232 u16 regnum, u16 val)
2234 return -EOPNOTSUPP;
2236 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2238 int genphy_suspend(struct phy_device *phydev)
2240 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2242 EXPORT_SYMBOL(genphy_suspend);
2244 int genphy_resume(struct phy_device *phydev)
2246 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2248 EXPORT_SYMBOL(genphy_resume);
2250 int genphy_loopback(struct phy_device *phydev, bool enable)
2252 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2253 enable ? BMCR_LOOPBACK : 0);
2255 EXPORT_SYMBOL(genphy_loopback);
2258 * phy_remove_link_mode - Remove a supported link mode
2259 * @phydev: phy_device structure to remove link mode from
2260 * @link_mode: Link mode to be removed
2262 * Description: Some MACs don't support all link modes which the PHY
2263 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2264 * to remove a link mode.
2266 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2268 linkmode_clear_bit(link_mode, phydev->supported);
2269 phy_advertise_supported(phydev);
2271 EXPORT_SYMBOL(phy_remove_link_mode);
2273 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2275 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2276 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2277 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2278 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2282 * phy_advertise_supported - Advertise all supported modes
2283 * @phydev: target phy_device struct
2285 * Description: Called to advertise all supported modes, doesn't touch
2286 * pause mode advertising.
2288 void phy_advertise_supported(struct phy_device *phydev)
2290 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2292 linkmode_copy(new, phydev->supported);
2293 phy_copy_pause_bits(new, phydev->advertising);
2294 linkmode_copy(phydev->advertising, new);
2296 EXPORT_SYMBOL(phy_advertise_supported);
2299 * phy_support_sym_pause - Enable support of symmetrical pause
2300 * @phydev: target phy_device struct
2302 * Description: Called by the MAC to indicate is supports symmetrical
2303 * Pause, but not asym pause.
2305 void phy_support_sym_pause(struct phy_device *phydev)
2307 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2308 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2310 EXPORT_SYMBOL(phy_support_sym_pause);
2313 * phy_support_asym_pause - Enable support of asym pause
2314 * @phydev: target phy_device struct
2316 * Description: Called by the MAC to indicate is supports Asym Pause.
2318 void phy_support_asym_pause(struct phy_device *phydev)
2320 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2322 EXPORT_SYMBOL(phy_support_asym_pause);
2325 * phy_set_sym_pause - Configure symmetric Pause
2326 * @phydev: target phy_device struct
2327 * @rx: Receiver Pause is supported
2328 * @tx: Transmit Pause is supported
2329 * @autoneg: Auto neg should be used
2331 * Description: Configure advertised Pause support depending on if
2332 * receiver pause and pause auto neg is supported. Generally called
2333 * from the set_pauseparam .ndo.
2335 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2336 bool autoneg)
2338 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2340 if (rx && tx && autoneg)
2341 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2342 phydev->supported);
2344 linkmode_copy(phydev->advertising, phydev->supported);
2346 EXPORT_SYMBOL(phy_set_sym_pause);
2349 * phy_set_asym_pause - Configure Pause and Asym Pause
2350 * @phydev: target phy_device struct
2351 * @rx: Receiver Pause is supported
2352 * @tx: Transmit Pause is supported
2354 * Description: Configure advertised Pause support depending on if
2355 * transmit and receiver pause is supported. If there has been a
2356 * change in adverting, trigger a new autoneg. Generally called from
2357 * the set_pauseparam .ndo.
2359 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2361 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2363 linkmode_copy(oldadv, phydev->advertising);
2365 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2366 phydev->advertising);
2367 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2368 phydev->advertising);
2370 if (rx) {
2371 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2372 phydev->advertising);
2373 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2374 phydev->advertising);
2377 if (tx)
2378 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2379 phydev->advertising);
2381 if (!linkmode_equal(oldadv, phydev->advertising) &&
2382 phydev->autoneg)
2383 phy_start_aneg(phydev);
2385 EXPORT_SYMBOL(phy_set_asym_pause);
2388 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2389 * @phydev: phy_device struct
2390 * @pp: requested pause configuration
2392 * Description: Test if the PHY/MAC combination supports the Pause
2393 * configuration the user is requesting. Returns True if it is
2394 * supported, false otherwise.
2396 bool phy_validate_pause(struct phy_device *phydev,
2397 struct ethtool_pauseparam *pp)
2399 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2400 phydev->supported) && pp->rx_pause)
2401 return false;
2403 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2404 phydev->supported) &&
2405 pp->rx_pause != pp->tx_pause)
2406 return false;
2408 return true;
2410 EXPORT_SYMBOL(phy_validate_pause);
2412 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2414 return phydrv->config_intr && phydrv->ack_interrupt;
2418 * phy_probe - probe and init a PHY device
2419 * @dev: device to probe and init
2421 * Description: Take care of setting up the phy_device structure,
2422 * set the state to READY (the driver's init function should
2423 * set it to STARTING if needed).
2425 static int phy_probe(struct device *dev)
2427 struct phy_device *phydev = to_phy_device(dev);
2428 struct device_driver *drv = phydev->mdio.dev.driver;
2429 struct phy_driver *phydrv = to_phy_driver(drv);
2430 int err = 0;
2432 phydev->drv = phydrv;
2434 /* Disable the interrupt if the PHY doesn't support it
2435 * but the interrupt is still a valid one
2437 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2438 phydev->irq = PHY_POLL;
2440 if (phydrv->flags & PHY_IS_INTERNAL)
2441 phydev->is_internal = true;
2443 mutex_lock(&phydev->lock);
2445 if (phydev->drv->probe) {
2446 /* Deassert the reset signal */
2447 phy_device_reset(phydev, 0);
2449 err = phydev->drv->probe(phydev);
2450 if (err) {
2451 /* Assert the reset signal */
2452 phy_device_reset(phydev, 1);
2453 goto out;
2457 /* Start out supporting everything. Eventually,
2458 * a controller will attach, and may modify one
2459 * or both of these values
2461 if (phydrv->features) {
2462 linkmode_copy(phydev->supported, phydrv->features);
2463 } else if (phydrv->get_features) {
2464 err = phydrv->get_features(phydev);
2465 } else if (phydev->is_c45) {
2466 err = genphy_c45_pma_read_abilities(phydev);
2467 } else {
2468 err = genphy_read_abilities(phydev);
2471 if (err)
2472 goto out;
2474 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2475 phydev->supported))
2476 phydev->autoneg = 0;
2478 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2479 phydev->supported))
2480 phydev->is_gigabit_capable = 1;
2481 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2482 phydev->supported))
2483 phydev->is_gigabit_capable = 1;
2485 of_set_phy_supported(phydev);
2486 phy_advertise_supported(phydev);
2488 /* Get the EEE modes we want to prohibit. We will ask
2489 * the PHY stop advertising these mode later on
2491 of_set_phy_eee_broken(phydev);
2493 /* The Pause Frame bits indicate that the PHY can support passing
2494 * pause frames. During autonegotiation, the PHYs will determine if
2495 * they should allow pause frames to pass. The MAC driver should then
2496 * use that result to determine whether to enable flow control via
2497 * pause frames.
2499 * Normally, PHY drivers should not set the Pause bits, and instead
2500 * allow phylib to do that. However, there may be some situations
2501 * (e.g. hardware erratum) where the driver wants to set only one
2502 * of these bits.
2504 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2505 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2506 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2507 phydev->supported);
2508 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2509 phydev->supported);
2512 /* Set the state to READY by default */
2513 phydev->state = PHY_READY;
2515 out:
2516 mutex_unlock(&phydev->lock);
2518 return err;
2521 static int phy_remove(struct device *dev)
2523 struct phy_device *phydev = to_phy_device(dev);
2525 cancel_delayed_work_sync(&phydev->state_queue);
2527 mutex_lock(&phydev->lock);
2528 phydev->state = PHY_DOWN;
2529 mutex_unlock(&phydev->lock);
2531 sfp_bus_del_upstream(phydev->sfp_bus);
2532 phydev->sfp_bus = NULL;
2534 if (phydev->drv && phydev->drv->remove) {
2535 phydev->drv->remove(phydev);
2537 /* Assert the reset signal */
2538 phy_device_reset(phydev, 1);
2540 phydev->drv = NULL;
2542 return 0;
2546 * phy_driver_register - register a phy_driver with the PHY layer
2547 * @new_driver: new phy_driver to register
2548 * @owner: module owning this PHY
2550 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2552 int retval;
2554 /* Either the features are hard coded, or dynamically
2555 * determined. It cannot be both.
2557 if (WARN_ON(new_driver->features && new_driver->get_features)) {
2558 pr_err("%s: features and get_features must not both be set\n",
2559 new_driver->name);
2560 return -EINVAL;
2563 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2564 new_driver->mdiodrv.driver.name = new_driver->name;
2565 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2566 new_driver->mdiodrv.driver.probe = phy_probe;
2567 new_driver->mdiodrv.driver.remove = phy_remove;
2568 new_driver->mdiodrv.driver.owner = owner;
2570 retval = driver_register(&new_driver->mdiodrv.driver);
2571 if (retval) {
2572 pr_err("%s: Error %d in registering driver\n",
2573 new_driver->name, retval);
2575 return retval;
2578 pr_debug("%s: Registered new driver\n", new_driver->name);
2580 return 0;
2582 EXPORT_SYMBOL(phy_driver_register);
2584 int phy_drivers_register(struct phy_driver *new_driver, int n,
2585 struct module *owner)
2587 int i, ret = 0;
2589 for (i = 0; i < n; i++) {
2590 ret = phy_driver_register(new_driver + i, owner);
2591 if (ret) {
2592 while (i-- > 0)
2593 phy_driver_unregister(new_driver + i);
2594 break;
2597 return ret;
2599 EXPORT_SYMBOL(phy_drivers_register);
2601 void phy_driver_unregister(struct phy_driver *drv)
2603 driver_unregister(&drv->mdiodrv.driver);
2605 EXPORT_SYMBOL(phy_driver_unregister);
2607 void phy_drivers_unregister(struct phy_driver *drv, int n)
2609 int i;
2611 for (i = 0; i < n; i++)
2612 phy_driver_unregister(drv + i);
2614 EXPORT_SYMBOL(phy_drivers_unregister);
2616 static struct phy_driver genphy_driver = {
2617 .phy_id = 0xffffffff,
2618 .phy_id_mask = 0xffffffff,
2619 .name = "Generic PHY",
2620 .soft_reset = genphy_no_soft_reset,
2621 .get_features = genphy_read_abilities,
2622 .suspend = genphy_suspend,
2623 .resume = genphy_resume,
2624 .set_loopback = genphy_loopback,
2627 static int __init phy_init(void)
2629 int rc;
2631 rc = mdio_bus_init();
2632 if (rc)
2633 return rc;
2635 features_init();
2637 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
2638 if (rc)
2639 goto err_c45;
2641 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2642 if (rc) {
2643 phy_driver_unregister(&genphy_c45_driver);
2644 err_c45:
2645 mdio_bus_exit();
2648 return rc;
2651 static void __exit phy_exit(void)
2653 phy_driver_unregister(&genphy_c45_driver);
2654 phy_driver_unregister(&genphy_driver);
2655 mdio_bus_exit();
2658 subsys_initcall(phy_init);
2659 module_exit(phy_exit);