pcie-dpc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. struct dpc_dev {
  17. struct pcie_device *dev;
  18. struct work_struct work;
  19. int cap_pos;
  20. };
  21. static void dpc_wait_link_inactive(struct pci_dev *pdev)
  22. {
  23. unsigned long timeout = jiffies + HZ;
  24. u16 lnk_status;
  25. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  26. while (lnk_status & PCI_EXP_LNKSTA_DLLLA &&
  27. !time_after(jiffies, timeout)) {
  28. msleep(10);
  29. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  30. }
  31. if (lnk_status & PCI_EXP_LNKSTA_DLLLA)
  32. dev_warn(&pdev->dev, "Link state not disabled for DPC event");
  33. }
  34. static void interrupt_event_handler(struct work_struct *work)
  35. {
  36. struct dpc_dev *dpc = container_of(work, struct dpc_dev, work);
  37. struct pci_dev *dev, *temp, *pdev = dpc->dev->port;
  38. struct pci_bus *parent = pdev->subordinate;
  39. pci_lock_rescan_remove();
  40. list_for_each_entry_safe_reverse(dev, temp, &parent->devices,
  41. bus_list) {
  42. pci_dev_get(dev);
  43. pci_stop_and_remove_bus_device(dev);
  44. pci_dev_put(dev);
  45. }
  46. pci_unlock_rescan_remove();
  47. dpc_wait_link_inactive(pdev);
  48. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS,
  49. PCI_EXP_DPC_STATUS_TRIGGER | PCI_EXP_DPC_STATUS_INTERRUPT);
  50. }
  51. static irqreturn_t dpc_irq(int irq, void *context)
  52. {
  53. struct dpc_dev *dpc = (struct dpc_dev *)context;
  54. struct pci_dev *pdev = dpc->dev->port;
  55. u16 status, source;
  56. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status);
  57. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_SOURCE_ID,
  58. &source);
  59. if (!status)
  60. return IRQ_NONE;
  61. dev_info(&dpc->dev->device, "DPC containment event, status:%#06x source:%#06x\n",
  62. status, source);
  63. if (status & PCI_EXP_DPC_STATUS_TRIGGER) {
  64. u16 reason = (status >> 1) & 0x3;
  65. dev_warn(&dpc->dev->device, "DPC %s triggered, remove downstream devices\n",
  66. (reason == 0) ? "unmasked uncorrectable error" :
  67. (reason == 1) ? "ERR_NONFATAL" :
  68. (reason == 2) ? "ERR_FATAL" : "extended error");
  69. schedule_work(&dpc->work);
  70. }
  71. return IRQ_HANDLED;
  72. }
  73. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  74. static int dpc_probe(struct pcie_device *dev)
  75. {
  76. struct dpc_dev *dpc;
  77. struct pci_dev *pdev = dev->port;
  78. int status;
  79. u16 ctl, cap;
  80. dpc = devm_kzalloc(&dev->device, sizeof(*dpc), GFP_KERNEL);
  81. if (!dpc)
  82. return -ENOMEM;
  83. dpc->cap_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DPC);
  84. dpc->dev = dev;
  85. INIT_WORK(&dpc->work, interrupt_event_handler);
  86. set_service_data(dev, dpc);
  87. status = devm_request_irq(&dev->device, dev->irq, dpc_irq, IRQF_SHARED,
  88. "pcie-dpc", dpc);
  89. if (status) {
  90. dev_warn(&dev->device, "request IRQ%d failed: %d\n", dev->irq,
  91. status);
  92. return status;
  93. }
  94. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CAP, &cap);
  95. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
  96. ctl |= PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN;
  97. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
  98. 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",
  99. cap & 0xf, FLAG(cap, PCI_EXP_DPC_CAP_RP_EXT),
  100. FLAG(cap, PCI_EXP_DPC_CAP_POISONED_TLP),
  101. FLAG(cap, PCI_EXP_DPC_CAP_SW_TRIGGER), (cap >> 8) & 0xf,
  102. FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE));
  103. return status;
  104. }
  105. static void dpc_remove(struct pcie_device *dev)
  106. {
  107. struct dpc_dev *dpc = get_service_data(dev);
  108. struct pci_dev *pdev = dev->port;
  109. u16 ctl;
  110. pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
  111. ctl &= ~(PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN);
  112. pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
  113. }
  114. static struct pcie_port_service_driver dpcdriver = {
  115. .name = "dpc",
  116. .port_type = PCIE_ANY_PORT,
  117. .service = PCIE_PORT_SERVICE_DPC,
  118. .probe = dpc_probe,
  119. .remove = dpc_remove,
  120. };
  121. static int __init dpc_service_init(void)
  122. {
  123. return pcie_port_service_register(&dpcdriver);
  124. }
  125. device_initcall(dpc_service_init);