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