drivers/edac: updated PCI monitoring
[safe/jmp/linux-2.6] / drivers / edac / edac_pci_sysfs.c
1 /*
2  * (C) 2005, 2006 Linux Networx (http://lnxi.com)
3  * This file may be distributed under the terms of the
4  * GNU General Public License.
5  *
6  * Written Doug Thompson <norsk5@xmission.com>
7  *
8  */
9 #include <linux/module.h>
10 #include <linux/sysdev.h>
11 #include <linux/ctype.h>
12
13 #include "edac_core.h"
14 #include "edac_module.h"
15
16
17 #ifdef CONFIG_PCI
18
19 #define EDAC_PCI_SYMLINK        "device"
20
21 static int check_pci_errors = 0;        /* default YES check PCI parity */
22 static int panic_on_pci_parity = 0;     /* default no panic on PCI Parity */
23 static int log_pci_errs = 1;
24 static atomic_t pci_parity_count = ATOMIC_INIT(0);
25 static atomic_t pci_nonparity_count = ATOMIC_INIT(0);
26
27 static struct kobject edac_pci_kobj; /* /sys/devices/system/edac/pci */
28 static struct completion edac_pci_kobj_complete;
29 static atomic_t edac_pci_sysfs_refcount = ATOMIC_INIT(0);
30
31 /**************************** EDAC PCI sysfs instance *******************/
32 static ssize_t instance_pe_count_show(struct edac_pci_ctl_info *pci, char *data)
33 {
34         return sprintf(data,"%u\n", atomic_read(&pci->counters.pe_count));
35 }
36
37 static ssize_t instance_npe_count_show(struct edac_pci_ctl_info *pci,
38                 char *data)
39 {
40         return sprintf(data,"%u\n", atomic_read(&pci->counters.npe_count));
41 }
42
43 #define to_instance(k) container_of(k, struct edac_pci_ctl_info, kobj)
44 #define to_instance_attr(a) container_of(a, struct instance_attribute, attr)
45
46 /* DEVICE instance kobject release() function */
47 static void edac_pci_instance_release(struct kobject *kobj)
48 {
49         struct edac_pci_ctl_info *pci;
50
51         debugf1("%s()\n", __func__);
52
53         pci = to_instance(kobj);
54         complete(&pci->kobj_complete);
55 }
56
57 /* instance specific attribute structure */
58 struct instance_attribute {
59         struct attribute attr;
60         ssize_t (*show)(struct edac_pci_ctl_info *, char *);
61         ssize_t (*store)(struct edac_pci_ctl_info *, const char *, size_t);
62 };
63
64 /* Function to 'show' fields from the edac_pci 'instance' structure */
65 static ssize_t edac_pci_instance_show(struct kobject *kobj,
66                         struct attribute *attr,
67                         char *buffer)
68 {
69         struct edac_pci_ctl_info *pci = to_instance(kobj);
70         struct instance_attribute *instance_attr = to_instance_attr(attr);
71
72         if (instance_attr->show)
73                 return instance_attr->show(pci, buffer);
74         return -EIO;
75 }
76
77
78 /* Function to 'store' fields into the edac_pci 'instance' structure */
79 static ssize_t edac_pci_instance_store(struct kobject *kobj,
80                         struct attribute *attr,
81                         const char *buffer, size_t count)
82 {
83         struct edac_pci_ctl_info *pci = to_instance(kobj);
84         struct instance_attribute *instance_attr = to_instance_attr(attr);
85
86         if (instance_attr->store)
87                 return instance_attr->store(pci, buffer, count);
88         return -EIO;
89 }
90
91 static struct sysfs_ops pci_instance_ops = {
92         .show = edac_pci_instance_show,
93         .store = edac_pci_instance_store
94 };
95
96 #define INSTANCE_ATTR(_name, _mode, _show, _store)      \
97 static struct instance_attribute attr_instance_##_name = {      \
98         .attr   = {.name = __stringify(_name), .mode = _mode }, \
99         .show   = _show,                                        \
100         .store  = _store,                                       \
101 };
102
103 INSTANCE_ATTR(pe_count, S_IRUGO, instance_pe_count_show, NULL);
104 INSTANCE_ATTR(npe_count, S_IRUGO, instance_npe_count_show, NULL);
105
106 /* pci instance attributes */
107 static struct instance_attribute *pci_instance_attr[] = {
108         &attr_instance_pe_count,
109         &attr_instance_npe_count,
110         NULL
111 };
112
113 /* the ktype for pci instance */
114 static struct kobj_type ktype_pci_instance = {
115         .release = edac_pci_instance_release,
116         .sysfs_ops = &pci_instance_ops,
117         .default_attrs = (struct attribute **)pci_instance_attr,
118 };
119
120 static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
121 {
122         int err;
123
124         pci->kobj.parent = &edac_pci_kobj;
125         pci->kobj.ktype = &ktype_pci_instance;
126
127         err = kobject_set_name(&pci->kobj, "pci%d", idx);
128         if (err)
129                 return err;
130
131         err = kobject_register(&pci->kobj);
132         if (err != 0) {
133                 debugf2("%s() failed to register instance pci%d\n",
134                                 __func__, idx);
135                 return err;
136         }
137
138         debugf1("%s() Register instance 'pci%d' kobject\n", __func__, idx);
139
140         return 0;
141 }
142
143 static void
144 edac_pci_delete_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
145 {
146         init_completion(&pci->kobj_complete);
147         kobject_unregister(&pci->kobj);
148         wait_for_completion(&pci->kobj_complete);
149 }
150
151 /***************************** EDAC PCI sysfs root **********************/
152 #define to_edacpci(k) container_of(k, struct edac_pci_ctl_info, kobj)
153 #define to_edacpci_attr(a) container_of(a, struct edac_pci_attr, attr)
154
155 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
156 {
157         int *value = ptr;
158         return sprintf(buffer,"%d\n",*value);
159 }
160
161 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
162 {
163         int *value = ptr;
164
165         if (isdigit(*buffer))
166                 *value = simple_strtoul(buffer,NULL,0);
167
168         return count;
169 }
170
171 struct edac_pci_dev_attribute {
172         struct attribute attr;
173         void *value;
174         ssize_t (*show)(void *,char *);
175         ssize_t (*store)(void *, const char *,size_t);
176 };
177
178 /* Set of show/store abstract level functions for PCI Parity object */
179 static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
180                 char *buffer)
181 {
182         struct edac_pci_dev_attribute *edac_pci_dev;
183         edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
184
185         if (edac_pci_dev->show)
186                 return edac_pci_dev->show(edac_pci_dev->value, buffer);
187         return -EIO;
188 }
189
190 static ssize_t edac_pci_dev_store(struct kobject *kobj,
191                 struct attribute *attr, const char *buffer, size_t count)
192 {
193         struct edac_pci_dev_attribute *edac_pci_dev;
194         edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
195
196         if (edac_pci_dev->show)
197                 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
198         return -EIO;
199 }
200
201 static struct sysfs_ops edac_pci_sysfs_ops = {
202         .show   = edac_pci_dev_show,
203         .store  = edac_pci_dev_store
204 };
205
206 #define EDAC_PCI_ATTR(_name,_mode,_show,_store)                 \
207 static struct edac_pci_dev_attribute edac_pci_attr_##_name = {          \
208         .attr = {.name = __stringify(_name), .mode = _mode },   \
209         .value  = &_name,                                       \
210         .show   = _show,                                        \
211         .store  = _store,                                       \
212 };
213
214 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store)    \
215 static struct edac_pci_dev_attribute edac_pci_attr_##_name = {          \
216         .attr = {.name = __stringify(_name), .mode = _mode },   \
217         .value  = _data,                                        \
218         .show   = _show,                                        \
219         .store  = _store,                                       \
220 };
221
222 /* PCI Parity control files */
223 EDAC_PCI_ATTR(check_pci_errors, S_IRUGO|S_IWUSR, edac_pci_int_show,
224         edac_pci_int_store);
225 EDAC_PCI_ATTR(log_pci_errs, S_IRUGO|S_IWUSR, edac_pci_int_show,
226         edac_pci_int_store);
227 EDAC_PCI_ATTR(panic_on_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
228         edac_pci_int_store);
229 EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
230 EDAC_PCI_ATTR(pci_nonparity_count, S_IRUGO, edac_pci_int_show, NULL);
231
232 /* Base Attributes of the memory ECC object */
233 static struct edac_pci_dev_attribute *edac_pci_attr[] = {
234         &edac_pci_attr_check_pci_errors,
235         &edac_pci_attr_log_pci_errs,
236         &edac_pci_attr_panic_on_pci_parity,
237         &edac_pci_attr_pci_parity_count,
238         &edac_pci_attr_pci_nonparity_count,
239         NULL,
240 };
241
242 /* No memory to release */
243 static void edac_pci_release(struct kobject *kobj)
244 {
245         struct edac_pci_ctl_info *pci;
246
247         pci = to_edacpci(kobj);
248
249         debugf1("%s()\n", __func__);
250         complete(&pci->kobj_complete);
251 }
252
253 static struct kobj_type ktype_edac_pci = {
254         .release = edac_pci_release,
255         .sysfs_ops = &edac_pci_sysfs_ops,
256         .default_attrs = (struct attribute **) edac_pci_attr,
257 };
258
259 /**
260  * edac_sysfs_pci_setup()
261  *
262  *      setup the sysfs for EDAC PCI attributes
263  *      assumes edac_class has already been initialized
264  */
265 int edac_pci_register_main_kobj(void)
266 {
267         int err;
268         struct sysdev_class *edac_class;
269
270         debugf1("%s()\n", __func__);
271
272         edac_class = edac_get_edac_class();
273         if (edac_class == NULL) {
274                 debugf1("%s() no edac_class\n", __func__);
275                 return -ENODEV;
276         }
277
278         edac_pci_kobj.ktype = &ktype_edac_pci;
279
280         edac_pci_kobj.parent = &edac_class->kset.kobj;
281
282         err = kobject_set_name(&edac_pci_kobj, "pci");
283         if(err)
284                 return err;
285
286         /* Instanstiate the pci object */
287         /* FIXME: maybe new sysdev_create_subdir() */
288         err = kobject_register(&edac_pci_kobj);
289
290         if (err) {
291                 debugf1("Failed to register '.../edac/pci'\n");
292                 return err;
293         }
294
295         debugf1("Registered '.../edac/pci' kobject\n");
296
297         return 0;
298 }
299
300 /*
301  * edac_pci_unregister_main_kobj()
302  *
303  *      perform the sysfs teardown for the PCI attributes
304  */
305 void edac_pci_unregister_main_kobj(void)
306 {
307         debugf0("%s()\n", __func__);
308         init_completion(&edac_pci_kobj_complete);
309         kobject_unregister(&edac_pci_kobj);
310         wait_for_completion(&edac_pci_kobj_complete);
311 }
312
313 int edac_pci_create_sysfs(struct edac_pci_ctl_info *pci)
314 {
315         int err;
316         struct kobject *edac_kobj = &pci->kobj;
317
318         if (atomic_inc_return(&edac_pci_sysfs_refcount) == 1) {
319                 err = edac_pci_register_main_kobj();
320                 if (err) {
321                         atomic_dec(&edac_pci_sysfs_refcount);
322                         return err;
323                 }
324         }
325
326         err = edac_pci_create_instance_kobj(pci, pci->pci_idx);
327         if (err) {
328                 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0)
329                         edac_pci_unregister_main_kobj();
330         }
331
332
333         debugf0("%s() idx=%d\n", __func__, pci->pci_idx);
334
335         err = sysfs_create_link(edac_kobj,
336                         &pci->dev->kobj,
337                         EDAC_PCI_SYMLINK);
338         if (err) {
339                 debugf0("%s() sysfs_create_link() returned err= %d\n",
340                                 __func__, err);
341                 return err;
342         }
343
344         return 0;
345 }
346
347 void edac_pci_remove_sysfs(struct edac_pci_ctl_info *pci)
348 {
349         debugf0("%s()\n", __func__);
350
351         edac_pci_delete_instance_kobj(pci, pci->pci_idx);
352
353         sysfs_remove_link(&pci->kobj, EDAC_PCI_SYMLINK);
354
355         if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0)
356                 edac_pci_unregister_main_kobj();
357 }
358
359 /************************ PCI error handling *************************/
360 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
361 {
362         int where;
363         u16 status;
364
365         where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
366         pci_read_config_word(dev, where, &status);
367
368         /* If we get back 0xFFFF then we must suspect that the card has been
369          * pulled but the Linux PCI layer has not yet finished cleaning up.
370          * We don't want to report on such devices
371          */
372
373         if (status == 0xFFFF) {
374                 u32 sanity;
375
376                 pci_read_config_dword(dev, 0, &sanity);
377
378                 if (sanity == 0xFFFFFFFF)
379                         return 0;
380         }
381
382         status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
383                 PCI_STATUS_PARITY;
384
385         if (status)
386                 /* reset only the bits we are interested in */
387                 pci_write_config_word(dev, where, status);
388
389         return status;
390 }
391
392 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
393
394 /* Clear any PCI parity errors logged by this device. */
395 static void edac_pci_dev_parity_clear(struct pci_dev *dev)
396 {
397         u8 header_type;
398
399         get_pci_parity_status(dev, 0);
400
401         /* read the device TYPE, looking for bridges */
402         pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
403
404         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
405                 get_pci_parity_status(dev, 1);
406 }
407
408 /*
409  *  PCI Parity polling
410  *
411  */
412 static void edac_pci_dev_parity_test(struct pci_dev *dev)
413 {
414         u16 status;
415         u8  header_type;
416
417         /* read the STATUS register on this device
418          */
419         status = get_pci_parity_status(dev, 0);
420
421         debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
422
423         /* check the status reg for errors */
424         if (status) {
425                 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
426                         edac_printk(KERN_CRIT, EDAC_PCI,
427                                 "Signaled System Error on %s\n",
428                                 pci_name(dev));
429                         atomic_inc(&pci_nonparity_count);
430                 }
431
432                 if (status & (PCI_STATUS_PARITY)) {
433                         edac_printk(KERN_CRIT, EDAC_PCI,
434                                 "Master Data Parity Error on %s\n",
435                                 pci_name(dev));
436
437                         atomic_inc(&pci_parity_count);
438                 }
439
440                 if (status & (PCI_STATUS_DETECTED_PARITY)) {
441                         edac_printk(KERN_CRIT, EDAC_PCI,
442                                 "Detected Parity Error on %s\n",
443                                 pci_name(dev));
444
445                         atomic_inc(&pci_parity_count);
446                 }
447         }
448
449         /* read the device TYPE, looking for bridges */
450         pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
451
452         debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
453
454         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
455                 /* On bridges, need to examine secondary status register  */
456                 status = get_pci_parity_status(dev, 1);
457
458                 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
459                                 status, dev->dev.bus_id );
460
461                 /* check the secondary status reg for errors */
462                 if (status) {
463                         if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
464                                 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
465                                         "Signaled System Error on %s\n",
466                                         pci_name(dev));
467                                 atomic_inc(&pci_nonparity_count);
468                         }
469
470                         if (status & (PCI_STATUS_PARITY)) {
471                                 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
472                                         "Master Data Parity Error on "
473                                         "%s\n", pci_name(dev));
474
475                                 atomic_inc(&pci_parity_count);
476                         }
477
478                         if (status & (PCI_STATUS_DETECTED_PARITY)) {
479                                 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
480                                         "Detected Parity Error on %s\n",
481                                         pci_name(dev));
482
483                                 atomic_inc(&pci_parity_count);
484                         }
485                 }
486         }
487 }
488
489 /*
490  * pci_dev parity list iterator
491  *      Scan the PCI device list for one iteration, looking for SERRORs
492  *      Master Parity ERRORS or Parity ERRORs on primary or secondary devices
493  */
494 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
495 {
496         struct pci_dev *dev = NULL;
497
498         /* request for kernel access to the next PCI device, if any,
499          * and while we are looking at it have its reference count
500          * bumped until we are done with it
501          */
502         while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
503                 fn(dev);
504         }
505 }
506
507 /*
508  * edac_pci_do_parity_check
509  *
510  *      performs the actual PCI parity check operation
511  */
512 void edac_pci_do_parity_check(void)
513 {
514         unsigned long flags;
515         int before_count;
516
517         debugf3("%s()\n", __func__);
518
519         if (!check_pci_errors)
520                 return;
521
522         before_count = atomic_read(&pci_parity_count);
523
524         /* scan all PCI devices looking for a Parity Error on devices and
525          * bridges
526          */
527         local_irq_save(flags);
528         edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
529         local_irq_restore(flags);
530
531         /* Only if operator has selected panic on PCI Error */
532         if (panic_on_pci_parity) {
533                 /* If the count is different 'after' from 'before' */
534                 if (before_count != atomic_read(&pci_parity_count))
535                         panic("EDAC: PCI Parity Error");
536         }
537 }
538
539 void edac_pci_clear_parity_errors(void)
540 {
541         /* Clear any PCI bus parity errors that devices initially have logged
542          * in their registers.
543          */
544         edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
545 }
546 void edac_pci_handle_pe(struct edac_pci_ctl_info *pci, const char *msg)
547 {
548
549         /* global PE counter incremented by edac_pci_do_parity_check() */
550         atomic_inc(&pci->counters.pe_count);
551
552         if (log_pci_errs)
553                 edac_pci_printk(pci, KERN_WARNING,
554                                 "Parity Error ctl: %s %d: %s\n",
555                                 pci->ctl_name, pci->pci_idx, msg);
556
557         /*
558          * poke all PCI devices and see which one is the troublemaker
559          * panic() is called if set
560          */
561         edac_pci_do_parity_check();
562 }
563 EXPORT_SYMBOL_GPL(edac_pci_handle_pe);
564
565 void edac_pci_handle_npe(struct edac_pci_ctl_info *pci, const char *msg)
566 {
567
568         /* global NPE counter incremented by edac_pci_do_parity_check() */
569         atomic_inc(&pci->counters.npe_count);
570
571         if (log_pci_errs)
572                 edac_pci_printk(pci, KERN_WARNING,
573                                 "Non-Parity Error ctl: %s %d: %s\n",
574                                 pci->ctl_name, pci->pci_idx, msg);
575
576         /*
577          * poke all PCI devices and see which one is the troublemaker
578          * panic() is called if set
579          */
580         edac_pci_do_parity_check();
581 }
582 EXPORT_SYMBOL_GPL(edac_pci_handle_npe);
583
584 /*
585  * Define the PCI parameter to the module
586  */
587 module_param(check_pci_errors, int, 0644);
588 MODULE_PARM_DESC(check_pci_errors, "Check for PCI bus parity errors: 0=off 1=on");
589 module_param(panic_on_pci_parity, int, 0644);
590 MODULE_PARM_DESC(panic_on_pci_parity, "Panic on PCI Bus Parity error: 0=off 1=on");
591
592 #endif  /* CONFIG_PCI */