/drivers/pci/pcie/aer/aer_inject.c
C | 539 lines | 455 code | 61 blank | 23 comment | 63 complexity | e9540c655605b115a7787708b47093ba MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- * PCIe AER software error injection support.
- *
- * Debuging PCIe AER code is quite difficult because it is hard to
- * trigger various real hardware errors. Software based error
- * injection can fake almost all kinds of errors with the help of a
- * user space helper tool aer-inject, which can be gotten from:
- * http://www.kernel.org/pub/linux/utils/pci/aer-inject/
- *
- * Copyright 2009 Intel Corporation.
- * Huang Ying <ying.huang@intel.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; version 2
- * of the License.
- *
- */
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/miscdevice.h>
- #include <linux/pci.h>
- #include <linux/slab.h>
- #include <linux/fs.h>
- #include <linux/uaccess.h>
- #include <linux/stddef.h>
- #include "aerdrv.h"
- /* Override the existing corrected and uncorrected error masks */
- static int aer_mask_override;
- module_param(aer_mask_override, bool, 0);
- struct aer_error_inj {
- u8 bus;
- u8 dev;
- u8 fn;
- u32 uncor_status;
- u32 cor_status;
- u32 header_log0;
- u32 header_log1;
- u32 header_log2;
- u32 header_log3;
- u16 domain;
- };
- struct aer_error {
- struct list_head list;
- u16 domain;
- unsigned int bus;
- unsigned int devfn;
- int pos_cap_err;
- u32 uncor_status;
- u32 cor_status;
- u32 header_log0;
- u32 header_log1;
- u32 header_log2;
- u32 header_log3;
- u32 root_status;
- u32 source_id;
- };
- struct pci_bus_ops {
- struct list_head list;
- struct pci_bus *bus;
- struct pci_ops *ops;
- };
- static LIST_HEAD(einjected);
- static LIST_HEAD(pci_bus_ops_list);
- /* Protect einjected and pci_bus_ops_list */
- static DEFINE_SPINLOCK(inject_lock);
- static void aer_error_init(struct aer_error *err, u16 domain,
- unsigned int bus, unsigned int devfn,
- int pos_cap_err)
- {
- INIT_LIST_HEAD(&err->list);
- err->domain = domain;
- err->bus = bus;
- err->devfn = devfn;
- err->pos_cap_err = pos_cap_err;
- }
- /* inject_lock must be held before calling */
- static struct aer_error *__find_aer_error(u16 domain, unsigned int bus,
- unsigned int devfn)
- {
- struct aer_error *err;
- list_for_each_entry(err, &einjected, list) {
- if (domain == err->domain &&
- bus == err->bus &&
- devfn == err->devfn)
- return err;
- }
- return NULL;
- }
- /* inject_lock must be held before calling */
- static struct aer_error *__find_aer_error_by_dev(struct pci_dev *dev)
- {
- int domain = pci_domain_nr(dev->bus);
- if (domain < 0)
- return NULL;
- return __find_aer_error((u16)domain, dev->bus->number, dev->devfn);
- }
- /* inject_lock must be held before calling */
- static struct pci_ops *__find_pci_bus_ops(struct pci_bus *bus)
- {
- struct pci_bus_ops *bus_ops;
- list_for_each_entry(bus_ops, &pci_bus_ops_list, list) {
- if (bus_ops->bus == bus)
- return bus_ops->ops;
- }
- return NULL;
- }
- static struct pci_bus_ops *pci_bus_ops_pop(void)
- {
- unsigned long flags;
- struct pci_bus_ops *bus_ops = NULL;
- spin_lock_irqsave(&inject_lock, flags);
- if (list_empty(&pci_bus_ops_list))
- bus_ops = NULL;
- else {
- struct list_head *lh = pci_bus_ops_list.next;
- list_del(lh);
- bus_ops = list_entry(lh, struct pci_bus_ops, list);
- }
- spin_unlock_irqrestore(&inject_lock, flags);
- return bus_ops;
- }
- static u32 *find_pci_config_dword(struct aer_error *err, int where,
- int *prw1cs)
- {
- int rw1cs = 0;
- u32 *target = NULL;
- if (err->pos_cap_err == -1)
- return NULL;
- switch (where - err->pos_cap_err) {
- case PCI_ERR_UNCOR_STATUS:
- target = &err->uncor_status;
- rw1cs = 1;
- break;
- case PCI_ERR_COR_STATUS:
- target = &err->cor_status;
- rw1cs = 1;
- break;
- case PCI_ERR_HEADER_LOG:
- target = &err->header_log0;
- break;
- case PCI_ERR_HEADER_LOG+4:
- target = &err->header_log1;
- break;
- case PCI_ERR_HEADER_LOG+8:
- target = &err->header_log2;
- break;
- case PCI_ERR_HEADER_LOG+12:
- target = &err->header_log3;
- break;
- case PCI_ERR_ROOT_STATUS:
- target = &err->root_status;
- rw1cs = 1;
- break;
- case PCI_ERR_ROOT_ERR_SRC:
- target = &err->source_id;
- break;
- }
- if (prw1cs)
- *prw1cs = rw1cs;
- return target;
- }
- static int pci_read_aer(struct pci_bus *bus, unsigned int devfn, int where,
- int size, u32 *val)
- {
- u32 *sim;
- struct aer_error *err;
- unsigned long flags;
- struct pci_ops *ops;
- int domain;
- spin_lock_irqsave(&inject_lock, flags);
- if (size != sizeof(u32))
- goto out;
- domain = pci_domain_nr(bus);
- if (domain < 0)
- goto out;
- err = __find_aer_error((u16)domain, bus->number, devfn);
- if (!err)
- goto out;
- sim = find_pci_config_dword(err, where, NULL);
- if (sim) {
- *val = *sim;
- spin_unlock_irqrestore(&inject_lock, flags);
- return 0;
- }
- out:
- ops = __find_pci_bus_ops(bus);
- spin_unlock_irqrestore(&inject_lock, flags);
- return ops->read(bus, devfn, where, size, val);
- }
- int pci_write_aer(struct pci_bus *bus, unsigned int devfn, int where, int size,
- u32 val)
- {
- u32 *sim;
- struct aer_error *err;
- unsigned long flags;
- int rw1cs;
- struct pci_ops *ops;
- int domain;
- spin_lock_irqsave(&inject_lock, flags);
- if (size != sizeof(u32))
- goto out;
- domain = pci_domain_nr(bus);
- if (domain < 0)
- goto out;
- err = __find_aer_error((u16)domain, bus->number, devfn);
- if (!err)
- goto out;
- sim = find_pci_config_dword(err, where, &rw1cs);
- if (sim) {
- if (rw1cs)
- *sim ^= val;
- else
- *sim = val;
- spin_unlock_irqrestore(&inject_lock, flags);
- return 0;
- }
- out:
- ops = __find_pci_bus_ops(bus);
- spin_unlock_irqrestore(&inject_lock, flags);
- return ops->write(bus, devfn, where, size, val);