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/irq.h>
18 #include <linux/miscdevice.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/stddef.h>
24 #include <linux/device.h>
28 /* Override the existing corrected and uncorrected error masks */
29 static bool aer_mask_override
;
30 module_param(aer_mask_override
, bool, 0);
32 struct aer_error_inj
{
46 struct list_head list
;
63 struct list_head list
;
68 static LIST_HEAD(einjected
);
70 static LIST_HEAD(pci_bus_ops_list
);
72 /* Protect einjected and pci_bus_ops_list */
73 static DEFINE_SPINLOCK(inject_lock
);
75 static void aer_error_init(struct aer_error
*err
, u32 domain
,
76 unsigned int bus
, unsigned int devfn
,
79 INIT_LIST_HEAD(&err
->list
);
83 err
->pos_cap_err
= pos_cap_err
;
86 /* inject_lock must be held before calling */
87 static struct aer_error
*__find_aer_error(u32 domain
, unsigned int bus
,
90 struct aer_error
*err
;
92 list_for_each_entry(err
, &einjected
, list
) {
93 if (domain
== err
->domain
&&
101 /* inject_lock must be held before calling */
102 static struct aer_error
*__find_aer_error_by_dev(struct pci_dev
*dev
)
104 int domain
= pci_domain_nr(dev
->bus
);
107 return __find_aer_error(domain
, dev
->bus
->number
, dev
->devfn
);
110 /* inject_lock must be held before calling */
111 static struct pci_ops
*__find_pci_bus_ops(struct pci_bus
*bus
)
113 struct pci_bus_ops
*bus_ops
;
115 list_for_each_entry(bus_ops
, &pci_bus_ops_list
, list
) {
116 if (bus_ops
->bus
== bus
)
122 static struct pci_bus_ops
*pci_bus_ops_pop(void)
125 struct pci_bus_ops
*bus_ops
;
127 spin_lock_irqsave(&inject_lock
, flags
);
128 bus_ops
= list_first_entry_or_null(&pci_bus_ops_list
,
129 struct pci_bus_ops
, list
);
131 list_del(&bus_ops
->list
);
132 spin_unlock_irqrestore(&inject_lock
, flags
);
136 static u32
*find_pci_config_dword(struct aer_error
*err
, int where
,
142 if (err
->pos_cap_err
== -1)
145 switch (where
- err
->pos_cap_err
) {
146 case PCI_ERR_UNCOR_STATUS
:
147 target
= &err
->uncor_status
;
150 case PCI_ERR_COR_STATUS
:
151 target
= &err
->cor_status
;
154 case PCI_ERR_HEADER_LOG
:
155 target
= &err
->header_log0
;
157 case PCI_ERR_HEADER_LOG
+4:
158 target
= &err
->header_log1
;
160 case PCI_ERR_HEADER_LOG
+8:
161 target
= &err
->header_log2
;
163 case PCI_ERR_HEADER_LOG
+12:
164 target
= &err
->header_log3
;
166 case PCI_ERR_ROOT_STATUS
:
167 target
= &err
->root_status
;
170 case PCI_ERR_ROOT_ERR_SRC
:
171 target
= &err
->source_id
;
179 static int aer_inj_read(struct pci_bus
*bus
, unsigned int devfn
, int where
,
182 struct pci_ops
*ops
, *my_ops
;
185 ops
= __find_pci_bus_ops(bus
);
191 rv
= ops
->read(bus
, devfn
, where
, size
, val
);
197 static int aer_inj_write(struct pci_bus
*bus
, unsigned int devfn
, int where
,
200 struct pci_ops
*ops
, *my_ops
;
203 ops
= __find_pci_bus_ops(bus
);
209 rv
= ops
->write(bus
, devfn
, where
, size
, val
);
215 static int aer_inj_read_config(struct pci_bus
*bus
, unsigned int devfn
,
216 int where
, int size
, u32
*val
)
219 struct aer_error
*err
;
224 spin_lock_irqsave(&inject_lock
, flags
);
225 if (size
!= sizeof(u32
))
227 domain
= pci_domain_nr(bus
);
230 err
= __find_aer_error(domain
, bus
->number
, devfn
);
234 sim
= find_pci_config_dword(err
, where
, NULL
);
237 spin_unlock_irqrestore(&inject_lock
, flags
);
241 rv
= aer_inj_read(bus
, devfn
, where
, size
, val
);
242 spin_unlock_irqrestore(&inject_lock
, flags
);
246 static int aer_inj_write_config(struct pci_bus
*bus
, unsigned int devfn
,
247 int where
, int size
, u32 val
)
250 struct aer_error
*err
;
256 spin_lock_irqsave(&inject_lock
, flags
);
257 if (size
!= sizeof(u32
))
259 domain
= pci_domain_nr(bus
);
262 err
= __find_aer_error(domain
, bus
->number
, devfn
);
266 sim
= find_pci_config_dword(err
, where
, &rw1cs
);
272 spin_unlock_irqrestore(&inject_lock
, flags
);
276 rv
= aer_inj_write(bus
, devfn
, where
, size
, val
);
277 spin_unlock_irqrestore(&inject_lock
, flags
);
281 static struct pci_ops aer_inj_pci_ops
= {
282 .read
= aer_inj_read_config
,
283 .write
= aer_inj_write_config
,
286 static void pci_bus_ops_init(struct pci_bus_ops
*bus_ops
,
290 INIT_LIST_HEAD(&bus_ops
->list
);
295 static int pci_bus_set_aer_ops(struct pci_bus
*bus
)
298 struct pci_bus_ops
*bus_ops
;
301 bus_ops
= kmalloc(sizeof(*bus_ops
), GFP_KERNEL
);
304 ops
= pci_bus_set_ops(bus
, &aer_inj_pci_ops
);
305 spin_lock_irqsave(&inject_lock
, flags
);
306 if (ops
== &aer_inj_pci_ops
)
308 pci_bus_ops_init(bus_ops
, bus
, ops
);
309 list_add(&bus_ops
->list
, &pci_bus_ops_list
);
312 spin_unlock_irqrestore(&inject_lock
, flags
);
317 static int aer_inject(struct aer_error_inj
*einj
)
319 struct aer_error
*err
, *rperr
;
320 struct aer_error
*err_alloc
= NULL
, *rperr_alloc
= NULL
;
321 struct pci_dev
*dev
, *rpdev
;
322 struct pcie_device
*edev
;
323 struct device
*device
;
325 unsigned int devfn
= PCI_DEVFN(einj
->dev
, einj
->fn
);
326 int pos_cap_err
, rp_pos_cap_err
;
327 u32 sever
, cor_mask
, uncor_mask
, cor_mask_orig
= 0, uncor_mask_orig
= 0;
330 dev
= pci_get_domain_bus_and_slot(einj
->domain
, einj
->bus
, devfn
);
333 rpdev
= pcie_find_root_port(dev
);
335 pci_err(dev
, "aer_inject: Root port not found\n");
340 pos_cap_err
= dev
->aer_cap
;
342 pci_err(dev
, "aer_inject: Device doesn't support AER\n");
343 ret
= -EPROTONOSUPPORT
;
346 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_SEVER
, &sever
);
347 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_COR_MASK
, &cor_mask
);
348 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_MASK
,
351 rp_pos_cap_err
= rpdev
->aer_cap
;
352 if (!rp_pos_cap_err
) {
353 pci_err(rpdev
, "aer_inject: Root port doesn't support AER\n");
354 ret
= -EPROTONOSUPPORT
;
358 err_alloc
= kzalloc(sizeof(struct aer_error
), GFP_KERNEL
);
363 rperr_alloc
= kzalloc(sizeof(struct aer_error
), GFP_KERNEL
);
369 if (aer_mask_override
) {
370 cor_mask_orig
= cor_mask
;
371 cor_mask
&= !(einj
->cor_status
);
372 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_COR_MASK
,
375 uncor_mask_orig
= uncor_mask
;
376 uncor_mask
&= !(einj
->uncor_status
);
377 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_MASK
,
381 spin_lock_irqsave(&inject_lock
, flags
);
383 err
= __find_aer_error_by_dev(dev
);
387 aer_error_init(err
, einj
->domain
, einj
->bus
, devfn
,
389 list_add(&err
->list
, &einjected
);
391 err
->uncor_status
|= einj
->uncor_status
;
392 err
->cor_status
|= einj
->cor_status
;
393 err
->header_log0
= einj
->header_log0
;
394 err
->header_log1
= einj
->header_log1
;
395 err
->header_log2
= einj
->header_log2
;
396 err
->header_log3
= einj
->header_log3
;
398 if (!aer_mask_override
&& einj
->cor_status
&&
399 !(einj
->cor_status
& ~cor_mask
)) {
401 pci_warn(dev
, "aer_inject: The correctable error(s) is masked by device\n");
402 spin_unlock_irqrestore(&inject_lock
, flags
);
405 if (!aer_mask_override
&& einj
->uncor_status
&&
406 !(einj
->uncor_status
& ~uncor_mask
)) {
408 pci_warn(dev
, "aer_inject: The uncorrectable error(s) is masked by device\n");
409 spin_unlock_irqrestore(&inject_lock
, flags
);
413 rperr
= __find_aer_error_by_dev(rpdev
);
417 aer_error_init(rperr
, pci_domain_nr(rpdev
->bus
),
418 rpdev
->bus
->number
, rpdev
->devfn
,
420 list_add(&rperr
->list
, &einjected
);
422 if (einj
->cor_status
) {
423 if (rperr
->root_status
& PCI_ERR_ROOT_COR_RCV
)
424 rperr
->root_status
|= PCI_ERR_ROOT_MULTI_COR_RCV
;
426 rperr
->root_status
|= PCI_ERR_ROOT_COR_RCV
;
427 rperr
->source_id
&= 0xffff0000;
428 rperr
->source_id
|= (einj
->bus
<< 8) | devfn
;
430 if (einj
->uncor_status
) {
431 if (rperr
->root_status
& PCI_ERR_ROOT_UNCOR_RCV
)
432 rperr
->root_status
|= PCI_ERR_ROOT_MULTI_UNCOR_RCV
;
433 if (sever
& einj
->uncor_status
) {
434 rperr
->root_status
|= PCI_ERR_ROOT_FATAL_RCV
;
435 if (!(rperr
->root_status
& PCI_ERR_ROOT_UNCOR_RCV
))
436 rperr
->root_status
|= PCI_ERR_ROOT_FIRST_FATAL
;
438 rperr
->root_status
|= PCI_ERR_ROOT_NONFATAL_RCV
;
439 rperr
->root_status
|= PCI_ERR_ROOT_UNCOR_RCV
;
440 rperr
->source_id
&= 0x0000ffff;
441 rperr
->source_id
|= ((einj
->bus
<< 8) | devfn
) << 16;
443 spin_unlock_irqrestore(&inject_lock
, flags
);
445 if (aer_mask_override
) {
446 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_COR_MASK
,
448 pci_write_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_MASK
,
452 ret
= pci_bus_set_aer_ops(dev
->bus
);
455 ret
= pci_bus_set_aer_ops(rpdev
->bus
);
459 device
= pcie_port_find_device(rpdev
, PCIE_PORT_SERVICE_AER
);
461 edev
= to_pcie_device(device
);
462 if (!get_service_data(edev
)) {
463 dev_warn(&edev
->device
,
464 "aer_inject: AER service is not initialized\n");
465 ret
= -EPROTONOSUPPORT
;
468 dev_info(&edev
->device
,
469 "aer_inject: Injecting errors %08x/%08x into device %s\n",
470 einj
->cor_status
, einj
->uncor_status
, pci_name(dev
));
472 generic_handle_irq(edev
->irq
);
475 pci_err(rpdev
, "aer_inject: AER device not found\n");
485 static ssize_t
aer_inject_write(struct file
*filp
, const char __user
*ubuf
,
486 size_t usize
, loff_t
*off
)
488 struct aer_error_inj einj
;
491 if (!capable(CAP_SYS_ADMIN
))
493 if (usize
< offsetof(struct aer_error_inj
, domain
) ||
494 usize
> sizeof(einj
))
497 memset(&einj
, 0, sizeof(einj
));
498 if (copy_from_user(&einj
, ubuf
, usize
))
501 ret
= aer_inject(&einj
);
502 return ret
? ret
: usize
;
505 static const struct file_operations aer_inject_fops
= {
506 .write
= aer_inject_write
,
507 .owner
= THIS_MODULE
,
508 .llseek
= noop_llseek
,
511 static struct miscdevice aer_inject_device
= {
512 .minor
= MISC_DYNAMIC_MINOR
,
513 .name
= "aer_inject",
514 .fops
= &aer_inject_fops
,
517 static int __init
aer_inject_init(void)
519 return misc_register(&aer_inject_device
);
522 static void __exit
aer_inject_exit(void)
524 struct aer_error
*err
, *err_next
;
526 struct pci_bus_ops
*bus_ops
;
528 misc_deregister(&aer_inject_device
);
530 while ((bus_ops
= pci_bus_ops_pop())) {
531 pci_bus_set_ops(bus_ops
->bus
, bus_ops
->ops
);
535 spin_lock_irqsave(&inject_lock
, flags
);
536 list_for_each_entry_safe(err
, err_next
, &einjected
, list
) {
537 list_del(&err
->list
);
540 spin_unlock_irqrestore(&inject_lock
, flags
);
543 module_init(aer_inject_init
);
544 module_exit(aer_inject_exit
);
546 MODULE_DESCRIPTION("PCIe AER software error injector");
547 MODULE_LICENSE("GPL");