2 * file for managing the edac_device class of devices for EDAC
4 * (C) 2007 SoftwareBitMaker(http://www.softwarebitmaker.com)
5 * This file may be distributed under the terms of the
6 * GNU General Public License.
8 * Written Doug Thompson <norsk5@xmission.com>
12 #include <linux/module.h>
13 #include <linux/sysdev.h>
14 #include <linux/ctype.h>
16 #include "edac_core.h"
17 #include "edac_module.h"
19 #define EDAC_DEVICE_SYMLINK "device"
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)
24 /************************** edac_device sysfs code and data **************/
27 * Set of edac_device_ctl_info attribute store/show functions
31 static ssize_t
edac_device_ctl_log_ue_show(struct edac_device_ctl_info
32 *ctl_info
, char *data
)
34 return sprintf(data
, "%u\n", ctl_info
->log_ue
);
37 static ssize_t
edac_device_ctl_log_ue_store(struct edac_device_ctl_info
38 *ctl_info
, const char *data
,
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);
48 static ssize_t
edac_device_ctl_log_ce_show(struct edac_device_ctl_info
49 *ctl_info
, char *data
)
51 return sprintf(data
, "%u\n", ctl_info
->log_ce
);
54 static ssize_t
edac_device_ctl_log_ce_store(struct edac_device_ctl_info
55 *ctl_info
, const char *data
,
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);
65 static ssize_t
edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
66 *ctl_info
, char *data
)
68 return sprintf(data
, "%u\n", ctl_info
->panic_on_ue
);
71 static ssize_t
edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
72 *ctl_info
, const char *data
,
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);
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
)
85 return sprintf(data
, "%u\n", ctl_info
->poll_msec
);
88 static ssize_t
edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
89 *ctl_info
, const char *data
,
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
99 value
= simple_strtoul(data
, NULL
, 0);
100 edac_device_reset_delay_period(ctl_info
, value
);
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);
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)
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
)
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
);
122 if (ctl_info_attr
->show
)
123 return ctl_info_attr
->show(edac_dev
, buffer
);
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
)
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
);
135 if (ctl_info_attr
->store
)
136 return ctl_info_attr
->store(edac_dev
, buffer
, count
);
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
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 }, \
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
);
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
,
173 /* Main DEVICE kobject release() function */
174 static void edac_device_ctrl_master_release(struct kobject
*kobj
)
176 struct edac_device_ctl_info
*edac_dev
;
178 edac_dev
= to_edacdev(kobj
);
180 debugf1("%s()\n", __func__
);
181 complete(&edac_dev
->kobj_complete
);
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
,
190 /**************** edac_device main kobj ctor/dtor code *********************/
193 * edac_device_register_main_kobj
195 * perform the high level setup for the new edac_device instance
200 static int edac_device_register_main_kobj(struct edac_device_ctl_info
*edac_dev
)
203 struct sysdev_class
*edac_class
;
205 debugf1("%s()\n", __func__
);
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
);
214 /* Point to the 'edac_class' this instance 'reports' to */
215 edac_dev
->edac_class
= edac_class
;
217 /* Init the devices's kobject */
218 memset(&edac_dev
->kobj
, 0, sizeof(struct kobject
));
219 edac_dev
->kobj
.ktype
= &ktype_device_ctrl
;
221 /* set this new device under the edac_class kobject */
222 edac_dev
->kobj
.parent
= &edac_class
->kset
.kobj
;
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
);
229 err
= kobject_register(&edac_dev
->kobj
);
231 debugf1("%s()Failed to register '.../edac/%s'\n",
232 __func__
, edac_dev
->name
);
236 debugf1("%s() Registered '.../edac/%s' kobject\n",
237 __func__
, edac_dev
->name
);
243 * edac_device_unregister_main_kobj:
244 * the '..../edac/<name>' kobject
246 static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
249 debugf0("%s()\n", __func__
);
250 debugf1("%s() name of kobject is: %s\n",
251 __func__
, kobject_name(&edac_dev
->kobj
));
253 init_completion(&edac_dev
->kobj_complete
);
256 * Unregister the edac device's kobject and
257 * wait for reference count to reach 0.
259 kobject_unregister(&edac_dev
->kobj
);
260 wait_for_completion(&edac_dev
->kobj_complete
);
263 /*************** edac_dev -> instance information ***********/
266 * Set of low-level instance attribute show functions
268 static ssize_t
instance_ue_count_show(struct edac_device_instance
*instance
,
271 return sprintf(data
, "%u\n", instance
->counters
.ue_count
);
274 static ssize_t
instance_ce_count_show(struct edac_device_instance
*instance
,
277 return sprintf(data
, "%u\n", instance
->counters
.ce_count
);
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)
283 /* DEVICE instance kobject release() function */
284 static void edac_device_ctrl_instance_release(struct kobject
*kobj
)
286 struct edac_device_instance
*instance
;
288 debugf1("%s()\n", __func__
);
290 instance
= to_instance(kobj
);
291 complete(&instance
->kobj_complete
);
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);
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
)
305 struct edac_device_instance
*instance
= to_instance(kobj
);
306 struct instance_attribute
*instance_attr
= to_instance_attr(attr
);
308 if (instance_attr
->show
)
309 return instance_attr
->show(instance
, buffer
);
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
)
318 struct edac_device_instance
*instance
= to_instance(kobj
);
319 struct instance_attribute
*instance_attr
= to_instance_attr(attr
);
321 if (instance_attr
->store
)
322 return instance_attr
->store(instance
, buffer
, count
);
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
332 #define INSTANCE_ATTR(_name,_mode,_show,_store) \
333 static struct instance_attribute attr_instance_##_name = { \
334 .attr = {.name = __stringify(_name), .mode = _mode }, \
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
344 INSTANCE_ATTR(ce_count
, S_IRUGO
, instance_ce_count_show
, NULL
);
345 INSTANCE_ATTR(ue_count
, S_IRUGO
, instance_ue_count_show
, NULL
);
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
,
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
,
361 /*************** edac_dev -> instance -> block information *********/
364 * Set of low-level block attribute show functions
366 static ssize_t
block_ue_count_show(struct edac_device_block
*block
, char *data
)
368 return sprintf(data
, "%u\n", block
->counters
.ue_count
);
371 static ssize_t
block_ce_count_show(struct edac_device_block
*block
, char *data
)
373 return sprintf(data
, "%u\n", block
->counters
.ce_count
);
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)
379 /* DEVICE block kobject release() function */
380 static void edac_device_ctrl_block_release(struct kobject
*kobj
)
382 struct edac_device_block
*block
;
384 debugf1("%s()\n", __func__
);
386 block
= to_block(kobj
);
387 complete(&block
->kobj_complete
);
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);
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
)
401 struct edac_device_block
*block
= to_block(kobj
);
402 struct block_attribute
*block_attr
= to_block_attr(attr
);
404 if (block_attr
->show
)
405 return block_attr
->show(block
, buffer
);
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
)
414 struct edac_device_block
*block
= to_block(kobj
);
415 struct block_attribute
*block_attr
= to_block_attr(attr
);
417 if (block_attr
->store
)
418 return block_attr
->store(block
, buffer
, count
);
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
428 #define BLOCK_ATTR(_name,_mode,_show,_store) \
429 static struct block_attribute attr_block_##_name = { \
430 .attr = {.name = __stringify(_name), .mode = _mode }, \
435 BLOCK_ATTR(ce_count
, S_IRUGO
, block_ce_count_show
, NULL
);
436 BLOCK_ATTR(ue_count
, S_IRUGO
, block_ue_count_show
, NULL
);
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
,
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
,
452 /************** block ctor/dtor code ************/
455 * edac_device_create_block
457 static int edac_device_create_block(struct edac_device_ctl_info
*edac_dev
,
458 struct edac_device_instance
*instance
,
462 struct edac_device_block
*block
;
464 block
= &instance
->blocks
[idx
];
466 debugf1("%s() Instance '%s' block[%d] '%s'\n",
467 __func__
, instance
->name
, idx
, block
->name
);
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
;
474 err
= kobject_set_name(&block
->kobj
, "%s", block
->name
);
478 err
= kobject_register(&block
->kobj
);
480 debugf1("%s()Failed to register instance '%s'\n",
481 __func__
, block
->name
);
489 * edac_device_delete_block(edac_dev,j);
491 static void edac_device_delete_block(struct edac_device_ctl_info
*edac_dev
,
492 struct edac_device_instance
*instance
,
495 struct edac_device_block
*block
;
497 block
= &instance
->blocks
[idx
];
499 /* unregister this block's kobject */
500 init_completion(&block
->kobj_complete
);
501 kobject_unregister(&block
->kobj
);
502 wait_for_completion(&block
->kobj_complete
);
505 /************** instance ctor/dtor code ************/
508 * edac_device_create_instance
509 * create just one instance of an edac_device 'instance'
511 static int edac_device_create_instance(struct edac_device_ctl_info
*edac_dev
,
516 struct edac_device_instance
*instance
;
518 instance
= &edac_dev
->instances
[idx
];
520 /* Init the instance's kobject */
521 memset(&instance
->kobj
, 0, sizeof(struct kobject
));
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
;
527 err
= kobject_set_name(&instance
->kobj
, "%s", instance
->name
);
531 err
= kobject_register(&instance
->kobj
);
533 debugf2("%s() Failed to register instance '%s'\n",
534 __func__
, instance
->name
);
538 debugf1("%s() now register '%d' blocks for instance %d\n",
539 __func__
, instance
->nr_blocks
, idx
);
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
);
545 for (j
= 0; j
< i
; j
++) {
546 edac_device_delete_block(edac_dev
, instance
, j
);
552 debugf1("%s() Registered instance %d '%s' kobject\n",
553 __func__
, idx
, instance
->name
);
559 * edac_device_remove_instance
560 * remove an edac_device instance
562 static void edac_device_delete_instance(struct edac_device_ctl_info
*edac_dev
,
566 struct edac_device_instance
*instance
;
568 instance
= &edac_dev
->instances
[idx
];
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
);
575 /* unregister this instance's kobject */
576 init_completion(&instance
->kobj_complete
);
577 kobject_unregister(&instance
->kobj
);
578 wait_for_completion(&instance
->kobj_complete
);
582 * edac_device_create_instances
583 * create the first level of 'instances' for this device
584 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
586 static int edac_device_create_instances(struct edac_device_ctl_info
*edac_dev
)
591 debugf0("%s()\n", __func__
);
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
);
597 /* unwind previous instances on error */
598 for (j
= 0; j
< i
; j
++) {
599 edac_device_delete_instance(edac_dev
, j
);
609 * edac_device_delete_instances(edac_dev);
610 * unregister all the kobjects of the instances
612 static void edac_device_delete_instances(struct edac_device_ctl_info
*edac_dev
)
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
);
622 /******************* edac_dev sysfs ctor/dtor code *************/
625 * edac_device_create_sysfs() Constructor
627 * Create a new edac_device kobject instance,
633 int edac_device_create_sysfs(struct edac_device_ctl_info
*edac_dev
)
636 struct kobject
*edac_kobj
= &edac_dev
->kobj
;
638 /* register this instance's main kobj with the edac class kobj */
639 err
= edac_device_register_main_kobj(edac_dev
);
643 debugf0("%s() idx=%d\n", __func__
, edac_dev
->dev_idx
);
645 /* create a symlink from the edac device
646 * to the platform 'device' being used for this
648 err
= sysfs_create_link(edac_kobj
,
649 &edac_dev
->dev
->kobj
, EDAC_DEVICE_SYMLINK
);
651 debugf0("%s() sysfs_create_link() returned err= %d\n",
656 debugf0("%s() calling create-instances, idx=%d\n",
657 __func__
, edac_dev
->dev_idx
);
659 /* Create the first level instance directories */
660 err
= edac_device_create_instances(edac_dev
);
667 /* Error unwind stack */
670 edac_device_unregister_main_kobj(edac_dev
);
676 * edac_device_remove_sysfs() destructor
678 * remove a edac_device instance
680 void edac_device_remove_sysfs(struct edac_device_ctl_info
*edac_dev
)
682 debugf0("%s()\n", __func__
);
684 edac_device_delete_instances(edac_dev
);
686 /* remove the sym link */
687 sysfs_remove_link(&edac_dev
->kobj
, EDAC_DEVICE_SYMLINK
);
689 /* unregister the instance's main kobj */
690 edac_device_unregister_main_kobj(edac_dev
);