[PATCH] edac: disable a few sysfs files to avoid them becoming an ABI
[linux/fpc-iii.git] / drivers / edac / edac_mc.c
blob262e44544dc874bca9eb962036a88f9c022ab980
1 /*
2 * edac_mc kernel module
3 * (C) 2005 Linux Networx (http://lnxi.com)
4 * This file may be distributed under the terms of the
5 * GNU General Public License.
7 * Written by Thayne Harbaugh
8 * Based on work by Dan Hollis <goemon at anime dot net> and others.
9 * http://www.anime.net/~goemon/linux-ecc/
11 * Modified by Dave Peterson and Doug Thompson
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/proc_fs.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/smp.h>
22 #include <linux/init.h>
23 #include <linux/sysctl.h>
24 #include <linux/highmem.h>
25 #include <linux/timer.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/sysdev.h>
31 #include <linux/ctype.h>
33 #include <asm/uaccess.h>
34 #include <asm/page.h>
35 #include <asm/edac.h>
37 #include "edac_mc.h"
39 #define EDAC_MC_VERSION "edac_mc Ver: 2.0.0 " __DATE__
41 #ifdef CONFIG_EDAC_DEBUG
42 /* Values of 0 to 4 will generate output */
43 int edac_debug_level = 1;
44 EXPORT_SYMBOL(edac_debug_level);
45 #endif
47 /* EDAC Controls, setable by module parameter, and sysfs */
48 static int log_ue = 1;
49 static int log_ce = 1;
50 static int panic_on_ue = 1;
51 static int poll_msec = 1000;
53 static int check_pci_parity = 0; /* default YES check PCI parity */
54 static int panic_on_pci_parity; /* default no panic on PCI Parity */
55 static atomic_t pci_parity_count = ATOMIC_INIT(0);
57 /* lock to memory controller's control array */
58 static DECLARE_MUTEX(mem_ctls_mutex);
59 static struct list_head mc_devices = LIST_HEAD_INIT(mc_devices);
61 /* Structure of the whitelist and blacklist arrays */
62 struct edac_pci_device_list {
63 unsigned int vendor; /* Vendor ID */
64 unsigned int device; /* Deviice ID */
68 #define MAX_LISTED_PCI_DEVICES 32
70 /* List of PCI devices (vendor-id:device-id) that should be skipped */
71 static struct edac_pci_device_list pci_blacklist[MAX_LISTED_PCI_DEVICES];
72 static int pci_blacklist_count;
74 /* List of PCI devices (vendor-id:device-id) that should be scanned */
75 static struct edac_pci_device_list pci_whitelist[MAX_LISTED_PCI_DEVICES];
76 static int pci_whitelist_count ;
78 /* START sysfs data and methods */
80 static const char *mem_types[] = {
81 [MEM_EMPTY] = "Empty",
82 [MEM_RESERVED] = "Reserved",
83 [MEM_UNKNOWN] = "Unknown",
84 [MEM_FPM] = "FPM",
85 [MEM_EDO] = "EDO",
86 [MEM_BEDO] = "BEDO",
87 [MEM_SDR] = "Unbuffered-SDR",
88 [MEM_RDR] = "Registered-SDR",
89 [MEM_DDR] = "Unbuffered-DDR",
90 [MEM_RDDR] = "Registered-DDR",
91 [MEM_RMBS] = "RMBS"
94 static const char *dev_types[] = {
95 [DEV_UNKNOWN] = "Unknown",
96 [DEV_X1] = "x1",
97 [DEV_X2] = "x2",
98 [DEV_X4] = "x4",
99 [DEV_X8] = "x8",
100 [DEV_X16] = "x16",
101 [DEV_X32] = "x32",
102 [DEV_X64] = "x64"
105 static const char *edac_caps[] = {
106 [EDAC_UNKNOWN] = "Unknown",
107 [EDAC_NONE] = "None",
108 [EDAC_RESERVED] = "Reserved",
109 [EDAC_PARITY] = "PARITY",
110 [EDAC_EC] = "EC",
111 [EDAC_SECDED] = "SECDED",
112 [EDAC_S2ECD2ED] = "S2ECD2ED",
113 [EDAC_S4ECD4ED] = "S4ECD4ED",
114 [EDAC_S8ECD8ED] = "S8ECD8ED",
115 [EDAC_S16ECD16ED] = "S16ECD16ED"
119 /* sysfs object: /sys/devices/system/edac */
120 static struct sysdev_class edac_class = {
121 set_kset_name("edac"),
124 /* sysfs objects:
125 * /sys/devices/system/edac/mc
126 * /sys/devices/system/edac/pci
128 static struct kobject edac_memctrl_kobj;
129 static struct kobject edac_pci_kobj;
132 * /sys/devices/system/edac/mc;
133 * data structures and methods
135 #if 0
136 static ssize_t memctrl_string_show(void *ptr, char *buffer)
138 char *value = (char*) ptr;
139 return sprintf(buffer, "%s\n", value);
141 #endif
143 static ssize_t memctrl_int_show(void *ptr, char *buffer)
145 int *value = (int*) ptr;
146 return sprintf(buffer, "%d\n", *value);
149 static ssize_t memctrl_int_store(void *ptr, const char *buffer, size_t count)
151 int *value = (int*) ptr;
153 if (isdigit(*buffer))
154 *value = simple_strtoul(buffer, NULL, 0);
156 return count;
159 struct memctrl_dev_attribute {
160 struct attribute attr;
161 void *value;
162 ssize_t (*show)(void *,char *);
163 ssize_t (*store)(void *, const char *, size_t);
166 /* Set of show/store abstract level functions for memory control object */
167 static ssize_t
168 memctrl_dev_show(struct kobject *kobj, struct attribute *attr, char *buffer)
170 struct memctrl_dev_attribute *memctrl_dev;
171 memctrl_dev = (struct memctrl_dev_attribute*)attr;
173 if (memctrl_dev->show)
174 return memctrl_dev->show(memctrl_dev->value, buffer);
175 return -EIO;
178 static ssize_t
179 memctrl_dev_store(struct kobject *kobj, struct attribute *attr,
180 const char *buffer, size_t count)
182 struct memctrl_dev_attribute *memctrl_dev;
183 memctrl_dev = (struct memctrl_dev_attribute*)attr;
185 if (memctrl_dev->store)
186 return memctrl_dev->store(memctrl_dev->value, buffer, count);
187 return -EIO;
190 static struct sysfs_ops memctrlfs_ops = {
191 .show = memctrl_dev_show,
192 .store = memctrl_dev_store
195 #define MEMCTRL_ATTR(_name,_mode,_show,_store) \
196 struct memctrl_dev_attribute attr_##_name = { \
197 .attr = {.name = __stringify(_name), .mode = _mode }, \
198 .value = &_name, \
199 .show = _show, \
200 .store = _store, \
203 #define MEMCTRL_STRING_ATTR(_name,_data,_mode,_show,_store) \
204 struct memctrl_dev_attribute attr_##_name = { \
205 .attr = {.name = __stringify(_name), .mode = _mode }, \
206 .value = _data, \
207 .show = _show, \
208 .store = _store, \
211 /* cwrow<id> attribute f*/
212 #if 0
213 MEMCTRL_STRING_ATTR(mc_version,EDAC_MC_VERSION,S_IRUGO,memctrl_string_show,NULL);
214 #endif
216 /* csrow<id> control files */
217 MEMCTRL_ATTR(panic_on_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
218 MEMCTRL_ATTR(log_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
219 MEMCTRL_ATTR(log_ce,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
220 MEMCTRL_ATTR(poll_msec,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
223 /* Base Attributes of the memory ECC object */
224 static struct memctrl_dev_attribute *memctrl_attr[] = {
225 &attr_panic_on_ue,
226 &attr_log_ue,
227 &attr_log_ce,
228 &attr_poll_msec,
229 NULL,
232 /* Main MC kobject release() function */
233 static void edac_memctrl_master_release(struct kobject *kobj)
235 debugf1("EDAC MC: " __FILE__ ": %s()\n", __func__);
238 static struct kobj_type ktype_memctrl = {
239 .release = edac_memctrl_master_release,
240 .sysfs_ops = &memctrlfs_ops,
241 .default_attrs = (struct attribute **) memctrl_attr,
245 /* Initialize the main sysfs entries for edac:
246 * /sys/devices/system/edac
248 * and children
250 * Return: 0 SUCCESS
251 * !0 FAILURE
253 static int edac_sysfs_memctrl_setup(void)
255 int err=0;
257 debugf1("MC: " __FILE__ ": %s()\n", __func__);
259 /* create the /sys/devices/system/edac directory */
260 err = sysdev_class_register(&edac_class);
261 if (!err) {
262 /* Init the MC's kobject */
263 memset(&edac_memctrl_kobj, 0, sizeof (edac_memctrl_kobj));
264 kobject_init(&edac_memctrl_kobj);
266 edac_memctrl_kobj.parent = &edac_class.kset.kobj;
267 edac_memctrl_kobj.ktype = &ktype_memctrl;
269 /* generate sysfs "..../edac/mc" */
270 err = kobject_set_name(&edac_memctrl_kobj,"mc");
271 if (!err) {
272 /* FIXME: maybe new sysdev_create_subdir() */
273 err = kobject_register(&edac_memctrl_kobj);
274 if (err) {
275 debugf1("Failed to register '.../edac/mc'\n");
276 } else {
277 debugf1("Registered '.../edac/mc' kobject\n");
280 } else {
281 debugf1(KERN_WARNING "__FILE__ %s() error=%d\n", __func__,err);
284 return err;
288 * MC teardown:
289 * the '..../edac/mc' kobject followed by '..../edac' itself
291 static void edac_sysfs_memctrl_teardown(void)
293 debugf0("MC: " __FILE__ ": %s()\n", __func__);
295 /* Unregister the MC's kobject */
296 kobject_unregister(&edac_memctrl_kobj);
298 /* release the master edac mc kobject */
299 kobject_put(&edac_memctrl_kobj);
301 /* Unregister the 'edac' object */
302 sysdev_class_unregister(&edac_class);
306 * /sys/devices/system/edac/pci;
307 * data structures and methods
310 struct list_control {
311 struct edac_pci_device_list *list;
312 int *count;
316 #if 0
317 /* Output the list as: vendor_id:device:id<,vendor_id:device_id> */
318 static ssize_t edac_pci_list_string_show(void *ptr, char *buffer)
320 struct list_control *listctl;
321 struct edac_pci_device_list *list;
322 char *p = buffer;
323 int len=0;
324 int i;
326 listctl = ptr;
327 list = listctl->list;
329 for (i = 0; i < *(listctl->count); i++, list++ ) {
330 if (len > 0)
331 len += snprintf(p + len, (PAGE_SIZE-len), ",");
333 len += snprintf(p + len,
334 (PAGE_SIZE-len),
335 "%x:%x",
336 list->vendor,list->device);
339 len += snprintf(p + len,(PAGE_SIZE-len), "\n");
341 return (ssize_t) len;
346 * Scan string from **s to **e looking for one 'vendor:device' tuple
347 * where each field is a hex value
349 * return 0 if an entry is NOT found
350 * return 1 if an entry is found
351 * fill in *vendor_id and *device_id with values found
353 * In both cases, make sure *s has been moved forward toward *e
355 static int parse_one_device(const char **s,const char **e,
356 unsigned int *vendor_id, unsigned int *device_id)
358 const char *runner, *p;
360 /* if null byte, we are done */
361 if (!**s) {
362 (*s)++; /* keep *s moving */
363 return 0;
366 /* skip over newlines & whitespace */
367 if ((**s == '\n') || isspace(**s)) {
368 (*s)++;
369 return 0;
372 if (!isxdigit(**s)) {
373 (*s)++;
374 return 0;
377 /* parse vendor_id */
378 runner = *s;
379 while (runner < *e) {
380 /* scan for vendor:device delimiter */
381 if (*runner == ':') {
382 *vendor_id = simple_strtol((char*) *s, (char**) &p, 16);
383 runner = p + 1;
384 break;
386 runner++;
389 if (!isxdigit(*runner)) {
390 *s = ++runner;
391 return 0;
394 /* parse device_id */
395 if (runner < *e) {
396 *device_id = simple_strtol((char*)runner, (char**)&p, 16);
397 runner = p;
400 *s = runner;
402 return 1;
405 static ssize_t edac_pci_list_string_store(void *ptr, const char *buffer,
406 size_t count)
408 struct list_control *listctl;
409 struct edac_pci_device_list *list;
410 unsigned int vendor_id, device_id;
411 const char *s, *e;
412 int *index;
414 s = (char*)buffer;
415 e = s + count;
417 listctl = ptr;
418 list = listctl->list;
419 index = listctl->count;
421 *index = 0;
422 while (*index < MAX_LISTED_PCI_DEVICES) {
424 if (parse_one_device(&s,&e,&vendor_id,&device_id)) {
425 list[ *index ].vendor = vendor_id;
426 list[ *index ].device = device_id;
427 (*index)++;
430 /* check for all data consume */
431 if (s >= e)
432 break;
435 return count;
438 #endif
439 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
441 int *value = ptr;
442 return sprintf(buffer,"%d\n",*value);
445 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
447 int *value = ptr;
449 if (isdigit(*buffer))
450 *value = simple_strtoul(buffer,NULL,0);
452 return count;
455 struct edac_pci_dev_attribute {
456 struct attribute attr;
457 void *value;
458 ssize_t (*show)(void *,char *);
459 ssize_t (*store)(void *, const char *,size_t);
462 /* Set of show/store abstract level functions for PCI Parity object */
463 static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
464 char *buffer)
466 struct edac_pci_dev_attribute *edac_pci_dev;
467 edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
469 if (edac_pci_dev->show)
470 return edac_pci_dev->show(edac_pci_dev->value, buffer);
471 return -EIO;
474 static ssize_t edac_pci_dev_store(struct kobject *kobj, struct attribute *attr,
475 const char *buffer, size_t count)
477 struct edac_pci_dev_attribute *edac_pci_dev;
478 edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
480 if (edac_pci_dev->show)
481 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
482 return -EIO;
485 static struct sysfs_ops edac_pci_sysfs_ops = {
486 .show = edac_pci_dev_show,
487 .store = edac_pci_dev_store
491 #define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
492 struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
493 .attr = {.name = __stringify(_name), .mode = _mode }, \
494 .value = &_name, \
495 .show = _show, \
496 .store = _store, \
499 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
500 struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
501 .attr = {.name = __stringify(_name), .mode = _mode }, \
502 .value = _data, \
503 .show = _show, \
504 .store = _store, \
507 #if 0
508 static struct list_control pci_whitelist_control = {
509 .list = pci_whitelist,
510 .count = &pci_whitelist_count
513 static struct list_control pci_blacklist_control = {
514 .list = pci_blacklist,
515 .count = &pci_blacklist_count
518 /* whitelist attribute */
519 EDAC_PCI_STRING_ATTR(pci_parity_whitelist,
520 &pci_whitelist_control,
521 S_IRUGO|S_IWUSR,
522 edac_pci_list_string_show,
523 edac_pci_list_string_store);
525 EDAC_PCI_STRING_ATTR(pci_parity_blacklist,
526 &pci_blacklist_control,
527 S_IRUGO|S_IWUSR,
528 edac_pci_list_string_show,
529 edac_pci_list_string_store);
530 #endif
532 /* PCI Parity control files */
533 EDAC_PCI_ATTR(check_pci_parity,S_IRUGO|S_IWUSR,edac_pci_int_show,edac_pci_int_store);
534 EDAC_PCI_ATTR(panic_on_pci_parity,S_IRUGO|S_IWUSR,edac_pci_int_show,edac_pci_int_store);
535 EDAC_PCI_ATTR(pci_parity_count,S_IRUGO,edac_pci_int_show,NULL);
537 /* Base Attributes of the memory ECC object */
538 static struct edac_pci_dev_attribute *edac_pci_attr[] = {
539 &edac_pci_attr_check_pci_parity,
540 &edac_pci_attr_panic_on_pci_parity,
541 &edac_pci_attr_pci_parity_count,
542 NULL,
545 /* No memory to release */
546 static void edac_pci_release(struct kobject *kobj)
548 debugf1("EDAC PCI: " __FILE__ ": %s()\n", __func__);
551 static struct kobj_type ktype_edac_pci = {
552 .release = edac_pci_release,
553 .sysfs_ops = &edac_pci_sysfs_ops,
554 .default_attrs = (struct attribute **) edac_pci_attr,
558 * edac_sysfs_pci_setup()
561 static int edac_sysfs_pci_setup(void)
563 int err;
565 debugf1("MC: " __FILE__ ": %s()\n", __func__);
567 memset(&edac_pci_kobj, 0, sizeof(edac_pci_kobj));
569 kobject_init(&edac_pci_kobj);
570 edac_pci_kobj.parent = &edac_class.kset.kobj;
571 edac_pci_kobj.ktype = &ktype_edac_pci;
573 err = kobject_set_name(&edac_pci_kobj, "pci");
574 if (!err) {
575 /* Instanstiate the csrow object */
576 /* FIXME: maybe new sysdev_create_subdir() */
577 err = kobject_register(&edac_pci_kobj);
578 if (err)
579 debugf1("Failed to register '.../edac/pci'\n");
580 else
581 debugf1("Registered '.../edac/pci' kobject\n");
583 return err;
587 static void edac_sysfs_pci_teardown(void)
589 debugf0("MC: " __FILE__ ": %s()\n", __func__);
591 kobject_unregister(&edac_pci_kobj);
592 kobject_put(&edac_pci_kobj);
595 /* EDAC sysfs CSROW data structures and methods */
597 /* Set of more detailed csrow<id> attribute show/store functions */
598 static ssize_t csrow_ch0_dimm_label_show(struct csrow_info *csrow, char *data)
600 ssize_t size = 0;
602 if (csrow->nr_channels > 0) {
603 size = snprintf(data, EDAC_MC_LABEL_LEN,"%s\n",
604 csrow->channels[0].label);
606 return size;
609 static ssize_t csrow_ch1_dimm_label_show(struct csrow_info *csrow, char *data)
611 ssize_t size = 0;
613 if (csrow->nr_channels > 0) {
614 size = snprintf(data, EDAC_MC_LABEL_LEN, "%s\n",
615 csrow->channels[1].label);
617 return size;
620 static ssize_t csrow_ch0_dimm_label_store(struct csrow_info *csrow,
621 const char *data, size_t size)
623 ssize_t max_size = 0;
625 if (csrow->nr_channels > 0) {
626 max_size = min((ssize_t)size,(ssize_t)EDAC_MC_LABEL_LEN-1);
627 strncpy(csrow->channels[0].label, data, max_size);
628 csrow->channels[0].label[max_size] = '\0';
630 return size;
633 static ssize_t csrow_ch1_dimm_label_store(struct csrow_info *csrow,
634 const char *data, size_t size)
636 ssize_t max_size = 0;
638 if (csrow->nr_channels > 1) {
639 max_size = min((ssize_t)size,(ssize_t)EDAC_MC_LABEL_LEN-1);
640 strncpy(csrow->channels[1].label, data, max_size);
641 csrow->channels[1].label[max_size] = '\0';
643 return max_size;
646 static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data)
648 return sprintf(data,"%u\n", csrow->ue_count);
651 static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data)
653 return sprintf(data,"%u\n", csrow->ce_count);
656 static ssize_t csrow_ch0_ce_count_show(struct csrow_info *csrow, char *data)
658 ssize_t size = 0;
660 if (csrow->nr_channels > 0) {
661 size = sprintf(data,"%u\n", csrow->channels[0].ce_count);
663 return size;
666 static ssize_t csrow_ch1_ce_count_show(struct csrow_info *csrow, char *data)
668 ssize_t size = 0;
670 if (csrow->nr_channels > 1) {
671 size = sprintf(data,"%u\n", csrow->channels[1].ce_count);
673 return size;
676 static ssize_t csrow_size_show(struct csrow_info *csrow, char *data)
678 return sprintf(data,"%u\n", PAGES_TO_MiB(csrow->nr_pages));
681 static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data)
683 return sprintf(data,"%s\n", mem_types[csrow->mtype]);
686 static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data)
688 return sprintf(data,"%s\n", dev_types[csrow->dtype]);
691 static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data)
693 return sprintf(data,"%s\n", edac_caps[csrow->edac_mode]);
696 struct csrowdev_attribute {
697 struct attribute attr;
698 ssize_t (*show)(struct csrow_info *,char *);
699 ssize_t (*store)(struct csrow_info *, const char *,size_t);
702 #define to_csrow(k) container_of(k, struct csrow_info, kobj)
703 #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
705 /* Set of show/store higher level functions for csrow objects */
706 static ssize_t csrowdev_show(struct kobject *kobj, struct attribute *attr,
707 char *buffer)
709 struct csrow_info *csrow = to_csrow(kobj);
710 struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
712 if (csrowdev_attr->show)
713 return csrowdev_attr->show(csrow, buffer);
714 return -EIO;
717 static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
718 const char *buffer, size_t count)
720 struct csrow_info *csrow = to_csrow(kobj);
721 struct csrowdev_attribute * csrowdev_attr = to_csrowdev_attr(attr);
723 if (csrowdev_attr->store)
724 return csrowdev_attr->store(csrow, buffer, count);
725 return -EIO;
728 static struct sysfs_ops csrowfs_ops = {
729 .show = csrowdev_show,
730 .store = csrowdev_store
733 #define CSROWDEV_ATTR(_name,_mode,_show,_store) \
734 struct csrowdev_attribute attr_##_name = { \
735 .attr = {.name = __stringify(_name), .mode = _mode }, \
736 .show = _show, \
737 .store = _store, \
740 /* cwrow<id>/attribute files */
741 CSROWDEV_ATTR(size_mb,S_IRUGO,csrow_size_show,NULL);
742 CSROWDEV_ATTR(dev_type,S_IRUGO,csrow_dev_type_show,NULL);
743 CSROWDEV_ATTR(mem_type,S_IRUGO,csrow_mem_type_show,NULL);
744 CSROWDEV_ATTR(edac_mode,S_IRUGO,csrow_edac_mode_show,NULL);
745 CSROWDEV_ATTR(ue_count,S_IRUGO,csrow_ue_count_show,NULL);
746 CSROWDEV_ATTR(ce_count,S_IRUGO,csrow_ce_count_show,NULL);
747 CSROWDEV_ATTR(ch0_ce_count,S_IRUGO,csrow_ch0_ce_count_show,NULL);
748 CSROWDEV_ATTR(ch1_ce_count,S_IRUGO,csrow_ch1_ce_count_show,NULL);
750 /* control/attribute files */
751 CSROWDEV_ATTR(ch0_dimm_label,S_IRUGO|S_IWUSR,
752 csrow_ch0_dimm_label_show,
753 csrow_ch0_dimm_label_store);
754 CSROWDEV_ATTR(ch1_dimm_label,S_IRUGO|S_IWUSR,
755 csrow_ch1_dimm_label_show,
756 csrow_ch1_dimm_label_store);
759 /* Attributes of the CSROW<id> object */
760 static struct csrowdev_attribute *csrow_attr[] = {
761 &attr_dev_type,
762 &attr_mem_type,
763 &attr_edac_mode,
764 &attr_size_mb,
765 &attr_ue_count,
766 &attr_ce_count,
767 &attr_ch0_ce_count,
768 &attr_ch1_ce_count,
769 &attr_ch0_dimm_label,
770 &attr_ch1_dimm_label,
771 NULL,
775 /* No memory to release */
776 static void edac_csrow_instance_release(struct kobject *kobj)
778 debugf1("EDAC MC: " __FILE__ ": %s()\n", __func__);
781 static struct kobj_type ktype_csrow = {
782 .release = edac_csrow_instance_release,
783 .sysfs_ops = &csrowfs_ops,
784 .default_attrs = (struct attribute **) csrow_attr,
787 /* Create a CSROW object under specifed edac_mc_device */
788 static int edac_create_csrow_object(struct kobject *edac_mci_kobj,
789 struct csrow_info *csrow, int index )
791 int err = 0;
793 debugf0("MC: " __FILE__ ": %s()\n", __func__);
795 memset(&csrow->kobj, 0, sizeof(csrow->kobj));
797 /* generate ..../edac/mc/mc<id>/csrow<index> */
799 kobject_init(&csrow->kobj);
800 csrow->kobj.parent = edac_mci_kobj;
801 csrow->kobj.ktype = &ktype_csrow;
803 /* name this instance of csrow<id> */
804 err = kobject_set_name(&csrow->kobj,"csrow%d",index);
805 if (!err) {
806 /* Instanstiate the csrow object */
807 err = kobject_register(&csrow->kobj);
808 if (err)
809 debugf0("Failed to register CSROW%d\n",index);
810 else
811 debugf0("Registered CSROW%d\n",index);
814 return err;
817 /* sysfs data structures and methods for the MCI kobjects */
819 static ssize_t mci_reset_counters_store(struct mem_ctl_info *mci,
820 const char *data, size_t count )
822 int row, chan;
824 mci->ue_noinfo_count = 0;
825 mci->ce_noinfo_count = 0;
826 mci->ue_count = 0;
827 mci->ce_count = 0;
828 for (row = 0; row < mci->nr_csrows; row++) {
829 struct csrow_info *ri = &mci->csrows[row];
831 ri->ue_count = 0;
832 ri->ce_count = 0;
833 for (chan = 0; chan < ri->nr_channels; chan++)
834 ri->channels[chan].ce_count = 0;
836 mci->start_time = jiffies;
838 return count;
841 static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
843 return sprintf(data,"%d\n", mci->ue_count);
846 static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
848 return sprintf(data,"%d\n", mci->ce_count);
851 static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
853 return sprintf(data,"%d\n", mci->ce_noinfo_count);
856 static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
858 return sprintf(data,"%d\n", mci->ue_noinfo_count);
861 static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
863 return sprintf(data,"%ld\n", (jiffies - mci->start_time) / HZ);
866 static ssize_t mci_mod_name_show(struct mem_ctl_info *mci, char *data)
868 return sprintf(data,"%s %s\n", mci->mod_name, mci->mod_ver);
871 static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
873 return sprintf(data,"%s\n", mci->ctl_name);
876 static int mci_output_edac_cap(char *buf, unsigned long edac_cap)
878 char *p = buf;
879 int bit_idx;
881 for (bit_idx = 0; bit_idx < 8 * sizeof(edac_cap); bit_idx++) {
882 if ((edac_cap >> bit_idx) & 0x1)
883 p += sprintf(p, "%s ", edac_caps[bit_idx]);
886 return p - buf;
889 static ssize_t mci_edac_capability_show(struct mem_ctl_info *mci, char *data)
891 char *p = data;
893 p += mci_output_edac_cap(p,mci->edac_ctl_cap);
894 p += sprintf(p, "\n");
896 return p - data;
899 static ssize_t mci_edac_current_capability_show(struct mem_ctl_info *mci,
900 char *data)
902 char *p = data;
904 p += mci_output_edac_cap(p,mci->edac_cap);
905 p += sprintf(p, "\n");
907 return p - data;
910 static int mci_output_mtype_cap(char *buf, unsigned long mtype_cap)
912 char *p = buf;
913 int bit_idx;
915 for (bit_idx = 0; bit_idx < 8 * sizeof(mtype_cap); bit_idx++) {
916 if ((mtype_cap >> bit_idx) & 0x1)
917 p += sprintf(p, "%s ", mem_types[bit_idx]);
920 return p - buf;
923 static ssize_t mci_supported_mem_type_show(struct mem_ctl_info *mci, char *data)
925 char *p = data;
927 p += mci_output_mtype_cap(p,mci->mtype_cap);
928 p += sprintf(p, "\n");
930 return p - data;
933 static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
935 int total_pages, csrow_idx;
937 for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
938 csrow_idx++) {
939 struct csrow_info *csrow = &mci->csrows[csrow_idx];
941 if (!csrow->nr_pages)
942 continue;
943 total_pages += csrow->nr_pages;
946 return sprintf(data,"%u\n", PAGES_TO_MiB(total_pages));
949 struct mcidev_attribute {
950 struct attribute attr;
951 ssize_t (*show)(struct mem_ctl_info *,char *);
952 ssize_t (*store)(struct mem_ctl_info *, const char *,size_t);
955 #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
956 #define to_mcidev_attr(a) container_of(a, struct mcidev_attribute, attr)
958 static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
959 char *buffer)
961 struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
962 struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
964 if (mcidev_attr->show)
965 return mcidev_attr->show(mem_ctl_info, buffer);
966 return -EIO;
969 static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
970 const char *buffer, size_t count)
972 struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
973 struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
975 if (mcidev_attr->store)
976 return mcidev_attr->store(mem_ctl_info, buffer, count);
977 return -EIO;
980 static struct sysfs_ops mci_ops = {
981 .show = mcidev_show,
982 .store = mcidev_store
985 #define MCIDEV_ATTR(_name,_mode,_show,_store) \
986 struct mcidev_attribute mci_attr_##_name = { \
987 .attr = {.name = __stringify(_name), .mode = _mode }, \
988 .show = _show, \
989 .store = _store, \
992 /* Control file */
993 MCIDEV_ATTR(reset_counters,S_IWUSR,NULL,mci_reset_counters_store);
995 /* Attribute files */
996 MCIDEV_ATTR(mc_name,S_IRUGO,mci_ctl_name_show,NULL);
997 MCIDEV_ATTR(module_name,S_IRUGO,mci_mod_name_show,NULL);
998 MCIDEV_ATTR(edac_capability,S_IRUGO,mci_edac_capability_show,NULL);
999 MCIDEV_ATTR(size_mb,S_IRUGO,mci_size_mb_show,NULL);
1000 MCIDEV_ATTR(seconds_since_reset,S_IRUGO,mci_seconds_show,NULL);
1001 MCIDEV_ATTR(ue_noinfo_count,S_IRUGO,mci_ue_noinfo_show,NULL);
1002 MCIDEV_ATTR(ce_noinfo_count,S_IRUGO,mci_ce_noinfo_show,NULL);
1003 MCIDEV_ATTR(ue_count,S_IRUGO,mci_ue_count_show,NULL);
1004 MCIDEV_ATTR(ce_count,S_IRUGO,mci_ce_count_show,NULL);
1005 MCIDEV_ATTR(edac_current_capability,S_IRUGO,
1006 mci_edac_current_capability_show,NULL);
1007 MCIDEV_ATTR(supported_mem_type,S_IRUGO,
1008 mci_supported_mem_type_show,NULL);
1011 static struct mcidev_attribute *mci_attr[] = {
1012 &mci_attr_reset_counters,
1013 &mci_attr_module_name,
1014 &mci_attr_mc_name,
1015 &mci_attr_edac_capability,
1016 &mci_attr_edac_current_capability,
1017 &mci_attr_supported_mem_type,
1018 &mci_attr_size_mb,
1019 &mci_attr_seconds_since_reset,
1020 &mci_attr_ue_noinfo_count,
1021 &mci_attr_ce_noinfo_count,
1022 &mci_attr_ue_count,
1023 &mci_attr_ce_count,
1024 NULL
1029 * Release of a MC controlling instance
1031 static void edac_mci_instance_release(struct kobject *kobj)
1033 struct mem_ctl_info *mci;
1034 mci = container_of(kobj,struct mem_ctl_info,edac_mci_kobj);
1036 debugf0("MC: " __FILE__ ": %s() idx=%d calling kfree\n",
1037 __func__, mci->mc_idx);
1039 kfree(mci);
1042 static struct kobj_type ktype_mci = {
1043 .release = edac_mci_instance_release,
1044 .sysfs_ops = &mci_ops,
1045 .default_attrs = (struct attribute **) mci_attr,
1048 #define EDAC_DEVICE_SYMLINK "device"
1051 * Create a new Memory Controller kobject instance,
1052 * mc<id> under the 'mc' directory
1054 * Return:
1055 * 0 Success
1056 * !0 Failure
1058 static int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
1060 int i;
1061 int err;
1062 struct csrow_info *csrow;
1063 struct kobject *edac_mci_kobj=&mci->edac_mci_kobj;
1065 debugf0("MC: " __FILE__ ": %s() idx=%d\n", __func__, mci->mc_idx);
1067 memset(edac_mci_kobj, 0, sizeof(*edac_mci_kobj));
1068 kobject_init(edac_mci_kobj);
1070 /* set the name of the mc<id> object */
1071 err = kobject_set_name(edac_mci_kobj,"mc%d",mci->mc_idx);
1072 if (err)
1073 return err;
1075 /* link to our parent the '..../edac/mc' object */
1076 edac_mci_kobj->parent = &edac_memctrl_kobj;
1077 edac_mci_kobj->ktype = &ktype_mci;
1079 /* register the mc<id> kobject */
1080 err = kobject_register(edac_mci_kobj);
1081 if (err)
1082 return err;
1084 /* create a symlink for the device */
1085 err = sysfs_create_link(edac_mci_kobj, &mci->pdev->dev.kobj,
1086 EDAC_DEVICE_SYMLINK);
1087 if (err) {
1088 kobject_unregister(edac_mci_kobj);
1089 return err;
1092 /* Make directories for each CSROW object
1093 * under the mc<id> kobject
1095 for (i = 0; i < mci->nr_csrows; i++) {
1097 csrow = &mci->csrows[i];
1099 /* Only expose populated CSROWs */
1100 if (csrow->nr_pages > 0) {
1101 err = edac_create_csrow_object(edac_mci_kobj,csrow,i);
1102 if (err)
1103 goto fail;
1107 /* Mark this MCI instance as having sysfs entries */
1108 mci->sysfs_active = MCI_SYSFS_ACTIVE;
1110 return 0;
1113 /* CSROW error: backout what has already been registered, */
1114 fail:
1115 for ( i--; i >= 0; i--) {
1116 if (csrow->nr_pages > 0) {
1117 kobject_unregister(&mci->csrows[i].kobj);
1118 kobject_put(&mci->csrows[i].kobj);
1122 kobject_unregister(edac_mci_kobj);
1123 kobject_put(edac_mci_kobj);
1125 return err;
1129 * remove a Memory Controller instance
1131 static void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
1133 int i;
1135 debugf0("MC: " __FILE__ ": %s()\n", __func__);
1137 /* remove all csrow kobjects */
1138 for (i = 0; i < mci->nr_csrows; i++) {
1139 if (mci->csrows[i].nr_pages > 0) {
1140 kobject_unregister(&mci->csrows[i].kobj);
1141 kobject_put(&mci->csrows[i].kobj);
1145 sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
1147 kobject_unregister(&mci->edac_mci_kobj);
1148 kobject_put(&mci->edac_mci_kobj);
1151 /* END OF sysfs data and methods */
1153 #ifdef CONFIG_EDAC_DEBUG
1155 EXPORT_SYMBOL(edac_mc_dump_channel);
1157 void edac_mc_dump_channel(struct channel_info *chan)
1159 debugf4("\tchannel = %p\n", chan);
1160 debugf4("\tchannel->chan_idx = %d\n", chan->chan_idx);
1161 debugf4("\tchannel->ce_count = %d\n", chan->ce_count);
1162 debugf4("\tchannel->label = '%s'\n", chan->label);
1163 debugf4("\tchannel->csrow = %p\n\n", chan->csrow);
1167 EXPORT_SYMBOL(edac_mc_dump_csrow);
1169 void edac_mc_dump_csrow(struct csrow_info *csrow)
1171 debugf4("\tcsrow = %p\n", csrow);
1172 debugf4("\tcsrow->csrow_idx = %d\n", csrow->csrow_idx);
1173 debugf4("\tcsrow->first_page = 0x%lx\n",
1174 csrow->first_page);
1175 debugf4("\tcsrow->last_page = 0x%lx\n", csrow->last_page);
1176 debugf4("\tcsrow->page_mask = 0x%lx\n", csrow->page_mask);
1177 debugf4("\tcsrow->nr_pages = 0x%x\n", csrow->nr_pages);
1178 debugf4("\tcsrow->nr_channels = %d\n",
1179 csrow->nr_channels);
1180 debugf4("\tcsrow->channels = %p\n", csrow->channels);
1181 debugf4("\tcsrow->mci = %p\n\n", csrow->mci);
1185 EXPORT_SYMBOL(edac_mc_dump_mci);
1187 void edac_mc_dump_mci(struct mem_ctl_info *mci)
1189 debugf3("\tmci = %p\n", mci);
1190 debugf3("\tmci->mtype_cap = %lx\n", mci->mtype_cap);
1191 debugf3("\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
1192 debugf3("\tmci->edac_cap = %lx\n", mci->edac_cap);
1193 debugf4("\tmci->edac_check = %p\n", mci->edac_check);
1194 debugf3("\tmci->nr_csrows = %d, csrows = %p\n",
1195 mci->nr_csrows, mci->csrows);
1196 debugf3("\tpdev = %p\n", mci->pdev);
1197 debugf3("\tmod_name:ctl_name = %s:%s\n",
1198 mci->mod_name, mci->ctl_name);
1199 debugf3("\tpvt_info = %p\n\n", mci->pvt_info);
1203 #endif /* CONFIG_EDAC_DEBUG */
1205 /* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'.
1206 * Adjust 'ptr' so that its alignment is at least as stringent as what the
1207 * compiler would provide for X and return the aligned result.
1209 * If 'size' is a constant, the compiler will optimize this whole function
1210 * down to either a no-op or the addition of a constant to the value of 'ptr'.
1212 static inline char * align_ptr (void *ptr, unsigned size)
1214 unsigned align, r;
1216 /* Here we assume that the alignment of a "long long" is the most
1217 * stringent alignment that the compiler will ever provide by default.
1218 * As far as I know, this is a reasonable assumption.
1220 if (size > sizeof(long))
1221 align = sizeof(long long);
1222 else if (size > sizeof(int))
1223 align = sizeof(long);
1224 else if (size > sizeof(short))
1225 align = sizeof(int);
1226 else if (size > sizeof(char))
1227 align = sizeof(short);
1228 else
1229 return (char *) ptr;
1231 r = size % align;
1233 if (r == 0)
1234 return (char *) ptr;
1236 return (char *) (((unsigned long) ptr) + align - r);
1240 EXPORT_SYMBOL(edac_mc_alloc);
1243 * edac_mc_alloc: Allocate a struct mem_ctl_info structure
1244 * @size_pvt: size of private storage needed
1245 * @nr_csrows: Number of CWROWS needed for this MC
1246 * @nr_chans: Number of channels for the MC
1248 * Everything is kmalloc'ed as one big chunk - more efficient.
1249 * Only can be used if all structures have the same lifetime - otherwise
1250 * you have to allocate and initialize your own structures.
1252 * Use edac_mc_free() to free mc structures allocated by this function.
1254 * Returns:
1255 * NULL allocation failed
1256 * struct mem_ctl_info pointer
1258 struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
1259 unsigned nr_chans)
1261 struct mem_ctl_info *mci;
1262 struct csrow_info *csi, *csrow;
1263 struct channel_info *chi, *chp, *chan;
1264 void *pvt;
1265 unsigned size;
1266 int row, chn;
1268 /* Figure out the offsets of the various items from the start of an mc
1269 * structure. We want the alignment of each item to be at least as
1270 * stringent as what the compiler would provide if we could simply
1271 * hardcode everything into a single struct.
1273 mci = (struct mem_ctl_info *) 0;
1274 csi = (struct csrow_info *)align_ptr(&mci[1], sizeof(*csi));
1275 chi = (struct channel_info *)
1276 align_ptr(&csi[nr_csrows], sizeof(*chi));
1277 pvt = align_ptr(&chi[nr_chans * nr_csrows], sz_pvt);
1278 size = ((unsigned long) pvt) + sz_pvt;
1280 if ((mci = kmalloc(size, GFP_KERNEL)) == NULL)
1281 return NULL;
1283 /* Adjust pointers so they point within the memory we just allocated
1284 * rather than an imaginary chunk of memory located at address 0.
1286 csi = (struct csrow_info *) (((char *) mci) + ((unsigned long) csi));
1287 chi = (struct channel_info *) (((char *) mci) + ((unsigned long) chi));
1288 pvt = sz_pvt ? (((char *) mci) + ((unsigned long) pvt)) : NULL;
1290 memset(mci, 0, size); /* clear all fields */
1292 mci->csrows = csi;
1293 mci->pvt_info = pvt;
1294 mci->nr_csrows = nr_csrows;
1296 for (row = 0; row < nr_csrows; row++) {
1297 csrow = &csi[row];
1298 csrow->csrow_idx = row;
1299 csrow->mci = mci;
1300 csrow->nr_channels = nr_chans;
1301 chp = &chi[row * nr_chans];
1302 csrow->channels = chp;
1304 for (chn = 0; chn < nr_chans; chn++) {
1305 chan = &chp[chn];
1306 chan->chan_idx = chn;
1307 chan->csrow = csrow;
1311 return mci;
1315 EXPORT_SYMBOL(edac_mc_free);
1318 * edac_mc_free: Free a previously allocated 'mci' structure
1319 * @mci: pointer to a struct mem_ctl_info structure
1321 * Free up a previously allocated mci structure
1322 * A MCI structure can be in 2 states after being allocated
1323 * by edac_mc_alloc().
1324 * 1) Allocated in a MC driver's probe, but not yet committed
1325 * 2) Allocated and committed, by a call to edac_mc_add_mc()
1326 * edac_mc_add_mc() is the function that adds the sysfs entries
1327 * thus, this free function must determine which state the 'mci'
1328 * structure is in, then either free it directly or
1329 * perform kobject cleanup by calling edac_remove_sysfs_mci_device().
1331 * VOID Return
1333 void edac_mc_free(struct mem_ctl_info *mci)
1335 /* only if sysfs entries for this mci instance exist
1336 * do we remove them and defer the actual kfree via
1337 * the kobject 'release()' callback.
1339 * Otherwise, do a straight kfree now.
1341 if (mci->sysfs_active == MCI_SYSFS_ACTIVE)
1342 edac_remove_sysfs_mci_device(mci);
1343 else
1344 kfree(mci);
1349 EXPORT_SYMBOL(edac_mc_find_mci_by_pdev);
1351 struct mem_ctl_info *edac_mc_find_mci_by_pdev(struct pci_dev *pdev)
1353 struct mem_ctl_info *mci;
1354 struct list_head *item;
1356 debugf3("MC: " __FILE__ ": %s()\n", __func__);
1358 list_for_each(item, &mc_devices) {
1359 mci = list_entry(item, struct mem_ctl_info, link);
1361 if (mci->pdev == pdev)
1362 return mci;
1365 return NULL;
1368 static int add_mc_to_global_list (struct mem_ctl_info *mci)
1370 struct list_head *item, *insert_before;
1371 struct mem_ctl_info *p;
1372 int i;
1374 if (list_empty(&mc_devices)) {
1375 mci->mc_idx = 0;
1376 insert_before = &mc_devices;
1377 } else {
1378 if (edac_mc_find_mci_by_pdev(mci->pdev)) {
1379 printk(KERN_WARNING
1380 "EDAC MC: %s (%s) %s %s already assigned %d\n",
1381 mci->pdev->dev.bus_id, pci_name(mci->pdev),
1382 mci->mod_name, mci->ctl_name, mci->mc_idx);
1383 return 1;
1386 insert_before = NULL;
1387 i = 0;
1389 list_for_each(item, &mc_devices) {
1390 p = list_entry(item, struct mem_ctl_info, link);
1392 if (p->mc_idx != i) {
1393 insert_before = item;
1394 break;
1397 i++;
1400 mci->mc_idx = i;
1402 if (insert_before == NULL)
1403 insert_before = &mc_devices;
1406 list_add_tail_rcu(&mci->link, insert_before);
1407 return 0;
1412 EXPORT_SYMBOL(edac_mc_add_mc);
1415 * edac_mc_add_mc: Insert the 'mci' structure into the mci global list
1416 * @mci: pointer to the mci structure to be added to the list
1418 * Return:
1419 * 0 Success
1420 * !0 Failure
1423 /* FIXME - should a warning be printed if no error detection? correction? */
1424 int edac_mc_add_mc(struct mem_ctl_info *mci)
1426 int rc = 1;
1428 debugf0("MC: " __FILE__ ": %s()\n", __func__);
1429 #ifdef CONFIG_EDAC_DEBUG
1430 if (edac_debug_level >= 3)
1431 edac_mc_dump_mci(mci);
1432 if (edac_debug_level >= 4) {
1433 int i;
1435 for (i = 0; i < mci->nr_csrows; i++) {
1436 int j;
1437 edac_mc_dump_csrow(&mci->csrows[i]);
1438 for (j = 0; j < mci->csrows[i].nr_channels; j++)
1439 edac_mc_dump_channel(&mci->csrows[i].
1440 channels[j]);
1443 #endif
1444 down(&mem_ctls_mutex);
1446 if (add_mc_to_global_list(mci))
1447 goto finish;
1449 /* set load time so that error rate can be tracked */
1450 mci->start_time = jiffies;
1452 if (edac_create_sysfs_mci_device(mci)) {
1453 printk(KERN_WARNING
1454 "EDAC MC%d: failed to create sysfs device\n",
1455 mci->mc_idx);
1456 /* FIXME - should there be an error code and unwind? */
1457 goto finish;
1460 /* Report action taken */
1461 printk(KERN_INFO
1462 "EDAC MC%d: Giving out device to %s %s: PCI %s\n",
1463 mci->mc_idx, mci->mod_name, mci->ctl_name,
1464 pci_name(mci->pdev));
1467 rc = 0;
1469 finish:
1470 up(&mem_ctls_mutex);
1471 return rc;
1476 static void complete_mc_list_del (struct rcu_head *head)
1478 struct mem_ctl_info *mci;
1480 mci = container_of(head, struct mem_ctl_info, rcu);
1481 INIT_LIST_HEAD(&mci->link);
1482 complete(&mci->complete);
1485 static void del_mc_from_global_list (struct mem_ctl_info *mci)
1487 list_del_rcu(&mci->link);
1488 init_completion(&mci->complete);
1489 call_rcu(&mci->rcu, complete_mc_list_del);
1490 wait_for_completion(&mci->complete);
1493 EXPORT_SYMBOL(edac_mc_del_mc);
1496 * edac_mc_del_mc: Remove the specified mci structure from global list
1497 * @mci: Pointer to struct mem_ctl_info structure
1499 * Returns:
1500 * 0 Success
1501 * 1 Failure
1503 int edac_mc_del_mc(struct mem_ctl_info *mci)
1505 int rc = 1;
1507 debugf0("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
1508 down(&mem_ctls_mutex);
1509 del_mc_from_global_list(mci);
1510 printk(KERN_INFO
1511 "EDAC MC%d: Removed device %d for %s %s: PCI %s\n",
1512 mci->mc_idx, mci->mc_idx, mci->mod_name, mci->ctl_name,
1513 pci_name(mci->pdev));
1514 rc = 0;
1515 up(&mem_ctls_mutex);
1517 return rc;
1521 EXPORT_SYMBOL(edac_mc_scrub_block);
1523 void edac_mc_scrub_block(unsigned long page, unsigned long offset,
1524 u32 size)
1526 struct page *pg;
1527 void *virt_addr;
1528 unsigned long flags = 0;
1530 debugf3("MC: " __FILE__ ": %s()\n", __func__);
1532 /* ECC error page was not in our memory. Ignore it. */
1533 if(!pfn_valid(page))
1534 return;
1536 /* Find the actual page structure then map it and fix */
1537 pg = pfn_to_page(page);
1539 if (PageHighMem(pg))
1540 local_irq_save(flags);
1542 virt_addr = kmap_atomic(pg, KM_BOUNCE_READ);
1544 /* Perform architecture specific atomic scrub operation */
1545 atomic_scrub(virt_addr + offset, size);
1547 /* Unmap and complete */
1548 kunmap_atomic(virt_addr, KM_BOUNCE_READ);
1550 if (PageHighMem(pg))
1551 local_irq_restore(flags);
1555 /* FIXME - should return -1 */
1556 EXPORT_SYMBOL(edac_mc_find_csrow_by_page);
1558 int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci,
1559 unsigned long page)
1561 struct csrow_info *csrows = mci->csrows;
1562 int row, i;
1564 debugf1("MC%d: " __FILE__ ": %s(): 0x%lx\n", mci->mc_idx, __func__,
1565 page);
1566 row = -1;
1568 for (i = 0; i < mci->nr_csrows; i++) {
1569 struct csrow_info *csrow = &csrows[i];
1571 if (csrow->nr_pages == 0)
1572 continue;
1574 debugf3("MC%d: " __FILE__
1575 ": %s(): first(0x%lx) page(0x%lx)"
1576 " last(0x%lx) mask(0x%lx)\n", mci->mc_idx,
1577 __func__, csrow->first_page, page,
1578 csrow->last_page, csrow->page_mask);
1580 if ((page >= csrow->first_page) &&
1581 (page <= csrow->last_page) &&
1582 ((page & csrow->page_mask) ==
1583 (csrow->first_page & csrow->page_mask))) {
1584 row = i;
1585 break;
1589 if (row == -1)
1590 printk(KERN_ERR
1591 "EDAC MC%d: could not look up page error address %lx\n",
1592 mci->mc_idx, (unsigned long) page);
1594 return row;
1598 EXPORT_SYMBOL(edac_mc_handle_ce);
1600 /* FIXME - setable log (warning/emerg) levels */
1601 /* FIXME - integrate with evlog: http://evlog.sourceforge.net/ */
1602 void edac_mc_handle_ce(struct mem_ctl_info *mci,
1603 unsigned long page_frame_number,
1604 unsigned long offset_in_page,
1605 unsigned long syndrome, int row, int channel,
1606 const char *msg)
1608 unsigned long remapped_page;
1610 debugf3("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
1612 /* FIXME - maybe make panic on INTERNAL ERROR an option */
1613 if (row >= mci->nr_csrows || row < 0) {
1614 /* something is wrong */
1615 printk(KERN_ERR
1616 "EDAC MC%d: INTERNAL ERROR: row out of range (%d >= %d)\n",
1617 mci->mc_idx, row, mci->nr_csrows);
1618 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1619 return;
1621 if (channel >= mci->csrows[row].nr_channels || channel < 0) {
1622 /* something is wrong */
1623 printk(KERN_ERR
1624 "EDAC MC%d: INTERNAL ERROR: channel out of range "
1625 "(%d >= %d)\n",
1626 mci->mc_idx, channel, mci->csrows[row].nr_channels);
1627 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1628 return;
1631 if (log_ce)
1632 /* FIXME - put in DIMM location */
1633 printk(KERN_WARNING
1634 "EDAC MC%d: CE page 0x%lx, offset 0x%lx,"
1635 " grain %d, syndrome 0x%lx, row %d, channel %d,"
1636 " label \"%s\": %s\n", mci->mc_idx,
1637 page_frame_number, offset_in_page,
1638 mci->csrows[row].grain, syndrome, row, channel,
1639 mci->csrows[row].channels[channel].label, msg);
1641 mci->ce_count++;
1642 mci->csrows[row].ce_count++;
1643 mci->csrows[row].channels[channel].ce_count++;
1645 if (mci->scrub_mode & SCRUB_SW_SRC) {
1647 * Some MC's can remap memory so that it is still available
1648 * at a different address when PCI devices map into memory.
1649 * MC's that can't do this lose the memory where PCI devices
1650 * are mapped. This mapping is MC dependant and so we call
1651 * back into the MC driver for it to map the MC page to
1652 * a physical (CPU) page which can then be mapped to a virtual
1653 * page - which can then be scrubbed.
1655 remapped_page = mci->ctl_page_to_phys ?
1656 mci->ctl_page_to_phys(mci, page_frame_number) :
1657 page_frame_number;
1659 edac_mc_scrub_block(remapped_page, offset_in_page,
1660 mci->csrows[row].grain);
1665 EXPORT_SYMBOL(edac_mc_handle_ce_no_info);
1667 void edac_mc_handle_ce_no_info(struct mem_ctl_info *mci,
1668 const char *msg)
1670 if (log_ce)
1671 printk(KERN_WARNING
1672 "EDAC MC%d: CE - no information available: %s\n",
1673 mci->mc_idx, msg);
1674 mci->ce_noinfo_count++;
1675 mci->ce_count++;
1679 EXPORT_SYMBOL(edac_mc_handle_ue);
1681 void edac_mc_handle_ue(struct mem_ctl_info *mci,
1682 unsigned long page_frame_number,
1683 unsigned long offset_in_page, int row,
1684 const char *msg)
1686 int len = EDAC_MC_LABEL_LEN * 4;
1687 char labels[len + 1];
1688 char *pos = labels;
1689 int chan;
1690 int chars;
1692 debugf3("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
1694 /* FIXME - maybe make panic on INTERNAL ERROR an option */
1695 if (row >= mci->nr_csrows || row < 0) {
1696 /* something is wrong */
1697 printk(KERN_ERR
1698 "EDAC MC%d: INTERNAL ERROR: row out of range (%d >= %d)\n",
1699 mci->mc_idx, row, mci->nr_csrows);
1700 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
1701 return;
1704 chars = snprintf(pos, len + 1, "%s",
1705 mci->csrows[row].channels[0].label);
1706 len -= chars;
1707 pos += chars;
1708 for (chan = 1; (chan < mci->csrows[row].nr_channels) && (len > 0);
1709 chan++) {
1710 chars = snprintf(pos, len + 1, ":%s",
1711 mci->csrows[row].channels[chan].label);
1712 len -= chars;
1713 pos += chars;
1716 if (log_ue)
1717 printk(KERN_EMERG
1718 "EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, row %d,"
1719 " labels \"%s\": %s\n", mci->mc_idx,
1720 page_frame_number, offset_in_page,
1721 mci->csrows[row].grain, row, labels, msg);
1723 if (panic_on_ue)
1724 panic
1725 ("EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, row %d,"
1726 " labels \"%s\": %s\n", mci->mc_idx,
1727 page_frame_number, offset_in_page,
1728 mci->csrows[row].grain, row, labels, msg);
1730 mci->ue_count++;
1731 mci->csrows[row].ue_count++;
1735 EXPORT_SYMBOL(edac_mc_handle_ue_no_info);
1737 void edac_mc_handle_ue_no_info(struct mem_ctl_info *mci,
1738 const char *msg)
1740 if (panic_on_ue)
1741 panic("EDAC MC%d: Uncorrected Error", mci->mc_idx);
1743 if (log_ue)
1744 printk(KERN_WARNING
1745 "EDAC MC%d: UE - no information available: %s\n",
1746 mci->mc_idx, msg);
1747 mci->ue_noinfo_count++;
1748 mci->ue_count++;
1752 #ifdef CONFIG_PCI
1754 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
1756 int where;
1757 u16 status;
1759 where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
1760 pci_read_config_word(dev, where, &status);
1762 /* If we get back 0xFFFF then we must suspect that the card has been pulled but
1763 the Linux PCI layer has not yet finished cleaning up. We don't want to report
1764 on such devices */
1766 if (status == 0xFFFF) {
1767 u32 sanity;
1768 pci_read_config_dword(dev, 0, &sanity);
1769 if (sanity == 0xFFFFFFFF)
1770 return 0;
1772 status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
1773 PCI_STATUS_PARITY;
1775 if (status)
1776 /* reset only the bits we are interested in */
1777 pci_write_config_word(dev, where, status);
1779 return status;
1782 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
1784 /* Clear any PCI parity errors logged by this device. */
1785 static void edac_pci_dev_parity_clear( struct pci_dev *dev )
1787 u8 header_type;
1789 get_pci_parity_status(dev, 0);
1791 /* read the device TYPE, looking for bridges */
1792 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
1794 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
1795 get_pci_parity_status(dev, 1);
1799 * PCI Parity polling
1802 static void edac_pci_dev_parity_test(struct pci_dev *dev)
1804 u16 status;
1805 u8 header_type;
1807 /* read the STATUS register on this device
1809 status = get_pci_parity_status(dev, 0);
1811 debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
1813 /* check the status reg for errors */
1814 if (status) {
1815 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
1816 printk(KERN_CRIT
1817 "EDAC PCI- "
1818 "Signaled System Error on %s\n",
1819 pci_name (dev));
1821 if (status & (PCI_STATUS_PARITY)) {
1822 printk(KERN_CRIT
1823 "EDAC PCI- "
1824 "Master Data Parity Error on %s\n",
1825 pci_name (dev));
1827 atomic_inc(&pci_parity_count);
1830 if (status & (PCI_STATUS_DETECTED_PARITY)) {
1831 printk(KERN_CRIT
1832 "EDAC PCI- "
1833 "Detected Parity Error on %s\n",
1834 pci_name (dev));
1836 atomic_inc(&pci_parity_count);
1840 /* read the device TYPE, looking for bridges */
1841 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
1843 debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
1845 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1846 /* On bridges, need to examine secondary status register */
1847 status = get_pci_parity_status(dev, 1);
1849 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
1850 status, dev->dev.bus_id );
1852 /* check the secondary status reg for errors */
1853 if (status) {
1854 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
1855 printk(KERN_CRIT
1856 "EDAC PCI-Bridge- "
1857 "Signaled System Error on %s\n",
1858 pci_name (dev));
1860 if (status & (PCI_STATUS_PARITY)) {
1861 printk(KERN_CRIT
1862 "EDAC PCI-Bridge- "
1863 "Master Data Parity Error on %s\n",
1864 pci_name (dev));
1866 atomic_inc(&pci_parity_count);
1869 if (status & (PCI_STATUS_DETECTED_PARITY)) {
1870 printk(KERN_CRIT
1871 "EDAC PCI-Bridge- "
1872 "Detected Parity Error on %s\n",
1873 pci_name (dev));
1875 atomic_inc(&pci_parity_count);
1882 * check_dev_on_list: Scan for a PCI device on a white/black list
1883 * @list: an EDAC &edac_pci_device_list white/black list pointer
1884 * @free_index: index of next free entry on the list
1885 * @pci_dev: PCI Device pointer
1887 * see if list contains the device.
1889 * Returns: 0 not found
1890 * 1 found on list
1892 static int check_dev_on_list(struct edac_pci_device_list *list, int free_index,
1893 struct pci_dev *dev)
1895 int i;
1896 int rc = 0; /* Assume not found */
1897 unsigned short vendor=dev->vendor;
1898 unsigned short device=dev->device;
1900 /* Scan the list, looking for a vendor/device match
1902 for (i = 0; i < free_index; i++, list++ ) {
1903 if ( (list->vendor == vendor ) &&
1904 (list->device == device )) {
1905 rc = 1;
1906 break;
1910 return rc;
1914 * pci_dev parity list iterator
1915 * Scan the PCI device list for one iteration, looking for SERRORs
1916 * Master Parity ERRORS or Parity ERRORs on primary or secondary devices
1918 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
1920 struct pci_dev *dev=NULL;
1922 /* request for kernel access to the next PCI device, if any,
1923 * and while we are looking at it have its reference count
1924 * bumped until we are done with it
1926 while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
1928 /* if whitelist exists then it has priority, so only scan those
1929 * devices on the whitelist
1931 if (pci_whitelist_count > 0 ) {
1932 if (check_dev_on_list(pci_whitelist,
1933 pci_whitelist_count, dev))
1934 fn(dev);
1935 } else {
1937 * if no whitelist, then check if this devices is
1938 * blacklisted
1940 if (!check_dev_on_list(pci_blacklist,
1941 pci_blacklist_count, dev))
1942 fn(dev);
1947 static void do_pci_parity_check(void)
1949 unsigned long flags;
1950 int before_count;
1952 debugf3("MC: " __FILE__ ": %s()\n", __func__);
1954 if (!check_pci_parity)
1955 return;
1957 before_count = atomic_read(&pci_parity_count);
1959 /* scan all PCI devices looking for a Parity Error on devices and
1960 * bridges
1962 local_irq_save(flags);
1963 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
1964 local_irq_restore(flags);
1966 /* Only if operator has selected panic on PCI Error */
1967 if (panic_on_pci_parity) {
1968 /* If the count is different 'after' from 'before' */
1969 if (before_count != atomic_read(&pci_parity_count))
1970 panic("EDAC: PCI Parity Error");
1975 static inline void clear_pci_parity_errors(void)
1977 /* Clear any PCI bus parity errors that devices initially have logged
1978 * in their registers.
1980 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
1984 #else /* CONFIG_PCI */
1987 static inline void do_pci_parity_check(void)
1989 /* no-op */
1993 static inline void clear_pci_parity_errors(void)
1995 /* no-op */
1999 #endif /* CONFIG_PCI */
2002 * Iterate over all MC instances and check for ECC, et al, errors
2004 static inline void check_mc_devices (void)
2006 unsigned long flags;
2007 struct list_head *item;
2008 struct mem_ctl_info *mci;
2010 debugf3("MC: " __FILE__ ": %s()\n", __func__);
2012 /* during poll, have interrupts off */
2013 local_irq_save(flags);
2015 list_for_each(item, &mc_devices) {
2016 mci = list_entry(item, struct mem_ctl_info, link);
2018 if (mci->edac_check != NULL)
2019 mci->edac_check(mci);
2022 local_irq_restore(flags);
2027 * Check MC status every poll_msec.
2028 * Check PCI status every poll_msec as well.
2030 * This where the work gets done for edac.
2032 * SMP safe, doesn't use NMI, and auto-rate-limits.
2034 static void do_edac_check(void)
2037 debugf3("MC: " __FILE__ ": %s()\n", __func__);
2039 check_mc_devices();
2041 do_pci_parity_check();
2046 * EDAC thread state information
2048 struct bs_thread_info
2050 struct task_struct *task;
2051 struct completion *event;
2052 char *name;
2053 void (*run)(void);
2056 static struct bs_thread_info bs_thread;
2059 * edac_kernel_thread
2060 * This the kernel thread that processes edac operations
2061 * in a normal thread environment
2063 static int edac_kernel_thread(void *arg)
2065 struct bs_thread_info *thread = (struct bs_thread_info *) arg;
2067 /* detach thread */
2068 daemonize(thread->name);
2070 current->exit_signal = SIGCHLD;
2071 allow_signal(SIGKILL);
2072 thread->task = current;
2074 /* indicate to starting task we have started */
2075 complete(thread->event);
2077 /* loop forever, until we are told to stop */
2078 while(thread->run != NULL) {
2079 void (*run)(void);
2081 /* call the function to check the memory controllers */
2082 run = thread->run;
2083 if (run)
2084 run();
2086 if (signal_pending(current))
2087 flush_signals(current);
2089 /* ensure we are interruptable */
2090 set_current_state(TASK_INTERRUPTIBLE);
2092 /* goto sleep for the interval */
2093 schedule_timeout((HZ * poll_msec) / 1000);
2094 try_to_freeze();
2097 /* notify waiter that we are exiting */
2098 complete(thread->event);
2100 return 0;
2104 * edac_mc_init
2105 * module initialization entry point
2107 static int __init edac_mc_init(void)
2109 int ret;
2110 struct completion event;
2112 printk(KERN_INFO "MC: " __FILE__ " version " EDAC_MC_VERSION "\n");
2115 * Harvest and clear any boot/initialization PCI parity errors
2117 * FIXME: This only clears errors logged by devices present at time of
2118 * module initialization. We should also do an initial clear
2119 * of each newly hotplugged device.
2121 clear_pci_parity_errors();
2123 /* perform check for first time to harvest boot leftovers */
2124 do_edac_check();
2126 /* Create the MC sysfs entires */
2127 if (edac_sysfs_memctrl_setup()) {
2128 printk(KERN_ERR "EDAC MC: Error initializing sysfs code\n");
2129 return -ENODEV;
2132 /* Create the PCI parity sysfs entries */
2133 if (edac_sysfs_pci_setup()) {
2134 edac_sysfs_memctrl_teardown();
2135 printk(KERN_ERR "EDAC PCI: Error initializing sysfs code\n");
2136 return -ENODEV;
2139 /* Create our kernel thread */
2140 init_completion(&event);
2141 bs_thread.event = &event;
2142 bs_thread.name = "kedac";
2143 bs_thread.run = do_edac_check;
2145 /* create our kernel thread */
2146 ret = kernel_thread(edac_kernel_thread, &bs_thread, CLONE_KERNEL);
2147 if (ret < 0) {
2148 /* remove the sysfs entries */
2149 edac_sysfs_memctrl_teardown();
2150 edac_sysfs_pci_teardown();
2151 return -ENOMEM;
2154 /* wait for our kernel theard ack that it is up and running */
2155 wait_for_completion(&event);
2157 return 0;
2162 * edac_mc_exit()
2163 * module exit/termination functioni
2165 static void __exit edac_mc_exit(void)
2167 struct completion event;
2169 debugf0("MC: " __FILE__ ": %s()\n", __func__);
2171 init_completion(&event);
2172 bs_thread.event = &event;
2174 /* As soon as ->run is set to NULL, the task could disappear,
2175 * so we need to hold tasklist_lock until we have sent the signal
2177 read_lock(&tasklist_lock);
2178 bs_thread.run = NULL;
2179 send_sig(SIGKILL, bs_thread.task, 1);
2180 read_unlock(&tasklist_lock);
2181 wait_for_completion(&event);
2183 /* tear down the sysfs device */
2184 edac_sysfs_memctrl_teardown();
2185 edac_sysfs_pci_teardown();
2191 module_init(edac_mc_init);
2192 module_exit(edac_mc_exit);
2194 MODULE_LICENSE("GPL");
2195 MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh et al\n"
2196 "Based on.work by Dan Hollis et al");
2197 MODULE_DESCRIPTION("Core library routines for MC reporting");
2199 module_param(panic_on_ue, int, 0644);
2200 MODULE_PARM_DESC(panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
2201 module_param(check_pci_parity, int, 0644);
2202 MODULE_PARM_DESC(check_pci_parity, "Check for PCI bus parity errors: 0=off 1=on");
2203 module_param(panic_on_pci_parity, int, 0644);
2204 MODULE_PARM_DESC(panic_on_pci_parity, "Panic on PCI Bus Parity error: 0=off 1=on");
2205 module_param(log_ue, int, 0644);
2206 MODULE_PARM_DESC(log_ue, "Log uncorrectable error to console: 0=off 1=on");
2207 module_param(log_ce, int, 0644);
2208 MODULE_PARM_DESC(log_ce, "Log correctable error to console: 0=off 1=on");
2209 module_param(poll_msec, int, 0644);
2210 MODULE_PARM_DESC(poll_msec, "Polling period in milliseconds");
2211 #ifdef CONFIG_EDAC_DEBUG
2212 module_param(edac_debug_level, int, 0644);
2213 MODULE_PARM_DESC(edac_debug_level, "Debug level");
2214 #endif