From patchwork Tue Aug 3 15:28:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai-Heng Feng X-Patchwork-Id: 12416597 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35452C4338F for ; Tue, 3 Aug 2021 15:29:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1998060F9C for ; Tue, 3 Aug 2021 15:29:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236776AbhHCPaA (ORCPT ); Tue, 3 Aug 2021 11:30:00 -0400 Received: from smtp-relay-canonical-1.canonical.com ([185.125.188.121]:58284 "EHLO smtp-relay-canonical-1.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236864AbhHCP3E (ORCPT ); Tue, 3 Aug 2021 11:29:04 -0400 Received: from localhost (36-229-239-123.dynamic-ip.hinet.net [36.229.239.123]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id 7FD523F07E; Tue, 3 Aug 2021 15:28:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1628004515; bh=vG/mdcvhjFHm43VGopi8z7l3Sq5+1VzRuGB94PyIYAo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=slRUaB08+pOfAwPunNoAbN/i5SaQ/TNvmX1gArogt8u6i3eeKu/7/Bva8zjuoaOZ0 WHpYiqIzHo2+k4sLQzs8yjv2Zqtv/47lX89ulwuL/kFuEKbjCKm0upSIegDwDRfVol tzpdKI0g8RxXEFsFoDr+HjeyLBeC1fSNsSdwvUzJ7du2FKkYMEexBfTfca+lNVkm8K yKjxKYBLvEFxFWy9fEOPVZ4LAF1k5TnsbyKdqqz6EnLbg/LvGM8gdod2+hA7+q2ONl 9gNXSb5nZMXYS6g0tsP+FOCzPTl/23GYEpxMOnx6FQgEa4s2E+DvIUykSikNTkHcae GukUuu9AO0TjA== From: Kai-Heng Feng To: hkallweit1@gmail.com, nic_swsd@realtek.com Cc: Kai-Heng Feng , "David S. Miller" , Jakub Kicinski , netdev@vger.kernel.org (open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER), linux-kernel@vger.kernel.org (open list) Subject: [PATCH 1/2] r8169: Implement dynamic ASPM mechanism Date: Tue, 3 Aug 2021 23:28:22 +0800 Message-Id: <20210803152823.515849-1-kai.heng.feng@canonical.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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 has substantial network traffic, and vice versa. So implement the same mechanism here to resolve the issue. Signed-off-by: Kai-Heng Feng --- drivers/net/ethernet/realtek/r8169_main.c | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c index c7af5bc3b8af..e257d3cd885e 100644 --- a/drivers/net/ethernet/realtek/r8169_main.c +++ b/drivers/net/ethernet/realtek/r8169_main.c @@ -624,6 +624,10 @@ struct rtl8169_private { unsigned supports_gmii:1; unsigned aspm_manageable:1; + unsigned aspm_enabled:1; + struct timer_list aspm_timer; + u32 aspm_packet_count; + dma_addr_t counters_phys_addr; struct rtl8169_counters *counters; struct rtl8169_tc_offsets tc_offset; @@ -2671,6 +2675,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 +4414,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, dirty_tx = tp->dirty_tx; + tp->aspm_packet_count += tp->cur_tx - dirty_tx; while (READ_ONCE(tp->cur_tx) != dirty_tx) { unsigned int entry = dirty_tx % NUM_TX_DESC; u32 status; @@ -4552,6 +4559,8 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget rtl8169_mark_to_asic(desc); } + tp->aspm_packet_count += count; + return count; } @@ -4659,8 +4668,31 @@ static int r8169_phy_connect(struct rtl8169_private *tp) return 0; } +#define ASPM_PACKET_THRESHOLD 10 +#define ASPM_TIMER_INTERVAL 1000 + +static void rtl8169_aspm_timer(struct timer_list *timer) +{ + struct rtl8169_private *tp = from_timer(tp, timer, aspm_timer); + bool enable; + + enable = tp->aspm_packet_count <= ASPM_PACKET_THRESHOLD; + + if (tp->aspm_enabled != enable) { + rtl_unlock_config_regs(tp); + rtl_hw_aspm_clkreq_enable(tp, enable); + rtl_lock_config_regs(tp); + } + + tp->aspm_packet_count = 0; + + mod_timer(timer, jiffies + msecs_to_jiffies(ASPM_TIMER_INTERVAL)); +} + static void rtl8169_down(struct rtl8169_private *tp) { + del_timer_sync(&tp->aspm_timer); + /* Clear all task flags */ bitmap_zero(tp->wk.flags, RTL_FLAG_MAX); @@ -4687,6 +4719,10 @@ static void rtl8169_up(struct rtl8169_private *tp) rtl_reset_work(tp); phy_start(tp->phydev); + + timer_setup(&tp->aspm_timer, rtl8169_aspm_timer, 0); + mod_timer(&tp->aspm_timer, + jiffies + msecs_to_jiffies(ASPM_TIMER_INTERVAL)); } static int rtl8169_close(struct net_device *dev) From patchwork Tue Aug 3 15:28:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai-Heng Feng X-Patchwork-Id: 12416599 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E941C4338F for ; Tue, 3 Aug 2021 15:29:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 85AC260F94 for ; Tue, 3 Aug 2021 15:29:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236863AbhHCPaG (ORCPT ); Tue, 3 Aug 2021 11:30:06 -0400 Received: from smtp-relay-canonical-1.canonical.com ([185.125.188.121]:58306 "EHLO smtp-relay-canonical-1.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236820AbhHCP3E (ORCPT ); Tue, 3 Aug 2021 11:29:04 -0400 Received: from localhost (36-229-239-123.dynamic-ip.hinet.net [36.229.239.123]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id 940D43F0F8; Tue, 3 Aug 2021 15:28:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1628004520; bh=LqOeJ8mEKmfOoRP9FP3XOeFTzokfryMcK7iPHjhmqGE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MjDRJEezUxjRkBPtMVv3lTpsQrx2fmWVsbBXc1zIoK9jRt6yLLwKAlc1qUUDlpSjE qSV+TgiNXaMBE/R7YNuXFIl4j8BUPYuSHtnfe4H3Ic5k9x1KIxwnxpDTwdlRO8OimE rV6WmIHlZmGVVBkbvmrmjeMscn0m3S+HWDCDt3P8WvBt9FU/fKaXz20aD3ioq79qrW webKg9s9YvZV3XruH1WTGy+Rl55nYX8fQLUyjsfyi6RKasjF9LwiB1DSxAPjGVXJiC Lep9ezES7XJsja5h8RJZdrJ30OIo/ng4At1yh0cKhKQQUaGcUS6OvTTlm2LfSzGN95 1Ge9pTHcFbGGw== From: Kai-Heng Feng To: hkallweit1@gmail.com, nic_swsd@realtek.com Cc: Kai-Heng Feng , "David S. Miller" , Jakub Kicinski , netdev@vger.kernel.org (open list:8169 10/100/1000 GIGABIT ETHERNET DRIVER), linux-kernel@vger.kernel.org (open list) Subject: [PATCH 2/2] r8169: Enable ASPM for selected NICs Date: Tue, 3 Aug 2021 23:28:23 +0800 Message-Id: <20210803152823.515849-2-kai.heng.feng@canonical.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803152823.515849-1-kai.heng.feng@canonical.com> References: <20210803152823.515849-1-kai.heng.feng@canonical.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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 --- 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 e257d3cd885e..ec09c13514bd 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 timer_list aspm_timer; u32 aspm_packet_count; @@ -2666,8 +2666,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 { @@ -5279,6 +5282,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; @@ -5310,12 +5328,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);