drivers/edac: Lindent r82600
[powerpc.git] / drivers / edac / edac_device_sysfs.c
1 /*
2  * file for managing the edac_device class of devices for EDAC
3  *
4  * (C) 2007 SoftwareBitMaker(http://www.softwarebitmaker.com)
5  * This file may be distributed under the terms of the
6  * GNU General Public License.
7  *
8  * Written Doug Thompson <norsk5@xmission.com>
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/sysdev.h>
14 #include <linux/ctype.h>
15
16 #include "edac_core.h"
17 #include "edac_module.h"
18
19 #define EDAC_DEVICE_SYMLINK     "device"
20
21 #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
22 #define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
23
24 /************************** edac_device sysfs code and data **************/
25
26 /*
27  * Set of edac_device_ctl_info attribute store/show functions
28  */
29
30 /* 'log_ue' */
31 static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
32                                            *ctl_info, char *data)
33 {
34         return sprintf(data, "%u\n", ctl_info->log_ue);
35 }
36
37 static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
38                                             *ctl_info, const char *data,
39                                             size_t count)
40 {
41         /* if parameter is zero, turn off flag, if non-zero turn on flag */
42         ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
43
44         return count;
45 }
46
47 /* 'log_ce' */
48 static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
49                                            *ctl_info, char *data)
50 {
51         return sprintf(data, "%u\n", ctl_info->log_ce);
52 }
53
54 static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
55                                             *ctl_info, const char *data,
56                                             size_t count)
57 {
58         /* if parameter is zero, turn off flag, if non-zero turn on flag */
59         ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
60
61         return count;
62 }
63
64 /* 'panic_on_ue' */
65 static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
66                                                 *ctl_info, char *data)
67 {
68         return sprintf(data, "%u\n", ctl_info->panic_on_ue);
69 }
70
71 static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
72                                                  *ctl_info, const char *data,
73                                                  size_t count)
74 {
75         /* if parameter is zero, turn off flag, if non-zero turn on flag */
76         ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
77
78         return count;
79 }
80
81 /* 'poll_msec' show and store functions*/
82 static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
83                                               *ctl_info, char *data)
84 {
85         return sprintf(data, "%u\n", ctl_info->poll_msec);
86 }
87
88 static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
89                                                *ctl_info, const char *data,
90                                                size_t count)
91 {
92         unsigned long value;
93
94         /* get the value and enforce that it is non-zero, must be at least
95          * one millisecond for the delay period, between scans
96          * Then cancel last outstanding delay for the work request
97          * and set a new one.
98          */
99         value = simple_strtoul(data, NULL, 0);
100         edac_device_reset_delay_period(ctl_info, value);
101
102         return count;
103 }
104
105 /* edac_device_ctl_info specific attribute structure */
106 struct ctl_info_attribute {
107         struct attribute attr;
108          ssize_t(*show) (struct edac_device_ctl_info *, char *);
109          ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
110 };
111
112 #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
113 #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
114
115 /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
116 static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
117                                       struct attribute *attr, char *buffer)
118 {
119         struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
120         struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
121
122         if (ctl_info_attr->show)
123                 return ctl_info_attr->show(edac_dev, buffer);
124         return -EIO;
125 }
126
127 /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
128 static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
129                                        struct attribute *attr,
130                                        const char *buffer, size_t count)
131 {
132         struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
133         struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
134
135         if (ctl_info_attr->store)
136                 return ctl_info_attr->store(edac_dev, buffer, count);
137         return -EIO;
138 }
139
140 /* edac_dev file operations for an 'ctl_info' */
141 static struct sysfs_ops device_ctl_info_ops = {
142         .show = edac_dev_ctl_info_show,
143         .store = edac_dev_ctl_info_store
144 };
145
146 #define CTL_INFO_ATTR(_name,_mode,_show,_store)        \
147 static struct ctl_info_attribute attr_ctl_info_##_name = {      \
148         .attr = {.name = __stringify(_name), .mode = _mode },   \
149         .show   = _show,                                        \
150         .store  = _store,                                       \
151 };
152
153 /* Declare the various ctl_info attributes here and their respective ops */
154 CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
155               edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
156 CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
157               edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
158 CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
159               edac_device_ctl_panic_on_ue_show,
160               edac_device_ctl_panic_on_ue_store);
161 CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
162               edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
163
164 /* Base Attributes of the EDAC_DEVICE ECC object */
165 static struct ctl_info_attribute *device_ctrl_attr[] = {
166         &attr_ctl_info_panic_on_ue,
167         &attr_ctl_info_log_ue,
168         &attr_ctl_info_log_ce,
169         &attr_ctl_info_poll_msec,
170         NULL,
171 };
172
173 /* Main DEVICE kobject release() function */
174 static void edac_device_ctrl_master_release(struct kobject *kobj)
175 {
176         struct edac_device_ctl_info *edac_dev;
177
178         edac_dev = to_edacdev(kobj);
179
180         debugf1("%s()\n", __func__);
181         complete(&edac_dev->kobj_complete);
182 }
183
184 static struct kobj_type ktype_device_ctrl = {
185         .release = edac_device_ctrl_master_release,
186         .sysfs_ops = &device_ctl_info_ops,
187         .default_attrs = (struct attribute **)device_ctrl_attr,
188 };
189
190 /**************** edac_device main kobj ctor/dtor code *********************/
191
192 /*
193  * edac_device_register_main_kobj
194  *
195  *      perform the high level setup for the new edac_device instance
196  *
197  * Return:  0 SUCCESS
198  *         !0 FAILURE
199  */
200 static int edac_device_register_main_kobj(struct edac_device_ctl_info *edac_dev)
201 {
202         int err = 0;
203         struct sysdev_class *edac_class;
204
205         debugf1("%s()\n", __func__);
206
207         /* get the /sys/devices/system/edac reference */
208         edac_class = edac_get_edac_class();
209         if (edac_class == NULL) {
210                 debugf1("%s() no edac_class error=%d\n", __func__, err);
211                 return err;
212         }
213
214         /* Point to the 'edac_class' this instance 'reports' to */
215         edac_dev->edac_class = edac_class;
216
217         /* Init the devices's kobject */
218         memset(&edac_dev->kobj, 0, sizeof(struct kobject));
219         edac_dev->kobj.ktype = &ktype_device_ctrl;
220
221         /* set this new device under the edac_class kobject */
222         edac_dev->kobj.parent = &edac_class->kset.kobj;
223
224         /* generate sysfs "..../edac/<name>"   */
225         debugf1("%s() set name of kobject to: %s\n", __func__, edac_dev->name);
226         err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name);
227         if (err)
228                 return err;
229         err = kobject_register(&edac_dev->kobj);
230         if (err) {
231                 debugf1("%s()Failed to register '.../edac/%s'\n",
232                         __func__, edac_dev->name);
233                 return err;
234         }
235
236         debugf1("%s() Registered '.../edac/%s' kobject\n",
237                 __func__, edac_dev->name);
238
239         return 0;
240 }
241
242 /*
243  * edac_device_unregister_main_kobj:
244  *      the '..../edac/<name>' kobject
245  */
246 static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
247                                              *edac_dev)
248 {
249         debugf0("%s()\n", __func__);
250         debugf1("%s() name of kobject is: %s\n",
251                 __func__, kobject_name(&edac_dev->kobj));
252
253         init_completion(&edac_dev->kobj_complete);
254
255         /*
256          * Unregister the edac device's kobject and
257          * wait for reference count to reach 0.
258          */
259         kobject_unregister(&edac_dev->kobj);
260         wait_for_completion(&edac_dev->kobj_complete);
261 }
262
263 /*************** edac_dev -> instance information ***********/
264
265 /*
266  * Set of low-level instance attribute show functions
267  */
268 static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
269                                       char *data)
270 {
271         return sprintf(data, "%u\n", instance->counters.ue_count);
272 }
273
274 static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
275                                       char *data)
276 {
277         return sprintf(data, "%u\n", instance->counters.ce_count);
278 }
279
280 #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
281 #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
282
283 /* DEVICE instance kobject release() function */
284 static void edac_device_ctrl_instance_release(struct kobject *kobj)
285 {
286         struct edac_device_instance *instance;
287
288         debugf1("%s()\n", __func__);
289
290         instance = to_instance(kobj);
291         complete(&instance->kobj_complete);
292 }
293
294 /* instance specific attribute structure */
295 struct instance_attribute {
296         struct attribute attr;
297          ssize_t(*show) (struct edac_device_instance *, char *);
298          ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
299 };
300
301 /* Function to 'show' fields from the edac_dev 'instance' structure */
302 static ssize_t edac_dev_instance_show(struct kobject *kobj,
303                                       struct attribute *attr, char *buffer)
304 {
305         struct edac_device_instance *instance = to_instance(kobj);
306         struct instance_attribute *instance_attr = to_instance_attr(attr);
307
308         if (instance_attr->show)
309                 return instance_attr->show(instance, buffer);
310         return -EIO;
311 }
312
313 /* Function to 'store' fields into the edac_dev 'instance' structure */
314 static ssize_t edac_dev_instance_store(struct kobject *kobj,
315                                        struct attribute *attr,
316                                        const char *buffer, size_t count)
317 {
318         struct edac_device_instance *instance = to_instance(kobj);
319         struct instance_attribute *instance_attr = to_instance_attr(attr);
320
321         if (instance_attr->store)
322                 return instance_attr->store(instance, buffer, count);
323         return -EIO;
324 }
325
326 /* edac_dev file operations for an 'instance' */
327 static struct sysfs_ops device_instance_ops = {
328         .show = edac_dev_instance_show,
329         .store = edac_dev_instance_store
330 };
331
332 #define INSTANCE_ATTR(_name,_mode,_show,_store)        \
333 static struct instance_attribute attr_instance_##_name = {      \
334         .attr = {.name = __stringify(_name), .mode = _mode },   \
335         .show   = _show,                                        \
336         .store  = _store,                                       \
337 };
338
339 /*
340  * Define attributes visible for the edac_device instance object
341  *      Each contains a pointer to a show and an optional set
342  *      function pointer that does the low level output/input
343  */
344 INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
345 INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
346
347 /* list of edac_dev 'instance' attributes */
348 static struct instance_attribute *device_instance_attr[] = {
349         &attr_instance_ce_count,
350         &attr_instance_ue_count,
351         NULL,
352 };
353
354 /* The 'ktype' for each edac_dev 'instance' */
355 static struct kobj_type ktype_instance_ctrl = {
356         .release = edac_device_ctrl_instance_release,
357         .sysfs_ops = &device_instance_ops,
358         .default_attrs = (struct attribute **)device_instance_attr,
359 };
360
361 /*************** edac_dev -> instance -> block information *********/
362
363 /*
364  * Set of low-level block attribute show functions
365  */
366 static ssize_t block_ue_count_show(struct edac_device_block *block, char *data)
367 {
368         return sprintf(data, "%u\n", block->counters.ue_count);
369 }
370
371 static ssize_t block_ce_count_show(struct edac_device_block *block, char *data)
372 {
373         return sprintf(data, "%u\n", block->counters.ce_count);
374 }
375
376 #define to_block(k) container_of(k, struct edac_device_block, kobj)
377 #define to_block_attr(a) container_of(a,struct block_attribute,attr)
378
379 /* DEVICE block kobject release() function */
380 static void edac_device_ctrl_block_release(struct kobject *kobj)
381 {
382         struct edac_device_block *block;
383
384         debugf1("%s()\n", __func__);
385
386         block = to_block(kobj);
387         complete(&block->kobj_complete);
388 }
389
390 /* block specific attribute structure */
391 struct block_attribute {
392         struct attribute attr;
393          ssize_t(*show) (struct edac_device_block *, char *);
394          ssize_t(*store) (struct edac_device_block *, const char *, size_t);
395 };
396
397 /* Function to 'show' fields from the edac_dev 'block' structure */
398 static ssize_t edac_dev_block_show(struct kobject *kobj,
399                                    struct attribute *attr, char *buffer)
400 {
401         struct edac_device_block *block = to_block(kobj);
402         struct block_attribute *block_attr = to_block_attr(attr);
403
404         if (block_attr->show)
405                 return block_attr->show(block, buffer);
406         return -EIO;
407 }
408
409 /* Function to 'store' fields into the edac_dev 'block' structure */
410 static ssize_t edac_dev_block_store(struct kobject *kobj,
411                                     struct attribute *attr,
412                                     const char *buffer, size_t count)
413 {
414         struct edac_device_block *block = to_block(kobj);
415         struct block_attribute *block_attr = to_block_attr(attr);
416
417         if (block_attr->store)
418                 return block_attr->store(block, buffer, count);
419         return -EIO;
420 }
421
422 /* edac_dev file operations for a 'block' */
423 static struct sysfs_ops device_block_ops = {
424         .show = edac_dev_block_show,
425         .store = edac_dev_block_store
426 };
427
428 #define BLOCK_ATTR(_name,_mode,_show,_store)        \
429 static struct block_attribute attr_block_##_name = {                       \
430         .attr = {.name = __stringify(_name), .mode = _mode },   \
431         .show   = _show,                                        \
432         .store  = _store,                                       \
433 };
434
435 BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
436 BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
437
438 /* list of edac_dev 'block' attributes */
439 static struct block_attribute *device_block_attr[] = {
440         &attr_block_ce_count,
441         &attr_block_ue_count,
442         NULL,
443 };
444
445 /* The 'ktype' for each edac_dev 'block' */
446 static struct kobj_type ktype_block_ctrl = {
447         .release = edac_device_ctrl_block_release,
448         .sysfs_ops = &device_block_ops,
449         .default_attrs = (struct attribute **)device_block_attr,
450 };
451
452 /************** block ctor/dtor  code ************/
453
454 /*
455  * edac_device_create_block
456  */
457 static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
458                                     struct edac_device_instance *instance,
459                                     int idx)
460 {
461         int err;
462         struct edac_device_block *block;
463
464         block = &instance->blocks[idx];
465
466         debugf1("%s() Instance '%s' block[%d] '%s'\n",
467                 __func__, instance->name, idx, block->name);
468
469         /* init this block's kobject */
470         memset(&block->kobj, 0, sizeof(struct kobject));
471         block->kobj.parent = &instance->kobj;
472         block->kobj.ktype = &ktype_block_ctrl;
473
474         err = kobject_set_name(&block->kobj, "%s", block->name);
475         if (err)
476                 return err;
477
478         err = kobject_register(&block->kobj);
479         if (err) {
480                 debugf1("%s()Failed to register instance '%s'\n",
481                         __func__, block->name);
482                 return err;
483         }
484
485         return 0;
486 }
487
488 /*
489  * edac_device_delete_block(edac_dev,j);
490  */
491 static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
492                                      struct edac_device_instance *instance,
493                                      int idx)
494 {
495         struct edac_device_block *block;
496
497         block = &instance->blocks[idx];
498
499         /* unregister this block's kobject */
500         init_completion(&block->kobj_complete);
501         kobject_unregister(&block->kobj);
502         wait_for_completion(&block->kobj_complete);
503 }
504
505 /************** instance ctor/dtor  code ************/
506
507 /*
508  * edac_device_create_instance
509  *      create just one instance of an edac_device 'instance'
510  */
511 static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
512                                        int idx)
513 {
514         int i, j;
515         int err;
516         struct edac_device_instance *instance;
517
518         instance = &edac_dev->instances[idx];
519
520         /* Init the instance's kobject */
521         memset(&instance->kobj, 0, sizeof(struct kobject));
522
523         /* set this new device under the edac_device main kobject */
524         instance->kobj.parent = &edac_dev->kobj;
525         instance->kobj.ktype = &ktype_instance_ctrl;
526
527         err = kobject_set_name(&instance->kobj, "%s", instance->name);
528         if (err)
529                 return err;
530
531         err = kobject_register(&instance->kobj);
532         if (err != 0) {
533                 debugf2("%s() Failed to register instance '%s'\n",
534                         __func__, instance->name);
535                 return err;
536         }
537
538         debugf1("%s() now register '%d' blocks for instance %d\n",
539                 __func__, instance->nr_blocks, idx);
540
541         /* register all blocks of this instance */
542         for (i = 0; i < instance->nr_blocks; i++) {
543                 err = edac_device_create_block(edac_dev, instance, i);
544                 if (err) {
545                         for (j = 0; j < i; j++) {
546                                 edac_device_delete_block(edac_dev, instance, j);
547                         }
548                         return err;
549                 }
550         }
551
552         debugf1("%s() Registered instance %d '%s' kobject\n",
553                 __func__, idx, instance->name);
554
555         return 0;
556 }
557
558 /*
559  * edac_device_remove_instance
560  *      remove an edac_device instance
561  */
562 static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
563                                         int idx)
564 {
565         int i;
566         struct edac_device_instance *instance;
567
568         instance = &edac_dev->instances[idx];
569
570         /* unregister all blocks in this instance */
571         for (i = 0; i < instance->nr_blocks; i++) {
572                 edac_device_delete_block(edac_dev, instance, i);
573         }
574
575         /* unregister this instance's kobject */
576         init_completion(&instance->kobj_complete);
577         kobject_unregister(&instance->kobj);
578         wait_for_completion(&instance->kobj_complete);
579 }
580
581 /*
582  * edac_device_create_instances
583  *      create the first level of 'instances' for this device
584  *      (ie  'cache' might have 'cache0', 'cache1', 'cache2', etc
585  */
586 static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
587 {
588         int i, j;
589         int err;
590
591         debugf0("%s()\n", __func__);
592
593         /* iterate over creation of the instances */
594         for (i = 0; i < edac_dev->nr_instances; i++) {
595                 err = edac_device_create_instance(edac_dev, i);
596                 if (err) {
597                         /* unwind previous instances on error */
598                         for (j = 0; j < i; j++) {
599                                 edac_device_delete_instance(edac_dev, j);
600                         }
601                         return err;
602                 }
603         }
604
605         return 0;
606 }
607
608 /*
609  * edac_device_delete_instances(edac_dev);
610  *      unregister all the kobjects of the instances
611  */
612 static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
613 {
614         int i;
615
616         /* iterate over creation of the instances */
617         for (i = 0; i < edac_dev->nr_instances; i++) {
618                 edac_device_delete_instance(edac_dev, i);
619         }
620 }
621
622 /******************* edac_dev sysfs ctor/dtor  code *************/
623
624 /*
625  * edac_device_create_sysfs() Constructor
626  *
627  * Create a new edac_device kobject instance,
628  *
629  * Return:
630  *      0       Success
631  *      !0      Failure
632  */
633 int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
634 {
635         int err;
636         struct kobject *edac_kobj = &edac_dev->kobj;
637
638         /* register this instance's main kobj with the edac class kobj */
639         err = edac_device_register_main_kobj(edac_dev);
640         if (err)
641                 return err;
642
643         debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
644
645         /* create a symlink from the edac device
646          * to the platform 'device' being used for this
647          */
648         err = sysfs_create_link(edac_kobj,
649                                 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
650         if (err) {
651                 debugf0("%s() sysfs_create_link() returned err= %d\n",
652                         __func__, err);
653                 return err;
654         }
655
656         debugf0("%s() calling create-instances, idx=%d\n",
657                 __func__, edac_dev->dev_idx);
658
659         /* Create the first level instance directories */
660         err = edac_device_create_instances(edac_dev);
661         if (err) {
662                 goto error0;
663         }
664
665         return 0;
666
667         /* Error unwind stack */
668
669       error0:
670         edac_device_unregister_main_kobj(edac_dev);
671
672         return err;
673 }
674
675 /*
676  * edac_device_remove_sysfs() destructor
677  *
678  * remove a edac_device instance
679  */
680 void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
681 {
682         debugf0("%s()\n", __func__);
683
684         edac_device_delete_instances(edac_dev);
685
686         /* remove the sym link */
687         sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
688
689         /* unregister the instance's main kobj */
690         edac_device_unregister_main_kobj(edac_dev);
691 }