1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe AER software error injection support.
5 * Debuging PCIe AER code is quite difficult because it is hard to
6 * trigger various real hardware errors. Software based error
7 * injection can fake almost all kinds of errors with the help of a
8 * user space helper tool aer-inject, which can be gotten from:
9 * http://www.kernel.org/pub/linux/utils/pci/aer-inject/
11 * Copyright 2009 Intel Corporation.
12 * Huang Ying <ying.huang@intel.com>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/miscdevice.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/stddef.h>
23 #include <linux/device.h>
27 /* Override the existing corrected and uncorrected error masks */
28 static bool aer_mask_override
;
29 module_param(aer_mask_override
, bool, 0);
31 struct aer_error_inj
{
45 struct list_head list
;
62 struct list_head list
;
67 static LIST_HEAD(einjected
);
69 static LIST_HEAD(pci_bus_ops_list
);
71 /* Protect einjected and pci_bus_ops_list */
72 static DEFINE_SPINLOCK(inject_lock
);
74 static void aer_error_init(struct aer_error
*err
, u32 domain
,
75 unsigned int bus
, unsigned int devfn
,
78 INIT_LIST_HEAD(&err
->list
);
82 err
->pos_cap_err
= pos_cap_err
;
85 /* inject_lock must be held before calling */
86 static struct aer_error
*__find_aer_error(u32 domain
, unsigned int bus
,
89 struct aer_error
*err
;
91 list_for_each_entry(err
, &einjected
, list
) {
92 if (domain
== err
->domain
&&
100 /* inject_lock must be held before calling */
101 static struct aer_error
*__find_aer_error_by_dev(struct pci_dev
*dev
)
103 int domain
= pci_domain_nr(dev
->bus
);
106 return __find_aer_error(domain
, dev
->bus
->number
, dev
->devfn
);
109 /* inject_lock must be held before calling */
110 static struct pci_ops
*__find_pci_bus_ops(struct pci_bus
*bus
)
112 struct pci_bus_ops
*bus_ops
;
114 list_for_each_entry(bus_ops
, &pci_bus_ops_list
, list
) {
115 if (bus_ops
->bus
== bus
)
121 static struct pci_bus_ops
*pci_bus_ops_pop(void)
124 struct pci_bus_ops
*bus_ops
;
126 spin_lock_irqsave(&inject_lock
, flags
);
127 bus_ops
= list_first_entry_or_null(&pci_bus_ops_list
,
128 struct pci_bus_ops
, list
);
130 list_del(&bus_ops
->list
);
131 spin_unlock_irqrestore(&inject_lock
, flags
);
135 static u32
*find_pci_config_dword(struct aer_error
*err
, int where
,
141 if (err
->pos_cap_err
== -1)
144 switch (where
- err
->pos_cap_err
) {
145 case PCI_ERR_UNCOR_STATUS
:
146 target
= &err
->uncor_status
;
149 case PCI_ERR_COR_STATUS
:
150 target
= &err
->cor_status
;
153 case PCI_ERR_HEADER_LOG
:
154 target
= &err
->header_log0
;
156 case PCI_ERR_HEADER_LOG
+4:
157 target
= &err
->header_log1
;
159 case PCI_ERR_HEADER_LOG
+8:
160 target
= &err
->header_log2
;
162 case PCI_ERR_HEADER_LOG
+12:
163 target
= &err
->header_log3
;
165 case PCI_ERR_ROOT_STATUS
:
166 target
= &err
->root_status
;
169 case PCI_ERR_ROOT_ERR_SRC
:
170 target
= &err
->source_id
;
178 static int aer_inj_read_config(struct pci_bus
*bus
, unsigned int devfn
,
179 int where
, int size
, u32
*val
)
182 struct aer_error
*err
;
185 struct pci_ops
*my_ops
;
189 spin_lock_irqsave(&inject_lock
, flags
);
190 if (size
!= sizeof(u32
))
192 domain
= pci_domain_nr(bus
);
195 err
= __find_aer_error(domain
, bus
->number
, devfn
);
199 sim
= find_pci_config_dword(err
, where
, NULL
);
202 spin_unlock_irqrestore(&inject_lock
, flags
);
206 ops
= __find_pci_bus_ops(bus
);
208 * pci_lock must already be held, so we can directly
209 * manipulate bus->ops. Many config access functions,
210 * including pci_generic_config_read() require the original
211 * bus->ops be installed to function, so temporarily put them
216 rv
= ops
->read(bus
, devfn
, where
, size
, val
);
218 spin_unlock_irqrestore(&inject_lock
, flags
);
222 static int aer_inj_write_config(struct pci_bus
*bus
, unsigned int devfn
,
223 int where
, int size
, u32 val
)
226 struct aer_error
*err
;
230 struct pci_ops
*my_ops
;
234 spin_lock_irqsave(&inject_lock
, flags
);
235 if (size
!= sizeof(u32
))
237 domain
= pci_domain_nr(bus
);
240 err
= __find_aer_error(domain
, bus
->number
, devfn
);
244 sim
= find_pci_config_dword(err
, where
, &rw1cs
);
250 spin_unlock_irqrestore(&inject_lock
, flags
);
254 ops
= __find_pci_bus_ops(bus
);
256 * pci_lock must already be held, so we can directly
257 * manipulate bus->ops. Many config access functions,
258 * including pci_generic_config_write() require the original
259 * bus->ops be installed to function, so temporarily put them
264 rv
= ops
->write(bus
, devfn
, where
, size
, val
);
266 spin_unlock_irqrestore(&inject_lock
, flags
);
270 static struct pci_ops aer_inj_pci_ops
= {
271 .read
= aer_inj_read_config
,
272 .write
= aer_inj_write_config
,
275 static void pci_bus_ops_init(struct pci_bus_ops
*bus_ops
,
279 INIT_LIST_HEAD(&bus_ops
->list
);
284 static int pci_bus_set_aer_ops(struct pci_bus
*bus
)
287 struct pci_bus_ops
*bus_ops
;
290 bus_ops
= kmalloc(sizeof(*bus_ops
), GFP_KERNEL
);
293 ops
= pci_bus_set_ops(bus
, &aer_inj_pci_ops
);
294 spin_lock_irqsave(&inject_lock
, flags
);
295 if (ops
== &aer_inj_pci_ops
)
297 pci_bus_ops_init(bus_ops
, bus
, ops
);
298 list_add(&bus_ops
->list
, &pci_bus_ops_list
);
301 spin_unlock_irqrestore(&inject_lock
, flags
);
306 static int find_aer_device_iter(struct device
*device
, void *data
)
308 struct pcie_device
**result
= data
;
309 struct pcie_device
*pcie_dev
;
311 if (device
->bus
== &pcie_port_bus_type
) {
312 pcie_dev
= to_pcie_device(device
);
313 if (pcie_dev
->service
& PCIE_PORT_SERVICE_AER
) {
321 static int find_aer_device(struct pci_dev
*dev
, struct pcie_device
**result
)
323 return device_for_each_child(&dev
->dev
, result
, find_aer_device_iter
);
326 static int aer_inject(struct aer_error_inj
*einj
)
328 struct aer_error
*err
, *rperr
;
329 struct aer_error
*err_alloc
= NULL
, *rperr_alloc
= NULL
;
330 struct pci_dev
*dev
, *rpdev
;
331 struct pcie_device
*edev
;
333 unsigned int devfn
= PCI_DEVFN(einj
->dev
, einj
->fn
);
334 int pos_cap_err
, rp_pos_cap_err
;
335 u32 sever
, cor_mask
, uncor_mask
, cor_mask_orig
= 0, uncor_mask_orig
= 0;
338 dev
= pci_get_domain_bus_and_slot(einj
->domain
, einj
->bus
, devfn
);
341 rpdev
= pcie_find_root_port(dev
);
343 pci_err(dev
, "aer_inject: Root port not found\n");
348 pos_cap_err
= dev
->aer_cap
;
350 pci_err(dev
, "aer_inject: Device doesn't support AER\n");
351 ret
= -EPROTONOSUPPORT
;
354 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_SEVER
, &sever
);
355 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_COR_MASK
, &cor_mask
);
356 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_MASK
,
359 rp_pos_cap_err
= rpdev
->aer_cap
;
360 if (!rp_pos_cap_err
) {
361 pci_err(rpdev
, "aer_inject: Root port doesn't support AER\n");
362 ret
= -EPROTONOSUPPORT
;
366 err_alloc
= kzalloc(sizeof(struct aer_error
), GFP_KERNEL
);
371 rperr_alloc
= kzalloc(sizeof(struct aer_error
), GFP_KERNEL
);
377 if (aer_mask_override
) {
378 cor_mask_orig
= cor_mask
;
379 cor_mask
&= !(einj
->cor_status
);
380 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_COR_MASK
,
383 uncor_mask_orig
= uncor_mask
;
384 uncor_mask
&= !(einj
->uncor_status
);
385 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_MASK
,
389 spin_lock_irqsave(&inject_lock
, flags
);
391 err
= __find_aer_error_by_dev(dev
);
395 aer_error_init(err
, einj
->domain
, einj
->bus
, devfn
,
397 list_add(&err
->list
, &einjected
);
399 err
->uncor_status
|= einj
->uncor_status
;
400 err
->cor_status
|= einj
->cor_status
;
401 err
->header_log0
= einj
->header_log0
;
402 err
->header_log1
= einj
->header_log1
;
403 err
->header_log2
= einj
->header_log2
;
404 err
->header_log3
= einj
->header_log3
;
406 if (!aer_mask_override
&& einj
->cor_status
&&
407 !(einj
->cor_status
& ~cor_mask
)) {
409 pci_warn(dev
, "aer_inject: The correctable error(s) is masked by device\n");
410 spin_unlock_irqrestore(&inject_lock
, flags
);
413 if (!aer_mask_override
&& einj
->uncor_status
&&
414 !(einj
->uncor_status
& ~uncor_mask
)) {
416 pci_warn(dev
, "aer_inject: The uncorrectable error(s) is masked by device\n");
417 spin_unlock_irqrestore(&inject_lock
, flags
);
421 rperr
= __find_aer_error_by_dev(rpdev
);
425 aer_error_init(rperr
, pci_domain_nr(rpdev
->bus
),
426 rpdev
->bus
->number
, rpdev
->devfn
,
428 list_add(&rperr
->list
, &einjected
);
430 if (einj
->cor_status
) {
431 if (rperr
->root_status
& PCI_ERR_ROOT_COR_RCV
)
432 rperr
->root_status
|= PCI_ERR_ROOT_MULTI_COR_RCV
;
434 rperr
->root_status
|= PCI_ERR_ROOT_COR_RCV
;
435 rperr
->source_id
&= 0xffff0000;
436 rperr
->source_id
|= (einj
->bus
<< 8) | devfn
;
438 if (einj
->uncor_status
) {
439 if (rperr
->root_status
& PCI_ERR_ROOT_UNCOR_RCV
)
440 rperr
->root_status
|= PCI_ERR_ROOT_MULTI_UNCOR_RCV
;
441 if (sever
& einj
->uncor_status
) {
442 rperr
->root_status
|= PCI_ERR_ROOT_FATAL_RCV
;
443 if (!(rperr
->root_status
& PCI_ERR_ROOT_UNCOR_RCV
))
444 rperr
->root_status
|= PCI_ERR_ROOT_FIRST_FATAL
;
446 rperr
->root_status
|= PCI_ERR_ROOT_NONFATAL_RCV
;
447 rperr
->root_status
|= PCI_ERR_ROOT_UNCOR_RCV
;
448 rperr
->source_id
&= 0x0000ffff;
449 rperr
->source_id
|= ((einj
->bus
<< 8) | devfn
) << 16;
451 spin_unlock_irqrestore(&inject_lock
, flags
);
453 if (aer_mask_override
) {
454 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_COR_MASK
,
456 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_MASK
,
460 ret
= pci_bus_set_aer_ops(dev
->bus
);
463 ret
= pci_bus_set_aer_ops(rpdev
->bus
);
467 if (find_aer_device(rpdev
, &edev
)) {
468 if (!get_service_data(edev
)) {
469 dev_warn(&edev
->device
,
470 "aer_inject: AER service is not initialized\n");
471 ret
= -EPROTONOSUPPORT
;
474 dev_info(&edev
->device
,
475 "aer_inject: Injecting errors %08x/%08x into device %s\n",
476 einj
->cor_status
, einj
->uncor_status
, pci_name(dev
));
479 pci_err(rpdev
, "aer_inject: AER device not found\n");
489 static ssize_t
aer_inject_write(struct file
*filp
, const char __user
*ubuf
,
490 size_t usize
, loff_t
*off
)
492 struct aer_error_inj einj
;
495 if (!capable(CAP_SYS_ADMIN
))
497 if (usize
< offsetof(struct aer_error_inj
, domain
) ||
498 usize
> sizeof(einj
))
501 memset(&einj
, 0, sizeof(einj
));
502 if (copy_from_user(&einj
, ubuf
, usize
))
505 ret
= aer_inject(&einj
);
506 return ret
? ret
: usize
;
509 static const struct file_operations aer_inject_fops
= {
510 .write
= aer_inject_write
,
511 .owner
= THIS_MODULE
,
512 .llseek
= noop_llseek
,
515 static struct miscdevice aer_inject_device
= {
516 .minor
= MISC_DYNAMIC_MINOR
,
517 .name
= "aer_inject",
518 .fops
= &aer_inject_fops
,
521 static int __init
aer_inject_init(void)
523 return misc_register(&aer_inject_device
);
526 static void __exit
aer_inject_exit(void)
528 struct aer_error
*err
, *err_next
;
530 struct pci_bus_ops
*bus_ops
;
532 misc_deregister(&aer_inject_device
);
534 while ((bus_ops
= pci_bus_ops_pop())) {
535 pci_bus_set_ops(bus_ops
->bus
, bus_ops
->ops
);
539 spin_lock_irqsave(&inject_lock
, flags
);
540 list_for_each_entry_safe(err
, err_next
, &einjected
, list
) {
541 list_del(&err
->list
);
544 spin_unlock_irqrestore(&inject_lock
, flags
);
547 module_init(aer_inject_init
);
548 module_exit(aer_inject_exit
);
550 MODULE_DESCRIPTION("PCIe AER software error injector");
551 MODULE_LICENSE("GPL");