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