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