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