drivers/edac: updated PCI monitoring
[linux-2.6/sactl.git] / drivers / edac / edac_pci_sysfs.c
blob0b179e0fd112730ffe3e4ce1a6d92cf29620a1be
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.
6 * Written Doug Thompson <norsk5@xmission.com>
8 */
9 #include <linux/module.h>
10 #include <linux/sysdev.h>
11 #include <linux/ctype.h>
13 #include "edac_core.h"
14 #include "edac_module.h"
17 #ifdef CONFIG_PCI
19 #define EDAC_PCI_SYMLINK "device"
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);
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);
31 /**************************** EDAC PCI sysfs instance *******************/
32 static ssize_t instance_pe_count_show(struct edac_pci_ctl_info *pci, char *data)
34 return sprintf(data,"%u\n", atomic_read(&pci->counters.pe_count));
37 static ssize_t instance_npe_count_show(struct edac_pci_ctl_info *pci,
38 char *data)
40 return sprintf(data,"%u\n", atomic_read(&pci->counters.npe_count));
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)
46 /* DEVICE instance kobject release() function */
47 static void edac_pci_instance_release(struct kobject *kobj)
49 struct edac_pci_ctl_info *pci;
51 debugf1("%s()\n", __func__);
53 pci = to_instance(kobj);
54 complete(&pci->kobj_complete);
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);
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)
69 struct edac_pci_ctl_info *pci = to_instance(kobj);
70 struct instance_attribute *instance_attr = to_instance_attr(attr);
72 if (instance_attr->show)
73 return instance_attr->show(pci, buffer);
74 return -EIO;
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)
83 struct edac_pci_ctl_info *pci = to_instance(kobj);
84 struct instance_attribute *instance_attr = to_instance_attr(attr);
86 if (instance_attr->store)
87 return instance_attr->store(pci, buffer, count);
88 return -EIO;
91 static struct sysfs_ops pci_instance_ops = {
92 .show = edac_pci_instance_show,
93 .store = edac_pci_instance_store
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, \
103 INSTANCE_ATTR(pe_count, S_IRUGO, instance_pe_count_show, NULL);
104 INSTANCE_ATTR(npe_count, S_IRUGO, instance_npe_count_show, NULL);
106 /* pci instance attributes */
107 static struct instance_attribute *pci_instance_attr[] = {
108 &attr_instance_pe_count,
109 &attr_instance_npe_count,
110 NULL
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,
120 static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
122 int err;
124 pci->kobj.parent = &edac_pci_kobj;
125 pci->kobj.ktype = &ktype_pci_instance;
127 err = kobject_set_name(&pci->kobj, "pci%d", idx);
128 if (err)
129 return err;
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;
138 debugf1("%s() Register instance 'pci%d' kobject\n", __func__, idx);
140 return 0;
143 static void
144 edac_pci_delete_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
146 init_completion(&pci->kobj_complete);
147 kobject_unregister(&pci->kobj);
148 wait_for_completion(&pci->kobj_complete);
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)
155 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
157 int *value = ptr;
158 return sprintf(buffer,"%d\n",*value);
161 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
163 int *value = ptr;
165 if (isdigit(*buffer))
166 *value = simple_strtoul(buffer,NULL,0);
168 return count;
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);
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)
182 struct edac_pci_dev_attribute *edac_pci_dev;
183 edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
185 if (edac_pci_dev->show)
186 return edac_pci_dev->show(edac_pci_dev->value, buffer);
187 return -EIO;
190 static ssize_t edac_pci_dev_store(struct kobject *kobj,
191 struct attribute *attr, const char *buffer, size_t count)
193 struct edac_pci_dev_attribute *edac_pci_dev;
194 edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
196 if (edac_pci_dev->show)
197 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
198 return -EIO;
201 static struct sysfs_ops edac_pci_sysfs_ops = {
202 .show = edac_pci_dev_show,
203 .store = edac_pci_dev_store
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, \
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, \
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);
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,
242 /* No memory to release */
243 static void edac_pci_release(struct kobject *kobj)
245 struct edac_pci_ctl_info *pci;
247 pci = to_edacpci(kobj);
249 debugf1("%s()\n", __func__);
250 complete(&pci->kobj_complete);
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,
260 * edac_sysfs_pci_setup()
262 * setup the sysfs for EDAC PCI attributes
263 * assumes edac_class has already been initialized
265 int edac_pci_register_main_kobj(void)
267 int err;
268 struct sysdev_class *edac_class;
270 debugf1("%s()\n", __func__);
272 edac_class = edac_get_edac_class();
273 if (edac_class == NULL) {
274 debugf1("%s() no edac_class\n", __func__);
275 return -ENODEV;
278 edac_pci_kobj.ktype = &ktype_edac_pci;
280 edac_pci_kobj.parent = &edac_class->kset.kobj;
282 err = kobject_set_name(&edac_pci_kobj, "pci");
283 if(err)
284 return err;
286 /* Instanstiate the pci object */
287 /* FIXME: maybe new sysdev_create_subdir() */
288 err = kobject_register(&edac_pci_kobj);
290 if (err) {
291 debugf1("Failed to register '.../edac/pci'\n");
292 return err;
295 debugf1("Registered '.../edac/pci' kobject\n");
297 return 0;
301 * edac_pci_unregister_main_kobj()
303 * perform the sysfs teardown for the PCI attributes
305 void edac_pci_unregister_main_kobj(void)
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);
313 int edac_pci_create_sysfs(struct edac_pci_ctl_info *pci)
315 int err;
316 struct kobject *edac_kobj = &pci->kobj;
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;
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();
333 debugf0("%s() idx=%d\n", __func__, pci->pci_idx);
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;
344 return 0;
347 void edac_pci_remove_sysfs(struct edac_pci_ctl_info *pci)
349 debugf0("%s()\n", __func__);
351 edac_pci_delete_instance_kobj(pci, pci->pci_idx);
353 sysfs_remove_link(&pci->kobj, EDAC_PCI_SYMLINK);
355 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0)
356 edac_pci_unregister_main_kobj();
359 /************************ PCI error handling *************************/
360 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
362 int where;
363 u16 status;
365 where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
366 pci_read_config_word(dev, where, &status);
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
373 if (status == 0xFFFF) {
374 u32 sanity;
376 pci_read_config_dword(dev, 0, &sanity);
378 if (sanity == 0xFFFFFFFF)
379 return 0;
382 status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
383 PCI_STATUS_PARITY;
385 if (status)
386 /* reset only the bits we are interested in */
387 pci_write_config_word(dev, where, status);
389 return status;
392 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
394 /* Clear any PCI parity errors logged by this device. */
395 static void edac_pci_dev_parity_clear(struct pci_dev *dev)
397 u8 header_type;
399 get_pci_parity_status(dev, 0);
401 /* read the device TYPE, looking for bridges */
402 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
404 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
405 get_pci_parity_status(dev, 1);
409 * PCI Parity polling
412 static void edac_pci_dev_parity_test(struct pci_dev *dev)
414 u16 status;
415 u8 header_type;
417 /* read the STATUS register on this device
419 status = get_pci_parity_status(dev, 0);
421 debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
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);
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));
437 atomic_inc(&pci_parity_count);
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));
445 atomic_inc(&pci_parity_count);
449 /* read the device TYPE, looking for bridges */
450 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
452 debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
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);
458 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
459 status, dev->dev.bus_id );
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);
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));
475 atomic_inc(&pci_parity_count);
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));
483 atomic_inc(&pci_parity_count);
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
494 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
496 struct pci_dev *dev = NULL;
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
502 while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
503 fn(dev);
508 * edac_pci_do_parity_check
510 * performs the actual PCI parity check operation
512 void edac_pci_do_parity_check(void)
514 unsigned long flags;
515 int before_count;
517 debugf3("%s()\n", __func__);
519 if (!check_pci_errors)
520 return;
522 before_count = atomic_read(&pci_parity_count);
524 /* scan all PCI devices looking for a Parity Error on devices and
525 * bridges
527 local_irq_save(flags);
528 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
529 local_irq_restore(flags);
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");
539 void edac_pci_clear_parity_errors(void)
541 /* Clear any PCI bus parity errors that devices initially have logged
542 * in their registers.
544 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
546 void edac_pci_handle_pe(struct edac_pci_ctl_info *pci, const char *msg)
549 /* global PE counter incremented by edac_pci_do_parity_check() */
550 atomic_inc(&pci->counters.pe_count);
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);
558 * poke all PCI devices and see which one is the troublemaker
559 * panic() is called if set
561 edac_pci_do_parity_check();
563 EXPORT_SYMBOL_GPL(edac_pci_handle_pe);
565 void edac_pci_handle_npe(struct edac_pci_ctl_info *pci, const char *msg)
568 /* global NPE counter incremented by edac_pci_do_parity_check() */
569 atomic_inc(&pci->counters.npe_count);
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);
577 * poke all PCI devices and see which one is the troublemaker
578 * panic() is called if set
580 edac_pci_do_parity_check();
582 EXPORT_SYMBOL_GPL(edac_pci_handle_npe);
585 * Define the PCI parameter to the module
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");
592 #endif /* CONFIG_PCI */