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