fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / edac / edac_mc.c
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.
6  *
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/
10  *
11  * Modified by Dave Peterson and Doug Thompson
12  *
13  */
14
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 <linux/freezer.h>
32 #include <asm/uaccess.h>
33 #include <asm/page.h>
34 #include <asm/edac.h>
35 #include "edac_mc.h"
36
37 #define EDAC_MC_VERSION "Ver: 2.0.1 " __DATE__
38
39
40 #ifdef CONFIG_EDAC_DEBUG
41 /* Values of 0 to 4 will generate output */
42 int edac_debug_level = 1;
43 EXPORT_SYMBOL_GPL(edac_debug_level);
44 #endif
45
46 /* EDAC Controls, setable by module parameter, and sysfs */
47 static int log_ue = 1;
48 static int log_ce = 1;
49 static int panic_on_ue;
50 static int poll_msec = 1000;
51
52 /* lock to memory controller's control array */
53 static DECLARE_MUTEX(mem_ctls_mutex);
54 static struct list_head mc_devices = LIST_HEAD_INIT(mc_devices);
55
56 static struct task_struct *edac_thread;
57
58 #ifdef CONFIG_PCI
59 static int check_pci_parity = 0;        /* default YES check PCI parity */
60 static int panic_on_pci_parity;         /* default no panic on PCI Parity */
61 static atomic_t pci_parity_count = ATOMIC_INIT(0);
62
63 static struct kobject edac_pci_kobj; /* /sys/devices/system/edac/pci */
64 static struct completion edac_pci_kobj_complete;
65 #endif  /* CONFIG_PCI */
66
67 /*  START sysfs data and methods */
68
69
70 static const char *mem_types[] = {
71         [MEM_EMPTY] = "Empty",
72         [MEM_RESERVED] = "Reserved",
73         [MEM_UNKNOWN] = "Unknown",
74         [MEM_FPM] = "FPM",
75         [MEM_EDO] = "EDO",
76         [MEM_BEDO] = "BEDO",
77         [MEM_SDR] = "Unbuffered-SDR",
78         [MEM_RDR] = "Registered-SDR",
79         [MEM_DDR] = "Unbuffered-DDR",
80         [MEM_RDDR] = "Registered-DDR",
81         [MEM_RMBS] = "RMBS",
82         [MEM_DDR2] = "DDR2",
83         [MEM_FB_DDR2] = "Fully-Buffered-DDR2",
84         [MEM_RDDR2] = "Registered-DDR2",
85 };
86
87 static const char *dev_types[] = {
88         [DEV_UNKNOWN] = "Unknown",
89         [DEV_X1] = "x1",
90         [DEV_X2] = "x2",
91         [DEV_X4] = "x4",
92         [DEV_X8] = "x8",
93         [DEV_X16] = "x16",
94         [DEV_X32] = "x32",
95         [DEV_X64] = "x64"
96 };
97
98 static const char *edac_caps[] = {
99         [EDAC_UNKNOWN] = "Unknown",
100         [EDAC_NONE] = "None",
101         [EDAC_RESERVED] = "Reserved",
102         [EDAC_PARITY] = "PARITY",
103         [EDAC_EC] = "EC",
104         [EDAC_SECDED] = "SECDED",
105         [EDAC_S2ECD2ED] = "S2ECD2ED",
106         [EDAC_S4ECD4ED] = "S4ECD4ED",
107         [EDAC_S8ECD8ED] = "S8ECD8ED",
108         [EDAC_S16ECD16ED] = "S16ECD16ED"
109 };
110
111 /* sysfs object: /sys/devices/system/edac */
112 static struct sysdev_class edac_class = {
113         set_kset_name("edac"),
114 };
115
116 /* sysfs object:
117  *      /sys/devices/system/edac/mc
118  */
119 static struct kobject edac_memctrl_kobj;
120
121 /* We use these to wait for the reference counts on edac_memctrl_kobj and
122  * edac_pci_kobj to reach 0.
123  */
124 static struct completion edac_memctrl_kobj_complete;
125
126 /*
127  * /sys/devices/system/edac/mc;
128  *      data structures and methods
129  */
130 static ssize_t memctrl_int_show(void *ptr, char *buffer)
131 {
132         int *value = (int*) ptr;
133         return sprintf(buffer, "%u\n", *value);
134 }
135
136 static ssize_t memctrl_int_store(void *ptr, const char *buffer, size_t count)
137 {
138         int *value = (int*) ptr;
139
140         if (isdigit(*buffer))
141                 *value = simple_strtoul(buffer, NULL, 0);
142
143         return count;
144 }
145
146 struct memctrl_dev_attribute {
147         struct attribute attr;
148         void *value;
149         ssize_t (*show)(void *,char *);
150         ssize_t (*store)(void *, const char *, size_t);
151 };
152
153 /* Set of show/store abstract level functions for memory control object */
154 static ssize_t memctrl_dev_show(struct kobject *kobj,
155                 struct attribute *attr, char *buffer)
156 {
157         struct memctrl_dev_attribute *memctrl_dev;
158         memctrl_dev = (struct memctrl_dev_attribute*)attr;
159
160         if (memctrl_dev->show)
161                 return memctrl_dev->show(memctrl_dev->value, buffer);
162
163         return -EIO;
164 }
165
166 static ssize_t memctrl_dev_store(struct kobject *kobj, struct attribute *attr,
167                 const char *buffer, size_t count)
168 {
169         struct memctrl_dev_attribute *memctrl_dev;
170         memctrl_dev = (struct memctrl_dev_attribute*)attr;
171
172         if (memctrl_dev->store)
173                 return memctrl_dev->store(memctrl_dev->value, buffer, count);
174
175         return -EIO;
176 }
177
178 static struct sysfs_ops memctrlfs_ops = {
179         .show   = memctrl_dev_show,
180         .store  = memctrl_dev_store
181 };
182
183 #define MEMCTRL_ATTR(_name,_mode,_show,_store)                  \
184 struct memctrl_dev_attribute attr_##_name = {                   \
185         .attr = {.name = __stringify(_name), .mode = _mode },   \
186         .value  = &_name,                                       \
187         .show   = _show,                                        \
188         .store  = _store,                                       \
189 };
190
191 #define MEMCTRL_STRING_ATTR(_name,_data,_mode,_show,_store)     \
192 struct memctrl_dev_attribute attr_##_name = {                   \
193         .attr = {.name = __stringify(_name), .mode = _mode },   \
194         .value  = _data,                                        \
195         .show   = _show,                                        \
196         .store  = _store,                                       \
197 };
198
199 /* csrow<id> control files */
200 MEMCTRL_ATTR(panic_on_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
201 MEMCTRL_ATTR(log_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
202 MEMCTRL_ATTR(log_ce,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
203 MEMCTRL_ATTR(poll_msec,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
204
205 /* Base Attributes of the memory ECC object */
206 static struct memctrl_dev_attribute *memctrl_attr[] = {
207         &attr_panic_on_ue,
208         &attr_log_ue,
209         &attr_log_ce,
210         &attr_poll_msec,
211         NULL,
212 };
213
214 /* Main MC kobject release() function */
215 static void edac_memctrl_master_release(struct kobject *kobj)
216 {
217         debugf1("%s()\n", __func__);
218         complete(&edac_memctrl_kobj_complete);
219 }
220
221 static struct kobj_type ktype_memctrl = {
222         .release = edac_memctrl_master_release,
223         .sysfs_ops = &memctrlfs_ops,
224         .default_attrs = (struct attribute **) memctrl_attr,
225 };
226
227 /* Initialize the main sysfs entries for edac:
228  *   /sys/devices/system/edac
229  *
230  * and children
231  *
232  * Return:  0 SUCCESS
233  *         !0 FAILURE
234  */
235 static int edac_sysfs_memctrl_setup(void)
236 {
237         int err = 0;
238
239         debugf1("%s()\n", __func__);
240
241         /* create the /sys/devices/system/edac directory */
242         err = sysdev_class_register(&edac_class);
243
244         if (err) {
245                 debugf1("%s() error=%d\n", __func__, err);
246                 return err;
247         }
248
249         /* Init the MC's kobject */
250         memset(&edac_memctrl_kobj, 0, sizeof (edac_memctrl_kobj));
251         edac_memctrl_kobj.parent = &edac_class.kset.kobj;
252         edac_memctrl_kobj.ktype = &ktype_memctrl;
253
254         /* generate sysfs "..../edac/mc"   */
255         err = kobject_set_name(&edac_memctrl_kobj,"mc");
256
257         if (err)
258                 goto fail;
259
260         /* FIXME: maybe new sysdev_create_subdir() */
261         err = kobject_register(&edac_memctrl_kobj);
262
263         if (err) {
264                 debugf1("Failed to register '.../edac/mc'\n");
265                 goto fail;
266         }
267
268         debugf1("Registered '.../edac/mc' kobject\n");
269
270         return 0;
271
272 fail:
273         sysdev_class_unregister(&edac_class);
274         return err;
275 }
276
277 /*
278  * MC teardown:
279  *      the '..../edac/mc' kobject followed by '..../edac' itself
280  */
281 static void edac_sysfs_memctrl_teardown(void)
282 {
283         debugf0("MC: " __FILE__ ": %s()\n", __func__);
284
285         /* Unregister the MC's kobject and wait for reference count to reach
286          * 0.
287          */
288         init_completion(&edac_memctrl_kobj_complete);
289         kobject_unregister(&edac_memctrl_kobj);
290         wait_for_completion(&edac_memctrl_kobj_complete);
291
292         /* Unregister the 'edac' object */
293         sysdev_class_unregister(&edac_class);
294 }
295
296 #ifdef CONFIG_PCI
297 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
298 {
299         int *value = ptr;
300         return sprintf(buffer,"%d\n",*value);
301 }
302
303 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
304 {
305         int *value = ptr;
306
307         if (isdigit(*buffer))
308                 *value = simple_strtoul(buffer,NULL,0);
309
310         return count;
311 }
312
313 struct edac_pci_dev_attribute {
314         struct attribute attr;
315         void *value;
316         ssize_t (*show)(void *,char *);
317         ssize_t (*store)(void *, const char *,size_t);
318 };
319
320 /* Set of show/store abstract level functions for PCI Parity object */
321 static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
322                 char *buffer)
323 {
324         struct edac_pci_dev_attribute *edac_pci_dev;
325         edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
326
327         if (edac_pci_dev->show)
328                 return edac_pci_dev->show(edac_pci_dev->value, buffer);
329         return -EIO;
330 }
331
332 static ssize_t edac_pci_dev_store(struct kobject *kobj,
333                 struct attribute *attr, const char *buffer, size_t count)
334 {
335         struct edac_pci_dev_attribute *edac_pci_dev;
336         edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
337
338         if (edac_pci_dev->show)
339                 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
340         return -EIO;
341 }
342
343 static struct sysfs_ops edac_pci_sysfs_ops = {
344         .show   = edac_pci_dev_show,
345         .store  = edac_pci_dev_store
346 };
347
348 #define EDAC_PCI_ATTR(_name,_mode,_show,_store)                 \
349 struct edac_pci_dev_attribute edac_pci_attr_##_name = {         \
350         .attr = {.name = __stringify(_name), .mode = _mode },   \
351         .value  = &_name,                                       \
352         .show   = _show,                                        \
353         .store  = _store,                                       \
354 };
355
356 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store)    \
357 struct edac_pci_dev_attribute edac_pci_attr_##_name = {         \
358         .attr = {.name = __stringify(_name), .mode = _mode },   \
359         .value  = _data,                                        \
360         .show   = _show,                                        \
361         .store  = _store,                                       \
362 };
363
364 /* PCI Parity control files */
365 EDAC_PCI_ATTR(check_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
366         edac_pci_int_store);
367 EDAC_PCI_ATTR(panic_on_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
368         edac_pci_int_store);
369 EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
370
371 /* Base Attributes of the memory ECC object */
372 static struct edac_pci_dev_attribute *edac_pci_attr[] = {
373         &edac_pci_attr_check_pci_parity,
374         &edac_pci_attr_panic_on_pci_parity,
375         &edac_pci_attr_pci_parity_count,
376         NULL,
377 };
378
379 /* No memory to release */
380 static void edac_pci_release(struct kobject *kobj)
381 {
382         debugf1("%s()\n", __func__);
383         complete(&edac_pci_kobj_complete);
384 }
385
386 static struct kobj_type ktype_edac_pci = {
387         .release = edac_pci_release,
388         .sysfs_ops = &edac_pci_sysfs_ops,
389         .default_attrs = (struct attribute **) edac_pci_attr,
390 };
391
392 /**
393  * edac_sysfs_pci_setup()
394  *
395  */
396 static int edac_sysfs_pci_setup(void)
397 {
398         int err;
399
400         debugf1("%s()\n", __func__);
401
402         memset(&edac_pci_kobj, 0, sizeof(edac_pci_kobj));
403         edac_pci_kobj.parent = &edac_class.kset.kobj;
404         edac_pci_kobj.ktype = &ktype_edac_pci;
405         err = kobject_set_name(&edac_pci_kobj, "pci");
406
407         if (!err) {
408                 /* Instanstiate the csrow object */
409                 /* FIXME: maybe new sysdev_create_subdir() */
410                 err = kobject_register(&edac_pci_kobj);
411
412                 if (err)
413                         debugf1("Failed to register '.../edac/pci'\n");
414                 else
415                         debugf1("Registered '.../edac/pci' kobject\n");
416         }
417
418         return err;
419 }
420
421 static void edac_sysfs_pci_teardown(void)
422 {
423         debugf0("%s()\n", __func__);
424         init_completion(&edac_pci_kobj_complete);
425         kobject_unregister(&edac_pci_kobj);
426         wait_for_completion(&edac_pci_kobj_complete);
427 }
428
429
430 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
431 {
432         int where;
433         u16 status;
434
435         where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
436         pci_read_config_word(dev, where, &status);
437
438         /* If we get back 0xFFFF then we must suspect that the card has been
439          * pulled but the Linux PCI layer has not yet finished cleaning up.
440          * We don't want to report on such devices
441          */
442
443         if (status == 0xFFFF) {
444                 u32 sanity;
445
446                 pci_read_config_dword(dev, 0, &sanity);
447
448                 if (sanity == 0xFFFFFFFF)
449                         return 0;
450         }
451
452         status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
453                 PCI_STATUS_PARITY;
454
455         if (status)
456                 /* reset only the bits we are interested in */
457                 pci_write_config_word(dev, where, status);
458
459         return status;
460 }
461
462 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
463
464 /* Clear any PCI parity errors logged by this device. */
465 static void edac_pci_dev_parity_clear(struct pci_dev *dev)
466 {
467         u8 header_type;
468
469         get_pci_parity_status(dev, 0);
470
471         /* read the device TYPE, looking for bridges */
472         pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
473
474         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
475                 get_pci_parity_status(dev, 1);
476 }
477
478 /*
479  *  PCI Parity polling
480  *
481  */
482 static void edac_pci_dev_parity_test(struct pci_dev *dev)
483 {
484         u16 status;
485         u8  header_type;
486
487         /* read the STATUS register on this device
488          */
489         status = get_pci_parity_status(dev, 0);
490
491         debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
492
493         /* check the status reg for errors */
494         if (status) {
495                 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
496                         edac_printk(KERN_CRIT, EDAC_PCI,
497                                 "Signaled System Error on %s\n",
498                                 pci_name(dev));
499
500                 if (status & (PCI_STATUS_PARITY)) {
501                         edac_printk(KERN_CRIT, EDAC_PCI,
502                                 "Master Data Parity Error on %s\n",
503                                 pci_name(dev));
504
505                         atomic_inc(&pci_parity_count);
506                 }
507
508                 if (status & (PCI_STATUS_DETECTED_PARITY)) {
509                         edac_printk(KERN_CRIT, EDAC_PCI,
510                                 "Detected Parity Error on %s\n",
511                                 pci_name(dev));
512
513                         atomic_inc(&pci_parity_count);
514                 }
515         }
516
517         /* read the device TYPE, looking for bridges */
518         pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
519
520         debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
521
522         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
523                 /* On bridges, need to examine secondary status register  */
524                 status = get_pci_parity_status(dev, 1);
525
526                 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
527                                 status, dev->dev.bus_id );
528
529                 /* check the secondary status reg for errors */
530                 if (status) {
531                         if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
532                                 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
533                                         "Signaled System Error on %s\n",
534                                         pci_name(dev));
535
536                         if (status & (PCI_STATUS_PARITY)) {
537                                 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
538                                         "Master Data Parity Error on "
539                                         "%s\n", pci_name(dev));
540
541                                 atomic_inc(&pci_parity_count);
542                         }
543
544                         if (status & (PCI_STATUS_DETECTED_PARITY)) {
545                                 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
546                                         "Detected Parity Error on %s\n",
547                                         pci_name(dev));
548
549                                 atomic_inc(&pci_parity_count);
550                         }
551                 }
552         }
553 }
554
555 /*
556  * pci_dev parity list iterator
557  *      Scan the PCI device list for one iteration, looking for SERRORs
558  *      Master Parity ERRORS or Parity ERRORs on primary or secondary devices
559  */
560 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
561 {
562         struct pci_dev *dev = NULL;
563
564         /* request for kernel access to the next PCI device, if any,
565          * and while we are looking at it have its reference count
566          * bumped until we are done with it
567          */
568         while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
569                 fn(dev);
570         }
571 }
572
573 static void do_pci_parity_check(void)
574 {
575         unsigned long flags;
576         int before_count;
577
578         debugf3("%s()\n", __func__);
579
580         if (!check_pci_parity)
581                 return;
582
583         before_count = atomic_read(&pci_parity_count);
584
585         /* scan all PCI devices looking for a Parity Error on devices and
586          * bridges
587          */
588         local_irq_save(flags);
589         edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
590         local_irq_restore(flags);
591
592         /* Only if operator has selected panic on PCI Error */
593         if (panic_on_pci_parity) {
594                 /* If the count is different 'after' from 'before' */
595                 if (before_count != atomic_read(&pci_parity_count))
596                         panic("EDAC: PCI Parity Error");
597         }
598 }
599
600 static inline void clear_pci_parity_errors(void)
601 {
602         /* Clear any PCI bus parity errors that devices initially have logged
603          * in their registers.
604          */
605         edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
606 }
607
608 #else   /* CONFIG_PCI */
609
610 /* pre-process these away */
611 #define do_pci_parity_check()
612 #define clear_pci_parity_errors()
613 #define edac_sysfs_pci_teardown()
614 #define edac_sysfs_pci_setup()  (0)
615
616 #endif  /* CONFIG_PCI */
617
618 /* EDAC sysfs CSROW data structures and methods
619  */
620
621 /* Set of more default csrow<id> attribute show/store functions */
622 static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data, int private)
623 {
624         return sprintf(data,"%u\n", csrow->ue_count);
625 }
626
627 static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data, int private)
628 {
629         return sprintf(data,"%u\n", csrow->ce_count);
630 }
631
632 static ssize_t csrow_size_show(struct csrow_info *csrow, char *data, int private)
633 {
634         return sprintf(data,"%u\n", PAGES_TO_MiB(csrow->nr_pages));
635 }
636
637 static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data, int private)
638 {
639         return sprintf(data,"%s\n", mem_types[csrow->mtype]);
640 }
641
642 static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data, int private)
643 {
644         return sprintf(data,"%s\n", dev_types[csrow->dtype]);
645 }
646
647 static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data, int private)
648 {
649         return sprintf(data,"%s\n", edac_caps[csrow->edac_mode]);
650 }
651
652 /* show/store functions for DIMM Label attributes */
653 static ssize_t channel_dimm_label_show(struct csrow_info *csrow,
654                 char *data, int channel)
655 {
656         return snprintf(data, EDAC_MC_LABEL_LEN,"%s",
657                         csrow->channels[channel].label);
658 }
659
660 static ssize_t channel_dimm_label_store(struct csrow_info *csrow,
661                                 const char *data,
662                                 size_t count,
663                                 int channel)
664 {
665         ssize_t max_size = 0;
666
667         max_size = min((ssize_t)count,(ssize_t)EDAC_MC_LABEL_LEN-1);
668         strncpy(csrow->channels[channel].label, data, max_size);
669         csrow->channels[channel].label[max_size] = '\0';
670
671         return max_size;
672 }
673
674 /* show function for dynamic chX_ce_count attribute */
675 static ssize_t channel_ce_count_show(struct csrow_info *csrow,
676                                 char *data,
677                                 int channel)
678 {
679         return sprintf(data, "%u\n", csrow->channels[channel].ce_count);
680 }
681
682 /* csrow specific attribute structure */
683 struct csrowdev_attribute {
684         struct attribute attr;
685         ssize_t (*show)(struct csrow_info *,char *,int);
686         ssize_t (*store)(struct csrow_info *, const char *,size_t,int);
687         int    private;
688 };
689
690 #define to_csrow(k) container_of(k, struct csrow_info, kobj)
691 #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
692
693 /* Set of show/store higher level functions for default csrow attributes */
694 static ssize_t csrowdev_show(struct kobject *kobj,
695                         struct attribute *attr,
696                         char *buffer)
697 {
698         struct csrow_info *csrow = to_csrow(kobj);
699         struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
700
701         if (csrowdev_attr->show)
702                 return csrowdev_attr->show(csrow,
703                                         buffer,
704                                         csrowdev_attr->private);
705         return -EIO;
706 }
707
708 static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
709                 const char *buffer, size_t count)
710 {
711         struct csrow_info *csrow = to_csrow(kobj);
712         struct csrowdev_attribute * csrowdev_attr = to_csrowdev_attr(attr);
713
714         if (csrowdev_attr->store)
715                 return csrowdev_attr->store(csrow,
716                                         buffer,
717                                         count,
718                                         csrowdev_attr->private);
719         return -EIO;
720 }
721
722 static struct sysfs_ops csrowfs_ops = {
723         .show   = csrowdev_show,
724         .store  = csrowdev_store
725 };
726
727 #define CSROWDEV_ATTR(_name,_mode,_show,_store,_private)        \
728 struct csrowdev_attribute attr_##_name = {                      \
729         .attr = {.name = __stringify(_name), .mode = _mode },   \
730         .show   = _show,                                        \
731         .store  = _store,                                       \
732         .private = _private,                                    \
733 };
734
735 /* default cwrow<id>/attribute files */
736 CSROWDEV_ATTR(size_mb,S_IRUGO,csrow_size_show,NULL,0);
737 CSROWDEV_ATTR(dev_type,S_IRUGO,csrow_dev_type_show,NULL,0);
738 CSROWDEV_ATTR(mem_type,S_IRUGO,csrow_mem_type_show,NULL,0);
739 CSROWDEV_ATTR(edac_mode,S_IRUGO,csrow_edac_mode_show,NULL,0);
740 CSROWDEV_ATTR(ue_count,S_IRUGO,csrow_ue_count_show,NULL,0);
741 CSROWDEV_ATTR(ce_count,S_IRUGO,csrow_ce_count_show,NULL,0);
742
743 /* default attributes of the CSROW<id> object */
744 static struct csrowdev_attribute *default_csrow_attr[] = {
745         &attr_dev_type,
746         &attr_mem_type,
747         &attr_edac_mode,
748         &attr_size_mb,
749         &attr_ue_count,
750         &attr_ce_count,
751         NULL,
752 };
753
754
755 /* possible dynamic channel DIMM Label attribute files */
756 CSROWDEV_ATTR(ch0_dimm_label,S_IRUGO|S_IWUSR,
757                 channel_dimm_label_show,
758                 channel_dimm_label_store,
759                 0 );
760 CSROWDEV_ATTR(ch1_dimm_label,S_IRUGO|S_IWUSR,
761                 channel_dimm_label_show,
762                 channel_dimm_label_store,
763                 1 );
764 CSROWDEV_ATTR(ch2_dimm_label,S_IRUGO|S_IWUSR,
765                 channel_dimm_label_show,
766                 channel_dimm_label_store,
767                 2 );
768 CSROWDEV_ATTR(ch3_dimm_label,S_IRUGO|S_IWUSR,
769                 channel_dimm_label_show,
770                 channel_dimm_label_store,
771                 3 );
772 CSROWDEV_ATTR(ch4_dimm_label,S_IRUGO|S_IWUSR,
773                 channel_dimm_label_show,
774                 channel_dimm_label_store,
775                 4 );
776 CSROWDEV_ATTR(ch5_dimm_label,S_IRUGO|S_IWUSR,
777                 channel_dimm_label_show,
778                 channel_dimm_label_store,
779                 5 );
780
781 /* Total possible dynamic DIMM Label attribute file table */
782 static struct csrowdev_attribute *dynamic_csrow_dimm_attr[] = {
783                 &attr_ch0_dimm_label,
784                 &attr_ch1_dimm_label,
785                 &attr_ch2_dimm_label,
786                 &attr_ch3_dimm_label,
787                 &attr_ch4_dimm_label,
788                 &attr_ch5_dimm_label
789 };
790
791 /* possible dynamic channel ce_count attribute files */
792 CSROWDEV_ATTR(ch0_ce_count,S_IRUGO|S_IWUSR,
793                 channel_ce_count_show,
794                 NULL,
795                 0 );
796 CSROWDEV_ATTR(ch1_ce_count,S_IRUGO|S_IWUSR,
797                 channel_ce_count_show,
798                 NULL,
799                 1 );
800 CSROWDEV_ATTR(ch2_ce_count,S_IRUGO|S_IWUSR,
801                 channel_ce_count_show,
802                 NULL,
803                 2 );
804 CSROWDEV_ATTR(ch3_ce_count,S_IRUGO|S_IWUSR,
805                 channel_ce_count_show,
806                 NULL,
807                 3 );
808 CSROWDEV_ATTR(ch4_ce_count,S_IRUGO|S_IWUSR,
809                 channel_ce_count_show,
810                 NULL,
811                 4 );
812 CSROWDEV_ATTR(ch5_ce_count,S_IRUGO|S_IWUSR,
813                 channel_ce_count_show,
814                 NULL,
815                 5 );
816
817 /* Total possible dynamic ce_count attribute file table */
818 static struct csrowdev_attribute *dynamic_csrow_ce_count_attr[] = {
819                 &attr_ch0_ce_count,
820                 &attr_ch1_ce_count,
821                 &attr_ch2_ce_count,
822                 &attr_ch3_ce_count,
823                 &attr_ch4_ce_count,
824                 &attr_ch5_ce_count
825 };
826
827
828 #define EDAC_NR_CHANNELS        6
829
830 /* Create dynamic CHANNEL files, indexed by 'chan',  under specifed CSROW */
831 static int edac_create_channel_files(struct kobject *kobj, int chan)
832 {
833         int err=-ENODEV;
834
835         if (chan >= EDAC_NR_CHANNELS)
836                 return err;
837
838         /* create the DIMM label attribute file */
839         err = sysfs_create_file(kobj,
840                         (struct attribute *) dynamic_csrow_dimm_attr[chan]);
841
842         if (!err) {
843                 /* create the CE Count attribute file */
844                 err = sysfs_create_file(kobj,
845                         (struct attribute *) dynamic_csrow_ce_count_attr[chan]);
846         } else {
847                 debugf1("%s()  dimm labels and ce_count files created", __func__);
848         }
849
850         return err;
851 }
852
853 /* No memory to release for this kobj */
854 static void edac_csrow_instance_release(struct kobject *kobj)
855 {
856         struct csrow_info *cs;
857
858         cs = container_of(kobj, struct csrow_info, kobj);
859         complete(&cs->kobj_complete);
860 }
861
862 /* the kobj_type instance for a CSROW */
863 static struct kobj_type ktype_csrow = {
864         .release = edac_csrow_instance_release,
865         .sysfs_ops = &csrowfs_ops,
866         .default_attrs = (struct attribute **) default_csrow_attr,
867 };
868
869 /* Create a CSROW object under specifed edac_mc_device */
870 static int edac_create_csrow_object(
871                 struct kobject *edac_mci_kobj,
872                 struct csrow_info *csrow,
873                 int index)
874 {
875         int err = 0;
876         int chan;
877
878         memset(&csrow->kobj, 0, sizeof(csrow->kobj));
879
880         /* generate ..../edac/mc/mc<id>/csrow<index>   */
881
882         csrow->kobj.parent = edac_mci_kobj;
883         csrow->kobj.ktype = &ktype_csrow;
884
885         /* name this instance of csrow<id> */
886         err = kobject_set_name(&csrow->kobj,"csrow%d",index);
887         if (err)
888                 goto error_exit;
889
890         /* Instanstiate the csrow object */
891         err = kobject_register(&csrow->kobj);
892         if (!err) {
893                 /* Create the dyanmic attribute files on this csrow,
894                  * namely, the DIMM labels and the channel ce_count
895                  */
896                 for (chan = 0; chan < csrow->nr_channels; chan++) {
897                         err = edac_create_channel_files(&csrow->kobj,chan);
898                         if (err)
899                                 break;
900                 }
901         }
902
903 error_exit:
904         return err;
905 }
906
907 /* default sysfs methods and data structures for the main MCI kobject */
908
909 static ssize_t mci_reset_counters_store(struct mem_ctl_info *mci,
910                 const char *data, size_t count)
911 {
912         int row, chan;
913
914         mci->ue_noinfo_count = 0;
915         mci->ce_noinfo_count = 0;
916         mci->ue_count = 0;
917         mci->ce_count = 0;
918
919         for (row = 0; row < mci->nr_csrows; row++) {
920                 struct csrow_info *ri = &mci->csrows[row];
921
922                 ri->ue_count = 0;
923                 ri->ce_count = 0;
924
925                 for (chan = 0; chan < ri->nr_channels; chan++)
926                         ri->channels[chan].ce_count = 0;
927         }
928
929         mci->start_time = jiffies;
930         return count;
931 }
932
933 /* default attribute files for the MCI object */
934 static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
935 {
936         return sprintf(data,"%d\n", mci->ue_count);
937 }
938
939 static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
940 {
941         return sprintf(data,"%d\n", mci->ce_count);
942 }
943
944 static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
945 {
946         return sprintf(data,"%d\n", mci->ce_noinfo_count);
947 }
948
949 static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
950 {
951         return sprintf(data,"%d\n", mci->ue_noinfo_count);
952 }
953
954 static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
955 {
956         return sprintf(data,"%ld\n", (jiffies - mci->start_time) / HZ);
957 }
958
959 static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
960 {
961         return sprintf(data,"%s\n", mci->ctl_name);
962 }
963
964 static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
965 {
966         int total_pages, csrow_idx;
967
968         for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
969                         csrow_idx++) {
970                 struct csrow_info *csrow = &mci->csrows[csrow_idx];
971
972                 if (!csrow->nr_pages)
973                         continue;
974
975                 total_pages += csrow->nr_pages;
976         }
977
978         return sprintf(data,"%u\n", PAGES_TO_MiB(total_pages));
979 }
980
981 struct mcidev_attribute {
982         struct attribute attr;
983         ssize_t (*show)(struct mem_ctl_info *,char *);
984         ssize_t (*store)(struct mem_ctl_info *, const char *,size_t);
985 };
986
987 #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
988 #define to_mcidev_attr(a) container_of(a, struct mcidev_attribute, attr)
989
990 /* MCI show/store functions for top most object */
991 static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
992                 char *buffer)
993 {
994         struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
995         struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
996
997         if (mcidev_attr->show)
998                 return mcidev_attr->show(mem_ctl_info, buffer);
999
1000         return -EIO;
1001 }
1002
1003 static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
1004                 const char *buffer, size_t count)
1005 {
1006         struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
1007         struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
1008
1009         if (mcidev_attr->store)
1010                 return mcidev_attr->store(mem_ctl_info, buffer, count);
1011
1012         return -EIO;
1013 }
1014
1015 static struct sysfs_ops mci_ops = {
1016         .show = mcidev_show,
1017         .store = mcidev_store
1018 };
1019
1020 #define MCIDEV_ATTR(_name,_mode,_show,_store)                   \
1021 struct mcidev_attribute mci_attr_##_name = {                    \
1022         .attr = {.name = __stringify(_name), .mode = _mode },   \
1023         .show   = _show,                                        \
1024         .store  = _store,                                       \
1025 };
1026
1027 /* default Control file */
1028 MCIDEV_ATTR(reset_counters,S_IWUSR,NULL,mci_reset_counters_store);
1029
1030 /* default Attribute files */
1031 MCIDEV_ATTR(mc_name,S_IRUGO,mci_ctl_name_show,NULL);
1032 MCIDEV_ATTR(size_mb,S_IRUGO,mci_size_mb_show,NULL);
1033 MCIDEV_ATTR(seconds_since_reset,S_IRUGO,mci_seconds_show,NULL);
1034 MCIDEV_ATTR(ue_noinfo_count,S_IRUGO,mci_ue_noinfo_show,NULL);
1035 MCIDEV_ATTR(ce_noinfo_count,S_IRUGO,mci_ce_noinfo_show,NULL);
1036 MCIDEV_ATTR(ue_count,S_IRUGO,mci_ue_count_show,NULL);
1037 MCIDEV_ATTR(ce_count,S_IRUGO,mci_ce_count_show,NULL);
1038
1039 static struct mcidev_attribute *mci_attr[] = {
1040         &mci_attr_reset_counters,
1041         &mci_attr_mc_name,
1042         &mci_attr_size_mb,
1043         &mci_attr_seconds_since_reset,
1044         &mci_attr_ue_noinfo_count,
1045         &mci_attr_ce_noinfo_count,
1046         &mci_attr_ue_count,
1047         &mci_attr_ce_count,
1048         NULL
1049 };
1050
1051 /*
1052  * Release of a MC controlling instance
1053  */
1054 static void edac_mci_instance_release(struct kobject *kobj)
1055 {
1056         struct mem_ctl_info *mci;
1057
1058         mci = to_mci(kobj);
1059         debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
1060         complete(&mci->kobj_complete);
1061 }
1062
1063 static struct kobj_type ktype_mci = {
1064         .release = edac_mci_instance_release,
1065         .sysfs_ops = &mci_ops,
1066         .default_attrs = (struct attribute **) mci_attr,
1067 };
1068
1069
1070 #define EDAC_DEVICE_SYMLINK     "device"
1071
1072 /*
1073  * Create a new Memory Controller kobject instance,
1074  *      mc<id> under the 'mc' directory
1075  *
1076  * Return:
1077  *      0       Success
1078  *      !0      Failure
1079  */
1080 static int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
1081 {
1082         int i;
1083         int err;
1084         struct csrow_info *csrow;
1085         struct kobject *edac_mci_kobj=&mci->edac_mci_kobj;
1086
1087         debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
1088         memset(edac_mci_kobj, 0, sizeof(*edac_mci_kobj));
1089
1090         /* set the name of the mc<id> object */
1091         err = kobject_set_name(edac_mci_kobj,"mc%d",mci->mc_idx);
1092         if (err)
1093                 return err;
1094
1095         /* link to our parent the '..../edac/mc' object */
1096         edac_mci_kobj->parent = &edac_memctrl_kobj;
1097         edac_mci_kobj->ktype = &ktype_mci;
1098
1099         /* register the mc<id> kobject */
1100         err = kobject_register(edac_mci_kobj);
1101         if (err)
1102                 return err;
1103
1104         /* create a symlink for the device */
1105         err = sysfs_create_link(edac_mci_kobj, &mci->dev->kobj,
1106                                 EDAC_DEVICE_SYMLINK);
1107         if (err)
1108                 goto fail0;
1109
1110         /* Make directories for each CSROW object
1111          * under the mc<id> kobject
1112          */
1113         for (i = 0; i < mci->nr_csrows; i++) {
1114                 csrow = &mci->csrows[i];
1115
1116                 /* Only expose populated CSROWs */
1117                 if (csrow->nr_pages > 0) {
1118                         err = edac_create_csrow_object(edac_mci_kobj,csrow,i);
1119                         if (err)
1120                                 goto fail1;
1121                 }
1122         }
1123
1124         return 0;
1125
1126         /* CSROW error: backout what has already been registered,  */
1127 fail1:
1128         for ( i--; i >= 0; i--) {
1129                 if (csrow->nr_pages > 0) {
1130                         init_completion(&csrow->kobj_complete);
1131                         kobject_unregister(&mci->csrows[i].kobj);
1132                         wait_for_completion(&csrow->kobj_complete);
1133                 }
1134         }
1135
1136 fail0:
1137         init_completion(&mci->kobj_complete);
1138         kobject_unregister(edac_mci_kobj);
1139         wait_for_completion(&mci->kobj_complete);
1140         return err;
1141 }
1142
1143 /*
1144  * remove a Memory Controller instance
1145  */
1146 static void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
1147 {
1148         int i;
1149
1150         debugf0("%s()\n", __func__);
1151
1152         /* remove all csrow kobjects */
1153         for (i = 0; i < mci->nr_csrows; i++) {
1154                 if (mci->csrows[i].nr_pages > 0) {
1155                         init_completion(&mci->csrows[i].kobj_complete);
1156                         kobject_unregister(&mci->csrows[i].kobj);
1157                         wait_for_completion(&mci->csrows[i].kobj_complete);
1158                 }
1159         }
1160
1161         sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
1162         init_completion(&mci->kobj_complete);
1163         kobject_unregister(&mci->edac_mci_kobj);
1164         wait_for_completion(&mci->kobj_complete);
1165 }
1166
1167 /* END OF sysfs data and methods */
1168
1169 #ifdef CONFIG_EDAC_DEBUG
1170
1171 void edac_mc_dump_channel(struct channel_info *chan)
1172 {
1173         debugf4("\tchannel = %p\n", chan);
1174         debugf4("\tchannel->chan_idx = %d\n", chan->chan_idx);
1175         debugf4("\tchannel->ce_count = %d\n", chan->ce_count);
1176         debugf4("\tchannel->label = '%s'\n", chan->label);
1177         debugf4("\tchannel->csrow = %p\n\n", chan->csrow);
1178 }
1179 EXPORT_SYMBOL_GPL(edac_mc_dump_channel);
1180
1181 void edac_mc_dump_csrow(struct csrow_info *csrow)
1182 {
1183         debugf4("\tcsrow = %p\n", csrow);
1184         debugf4("\tcsrow->csrow_idx = %d\n", csrow->csrow_idx);
1185         debugf4("\tcsrow->first_page = 0x%lx\n",
1186                 csrow->first_page);
1187         debugf4("\tcsrow->last_page = 0x%lx\n", csrow->last_page);
1188         debugf4("\tcsrow->page_mask = 0x%lx\n", csrow->page_mask);
1189         debugf4("\tcsrow->nr_pages = 0x%x\n", csrow->nr_pages);
1190         debugf4("\tcsrow->nr_channels = %d\n",
1191                 csrow->nr_channels);
1192         debugf4("\tcsrow->channels = %p\n", csrow->channels);
1193         debugf4("\tcsrow->mci = %p\n\n", csrow->mci);
1194 }
1195 EXPORT_SYMBOL_GPL(edac_mc_dump_csrow);
1196
1197 void edac_mc_dump_mci(struct mem_ctl_info *mci)
1198 {
1199         debugf3("\tmci = %p\n", mci);
1200         debugf3("\tmci->mtype_cap = %lx\n", mci->mtype_cap);
1201         debugf3("\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
1202         debugf3("\tmci->edac_cap = %lx\n", mci->edac_cap);
1203         debugf4("\tmci->edac_check = %p\n", mci->edac_check);
1204         debugf3("\tmci->nr_csrows = %d, csrows = %p\n",
1205                 mci->nr_csrows, mci->csrows);
1206         debugf3("\tdev = %p\n", mci->dev);
1207         debugf3("\tmod_name:ctl_name = %s:%s\n",
1208                 mci->mod_name, mci->ctl_name);
1209         debugf3("\tpvt_info = %p\n\n", mci->pvt_info);
1210 }
1211 EXPORT_SYMBOL_GPL(edac_mc_dump_mci);
1212
1213 #endif  /* CONFIG_EDAC_DEBUG */
1214
1215 /* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'.
1216  * Adjust 'ptr' so that its alignment is at least as stringent as what the
1217  * compiler would provide for X and return the aligned result.
1218  *
1219  * If 'size' is a constant, the compiler will optimize this whole function
1220  * down to either a no-op or the addition of a constant to the value of 'ptr'.
1221  */
1222 static inline char * align_ptr(void *ptr, unsigned size)
1223 {
1224         unsigned align, r;
1225
1226         /* Here we assume that the alignment of a "long long" is the most
1227          * stringent alignment that the compiler will ever provide by default.
1228          * As far as I know, this is a reasonable assumption.
1229          */
1230         if (size > sizeof(long))
1231                 align = sizeof(long long);
1232         else if (size > sizeof(int))
1233                 align = sizeof(long);
1234         else if (size > sizeof(short))
1235                 align = sizeof(int);
1236         else if (size > sizeof(char))
1237                 align = sizeof(short);
1238         else
1239                 return (char *) ptr;
1240
1241         r = size % align;
1242
1243         if (r == 0)
1244                 return (char *) ptr;
1245
1246         return (char *) (((unsigned long) ptr) + align - r);
1247 }
1248
1249 /**
1250  * edac_mc_alloc: Allocate a struct mem_ctl_info structure
1251  * @size_pvt:   size of private storage needed
1252  * @nr_csrows:  Number of CWROWS needed for this MC
1253  * @nr_chans:   Number of channels for the MC
1254  *
1255  * Everything is kmalloc'ed as one big chunk - more efficient.
1256  * Only can be used if all structures have the same lifetime - otherwise
1257  * you have to allocate and initialize your own structures.
1258  *
1259  * Use edac_mc_free() to free mc structures allocated by this function.
1260  *
1261  * Returns:
1262  *      NULL allocation failed
1263  *      struct mem_ctl_info pointer
1264  */
1265 struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
1266                 unsigned nr_chans)
1267 {
1268         struct mem_ctl_info *mci;
1269         struct csrow_info *csi, *csrow;
1270         struct channel_info *chi, *chp, *chan;
1271         void *pvt;
1272         unsigned size;
1273         int row, chn;
1274
1275         /* Figure out the offsets of the various items from the start of an mc
1276          * structure.  We want the alignment of each item to be at least as
1277          * stringent as what the compiler would provide if we could simply
1278          * hardcode everything into a single struct.
1279          */
1280         mci = (struct mem_ctl_info *) 0;
1281         csi = (struct csrow_info *)align_ptr(&mci[1], sizeof(*csi));
1282         chi = (struct channel_info *)
1283                         align_ptr(&csi[nr_csrows], sizeof(*chi));
1284         pvt = align_ptr(&chi[nr_chans * nr_csrows], sz_pvt);
1285         size = ((unsigned long) pvt) + sz_pvt;
1286
1287         if ((mci = kmalloc(size, GFP_KERNEL)) == NULL)
1288                 return NULL;
1289
1290         /* Adjust pointers so they point within the memory we just allocated
1291          * rather than an imaginary chunk of memory located at address 0.
1292          */
1293         csi = (struct csrow_info *) (((char *) mci) + ((unsigned long) csi));
1294         chi = (struct channel_info *) (((char *) mci) + ((unsigned long) chi));
1295         pvt = sz_pvt ? (((char *) mci) + ((unsigned long) pvt)) : NULL;
1296
1297         memset(mci, 0, size);  /* clear all fields */
1298         mci->csrows = csi;
1299         mci->pvt_info = pvt;
1300         mci->nr_csrows = nr_csrows;
1301
1302         for (row = 0; row < nr_csrows; row++) {
1303                 csrow = &csi[row];
1304                 csrow->csrow_idx = row;
1305                 csrow->mci = mci;
1306                 csrow->nr_channels = nr_chans;
1307                 chp = &chi[row * nr_chans];
1308                 csrow->channels = chp;
1309
1310                 for (chn = 0; chn < nr_chans; chn++) {
1311                         chan = &chp[chn];
1312                         chan->chan_idx = chn;
1313                         chan->csrow = csrow;
1314                 }
1315         }
1316
1317         return mci;
1318 }
1319 EXPORT_SYMBOL_GPL(edac_mc_alloc);
1320
1321 /**
1322  * edac_mc_free:  Free a previously allocated 'mci' structure
1323  * @mci: pointer to a struct mem_ctl_info structure
1324  */
1325 void edac_mc_free(struct mem_ctl_info *mci)
1326 {
1327         kfree(mci);
1328 }
1329 EXPORT_SYMBOL_GPL(edac_mc_free);
1330
1331 static struct mem_ctl_info *find_mci_by_dev(struct device *dev)
1332 {
1333         struct mem_ctl_info *mci;
1334         struct list_head *item;
1335
1336         debugf3("%s()\n", __func__);
1337
1338         list_for_each(item, &mc_devices) {
1339                 mci = list_entry(item, struct mem_ctl_info, link);
1340
1341                 if (mci->dev == dev)
1342                         return mci;
1343         }
1344
1345         return NULL;
1346 }
1347
1348 /* Return 0 on success, 1 on failure.
1349  * Before calling this function, caller must
1350  * assign a unique value to mci->mc_idx.
1351  */
1352 static int add_mc_to_global_list (struct mem_ctl_info *mci)
1353 {
1354         struct list_head *item, *insert_before;
1355         struct mem_ctl_info *p;
1356
1357         insert_before = &mc_devices;
1358
1359         if (unlikely((p = find_mci_by_dev(mci->dev)) != NULL))
1360                 goto fail0;
1361
1362         list_for_each(item, &mc_devices) {
1363                 p = list_entry(item, struct mem_ctl_info, link);
1364
1365                 if (p->mc_idx >= mci->mc_idx) {
1366                         if (unlikely(p->mc_idx == mci->mc_idx))
1367                                 goto fail1;
1368
1369                         insert_before = item;
1370                         break;
1371                 }
1372         }
1373
1374         list_add_tail_rcu(&mci->link, insert_before);
1375         return 0;
1376
1377 fail0:
1378         edac_printk(KERN_WARNING, EDAC_MC,
1379                     "%s (%s) %s %s already assigned %d\n", p->dev->bus_id,
1380                     dev_name(p->dev), p->mod_name, p->ctl_name, p->mc_idx);
1381         return 1;
1382
1383 fail1:
1384         edac_printk(KERN_WARNING, EDAC_MC,
1385                     "bug in low-level driver: attempt to assign\n"
1386                     "    duplicate mc_idx %d in %s()\n", p->mc_idx, __func__);
1387         return 1;
1388 }
1389
1390 static void complete_mc_list_del(struct rcu_head *head)
1391 {
1392         struct mem_ctl_info *mci;
1393
1394         mci = container_of(head, struct mem_ctl_info, rcu);
1395         INIT_LIST_HEAD(&mci->link);
1396         complete(&mci->complete);
1397 }
1398
1399 static void del_mc_from_global_list(struct mem_ctl_info *mci)
1400 {
1401         list_del_rcu(&mci->link);
1402         init_completion(&mci->complete);
1403         call_rcu(&mci->rcu, complete_mc_list_del);
1404         wait_for_completion(&mci->complete);
1405 }
1406
1407 /**
1408  * edac_mc_find: Search for a mem_ctl_info structure whose index is 'idx'.
1409  *
1410  * If found, return a pointer to the structure.
1411  * Else return NULL.
1412  *
1413  * Caller must hold mem_ctls_mutex.
1414  */
1415 struct mem_ctl_info * edac_mc_find(int idx)
1416 {
1417         struct list_head *item;
1418         struct mem_ctl_info *mci;
1419
1420         list_for_each(item, &mc_devices) {
1421                 mci = list_entry(item, struct mem_ctl_info, link);
1422
1423                 if (mci->mc_idx >= idx) {
1424                         if (mci->mc_idx == idx)
1425                                 return mci;
1426
1427                         break;
1428                 }
1429         }
1430
1431         return NULL;
1432 }
1433 EXPORT_SYMBOL(edac_mc_find);
1434
1435 /**
1436  * edac_mc_add_mc: Insert the 'mci' structure into the mci global list and
1437  *                 create sysfs entries associated with mci structure
1438  * @mci: pointer to the mci structure to be added to the list
1439  * @mc_idx: A unique numeric identifier to be assigned to the 'mci' structure.
1440  *
1441  * Return:
1442  *      0       Success
1443  *      !0      Failure
1444  */
1445
1446 /* FIXME - should a warning be printed if no error detection? correction? */
1447 int edac_mc_add_mc(struct mem_ctl_info *mci, int mc_idx)
1448 {
1449         debugf0("%s()\n", __func__);
1450         mci->mc_idx = mc_idx;
1451 #ifdef CONFIG_EDAC_DEBUG
1452         if (edac_debug_level >= 3)
1453                 edac_mc_dump_mci(mci);
1454
1455         if (edac_debug_level >= 4) {
1456                 int i;
1457
1458                 for (i = 0; i < mci->nr_csrows; i++) {
1459                         int j;
1460
1461                         edac_mc_dump_csrow(&mci->csrows[i]);
1462                         for (j = 0; j < mci->csrows[i].nr_channels; j++)
1463                                 edac_mc_dump_channel(
1464                                         &mci->csrows[i].channels[j]);
1465                 }
1466         }
1467 #endif
1468         down(&mem_ctls_mutex);
1469
1470         if (add_mc_to_global_list(mci))
1471                 goto fail0;
1472
1473         /* set load time so that error rate can be tracked */
1474         mci->start_time = jiffies;
1475
1476         if (edac_create_sysfs_mci_device(mci)) {
1477                 edac_mc_printk(mci, KERN_WARNING,
1478                         "failed to create sysfs device\n");
1479                 goto fail1;
1480         }
1481
1482         /* Report action taken */
1483         edac_mc_printk(mci, KERN_INFO, "Giving out device to %s %s: DEV %s\n",
1484                 mci->mod_name, mci->ctl_name, dev_name(mci->dev));
1485
1486         up(&mem_ctls_mutex);
1487         return 0;
1488
1489 fail1:
1490         del_mc_from_global_list(mci);
1491
1492 fail0:
1493         up(&mem_ctls_mutex);
1494         return 1;
1495 }
1496 EXPORT_SYMBOL_GPL(edac_mc_add_mc);
1497
1498 /**
1499  * edac_mc_del_mc: Remove sysfs entries for specified mci structure and
1500  *                 remove mci structure from global list
1501  * @pdev: Pointer to 'struct device' representing mci structure to remove.
1502  *
1503  * Return pointer to removed mci structure, or NULL if device not found.
1504  */
1505 struct mem_ctl_info * edac_mc_del_mc(struct device *dev)
1506 {
1507         struct mem_ctl_info *mci;
1508
1509         debugf0("MC: %s()\n", __func__);
1510         down(&mem_ctls_mutex);
1511
1512         if ((mci = find_mci_by_dev(dev)) == NULL) {
1513                 up(&mem_ctls_mutex);
1514                 return NULL;
1515         }
1516
1517         edac_remove_sysfs_mci_device(mci);
1518         del_mc_from_global_list(mci);
1519         up(&mem_ctls_mutex);
1520         edac_printk(KERN_INFO, EDAC_MC,
1521                 "Removed device %d for %s %s: DEV %s\n", mci->mc_idx,
1522                 mci->mod_name, mci->ctl_name, dev_name(mci->dev));
1523         return mci;
1524 }
1525 EXPORT_SYMBOL_GPL(edac_mc_del_mc);
1526
1527 void edac_mc_scrub_block(unsigned long page, unsigned long offset, u32 size)
1528 {
1529         struct page *pg;
1530         void *virt_addr;
1531         unsigned long flags = 0;
1532
1533         debugf3("%s()\n", __func__);
1534
1535         /* ECC error page was not in our memory. Ignore it. */
1536         if(!pfn_valid(page))
1537                 return;
1538
1539         /* Find the actual page structure then map it and fix */
1540         pg = pfn_to_page(page);
1541
1542         if (PageHighMem(pg))
1543                 local_irq_save(flags);
1544
1545         virt_addr = kmap_atomic(pg, KM_BOUNCE_READ);
1546
1547         /* Perform architecture specific atomic scrub operation */
1548         atomic_scrub(virt_addr + offset, size);
1549
1550         /* Unmap and complete */
1551         kunmap_atomic(virt_addr, KM_BOUNCE_READ);
1552
1553         if (PageHighMem(pg))
1554                 local_irq_restore(flags);
1555 }
1556 EXPORT_SYMBOL_GPL(edac_mc_scrub_block);
1557
1558 /* FIXME - should return -1 */
1559 int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, unsigned long page)
1560 {
1561         struct csrow_info *csrows = mci->csrows;
1562         int row, i;
1563
1564         debugf1("MC%d: %s(): 0x%lx\n", mci->mc_idx, __func__, page);
1565         row = -1;
1566
1567         for (i = 0; i < mci->nr_csrows; i++) {
1568                 struct csrow_info *csrow = &csrows[i];
1569
1570                 if (csrow->nr_pages == 0)
1571                         continue;
1572
1573                 debugf3("MC%d: %s(): first(0x%lx) page(0x%lx) last(0x%lx) "
1574                         "mask(0x%lx)\n", mci->mc_idx, __func__,
1575                         csrow->first_page, page, csrow->last_page,
1576                         csrow->page_mask);
1577
1578                 if ((page >= csrow->first_page) &&
1579                     (page <= csrow->last_page) &&
1580                     ((page & csrow->page_mask) ==
1581                      (csrow->first_page & csrow->page_mask))) {
1582                         row = i;
1583                         break;
1584                 }
1585         }
1586
1587         if (row == -1)
1588                 edac_mc_printk(mci, KERN_ERR,
1589                         "could not look up page error address %lx\n",
1590                         (unsigned long) page);
1591
1592         return row;
1593 }
1594 EXPORT_SYMBOL_GPL(edac_mc_find_csrow_by_page);
1595
1596 /* FIXME - setable log (warning/emerg) levels */
1597 /* FIXME - integrate with evlog: http://evlog.sourceforge.net/ */
1598 void edac_mc_handle_ce(struct mem_ctl_info *mci,
1599                 unsigned long page_frame_number, unsigned long offset_in_page,
1600                 unsigned long syndrome, int row, int channel, const char *msg)
1601 {
1602         unsigned long remapped_page;
1603
1604         debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
1605
1606         /* FIXME - maybe make panic on INTERNAL ERROR an option */
1607         if (row >= mci->nr_csrows || row < 0) {
1608                 /* something is wrong */
1609                 edac_mc_printk(mci, KERN_ERR,
1610                         "INTERNAL ERROR: row out of range "
1611                         "(%d >= %d)\n", row, mci->nr_csrows);
1612                 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1613                 return;
1614         }
1615
1616         if (channel >= mci->csrows[row].nr_channels || channel < 0) {
1617                 /* something is wrong */
1618                 edac_mc_printk(mci, KERN_ERR,
1619                         "INTERNAL ERROR: channel out of range "
1620                         "(%d >= %d)\n", channel,
1621                         mci->csrows[row].nr_channels);
1622                 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1623                 return;
1624         }
1625
1626         if (log_ce)
1627                 /* FIXME - put in DIMM location */
1628                 edac_mc_printk(mci, KERN_WARNING,
1629                         "CE page 0x%lx, offset 0x%lx, grain %d, syndrome "
1630                         "0x%lx, row %d, channel %d, label \"%s\": %s\n",
1631                         page_frame_number, offset_in_page,
1632                         mci->csrows[row].grain, syndrome, row, channel,
1633                         mci->csrows[row].channels[channel].label, msg);
1634
1635         mci->ce_count++;
1636         mci->csrows[row].ce_count++;
1637         mci->csrows[row].channels[channel].ce_count++;
1638
1639         if (mci->scrub_mode & SCRUB_SW_SRC) {
1640                 /*
1641                  * Some MC's can remap memory so that it is still available
1642                  * at a different address when PCI devices map into memory.
1643                  * MC's that can't do this lose the memory where PCI devices
1644                  * are mapped.  This mapping is MC dependant and so we call
1645                  * back into the MC driver for it to map the MC page to
1646                  * a physical (CPU) page which can then be mapped to a virtual
1647                  * page - which can then be scrubbed.
1648                  */
1649                 remapped_page = mci->ctl_page_to_phys ?
1650                     mci->ctl_page_to_phys(mci, page_frame_number) :
1651                     page_frame_number;
1652
1653                 edac_mc_scrub_block(remapped_page, offset_in_page,
1654                                         mci->csrows[row].grain);
1655         }
1656 }
1657 EXPORT_SYMBOL_GPL(edac_mc_handle_ce);
1658
1659 void edac_mc_handle_ce_no_info(struct mem_ctl_info *mci, const char *msg)
1660 {
1661         if (log_ce)
1662                 edac_mc_printk(mci, KERN_WARNING,
1663                         "CE - no information available: %s\n", msg);
1664
1665         mci->ce_noinfo_count++;
1666         mci->ce_count++;
1667 }
1668 EXPORT_SYMBOL_GPL(edac_mc_handle_ce_no_info);
1669
1670 void edac_mc_handle_ue(struct mem_ctl_info *mci,
1671                 unsigned long page_frame_number, unsigned long offset_in_page,
1672                 int row, const char *msg)
1673 {
1674         int len = EDAC_MC_LABEL_LEN * 4;
1675         char labels[len + 1];
1676         char *pos = labels;
1677         int chan;
1678         int chars;
1679
1680         debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
1681
1682         /* FIXME - maybe make panic on INTERNAL ERROR an option */
1683         if (row >= mci->nr_csrows || row < 0) {
1684                 /* something is wrong */
1685                 edac_mc_printk(mci, KERN_ERR,
1686                         "INTERNAL ERROR: row out of range "
1687                         "(%d >= %d)\n", row, mci->nr_csrows);
1688                 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
1689                 return;
1690         }
1691
1692         chars = snprintf(pos, len + 1, "%s",
1693                         mci->csrows[row].channels[0].label);
1694         len -= chars;
1695         pos += chars;
1696
1697         for (chan = 1; (chan < mci->csrows[row].nr_channels) && (len > 0);
1698              chan++) {
1699                 chars = snprintf(pos, len + 1, ":%s",
1700                                 mci->csrows[row].channels[chan].label);
1701                 len -= chars;
1702                 pos += chars;
1703         }
1704
1705         if (log_ue)
1706                 edac_mc_printk(mci, KERN_EMERG,
1707                         "UE page 0x%lx, offset 0x%lx, grain %d, row %d, "
1708                         "labels \"%s\": %s\n", page_frame_number,
1709                         offset_in_page, mci->csrows[row].grain, row, labels,
1710                         msg);
1711
1712         if (panic_on_ue)
1713                 panic("EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, "
1714                         "row %d, labels \"%s\": %s\n", mci->mc_idx,
1715                         page_frame_number, offset_in_page,
1716                         mci->csrows[row].grain, row, labels, msg);
1717
1718         mci->ue_count++;
1719         mci->csrows[row].ue_count++;
1720 }
1721 EXPORT_SYMBOL_GPL(edac_mc_handle_ue);
1722
1723 void edac_mc_handle_ue_no_info(struct mem_ctl_info *mci, const char *msg)
1724 {
1725         if (panic_on_ue)
1726                 panic("EDAC MC%d: Uncorrected Error", mci->mc_idx);
1727
1728         if (log_ue)
1729                 edac_mc_printk(mci, KERN_WARNING,
1730                         "UE - no information available: %s\n", msg);
1731         mci->ue_noinfo_count++;
1732         mci->ue_count++;
1733 }
1734 EXPORT_SYMBOL_GPL(edac_mc_handle_ue_no_info);
1735
1736
1737 /*
1738  * Iterate over all MC instances and check for ECC, et al, errors
1739  */
1740 static inline void check_mc_devices(void)
1741 {
1742         struct list_head *item;
1743         struct mem_ctl_info *mci;
1744
1745         debugf3("%s()\n", __func__);
1746         down(&mem_ctls_mutex);
1747
1748         list_for_each(item, &mc_devices) {
1749                 mci = list_entry(item, struct mem_ctl_info, link);
1750
1751                 if (mci->edac_check != NULL)
1752                         mci->edac_check(mci);
1753         }
1754
1755         up(&mem_ctls_mutex);
1756 }
1757
1758 /*
1759  * Check MC status every poll_msec.
1760  * Check PCI status every poll_msec as well.
1761  *
1762  * This where the work gets done for edac.
1763  *
1764  * SMP safe, doesn't use NMI, and auto-rate-limits.
1765  */
1766 static void do_edac_check(void)
1767 {
1768         debugf3("%s()\n", __func__);
1769         check_mc_devices();
1770         do_pci_parity_check();
1771 }
1772
1773 static int edac_kernel_thread(void *arg)
1774 {
1775         while (!kthread_should_stop()) {
1776                 do_edac_check();
1777
1778                 /* goto sleep for the interval */
1779                 schedule_timeout_interruptible((HZ * poll_msec) / 1000);
1780                 try_to_freeze();
1781         }
1782
1783         return 0;
1784 }
1785
1786 /*
1787  * edac_mc_init
1788  *      module initialization entry point
1789  */
1790 static int __init edac_mc_init(void)
1791 {
1792         edac_printk(KERN_INFO, EDAC_MC, EDAC_MC_VERSION "\n");
1793
1794         /*
1795          * Harvest and clear any boot/initialization PCI parity errors
1796          *
1797          * FIXME: This only clears errors logged by devices present at time of
1798          *      module initialization.  We should also do an initial clear
1799          *      of each newly hotplugged device.
1800          */
1801         clear_pci_parity_errors();
1802
1803         /* Create the MC sysfs entries */
1804         if (edac_sysfs_memctrl_setup()) {
1805                 edac_printk(KERN_ERR, EDAC_MC,
1806                         "Error initializing sysfs code\n");
1807                 return -ENODEV;
1808         }
1809
1810         /* Create the PCI parity sysfs entries */
1811         if (edac_sysfs_pci_setup()) {
1812                 edac_sysfs_memctrl_teardown();
1813                 edac_printk(KERN_ERR, EDAC_MC,
1814                         "EDAC PCI: Error initializing sysfs code\n");
1815                 return -ENODEV;
1816         }
1817
1818         /* create our kernel thread */
1819         edac_thread = kthread_run(edac_kernel_thread, NULL, "kedac");
1820
1821         if (IS_ERR(edac_thread)) {
1822                 /* remove the sysfs entries */
1823                 edac_sysfs_memctrl_teardown();
1824                 edac_sysfs_pci_teardown();
1825                 return PTR_ERR(edac_thread);
1826         }
1827
1828         return 0;
1829 }
1830
1831 /*
1832  * edac_mc_exit()
1833  *      module exit/termination functioni
1834  */
1835 static void __exit edac_mc_exit(void)
1836 {
1837         debugf0("%s()\n", __func__);
1838         kthread_stop(edac_thread);
1839
1840         /* tear down the sysfs device */
1841         edac_sysfs_memctrl_teardown();
1842         edac_sysfs_pci_teardown();
1843 }
1844
1845 module_init(edac_mc_init);
1846 module_exit(edac_mc_exit);
1847
1848 MODULE_LICENSE("GPL");
1849 MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh et al\n"
1850         "Based on work by Dan Hollis et al");
1851 MODULE_DESCRIPTION("Core library routines for MC reporting");
1852
1853 module_param(panic_on_ue, int, 0644);
1854 MODULE_PARM_DESC(panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
1855 #ifdef CONFIG_PCI
1856 module_param(check_pci_parity, int, 0644);
1857 MODULE_PARM_DESC(check_pci_parity, "Check for PCI bus parity errors: 0=off 1=on");
1858 module_param(panic_on_pci_parity, int, 0644);
1859 MODULE_PARM_DESC(panic_on_pci_parity, "Panic on PCI Bus Parity error: 0=off 1=on");
1860 #endif
1861 module_param(log_ue, int, 0644);
1862 MODULE_PARM_DESC(log_ue, "Log uncorrectable error to console: 0=off 1=on");
1863 module_param(log_ce, int, 0644);
1864 MODULE_PARM_DESC(log_ce, "Log correctable error to console: 0=off 1=on");
1865 module_param(poll_msec, int, 0644);
1866 MODULE_PARM_DESC(poll_msec, "Polling period in milliseconds");
1867 #ifdef CONFIG_EDAC_DEBUG
1868 module_param(edac_debug_level, int, 0644);
1869 MODULE_PARM_DESC(edac_debug_level, "Debug level");
1870 #endif