dpc.c 9.1 KB

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