From patchwork Mon Oct 10 02:59:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koba Ko X-Patchwork-Id: 1687903 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=DuHsgE+2; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Mm3Y53J2Tz1yqk for ; Mon, 10 Oct 2022 13:59:36 +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 1ohj0n-0000N5-8R; Mon, 10 Oct 2022 02:59:25 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1ohj0l-0000MA-8u for kernel-team@lists.ubuntu.com; Mon, 10 Oct 2022 02:59:23 +0000 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 786B340275 for ; Mon, 10 Oct 2022 02:59:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1665370761; bh=qgzd7zpfGP7azaG75dtIhKxTpxyS0kXqWqAa5Ta/PYg=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DuHsgE+2QHNjGc2wkmT8FFgW9dRl6feS8Mp0pjnepfeDtuEiMRXmJl3cGjGQeq5tq vLfixneUMqJBsGW4vlzqo5IsqfxrOJKoZkmje0TZTFI22fd5ka9tqb/g5FilQHMfbf AzcO4TD860mdr7L8AHymv+/yR3Jo7DJ3CiRjuUxEfQazqr8QecsshWpK9Tfi29xesK DPEulv9vqDWxHx2TkFVZoDuHF9jJICZ2XuKjrnjVLvPfwasCdAB8w8UxMQ+lD/uUkC Ip7vZTnPeH8jwFuShmH92Oe4hQJlfM9GauQAz4kdjwzHyh0B6PxqVciRHWG3ZxZJFJ MT0lL5dDpOCQA== Received: by mail-pl1-f199.google.com with SMTP id n9-20020a170902d2c900b001782ad97c7aso7175876plc.8 for ; Sun, 09 Oct 2022 19:59:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qgzd7zpfGP7azaG75dtIhKxTpxyS0kXqWqAa5Ta/PYg=; b=lOc1Ky3MrqVhtRDw3P8p5MKGzvrbO8RD0Gu+ETwevBXDneUa16I+/WBpr3WR/Cn/xv 0Kh7K3q/FfsM17QMT3zgqEi25JsG0zCog/xOfbhYPwaQU4k/FeQ0Geqv0Za5H5jBQxrP FjtGVU6eRNmB/INN+xc8Vc2LJxf0bCnBd9DOsKR21ujxBcXjSSV1wE0jCdgH3sZrameA 5Xa1EBBoB8C2U/17EkZpSH0kyjPhOTWw1H334Fi+nnk2HsyILqhlUgstvPV/SPMPQ9ou ffeT23F+D9oAAdctbbu31q0mG2wmt98nEvFplnjoi7IOvCaUGBDTOShClG4J+WsnJk02 N5Lg== X-Gm-Message-State: ACrzQf0rPvwG5A6xY8qB+2iM3Tp13HQXhcfW1pKzkBJFkGOaQ3JYmyEF UyC0IYb2gVuy+ps7vr2NmDukVy1R20mf3JfMjnGy6SVMd5+paiw7U2B0M7S2d/3rlkMJQe/ang3 neA6iddV38tacKdBaTFwtOoOZxw7W3hT0V9/1aFzmew== X-Received: by 2002:a05:6a00:4c8e:b0:562:a157:6599 with SMTP id eb14-20020a056a004c8e00b00562a1576599mr17612187pfb.59.1665370759914; Sun, 09 Oct 2022 19:59:19 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7gtqabL3NpYocAy9VykrVJDaH9BLuBpNRrLgaYKyFFZS0hACMvNX6BbBd00gYNifSuN+dtSA== X-Received: by 2002:a05:6a00:4c8e:b0:562:a157:6599 with SMTP id eb14-20020a056a004c8e00b00562a1576599mr17612168pfb.59.1665370759520; Sun, 09 Oct 2022 19:59:19 -0700 (PDT) Received: from canonical.com (2001-b011-3815-3445-4d14-f43c-3a40-bf20.dynamic-ip6.hinet.net. [2001:b011:3815:3445:4d14:f43c:3a40:bf20]) by smtp.gmail.com with ESMTPSA id h8-20020a17090a3d0800b001fd76f7a0d1sm8191224pjc.54.2022.10.09.19.59.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 19:59:19 -0700 (PDT) From: Koba Ko To: kernel-team@lists.ubuntu.com Subject: [PATCH 3/3] [SRU][OEM-6.0/OEM-5.17] r8169: Implement dynamic ASPM mechanism Date: Mon, 10 Oct 2022 10:59:12 +0800 Message-Id: <20221010025912.1042437-4-koba.ko@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221010025912.1042437-1-koba.ko@canonical.com> References: <20221010025912.1042437-1-koba.ko@canonical.com> MIME-Version: 1.0 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 (cherry picked from https://patchwork.kernel.org/project/netdevbpf/patch/20211016075442.650311-5-kai.heng.feng@canonical.com/) Signed-off-by: Koba Ko --- drivers/net/ethernet/realtek/r8169_main.c | 60 ++++++++++++++++++----- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c index 0fab9a6f6aecd..207abd9a74012 100644 --- a/drivers/net/ethernet/realtek/r8169_main.c +++ b/drivers/net/ethernet/realtek/r8169_main.c @@ -626,6 +626,10 @@ struct rtl8169_private { struct mutex config_lock; unsigned supports_gmii:1; + unsigned rtl_aspm_enabled:1; + struct delayed_work aspm_toggle; + atomic_t aspm_packet_count; + dma_addr_t counters_phys_addr; struct rtl8169_counters *counters; struct rtl8169_tc_offsets tc_offset; @@ -2703,6 +2707,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); @@ -4461,6 +4467,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; @@ -4605,6 +4612,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; } @@ -4712,8 +4721,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); @@ -4740,6 +4780,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) @@ -5382,18 +5426,6 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) tp->mac_version = chipset; - /* Disable ASPM L1 as that cause random device stop working - * problems as well as full system hangs for some PCIe devices users. - * Chips from RTL8168h partially have issues with L1.2, but seem - * to work fine with L1 and L1.1. - */ - if (rtl_aspm_is_safe(tp)) - rc = 0; - else if (tp->mac_version >= RTL_GIGA_MAC_VER_45) - rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L1_2); - else - pci_disable_link_state(pdev, PCIE_LINK_STATE_L1); - tp->dash_type = rtl_check_dash(tp); tp->cp_cmd = RTL_R16(tp, CPlusCmd) & CPCMD_MASK; @@ -5419,6 +5451,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;