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