]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
r8169: add general EEE support for chip versions from RTL8168g
authorHeiner Kallweit <hkallweit1@gmail.com>
Fri, 25 Jan 2019 19:38:38 +0000 (20:38 +0100)
committerDavid S. Miller <davem@davemloft.net>
Sat, 26 Jan 2019 06:02:52 +0000 (22:02 -0800)
This patch adds the general framework to deal with EEE in this driver
plus EEE support for chip versions from RTL8168g. We don't touch the
default chip settings, therefore EEE will usually be disabled and it's
up to the user to enable it.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/realtek/r8169.c

index 83c4663e6cd38028cd2e4a5074b730183a2c9900..f46bf11873391b053e87b9f93f4d90d6919cc50d 100644 (file)
@@ -1984,6 +1984,172 @@ static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
        return 0;
 }
 
+static int rtl_get_eee_supp(struct rtl8169_private *tp)
+{
+       struct phy_device *phydev = tp->phydev;
+       int ret;
+
+       switch (tp->mac_version) {
+       case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
+               phy_write(phydev, 0x1f, 0x0a5c);
+               ret = phy_read(phydev, 0x12);
+               phy_write(phydev, 0x1f, 0x0000);
+               break;
+       default:
+               ret = -EPROTONOSUPPORT;
+               break;
+       }
+
+       return ret;
+}
+
+static int rtl_get_eee_lpadv(struct rtl8169_private *tp)
+{
+       struct phy_device *phydev = tp->phydev;
+       int ret;
+
+       switch (tp->mac_version) {
+       case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
+               phy_write(phydev, 0x1f, 0x0a5d);
+               ret = phy_read(phydev, 0x11);
+               phy_write(phydev, 0x1f, 0x0000);
+               break;
+       default:
+               ret = -EPROTONOSUPPORT;
+               break;
+       }
+
+       return ret;
+}
+
+static int rtl_get_eee_adv(struct rtl8169_private *tp)
+{
+       struct phy_device *phydev = tp->phydev;
+       int ret;
+
+       switch (tp->mac_version) {
+       case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
+               phy_write(phydev, 0x1f, 0x0a5d);
+               ret = phy_read(phydev, 0x10);
+               phy_write(phydev, 0x1f, 0x0000);
+               break;
+       default:
+               ret = -EPROTONOSUPPORT;
+               break;
+       }
+
+       return ret;
+}
+
+static int rtl_set_eee_adv(struct rtl8169_private *tp, int val)
+{
+       struct phy_device *phydev = tp->phydev;
+       int ret = 0;
+
+       switch (tp->mac_version) {
+       case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
+               phy_write(phydev, 0x1f, 0x0a5d);
+               phy_write(phydev, 0x10, val);
+               phy_write(phydev, 0x1f, 0x0000);
+               break;
+       default:
+               ret = -EPROTONOSUPPORT;
+               break;
+       }
+
+       return ret;
+}
+
+static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
+{
+       struct rtl8169_private *tp = netdev_priv(dev);
+       struct device *d = tp_to_dev(tp);
+       int ret;
+
+       pm_runtime_get_noresume(d);
+
+       if (!pm_runtime_active(d)) {
+               ret = -EOPNOTSUPP;
+               goto out;
+       }
+
+       /* Get Supported EEE */
+       ret = rtl_get_eee_supp(tp);
+       if (ret < 0)
+               goto out;
+       data->supported = mmd_eee_cap_to_ethtool_sup_t(ret);
+
+       /* Get advertisement EEE */
+       ret = rtl_get_eee_adv(tp);
+       if (ret < 0)
+               goto out;
+       data->advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
+       data->eee_enabled = !!data->advertised;
+
+       /* Get LP advertisement EEE */
+       ret = rtl_get_eee_lpadv(tp);
+       if (ret < 0)
+               goto out;
+       data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
+       data->eee_active = !!(data->advertised & data->lp_advertised);
+out:
+       pm_runtime_put_noidle(d);
+       return ret < 0 ? ret : 0;
+}
+
+static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
+{
+       struct rtl8169_private *tp = netdev_priv(dev);
+       struct device *d = tp_to_dev(tp);
+       int old_adv, adv = 0, cap, ret;
+
+       pm_runtime_get_noresume(d);
+
+       if (!dev->phydev || !pm_runtime_active(d)) {
+               ret = -EOPNOTSUPP;
+               goto out;
+       }
+
+       if (dev->phydev->autoneg == AUTONEG_DISABLE ||
+           dev->phydev->duplex != DUPLEX_FULL) {
+               ret = -EPROTONOSUPPORT;
+               goto out;
+       }
+
+       /* Get Supported EEE */
+       ret = rtl_get_eee_supp(tp);
+       if (ret < 0)
+               goto out;
+       cap = ret;
+
+       ret = rtl_get_eee_adv(tp);
+       if (ret < 0)
+               goto out;
+       old_adv = ret;
+
+       if (data->eee_enabled) {
+               adv = !data->advertised ? cap :
+                     ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
+               /* Mask prohibited EEE modes */
+               adv &= ~dev->phydev->eee_broken_modes;
+       }
+
+       if (old_adv != adv) {
+               ret = rtl_set_eee_adv(tp, adv);
+               if (ret < 0)
+                       goto out;
+
+               /* Restart autonegotiation so the new modes get sent to the
+                * link partner.
+                */
+               ret = phy_restart_aneg(dev->phydev);
+       }
+
+out:
+       pm_runtime_put_noidle(d);
+       return ret < 0 ? ret : 0;
+}
+
 static const struct ethtool_ops rtl8169_ethtool_ops = {
        .get_drvinfo            = rtl8169_get_drvinfo,
        .get_regs_len           = rtl8169_get_regs_len,
@@ -2000,6 +2166,8 @@ static const struct ethtool_ops rtl8169_ethtool_ops = {
        .get_ethtool_stats      = rtl8169_get_ethtool_stats,
        .get_ts_info            = ethtool_op_get_ts_info,
        .nway_reset             = phy_ethtool_nway_reset,
+       .get_eee                = rtl8169_get_eee,
+       .set_eee                = rtl8169_set_eee,
        .get_link_ksettings     = phy_ethtool_get_link_ksettings,
        .set_link_ksettings     = phy_ethtool_set_link_ksettings,
 };
@@ -2393,6 +2561,18 @@ static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
                rtl_apply_firmware(tp);
 }
 
+static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
+{
+       rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
+}
+
+static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
+{
+       phy_write(tp->phydev, 0x1f, 0x0a43);
+       phy_set_bits(tp->phydev, 0x11, BIT(4));
+       phy_write(tp->phydev, 0x1f, 0x0000);
+}
+
 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
 {
        static const struct phy_reg phy_reg_init[] = {
@@ -3442,13 +3622,13 @@ static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
        rtl_writephy(tp, 0x14, 0x1065);
 
        rtl8168g_disable_aldps(tp);
-
-       rtl_writephy(tp, 0x1f, 0x0000);
+       rtl8168g_config_eee_phy(tp);
 }
 
 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
 {
        rtl_apply_firmware(tp);
+       rtl8168g_config_eee_phy(tp);
 }
 
 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
@@ -3554,8 +3734,7 @@ static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
        rtl_writephy(tp, 0x1f, 0x0000);
 
        rtl8168g_disable_aldps(tp);
-
-       rtl_writephy(tp, 0x1f, 0x0000);
+       rtl8168g_config_eee_phy(tp);
 }
 
 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
@@ -3624,8 +3803,7 @@ static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
        rtl_writephy(tp, 0x1f, 0x0000);
 
        rtl8168g_disable_aldps(tp);
-
-       rtl_writephy(tp, 0x1f, 0x0000);
+       rtl8168g_config_eee_phy(tp);
 }
 
 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
@@ -3654,8 +3832,7 @@ static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
        rtl_writephy(tp, 0x1f, 0x0000);
 
        rtl8168g_disable_aldps(tp);
-
-       rtl_writephy(tp, 0x1f, 0x0000);
+       rtl8168g_config_eee_phy(tp);
 }
 
 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
@@ -3733,8 +3910,7 @@ static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
        rtl_writephy(tp, 0x1f, 0x0000);
 
        rtl8168g_disable_aldps(tp);
-
-       rtl_writephy(tp, 0x1f, 0x0000);
+       rtl8168g_config_eee_phy(tp);
 }
 
 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
@@ -5017,6 +5193,8 @@ static void rtl_hw_start_8168g(struct rtl8169_private *tp)
        /* Adjust EEE LED frequency */
        RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
 
+       rtl8168_config_eee_mac(tp);
+
        rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
        rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
 
@@ -5119,6 +5297,8 @@ static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
        /* Adjust EEE LED frequency */
        RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
 
+       rtl8168_config_eee_mac(tp);
+
        RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
        RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
 
@@ -5199,6 +5379,8 @@ static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
        /* Adjust EEE LED frequency */
        RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
 
+       rtl8168_config_eee_mac(tp);
+
        rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
 
        RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);