aer_inject.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe AER software error injection support.
  4. *
  5. * Debuging PCIe AER code is quite difficult because it is hard to
  6. * trigger various real hardware errors. Software based error
  7. * injection can fake almost all kinds of errors with the help of a
  8. * user space helper tool aer-inject, which can be gotten from:
  9. * http://www.kernel.org/pub/linux/utils/pci/aer-inject/
  10. *
  11. * Copyright 2009 Intel Corporation.
  12. * Huang Ying <ying.huang@intel.com>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/irq.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/stddef.h>
  23. #include <linux/device.h>
  24. #include "portdrv.h"
  25. /* Override the existing corrected and uncorrected error masks */
  26. static bool aer_mask_override;
  27. module_param(aer_mask_override, bool, 0);
  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. u32 domain;
  39. };
  40. struct aer_error {
  41. struct list_head list;
  42. u32 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, u32 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(u32 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(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;
  109. spin_lock_irqsave(&inject_lock, flags);
  110. bus_ops = list_first_entry_or_null(&pci_bus_ops_list,
  111. struct pci_bus_ops, list);
  112. if (bus_ops)
  113. list_del(&bus_ops->list);
  114. spin_unlock_irqrestore(&inject_lock, flags);
  115. return bus_ops;
  116. }
  117. static u32 *find_pci_config_dword(struct aer_error *err, int where,
  118. int *prw1cs)
  119. {
  120. int rw1cs = 0;
  121. u32 *target = NULL;
  122. if (err->pos_cap_err == -1)
  123. return NULL;
  124. switch (where - err->pos_cap_err) {
  125. case PCI_ERR_UNCOR_STATUS:
  126. target = &err->uncor_status;
  127. rw1cs = 1;
  128. break;
  129. case PCI_ERR_COR_STATUS:
  130. target = &err->cor_status;
  131. rw1cs = 1;
  132. break;
  133. case PCI_ERR_HEADER_LOG:
  134. target = &err->header_log0;
  135. break;
  136. case PCI_ERR_HEADER_LOG+4:
  137. target = &err->header_log1;
  138. break;
  139. case PCI_ERR_HEADER_LOG+8:
  140. target = &err->header_log2;
  141. break;
  142. case PCI_ERR_HEADER_LOG+12:
  143. target = &err->header_log3;
  144. break;
  145. case PCI_ERR_ROOT_STATUS:
  146. target = &err->root_status;
  147. rw1cs = 1;
  148. break;
  149. case PCI_ERR_ROOT_ERR_SRC:
  150. target = &err->source_id;
  151. break;
  152. }
  153. if (prw1cs)
  154. *prw1cs = rw1cs;
  155. return target;
  156. }
  157. static int aer_inj_read(struct pci_bus *bus, unsigned int devfn, int where,
  158. int size, u32 *val)
  159. {
  160. struct pci_ops *ops, *my_ops;
  161. int rv;
  162. ops = __find_pci_bus_ops(bus);
  163. if (!ops)
  164. return -1;
  165. my_ops = bus->ops;
  166. bus->ops = ops;
  167. rv = ops->read(bus, devfn, where, size, val);
  168. bus->ops = my_ops;
  169. return rv;
  170. }
  171. static int aer_inj_write(struct pci_bus *bus, unsigned int devfn, int where,
  172. int size, u32 val)
  173. {
  174. struct pci_ops *ops, *my_ops;
  175. int rv;
  176. ops = __find_pci_bus_ops(bus);
  177. if (!ops)
  178. return -1;
  179. my_ops = bus->ops;
  180. bus->ops = ops;
  181. rv = ops->write(bus, devfn, where, size, val);
  182. bus->ops = my_ops;
  183. return rv;
  184. }
  185. static int aer_inj_read_config(struct pci_bus *bus, unsigned int devfn,
  186. int where, int size, u32 *val)
  187. {
  188. u32 *sim;
  189. struct aer_error *err;
  190. unsigned long flags;
  191. int domain;
  192. int rv;
  193. spin_lock_irqsave(&inject_lock, flags);
  194. if (size != sizeof(u32))
  195. goto out;
  196. domain = pci_domain_nr(bus);
  197. if (domain < 0)
  198. goto out;
  199. err = __find_aer_error(domain, bus->number, devfn);
  200. if (!err)
  201. goto out;
  202. sim = find_pci_config_dword(err, where, NULL);
  203. if (sim) {
  204. *val = *sim;
  205. spin_unlock_irqrestore(&inject_lock, flags);
  206. return 0;
  207. }
  208. out:
  209. rv = aer_inj_read(bus, devfn, where, size, val);
  210. spin_unlock_irqrestore(&inject_lock, flags);
  211. return rv;
  212. }
  213. static int aer_inj_write_config(struct pci_bus *bus, unsigned int devfn,
  214. int where, int size, u32 val)
  215. {
  216. u32 *sim;
  217. struct aer_error *err;
  218. unsigned long flags;
  219. int rw1cs;
  220. int domain;
  221. int rv;
  222. spin_lock_irqsave(&inject_lock, flags);
  223. if (size != sizeof(u32))
  224. goto out;
  225. domain = pci_domain_nr(bus);
  226. if (domain < 0)
  227. goto out;
  228. err = __find_aer_error(domain, bus->number, devfn);
  229. if (!err)
  230. goto out;
  231. sim = find_pci_config_dword(err, where, &rw1cs);
  232. if (sim) {
  233. if (rw1cs)
  234. *sim ^= val;
  235. else
  236. *sim = val;
  237. spin_unlock_irqrestore(&inject_lock, flags);
  238. return 0;
  239. }
  240. out:
  241. rv = aer_inj_write(bus, devfn, where, size, val);
  242. spin_unlock_irqrestore(&inject_lock, flags);
  243. return rv;
  244. }
  245. static struct pci_ops aer_inj_pci_ops = {
  246. .read = aer_inj_read_config,
  247. .write = aer_inj_write_config,
  248. };
  249. static void pci_bus_ops_init(struct pci_bus_ops *bus_ops,
  250. struct pci_bus *bus,
  251. struct pci_ops *ops)
  252. {
  253. INIT_LIST_HEAD(&bus_ops->list);
  254. bus_ops->bus = bus;
  255. bus_ops->ops = ops;
  256. }
  257. static int pci_bus_set_aer_ops(struct pci_bus *bus)
  258. {
  259. struct pci_ops *ops;
  260. struct pci_bus_ops *bus_ops;
  261. unsigned long flags;
  262. bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL);
  263. if (!bus_ops)
  264. return -ENOMEM;
  265. ops = pci_bus_set_ops(bus, &aer_inj_pci_ops);
  266. spin_lock_irqsave(&inject_lock, flags);
  267. if (ops == &aer_inj_pci_ops)
  268. goto out;
  269. pci_bus_ops_init(bus_ops, bus, ops);
  270. list_add(&bus_ops->list, &pci_bus_ops_list);
  271. bus_ops = NULL;
  272. out:
  273. spin_unlock_irqrestore(&inject_lock, flags);
  274. kfree(bus_ops);
  275. return 0;
  276. }
  277. static int aer_inject(struct aer_error_inj *einj)
  278. {
  279. struct aer_error *err, *rperr;
  280. struct aer_error *err_alloc = NULL, *rperr_alloc = NULL;
  281. struct pci_dev *dev, *rpdev;
  282. struct pcie_device *edev;
  283. struct device *device;
  284. unsigned long flags;
  285. unsigned int devfn = PCI_DEVFN(einj->dev, einj->fn);
  286. int pos_cap_err, rp_pos_cap_err;
  287. u32 sever, cor_mask, uncor_mask, cor_mask_orig = 0, uncor_mask_orig = 0;
  288. int ret = 0;
  289. dev = pci_get_domain_bus_and_slot(einj->domain, einj->bus, devfn);
  290. if (!dev)
  291. return -ENODEV;
  292. rpdev = pcie_find_root_port(dev);
  293. if (!rpdev) {
  294. pci_err(dev, "aer_inject: Root port not found\n");
  295. ret = -ENODEV;
  296. goto out_put;
  297. }
  298. pos_cap_err = dev->aer_cap;
  299. if (!pos_cap_err) {
  300. pci_err(dev, "aer_inject: Device doesn't support AER\n");
  301. ret = -EPROTONOSUPPORT;
  302. goto out_put;
  303. }
  304. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_SEVER, &sever);
  305. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK, &cor_mask);
  306. pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  307. &uncor_mask);
  308. rp_pos_cap_err = rpdev->aer_cap;
  309. if (!rp_pos_cap_err) {
  310. pci_err(rpdev, "aer_inject: Root port doesn't support AER\n");
  311. ret = -EPROTONOSUPPORT;
  312. goto out_put;
  313. }
  314. err_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
  315. if (!err_alloc) {
  316. ret = -ENOMEM;
  317. goto out_put;
  318. }
  319. rperr_alloc = kzalloc(sizeof(struct aer_error), GFP_KERNEL);
  320. if (!rperr_alloc) {
  321. ret = -ENOMEM;
  322. goto out_put;
  323. }
  324. if (aer_mask_override) {
  325. cor_mask_orig = cor_mask;
  326. cor_mask &= !(einj->cor_status);
  327. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
  328. cor_mask);
  329. uncor_mask_orig = uncor_mask;
  330. uncor_mask &= !(einj->uncor_status);
  331. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  332. uncor_mask);
  333. }
  334. spin_lock_irqsave(&inject_lock, flags);
  335. err = __find_aer_error_by_dev(dev);
  336. if (!err) {
  337. err = err_alloc;
  338. err_alloc = NULL;
  339. aer_error_init(err, einj->domain, einj->bus, devfn,
  340. pos_cap_err);
  341. list_add(&err->list, &einjected);
  342. }
  343. err->uncor_status |= einj->uncor_status;
  344. err->cor_status |= einj->cor_status;
  345. err->header_log0 = einj->header_log0;
  346. err->header_log1 = einj->header_log1;
  347. err->header_log2 = einj->header_log2;
  348. err->header_log3 = einj->header_log3;
  349. if (!aer_mask_override && einj->cor_status &&
  350. !(einj->cor_status & ~cor_mask)) {
  351. ret = -EINVAL;
  352. pci_warn(dev, "aer_inject: The correctable error(s) is masked by device\n");
  353. spin_unlock_irqrestore(&inject_lock, flags);
  354. goto out_put;
  355. }
  356. if (!aer_mask_override && einj->uncor_status &&
  357. !(einj->uncor_status & ~uncor_mask)) {
  358. ret = -EINVAL;
  359. pci_warn(dev, "aer_inject: The uncorrectable error(s) is masked by device\n");
  360. spin_unlock_irqrestore(&inject_lock, flags);
  361. goto out_put;
  362. }
  363. rperr = __find_aer_error_by_dev(rpdev);
  364. if (!rperr) {
  365. rperr = rperr_alloc;
  366. rperr_alloc = NULL;
  367. aer_error_init(rperr, pci_domain_nr(rpdev->bus),
  368. rpdev->bus->number, rpdev->devfn,
  369. rp_pos_cap_err);
  370. list_add(&rperr->list, &einjected);
  371. }
  372. if (einj->cor_status) {
  373. if (rperr->root_status & PCI_ERR_ROOT_COR_RCV)
  374. rperr->root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
  375. else
  376. rperr->root_status |= PCI_ERR_ROOT_COR_RCV;
  377. rperr->source_id &= 0xffff0000;
  378. rperr->source_id |= (einj->bus << 8) | devfn;
  379. }
  380. if (einj->uncor_status) {
  381. if (rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV)
  382. rperr->root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
  383. if (sever & einj->uncor_status) {
  384. rperr->root_status |= PCI_ERR_ROOT_FATAL_RCV;
  385. if (!(rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV))
  386. rperr->root_status |= PCI_ERR_ROOT_FIRST_FATAL;
  387. } else
  388. rperr->root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
  389. rperr->root_status |= PCI_ERR_ROOT_UNCOR_RCV;
  390. rperr->source_id &= 0x0000ffff;
  391. rperr->source_id |= ((einj->bus << 8) | devfn) << 16;
  392. }
  393. spin_unlock_irqrestore(&inject_lock, flags);
  394. if (aer_mask_override) {
  395. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
  396. cor_mask_orig);
  397. pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
  398. uncor_mask_orig);
  399. }
  400. ret = pci_bus_set_aer_ops(dev->bus);
  401. if (ret)
  402. goto out_put;
  403. ret = pci_bus_set_aer_ops(rpdev->bus);
  404. if (ret)
  405. goto out_put;
  406. device = pcie_port_find_device(rpdev, PCIE_PORT_SERVICE_AER);
  407. if (device) {
  408. edev = to_pcie_device(device);
  409. if (!get_service_data(edev)) {
  410. dev_warn(&edev->device,
  411. "aer_inject: AER service is not initialized\n");
  412. ret = -EPROTONOSUPPORT;
  413. goto out_put;
  414. }
  415. dev_info(&edev->device,
  416. "aer_inject: Injecting errors %08x/%08x into device %s\n",
  417. einj->cor_status, einj->uncor_status, pci_name(dev));
  418. local_irq_disable();
  419. generic_handle_irq(edev->irq);
  420. local_irq_enable();
  421. } else {
  422. pci_err(rpdev, "aer_inject: AER device not found\n");
  423. ret = -ENODEV;
  424. }
  425. out_put:
  426. kfree(err_alloc);
  427. kfree(rperr_alloc);
  428. pci_dev_put(dev);
  429. return ret;
  430. }
  431. static ssize_t aer_inject_write(struct file *filp, const char __user *ubuf,
  432. size_t usize, loff_t *off)
  433. {
  434. struct aer_error_inj einj;
  435. int ret;
  436. if (!capable(CAP_SYS_ADMIN))
  437. return -EPERM;
  438. if (usize < offsetof(struct aer_error_inj, domain) ||
  439. usize > sizeof(einj))
  440. return -EINVAL;
  441. memset(&einj, 0, sizeof(einj));
  442. if (copy_from_user(&einj, ubuf, usize))
  443. return -EFAULT;
  444. ret = aer_inject(&einj);
  445. return ret ? ret : usize;
  446. }
  447. static const struct file_operations aer_inject_fops = {
  448. .write = aer_inject_write,
  449. .owner = THIS_MODULE,
  450. .llseek = noop_llseek,
  451. };
  452. static struct miscdevice aer_inject_device = {
  453. .minor = MISC_DYNAMIC_MINOR,
  454. .name = "aer_inject",
  455. .fops = &aer_inject_fops,
  456. };
  457. static int __init aer_inject_init(void)
  458. {
  459. return misc_register(&aer_inject_device);
  460. }
  461. static void __exit aer_inject_exit(void)
  462. {
  463. struct aer_error *err, *err_next;
  464. unsigned long flags;
  465. struct pci_bus_ops *bus_ops;
  466. misc_deregister(&aer_inject_device);
  467. while ((bus_ops = pci_bus_ops_pop())) {
  468. pci_bus_set_ops(bus_ops->bus, bus_ops->ops);
  469. kfree(bus_ops);
  470. }
  471. spin_lock_irqsave(&inject_lock, flags);
  472. list_for_each_entry_safe(err, err_next, &einjected, list) {
  473. list_del(&err->list);
  474. kfree(err);
  475. }
  476. spin_unlock_irqrestore(&inject_lock, flags);
  477. }
  478. module_init(aer_inject_init);
  479. module_exit(aer_inject_exit);
  480. MODULE_DESCRIPTION("PCIe AER software error injector");
  481. MODULE_LICENSE("GPL");