Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 1 | /* |
| 2 | * PCI Express Downstream Port Containment services driver |
Paul Gortmaker | 61612e6 | 2016-08-24 16:57:44 -0400 | [diff] [blame] | 3 | * Author: Keith Busch <keith.busch@intel.com> |
| 4 | * |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 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 | |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/interrupt.h> |
Paul Gortmaker | 61612e6 | 2016-08-24 16:57:44 -0400 | [diff] [blame] | 14 | #include <linux/init.h> |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 15 | #include <linux/pci.h> |
| 16 | #include <linux/pcieport_if.h> |
Keith Busch | 89ee9f7 | 2017-03-29 22:48:59 -0500 | [diff] [blame] | 17 | #include "../pci.h" |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 18 | |
| 19 | struct dpc_dev { |
| 20 | struct pcie_device *dev; |
Mika Westerberg | 14a16d5 | 2016-06-06 16:06:08 +0300 | [diff] [blame] | 21 | struct work_struct work; |
| 22 | int cap_pos; |
Keith Busch | abdbf4d | 2017-02-03 16:46:13 -0500 | [diff] [blame] | 23 | bool rp; |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 24 | }; |
| 25 | |
Keith Busch | abdbf4d | 2017-02-03 16:46:13 -0500 | [diff] [blame] | 26 | static int dpc_wait_rp_inactive(struct dpc_dev *dpc) |
| 27 | { |
| 28 | unsigned long timeout = jiffies + HZ; |
| 29 | struct pci_dev *pdev = dpc->dev->port; |
| 30 | u16 status; |
| 31 | |
| 32 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status); |
| 33 | while (status & PCI_EXP_DPC_RP_BUSY && |
| 34 | !time_after(jiffies, timeout)) { |
| 35 | msleep(10); |
| 36 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status); |
| 37 | } |
| 38 | if (status & PCI_EXP_DPC_RP_BUSY) { |
| 39 | dev_warn(&pdev->dev, "DPC root port still busy\n"); |
| 40 | return -EBUSY; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 45 | static void dpc_wait_link_inactive(struct pci_dev *pdev) |
| 46 | { |
| 47 | unsigned long timeout = jiffies + HZ; |
| 48 | u16 lnk_status; |
| 49 | |
| 50 | pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status); |
| 51 | while (lnk_status & PCI_EXP_LNKSTA_DLLLA && |
| 52 | !time_after(jiffies, timeout)) { |
| 53 | msleep(10); |
| 54 | pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status); |
| 55 | } |
| 56 | if (lnk_status & PCI_EXP_LNKSTA_DLLLA) |
Keith Busch | abdbf4d | 2017-02-03 16:46:13 -0500 | [diff] [blame] | 57 | dev_warn(&pdev->dev, "Link state not disabled for DPC event\n"); |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void interrupt_event_handler(struct work_struct *work) |
| 61 | { |
| 62 | struct dpc_dev *dpc = container_of(work, struct dpc_dev, work); |
| 63 | struct pci_dev *dev, *temp, *pdev = dpc->dev->port; |
| 64 | struct pci_bus *parent = pdev->subordinate; |
| 65 | |
| 66 | pci_lock_rescan_remove(); |
| 67 | list_for_each_entry_safe_reverse(dev, temp, &parent->devices, |
| 68 | bus_list) { |
| 69 | pci_dev_get(dev); |
Keith Busch | 89ee9f7 | 2017-03-29 22:48:59 -0500 | [diff] [blame] | 70 | pci_dev_set_disconnected(dev, NULL); |
| 71 | if (pci_has_subordinate(dev)) |
| 72 | pci_walk_bus(dev->subordinate, |
| 73 | pci_dev_set_disconnected, NULL); |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 74 | pci_stop_and_remove_bus_device(dev); |
| 75 | pci_dev_put(dev); |
| 76 | } |
| 77 | pci_unlock_rescan_remove(); |
| 78 | |
| 79 | dpc_wait_link_inactive(pdev); |
Keith Busch | abdbf4d | 2017-02-03 16:46:13 -0500 | [diff] [blame] | 80 | if (dpc->rp && dpc_wait_rp_inactive(dpc)) |
| 81 | return; |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 82 | pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, |
| 83 | PCI_EXP_DPC_STATUS_TRIGGER | PCI_EXP_DPC_STATUS_INTERRUPT); |
| 84 | } |
| 85 | |
| 86 | static irqreturn_t dpc_irq(int irq, void *context) |
| 87 | { |
| 88 | struct dpc_dev *dpc = (struct dpc_dev *)context; |
| 89 | struct pci_dev *pdev = dpc->dev->port; |
| 90 | u16 status, source; |
| 91 | |
| 92 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_STATUS, &status); |
| 93 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_SOURCE_ID, |
| 94 | &source); |
| 95 | if (!status) |
| 96 | return IRQ_NONE; |
| 97 | |
| 98 | dev_info(&dpc->dev->device, "DPC containment event, status:%#06x source:%#06x\n", |
| 99 | status, source); |
| 100 | |
| 101 | if (status & PCI_EXP_DPC_STATUS_TRIGGER) { |
| 102 | u16 reason = (status >> 1) & 0x3; |
Keith Busch | 87b336d | 2017-02-03 16:46:12 -0500 | [diff] [blame] | 103 | u16 ext_reason = (status >> 5) & 0x3; |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 104 | |
Keith Busch | 87b336d | 2017-02-03 16:46:12 -0500 | [diff] [blame] | 105 | dev_warn(&dpc->dev->device, "DPC %s detected, remove downstream devices\n", |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 106 | (reason == 0) ? "unmasked uncorrectable error" : |
| 107 | (reason == 1) ? "ERR_NONFATAL" : |
Keith Busch | 87b336d | 2017-02-03 16:46:12 -0500 | [diff] [blame] | 108 | (reason == 2) ? "ERR_FATAL" : |
| 109 | (ext_reason == 0) ? "RP PIO error" : |
| 110 | (ext_reason == 1) ? "software trigger" : |
| 111 | "reserved error"); |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 112 | schedule_work(&dpc->work); |
| 113 | } |
| 114 | return IRQ_HANDLED; |
| 115 | } |
| 116 | |
| 117 | #define FLAG(x, y) (((x) & (y)) ? '+' : '-') |
| 118 | static int dpc_probe(struct pcie_device *dev) |
| 119 | { |
| 120 | struct dpc_dev *dpc; |
| 121 | struct pci_dev *pdev = dev->port; |
| 122 | int status; |
| 123 | u16 ctl, cap; |
| 124 | |
Mika Westerberg | 733f3d1 | 2016-06-06 16:06:07 +0300 | [diff] [blame] | 125 | dpc = devm_kzalloc(&dev->device, sizeof(*dpc), GFP_KERNEL); |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 126 | if (!dpc) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | dpc->cap_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DPC); |
| 130 | dpc->dev = dev; |
| 131 | INIT_WORK(&dpc->work, interrupt_event_handler); |
| 132 | set_service_data(dev, dpc); |
| 133 | |
Mika Westerberg | 733f3d1 | 2016-06-06 16:06:07 +0300 | [diff] [blame] | 134 | status = devm_request_irq(&dev->device, dev->irq, dpc_irq, IRQF_SHARED, |
| 135 | "pcie-dpc", dpc); |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 136 | if (status) { |
| 137 | dev_warn(&dev->device, "request IRQ%d failed: %d\n", dev->irq, |
| 138 | status); |
Mika Westerberg | 733f3d1 | 2016-06-06 16:06:07 +0300 | [diff] [blame] | 139 | return status; |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CAP, &cap); |
| 143 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl); |
| 144 | |
Keith Busch | abdbf4d | 2017-02-03 16:46:13 -0500 | [diff] [blame] | 145 | dpc->rp = (cap & PCI_EXP_DPC_CAP_RP_EXT); |
| 146 | |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 147 | ctl |= PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN; |
| 148 | pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl); |
| 149 | |
| 150 | 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", |
| 151 | cap & 0xf, FLAG(cap, PCI_EXP_DPC_CAP_RP_EXT), |
| 152 | FLAG(cap, PCI_EXP_DPC_CAP_POISONED_TLP), |
| 153 | FLAG(cap, PCI_EXP_DPC_CAP_SW_TRIGGER), (cap >> 8) & 0xf, |
| 154 | FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE)); |
| 155 | return status; |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void dpc_remove(struct pcie_device *dev) |
| 159 | { |
| 160 | struct dpc_dev *dpc = get_service_data(dev); |
| 161 | struct pci_dev *pdev = dev->port; |
| 162 | u16 ctl; |
| 163 | |
| 164 | pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl); |
| 165 | ctl &= ~(PCI_EXP_DPC_CTL_EN_NONFATAL | PCI_EXP_DPC_CTL_INT_EN); |
| 166 | pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl); |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static struct pcie_port_service_driver dpcdriver = { |
| 170 | .name = "dpc", |
Keith Busch | 7e16fd6 | 2016-07-06 10:06:00 -0600 | [diff] [blame] | 171 | .port_type = PCIE_ANY_PORT, |
Keith Busch | 26e5157 | 2016-04-28 16:24:48 -0600 | [diff] [blame] | 172 | .service = PCIE_PORT_SERVICE_DPC, |
| 173 | .probe = dpc_probe, |
| 174 | .remove = dpc_remove, |
| 175 | }; |
| 176 | |
| 177 | static int __init dpc_service_init(void) |
| 178 | { |
| 179 | return pcie_port_service_register(&dpcdriver); |
| 180 | } |
Paul Gortmaker | 61612e6 | 2016-08-24 16:57:44 -0400 | [diff] [blame] | 181 | device_initcall(dpc_service_init); |