aer_inject.c 13 KB

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