0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/edac.h>
0011 #include <linux/slab.h>
0012 #include <linux/ctype.h>
0013
0014 #include "edac_pci.h"
0015 #include "edac_module.h"
0016
0017 #define EDAC_PCI_SYMLINK "device"
0018
0019
0020 static int check_pci_errors;
0021 static int edac_pci_panic_on_pe;
0022 static int edac_pci_log_pe = 1;
0023 static int edac_pci_log_npe = 1;
0024 static int edac_pci_poll_msec = 1000;
0025
0026 static atomic_t pci_parity_count = ATOMIC_INIT(0);
0027 static atomic_t pci_nonparity_count = ATOMIC_INIT(0);
0028
0029 static struct kobject *edac_pci_top_main_kobj;
0030 static atomic_t edac_pci_sysfs_refcount = ATOMIC_INIT(0);
0031
0032
0033 int edac_pci_get_check_errors(void)
0034 {
0035 return check_pci_errors;
0036 }
0037
0038 static int edac_pci_get_log_pe(void)
0039 {
0040 return edac_pci_log_pe;
0041 }
0042
0043 static int edac_pci_get_log_npe(void)
0044 {
0045 return edac_pci_log_npe;
0046 }
0047
0048 static int edac_pci_get_panic_on_pe(void)
0049 {
0050 return edac_pci_panic_on_pe;
0051 }
0052
0053 int edac_pci_get_poll_msec(void)
0054 {
0055 return edac_pci_poll_msec;
0056 }
0057
0058
0059 static ssize_t instance_pe_count_show(struct edac_pci_ctl_info *pci, char *data)
0060 {
0061 return sprintf(data, "%u\n", atomic_read(&pci->counters.pe_count));
0062 }
0063
0064 static ssize_t instance_npe_count_show(struct edac_pci_ctl_info *pci,
0065 char *data)
0066 {
0067 return sprintf(data, "%u\n", atomic_read(&pci->counters.npe_count));
0068 }
0069
0070 #define to_instance(k) container_of(k, struct edac_pci_ctl_info, kobj)
0071 #define to_instance_attr(a) container_of(a, struct instance_attribute, attr)
0072
0073
0074 static void edac_pci_instance_release(struct kobject *kobj)
0075 {
0076 struct edac_pci_ctl_info *pci;
0077
0078 edac_dbg(0, "\n");
0079
0080
0081 pci = to_instance(kobj);
0082
0083
0084 kobject_put(edac_pci_top_main_kobj);
0085
0086 kfree(pci);
0087 }
0088
0089
0090 struct instance_attribute {
0091 struct attribute attr;
0092 ssize_t(*show) (struct edac_pci_ctl_info *, char *);
0093 ssize_t(*store) (struct edac_pci_ctl_info *, const char *, size_t);
0094 };
0095
0096
0097 static ssize_t edac_pci_instance_show(struct kobject *kobj,
0098 struct attribute *attr, char *buffer)
0099 {
0100 struct edac_pci_ctl_info *pci = to_instance(kobj);
0101 struct instance_attribute *instance_attr = to_instance_attr(attr);
0102
0103 if (instance_attr->show)
0104 return instance_attr->show(pci, buffer);
0105 return -EIO;
0106 }
0107
0108
0109 static ssize_t edac_pci_instance_store(struct kobject *kobj,
0110 struct attribute *attr,
0111 const char *buffer, size_t count)
0112 {
0113 struct edac_pci_ctl_info *pci = to_instance(kobj);
0114 struct instance_attribute *instance_attr = to_instance_attr(attr);
0115
0116 if (instance_attr->store)
0117 return instance_attr->store(pci, buffer, count);
0118 return -EIO;
0119 }
0120
0121
0122 static const struct sysfs_ops pci_instance_ops = {
0123 .show = edac_pci_instance_show,
0124 .store = edac_pci_instance_store
0125 };
0126
0127 #define INSTANCE_ATTR(_name, _mode, _show, _store) \
0128 static struct instance_attribute attr_instance_##_name = { \
0129 .attr = {.name = __stringify(_name), .mode = _mode }, \
0130 .show = _show, \
0131 .store = _store, \
0132 };
0133
0134 INSTANCE_ATTR(pe_count, S_IRUGO, instance_pe_count_show, NULL);
0135 INSTANCE_ATTR(npe_count, S_IRUGO, instance_npe_count_show, NULL);
0136
0137
0138 static struct attribute *pci_instance_attrs[] = {
0139 &attr_instance_pe_count.attr,
0140 &attr_instance_npe_count.attr,
0141 NULL
0142 };
0143 ATTRIBUTE_GROUPS(pci_instance);
0144
0145
0146 static struct kobj_type ktype_pci_instance = {
0147 .release = edac_pci_instance_release,
0148 .sysfs_ops = &pci_instance_ops,
0149 .default_groups = pci_instance_groups,
0150 };
0151
0152
0153
0154
0155
0156
0157 static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
0158 {
0159 struct kobject *main_kobj;
0160 int err;
0161
0162 edac_dbg(0, "\n");
0163
0164
0165
0166
0167
0168 main_kobj = kobject_get(edac_pci_top_main_kobj);
0169 if (!main_kobj) {
0170 err = -ENODEV;
0171 goto error_out;
0172 }
0173
0174
0175 err = kobject_init_and_add(&pci->kobj, &ktype_pci_instance,
0176 edac_pci_top_main_kobj, "pci%d", idx);
0177 if (err != 0) {
0178 edac_dbg(2, "failed to register instance pci%d\n", idx);
0179 kobject_put(edac_pci_top_main_kobj);
0180 goto error_out;
0181 }
0182
0183 kobject_uevent(&pci->kobj, KOBJ_ADD);
0184 edac_dbg(1, "Register instance 'pci%d' kobject\n", idx);
0185
0186 return 0;
0187
0188
0189 error_out:
0190 return err;
0191 }
0192
0193
0194
0195
0196
0197
0198 static void edac_pci_unregister_sysfs_instance_kobj(
0199 struct edac_pci_ctl_info *pci)
0200 {
0201 edac_dbg(0, "\n");
0202
0203
0204
0205
0206
0207 kobject_put(&pci->kobj);
0208 }
0209
0210
0211 #define to_edacpci(k) container_of(k, struct edac_pci_ctl_info, kobj)
0212 #define to_edacpci_attr(a) container_of(a, struct edac_pci_attr, attr)
0213
0214
0215 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
0216 {
0217 int *value = ptr;
0218 return sprintf(buffer, "%d\n", *value);
0219 }
0220
0221 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
0222 {
0223 int *value = ptr;
0224
0225 if (isdigit(*buffer))
0226 *value = simple_strtoul(buffer, NULL, 0);
0227
0228 return count;
0229 }
0230
0231 struct edac_pci_dev_attribute {
0232 struct attribute attr;
0233 void *value;
0234 ssize_t(*show) (void *, char *);
0235 ssize_t(*store) (void *, const char *, size_t);
0236 };
0237
0238
0239 static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
0240 char *buffer)
0241 {
0242 struct edac_pci_dev_attribute *edac_pci_dev;
0243 edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
0244
0245 if (edac_pci_dev->show)
0246 return edac_pci_dev->show(edac_pci_dev->value, buffer);
0247 return -EIO;
0248 }
0249
0250 static ssize_t edac_pci_dev_store(struct kobject *kobj,
0251 struct attribute *attr, const char *buffer,
0252 size_t count)
0253 {
0254 struct edac_pci_dev_attribute *edac_pci_dev;
0255 edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
0256
0257 if (edac_pci_dev->store)
0258 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
0259 return -EIO;
0260 }
0261
0262 static const struct sysfs_ops edac_pci_sysfs_ops = {
0263 .show = edac_pci_dev_show,
0264 .store = edac_pci_dev_store
0265 };
0266
0267 #define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
0268 static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
0269 .attr = {.name = __stringify(_name), .mode = _mode }, \
0270 .value = &_name, \
0271 .show = _show, \
0272 .store = _store, \
0273 };
0274
0275 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
0276 static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
0277 .attr = {.name = __stringify(_name), .mode = _mode }, \
0278 .value = _data, \
0279 .show = _show, \
0280 .store = _store, \
0281 };
0282
0283
0284 EDAC_PCI_ATTR(check_pci_errors, S_IRUGO | S_IWUSR, edac_pci_int_show,
0285 edac_pci_int_store);
0286 EDAC_PCI_ATTR(edac_pci_log_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
0287 edac_pci_int_store);
0288 EDAC_PCI_ATTR(edac_pci_log_npe, S_IRUGO | S_IWUSR, edac_pci_int_show,
0289 edac_pci_int_store);
0290 EDAC_PCI_ATTR(edac_pci_panic_on_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
0291 edac_pci_int_store);
0292 EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
0293 EDAC_PCI_ATTR(pci_nonparity_count, S_IRUGO, edac_pci_int_show, NULL);
0294
0295
0296 static struct attribute *edac_pci_attrs[] = {
0297 &edac_pci_attr_check_pci_errors.attr,
0298 &edac_pci_attr_edac_pci_log_pe.attr,
0299 &edac_pci_attr_edac_pci_log_npe.attr,
0300 &edac_pci_attr_edac_pci_panic_on_pe.attr,
0301 &edac_pci_attr_pci_parity_count.attr,
0302 &edac_pci_attr_pci_nonparity_count.attr,
0303 NULL,
0304 };
0305 ATTRIBUTE_GROUPS(edac_pci);
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316 static void edac_pci_release_main_kobj(struct kobject *kobj)
0317 {
0318 edac_dbg(0, "here to module_put(THIS_MODULE)\n");
0319
0320 kfree(kobj);
0321
0322
0323
0324
0325 module_put(THIS_MODULE);
0326 }
0327
0328
0329 static struct kobj_type ktype_edac_pci_main_kobj = {
0330 .release = edac_pci_release_main_kobj,
0331 .sysfs_ops = &edac_pci_sysfs_ops,
0332 .default_groups = edac_pci_groups,
0333 };
0334
0335
0336
0337
0338 static int edac_pci_main_kobj_setup(void)
0339 {
0340 int err;
0341 struct bus_type *edac_subsys;
0342
0343 edac_dbg(0, "\n");
0344
0345
0346 if (atomic_inc_return(&edac_pci_sysfs_refcount) != 1)
0347 return 0;
0348
0349
0350
0351
0352 edac_subsys = edac_get_sysfs_subsys();
0353
0354
0355
0356
0357
0358 if (!try_module_get(THIS_MODULE)) {
0359 edac_dbg(1, "try_module_get() failed\n");
0360 err = -ENODEV;
0361 goto decrement_count_fail;
0362 }
0363
0364 edac_pci_top_main_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
0365 if (!edac_pci_top_main_kobj) {
0366 edac_dbg(1, "Failed to allocate\n");
0367 err = -ENOMEM;
0368 goto kzalloc_fail;
0369 }
0370
0371
0372 err = kobject_init_and_add(edac_pci_top_main_kobj,
0373 &ktype_edac_pci_main_kobj,
0374 &edac_subsys->dev_root->kobj, "pci");
0375 if (err) {
0376 edac_dbg(1, "Failed to register '.../edac/pci'\n");
0377 goto kobject_init_and_add_fail;
0378 }
0379
0380
0381
0382
0383
0384 kobject_uevent(edac_pci_top_main_kobj, KOBJ_ADD);
0385 edac_dbg(1, "Registered '.../edac/pci' kobject\n");
0386
0387 return 0;
0388
0389
0390 kobject_init_and_add_fail:
0391 kobject_put(edac_pci_top_main_kobj);
0392
0393 kzalloc_fail:
0394 module_put(THIS_MODULE);
0395
0396 decrement_count_fail:
0397
0398 atomic_dec(&edac_pci_sysfs_refcount);
0399
0400 return err;
0401 }
0402
0403
0404
0405
0406
0407
0408
0409 static void edac_pci_main_kobj_teardown(void)
0410 {
0411 edac_dbg(0, "\n");
0412
0413
0414
0415
0416
0417 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0) {
0418 edac_dbg(0, "called kobject_put on main kobj\n");
0419 kobject_put(edac_pci_top_main_kobj);
0420 }
0421 }
0422
0423 int edac_pci_create_sysfs(struct edac_pci_ctl_info *pci)
0424 {
0425 int err;
0426 struct kobject *edac_kobj = &pci->kobj;
0427
0428 edac_dbg(0, "idx=%d\n", pci->pci_idx);
0429
0430
0431 err = edac_pci_main_kobj_setup();
0432 if (err)
0433 return err;
0434
0435
0436 err = edac_pci_create_instance_kobj(pci, pci->pci_idx);
0437 if (err)
0438 goto unregister_cleanup;
0439
0440 err = sysfs_create_link(edac_kobj, &pci->dev->kobj, EDAC_PCI_SYMLINK);
0441 if (err) {
0442 edac_dbg(0, "sysfs_create_link() returned err= %d\n", err);
0443 goto symlink_fail;
0444 }
0445
0446 return 0;
0447
0448
0449 symlink_fail:
0450 edac_pci_unregister_sysfs_instance_kobj(pci);
0451
0452 unregister_cleanup:
0453 edac_pci_main_kobj_teardown();
0454
0455 return err;
0456 }
0457
0458 void edac_pci_remove_sysfs(struct edac_pci_ctl_info *pci)
0459 {
0460 edac_dbg(0, "index=%d\n", pci->pci_idx);
0461
0462
0463 sysfs_remove_link(&pci->kobj, EDAC_PCI_SYMLINK);
0464
0465
0466 edac_pci_unregister_sysfs_instance_kobj(pci);
0467
0468
0469
0470
0471
0472 edac_dbg(0, "calling edac_pci_main_kobj_teardown()\n");
0473 edac_pci_main_kobj_teardown();
0474 }
0475
0476
0477 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
0478 {
0479 int where;
0480 u16 status;
0481
0482 where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
0483 pci_read_config_word(dev, where, &status);
0484
0485
0486
0487
0488
0489
0490 if (status == 0xFFFF) {
0491 u32 sanity;
0492
0493 pci_read_config_dword(dev, 0, &sanity);
0494
0495 if (sanity == 0xFFFFFFFF)
0496 return 0;
0497 }
0498
0499 status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
0500 PCI_STATUS_PARITY;
0501
0502 if (status)
0503
0504 pci_write_config_word(dev, where, status);
0505
0506 return status;
0507 }
0508
0509
0510
0511 static void edac_pci_dev_parity_clear(struct pci_dev *dev)
0512 {
0513 u8 header_type;
0514
0515 get_pci_parity_status(dev, 0);
0516
0517
0518 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
0519
0520 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
0521 get_pci_parity_status(dev, 1);
0522 }
0523
0524
0525
0526
0527
0528
0529
0530
0531 static void edac_pci_dev_parity_test(struct pci_dev *dev)
0532 {
0533 unsigned long flags;
0534 u16 status;
0535 u8 header_type;
0536
0537
0538 local_irq_save(flags);
0539
0540
0541 status = get_pci_parity_status(dev, 0);
0542
0543
0544 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
0545
0546 local_irq_restore(flags);
0547
0548 edac_dbg(4, "PCI STATUS= 0x%04x %s\n", status, dev_name(&dev->dev));
0549
0550
0551
0552
0553 if (status && !dev->broken_parity_status) {
0554 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
0555 edac_printk(KERN_CRIT, EDAC_PCI,
0556 "Signaled System Error on %s\n",
0557 pci_name(dev));
0558 atomic_inc(&pci_nonparity_count);
0559 }
0560
0561 if (status & (PCI_STATUS_PARITY)) {
0562 edac_printk(KERN_CRIT, EDAC_PCI,
0563 "Master Data Parity Error on %s\n",
0564 pci_name(dev));
0565
0566 atomic_inc(&pci_parity_count);
0567 }
0568
0569 if (status & (PCI_STATUS_DETECTED_PARITY)) {
0570 edac_printk(KERN_CRIT, EDAC_PCI,
0571 "Detected Parity Error on %s\n",
0572 pci_name(dev));
0573
0574 atomic_inc(&pci_parity_count);
0575 }
0576 }
0577
0578
0579 edac_dbg(4, "PCI HEADER TYPE= 0x%02x %s\n",
0580 header_type, dev_name(&dev->dev));
0581
0582 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
0583
0584 status = get_pci_parity_status(dev, 1);
0585
0586 edac_dbg(4, "PCI SEC_STATUS= 0x%04x %s\n",
0587 status, dev_name(&dev->dev));
0588
0589
0590
0591
0592 if (status && !dev->broken_parity_status) {
0593 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
0594 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
0595 "Signaled System Error on %s\n",
0596 pci_name(dev));
0597 atomic_inc(&pci_nonparity_count);
0598 }
0599
0600 if (status & (PCI_STATUS_PARITY)) {
0601 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
0602 "Master Data Parity Error on "
0603 "%s\n", pci_name(dev));
0604
0605 atomic_inc(&pci_parity_count);
0606 }
0607
0608 if (status & (PCI_STATUS_DETECTED_PARITY)) {
0609 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
0610 "Detected Parity Error on %s\n",
0611 pci_name(dev));
0612
0613 atomic_inc(&pci_parity_count);
0614 }
0615 }
0616 }
0617 }
0618
0619
0620 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
0621
0622
0623
0624
0625
0626
0627
0628 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
0629 {
0630 struct pci_dev *dev = NULL;
0631
0632 for_each_pci_dev(dev)
0633 fn(dev);
0634 }
0635
0636
0637
0638
0639
0640
0641 void edac_pci_do_parity_check(void)
0642 {
0643 int before_count;
0644
0645 edac_dbg(3, "\n");
0646
0647
0648 if (!check_pci_errors)
0649 return;
0650
0651 before_count = atomic_read(&pci_parity_count);
0652
0653
0654
0655
0656
0657
0658 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
0659
0660
0661 if (edac_pci_get_panic_on_pe()) {
0662
0663 if (before_count != atomic_read(&pci_parity_count))
0664 panic("EDAC: PCI Parity Error");
0665 }
0666 }
0667
0668
0669
0670
0671
0672
0673
0674 void edac_pci_clear_parity_errors(void)
0675 {
0676
0677
0678
0679 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
0680 }
0681
0682
0683
0684
0685
0686
0687 void edac_pci_handle_pe(struct edac_pci_ctl_info *pci, const char *msg)
0688 {
0689
0690
0691 atomic_inc(&pci->counters.pe_count);
0692
0693 if (edac_pci_get_log_pe())
0694 edac_pci_printk(pci, KERN_WARNING,
0695 "Parity Error ctl: %s %d: %s\n",
0696 pci->ctl_name, pci->pci_idx, msg);
0697
0698
0699
0700
0701
0702 edac_pci_do_parity_check();
0703 }
0704 EXPORT_SYMBOL_GPL(edac_pci_handle_pe);
0705
0706
0707
0708
0709
0710
0711
0712 void edac_pci_handle_npe(struct edac_pci_ctl_info *pci, const char *msg)
0713 {
0714
0715
0716 atomic_inc(&pci->counters.npe_count);
0717
0718 if (edac_pci_get_log_npe())
0719 edac_pci_printk(pci, KERN_WARNING,
0720 "Non-Parity Error ctl: %s %d: %s\n",
0721 pci->ctl_name, pci->pci_idx, msg);
0722
0723
0724
0725
0726
0727 edac_pci_do_parity_check();
0728 }
0729 EXPORT_SYMBOL_GPL(edac_pci_handle_npe);
0730
0731
0732
0733
0734 module_param(check_pci_errors, int, 0644);
0735 MODULE_PARM_DESC(check_pci_errors,
0736 "Check for PCI bus parity errors: 0=off 1=on");
0737 module_param(edac_pci_panic_on_pe, int, 0644);
0738 MODULE_PARM_DESC(edac_pci_panic_on_pe,
0739 "Panic on PCI Bus Parity error: 0=off 1=on");