pcie-dpc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * PCI Express Downstream Port Containment services driver
  3. * Author: Keith Busch <keith.busch@intel.com>
  4. *
  5. * Copyright (C) 2016 Intel Corp.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/pcieport_if.h>
  16. #include "../pci.h"
  17. struct dpc_dev {
  18. struct pcie_device *dev;
  19. struct work_struct work;
  20. int cap_pos;
  21. bool rp;
  22. };
  23. static int dpc_wait_rp_inactive(struct dpc_dev *dpc)
  24. {
  25. unsigned long timeout = jiffies + HZ;
  26. struct pci_dev *pdev = dpc->dev->port;
  27. u16 status;
  28. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status);
  29. while (status & PCI_EXP_DPC_RP_BUSY &&
  30. !time_after(jiffies, timeout)) {
  31. msleep(10);
  32. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status);
  33. }
  34. if (status & PCI_EXP_DPC_RP_BUSY) {
  35. dev_warn(&pdev->dev, "DPC root port still busy\n");
  36. return -EBUSY;
  37. }
  38. return 0;
  39. }
  40. static void dpc_wait_link_inactive(struct pci_dev *pdev)
  41. {
  42. unsigned long timeout = jiffies + HZ;
  43. u16 lnk_status;
  44. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  45. while (lnk_status & PCI_EXP_LNKSTA_DLLLA &&
  46. !time_after(jiffies, timeout)) {
  47. msleep(10);
  48. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  49. }
  50. if (lnk_status & PCI_EXP_LNKSTA_DLLLA)
  51. dev_warn(&pdev->dev, "Link state not disabled for DPC event\n");
  52. }
  53. static void interrupt_event_handler(struct work_struct *work)
  54. {
  55. struct dpc_dev *dpc = container_of(work, struct dpc_dev, work);
  56. struct pci_dev *dev, *temp, *pdev = dpc->dev->port;
  57. struct pci_bus *parent = pdev->subordinate;
  58. pci_lock_rescan_remove();
  59. list_for_each_entry_safe_reverse(dev, temp, &parent->devices,
  60. bus_list) {
  61. pci_dev_get(dev);
  62. pci_dev_set_disconnected(dev, NULL);
  63. if (pci_has_subordinate(dev))
  64. pci_walk_bus(dev->subordinate,
  65. pci_dev_set_disconnected, NULL);
  66. pci_stop_and_remove_bus_device(dev);
  67. pci_dev_put(dev);
  68. }
  69. pci_unlock_rescan_remove();
  70. dpc_wait_link_inactive(pdev);
  71. if (dpc->rp && dpc_wait_rp_inactive(dpc))
  72. return;
  73. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS,
  74. PCI_EXP_DPC_STATUS_TRIGGER | PCI_EXP_DPC_STATUS_INTERRUPT);
  75. }
  76. static irqreturn_t dpc_irq(int irq, void *context)
  77. {
  78. struct dpc_dev *dpc = (struct dpc_dev *)context;
  79. struct pci_dev *pdev = dpc->dev->port;
  80. u16 status, source;
  81. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status);
  82. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_SOURCE_ID,
  83. &source);
  84. if (!status)
  85. return IRQ_NONE;
  86. dev_info(&dpc->dev->device, "DPC containment event, status:%#06x source:%#06x\n",
  87. status, source);
  88. if (status & PCI_EXP_DPC_STATUS_TRIGGER) {
  89. u16 reason = (status >> 1) & 0x3;
  90. u16 ext_reason = (status >> 5) & 0x3;
  91. dev_warn(&dpc->dev->device, "DPC %s detected, remove downstream devices\n",
  92. (reason == 0) ? "unmasked uncorrectable error" :
  93. (reason == 1) ? "ERR_NONFATAL" :
  94. (reason == 2) ? "ERR_FATAL" :
  95. (ext_reason == 0) ? "RP PIO error" :
  96. (ext_reason == 1) ? "software trigger" :
  97. "reserved error");
  98. schedule_work(&dpc->work);
  99. }
  100. return IRQ_HANDLED;
  101. }
  102. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  103. static int dpc_probe(struct pcie_device *dev)
  104. {
  105. struct dpc_dev *dpc;
  106. struct pci_dev *pdev = dev->port;
  107. int status;
  108. u16 ctl, cap;
  109. dpc = devm_kzalloc(&dev->device, sizeof(*dpc), GFP_KERNEL);
  110. if (!dpc)
  111. return -ENOMEM;
  112. dpc->cap_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DPC);
  113. dpc->dev = dev;
  114. INIT_WORK(&dpc->work, interrupt_event_handler);
  115. set_service_data(dev, dpc);
  116. status = devm_request_irq(&dev->device, dev->irq, dpc_irq, IRQF_SHARED,
  117. "pcie-dpc", dpc);
  118. if (status) {
  119. dev_warn(&dev->device, "request IRQ%d failed: %d\n", dev->irq,
  120. status);
  121. return status;
  122. }
  123. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CAP, &cap);
  124. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
  125. dpc->rp = (cap & PCI_EXP_DPC_CAP_RP_EXT);
  126. ctl |= PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN;
  127. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
  128. dev_info(&dev->device, "DPC error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
  129. cap & 0xf, FLAG(cap, PCI_EXP_DPC_CAP_RP_EXT),
  130. FLAG(cap, PCI_EXP_DPC_CAP_POISONED_TLP),
  131. FLAG(cap, PCI_EXP_DPC_CAP_SW_TRIGGER), (cap >> 8) & 0xf,
  132. FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE));
  133. return status;
  134. }
  135. static void dpc_remove(struct pcie_device *dev)
  136. {
  137. struct dpc_dev *dpc = get_service_data(dev);
  138. struct pci_dev *pdev = dev->port;
  139. u16 ctl;
  140. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
  141. ctl &= ~(PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN);
  142. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
  143. }
  144. static struct pcie_port_service_driver dpcdriver = {
  145. .name = "dpc",
  146. .port_type = PCIE_ANY_PORT,
  147. .service = PCIE_PORT_SERVICE_DPC,
  148. .probe = dpc_probe,
  149. .remove = dpc_remove,
  150. };
  151. static int __init dpc_service_init(void)
  152. {
  153. return pcie_port_service_register(&dpcdriver);
  154. }
  155. device_initcall(dpc_service_init);