]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/pcie/aer/aer_inject.c
Merge remote-tracking branch 'thermal-soc/next'
[karo-tx-linux.git] / drivers / pci / pcie / aer / aer_inject.c
1 /*
2  * PCIe AER software error injection support.
3  *
4  * Debuging PCIe AER code is quite difficult because it is hard to
5  * trigger various real hardware errors. Software based error
6  * injection can fake almost all kinds of errors with the help of a
7  * user space helper tool aer-inject, which can be gotten from:
8  *   http://www.kernel.org/pub/linux/utils/pci/aer-inject/
9  *
10  * Copyright 2009 Intel Corporation.
11  *     Huang Ying <ying.huang@intel.com>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; version 2
16  * of the License.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/miscdevice.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/fs.h>
26 #include <linux/uaccess.h>
27 #include <linux/stddef.h>
28 #include "aerdrv.h"
29
30 /* Override the existing corrected and uncorrected error masks */
31 static bool aer_mask_override;
32 module_param(aer_mask_override, bool, 0);
33
34 struct aer_error_inj {
35         u8 bus;
36         u8 dev;
37         u8 fn;
38         u32 uncor_status;
39         u32 cor_status;
40         u32 header_log0;
41         u32 header_log1;
42         u32 header_log2;
43         u32 header_log3;
44         u32 domain;
45 };
46
47 struct aer_error {
48         struct list_head list;
49         u32 domain;
50         unsigned int bus;
51         unsigned int devfn;
52         int pos_cap_err;
53
54         u32 uncor_status;
55         u32 cor_status;
56         u32 header_log0;
57         u32 header_log1;
58         u32 header_log2;
59         u32 header_log3;
60         u32 root_status;
61         u32 source_id;
62 };
63
64 struct pci_bus_ops {
65         struct list_head list;
66         struct pci_bus *bus;
67         struct pci_ops *ops;
68 };
69
70 static LIST_HEAD(einjected);
71
72 static LIST_HEAD(pci_bus_ops_list);
73
74 /* Protect einjected and pci_bus_ops_list */
75 static DEFINE_SPINLOCK(inject_lock);
76
77 static void aer_error_init(struct aer_error *err, u32 domain,
78                            unsigned int bus, unsigned int devfn,
79                            int pos_cap_err)
80 {
81         INIT_LIST_HEAD(&err->list);
82         err->domain = domain;
83         err->bus = bus;
84         err->devfn = devfn;
85         err->pos_cap_err = pos_cap_err;
86 }
87
88 /* inject_lock must be held before calling */
89 static struct aer_error *__find_aer_error(u32 domain, unsigned int bus,
90                                           unsigned int devfn)
91 {
92         struct aer_error *err;
93
94         list_for_each_entry(err, &einjected, list) {
95                 if (domain == err->domain &&
96                     bus == err->bus &&
97                     devfn == err->devfn)
98                         return err;
99         }
100         return NULL;
101 }
102
103 /* inject_lock must be held before calling */
104 static struct aer_error *__find_aer_error_by_dev(struct pci_dev *dev)
105 {
106         int domain = pci_domain_nr(dev->bus);
107         if (domain < 0)
108                 return NULL;
109         return __find_aer_error(domain, dev->bus->number, dev->devfn);
110 }
111
112 /* inject_lock must be held before calling */
113 static struct pci_ops *__find_pci_bus_ops(struct pci_bus *bus)
114 {
115         struct pci_bus_ops *bus_ops;
116
117         list_for_each_entry(bus_ops, &pci_bus_ops_list, list) {
118                 if (bus_ops->bus == bus)
119                         return bus_ops->ops;
120         }
121         return NULL;
122 }
123
124 static struct pci_bus_ops *pci_bus_ops_pop(void)
125 {
126         unsigned long flags;
127         struct pci_bus_ops *bus_ops;
128
129         spin_lock_irqsave(&inject_lock, flags);
130         bus_ops = list_first_entry_or_null(&pci_bus_ops_list,
131                                            struct pci_bus_ops, list);
132         if (bus_ops)
133                 list_del(&bus_ops->list);
134         spin_unlock_irqrestore(&inject_lock, flags);
135         return bus_ops;
136 }
137
138 static u32 *find_pci_config_dword(struct aer_error *err, int where,
139                                   int *prw1cs)
140 {
141         int rw1cs = 0;
142         u32 *target = NULL;
143
144         if (err->pos_cap_err == -1)
145                 return NULL;
146
147         switch (where - err->pos_cap_err) {
148         case PCI_ERR_UNCOR_STATUS:
149                 target = &err->uncor_status;
150                 rw1cs = 1;
151                 break;
152         case PCI_ERR_COR_STATUS:
153                 target = &err->cor_status;
154                 rw1cs = 1;
155                 break;
156         case PCI_ERR_HEADER_LOG:
157                 target = &err->header_log0;
158                 break;
159         case PCI_ERR_HEADER_LOG+4:
160                 target = &err->header_log1;
161                 break;
162         case PCI_ERR_HEADER_LOG+8:
163                 target = &err->header_log2;
164                 break;
165         case PCI_ERR_HEADER_LOG+12:
166                 target = &err->header_log3;
167                 break;
168         case PCI_ERR_ROOT_STATUS:
169                 target = &err->root_status;
170                 rw1cs = 1;
171                 break;
172         case PCI_ERR_ROOT_ERR_SRC:
173                 target = &err->source_id;
174                 break;
175         }
176         if (prw1cs)
177                 *prw1cs = rw1cs;
178         return target;
179 }
180
181 static int aer_inj_read_config(struct pci_bus *bus, unsigned int devfn,
182                                int where, int size, u32 *val)
183 {
184         u32 *sim;
185         struct aer_error *err;
186         unsigned long flags;
187         struct pci_ops *ops;
188         struct pci_ops *my_ops;
189         int domain;
190         int rv;
191
192         spin_lock_irqsave(&inject_lock, flags);
193         if (size != sizeof(u32))
194                 goto out;
195         domain = pci_domain_nr(bus);
196         if (domain < 0)
197                 goto out;
198         err = __find_aer_error(domain, bus->number, devfn);
199         if (!err)
200                 goto out;
201
202         sim = find_pci_config_dword(err, where, NULL);
203         if (sim) {
204                 *val = *sim;
205                 spin_unlock_irqrestore(&inject_lock, flags);
206                 return 0;
207         }
208 out:
209         ops = __find_pci_bus_ops(bus);
210         /*
211          * pci_lock must already be held, so we can directly
212          * manipulate bus->ops.  Many config access functions,
213          * including pci_generic_config_read() require the original
214          * bus->ops be installed to function, so temporarily put them
215          * back.
216          */
217         my_ops = bus->ops;
218         bus->ops = ops;
219         rv = ops->read(bus, devfn, where, size, val);
220         bus->ops = my_ops;
221         spin_unlock_irqrestore(&inject_lock, flags);
222         return rv;
223 }
224
225 static int aer_inj_write_config(struct pci_bus *bus, unsigned int devfn,
226                                 int where, int size, u32 val)
227 {
228         u32 *sim;
229         struct aer_error *err;
230         unsigned long flags;
231         int rw1cs;
232         struct pci_ops *ops;
233         struct pci_ops *my_ops;
234         int domain;
235         int rv;
236
237         spin_lock_irqsave(&inject_lock, flags);
238         if (size != sizeof(u32))
239                 goto out;
240         domain = pci_domain_nr(bus);
241         if (domain < 0)
242                 goto out;
243         err = __find_aer_error(domain, bus->number, devfn);
244         if (!err)
245                 goto out;
246
247         sim = find_pci_config_dword(err, where, &rw1cs);
248         if (sim) {
249                 if (rw1cs)
250                         *sim ^= val;
251                 else
252                         *sim = val;
253                 spin_unlock_irqrestore(&inject_lock, flags);
254                 return 0;
255         }
256 out:
257         ops = __find_pci_bus_ops(bus);
258         /*
259          * pci_lock must already be held, so we can directly
260          * manipulate bus->ops.  Many config access functions,
261          * including pci_generic_config_write() require the original
262          * bus->ops be installed to function, so temporarily put them
263          * back.
264          */
265         my_ops = bus->ops;
266         bus->ops = ops;
267         rv = ops->write(bus, devfn, where, size, val);
268         bus->ops = my_ops;
269         spin_unlock_irqrestore(&inject_lock, flags);
270         return rv;
271 }
272
273 static struct pci_ops aer_inj_pci_ops = {
274         .read = aer_inj_read_config,
275         .write = aer_inj_write_config,
276 };
277
278 static void pci_bus_ops_init(struct pci_bus_ops *bus_ops,
279                              struct pci_bus *bus,
280                              struct pci_ops *ops)
281 {
282         INIT_LIST_HEAD(&bus_ops->list);
283         bus_ops->bus = bus;
284         bus_ops->ops = ops;
285 }
286
287 static int pci_bus_set_aer_ops(struct pci_bus *bus)
288 {
289         struct pci_ops *ops;
290         struct pci_bus_ops *bus_ops;
291         unsigned long flags;
292
293         bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL);
294         if (!bus_ops)
295                 return -ENOMEM;
296         ops = pci_bus_set_ops(bus, &aer_inj_pci_ops);
297         spin_lock_irqsave(&inject_lock, flags);
298         if (ops == &aer_inj_pci_ops)
299                 goto out;
300         pci_bus_ops_init(bus_ops, bus, ops);
301         list_add(&bus_ops->list, &pci_bus_ops_list);
302         bus_ops = NULL;
303 out:
304         spin_unlock_irqrestore(&inject_lock, flags);
305         kfree(bus_ops);
306         return 0;
307 }
308
309 static struct pci_dev *pcie_find_root_port(struct pci_dev *dev)
310 {
311         while (1) {
312                 if (!pci_is_pcie(dev))
313                         break;
314                 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
315                         return dev;
316                 if (!dev->bus->self)
317                         break;
318                 dev = dev->bus->self;
319         }
320         return NULL;
321 }
322
323 static int find_aer_device_iter(struct device *device, void *data)
324 {
325         struct pcie_device **result = data;
326         struct pcie_device *pcie_dev;
327
328         if (device->bus == &pcie_port_bus_type) {
329                 pcie_dev = to_pcie_device(device);
330                 if (pcie_dev->service & PCIE_PORT_SERVICE_AER) {
331                         *result = pcie_dev;
332                         return 1;
333                 }
334         }
335         return 0;
336 }
337
338 static int find_aer_device(struct pci_dev *dev, struct pcie_device **result)
339 {
340         return device_for_each_child(&dev->dev, result, find_aer_device_iter);
341 }
342
343 static int aer_inject(struct aer_error_inj *einj)
344 {
345         struct aer_error *err, *rperr;
346         struct aer_error *err_alloc = NULL, *rperr_alloc = NULL;
347         struct pci_dev *dev, *rpdev;
348         struct pcie_device *edev;
349         unsigned long flags;
350         unsigned int devfn = PCI_DEVFN(einj->dev, einj->fn);
351         int pos_cap_err, rp_pos_cap_err;
352         u32 sever, cor_mask, uncor_mask, cor_mask_orig = 0, uncor_mask_orig = 0;
353         int ret = 0;
354
355         dev = pci_get_domain_bus_and_slot(einj->domain, einj->bus, devfn);
356         if (!dev)
357                 return -ENODEV;
358         rpdev = pcie_find_root_port(dev);
359         if (!rpdev) {
360                 ret = -ENODEV;
361                 goto out_put;
362         }
363
364         pos_cap_err = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
365         if (!pos_cap_err) {
366                 ret = -EPERM;
367                 goto out_put;
368         }
369         pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_SEVER, &sever);
370         pci_read_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK, &cor_mask);
371         pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
372                               &uncor_mask);
373
374         rp_pos_cap_err = pci_find_ext_capability(rpdev, PCI_EXT_CAP_ID_ERR);
375         if (!rp_pos_cap_err) {
376                 ret = -EPERM;
377                 goto out_put;
378         }
379
380         err_alloc =  kzalloc(sizeof(struct aer_error), GFP_KERNEL);
381         if (!err_alloc) {
382                 ret = -ENOMEM;
383                 goto out_put;
384         }
385         rperr_alloc =  kzalloc(sizeof(struct aer_error), GFP_KERNEL);
386         if (!rperr_alloc) {
387                 ret = -ENOMEM;
388                 goto out_put;
389         }
390
391         if (aer_mask_override) {
392                 cor_mask_orig = cor_mask;
393                 cor_mask &= !(einj->cor_status);
394                 pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
395                                        cor_mask);
396
397                 uncor_mask_orig = uncor_mask;
398                 uncor_mask &= !(einj->uncor_status);
399                 pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
400                                        uncor_mask);
401         }
402
403         spin_lock_irqsave(&inject_lock, flags);
404
405         err = __find_aer_error_by_dev(dev);
406         if (!err) {
407                 err = err_alloc;
408                 err_alloc = NULL;
409                 aer_error_init(err, einj->domain, einj->bus, devfn,
410                                pos_cap_err);
411                 list_add(&err->list, &einjected);
412         }
413         err->uncor_status |= einj->uncor_status;
414         err->cor_status |= einj->cor_status;
415         err->header_log0 = einj->header_log0;
416         err->header_log1 = einj->header_log1;
417         err->header_log2 = einj->header_log2;
418         err->header_log3 = einj->header_log3;
419
420         if (!aer_mask_override && einj->cor_status &&
421             !(einj->cor_status & ~cor_mask)) {
422                 ret = -EINVAL;
423                 printk(KERN_WARNING "The correctable error(s) is masked by device\n");
424                 spin_unlock_irqrestore(&inject_lock, flags);
425                 goto out_put;
426         }
427         if (!aer_mask_override && einj->uncor_status &&
428             !(einj->uncor_status & ~uncor_mask)) {
429                 ret = -EINVAL;
430                 printk(KERN_WARNING "The uncorrectable error(s) is masked by device\n");
431                 spin_unlock_irqrestore(&inject_lock, flags);
432                 goto out_put;
433         }
434
435         rperr = __find_aer_error_by_dev(rpdev);
436         if (!rperr) {
437                 rperr = rperr_alloc;
438                 rperr_alloc = NULL;
439                 aer_error_init(rperr, pci_domain_nr(rpdev->bus),
440                                rpdev->bus->number, rpdev->devfn,
441                                rp_pos_cap_err);
442                 list_add(&rperr->list, &einjected);
443         }
444         if (einj->cor_status) {
445                 if (rperr->root_status & PCI_ERR_ROOT_COR_RCV)
446                         rperr->root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
447                 else
448                         rperr->root_status |= PCI_ERR_ROOT_COR_RCV;
449                 rperr->source_id &= 0xffff0000;
450                 rperr->source_id |= (einj->bus << 8) | devfn;
451         }
452         if (einj->uncor_status) {
453                 if (rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV)
454                         rperr->root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
455                 if (sever & einj->uncor_status) {
456                         rperr->root_status |= PCI_ERR_ROOT_FATAL_RCV;
457                         if (!(rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV))
458                                 rperr->root_status |= PCI_ERR_ROOT_FIRST_FATAL;
459                 } else
460                         rperr->root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
461                 rperr->root_status |= PCI_ERR_ROOT_UNCOR_RCV;
462                 rperr->source_id &= 0x0000ffff;
463                 rperr->source_id |= ((einj->bus << 8) | devfn) << 16;
464         }
465         spin_unlock_irqrestore(&inject_lock, flags);
466
467         if (aer_mask_override) {
468                 pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
469                                        cor_mask_orig);
470                 pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
471                                        uncor_mask_orig);
472         }
473
474         ret = pci_bus_set_aer_ops(dev->bus);
475         if (ret)
476                 goto out_put;
477         ret = pci_bus_set_aer_ops(rpdev->bus);
478         if (ret)
479                 goto out_put;
480
481         if (find_aer_device(rpdev, &edev)) {
482                 if (!get_service_data(edev)) {
483                         printk(KERN_WARNING "AER service is not initialized\n");
484                         ret = -EINVAL;
485                         goto out_put;
486                 }
487                 aer_irq(-1, edev);
488         } else
489                 ret = -EINVAL;
490 out_put:
491         kfree(err_alloc);
492         kfree(rperr_alloc);
493         pci_dev_put(dev);
494         return ret;
495 }
496
497 static ssize_t aer_inject_write(struct file *filp, const char __user *ubuf,
498                                 size_t usize, loff_t *off)
499 {
500         struct aer_error_inj einj;
501         int ret;
502
503         if (!capable(CAP_SYS_ADMIN))
504                 return -EPERM;
505         if (usize < offsetof(struct aer_error_inj, domain) ||
506             usize > sizeof(einj))
507                 return -EINVAL;
508
509         memset(&einj, 0, sizeof(einj));
510         if (copy_from_user(&einj, ubuf, usize))
511                 return -EFAULT;
512
513         ret = aer_inject(&einj);
514         return ret ? ret : usize;
515 }
516
517 static const struct file_operations aer_inject_fops = {
518         .write = aer_inject_write,
519         .owner = THIS_MODULE,
520         .llseek = noop_llseek,
521 };
522
523 static struct miscdevice aer_inject_device = {
524         .minor = MISC_DYNAMIC_MINOR,
525         .name = "aer_inject",
526         .fops = &aer_inject_fops,
527 };
528
529 static int __init aer_inject_init(void)
530 {
531         return misc_register(&aer_inject_device);
532 }
533
534 static void __exit aer_inject_exit(void)
535 {
536         struct aer_error *err, *err_next;
537         unsigned long flags;
538         struct pci_bus_ops *bus_ops;
539
540         misc_deregister(&aer_inject_device);
541
542         while ((bus_ops = pci_bus_ops_pop())) {
543                 pci_bus_set_ops(bus_ops->bus, bus_ops->ops);
544                 kfree(bus_ops);
545         }
546
547         spin_lock_irqsave(&inject_lock, flags);
548         list_for_each_entry_safe(err, err_next, &einjected, list) {
549                 list_del(&err->list);
550                 kfree(err);
551         }
552         spin_unlock_irqrestore(&inject_lock, flags);
553 }
554
555 module_init(aer_inject_init);
556 module_exit(aer_inject_exit);
557
558 MODULE_DESCRIPTION("PCIe AER software error injector");
559 MODULE_LICENSE("GPL");