]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
net: usb: lan78xx: Integrate EEE support with phylink LPI API
authorOleksij Rempel <o.rempel@pengutronix.de>
Wed, 18 Jun 2025 12:26:01 +0000 (14:26 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 19 Jun 2025 22:37:06 +0000 (15:37 -0700)
Refactor Energy-Efficient Ethernet (EEE) support in the LAN78xx driver to
fully integrate with the phylink Low Power Idle (LPI) API. This includes:

- Replacing direct calls to `phy_ethtool_get_eee` and `phy_ethtool_set_eee`
  with `phylink_ethtool_get_eee` and `phylink_ethtool_set_eee`.
- Implementing `.mac_enable_tx_lpi` and `.mac_disable_tx_lpi` to control
  LPI transitions via phylink.
- Configuring `lpi_timer_default` to align with recommended values from
  LAN7800 documentation.
- ensure EEE is disabled on controller reset

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Reviewed-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
Link: https://patch.msgid.link/20250618122602.3156678-6-o.rempel@pengutronix.de
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/usb/lan78xx.c

index 8df0a2323fb9a5dd5c0d528c0f78ba81d6b136db..3bff1e72a89f59e3ee3ced0f5915282e4d5d79dc 100644 (file)
@@ -1789,54 +1789,15 @@ exit_pm_put:
 static int lan78xx_get_eee(struct net_device *net, struct ethtool_keee *edata)
 {
        struct lan78xx_net *dev = netdev_priv(net);
-       struct phy_device *phydev = net->phydev;
-       int ret;
-       u32 buf;
-
-       ret = usb_autopm_get_interface(dev->intf);
-       if (ret < 0)
-               return ret;
-
-       ret = phy_ethtool_get_eee(phydev, edata);
-       if (ret < 0)
-               goto exit;
 
-       ret = lan78xx_read_reg(dev, MAC_CR, &buf);
-       if (buf & MAC_CR_EEE_EN_) {
-               /* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */
-               ret = lan78xx_read_reg(dev, EEE_TX_LPI_REQ_DLY, &buf);
-               edata->tx_lpi_timer = buf;
-       } else {
-               edata->tx_lpi_timer = 0;
-       }
-
-       ret = 0;
-exit:
-       usb_autopm_put_interface(dev->intf);
-
-       return ret;
+       return phylink_ethtool_get_eee(dev->phylink, edata);
 }
 
 static int lan78xx_set_eee(struct net_device *net, struct ethtool_keee *edata)
 {
        struct lan78xx_net *dev = netdev_priv(net);
-       int ret;
-       u32 buf;
-
-       ret = usb_autopm_get_interface(dev->intf);
-       if (ret < 0)
-               return ret;
 
-       ret = phy_ethtool_set_eee(net->phydev, edata);
-       if (ret < 0)
-               goto out;
-
-       buf = (u32)edata->tx_lpi_timer;
-       ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, buf);
-out:
-       usb_autopm_put_interface(dev->intf);
-
-       return ret;
+       return phylink_ethtool_set_eee(dev->phylink, edata);
 }
 
 static void lan78xx_get_drvinfo(struct net_device *net,
@@ -2557,10 +2518,62 @@ link_up_fail:
                   ERR_PTR(ret));
 }
 
+/**
+ * lan78xx_mac_eee_enable - Enable or disable MAC-side EEE support
+ * @dev: LAN78xx device
+ * @enable: true to enable EEE, false to disable
+ *
+ * This function sets or clears the MAC_CR_EEE_EN_ bit to control Energy
+ * Efficient Ethernet (EEE) operation. According to current understanding
+ * of the LAN7800 documentation, this bit can be modified while TX and RX
+ * are enabled. No explicit requirement was found to disable data paths
+ * before changing this bit.
+ *
+ * Return: 0 on success or a negative error code
+ */
+static int lan78xx_mac_eee_enable(struct lan78xx_net *dev, bool enable)
+{
+       u32 mac_cr = 0;
+
+       if (enable)
+               mac_cr |= MAC_CR_EEE_EN_;
+
+       return lan78xx_update_reg(dev, MAC_CR, MAC_CR_EEE_EN_, mac_cr);
+}
+
+static void lan78xx_mac_disable_tx_lpi(struct phylink_config *config)
+{
+       struct net_device *net = to_net_dev(config->dev);
+       struct lan78xx_net *dev = netdev_priv(net);
+
+       lan78xx_mac_eee_enable(dev, false);
+}
+
+static int lan78xx_mac_enable_tx_lpi(struct phylink_config *config, u32 timer,
+                                    bool tx_clk_stop)
+{
+       struct net_device *net = to_net_dev(config->dev);
+       struct lan78xx_net *dev = netdev_priv(net);
+       int ret;
+
+       /* Software should only change this field when Energy Efficient
+        * Ethernet Enable (EEEEN) is cleared. We ensure that by clearing
+        * EEEEN during probe, and phylink itself guarantees that
+        * mac_disable_tx_lpi() will have been previously called.
+        */
+       ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, timer);
+       if (ret < 0)
+               return ret;
+
+       return lan78xx_mac_eee_enable(dev, true);
+}
+
 static const struct phylink_mac_ops lan78xx_phylink_mac_ops = {
        .mac_config = lan78xx_mac_config,
        .mac_link_down = lan78xx_mac_link_down,
        .mac_link_up = lan78xx_mac_link_up,
+       .mac_disable_tx_lpi = lan78xx_mac_disable_tx_lpi,
+       .mac_enable_tx_lpi = lan78xx_mac_enable_tx_lpi,
 };
 
 /**
@@ -2756,12 +2769,36 @@ static int lan78xx_phylink_setup(struct lan78xx_net *dev)
        pc->mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | MAC_10 |
                               MAC_100 | MAC_1000FD;
        pc->mac_managed_pm = true;
+       pc->lpi_capabilities = MAC_100FD | MAC_1000FD;
+       /*
+        * Default TX LPI (Low Power Idle) request delay count is set to 50us.
+        *
+        * Source: LAN7800 Documentation, DS00001992H, Section 15.1.57, Page 204.
+        *
+        * Reasoning:
+        * According to the application note in the LAN7800 documentation, a
+        * zero delay may negatively impact the TX data path’s ability to
+        * support Gigabit operation. A value of 50us is recommended as a
+        * reasonable default when the part operates at Gigabit speeds,
+        * balancing stability and power efficiency in EEE mode. This delay can
+        * be increased based on performance testing, as EEE is designed for
+        * scenarios with mostly idle links and occasional bursts of full
+        * bandwidth transmission. The goal is to ensure reliable Gigabit
+        * performance without overly aggressive power optimization during
+        * inactive periods.
+        */
+       pc->lpi_timer_default = 50;
+       pc->eee_enabled_default = true;
 
        if (dev->chipid == ID_REV_CHIP_ID_7801_)
                phy_interface_set_rgmii(pc->supported_interfaces);
        else
                __set_bit(PHY_INTERFACE_MODE_GMII, pc->supported_interfaces);
 
+       memcpy(dev->phylink_config.lpi_interfaces,
+              dev->phylink_config.supported_interfaces,
+              sizeof(dev->phylink_config.lpi_interfaces));
+
        phylink = phylink_create(pc, dev->net->dev.fwnode,
                                 dev->interface, &lan78xx_phylink_mac_ops);
        if (IS_ERR(phylink))
@@ -2827,8 +2864,6 @@ static int lan78xx_phy_init(struct lan78xx_net *dev)
                goto phylink_uninit;
        }
 
-       phy_support_eee(phydev);
-
        ret = lan78xx_configure_leds_from_dt(dev, phydev);
        if (ret < 0)
                goto phylink_uninit;
@@ -3333,7 +3368,7 @@ static int lan78xx_reset(struct lan78xx_net *dev)
        if (ret < 0)
                return ret;
 
-       buf &= ~(MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_);
+       buf &= ~(MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_ | MAC_CR_EEE_EN_);
 
        /* LAN7801 only has RGMII mode */
        if (dev->chipid == ID_REV_CHIP_ID_7801_)