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 <linux/uaccess.h>
26 #include <linux/stddef.h>
29 struct aer_error_inj {
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, u16 domain,
73 unsigned int bus, unsigned int devfn,
76 INIT_LIST_HEAD(&err->list);
80 err->pos_cap_err = pos_cap_err;
83 /* inject_lock must be held before calling */
84 static struct aer_error *__find_aer_error(u16 domain, unsigned int bus,
87 struct aer_error *err;
89 list_for_each_entry(err, &einjected, list) {
90 if (domain == err->domain &&
98 /* inject_lock must be held before calling */
99 static struct aer_error *__find_aer_error_by_dev(struct pci_dev *dev)
101 int domain = pci_domain_nr(dev->bus);
104 return __find_aer_error((u16)domain, dev->bus->number, dev->devfn);
107 /* inject_lock must be held before calling */
108 static struct pci_ops *__find_pci_bus_ops(struct pci_bus *bus)
110 struct pci_bus_ops *bus_ops;
112 list_for_each_entry(bus_ops, &pci_bus_ops_list, list) {
113 if (bus_ops->bus == bus)
119 static struct pci_bus_ops *pci_bus_ops_pop(void)
122 struct pci_bus_ops *bus_ops = NULL;
124 spin_lock_irqsave(&inject_lock, flags);
125 if (list_empty(&pci_bus_ops_list))
128 struct list_head *lh = pci_bus_ops_list.next;
130 bus_ops = list_entry(lh, struct pci_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_COR_SRC:
171 target = &err->source_id;
179 static int pci_read_aer(struct pci_bus *bus, unsigned int devfn, int where,
183 struct aer_error *err;
188 spin_lock_irqsave(&inject_lock, flags);
189 if (size != sizeof(u32))
191 domain = pci_domain_nr(bus);
194 err = __find_aer_error((u16)domain, bus->number, devfn);
198 sim = find_pci_config_dword(err, where, NULL);
201 spin_unlock_irqrestore(&inject_lock, flags);
205 ops = __find_pci_bus_ops(bus);
206 spin_unlock_irqrestore(&inject_lock, flags);
207 return ops->read(bus, devfn, where, size, val);
210 int pci_write_aer(struct pci_bus *bus, unsigned int devfn, int where, int size,
214 struct aer_error *err;
220 spin_lock_irqsave(&inject_lock, flags);
221 if (size != sizeof(u32))
223 domain = pci_domain_nr(bus);
226 err = __find_aer_error((u16)domain, bus->number, devfn);
230 sim = find_pci_config_dword(err, where, &rw1cs);
236 spin_unlock_irqrestore(&inject_lock, flags);
240 ops = __find_pci_bus_ops(bus);
241 spin_unlock_irqrestore(&inject_lock, flags);
242 return ops->write(bus, devfn, where, size, val);
245 static struct pci_ops pci_ops_aer = {
246 .read = pci_read_aer,
247 .write = pci_write_aer,
250 static void pci_bus_ops_init(struct pci_bus_ops *bus_ops,
254 INIT_LIST_HEAD(&bus_ops->list);
259 static int pci_bus_set_aer_ops(struct pci_bus *bus)
262 struct pci_bus_ops *bus_ops;
265 bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL);
268 ops = pci_bus_set_ops(bus, &pci_ops_aer);
269 spin_lock_irqsave(&inject_lock, flags);
270 if (ops == &pci_ops_aer)
272 pci_bus_ops_init(bus_ops, bus, ops);
273 list_add(&bus_ops->list, &pci_bus_ops_list);
276 spin_unlock_irqrestore(&inject_lock, flags);
281 static struct pci_dev *pcie_find_root_port(struct pci_dev *dev)
284 if (!pci_is_pcie(dev))
286 if (dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT)
290 dev = dev->bus->self;
295 static int find_aer_device_iter(struct device *device, void *data)
297 struct pcie_device **result = data;
298 struct pcie_device *pcie_dev;
300 if (device->bus == &pcie_port_bus_type) {
301 pcie_dev = to_pcie_device(device);
302 if (pcie_dev->service & PCIE_PORT_SERVICE_AER) {
310 static int find_aer_device(struct pci_dev *dev, struct pcie_device **result)
312 return device_for_each_child(&dev->dev, result, find_aer_device_iter);
315 static int aer_inject(struct aer_error_inj *einj)
317 struct aer_error *err, *rperr;
318 struct aer_error *err_alloc = NULL, *rperr_alloc = NULL;
319 struct pci_dev *dev, *rpdev;
320 struct pcie_device *edev;
322 unsigned int devfn = PCI_DEVFN(einj->dev, einj->fn);
323 int pos_cap_err, rp_pos_cap_err;
327 dev = pci_get_domain_bus_and_slot((int)einj->domain, einj->bus, devfn);
330 rpdev = pcie_find_root_port(dev);
336 pos_cap_err = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
341 pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_SEVER, &sever);
343 rp_pos_cap_err = pci_find_ext_capability(rpdev, PCI_EXT_CAP_ID_ERR);
344 if (!rp_pos_cap_err) {
349 err_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
354 rperr_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
360 spin_lock_irqsave(&inject_lock, flags);
362 err = __find_aer_error_by_dev(dev);
366 aer_error_init(err, einj->domain, einj->bus, devfn,
368 list_add(&err->list, &einjected);
370 err->uncor_status |= einj->uncor_status;
371 err->cor_status |= einj->cor_status;
372 err->header_log0 = einj->header_log0;
373 err->header_log1 = einj->header_log1;
374 err->header_log2 = einj->header_log2;
375 err->header_log3 = einj->header_log3;
377 rperr = __find_aer_error_by_dev(rpdev);
381 aer_error_init(rperr, pci_domain_nr(rpdev->bus),
382 rpdev->bus->number, rpdev->devfn,
384 list_add(&rperr->list, &einjected);
386 if (einj->cor_status) {
387 if (rperr->root_status & PCI_ERR_ROOT_COR_RCV)
388 rperr->root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
390 rperr->root_status |= PCI_ERR_ROOT_COR_RCV;
391 rperr->source_id &= 0xffff0000;
392 rperr->source_id |= (einj->bus << 8) | devfn;
394 if (einj->uncor_status) {
395 if (rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV)
396 rperr->root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
397 if (sever & einj->uncor_status) {
398 rperr->root_status |= PCI_ERR_ROOT_FATAL_RCV;
399 if (!(rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV))
400 rperr->root_status |= PCI_ERR_ROOT_FIRST_FATAL;
402 rperr->root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
403 rperr->root_status |= PCI_ERR_ROOT_UNCOR_RCV;
404 rperr->source_id &= 0x0000ffff;
405 rperr->source_id |= ((einj->bus << 8) | devfn) << 16;
407 spin_unlock_irqrestore(&inject_lock, flags);
409 ret = pci_bus_set_aer_ops(dev->bus);
412 ret = pci_bus_set_aer_ops(rpdev->bus);
416 if (find_aer_device(rpdev, &edev))
427 static ssize_t aer_inject_write(struct file *filp, const char __user *ubuf,
428 size_t usize, loff_t *off)
430 struct aer_error_inj einj;
433 if (!capable(CAP_SYS_ADMIN))
435 if (usize < offsetof(struct aer_error_inj, domain) ||
436 usize > sizeof(einj))
439 memset(&einj, 0, sizeof(einj));
440 if (copy_from_user(&einj, ubuf, usize))
443 ret = aer_inject(&einj);
444 return ret ? ret : usize;
447 static const struct file_operations aer_inject_fops = {
448 .write = aer_inject_write,
449 .owner = THIS_MODULE,
452 static struct miscdevice aer_inject_device = {
453 .minor = MISC_DYNAMIC_MINOR,
454 .name = "aer_inject",
455 .fops = &aer_inject_fops,
458 static int __init aer_inject_init(void)
460 return misc_register(&aer_inject_device);
463 static void __exit aer_inject_exit(void)
465 struct aer_error *err, *err_next;
467 struct pci_bus_ops *bus_ops;
469 misc_deregister(&aer_inject_device);
471 while ((bus_ops = pci_bus_ops_pop())) {
472 pci_bus_set_ops(bus_ops->bus, bus_ops->ops);
476 spin_lock_irqsave(&inject_lock, flags);
477 list_for_each_entry_safe(err, err_next, &einjected, list) {
478 list_del(&err->list);
481 spin_unlock_irqrestore(&inject_lock, flags);
484 module_init(aer_inject_init);
485 module_exit(aer_inject_exit);
487 MODULE_DESCRIPTION("PCIE AER software error injector");
488 MODULE_LICENSE("GPL");