[PATCH RFC 2/3] PCI: Allow quirks to extend the link-up wait after reset
From: Brett Creeley
Date: Wed Dec 02 2026 - 19:43:57 EST
After a DPC event, Secondary Bus Reset or D3cold->D0 transition,
pci_bridge_wait_for_secondary_bus() waits for the link below the
Downstream Port to become active again. On ports faster than 5 GT/s,
the wait is bounded by PCIE_LINK_RETRAIN_TIMEOUT_MS (1 s). If the link
has not trained by then, the device is considered gone and, for DPC,
error recovery fails with PCI_ERS_RESULT_DISCONNECT.
PCIe r5.0 sec 6.6.1 requires that, within 1.0 s of a Conventional Reset,
a device can receive a Configuration Request and return a Successful
Completion, independent of how quickly link training completes, so 1 s
is enough for compliant devices. Some devices need much longer to bring
the link up, for instance when the reset also restarts embedded firmware
that controls the link. The Readiness Time Reporting Extended
Capability (sec 7.9.17) cannot describe this: its times are capped at
about 1.006 s and are only meant to shorten the spec-mandated waits.
Add a link_up_timeout member to struct pci_dev that quirks can set for
such devices. When waiting for the secondary bus, use the largest
value of the devices on that bus, falling back to the existing 1 s, so
devices without the quirk see no change. The value lives in the
child's struct pci_dev, which persists across DPC recovery and bus
resets, so it remains available even though the device's config space
is inaccessible while the link is down.
The extended timeout is only used where the link can be polled:
Downstream Ports faster than 5 GT/s that report Data Link Layer Link
Active. Everywhere else the existing waits apply unchanged; ports
without that reporting can only sleep, and would otherwise sleep for
the full extended timeout on every reset. Where it is used, the
device is waited for even when firmware has zeroed every d3cold_delay
on the bus (e.g. via the ACPI reset delay _DSM), which otherwise skips
the wait entirely: firmware cannot account for a device erratum. A
separate message reports when the extended wait expires.
Signed-off-by: Brett Creeley <brett.creeley@xxxxxxx>
---
drivers/pci/pci.c | 73 +++++++++++++++++++++++++++++++++++++++++++++--------
include/linux/pci.h | 1 +
2 files changed, 63 insertions(+), 11 deletions(-)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index f89ad49e61e6..c387a564d997 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -4614,12 +4614,14 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe)
* @pdev: Device whose link to wait for.
* @use_lt: Use the LT bit if TRUE, or the DLLLA bit if FALSE.
* @active: Waiting for active or inactive?
+ * @timeout_ms: How long to wait for the status change (in ms)
*
* Return 0 if successful, or -ETIMEDOUT if status has not changed within
- * PCIE_LINK_RETRAIN_TIMEOUT_MS milliseconds.
+ * @timeout_ms milliseconds.
*/
static int pcie_wait_for_link_status(struct pci_dev *pdev,
- bool use_lt, bool active)
+ bool use_lt, bool active,
+ unsigned int timeout_ms)
{
u16 lnksta_mask, lnksta_match;
unsigned long end_jiffies;
@@ -4628,7 +4630,7 @@ static int pcie_wait_for_link_status(struct pci_dev *pdev,
lnksta_mask = use_lt ? PCI_EXP_LNKSTA_LT : PCI_EXP_LNKSTA_DLLLA;
lnksta_match = active ? lnksta_mask : 0;
- end_jiffies = jiffies + msecs_to_jiffies(PCIE_LINK_RETRAIN_TIMEOUT_MS);
+ end_jiffies = jiffies + msecs_to_jiffies(timeout_ms);
do {
pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
if ((lnksta & lnksta_mask) == lnksta_match)
@@ -4667,7 +4669,8 @@ int pcie_retrain_link(struct pci_dev *pdev, bool use_lt)
* avoid LTSSM race as recommended in Implementation Note at the end
* of PCIe r6.1 sec 7.5.3.7.
*/
- rc = pcie_wait_for_link_status(pdev, true, false);
+ rc = pcie_wait_for_link_status(pdev, true, false,
+ PCIE_LINK_RETRAIN_TIMEOUT_MS);
if (rc)
return rc;
@@ -4681,7 +4684,8 @@ int pcie_retrain_link(struct pci_dev *pdev, bool use_lt)
pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL, PCI_EXP_LNKCTL_RL);
}
- rc = pcie_wait_for_link_status(pdev, use_lt, !use_lt);
+ rc = pcie_wait_for_link_status(pdev, use_lt, !use_lt,
+ PCIE_LINK_RETRAIN_TIMEOUT_MS);
/*
* Clear LBMS after a manual retrain so that the bit can be used
@@ -4709,11 +4713,12 @@ int pcie_retrain_link(struct pci_dev *pdev, bool use_lt)
* @pdev: Bridge device
* @active: waiting for active or inactive?
* @delay: Delay to wait after link has become active (in ms)
+ * @timeout_ms: How long to poll for the link to change state (in ms)
*
* Use this to wait till link becomes active or inactive.
*/
static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active,
- int delay)
+ int delay, unsigned int timeout_ms)
{
int rc;
@@ -4737,7 +4742,7 @@ static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active,
*/
if (active)
msleep(20);
- rc = pcie_wait_for_link_status(pdev, false, active);
+ rc = pcie_wait_for_link_status(pdev, false, active, timeout_ms);
if (active) {
if (rc)
rc = pcie_failed_link_retrain(pdev);
@@ -4763,7 +4768,8 @@ static bool pcie_wait_for_link_delay(struct pci_dev *pdev, bool active,
*/
bool pcie_wait_for_link(struct pci_dev *pdev, bool active)
{
- return pcie_wait_for_link_delay(pdev, active, 100);
+ return pcie_wait_for_link_delay(pdev, active, 100,
+ PCIE_LINK_RETRAIN_TIMEOUT_MS);
}
/*
@@ -4791,6 +4797,40 @@ static int pci_bus_max_d3cold_delay(const struct pci_bus *bus)
return max(min_delay, max_delay);
}
+/*
+ * Find the longest time any device on the bus needs for its link to come
+ * up after reset. Quirks may extend it for devices whose link takes longer
+ * than the spec allows to train.
+ *
+ * Context: Called with @pci_bus_sem locked for reading.
+ */
+static unsigned int pci_bus_max_link_up_timeout(const struct pci_bus *bus)
+{
+ unsigned int timeout = PCIE_LINK_RETRAIN_TIMEOUT_MS;
+ const struct pci_dev *pdev;
+
+ lockdep_assert_held(&pci_bus_sem);
+
+ list_for_each_entry(pdev, &bus->devices, bus_list)
+ timeout = max(timeout, pdev->link_up_timeout);
+
+ return timeout;
+}
+
+/*
+ * An extended link-up timeout is only used where the link can be polled:
+ * Downstream Ports faster than 5 GT/s that report Data Link Layer Link
+ * Active. Elsewhere the existing waits apply unchanged.
+ */
+static bool pcie_link_up_timeout_extended(struct pci_dev *bridge,
+ unsigned int timeout)
+{
+ return timeout > PCIE_LINK_RETRAIN_TIMEOUT_MS &&
+ pcie_downstream_port(bridge) &&
+ pcie_get_speed_cap(bridge) > PCIE_SPEED_5_0GT &&
+ bridge->link_active_reporting;
+}
+
/**
* pci_bridge_wait_for_secondary_bus - Wait for secondary bus to be accessible
* @dev: PCI bridge
@@ -4810,6 +4850,8 @@ static int pci_bus_max_d3cold_delay(const struct pci_bus *bus)
int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
{
struct pci_dev *child __free(pci_dev_put) = NULL;
+ unsigned int link_up_timeout;
+ bool extended;
int delay;
if (pci_dev_is_disconnected(dev))
@@ -4833,7 +4875,9 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
/* Take d3cold_delay requirements into account */
delay = pci_bus_max_d3cold_delay(dev->subordinate);
- if (!delay) {
+ link_up_timeout = pci_bus_max_link_up_timeout(dev->subordinate);
+ extended = pcie_link_up_timeout_extended(dev, link_up_timeout);
+ if (!delay && !extended) {
up_read(&pci_bus_sem);
return 0;
}
@@ -4897,9 +4941,16 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
pci_dbg(dev, "waiting %d ms for downstream link, after activation\n",
delay);
- if (!pcie_wait_for_link_delay(dev, true, delay)) {
+ if (extended)
+ pci_dbg(dev, "allowing %u ms for downstream link to come up\n",
+ link_up_timeout);
+ if (!pcie_wait_for_link_delay(dev, true, delay, link_up_timeout)) {
/* Did not train, no need to wait any further */
- pci_info(dev, "Data Link Layer Link Active not set in %d msec\n", delay);
+ if (extended)
+ pci_info(dev, "Data Link Layer Link Active not set in extended %u msec\n",
+ link_up_timeout);
+ else
+ pci_info(dev, "Data Link Layer Link Active not set in %d msec\n", delay);
return -ENOTTY;
}
diff --git a/include/linux/pci.h b/include/linux/pci.h
index aee6a178a535..33de05d509e4 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -434,6 +434,7 @@ struct pci_dev {
unsigned int no_bw_notif:1; /* BW notifications may cause issues */
unsigned int d3hot_delay; /* D3hot->D0 transition time in ms */
unsigned int d3cold_delay; /* D3cold->D0 transition time in ms */
+ unsigned int link_up_timeout; /* Link-up wait (ms), >5 GT/s ports */
u16 l1ss; /* L1SS Capability pointer */
#ifdef CONFIG_PCIEASPM
--
2.43.0