Lines Matching +full:ethernet +full:- +full:phy +full:- +full:package

1 // SPDX-License-Identifier: GPL-2.0+
3 * Also contains generic PHY driver
29 #include <linux/phy.h>
33 #include <linux/pse-pd/pse.h>
44 MODULE_DESCRIPTION("PHY library");
203 put_device(&phydev->mdio.dev);
217 fwnode_handle_put(dev->fwnode);
245 struct device_driver *drv = phydev->mdio.dev.driver;
247 struct net_device *netdev = phydev->attached_dev;
249 if (!drv || !phydrv->suspend)
252 /* If the PHY on the mido bus is not attached but has WOL enabled
253 * we cannot suspend the PHY.
258 /* PHY not attached? May suspend if the PHY has not already been
259 * suspended as part of a prior call to phy_disconnect() ->
260 * phy_detach() -> phy_suspend() because the parent netdev might be the
266 if (netdev->ethtool->wol_enabled)
271 * Don't suspend PHY if the attached netdev parent may wake up.
274 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
277 /* Also don't suspend PHY if the netdev itself may wakeup. This
281 if (device_may_wakeup(&netdev->dev))
285 return !phydev->suspended;
292 if (phydev->mac_managed_pm)
296 * the PHY is inaccessible. Set flag to postpone handling until the PHY
300 phydev->irq_suspended = 1;
301 synchronize_irq(phydev->irq);
305 * control, possibly with the phydev->lock held. Upon resume, netdev
306 * may call phy routines that try to grab the same lock, and that may
309 if (phydev->attached_dev && phydev->adjust_link)
315 phydev->suspended_by_mdio_bus = 1;
325 if (phydev->mac_managed_pm)
328 if (!phydev->suspended_by_mdio_bus)
331 phydev->suspended_by_mdio_bus = 0;
333 /* If we managed to get here with the PHY state machine in a state
338 WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY &&
339 phydev->state != PHY_UP);
350 phydev->irq_suspended = 0;
351 synchronize_irq(phydev->irq);
356 if (phydev->irq_rerun) {
357 phydev->irq_rerun = 0;
358 enable_irq(phydev->irq);
359 irq_wake_thread(phydev->irq, phydev);
363 if (phydev->attached_dev && phydev->adjust_link)
373 * phy_register_fixup - creates a new phy_fixup and adds it to the list
374 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
375 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
377 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
379 * @run: The actual code to be run when a matching PHY is found
387 return -ENOMEM;
389 strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
390 fixup->phy_uid = phy_uid;
391 fixup->phy_uid_mask = phy_uid_mask;
392 fixup->run = run;
395 list_add_tail(&fixup->list, &phy_fixup_list);
402 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
410 /* Registers a fixup to be run on the PHY with id string bus_id */
419 * phy_unregister_fixup - remove a phy_fixup from the list
420 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
421 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
422 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
430 ret = -ENODEV;
436 if ((!strcmp(fixup->bus_id, bus_id)) &&
437 phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
438 list_del(&fixup->list);
450 /* Unregisters a fixup of any PHY with the UID in phy_uid */
457 /* Unregisters a fixup of the PHY with id string bus_id */
469 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
470 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
473 if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
474 fixup->phy_uid_mask))
475 if (fixup->phy_uid != PHY_ANY_UID)
489 int err = fixup->run(phydev);
495 phydev->has_fixups = true;
507 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
510 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
513 if (phydrv->match_phy_device)
514 return phydrv->match_phy_device(phydev);
516 if (phydev->is_c45) {
518 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
521 if (phy_id_compare(phydev->c45_ids.device_ids[i],
522 phydrv->phy_id, phydrv->phy_id_mask))
527 return phy_id_compare(phydev->phy_id, phydrv->phy_id,
528 phydrv->phy_id_mask);
537 return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
550 mode = phy_modes(phydev->interface);
562 return sysfs_emit(buf, "%d\n", phydev->has_fixups);
572 return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags);
586 .name = "PHY",
599 * not whether a PHY driver module exists for the PHY ID.
600 * Accept -ENOENT because this may occur in case no initramfs exists,
603 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
604 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
623 return ERR_PTR(-ENOMEM);
625 mdiodev = &dev->mdio;
626 mdiodev->dev.parent = &bus->dev;
627 mdiodev->dev.bus = &mdio_bus_type;
628 mdiodev->dev.type = &mdio_bus_phy_type;
629 mdiodev->bus = bus;
630 mdiodev->bus_match = phy_bus_match;
631 mdiodev->addr = addr;
632 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
633 mdiodev->device_free = phy_mdio_device_free;
634 mdiodev->device_remove = phy_mdio_device_remove;
635 mdiodev->reset_state = -1;
637 dev->speed = SPEED_UNKNOWN;
638 dev->duplex = DUPLEX_UNKNOWN;
639 dev->pause = 0;
640 dev->asym_pause = 0;
641 dev->link = 0;
642 dev->port = PORT_TP;
643 dev->interface = PHY_INTERFACE_MODE_GMII;
645 dev->autoneg = AUTONEG_ENABLE;
647 dev->pma_extable = -ENODATA;
648 dev->is_c45 = is_c45;
649 dev->phy_id = phy_id;
651 dev->c45_ids = *c45_ids;
652 dev->irq = bus->irq[addr];
654 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
655 device_initialize(&mdiodev->dev);
657 dev->state = PHY_DOWN;
658 INIT_LIST_HEAD(&dev->leds);
660 mutex_init(&dev->lock);
661 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
668 * -- because it relies on the device staying around for long
674 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
678 if (c45_ids->device_ids[i] == 0xffffffff)
682 c45_ids->device_ids[i]);
691 put_device(&mdiodev->dev);
699 /* phy_c45_probe_present - checks to see if a MMD is present in the package
701 * @prtad: PHY package address on the MII bus
702 * @devad: PHY device (MMD) address
721 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
723 * @addr: PHY address on the MII bus
724 * @dev_addr: MMD address in the PHY.
725 * @devices_in_package: where to store the devices in package information.
727 * Description: reads devices in package registers of a MMD at @dev_addr
728 * from PHY at @addr on @bus.
730 * Returns: 0 on success, -EIO on failure.
739 return -EIO;
744 return -EIO;
751 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
753 * @addr: PHY address on the MII bus
756 * Read the PHY "devices in package". If this appears to be valid, read
757 * the PHY identifiers for each device. Return the "devices in package"
760 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
761 * the "devices in package" is invalid or no device responds.
766 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
770 /* Find first non-zero Devices In package. Device zero is reserved
777 * address before reading the devices-in-package
778 * register to avoid reading garbage from the PHY.
784 /* returning -ENODEV doesn't stop bus
787 return (phy_reg == -EIO ||
788 phy_reg == -ENODEV) ? -ENODEV : -EIO;
795 return -EIO;
800 * MMD 0, as some 10G PHYs have zero Devices In package,
801 * e.g. Cortina CS4315/CS4340 PHY.
805 return -EIO;
809 return -ENODEV;
832 return -EIO;
833 c45_ids->device_ids[i] = phy_reg << 16;
837 return -EIO;
838 c45_ids->device_ids[i] |= phy_reg;
841 c45_ids->devices_in_package = devs_in_pkg;
843 c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
849 * get_phy_c22_id - reads the specified addr for its clause 22 ID.
851 * @addr: PHY address on the MII bus
854 * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
856 * valid, %-EIO on bus access error, or %-ENODEV if no device responds
866 /* returning -ENODEV doesn't stop bus scanning */
867 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
875 /* returning -ENODEV doesn't stop bus scanning */
876 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
883 return -ENODEV;
888 /* Extract the phy ID from the compatible string of the form
889 * ethernet-phy-idAAAA.BBBB.
901 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2)
902 return -EINVAL;
910 * get_phy_device - reads the specified PHY device and returns its @phy_device
913 * @addr: PHY address on the MII bus
914 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
916 * Probe for a PHY at @addr on @bus.
918 * When probing for a clause 22 PHY, then read the ID registers. If we find
921 * When probing for a clause 45 PHY, read the "devices in package" registers.
922 * If the "devices in package" appears valid, read the ID registers for each
925 * Returns an allocated &struct phy_device on success, %-ENODEV if there is
926 * no PHY present, or %-EIO on bus access error.
946 /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID
948 * probe with C45 to see if we're able to get a valid PHY ID in the C45
949 * space, if successful, create the C45 PHY device.
951 if (!is_c45 && phy_id == 0 && bus->read_c45) {
963 * phy_device_register - Register the phy device on the MDIO bus
970 err = mdiobus_register_device(&phydev->mdio);
977 /* Run all of the fixups for this PHY */
984 err = device_add(&phydev->mdio.dev);
996 mdiobus_unregister_device(&phydev->mdio);
1002 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
1011 unregister_mii_timestamper(phydev->mii_ts);
1012 pse_control_put(phydev->psec);
1014 device_del(&phydev->mdio.dev);
1019 mdiobus_unregister_device(&phydev->mdio);
1024 * phy_get_c45_ids - Read 802.3-c45 IDs for phy device.
1025 * @phydev: phy_device structure to read 802.3-c45 IDs
1027 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
1028 * the "devices in package" is invalid.
1032 return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr,
1033 &phydev->c45_ids);
1038 * phy_find_first - finds the first PHY device on the bus
1057 struct net_device *netdev = phydev->attached_dev;
1063 phydev->adjust_link(netdev);
1064 if (phydev->mii_ts && phydev->mii_ts->link_state)
1065 phydev->mii_ts->link_state(phydev->mii_ts, phydev);
1069 * phy_prepare_link - prepares the PHY layer to monitor link status
1073 * Description: Tells the PHY infrastructure to handle the
1083 phydev->adjust_link = handler;
1087 * phy_connect_direct - connect an ethernet device to a specific phy_device
1089 * @phydev: the pointer to the phy device
1091 * @interface: PHY device's interface
1100 return -EINVAL;
1102 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1115 * phy_connect - connect an ethernet device to a PHY device
1117 * @bus_id: the id string of the PHY device to connect
1119 * @interface: PHY device's interface
1121 * Description: Convenience function for connecting ethernet
1122 * devices to PHY devices. The default behavior is for
1123 * the PHY infrastructure to handle everything, and only notify
1137 /* Search the list of PHY devices on the mdio bus for the
1138 * PHY with the requested name
1142 pr_err("PHY %s not found\n", bus_id);
1143 return ERR_PTR(-ENODEV);
1157 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1169 phydev->adjust_link = NULL;
1176 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1177 * @phydev: The PHY device to poll
1180 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1183 * Furthermore, any attempts to write to PHY registers may have no effect
1188 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1191 * and reapply all driver-specific and board-specific fixups.
1216 if (!phydev->drv)
1219 if (phydev->drv->soft_reset) {
1220 ret = phydev->drv->soft_reset(phydev);
1225 phydev->suspended = 0;
1232 phy_interface_zero(phydev->possible_interfaces);
1234 if (phydev->drv->config_init) {
1235 ret = phydev->drv->config_init(phydev);
1240 if (phydev->drv->config_intr) {
1241 ret = phydev->drv->config_intr(phydev);
1256 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)"
1262 switch(phydev->irq) {
1270 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1281 const char *unbound = phydev->drv ? "" : "[unbound] ";
1303 struct net_device *dev = phydev->attached_dev;
1309 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1314 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1315 &phydev->mdio.dev.kobj,
1318 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1319 kobject_name(&phydev->mdio.dev.kobj),
1321 /* non-fatal - some net drivers can use one netdevice
1322 * with more then one phy
1326 phydev->sysfs_links = true;
1335 return sysfs_emit(buf, "%d\n", !phydev->attached_dev);
1340 * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY
1341 * @upstream: pointer to the upstream phy device
1342 * @phy: pointer to the SFP module's phy device
1344 * This helper allows keeping track of PHY devices on the link. It adds the
1345 * SFP module's phy to the phy namespace of the upstream phy
1349 int phy_sfp_connect_phy(void *upstream, struct phy_device *phy)
1352 struct net_device *dev = phydev->attached_dev;
1355 return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev);
1362 * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY
1363 * @upstream: pointer to the upstream phy device
1364 * @phy: pointer to the SFP module's phy device
1366 * This helper allows keeping track of PHY devices on the link. It removes the
1367 * SFP module's phy to the phy namespace of the upstream phy. As the module phy
1368 * will be destroyed, re-inserting the same module will add a new phy with a
1371 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy)
1374 struct net_device *dev = phydev->attached_dev;
1377 phy_link_topo_del_phy(dev, phy);
1382 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1383 * @upstream: pointer to the phy device
1392 if (phydev->attached_dev)
1393 phydev->attached_dev->sfp_bus = bus;
1394 phydev->sfp_bus_attached = true;
1399 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1400 * @upstream: pointer to the phy device
1409 if (phydev->attached_dev)
1410 phydev->attached_dev->sfp_bus = NULL;
1411 phydev->sfp_bus_attached = false;
1416 * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1426 if (phydev->mdio.dev.fwnode) {
1427 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1431 phydev->sfp_bus = bus;
1442 return phydrv->config_intr && phydrv->handle_interrupt;
1446 * phy_attach_direct - attach a network device to a given PHY device pointer
1449 * @flags: PHY device's dev_flags
1450 * @interface: PHY device's interface
1452 * Description: Called by drivers to attach to a particular PHY
1458 * This function takes a reference on the phy device.
1463 struct mii_bus *bus = phydev->mdio.bus;
1464 struct device *d = &phydev->mdio.dev;
1469 /* For Ethernet device drivers that register their own MDIO bus, we
1470 * will have bus->owner match ndev_mod, so we do not want to increment
1471 * our own module->refcnt here, otherwise we would not be able to
1475 ndev_owner = dev->dev.parent->driver->owner;
1476 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1478 return -EIO;
1486 if (!d->driver) {
1487 if (phydev->is_c45)
1488 d->driver = &genphy_c45_driver.mdiodrv.driver;
1490 d->driver = &genphy_driver.mdiodrv.driver;
1495 if (!try_module_get(d->driver->owner)) {
1497 err = -EIO;
1502 err = d->driver->probe(d);
1510 if (phydev->attached_dev) {
1511 dev_err(&dev->dev, "PHY already attached\n");
1512 err = -EBUSY;
1516 phydev->phy_link_change = phy_link_change;
1518 phydev->attached_dev = dev;
1519 dev->phydev = phydev;
1521 if (phydev->sfp_bus_attached)
1522 dev->sfp_bus = phydev->sfp_bus;
1529 /* Some Ethernet drivers try to connect to a PHY device before
1530 * calling register_netdevice() -> netdev_register_kobject() and
1531 * does the dev->dev.kobj initialization. Here we only check for
1537 phydev->sysfs_links = false;
1541 if (!phydev->attached_dev) {
1542 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1548 phydev->dev_flags |= flags;
1550 phydev->interface = interface;
1552 phydev->state = PHY_READY;
1554 phydev->interrupts = PHY_INTERRUPT_DISABLED;
1560 if (phydev->dev_flags & PHY_F_NO_IRQ)
1561 phydev->irq = PHY_POLL;
1563 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev))
1564 phydev->irq = PHY_POLL;
1566 /* Port is set to PORT_TP by default and the actual PHY driver will set
1567 * it to different value depending on the PHY configuration. If we have
1568 * the generic PHY driver we can't figure it out, thus set the old
1572 phydev->port = PORT_MII;
1574 /* Initial carrier state is off as the phy is about to be
1578 netif_carrier_off(phydev->attached_dev);
1589 if (!phydev->is_on_sfp_module)
1593 * If the external phy used by current mac interface is managed by
1595 * phy dev and mac dev.
1597 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
1598 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
1609 module_put(d->driver->owner);
1610 d->driver = NULL;
1613 if (ndev_owner != bus->owner)
1614 module_put(bus->owner);
1620 * phy_attach - attach a network device to a particular PHY device
1622 * @bus_id: Bus ID of PHY device to attach
1623 * @interface: PHY device's interface
1625 * Description: Same as phy_attach_direct() except that a PHY bus_id
1636 return ERR_PTR(-EINVAL);
1638 /* Search the list of PHY devices on the mdio bus for the
1639 * PHY with the requested name
1643 pr_err("PHY %s not found\n", bus_id);
1644 return ERR_PTR(-ENODEV);
1648 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1660 struct device *d = &phydev->mdio.dev;
1663 if (!phydev->drv)
1667 ret = d->driver == driver;
1688 * phy_package_join - join a common PHY group
1690 * @base_addr: cookie and base PHY address of PHY package for offset
1692 * @priv_size: if non-zero allocate this amount of bytes for private data
1694 * This joins a PHY group and provides a shared storage for all phydevs in
1696 * more than one PHY, for example a quad PHY transceiver.
1699 * for all members of one group and as the base PHY address of the PHY package
1700 * for offset calculation to access generic registers of a PHY package.
1701 * Usually, one of the PHY addresses of the different PHYs in the package
1709 * allocated. If priv_size is non-zero, the given amount of bytes are
1717 struct mii_bus *bus = phydev->mdio.bus;
1722 return -EINVAL;
1724 mutex_lock(&bus->shared_lock);
1725 shared = bus->shared[base_addr];
1727 ret = -ENOMEM;
1732 shared->priv = kzalloc(priv_size, GFP_KERNEL);
1733 if (!shared->priv)
1735 shared->priv_size = priv_size;
1737 shared->base_addr = base_addr;
1738 shared->np = NULL;
1739 refcount_set(&shared->refcnt, 1);
1740 bus->shared[base_addr] = shared;
1742 ret = -EINVAL;
1743 if (priv_size && priv_size != shared->priv_size)
1745 refcount_inc(&shared->refcnt);
1747 mutex_unlock(&bus->shared_lock);
1749 phydev->shared = shared;
1756 mutex_unlock(&bus->shared_lock);
1762 * of_phy_package_join - join a common PHY group in PHY package
1764 * @priv_size: if non-zero allocate this amount of bytes for private data
1766 * This is a variant of phy_package_join for PHY package defined in DT.
1768 * The parent node of the @phydev is checked as a valid PHY package node
1769 * structure (by matching the node name "ethernet-phy-package") and the
1770 * base_addr for the PHY package is passed to phy_package_join.
1773 * filled to use additional DT defined properties in PHY specific
1774 * probe_once and config_init_once PHY package OPs.
1779 * name for PHY package.
1783 struct device_node *node = phydev->mdio.dev.of_node;
1789 return -EINVAL;
1793 return -EINVAL;
1795 if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
1796 ret = -EINVAL;
1801 ret = -EINVAL;
1809 phydev->shared->np = package_node;
1819 * phy_package_leave - leave a common PHY group
1822 * This leaves a PHY group created by phy_package_join(). If this phydev
1824 * freed. Resets the phydev->shared pointer to NULL.
1828 struct phy_package_shared *shared = phydev->shared;
1829 struct mii_bus *bus = phydev->mdio.bus;
1835 if (shared->np)
1836 of_node_put(shared->np);
1838 if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
1839 bus->shared[shared->base_addr] = NULL;
1840 mutex_unlock(&bus->shared_lock);
1841 kfree(shared->priv);
1845 phydev->shared = NULL;
1855 * devm_phy_package_join - resource managed phy_package_join()
1856 * @dev: device that is registering this PHY package
1858 * @base_addr: cookie and base PHY address of PHY package for offset
1860 * @priv_size: if non-zero allocate this amount of bytes for private data
1875 return -ENOMEM;
1891 * devm_of_phy_package_join - resource managed of_phy_package_join()
1892 * @dev: device that is registering this PHY package
1894 * @priv_size: if non-zero allocate this amount of bytes for private data
1909 return -ENOMEM;
1925 * phy_detach - detach a PHY device from its network device
1928 * This detaches the phy device from its network device and the phy
1933 struct net_device *dev = phydev->attached_dev;
1937 if (phydev->devlink)
1938 device_link_del(phydev->devlink);
1940 if (phydev->sysfs_links) {
1942 sysfs_remove_link(&dev->dev.kobj, "phydev");
1943 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1946 if (!phydev->attached_dev)
1947 sysfs_remove_file(&phydev->mdio.dev.kobj,
1954 hwprov = rtnl_dereference(dev->hwprov);
1956 if (hwprov && hwprov->phydev == phydev) {
1957 rcu_assign_pointer(dev->hwprov, NULL);
1961 phydev->attached_dev->phydev = NULL;
1962 phydev->attached_dev = NULL;
1965 phydev->phylink = NULL;
1967 if (!phydev->is_on_sfp_module)
1970 if (phydev->mdio.dev.driver)
1971 module_put(phydev->mdio.dev.driver->owner);
1973 /* If the device had no specific driver before (i.e. - it
1980 device_release_driver(&phydev->mdio.dev);
1987 * a use-after-free bug by reading the underlying bus first.
1989 bus = phydev->mdio.bus;
1991 put_device(&phydev->mdio.dev);
1993 ndev_owner = dev->dev.parent->driver->owner;
1994 if (ndev_owner != bus->owner)
1995 module_put(bus->owner);
2001 struct net_device *netdev = phydev->attached_dev;
2002 const struct phy_driver *phydrv = phydev->drv;
2005 if (phydev->suspended || !phydrv)
2008 phydev->wol_enabled = phy_drv_wol_enabled(phydev) ||
2009 (netdev && netdev->ethtool->wol_enabled);
2010 /* If the device has WOL enabled, we cannot suspend the PHY */
2011 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND))
2012 return -EBUSY;
2014 if (!phydrv->suspend)
2017 ret = phydrv->suspend(phydev);
2019 phydev->suspended = true;
2027 const struct phy_driver *phydrv = phydev->drv;
2030 lockdep_assert_held(&phydev->lock);
2032 if (!phydrv || !phydrv->resume)
2035 ret = phydrv->resume(phydev);
2037 phydev->suspended = false;
2047 mutex_lock(&phydev->lock);
2049 mutex_unlock(&phydev->lock);
2059 if (!phydev->drv)
2060 return -EIO;
2062 mutex_lock(&phydev->lock);
2064 if (enable && phydev->loopback_enabled) {
2065 ret = -EBUSY;
2069 if (!enable && !phydev->loopback_enabled) {
2070 ret = -EINVAL;
2074 if (phydev->drv->set_loopback)
2075 ret = phydev->drv->set_loopback(phydev, enable);
2082 phydev->loopback_enabled = enable;
2085 mutex_unlock(&phydev->lock);
2091 * phy_reset_after_clk_enable - perform a PHY reset if needed
2096 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
2101 if (!phydev || !phydev->drv)
2102 return -ENODEV;
2104 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
2114 /* Generic PHY support and helper functions */
2117 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
2119 * @advert: auto-negotiation parameters to advertise
2123 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
2148 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
2169 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
2174 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
2176 * for Clause 37 1000Base-X mode.
2182 /* Only allow advertising what this PHY supports */
2183 linkmode_and(phydev->advertising, phydev->advertising,
2184 phydev->supported);
2187 phydev->advertising))
2190 phydev->advertising))
2193 phydev->advertising))
2203 * genphy_setup_forced - configures/forces speed/duplex from @phydev
2214 phydev->pause = 0;
2215 phydev->asym_pause = 0;
2217 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2228 if (!phydev->is_gigabit_capable)
2231 switch (phydev->master_slave_set) {
2248 return -EOPNOTSUPP;
2261 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2262 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2286 } else if (phydev->link) {
2296 phydev->master_slave_get = cfg;
2297 phydev->master_slave_state = state;
2304 * genphy_restart_aneg - Enable and Restart Autonegotiation
2309 /* Don't isolate the PHY if we're negotiating */
2316 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2320 * Check, and restart auto-negotiation if needed.
2328 * begin with? Or maybe phy was isolated?
2346 * __genphy_config_aneg - restart auto-negotiation or write BMCR
2350 * Description: If auto-negotiation is enabled, we configure the
2351 * advertising, and then restart auto-negotiation. If it is not
2373 if (phydev->autoneg == AUTONEG_ENABLE) {
2374 /* Only allow advertising what this PHY supports */
2375 linkmode_and(phydev->advertising, phydev->advertising,
2376 phydev->supported);
2377 advert = phydev->advertising;
2378 } else if (phydev->speed < SPEED_1000) {
2383 set = phy_lookup_setting(phydev->speed, phydev->duplex,
2384 phydev->supported, true);
2386 linkmode_set_bit(set->bit, fixed_advert);
2402 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2405 * Description: If auto-negotiation is enabled, we configure the
2406 * advertising, and then restart auto-negotiation. If it is not
2408 * for use with Clause 37 1000Base-X mode.
2414 if (phydev->autoneg != AUTONEG_ENABLE)
2428 * begin with? Or maybe phy was isolated?
2450 * genphy_aneg_done - return auto-negotiation status
2454 * auto-negotiation is incomplete, or if there was an error.
2455 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2466 * genphy_update_link - update link status in @phydev
2469 * Description: Update the value in phydev->link to reflect the
2488 * drops can be detected. Do not double-read the status
2492 if (!phy_polling_mode(phydev) || !phydev->link) {
2505 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2506 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2511 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2512 phydev->link = 0;
2522 if (phydev->autoneg == AUTONEG_ENABLE) {
2523 if (!phydev->autoneg_complete) {
2524 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2526 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2530 if (phydev->is_gigabit_capable) {
2545 return -ENOLINK;
2548 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2556 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2558 linkmode_zero(phydev->lp_advertising);
2566 * genphy_read_status_fixed - read the link parameters for !aneg mode
2569 * Read the current duplex and speed state for a PHY operating with
2580 phydev->duplex = DUPLEX_FULL;
2582 phydev->duplex = DUPLEX_HALF;
2585 phydev->speed = SPEED_1000;
2587 phydev->speed = SPEED_100;
2589 phydev->speed = SPEED_10;
2596 * genphy_read_status - check the link status and update current link state
2606 int err, old_link = phydev->link;
2613 /* why bother the PHY if nothing can have changed */
2614 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2617 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2618 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2619 phydev->speed = SPEED_UNKNOWN;
2620 phydev->duplex = DUPLEX_UNKNOWN;
2621 phydev->pause = 0;
2622 phydev->asym_pause = 0;
2624 if (phydev->is_gigabit_capable) {
2634 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2636 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2647 * genphy_c37_read_status - check the link status and update current link state
2653 * advertises. This function is for Clause 37 1000Base-X mode.
2659 int lpa, err, old_link = phydev->link;
2666 /* why bother the PHY if nothing can have changed */
2667 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
2674 phydev->duplex = DUPLEX_UNKNOWN;
2675 phydev->pause = 0;
2676 phydev->asym_pause = 0;
2678 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2684 phydev->lp_advertising, lpa & LPA_LPACK);
2686 phydev->lp_advertising, lpa & LPA_1000XFULL);
2688 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2690 phydev->lp_advertising,
2694 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2701 phydev->duplex = DUPLEX_FULL;
2703 phydev->duplex = DUPLEX_HALF;
2711 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2714 * Description: Perform a software PHY reset using the standard
2724 if (phydev->autoneg == AUTONEG_ENABLE)
2735 phydev->suspended = 0;
2742 if (phydev->autoneg == AUTONEG_DISABLE)
2752 * entity (ie an IRQ controller embedded inside the PHY) and do not
2763 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2766 * Description: Reads the PHY's abilities and populates
2767 * phydev->supported accordingly.
2777 phydev->supported);
2783 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2786 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2788 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2790 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2792 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2801 phydev->supported, val & ESTATUS_1000_TFULL);
2803 phydev->supported, val & ESTATUS_1000_THALF);
2805 phydev->supported, val & ESTATUS_1000_XFULL);
2817 /* This is used for the phy device which doesn't support the MMD extended
2823 return -EOPNOTSUPP;
2830 return -EOPNOTSUPP;
2852 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2872 * phy_remove_link_mode - Remove a supported link mode
2876 * Description: Some MACs don't support all link modes which the PHY
2882 linkmode_clear_bit(link_mode, phydev->supported);
2896 * phy_advertise_supported - Advertise all supported modes
2906 linkmode_copy(new, phydev->supported);
2907 phy_copy_pause_bits(new, phydev->advertising);
2908 linkmode_copy(phydev->advertising, new);
2913 * phy_advertise_eee_all - Advertise all supported EEE modes
2917 * phy probing, which might be a subset of the supported EEE modes. Use this
2919 * trigger auto-negotiation, so must be called before phy_start()/
2920 * phylink_start() which will start auto-negotiation.
2924 linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2929 * phy_support_eee - Set initial EEE policy configuration
2932 * This function configures the initial policy for Energy Efficient Ethernet
2933 * (EEE) on the specified PHY device, influencing that EEE capabilities are
2934 * advertised before the link is established. It should be called during PHY
2935 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
2936 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
2939 * The function sets default EEE policy parameters, including preparing the PHY
2943 * driver. If the PHY framework determines that both local and remote
2950 linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2951 phydev->eee_cfg.tx_lpi_enabled = true;
2952 phydev->eee_cfg.eee_enabled = true;
2957 * phy_disable_eee - Disable EEE for the PHY
2961 * It disables EEE on the PHY layer.
2965 linkmode_zero(phydev->advertising_eee);
2966 phydev->eee_cfg.tx_lpi_enabled = false;
2967 phydev->eee_cfg.eee_enabled = false;
2968 /* don't let userspace re-enable EEE advertisement */
2969 linkmode_fill(phydev->eee_broken_modes);
2974 * phy_support_sym_pause - Enable support of symmetrical pause
2982 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2983 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2988 * phy_support_asym_pause - Enable support of asym pause
2995 phy_copy_pause_bits(phydev->advertising, phydev->supported);
3000 * phy_set_sym_pause - Configure symmetric Pause
3013 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
3017 phydev->supported);
3019 linkmode_copy(phydev->advertising, phydev->supported);
3024 * phy_set_asym_pause - Configure Pause and Asym Pause
3038 linkmode_copy(oldadv, phydev->advertising);
3039 linkmode_set_pause(phydev->advertising, tx, rx);
3041 if (!linkmode_equal(oldadv, phydev->advertising) &&
3042 phydev->autoneg)
3048 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
3052 * Description: Test if the PHY/MAC combination supports the Pause
3060 phydev->supported) && pp->rx_pause)
3064 phydev->supported) &&
3065 pp->rx_pause != pp->tx_pause)
3073 * phy_get_pause - resolve negotiated pause modes
3086 if (phydev->duplex != DUPLEX_FULL) {
3092 return linkmode_resolve_pause(phydev->advertising,
3093 phydev->lp_advertising,
3113 return -EINVAL;
3118 * phy_get_internal_delay - returns the index of the internal delay
3121 * @delay_values: array of delays the PHY supports
3129 * The array must be in ascending order. If PHY does not have an ascending order
3131 * Return -EINVAL if the delay is invalid or cannot be found.
3140 delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
3142 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3143 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
3150 delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
3152 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
3153 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
3166 if (delay < delay_values[0] || delay > delay_values[size - 1]) {
3168 return -EINVAL;
3179 if (delay > delay_values[i - 1] &&
3181 if (delay - delay_values[i - 1] <
3182 delay_values[i] - delay)
3183 return i - 1;
3192 return -EINVAL;
3200 struct phy_device *phydev = phyled->phydev;
3203 mutex_lock(&phydev->lock);
3204 err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
3205 mutex_unlock(&phydev->lock);
3215 struct phy_device *phydev = phyled->phydev;
3218 mutex_lock(&phydev->lock);
3219 err = phydev->drv->led_blink_set(phydev, phyled->index,
3221 mutex_unlock(&phydev->lock);
3230 struct phy_device *phydev = phyled->phydev;
3232 if (phydev->attached_dev)
3233 return &phydev->attached_dev->dev;
3242 struct phy_device *phydev = phyled->phydev;
3245 mutex_lock(&phydev->lock);
3246 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
3247 mutex_unlock(&phydev->lock);
3257 struct phy_device *phydev = phyled->phydev;
3260 mutex_lock(&phydev->lock);
3261 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
3262 mutex_unlock(&phydev->lock);
3271 struct phy_device *phydev = phyled->phydev;
3274 mutex_lock(&phydev->lock);
3275 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
3276 mutex_unlock(&phydev->lock);
3285 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) {
3286 led_classdev_unregister(&phyled->led_cdev);
3287 list_del(&phyled->list);
3294 struct device *dev = &phydev->mdio.dev;
3304 return -ENOMEM;
3306 cdev = &phyled->led_cdev;
3307 phyled->phydev = phydev;
3313 return -EINVAL;
3315 if (of_property_read_bool(led, "active-high"))
3317 if (of_property_read_bool(led, "active-low"))
3319 if (of_property_read_bool(led, "inactive-high-impedance"))
3324 return -EINVAL;
3328 if (!phydev->drv->led_polarity_set)
3329 return -EINVAL;
3331 err = phydev->drv->led_polarity_set(phydev, index, modes);
3336 phyled->index = index;
3337 if (phydev->drv->led_brightness_set)
3338 cdev->brightness_set_blocking = phy_led_set_brightness;
3339 if (phydev->drv->led_blink_set)
3340 cdev->blink_set = phy_led_blink_set;
3343 if (phydev->drv->led_hw_is_supported &&
3344 phydev->drv->led_hw_control_set &&
3345 phydev->drv->led_hw_control_get) {
3346 cdev->hw_control_is_supported = phy_led_hw_is_supported;
3347 cdev->hw_control_set = phy_led_hw_control_set;
3348 cdev->hw_control_get = phy_led_hw_control_get;
3349 cdev->hw_control_trigger = "netdev";
3352 cdev->hw_control_get_device = phy_led_hw_control_get_device;
3354 cdev->max_brightness = 1;
3355 init_data.devicename = dev_name(&phydev->mdio.dev);
3363 list_add(&phyled->list, &phydev->leds);
3370 struct device_node *node = phydev->mdio.dev.of_node;
3384 /* Check if the PHY driver have at least an OP to
3387 if (!(phydev->drv->led_brightness_set ||
3388 phydev->drv->led_blink_set ||
3389 phydev->drv->led_hw_control_set)) {
3390 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n");
3409 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
3432 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
3434 * @phy_fwnode: Pointer to the phy's fwnode.
3447 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
3448 return to_phy_device(&mdiodev->dev);
3450 put_device(&mdiodev->dev);
3457 * device_phy_find_device - For the given device, get the phy_device
3469 * fwnode_get_phy_node - Get the phy_node using the named reference.
3473 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
3474 * and "phy-device" are not supported in ACPI. DT supports all the three
3475 * named references to the phy node.
3481 /* Only phy-handle is used for ACPI */
3482 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
3485 phy_node = fwnode_find_reference(fwnode, "phy", 0);
3487 phy_node = fwnode_find_reference(fwnode, "phy-device", 0);
3493 * phy_probe - probe and init a PHY device
3501 struct device_driver *drv = phydev->mdio.dev.driver;
3505 phydev->drv = phydrv;
3507 /* Disable the interrupt if the PHY doesn't support it
3511 phydev->irq = PHY_POLL;
3513 if (phydrv->flags & PHY_IS_INTERNAL)
3514 phydev->is_internal = true;
3519 if (phydev->drv->probe) {
3520 err = phydev->drv->probe(phydev);
3531 if (phydrv->features) {
3532 linkmode_copy(phydev->supported, phydrv->features);
3535 else if (phydrv->get_features)
3536 err = phydrv->get_features(phydev);
3537 else if (phydev->is_c45)
3546 phydev->supported))
3547 phydev->autoneg = 0;
3550 phydev->supported))
3551 phydev->is_gigabit_capable = 1;
3553 phydev->supported))
3554 phydev->is_gigabit_capable = 1;
3559 /* Get PHY default EEE advertising modes and handle them as potentially
3562 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
3566 /* There is no "enabled" flag. If PHY is advertising, assume it is
3569 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
3574 if (phydev->eee_cfg.eee_enabled)
3575 linkmode_and(phydev->advertising_eee, phydev->supported_eee,
3576 phydev->advertising_eee);
3579 * the PHY stop advertising these mode later on
3586 /* The Pause Frame bits indicate that the PHY can support passing
3592 * Normally, PHY drivers should not set the Pause bits, and instead
3597 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
3598 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
3600 phydev->supported);
3602 phydev->supported);
3606 phydev->state = PHY_READY;
3615 /* Re-assert the reset signal on error */
3626 cancel_delayed_work_sync(&phydev->state_queue);
3631 phydev->state = PHY_DOWN;
3633 sfp_bus_del_upstream(phydev->sfp_bus);
3634 phydev->sfp_bus = NULL;
3636 if (phydev->drv && phydev->drv->remove)
3637 phydev->drv->remove(phydev);
3642 phydev->drv = NULL;
3648 * phy_driver_register - register a phy_driver with the PHY layer
3650 * @owner: module owning this PHY
3659 if (WARN_ON(new_driver->features && new_driver->get_features)) {
3661 new_driver->name);
3662 return -EINVAL;
3668 * make out-of-bounds accesses and lockup in phydev->lock.
3670 if (WARN(new_driver->mdiodrv.driver.of_match_table,
3672 new_driver->name))
3673 return -EINVAL;
3675 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3676 new_driver->mdiodrv.driver.name = new_driver->name;
3677 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3678 new_driver->mdiodrv.driver.probe = phy_probe;
3679 new_driver->mdiodrv.driver.remove = phy_remove;
3680 new_driver->mdiodrv.driver.owner = owner;
3681 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3683 retval = driver_register(&new_driver->mdiodrv.driver);
3686 new_driver->name, retval);
3691 pr_debug("%s: Registered new driver\n", new_driver->name);
3705 while (i-- > 0)
3716 driver_unregister(&drv->mdiodrv.driver);
3732 .name = "Generic PHY",