pcie-dpc.c 9.7 KB

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