[PATCH] TCP: Fix sorting of SACK blocks.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / edac / edac_mc.c
blob75e9e38330ff4d4de4785ba116c7c2cd20fb6f4d
1 /*
2 * edac_mc kernel module
3 * (C) 2005, 2006 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
15 #include <linux/module.h>
16 #include <linux/proc_fs.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/sysctl.h>
22 #include <linux/highmem.h>
23 #include <linux/timer.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/spinlock.h>
27 #include <linux/list.h>
28 #include <linux/sysdev.h>
29 #include <linux/ctype.h>
30 #include <linux/kthread.h>
31 #include <asm/uaccess.h>
32 #include <asm/page.h>
33 #include <asm/edac.h>
34 #include "edac_mc.h"
36 #define EDAC_MC_VERSION "Ver: 2.0.1 " __DATE__
39 #ifdef CONFIG_EDAC_DEBUG
40 /* Values of 0 to 4 will generate output */
41 int edac_debug_level = 1;
42 EXPORT_SYMBOL_GPL(edac_debug_level);
43 #endif
45 /* EDAC Controls, setable by module parameter, and sysfs */
46 static int log_ue = 1;
47 static int log_ce = 1;
48 static int panic_on_ue;
49 static int poll_msec = 1000;
51 /* lock to memory controller's control array */
52 static DECLARE_MUTEX(mem_ctls_mutex);
53 static struct list_head mc_devices = LIST_HEAD_INIT(mc_devices);
55 static struct task_struct *edac_thread;
57 #ifdef CONFIG_PCI
58 static int check_pci_parity = 0; /* default YES check PCI parity */
59 static int panic_on_pci_parity; /* default no panic on PCI Parity */
60 static atomic_t pci_parity_count = ATOMIC_INIT(0);
62 static struct kobject edac_pci_kobj; /* /sys/devices/system/edac/pci */
63 static struct completion edac_pci_kobj_complete;
64 #endif /* CONFIG_PCI */
66 /* START sysfs data and methods */
69 static const char *mem_types[] = {
70 [MEM_EMPTY] = "Empty",
71 [MEM_RESERVED] = "Reserved",
72 [MEM_UNKNOWN] = "Unknown",
73 [MEM_FPM] = "FPM",
74 [MEM_EDO] = "EDO",
75 [MEM_BEDO] = "BEDO",
76 [MEM_SDR] = "Unbuffered-SDR",
77 [MEM_RDR] = "Registered-SDR",
78 [MEM_DDR] = "Unbuffered-DDR",
79 [MEM_RDDR] = "Registered-DDR",
80 [MEM_RMBS] = "RMBS"
83 static const char *dev_types[] = {
84 [DEV_UNKNOWN] = "Unknown",
85 [DEV_X1] = "x1",
86 [DEV_X2] = "x2",
87 [DEV_X4] = "x4",
88 [DEV_X8] = "x8",
89 [DEV_X16] = "x16",
90 [DEV_X32] = "x32",
91 [DEV_X64] = "x64"
94 static const char *edac_caps[] = {
95 [EDAC_UNKNOWN] = "Unknown",
96 [EDAC_NONE] = "None",
97 [EDAC_RESERVED] = "Reserved",
98 [EDAC_PARITY] = "PARITY",
99 [EDAC_EC] = "EC",
100 [EDAC_SECDED] = "SECDED",
101 [EDAC_S2ECD2ED] = "S2ECD2ED",
102 [EDAC_S4ECD4ED] = "S4ECD4ED",
103 [EDAC_S8ECD8ED] = "S8ECD8ED",
104 [EDAC_S16ECD16ED] = "S16ECD16ED"
107 /* sysfs object: /sys/devices/system/edac */
108 static struct sysdev_class edac_class = {
109 set_kset_name("edac"),
112 /* sysfs object:
113 * /sys/devices/system/edac/mc
115 static struct kobject edac_memctrl_kobj;
117 /* We use these to wait for the reference counts on edac_memctrl_kobj and
118 * edac_pci_kobj to reach 0.
120 static struct completion edac_memctrl_kobj_complete;
123 * /sys/devices/system/edac/mc;
124 * data structures and methods
126 static ssize_t memctrl_int_show(void *ptr, char *buffer)
128 int *value = (int*) ptr;
129 return sprintf(buffer, "%u\n", *value);
132 static ssize_t memctrl_int_store(void *ptr, const char *buffer, size_t count)
134 int *value = (int*) ptr;
136 if (isdigit(*buffer))
137 *value = simple_strtoul(buffer, NULL, 0);
139 return count;
142 struct memctrl_dev_attribute {
143 struct attribute attr;
144 void *value;
145 ssize_t (*show)(void *,char *);
146 ssize_t (*store)(void *, const char *, size_t);
149 /* Set of show/store abstract level functions for memory control object */
150 static ssize_t memctrl_dev_show(struct kobject *kobj,
151 struct attribute *attr, char *buffer)
153 struct memctrl_dev_attribute *memctrl_dev;
154 memctrl_dev = (struct memctrl_dev_attribute*)attr;
156 if (memctrl_dev->show)
157 return memctrl_dev->show(memctrl_dev->value, buffer);
159 return -EIO;
162 static ssize_t memctrl_dev_store(struct kobject *kobj, struct attribute *attr,
163 const char *buffer, size_t count)
165 struct memctrl_dev_attribute *memctrl_dev;
166 memctrl_dev = (struct memctrl_dev_attribute*)attr;
168 if (memctrl_dev->store)
169 return memctrl_dev->store(memctrl_dev->value, buffer, count);
171 return -EIO;
174 static struct sysfs_ops memctrlfs_ops = {
175 .show = memctrl_dev_show,
176 .store = memctrl_dev_store
179 #define MEMCTRL_ATTR(_name,_mode,_show,_store) \
180 struct memctrl_dev_attribute attr_##_name = { \
181 .attr = {.name = __stringify(_name), .mode = _mode }, \
182 .value = &_name, \
183 .show = _show, \
184 .store = _store, \
187 #define MEMCTRL_STRING_ATTR(_name,_data,_mode,_show,_store) \
188 struct memctrl_dev_attribute attr_##_name = { \
189 .attr = {.name = __stringify(_name), .mode = _mode }, \
190 .value = _data, \
191 .show = _show, \
192 .store = _store, \
195 /* csrow<id> control files */
196 MEMCTRL_ATTR(panic_on_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
197 MEMCTRL_ATTR(log_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
198 MEMCTRL_ATTR(log_ce,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
199 MEMCTRL_ATTR(poll_msec,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
201 /* Base Attributes of the memory ECC object */
202 static struct memctrl_dev_attribute *memctrl_attr[] = {
203 &attr_panic_on_ue,
204 &attr_log_ue,
205 &attr_log_ce,
206 &attr_poll_msec,
207 NULL,
210 /* Main MC kobject release() function */
211 static void edac_memctrl_master_release(struct kobject *kobj)
213 debugf1("%s()\n", __func__);
214 complete(&edac_memctrl_kobj_complete);
217 static struct kobj_type ktype_memctrl = {
218 .release = edac_memctrl_master_release,
219 .sysfs_ops = &memctrlfs_ops,
220 .default_attrs = (struct attribute **) memctrl_attr,
223 /* Initialize the main sysfs entries for edac:
224 * /sys/devices/system/edac
226 * and children
228 * Return: 0 SUCCESS
229 * !0 FAILURE
231 static int edac_sysfs_memctrl_setup(void)
233 int err = 0;
235 debugf1("%s()\n", __func__);
237 /* create the /sys/devices/system/edac directory */
238 err = sysdev_class_register(&edac_class);
240 if (err) {
241 debugf1("%s() error=%d\n", __func__, err);
242 return err;
245 /* Init the MC's kobject */
246 memset(&edac_memctrl_kobj, 0, sizeof (edac_memctrl_kobj));
247 edac_memctrl_kobj.parent = &edac_class.kset.kobj;
248 edac_memctrl_kobj.ktype = &ktype_memctrl;
250 /* generate sysfs "..../edac/mc" */
251 err = kobject_set_name(&edac_memctrl_kobj,"mc");
253 if (err)
254 goto fail;
256 /* FIXME: maybe new sysdev_create_subdir() */
257 err = kobject_register(&edac_memctrl_kobj);
259 if (err) {
260 debugf1("Failed to register '.../edac/mc'\n");
261 goto fail;
264 debugf1("Registered '.../edac/mc' kobject\n");
266 return 0;
268 fail:
269 sysdev_class_unregister(&edac_class);
270 return err;
274 * MC teardown:
275 * the '..../edac/mc' kobject followed by '..../edac' itself
277 static void edac_sysfs_memctrl_teardown(void)
279 debugf0("MC: " __FILE__ ": %s()\n", __func__);
281 /* Unregister the MC's kobject and wait for reference count to reach
282 * 0.
284 init_completion(&edac_memctrl_kobj_complete);
285 kobject_unregister(&edac_memctrl_kobj);
286 wait_for_completion(&edac_memctrl_kobj_complete);
288 /* Unregister the 'edac' object */
289 sysdev_class_unregister(&edac_class);
292 #ifdef CONFIG_PCI
293 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
295 int *value = ptr;
296 return sprintf(buffer,"%d\n",*value);
299 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
301 int *value = ptr;
303 if (isdigit(*buffer))
304 *value = simple_strtoul(buffer,NULL,0);
306 return count;
309 struct edac_pci_dev_attribute {
310 struct attribute attr;
311 void *value;
312 ssize_t (*show)(void *,char *);
313 ssize_t (*store)(void *, const char *,size_t);
316 /* Set of show/store abstract level functions for PCI Parity object */
317 static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
318 char *buffer)
320 struct edac_pci_dev_attribute *edac_pci_dev;
321 edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
323 if (edac_pci_dev->show)
324 return edac_pci_dev->show(edac_pci_dev->value, buffer);
325 return -EIO;
328 static ssize_t edac_pci_dev_store(struct kobject *kobj,
329 struct attribute *attr, const char *buffer, size_t count)
331 struct edac_pci_dev_attribute *edac_pci_dev;
332 edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
334 if (edac_pci_dev->show)
335 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
336 return -EIO;
339 static struct sysfs_ops edac_pci_sysfs_ops = {
340 .show = edac_pci_dev_show,
341 .store = edac_pci_dev_store
344 #define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
345 struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
346 .attr = {.name = __stringify(_name), .mode = _mode }, \
347 .value = &_name, \
348 .show = _show, \
349 .store = _store, \
352 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
353 struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
354 .attr = {.name = __stringify(_name), .mode = _mode }, \
355 .value = _data, \
356 .show = _show, \
357 .store = _store, \
360 /* PCI Parity control files */
361 EDAC_PCI_ATTR(check_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
362 edac_pci_int_store);
363 EDAC_PCI_ATTR(panic_on_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
364 edac_pci_int_store);
365 EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
367 /* Base Attributes of the memory ECC object */
368 static struct edac_pci_dev_attribute *edac_pci_attr[] = {
369 &edac_pci_attr_check_pci_parity,
370 &edac_pci_attr_panic_on_pci_parity,
371 &edac_pci_attr_pci_parity_count,
372 NULL,
375 /* No memory to release */
376 static void edac_pci_release(struct kobject *kobj)
378 debugf1("%s()\n", __func__);
379 complete(&edac_pci_kobj_complete);
382 static struct kobj_type ktype_edac_pci = {
383 .release = edac_pci_release,
384 .sysfs_ops = &edac_pci_sysfs_ops,
385 .default_attrs = (struct attribute **) edac_pci_attr,
389 * edac_sysfs_pci_setup()
392 static int edac_sysfs_pci_setup(void)
394 int err;
396 debugf1("%s()\n", __func__);
398 memset(&edac_pci_kobj, 0, sizeof(edac_pci_kobj));
399 edac_pci_kobj.parent = &edac_class.kset.kobj;
400 edac_pci_kobj.ktype = &ktype_edac_pci;
401 err = kobject_set_name(&edac_pci_kobj, "pci");
403 if (!err) {
404 /* Instanstiate the csrow object */
405 /* FIXME: maybe new sysdev_create_subdir() */
406 err = kobject_register(&edac_pci_kobj);
408 if (err)
409 debugf1("Failed to register '.../edac/pci'\n");
410 else
411 debugf1("Registered '.../edac/pci' kobject\n");
414 return err;
417 static void edac_sysfs_pci_teardown(void)
419 debugf0("%s()\n", __func__);
420 init_completion(&edac_pci_kobj_complete);
421 kobject_unregister(&edac_pci_kobj);
422 wait_for_completion(&edac_pci_kobj_complete);
426 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
428 int where;
429 u16 status;
431 where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
432 pci_read_config_word(dev, where, &status);
434 /* If we get back 0xFFFF then we must suspect that the card has been
435 * pulled but the Linux PCI layer has not yet finished cleaning up.
436 * We don't want to report on such devices
439 if (status == 0xFFFF) {
440 u32 sanity;
442 pci_read_config_dword(dev, 0, &sanity);
444 if (sanity == 0xFFFFFFFF)
445 return 0;
448 status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
449 PCI_STATUS_PARITY;
451 if (status)
452 /* reset only the bits we are interested in */
453 pci_write_config_word(dev, where, status);
455 return status;
458 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
460 /* Clear any PCI parity errors logged by this device. */
461 static void edac_pci_dev_parity_clear(struct pci_dev *dev)
463 u8 header_type;
465 get_pci_parity_status(dev, 0);
467 /* read the device TYPE, looking for bridges */
468 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
470 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
471 get_pci_parity_status(dev, 1);
475 * PCI Parity polling
478 static void edac_pci_dev_parity_test(struct pci_dev *dev)
480 u16 status;
481 u8 header_type;
483 /* read the STATUS register on this device
485 status = get_pci_parity_status(dev, 0);
487 debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
489 /* check the status reg for errors */
490 if (status) {
491 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
492 edac_printk(KERN_CRIT, EDAC_PCI,
493 "Signaled System Error on %s\n",
494 pci_name(dev));
496 if (status & (PCI_STATUS_PARITY)) {
497 edac_printk(KERN_CRIT, EDAC_PCI,
498 "Master Data Parity Error on %s\n",
499 pci_name(dev));
501 atomic_inc(&pci_parity_count);
504 if (status & (PCI_STATUS_DETECTED_PARITY)) {
505 edac_printk(KERN_CRIT, EDAC_PCI,
506 "Detected Parity Error on %s\n",
507 pci_name(dev));
509 atomic_inc(&pci_parity_count);
513 /* read the device TYPE, looking for bridges */
514 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
516 debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
518 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
519 /* On bridges, need to examine secondary status register */
520 status = get_pci_parity_status(dev, 1);
522 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
523 status, dev->dev.bus_id );
525 /* check the secondary status reg for errors */
526 if (status) {
527 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
528 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
529 "Signaled System Error on %s\n",
530 pci_name(dev));
532 if (status & (PCI_STATUS_PARITY)) {
533 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
534 "Master Data Parity Error on "
535 "%s\n", pci_name(dev));
537 atomic_inc(&pci_parity_count);
540 if (status & (PCI_STATUS_DETECTED_PARITY)) {
541 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
542 "Detected Parity Error on %s\n",
543 pci_name(dev));
545 atomic_inc(&pci_parity_count);
552 * pci_dev parity list iterator
553 * Scan the PCI device list for one iteration, looking for SERRORs
554 * Master Parity ERRORS or Parity ERRORs on primary or secondary devices
556 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
558 struct pci_dev *dev = NULL;
560 /* request for kernel access to the next PCI device, if any,
561 * and while we are looking at it have its reference count
562 * bumped until we are done with it
564 while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
565 fn(dev);
569 static void do_pci_parity_check(void)
571 unsigned long flags;
572 int before_count;
574 debugf3("%s()\n", __func__);
576 if (!check_pci_parity)
577 return;
579 before_count = atomic_read(&pci_parity_count);
581 /* scan all PCI devices looking for a Parity Error on devices and
582 * bridges
584 local_irq_save(flags);
585 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
586 local_irq_restore(flags);
588 /* Only if operator has selected panic on PCI Error */
589 if (panic_on_pci_parity) {
590 /* If the count is different 'after' from 'before' */
591 if (before_count != atomic_read(&pci_parity_count))
592 panic("EDAC: PCI Parity Error");
596 static inline void clear_pci_parity_errors(void)
598 /* Clear any PCI bus parity errors that devices initially have logged
599 * in their registers.
601 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
604 #else /* CONFIG_PCI */
606 /* pre-process these away */
607 #define do_pci_parity_check()
608 #define clear_pci_parity_errors()
609 #define edac_sysfs_pci_teardown()
610 #define edac_sysfs_pci_setup() (0)
612 #endif /* CONFIG_PCI */
614 /* EDAC sysfs CSROW data structures and methods
617 /* Set of more default csrow<id> attribute show/store functions */
618 static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data, int private)
620 return sprintf(data,"%u\n", csrow->ue_count);
623 static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data, int private)
625 return sprintf(data,"%u\n", csrow->ce_count);
628 static ssize_t csrow_size_show(struct csrow_info *csrow, char *data, int private)
630 return sprintf(data,"%u\n", PAGES_TO_MiB(csrow->nr_pages));
633 static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data, int private)
635 return sprintf(data,"%s\n", mem_types[csrow->mtype]);
638 static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data, int private)
640 return sprintf(data,"%s\n", dev_types[csrow->dtype]);
643 static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data, int private)
645 return sprintf(data,"%s\n", edac_caps[csrow->edac_mode]);
648 /* show/store functions for DIMM Label attributes */
649 static ssize_t channel_dimm_label_show(struct csrow_info *csrow,
650 char *data, int channel)
652 return snprintf(data, EDAC_MC_LABEL_LEN,"%s",
653 csrow->channels[channel].label);
656 static ssize_t channel_dimm_label_store(struct csrow_info *csrow,
657 const char *data,
658 size_t count,
659 int channel)
661 ssize_t max_size = 0;
663 max_size = min((ssize_t)count,(ssize_t)EDAC_MC_LABEL_LEN-1);
664 strncpy(csrow->channels[channel].label, data, max_size);
665 csrow->channels[channel].label[max_size] = '\0';
667 return max_size;
670 /* show function for dynamic chX_ce_count attribute */
671 static ssize_t channel_ce_count_show(struct csrow_info *csrow,
672 char *data,
673 int channel)
675 return sprintf(data, "%u\n", csrow->channels[channel].ce_count);
678 /* csrow specific attribute structure */
679 struct csrowdev_attribute {
680 struct attribute attr;
681 ssize_t (*show)(struct csrow_info *,char *,int);
682 ssize_t (*store)(struct csrow_info *, const char *,size_t,int);
683 int private;
686 #define to_csrow(k) container_of(k, struct csrow_info, kobj)
687 #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
689 /* Set of show/store higher level functions for default csrow attributes */
690 static ssize_t csrowdev_show(struct kobject *kobj,
691 struct attribute *attr,
692 char *buffer)
694 struct csrow_info *csrow = to_csrow(kobj);
695 struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
697 if (csrowdev_attr->show)
698 return csrowdev_attr->show(csrow,
699 buffer,
700 csrowdev_attr->private);
701 return -EIO;
704 static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
705 const char *buffer, size_t count)
707 struct csrow_info *csrow = to_csrow(kobj);
708 struct csrowdev_attribute * csrowdev_attr = to_csrowdev_attr(attr);
710 if (csrowdev_attr->store)
711 return csrowdev_attr->store(csrow,
712 buffer,
713 count,
714 csrowdev_attr->private);
715 return -EIO;
718 static struct sysfs_ops csrowfs_ops = {
719 .show = csrowdev_show,
720 .store = csrowdev_store
723 #define CSROWDEV_ATTR(_name,_mode,_show,_store,_private) \
724 struct csrowdev_attribute attr_##_name = { \
725 .attr = {.name = __stringify(_name), .mode = _mode }, \
726 .show = _show, \
727 .store = _store, \
728 .private = _private, \
731 /* default cwrow<id>/attribute files */
732 CSROWDEV_ATTR(size_mb,S_IRUGO,csrow_size_show,NULL,0);
733 CSROWDEV_ATTR(dev_type,S_IRUGO,csrow_dev_type_show,NULL,0);
734 CSROWDEV_ATTR(mem_type,S_IRUGO,csrow_mem_type_show,NULL,0);
735 CSROWDEV_ATTR(edac_mode,S_IRUGO,csrow_edac_mode_show,NULL,0);
736 CSROWDEV_ATTR(ue_count,S_IRUGO,csrow_ue_count_show,NULL,0);
737 CSROWDEV_ATTR(ce_count,S_IRUGO,csrow_ce_count_show,NULL,0);
739 /* default attributes of the CSROW<id> object */
740 static struct csrowdev_attribute *default_csrow_attr[] = {
741 &attr_dev_type,
742 &attr_mem_type,
743 &attr_edac_mode,
744 &attr_size_mb,
745 &attr_ue_count,
746 &attr_ce_count,
747 NULL,
751 /* possible dynamic channel DIMM Label attribute files */
752 CSROWDEV_ATTR(ch0_dimm_label,S_IRUGO|S_IWUSR,
753 channel_dimm_label_show,
754 channel_dimm_label_store,
755 0 );
756 CSROWDEV_ATTR(ch1_dimm_label,S_IRUGO|S_IWUSR,
757 channel_dimm_label_show,
758 channel_dimm_label_store,
759 1 );
760 CSROWDEV_ATTR(ch2_dimm_label,S_IRUGO|S_IWUSR,
761 channel_dimm_label_show,
762 channel_dimm_label_store,
763 2 );
764 CSROWDEV_ATTR(ch3_dimm_label,S_IRUGO|S_IWUSR,
765 channel_dimm_label_show,
766 channel_dimm_label_store,
767 3 );
768 CSROWDEV_ATTR(ch4_dimm_label,S_IRUGO|S_IWUSR,
769 channel_dimm_label_show,
770 channel_dimm_label_store,
771 4 );
772 CSROWDEV_ATTR(ch5_dimm_label,S_IRUGO|S_IWUSR,
773 channel_dimm_label_show,
774 channel_dimm_label_store,
775 5 );
777 /* Total possible dynamic DIMM Label attribute file table */
778 static struct csrowdev_attribute *dynamic_csrow_dimm_attr[] = {
779 &attr_ch0_dimm_label,
780 &attr_ch1_dimm_label,
781 &attr_ch2_dimm_label,
782 &attr_ch3_dimm_label,
783 &attr_ch4_dimm_label,
784 &attr_ch5_dimm_label
787 /* possible dynamic channel ce_count attribute files */
788 CSROWDEV_ATTR(ch0_ce_count,S_IRUGO|S_IWUSR,
789 channel_ce_count_show,
790 NULL,
791 0 );
792 CSROWDEV_ATTR(ch1_ce_count,S_IRUGO|S_IWUSR,
793 channel_ce_count_show,
794 NULL,
795 1 );
796 CSROWDEV_ATTR(ch2_ce_count,S_IRUGO|S_IWUSR,
797 channel_ce_count_show,
798 NULL,
799 2 );
800 CSROWDEV_ATTR(ch3_ce_count,S_IRUGO|S_IWUSR,
801 channel_ce_count_show,
802 NULL,
803 3 );
804 CSROWDEV_ATTR(ch4_ce_count,S_IRUGO|S_IWUSR,
805 channel_ce_count_show,
806 NULL,
807 4 );
808 CSROWDEV_ATTR(ch5_ce_count,S_IRUGO|S_IWUSR,
809 channel_ce_count_show,
810 NULL,
811 5 );
813 /* Total possible dynamic ce_count attribute file table */
814 static struct csrowdev_attribute *dynamic_csrow_ce_count_attr[] = {
815 &attr_ch0_ce_count,
816 &attr_ch1_ce_count,
817 &attr_ch2_ce_count,
818 &attr_ch3_ce_count,
819 &attr_ch4_ce_count,
820 &attr_ch5_ce_count
824 #define EDAC_NR_CHANNELS 6
826 /* Create dynamic CHANNEL files, indexed by 'chan', under specifed CSROW */
827 static int edac_create_channel_files(struct kobject *kobj, int chan)
829 int err=-ENODEV;
831 if (chan >= EDAC_NR_CHANNELS)
832 return err;
834 /* create the DIMM label attribute file */
835 err = sysfs_create_file(kobj,
836 (struct attribute *) dynamic_csrow_dimm_attr[chan]);
838 if (!err) {
839 /* create the CE Count attribute file */
840 err = sysfs_create_file(kobj,
841 (struct attribute *) dynamic_csrow_ce_count_attr[chan]);
842 } else {
843 debugf1("%s() dimm labels and ce_count files created", __func__);
846 return err;
849 /* No memory to release for this kobj */
850 static void edac_csrow_instance_release(struct kobject *kobj)
852 struct csrow_info *cs;
854 cs = container_of(kobj, struct csrow_info, kobj);
855 complete(&cs->kobj_complete);
858 /* the kobj_type instance for a CSROW */
859 static struct kobj_type ktype_csrow = {
860 .release = edac_csrow_instance_release,
861 .sysfs_ops = &csrowfs_ops,
862 .default_attrs = (struct attribute **) default_csrow_attr,
865 /* Create a CSROW object under specifed edac_mc_device */
866 static int edac_create_csrow_object(
867 struct kobject *edac_mci_kobj,
868 struct csrow_info *csrow,
869 int index)
871 int err = 0;
872 int chan;
874 memset(&csrow->kobj, 0, sizeof(csrow->kobj));
876 /* generate ..../edac/mc/mc<id>/csrow<index> */
878 csrow->kobj.parent = edac_mci_kobj;
879 csrow->kobj.ktype = &ktype_csrow;
881 /* name this instance of csrow<id> */
882 err = kobject_set_name(&csrow->kobj,"csrow%d",index);
883 if (err)
884 goto error_exit;
886 /* Instanstiate the csrow object */
887 err = kobject_register(&csrow->kobj);
888 if (!err) {
889 /* Create the dyanmic attribute files on this csrow,
890 * namely, the DIMM labels and the channel ce_count
892 for (chan = 0; chan < csrow->nr_channels; chan++) {
893 err = edac_create_channel_files(&csrow->kobj,chan);
894 if (err)
895 break;
899 error_exit:
900 return err;
903 /* default sysfs methods and data structures for the main MCI kobject */
905 static ssize_t mci_reset_counters_store(struct mem_ctl_info *mci,
906 const char *data, size_t count)
908 int row, chan;
910 mci->ue_noinfo_count = 0;
911 mci->ce_noinfo_count = 0;
912 mci->ue_count = 0;
913 mci->ce_count = 0;
915 for (row = 0; row < mci->nr_csrows; row++) {
916 struct csrow_info *ri = &mci->csrows[row];
918 ri->ue_count = 0;
919 ri->ce_count = 0;
921 for (chan = 0; chan < ri->nr_channels; chan++)
922 ri->channels[chan].ce_count = 0;
925 mci->start_time = jiffies;
926 return count;
929 /* default attribute files for the MCI object */
930 static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
932 return sprintf(data,"%d\n", mci->ue_count);
935 static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
937 return sprintf(data,"%d\n", mci->ce_count);
940 static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
942 return sprintf(data,"%d\n", mci->ce_noinfo_count);
945 static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
947 return sprintf(data,"%d\n", mci->ue_noinfo_count);
950 static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
952 return sprintf(data,"%ld\n", (jiffies - mci->start_time) / HZ);
955 static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
957 return sprintf(data,"%s\n", mci->ctl_name);
960 static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
962 int total_pages, csrow_idx;
964 for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
965 csrow_idx++) {
966 struct csrow_info *csrow = &mci->csrows[csrow_idx];
968 if (!csrow->nr_pages)
969 continue;
971 total_pages += csrow->nr_pages;
974 return sprintf(data,"%u\n", PAGES_TO_MiB(total_pages));
977 struct mcidev_attribute {
978 struct attribute attr;
979 ssize_t (*show)(struct mem_ctl_info *,char *);
980 ssize_t (*store)(struct mem_ctl_info *, const char *,size_t);
983 #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
984 #define to_mcidev_attr(a) container_of(a, struct mcidev_attribute, attr)
986 /* MCI show/store functions for top most object */
987 static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
988 char *buffer)
990 struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
991 struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
993 if (mcidev_attr->show)
994 return mcidev_attr->show(mem_ctl_info, buffer);
996 return -EIO;
999 static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
1000 const char *buffer, size_t count)
1002 struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
1003 struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
1005 if (mcidev_attr->store)
1006 return mcidev_attr->store(mem_ctl_info, buffer, count);
1008 return -EIO;
1011 static struct sysfs_ops mci_ops = {
1012 .show = mcidev_show,
1013 .store = mcidev_store
1016 #define MCIDEV_ATTR(_name,_mode,_show,_store) \
1017 struct mcidev_attribute mci_attr_##_name = { \
1018 .attr = {.name = __stringify(_name), .mode = _mode }, \
1019 .show = _show, \
1020 .store = _store, \
1023 /* default Control file */
1024 MCIDEV_ATTR(reset_counters,S_IWUSR,NULL,mci_reset_counters_store);
1026 /* default Attribute files */
1027 MCIDEV_ATTR(mc_name,S_IRUGO,mci_ctl_name_show,NULL);
1028 MCIDEV_ATTR(size_mb,S_IRUGO,mci_size_mb_show,NULL);
1029 MCIDEV_ATTR(seconds_since_reset,S_IRUGO,mci_seconds_show,NULL);
1030 MCIDEV_ATTR(ue_noinfo_count,S_IRUGO,mci_ue_noinfo_show,NULL);
1031 MCIDEV_ATTR(ce_noinfo_count,S_IRUGO,mci_ce_noinfo_show,NULL);
1032 MCIDEV_ATTR(ue_count,S_IRUGO,mci_ue_count_show,NULL);
1033 MCIDEV_ATTR(ce_count,S_IRUGO,mci_ce_count_show,NULL);
1035 static struct mcidev_attribute *mci_attr[] = {
1036 &mci_attr_reset_counters,
1037 &mci_attr_mc_name,
1038 &mci_attr_size_mb,
1039 &mci_attr_seconds_since_reset,
1040 &mci_attr_ue_noinfo_count,
1041 &mci_attr_ce_noinfo_count,
1042 &mci_attr_ue_count,
1043 &mci_attr_ce_count,
1044 NULL
1048 * Release of a MC controlling instance
1050 static void edac_mci_instance_release(struct kobject *kobj)
1052 struct mem_ctl_info *mci;
1054 mci = to_mci(kobj);
1055 debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
1056 complete(&mci->kobj_complete);
1059 static struct kobj_type ktype_mci = {
1060 .release = edac_mci_instance_release,
1061 .sysfs_ops = &mci_ops,
1062 .default_attrs = (struct attribute **) mci_attr,
1066 #define EDAC_DEVICE_SYMLINK "device"
1069 * Create a new Memory Controller kobject instance,
1070 * mc<id> under the 'mc' directory
1072 * Return:
1073 * 0 Success
1074 * !0 Failure
1076 static int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
1078 int i;
1079 int err;
1080 struct csrow_info *csrow;
1081 struct kobject *edac_mci_kobj=&mci->edac_mci_kobj;
1083 debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
1084 memset(edac_mci_kobj, 0, sizeof(*edac_mci_kobj));
1086 /* set the name of the mc<id> object */
1087 err = kobject_set_name(edac_mci_kobj,"mc%d",mci->mc_idx);
1088 if (err)
1089 return err;
1091 /* link to our parent the '..../edac/mc' object */
1092 edac_mci_kobj->parent = &edac_memctrl_kobj;
1093 edac_mci_kobj->ktype = &ktype_mci;
1095 /* register the mc<id> kobject */
1096 err = kobject_register(edac_mci_kobj);
1097 if (err)
1098 return err;
1100 /* create a symlink for the device */
1101 err = sysfs_create_link(edac_mci_kobj, &mci->dev->kobj,
1102 EDAC_DEVICE_SYMLINK);
1103 if (err)
1104 goto fail0;
1106 /* Make directories for each CSROW object
1107 * under the mc<id> kobject
1109 for (i = 0; i < mci->nr_csrows; i++) {
1110 csrow = &mci->csrows[i];
1112 /* Only expose populated CSROWs */
1113 if (csrow->nr_pages > 0) {
1114 err = edac_create_csrow_object(edac_mci_kobj,csrow,i);
1115 if (err)
1116 goto fail1;
1120 return 0;
1122 /* CSROW error: backout what has already been registered, */
1123 fail1:
1124 for ( i--; i >= 0; i--) {
1125 if (csrow->nr_pages > 0) {
1126 init_completion(&csrow->kobj_complete);
1127 kobject_unregister(&mci->csrows[i].kobj);
1128 wait_for_completion(&csrow->kobj_complete);
1132 fail0:
1133 init_completion(&mci->kobj_complete);
1134 kobject_unregister(edac_mci_kobj);
1135 wait_for_completion(&mci->kobj_complete);
1136 return err;
1140 * remove a Memory Controller instance
1142 static void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
1144 int i;
1146 debugf0("%s()\n", __func__);
1148 /* remove all csrow kobjects */
1149 for (i = 0; i < mci->nr_csrows; i++) {
1150 if (mci->csrows[i].nr_pages > 0) {
1151 init_completion(&mci->csrows[i].kobj_complete);
1152 kobject_unregister(&mci->csrows[i].kobj);
1153 wait_for_completion(&mci->csrows[i].kobj_complete);
1157 sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
1158 init_completion(&mci->kobj_complete);
1159 kobject_unregister(&mci->edac_mci_kobj);
1160 wait_for_completion(&mci->kobj_complete);
1163 /* END OF sysfs data and methods */
1165 #ifdef CONFIG_EDAC_DEBUG
1167 void edac_mc_dump_channel(struct channel_info *chan)
1169 debugf4("\tchannel = %p\n", chan);
1170 debugf4("\tchannel->chan_idx = %d\n", chan->chan_idx);
1171 debugf4("\tchannel->ce_count = %d\n", chan->ce_count);
1172 debugf4("\tchannel->label = '%s'\n", chan->label);
1173 debugf4("\tchannel->csrow = %p\n\n", chan->csrow);
1175 EXPORT_SYMBOL_GPL(edac_mc_dump_channel);
1177 void edac_mc_dump_csrow(struct csrow_info *csrow)
1179 debugf4("\tcsrow = %p\n", csrow);
1180 debugf4("\tcsrow->csrow_idx = %d\n", csrow->csrow_idx);
1181 debugf4("\tcsrow->first_page = 0x%lx\n",
1182 csrow->first_page);
1183 debugf4("\tcsrow->last_page = 0x%lx\n", csrow->last_page);
1184 debugf4("\tcsrow->page_mask = 0x%lx\n", csrow->page_mask);
1185 debugf4("\tcsrow->nr_pages = 0x%x\n", csrow->nr_pages);
1186 debugf4("\tcsrow->nr_channels = %d\n",
1187 csrow->nr_channels);
1188 debugf4("\tcsrow->channels = %p\n", csrow->channels);
1189 debugf4("\tcsrow->mci = %p\n\n", csrow->mci);
1191 EXPORT_SYMBOL_GPL(edac_mc_dump_csrow);
1193 void edac_mc_dump_mci(struct mem_ctl_info *mci)
1195 debugf3("\tmci = %p\n", mci);
1196 debugf3("\tmci->mtype_cap = %lx\n", mci->mtype_cap);
1197 debugf3("\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
1198 debugf3("\tmci->edac_cap = %lx\n", mci->edac_cap);
1199 debugf4("\tmci->edac_check = %p\n", mci->edac_check);
1200 debugf3("\tmci->nr_csrows = %d, csrows = %p\n",
1201 mci->nr_csrows, mci->csrows);
1202 debugf3("\tdev = %p\n", mci->dev);
1203 debugf3("\tmod_name:ctl_name = %s:%s\n",
1204 mci->mod_name, mci->ctl_name);
1205 debugf3("\tpvt_info = %p\n\n", mci->pvt_info);
1207 EXPORT_SYMBOL_GPL(edac_mc_dump_mci);
1209 #endif /* CONFIG_EDAC_DEBUG */
1211 /* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'.
1212 * Adjust 'ptr' so that its alignment is at least as stringent as what the
1213 * compiler would provide for X and return the aligned result.
1215 * If 'size' is a constant, the compiler will optimize this whole function
1216 * down to either a no-op or the addition of a constant to the value of 'ptr'.
1218 static inline char * align_ptr(void *ptr, unsigned size)
1220 unsigned align, r;
1222 /* Here we assume that the alignment of a "long long" is the most
1223 * stringent alignment that the compiler will ever provide by default.
1224 * As far as I know, this is a reasonable assumption.
1226 if (size > sizeof(long))
1227 align = sizeof(long long);
1228 else if (size > sizeof(int))
1229 align = sizeof(long);
1230 else if (size > sizeof(short))
1231 align = sizeof(int);
1232 else if (size > sizeof(char))
1233 align = sizeof(short);
1234 else
1235 return (char *) ptr;
1237 r = size % align;
1239 if (r == 0)
1240 return (char *) ptr;
1242 return (char *) (((unsigned long) ptr) + align - r);
1246 * edac_mc_alloc: Allocate a struct mem_ctl_info structure
1247 * @size_pvt: size of private storage needed
1248 * @nr_csrows: Number of CWROWS needed for this MC
1249 * @nr_chans: Number of channels for the MC
1251 * Everything is kmalloc'ed as one big chunk - more efficient.
1252 * Only can be used if all structures have the same lifetime - otherwise
1253 * you have to allocate and initialize your own structures.
1255 * Use edac_mc_free() to free mc structures allocated by this function.
1257 * Returns:
1258 * NULL allocation failed
1259 * struct mem_ctl_info pointer
1261 struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
1262 unsigned nr_chans)
1264 struct mem_ctl_info *mci;
1265 struct csrow_info *csi, *csrow;
1266 struct channel_info *chi, *chp, *chan;
1267 void *pvt;
1268 unsigned size;
1269 int row, chn;
1271 /* Figure out the offsets of the various items from the start of an mc
1272 * structure. We want the alignment of each item to be at least as
1273 * stringent as what the compiler would provide if we could simply
1274 * hardcode everything into a single struct.
1276 mci = (struct mem_ctl_info *) 0;
1277 csi = (struct csrow_info *)align_ptr(&mci[1], sizeof(*csi));
1278 chi = (struct channel_info *)
1279 align_ptr(&csi[nr_csrows], sizeof(*chi));
1280 pvt = align_ptr(&chi[nr_chans * nr_csrows], sz_pvt);
1281 size = ((unsigned long) pvt) + sz_pvt;
1283 if ((mci = kmalloc(size, GFP_KERNEL)) == NULL)
1284 return NULL;
1286 /* Adjust pointers so they point within the memory we just allocated
1287 * rather than an imaginary chunk of memory located at address 0.
1289 csi = (struct csrow_info *) (((char *) mci) + ((unsigned long) csi));
1290 chi = (struct channel_info *) (((char *) mci) + ((unsigned long) chi));
1291 pvt = sz_pvt ? (((char *) mci) + ((unsigned long) pvt)) : NULL;
1293 memset(mci, 0, size); /* clear all fields */
1294 mci->csrows = csi;
1295 mci->pvt_info = pvt;
1296 mci->nr_csrows = nr_csrows;
1298 for (row = 0; row < nr_csrows; row++) {
1299 csrow = &csi[row];
1300 csrow->csrow_idx = row;
1301 csrow->mci = mci;
1302 csrow->nr_channels = nr_chans;
1303 chp = &chi[row * nr_chans];
1304 csrow->channels = chp;
1306 for (chn = 0; chn < nr_chans; chn++) {
1307 chan = &chp[chn];
1308 chan->chan_idx = chn;
1309 chan->csrow = csrow;
1313 return mci;
1315 EXPORT_SYMBOL_GPL(edac_mc_alloc);
1318 * edac_mc_free: Free a previously allocated 'mci' structure
1319 * @mci: pointer to a struct mem_ctl_info structure
1321 void edac_mc_free(struct mem_ctl_info *mci)
1323 kfree(mci);
1325 EXPORT_SYMBOL_GPL(edac_mc_free);
1327 static struct mem_ctl_info *find_mci_by_dev(struct device *dev)
1329 struct mem_ctl_info *mci;
1330 struct list_head *item;
1332 debugf3("%s()\n", __func__);
1334 list_for_each(item, &mc_devices) {
1335 mci = list_entry(item, struct mem_ctl_info, link);
1337 if (mci->dev == dev)
1338 return mci;
1341 return NULL;
1344 /* Return 0 on success, 1 on failure.
1345 * Before calling this function, caller must
1346 * assign a unique value to mci->mc_idx.
1348 static int add_mc_to_global_list (struct mem_ctl_info *mci)
1350 struct list_head *item, *insert_before;
1351 struct mem_ctl_info *p;
1353 insert_before = &mc_devices;
1355 if (unlikely((p = find_mci_by_dev(mci->dev)) != NULL))
1356 goto fail0;
1358 list_for_each(item, &mc_devices) {
1359 p = list_entry(item, struct mem_ctl_info, link);
1361 if (p->mc_idx >= mci->mc_idx) {
1362 if (unlikely(p->mc_idx == mci->mc_idx))
1363 goto fail1;
1365 insert_before = item;
1366 break;
1370 list_add_tail_rcu(&mci->link, insert_before);
1371 return 0;
1373 fail0:
1374 edac_printk(KERN_WARNING, EDAC_MC,
1375 "%s (%s) %s %s already assigned %d\n", p->dev->bus_id,
1376 dev_name(p->dev), p->mod_name, p->ctl_name, p->mc_idx);
1377 return 1;
1379 fail1:
1380 edac_printk(KERN_WARNING, EDAC_MC,
1381 "bug in low-level driver: attempt to assign\n"
1382 " duplicate mc_idx %d in %s()\n", p->mc_idx, __func__);
1383 return 1;
1386 static void complete_mc_list_del(struct rcu_head *head)
1388 struct mem_ctl_info *mci;
1390 mci = container_of(head, struct mem_ctl_info, rcu);
1391 INIT_LIST_HEAD(&mci->link);
1392 complete(&mci->complete);
1395 static void del_mc_from_global_list(struct mem_ctl_info *mci)
1397 list_del_rcu(&mci->link);
1398 init_completion(&mci->complete);
1399 call_rcu(&mci->rcu, complete_mc_list_del);
1400 wait_for_completion(&mci->complete);
1404 * edac_mc_add_mc: Insert the 'mci' structure into the mci global list and
1405 * create sysfs entries associated with mci structure
1406 * @mci: pointer to the mci structure to be added to the list
1407 * @mc_idx: A unique numeric identifier to be assigned to the 'mci' structure.
1409 * Return:
1410 * 0 Success
1411 * !0 Failure
1414 /* FIXME - should a warning be printed if no error detection? correction? */
1415 int edac_mc_add_mc(struct mem_ctl_info *mci, int mc_idx)
1417 debugf0("%s()\n", __func__);
1418 mci->mc_idx = mc_idx;
1419 #ifdef CONFIG_EDAC_DEBUG
1420 if (edac_debug_level >= 3)
1421 edac_mc_dump_mci(mci);
1423 if (edac_debug_level >= 4) {
1424 int i;
1426 for (i = 0; i < mci->nr_csrows; i++) {
1427 int j;
1429 edac_mc_dump_csrow(&mci->csrows[i]);
1430 for (j = 0; j < mci->csrows[i].nr_channels; j++)
1431 edac_mc_dump_channel(
1432 &mci->csrows[i].channels[j]);
1435 #endif
1436 down(&mem_ctls_mutex);
1438 if (add_mc_to_global_list(mci))
1439 goto fail0;
1441 /* set load time so that error rate can be tracked */
1442 mci->start_time = jiffies;
1444 if (edac_create_sysfs_mci_device(mci)) {
1445 edac_mc_printk(mci, KERN_WARNING,
1446 "failed to create sysfs device\n");
1447 goto fail1;
1450 /* Report action taken */
1451 edac_mc_printk(mci, KERN_INFO, "Giving out device to %s %s: DEV %s\n",
1452 mci->mod_name, mci->ctl_name, dev_name(mci->dev));
1454 up(&mem_ctls_mutex);
1455 return 0;
1457 fail1:
1458 del_mc_from_global_list(mci);
1460 fail0:
1461 up(&mem_ctls_mutex);
1462 return 1;
1464 EXPORT_SYMBOL_GPL(edac_mc_add_mc);
1467 * edac_mc_del_mc: Remove sysfs entries for specified mci structure and
1468 * remove mci structure from global list
1469 * @pdev: Pointer to 'struct device' representing mci structure to remove.
1471 * Return pointer to removed mci structure, or NULL if device not found.
1473 struct mem_ctl_info * edac_mc_del_mc(struct device *dev)
1475 struct mem_ctl_info *mci;
1477 debugf0("MC: %s()\n", __func__);
1478 down(&mem_ctls_mutex);
1480 if ((mci = find_mci_by_dev(dev)) == NULL) {
1481 up(&mem_ctls_mutex);
1482 return NULL;
1485 edac_remove_sysfs_mci_device(mci);
1486 del_mc_from_global_list(mci);
1487 up(&mem_ctls_mutex);
1488 edac_printk(KERN_INFO, EDAC_MC,
1489 "Removed device %d for %s %s: DEV %s\n", mci->mc_idx,
1490 mci->mod_name, mci->ctl_name, dev_name(mci->dev));
1491 return mci;
1493 EXPORT_SYMBOL_GPL(edac_mc_del_mc);
1495 void edac_mc_scrub_block(unsigned long page, unsigned long offset, u32 size)
1497 struct page *pg;
1498 void *virt_addr;
1499 unsigned long flags = 0;
1501 debugf3("%s()\n", __func__);
1503 /* ECC error page was not in our memory. Ignore it. */
1504 if(!pfn_valid(page))
1505 return;
1507 /* Find the actual page structure then map it and fix */
1508 pg = pfn_to_page(page);
1510 if (PageHighMem(pg))
1511 local_irq_save(flags);
1513 virt_addr = kmap_atomic(pg, KM_BOUNCE_READ);
1515 /* Perform architecture specific atomic scrub operation */
1516 atomic_scrub(virt_addr + offset, size);
1518 /* Unmap and complete */
1519 kunmap_atomic(virt_addr, KM_BOUNCE_READ);
1521 if (PageHighMem(pg))
1522 local_irq_restore(flags);
1524 EXPORT_SYMBOL_GPL(edac_mc_scrub_block);
1526 /* FIXME - should return -1 */
1527 int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, unsigned long page)
1529 struct csrow_info *csrows = mci->csrows;
1530 int row, i;
1532 debugf1("MC%d: %s(): 0x%lx\n", mci->mc_idx, __func__, page);
1533 row = -1;
1535 for (i = 0; i < mci->nr_csrows; i++) {
1536 struct csrow_info *csrow = &csrows[i];
1538 if (csrow->nr_pages == 0)
1539 continue;
1541 debugf3("MC%d: %s(): first(0x%lx) page(0x%lx) last(0x%lx) "
1542 "mask(0x%lx)\n", mci->mc_idx, __func__,
1543 csrow->first_page, page, csrow->last_page,
1544 csrow->page_mask);
1546 if ((page >= csrow->first_page) &&
1547 (page <= csrow->last_page) &&
1548 ((page & csrow->page_mask) ==
1549 (csrow->first_page & csrow->page_mask))) {
1550 row = i;
1551 break;
1555 if (row == -1)
1556 edac_mc_printk(mci, KERN_ERR,
1557 "could not look up page error address %lx\n",
1558 (unsigned long) page);
1560 return row;
1562 EXPORT_SYMBOL_GPL(edac_mc_find_csrow_by_page);
1564 /* FIXME - setable log (warning/emerg) levels */
1565 /* FIXME - integrate with evlog: http://evlog.sourceforge.net/ */
1566 void edac_mc_handle_ce(struct mem_ctl_info *mci,
1567 unsigned long page_frame_number, unsigned long offset_in_page,
1568 unsigned long syndrome, int row, int channel, const char *msg)
1570 unsigned long remapped_page;
1572 debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
1574 /* FIXME - maybe make panic on INTERNAL ERROR an option */
1575 if (row >= mci->nr_csrows || row < 0) {
1576 /* something is wrong */
1577 edac_mc_printk(mci, KERN_ERR,
1578 "INTERNAL ERROR: row out of range "
1579 "(%d >= %d)\n", row, mci->nr_csrows);
1580 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1581 return;
1584 if (channel >= mci->csrows[row].nr_channels || channel < 0) {
1585 /* something is wrong */
1586 edac_mc_printk(mci, KERN_ERR,
1587 "INTERNAL ERROR: channel out of range "
1588 "(%d >= %d)\n", channel,
1589 mci->csrows[row].nr_channels);
1590 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1591 return;
1594 if (log_ce)
1595 /* FIXME - put in DIMM location */
1596 edac_mc_printk(mci, KERN_WARNING,
1597 "CE page 0x%lx, offset 0x%lx, grain %d, syndrome "
1598 "0x%lx, row %d, channel %d, label \"%s\": %s\n",
1599 page_frame_number, offset_in_page,
1600 mci->csrows[row].grain, syndrome, row, channel,
1601 mci->csrows[row].channels[channel].label, msg);
1603 mci->ce_count++;
1604 mci->csrows[row].ce_count++;
1605 mci->csrows[row].channels[channel].ce_count++;
1607 if (mci->scrub_mode & SCRUB_SW_SRC) {
1609 * Some MC's can remap memory so that it is still available
1610 * at a different address when PCI devices map into memory.
1611 * MC's that can't do this lose the memory where PCI devices
1612 * are mapped. This mapping is MC dependant and so we call
1613 * back into the MC driver for it to map the MC page to
1614 * a physical (CPU) page which can then be mapped to a virtual
1615 * page - which can then be scrubbed.
1617 remapped_page = mci->ctl_page_to_phys ?
1618 mci->ctl_page_to_phys(mci, page_frame_number) :
1619 page_frame_number;
1621 edac_mc_scrub_block(remapped_page, offset_in_page,
1622 mci->csrows[row].grain);
1625 EXPORT_SYMBOL_GPL(edac_mc_handle_ce);
1627 void edac_mc_handle_ce_no_info(struct mem_ctl_info *mci, const char *msg)
1629 if (log_ce)
1630 edac_mc_printk(mci, KERN_WARNING,
1631 "CE - no information available: %s\n", msg);
1633 mci->ce_noinfo_count++;
1634 mci->ce_count++;
1636 EXPORT_SYMBOL_GPL(edac_mc_handle_ce_no_info);
1638 void edac_mc_handle_ue(struct mem_ctl_info *mci,
1639 unsigned long page_frame_number, unsigned long offset_in_page,
1640 int row, const char *msg)
1642 int len = EDAC_MC_LABEL_LEN * 4;
1643 char labels[len + 1];
1644 char *pos = labels;
1645 int chan;
1646 int chars;
1648 debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
1650 /* FIXME - maybe make panic on INTERNAL ERROR an option */
1651 if (row >= mci->nr_csrows || row < 0) {
1652 /* something is wrong */
1653 edac_mc_printk(mci, KERN_ERR,
1654 "INTERNAL ERROR: row out of range "
1655 "(%d >= %d)\n", row, mci->nr_csrows);
1656 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
1657 return;
1660 chars = snprintf(pos, len + 1, "%s",
1661 mci->csrows[row].channels[0].label);
1662 len -= chars;
1663 pos += chars;
1665 for (chan = 1; (chan < mci->csrows[row].nr_channels) && (len > 0);
1666 chan++) {
1667 chars = snprintf(pos, len + 1, ":%s",
1668 mci->csrows[row].channels[chan].label);
1669 len -= chars;
1670 pos += chars;
1673 if (log_ue)
1674 edac_mc_printk(mci, KERN_EMERG,
1675 "UE page 0x%lx, offset 0x%lx, grain %d, row %d, "
1676 "labels \"%s\": %s\n", page_frame_number,
1677 offset_in_page, mci->csrows[row].grain, row, labels,
1678 msg);
1680 if (panic_on_ue)
1681 panic("EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, "
1682 "row %d, labels \"%s\": %s\n", mci->mc_idx,
1683 page_frame_number, offset_in_page,
1684 mci->csrows[row].grain, row, labels, msg);
1686 mci->ue_count++;
1687 mci->csrows[row].ue_count++;
1689 EXPORT_SYMBOL_GPL(edac_mc_handle_ue);
1691 void edac_mc_handle_ue_no_info(struct mem_ctl_info *mci, const char *msg)
1693 if (panic_on_ue)
1694 panic("EDAC MC%d: Uncorrected Error", mci->mc_idx);
1696 if (log_ue)
1697 edac_mc_printk(mci, KERN_WARNING,
1698 "UE - no information available: %s\n", msg);
1699 mci->ue_noinfo_count++;
1700 mci->ue_count++;
1702 EXPORT_SYMBOL_GPL(edac_mc_handle_ue_no_info);
1706 * Iterate over all MC instances and check for ECC, et al, errors
1708 static inline void check_mc_devices(void)
1710 struct list_head *item;
1711 struct mem_ctl_info *mci;
1713 debugf3("%s()\n", __func__);
1714 down(&mem_ctls_mutex);
1716 list_for_each(item, &mc_devices) {
1717 mci = list_entry(item, struct mem_ctl_info, link);
1719 if (mci->edac_check != NULL)
1720 mci->edac_check(mci);
1723 up(&mem_ctls_mutex);
1727 * Check MC status every poll_msec.
1728 * Check PCI status every poll_msec as well.
1730 * This where the work gets done for edac.
1732 * SMP safe, doesn't use NMI, and auto-rate-limits.
1734 static void do_edac_check(void)
1736 debugf3("%s()\n", __func__);
1737 check_mc_devices();
1738 do_pci_parity_check();
1741 static int edac_kernel_thread(void *arg)
1743 while (!kthread_should_stop()) {
1744 do_edac_check();
1746 /* goto sleep for the interval */
1747 schedule_timeout_interruptible((HZ * poll_msec) / 1000);
1748 try_to_freeze();
1751 return 0;
1755 * edac_mc_init
1756 * module initialization entry point
1758 static int __init edac_mc_init(void)
1760 edac_printk(KERN_INFO, EDAC_MC, EDAC_MC_VERSION "\n");
1763 * Harvest and clear any boot/initialization PCI parity errors
1765 * FIXME: This only clears errors logged by devices present at time of
1766 * module initialization. We should also do an initial clear
1767 * of each newly hotplugged device.
1769 clear_pci_parity_errors();
1771 /* Create the MC sysfs entries */
1772 if (edac_sysfs_memctrl_setup()) {
1773 edac_printk(KERN_ERR, EDAC_MC,
1774 "Error initializing sysfs code\n");
1775 return -ENODEV;
1778 /* Create the PCI parity sysfs entries */
1779 if (edac_sysfs_pci_setup()) {
1780 edac_sysfs_memctrl_teardown();
1781 edac_printk(KERN_ERR, EDAC_MC,
1782 "EDAC PCI: Error initializing sysfs code\n");
1783 return -ENODEV;
1786 /* create our kernel thread */
1787 edac_thread = kthread_run(edac_kernel_thread, NULL, "kedac");
1789 if (IS_ERR(edac_thread)) {
1790 /* remove the sysfs entries */
1791 edac_sysfs_memctrl_teardown();
1792 edac_sysfs_pci_teardown();
1793 return PTR_ERR(edac_thread);
1796 return 0;
1800 * edac_mc_exit()
1801 * module exit/termination functioni
1803 static void __exit edac_mc_exit(void)
1805 debugf0("%s()\n", __func__);
1806 kthread_stop(edac_thread);
1808 /* tear down the sysfs device */
1809 edac_sysfs_memctrl_teardown();
1810 edac_sysfs_pci_teardown();
1813 module_init(edac_mc_init);
1814 module_exit(edac_mc_exit);
1816 MODULE_LICENSE("GPL");
1817 MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh et al\n"
1818 "Based on work by Dan Hollis et al");
1819 MODULE_DESCRIPTION("Core library routines for MC reporting");
1821 module_param(panic_on_ue, int, 0644);
1822 MODULE_PARM_DESC(panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
1823 #ifdef CONFIG_PCI
1824 module_param(check_pci_parity, int, 0644);
1825 MODULE_PARM_DESC(check_pci_parity, "Check for PCI bus parity errors: 0=off 1=on");
1826 module_param(panic_on_pci_parity, int, 0644);
1827 MODULE_PARM_DESC(panic_on_pci_parity, "Panic on PCI Bus Parity error: 0=off 1=on");
1828 #endif
1829 module_param(log_ue, int, 0644);
1830 MODULE_PARM_DESC(log_ue, "Log uncorrectable error to console: 0=off 1=on");
1831 module_param(log_ce, int, 0644);
1832 MODULE_PARM_DESC(log_ce, "Log correctable error to console: 0=off 1=on");
1833 module_param(poll_msec, int, 0644);
1834 MODULE_PARM_DESC(poll_msec, "Polling period in milliseconds");
1835 #ifdef CONFIG_EDAC_DEBUG
1836 module_param(edac_debug_level, int, 0644);
1837 MODULE_PARM_DESC(edac_debug_level, "Debug level");
1838 #endif