1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Express Downstream Port Containment services driver
4 * Author: Keith Busch <keith.busch@intel.com>
6 * Copyright (C) 2016 Intel Corp.
9 #define dev_fmt(fmt) "DPC: " fmt
11 #include <linux/aer.h>
12 #include <linux/bitfield.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/pci.h>
21 #define PCI_EXP_DPC_CTL_EN_MASK (PCI_EXP_DPC_CTL_EN_FATAL | \
22 PCI_EXP_DPC_CTL_EN_NONFATAL)
24 static const char * const rp_pio_error_string
[] = {
25 "Configuration Request received UR Completion", /* Bit Position 0 */
26 "Configuration Request received CA Completion", /* Bit Position 1 */
27 "Configuration Request Completion Timeout", /* Bit Position 2 */
33 "I/O Request received UR Completion", /* Bit Position 8 */
34 "I/O Request received CA Completion", /* Bit Position 9 */
35 "I/O Request Completion Timeout", /* Bit Position 10 */
41 "Memory Request received UR Completion", /* Bit Position 16 */
42 "Memory Request received CA Completion", /* Bit Position 17 */
43 "Memory Request Completion Timeout", /* Bit Position 18 */
46 void pci_save_dpc_state(struct pci_dev
*dev
)
48 struct pci_cap_saved_state
*save_state
;
51 if (!pci_is_pcie(dev
))
54 save_state
= pci_find_saved_ext_cap(dev
, PCI_EXT_CAP_ID_DPC
);
58 cap
= (u16
*)&save_state
->cap
.data
[0];
59 pci_read_config_word(dev
, dev
->dpc_cap
+ PCI_EXP_DPC_CTL
, cap
);
62 void pci_restore_dpc_state(struct pci_dev
*dev
)
64 struct pci_cap_saved_state
*save_state
;
67 if (!pci_is_pcie(dev
))
70 save_state
= pci_find_saved_ext_cap(dev
, PCI_EXT_CAP_ID_DPC
);
74 cap
= (u16
*)&save_state
->cap
.data
[0];
75 pci_write_config_word(dev
, dev
->dpc_cap
+ PCI_EXP_DPC_CTL
, *cap
);
78 static DECLARE_WAIT_QUEUE_HEAD(dpc_completed_waitqueue
);
80 #ifdef CONFIG_HOTPLUG_PCI_PCIE
81 static bool dpc_completed(struct pci_dev
*pdev
)
85 pci_read_config_word(pdev
, pdev
->dpc_cap
+ PCI_EXP_DPC_STATUS
, &status
);
86 if ((!PCI_POSSIBLE_ERROR(status
)) && (status
& PCI_EXP_DPC_STATUS_TRIGGER
))
89 if (test_bit(PCI_DPC_RECOVERING
, &pdev
->priv_flags
))
96 * pci_dpc_recovered - whether DPC triggered and has recovered successfully
99 * Return true if DPC was triggered for @pdev and has recovered successfully.
100 * Wait for recovery if it hasn't completed yet. Called from the PCIe hotplug
101 * driver to recognize and ignore Link Down/Up events caused by DPC.
103 bool pci_dpc_recovered(struct pci_dev
*pdev
)
105 struct pci_host_bridge
*host
;
111 * Synchronization between hotplug and DPC is not supported
112 * if DPC is owned by firmware and EDR is not enabled.
114 host
= pci_find_host_bridge(pdev
->bus
);
115 if (!host
->native_dpc
&& !IS_ENABLED(CONFIG_PCIE_EDR
))
119 * Need a timeout in case DPC never completes due to failure of
120 * dpc_wait_rp_inactive(). The spec doesn't mandate a time limit,
121 * but reports indicate that DPC completes within 4 seconds.
123 wait_event_timeout(dpc_completed_waitqueue
, dpc_completed(pdev
),
124 msecs_to_jiffies(4000));
126 return test_and_clear_bit(PCI_DPC_RECOVERED
, &pdev
->priv_flags
);
128 #endif /* CONFIG_HOTPLUG_PCI_PCIE */
130 static int dpc_wait_rp_inactive(struct pci_dev
*pdev
)
132 unsigned long timeout
= jiffies
+ HZ
;
133 u16 cap
= pdev
->dpc_cap
, status
;
135 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
136 while (status
& PCI_EXP_DPC_RP_BUSY
&&
137 !time_after(jiffies
, timeout
)) {
139 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
141 if (status
& PCI_EXP_DPC_RP_BUSY
) {
142 pci_warn(pdev
, "root port still busy\n");
148 pci_ers_result_t
dpc_reset_link(struct pci_dev
*pdev
)
150 pci_ers_result_t ret
;
153 set_bit(PCI_DPC_RECOVERING
, &pdev
->priv_flags
);
156 * DPC disables the Link automatically in hardware, so it has
157 * already been reset by the time we get here.
162 * Wait until the Link is inactive, then clear DPC Trigger Status
163 * to allow the Port to leave DPC.
165 if (!pcie_wait_for_link(pdev
, false))
166 pci_info(pdev
, "Data Link Layer Link Active not cleared in 1000 msec\n");
168 if (pdev
->dpc_rp_extensions
&& dpc_wait_rp_inactive(pdev
)) {
169 clear_bit(PCI_DPC_RECOVERED
, &pdev
->priv_flags
);
170 ret
= PCI_ERS_RESULT_DISCONNECT
;
174 pci_write_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
,
175 PCI_EXP_DPC_STATUS_TRIGGER
);
177 if (pci_bridge_wait_for_secondary_bus(pdev
, "DPC")) {
178 clear_bit(PCI_DPC_RECOVERED
, &pdev
->priv_flags
);
179 ret
= PCI_ERS_RESULT_DISCONNECT
;
181 set_bit(PCI_DPC_RECOVERED
, &pdev
->priv_flags
);
182 ret
= PCI_ERS_RESULT_RECOVERED
;
185 clear_bit(PCI_DPC_RECOVERING
, &pdev
->priv_flags
);
186 wake_up_all(&dpc_completed_waitqueue
);
190 static void dpc_process_rp_pio_error(struct pci_dev
*pdev
)
192 u16 cap
= pdev
->dpc_cap
, dpc_status
, first_error
;
193 u32 status
, mask
, sev
, syserr
, exc
, log
, prefix
;
194 struct pcie_tlp_log tlp_log
;
197 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_STATUS
, &status
);
198 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_MASK
, &mask
);
199 pci_err(pdev
, "rp_pio_status: %#010x, rp_pio_mask: %#010x\n",
202 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_SEVERITY
, &sev
);
203 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_SYSERROR
, &syserr
);
204 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_EXCEPTION
, &exc
);
205 pci_err(pdev
, "RP PIO severity=%#010x, syserror=%#010x, exception=%#010x\n",
208 /* Get First Error Pointer */
209 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &dpc_status
);
210 first_error
= FIELD_GET(PCI_EXP_DPC_RP_PIO_FEP
, dpc_status
);
212 for (i
= 0; i
< ARRAY_SIZE(rp_pio_error_string
); i
++) {
213 if ((status
& ~mask
) & (1 << i
))
214 pci_err(pdev
, "[%2d] %s%s\n", i
, rp_pio_error_string
[i
],
215 first_error
== i
? " (First)" : "");
218 if (pdev
->dpc_rp_log_size
< 4)
220 pcie_read_tlp_log(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_HEADER_LOG
, &tlp_log
);
221 pci_err(pdev
, "TLP Header: %#010x %#010x %#010x %#010x\n",
222 tlp_log
.dw
[0], tlp_log
.dw
[1], tlp_log
.dw
[2], tlp_log
.dw
[3]);
224 if (pdev
->dpc_rp_log_size
< 5)
226 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_IMPSPEC_LOG
, &log
);
227 pci_err(pdev
, "RP PIO ImpSpec Log %#010x\n", log
);
229 for (i
= 0; i
< pdev
->dpc_rp_log_size
- 5; i
++) {
230 pci_read_config_dword(pdev
,
231 cap
+ PCI_EXP_DPC_RP_PIO_TLPPREFIX_LOG
+ i
* 4, &prefix
);
232 pci_err(pdev
, "TLP Prefix Header: dw%d, %#010x\n", i
, prefix
);
235 pci_write_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_STATUS
, status
);
238 static int dpc_get_aer_uncorrect_severity(struct pci_dev
*dev
,
239 struct aer_err_info
*info
)
241 int pos
= dev
->aer_cap
;
242 u32 status
, mask
, sev
;
244 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_STATUS
, &status
);
245 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_MASK
, &mask
);
250 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_SEVER
, &sev
);
253 info
->severity
= AER_FATAL
;
255 info
->severity
= AER_NONFATAL
;
260 void dpc_process_error(struct pci_dev
*pdev
)
262 u16 cap
= pdev
->dpc_cap
, status
, source
, reason
, ext_reason
;
263 struct aer_err_info info
;
265 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
266 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_SOURCE_ID
, &source
);
268 pci_info(pdev
, "containment event, status:%#06x source:%#06x\n",
271 reason
= status
& PCI_EXP_DPC_STATUS_TRIGGER_RSN
;
272 ext_reason
= status
& PCI_EXP_DPC_STATUS_TRIGGER_RSN_EXT
;
273 pci_warn(pdev
, "%s detected\n",
274 (reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_UNCOR
) ?
275 "unmasked uncorrectable error" :
276 (reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_NFE
) ?
278 (reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_FE
) ?
280 (ext_reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_RP_PIO
) ?
282 (ext_reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_SW_TRIGGER
) ?
286 /* show RP PIO error detail information */
287 if (pdev
->dpc_rp_extensions
&&
288 reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_IN_EXT
&&
289 ext_reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_RP_PIO
)
290 dpc_process_rp_pio_error(pdev
);
291 else if (reason
== PCI_EXP_DPC_STATUS_TRIGGER_RSN_UNCOR
&&
292 dpc_get_aer_uncorrect_severity(pdev
, &info
) &&
293 aer_get_device_error_info(pdev
, &info
)) {
294 aer_print_error(pdev
, &info
);
295 pci_aer_clear_nonfatal_status(pdev
);
296 pci_aer_clear_fatal_status(pdev
);
300 static void pci_clear_surpdn_errors(struct pci_dev
*pdev
)
302 if (pdev
->dpc_rp_extensions
)
303 pci_write_config_dword(pdev
, pdev
->dpc_cap
+
304 PCI_EXP_DPC_RP_PIO_STATUS
, ~0);
307 * In practice, Surprise Down errors have been observed to also set
308 * error bits in the Status Register as well as the Fatal Error
309 * Detected bit in the Device Status Register.
311 pci_write_config_word(pdev
, PCI_STATUS
, 0xffff);
313 pcie_capability_write_word(pdev
, PCI_EXP_DEVSTA
, PCI_EXP_DEVSTA_FED
);
316 static void dpc_handle_surprise_removal(struct pci_dev
*pdev
)
318 if (!pcie_wait_for_link(pdev
, false)) {
319 pci_info(pdev
, "Data Link Layer Link Active not cleared in 1000 msec\n");
323 if (pdev
->dpc_rp_extensions
&& dpc_wait_rp_inactive(pdev
))
326 pci_aer_raw_clear_status(pdev
);
327 pci_clear_surpdn_errors(pdev
);
329 pci_write_config_word(pdev
, pdev
->dpc_cap
+ PCI_EXP_DPC_STATUS
,
330 PCI_EXP_DPC_STATUS_TRIGGER
);
333 clear_bit(PCI_DPC_RECOVERED
, &pdev
->priv_flags
);
334 wake_up_all(&dpc_completed_waitqueue
);
337 static bool dpc_is_surprise_removal(struct pci_dev
*pdev
)
341 if (!pdev
->is_hotplug_bridge
)
344 if (pci_read_config_word(pdev
, pdev
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
348 return status
& PCI_ERR_UNC_SURPDN
;
351 static irqreturn_t
dpc_handler(int irq
, void *context
)
353 struct pci_dev
*pdev
= context
;
356 * According to PCIe r6.0 sec 6.7.6, errors are an expected side effect
357 * of async removal and should be ignored by software.
359 if (dpc_is_surprise_removal(pdev
)) {
360 dpc_handle_surprise_removal(pdev
);
364 dpc_process_error(pdev
);
366 /* We configure DPC so it only triggers on ERR_FATAL */
367 pcie_do_recovery(pdev
, pci_channel_io_frozen
, dpc_reset_link
);
372 static irqreturn_t
dpc_irq(int irq
, void *context
)
374 struct pci_dev
*pdev
= context
;
375 u16 cap
= pdev
->dpc_cap
, status
;
377 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
379 if (!(status
& PCI_EXP_DPC_STATUS_INTERRUPT
) || PCI_POSSIBLE_ERROR(status
))
382 pci_write_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
,
383 PCI_EXP_DPC_STATUS_INTERRUPT
);
384 if (status
& PCI_EXP_DPC_STATUS_TRIGGER
)
385 return IRQ_WAKE_THREAD
;
389 void pci_dpc_init(struct pci_dev
*pdev
)
393 pdev
->dpc_cap
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_DPC
);
397 pci_read_config_word(pdev
, pdev
->dpc_cap
+ PCI_EXP_DPC_CAP
, &cap
);
398 if (!(cap
& PCI_EXP_DPC_CAP_RP_EXT
))
401 pdev
->dpc_rp_extensions
= true;
403 /* Quirks may set dpc_rp_log_size if device or firmware is buggy */
404 if (!pdev
->dpc_rp_log_size
) {
405 pdev
->dpc_rp_log_size
=
406 FIELD_GET(PCI_EXP_DPC_RP_PIO_LOG_SIZE
, cap
);
407 if (pdev
->dpc_rp_log_size
< 4 || pdev
->dpc_rp_log_size
> 9) {
408 pci_err(pdev
, "RP PIO log size %u is invalid\n",
409 pdev
->dpc_rp_log_size
);
410 pdev
->dpc_rp_log_size
= 0;
415 static void dpc_enable(struct pcie_device
*dev
)
417 struct pci_dev
*pdev
= dev
->port
;
418 int dpc
= pdev
->dpc_cap
;
422 * Clear DPC Interrupt Status so we don't get an interrupt for an
423 * old event when setting DPC Interrupt Enable.
425 pci_write_config_word(pdev
, dpc
+ PCI_EXP_DPC_STATUS
,
426 PCI_EXP_DPC_STATUS_INTERRUPT
);
428 pci_read_config_word(pdev
, dpc
+ PCI_EXP_DPC_CTL
, &ctl
);
429 ctl
&= ~PCI_EXP_DPC_CTL_EN_MASK
;
430 ctl
|= PCI_EXP_DPC_CTL_EN_FATAL
| PCI_EXP_DPC_CTL_INT_EN
;
431 pci_write_config_word(pdev
, dpc
+ PCI_EXP_DPC_CTL
, ctl
);
434 static void dpc_disable(struct pcie_device
*dev
)
436 struct pci_dev
*pdev
= dev
->port
;
437 int dpc
= pdev
->dpc_cap
;
440 /* Disable DPC triggering and DPC interrupts */
441 pci_read_config_word(pdev
, dpc
+ PCI_EXP_DPC_CTL
, &ctl
);
442 ctl
&= ~(PCI_EXP_DPC_CTL_EN_FATAL
| PCI_EXP_DPC_CTL_INT_EN
);
443 pci_write_config_word(pdev
, dpc
+ PCI_EXP_DPC_CTL
, ctl
);
446 #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
447 static int dpc_probe(struct pcie_device
*dev
)
449 struct pci_dev
*pdev
= dev
->port
;
450 struct device
*device
= &dev
->device
;
454 if (!pcie_aer_is_native(pdev
) && !pcie_ports_dpc_native
)
457 status
= devm_request_threaded_irq(device
, dev
->irq
, dpc_irq
,
458 dpc_handler
, IRQF_SHARED
,
461 pci_warn(pdev
, "request IRQ%d failed: %d\n", dev
->irq
,
466 pci_read_config_word(pdev
, pdev
->dpc_cap
+ PCI_EXP_DPC_CAP
, &cap
);
469 pci_info(pdev
, "enabled with IRQ %d\n", dev
->irq
);
470 pci_info(pdev
, "error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
471 cap
& PCI_EXP_DPC_IRQ
, FLAG(cap
, PCI_EXP_DPC_CAP_RP_EXT
),
472 FLAG(cap
, PCI_EXP_DPC_CAP_POISONED_TLP
),
473 FLAG(cap
, PCI_EXP_DPC_CAP_SW_TRIGGER
), pdev
->dpc_rp_log_size
,
474 FLAG(cap
, PCI_EXP_DPC_CAP_DL_ACTIVE
));
476 pci_add_ext_cap_save_buffer(pdev
, PCI_EXT_CAP_ID_DPC
, sizeof(u16
));
480 static int dpc_suspend(struct pcie_device
*dev
)
486 static int dpc_resume(struct pcie_device
*dev
)
492 static void dpc_remove(struct pcie_device
*dev
)
497 static struct pcie_port_service_driver dpcdriver
= {
499 .port_type
= PCIE_ANY_PORT
,
500 .service
= PCIE_PORT_SERVICE_DPC
,
502 .suspend
= dpc_suspend
,
503 .resume
= dpc_resume
,
504 .remove
= dpc_remove
,
507 int __init
pcie_dpc_init(void)
509 return pcie_port_service_register(&dpcdriver
);