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