From nobody Mon Dec 1 22:07:35 2025 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E74321DFD96; Sat, 29 Nov 2025 16:37:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764434274; cv=none; b=GZdWexXHku2NMTcQqFd6uP0DZgLM4A3oSv9lXxTQ+YyP2tJ+2ttlPYDXloNRbNfWzbTeS9LpLQbVXVZ8UTdJ9UGzhpJJbjAC4QWeGlGoWK5mTQn4/V4OnU56or4KtRASWOKr5nYRX5FISzA8UVI1te4LbI2UDBDdrTdOBsODJBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764434274; c=relaxed/simple; bh=J7wnaEXmtkaJ/djJC/Qk0yODrzGAEwUpCkQ9csnMHZc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X8bcoGDS7Ex1nPadCpn5zt1SFwap3z6OOwajEHSVlVFpkYBFQqDjtFwNZZPjt0hm1JNndYTv1dgi4MSxdySXsfqL3169vuNgMks8FSXhOXdxGorESB3+FEUGtCKN2Z2Iktni/DI2hFilwrDW9XePgxHYVI06GlnuDYkznZg5SWQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=KNJ/FvaH; arc=none smtp.client-ip=115.124.30.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="KNJ/FvaH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1764434261; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=08kxHX+2FZoqxP5sqq+PN3/iGoZOiq4d3YSpHT8HzpU=; b=KNJ/FvaH9mEpWGE6r6NZ0IZBAn899L7lHJjGlITj5Jgb3ZsXi/GbuSRFZaYvKiMSL8vsnRk+yRM46BiSz0r5Yw7n2TJEdC3tVg1Obllzm1wNaY5DPvkBl0EJbTO2gVh/fPIPHhzhz2kEWTfOaC4pYl41tYDegizXjLRabWMZSa8= Received: from VM20241011-104.tbsite.net(mailfrom:guanghuifeng@linux.alibaba.com fp:SMTPD_---0WtfxjJL_1764434240 cluster:ay36) by smtp.aliyun-inc.com; Sun, 30 Nov 2025 00:37:40 +0800 From: Guanghui Feng To: bhelgaas@google.com, ilpo.jarvinen@linux.intel.com Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, kanie@linux.alibaba.com, alikernel-developer@linux.alibaba.com, Guanghui Feng Subject: [PATCH v2] PCI: Fix PCIe SBR dev/link wait error Date: Sun, 30 Nov 2025 00:36:31 +0800 Message-ID: <20251129163631.2908340-1-guanghuifeng@linux.alibaba.com> X-Mailer: git-send-email 2.43.7 In-Reply-To: <2e3a1e6b-40ae-3878-e237-fb9032796af8@linux.intel.com> References: <2e3a1e6b-40ae-3878-e237-fb9032796af8@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When executing a PCIe secondary bus reset, all downstream switches and endpoints will generate reset events. Simultaneously, all PCIe links will undergo retraining, and each link will independently re-execute the LTSSM state machine training. Therefore, after executing the SBR, it is necessary to wait for all downstream links and devices to complete recovery. Otherwise, after the SBR returns, accessing devices with some links or endpoints not yet fully recovered may result in driver errors, or even trigger device offline issues. Signed-off-by: Guanghui Feng Reviewed-by: Guixin Liu --- drivers/pci/pci.c | 138 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 41 deletions(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index b14dd064006c..76afecb11164 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -4788,6 +4788,63 @@ static int pci_bus_max_d3cold_delay(const struct pci= _bus *bus) return max(min_delay, max_delay); } =20 +static int pci_readiness_check(struct pci_dev *pdev, struct pci_dev *child, + unsigned long start_t, char *reset_type) +{ + int elapsed =3D jiffies_to_msecs(jiffies - start_t); + + if (pci_dev_is_disconnected(pdev) || pci_dev_is_disconnected(child)) + return 0; + + if (pcie_get_speed_cap(pdev) <=3D PCIE_SPEED_5_0GT) { + u16 status; + + pci_dbg(pdev, "waiting %d ms for downstream link\n", elapsed); + + if (!pci_dev_wait(child, reset_type, 0)) + return 0; + + if (PCI_RESET_WAIT > elapsed) + return PCI_RESET_WAIT - elapsed; + + /* + * If the port supports active link reporting we now check + * whether the link is active and if not bail out early with + * the assumption that the device is not present anymore. + */ + if (!pdev->link_active_reporting) + return -ENOTTY; + + pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &status); + if (!(status & PCI_EXP_LNKSTA_DLLLA)) + return -ENOTTY; + + if (!pci_dev_wait(child, reset_type, 0)) + return 0; + + if (PCIE_RESET_READY_POLL_MS > elapsed) + return PCIE_RESET_READY_POLL_MS - elapsed; + + return -ENOTTY; + } + + pci_dbg(pdev, "waiting %d ms for downstream link, after activation\n", + elapsed); + if (!pcie_wait_for_link_delay(pdev, true, 0)) { + /* Did not train, no need to wait any further */ + pci_info(pdev, "Data Link Layer Link Active not set in %d msec\n", elaps= ed); + return -ENOTTY; + } + + if (!pci_dev_wait(child, reset_type, 0)) + return 0; + + if (PCIE_RESET_READY_POLL_MS > elapsed) + return PCIE_RESET_READY_POLL_MS - elapsed; + + return -ENOTTY; +} + /** * pci_bridge_wait_for_secondary_bus - Wait for secondary bus to be access= ible * @dev: PCI bridge @@ -4802,12 +4859,14 @@ static int pci_bus_max_d3cold_delay(const struct pc= i_bus *bus) * 4.3.2. * * Return 0 on success or -ENOTTY if the first device on the secondary bus - * failed to become accessible. + * failed to become accessible or a value greater than 0 indicates the + * left required waiting time.. */ -int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_typ= e) +static int __pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, unsign= ed long start_t, + char *reset_type) { - struct pci_dev *child __free(pci_dev_put) =3D NULL; - int delay; + struct pci_dev *child; + int delay, ret, elapsed =3D jiffies_to_msecs(jiffies - start_t); =20 if (pci_dev_is_disconnected(dev)) return 0; @@ -4835,8 +4894,6 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev = *dev, char *reset_type) return 0; } =20 - child =3D pci_dev_get(list_first_entry(&dev->subordinate->devices, - struct pci_dev, bus_list)); up_read(&pci_bus_sem); =20 /* @@ -4844,8 +4901,10 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev= *dev, char *reset_type) * accessing the device after reset (that is 1000 ms + 100 ms). */ if (!pci_is_pcie(dev)) { - pci_dbg(dev, "waiting %d ms for secondary bus\n", 1000 + delay); - msleep(1000 + delay); + if (1000 + delay > elapsed) + return 1000 + delay - elapsed; + + pci_dbg(dev, "waiting %d ms for secondary bus\n", elapsed); return 0; } =20 @@ -4867,41 +4926,40 @@ int pci_bridge_wait_for_secondary_bus(struct pci_de= v *dev, char *reset_type) if (!pcie_downstream_port(dev)) return 0; =20 - if (pcie_get_speed_cap(dev) <=3D PCIE_SPEED_5_0GT) { - u16 status; - - pci_dbg(dev, "waiting %d ms for downstream link\n", delay); - msleep(delay); - - if (!pci_dev_wait(child, reset_type, PCI_RESET_WAIT - delay)) - return 0; + if (delay > elapsed) + return delay - elapsed; =20 + down_read(&pci_bus_sem); + list_for_each_entry(child, &dev->subordinate->devices, bus_list) { /* - * If the port supports active link reporting we now check - * whether the link is active and if not bail out early with - * the assumption that the device is not present anymore. + * Check if all devices under the same bus have completed + * the reset process, including multifunction devices in + * the same bus. */ - if (!dev->link_active_reporting) - return -ENOTTY; + ret =3D pci_readiness_check(dev, child, start_t, reset_type); =20 - pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &status); - if (!(status & PCI_EXP_LNKSTA_DLLLA)) - return -ENOTTY; + if (ret =3D=3D 0 && child->subordinate) + ret =3D __pci_bridge_wait_for_secondary_bus(child, start_t, reset_type); =20 - return pci_dev_wait(child, reset_type, - PCIE_RESET_READY_POLL_MS - PCI_RESET_WAIT); + if(ret) + break; } + up_read(&pci_bus_sem); =20 - pci_dbg(dev, "waiting %d ms for downstream link, after activation\n", - delay); - if (!pcie_wait_for_link_delay(dev, true, delay)) { - /* Did not train, no need to wait any further */ - pci_info(dev, "Data Link Layer Link Active not set in %d msec\n", delay); - return -ENOTTY; - } + return ret; +} =20 - return pci_dev_wait(child, reset_type, - PCIE_RESET_READY_POLL_MS - delay); +int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_typ= e) +{ + int res =3D 0; + unsigned long start_t =3D jiffies; + + do { + msleep(res); + res =3D __pci_bridge_wait_for_secondary_bus(dev, start_t, reset_type); + } while (res > 0); + + return res; } =20 void pci_reset_secondary_bus(struct pci_dev *dev) @@ -5542,10 +5600,8 @@ static void pci_bus_restore_locked(struct pci_bus *b= us) =20 list_for_each_entry(dev, &bus->devices, bus_list) { pci_dev_restore(dev); - if (dev->subordinate) { - pci_bridge_wait_for_secondary_bus(dev, "bus reset"); + if (dev->subordinate) pci_bus_restore_locked(dev->subordinate); - } } } =20 @@ -5575,14 +5631,14 @@ static void pci_slot_restore_locked(struct pci_slot= *slot) { struct pci_dev *dev; =20 + pci_bridge_wait_for_secondary_bus(slot->bus->self, "slot reset"); + list_for_each_entry(dev, &slot->bus->devices, bus_list) { if (!dev->slot || dev->slot !=3D slot) continue; pci_dev_restore(dev); - if (dev->subordinate) { - pci_bridge_wait_for_secondary_bus(dev, "slot reset"); + if (dev->subordinate) pci_bus_restore_locked(dev->subordinate); - } } } =20 --=20 2.32.0.3.gf3a3e56d6