aer_inject.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * PCIe AER software error injection support.
  3. *
  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/
  9. *
  10. * Copyright 2009 Intel Corporation.
  11. * Huang Ying <ying.huang@intel.com>
  12. *
  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
  16. * of the License.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/fs.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/stddef.h>
  27. #include "aerdrv.h"
  28. struct aer_error_inj {
  29. u8 bus;
  30. u8 dev;
  31. u8 fn;
  32. u32 uncor_status;
  33. u32 cor_status;
  34. u32 header_log0;
  35. u32 header_log1;
  36. u32 header_log2;
  37. u32 header_log3;
  38. u16 domain;
  39. };
  40. struct aer_error {
  41. struct list_head list;
  42. u16 domain;
  43. unsigned int bus;
  44. unsigned int devfn;
  45. int pos_cap_err;
  46. u32 uncor_status;
  47. u32 cor_status;
  48. u32 header_log0;
  49. u32 header_log1;
  50. u32 header_log2;
  51. u32 header_log3;
  52. u32 root_status;
  53. u32 source_id;
  54. };
  55. struct pci_bus_ops {
  56. struct list_head list;
  57. struct pci_bus *bus;
  58. struct pci_ops *ops;
  59. };
  60. static LIST_HEAD(einjected);
  61. static LIST_HEAD(pci_bus_ops_list);
  62. /* Protect einjected and pci_bus_ops_list */
  63. static DEFINE_SPINLOCK(inject_lock);
  64. static void aer_error_init(struct aer_error *err, u16 domain,
  65. unsigned int bus, unsigned int devfn,
  66. int pos_cap_err)
  67. {
  68. INIT_LIST_HEAD(&err->list);
  69. err->domain = domain;
  70. err->bus = bus;
  71. err->devfn = devfn;
  72. err->pos_cap_err = pos_cap_err;
  73. }
  74. /* inject_lock must be held before calling */
  75. static struct aer_error *__find_aer_error(u16 domain, unsigned int bus,
  76. unsigned int devfn)
  77. {
  78. struct aer_error *err;
  79. list_for_each_entry(err, &einjected, list) {
  80. if (domain == err->domain &&
  81. bus == err->bus &&
  82. devfn == err->devfn)
  83. return err;
  84. }
  85. return NULL;
  86. }
  87. /* inject_lock must be held before calling */
  88. static struct aer_error *__find_aer_error_by_dev(struct pci_dev *dev)
  89. {
  90. int domain = pci_domain_nr(dev->bus);
  91. if (domain < 0)
  92. return NULL;
  93. return __find_aer_error((u16)domain, dev->bus->number, dev->devfn);
  94. }
  95. /* inject_lock must be held before calling */
  96. static struct pci_ops *__find_pci_bus_ops(struct pci_bus *bus)
  97. {
  98. struct pci_bus_ops *bus_ops;
  99. list_for_each_entry(bus_ops, &pci_bus_ops_list, list) {
  100. if (bus_ops->bus == bus)
  101. return bus_ops->ops;
  102. }
  103. return NULL;
  104. }
  105. static struct pci_bus_ops *pci_bus_ops_pop(void)
  106. {
  107. unsigned long flags;
  108. struct pci_bus_ops *bus_ops = NULL;
  109. spin_lock_irqsave(&inject_lock, flags);
  110. if (list_empty(&pci_bus_ops_list))
  111. bus_ops = NULL;
  112. else {
  113. struct list_head *lh = pci_bus_ops_list.next;
  114. list_del(lh);
  115. bus_ops = list_entry(lh, struct pci_bus_ops, list);
  116. }
  117. spin_unlock_irqrestore(&inject_lock, flags);
  118. return bus_ops;
  119. }
  120. static u32 *find_pci_config_dword(struct aer_error *err, int where,
  121. int *prw1cs)
  122. {
  123. int rw1cs = 0;
  124. u32 *target = NULL;
  125. if (err->pos_cap_err == -1)
  126. return NULL;
  127. switch (where - err->pos_cap_err) {
  128. case PCI_ERR_UNCOR_STATUS:
  129. target = &err->uncor_status;
  130. rw1cs = 1;
  131. break;
  132. case PCI_ERR_COR_STATUS:
  133. target = &err->cor_status;
  134. rw1cs = 1;
  135. break;
  136. case PCI_ERR_HEADER_LOG:
  137. target = &err->header_log0;
  138. break;
  139. case PCI_ERR_HEADER_LOG+4:
  140. target = &err->header_log1;
  141. break;
  142. case PCI_ERR_HEADER_LOG+8:
  143. target = &err->header_log2;
  144. break;
  145. case PCI_ERR_HEADER_LOG+12:
  146. target = &err->header_log3;
  147. break;
  148. case PCI_ERR_ROOT_STATUS:
  149. target = &err->root_status;
  150. rw1cs = 1;
  151. break;
  152. case PCI_ERR_ROOT_COR_SRC:
  153. target = &err->source_id;
  154. break;
  155. }
  156. if (prw1cs)
  157. *prw1cs = rw1cs;
  158. return target;
  159. }
  160. static int pci_read_aer(struct pci_bus *bus, unsigned int devfn, int where,
  161. int size, u32 *val)
  162. {
  163. u32 *sim;
  164. struct aer_error *err;
  165. unsigned long flags;
  166. struct pci_ops *ops;
  167. int domain;
  168. spin_lock_irqsave(&inject_lock, flags);
  169. if (size != sizeof(u32))
  170. goto out;
  171. domain = pci_domain_nr(bus);
  172. if (domain < 0)
  173. goto out;
  174. err = __find_aer_error((u16)domain, bus->number, devfn);
  175. if (!err)
  176. goto out;
  177. sim = find_pci_config_dword(err, where, NULL);
  178. if (sim) {
  179. *val = *sim;
  180. spin_unlock_irqrestore(&inject_lock, flags);
  181. return 0;
  182. }
  183. out:
  184. ops = __find_pci_bus_ops(bus);
  185. spin_unlock_irqrestore(&inject_lock, flags);
  186. return ops->read(bus, devfn, where, size, val);
  187. }
  188. int pci_write_aer(struct pci_bus *bus, unsigned int devfn, int where, int size,
  189. u32 val)
  190. {
  191. u32 *sim;
  192. struct aer_error *err;
  193. unsigned long flags;
  194. int rw1cs;
  195. struct pci_ops *ops;
  196. int domain;
  197. spin_lock_irqsave(&inject_lock, flags);
  198. if (size != sizeof(u32))
  199. goto out;
  200. domain = pci_domain_nr(bus);
  201. if (domain < 0)
  202. goto out;
  203. err = __find_aer_error((u16)domain, bus->number, devfn);
  204. if (!err)
  205. goto out;
  206. sim = find_pci_config_dword(err, where, &rw1cs);
  207. if (sim) {
  208. if (rw1cs)
  209. *sim ^= val;
  210. else
  211. *sim = val;
  212. spin_unlock_irqrestore(&inject_lock, flags);
  213. return 0;
  214. }
  215. out:
  216. ops = __find_pci_bus_ops(bus);
  217. spin_unlock_irqrestore(&inject_lock, flags);
  218. return ops->write(bus, devfn, where, size, val);
  219. }
  220. static struct pci_ops pci_ops_aer = {
  221. .read = pci_read_aer,
  222. .write = pci_write_aer,
  223. };
  224. static void pci_bus_ops_init(struct pci_bus_ops *bus_ops,
  225. struct pci_bus *bus,
  226. struct pci_ops *ops)
  227. {
  228. INIT_LIST_HEAD(&bus_ops->list);
  229. bus_ops->bus = bus;
  230. bus_ops->ops = ops;
  231. }
  232. static int pci_bus_set_aer_ops(struct pci_bus *bus)
  233. {
  234. struct pci_ops *ops;
  235. struct pci_bus_ops *bus_ops;
  236. unsigned long flags;
  237. bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL);
  238. if (!bus_ops)
  239. return -ENOMEM;
  240. ops = pci_bus_set_ops(bus, &pci_ops_aer);
  241. spin_lock_irqsave(&inject_lock, flags);
  242. if (ops == &pci_ops_aer)
  243. goto out;
  244. pci_bus_ops_init(bus_ops, bus, ops);
  245. list_add(&bus_ops->list, &pci_bus_ops_list);
  246. bus_ops = NULL;
  247. out:
  248. spin_unlock_irqrestore(&inject_lock, flags);
  249. kfree(bus_ops);
  250. return 0;
  251. }
  252. static struct pci_dev *pcie_find_root_port(struct pci_dev *dev)
  253. {
  254. while (1) {
  255. if (!pci_is_pcie(dev))
  256. break;
  257. if (dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT)
  258. return dev;
  259. if (!dev->bus->self)
  260. break;
  261. dev = dev->bus->self;
  262. }
  263. return NULL;
  264. }
  265. static int find_aer_device_iter(struct device *device, void *data)
  266. {
  267. struct pcie_device **result = data;
  268. struct pcie_device *pcie_dev;
  269. if (device->bus == &pcie_port_bus_type) {
  270. pcie_dev = to_pcie_device(device);
  271. if (pcie_dev->service & PCIE_PORT_SERVICE_AER) {
  272. *result = pcie_dev;
  273. return 1;
  274. }
  275. }
  276. return 0;
  277. }
  278. static int find_aer_device(struct pci_dev *dev, struct pcie_device **result)
  279. {
  280. return device_for_each_child(&dev->dev, result, find_aer_device_iter);
  281. }
  282. static int aer_inject(struct aer_error_inj *einj)
  283. {
  284. struct aer_error *err, *rperr;
  285. struct aer_error *err_alloc = NULL, *rperr_alloc = NULL;
  286. struct pci_dev *dev, *rpdev;
  287. struct pcie_device *edev;
  288. unsigned long flags;
  289. unsigned int devfn = PCI_DEVFN(einj->dev, einj->fn);
  290. int pos_cap_err, rp_pos_cap_err;
  291. u32 sever, cor_mask, uncor_mask;
  292. int ret = 0;
  293. dev = pci_get_domain_bus_and_slot((int)einj->domain, einj->bus, devfn);
  294. if (!dev)
  295. return -ENODEV;
  296. rpdev = pcie_find_root_port(dev);
  297. if (!rpdev) {
  298. ret = -ENOTTY;
  299. goto out_put;
  300. }
  301. pos_cap_err = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  302. if (!pos_cap_err) {
  303. ret = -ENOTTY;
  304. goto out_put;
  305. }
  306. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_SEVER, &sever);
  307. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK, &cor_mask);
  308. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  309. &uncor_mask);
  310. rp_pos_cap_err = pci_find_ext_capability(rpdev, PCI_EXT_CAP_ID_ERR);
  311. if (!rp_pos_cap_err) {
  312. ret = -ENOTTY;
  313. goto out_put;
  314. }
  315. err_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
  316. if (!err_alloc) {
  317. ret = -ENOMEM;
  318. goto out_put;
  319. }
  320. rperr_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
  321. if (!rperr_alloc) {
  322. ret = -ENOMEM;
  323. goto out_put;
  324. }
  325. spin_lock_irqsave(&inject_lock, flags);
  326. err = __find_aer_error_by_dev(dev);
  327. if (!err) {
  328. err = err_alloc;
  329. err_alloc = NULL;
  330. aer_error_init(err, einj->domain, einj->bus, devfn,
  331. pos_cap_err);
  332. list_add(&err->list, &einjected);
  333. }
  334. err->uncor_status |= einj->uncor_status;
  335. err->cor_status |= einj->cor_status;
  336. err->header_log0 = einj->header_log0;
  337. err->header_log1 = einj->header_log1;
  338. err->header_log2 = einj->header_log2;
  339. err->header_log3 = einj->header_log3;
  340. if (einj->cor_status && !(einj->cor_status & ~cor_mask)) {
  341. ret = -EINVAL;
  342. printk(KERN_WARNING "The correctable error(s) is masked "
  343. "by device\n");
  344. spin_unlock_irqrestore(&inject_lock, flags);
  345. goto out_put;
  346. }
  347. if (einj->uncor_status && !(einj->uncor_status & ~uncor_mask)) {
  348. ret = -EINVAL;
  349. printk(KERN_WARNING "The uncorrectable error(s) is masked "
  350. "by device\n");
  351. spin_unlock_irqrestore(&inject_lock, flags);
  352. goto out_put;
  353. }
  354. rperr = __find_aer_error_by_dev(rpdev);
  355. if (!rperr) {
  356. rperr = rperr_alloc;
  357. rperr_alloc = NULL;
  358. aer_error_init(rperr, pci_domain_nr(rpdev->bus),
  359. rpdev->bus->number, rpdev->devfn,
  360. rp_pos_cap_err);
  361. list_add(&rperr->list, &einjected);
  362. }
  363. if (einj->cor_status) {
  364. if (rperr->root_status & PCI_ERR_ROOT_COR_RCV)
  365. rperr->root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
  366. else
  367. rperr->root_status |= PCI_ERR_ROOT_COR_RCV;
  368. rperr->source_id &= 0xffff0000;
  369. rperr->source_id |= (einj->bus << 8) | devfn;
  370. }
  371. if (einj->uncor_status) {
  372. if (rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV)
  373. rperr->root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
  374. if (sever & einj->uncor_status) {
  375. rperr->root_status |= PCI_ERR_ROOT_FATAL_RCV;
  376. if (!(rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV))
  377. rperr->root_status |= PCI_ERR_ROOT_FIRST_FATAL;
  378. } else
  379. rperr->root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
  380. rperr->root_status |= PCI_ERR_ROOT_UNCOR_RCV;
  381. rperr->source_id &= 0x0000ffff;
  382. rperr->source_id |= ((einj->bus << 8) | devfn) << 16;
  383. }
  384. spin_unlock_irqrestore(&inject_lock, flags);
  385. ret = pci_bus_set_aer_ops(dev->bus);
  386. if (ret)
  387. goto out_put;
  388. ret = pci_bus_set_aer_ops(rpdev->bus);
  389. if (ret)
  390. goto out_put;
  391. if (find_aer_device(rpdev, &edev)) {
  392. if (!get_service_data(edev)) {
  393. printk(KERN_WARNING "AER service is not initialized\n");
  394. ret = -EINVAL;
  395. goto out_put;
  396. }
  397. aer_irq(-1, edev);
  398. }
  399. else
  400. ret = -EINVAL;
  401. out_put:
  402. kfree(err_alloc);
  403. kfree(rperr_alloc);
  404. pci_dev_put(dev);
  405. return ret;
  406. }
  407. static ssize_t aer_inject_write(struct file *filp, const char __user *ubuf,
  408. size_t usize, loff_t *off)
  409. {
  410. struct aer_error_inj einj;
  411. int ret;
  412. if (!capable(CAP_SYS_ADMIN))
  413. return -EPERM;
  414. if (usize < offsetof(struct aer_error_inj, domain) ||
  415. usize > sizeof(einj))
  416. return -EINVAL;
  417. memset(&einj, 0, sizeof(einj));
  418. if (copy_from_user(&einj, ubuf, usize))
  419. return -EFAULT;
  420. ret = aer_inject(&einj);
  421. return ret ? ret : usize;
  422. }
  423. static const struct file_operations aer_inject_fops = {
  424. .write = aer_inject_write,
  425. .owner = THIS_MODULE,
  426. };
  427. static struct miscdevice aer_inject_device = {
  428. .minor = MISC_DYNAMIC_MINOR,
  429. .name = "aer_inject",
  430. .fops = &aer_inject_fops,
  431. };
  432. static int __init aer_inject_init(void)
  433. {
  434. return misc_register(&aer_inject_device);
  435. }
  436. static void __exit aer_inject_exit(void)
  437. {
  438. struct aer_error *err, *err_next;
  439. unsigned long flags;
  440. struct pci_bus_ops *bus_ops;
  441. misc_deregister(&aer_inject_device);
  442. while ((bus_ops = pci_bus_ops_pop())) {
  443. pci_bus_set_ops(bus_ops->bus, bus_ops->ops);
  444. kfree(bus_ops);
  445. }
  446. spin_lock_irqsave(&inject_lock, flags);
  447. list_for_each_entry_safe(err, err_next, &einjected, list) {
  448. list_del(&err->list);
  449. kfree(err);
  450. }
  451. spin_unlock_irqrestore(&inject_lock, flags);
  452. }
  453. module_init(aer_inject_init);
  454. module_exit(aer_inject_exit);
  455. MODULE_DESCRIPTION("PCIe AER software error injector");
  456. MODULE_LICENSE("GPL");