netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Heiner Kallweit <hkallweit1@gmail.com>
To: David Miller <davem@davemloft.net>,
	Realtek linux nic maintainers <nic_swsd@realtek.com>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>
Subject: [PATCH net] r8169: switch off ASPM by default and add sysfs attribute to control ASPM
Date: Tue, 2 Apr 2019 21:55:25 +0200	[thread overview]
Message-ID: <275eb60f-8cfe-9ff6-97bc-39d9ef280d36@gmail.com> (raw)

There are numerous reports about different problems caused by ASPM
incompatibilities between certain network chip versions and board
chipsets. On the other hand on (especially mobile) systems where ASPM
works properly it can significantly contribute to power-saving and
increased battery runtime.
One problem so far to make ASPM configurable was to find an acceptable
way of configuration (e.g. module parameters are discouraged for that
purpose).

As a new attempt let's switch off ASPM per default and make it
configurable by a sysfs attribute. The attribute is documented in
new file Documentation/networking/device_drivers/realtek/r8169.txt.

Fixes: a99790bf5c7f ("r8169: Reinstate ASPM Support")
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 .../device_drivers/realtek/r8169.txt          | 19 +++++
 drivers/net/ethernet/realtek/r8169.c          | 75 +++++++++++++++++--
 2 files changed, 86 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/networking/device_drivers/realtek/r8169.txt

diff --git a/Documentation/networking/device_drivers/realtek/r8169.txt b/Documentation/networking/device_drivers/realtek/r8169.txt
new file mode 100644
index 000000000..669995d0c
--- /dev/null
+++ b/Documentation/networking/device_drivers/realtek/r8169.txt
@@ -0,0 +1,19 @@
+Written by Heiner Kallweit <hkallweit1@gmail.com>
+
+Version 04/02/2019
+
+Driver-specific sysfs attributes
+================================
+
+rtl_aspm (bool)
+---------------
+
+Certain combinations of network chip versions and board chipsets result in
+increased packet latency, PCIe errors, or significantly reduced network
+performance. Therefore ASPM is off by default. On the other hand ASPM can
+significantly contribute to power-saving and thus increased battery
+runtime on notebooks. Therefore this sysfs attribute allows to switch on
+ASPM on systems where ASPM works properly. The attribute accepts any form
+of bool value, e.g. 1/y/on. See also kerneldoc of kstrtobool().
+Note that the attribute is accessible only if interface is up.
+Else network chip and PCIe link may be powered-down and not reachable.
diff --git a/drivers/net/ethernet/realtek/r8169.c b/drivers/net/ethernet/realtek/r8169.c
index 19efa88f3..e2de94dc3 100644
--- a/drivers/net/ethernet/realtek/r8169.c
+++ b/drivers/net/ethernet/realtek/r8169.c
@@ -678,6 +678,8 @@ struct rtl8169_private {
 		struct work_struct work;
 	} wk;
 
+	unsigned aspm_supported:1;
+	unsigned aspm_enabled:1;
 	unsigned irq_enabled:1;
 	unsigned supports_gmii:1;
 	dma_addr_t counters_phys_addr;
@@ -5091,7 +5093,7 @@ static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
 	RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
 	RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
 
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
@@ -5205,7 +5207,7 @@ static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
 	/* disable aspm and clock request before access ephy */
 	rtl_hw_aspm_clkreq_enable(tp, false);
 	rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
@@ -5240,7 +5242,7 @@ static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
 	/* disable aspm and clock request before access ephy */
 	rtl_hw_aspm_clkreq_enable(tp, false);
 	rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
@@ -5337,7 +5339,7 @@ static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
 	r8168_mac_ocp_write(tp, 0xc094, 0x0000);
 	r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
 
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
@@ -5394,7 +5396,7 @@ static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
 
 	rtl_hw_start_8168ep(tp);
 
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
@@ -5414,7 +5416,7 @@ static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
 	RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
 
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
@@ -5449,7 +5451,7 @@ static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
 	data |= 0x0080;
 	r8168_mac_ocp_write(tp, 0xe860, data);
 
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8168(struct rtl8169_private *tp)
@@ -5696,7 +5698,7 @@ static void rtl_hw_start_8106(struct rtl8169_private *tp)
 	RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
 
 	rtl_pcie_state_l2l3_disable(tp);
-	rtl_hw_aspm_clkreq_enable(tp, true);
+	tp->aspm_supported = 1;
 }
 
 static void rtl_hw_start_8101(struct rtl8169_private *tp)
@@ -7097,6 +7099,49 @@ static const struct rtl_cfg_info {
 	}
 };
 
+static ssize_t rtl_aspm_show(struct device *dev, struct device_attribute *attr,
+			     char *buf)
+{
+	struct net_device *ndev = dev_get_drvdata(dev);
+	struct rtl8169_private *tp = netdev_priv(ndev);
+
+	return snprintf(buf, PAGE_SIZE, "%d\n", tp->aspm_enabled);
+}
+
+static ssize_t rtl_aspm_store(struct device *dev, struct device_attribute *attr,
+			      const char *buf, size_t size)
+{
+	struct net_device *ndev = dev_get_drvdata(dev);
+	struct rtl8169_private *tp = netdev_priv(ndev);
+	bool aspm;
+
+	if (!tp->aspm_supported)
+		return -EOPNOTSUPP;
+
+	if (strtobool(buf, &aspm) < 0)
+		return -EINVAL;
+
+	pm_runtime_get_noresume(dev);
+
+	if (!pm_runtime_active(dev)) {
+		size = -ENETDOWN;
+		goto out;
+	}
+
+	rtl_lock_work(tp);
+	rtl_unlock_config_regs(tp);
+	rtl_hw_aspm_clkreq_enable(tp, aspm);
+	tp->aspm_enabled = aspm ? 1 : 0;
+	rtl_lock_config_regs(tp);
+	rtl_unlock_work(tp);
+out:
+	pm_runtime_put_noidle(dev);
+
+	return size;
+}
+
+static DEVICE_ATTR_RW(rtl_aspm);
+
 static int rtl_alloc_irq(struct rtl8169_private *tp)
 {
 	unsigned int flags;
@@ -7325,6 +7370,11 @@ static int rtl_get_ether_clk(struct rtl8169_private *tp)
 	return rc;
 }
 
+static void rtl_remove_sysfs_aspm_file(void *data)
+{
+	device_remove_file(data, &dev_attr_rtl_aspm);
+}
+
 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
 	const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
@@ -7498,6 +7548,15 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	pci_set_drvdata(pdev, dev);
 
+	rc = device_create_file(&pdev->dev, &dev_attr_rtl_aspm);
+	if (rc)
+		return rc;
+
+	rc = devm_add_action_or_reset(&pdev->dev, rtl_remove_sysfs_aspm_file,
+				      &pdev->dev);
+	if (rc)
+		return rc;
+
 	rc = r8169_mdio_register(tp);
 	if (rc)
 		return rc;
-- 
2.21.0


             reply	other threads:[~2019-04-02 19:55 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-02 19:55 Heiner Kallweit [this message]
2019-04-02 20:16 ` [PATCH net] r8169: switch off ASPM by default and add sysfs attribute to control ASPM Florian Fainelli
2019-04-02 20:41   ` Heiner Kallweit
2019-04-02 21:08     ` Rajat Jain
2019-04-02 21:25       ` Heiner Kallweit
2019-04-02 21:57     ` Bjorn Helgaas
2019-04-03  5:53       ` Heiner Kallweit
2019-04-03 13:14         ` Bjorn Helgaas
2019-04-03 17:45           ` Heiner Kallweit
2019-04-03 19:18             ` Frederick Lawler
2019-04-03 20:10               ` Heiner Kallweit
2019-04-05 19:10             ` Bjorn Helgaas
2019-04-05 19:28               ` Heiner Kallweit
2019-04-09 17:32                 ` Heiner Kallweit
2019-04-09 18:20                   ` Bjorn Helgaas
2019-04-09 18:27                     ` Heiner Kallweit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=275eb60f-8cfe-9ff6-97bc-39d9ef280d36@gmail.com \
    --to=hkallweit1@gmail.com \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=nic_swsd@realtek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).