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