netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
@ 2021-08-12 15:53 Kai-Heng Feng
  2021-08-12 15:53 ` [PATCH v2 2/2] r8169: Enable ASPM for selected NICs Kai-Heng Feng
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Kai-Heng Feng @ 2021-08-12 15:53 UTC (permalink / raw)
  To: hkallweit1, nic_swsd
  Cc: Kai-Heng Feng, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
Same issue can be observed with older vendor drivers.

The issue is however solved by the latest vendor driver. There's a new
mechanism, which disables r8169's internal ASPM when the NIC traffic has
more than 10 packets, and vice versa.

Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
use dynamic ASPM under Windows. So implement the same mechanism here to
resolve the issue.

Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
---
v2: 
 - Use delayed_work instead of timer_list to avoid interrupt context
 - Use mutex to serialize packet counter read/write
 - Wording change

 drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index c7af5bc3b8af..7ab2e841dc69 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -624,6 +624,11 @@ struct rtl8169_private {
 
 	unsigned supports_gmii:1;
 	unsigned aspm_manageable:1;
+	unsigned aspm_enabled:1;
+	struct delayed_work aspm_toggle;
+	struct mutex aspm_mutex;
+	u32 aspm_packet_count;
+
 	dma_addr_t counters_phys_addr;
 	struct rtl8169_counters *counters;
 	struct rtl8169_tc_offsets tc_offset;
@@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
 		RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
 	}
 
+	tp->aspm_enabled = enable;
+
 	udelay(10);
 }
 
@@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
 
 	dirty_tx = tp->dirty_tx;
 
+	mutex_lock(&tp->aspm_mutex);
+	tp->aspm_packet_count += tp->cur_tx - dirty_tx;
+	mutex_unlock(&tp->aspm_mutex);
 	while (READ_ONCE(tp->cur_tx) != dirty_tx) {
 		unsigned int entry = dirty_tx % NUM_TX_DESC;
 		u32 status;
@@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
 		rtl8169_mark_to_asic(desc);
 	}
 
+	mutex_lock(&tp->aspm_mutex);
+	tp->aspm_packet_count += count;
+	mutex_unlock(&tp->aspm_mutex);
+
 	return count;
 }
 
@@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
 	return 0;
 }
 
+#define ASPM_PACKET_THRESHOLD 10
+#define ASPM_TOGGLE_INTERVAL 1000
+
+static void rtl8169_aspm_toggle(struct work_struct *work)
+{
+	struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
+						  aspm_toggle.work);
+	bool enable;
+
+	mutex_lock(&tp->aspm_mutex);
+	enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
+	tp->aspm_packet_count = 0;
+	mutex_unlock(&tp->aspm_mutex);
+
+	if (tp->aspm_enabled != enable) {
+		rtl_unlock_config_regs(tp);
+		rtl_hw_aspm_clkreq_enable(tp, enable);
+		rtl_lock_config_regs(tp);
+	}
+
+	schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
+}
+
 static void rtl8169_down(struct rtl8169_private *tp)
 {
+	cancel_delayed_work_sync(&tp->aspm_toggle);
+
 	/* Clear all task flags */
 	bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
 
@@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
 	rtl_reset_work(tp);
 
 	phy_start(tp->phydev);
+
+	schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
 }
 
 static int rtl8169_close(struct net_device *dev)
@@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	INIT_WORK(&tp->wk.work, rtl_task);
 
+	INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
+
+	mutex_init(&tp->aspm_mutex);
+
 	rtl_init_mac_address(tp);
 
 	dev->ethtool_ops = &rtl8169_ethtool_ops;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 2/2] r8169: Enable ASPM for selected NICs
  2021-08-12 15:53 [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Kai-Heng Feng
@ 2021-08-12 15:53 ` Kai-Heng Feng
  2021-08-12 19:38   ` Heiner Kallweit
  2021-08-12 19:34 ` [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Heiner Kallweit
  2021-08-13  6:48 ` Heiner Kallweit
  2 siblings, 1 reply; 12+ messages in thread
From: Kai-Heng Feng @ 2021-08-12 15:53 UTC (permalink / raw)
  To: hkallweit1, nic_swsd
  Cc: Kai-Heng Feng, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

The latest vendor driver enables ASPM for more recent r8168 NICs, do the
same here to match the behavior.

In addition, pci_disable_link_state() is only used for RTL8168D/8111D in
vendor driver, also match that.

Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
---
v2:
 - No change

 drivers/net/ethernet/realtek/r8169_main.c | 34 +++++++++++++++++------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index 7ab2e841dc69..caa29e72a21a 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -623,7 +623,7 @@ struct rtl8169_private {
 	} wk;
 
 	unsigned supports_gmii:1;
-	unsigned aspm_manageable:1;
+	unsigned aspm_supported:1;
 	unsigned aspm_enabled:1;
 	struct delayed_work aspm_toggle;
 	struct mutex aspm_mutex;
@@ -2667,8 +2667,11 @@ static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
 
 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
 {
+	if (!tp->aspm_supported)
+		return;
+
 	/* Don't enable ASPM in the chip if OS can't control ASPM */
-	if (enable && tp->aspm_manageable) {
+	if (enable) {
 		RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
 		RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
 	} else {
@@ -5284,6 +5287,21 @@ static void rtl_init_mac_address(struct rtl8169_private *tp)
 	rtl_rar_set(tp, mac_addr);
 }
 
+static int rtl_hw_aspm_supported(struct rtl8169_private *tp)
+{
+	switch (tp->mac_version) {
+	case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_36:
+	case RTL_GIGA_MAC_VER_38:
+	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_42:
+	case RTL_GIGA_MAC_VER_44 ... RTL_GIGA_MAC_VER_46:
+	case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_63:
+		return 1;
+
+	default:
+		return 0;
+	}
+}
+
 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
 	struct rtl8169_private *tp;
@@ -5315,12 +5333,12 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (rc)
 		return rc;
 
-	/* Disable ASPM completely as that cause random device stop working
-	 * problems as well as full system hangs for some PCIe devices users.
-	 */
-	rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
-					  PCIE_LINK_STATE_L1);
-	tp->aspm_manageable = !rc;
+	if (tp->mac_version == RTL_GIGA_MAC_VER_25)
+		pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
+				       PCIE_LINK_STATE_L1 |
+				       PCIE_LINK_STATE_CLKPM);
+
+	tp->aspm_supported = rtl_hw_aspm_supported(tp);
 
 	/* enable device (incl. PCI PM wakeup and hotplug setup) */
 	rc = pcim_enable_device(pdev);
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-12 15:53 [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Kai-Heng Feng
  2021-08-12 15:53 ` [PATCH v2 2/2] r8169: Enable ASPM for selected NICs Kai-Heng Feng
@ 2021-08-12 19:34 ` Heiner Kallweit
  2021-08-13  9:46   ` Kai-Heng Feng
  2021-08-13  6:48 ` Heiner Kallweit
  2 siblings, 1 reply; 12+ messages in thread
From: Heiner Kallweit @ 2021-08-12 19:34 UTC (permalink / raw)
  To: Kai-Heng Feng, nic_swsd
  Cc: David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On 12.08.2021 17:53, Kai-Heng Feng wrote:
> r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
> Same issue can be observed with older vendor drivers.
> 
> The issue is however solved by the latest vendor driver. There's a new
> mechanism, which disables r8169's internal ASPM when the NIC traffic has
> more than 10 packets, and vice versa.
> 
> Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
> use dynamic ASPM under Windows. So implement the same mechanism here to
> resolve the issue.
> 
Realtek using something in their Windows drivers isn't really a proof of
quality. Still my concerns haven't been addressed. If ASPM is enabled and
there's a congestion in the chip it may take up to a second until ASPM
gets disabled. In this second traffic very likely is heavily affected.
Who takes care in case of problem reports?

This is a massive change for basically all chip versions. And experience
shows that in case of problem reports Realtek never cares, even though
they are listed as maintainers. All I see is that they copy more and more
code from r8169 into their own drivers. This seems to indicate that they
consider quality of their own drivers as not sufficient.

Still my proposal: Apply this downstream, and if there are no complaints
after a few months it may be considered for mainline.

Last but not least the formal issues:
- no cover letter
- no net/net-next annotation

> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> ---
> v2: 
>  - Use delayed_work instead of timer_list to avoid interrupt context
>  - Use mutex to serialize packet counter read/write
>  - Wording change
> 
>  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
> 
> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> index c7af5bc3b8af..7ab2e841dc69 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -624,6 +624,11 @@ struct rtl8169_private {
>  
>  	unsigned supports_gmii:1;
>  	unsigned aspm_manageable:1;
> +	unsigned aspm_enabled:1;
> +	struct delayed_work aspm_toggle;
> +	struct mutex aspm_mutex;
> +	u32 aspm_packet_count;
> +
>  	dma_addr_t counters_phys_addr;
>  	struct rtl8169_counters *counters;
>  	struct rtl8169_tc_offsets tc_offset;
> @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
>  		RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
>  	}
>  
> +	tp->aspm_enabled = enable;
> +
>  	udelay(10);
>  }
>  
> @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
>  
>  	dirty_tx = tp->dirty_tx;
>  
> +	mutex_lock(&tp->aspm_mutex);

We are in soft irq context here, therefore you shouldn't sleep.

> +	tp->aspm_packet_count += tp->cur_tx - dirty_tx;
> +	mutex_unlock(&tp->aspm_mutex);
>  	while (READ_ONCE(tp->cur_tx) != dirty_tx) {
>  		unsigned int entry = dirty_tx % NUM_TX_DESC;
>  		u32 status;
> @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
>  		rtl8169_mark_to_asic(desc);
>  	}
>  
> +	mutex_lock(&tp->aspm_mutex);
> +	tp->aspm_packet_count += count;
> +	mutex_unlock(&tp->aspm_mutex);
> +
>  	return count;
>  }
>  
> @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
>  	return 0;
>  }
>  
> +#define ASPM_PACKET_THRESHOLD 10
> +#define ASPM_TOGGLE_INTERVAL 1000
> +
> +static void rtl8169_aspm_toggle(struct work_struct *work)
> +{
> +	struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
> +						  aspm_toggle.work);
> +	bool enable;
> +
> +	mutex_lock(&tp->aspm_mutex);
> +	enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
> +	tp->aspm_packet_count = 0;
> +	mutex_unlock(&tp->aspm_mutex);
> +
> +	if (tp->aspm_enabled != enable) {
> +		rtl_unlock_config_regs(tp);
> +		rtl_hw_aspm_clkreq_enable(tp, enable);
> +		rtl_lock_config_regs(tp);
> +	}
> +
> +	schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> +}
> +
>  static void rtl8169_down(struct rtl8169_private *tp)
>  {
> +	cancel_delayed_work_sync(&tp->aspm_toggle);
> +
>  	/* Clear all task flags */
>  	bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
>  
> @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
>  	rtl_reset_work(tp);
>  
>  	phy_start(tp->phydev);
> +
> +	schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);

In the first version you used msecs_to_jiffies(ASPM_TIMER_INTERVAL).
Now you use 1000 jiffies what is a major difference.

>  }
>  
>  static int rtl8169_close(struct net_device *dev)
> @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>  
>  	INIT_WORK(&tp->wk.work, rtl_task);
>  
> +	INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
> +
> +	mutex_init(&tp->aspm_mutex);
> +
>  	rtl_init_mac_address(tp);
>  
>  	dev->ethtool_ops = &rtl8169_ethtool_ops;
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] r8169: Enable ASPM for selected NICs
  2021-08-12 15:53 ` [PATCH v2 2/2] r8169: Enable ASPM for selected NICs Kai-Heng Feng
@ 2021-08-12 19:38   ` Heiner Kallweit
  2021-08-13 10:11     ` Kai-Heng Feng
  0 siblings, 1 reply; 12+ messages in thread
From: Heiner Kallweit @ 2021-08-12 19:38 UTC (permalink / raw)
  To: Kai-Heng Feng, nic_swsd
  Cc: David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On 12.08.2021 17:53, Kai-Heng Feng wrote:
> The latest vendor driver enables ASPM for more recent r8168 NICs, do the
> same here to match the behavior.
> 
> In addition, pci_disable_link_state() is only used for RTL8168D/8111D in
> vendor driver, also match that.
> 
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> ---
> v2:
>  - No change
> 
>  drivers/net/ethernet/realtek/r8169_main.c | 34 +++++++++++++++++------
>  1 file changed, 26 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> index 7ab2e841dc69..caa29e72a21a 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -623,7 +623,7 @@ struct rtl8169_private {
>  	} wk;
>  
>  	unsigned supports_gmii:1;
> -	unsigned aspm_manageable:1;
> +	unsigned aspm_supported:1;
>  	unsigned aspm_enabled:1;
>  	struct delayed_work aspm_toggle;
>  	struct mutex aspm_mutex;
> @@ -2667,8 +2667,11 @@ static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
>  
>  static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
>  {
> +	if (!tp->aspm_supported)
> +		return;
> +
>  	/* Don't enable ASPM in the chip if OS can't control ASPM */
> -	if (enable && tp->aspm_manageable) {
> +	if (enable) {
>  		RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
>  		RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
>  	} else {
> @@ -5284,6 +5287,21 @@ static void rtl_init_mac_address(struct rtl8169_private *tp)
>  	rtl_rar_set(tp, mac_addr);
>  }
>  
> +static int rtl_hw_aspm_supported(struct rtl8169_private *tp)
> +{
> +	switch (tp->mac_version) {
> +	case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_36:
> +	case RTL_GIGA_MAC_VER_38:
> +	case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_42:
> +	case RTL_GIGA_MAC_VER_44 ... RTL_GIGA_MAC_VER_46:
> +	case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_63:

This shouldn't be needed because ASPM support is announced the
standard PCI way. Max a blacklist should be needed if there are
chip versions that announce ASPM support whilst in reality they
do not support it (or support is completely broken).

> +		return 1;
> +
> +	default:
> +		return 0;
> +	}
> +}
> +
>  static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>  {
>  	struct rtl8169_private *tp;
> @@ -5315,12 +5333,12 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>  	if (rc)
>  		return rc;
>  
> -	/* Disable ASPM completely as that cause random device stop working
> -	 * problems as well as full system hangs for some PCIe devices users.
> -	 */
> -	rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
> -					  PCIE_LINK_STATE_L1);
> -	tp->aspm_manageable = !rc;
> +	if (tp->mac_version == RTL_GIGA_MAC_VER_25)
> +		pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
> +				       PCIE_LINK_STATE_L1 |
> +				       PCIE_LINK_STATE_CLKPM);
> +
> +	tp->aspm_supported = rtl_hw_aspm_supported(tp);
>  
>  	/* enable device (incl. PCI PM wakeup and hotplug setup) */
>  	rc = pcim_enable_device(pdev);
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-12 15:53 [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Kai-Heng Feng
  2021-08-12 15:53 ` [PATCH v2 2/2] r8169: Enable ASPM for selected NICs Kai-Heng Feng
  2021-08-12 19:34 ` [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Heiner Kallweit
@ 2021-08-13  6:48 ` Heiner Kallweit
  2021-08-13  9:54   ` Kai-Heng Feng
  2 siblings, 1 reply; 12+ messages in thread
From: Heiner Kallweit @ 2021-08-13  6:48 UTC (permalink / raw)
  To: Kai-Heng Feng, nic_swsd
  Cc: David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On 12.08.2021 17:53, Kai-Heng Feng wrote:
> r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
> Same issue can be observed with older vendor drivers.
> 
> The issue is however solved by the latest vendor driver. There's a new
> mechanism, which disables r8169's internal ASPM when the NIC traffic has
> more than 10 packets, and vice versa.
> 
> Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125

As we have Realtek in this mail thread:
Typically hw issues affect 1-3 chip versions only. The ASPM problems seem
to have been existing for at least 15 years now, in every chip version.
It seems that even the new RTL8125 chip generation still has broken ASPM.
Why was this never fixed? ASPM not considered to be relevant? HW design
too broken?

> use dynamic ASPM under Windows. So implement the same mechanism here to
> resolve the issue.
> 
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> ---
> v2: 
>  - Use delayed_work instead of timer_list to avoid interrupt context
>  - Use mutex to serialize packet counter read/write
>  - Wording change
> 
>  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
> 
> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> index c7af5bc3b8af..7ab2e841dc69 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -624,6 +624,11 @@ struct rtl8169_private {
>  
>  	unsigned supports_gmii:1;
>  	unsigned aspm_manageable:1;
> +	unsigned aspm_enabled:1;
> +	struct delayed_work aspm_toggle;
> +	struct mutex aspm_mutex;
> +	u32 aspm_packet_count;
> +
>  	dma_addr_t counters_phys_addr;
>  	struct rtl8169_counters *counters;
>  	struct rtl8169_tc_offsets tc_offset;
> @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
>  		RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
>  	}
>  
> +	tp->aspm_enabled = enable;
> +
>  	udelay(10);
>  }
>  
> @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
>  
>  	dirty_tx = tp->dirty_tx;
>  
> +	mutex_lock(&tp->aspm_mutex);
> +	tp->aspm_packet_count += tp->cur_tx - dirty_tx;
> +	mutex_unlock(&tp->aspm_mutex);
>  	while (READ_ONCE(tp->cur_tx) != dirty_tx) {
>  		unsigned int entry = dirty_tx % NUM_TX_DESC;
>  		u32 status;
> @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
>  		rtl8169_mark_to_asic(desc);
>  	}
>  
> +	mutex_lock(&tp->aspm_mutex);
> +	tp->aspm_packet_count += count;
> +	mutex_unlock(&tp->aspm_mutex);
> +
>  	return count;
>  }
>  
> @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
>  	return 0;
>  }
>  
> +#define ASPM_PACKET_THRESHOLD 10
> +#define ASPM_TOGGLE_INTERVAL 1000
> +
> +static void rtl8169_aspm_toggle(struct work_struct *work)
> +{
> +	struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
> +						  aspm_toggle.work);
> +	bool enable;
> +
> +	mutex_lock(&tp->aspm_mutex);
> +	enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
> +	tp->aspm_packet_count = 0;
> +	mutex_unlock(&tp->aspm_mutex);
> +
> +	if (tp->aspm_enabled != enable) {
> +		rtl_unlock_config_regs(tp);
> +		rtl_hw_aspm_clkreq_enable(tp, enable);
> +		rtl_lock_config_regs(tp);
> +	}
> +
> +	schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> +}
> +
>  static void rtl8169_down(struct rtl8169_private *tp)
>  {
> +	cancel_delayed_work_sync(&tp->aspm_toggle);
> +
>  	/* Clear all task flags */
>  	bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
>  
> @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
>  	rtl_reset_work(tp);
>  
>  	phy_start(tp->phydev);
> +
> +	schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
>  }
>  
>  static int rtl8169_close(struct net_device *dev)
> @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>  
>  	INIT_WORK(&tp->wk.work, rtl_task);
>  
> +	INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
> +
> +	mutex_init(&tp->aspm_mutex);
> +
>  	rtl_init_mac_address(tp);
>  
>  	dev->ethtool_ops = &rtl8169_ethtool_ops;
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-12 19:34 ` [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Heiner Kallweit
@ 2021-08-13  9:46   ` Kai-Heng Feng
  2021-08-14 11:33     ` Heiner Kallweit
  0 siblings, 1 reply; 12+ messages in thread
From: Kai-Heng Feng @ 2021-08-13  9:46 UTC (permalink / raw)
  To: Heiner Kallweit
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

j

On Fri, Aug 13, 2021 at 3:39 AM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>
> On 12.08.2021 17:53, Kai-Heng Feng wrote:
> > r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
> > Same issue can be observed with older vendor drivers.
> >
> > The issue is however solved by the latest vendor driver. There's a new
> > mechanism, which disables r8169's internal ASPM when the NIC traffic has
> > more than 10 packets, and vice versa.
> >
> > Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
> > use dynamic ASPM under Windows. So implement the same mechanism here to
> > resolve the issue.
> >
> Realtek using something in their Windows drivers isn't really a proof of
> quality.

I agree. So it'll be great if Realtek can work with us here.

> Still my concerns haven't been addressed. If ASPM is enabled and
> there's a congestion in the chip it may take up to a second until ASPM
> gets disabled. In this second traffic very likely is heavily affected.
> Who takes care in case of problem reports?

I think we'll know that once the patch is merged in downstream kernel.

>
> This is a massive change for basically all chip versions. And experience
> shows that in case of problem reports Realtek never cares, even though
> they are listed as maintainers. All I see is that they copy more and more
> code from r8169 into their own drivers. This seems to indicate that they
> consider quality of their own drivers as not sufficient.

I wonder why they don't want to put their efforts to r8169...
Obviously they are doing a great job for rtw88 and r8152.

>
> Still my proposal: Apply this downstream, and if there are no complaints
> after a few months it may be considered for mainline.

Yes that's my plan. But I'd still like it to be reviewed before
putting it to the downstream kernel.

>
> Last but not least the formal issues:
> - no cover letter

Will write it up once it's tested dowstream.

> - no net/net-next annotation

Does it mean put "net/net-next" in the subject line?


>
> > Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> > ---
> > v2:
> >  - Use delayed_work instead of timer_list to avoid interrupt context
> >  - Use mutex to serialize packet counter read/write
> >  - Wording change
> >
> >  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
> >  1 file changed, 45 insertions(+)
> >
> > diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> > index c7af5bc3b8af..7ab2e841dc69 100644
> > --- a/drivers/net/ethernet/realtek/r8169_main.c
> > +++ b/drivers/net/ethernet/realtek/r8169_main.c
> > @@ -624,6 +624,11 @@ struct rtl8169_private {
> >
> >       unsigned supports_gmii:1;
> >       unsigned aspm_manageable:1;
> > +     unsigned aspm_enabled:1;
> > +     struct delayed_work aspm_toggle;
> > +     struct mutex aspm_mutex;
> > +     u32 aspm_packet_count;
> > +
> >       dma_addr_t counters_phys_addr;
> >       struct rtl8169_counters *counters;
> >       struct rtl8169_tc_offsets tc_offset;
> > @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
> >               RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
> >       }
> >
> > +     tp->aspm_enabled = enable;
> > +
> >       udelay(10);
> >  }
> >
> > @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
> >
> >       dirty_tx = tp->dirty_tx;
> >
> > +     mutex_lock(&tp->aspm_mutex);
>
> We are in soft irq context here, therefore you shouldn't sleep.

I thought napi_poll is not using softirq, apparent I was wrong. Will
correct it too.

>
> > +     tp->aspm_packet_count += tp->cur_tx - dirty_tx;
> > +     mutex_unlock(&tp->aspm_mutex);
> >       while (READ_ONCE(tp->cur_tx) != dirty_tx) {
> >               unsigned int entry = dirty_tx % NUM_TX_DESC;
> >               u32 status;
> > @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
> >               rtl8169_mark_to_asic(desc);
> >       }
> >
> > +     mutex_lock(&tp->aspm_mutex);
> > +     tp->aspm_packet_count += count;
> > +     mutex_unlock(&tp->aspm_mutex);
> > +
> >       return count;
> >  }
> >
> > @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
> >       return 0;
> >  }
> >
> > +#define ASPM_PACKET_THRESHOLD 10
> > +#define ASPM_TOGGLE_INTERVAL 1000
> > +
> > +static void rtl8169_aspm_toggle(struct work_struct *work)
> > +{
> > +     struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
> > +                                               aspm_toggle.work);
> > +     bool enable;
> > +
> > +     mutex_lock(&tp->aspm_mutex);
> > +     enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
> > +     tp->aspm_packet_count = 0;
> > +     mutex_unlock(&tp->aspm_mutex);
> > +
> > +     if (tp->aspm_enabled != enable) {
> > +             rtl_unlock_config_regs(tp);
> > +             rtl_hw_aspm_clkreq_enable(tp, enable);
> > +             rtl_lock_config_regs(tp);
> > +     }
> > +
> > +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> > +}
> > +
> >  static void rtl8169_down(struct rtl8169_private *tp)
> >  {
> > +     cancel_delayed_work_sync(&tp->aspm_toggle);
> > +
> >       /* Clear all task flags */
> >       bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
> >
> > @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
> >       rtl_reset_work(tp);
> >
> >       phy_start(tp->phydev);
> > +
> > +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
>
> In the first version you used msecs_to_jiffies(ASPM_TIMER_INTERVAL).
> Now you use 1000 jiffies what is a major difference.

msecs_to_jiffies() was omitted. Will correct it.

Kai-Heng

>
> >  }
> >
> >  static int rtl8169_close(struct net_device *dev)
> > @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> >
> >       INIT_WORK(&tp->wk.work, rtl_task);
> >
> > +     INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
> > +
> > +     mutex_init(&tp->aspm_mutex);
> > +
> >       rtl_init_mac_address(tp);
> >
> >       dev->ethtool_ops = &rtl8169_ethtool_ops;
> >
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-13  6:48 ` Heiner Kallweit
@ 2021-08-13  9:54   ` Kai-Heng Feng
  2021-08-14 11:31     ` Heiner Kallweit
  0 siblings, 1 reply; 12+ messages in thread
From: Kai-Heng Feng @ 2021-08-13  9:54 UTC (permalink / raw)
  To: Heiner Kallweit
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On Fri, Aug 13, 2021 at 2:49 PM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>
> On 12.08.2021 17:53, Kai-Heng Feng wrote:
> > r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
> > Same issue can be observed with older vendor drivers.
> >
> > The issue is however solved by the latest vendor driver. There's a new
> > mechanism, which disables r8169's internal ASPM when the NIC traffic has
> > more than 10 packets, and vice versa.
> >
> > Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
>
> As we have Realtek in this mail thread:

Is it still in active use? I always think it's just a dummy address...

> Typically hw issues affect 1-3 chip versions only. The ASPM problems seem
> to have been existing for at least 15 years now, in every chip version.
> It seems that even the new RTL8125 chip generation still has broken ASPM.

Is there a bug report for that?

> Why was this never fixed? ASPM not considered to be relevant? HW design
> too broken?

IIUC, ASPM is extremely relevant to pass EU/US power consumption
regulation. So I really don't know why the situation under Linux is so
dire.

Kai-Heng

>
> > use dynamic ASPM under Windows. So implement the same mechanism here to
> > resolve the issue.
> >
> > Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> > ---
> > v2:
> >  - Use delayed_work instead of timer_list to avoid interrupt context
> >  - Use mutex to serialize packet counter read/write
> >  - Wording change
> >
> >  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
> >  1 file changed, 45 insertions(+)
> >
> > diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> > index c7af5bc3b8af..7ab2e841dc69 100644
> > --- a/drivers/net/ethernet/realtek/r8169_main.c
> > +++ b/drivers/net/ethernet/realtek/r8169_main.c
> > @@ -624,6 +624,11 @@ struct rtl8169_private {
> >
> >       unsigned supports_gmii:1;
> >       unsigned aspm_manageable:1;
> > +     unsigned aspm_enabled:1;
> > +     struct delayed_work aspm_toggle;
> > +     struct mutex aspm_mutex;
> > +     u32 aspm_packet_count;
> > +
> >       dma_addr_t counters_phys_addr;
> >       struct rtl8169_counters *counters;
> >       struct rtl8169_tc_offsets tc_offset;
> > @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
> >               RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
> >       }
> >
> > +     tp->aspm_enabled = enable;
> > +
> >       udelay(10);
> >  }
> >
> > @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
> >
> >       dirty_tx = tp->dirty_tx;
> >
> > +     mutex_lock(&tp->aspm_mutex);
> > +     tp->aspm_packet_count += tp->cur_tx - dirty_tx;
> > +     mutex_unlock(&tp->aspm_mutex);
> >       while (READ_ONCE(tp->cur_tx) != dirty_tx) {
> >               unsigned int entry = dirty_tx % NUM_TX_DESC;
> >               u32 status;
> > @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
> >               rtl8169_mark_to_asic(desc);
> >       }
> >
> > +     mutex_lock(&tp->aspm_mutex);
> > +     tp->aspm_packet_count += count;
> > +     mutex_unlock(&tp->aspm_mutex);
> > +
> >       return count;
> >  }
> >
> > @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
> >       return 0;
> >  }
> >
> > +#define ASPM_PACKET_THRESHOLD 10
> > +#define ASPM_TOGGLE_INTERVAL 1000
> > +
> > +static void rtl8169_aspm_toggle(struct work_struct *work)
> > +{
> > +     struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
> > +                                               aspm_toggle.work);
> > +     bool enable;
> > +
> > +     mutex_lock(&tp->aspm_mutex);
> > +     enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
> > +     tp->aspm_packet_count = 0;
> > +     mutex_unlock(&tp->aspm_mutex);
> > +
> > +     if (tp->aspm_enabled != enable) {
> > +             rtl_unlock_config_regs(tp);
> > +             rtl_hw_aspm_clkreq_enable(tp, enable);
> > +             rtl_lock_config_regs(tp);
> > +     }
> > +
> > +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> > +}
> > +
> >  static void rtl8169_down(struct rtl8169_private *tp)
> >  {
> > +     cancel_delayed_work_sync(&tp->aspm_toggle);
> > +
> >       /* Clear all task flags */
> >       bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
> >
> > @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
> >       rtl_reset_work(tp);
> >
> >       phy_start(tp->phydev);
> > +
> > +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> >  }
> >
> >  static int rtl8169_close(struct net_device *dev)
> > @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> >
> >       INIT_WORK(&tp->wk.work, rtl_task);
> >
> > +     INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
> > +
> > +     mutex_init(&tp->aspm_mutex);
> > +
> >       rtl_init_mac_address(tp);
> >
> >       dev->ethtool_ops = &rtl8169_ethtool_ops;
> >
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] r8169: Enable ASPM for selected NICs
  2021-08-12 19:38   ` Heiner Kallweit
@ 2021-08-13 10:11     ` Kai-Heng Feng
  2021-08-14 11:23       ` Heiner Kallweit
  0 siblings, 1 reply; 12+ messages in thread
From: Kai-Heng Feng @ 2021-08-13 10:11 UTC (permalink / raw)
  To: Heiner Kallweit
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On Fri, Aug 13, 2021 at 3:39 AM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>
> On 12.08.2021 17:53, Kai-Heng Feng wrote:
> > The latest vendor driver enables ASPM for more recent r8168 NICs, do the
> > same here to match the behavior.
> >
> > In addition, pci_disable_link_state() is only used for RTL8168D/8111D in
> > vendor driver, also match that.
> >
> > Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> > ---
> > v2:
> >  - No change
> >
> >  drivers/net/ethernet/realtek/r8169_main.c | 34 +++++++++++++++++------
> >  1 file changed, 26 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> > index 7ab2e841dc69..caa29e72a21a 100644
> > --- a/drivers/net/ethernet/realtek/r8169_main.c
> > +++ b/drivers/net/ethernet/realtek/r8169_main.c
> > @@ -623,7 +623,7 @@ struct rtl8169_private {
> >       } wk;
> >
> >       unsigned supports_gmii:1;
> > -     unsigned aspm_manageable:1;
> > +     unsigned aspm_supported:1;
> >       unsigned aspm_enabled:1;
> >       struct delayed_work aspm_toggle;
> >       struct mutex aspm_mutex;
> > @@ -2667,8 +2667,11 @@ static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
> >
> >  static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
> >  {
> > +     if (!tp->aspm_supported)
> > +             return;
> > +
> >       /* Don't enable ASPM in the chip if OS can't control ASPM */
> > -     if (enable && tp->aspm_manageable) {
> > +     if (enable) {
> >               RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
> >               RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
> >       } else {
> > @@ -5284,6 +5287,21 @@ static void rtl_init_mac_address(struct rtl8169_private *tp)
> >       rtl_rar_set(tp, mac_addr);
> >  }
> >
> > +static int rtl_hw_aspm_supported(struct rtl8169_private *tp)
> > +{
> > +     switch (tp->mac_version) {
> > +     case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_36:
> > +     case RTL_GIGA_MAC_VER_38:
> > +     case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_42:
> > +     case RTL_GIGA_MAC_VER_44 ... RTL_GIGA_MAC_VER_46:
> > +     case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_63:
>
> This shouldn't be needed because ASPM support is announced the
> standard PCI way. Max a blacklist should be needed if there are
> chip versions that announce ASPM support whilst in reality they
> do not support it (or support is completely broken).

So can we also remove aspm_manageable since blacklist will be used?

Kai-Heng

>
> > +             return 1;
> > +
> > +     default:
> > +             return 0;
> > +     }
> > +}
> > +
> >  static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> >  {
> >       struct rtl8169_private *tp;
> > @@ -5315,12 +5333,12 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> >       if (rc)
> >               return rc;
> >
> > -     /* Disable ASPM completely as that cause random device stop working
> > -      * problems as well as full system hangs for some PCIe devices users.
> > -      */
> > -     rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
> > -                                       PCIE_LINK_STATE_L1);
> > -     tp->aspm_manageable = !rc;
> > +     if (tp->mac_version == RTL_GIGA_MAC_VER_25)
> > +             pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
> > +                                    PCIE_LINK_STATE_L1 |
> > +                                    PCIE_LINK_STATE_CLKPM);
> > +
> > +     tp->aspm_supported = rtl_hw_aspm_supported(tp);
> >
> >       /* enable device (incl. PCI PM wakeup and hotplug setup) */
> >       rc = pcim_enable_device(pdev);
> >
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] r8169: Enable ASPM for selected NICs
  2021-08-13 10:11     ` Kai-Heng Feng
@ 2021-08-14 11:23       ` Heiner Kallweit
  0 siblings, 0 replies; 12+ messages in thread
From: Heiner Kallweit @ 2021-08-14 11:23 UTC (permalink / raw)
  To: Kai-Heng Feng
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On 13.08.2021 12:11, Kai-Heng Feng wrote:
> On Fri, Aug 13, 2021 at 3:39 AM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>>
>> On 12.08.2021 17:53, Kai-Heng Feng wrote:
>>> The latest vendor driver enables ASPM for more recent r8168 NICs, do the
>>> same here to match the behavior.
>>>
>>> In addition, pci_disable_link_state() is only used for RTL8168D/8111D in
>>> vendor driver, also match that.
>>>
>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>> ---
>>> v2:
>>>  - No change
>>>
>>>  drivers/net/ethernet/realtek/r8169_main.c | 34 +++++++++++++++++------
>>>  1 file changed, 26 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
>>> index 7ab2e841dc69..caa29e72a21a 100644
>>> --- a/drivers/net/ethernet/realtek/r8169_main.c
>>> +++ b/drivers/net/ethernet/realtek/r8169_main.c
>>> @@ -623,7 +623,7 @@ struct rtl8169_private {
>>>       } wk;
>>>
>>>       unsigned supports_gmii:1;
>>> -     unsigned aspm_manageable:1;
>>> +     unsigned aspm_supported:1;
>>>       unsigned aspm_enabled:1;
>>>       struct delayed_work aspm_toggle;
>>>       struct mutex aspm_mutex;
>>> @@ -2667,8 +2667,11 @@ static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
>>>
>>>  static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
>>>  {
>>> +     if (!tp->aspm_supported)
>>> +             return;
>>> +
>>>       /* Don't enable ASPM in the chip if OS can't control ASPM */
>>> -     if (enable && tp->aspm_manageable) {
>>> +     if (enable) {
>>>               RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
>>>               RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
>>>       } else {
>>> @@ -5284,6 +5287,21 @@ static void rtl_init_mac_address(struct rtl8169_private *tp)
>>>       rtl_rar_set(tp, mac_addr);
>>>  }
>>>
>>> +static int rtl_hw_aspm_supported(struct rtl8169_private *tp)
>>> +{
>>> +     switch (tp->mac_version) {
>>> +     case RTL_GIGA_MAC_VER_32 ... RTL_GIGA_MAC_VER_36:
>>> +     case RTL_GIGA_MAC_VER_38:
>>> +     case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_42:
>>> +     case RTL_GIGA_MAC_VER_44 ... RTL_GIGA_MAC_VER_46:
>>> +     case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_63:
>>
>> This shouldn't be needed because ASPM support is announced the
>> standard PCI way. Max a blacklist should be needed if there are
>> chip versions that announce ASPM support whilst in reality they
>> do not support it (or support is completely broken).
> 
> So can we also remove aspm_manageable since blacklist will be used?
> 
That's independent. What I mean is replace the whitelist with auto-
detected ASPM support and blacklist just the ones that are where
ASPM is completely unusable.
Retrieving the info about ASPM support may need a smll PCI core
extension. We need something similar to pcie_aspm_enabled(),
just exposing link->aspm_support. link->aspm_enabled may change
at runtime (sysfs link attributes).

> Kai-Heng
> 
>>
>>> +             return 1;
>>> +
>>> +     default:
>>> +             return 0;
>>> +     }
>>> +}
>>> +
>>>  static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>  {
>>>       struct rtl8169_private *tp;
>>> @@ -5315,12 +5333,12 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>       if (rc)
>>>               return rc;
>>>
>>> -     /* Disable ASPM completely as that cause random device stop working
>>> -      * problems as well as full system hangs for some PCIe devices users.
>>> -      */
>>> -     rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
>>> -                                       PCIE_LINK_STATE_L1);
>>> -     tp->aspm_manageable = !rc;
>>> +     if (tp->mac_version == RTL_GIGA_MAC_VER_25)
>>> +             pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
>>> +                                    PCIE_LINK_STATE_L1 |
>>> +                                    PCIE_LINK_STATE_CLKPM);
>>> +
>>> +     tp->aspm_supported = rtl_hw_aspm_supported(tp);
>>>
>>>       /* enable device (incl. PCI PM wakeup and hotplug setup) */
>>>       rc = pcim_enable_device(pdev);
>>>
>>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-13  9:54   ` Kai-Heng Feng
@ 2021-08-14 11:31     ` Heiner Kallweit
  2021-08-19  3:11       ` Kai-Heng Feng
  0 siblings, 1 reply; 12+ messages in thread
From: Heiner Kallweit @ 2021-08-14 11:31 UTC (permalink / raw)
  To: Kai-Heng Feng
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On 13.08.2021 11:54, Kai-Heng Feng wrote:
> On Fri, Aug 13, 2021 at 2:49 PM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>>
>> On 12.08.2021 17:53, Kai-Heng Feng wrote:
>>> r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
>>> Same issue can be observed with older vendor drivers.
>>>
>>> The issue is however solved by the latest vendor driver. There's a new
>>> mechanism, which disables r8169's internal ASPM when the NIC traffic has
>>> more than 10 packets, and vice versa.
>>>
>>> Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
>>
>> As we have Realtek in this mail thread:
> 
> Is it still in active use? I always think it's just a dummy address...
At least mails to this address are not bounced, and this address still is
in MAINTAINERS. But right, I've never any reaction on mails to this
address. So it may make sense to remove it from MAINTAINERS.
Not sure what the process would be to do this.

> 
>> Typically hw issues affect 1-3 chip versions only. The ASPM problems seem
>> to have been existing for at least 15 years now, in every chip version.
>> It seems that even the new RTL8125 chip generation still has broken ASPM.
> 
> Is there a bug report for that?
> 
No. This was referring to your statement that also r8125 vendor driver
includes this "dynamic ASPM" workaround. They wouldn't have done this
if RTL8125 had proper ASPM support, or?

>> Why was this never fixed? ASPM not considered to be relevant? HW design
>> too broken?
> 
> IIUC, ASPM is extremely relevant to pass EU/US power consumption
> regulation. So I really don't know why the situation under Linux is so
> dire.
> 
It's not something related to Linux, ASPM support in the Realtek chips
is simply broken. This needs to be fixed in HW.
The behavior we see may indicate that certain buffers in the chips are
too small to buffer traffic for full period of ASPM exit latency.

> Kai-Heng
> 
>>
>>> use dynamic ASPM under Windows. So implement the same mechanism here to
>>> resolve the issue.
>>>
>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>> ---
>>> v2:
>>>  - Use delayed_work instead of timer_list to avoid interrupt context
>>>  - Use mutex to serialize packet counter read/write
>>>  - Wording change
>>>
>>>  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
>>>  1 file changed, 45 insertions(+)
>>>
>>> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
>>> index c7af5bc3b8af..7ab2e841dc69 100644
>>> --- a/drivers/net/ethernet/realtek/r8169_main.c
>>> +++ b/drivers/net/ethernet/realtek/r8169_main.c
>>> @@ -624,6 +624,11 @@ struct rtl8169_private {
>>>
>>>       unsigned supports_gmii:1;
>>>       unsigned aspm_manageable:1;
>>> +     unsigned aspm_enabled:1;
>>> +     struct delayed_work aspm_toggle;
>>> +     struct mutex aspm_mutex;
>>> +     u32 aspm_packet_count;
>>> +
>>>       dma_addr_t counters_phys_addr;
>>>       struct rtl8169_counters *counters;
>>>       struct rtl8169_tc_offsets tc_offset;
>>> @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
>>>               RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
>>>       }
>>>
>>> +     tp->aspm_enabled = enable;
>>> +
>>>       udelay(10);
>>>  }
>>>
>>> @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
>>>
>>>       dirty_tx = tp->dirty_tx;
>>>
>>> +     mutex_lock(&tp->aspm_mutex);
>>> +     tp->aspm_packet_count += tp->cur_tx - dirty_tx;
>>> +     mutex_unlock(&tp->aspm_mutex);
>>>       while (READ_ONCE(tp->cur_tx) != dirty_tx) {
>>>               unsigned int entry = dirty_tx % NUM_TX_DESC;
>>>               u32 status;
>>> @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
>>>               rtl8169_mark_to_asic(desc);
>>>       }
>>>
>>> +     mutex_lock(&tp->aspm_mutex);
>>> +     tp->aspm_packet_count += count;
>>> +     mutex_unlock(&tp->aspm_mutex);
>>> +
>>>       return count;
>>>  }
>>>
>>> @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
>>>       return 0;
>>>  }
>>>
>>> +#define ASPM_PACKET_THRESHOLD 10
>>> +#define ASPM_TOGGLE_INTERVAL 1000
>>> +
>>> +static void rtl8169_aspm_toggle(struct work_struct *work)
>>> +{
>>> +     struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
>>> +                                               aspm_toggle.work);
>>> +     bool enable;
>>> +
>>> +     mutex_lock(&tp->aspm_mutex);
>>> +     enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
>>> +     tp->aspm_packet_count = 0;
>>> +     mutex_unlock(&tp->aspm_mutex);
>>> +
>>> +     if (tp->aspm_enabled != enable) {
>>> +             rtl_unlock_config_regs(tp);
>>> +             rtl_hw_aspm_clkreq_enable(tp, enable);
>>> +             rtl_lock_config_regs(tp);
>>> +     }
>>> +
>>> +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
>>> +}
>>> +
>>>  static void rtl8169_down(struct rtl8169_private *tp)
>>>  {
>>> +     cancel_delayed_work_sync(&tp->aspm_toggle);
>>> +
>>>       /* Clear all task flags */
>>>       bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
>>>
>>> @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
>>>       rtl_reset_work(tp);
>>>
>>>       phy_start(tp->phydev);
>>> +
>>> +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
>>>  }
>>>
>>>  static int rtl8169_close(struct net_device *dev)
>>> @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>
>>>       INIT_WORK(&tp->wk.work, rtl_task);
>>>
>>> +     INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
>>> +
>>> +     mutex_init(&tp->aspm_mutex);
>>> +
>>>       rtl_init_mac_address(tp);
>>>
>>>       dev->ethtool_ops = &rtl8169_ethtool_ops;
>>>
>>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-13  9:46   ` Kai-Heng Feng
@ 2021-08-14 11:33     ` Heiner Kallweit
  0 siblings, 0 replies; 12+ messages in thread
From: Heiner Kallweit @ 2021-08-14 11:33 UTC (permalink / raw)
  To: Kai-Heng Feng
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On 13.08.2021 11:46, Kai-Heng Feng wrote:
> j
> 
> On Fri, Aug 13, 2021 at 3:39 AM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>>
>> On 12.08.2021 17:53, Kai-Heng Feng wrote:
>>> r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
>>> Same issue can be observed with older vendor drivers.
>>>
>>> The issue is however solved by the latest vendor driver. There's a new
>>> mechanism, which disables r8169's internal ASPM when the NIC traffic has
>>> more than 10 packets, and vice versa.
>>>
>>> Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
>>> use dynamic ASPM under Windows. So implement the same mechanism here to
>>> resolve the issue.
>>>
>> Realtek using something in their Windows drivers isn't really a proof of
>> quality.
> 
> I agree. So it'll be great if Realtek can work with us here.
> 
>> Still my concerns haven't been addressed. If ASPM is enabled and
>> there's a congestion in the chip it may take up to a second until ASPM
>> gets disabled. In this second traffic very likely is heavily affected.
>> Who takes care in case of problem reports?
> 
> I think we'll know that once the patch is merged in downstream kernel.
> 
>>
>> This is a massive change for basically all chip versions. And experience
>> shows that in case of problem reports Realtek never cares, even though
>> they are listed as maintainers. All I see is that they copy more and more
>> code from r8169 into their own drivers. This seems to indicate that they
>> consider quality of their own drivers as not sufficient.
> 
> I wonder why they don't want to put their efforts to r8169...
> Obviously they are doing a great job for rtw88 and r8152.
> 
>>
>> Still my proposal: Apply this downstream, and if there are no complaints
>> after a few months it may be considered for mainline.
> 
> Yes that's my plan. But I'd still like it to be reviewed before
> putting it to the downstream kernel.
> 
>>
>> Last but not least the formal issues:
>> - no cover letter
> 
> Will write it up once it's tested dowstream.
> 
>> - no net/net-next annotation
> 
> Does it mean put "net/net-next" in the subject line?
> 

https://www.kernel.org/doc/html/latest/networking/netdev-FAQ.html#how-do-i-indicate-which-tree-net-vs-net-next-my-patch-should-be-in

> 
>>
>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>> ---
>>> v2:
>>>  - Use delayed_work instead of timer_list to avoid interrupt context
>>>  - Use mutex to serialize packet counter read/write
>>>  - Wording change
>>>
>>>  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
>>>  1 file changed, 45 insertions(+)
>>>
>>> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
>>> index c7af5bc3b8af..7ab2e841dc69 100644
>>> --- a/drivers/net/ethernet/realtek/r8169_main.c
>>> +++ b/drivers/net/ethernet/realtek/r8169_main.c
>>> @@ -624,6 +624,11 @@ struct rtl8169_private {
>>>
>>>       unsigned supports_gmii:1;
>>>       unsigned aspm_manageable:1;
>>> +     unsigned aspm_enabled:1;
>>> +     struct delayed_work aspm_toggle;
>>> +     struct mutex aspm_mutex;
>>> +     u32 aspm_packet_count;
>>> +
>>>       dma_addr_t counters_phys_addr;
>>>       struct rtl8169_counters *counters;
>>>       struct rtl8169_tc_offsets tc_offset;
>>> @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
>>>               RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
>>>       }
>>>
>>> +     tp->aspm_enabled = enable;
>>> +
>>>       udelay(10);
>>>  }
>>>
>>> @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
>>>
>>>       dirty_tx = tp->dirty_tx;
>>>
>>> +     mutex_lock(&tp->aspm_mutex);
>>
>> We are in soft irq context here, therefore you shouldn't sleep.
> 
> I thought napi_poll is not using softirq, apparent I was wrong. Will
> correct it too.
> 
I saw an automated mail from a test bot to you complaining about this.

>>
>>> +     tp->aspm_packet_count += tp->cur_tx - dirty_tx;
>>> +     mutex_unlock(&tp->aspm_mutex);
>>>       while (READ_ONCE(tp->cur_tx) != dirty_tx) {
>>>               unsigned int entry = dirty_tx % NUM_TX_DESC;
>>>               u32 status;
>>> @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
>>>               rtl8169_mark_to_asic(desc);
>>>       }
>>>
>>> +     mutex_lock(&tp->aspm_mutex);
>>> +     tp->aspm_packet_count += count;
>>> +     mutex_unlock(&tp->aspm_mutex);
>>> +
>>>       return count;
>>>  }
>>>
>>> @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
>>>       return 0;
>>>  }
>>>
>>> +#define ASPM_PACKET_THRESHOLD 10
>>> +#define ASPM_TOGGLE_INTERVAL 1000
>>> +
>>> +static void rtl8169_aspm_toggle(struct work_struct *work)
>>> +{
>>> +     struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
>>> +                                               aspm_toggle.work);
>>> +     bool enable;
>>> +
>>> +     mutex_lock(&tp->aspm_mutex);
>>> +     enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
>>> +     tp->aspm_packet_count = 0;
>>> +     mutex_unlock(&tp->aspm_mutex);
>>> +
>>> +     if (tp->aspm_enabled != enable) {
>>> +             rtl_unlock_config_regs(tp);
>>> +             rtl_hw_aspm_clkreq_enable(tp, enable);
>>> +             rtl_lock_config_regs(tp);
>>> +     }
>>> +
>>> +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
>>> +}
>>> +
>>>  static void rtl8169_down(struct rtl8169_private *tp)
>>>  {
>>> +     cancel_delayed_work_sync(&tp->aspm_toggle);
>>> +
>>>       /* Clear all task flags */
>>>       bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
>>>
>>> @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
>>>       rtl_reset_work(tp);
>>>
>>>       phy_start(tp->phydev);
>>> +
>>> +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
>>
>> In the first version you used msecs_to_jiffies(ASPM_TIMER_INTERVAL).
>> Now you use 1000 jiffies what is a major difference.
> 
> msecs_to_jiffies() was omitted. Will correct it.
> 
> Kai-Heng
> 
>>
>>>  }
>>>
>>>  static int rtl8169_close(struct net_device *dev)
>>> @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>
>>>       INIT_WORK(&tp->wk.work, rtl_task);
>>>
>>> +     INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
>>> +
>>> +     mutex_init(&tp->aspm_mutex);
>>> +
>>>       rtl_init_mac_address(tp);
>>>
>>>       dev->ethtool_ops = &rtl8169_ethtool_ops;
>>>
>>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism
  2021-08-14 11:31     ` Heiner Kallweit
@ 2021-08-19  3:11       ` Kai-Heng Feng
  0 siblings, 0 replies; 12+ messages in thread
From: Kai-Heng Feng @ 2021-08-19  3:11 UTC (permalink / raw)
  To: Heiner Kallweit
  Cc: nic_swsd, David S. Miller, Jakub Kicinski,
	open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER, open list

On Sat, Aug 14, 2021 at 7:34 PM Heiner Kallweit <hkallweit1@gmail.com> wrote:
>
> On 13.08.2021 11:54, Kai-Heng Feng wrote:
> > On Fri, Aug 13, 2021 at 2:49 PM Heiner Kallweit <hkallweit1@gmail.com> wrote:
> >>
> >> On 12.08.2021 17:53, Kai-Heng Feng wrote:
> >>> r8169 NICs on some platforms have abysmal speed when ASPM is enabled.
> >>> Same issue can be observed with older vendor drivers.
> >>>
> >>> The issue is however solved by the latest vendor driver. There's a new
> >>> mechanism, which disables r8169's internal ASPM when the NIC traffic has
> >>> more than 10 packets, and vice versa.
> >>>
> >>> Realtek confirmed that all their PCIe LAN NICs, r8106, r8168 and r8125
> >>
> >> As we have Realtek in this mail thread:
> >
> > Is it still in active use? I always think it's just a dummy address...
> At least mails to this address are not bounced, and this address still is
> in MAINTAINERS. But right, I've never any reaction on mails to this
> address. So it may make sense to remove it from MAINTAINERS.
> Not sure what the process would be to do this.
>
> >
> >> Typically hw issues affect 1-3 chip versions only. The ASPM problems seem
> >> to have been existing for at least 15 years now, in every chip version.
> >> It seems that even the new RTL8125 chip generation still has broken ASPM.
> >
> > Is there a bug report for that?
> >
> No. This was referring to your statement that also r8125 vendor driver
> includes this "dynamic ASPM" workaround. They wouldn't have done this
> if RTL8125 had proper ASPM support, or?

They call it "performance tuning".

>
> >> Why was this never fixed? ASPM not considered to be relevant? HW design
> >> too broken?
> >
> > IIUC, ASPM is extremely relevant to pass EU/US power consumption
> > regulation. So I really don't know why the situation under Linux is so
> > dire.
> >
> It's not something related to Linux, ASPM support in the Realtek chips
> is simply broken. This needs to be fixed in HW.
> The behavior we see may indicate that certain buffers in the chips are
> too small to buffer traffic for full period of ASPM exit latency.

The smaller buffers is part of the reason why they are dirt cheap and
makes them so pervasive...
So the dynamic ASPM is actually a good thing because it can deal with
this defect and saves power at the same time.

Kai-Heng

>
> > Kai-Heng
> >
> >>
> >>> use dynamic ASPM under Windows. So implement the same mechanism here to
> >>> resolve the issue.
> >>>
> >>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> >>> ---
> >>> v2:
> >>>  - Use delayed_work instead of timer_list to avoid interrupt context
> >>>  - Use mutex to serialize packet counter read/write
> >>>  - Wording change
> >>>
> >>>  drivers/net/ethernet/realtek/r8169_main.c | 45 +++++++++++++++++++++++
> >>>  1 file changed, 45 insertions(+)
> >>>
> >>> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> >>> index c7af5bc3b8af..7ab2e841dc69 100644
> >>> --- a/drivers/net/ethernet/realtek/r8169_main.c
> >>> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> >>> @@ -624,6 +624,11 @@ struct rtl8169_private {
> >>>
> >>>       unsigned supports_gmii:1;
> >>>       unsigned aspm_manageable:1;
> >>> +     unsigned aspm_enabled:1;
> >>> +     struct delayed_work aspm_toggle;
> >>> +     struct mutex aspm_mutex;
> >>> +     u32 aspm_packet_count;
> >>> +
> >>>       dma_addr_t counters_phys_addr;
> >>>       struct rtl8169_counters *counters;
> >>>       struct rtl8169_tc_offsets tc_offset;
> >>> @@ -2671,6 +2676,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
> >>>               RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
> >>>       }
> >>>
> >>> +     tp->aspm_enabled = enable;
> >>> +
> >>>       udelay(10);
> >>>  }
> >>>
> >>> @@ -4408,6 +4415,9 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
> >>>
> >>>       dirty_tx = tp->dirty_tx;
> >>>
> >>> +     mutex_lock(&tp->aspm_mutex);
> >>> +     tp->aspm_packet_count += tp->cur_tx - dirty_tx;
> >>> +     mutex_unlock(&tp->aspm_mutex);
> >>>       while (READ_ONCE(tp->cur_tx) != dirty_tx) {
> >>>               unsigned int entry = dirty_tx % NUM_TX_DESC;
> >>>               u32 status;
> >>> @@ -4552,6 +4562,10 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget
> >>>               rtl8169_mark_to_asic(desc);
> >>>       }
> >>>
> >>> +     mutex_lock(&tp->aspm_mutex);
> >>> +     tp->aspm_packet_count += count;
> >>> +     mutex_unlock(&tp->aspm_mutex);
> >>> +
> >>>       return count;
> >>>  }
> >>>
> >>> @@ -4659,8 +4673,33 @@ static int r8169_phy_connect(struct rtl8169_private *tp)
> >>>       return 0;
> >>>  }
> >>>
> >>> +#define ASPM_PACKET_THRESHOLD 10
> >>> +#define ASPM_TOGGLE_INTERVAL 1000
> >>> +
> >>> +static void rtl8169_aspm_toggle(struct work_struct *work)
> >>> +{
> >>> +     struct rtl8169_private *tp = container_of(work, struct rtl8169_private,
> >>> +                                               aspm_toggle.work);
> >>> +     bool enable;
> >>> +
> >>> +     mutex_lock(&tp->aspm_mutex);
> >>> +     enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD;
> >>> +     tp->aspm_packet_count = 0;
> >>> +     mutex_unlock(&tp->aspm_mutex);
> >>> +
> >>> +     if (tp->aspm_enabled != enable) {
> >>> +             rtl_unlock_config_regs(tp);
> >>> +             rtl_hw_aspm_clkreq_enable(tp, enable);
> >>> +             rtl_lock_config_regs(tp);
> >>> +     }
> >>> +
> >>> +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> >>> +}
> >>> +
> >>>  static void rtl8169_down(struct rtl8169_private *tp)
> >>>  {
> >>> +     cancel_delayed_work_sync(&tp->aspm_toggle);
> >>> +
> >>>       /* Clear all task flags */
> >>>       bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
> >>>
> >>> @@ -4687,6 +4726,8 @@ static void rtl8169_up(struct rtl8169_private *tp)
> >>>       rtl_reset_work(tp);
> >>>
> >>>       phy_start(tp->phydev);
> >>> +
> >>> +     schedule_delayed_work(&tp->aspm_toggle, ASPM_TOGGLE_INTERVAL);
> >>>  }
> >>>
> >>>  static int rtl8169_close(struct net_device *dev)
> >>> @@ -5347,6 +5388,10 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> >>>
> >>>       INIT_WORK(&tp->wk.work, rtl_task);
> >>>
> >>> +     INIT_DELAYED_WORK(&tp->aspm_toggle, rtl8169_aspm_toggle);
> >>> +
> >>> +     mutex_init(&tp->aspm_mutex);
> >>> +
> >>>       rtl_init_mac_address(tp);
> >>>
> >>>       dev->ethtool_ops = &rtl8169_ethtool_ops;
> >>>
> >>
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-08-19  3:11 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-12 15:53 [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Kai-Heng Feng
2021-08-12 15:53 ` [PATCH v2 2/2] r8169: Enable ASPM for selected NICs Kai-Heng Feng
2021-08-12 19:38   ` Heiner Kallweit
2021-08-13 10:11     ` Kai-Heng Feng
2021-08-14 11:23       ` Heiner Kallweit
2021-08-12 19:34 ` [PATCH v2 1/2] r8169: Implement dynamic ASPM mechanism Heiner Kallweit
2021-08-13  9:46   ` Kai-Heng Feng
2021-08-14 11:33     ` Heiner Kallweit
2021-08-13  6:48 ` Heiner Kallweit
2021-08-13  9:54   ` Kai-Heng Feng
2021-08-14 11:31     ` Heiner Kallweit
2021-08-19  3:11       ` Kai-Heng Feng

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).