Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'convert-lan78xx-driver-to-the-phylink'

Oleksij Rempel says:

====================
convert lan78xx driver to the PHYLINK

This series converts the lan78xx driver to use the PHYLINK framework,
which enhances PHY and MAC management. The changes also streamline the
driver by removing unused elements and improving link status reporting.

This is the final part of the previously split conversion series:
https://lore.kernel.org/all/20250428130542.3879769-1-o.rempel@pengutronix.de/

Tested on EVB_LAN7850.
====================

Link: https://patch.msgid.link/20250618122602.3156678-1-o.rempel@pengutronix.de
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+350 -387
+1 -2
drivers/net/usb/Kconfig
··· 113 113 config USB_LAN78XX 114 114 tristate "Microchip LAN78XX Based USB Ethernet Adapters" 115 115 select MII 116 - select PHYLIB 116 + select PHYLINK 117 117 select MICROCHIP_PHY 118 - select FIXED_PHY 119 118 select CRC32 120 119 help 121 120 This option adds support for Microchip LAN78XX based USB 2
+349 -385
drivers/net/usb/lan78xx.c
··· 6 6 #include <linux/netdevice.h> 7 7 #include <linux/etherdevice.h> 8 8 #include <linux/ethtool.h> 9 + #include <linux/phylink.h> 9 10 #include <linux/usb.h> 10 11 #include <linux/crc32.h> 11 12 #include <linux/signal.h> ··· 385 384 #define EVENT_RX_HALT 1 386 385 #define EVENT_RX_MEMORY 2 387 386 #define EVENT_STS_SPLIT 3 388 - #define EVENT_LINK_RESET 4 387 + #define EVENT_PHY_INT_ACK 4 389 388 #define EVENT_RX_PAUSED 5 390 389 #define EVENT_DEV_WAKING 6 391 390 #define EVENT_DEV_ASLEEP 7 ··· 414 413 struct net_device *net; 415 414 struct usb_device *udev; 416 415 struct usb_interface *intf; 417 - void *driver_priv; 418 416 419 417 unsigned int tx_pend_data_len; 420 418 size_t n_tx_urbs; ··· 448 448 unsigned long flags; 449 449 450 450 wait_queue_head_t *wait; 451 - unsigned char suspend_count; 452 451 453 452 unsigned int maxpacket; 454 453 struct timer_list stat_monitor; 455 454 456 455 unsigned long data[5]; 457 456 458 - int link_on; 459 - u8 mdix_ctrl; 460 - 461 457 u32 chipid; 462 458 u32 chiprev; 463 459 struct mii_bus *mdiobus; 464 460 phy_interface_t interface; 465 461 466 - int fc_autoneg; 467 - u8 fc_request_control; 468 - 469 462 int delta; 470 463 struct statstage stats; 471 464 472 465 struct irq_domain_data domain_data; 466 + 467 + struct phylink *phylink; 468 + struct phylink_config phylink_config; 473 469 }; 474 470 475 471 /* use ethtool to change the level for any given device */ ··· 1550 1554 schedule_work(&pdata->set_multicast); 1551 1555 } 1552 1556 1553 - static int lan78xx_configure_flowcontrol(struct lan78xx_net *dev, 1554 - bool tx_pause, bool rx_pause); 1555 - 1556 - static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex, 1557 - u16 lcladv, u16 rmtadv) 1558 - { 1559 - u8 cap; 1560 - 1561 - if (dev->fc_autoneg) 1562 - cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv); 1563 - else 1564 - cap = dev->fc_request_control; 1565 - 1566 - netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s", 1567 - (cap & FLOW_CTRL_RX ? "enabled" : "disabled"), 1568 - (cap & FLOW_CTRL_TX ? "enabled" : "disabled")); 1569 - 1570 - return lan78xx_configure_flowcontrol(dev, 1571 - cap & FLOW_CTRL_TX, 1572 - cap & FLOW_CTRL_RX); 1573 - } 1574 - 1575 1557 static void lan78xx_rx_urb_submit_all(struct lan78xx_net *dev); 1576 1558 1577 1559 static int lan78xx_mac_reset(struct lan78xx_net *dev) ··· 1612 1638 return lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_); 1613 1639 } 1614 1640 1615 - static int lan78xx_configure_usb(struct lan78xx_net *dev, int speed); 1616 - 1617 - static int lan78xx_link_reset(struct lan78xx_net *dev) 1618 - { 1619 - struct phy_device *phydev = dev->net->phydev; 1620 - struct ethtool_link_ksettings ecmd; 1621 - int ladv, radv, ret, link; 1622 - 1623 - /* clear LAN78xx interrupt status */ 1624 - ret = lan78xx_phy_int_ack(dev); 1625 - if (unlikely(ret < 0)) 1626 - return ret; 1627 - 1628 - mutex_lock(&phydev->lock); 1629 - phy_read_status(phydev); 1630 - link = phydev->link; 1631 - mutex_unlock(&phydev->lock); 1632 - 1633 - if (!link && dev->link_on) { 1634 - dev->link_on = false; 1635 - 1636 - /* reset MAC */ 1637 - ret = lan78xx_mac_reset(dev); 1638 - if (ret < 0) 1639 - return ret; 1640 - 1641 - timer_delete(&dev->stat_monitor); 1642 - } else if (link && !dev->link_on) { 1643 - dev->link_on = true; 1644 - 1645 - phy_ethtool_ksettings_get(phydev, &ecmd); 1646 - 1647 - ret = lan78xx_configure_usb(dev, ecmd.base.speed); 1648 - if (ret < 0) 1649 - return ret; 1650 - 1651 - ladv = phy_read(phydev, MII_ADVERTISE); 1652 - if (ladv < 0) 1653 - return ladv; 1654 - 1655 - radv = phy_read(phydev, MII_LPA); 1656 - if (radv < 0) 1657 - return radv; 1658 - 1659 - netif_dbg(dev, link, dev->net, 1660 - "speed: %u duplex: %d anadv: 0x%04x anlpa: 0x%04x", 1661 - ecmd.base.speed, ecmd.base.duplex, ladv, radv); 1662 - 1663 - ret = lan78xx_update_flowcontrol(dev, ecmd.base.duplex, ladv, 1664 - radv); 1665 - if (ret < 0) 1666 - return ret; 1667 - 1668 - if (!timer_pending(&dev->stat_monitor)) { 1669 - dev->delta = 1; 1670 - mod_timer(&dev->stat_monitor, 1671 - jiffies + STAT_UPDATE_TIMER); 1672 - } 1673 - 1674 - lan78xx_rx_urb_submit_all(dev); 1675 - 1676 - local_bh_disable(); 1677 - napi_schedule(&dev->napi); 1678 - local_bh_enable(); 1679 - } 1680 - 1681 - return 0; 1682 - } 1683 - 1684 1641 /* some work can't be done in tasklets, so we use keventd 1685 1642 * 1686 1643 * NOTE: annoying asymmetry: if it's active, schedule_work() fails, ··· 1638 1733 1639 1734 if (intdata & INT_ENP_PHY_INT) { 1640 1735 netif_dbg(dev, link, dev->net, "PHY INTR: 0x%08x\n", intdata); 1641 - lan78xx_defer_kevent(dev, EVENT_LINK_RESET); 1736 + lan78xx_defer_kevent(dev, EVENT_PHY_INT_ACK); 1642 1737 1643 1738 if (dev->domain_data.phyirq > 0) 1644 1739 generic_handle_irq_safe(dev->domain_data.phyirq); ··· 1785 1880 static int lan78xx_get_eee(struct net_device *net, struct ethtool_keee *edata) 1786 1881 { 1787 1882 struct lan78xx_net *dev = netdev_priv(net); 1788 - struct phy_device *phydev = net->phydev; 1789 - int ret; 1790 - u32 buf; 1791 1883 1792 - ret = usb_autopm_get_interface(dev->intf); 1793 - if (ret < 0) 1794 - return ret; 1795 - 1796 - ret = phy_ethtool_get_eee(phydev, edata); 1797 - if (ret < 0) 1798 - goto exit; 1799 - 1800 - ret = lan78xx_read_reg(dev, MAC_CR, &buf); 1801 - if (buf & MAC_CR_EEE_EN_) { 1802 - /* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */ 1803 - ret = lan78xx_read_reg(dev, EEE_TX_LPI_REQ_DLY, &buf); 1804 - edata->tx_lpi_timer = buf; 1805 - } else { 1806 - edata->tx_lpi_timer = 0; 1807 - } 1808 - 1809 - ret = 0; 1810 - exit: 1811 - usb_autopm_put_interface(dev->intf); 1812 - 1813 - return ret; 1884 + return phylink_ethtool_get_eee(dev->phylink, edata); 1814 1885 } 1815 1886 1816 1887 static int lan78xx_set_eee(struct net_device *net, struct ethtool_keee *edata) 1817 1888 { 1818 1889 struct lan78xx_net *dev = netdev_priv(net); 1819 - int ret; 1820 - u32 buf; 1821 1890 1822 - ret = usb_autopm_get_interface(dev->intf); 1823 - if (ret < 0) 1824 - return ret; 1825 - 1826 - ret = phy_ethtool_set_eee(net->phydev, edata); 1827 - if (ret < 0) 1828 - goto out; 1829 - 1830 - buf = (u32)edata->tx_lpi_timer; 1831 - ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, buf); 1832 - out: 1833 - usb_autopm_put_interface(dev->intf); 1834 - 1835 - return ret; 1836 - } 1837 - 1838 - static u32 lan78xx_get_link(struct net_device *net) 1839 - { 1840 - u32 link; 1841 - 1842 - mutex_lock(&net->phydev->lock); 1843 - phy_read_status(net->phydev); 1844 - link = net->phydev->link; 1845 - mutex_unlock(&net->phydev->lock); 1846 - 1847 - return link; 1891 + return phylink_ethtool_set_eee(dev->phylink, edata); 1848 1892 } 1849 1893 1850 1894 static void lan78xx_get_drvinfo(struct net_device *net, ··· 1823 1969 struct ethtool_link_ksettings *cmd) 1824 1970 { 1825 1971 struct lan78xx_net *dev = netdev_priv(net); 1826 - struct phy_device *phydev = net->phydev; 1827 - int ret; 1828 1972 1829 - ret = usb_autopm_get_interface(dev->intf); 1830 - if (ret < 0) 1831 - return ret; 1832 - 1833 - phy_ethtool_ksettings_get(phydev, cmd); 1834 - 1835 - usb_autopm_put_interface(dev->intf); 1836 - 1837 - return ret; 1973 + return phylink_ethtool_ksettings_get(dev->phylink, cmd); 1838 1974 } 1839 1975 1840 1976 static int lan78xx_set_link_ksettings(struct net_device *net, 1841 1977 const struct ethtool_link_ksettings *cmd) 1842 1978 { 1843 1979 struct lan78xx_net *dev = netdev_priv(net); 1844 - struct phy_device *phydev = net->phydev; 1845 - int ret = 0; 1846 - int temp; 1847 1980 1848 - ret = usb_autopm_get_interface(dev->intf); 1849 - if (ret < 0) 1850 - return ret; 1851 - 1852 - /* change speed & duplex */ 1853 - ret = phy_ethtool_ksettings_set(phydev, cmd); 1854 - 1855 - if (!cmd->base.autoneg) { 1856 - /* force link down */ 1857 - temp = phy_read(phydev, MII_BMCR); 1858 - phy_write(phydev, MII_BMCR, temp | BMCR_LOOPBACK); 1859 - mdelay(1); 1860 - phy_write(phydev, MII_BMCR, temp); 1861 - } 1862 - 1863 - usb_autopm_put_interface(dev->intf); 1864 - 1865 - return ret; 1981 + return phylink_ethtool_ksettings_set(dev->phylink, cmd); 1866 1982 } 1867 1983 1868 1984 static void lan78xx_get_pause(struct net_device *net, 1869 1985 struct ethtool_pauseparam *pause) 1870 1986 { 1871 1987 struct lan78xx_net *dev = netdev_priv(net); 1872 - struct phy_device *phydev = net->phydev; 1873 - struct ethtool_link_ksettings ecmd; 1874 1988 1875 - phy_ethtool_ksettings_get(phydev, &ecmd); 1876 - 1877 - pause->autoneg = dev->fc_autoneg; 1878 - 1879 - if (dev->fc_request_control & FLOW_CTRL_TX) 1880 - pause->tx_pause = 1; 1881 - 1882 - if (dev->fc_request_control & FLOW_CTRL_RX) 1883 - pause->rx_pause = 1; 1989 + phylink_ethtool_get_pauseparam(dev->phylink, pause); 1884 1990 } 1885 1991 1886 1992 static int lan78xx_set_pause(struct net_device *net, 1887 1993 struct ethtool_pauseparam *pause) 1888 1994 { 1889 1995 struct lan78xx_net *dev = netdev_priv(net); 1890 - struct phy_device *phydev = net->phydev; 1891 - struct ethtool_link_ksettings ecmd; 1892 - int ret; 1893 1996 1894 - phy_ethtool_ksettings_get(phydev, &ecmd); 1895 - 1896 - if (pause->autoneg && !ecmd.base.autoneg) { 1897 - ret = -EINVAL; 1898 - goto exit; 1899 - } 1900 - 1901 - dev->fc_request_control = 0; 1902 - if (pause->rx_pause) 1903 - dev->fc_request_control |= FLOW_CTRL_RX; 1904 - 1905 - if (pause->tx_pause) 1906 - dev->fc_request_control |= FLOW_CTRL_TX; 1907 - 1908 - if (ecmd.base.autoneg) { 1909 - __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) = { 0, }; 1910 - u32 mii_adv; 1911 - 1912 - linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, 1913 - ecmd.link_modes.advertising); 1914 - linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 1915 - ecmd.link_modes.advertising); 1916 - mii_adv = (u32)mii_advertise_flowctrl(dev->fc_request_control); 1917 - mii_adv_to_linkmode_adv_t(fc, mii_adv); 1918 - linkmode_or(ecmd.link_modes.advertising, fc, 1919 - ecmd.link_modes.advertising); 1920 - 1921 - phy_ethtool_ksettings_set(phydev, &ecmd); 1922 - } 1923 - 1924 - dev->fc_autoneg = pause->autoneg; 1925 - 1926 - ret = 0; 1927 - exit: 1928 - return ret; 1997 + return phylink_ethtool_set_pauseparam(dev->phylink, pause); 1929 1998 } 1930 1999 1931 2000 static int lan78xx_get_regs_len(struct net_device *netdev) ··· 1885 2108 } 1886 2109 1887 2110 static const struct ethtool_ops lan78xx_ethtool_ops = { 1888 - .get_link = lan78xx_get_link, 2111 + .get_link = ethtool_op_get_link, 1889 2112 .nway_reset = phy_ethtool_nway_reset, 1890 2113 .get_drvinfo = lan78xx_get_drvinfo, 1891 2114 .get_msglevel = lan78xx_get_msglevel, ··· 2109 2332 mdiobus_free(dev->mdiobus); 2110 2333 } 2111 2334 2112 - static void lan78xx_link_status_change(struct net_device *net) 2113 - { 2114 - struct lan78xx_net *dev = netdev_priv(net); 2115 - struct phy_device *phydev = net->phydev; 2116 - u32 data; 2117 - int ret; 2118 - 2119 - ret = lan78xx_read_reg(dev, MAC_CR, &data); 2120 - if (ret < 0) 2121 - return; 2122 - 2123 - if (phydev->enable_tx_lpi) 2124 - data |= MAC_CR_EEE_EN_; 2125 - else 2126 - data &= ~MAC_CR_EEE_EN_; 2127 - lan78xx_write_reg(dev, MAC_CR, data); 2128 - 2129 - phy_print_status(phydev); 2130 - } 2131 - 2132 2335 static int irq_map(struct irq_domain *d, unsigned int irq, 2133 2336 irq_hw_number_t hwirq) 2134 2337 { ··· 2236 2479 } 2237 2480 dev->domain_data.phyirq = 0; 2238 2481 dev->domain_data.irqdomain = NULL; 2482 + } 2483 + 2484 + static void lan78xx_mac_config(struct phylink_config *config, unsigned int mode, 2485 + const struct phylink_link_state *state) 2486 + { 2487 + struct net_device *net = to_net_dev(config->dev); 2488 + struct lan78xx_net *dev = netdev_priv(net); 2489 + u32 mac_cr = 0; 2490 + int ret; 2491 + 2492 + /* Check if the mode is supported */ 2493 + if (mode != MLO_AN_FIXED && mode != MLO_AN_PHY) { 2494 + netdev_err(net, "Unsupported negotiation mode: %u\n", mode); 2495 + return; 2496 + } 2497 + 2498 + switch (state->interface) { 2499 + case PHY_INTERFACE_MODE_GMII: 2500 + mac_cr |= MAC_CR_GMII_EN_; 2501 + break; 2502 + case PHY_INTERFACE_MODE_RGMII: 2503 + case PHY_INTERFACE_MODE_RGMII_ID: 2504 + case PHY_INTERFACE_MODE_RGMII_TXID: 2505 + case PHY_INTERFACE_MODE_RGMII_RXID: 2506 + break; 2507 + default: 2508 + netdev_warn(net, "Unsupported interface mode: %d\n", 2509 + state->interface); 2510 + return; 2511 + } 2512 + 2513 + ret = lan78xx_update_reg(dev, MAC_CR, MAC_CR_GMII_EN_, mac_cr); 2514 + if (ret < 0) 2515 + netdev_err(net, "Failed to config MAC with error %pe\n", 2516 + ERR_PTR(ret)); 2517 + } 2518 + 2519 + static void lan78xx_mac_link_down(struct phylink_config *config, 2520 + unsigned int mode, phy_interface_t interface) 2521 + { 2522 + struct net_device *net = to_net_dev(config->dev); 2523 + struct lan78xx_net *dev = netdev_priv(net); 2524 + int ret; 2525 + 2526 + netif_stop_queue(net); 2527 + 2528 + /* MAC reset will not de-assert TXEN/RXEN, we need to stop them 2529 + * manually before reset. TX and RX should be disabled before running 2530 + * link_up sequence. 2531 + */ 2532 + ret = lan78xx_stop_tx_path(dev); 2533 + if (ret < 0) 2534 + goto link_down_fail; 2535 + 2536 + ret = lan78xx_stop_rx_path(dev); 2537 + if (ret < 0) 2538 + goto link_down_fail; 2539 + 2540 + /* MAC reset seems to not affect MAC configuration, no idea if it is 2541 + * really needed, but it was done in previous driver version. So, leave 2542 + * it here. 2543 + */ 2544 + ret = lan78xx_mac_reset(dev); 2545 + if (ret < 0) 2546 + goto link_down_fail; 2547 + 2548 + return; 2549 + 2550 + link_down_fail: 2551 + netdev_err(dev->net, "Failed to set MAC down with error %pe\n", 2552 + ERR_PTR(ret)); 2239 2553 } 2240 2554 2241 2555 /** ··· 2444 2616 return lan78xx_write_reg(dev, FLOW, flow); 2445 2617 } 2446 2618 2619 + static void lan78xx_mac_link_up(struct phylink_config *config, 2620 + struct phy_device *phy, 2621 + unsigned int mode, phy_interface_t interface, 2622 + int speed, int duplex, 2623 + bool tx_pause, bool rx_pause) 2624 + { 2625 + struct net_device *net = to_net_dev(config->dev); 2626 + struct lan78xx_net *dev = netdev_priv(net); 2627 + u32 mac_cr = 0; 2628 + int ret; 2629 + 2630 + switch (speed) { 2631 + case SPEED_1000: 2632 + mac_cr |= MAC_CR_SPEED_1000_; 2633 + break; 2634 + case SPEED_100: 2635 + mac_cr |= MAC_CR_SPEED_100_; 2636 + break; 2637 + case SPEED_10: 2638 + mac_cr |= MAC_CR_SPEED_10_; 2639 + break; 2640 + default: 2641 + netdev_err(dev->net, "Unsupported speed %d\n", speed); 2642 + return; 2643 + } 2644 + 2645 + if (duplex == DUPLEX_FULL) 2646 + mac_cr |= MAC_CR_FULL_DUPLEX_; 2647 + 2648 + /* make sure TXEN and RXEN are disabled before reconfiguring MAC */ 2649 + ret = lan78xx_update_reg(dev, MAC_CR, MAC_CR_SPEED_MASK_ | 2650 + MAC_CR_FULL_DUPLEX_ | MAC_CR_EEE_EN_, mac_cr); 2651 + if (ret < 0) 2652 + goto link_up_fail; 2653 + 2654 + ret = lan78xx_configure_flowcontrol(dev, tx_pause, rx_pause); 2655 + if (ret < 0) 2656 + goto link_up_fail; 2657 + 2658 + ret = lan78xx_configure_usb(dev, speed); 2659 + if (ret < 0) 2660 + goto link_up_fail; 2661 + 2662 + lan78xx_rx_urb_submit_all(dev); 2663 + 2664 + ret = lan78xx_flush_rx_fifo(dev); 2665 + if (ret < 0) 2666 + goto link_up_fail; 2667 + 2668 + ret = lan78xx_flush_tx_fifo(dev); 2669 + if (ret < 0) 2670 + goto link_up_fail; 2671 + 2672 + ret = lan78xx_start_tx_path(dev); 2673 + if (ret < 0) 2674 + goto link_up_fail; 2675 + 2676 + ret = lan78xx_start_rx_path(dev); 2677 + if (ret < 0) 2678 + goto link_up_fail; 2679 + 2680 + netif_start_queue(net); 2681 + 2682 + return; 2683 + 2684 + link_up_fail: 2685 + netdev_err(dev->net, "Failed to set MAC up with error %pe\n", 2686 + ERR_PTR(ret)); 2687 + } 2688 + 2447 2689 /** 2448 - * lan78xx_register_fixed_phy() - Register a fallback fixed PHY 2690 + * lan78xx_mac_eee_enable - Enable or disable MAC-side EEE support 2691 + * @dev: LAN78xx device 2692 + * @enable: true to enable EEE, false to disable 2693 + * 2694 + * This function sets or clears the MAC_CR_EEE_EN_ bit to control Energy 2695 + * Efficient Ethernet (EEE) operation. According to current understanding 2696 + * of the LAN7800 documentation, this bit can be modified while TX and RX 2697 + * are enabled. No explicit requirement was found to disable data paths 2698 + * before changing this bit. 2699 + * 2700 + * Return: 0 on success or a negative error code 2701 + */ 2702 + static int lan78xx_mac_eee_enable(struct lan78xx_net *dev, bool enable) 2703 + { 2704 + u32 mac_cr = 0; 2705 + 2706 + if (enable) 2707 + mac_cr |= MAC_CR_EEE_EN_; 2708 + 2709 + return lan78xx_update_reg(dev, MAC_CR, MAC_CR_EEE_EN_, mac_cr); 2710 + } 2711 + 2712 + static void lan78xx_mac_disable_tx_lpi(struct phylink_config *config) 2713 + { 2714 + struct net_device *net = to_net_dev(config->dev); 2715 + struct lan78xx_net *dev = netdev_priv(net); 2716 + 2717 + lan78xx_mac_eee_enable(dev, false); 2718 + } 2719 + 2720 + static int lan78xx_mac_enable_tx_lpi(struct phylink_config *config, u32 timer, 2721 + bool tx_clk_stop) 2722 + { 2723 + struct net_device *net = to_net_dev(config->dev); 2724 + struct lan78xx_net *dev = netdev_priv(net); 2725 + int ret; 2726 + 2727 + /* Software should only change this field when Energy Efficient 2728 + * Ethernet Enable (EEEEN) is cleared. We ensure that by clearing 2729 + * EEEEN during probe, and phylink itself guarantees that 2730 + * mac_disable_tx_lpi() will have been previously called. 2731 + */ 2732 + ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, timer); 2733 + if (ret < 0) 2734 + return ret; 2735 + 2736 + return lan78xx_mac_eee_enable(dev, true); 2737 + } 2738 + 2739 + static const struct phylink_mac_ops lan78xx_phylink_mac_ops = { 2740 + .mac_config = lan78xx_mac_config, 2741 + .mac_link_down = lan78xx_mac_link_down, 2742 + .mac_link_up = lan78xx_mac_link_up, 2743 + .mac_disable_tx_lpi = lan78xx_mac_disable_tx_lpi, 2744 + .mac_enable_tx_lpi = lan78xx_mac_enable_tx_lpi, 2745 + }; 2746 + 2747 + /** 2748 + * lan78xx_set_fixed_link() - Set fixed link configuration for LAN7801 2449 2749 * @dev: LAN78xx device 2450 2750 * 2451 - * Registers a fixed PHY with 1 Gbps full duplex. This is used in special cases 2452 - * like EVB-KSZ9897-1, where LAN7801 acts as a USB-to-Ethernet interface to a 2453 - * switch without a visible PHY. 2751 + * Use fixed link configuration with 1 Gbps full duplex. This is used in special 2752 + * cases like EVB-KSZ9897-1, where LAN7801 acts as a USB-to-Ethernet interface 2753 + * to a switch without a visible PHY. 2454 2754 * 2455 2755 * Return: pointer to the registered fixed PHY, or ERR_PTR() on error. 2456 2756 */ 2457 - static struct phy_device *lan78xx_register_fixed_phy(struct lan78xx_net *dev) 2757 + static int lan78xx_set_fixed_link(struct lan78xx_net *dev) 2458 2758 { 2459 - static const struct fixed_phy_status fphy_status = { 2460 - .link = 1, 2759 + static const struct phylink_link_state state = { 2461 2760 .speed = SPEED_1000, 2462 2761 .duplex = DUPLEX_FULL, 2463 2762 }; 2464 2763 2465 2764 netdev_info(dev->net, 2466 - "No PHY found on LAN7801 – registering fixed PHY (e.g. EVB-KSZ9897-1)\n"); 2765 + "No PHY found on LAN7801 – using fixed link instead (e.g. EVB-KSZ9897-1)\n"); 2467 2766 2468 - return fixed_phy_register(&fphy_status, NULL); 2767 + return phylink_set_fixed_link(dev->phylink, &state); 2469 2768 } 2470 2769 2471 2770 /** ··· 2628 2673 2629 2674 dev->interface = PHY_INTERFACE_MODE_RGMII; 2630 2675 /* No PHY found – fallback to fixed PHY (e.g. KSZ switch board) */ 2631 - return lan78xx_register_fixed_phy(dev); 2676 + return NULL; 2632 2677 2633 2678 case ID_REV_CHIP_ID_7800_: 2634 2679 case ID_REV_CHIP_ID_7850_: ··· 2755 2800 return lan78xx_write_reg(dev, HW_CFG, reg); 2756 2801 } 2757 2802 2803 + static int lan78xx_phylink_setup(struct lan78xx_net *dev) 2804 + { 2805 + struct phylink_config *pc = &dev->phylink_config; 2806 + struct phylink *phylink; 2807 + 2808 + pc->dev = &dev->net->dev; 2809 + pc->type = PHYLINK_NETDEV; 2810 + pc->mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | MAC_10 | 2811 + MAC_100 | MAC_1000FD; 2812 + pc->mac_managed_pm = true; 2813 + pc->lpi_capabilities = MAC_100FD | MAC_1000FD; 2814 + /* 2815 + * Default TX LPI (Low Power Idle) request delay count is set to 50us. 2816 + * 2817 + * Source: LAN7800 Documentation, DS00001992H, Section 15.1.57, Page 204. 2818 + * 2819 + * Reasoning: 2820 + * According to the application note in the LAN7800 documentation, a 2821 + * zero delay may negatively impact the TX data path’s ability to 2822 + * support Gigabit operation. A value of 50us is recommended as a 2823 + * reasonable default when the part operates at Gigabit speeds, 2824 + * balancing stability and power efficiency in EEE mode. This delay can 2825 + * be increased based on performance testing, as EEE is designed for 2826 + * scenarios with mostly idle links and occasional bursts of full 2827 + * bandwidth transmission. The goal is to ensure reliable Gigabit 2828 + * performance without overly aggressive power optimization during 2829 + * inactive periods. 2830 + */ 2831 + pc->lpi_timer_default = 50; 2832 + pc->eee_enabled_default = true; 2833 + 2834 + if (dev->chipid == ID_REV_CHIP_ID_7801_) 2835 + phy_interface_set_rgmii(pc->supported_interfaces); 2836 + else 2837 + __set_bit(PHY_INTERFACE_MODE_GMII, pc->supported_interfaces); 2838 + 2839 + memcpy(dev->phylink_config.lpi_interfaces, 2840 + dev->phylink_config.supported_interfaces, 2841 + sizeof(dev->phylink_config.lpi_interfaces)); 2842 + 2843 + phylink = phylink_create(pc, dev->net->dev.fwnode, 2844 + dev->interface, &lan78xx_phylink_mac_ops); 2845 + if (IS_ERR(phylink)) 2846 + return PTR_ERR(phylink); 2847 + 2848 + dev->phylink = phylink; 2849 + 2850 + return 0; 2851 + } 2852 + 2853 + static void lan78xx_phy_uninit(struct lan78xx_net *dev) 2854 + { 2855 + if (dev->phylink) { 2856 + phylink_disconnect_phy(dev->phylink); 2857 + phylink_destroy(dev->phylink); 2858 + dev->phylink = NULL; 2859 + } 2860 + } 2861 + 2758 2862 static int lan78xx_phy_init(struct lan78xx_net *dev) 2759 2863 { 2760 - __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) = { 0, }; 2761 - int ret; 2762 - u32 mii_adv; 2763 2864 struct phy_device *phydev; 2865 + int ret; 2764 2866 2765 2867 phydev = lan78xx_get_phy(dev); 2868 + /* phydev can be NULL if no PHY is found and the chip is LAN7801, 2869 + * which will use a fixed link later. 2870 + * If an error occurs, return the error code immediately. 2871 + */ 2766 2872 if (IS_ERR(phydev)) 2767 2873 return PTR_ERR(phydev); 2768 2874 2875 + ret = lan78xx_phylink_setup(dev); 2876 + if (ret < 0) 2877 + return ret; 2878 + 2879 + /* If no PHY is found, set up a fixed link. It is very specific to 2880 + * the LAN7801 and is used in special cases like EVB-KSZ9897-1 where 2881 + * LAN7801 acts as a USB-to-Ethernet interface to a switch without 2882 + * a visible PHY. 2883 + */ 2884 + if (!phydev) { 2885 + ret = lan78xx_set_fixed_link(dev); 2886 + if (ret < 0) 2887 + goto phylink_uninit; 2888 + } 2889 + 2769 2890 ret = lan78xx_mac_prepare_for_phy(dev); 2770 2891 if (ret < 0) 2771 - goto free_phy; 2892 + goto phylink_uninit; 2772 2893 2773 2894 /* if phyirq is not set, use polling mode in phylib */ 2774 2895 if (dev->domain_data.phyirq > 0) ··· 2853 2822 phydev->irq = PHY_POLL; 2854 2823 netdev_dbg(dev->net, "phydev->irq = %d\n", phydev->irq); 2855 2824 2856 - /* set to AUTOMDIX */ 2857 - phydev->mdix = ETH_TP_MDI_AUTO; 2858 - 2859 - ret = phy_connect_direct(dev->net, phydev, 2860 - lan78xx_link_status_change, 2861 - dev->interface); 2825 + ret = phylink_connect_phy(dev->phylink, phydev); 2862 2826 if (ret) { 2863 - netdev_err(dev->net, "can't attach PHY to %s\n", 2864 - dev->mdiobus->id); 2865 - if (dev->chipid == ID_REV_CHIP_ID_7801_) { 2866 - if (phy_is_pseudo_fixed_link(phydev)) { 2867 - fixed_phy_unregister(phydev); 2868 - phy_device_free(phydev); 2869 - } 2870 - } 2871 - return -EIO; 2827 + netdev_err(dev->net, "can't attach PHY to %s, error %pe\n", 2828 + dev->mdiobus->id, ERR_PTR(ret)); 2829 + goto phylink_uninit; 2872 2830 } 2873 2831 2874 - /* MAC doesn't support 1000T Half */ 2875 - phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 2876 - 2877 - /* support both flow controls */ 2878 - dev->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX); 2879 - linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2880 - phydev->advertising); 2881 - linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2882 - phydev->advertising); 2883 - mii_adv = (u32)mii_advertise_flowctrl(dev->fc_request_control); 2884 - mii_adv_to_linkmode_adv_t(fc, mii_adv); 2885 - linkmode_or(phydev->advertising, fc, phydev->advertising); 2886 - 2887 - phy_support_eee(phydev); 2888 - 2889 2832 ret = lan78xx_configure_leds_from_dt(dev, phydev); 2890 - if (ret) 2891 - goto free_phy; 2892 - 2893 - genphy_config_aneg(phydev); 2894 - 2895 - dev->fc_autoneg = phydev->autoneg; 2833 + if (ret < 0) 2834 + goto phylink_uninit; 2896 2835 2897 2836 return 0; 2898 2837 2899 - free_phy: 2900 - if (phy_is_pseudo_fixed_link(phydev)) { 2901 - fixed_phy_unregister(phydev); 2902 - phy_device_free(phydev); 2903 - } 2838 + phylink_uninit: 2839 + lan78xx_phy_uninit(dev); 2904 2840 2905 2841 return ret; 2906 2842 } ··· 3208 3210 unsigned long timeout; 3209 3211 int ret; 3210 3212 u32 buf; 3211 - u8 sig; 3212 3213 3213 3214 ret = lan78xx_read_reg(dev, HW_CFG, &buf); 3214 3215 if (ret < 0) ··· 3364 3367 if (ret < 0) 3365 3368 return ret; 3366 3369 3367 - /* LAN7801 only has RGMII mode */ 3368 - if (dev->chipid == ID_REV_CHIP_ID_7801_) { 3369 - buf &= ~MAC_CR_GMII_EN_; 3370 - /* Enable Auto Duplex and Auto speed */ 3371 - buf |= MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_; 3372 - } 3370 + buf &= ~(MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_ | MAC_CR_EEE_EN_); 3373 3371 3374 - if (dev->chipid == ID_REV_CHIP_ID_7800_ || 3375 - dev->chipid == ID_REV_CHIP_ID_7850_) { 3376 - ret = lan78xx_read_raw_eeprom(dev, 0, 1, &sig); 3377 - if (!ret && sig != EEPROM_INDICATOR) { 3378 - /* Implies there is no external eeprom. Set mac speed */ 3379 - netdev_info(dev->net, "No External EEPROM. Setting MAC Speed\n"); 3380 - buf |= MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_; 3381 - } 3382 - } 3372 + /* LAN7801 only has RGMII mode */ 3373 + if (dev->chipid == ID_REV_CHIP_ID_7801_) 3374 + buf &= ~MAC_CR_GMII_EN_; 3375 + 3383 3376 ret = lan78xx_write_reg(dev, MAC_CR, buf); 3384 3377 if (ret < 0) 3385 3378 return ret; ··· 3419 3432 3420 3433 mutex_lock(&dev->dev_mutex); 3421 3434 3422 - phy_start(net->phydev); 3435 + lan78xx_init_stats(dev); 3423 3436 3424 - netif_dbg(dev, ifup, dev->net, "phy initialised successfully"); 3437 + napi_enable(&dev->napi); 3438 + 3439 + set_bit(EVENT_DEV_OPEN, &dev->flags); 3425 3440 3426 3441 /* for Link Check */ 3427 3442 if (dev->urb_intr) { ··· 3435 3446 } 3436 3447 } 3437 3448 3438 - ret = lan78xx_flush_rx_fifo(dev); 3439 - if (ret < 0) 3440 - goto done; 3441 - ret = lan78xx_flush_tx_fifo(dev); 3442 - if (ret < 0) 3443 - goto done; 3449 + phylink_start(dev->phylink); 3444 3450 3445 - ret = lan78xx_start_tx_path(dev); 3446 - if (ret < 0) 3447 - goto done; 3448 - ret = lan78xx_start_rx_path(dev); 3449 - if (ret < 0) 3450 - goto done; 3451 - 3452 - lan78xx_init_stats(dev); 3453 - 3454 - set_bit(EVENT_DEV_OPEN, &dev->flags); 3455 - 3456 - netif_start_queue(net); 3457 - 3458 - dev->link_on = false; 3459 - 3460 - napi_enable(&dev->napi); 3461 - 3462 - lan78xx_defer_kevent(dev, EVENT_LINK_RESET); 3463 3451 done: 3464 3452 mutex_unlock(&dev->dev_mutex); 3465 3453 ··· 3494 3528 timer_delete_sync(&dev->stat_monitor); 3495 3529 3496 3530 clear_bit(EVENT_DEV_OPEN, &dev->flags); 3497 - netif_stop_queue(net); 3498 3531 napi_disable(&dev->napi); 3499 3532 3500 3533 lan78xx_terminate_urbs(dev); ··· 3503 3538 net->stats.rx_packets, net->stats.tx_packets, 3504 3539 net->stats.rx_errors, net->stats.tx_errors); 3505 3540 3506 - /* ignore errors that occur stopping the Tx and Rx data paths */ 3507 - lan78xx_stop_tx_path(dev); 3508 - lan78xx_stop_rx_path(dev); 3509 - 3510 - if (net->phydev) 3511 - phy_stop(net->phydev); 3541 + phylink_stop(dev->phylink); 3512 3542 3513 3543 usb_kill_urb(dev->urb_intr); 3514 3544 ··· 3513 3553 */ 3514 3554 clear_bit(EVENT_TX_HALT, &dev->flags); 3515 3555 clear_bit(EVENT_RX_HALT, &dev->flags); 3516 - clear_bit(EVENT_LINK_RESET, &dev->flags); 3556 + clear_bit(EVENT_PHY_INT_ACK, &dev->flags); 3517 3557 clear_bit(EVENT_STAT_UPDATE, &dev->flags); 3518 3558 3519 3559 cancel_delayed_work_sync(&dev->wq); ··· 4437 4477 } 4438 4478 } 4439 4479 4440 - if (test_bit(EVENT_LINK_RESET, &dev->flags)) { 4480 + if (test_bit(EVENT_PHY_INT_ACK, &dev->flags)) { 4441 4481 int ret = 0; 4442 4482 4443 - clear_bit(EVENT_LINK_RESET, &dev->flags); 4444 - if (lan78xx_link_reset(dev) < 0) { 4445 - netdev_info(dev->net, "link reset failed (%d)\n", 4446 - ret); 4447 - } 4483 + clear_bit(EVENT_PHY_INT_ACK, &dev->flags); 4484 + ret = lan78xx_phy_int_ack(dev); 4485 + if (ret) 4486 + netdev_info(dev->net, "PHY INT ack failed (%pe)\n", 4487 + ERR_PTR(ret)); 4448 4488 } 4449 4489 4450 4490 if (test_bit(EVENT_STAT_UPDATE, &dev->flags)) { ··· 4518 4558 struct lan78xx_net *dev; 4519 4559 struct usb_device *udev; 4520 4560 struct net_device *net; 4521 - struct phy_device *phydev; 4522 4561 4523 4562 dev = usb_get_intfdata(intf); 4524 4563 usb_set_intfdata(intf, NULL); 4525 4564 if (!dev) 4526 4565 return; 4527 4566 4528 - netif_napi_del(&dev->napi); 4529 - 4530 4567 udev = interface_to_usbdev(intf); 4531 4568 net = dev->net; 4569 + 4570 + rtnl_lock(); 4571 + phylink_stop(dev->phylink); 4572 + phylink_disconnect_phy(dev->phylink); 4573 + rtnl_unlock(); 4574 + 4575 + netif_napi_del(&dev->napi); 4532 4576 4533 4577 unregister_netdev(net); 4534 4578 ··· 4540 4576 set_bit(EVENT_DEV_DISCONNECT, &dev->flags); 4541 4577 cancel_delayed_work_sync(&dev->wq); 4542 4578 4543 - phydev = net->phydev; 4544 - 4545 - phy_disconnect(net->phydev); 4546 - 4547 - if (phy_is_pseudo_fixed_link(phydev)) { 4548 - fixed_phy_unregister(phydev); 4549 - phy_device_free(phydev); 4550 - } 4579 + phylink_destroy(dev->phylink); 4551 4580 4552 4581 usb_scuttle_anchored_urbs(&dev->deferred); 4553 4582 ··· 4624 4667 goto out1; 4625 4668 } 4626 4669 4627 - /* netdev_printk() needs this */ 4628 4670 SET_NETDEV_DEV(netdev, &intf->dev); 4629 4671 4630 4672 dev = netdev_priv(netdev); ··· 4742 4786 ret = register_netdev(netdev); 4743 4787 if (ret != 0) { 4744 4788 netif_err(dev, probe, netdev, "couldn't register the device\n"); 4745 - goto out8; 4789 + goto phy_uninit; 4746 4790 } 4747 4791 4748 4792 usb_set_intfdata(intf, dev); ··· 4757 4801 4758 4802 return 0; 4759 4803 4760 - out8: 4761 - phy_disconnect(netdev->phydev); 4804 + phy_uninit: 4805 + lan78xx_phy_uninit(dev); 4762 4806 free_urbs: 4763 4807 usb_free_urb(dev->urb_intr); 4764 4808 out5: ··· 5093 5137 spin_unlock_irq(&dev->txq.lock); 5094 5138 } 5095 5139 5140 + rtnl_lock(); 5141 + phylink_suspend(dev->phylink, false); 5142 + rtnl_unlock(); 5143 + 5096 5144 /* stop RX */ 5097 5145 ret = lan78xx_stop_rx_path(dev); 5098 5146 if (ret < 0) ··· 5324 5364 if (ret < 0) 5325 5365 return ret; 5326 5366 5327 - phy_start(dev->net->phydev); 5328 - 5329 5367 ret = lan78xx_resume(intf); 5368 + if (ret < 0) 5369 + return ret; 5330 5370 5331 - return ret; 5371 + rtnl_lock(); 5372 + phylink_resume(dev->phylink); 5373 + rtnl_unlock(); 5374 + 5375 + return 0; 5332 5376 } 5333 5377 5334 5378 static const struct usb_device_id products[] = {