drivers/edac: core Lindent cleanup
[safe/jmp/linux-2.6] / drivers / edac / edac_mc_sysfs.c
1 /*
2  * edac_mc kernel module
3  * (C) 2005, 2006 Linux Networx (http://lnxi.com)
4  * This file may be distributed under the terms of the
5  * GNU General Public License.
6  *
7  * Written Doug Thompson <norsk5@xmission.com>
8  *
9  */
10
11 #include <linux/module.h>
12 #include <linux/sysdev.h>
13 #include <linux/ctype.h>
14
15 #include "edac_core.h"
16 #include "edac_module.h"
17
18 /* MC EDAC Controls, setable by module parameter, and sysfs */
19 static int edac_mc_log_ue = 1;
20 static int edac_mc_log_ce = 1;
21 static int edac_mc_panic_on_ue = 0;
22 static int edac_mc_poll_msec = 1000;
23
24 /* Getter functions for above */
25 int edac_mc_get_log_ue(void)
26 {
27         return edac_mc_log_ue;
28 }
29
30 int edac_mc_get_log_ce(void)
31 {
32         return edac_mc_log_ce;
33 }
34
35 int edac_mc_get_panic_on_ue(void)
36 {
37         return edac_mc_panic_on_ue;
38 }
39
40 /* this is temporary */
41 int edac_mc_get_poll_msec(void)
42 {
43         return edac_mc_poll_msec;
44 }
45
46 /* Parameter declarations for above */
47 module_param(edac_mc_panic_on_ue, int, 0644);
48 MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
49 module_param(edac_mc_log_ue, int, 0644);
50 MODULE_PARM_DESC(edac_mc_log_ue,
51                  "Log uncorrectable error to console: 0=off 1=on");
52 module_param(edac_mc_log_ce, int, 0644);
53 MODULE_PARM_DESC(edac_mc_log_ce,
54                  "Log correctable error to console: 0=off 1=on");
55 module_param(edac_mc_poll_msec, int, 0644);
56 MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
57
58 /*
59  * various constants for Memory Controllers
60  */
61 static const char *mem_types[] = {
62         [MEM_EMPTY] = "Empty",
63         [MEM_RESERVED] = "Reserved",
64         [MEM_UNKNOWN] = "Unknown",
65         [MEM_FPM] = "FPM",
66         [MEM_EDO] = "EDO",
67         [MEM_BEDO] = "BEDO",
68         [MEM_SDR] = "Unbuffered-SDR",
69         [MEM_RDR] = "Registered-SDR",
70         [MEM_DDR] = "Unbuffered-DDR",
71         [MEM_RDDR] = "Registered-DDR",
72         [MEM_RMBS] = "RMBS",
73         [MEM_DDR2] = "Unbuffered-DDR2",
74         [MEM_FB_DDR2] = "FullyBuffered-DDR2",
75         [MEM_RDDR2] = "Registered-DDR2"
76 };
77
78 static const char *dev_types[] = {
79         [DEV_UNKNOWN] = "Unknown",
80         [DEV_X1] = "x1",
81         [DEV_X2] = "x2",
82         [DEV_X4] = "x4",
83         [DEV_X8] = "x8",
84         [DEV_X16] = "x16",
85         [DEV_X32] = "x32",
86         [DEV_X64] = "x64"
87 };
88
89 static const char *edac_caps[] = {
90         [EDAC_UNKNOWN] = "Unknown",
91         [EDAC_NONE] = "None",
92         [EDAC_RESERVED] = "Reserved",
93         [EDAC_PARITY] = "PARITY",
94         [EDAC_EC] = "EC",
95         [EDAC_SECDED] = "SECDED",
96         [EDAC_S2ECD2ED] = "S2ECD2ED",
97         [EDAC_S4ECD4ED] = "S4ECD4ED",
98         [EDAC_S8ECD8ED] = "S8ECD8ED",
99         [EDAC_S16ECD16ED] = "S16ECD16ED"
100 };
101
102 /* sysfs object:
103  *      /sys/devices/system/edac/mc
104  */
105 static struct kobject edac_memctrl_kobj;
106
107 /* We use these to wait for the reference counts on edac_memctrl_kobj and
108  * edac_pci_kobj to reach 0.
109  */
110 static struct completion edac_memctrl_kobj_complete;
111
112 /*
113  * /sys/devices/system/edac/mc;
114  *      data structures and methods
115  */
116 static ssize_t memctrl_int_show(void *ptr, char *buffer)
117 {
118         int *value = (int *)ptr;
119         return sprintf(buffer, "%u\n", *value);
120 }
121
122 static ssize_t memctrl_int_store(void *ptr, const char *buffer, size_t count)
123 {
124         int *value = (int *)ptr;
125
126         if (isdigit(*buffer))
127                 *value = simple_strtoul(buffer, NULL, 0);
128
129         return count;
130 }
131
132 struct memctrl_dev_attribute {
133         struct attribute attr;
134         void *value;
135          ssize_t(*show) (void *, char *);
136          ssize_t(*store) (void *, const char *, size_t);
137 };
138
139 /* Set of show/store abstract level functions for memory control object */
140 static ssize_t memctrl_dev_show(struct kobject *kobj,
141                                 struct attribute *attr, char *buffer)
142 {
143         struct memctrl_dev_attribute *memctrl_dev;
144         memctrl_dev = (struct memctrl_dev_attribute *)attr;
145
146         if (memctrl_dev->show)
147                 return memctrl_dev->show(memctrl_dev->value, buffer);
148
149         return -EIO;
150 }
151
152 static ssize_t memctrl_dev_store(struct kobject *kobj, struct attribute *attr,
153                                  const char *buffer, size_t count)
154 {
155         struct memctrl_dev_attribute *memctrl_dev;
156         memctrl_dev = (struct memctrl_dev_attribute *)attr;
157
158         if (memctrl_dev->store)
159                 return memctrl_dev->store(memctrl_dev->value, buffer, count);
160
161         return -EIO;
162 }
163
164 static struct sysfs_ops memctrlfs_ops = {
165         .show = memctrl_dev_show,
166         .store = memctrl_dev_store
167 };
168
169 #define MEMCTRL_ATTR(_name,_mode,_show,_store)                  \
170 static struct memctrl_dev_attribute attr_##_name = {                    \
171         .attr = {.name = __stringify(_name), .mode = _mode },   \
172         .value  = &_name,                                       \
173         .show   = _show,                                        \
174         .store  = _store,                                       \
175 };
176
177 #define MEMCTRL_STRING_ATTR(_name,_data,_mode,_show,_store)     \
178 static struct memctrl_dev_attribute attr_##_name = {                    \
179         .attr = {.name = __stringify(_name), .mode = _mode },   \
180         .value  = _data,                                        \
181         .show   = _show,                                        \
182         .store  = _store,                                       \
183 };
184
185 /* csrow<id> control files */
186 MEMCTRL_ATTR(edac_mc_panic_on_ue,
187              S_IRUGO | S_IWUSR, memctrl_int_show, memctrl_int_store);
188
189 MEMCTRL_ATTR(edac_mc_log_ue,
190              S_IRUGO | S_IWUSR, memctrl_int_show, memctrl_int_store);
191
192 MEMCTRL_ATTR(edac_mc_log_ce,
193              S_IRUGO | S_IWUSR, memctrl_int_show, memctrl_int_store);
194
195 MEMCTRL_ATTR(edac_mc_poll_msec,
196              S_IRUGO | S_IWUSR, memctrl_int_show, memctrl_int_store);
197
198 /* Base Attributes of the memory ECC object */
199 static struct memctrl_dev_attribute *memctrl_attr[] = {
200         &attr_edac_mc_panic_on_ue,
201         &attr_edac_mc_log_ue,
202         &attr_edac_mc_log_ce,
203         &attr_edac_mc_poll_msec,
204         NULL,
205 };
206
207 /* Main MC kobject release() function */
208 static void edac_memctrl_master_release(struct kobject *kobj)
209 {
210         debugf1("%s()\n", __func__);
211         complete(&edac_memctrl_kobj_complete);
212 }
213
214 static struct kobj_type ktype_memctrl = {
215         .release = edac_memctrl_master_release,
216         .sysfs_ops = &memctrlfs_ops,
217         .default_attrs = (struct attribute **)memctrl_attr,
218 };
219
220 /* Initialize the main sysfs entries for edac:
221  *   /sys/devices/system/edac
222  *
223  * and children
224  *
225  * Return:  0 SUCCESS
226  *         !0 FAILURE
227  */
228 int edac_sysfs_memctrl_setup(void)
229 {
230         int err = 0;
231         struct sysdev_class *edac_class;
232
233         debugf1("%s()\n", __func__);
234
235         /* get the /sys/devices/system/edac class reference */
236         edac_class = edac_get_edac_class();
237         if (edac_class == NULL) {
238                 debugf1("%s() no edac_class error=%d\n", __func__, err);
239                 return err;
240         }
241
242         /* Init the MC's kobject */
243         memset(&edac_memctrl_kobj, 0, sizeof(edac_memctrl_kobj));
244         edac_memctrl_kobj.parent = &edac_class->kset.kobj;
245         edac_memctrl_kobj.ktype = &ktype_memctrl;
246
247         /* generate sysfs "..../edac/mc"   */
248         err = kobject_set_name(&edac_memctrl_kobj, "mc");
249         if (err) {
250                 debugf1("%s() Failed to set name '.../edac/mc'\n", __func__);
251                 return err;
252         }
253
254         /* FIXME: maybe new sysdev_create_subdir() */
255         err = kobject_register(&edac_memctrl_kobj);
256         if (err) {
257                 debugf1("%s() Failed to register '.../edac/mc'\n", __func__);
258                 return err;
259         }
260
261         debugf1("%s() Registered '.../edac/mc' kobject\n", __func__);
262         return 0;
263 }
264
265 /*
266  * MC teardown:
267  *      the '..../edac/mc' kobject followed by '..../edac' itself
268  */
269 void edac_sysfs_memctrl_teardown(void)
270 {
271         debugf0("MC: " __FILE__ ": %s()\n", __func__);
272
273         /* Unregister the MC's kobject and wait for reference count to reach 0.
274          */
275         init_completion(&edac_memctrl_kobj_complete);
276         kobject_unregister(&edac_memctrl_kobj);
277         wait_for_completion(&edac_memctrl_kobj_complete);
278 }
279
280 /* EDAC sysfs CSROW data structures and methods
281  */
282
283 /* Set of more default csrow<id> attribute show/store functions */
284 static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data,
285                                    int private)
286 {
287         return sprintf(data, "%u\n", csrow->ue_count);
288 }
289
290 static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data,
291                                    int private)
292 {
293         return sprintf(data, "%u\n", csrow->ce_count);
294 }
295
296 static ssize_t csrow_size_show(struct csrow_info *csrow, char *data,
297                                int private)
298 {
299         return sprintf(data, "%u\n", PAGES_TO_MiB(csrow->nr_pages));
300 }
301
302 static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data,
303                                    int private)
304 {
305         return sprintf(data, "%s\n", mem_types[csrow->mtype]);
306 }
307
308 static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data,
309                                    int private)
310 {
311         return sprintf(data, "%s\n", dev_types[csrow->dtype]);
312 }
313
314 static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data,
315                                     int private)
316 {
317         return sprintf(data, "%s\n", edac_caps[csrow->edac_mode]);
318 }
319
320 /* show/store functions for DIMM Label attributes */
321 static ssize_t channel_dimm_label_show(struct csrow_info *csrow,
322                                        char *data, int channel)
323 {
324         return snprintf(data, EDAC_MC_LABEL_LEN, "%s",
325                         csrow->channels[channel].label);
326 }
327
328 static ssize_t channel_dimm_label_store(struct csrow_info *csrow,
329                                         const char *data,
330                                         size_t count, int channel)
331 {
332         ssize_t max_size = 0;
333
334         max_size = min((ssize_t) count, (ssize_t) EDAC_MC_LABEL_LEN - 1);
335         strncpy(csrow->channels[channel].label, data, max_size);
336         csrow->channels[channel].label[max_size] = '\0';
337
338         return max_size;
339 }
340
341 /* show function for dynamic chX_ce_count attribute */
342 static ssize_t channel_ce_count_show(struct csrow_info *csrow,
343                                      char *data, int channel)
344 {
345         return sprintf(data, "%u\n", csrow->channels[channel].ce_count);
346 }
347
348 /* csrow specific attribute structure */
349 struct csrowdev_attribute {
350         struct attribute attr;
351          ssize_t(*show) (struct csrow_info *, char *, int);
352          ssize_t(*store) (struct csrow_info *, const char *, size_t, int);
353         int private;
354 };
355
356 #define to_csrow(k) container_of(k, struct csrow_info, kobj)
357 #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
358
359 /* Set of show/store higher level functions for default csrow attributes */
360 static ssize_t csrowdev_show(struct kobject *kobj,
361                              struct attribute *attr, char *buffer)
362 {
363         struct csrow_info *csrow = to_csrow(kobj);
364         struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
365
366         if (csrowdev_attr->show)
367                 return csrowdev_attr->show(csrow,
368                                            buffer, csrowdev_attr->private);
369         return -EIO;
370 }
371
372 static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
373                               const char *buffer, size_t count)
374 {
375         struct csrow_info *csrow = to_csrow(kobj);
376         struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
377
378         if (csrowdev_attr->store)
379                 return csrowdev_attr->store(csrow,
380                                             buffer,
381                                             count, csrowdev_attr->private);
382         return -EIO;
383 }
384
385 static struct sysfs_ops csrowfs_ops = {
386         .show = csrowdev_show,
387         .store = csrowdev_store
388 };
389
390 #define CSROWDEV_ATTR(_name,_mode,_show,_store,_private)        \
391 static struct csrowdev_attribute attr_##_name = {                       \
392         .attr = {.name = __stringify(_name), .mode = _mode },   \
393         .show   = _show,                                        \
394         .store  = _store,                                       \
395         .private = _private,                                    \
396 };
397
398 /* default cwrow<id>/attribute files */
399 CSROWDEV_ATTR(size_mb, S_IRUGO, csrow_size_show, NULL, 0);
400 CSROWDEV_ATTR(dev_type, S_IRUGO, csrow_dev_type_show, NULL, 0);
401 CSROWDEV_ATTR(mem_type, S_IRUGO, csrow_mem_type_show, NULL, 0);
402 CSROWDEV_ATTR(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL, 0);
403 CSROWDEV_ATTR(ue_count, S_IRUGO, csrow_ue_count_show, NULL, 0);
404 CSROWDEV_ATTR(ce_count, S_IRUGO, csrow_ce_count_show, NULL, 0);
405
406 /* default attributes of the CSROW<id> object */
407 static struct csrowdev_attribute *default_csrow_attr[] = {
408         &attr_dev_type,
409         &attr_mem_type,
410         &attr_edac_mode,
411         &attr_size_mb,
412         &attr_ue_count,
413         &attr_ce_count,
414         NULL,
415 };
416
417 /* possible dynamic channel DIMM Label attribute files */
418 CSROWDEV_ATTR(ch0_dimm_label, S_IRUGO | S_IWUSR,
419               channel_dimm_label_show, channel_dimm_label_store, 0);
420 CSROWDEV_ATTR(ch1_dimm_label, S_IRUGO | S_IWUSR,
421               channel_dimm_label_show, channel_dimm_label_store, 1);
422 CSROWDEV_ATTR(ch2_dimm_label, S_IRUGO | S_IWUSR,
423               channel_dimm_label_show, channel_dimm_label_store, 2);
424 CSROWDEV_ATTR(ch3_dimm_label, S_IRUGO | S_IWUSR,
425               channel_dimm_label_show, channel_dimm_label_store, 3);
426 CSROWDEV_ATTR(ch4_dimm_label, S_IRUGO | S_IWUSR,
427               channel_dimm_label_show, channel_dimm_label_store, 4);
428 CSROWDEV_ATTR(ch5_dimm_label, S_IRUGO | S_IWUSR,
429               channel_dimm_label_show, channel_dimm_label_store, 5);
430
431 /* Total possible dynamic DIMM Label attribute file table */
432 static struct csrowdev_attribute *dynamic_csrow_dimm_attr[] = {
433         &attr_ch0_dimm_label,
434         &attr_ch1_dimm_label,
435         &attr_ch2_dimm_label,
436         &attr_ch3_dimm_label,
437         &attr_ch4_dimm_label,
438         &attr_ch5_dimm_label
439 };
440
441 /* possible dynamic channel ce_count attribute files */
442 CSROWDEV_ATTR(ch0_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 0);
443 CSROWDEV_ATTR(ch1_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 1);
444 CSROWDEV_ATTR(ch2_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 2);
445 CSROWDEV_ATTR(ch3_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 3);
446 CSROWDEV_ATTR(ch4_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 4);
447 CSROWDEV_ATTR(ch5_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 5);
448
449 /* Total possible dynamic ce_count attribute file table */
450 static struct csrowdev_attribute *dynamic_csrow_ce_count_attr[] = {
451         &attr_ch0_ce_count,
452         &attr_ch1_ce_count,
453         &attr_ch2_ce_count,
454         &attr_ch3_ce_count,
455         &attr_ch4_ce_count,
456         &attr_ch5_ce_count
457 };
458
459 #define EDAC_NR_CHANNELS        6
460
461 /* Create dynamic CHANNEL files, indexed by 'chan',  under specifed CSROW */
462 static int edac_create_channel_files(struct kobject *kobj, int chan)
463 {
464         int err = -ENODEV;
465
466         if (chan >= EDAC_NR_CHANNELS)
467                 return err;
468
469         /* create the DIMM label attribute file */
470         err = sysfs_create_file(kobj,
471                                 (struct attribute *)
472                                 dynamic_csrow_dimm_attr[chan]);
473
474         if (!err) {
475                 /* create the CE Count attribute file */
476                 err = sysfs_create_file(kobj,
477                                         (struct attribute *)
478                                         dynamic_csrow_ce_count_attr[chan]);
479         } else {
480                 debugf1("%s()  dimm labels and ce_count files created",
481                         __func__);
482         }
483
484         return err;
485 }
486
487 /* No memory to release for this kobj */
488 static void edac_csrow_instance_release(struct kobject *kobj)
489 {
490         struct csrow_info *cs;
491
492         cs = container_of(kobj, struct csrow_info, kobj);
493         complete(&cs->kobj_complete);
494 }
495
496 /* the kobj_type instance for a CSROW */
497 static struct kobj_type ktype_csrow = {
498         .release = edac_csrow_instance_release,
499         .sysfs_ops = &csrowfs_ops,
500         .default_attrs = (struct attribute **)default_csrow_attr,
501 };
502
503 /* Create a CSROW object under specifed edac_mc_device */
504 static int edac_create_csrow_object(struct kobject *edac_mci_kobj,
505                                     struct csrow_info *csrow, int index)
506 {
507         int err = 0;
508         int chan;
509
510         memset(&csrow->kobj, 0, sizeof(csrow->kobj));
511
512         /* generate ..../edac/mc/mc<id>/csrow<index>   */
513
514         csrow->kobj.parent = edac_mci_kobj;
515         csrow->kobj.ktype = &ktype_csrow;
516
517         /* name this instance of csrow<id> */
518         err = kobject_set_name(&csrow->kobj, "csrow%d", index);
519         if (err)
520                 goto error_exit;
521
522         /* Instanstiate the csrow object */
523         err = kobject_register(&csrow->kobj);
524         if (!err) {
525                 /* Create the dyanmic attribute files on this csrow,
526                  * namely, the DIMM labels and the channel ce_count
527                  */
528                 for (chan = 0; chan < csrow->nr_channels; chan++) {
529                         err = edac_create_channel_files(&csrow->kobj, chan);
530                         if (err)
531                                 break;
532                 }
533         }
534
535       error_exit:
536         return err;
537 }
538
539 /* default sysfs methods and data structures for the main MCI kobject */
540
541 static ssize_t mci_reset_counters_store(struct mem_ctl_info *mci,
542                                         const char *data, size_t count)
543 {
544         int row, chan;
545
546         mci->ue_noinfo_count = 0;
547         mci->ce_noinfo_count = 0;
548         mci->ue_count = 0;
549         mci->ce_count = 0;
550
551         for (row = 0; row < mci->nr_csrows; row++) {
552                 struct csrow_info *ri = &mci->csrows[row];
553
554                 ri->ue_count = 0;
555                 ri->ce_count = 0;
556
557                 for (chan = 0; chan < ri->nr_channels; chan++)
558                         ri->channels[chan].ce_count = 0;
559         }
560
561         mci->start_time = jiffies;
562         return count;
563 }
564
565 /* memory scrubbing */
566 static ssize_t mci_sdram_scrub_rate_store(struct mem_ctl_info *mci,
567                                           const char *data, size_t count)
568 {
569         u32 bandwidth = -1;
570
571         if (mci->set_sdram_scrub_rate) {
572
573                 memctrl_int_store(&bandwidth, data, count);
574
575                 if (!(*mci->set_sdram_scrub_rate) (mci, &bandwidth)) {
576                         edac_printk(KERN_DEBUG, EDAC_MC,
577                                     "Scrub rate set successfully, applied: %d\n",
578                                     bandwidth);
579                 } else {
580                         /* FIXME: error codes maybe? */
581                         edac_printk(KERN_DEBUG, EDAC_MC,
582                                     "Scrub rate set FAILED, could not apply: %d\n",
583                                     bandwidth);
584                 }
585         } else {
586                 /* FIXME: produce "not implemented" ERROR for user-side. */
587                 edac_printk(KERN_WARNING, EDAC_MC,
588                             "Memory scrubbing 'set'control is not implemented!\n");
589         }
590         return count;
591 }
592
593 static ssize_t mci_sdram_scrub_rate_show(struct mem_ctl_info *mci, char *data)
594 {
595         u32 bandwidth = -1;
596
597         if (mci->get_sdram_scrub_rate) {
598                 if (!(*mci->get_sdram_scrub_rate) (mci, &bandwidth)) {
599                         edac_printk(KERN_DEBUG, EDAC_MC,
600                                     "Scrub rate successfully, fetched: %d\n",
601                                     bandwidth);
602                 } else {
603                         /* FIXME: error codes maybe? */
604                         edac_printk(KERN_DEBUG, EDAC_MC,
605                                     "Scrub rate fetch FAILED, got: %d\n",
606                                     bandwidth);
607                 }
608         } else {
609                 /* FIXME: produce "not implemented" ERROR for user-side.  */
610                 edac_printk(KERN_WARNING, EDAC_MC,
611                             "Memory scrubbing 'get' control is not implemented\n");
612         }
613         return sprintf(data, "%d\n", bandwidth);
614 }
615
616 /* default attribute files for the MCI object */
617 static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
618 {
619         return sprintf(data, "%d\n", mci->ue_count);
620 }
621
622 static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
623 {
624         return sprintf(data, "%d\n", mci->ce_count);
625 }
626
627 static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
628 {
629         return sprintf(data, "%d\n", mci->ce_noinfo_count);
630 }
631
632 static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
633 {
634         return sprintf(data, "%d\n", mci->ue_noinfo_count);
635 }
636
637 static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
638 {
639         return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
640 }
641
642 static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
643 {
644         return sprintf(data, "%s\n", mci->ctl_name);
645 }
646
647 static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
648 {
649         int total_pages, csrow_idx;
650
651         for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
652              csrow_idx++) {
653                 struct csrow_info *csrow = &mci->csrows[csrow_idx];
654
655                 if (!csrow->nr_pages)
656                         continue;
657
658                 total_pages += csrow->nr_pages;
659         }
660
661         return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
662 }
663
664 struct mcidev_attribute {
665         struct attribute attr;
666          ssize_t(*show) (struct mem_ctl_info *, char *);
667          ssize_t(*store) (struct mem_ctl_info *, const char *, size_t);
668 };
669
670 #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
671 #define to_mcidev_attr(a) container_of(a, struct mcidev_attribute, attr)
672
673 /* MCI show/store functions for top most object */
674 static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
675                            char *buffer)
676 {
677         struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
678         struct mcidev_attribute *mcidev_attr = to_mcidev_attr(attr);
679
680         if (mcidev_attr->show)
681                 return mcidev_attr->show(mem_ctl_info, buffer);
682
683         return -EIO;
684 }
685
686 static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
687                             const char *buffer, size_t count)
688 {
689         struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
690         struct mcidev_attribute *mcidev_attr = to_mcidev_attr(attr);
691
692         if (mcidev_attr->store)
693                 return mcidev_attr->store(mem_ctl_info, buffer, count);
694
695         return -EIO;
696 }
697
698 static struct sysfs_ops mci_ops = {
699         .show = mcidev_show,
700         .store = mcidev_store
701 };
702
703 #define MCIDEV_ATTR(_name,_mode,_show,_store)                   \
704 static struct mcidev_attribute mci_attr_##_name = {                     \
705         .attr = {.name = __stringify(_name), .mode = _mode },   \
706         .show   = _show,                                        \
707         .store  = _store,                                       \
708 };
709
710 /* default Control file */
711 MCIDEV_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
712
713 /* default Attribute files */
714 MCIDEV_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
715 MCIDEV_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
716 MCIDEV_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
717 MCIDEV_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
718 MCIDEV_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
719 MCIDEV_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
720 MCIDEV_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
721
722 /* memory scrubber attribute file */
723 MCIDEV_ATTR(sdram_scrub_rate, S_IRUGO | S_IWUSR, mci_sdram_scrub_rate_show,
724             mci_sdram_scrub_rate_store);
725
726 static struct mcidev_attribute *mci_attr[] = {
727         &mci_attr_reset_counters,
728         &mci_attr_mc_name,
729         &mci_attr_size_mb,
730         &mci_attr_seconds_since_reset,
731         &mci_attr_ue_noinfo_count,
732         &mci_attr_ce_noinfo_count,
733         &mci_attr_ue_count,
734         &mci_attr_ce_count,
735         &mci_attr_sdram_scrub_rate,
736         NULL
737 };
738
739 /*
740  * Release of a MC controlling instance
741  */
742 static void edac_mci_instance_release(struct kobject *kobj)
743 {
744         struct mem_ctl_info *mci;
745
746         mci = to_mci(kobj);
747         debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
748         complete(&mci->kobj_complete);
749 }
750
751 static struct kobj_type ktype_mci = {
752         .release = edac_mci_instance_release,
753         .sysfs_ops = &mci_ops,
754         .default_attrs = (struct attribute **)mci_attr,
755 };
756
757 #define EDAC_DEVICE_SYMLINK     "device"
758
759 /*
760  * Create a new Memory Controller kobject instance,
761  *      mc<id> under the 'mc' directory
762  *
763  * Return:
764  *      0       Success
765  *      !0      Failure
766  */
767 int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
768 {
769         int i;
770         int err;
771         struct csrow_info *csrow;
772         struct kobject *edac_mci_kobj = &mci->edac_mci_kobj;
773
774         debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
775         memset(edac_mci_kobj, 0, sizeof(*edac_mci_kobj));
776
777         /* set the name of the mc<id> object */
778         err = kobject_set_name(edac_mci_kobj, "mc%d", mci->mc_idx);
779         if (err)
780                 return err;
781
782         /* link to our parent the '..../edac/mc' object */
783         edac_mci_kobj->parent = &edac_memctrl_kobj;
784         edac_mci_kobj->ktype = &ktype_mci;
785
786         /* register the mc<id> kobject */
787         err = kobject_register(edac_mci_kobj);
788         if (err)
789                 return err;
790
791         /* create a symlink for the device */
792         err = sysfs_create_link(edac_mci_kobj, &mci->dev->kobj,
793                                 EDAC_DEVICE_SYMLINK);
794         if (err)
795                 goto fail0;
796
797         /* Make directories for each CSROW object
798          * under the mc<id> kobject
799          */
800         for (i = 0; i < mci->nr_csrows; i++) {
801                 csrow = &mci->csrows[i];
802
803                 /* Only expose populated CSROWs */
804                 if (csrow->nr_pages > 0) {
805                         err = edac_create_csrow_object(edac_mci_kobj, csrow, i);
806                         if (err)
807                                 goto fail1;
808                 }
809         }
810
811         return 0;
812
813         /* CSROW error: backout what has already been registered,  */
814       fail1:
815         for (i--; i >= 0; i--) {
816                 if (csrow->nr_pages > 0) {
817                         init_completion(&csrow->kobj_complete);
818                         kobject_unregister(&mci->csrows[i].kobj);
819                         wait_for_completion(&csrow->kobj_complete);
820                 }
821         }
822
823       fail0:
824         init_completion(&mci->kobj_complete);
825         kobject_unregister(edac_mci_kobj);
826         wait_for_completion(&mci->kobj_complete);
827         return err;
828 }
829
830 /*
831  * remove a Memory Controller instance
832  */
833 void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
834 {
835         int i;
836
837         debugf0("%s()\n", __func__);
838
839         /* remove all csrow kobjects */
840         for (i = 0; i < mci->nr_csrows; i++) {
841                 if (mci->csrows[i].nr_pages > 0) {
842                         init_completion(&mci->csrows[i].kobj_complete);
843                         kobject_unregister(&mci->csrows[i].kobj);
844                         wait_for_completion(&mci->csrows[i].kobj_complete);
845                 }
846         }
847
848         sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
849         init_completion(&mci->kobj_complete);
850         kobject_unregister(&mci->edac_mci_kobj);
851         wait_for_completion(&mci->kobj_complete);
852 }