Merge branch 'for-jeff' of git://electric-eye.fr.zoreil.com/home/romieu/linux-2.6
[sfrench/cifs-2.6.git] / drivers / edac / edac_mc.c
1 /*
2  * edac_mc kernel module
3  * (C) 2005 Linux Networx (http://lnxi.com)
4  * This file may be distributed under the terms of the
5  * GNU General Public License.
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
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/proc_fs.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/smp.h>
22 #include <linux/init.h>
23 #include <linux/sysctl.h>
24 #include <linux/highmem.h>
25 #include <linux/timer.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/sysdev.h>
31 #include <linux/ctype.h>
32
33 #include <asm/uaccess.h>
34 #include <asm/page.h>
35 #include <asm/edac.h>
36
37 #include "edac_mc.h"
38
39 #define EDAC_MC_VERSION "edac_mc  Ver: 2.0.0 " __DATE__
40
41 #ifdef CONFIG_EDAC_DEBUG
42 /* Values of 0 to 4 will generate output */
43 int edac_debug_level = 1;
44 EXPORT_SYMBOL(edac_debug_level);
45 #endif
46
47 /* EDAC Controls, setable by module parameter, and sysfs */
48 static int log_ue = 1;
49 static int log_ce = 1;
50 static int panic_on_ue = 1;
51 static int poll_msec = 1000;
52
53 static int check_pci_parity = 0;        /* default YES check PCI parity */
54 static int panic_on_pci_parity;         /* default no panic on PCI Parity */
55 static atomic_t pci_parity_count = ATOMIC_INIT(0);
56
57 /* lock to memory controller's control array */
58 static DECLARE_MUTEX(mem_ctls_mutex);
59 static struct list_head mc_devices = LIST_HEAD_INIT(mc_devices);
60
61 /* Structure of the whitelist and blacklist arrays */
62 struct edac_pci_device_list {
63         unsigned int  vendor;           /* Vendor ID */
64         unsigned int  device;           /* Deviice ID */
65 };
66
67
68 #define MAX_LISTED_PCI_DEVICES          32
69
70 /* List of PCI devices (vendor-id:device-id) that should be skipped */
71 static struct edac_pci_device_list pci_blacklist[MAX_LISTED_PCI_DEVICES];
72 static int pci_blacklist_count;
73
74 /* List of PCI devices (vendor-id:device-id) that should be scanned */
75 static struct edac_pci_device_list pci_whitelist[MAX_LISTED_PCI_DEVICES];
76 static int pci_whitelist_count ;
77
78 /*  START sysfs data and methods */
79
80 static const char *mem_types[] = {
81         [MEM_EMPTY] = "Empty",
82         [MEM_RESERVED] = "Reserved",
83         [MEM_UNKNOWN] = "Unknown",
84         [MEM_FPM] = "FPM",
85         [MEM_EDO] = "EDO",
86         [MEM_BEDO] = "BEDO",
87         [MEM_SDR] = "Unbuffered-SDR",
88         [MEM_RDR] = "Registered-SDR",
89         [MEM_DDR] = "Unbuffered-DDR",
90         [MEM_RDDR] = "Registered-DDR",
91         [MEM_RMBS] = "RMBS"
92 };
93
94 static const char *dev_types[] = {
95         [DEV_UNKNOWN] = "Unknown",
96         [DEV_X1] = "x1",
97         [DEV_X2] = "x2",
98         [DEV_X4] = "x4",
99         [DEV_X8] = "x8",
100         [DEV_X16] = "x16",
101         [DEV_X32] = "x32",
102         [DEV_X64] = "x64"
103 };
104
105 static const char *edac_caps[] = {
106         [EDAC_UNKNOWN] = "Unknown",
107         [EDAC_NONE] = "None",
108         [EDAC_RESERVED] = "Reserved",
109         [EDAC_PARITY] = "PARITY",
110         [EDAC_EC] = "EC",
111         [EDAC_SECDED] = "SECDED",
112         [EDAC_S2ECD2ED] = "S2ECD2ED",
113         [EDAC_S4ECD4ED] = "S4ECD4ED",
114         [EDAC_S8ECD8ED] = "S8ECD8ED",
115         [EDAC_S16ECD16ED] = "S16ECD16ED"
116 };
117
118
119 /* sysfs object: /sys/devices/system/edac */
120 static struct sysdev_class edac_class = {
121         set_kset_name("edac"),
122 };
123
124 /* sysfs objects:
125  *      /sys/devices/system/edac/mc
126  *      /sys/devices/system/edac/pci
127  */
128 static struct kobject edac_memctrl_kobj;
129 static struct kobject edac_pci_kobj;
130
131 /*
132  * /sys/devices/system/edac/mc;
133  *      data structures and methods
134  */
135 static ssize_t memctrl_string_show(void *ptr, char *buffer)
136 {
137         char *value = (char*) ptr;
138         return sprintf(buffer, "%s\n", value);
139 }
140
141 static ssize_t memctrl_int_show(void *ptr, char *buffer)
142 {
143         int *value = (int*) ptr;
144         return sprintf(buffer, "%d\n", *value);
145 }
146
147 static ssize_t memctrl_int_store(void *ptr, const char *buffer, size_t count)
148 {
149         int *value = (int*) ptr;
150
151         if (isdigit(*buffer))
152                 *value = simple_strtoul(buffer, NULL, 0);
153
154         return count;
155 }
156
157 struct memctrl_dev_attribute {
158         struct attribute        attr;
159         void    *value;
160         ssize_t (*show)(void *,char *);
161         ssize_t (*store)(void *, const char *, size_t);
162 };
163
164 /* Set of show/store abstract level functions for memory control object */
165 static ssize_t
166 memctrl_dev_show(struct kobject *kobj, struct attribute *attr, char *buffer)
167 {
168         struct memctrl_dev_attribute *memctrl_dev;
169         memctrl_dev = (struct memctrl_dev_attribute*)attr;
170
171         if (memctrl_dev->show)
172                 return memctrl_dev->show(memctrl_dev->value, buffer);
173         return -EIO;
174 }
175
176 static ssize_t
177 memctrl_dev_store(struct kobject *kobj, struct attribute *attr,
178                         const char *buffer, size_t count)
179 {
180         struct memctrl_dev_attribute *memctrl_dev;
181         memctrl_dev = (struct memctrl_dev_attribute*)attr;
182
183         if (memctrl_dev->store)
184                 return memctrl_dev->store(memctrl_dev->value, buffer, count);
185         return -EIO;
186 }
187
188 static struct sysfs_ops memctrlfs_ops = {
189         .show   = memctrl_dev_show,
190         .store  = memctrl_dev_store
191 };
192
193 #define MEMCTRL_ATTR(_name,_mode,_show,_store)                  \
194 struct memctrl_dev_attribute attr_##_name = {                   \
195         .attr = {.name = __stringify(_name), .mode = _mode },   \
196         .value  = &_name,                                       \
197         .show   = _show,                                        \
198         .store  = _store,                                       \
199 };
200
201 #define MEMCTRL_STRING_ATTR(_name,_data,_mode,_show,_store)     \
202 struct memctrl_dev_attribute attr_##_name = {                   \
203         .attr = {.name = __stringify(_name), .mode = _mode },   \
204         .value  = _data,                                        \
205         .show   = _show,                                        \
206         .store  = _store,                                       \
207 };
208
209 /* cwrow<id> attribute f*/
210 MEMCTRL_STRING_ATTR(mc_version,EDAC_MC_VERSION,S_IRUGO,memctrl_string_show,NULL);
211
212 /* csrow<id> control files */
213 MEMCTRL_ATTR(panic_on_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
214 MEMCTRL_ATTR(log_ue,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
215 MEMCTRL_ATTR(log_ce,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
216 MEMCTRL_ATTR(poll_msec,S_IRUGO|S_IWUSR,memctrl_int_show,memctrl_int_store);
217
218
219 /* Base Attributes of the memory ECC object */
220 static struct memctrl_dev_attribute *memctrl_attr[] = {
221         &attr_panic_on_ue,
222         &attr_log_ue,
223         &attr_log_ce,
224         &attr_poll_msec,
225         &attr_mc_version,
226         NULL,
227 };
228
229 /* Main MC kobject release() function */
230 static void edac_memctrl_master_release(struct kobject *kobj)
231 {
232         debugf1("EDAC MC: " __FILE__ ": %s()\n", __func__);
233 }
234
235 static struct kobj_type ktype_memctrl = {
236         .release        = edac_memctrl_master_release,
237         .sysfs_ops      = &memctrlfs_ops,
238         .default_attrs  = (struct attribute **) memctrl_attr,
239 };
240
241
242 /* Initialize the main sysfs entries for edac:
243  *   /sys/devices/system/edac
244  *
245  * and children
246  *
247  * Return:  0 SUCCESS
248  *         !0 FAILURE
249  */
250 static int edac_sysfs_memctrl_setup(void)
251 {
252         int err=0;
253
254         debugf1("MC: " __FILE__ ": %s()\n", __func__);
255
256         /* create the /sys/devices/system/edac directory */
257         err = sysdev_class_register(&edac_class);
258         if (!err) {
259                 /* Init the MC's kobject */
260                 memset(&edac_memctrl_kobj, 0, sizeof (edac_memctrl_kobj));
261                 kobject_init(&edac_memctrl_kobj);
262
263                 edac_memctrl_kobj.parent = &edac_class.kset.kobj;
264                 edac_memctrl_kobj.ktype = &ktype_memctrl;
265
266                 /* generate sysfs "..../edac/mc"   */
267                 err = kobject_set_name(&edac_memctrl_kobj,"mc");
268                 if (!err) {
269                         /* FIXME: maybe new sysdev_create_subdir() */
270                         err = kobject_register(&edac_memctrl_kobj);
271                         if (err) {
272                                 debugf1("Failed to register '.../edac/mc'\n");
273                         } else {
274                                 debugf1("Registered '.../edac/mc' kobject\n");
275                         }
276                 }
277         } else {
278                 debugf1(KERN_WARNING "__FILE__ %s() error=%d\n", __func__,err);
279         }
280
281         return err;
282 }
283
284 /*
285  * MC teardown:
286  *      the '..../edac/mc' kobject followed by '..../edac' itself
287  */
288 static void edac_sysfs_memctrl_teardown(void)
289 {
290         debugf0("MC: " __FILE__ ": %s()\n", __func__);
291
292         /* Unregister the MC's kobject */
293         kobject_unregister(&edac_memctrl_kobj);
294
295         /* release the master edac mc kobject */
296         kobject_put(&edac_memctrl_kobj);
297
298         /* Unregister the 'edac' object */
299         sysdev_class_unregister(&edac_class);
300 }
301
302 /*
303  * /sys/devices/system/edac/pci;
304  *      data structures and methods
305  */
306
307 struct list_control {
308         struct edac_pci_device_list *list;
309         int *count;
310 };
311
312 /* Output the list as:  vendor_id:device:id<,vendor_id:device_id> */
313 static ssize_t edac_pci_list_string_show(void *ptr, char *buffer)
314 {
315         struct list_control *listctl;
316         struct edac_pci_device_list *list;
317         char *p = buffer;
318         int len=0;
319         int i;
320
321         listctl = ptr;
322         list = listctl->list;
323
324         for (i = 0; i < *(listctl->count); i++, list++ ) {
325                 if (len > 0)
326                         len += snprintf(p + len, (PAGE_SIZE-len), ",");
327
328                 len += snprintf(p + len,
329                                 (PAGE_SIZE-len),
330                                 "%x:%x",
331                                 list->vendor,list->device);
332         }
333
334         len += snprintf(p + len,(PAGE_SIZE-len), "\n");
335
336         return (ssize_t) len;
337 }
338
339 /**
340  *
341  * Scan string from **s to **e looking for one 'vendor:device' tuple
342  * where each field is a hex value
343  *
344  * return 0 if an entry is NOT found
345  * return 1 if an entry is found
346  *      fill in *vendor_id and *device_id with values found
347  *
348  * In both cases, make sure *s has been moved forward toward *e
349  */
350 static int parse_one_device(const char **s,const char **e,
351         unsigned int *vendor_id, unsigned int *device_id)
352 {
353         const char *runner, *p;
354
355         /* if null byte, we are done */
356         if (!**s) {
357                 (*s)++; /* keep *s moving */
358                 return 0;
359         }
360
361         /* skip over newlines & whitespace */
362         if ((**s == '\n') || isspace(**s)) {
363                 (*s)++;
364                 return 0;
365         }
366
367         if (!isxdigit(**s)) {
368                 (*s)++;
369                 return 0;
370         }
371
372         /* parse vendor_id */
373         runner = *s;
374         while (runner < *e) {
375                 /* scan for vendor:device delimiter */
376                 if (*runner == ':') {
377                         *vendor_id = simple_strtol((char*) *s, (char**) &p, 16);
378                         runner = p + 1;
379                         break;
380                 }
381                 runner++;
382         }
383
384         if (!isxdigit(*runner)) {
385                 *s = ++runner;
386                 return 0;
387         }
388
389         /* parse device_id */
390         if (runner < *e) {
391                 *device_id = simple_strtol((char*)runner, (char**)&p, 16);
392                 runner = p;
393         }
394
395         *s = runner;
396
397         return 1;
398 }
399
400 static ssize_t edac_pci_list_string_store(void *ptr, const char *buffer,
401                                         size_t count)
402 {
403         struct list_control *listctl;
404         struct edac_pci_device_list *list;
405         unsigned int vendor_id, device_id;
406         const char *s, *e;
407         int *index;
408
409         s = (char*)buffer;
410         e = s + count;
411
412         listctl = ptr;
413         list = listctl->list;
414         index = listctl->count;
415
416         *index = 0;
417         while (*index < MAX_LISTED_PCI_DEVICES) {
418
419                 if (parse_one_device(&s,&e,&vendor_id,&device_id)) {
420                         list[ *index ].vendor = vendor_id;
421                         list[ *index ].device = device_id;
422                         (*index)++;
423                 }
424
425                 /* check for all data consume */
426                 if (s >= e)
427                         break;
428         }
429
430         return count;
431 }
432
433 static ssize_t edac_pci_int_show(void *ptr, char *buffer)
434 {
435         int *value = ptr;
436         return sprintf(buffer,"%d\n",*value);
437 }
438
439 static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
440 {
441         int *value = ptr;
442
443         if (isdigit(*buffer))
444                 *value = simple_strtoul(buffer,NULL,0);
445
446         return count;
447 }
448
449 struct edac_pci_dev_attribute {
450         struct attribute        attr;
451         void    *value;
452         ssize_t (*show)(void *,char *);
453         ssize_t (*store)(void *, const char *,size_t);
454 };
455
456 /* Set of show/store abstract level functions for PCI Parity object */
457 static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
458                                 char *buffer)
459 {
460         struct edac_pci_dev_attribute *edac_pci_dev;
461         edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
462
463         if (edac_pci_dev->show)
464                 return edac_pci_dev->show(edac_pci_dev->value, buffer);
465         return -EIO;
466 }
467
468 static ssize_t edac_pci_dev_store(struct kobject *kobj, struct attribute *attr,
469                                 const char *buffer, size_t count)
470 {
471         struct edac_pci_dev_attribute *edac_pci_dev;
472         edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
473
474         if (edac_pci_dev->show)
475                 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
476         return -EIO;
477 }
478
479 static struct sysfs_ops edac_pci_sysfs_ops = {
480         .show   = edac_pci_dev_show,
481         .store  = edac_pci_dev_store
482 };
483
484
485 #define EDAC_PCI_ATTR(_name,_mode,_show,_store)                 \
486 struct edac_pci_dev_attribute edac_pci_attr_##_name = {         \
487         .attr = {.name = __stringify(_name), .mode = _mode },   \
488         .value  = &_name,                                       \
489         .show   = _show,                                        \
490         .store  = _store,                                       \
491 };
492
493 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store)    \
494 struct edac_pci_dev_attribute edac_pci_attr_##_name = {         \
495         .attr = {.name = __stringify(_name), .mode = _mode },   \
496         .value  = _data,                                        \
497         .show   = _show,                                        \
498         .store  = _store,                                       \
499 };
500
501 static struct list_control pci_whitelist_control = {
502         .list = pci_whitelist,
503         .count = &pci_whitelist_count
504 };
505
506 static struct list_control pci_blacklist_control = {
507         .list = pci_blacklist,
508         .count = &pci_blacklist_count
509 };
510
511 /* whitelist attribute */
512 EDAC_PCI_STRING_ATTR(pci_parity_whitelist,
513         &pci_whitelist_control,
514         S_IRUGO|S_IWUSR,
515         edac_pci_list_string_show,
516         edac_pci_list_string_store);
517
518 EDAC_PCI_STRING_ATTR(pci_parity_blacklist,
519         &pci_blacklist_control,
520         S_IRUGO|S_IWUSR,
521         edac_pci_list_string_show,
522         edac_pci_list_string_store);
523
524 /* PCI Parity control files */
525 EDAC_PCI_ATTR(check_pci_parity,S_IRUGO|S_IWUSR,edac_pci_int_show,edac_pci_int_store);
526 EDAC_PCI_ATTR(panic_on_pci_parity,S_IRUGO|S_IWUSR,edac_pci_int_show,edac_pci_int_store);
527 EDAC_PCI_ATTR(pci_parity_count,S_IRUGO,edac_pci_int_show,NULL);
528
529 /* Base Attributes of the memory ECC object */
530 static struct edac_pci_dev_attribute *edac_pci_attr[] = {
531         &edac_pci_attr_check_pci_parity,
532         &edac_pci_attr_panic_on_pci_parity,
533         &edac_pci_attr_pci_parity_count,
534         &edac_pci_attr_pci_parity_whitelist,
535         &edac_pci_attr_pci_parity_blacklist,
536         NULL,
537 };
538
539 /* No memory to release */
540 static void edac_pci_release(struct kobject *kobj)
541 {
542         debugf1("EDAC PCI: " __FILE__ ": %s()\n", __func__);
543 }
544
545 static struct kobj_type ktype_edac_pci = {
546         .release        = edac_pci_release,
547         .sysfs_ops      = &edac_pci_sysfs_ops,
548         .default_attrs  = (struct attribute **) edac_pci_attr,
549 };
550
551 /**
552  * edac_sysfs_pci_setup()
553  *
554  */
555 static int edac_sysfs_pci_setup(void)
556 {
557         int err;
558
559         debugf1("MC: " __FILE__ ": %s()\n", __func__);
560
561         memset(&edac_pci_kobj, 0, sizeof(edac_pci_kobj));
562
563         kobject_init(&edac_pci_kobj);
564         edac_pci_kobj.parent = &edac_class.kset.kobj;
565         edac_pci_kobj.ktype = &ktype_edac_pci;
566
567         err = kobject_set_name(&edac_pci_kobj, "pci");
568         if (!err) {
569                 /* Instanstiate the csrow object */
570                 /* FIXME: maybe new sysdev_create_subdir() */
571                 err = kobject_register(&edac_pci_kobj);
572                 if (err)
573                         debugf1("Failed to register '.../edac/pci'\n");
574                 else
575                         debugf1("Registered '.../edac/pci' kobject\n");
576         }
577         return err;
578 }
579
580
581 static void edac_sysfs_pci_teardown(void)
582 {
583         debugf0("MC: " __FILE__ ": %s()\n", __func__);
584
585         kobject_unregister(&edac_pci_kobj);
586         kobject_put(&edac_pci_kobj);
587 }
588
589 /* EDAC sysfs CSROW data structures and methods */
590
591 /* Set of more detailed csrow<id> attribute show/store functions */
592 static ssize_t csrow_ch0_dimm_label_show(struct csrow_info *csrow, char *data)
593 {
594         ssize_t size = 0;
595
596         if (csrow->nr_channels > 0) {
597                 size = snprintf(data, EDAC_MC_LABEL_LEN,"%s\n",
598                         csrow->channels[0].label);
599         }
600         return size;
601 }
602
603 static ssize_t csrow_ch1_dimm_label_show(struct csrow_info *csrow, char *data)
604 {
605         ssize_t size = 0;
606
607         if (csrow->nr_channels > 0) {
608                 size = snprintf(data, EDAC_MC_LABEL_LEN, "%s\n",
609                         csrow->channels[1].label);
610         }
611         return size;
612 }
613
614 static ssize_t csrow_ch0_dimm_label_store(struct csrow_info *csrow,
615                         const char *data, size_t size)
616 {
617         ssize_t max_size = 0;
618
619         if (csrow->nr_channels > 0) {
620                 max_size = min((ssize_t)size,(ssize_t)EDAC_MC_LABEL_LEN-1);
621                 strncpy(csrow->channels[0].label, data, max_size);
622                 csrow->channels[0].label[max_size] = '\0';
623         }
624         return size;
625 }
626
627 static ssize_t csrow_ch1_dimm_label_store(struct csrow_info *csrow,
628                         const char *data, size_t size)
629 {
630         ssize_t max_size = 0;
631
632         if (csrow->nr_channels > 1) {
633                 max_size = min((ssize_t)size,(ssize_t)EDAC_MC_LABEL_LEN-1);
634                 strncpy(csrow->channels[1].label, data, max_size);
635                 csrow->channels[1].label[max_size] = '\0';
636         }
637         return max_size;
638 }
639
640 static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data)
641 {
642         return sprintf(data,"%u\n", csrow->ue_count);
643 }
644
645 static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data)
646 {
647         return sprintf(data,"%u\n", csrow->ce_count);
648 }
649
650 static ssize_t csrow_ch0_ce_count_show(struct csrow_info *csrow, char *data)
651 {
652         ssize_t size = 0;
653
654         if (csrow->nr_channels > 0) {
655                 size = sprintf(data,"%u\n", csrow->channels[0].ce_count);
656         }
657         return size;
658 }
659
660 static ssize_t csrow_ch1_ce_count_show(struct csrow_info *csrow, char *data)
661 {
662         ssize_t size = 0;
663
664         if (csrow->nr_channels > 1) {
665                 size = sprintf(data,"%u\n", csrow->channels[1].ce_count);
666         }
667         return size;
668 }
669
670 static ssize_t csrow_size_show(struct csrow_info *csrow, char *data)
671 {
672         return sprintf(data,"%u\n", PAGES_TO_MiB(csrow->nr_pages));
673 }
674
675 static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data)
676 {
677         return sprintf(data,"%s\n", mem_types[csrow->mtype]);
678 }
679
680 static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data)
681 {
682         return sprintf(data,"%s\n", dev_types[csrow->dtype]);
683 }
684
685 static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data)
686 {
687         return sprintf(data,"%s\n", edac_caps[csrow->edac_mode]);
688 }
689
690 struct csrowdev_attribute {
691         struct attribute        attr;
692         ssize_t (*show)(struct csrow_info *,char *);
693         ssize_t (*store)(struct csrow_info *, const char *,size_t);
694 };
695
696 #define to_csrow(k) container_of(k, struct csrow_info, kobj)
697 #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
698
699 /* Set of show/store higher level functions for csrow objects */
700 static ssize_t csrowdev_show(struct kobject *kobj, struct attribute *attr,
701                                 char *buffer)
702 {
703         struct csrow_info *csrow = to_csrow(kobj);
704         struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
705
706         if (csrowdev_attr->show)
707                 return csrowdev_attr->show(csrow, buffer);
708         return -EIO;
709 }
710
711 static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
712                                 const char *buffer, size_t count)
713 {
714         struct csrow_info *csrow = to_csrow(kobj);
715         struct csrowdev_attribute * csrowdev_attr = to_csrowdev_attr(attr);
716
717         if (csrowdev_attr->store)
718                 return csrowdev_attr->store(csrow, buffer, count);
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)                 \
728 struct csrowdev_attribute attr_##_name = {                      \
729         .attr = {.name = __stringify(_name), .mode = _mode },   \
730         .show   = _show,                                        \
731         .store  = _store,                                       \
732 };
733
734 /* cwrow<id>/attribute files */
735 CSROWDEV_ATTR(size_mb,S_IRUGO,csrow_size_show,NULL);
736 CSROWDEV_ATTR(dev_type,S_IRUGO,csrow_dev_type_show,NULL);
737 CSROWDEV_ATTR(mem_type,S_IRUGO,csrow_mem_type_show,NULL);
738 CSROWDEV_ATTR(edac_mode,S_IRUGO,csrow_edac_mode_show,NULL);
739 CSROWDEV_ATTR(ue_count,S_IRUGO,csrow_ue_count_show,NULL);
740 CSROWDEV_ATTR(ce_count,S_IRUGO,csrow_ce_count_show,NULL);
741 CSROWDEV_ATTR(ch0_ce_count,S_IRUGO,csrow_ch0_ce_count_show,NULL);
742 CSROWDEV_ATTR(ch1_ce_count,S_IRUGO,csrow_ch1_ce_count_show,NULL);
743
744 /* control/attribute files */
745 CSROWDEV_ATTR(ch0_dimm_label,S_IRUGO|S_IWUSR,
746                 csrow_ch0_dimm_label_show,
747                 csrow_ch0_dimm_label_store);
748 CSROWDEV_ATTR(ch1_dimm_label,S_IRUGO|S_IWUSR,
749                 csrow_ch1_dimm_label_show,
750                 csrow_ch1_dimm_label_store);
751
752
753 /* Attributes of the CSROW<id> object */
754 static struct csrowdev_attribute *csrow_attr[] = {
755         &attr_dev_type,
756         &attr_mem_type,
757         &attr_edac_mode,
758         &attr_size_mb,
759         &attr_ue_count,
760         &attr_ce_count,
761         &attr_ch0_ce_count,
762         &attr_ch1_ce_count,
763         &attr_ch0_dimm_label,
764         &attr_ch1_dimm_label,
765         NULL,
766 };
767
768
769 /* No memory to release */
770 static void edac_csrow_instance_release(struct kobject *kobj)
771 {
772         debugf1("EDAC MC: " __FILE__ ": %s()\n", __func__);
773 }
774
775 static struct kobj_type ktype_csrow = {
776         .release        = edac_csrow_instance_release,
777         .sysfs_ops      = &csrowfs_ops,
778         .default_attrs  = (struct attribute **) csrow_attr,
779 };
780
781 /* Create a CSROW object under specifed edac_mc_device */
782 static int edac_create_csrow_object(struct kobject *edac_mci_kobj,
783                                 struct csrow_info *csrow, int index )
784 {
785         int err = 0;
786
787         debugf0("MC: " __FILE__ ": %s()\n", __func__);
788
789         memset(&csrow->kobj, 0, sizeof(csrow->kobj));
790
791         /* generate ..../edac/mc/mc<id>/csrow<index>   */
792
793         kobject_init(&csrow->kobj);
794         csrow->kobj.parent = edac_mci_kobj;
795         csrow->kobj.ktype = &ktype_csrow;
796
797         /* name this instance of csrow<id> */
798         err = kobject_set_name(&csrow->kobj,"csrow%d",index);
799         if (!err) {
800                 /* Instanstiate the csrow object */
801                 err = kobject_register(&csrow->kobj);
802                 if (err)
803                         debugf0("Failed to register CSROW%d\n",index);
804                 else
805                         debugf0("Registered CSROW%d\n",index);
806         }
807
808         return err;
809 }
810
811 /* sysfs data structures and methods for the MCI kobjects */
812
813 static ssize_t mci_reset_counters_store(struct mem_ctl_info  *mci,
814                                         const char *data, size_t count )
815 {
816         int row, chan;
817
818         mci->ue_noinfo_count = 0;
819         mci->ce_noinfo_count = 0;
820         mci->ue_count = 0;
821         mci->ce_count = 0;
822         for (row = 0; row < mci->nr_csrows; row++) {
823                 struct csrow_info *ri = &mci->csrows[row];
824
825                 ri->ue_count = 0;
826                 ri->ce_count = 0;
827                 for (chan = 0; chan < ri->nr_channels; chan++)
828                         ri->channels[chan].ce_count = 0;
829         }
830         mci->start_time = jiffies;
831
832         return count;
833 }
834
835 static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
836 {
837         return sprintf(data,"%d\n", mci->ue_count);
838 }
839
840 static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
841 {
842         return sprintf(data,"%d\n", mci->ce_count);
843 }
844
845 static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
846 {
847         return sprintf(data,"%d\n", mci->ce_noinfo_count);
848 }
849
850 static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
851 {
852         return sprintf(data,"%d\n", mci->ue_noinfo_count);
853 }
854
855 static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
856 {
857         return sprintf(data,"%ld\n", (jiffies - mci->start_time) / HZ);
858 }
859
860 static ssize_t mci_mod_name_show(struct mem_ctl_info *mci, char *data)
861 {
862         return sprintf(data,"%s %s\n", mci->mod_name, mci->mod_ver);
863 }
864
865 static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
866 {
867         return sprintf(data,"%s\n", mci->ctl_name);
868 }
869
870 static int mci_output_edac_cap(char *buf, unsigned long edac_cap)
871 {
872         char *p = buf;
873         int bit_idx;
874
875         for (bit_idx = 0; bit_idx < 8 * sizeof(edac_cap); bit_idx++) {
876                 if ((edac_cap >> bit_idx) & 0x1)
877                         p += sprintf(p, "%s ", edac_caps[bit_idx]);
878         }
879
880         return p - buf;
881 }
882
883 static ssize_t mci_edac_capability_show(struct mem_ctl_info *mci, char *data)
884 {
885         char *p = data;
886
887         p += mci_output_edac_cap(p,mci->edac_ctl_cap);
888         p += sprintf(p, "\n");
889
890         return p - data;
891 }
892
893 static ssize_t mci_edac_current_capability_show(struct mem_ctl_info *mci,
894                                                 char *data)
895 {
896         char *p = data;
897
898         p += mci_output_edac_cap(p,mci->edac_cap);
899         p += sprintf(p, "\n");
900
901         return p - data;
902 }
903
904 static int mci_output_mtype_cap(char *buf, unsigned long mtype_cap)
905 {
906         char *p = buf;
907         int bit_idx;
908
909         for (bit_idx = 0; bit_idx < 8 * sizeof(mtype_cap); bit_idx++) {
910                 if ((mtype_cap >> bit_idx) & 0x1)
911                         p += sprintf(p, "%s ", mem_types[bit_idx]);
912         }
913
914         return p - buf;
915 }
916
917 static ssize_t mci_supported_mem_type_show(struct mem_ctl_info *mci, char *data)
918 {
919         char *p = data;
920
921         p += mci_output_mtype_cap(p,mci->mtype_cap);
922         p += sprintf(p, "\n");
923
924         return p - data;
925 }
926
927 static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
928 {
929         int total_pages, csrow_idx;
930
931         for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
932                         csrow_idx++) {
933                 struct csrow_info *csrow = &mci->csrows[csrow_idx];
934
935                 if (!csrow->nr_pages)
936                         continue;
937                 total_pages += csrow->nr_pages;
938         }
939
940         return sprintf(data,"%u\n", PAGES_TO_MiB(total_pages));
941 }
942
943 struct mcidev_attribute {
944         struct attribute        attr;
945         ssize_t (*show)(struct mem_ctl_info *,char *);
946         ssize_t (*store)(struct mem_ctl_info *, const char *,size_t);
947 };
948
949 #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
950 #define to_mcidev_attr(a) container_of(a, struct mcidev_attribute, attr)
951
952 static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
953                         char *buffer)
954 {
955         struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
956         struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
957
958         if (mcidev_attr->show)
959                 return mcidev_attr->show(mem_ctl_info, buffer);
960         return -EIO;
961 }
962
963 static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
964                                 const char *buffer, size_t count)
965 {
966         struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
967         struct mcidev_attribute * mcidev_attr = to_mcidev_attr(attr);
968
969         if (mcidev_attr->store)
970                 return mcidev_attr->store(mem_ctl_info, buffer, count);
971         return -EIO;
972 }
973
974 static struct sysfs_ops mci_ops = {
975         .show   = mcidev_show,
976         .store  = mcidev_store
977 };
978
979 #define MCIDEV_ATTR(_name,_mode,_show,_store)                   \
980 struct mcidev_attribute mci_attr_##_name = {                    \
981         .attr = {.name = __stringify(_name), .mode = _mode },   \
982         .show   = _show,                                        \
983         .store  = _store,                                       \
984 };
985
986 /* Control file */
987 MCIDEV_ATTR(reset_counters,S_IWUSR,NULL,mci_reset_counters_store);
988
989 /* Attribute files */
990 MCIDEV_ATTR(mc_name,S_IRUGO,mci_ctl_name_show,NULL);
991 MCIDEV_ATTR(module_name,S_IRUGO,mci_mod_name_show,NULL);
992 MCIDEV_ATTR(edac_capability,S_IRUGO,mci_edac_capability_show,NULL);
993 MCIDEV_ATTR(size_mb,S_IRUGO,mci_size_mb_show,NULL);
994 MCIDEV_ATTR(seconds_since_reset,S_IRUGO,mci_seconds_show,NULL);
995 MCIDEV_ATTR(ue_noinfo_count,S_IRUGO,mci_ue_noinfo_show,NULL);
996 MCIDEV_ATTR(ce_noinfo_count,S_IRUGO,mci_ce_noinfo_show,NULL);
997 MCIDEV_ATTR(ue_count,S_IRUGO,mci_ue_count_show,NULL);
998 MCIDEV_ATTR(ce_count,S_IRUGO,mci_ce_count_show,NULL);
999 MCIDEV_ATTR(edac_current_capability,S_IRUGO,
1000         mci_edac_current_capability_show,NULL);
1001 MCIDEV_ATTR(supported_mem_type,S_IRUGO,
1002         mci_supported_mem_type_show,NULL);
1003
1004
1005 static struct mcidev_attribute *mci_attr[] = {
1006         &mci_attr_reset_counters,
1007         &mci_attr_module_name,
1008         &mci_attr_mc_name,
1009         &mci_attr_edac_capability,
1010         &mci_attr_edac_current_capability,
1011         &mci_attr_supported_mem_type,
1012         &mci_attr_size_mb,
1013         &mci_attr_seconds_since_reset,
1014         &mci_attr_ue_noinfo_count,
1015         &mci_attr_ce_noinfo_count,
1016         &mci_attr_ue_count,
1017         &mci_attr_ce_count,
1018         NULL
1019 };
1020
1021
1022 /*
1023  * Release of a MC controlling instance
1024  */
1025 static void edac_mci_instance_release(struct kobject *kobj)
1026 {
1027         struct mem_ctl_info *mci;
1028         mci = container_of(kobj,struct mem_ctl_info,edac_mci_kobj);
1029
1030         debugf0("MC: " __FILE__ ": %s() idx=%d calling kfree\n",
1031                 __func__, mci->mc_idx);
1032
1033         kfree(mci);
1034 }
1035
1036 static struct kobj_type ktype_mci = {
1037         .release        = edac_mci_instance_release,
1038         .sysfs_ops      = &mci_ops,
1039         .default_attrs  = (struct attribute **) mci_attr,
1040 };
1041
1042 #define EDAC_DEVICE_SYMLINK     "device"
1043
1044 /*
1045  * Create a new Memory Controller kobject instance,
1046  *      mc<id> under the 'mc' directory
1047  *
1048  * Return:
1049  *      0       Success
1050  *      !0      Failure
1051  */
1052 static int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
1053 {
1054         int i;
1055         int err;
1056         struct csrow_info *csrow;
1057         struct kobject *edac_mci_kobj=&mci->edac_mci_kobj;
1058
1059         debugf0("MC: " __FILE__ ": %s() idx=%d\n", __func__, mci->mc_idx);
1060
1061         memset(edac_mci_kobj, 0, sizeof(*edac_mci_kobj));
1062         kobject_init(edac_mci_kobj);
1063
1064         /* set the name of the mc<id> object */
1065         err = kobject_set_name(edac_mci_kobj,"mc%d",mci->mc_idx);
1066         if (err)
1067                 return err;
1068
1069         /* link to our parent the '..../edac/mc' object */
1070         edac_mci_kobj->parent = &edac_memctrl_kobj;
1071         edac_mci_kobj->ktype = &ktype_mci;
1072
1073         /* register the mc<id> kobject */
1074         err = kobject_register(edac_mci_kobj);
1075         if (err)
1076                 return err;
1077
1078         /* create a symlink for the device */
1079         err = sysfs_create_link(edac_mci_kobj, &mci->pdev->dev.kobj,
1080                                 EDAC_DEVICE_SYMLINK);
1081         if (err) {
1082                 kobject_unregister(edac_mci_kobj);
1083                 return err;
1084         }
1085
1086         /* Make directories for each CSROW object
1087          * under the mc<id> kobject
1088          */
1089         for (i = 0; i < mci->nr_csrows; i++) {
1090
1091                 csrow = &mci->csrows[i];
1092
1093                 /* Only expose populated CSROWs */
1094                 if (csrow->nr_pages > 0) {
1095                         err = edac_create_csrow_object(edac_mci_kobj,csrow,i);
1096                         if (err)
1097                                 goto fail;
1098                 }
1099         }
1100
1101         /* Mark this MCI instance as having sysfs entries */
1102         mci->sysfs_active = MCI_SYSFS_ACTIVE;
1103
1104         return 0;
1105
1106
1107         /* CSROW error: backout what has already been registered,  */
1108 fail:
1109         for ( i--; i >= 0; i--) {
1110                 if (csrow->nr_pages > 0) {
1111                         kobject_unregister(&mci->csrows[i].kobj);
1112                         kobject_put(&mci->csrows[i].kobj);
1113                 }
1114         }
1115
1116         kobject_unregister(edac_mci_kobj);
1117         kobject_put(edac_mci_kobj);
1118
1119         return err;
1120 }
1121
1122 /*
1123  * remove a Memory Controller instance
1124  */
1125 static void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
1126 {
1127         int i;
1128
1129         debugf0("MC: " __FILE__ ": %s()\n", __func__);
1130
1131         /* remove all csrow kobjects */
1132         for (i = 0; i < mci->nr_csrows; i++) {
1133                 if (mci->csrows[i].nr_pages > 0)  {
1134                         kobject_unregister(&mci->csrows[i].kobj);
1135                         kobject_put(&mci->csrows[i].kobj);
1136                 }
1137         }
1138
1139         sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
1140
1141         kobject_unregister(&mci->edac_mci_kobj);
1142         kobject_put(&mci->edac_mci_kobj);
1143 }
1144
1145 /* END OF sysfs data and methods */
1146
1147 #ifdef CONFIG_EDAC_DEBUG
1148
1149 EXPORT_SYMBOL(edac_mc_dump_channel);
1150
1151 void edac_mc_dump_channel(struct channel_info *chan)
1152 {
1153         debugf4("\tchannel = %p\n", chan);
1154         debugf4("\tchannel->chan_idx = %d\n", chan->chan_idx);
1155         debugf4("\tchannel->ce_count = %d\n", chan->ce_count);
1156         debugf4("\tchannel->label = '%s'\n", chan->label);
1157         debugf4("\tchannel->csrow = %p\n\n", chan->csrow);
1158 }
1159
1160
1161 EXPORT_SYMBOL(edac_mc_dump_csrow);
1162
1163 void edac_mc_dump_csrow(struct csrow_info *csrow)
1164 {
1165         debugf4("\tcsrow = %p\n", csrow);
1166         debugf4("\tcsrow->csrow_idx = %d\n", csrow->csrow_idx);
1167         debugf4("\tcsrow->first_page = 0x%lx\n",
1168                 csrow->first_page);
1169         debugf4("\tcsrow->last_page = 0x%lx\n", csrow->last_page);
1170         debugf4("\tcsrow->page_mask = 0x%lx\n", csrow->page_mask);
1171         debugf4("\tcsrow->nr_pages = 0x%x\n", csrow->nr_pages);
1172         debugf4("\tcsrow->nr_channels = %d\n",
1173                 csrow->nr_channels);
1174         debugf4("\tcsrow->channels = %p\n", csrow->channels);
1175         debugf4("\tcsrow->mci = %p\n\n", csrow->mci);
1176 }
1177
1178
1179 EXPORT_SYMBOL(edac_mc_dump_mci);
1180
1181 void edac_mc_dump_mci(struct mem_ctl_info *mci)
1182 {
1183         debugf3("\tmci = %p\n", mci);
1184         debugf3("\tmci->mtype_cap = %lx\n", mci->mtype_cap);
1185         debugf3("\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
1186         debugf3("\tmci->edac_cap = %lx\n", mci->edac_cap);
1187         debugf4("\tmci->edac_check = %p\n", mci->edac_check);
1188         debugf3("\tmci->nr_csrows = %d, csrows = %p\n",
1189                 mci->nr_csrows, mci->csrows);
1190         debugf3("\tpdev = %p\n", mci->pdev);
1191         debugf3("\tmod_name:ctl_name = %s:%s\n",
1192                 mci->mod_name, mci->ctl_name);
1193         debugf3("\tpvt_info = %p\n\n", mci->pvt_info);
1194 }
1195
1196
1197 #endif                          /* CONFIG_EDAC_DEBUG */
1198
1199 /* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'.
1200  * Adjust 'ptr' so that its alignment is at least as stringent as what the
1201  * compiler would provide for X and return the aligned result.
1202  *
1203  * If 'size' is a constant, the compiler will optimize this whole function
1204  * down to either a no-op or the addition of a constant to the value of 'ptr'.
1205  */
1206 static inline char * align_ptr (void *ptr, unsigned size)
1207 {
1208         unsigned align, r;
1209
1210         /* Here we assume that the alignment of a "long long" is the most
1211          * stringent alignment that the compiler will ever provide by default.
1212          * As far as I know, this is a reasonable assumption.
1213          */
1214         if (size > sizeof(long))
1215                 align = sizeof(long long);
1216         else if (size > sizeof(int))
1217                 align = sizeof(long);
1218         else if (size > sizeof(short))
1219                 align = sizeof(int);
1220         else if (size > sizeof(char))
1221                 align = sizeof(short);
1222         else
1223                 return (char *) ptr;
1224
1225         r = size % align;
1226
1227         if (r == 0)
1228                 return (char *) ptr;
1229
1230         return (char *) (((unsigned long) ptr) + align - r);
1231 }
1232
1233
1234 EXPORT_SYMBOL(edac_mc_alloc);
1235
1236 /**
1237  * edac_mc_alloc: Allocate a struct mem_ctl_info structure
1238  * @size_pvt:   size of private storage needed
1239  * @nr_csrows:  Number of CWROWS needed for this MC
1240  * @nr_chans:   Number of channels for the MC
1241  *
1242  * Everything is kmalloc'ed as one big chunk - more efficient.
1243  * Only can be used if all structures have the same lifetime - otherwise
1244  * you have to allocate and initialize your own structures.
1245  *
1246  * Use edac_mc_free() to free mc structures allocated by this function.
1247  *
1248  * Returns:
1249  *      NULL allocation failed
1250  *      struct mem_ctl_info pointer
1251  */
1252 struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
1253                                         unsigned nr_chans)
1254 {
1255         struct mem_ctl_info *mci;
1256         struct csrow_info *csi, *csrow;
1257         struct channel_info *chi, *chp, *chan;
1258         void *pvt;
1259         unsigned size;
1260         int row, chn;
1261
1262         /* Figure out the offsets of the various items from the start of an mc
1263          * structure.  We want the alignment of each item to be at least as
1264          * stringent as what the compiler would provide if we could simply
1265          * hardcode everything into a single struct.
1266          */
1267         mci = (struct mem_ctl_info *) 0;
1268         csi = (struct csrow_info *)align_ptr(&mci[1], sizeof(*csi));
1269         chi = (struct channel_info *)
1270                         align_ptr(&csi[nr_csrows], sizeof(*chi));
1271         pvt = align_ptr(&chi[nr_chans * nr_csrows], sz_pvt);
1272         size = ((unsigned long) pvt) + sz_pvt;
1273
1274         if ((mci = kmalloc(size, GFP_KERNEL)) == NULL)
1275                 return NULL;
1276
1277         /* Adjust pointers so they point within the memory we just allocated
1278          * rather than an imaginary chunk of memory located at address 0.
1279          */
1280         csi = (struct csrow_info *) (((char *) mci) + ((unsigned long) csi));
1281         chi = (struct channel_info *) (((char *) mci) + ((unsigned long) chi));
1282         pvt = sz_pvt ? (((char *) mci) + ((unsigned long) pvt)) : NULL;
1283
1284         memset(mci, 0, size);   /* clear all fields */
1285
1286         mci->csrows = csi;
1287         mci->pvt_info = pvt;
1288         mci->nr_csrows = nr_csrows;
1289
1290         for (row = 0; row < nr_csrows; row++) {
1291                 csrow = &csi[row];
1292                 csrow->csrow_idx = row;
1293                 csrow->mci = mci;
1294                 csrow->nr_channels = nr_chans;
1295                 chp = &chi[row * nr_chans];
1296                 csrow->channels = chp;
1297
1298                 for (chn = 0; chn < nr_chans; chn++) {
1299                         chan = &chp[chn];
1300                         chan->chan_idx = chn;
1301                         chan->csrow = csrow;
1302                 }
1303         }
1304
1305         return mci;
1306 }
1307
1308
1309 EXPORT_SYMBOL(edac_mc_free);
1310
1311 /**
1312  * edac_mc_free:  Free a previously allocated 'mci' structure
1313  * @mci: pointer to a struct mem_ctl_info structure
1314  *
1315  * Free up a previously allocated mci structure
1316  * A MCI structure can be in 2 states after being allocated
1317  * by edac_mc_alloc().
1318  *      1) Allocated in a MC driver's probe, but not yet committed
1319  *      2) Allocated and committed, by a call to  edac_mc_add_mc()
1320  * edac_mc_add_mc() is the function that adds the sysfs entries
1321  * thus, this free function must determine which state the 'mci'
1322  * structure is in, then either free it directly or
1323  * perform kobject cleanup by calling edac_remove_sysfs_mci_device().
1324  *
1325  * VOID Return
1326  */
1327 void edac_mc_free(struct mem_ctl_info *mci)
1328 {
1329         /* only if sysfs entries for this mci instance exist
1330          * do we remove them and defer the actual kfree via
1331          * the kobject 'release()' callback.
1332          *
1333          * Otherwise, do a straight kfree now.
1334          */
1335         if (mci->sysfs_active == MCI_SYSFS_ACTIVE)
1336                 edac_remove_sysfs_mci_device(mci);
1337         else
1338                 kfree(mci);
1339 }
1340
1341
1342
1343 EXPORT_SYMBOL(edac_mc_find_mci_by_pdev);
1344
1345 struct mem_ctl_info *edac_mc_find_mci_by_pdev(struct pci_dev *pdev)
1346 {
1347         struct mem_ctl_info *mci;
1348         struct list_head *item;
1349
1350         debugf3("MC: " __FILE__ ": %s()\n", __func__);
1351
1352         list_for_each(item, &mc_devices) {
1353                 mci = list_entry(item, struct mem_ctl_info, link);
1354
1355                 if (mci->pdev == pdev)
1356                         return mci;
1357         }
1358
1359         return NULL;
1360 }
1361
1362 static int add_mc_to_global_list (struct mem_ctl_info *mci)
1363 {
1364         struct list_head *item, *insert_before;
1365         struct mem_ctl_info *p;
1366         int i;
1367
1368         if (list_empty(&mc_devices)) {
1369                 mci->mc_idx = 0;
1370                 insert_before = &mc_devices;
1371         } else {
1372                 if (edac_mc_find_mci_by_pdev(mci->pdev)) {
1373                         printk(KERN_WARNING
1374                                 "EDAC MC: %s (%s) %s %s already assigned %d\n",
1375                                 mci->pdev->dev.bus_id, pci_name(mci->pdev),
1376                                 mci->mod_name, mci->ctl_name, mci->mc_idx);
1377                         return 1;
1378                 }
1379
1380                 insert_before = NULL;
1381                 i = 0;
1382
1383                 list_for_each(item, &mc_devices) {
1384                         p = list_entry(item, struct mem_ctl_info, link);
1385
1386                         if (p->mc_idx != i) {
1387                                 insert_before = item;
1388                                 break;
1389                         }
1390
1391                         i++;
1392                 }
1393
1394                 mci->mc_idx = i;
1395
1396                 if (insert_before == NULL)
1397                         insert_before = &mc_devices;
1398         }
1399
1400         list_add_tail_rcu(&mci->link, insert_before);
1401         return 0;
1402 }
1403
1404
1405
1406 EXPORT_SYMBOL(edac_mc_add_mc);
1407
1408 /**
1409  * edac_mc_add_mc: Insert the 'mci' structure into the mci global list
1410  * @mci: pointer to the mci structure to be added to the list
1411  *
1412  * Return:
1413  *      0       Success
1414  *      !0      Failure
1415  */
1416
1417 /* FIXME - should a warning be printed if no error detection? correction? */
1418 int edac_mc_add_mc(struct mem_ctl_info *mci)
1419 {
1420         int rc = 1;
1421
1422         debugf0("MC: " __FILE__ ": %s()\n", __func__);
1423 #ifdef CONFIG_EDAC_DEBUG
1424         if (edac_debug_level >= 3)
1425                 edac_mc_dump_mci(mci);
1426         if (edac_debug_level >= 4) {
1427                 int i;
1428
1429                 for (i = 0; i < mci->nr_csrows; i++) {
1430                         int j;
1431                         edac_mc_dump_csrow(&mci->csrows[i]);
1432                         for (j = 0; j < mci->csrows[i].nr_channels; j++)
1433                                 edac_mc_dump_channel(&mci->csrows[i].
1434                                                           channels[j]);
1435                 }
1436         }
1437 #endif
1438         down(&mem_ctls_mutex);
1439
1440         if (add_mc_to_global_list(mci))
1441                 goto finish;
1442
1443         /* set load time so that error rate can be tracked */
1444         mci->start_time = jiffies;
1445
1446         if (edac_create_sysfs_mci_device(mci)) {
1447                 printk(KERN_WARNING
1448                        "EDAC MC%d: failed to create sysfs device\n",
1449                        mci->mc_idx);
1450                 /* FIXME - should there be an error code and unwind? */
1451                 goto finish;
1452         }
1453
1454         /* Report action taken */
1455         printk(KERN_INFO
1456                "EDAC MC%d: Giving out device to %s %s: PCI %s\n",
1457                mci->mc_idx, mci->mod_name, mci->ctl_name,
1458                pci_name(mci->pdev));
1459
1460
1461         rc = 0;
1462
1463 finish:
1464         up(&mem_ctls_mutex);
1465         return rc;
1466 }
1467
1468
1469
1470 static void complete_mc_list_del (struct rcu_head *head)
1471 {
1472         struct mem_ctl_info *mci;
1473
1474         mci = container_of(head, struct mem_ctl_info, rcu);
1475         INIT_LIST_HEAD(&mci->link);
1476         complete(&mci->complete);
1477 }
1478
1479 static void del_mc_from_global_list (struct mem_ctl_info *mci)
1480 {
1481         list_del_rcu(&mci->link);
1482         init_completion(&mci->complete);
1483         call_rcu(&mci->rcu, complete_mc_list_del);
1484         wait_for_completion(&mci->complete);
1485 }
1486
1487 EXPORT_SYMBOL(edac_mc_del_mc);
1488
1489 /**
1490  * edac_mc_del_mc:  Remove the specified mci structure from global list
1491  * @mci:        Pointer to struct mem_ctl_info structure
1492  *
1493  * Returns:
1494  *      0       Success
1495  *      1       Failure
1496  */
1497 int edac_mc_del_mc(struct mem_ctl_info *mci)
1498 {
1499         int rc = 1;
1500
1501         debugf0("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
1502         down(&mem_ctls_mutex);
1503         del_mc_from_global_list(mci);
1504         printk(KERN_INFO
1505                "EDAC MC%d: Removed device %d for %s %s: PCI %s\n",
1506                mci->mc_idx, mci->mc_idx, mci->mod_name, mci->ctl_name,
1507                pci_name(mci->pdev));
1508         rc = 0;
1509         up(&mem_ctls_mutex);
1510
1511         return rc;
1512 }
1513
1514
1515 EXPORT_SYMBOL(edac_mc_scrub_block);
1516
1517 void edac_mc_scrub_block(unsigned long page, unsigned long offset,
1518                               u32 size)
1519 {
1520         struct page *pg;
1521         void *virt_addr;
1522         unsigned long flags = 0;
1523
1524         debugf3("MC: " __FILE__ ": %s()\n", __func__);
1525
1526         /* ECC error page was not in our memory. Ignore it. */
1527         if(!pfn_valid(page))
1528                 return;
1529
1530         /* Find the actual page structure then map it and fix */
1531         pg = pfn_to_page(page);
1532
1533         if (PageHighMem(pg))
1534                 local_irq_save(flags);
1535
1536         virt_addr = kmap_atomic(pg, KM_BOUNCE_READ);
1537
1538         /* Perform architecture specific atomic scrub operation */
1539         atomic_scrub(virt_addr + offset, size);
1540
1541         /* Unmap and complete */
1542         kunmap_atomic(virt_addr, KM_BOUNCE_READ);
1543
1544         if (PageHighMem(pg))
1545                 local_irq_restore(flags);
1546 }
1547
1548
1549 /* FIXME - should return -1 */
1550 EXPORT_SYMBOL(edac_mc_find_csrow_by_page);
1551
1552 int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci,
1553                                     unsigned long page)
1554 {
1555         struct csrow_info *csrows = mci->csrows;
1556         int row, i;
1557
1558         debugf1("MC%d: " __FILE__ ": %s(): 0x%lx\n", mci->mc_idx, __func__,
1559                 page);
1560         row = -1;
1561
1562         for (i = 0; i < mci->nr_csrows; i++) {
1563                 struct csrow_info *csrow = &csrows[i];
1564
1565                 if (csrow->nr_pages == 0)
1566                         continue;
1567
1568                 debugf3("MC%d: " __FILE__
1569                         ": %s(): first(0x%lx) page(0x%lx)"
1570                         " last(0x%lx) mask(0x%lx)\n", mci->mc_idx,
1571                         __func__, csrow->first_page, page,
1572                         csrow->last_page, csrow->page_mask);
1573
1574                 if ((page >= csrow->first_page) &&
1575                     (page <= csrow->last_page) &&
1576                     ((page & csrow->page_mask) ==
1577                      (csrow->first_page & csrow->page_mask))) {
1578                         row = i;
1579                         break;
1580                 }
1581         }
1582
1583         if (row == -1)
1584                 printk(KERN_ERR
1585                        "EDAC MC%d: could not look up page error address %lx\n",
1586                        mci->mc_idx, (unsigned long) page);
1587
1588         return row;
1589 }
1590
1591
1592 EXPORT_SYMBOL(edac_mc_handle_ce);
1593
1594 /* FIXME - setable log (warning/emerg) levels */
1595 /* FIXME - integrate with evlog: http://evlog.sourceforge.net/ */
1596 void edac_mc_handle_ce(struct mem_ctl_info *mci,
1597                             unsigned long page_frame_number,
1598                             unsigned long offset_in_page,
1599                             unsigned long syndrome, int row, int channel,
1600                             const char *msg)
1601 {
1602         unsigned long remapped_page;
1603
1604         debugf3("MC%d: " __FILE__ ": %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                 printk(KERN_ERR
1610                        "EDAC MC%d: INTERNAL ERROR: row out of range (%d >= %d)\n",
1611                        mci->mc_idx, row, mci->nr_csrows);
1612                 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1613                 return;
1614         }
1615         if (channel >= mci->csrows[row].nr_channels || channel < 0) {
1616                 /* something is wrong */
1617                 printk(KERN_ERR
1618                        "EDAC MC%d: INTERNAL ERROR: channel out of range "
1619                        "(%d >= %d)\n",
1620                        mci->mc_idx, channel, mci->csrows[row].nr_channels);
1621                 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
1622                 return;
1623         }
1624
1625         if (log_ce)
1626                 /* FIXME - put in DIMM location */
1627                 printk(KERN_WARNING
1628                        "EDAC MC%d: CE page 0x%lx, offset 0x%lx,"
1629                        " grain %d, syndrome 0x%lx, row %d, channel %d,"
1630                        " label \"%s\": %s\n", mci->mc_idx,
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
1658
1659 EXPORT_SYMBOL(edac_mc_handle_ce_no_info);
1660
1661 void edac_mc_handle_ce_no_info(struct mem_ctl_info *mci,
1662                                     const char *msg)
1663 {
1664         if (log_ce)
1665                 printk(KERN_WARNING
1666                        "EDAC MC%d: CE - no information available: %s\n",
1667                        mci->mc_idx, msg);
1668         mci->ce_noinfo_count++;
1669         mci->ce_count++;
1670 }
1671
1672
1673 EXPORT_SYMBOL(edac_mc_handle_ue);
1674
1675 void edac_mc_handle_ue(struct mem_ctl_info *mci,
1676                             unsigned long page_frame_number,
1677                             unsigned long offset_in_page, int row,
1678                             const char *msg)
1679 {
1680         int len = EDAC_MC_LABEL_LEN * 4;
1681         char labels[len + 1];
1682         char *pos = labels;
1683         int chan;
1684         int chars;
1685
1686         debugf3("MC%d: " __FILE__ ": %s()\n", mci->mc_idx, __func__);
1687
1688         /* FIXME - maybe make panic on INTERNAL ERROR an option */
1689         if (row >= mci->nr_csrows || row < 0) {
1690                 /* something is wrong */
1691                 printk(KERN_ERR
1692                        "EDAC MC%d: INTERNAL ERROR: row out of range (%d >= %d)\n",
1693                        mci->mc_idx, row, mci->nr_csrows);
1694                 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
1695                 return;
1696         }
1697
1698         chars = snprintf(pos, len + 1, "%s",
1699                          mci->csrows[row].channels[0].label);
1700         len -= chars;
1701         pos += chars;
1702         for (chan = 1; (chan < mci->csrows[row].nr_channels) && (len > 0);
1703              chan++) {
1704                 chars = snprintf(pos, len + 1, ":%s",
1705                                  mci->csrows[row].channels[chan].label);
1706                 len -= chars;
1707                 pos += chars;
1708         }
1709
1710         if (log_ue)
1711                 printk(KERN_EMERG
1712                        "EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, row %d,"
1713                        " labels \"%s\": %s\n", mci->mc_idx,
1714                        page_frame_number, offset_in_page,
1715                        mci->csrows[row].grain, row, labels, msg);
1716
1717         if (panic_on_ue)
1718                 panic
1719                     ("EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, row %d,"
1720                      " labels \"%s\": %s\n", mci->mc_idx,
1721                      page_frame_number, offset_in_page,
1722                      mci->csrows[row].grain, row, labels, msg);
1723
1724         mci->ue_count++;
1725         mci->csrows[row].ue_count++;
1726 }
1727
1728
1729 EXPORT_SYMBOL(edac_mc_handle_ue_no_info);
1730
1731 void edac_mc_handle_ue_no_info(struct mem_ctl_info *mci,
1732                                     const char *msg)
1733 {
1734         if (panic_on_ue)
1735                 panic("EDAC MC%d: Uncorrected Error", mci->mc_idx);
1736
1737         if (log_ue)
1738                 printk(KERN_WARNING
1739                        "EDAC MC%d: UE - no information available: %s\n",
1740                        mci->mc_idx, msg);
1741         mci->ue_noinfo_count++;
1742         mci->ue_count++;
1743 }
1744
1745
1746 #ifdef CONFIG_PCI
1747
1748 static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
1749 {
1750         int where;
1751         u16 status;
1752
1753         where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
1754         pci_read_config_word(dev, where, &status);
1755
1756         /* If we get back 0xFFFF then we must suspect that the card has been pulled but
1757            the Linux PCI layer has not yet finished cleaning up. We don't want to report
1758            on such devices */
1759
1760         if (status == 0xFFFF) {
1761                 u32 sanity;
1762                 pci_read_config_dword(dev, 0, &sanity);
1763                 if (sanity == 0xFFFFFFFF)
1764                         return 0;
1765         }
1766         status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
1767                   PCI_STATUS_PARITY;
1768
1769         if (status)
1770                 /* reset only the bits we are interested in */
1771                 pci_write_config_word(dev, where, status);
1772
1773         return status;
1774 }
1775
1776 typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
1777
1778 /* Clear any PCI parity errors logged by this device. */
1779 static void edac_pci_dev_parity_clear( struct pci_dev *dev )
1780 {
1781         u8 header_type;
1782
1783         get_pci_parity_status(dev, 0);
1784
1785         /* read the device TYPE, looking for bridges */
1786         pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
1787
1788         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
1789                 get_pci_parity_status(dev, 1);
1790 }
1791
1792 /*
1793  *  PCI Parity polling
1794  *
1795  */
1796 static void edac_pci_dev_parity_test(struct pci_dev *dev)
1797 {
1798         u16 status;
1799         u8  header_type;
1800
1801         /* read the STATUS register on this device
1802          */
1803         status = get_pci_parity_status(dev, 0);
1804
1805         debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
1806
1807         /* check the status reg for errors */
1808         if (status) {
1809                 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
1810                         printk(KERN_CRIT
1811                                 "EDAC PCI- "
1812                                 "Signaled System Error on %s\n",
1813                                 pci_name (dev));
1814
1815                 if (status & (PCI_STATUS_PARITY)) {
1816                         printk(KERN_CRIT
1817                                 "EDAC PCI- "
1818                                 "Master Data Parity Error on %s\n",
1819                                 pci_name (dev));
1820
1821                         atomic_inc(&pci_parity_count);
1822                 }
1823
1824                 if (status & (PCI_STATUS_DETECTED_PARITY)) {
1825                         printk(KERN_CRIT
1826                                 "EDAC PCI- "
1827                                 "Detected Parity Error on %s\n",
1828                                 pci_name (dev));
1829
1830                         atomic_inc(&pci_parity_count);
1831                 }
1832         }
1833
1834         /* read the device TYPE, looking for bridges */
1835         pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
1836
1837         debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
1838
1839         if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1840                 /* On bridges, need to examine secondary status register  */
1841                 status = get_pci_parity_status(dev, 1);
1842
1843                 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
1844                                 status, dev->dev.bus_id );
1845
1846                 /* check the secondary status reg for errors */
1847                 if (status) {
1848                         if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
1849                                 printk(KERN_CRIT
1850                                         "EDAC PCI-Bridge- "
1851                                         "Signaled System Error on %s\n",
1852                                         pci_name (dev));
1853
1854                         if (status & (PCI_STATUS_PARITY)) {
1855                                 printk(KERN_CRIT
1856                                         "EDAC PCI-Bridge- "
1857                                         "Master Data Parity Error on %s\n",
1858                                         pci_name (dev));
1859
1860                                 atomic_inc(&pci_parity_count);
1861                         }
1862
1863                         if (status & (PCI_STATUS_DETECTED_PARITY)) {
1864                                 printk(KERN_CRIT
1865                                         "EDAC PCI-Bridge- "
1866                                         "Detected Parity Error on %s\n",
1867                                         pci_name (dev));
1868
1869                                 atomic_inc(&pci_parity_count);
1870                         }
1871                 }
1872         }
1873 }
1874
1875 /*
1876  * check_dev_on_list: Scan for a PCI device on a white/black list
1877  * @list:       an EDAC  &edac_pci_device_list  white/black list pointer
1878  * @free_index: index of next free entry on the list
1879  * @pci_dev:    PCI Device pointer
1880  *
1881  * see if list contains the device.
1882  *
1883  * Returns:     0 not found
1884  *              1 found on list
1885  */
1886 static int check_dev_on_list(struct edac_pci_device_list *list, int free_index,
1887                                 struct pci_dev *dev)
1888 {
1889         int i;
1890         int rc = 0;     /* Assume not found */
1891         unsigned short vendor=dev->vendor;
1892         unsigned short device=dev->device;
1893
1894         /* Scan the list, looking for a vendor/device match
1895          */
1896         for (i = 0; i < free_index; i++, list++ ) {
1897                 if (    (list->vendor == vendor ) &&
1898                         (list->device == device )) {
1899                         rc = 1;
1900                         break;
1901                 }
1902         }
1903
1904         return rc;
1905 }
1906
1907 /*
1908  * pci_dev parity list iterator
1909  *      Scan the PCI device list for one iteration, looking for SERRORs
1910  *      Master Parity ERRORS or Parity ERRORs on primary or secondary devices
1911  */
1912 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
1913 {
1914         struct pci_dev *dev=NULL;
1915
1916         /* request for kernel access to the next PCI device, if any,
1917          * and while we are looking at it have its reference count
1918          * bumped until we are done with it
1919          */
1920         while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
1921
1922                 /* if whitelist exists then it has priority, so only scan those
1923                  * devices on the whitelist
1924                  */
1925                 if (pci_whitelist_count > 0 ) {
1926                         if (check_dev_on_list(pci_whitelist,
1927                                         pci_whitelist_count, dev))
1928                                 fn(dev);
1929                 } else {
1930                         /*
1931                          * if no whitelist, then check if this devices is
1932                          * blacklisted
1933                          */
1934                         if (!check_dev_on_list(pci_blacklist,
1935                                         pci_blacklist_count, dev))
1936                                 fn(dev);
1937                 }
1938         }
1939 }
1940
1941 static void do_pci_parity_check(void)
1942 {
1943         unsigned long flags;
1944         int before_count;
1945
1946         debugf3("MC: " __FILE__ ": %s()\n", __func__);
1947
1948         if (!check_pci_parity)
1949                 return;
1950
1951         before_count = atomic_read(&pci_parity_count);
1952
1953         /* scan all PCI devices looking for a Parity Error on devices and
1954          * bridges
1955          */
1956         local_irq_save(flags);
1957         edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
1958         local_irq_restore(flags);
1959
1960         /* Only if operator has selected panic on PCI Error */
1961         if (panic_on_pci_parity) {
1962                 /* If the count is different 'after' from 'before' */
1963                 if (before_count != atomic_read(&pci_parity_count))
1964                         panic("EDAC: PCI Parity Error");
1965         }
1966 }
1967
1968
1969 static inline void clear_pci_parity_errors(void)
1970 {
1971         /* Clear any PCI bus parity errors that devices initially have logged
1972          * in their registers.
1973          */
1974         edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
1975 }
1976
1977
1978 #else  /* CONFIG_PCI */
1979
1980
1981 static inline void do_pci_parity_check(void)
1982 {
1983         /* no-op */
1984 }
1985
1986
1987 static inline void clear_pci_parity_errors(void)
1988 {
1989         /* no-op */
1990 }
1991
1992
1993 #endif  /* CONFIG_PCI */
1994
1995 /*
1996  * Iterate over all MC instances and check for ECC, et al, errors
1997  */
1998 static inline void check_mc_devices (void)
1999 {
2000         unsigned long flags;
2001         struct list_head *item;
2002         struct mem_ctl_info *mci;
2003
2004         debugf3("MC: " __FILE__ ": %s()\n", __func__);
2005
2006         /* during poll, have interrupts off */
2007         local_irq_save(flags);
2008
2009         list_for_each(item, &mc_devices) {
2010                 mci = list_entry(item, struct mem_ctl_info, link);
2011
2012                 if (mci->edac_check != NULL)
2013                         mci->edac_check(mci);
2014         }
2015
2016         local_irq_restore(flags);
2017 }
2018
2019
2020 /*
2021  * Check MC status every poll_msec.
2022  * Check PCI status every poll_msec as well.
2023  *
2024  * This where the work gets done for edac.
2025  *
2026  * SMP safe, doesn't use NMI, and auto-rate-limits.
2027  */
2028 static void do_edac_check(void)
2029 {
2030
2031         debugf3("MC: " __FILE__ ": %s()\n", __func__);
2032
2033         check_mc_devices();
2034
2035         do_pci_parity_check();
2036 }
2037
2038
2039 /*
2040  * EDAC thread state information
2041  */
2042 struct bs_thread_info
2043 {
2044         struct task_struct *task;
2045         struct completion *event;
2046         char *name;
2047         void (*run)(void);
2048 };
2049
2050 static struct bs_thread_info bs_thread;
2051
2052 /*
2053  *  edac_kernel_thread
2054  *      This the kernel thread that processes edac operations
2055  *      in a normal thread environment
2056  */
2057 static int edac_kernel_thread(void *arg)
2058 {
2059         struct bs_thread_info *thread = (struct bs_thread_info *) arg;
2060
2061         /* detach thread */
2062         daemonize(thread->name);
2063
2064         current->exit_signal = SIGCHLD;
2065         allow_signal(SIGKILL);
2066         thread->task = current;
2067
2068         /* indicate to starting task we have started */
2069         complete(thread->event);
2070
2071         /* loop forever, until we are told to stop */
2072         while(thread->run != NULL) {
2073                 void (*run)(void);
2074
2075                 /* call the function to check the memory controllers */
2076                 run = thread->run;
2077                 if (run)
2078                         run();
2079
2080                 if (signal_pending(current))
2081                         flush_signals(current);
2082
2083                 /* ensure we are interruptable */
2084                 set_current_state(TASK_INTERRUPTIBLE);
2085
2086                 /* goto sleep for the interval */
2087                 schedule_timeout((HZ * poll_msec) / 1000);
2088                 try_to_freeze();
2089         }
2090
2091         /* notify waiter that we are exiting */
2092         complete(thread->event);
2093
2094         return 0;
2095 }
2096
2097 /*
2098  * edac_mc_init
2099  *      module initialization entry point
2100  */
2101 static int __init edac_mc_init(void)
2102 {
2103         int ret;
2104         struct completion event;
2105
2106         printk(KERN_INFO "MC: " __FILE__ " version " EDAC_MC_VERSION "\n");
2107
2108         /*
2109          * Harvest and clear any boot/initialization PCI parity errors
2110          *
2111          * FIXME: This only clears errors logged by devices present at time of
2112          *      module initialization.  We should also do an initial clear
2113          *      of each newly hotplugged device.
2114          */
2115         clear_pci_parity_errors();
2116
2117         /* perform check for first time to harvest boot leftovers */
2118         do_edac_check();
2119
2120         /* Create the MC sysfs entires */
2121         if (edac_sysfs_memctrl_setup()) {
2122                 printk(KERN_ERR "EDAC MC: Error initializing sysfs code\n");
2123                 return -ENODEV;
2124         }
2125
2126         /* Create the PCI parity sysfs entries */
2127         if (edac_sysfs_pci_setup()) {
2128                 edac_sysfs_memctrl_teardown();
2129                 printk(KERN_ERR "EDAC PCI: Error initializing sysfs code\n");
2130                 return -ENODEV;
2131         }
2132
2133         /* Create our kernel thread */
2134         init_completion(&event);
2135         bs_thread.event = &event;
2136         bs_thread.name = "kedac";
2137         bs_thread.run = do_edac_check;
2138
2139         /* create our kernel thread */
2140         ret = kernel_thread(edac_kernel_thread, &bs_thread, CLONE_KERNEL);
2141         if (ret < 0) {
2142                 /* remove the sysfs entries */
2143                 edac_sysfs_memctrl_teardown();
2144                 edac_sysfs_pci_teardown();
2145                 return -ENOMEM;
2146         }
2147
2148         /* wait for our kernel theard ack that it is up and running */
2149         wait_for_completion(&event);
2150
2151         return 0;
2152 }
2153
2154
2155 /*
2156  * edac_mc_exit()
2157  *      module exit/termination functioni
2158  */
2159 static void __exit edac_mc_exit(void)
2160 {
2161         struct completion event;
2162
2163         debugf0("MC: " __FILE__ ": %s()\n", __func__);
2164
2165         init_completion(&event);
2166         bs_thread.event = &event;
2167
2168         /* As soon as ->run is set to NULL, the task could disappear,
2169          * so we need to hold tasklist_lock until we have sent the signal
2170          */
2171         read_lock(&tasklist_lock);
2172         bs_thread.run = NULL;
2173         send_sig(SIGKILL, bs_thread.task, 1);
2174         read_unlock(&tasklist_lock);
2175         wait_for_completion(&event);
2176
2177         /* tear down the sysfs device */
2178         edac_sysfs_memctrl_teardown();
2179         edac_sysfs_pci_teardown();
2180 }
2181
2182
2183
2184
2185 module_init(edac_mc_init);
2186 module_exit(edac_mc_exit);
2187
2188 MODULE_LICENSE("GPL");
2189 MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh et al\n"
2190               "Based on.work by Dan Hollis et al");
2191 MODULE_DESCRIPTION("Core library routines for MC reporting");
2192
2193 module_param(panic_on_ue, int, 0644);
2194 MODULE_PARM_DESC(panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
2195 module_param(check_pci_parity, int, 0644);
2196 MODULE_PARM_DESC(check_pci_parity, "Check for PCI bus parity errors: 0=off 1=on");
2197 module_param(panic_on_pci_parity, int, 0644);
2198 MODULE_PARM_DESC(panic_on_pci_parity, "Panic on PCI Bus Parity error: 0=off 1=on");
2199 module_param(log_ue, int, 0644);
2200 MODULE_PARM_DESC(log_ue, "Log uncorrectable error to console: 0=off 1=on");
2201 module_param(log_ce, int, 0644);
2202 MODULE_PARM_DESC(log_ce, "Log correctable error to console: 0=off 1=on");
2203 module_param(poll_msec, int, 0644);
2204 MODULE_PARM_DESC(poll_msec, "Polling period in milliseconds");
2205 #ifdef CONFIG_EDAC_DEBUG
2206 module_param(edac_debug_level, int, 0644);
2207 MODULE_PARM_DESC(edac_debug_level, "Debug level");
2208 #endif