From patchwork Mon Oct 11 04:16:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AceLan Kao X-Patchwork-Id: 1539105 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=gQarG/78; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HSQVf6hQCz9sWJ for ; Mon, 11 Oct 2021 15:17:14 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1mZmkO-0003hw-20; Mon, 11 Oct 2021 04:17:08 +0000 Received: from mail-pf1-f174.google.com ([209.85.210.174]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1mZmkK-0003YB-Aq for kernel-team@lists.ubuntu.com; Mon, 11 Oct 2021 04:17:04 +0000 Received: by mail-pf1-f174.google.com with SMTP id m26so13751831pff.3 for ; Sun, 10 Oct 2021 21:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7SBZD2dr5j4iZb+2lsvRfu9joNL+ws4wwhcAbD7V51E=; b=gQarG/78FdI0vJiH0C/zpkvvgzG1oo72v95ioI137v6ercl5BG1QEmAtrngJyg6Ta3 Zl0TwogXb4eixJ7J2O5BgGXw2M6+euU7SstKP/SqKX6tL4UD3WjqL+cvFg8f4hEbNV9R 3dEwKwJtx4kcMooCyViHR+fg2y3GPZnOI/vaADdVEAX9pAfkfXMuob0md6rv5HqFfSdv iYWP2Oq+m2q+N4OqWXuN+hQ+TT4GP14a4jEuItPZsYxS5RjJl9OP6b82cLOSpsVSs7GQ otjlno7KEFdevkTwXtCq7TLIr2yG2rmyGYqkJehzkBP73Ziso/oFgbEvQ2R/ewhJdWqo HALA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=7SBZD2dr5j4iZb+2lsvRfu9joNL+ws4wwhcAbD7V51E=; b=pWA9LcjcbR8qbOoGjcmcblGJENym0XbriErBX19h4LdYZw/5Ic7ITWxtWxvNG8AXR+ x6GcV7IqRoksScYo29OzpGF9ugcGEK1EVy4GBB0p4pjUEDstorP4HMF+gT3xAk3cJIwq QuaUB7M+s/zYYJ6JG6INBxvScqUpwounhFdTy6D1l5VONPj5wJyJdepnHJBbnX9GFAMA SXr3lNDT30H/VCwPlnbTAKGNMvxPs7D+oBZfWNrcVojegZcGiOrRtFeb/Z5ReRpzoEJY LVWZXhQlv54b0ZJG5Qi3u4btCKnF5j89V9ygh3lqT96gzt85DSUhRZ1xwq4OBf8t20kY iEHA== X-Gm-Message-State: AOAM533qObCh+DLO9pvFGv0c40OxlCfaDO8Z1qE9Gb3NYz/BK7JVKnoS hiBsXaDurTU24A/gBaJ+qJTjqRoC1iQ= X-Google-Smtp-Source: ABdhPJzmFE61nOUMQAuEr8fTvK/AH49mvmXi8jkQNdThUfL2/pW6mKt1gygJd75p33til2V+S3XG5g== X-Received: by 2002:a62:be14:0:b0:44c:c997:6700 with SMTP id l20-20020a62be14000000b0044cc9976700mr21385480pff.80.1633925819547; Sun, 10 Oct 2021 21:16:59 -0700 (PDT) Received: from localhost (220-135-95-34.hinet-ip.hinet.net. [220.135.95.34]) by smtp.gmail.com with ESMTPSA id q11sm5913236pjf.14.2021.10.10.21.16.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Oct 2021 21:16:59 -0700 (PDT) From: AceLan Kao To: kernel-team@lists.ubuntu.com Subject: [PATCH 3/3][SRU][OEM-5.13][OEM-5.14] r8169: Implement dynamic ASPM mechanism Date: Mon, 11 Oct 2021 12:16:32 +0800 Message-Id: <20211011041632.52463-10-acelan.kao@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211011041632.52463-1-acelan.kao@canonical.com> References: <20211011041632.52463-1-acelan.kao@canonical.com> MIME-Version: 1.0 Received-SPF: pass client-ip=209.85.210.174; envelope-from=acelan@gmail.com; helo=mail-pf1-f174.google.com X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Kai-Heng Feng BugLink: https://bugs.launchpad.net/bugs/1946433 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 per second, and vice versa. The possible reason for this is likely because the buffer on the chip is too small for its ASPM exit latency. 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. Also introduce a lock to prevent race on accessing config registers. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=214307 Signed-off-by: Kai-Heng Feng (backported from https://patchwork.kernel.org/project/linux-pci/patch/20211007161552.272771-4-kai.heng.feng@canonical.com/) Signed-off-by: Chia-Lin Kao (AceLan) --- drivers/net/ethernet/realtek/r8169_main.c | 58 +++++++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c index 19c92aeccce1c..0b0db8761ef24 100644 --- a/drivers/net/ethernet/realtek/r8169_main.c +++ b/drivers/net/ethernet/realtek/r8169_main.c @@ -623,6 +623,11 @@ struct rtl8169_private { } wk; unsigned supports_gmii:1; + unsigned rtl_aspm_enabled:1; + struct delayed_work aspm_toggle; + atomic_t aspm_packet_count; + struct mutex config_lock; + dma_addr_t counters_phys_addr; struct rtl8169_counters *counters; struct rtl8169_tc_offsets tc_offset; @@ -671,12 +676,14 @@ static inline struct device *tp_to_dev(struct rtl8169_private *tp) static void rtl_lock_config_regs(struct rtl8169_private *tp) { + mutex_lock(&tp->config_lock); RTL_W8(tp, Cfg9346, Cfg9346_Lock); } static void rtl_unlock_config_regs(struct rtl8169_private *tp) { RTL_W8(tp, Cfg9346, Cfg9346_Unlock); + mutex_unlock(&tp->config_lock); } static void rtl_pci_commit(struct rtl8169_private *tp) @@ -2680,6 +2687,8 @@ static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable) if (!pcie_aspm_support_enabled() || !pcie_aspm_capable(pdev)) return; + tp->rtl_aspm_enabled = enable; + if (enable) { RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en); RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn); @@ -4430,6 +4439,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, dirty_tx = tp->dirty_tx; + atomic_add(tp->cur_tx - dirty_tx, &tp->aspm_packet_count); while (READ_ONCE(tp->cur_tx) != dirty_tx) { unsigned int entry = dirty_tx % NUM_TX_DESC; u32 status; @@ -4574,6 +4584,8 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget rtl8169_mark_to_asic(desc); } + atomic_add(count, &tp->aspm_packet_count); + return count; } @@ -4681,8 +4693,39 @@ 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); + int packet_count; + bool enable; + + packet_count = atomic_xchg(&tp->aspm_packet_count, 0); + + if (pcie_aspm_enabled(tp->pci_dev)) { + enable = packet_count <= ASPM_PACKET_THRESHOLD; + + if (tp->rtl_aspm_enabled != enable) { + rtl_unlock_config_regs(tp); + rtl_hw_aspm_clkreq_enable(tp, enable); + rtl_lock_config_regs(tp); + } + } else if (tp->rtl_aspm_enabled) { + rtl_unlock_config_regs(tp); + rtl_hw_aspm_clkreq_enable(tp, false); + rtl_lock_config_regs(tp); + } + + schedule_delayed_work(&tp->aspm_toggle, msecs_to_jiffies(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); @@ -4709,6 +4752,10 @@ static void rtl8169_up(struct rtl8169_private *tp) rtl_reset_work(tp); phy_start(tp->phydev); + + /* pcie_aspm_capable may change after system resume */ + if (pcie_aspm_support_enabled() && pcie_aspm_capable(tp->pci_dev)) + schedule_delayed_work(&tp->aspm_toggle, 0); } static int rtl8169_close(struct net_device *dev) @@ -5297,11 +5344,6 @@ 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. - */ - pci_disable_link_state(pdev, PCIE_LINK_STATE_L1); - /* enable device (incl. PCI PM wakeup and hotplug setup) */ rc = pcim_enable_device(pdev); if (rc < 0) { @@ -5331,6 +5373,8 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) return rc; } + mutex_init(&tp->config_lock); + tp->mmio_addr = pcim_iomap_table(pdev)[region]; xid = (RTL_R32(tp, TxConfig) >> 20) & 0xfcf; @@ -5368,6 +5412,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); + + atomic_set(&tp->aspm_packet_count, 0); + rtl_init_mac_address(tp); dev->ethtool_ops = &rtl8169_ethtool_ops;