2 * PCIE AER software error injection support.
4 * Debuging PCIE AER code is quite difficult because it is hard to
5 * trigger various real hardware errors. Software based error
6 * injection can fake almost all kinds of errors with the help of a
7 * user space helper tool aer-inject, which can be gotten from:
8 * http://www.kernel.org/pub/linux/utils/pci/aer-inject/
10 * Copyright 2009 Intel Corporation.
11 * Huang Ying <ying.huang@intel.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; version 2
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/miscdevice.h>
23 #include <linux/pci.h>
25 #include <asm/uaccess.h>
43 struct list_head list
;
60 struct list_head list
;
65 static LIST_HEAD(einjected
);
67 static LIST_HEAD(pci_bus_ops_list
);
69 /* Protect einjected and pci_bus_ops_list */
70 static DEFINE_SPINLOCK(inject_lock
);
72 static void aer_error_init(struct aer_error
*err
, unsigned int bus
,
73 unsigned int devfn
, int pos_cap_err
)
75 INIT_LIST_HEAD(&err
->list
);
78 err
->pos_cap_err
= pos_cap_err
;
81 /* inject_lock must be held before calling */
82 static struct aer_error
*__find_aer_error(unsigned int bus
, unsigned int devfn
)
84 struct aer_error
*err
;
86 list_for_each_entry(err
, &einjected
, list
) {
87 if (bus
== err
->bus
&& devfn
== err
->devfn
)
93 /* inject_lock must be held before calling */
94 static struct aer_error
*__find_aer_error_by_dev(struct pci_dev
*dev
)
96 return __find_aer_error(dev
->bus
->number
, dev
->devfn
);
99 /* inject_lock must be held before calling */
100 static struct pci_ops
*__find_pci_bus_ops(struct pci_bus
*bus
)
102 struct pci_bus_ops
*bus_ops
;
104 list_for_each_entry(bus_ops
, &pci_bus_ops_list
, list
) {
105 if (bus_ops
->bus
== bus
)
111 static struct pci_bus_ops
*pci_bus_ops_pop(void)
114 struct pci_bus_ops
*bus_ops
= NULL
;
116 spin_lock_irqsave(&inject_lock
, flags
);
117 if (list_empty(&pci_bus_ops_list
))
120 struct list_head
*lh
= pci_bus_ops_list
.next
;
122 bus_ops
= list_entry(lh
, struct pci_bus_ops
, list
);
124 spin_unlock_irqrestore(&inject_lock
, flags
);
128 static u32
*find_pci_config_dword(struct aer_error
*err
, int where
,
134 if (err
->pos_cap_err
== -1)
137 switch (where
- err
->pos_cap_err
) {
138 case PCI_ERR_UNCOR_STATUS
:
139 target
= &err
->uncor_status
;
142 case PCI_ERR_COR_STATUS
:
143 target
= &err
->cor_status
;
146 case PCI_ERR_HEADER_LOG
:
147 target
= &err
->header_log0
;
149 case PCI_ERR_HEADER_LOG
+4:
150 target
= &err
->header_log1
;
152 case PCI_ERR_HEADER_LOG
+8:
153 target
= &err
->header_log2
;
155 case PCI_ERR_HEADER_LOG
+12:
156 target
= &err
->header_log3
;
158 case PCI_ERR_ROOT_STATUS
:
159 target
= &err
->root_status
;
162 case PCI_ERR_ROOT_COR_SRC
:
163 target
= &err
->source_id
;
171 static int pci_read_aer(struct pci_bus
*bus
, unsigned int devfn
, int where
,
175 struct aer_error
*err
;
179 spin_lock_irqsave(&inject_lock
, flags
);
180 if (size
!= sizeof(u32
))
182 err
= __find_aer_error(bus
->number
, devfn
);
186 sim
= find_pci_config_dword(err
, where
, NULL
);
189 spin_unlock_irqrestore(&inject_lock
, flags
);
193 ops
= __find_pci_bus_ops(bus
);
194 spin_unlock_irqrestore(&inject_lock
, flags
);
195 return ops
->read(bus
, devfn
, where
, size
, val
);
198 int pci_write_aer(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
,
202 struct aer_error
*err
;
207 spin_lock_irqsave(&inject_lock
, flags
);
208 if (size
!= sizeof(u32
))
210 err
= __find_aer_error(bus
->number
, devfn
);
214 sim
= find_pci_config_dword(err
, where
, &rw1cs
);
220 spin_unlock_irqrestore(&inject_lock
, flags
);
224 ops
= __find_pci_bus_ops(bus
);
225 spin_unlock_irqrestore(&inject_lock
, flags
);
226 return ops
->write(bus
, devfn
, where
, size
, val
);
229 static struct pci_ops pci_ops_aer
= {
230 .read
= pci_read_aer
,
231 .write
= pci_write_aer
,
234 static void pci_bus_ops_init(struct pci_bus_ops
*bus_ops
,
238 INIT_LIST_HEAD(&bus_ops
->list
);
243 static int pci_bus_set_aer_ops(struct pci_bus
*bus
)
246 struct pci_bus_ops
*bus_ops
;
249 bus_ops
= kmalloc(sizeof(*bus_ops
), GFP_KERNEL
);
252 ops
= pci_bus_set_ops(bus
, &pci_ops_aer
);
253 spin_lock_irqsave(&inject_lock
, flags
);
254 if (ops
== &pci_ops_aer
)
256 pci_bus_ops_init(bus_ops
, bus
, ops
);
257 list_add(&bus_ops
->list
, &pci_bus_ops_list
);
260 spin_unlock_irqrestore(&inject_lock
, flags
);
266 static struct pci_dev
*pcie_find_root_port(struct pci_dev
*dev
)
271 if (dev
->pcie_type
== PCI_EXP_TYPE_ROOT_PORT
)
275 dev
= dev
->bus
->self
;
280 static int find_aer_device_iter(struct device
*device
, void *data
)
282 struct pcie_device
**result
= data
;
283 struct pcie_device
*pcie_dev
;
285 if (device
->bus
== &pcie_port_bus_type
) {
286 pcie_dev
= to_pcie_device(device
);
287 if (pcie_dev
->service
& PCIE_PORT_SERVICE_AER
) {
295 static int find_aer_device(struct pci_dev
*dev
, struct pcie_device
**result
)
297 return device_for_each_child(&dev
->dev
, result
, find_aer_device_iter
);
300 static int aer_inject(struct aer_error_inj
*einj
)
302 struct aer_error
*err
, *rperr
;
303 struct aer_error
*err_alloc
= NULL
, *rperr_alloc
= NULL
;
304 struct pci_dev
*dev
, *rpdev
;
305 struct pcie_device
*edev
;
307 unsigned int devfn
= PCI_DEVFN(einj
->dev
, einj
->fn
);
308 int pos_cap_err
, rp_pos_cap_err
;
312 dev
= pci_get_bus_and_slot(einj
->bus
, devfn
);
315 rpdev
= pcie_find_root_port(dev
);
321 pos_cap_err
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
);
326 pci_read_config_dword(dev
, pos_cap_err
+ PCI_ERR_UNCOR_SEVER
, &sever
);
328 rp_pos_cap_err
= pci_find_ext_capability(rpdev
, PCI_EXT_CAP_ID_ERR
);
329 if (!rp_pos_cap_err
) {
334 err_alloc
= kzalloc(sizeof(struct aer_error
), GFP_KERNEL
);
339 rperr_alloc
= kzalloc(sizeof(struct aer_error
), GFP_KERNEL
);
345 spin_lock_irqsave(&inject_lock
, flags
);
347 err
= __find_aer_error_by_dev(dev
);
351 aer_error_init(err
, einj
->bus
, devfn
, pos_cap_err
);
352 list_add(&err
->list
, &einjected
);
354 err
->uncor_status
|= einj
->uncor_status
;
355 err
->cor_status
|= einj
->cor_status
;
356 err
->header_log0
= einj
->header_log0
;
357 err
->header_log1
= einj
->header_log1
;
358 err
->header_log2
= einj
->header_log2
;
359 err
->header_log3
= einj
->header_log3
;
361 rperr
= __find_aer_error_by_dev(rpdev
);
365 aer_error_init(rperr
, rpdev
->bus
->number
, rpdev
->devfn
,
367 list_add(&rperr
->list
, &einjected
);
369 if (einj
->cor_status
) {
370 if (rperr
->root_status
& PCI_ERR_ROOT_COR_RCV
)
371 rperr
->root_status
|= PCI_ERR_ROOT_MULTI_COR_RCV
;
373 rperr
->root_status
|= PCI_ERR_ROOT_COR_RCV
;
374 rperr
->source_id
&= 0xffff0000;
375 rperr
->source_id
|= (einj
->bus
<< 8) | devfn
;
377 if (einj
->uncor_status
) {
378 if (rperr
->root_status
& PCI_ERR_ROOT_UNCOR_RCV
)
379 rperr
->root_status
|= PCI_ERR_ROOT_MULTI_UNCOR_RCV
;
380 if (sever
& einj
->uncor_status
) {
381 rperr
->root_status
|= PCI_ERR_ROOT_FATAL_RCV
;
382 if (!(rperr
->root_status
& PCI_ERR_ROOT_UNCOR_RCV
))
383 rperr
->root_status
|= PCI_ERR_ROOT_FIRST_FATAL
;
385 rperr
->root_status
|= PCI_ERR_ROOT_NONFATAL_RCV
;
386 rperr
->root_status
|= PCI_ERR_ROOT_UNCOR_RCV
;
387 rperr
->source_id
&= 0x0000ffff;
388 rperr
->source_id
|= ((einj
->bus
<< 8) | devfn
) << 16;
390 spin_unlock_irqrestore(&inject_lock
, flags
);
392 ret
= pci_bus_set_aer_ops(dev
->bus
);
395 ret
= pci_bus_set_aer_ops(rpdev
->bus
);
399 if (find_aer_device(rpdev
, &edev
))
412 static ssize_t
aer_inject_write(struct file
*filp
, const char __user
*ubuf
,
413 size_t usize
, loff_t
*off
)
415 struct aer_error_inj einj
;
418 if (!capable(CAP_SYS_ADMIN
))
421 if (usize
!= sizeof(struct aer_error_inj
))
424 if (copy_from_user(&einj
, ubuf
, usize
))
427 ret
= aer_inject(&einj
);
428 return ret
? ret
: usize
;
431 static const struct file_operations aer_inject_fops
= {
432 .write
= aer_inject_write
,
433 .owner
= THIS_MODULE
,
436 static struct miscdevice aer_inject_device
= {
437 .minor
= MISC_DYNAMIC_MINOR
,
438 .name
= "aer_inject",
439 .fops
= &aer_inject_fops
,
442 static int __init
aer_inject_init(void)
444 return misc_register(&aer_inject_device
);
447 static void __exit
aer_inject_exit(void)
449 struct aer_error
*err
, *err_next
;
451 struct pci_bus_ops
*bus_ops
;
453 misc_deregister(&aer_inject_device
);
455 while ((bus_ops
= pci_bus_ops_pop())) {
456 pci_bus_set_ops(bus_ops
->bus
, bus_ops
->ops
);
460 spin_lock_irqsave(&inject_lock
, flags
);
461 list_for_each_entry_safe(err
, err_next
,
462 &pci_bus_ops_list
, list
) {
463 list_del(&err
->list
);
466 spin_unlock_irqrestore(&inject_lock
, flags
);
469 module_init(aer_inject_init
);
470 module_exit(aer_inject_exit
);
472 MODULE_DESCRIPTION("PCIE AER software error injector");
473 MODULE_LICENSE("GPL");