aer_inject.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. int pci_write_aer(struct pci_bus *bus, unsigned int devfn, int where, int size,
  192. 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 struct pci_dev *pcie_find_root_port(struct pci_dev *dev)
  256. {
  257. while (1) {
  258. if (!pci_is_pcie(dev))
  259. break;
  260. if (dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT)
  261. return dev;
  262. if (!dev->bus->self)
  263. break;
  264. dev = dev->bus->self;
  265. }
  266. return NULL;
  267. }
  268. static int find_aer_device_iter(struct device *device, void *data)
  269. {
  270. struct pcie_device **result = data;
  271. struct pcie_device *pcie_dev;
  272. if (device->bus == &pcie_port_bus_type) {
  273. pcie_dev = to_pcie_device(device);
  274. if (pcie_dev->service & PCIE_PORT_SERVICE_AER) {
  275. *result = pcie_dev;
  276. return 1;
  277. }
  278. }
  279. return 0;
  280. }
  281. static int find_aer_device(struct pci_dev *dev, struct pcie_device **result)
  282. {
  283. return device_for_each_child(&dev->dev, result, find_aer_device_iter);
  284. }
  285. static int aer_inject(struct aer_error_inj *einj)
  286. {
  287. struct aer_error *err, *rperr;
  288. struct aer_error *err_alloc = NULL, *rperr_alloc = NULL;
  289. struct pci_dev *dev, *rpdev;
  290. struct pcie_device *edev;
  291. unsigned long flags;
  292. unsigned int devfn = PCI_DEVFN(einj->dev, einj->fn);
  293. int pos_cap_err, rp_pos_cap_err;
  294. u32 sever, cor_mask, uncor_mask, cor_mask_orig = 0, uncor_mask_orig = 0;
  295. int ret = 0;
  296. dev = pci_get_domain_bus_and_slot((int)einj->domain, einj->bus, devfn);
  297. if (!dev)
  298. return -ENODEV;
  299. rpdev = pcie_find_root_port(dev);
  300. if (!rpdev) {
  301. ret = -ENOTTY;
  302. goto out_put;
  303. }
  304. pos_cap_err = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  305. if (!pos_cap_err) {
  306. ret = -ENOTTY;
  307. goto out_put;
  308. }
  309. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_SEVER, &sever);
  310. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK, &cor_mask);
  311. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  312. &uncor_mask);
  313. rp_pos_cap_err = pci_find_ext_capability(rpdev, PCI_EXT_CAP_ID_ERR);
  314. if (!rp_pos_cap_err) {
  315. ret = -ENOTTY;
  316. goto out_put;
  317. }
  318. err_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
  319. if (!err_alloc) {
  320. ret = -ENOMEM;
  321. goto out_put;
  322. }
  323. rperr_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
  324. if (!rperr_alloc) {
  325. ret = -ENOMEM;
  326. goto out_put;
  327. }
  328. if (aer_mask_override) {
  329. cor_mask_orig = cor_mask;
  330. cor_mask &= !(einj->cor_status);
  331. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
  332. cor_mask);
  333. uncor_mask_orig = uncor_mask;
  334. uncor_mask &= !(einj->uncor_status);
  335. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  336. uncor_mask);
  337. }
  338. spin_lock_irqsave(&inject_lock, flags);
  339. err = __find_aer_error_by_dev(dev);
  340. if (!err) {
  341. err = err_alloc;
  342. err_alloc = NULL;
  343. aer_error_init(err, einj->domain, einj->bus, devfn,
  344. pos_cap_err);
  345. list_add(&err->list, &einjected);
  346. }
  347. err->uncor_status |= einj->uncor_status;
  348. err->cor_status |= einj->cor_status;
  349. err->header_log0 = einj->header_log0;
  350. err->header_log1 = einj->header_log1;
  351. err->header_log2 = einj->header_log2;
  352. err->header_log3 = einj->header_log3;
  353. if (!aer_mask_override && einj->cor_status &&
  354. !(einj->cor_status & ~cor_mask)) {
  355. ret = -EINVAL;
  356. printk(KERN_WARNING "The correctable error(s) is masked "
  357. "by device\n");
  358. spin_unlock_irqrestore(&inject_lock, flags);
  359. goto out_put;
  360. }
  361. if (!aer_mask_override && einj->uncor_status &&
  362. !(einj->uncor_status & ~uncor_mask)) {
  363. ret = -EINVAL;
  364. printk(KERN_WARNING "The uncorrectable error(s) is masked "
  365. "by device\n");
  366. spin_unlock_irqrestore(&inject_lock, flags);
  367. goto out_put;
  368. }
  369. rperr = __find_aer_error_by_dev(rpdev);
  370. if (!rperr) {
  371. rperr = rperr_alloc;
  372. rperr_alloc = NULL;
  373. aer_error_init(rperr, pci_domain_nr(rpdev->bus),
  374. rpdev->bus->number, rpdev->devfn,
  375. rp_pos_cap_err);
  376. list_add(&rperr->list, &einjected);
  377. }
  378. if (einj->cor_status) {
  379. if (rperr->root_status & PCI_ERR_ROOT_COR_RCV)
  380. rperr->root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
  381. else
  382. rperr->root_status |= PCI_ERR_ROOT_COR_RCV;
  383. rperr->source_id &= 0xffff0000;
  384. rperr->source_id |= (einj->bus << 8) | devfn;
  385. }
  386. if (einj->uncor_status) {
  387. if (rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV)
  388. rperr->root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
  389. if (sever & einj->uncor_status) {
  390. rperr->root_status |= PCI_ERR_ROOT_FATAL_RCV;
  391. if (!(rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV))
  392. rperr->root_status |= PCI_ERR_ROOT_FIRST_FATAL;
  393. } else
  394. rperr->root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
  395. rperr->root_status |= PCI_ERR_ROOT_UNCOR_RCV;
  396. rperr->source_id &= 0x0000ffff;
  397. rperr->source_id |= ((einj->bus << 8) | devfn) << 16;
  398. }
  399. spin_unlock_irqrestore(&inject_lock, flags);
  400. if (aer_mask_override) {
  401. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
  402. cor_mask_orig);
  403. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  404. uncor_mask_orig);
  405. }
  406. ret = pci_bus_set_aer_ops(dev->bus);
  407. if (ret)
  408. goto out_put;
  409. ret = pci_bus_set_aer_ops(rpdev->bus);
  410. if (ret)
  411. goto out_put;
  412. if (find_aer_device(rpdev, &edev)) {
  413. if (!get_service_data(edev)) {
  414. printk(KERN_WARNING "AER service is not initialized\n");
  415. ret = -EINVAL;
  416. goto out_put;
  417. }
  418. aer_irq(-1, edev);
  419. }
  420. else
  421. ret = -EINVAL;
  422. out_put:
  423. kfree(err_alloc);
  424. kfree(rperr_alloc);
  425. pci_dev_put(dev);
  426. return ret;
  427. }
  428. static ssize_t aer_inject_write(struct file *filp, const char __user *ubuf,
  429. size_t usize, loff_t *off)
  430. {
  431. struct aer_error_inj einj;
  432. int ret;
  433. if (!capable(CAP_SYS_ADMIN))
  434. return -EPERM;
  435. if (usize < offsetof(struct aer_error_inj, domain) ||
  436. usize > sizeof(einj))
  437. return -EINVAL;
  438. memset(&einj, 0, sizeof(einj));
  439. if (copy_from_user(&einj, ubuf, usize))
  440. return -EFAULT;
  441. ret = aer_inject(&einj);
  442. return ret ? ret : usize;
  443. }
  444. static const struct file_operations aer_inject_fops = {
  445. .write = aer_inject_write,
  446. .owner = THIS_MODULE,
  447. .llseek = noop_llseek,
  448. };
  449. static struct miscdevice aer_inject_device = {
  450. .minor = MISC_DYNAMIC_MINOR,
  451. .name = "aer_inject",
  452. .fops = &aer_inject_fops,
  453. };
  454. static int __init aer_inject_init(void)
  455. {
  456. return misc_register(&aer_inject_device);
  457. }
  458. static void __exit aer_inject_exit(void)
  459. {
  460. struct aer_error *err, *err_next;
  461. unsigned long flags;
  462. struct pci_bus_ops *bus_ops;
  463. misc_deregister(&aer_inject_device);
  464. while ((bus_ops = pci_bus_ops_pop())) {
  465. pci_bus_set_ops(bus_ops->bus, bus_ops->ops);
  466. kfree(bus_ops);
  467. }
  468. spin_lock_irqsave(&inject_lock, flags);
  469. list_for_each_entry_safe(err, err_next, &einjected, list) {
  470. list_del(&err->list);
  471. kfree(err);
  472. }
  473. spin_unlock_irqrestore(&inject_lock, flags);
  474. }
  475. module_init(aer_inject_init);
  476. module_exit(aer_inject_exit);
  477. MODULE_DESCRIPTION("PCIe AER software error injector");
  478. MODULE_LICENSE("GPL");