aer_inject.c 12 KB

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