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/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/init.h>
15 #include <linux/pci.h>
21 struct pcie_device
*dev
;
27 static const char * const rp_pio_error_string
[] = {
28 "Configuration Request received UR Completion", /* Bit Position 0 */
29 "Configuration Request received CA Completion", /* Bit Position 1 */
30 "Configuration Request Completion Timeout", /* Bit Position 2 */
36 "I/O Request received UR Completion", /* Bit Position 8 */
37 "I/O Request received CA Completion", /* Bit Position 9 */
38 "I/O Request Completion Timeout", /* Bit Position 10 */
44 "Memory Request received UR Completion", /* Bit Position 16 */
45 "Memory Request received CA Completion", /* Bit Position 17 */
46 "Memory Request Completion Timeout", /* Bit Position 18 */
49 static struct dpc_dev
*to_dpc_dev(struct pci_dev
*dev
)
51 struct device
*device
;
53 device
= pcie_port_find_device(dev
, PCIE_PORT_SERVICE_DPC
);
56 return get_service_data(to_pcie_device(device
));
59 void pci_save_dpc_state(struct pci_dev
*dev
)
62 struct pci_cap_saved_state
*save_state
;
65 if (!pci_is_pcie(dev
))
68 dpc
= to_dpc_dev(dev
);
72 save_state
= pci_find_saved_ext_cap(dev
, PCI_EXT_CAP_ID_DPC
);
76 cap
= (u16
*)&save_state
->cap
.data
[0];
77 pci_read_config_word(dev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, cap
);
80 void pci_restore_dpc_state(struct pci_dev
*dev
)
83 struct pci_cap_saved_state
*save_state
;
86 if (!pci_is_pcie(dev
))
89 dpc
= to_dpc_dev(dev
);
93 save_state
= pci_find_saved_ext_cap(dev
, PCI_EXT_CAP_ID_DPC
);
97 cap
= (u16
*)&save_state
->cap
.data
[0];
98 pci_write_config_word(dev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, *cap
);
101 static int dpc_wait_rp_inactive(struct dpc_dev
*dpc
)
103 unsigned long timeout
= jiffies
+ HZ
;
104 struct pci_dev
*pdev
= dpc
->dev
->port
;
105 u16 cap
= dpc
->cap_pos
, status
;
107 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
108 while (status
& PCI_EXP_DPC_RP_BUSY
&&
109 !time_after(jiffies
, timeout
)) {
111 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
113 if (status
& PCI_EXP_DPC_RP_BUSY
) {
114 pci_warn(pdev
, "root port still busy\n");
120 static pci_ers_result_t
dpc_reset_link(struct pci_dev
*pdev
)
126 * DPC disables the Link automatically in hardware, so it has
127 * already been reset by the time we get here.
129 dpc
= to_dpc_dev(pdev
);
133 * Wait until the Link is inactive, then clear DPC Trigger Status
134 * to allow the Port to leave DPC.
136 pcie_wait_for_link(pdev
, false);
138 if (dpc
->rp_extensions
&& dpc_wait_rp_inactive(dpc
))
139 return PCI_ERS_RESULT_DISCONNECT
;
141 pci_write_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
,
142 PCI_EXP_DPC_STATUS_TRIGGER
);
144 if (!pcie_wait_for_link(pdev
, true))
145 return PCI_ERS_RESULT_DISCONNECT
;
147 return PCI_ERS_RESULT_RECOVERED
;
150 static void dpc_process_rp_pio_error(struct dpc_dev
*dpc
)
152 struct pci_dev
*pdev
= dpc
->dev
->port
;
153 u16 cap
= dpc
->cap_pos
, dpc_status
, first_error
;
154 u32 status
, mask
, sev
, syserr
, exc
, dw0
, dw1
, dw2
, dw3
, log
, prefix
;
157 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_STATUS
, &status
);
158 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_MASK
, &mask
);
159 pci_err(pdev
, "rp_pio_status: %#010x, rp_pio_mask: %#010x\n",
162 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_SEVERITY
, &sev
);
163 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_SYSERROR
, &syserr
);
164 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_EXCEPTION
, &exc
);
165 pci_err(pdev
, "RP PIO severity=%#010x, syserror=%#010x, exception=%#010x\n",
168 /* Get First Error Pointer */
169 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &dpc_status
);
170 first_error
= (dpc_status
& 0x1f00) >> 8;
172 for (i
= 0; i
< ARRAY_SIZE(rp_pio_error_string
); i
++) {
173 if ((status
& ~mask
) & (1 << i
))
174 pci_err(pdev
, "[%2d] %s%s\n", i
, rp_pio_error_string
[i
],
175 first_error
== i
? " (First)" : "");
178 if (dpc
->rp_log_size
< 4)
180 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_HEADER_LOG
,
182 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_HEADER_LOG
+ 4,
184 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_HEADER_LOG
+ 8,
186 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_HEADER_LOG
+ 12,
188 pci_err(pdev
, "TLP Header: %#010x %#010x %#010x %#010x\n",
191 if (dpc
->rp_log_size
< 5)
193 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_IMPSPEC_LOG
, &log
);
194 pci_err(pdev
, "RP PIO ImpSpec Log %#010x\n", log
);
196 for (i
= 0; i
< dpc
->rp_log_size
- 5; i
++) {
197 pci_read_config_dword(pdev
,
198 cap
+ PCI_EXP_DPC_RP_PIO_TLPPREFIX_LOG
, &prefix
);
199 pci_err(pdev
, "TLP Prefix Header: dw%d, %#010x\n", i
, prefix
);
202 pci_write_config_dword(pdev
, cap
+ PCI_EXP_DPC_RP_PIO_STATUS
, status
);
205 static int dpc_get_aer_uncorrect_severity(struct pci_dev
*dev
,
206 struct aer_err_info
*info
)
208 int pos
= dev
->aer_cap
;
209 u32 status
, mask
, sev
;
211 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_STATUS
, &status
);
212 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_MASK
, &mask
);
217 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_SEVER
, &sev
);
220 info
->severity
= AER_FATAL
;
222 info
->severity
= AER_NONFATAL
;
227 static irqreturn_t
dpc_handler(int irq
, void *context
)
229 struct aer_err_info info
;
230 struct dpc_dev
*dpc
= context
;
231 struct pci_dev
*pdev
= dpc
->dev
->port
;
232 u16 cap
= dpc
->cap_pos
, status
, source
, reason
, ext_reason
;
234 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
235 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_SOURCE_ID
, &source
);
237 pci_info(pdev
, "containment event, status:%#06x source:%#06x\n",
240 reason
= (status
& PCI_EXP_DPC_STATUS_TRIGGER_RSN
) >> 1;
241 ext_reason
= (status
& PCI_EXP_DPC_STATUS_TRIGGER_RSN_EXT
) >> 5;
242 pci_warn(pdev
, "%s detected\n",
243 (reason
== 0) ? "unmasked uncorrectable error" :
244 (reason
== 1) ? "ERR_NONFATAL" :
245 (reason
== 2) ? "ERR_FATAL" :
246 (ext_reason
== 0) ? "RP PIO error" :
247 (ext_reason
== 1) ? "software trigger" :
250 /* show RP PIO error detail information */
251 if (dpc
->rp_extensions
&& reason
== 3 && ext_reason
== 0)
252 dpc_process_rp_pio_error(dpc
);
253 else if (reason
== 0 &&
254 dpc_get_aer_uncorrect_severity(pdev
, &info
) &&
255 aer_get_device_error_info(pdev
, &info
)) {
256 aer_print_error(pdev
, &info
);
257 pci_cleanup_aer_uncorrect_error_status(pdev
);
258 pci_aer_clear_fatal_status(pdev
);
261 /* We configure DPC so it only triggers on ERR_FATAL */
262 pcie_do_recovery(pdev
, pci_channel_io_frozen
, PCIE_PORT_SERVICE_DPC
);
267 static irqreturn_t
dpc_irq(int irq
, void *context
)
269 struct dpc_dev
*dpc
= (struct dpc_dev
*)context
;
270 struct pci_dev
*pdev
= dpc
->dev
->port
;
271 u16 cap
= dpc
->cap_pos
, status
;
273 pci_read_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
, &status
);
275 if (!(status
& PCI_EXP_DPC_STATUS_INTERRUPT
) || status
== (u16
)(~0))
278 pci_write_config_word(pdev
, cap
+ PCI_EXP_DPC_STATUS
,
279 PCI_EXP_DPC_STATUS_INTERRUPT
);
280 if (status
& PCI_EXP_DPC_STATUS_TRIGGER
)
281 return IRQ_WAKE_THREAD
;
285 #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
286 static int dpc_probe(struct pcie_device
*dev
)
289 struct pci_dev
*pdev
= dev
->port
;
290 struct device
*device
= &dev
->device
;
294 if (pcie_aer_get_firmware_first(pdev
) && !pcie_ports_dpc_native
)
297 dpc
= devm_kzalloc(device
, sizeof(*dpc
), GFP_KERNEL
);
301 dpc
->cap_pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_DPC
);
303 set_service_data(dev
, dpc
);
305 status
= devm_request_threaded_irq(device
, dev
->irq
, dpc_irq
,
306 dpc_handler
, IRQF_SHARED
,
309 pci_warn(pdev
, "request IRQ%d failed: %d\n", dev
->irq
,
314 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CAP
, &cap
);
315 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, &ctl
);
317 dpc
->rp_extensions
= (cap
& PCI_EXP_DPC_CAP_RP_EXT
);
318 if (dpc
->rp_extensions
) {
319 dpc
->rp_log_size
= (cap
& PCI_EXP_DPC_RP_PIO_LOG_SIZE
) >> 8;
320 if (dpc
->rp_log_size
< 4 || dpc
->rp_log_size
> 9) {
321 pci_err(pdev
, "RP PIO log size %u is invalid\n",
323 dpc
->rp_log_size
= 0;
327 ctl
= (ctl
& 0xfff4) | PCI_EXP_DPC_CTL_EN_FATAL
| PCI_EXP_DPC_CTL_INT_EN
;
328 pci_write_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, ctl
);
330 pci_info(pdev
, "error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
331 cap
& PCI_EXP_DPC_IRQ
, FLAG(cap
, PCI_EXP_DPC_CAP_RP_EXT
),
332 FLAG(cap
, PCI_EXP_DPC_CAP_POISONED_TLP
),
333 FLAG(cap
, PCI_EXP_DPC_CAP_SW_TRIGGER
), dpc
->rp_log_size
,
334 FLAG(cap
, PCI_EXP_DPC_CAP_DL_ACTIVE
));
336 pci_add_ext_cap_save_buffer(pdev
, PCI_EXT_CAP_ID_DPC
, sizeof(u16
));
340 static void dpc_remove(struct pcie_device
*dev
)
342 struct dpc_dev
*dpc
= get_service_data(dev
);
343 struct pci_dev
*pdev
= dev
->port
;
346 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, &ctl
);
347 ctl
&= ~(PCI_EXP_DPC_CTL_EN_FATAL
| PCI_EXP_DPC_CTL_INT_EN
);
348 pci_write_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, ctl
);
351 static struct pcie_port_service_driver dpcdriver
= {
353 .port_type
= PCIE_ANY_PORT
,
354 .service
= PCIE_PORT_SERVICE_DPC
,
356 .remove
= dpc_remove
,
357 .reset_link
= dpc_reset_link
,
360 int __init
pcie_dpc_init(void)
362 return pcie_port_service_register(&dpcdriver
);