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