dpc.c 9.1 KB

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