dpc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Express Downstream Port Containment services driver
  4. * Author: Keith Busch <keith.busch@intel.com>
  5. *
  6. * Copyright (C) 2016 Intel Corp.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/init.h>
  11. #include <linux/pci.h>
  12. #include "portdrv.h"
  13. #include "../pci.h"
  14. #include "aer/aerdrv.h"
  15. struct dpc_dev {
  16. struct pcie_device *dev;
  17. struct work_struct work;
  18. u16 cap_pos;
  19. bool rp_extensions;
  20. u32 rp_pio_status;
  21. u8 rp_log_size;
  22. };
  23. static const char * const rp_pio_error_string[] = {
  24. "Configuration Request received UR Completion", /* Bit Position 0 */
  25. "Configuration Request received CA Completion", /* Bit Position 1 */
  26. "Configuration Request Completion Timeout", /* Bit Position 2 */
  27. NULL,
  28. NULL,
  29. NULL,
  30. NULL,
  31. NULL,
  32. "I/O Request received UR Completion", /* Bit Position 8 */
  33. "I/O Request received CA Completion", /* Bit Position 9 */
  34. "I/O Request Completion Timeout", /* Bit Position 10 */
  35. NULL,
  36. NULL,
  37. NULL,
  38. NULL,
  39. NULL,
  40. "Memory Request received UR Completion", /* Bit Position 16 */
  41. "Memory Request received CA Completion", /* Bit Position 17 */
  42. "Memory Request Completion Timeout", /* Bit Position 18 */
  43. };
  44. static int dpc_wait_rp_inactive(struct dpc_dev *dpc)
  45. {
  46. unsigned long timeout = jiffies + HZ;
  47. struct pci_dev *pdev = dpc->dev->port;
  48. struct device *dev = &dpc->dev->device;
  49. u16 cap = dpc->cap_pos, status;
  50. pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
  51. while (status & PCI_EXP_DPC_RP_BUSY &&
  52. !time_after(jiffies, timeout)) {
  53. msleep(10);
  54. pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
  55. }
  56. if (status & PCI_EXP_DPC_RP_BUSY) {
  57. dev_warn(dev, "DPC root port still busy\n");
  58. return -EBUSY;
  59. }
  60. return 0;
  61. }
  62. static void dpc_wait_link_inactive(struct dpc_dev *dpc)
  63. {
  64. unsigned long timeout = jiffies + HZ;
  65. struct pci_dev *pdev = dpc->dev->port;
  66. struct device *dev = &dpc->dev->device;
  67. u16 lnk_status;
  68. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  69. while (lnk_status & PCI_EXP_LNKSTA_DLLLA &&
  70. !time_after(jiffies, timeout)) {
  71. msleep(10);
  72. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  73. }
  74. if (lnk_status & PCI_EXP_LNKSTA_DLLLA)
  75. dev_warn(dev, "Link state not disabled for DPC event\n");
  76. }
  77. static void dpc_work(struct work_struct *work)
  78. {
  79. struct dpc_dev *dpc = container_of(work, struct dpc_dev, work);
  80. struct pci_dev *dev, *temp, *pdev = dpc->dev->port;
  81. struct pci_bus *parent = pdev->subordinate;
  82. u16 cap = dpc->cap_pos, ctl;
  83. pci_lock_rescan_remove();
  84. list_for_each_entry_safe_reverse(dev, temp, &parent->devices,
  85. bus_list) {
  86. pci_dev_get(dev);
  87. pci_dev_set_disconnected(dev, NULL);
  88. if (pci_has_subordinate(dev))
  89. pci_walk_bus(dev->subordinate,
  90. pci_dev_set_disconnected, NULL);
  91. pci_stop_and_remove_bus_device(dev);
  92. pci_dev_put(dev);
  93. }
  94. pci_unlock_rescan_remove();
  95. dpc_wait_link_inactive(dpc);
  96. if (dpc->rp_extensions && dpc_wait_rp_inactive(dpc))
  97. return;
  98. if (dpc->rp_extensions && dpc->rp_pio_status) {
  99. pci_write_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_STATUS,
  100. dpc->rp_pio_status);
  101. dpc->rp_pio_status = 0;
  102. }
  103. pci_write_config_word(pdev, cap + PCI_EXP_DPC_STATUS,
  104. PCI_EXP_DPC_STATUS_TRIGGER | PCI_EXP_DPC_STATUS_INTERRUPT);
  105. pci_read_config_word(pdev, cap + PCI_EXP_DPC_CTL, &ctl);
  106. pci_write_config_word(pdev, cap + PCI_EXP_DPC_CTL,
  107. ctl | PCI_EXP_DPC_CTL_INT_EN);
  108. }
  109. static void dpc_process_rp_pio_error(struct dpc_dev *dpc)
  110. {
  111. struct device *dev = &dpc->dev->device;
  112. struct pci_dev *pdev = dpc->dev->port;
  113. u16 cap = dpc->cap_pos, dpc_status, first_error;
  114. u32 status, mask, sev, syserr, exc, dw0, dw1, dw2, dw3, log, prefix;
  115. int i;
  116. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_STATUS, &status);
  117. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_MASK, &mask);
  118. dev_err(dev, "rp_pio_status: %#010x, rp_pio_mask: %#010x\n",
  119. status, mask);
  120. dpc->rp_pio_status = status;
  121. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_SEVERITY, &sev);
  122. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_SYSERROR, &syserr);
  123. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_EXCEPTION, &exc);
  124. dev_err(dev, "RP PIO severity=%#010x, syserror=%#010x, exception=%#010x\n",
  125. sev, syserr, exc);
  126. /* Get First Error Pointer */
  127. pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &dpc_status);
  128. first_error = (dpc_status & 0x1f00) >> 8;
  129. status &= ~mask;
  130. for (i = 0; i < ARRAY_SIZE(rp_pio_error_string); i++) {
  131. if (status & (1 << i))
  132. dev_err(dev, "[%2d] %s%s\n", i, rp_pio_error_string[i],
  133. first_error == i ? " (First)" : "");
  134. }
  135. if (dpc->rp_log_size < 4)
  136. return;
  137. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG,
  138. &dw0);
  139. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 4,
  140. &dw1);
  141. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 8,
  142. &dw2);
  143. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 12,
  144. &dw3);
  145. dev_err(dev, "TLP Header: %#010x %#010x %#010x %#010x\n",
  146. dw0, dw1, dw2, dw3);
  147. if (dpc->rp_log_size < 5)
  148. return;
  149. pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_IMPSPEC_LOG, &log);
  150. dev_err(dev, "RP PIO ImpSpec Log %#010x\n", log);
  151. for (i = 0; i < dpc->rp_log_size - 5; i++) {
  152. pci_read_config_dword(pdev,
  153. cap + PCI_EXP_DPC_RP_PIO_TLPPREFIX_LOG, &prefix);
  154. dev_err(dev, "TLP Prefix Header: dw%d, %#010x\n", i, prefix);
  155. }
  156. }
  157. static irqreturn_t dpc_irq(int irq, void *context)
  158. {
  159. struct dpc_dev *dpc = (struct dpc_dev *)context;
  160. struct pci_dev *pdev = dpc->dev->port;
  161. struct device *dev = &dpc->dev->device;
  162. u16 cap = dpc->cap_pos, ctl, status, source, reason, ext_reason;
  163. pci_read_config_word(pdev, cap + PCI_EXP_DPC_CTL, &ctl);
  164. if (!(ctl & PCI_EXP_DPC_CTL_INT_EN) || ctl == (u16)(~0))
  165. return IRQ_NONE;
  166. pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
  167. if (!(status & PCI_EXP_DPC_STATUS_INTERRUPT))
  168. return IRQ_NONE;
  169. if (!(status & PCI_EXP_DPC_STATUS_TRIGGER)) {
  170. pci_write_config_word(pdev, cap + PCI_EXP_DPC_STATUS,
  171. PCI_EXP_DPC_STATUS_INTERRUPT);
  172. return IRQ_HANDLED;
  173. }
  174. pci_write_config_word(pdev, cap + PCI_EXP_DPC_CTL,
  175. ctl & ~PCI_EXP_DPC_CTL_INT_EN);
  176. pci_read_config_word(pdev, cap + PCI_EXP_DPC_SOURCE_ID,
  177. &source);
  178. dev_info(dev, "DPC containment event, status:%#06x source:%#06x\n",
  179. status, source);
  180. reason = (status & PCI_EXP_DPC_STATUS_TRIGGER_RSN) >> 1;
  181. ext_reason = (status & PCI_EXP_DPC_STATUS_TRIGGER_RSN_EXT) >> 5;
  182. dev_warn(dev, "DPC %s detected, remove downstream devices\n",
  183. (reason == 0) ? "unmasked uncorrectable error" :
  184. (reason == 1) ? "ERR_NONFATAL" :
  185. (reason == 2) ? "ERR_FATAL" :
  186. (ext_reason == 0) ? "RP PIO error" :
  187. (ext_reason == 1) ? "software trigger" :
  188. "reserved error");
  189. /* show RP PIO error detail information */
  190. if (dpc->rp_extensions && reason == 3 && ext_reason == 0)
  191. dpc_process_rp_pio_error(dpc);
  192. schedule_work(&dpc->work);
  193. return IRQ_HANDLED;
  194. }
  195. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  196. static int dpc_probe(struct pcie_device *dev)
  197. {
  198. struct dpc_dev *dpc;
  199. struct pci_dev *pdev = dev->port;
  200. struct device *device = &dev->device;
  201. int status;
  202. u16 ctl, cap;
  203. if (pcie_aer_get_firmware_first(pdev))
  204. return -ENOTSUPP;
  205. dpc = devm_kzalloc(device, sizeof(*dpc), GFP_KERNEL);
  206. if (!dpc)
  207. return -ENOMEM;
  208. dpc->cap_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DPC);
  209. dpc->dev = dev;
  210. INIT_WORK(&dpc->work, dpc_work);
  211. set_service_data(dev, dpc);
  212. status = devm_request_irq(device, dev->irq, dpc_irq, IRQF_SHARED,
  213. "pcie-dpc", dpc);
  214. if (status) {
  215. dev_warn(device, "request IRQ%d failed: %d\n", dev->irq,
  216. status);
  217. return status;
  218. }
  219. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CAP, &cap);
  220. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
  221. dpc->rp_extensions = (cap & PCI_EXP_DPC_CAP_RP_EXT);
  222. if (dpc->rp_extensions) {
  223. dpc->rp_log_size = (cap & PCI_EXP_DPC_RP_PIO_LOG_SIZE) >> 8;
  224. if (dpc->rp_log_size < 4 || dpc->rp_log_size > 9) {
  225. dev_err(device, "RP PIO log size %u is invalid\n",
  226. dpc->rp_log_size);
  227. dpc->rp_log_size = 0;
  228. }
  229. }
  230. ctl = (ctl & 0xfff4) | PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN;
  231. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
  232. dev_info(device, "DPC error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
  233. cap & PCI_EXP_DPC_IRQ, FLAG(cap, PCI_EXP_DPC_CAP_RP_EXT),
  234. FLAG(cap, PCI_EXP_DPC_CAP_POISONED_TLP),
  235. FLAG(cap, PCI_EXP_DPC_CAP_SW_TRIGGER), dpc->rp_log_size,
  236. FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE));
  237. return status;
  238. }
  239. static void dpc_remove(struct pcie_device *dev)
  240. {
  241. struct dpc_dev *dpc = get_service_data(dev);
  242. struct pci_dev *pdev = dev->port;
  243. u16 ctl;
  244. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
  245. ctl &= ~(PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN);
  246. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
  247. }
  248. static struct pcie_port_service_driver dpcdriver = {
  249. .name = "dpc",
  250. .port_type = PCIE_ANY_PORT,
  251. .service = PCIE_PORT_SERVICE_DPC,
  252. .probe = dpc_probe,
  253. .remove = dpc_remove,
  254. };
  255. static int __init dpc_service_init(void)
  256. {
  257. return pcie_port_service_register(&dpcdriver);
  258. }
  259. device_initcall(dpc_service_init);