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