aer_inject.c 12 KB

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