aer_inject.c 13 KB

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