dpc.c 9.5 KB

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