1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <linux/edac.h>
11#include <linux/slab.h>
12#include <linux/ctype.h>
13
14#include "edac_core.h"
15#include "edac_module.h"
16
17
18#ifdef CONFIG_PCI
19
20#define EDAC_PCI_SYMLINK "device"
21
22
23static int check_pci_errors;
24static int edac_pci_panic_on_pe;
25static int edac_pci_log_pe = 1;
26static int edac_pci_log_npe = 1;
27static int edac_pci_poll_msec = 1000;
28
29static atomic_t pci_parity_count = ATOMIC_INIT(0);
30static atomic_t pci_nonparity_count = ATOMIC_INIT(0);
31
32static struct kobject *edac_pci_top_main_kobj;
33static atomic_t edac_pci_sysfs_refcount = ATOMIC_INIT(0);
34
35
36int edac_pci_get_check_errors(void)
37{
38 return check_pci_errors;
39}
40
41static int edac_pci_get_log_pe(void)
42{
43 return edac_pci_log_pe;
44}
45
46static int edac_pci_get_log_npe(void)
47{
48 return edac_pci_log_npe;
49}
50
51static int edac_pci_get_panic_on_pe(void)
52{
53 return edac_pci_panic_on_pe;
54}
55
56int edac_pci_get_poll_msec(void)
57{
58 return edac_pci_poll_msec;
59}
60
61
62static ssize_t instance_pe_count_show(struct edac_pci_ctl_info *pci, char *data)
63{
64 return sprintf(data, "%u\n", atomic_read(&pci->counters.pe_count));
65}
66
67static ssize_t instance_npe_count_show(struct edac_pci_ctl_info *pci,
68 char *data)
69{
70 return sprintf(data, "%u\n", atomic_read(&pci->counters.npe_count));
71}
72
73#define to_instance(k) container_of(k, struct edac_pci_ctl_info, kobj)
74#define to_instance_attr(a) container_of(a, struct instance_attribute, attr)
75
76
77static void edac_pci_instance_release(struct kobject *kobj)
78{
79 struct edac_pci_ctl_info *pci;
80
81 debugf0("%s()\n", __func__);
82
83
84 pci = to_instance(kobj);
85
86
87 kobject_put(edac_pci_top_main_kobj);
88
89 kfree(pci);
90}
91
92
93struct instance_attribute {
94 struct attribute attr;
95 ssize_t(*show) (struct edac_pci_ctl_info *, char *);
96 ssize_t(*store) (struct edac_pci_ctl_info *, const char *, size_t);
97};
98
99
100static ssize_t edac_pci_instance_show(struct kobject *kobj,
101 struct attribute *attr, char *buffer)
102{
103 struct edac_pci_ctl_info *pci = to_instance(kobj);
104 struct instance_attribute *instance_attr = to_instance_attr(attr);
105
106 if (instance_attr->show)
107 return instance_attr->show(pci, buffer);
108 return -EIO;
109}
110
111
112static ssize_t edac_pci_instance_store(struct kobject *kobj,
113 struct attribute *attr,
114 const char *buffer, size_t count)
115{
116 struct edac_pci_ctl_info *pci = to_instance(kobj);
117 struct instance_attribute *instance_attr = to_instance_attr(attr);
118
119 if (instance_attr->store)
120 return instance_attr->store(pci, buffer, count);
121 return -EIO;
122}
123
124
125static const struct sysfs_ops pci_instance_ops = {
126 .show = edac_pci_instance_show,
127 .store = edac_pci_instance_store
128};
129
130#define INSTANCE_ATTR(_name, _mode, _show, _store) \
131static struct instance_attribute attr_instance_##_name = { \
132 .attr = {.name = __stringify(_name), .mode = _mode }, \
133 .show = _show, \
134 .store = _store, \
135};
136
137INSTANCE_ATTR(pe_count, S_IRUGO, instance_pe_count_show, NULL);
138INSTANCE_ATTR(npe_count, S_IRUGO, instance_npe_count_show, NULL);
139
140
141static struct instance_attribute *pci_instance_attr[] = {
142 &attr_instance_pe_count,
143 &attr_instance_npe_count,
144 NULL
145};
146
147
148static struct kobj_type ktype_pci_instance = {
149 .release = edac_pci_instance_release,
150 .sysfs_ops = &pci_instance_ops,
151 .default_attrs = (struct attribute **)pci_instance_attr,
152};
153
154
155
156
157
158
159static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
160{
161 struct kobject *main_kobj;
162 int err;
163
164 debugf0("%s()\n", __func__);
165
166
167
168
169
170 main_kobj = kobject_get(edac_pci_top_main_kobj);
171 if (!main_kobj) {
172 err = -ENODEV;
173 goto error_out;
174 }
175
176
177 err = kobject_init_and_add(&pci->kobj, &ktype_pci_instance,
178 edac_pci_top_main_kobj, "pci%d", idx);
179 if (err != 0) {
180 debugf2("%s() failed to register instance pci%d\n",
181 __func__, idx);
182 kobject_put(edac_pci_top_main_kobj);
183 goto error_out;
184 }
185
186 kobject_uevent(&pci->kobj, KOBJ_ADD);
187 debugf1("%s() Register instance 'pci%d' kobject\n", __func__, idx);
188
189 return 0;
190
191
192error_out:
193 return err;
194}
195
196
197
198
199
200
201static void edac_pci_unregister_sysfs_instance_kobj(
202 struct edac_pci_ctl_info *pci)
203{
204 debugf0("%s()\n", __func__);
205
206
207
208
209
210 kobject_put(&pci->kobj);
211}
212
213
214#define to_edacpci(k) container_of(k, struct edac_pci_ctl_info, kobj)
215#define to_edacpci_attr(a) container_of(a, struct edac_pci_attr, attr)
216
217
218static ssize_t edac_pci_int_show(void *ptr, char *buffer)
219{
220 int *value = ptr;
221 return sprintf(buffer, "%d\n", *value);
222}
223
224static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
225{
226 int *value = ptr;
227
228 if (isdigit(*buffer))
229 *value = simple_strtoul(buffer, NULL, 0);
230
231 return count;
232}
233
234struct edac_pci_dev_attribute {
235 struct attribute attr;
236 void *value;
237 ssize_t(*show) (void *, char *);
238 ssize_t(*store) (void *, const char *, size_t);
239};
240
241
242static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
243 char *buffer)
244{
245 struct edac_pci_dev_attribute *edac_pci_dev;
246 edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
247
248 if (edac_pci_dev->show)
249 return edac_pci_dev->show(edac_pci_dev->value, buffer);
250 return -EIO;
251}
252
253static ssize_t edac_pci_dev_store(struct kobject *kobj,
254 struct attribute *attr, const char *buffer,
255 size_t count)
256{
257 struct edac_pci_dev_attribute *edac_pci_dev;
258 edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
259
260 if (edac_pci_dev->show)
261 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
262 return -EIO;
263}
264
265static const struct sysfs_ops edac_pci_sysfs_ops = {
266 .show = edac_pci_dev_show,
267 .store = edac_pci_dev_store
268};
269
270#define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
271static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
272 .attr = {.name = __stringify(_name), .mode = _mode }, \
273 .value = &_name, \
274 .show = _show, \
275 .store = _store, \
276};
277
278#define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
279static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
280 .attr = {.name = __stringify(_name), .mode = _mode }, \
281 .value = _data, \
282 .show = _show, \
283 .store = _store, \
284};
285
286
287EDAC_PCI_ATTR(check_pci_errors, S_IRUGO | S_IWUSR, edac_pci_int_show,
288 edac_pci_int_store);
289EDAC_PCI_ATTR(edac_pci_log_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
290 edac_pci_int_store);
291EDAC_PCI_ATTR(edac_pci_log_npe, S_IRUGO | S_IWUSR, edac_pci_int_show,
292 edac_pci_int_store);
293EDAC_PCI_ATTR(edac_pci_panic_on_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
294 edac_pci_int_store);
295EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
296EDAC_PCI_ATTR(pci_nonparity_count, S_IRUGO, edac_pci_int_show, NULL);
297
298
299static struct edac_pci_dev_attribute *edac_pci_attr[] = {
300 &edac_pci_attr_check_pci_errors,
301 &edac_pci_attr_edac_pci_log_pe,
302 &edac_pci_attr_edac_pci_log_npe,
303 &edac_pci_attr_edac_pci_panic_on_pe,
304 &edac_pci_attr_pci_parity_count,
305 &edac_pci_attr_pci_nonparity_count,
306 NULL,
307};
308
309
310
311
312
313
314
315
316
317
318static void edac_pci_release_main_kobj(struct kobject *kobj)
319{
320 debugf0("%s() here to module_put(THIS_MODULE)\n", __func__);
321
322 kfree(kobj);
323
324
325
326
327 module_put(THIS_MODULE);
328}
329
330
331static struct kobj_type ktype_edac_pci_main_kobj = {
332 .release = edac_pci_release_main_kobj,
333 .sysfs_ops = &edac_pci_sysfs_ops,
334 .default_attrs = (struct attribute **)edac_pci_attr,
335};
336
337
338
339
340
341
342
343static int edac_pci_main_kobj_setup(void)
344{
345 int err;
346 struct bus_type *edac_subsys;
347
348 debugf0("%s()\n", __func__);
349
350
351 if (atomic_inc_return(&edac_pci_sysfs_refcount) != 1)
352 return 0;
353
354
355
356
357 edac_subsys = edac_get_sysfs_subsys();
358 if (edac_subsys == NULL) {
359 debugf1("%s() no edac_subsys\n", __func__);
360 err = -ENODEV;
361 goto decrement_count_fail;
362 }
363
364
365
366
367
368 if (!try_module_get(THIS_MODULE)) {
369 debugf1("%s() try_module_get() failed\n", __func__);
370 err = -ENODEV;
371 goto mod_get_fail;
372 }
373
374 edac_pci_top_main_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
375 if (!edac_pci_top_main_kobj) {
376 debugf1("Failed to allocate\n");
377 err = -ENOMEM;
378 goto kzalloc_fail;
379 }
380
381
382 err = kobject_init_and_add(edac_pci_top_main_kobj,
383 &ktype_edac_pci_main_kobj,
384 &edac_subsys->dev_root->kobj, "pci");
385 if (err) {
386 debugf1("Failed to register '.../edac/pci'\n");
387 goto kobject_init_and_add_fail;
388 }
389
390
391
392
393
394 kobject_uevent(edac_pci_top_main_kobj, KOBJ_ADD);
395 debugf1("Registered '.../edac/pci' kobject\n");
396
397 return 0;
398
399
400kobject_init_and_add_fail:
401 kfree(edac_pci_top_main_kobj);
402
403kzalloc_fail:
404 module_put(THIS_MODULE);
405
406mod_get_fail:
407 edac_put_sysfs_subsys();
408
409decrement_count_fail:
410
411 atomic_dec(&edac_pci_sysfs_refcount);
412
413 return err;
414}
415
416
417
418
419
420
421
422static void edac_pci_main_kobj_teardown(void)
423{
424 debugf0("%s()\n", __func__);
425
426
427
428
429
430 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0) {
431 debugf0("%s() called kobject_put on main kobj\n",
432 __func__);
433 kobject_put(edac_pci_top_main_kobj);
434 }
435 edac_put_sysfs_subsys();
436}
437
438
439
440
441
442
443
444int edac_pci_create_sysfs(struct edac_pci_ctl_info *pci)
445{
446 int err;
447 struct kobject *edac_kobj = &pci->kobj;
448
449 debugf0("%s() idx=%d\n", __func__, pci->pci_idx);
450
451
452 err = edac_pci_main_kobj_setup();
453 if (err)
454 return err;
455
456
457 err = edac_pci_create_instance_kobj(pci, pci->pci_idx);
458 if (err)
459 goto unregister_cleanup;
460
461 err = sysfs_create_link(edac_kobj, &pci->dev->kobj, EDAC_PCI_SYMLINK);
462 if (err) {
463 debugf0("%s() sysfs_create_link() returned err= %d\n",
464 __func__, err);
465 goto symlink_fail;
466 }
467
468 return 0;
469
470
471symlink_fail:
472 edac_pci_unregister_sysfs_instance_kobj(pci);
473
474unregister_cleanup:
475 edac_pci_main_kobj_teardown();
476
477 return err;
478}
479
480
481
482
483
484
485void edac_pci_remove_sysfs(struct edac_pci_ctl_info *pci)
486{
487 debugf0("%s() index=%d\n", __func__, pci->pci_idx);
488
489
490 sysfs_remove_link(&pci->kobj, EDAC_PCI_SYMLINK);
491
492
493 edac_pci_unregister_sysfs_instance_kobj(pci);
494
495
496
497
498
499 debugf0("%s() calling edac_pci_main_kobj_teardown()\n", __func__);
500 edac_pci_main_kobj_teardown();
501}
502
503
504static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
505{
506 int where;
507 u16 status;
508
509 where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
510 pci_read_config_word(dev, where, &status);
511
512
513
514
515
516
517 if (status == 0xFFFF) {
518 u32 sanity;
519
520 pci_read_config_dword(dev, 0, &sanity);
521
522 if (sanity == 0xFFFFFFFF)
523 return 0;
524 }
525
526 status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
527 PCI_STATUS_PARITY;
528
529 if (status)
530
531 pci_write_config_word(dev, where, status);
532
533 return status;
534}
535
536
537
538static void edac_pci_dev_parity_clear(struct pci_dev *dev)
539{
540 u8 header_type;
541
542 get_pci_parity_status(dev, 0);
543
544
545 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
546
547 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
548 get_pci_parity_status(dev, 1);
549}
550
551
552
553
554
555
556
557
558static void edac_pci_dev_parity_test(struct pci_dev *dev)
559{
560 unsigned long flags;
561 u16 status;
562 u8 header_type;
563
564
565 local_irq_save(flags);
566
567
568 status = get_pci_parity_status(dev, 0);
569
570
571 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
572
573 local_irq_restore(flags);
574
575 debugf4("PCI STATUS= 0x%04x %s\n", status, dev_name(&dev->dev));
576
577
578
579
580 if (status && !dev->broken_parity_status) {
581 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
582 edac_printk(KERN_CRIT, EDAC_PCI,
583 "Signaled System Error on %s\n",
584 pci_name(dev));
585 atomic_inc(&pci_nonparity_count);
586 }
587
588 if (status & (PCI_STATUS_PARITY)) {
589 edac_printk(KERN_CRIT, EDAC_PCI,
590 "Master Data Parity Error on %s\n",
591 pci_name(dev));
592
593 atomic_inc(&pci_parity_count);
594 }
595
596 if (status & (PCI_STATUS_DETECTED_PARITY)) {
597 edac_printk(KERN_CRIT, EDAC_PCI,
598 "Detected Parity Error on %s\n",
599 pci_name(dev));
600
601 atomic_inc(&pci_parity_count);
602 }
603 }
604
605
606 debugf4("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev_name(&dev->dev));
607
608 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
609
610 status = get_pci_parity_status(dev, 1);
611
612 debugf4("PCI SEC_STATUS= 0x%04x %s\n", status, dev_name(&dev->dev));
613
614
615
616
617 if (status && !dev->broken_parity_status) {
618 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
619 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
620 "Signaled System Error on %s\n",
621 pci_name(dev));
622 atomic_inc(&pci_nonparity_count);
623 }
624
625 if (status & (PCI_STATUS_PARITY)) {
626 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
627 "Master Data Parity Error on "
628 "%s\n", pci_name(dev));
629
630 atomic_inc(&pci_parity_count);
631 }
632
633 if (status & (PCI_STATUS_DETECTED_PARITY)) {
634 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
635 "Detected Parity Error on %s\n",
636 pci_name(dev));
637
638 atomic_inc(&pci_parity_count);
639 }
640 }
641 }
642}
643
644
645typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
646
647
648
649
650
651
652static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
653{
654 struct pci_dev *dev = NULL;
655
656
657
658
659
660 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
661 fn(dev);
662 }
663}
664
665
666
667
668
669
670void edac_pci_do_parity_check(void)
671{
672 int before_count;
673
674 debugf3("%s()\n", __func__);
675
676
677 if (!check_pci_errors)
678 return;
679
680 before_count = atomic_read(&pci_parity_count);
681
682
683
684
685
686
687 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
688
689
690 if (edac_pci_get_panic_on_pe()) {
691
692 if (before_count != atomic_read(&pci_parity_count))
693 panic("EDAC: PCI Parity Error");
694 }
695}
696
697
698
699
700
701
702
703void edac_pci_clear_parity_errors(void)
704{
705
706
707
708 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
709}
710
711
712
713
714
715
716void edac_pci_handle_pe(struct edac_pci_ctl_info *pci, const char *msg)
717{
718
719
720 atomic_inc(&pci->counters.pe_count);
721
722 if (edac_pci_get_log_pe())
723 edac_pci_printk(pci, KERN_WARNING,
724 "Parity Error ctl: %s %d: %s\n",
725 pci->ctl_name, pci->pci_idx, msg);
726
727
728
729
730
731 edac_pci_do_parity_check();
732}
733EXPORT_SYMBOL_GPL(edac_pci_handle_pe);
734
735
736
737
738
739
740
741void edac_pci_handle_npe(struct edac_pci_ctl_info *pci, const char *msg)
742{
743
744
745 atomic_inc(&pci->counters.npe_count);
746
747 if (edac_pci_get_log_npe())
748 edac_pci_printk(pci, KERN_WARNING,
749 "Non-Parity Error ctl: %s %d: %s\n",
750 pci->ctl_name, pci->pci_idx, msg);
751
752
753
754
755
756 edac_pci_do_parity_check();
757}
758EXPORT_SYMBOL_GPL(edac_pci_handle_npe);
759
760
761
762
763module_param(check_pci_errors, int, 0644);
764MODULE_PARM_DESC(check_pci_errors,
765 "Check for PCI bus parity errors: 0=off 1=on");
766module_param(edac_pci_panic_on_pe, int, 0644);
767MODULE_PARM_DESC(edac_pci_panic_on_pe,
768 "Panic on PCI Bus Parity error: 0=off 1=on");
769
770#endif
771