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