• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	u16 domain;
45 };
46 
47 struct aer_error {
48 	struct list_head list;
49 	u16 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 
aer_error_init(struct aer_error * err,u16 domain,unsigned int bus,unsigned int devfn,int pos_cap_err)77 static void aer_error_init(struct aer_error *err, u16 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 */
__find_aer_error(u16 domain,unsigned int bus,unsigned int devfn)89 static struct aer_error *__find_aer_error(u16 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 */
__find_aer_error_by_dev(struct pci_dev * dev)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((u16)domain, dev->bus->number, dev->devfn);
110 }
111 
112 /* inject_lock must be held before calling */
__find_pci_bus_ops(struct pci_bus * bus)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 
pci_bus_ops_pop(void)124 static struct pci_bus_ops *pci_bus_ops_pop(void)
125 {
126 	unsigned long flags;
127 	struct pci_bus_ops *bus_ops = NULL;
128 
129 	spin_lock_irqsave(&inject_lock, flags);
130 	if (list_empty(&pci_bus_ops_list))
131 		bus_ops = NULL;
132 	else {
133 		struct list_head *lh = pci_bus_ops_list.next;
134 		list_del(lh);
135 		bus_ops = list_entry(lh, struct pci_bus_ops, list);
136 	}
137 	spin_unlock_irqrestore(&inject_lock, flags);
138 	return bus_ops;
139 }
140 
find_pci_config_dword(struct aer_error * err,int where,int * prw1cs)141 static u32 *find_pci_config_dword(struct aer_error *err, int where,
142 				  int *prw1cs)
143 {
144 	int rw1cs = 0;
145 	u32 *target = NULL;
146 
147 	if (err->pos_cap_err == -1)
148 		return NULL;
149 
150 	switch (where - err->pos_cap_err) {
151 	case PCI_ERR_UNCOR_STATUS:
152 		target = &err->uncor_status;
153 		rw1cs = 1;
154 		break;
155 	case PCI_ERR_COR_STATUS:
156 		target = &err->cor_status;
157 		rw1cs = 1;
158 		break;
159 	case PCI_ERR_HEADER_LOG:
160 		target = &err->header_log0;
161 		break;
162 	case PCI_ERR_HEADER_LOG+4:
163 		target = &err->header_log1;
164 		break;
165 	case PCI_ERR_HEADER_LOG+8:
166 		target = &err->header_log2;
167 		break;
168 	case PCI_ERR_HEADER_LOG+12:
169 		target = &err->header_log3;
170 		break;
171 	case PCI_ERR_ROOT_STATUS:
172 		target = &err->root_status;
173 		rw1cs = 1;
174 		break;
175 	case PCI_ERR_ROOT_ERR_SRC:
176 		target = &err->source_id;
177 		break;
178 	}
179 	if (prw1cs)
180 		*prw1cs = rw1cs;
181 	return target;
182 }
183 
pci_read_aer(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)184 static int pci_read_aer(struct pci_bus *bus, unsigned int devfn, int where,
185 			int size, u32 *val)
186 {
187 	u32 *sim;
188 	struct aer_error *err;
189 	unsigned long flags;
190 	struct pci_ops *ops;
191 	int domain;
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((u16)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 	spin_unlock_irqrestore(&inject_lock, flags);
212 	return ops->read(bus, devfn, where, size, val);
213 }
214 
pci_write_aer(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)215 static int pci_write_aer(struct pci_bus *bus, unsigned int devfn, int where,
216 			 int size, u32 val)
217 {
218 	u32 *sim;
219 	struct aer_error *err;
220 	unsigned long flags;
221 	int rw1cs;
222 	struct pci_ops *ops;
223 	int domain;
224 
225 	spin_lock_irqsave(&inject_lock, flags);
226 	if (size != sizeof(u32))
227 		goto out;
228 	domain = pci_domain_nr(bus);
229 	if (domain < 0)
230 		goto out;
231 	err = __find_aer_error((u16)domain, bus->number, devfn);
232 	if (!err)
233 		goto out;
234 
235 	sim = find_pci_config_dword(err, where, &rw1cs);
236 	if (sim) {
237 		if (rw1cs)
238 			*sim ^= val;
239 		else
240 			*sim = val;
241 		spin_unlock_irqrestore(&inject_lock, flags);
242 		return 0;
243 	}
244 out:
245 	ops = __find_pci_bus_ops(bus);
246 	spin_unlock_irqrestore(&inject_lock, flags);
247 	return ops->write(bus, devfn, where, size, val);
248 }
249 
250 static struct pci_ops pci_ops_aer = {
251 	.read = pci_read_aer,
252 	.write = pci_write_aer,
253 };
254 
pci_bus_ops_init(struct pci_bus_ops * bus_ops,struct pci_bus * bus,struct pci_ops * ops)255 static void pci_bus_ops_init(struct pci_bus_ops *bus_ops,
256 			     struct pci_bus *bus,
257 			     struct pci_ops *ops)
258 {
259 	INIT_LIST_HEAD(&bus_ops->list);
260 	bus_ops->bus = bus;
261 	bus_ops->ops = ops;
262 }
263 
pci_bus_set_aer_ops(struct pci_bus * bus)264 static int pci_bus_set_aer_ops(struct pci_bus *bus)
265 {
266 	struct pci_ops *ops;
267 	struct pci_bus_ops *bus_ops;
268 	unsigned long flags;
269 
270 	bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL);
271 	if (!bus_ops)
272 		return -ENOMEM;
273 	ops = pci_bus_set_ops(bus, &pci_ops_aer);
274 	spin_lock_irqsave(&inject_lock, flags);
275 	if (ops == &pci_ops_aer)
276 		goto out;
277 	pci_bus_ops_init(bus_ops, bus, ops);
278 	list_add(&bus_ops->list, &pci_bus_ops_list);
279 	bus_ops = NULL;
280 out:
281 	spin_unlock_irqrestore(&inject_lock, flags);
282 	kfree(bus_ops);
283 	return 0;
284 }
285 
find_aer_device_iter(struct device * device,void * data)286 static int find_aer_device_iter(struct device *device, void *data)
287 {
288 	struct pcie_device **result = data;
289 	struct pcie_device *pcie_dev;
290 
291 	if (device->bus == &pcie_port_bus_type) {
292 		pcie_dev = to_pcie_device(device);
293 		if (pcie_dev->service & PCIE_PORT_SERVICE_AER) {
294 			*result = pcie_dev;
295 			return 1;
296 		}
297 	}
298 	return 0;
299 }
300 
find_aer_device(struct pci_dev * dev,struct pcie_device ** result)301 static int find_aer_device(struct pci_dev *dev, struct pcie_device **result)
302 {
303 	return device_for_each_child(&dev->dev, result, find_aer_device_iter);
304 }
305 
aer_inject(struct aer_error_inj * einj)306 static int aer_inject(struct aer_error_inj *einj)
307 {
308 	struct aer_error *err, *rperr;
309 	struct aer_error *err_alloc = NULL, *rperr_alloc = NULL;
310 	struct pci_dev *dev, *rpdev;
311 	struct pcie_device *edev;
312 	unsigned long flags;
313 	unsigned int devfn = PCI_DEVFN(einj->dev, einj->fn);
314 	int pos_cap_err, rp_pos_cap_err;
315 	u32 sever, cor_mask, uncor_mask, cor_mask_orig = 0, uncor_mask_orig = 0;
316 	int ret = 0;
317 
318 	dev = pci_get_domain_bus_and_slot((int)einj->domain, einj->bus, devfn);
319 	if (!dev)
320 		return -ENODEV;
321 	rpdev = pcie_find_root_port(dev);
322 	if (!rpdev) {
323 		ret = -ENODEV;
324 		goto out_put;
325 	}
326 
327 	pos_cap_err = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
328 	if (!pos_cap_err) {
329 		ret = -EPERM;
330 		goto out_put;
331 	}
332 	pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_SEVER, &sever);
333 	pci_read_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK, &cor_mask);
334 	pci_read_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
335 			      &uncor_mask);
336 
337 	rp_pos_cap_err = pci_find_ext_capability(rpdev, PCI_EXT_CAP_ID_ERR);
338 	if (!rp_pos_cap_err) {
339 		ret = -EPERM;
340 		goto out_put;
341 	}
342 
343 	err_alloc =  kzalloc(sizeof(struct aer_error), GFP_KERNEL);
344 	if (!err_alloc) {
345 		ret = -ENOMEM;
346 		goto out_put;
347 	}
348 	rperr_alloc =  kzalloc(sizeof(struct aer_error), GFP_KERNEL);
349 	if (!rperr_alloc) {
350 		ret = -ENOMEM;
351 		goto out_put;
352 	}
353 
354 	if (aer_mask_override) {
355 		cor_mask_orig = cor_mask;
356 		cor_mask &= !(einj->cor_status);
357 		pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
358 				       cor_mask);
359 
360 		uncor_mask_orig = uncor_mask;
361 		uncor_mask &= !(einj->uncor_status);
362 		pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
363 				       uncor_mask);
364 	}
365 
366 	spin_lock_irqsave(&inject_lock, flags);
367 
368 	err = __find_aer_error_by_dev(dev);
369 	if (!err) {
370 		err = err_alloc;
371 		err_alloc = NULL;
372 		aer_error_init(err, einj->domain, einj->bus, devfn,
373 			       pos_cap_err);
374 		list_add(&err->list, &einjected);
375 	}
376 	err->uncor_status |= einj->uncor_status;
377 	err->cor_status |= einj->cor_status;
378 	err->header_log0 = einj->header_log0;
379 	err->header_log1 = einj->header_log1;
380 	err->header_log2 = einj->header_log2;
381 	err->header_log3 = einj->header_log3;
382 
383 	if (!aer_mask_override && einj->cor_status &&
384 	    !(einj->cor_status & ~cor_mask)) {
385 		ret = -EINVAL;
386 		printk(KERN_WARNING "The correctable error(s) is masked by device\n");
387 		spin_unlock_irqrestore(&inject_lock, flags);
388 		goto out_put;
389 	}
390 	if (!aer_mask_override && einj->uncor_status &&
391 	    !(einj->uncor_status & ~uncor_mask)) {
392 		ret = -EINVAL;
393 		printk(KERN_WARNING "The uncorrectable error(s) is masked by device\n");
394 		spin_unlock_irqrestore(&inject_lock, flags);
395 		goto out_put;
396 	}
397 
398 	rperr = __find_aer_error_by_dev(rpdev);
399 	if (!rperr) {
400 		rperr = rperr_alloc;
401 		rperr_alloc = NULL;
402 		aer_error_init(rperr, pci_domain_nr(rpdev->bus),
403 			       rpdev->bus->number, rpdev->devfn,
404 			       rp_pos_cap_err);
405 		list_add(&rperr->list, &einjected);
406 	}
407 	if (einj->cor_status) {
408 		if (rperr->root_status & PCI_ERR_ROOT_COR_RCV)
409 			rperr->root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
410 		else
411 			rperr->root_status |= PCI_ERR_ROOT_COR_RCV;
412 		rperr->source_id &= 0xffff0000;
413 		rperr->source_id |= (einj->bus << 8) | devfn;
414 	}
415 	if (einj->uncor_status) {
416 		if (rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV)
417 			rperr->root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
418 		if (sever & einj->uncor_status) {
419 			rperr->root_status |= PCI_ERR_ROOT_FATAL_RCV;
420 			if (!(rperr->root_status & PCI_ERR_ROOT_UNCOR_RCV))
421 				rperr->root_status |= PCI_ERR_ROOT_FIRST_FATAL;
422 		} else
423 			rperr->root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
424 		rperr->root_status |= PCI_ERR_ROOT_UNCOR_RCV;
425 		rperr->source_id &= 0x0000ffff;
426 		rperr->source_id |= ((einj->bus << 8) | devfn) << 16;
427 	}
428 	spin_unlock_irqrestore(&inject_lock, flags);
429 
430 	if (aer_mask_override) {
431 		pci_write_config_dword(dev, pos_cap_err + PCI_ERR_COR_MASK,
432 				       cor_mask_orig);
433 		pci_write_config_dword(dev, pos_cap_err + PCI_ERR_UNCOR_MASK,
434 				       uncor_mask_orig);
435 	}
436 
437 	ret = pci_bus_set_aer_ops(dev->bus);
438 	if (ret)
439 		goto out_put;
440 	ret = pci_bus_set_aer_ops(rpdev->bus);
441 	if (ret)
442 		goto out_put;
443 
444 	if (find_aer_device(rpdev, &edev)) {
445 		if (!get_service_data(edev)) {
446 			printk(KERN_WARNING "AER service is not initialized\n");
447 			ret = -EINVAL;
448 			goto out_put;
449 		}
450 		aer_irq(-1, edev);
451 	} else
452 		ret = -EINVAL;
453 out_put:
454 	kfree(err_alloc);
455 	kfree(rperr_alloc);
456 	pci_dev_put(dev);
457 	return ret;
458 }
459 
aer_inject_write(struct file * filp,const char __user * ubuf,size_t usize,loff_t * off)460 static ssize_t aer_inject_write(struct file *filp, const char __user *ubuf,
461 				size_t usize, loff_t *off)
462 {
463 	struct aer_error_inj einj;
464 	int ret;
465 
466 	if (!capable(CAP_SYS_ADMIN))
467 		return -EPERM;
468 	if (usize < offsetof(struct aer_error_inj, domain) ||
469 	    usize > sizeof(einj))
470 		return -EINVAL;
471 
472 	memset(&einj, 0, sizeof(einj));
473 	if (copy_from_user(&einj, ubuf, usize))
474 		return -EFAULT;
475 
476 	ret = aer_inject(&einj);
477 	return ret ? ret : usize;
478 }
479 
480 static const struct file_operations aer_inject_fops = {
481 	.write = aer_inject_write,
482 	.owner = THIS_MODULE,
483 	.llseek = noop_llseek,
484 };
485 
486 static struct miscdevice aer_inject_device = {
487 	.minor = MISC_DYNAMIC_MINOR,
488 	.name = "aer_inject",
489 	.fops = &aer_inject_fops,
490 };
491 
aer_inject_init(void)492 static int __init aer_inject_init(void)
493 {
494 	return misc_register(&aer_inject_device);
495 }
496 
aer_inject_exit(void)497 static void __exit aer_inject_exit(void)
498 {
499 	struct aer_error *err, *err_next;
500 	unsigned long flags;
501 	struct pci_bus_ops *bus_ops;
502 
503 	misc_deregister(&aer_inject_device);
504 
505 	while ((bus_ops = pci_bus_ops_pop())) {
506 		pci_bus_set_ops(bus_ops->bus, bus_ops->ops);
507 		kfree(bus_ops);
508 	}
509 
510 	spin_lock_irqsave(&inject_lock, flags);
511 	list_for_each_entry_safe(err, err_next, &einjected, list) {
512 		list_del(&err->list);
513 		kfree(err);
514 	}
515 	spin_unlock_irqrestore(&inject_lock, flags);
516 }
517 
518 module_init(aer_inject_init);
519 module_exit(aer_inject_exit);
520 
521 MODULE_DESCRIPTION("PCIe AER software error injector");
522 MODULE_LICENSE("GPL");
523