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/ctype.h>
14 #include "edac_core.h"
15 #include "edac_module.h"
17 #define EDAC_DEVICE_SYMLINK "device"
19 #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
20 #define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
22 /************************** edac_device sysfs code and data **************/
25 * Set of edac_device_ctl_info attribute store/show functions
29 static ssize_t
edac_device_ctl_log_ue_show(struct edac_device_ctl_info
30 *ctl_info
, char *data
)
32 return sprintf(data
, "%u\n", ctl_info
->log_ue
);
35 static ssize_t
edac_device_ctl_log_ue_store(struct edac_device_ctl_info
36 *ctl_info
, const char *data
,
39 /* if parameter is zero, turn off flag, if non-zero turn on flag */
40 ctl_info
->log_ue
= (simple_strtoul(data
, NULL
, 0) != 0);
46 static ssize_t
edac_device_ctl_log_ce_show(struct edac_device_ctl_info
47 *ctl_info
, char *data
)
49 return sprintf(data
, "%u\n", ctl_info
->log_ce
);
52 static ssize_t
edac_device_ctl_log_ce_store(struct edac_device_ctl_info
53 *ctl_info
, const char *data
,
56 /* if parameter is zero, turn off flag, if non-zero turn on flag */
57 ctl_info
->log_ce
= (simple_strtoul(data
, NULL
, 0) != 0);
63 static ssize_t
edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
64 *ctl_info
, char *data
)
66 return sprintf(data
, "%u\n", ctl_info
->panic_on_ue
);
69 static ssize_t
edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
70 *ctl_info
, const char *data
,
73 /* if parameter is zero, turn off flag, if non-zero turn on flag */
74 ctl_info
->panic_on_ue
= (simple_strtoul(data
, NULL
, 0) != 0);
79 /* 'poll_msec' show and store functions*/
80 static ssize_t
edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
81 *ctl_info
, char *data
)
83 return sprintf(data
, "%u\n", ctl_info
->poll_msec
);
86 static ssize_t
edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
87 *ctl_info
, const char *data
,
92 /* get the value and enforce that it is non-zero, must be at least
93 * one millisecond for the delay period, between scans
94 * Then cancel last outstanding delay for the work request
97 value
= simple_strtoul(data
, NULL
, 0);
98 edac_device_reset_delay_period(ctl_info
, value
);
103 /* edac_device_ctl_info specific attribute structure */
104 struct ctl_info_attribute
{
105 struct attribute attr
;
106 ssize_t(*show
) (struct edac_device_ctl_info
*, char *);
107 ssize_t(*store
) (struct edac_device_ctl_info
*, const char *, size_t);
110 #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
111 #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
113 /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
114 static ssize_t
edac_dev_ctl_info_show(struct kobject
*kobj
,
115 struct attribute
*attr
, char *buffer
)
117 struct edac_device_ctl_info
*edac_dev
= to_ctl_info(kobj
);
118 struct ctl_info_attribute
*ctl_info_attr
= to_ctl_info_attr(attr
);
120 if (ctl_info_attr
->show
)
121 return ctl_info_attr
->show(edac_dev
, buffer
);
125 /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
126 static ssize_t
edac_dev_ctl_info_store(struct kobject
*kobj
,
127 struct attribute
*attr
,
128 const char *buffer
, size_t count
)
130 struct edac_device_ctl_info
*edac_dev
= to_ctl_info(kobj
);
131 struct ctl_info_attribute
*ctl_info_attr
= to_ctl_info_attr(attr
);
133 if (ctl_info_attr
->store
)
134 return ctl_info_attr
->store(edac_dev
, buffer
, count
);
138 /* edac_dev file operations for an 'ctl_info' */
139 static struct sysfs_ops device_ctl_info_ops
= {
140 .show
= edac_dev_ctl_info_show
,
141 .store
= edac_dev_ctl_info_store
144 #define CTL_INFO_ATTR(_name,_mode,_show,_store) \
145 static struct ctl_info_attribute attr_ctl_info_##_name = { \
146 .attr = {.name = __stringify(_name), .mode = _mode }, \
151 /* Declare the various ctl_info attributes here and their respective ops */
152 CTL_INFO_ATTR(log_ue
, S_IRUGO
| S_IWUSR
,
153 edac_device_ctl_log_ue_show
, edac_device_ctl_log_ue_store
);
154 CTL_INFO_ATTR(log_ce
, S_IRUGO
| S_IWUSR
,
155 edac_device_ctl_log_ce_show
, edac_device_ctl_log_ce_store
);
156 CTL_INFO_ATTR(panic_on_ue
, S_IRUGO
| S_IWUSR
,
157 edac_device_ctl_panic_on_ue_show
,
158 edac_device_ctl_panic_on_ue_store
);
159 CTL_INFO_ATTR(poll_msec
, S_IRUGO
| S_IWUSR
,
160 edac_device_ctl_poll_msec_show
, edac_device_ctl_poll_msec_store
);
162 /* Base Attributes of the EDAC_DEVICE ECC object */
163 static struct ctl_info_attribute
*device_ctrl_attr
[] = {
164 &attr_ctl_info_panic_on_ue
,
165 &attr_ctl_info_log_ue
,
166 &attr_ctl_info_log_ce
,
167 &attr_ctl_info_poll_msec
,
171 /* Main DEVICE kobject release() function */
172 static void edac_device_ctrl_master_release(struct kobject
*kobj
)
174 struct edac_device_ctl_info
*edac_dev
;
176 edac_dev
= to_edacdev(kobj
);
178 debugf1("%s()\n", __func__
);
179 complete(&edac_dev
->kobj_complete
);
182 static struct kobj_type ktype_device_ctrl
= {
183 .release
= edac_device_ctrl_master_release
,
184 .sysfs_ops
= &device_ctl_info_ops
,
185 .default_attrs
= (struct attribute
**)device_ctrl_attr
,
188 /**************** edac_device main kobj ctor/dtor code *********************/
191 * edac_device_register_main_kobj
193 * perform the high level setup for the new edac_device instance
198 static int edac_device_register_main_kobj(struct edac_device_ctl_info
*edac_dev
)
201 struct sysdev_class
*edac_class
;
203 debugf1("%s()\n", __func__
);
205 /* get the /sys/devices/system/edac reference */
206 edac_class
= edac_get_edac_class();
207 if (edac_class
== NULL
) {
208 debugf1("%s() no edac_class error=%d\n", __func__
, err
);
212 /* Point to the 'edac_class' this instance 'reports' to */
213 edac_dev
->edac_class
= edac_class
;
215 /* Init the devices's kobject */
216 memset(&edac_dev
->kobj
, 0, sizeof(struct kobject
));
217 edac_dev
->kobj
.ktype
= &ktype_device_ctrl
;
219 /* set this new device under the edac_class kobject */
220 edac_dev
->kobj
.parent
= &edac_class
->kset
.kobj
;
222 /* generate sysfs "..../edac/<name>" */
223 debugf1("%s() set name of kobject to: %s\n", __func__
, edac_dev
->name
);
224 err
= kobject_set_name(&edac_dev
->kobj
, "%s", edac_dev
->name
);
227 err
= kobject_register(&edac_dev
->kobj
);
229 debugf1("%s()Failed to register '.../edac/%s'\n",
230 __func__
, edac_dev
->name
);
234 debugf1("%s() Registered '.../edac/%s' kobject\n",
235 __func__
, edac_dev
->name
);
241 * edac_device_unregister_main_kobj:
242 * the '..../edac/<name>' kobject
244 static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
247 debugf0("%s()\n", __func__
);
248 debugf1("%s() name of kobject is: %s\n",
249 __func__
, kobject_name(&edac_dev
->kobj
));
251 init_completion(&edac_dev
->kobj_complete
);
254 * Unregister the edac device's kobject and
255 * wait for reference count to reach 0.
257 kobject_unregister(&edac_dev
->kobj
);
258 wait_for_completion(&edac_dev
->kobj_complete
);
261 /*************** edac_dev -> instance information ***********/
264 * Set of low-level instance attribute show functions
266 static ssize_t
instance_ue_count_show(struct edac_device_instance
*instance
,
269 return sprintf(data
, "%u\n", instance
->counters
.ue_count
);
272 static ssize_t
instance_ce_count_show(struct edac_device_instance
*instance
,
275 return sprintf(data
, "%u\n", instance
->counters
.ce_count
);
278 #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
279 #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
281 /* DEVICE instance kobject release() function */
282 static void edac_device_ctrl_instance_release(struct kobject
*kobj
)
284 struct edac_device_instance
*instance
;
286 debugf1("%s()\n", __func__
);
288 instance
= to_instance(kobj
);
289 complete(&instance
->kobj_complete
);
292 /* instance specific attribute structure */
293 struct instance_attribute
{
294 struct attribute attr
;
295 ssize_t(*show
) (struct edac_device_instance
*, char *);
296 ssize_t(*store
) (struct edac_device_instance
*, const char *, size_t);
299 /* Function to 'show' fields from the edac_dev 'instance' structure */
300 static ssize_t
edac_dev_instance_show(struct kobject
*kobj
,
301 struct attribute
*attr
, char *buffer
)
303 struct edac_device_instance
*instance
= to_instance(kobj
);
304 struct instance_attribute
*instance_attr
= to_instance_attr(attr
);
306 if (instance_attr
->show
)
307 return instance_attr
->show(instance
, buffer
);
311 /* Function to 'store' fields into the edac_dev 'instance' structure */
312 static ssize_t
edac_dev_instance_store(struct kobject
*kobj
,
313 struct attribute
*attr
,
314 const char *buffer
, size_t count
)
316 struct edac_device_instance
*instance
= to_instance(kobj
);
317 struct instance_attribute
*instance_attr
= to_instance_attr(attr
);
319 if (instance_attr
->store
)
320 return instance_attr
->store(instance
, buffer
, count
);
324 /* edac_dev file operations for an 'instance' */
325 static struct sysfs_ops device_instance_ops
= {
326 .show
= edac_dev_instance_show
,
327 .store
= edac_dev_instance_store
330 #define INSTANCE_ATTR(_name,_mode,_show,_store) \
331 static struct instance_attribute attr_instance_##_name = { \
332 .attr = {.name = __stringify(_name), .mode = _mode }, \
338 * Define attributes visible for the edac_device instance object
339 * Each contains a pointer to a show and an optional set
340 * function pointer that does the low level output/input
342 INSTANCE_ATTR(ce_count
, S_IRUGO
, instance_ce_count_show
, NULL
);
343 INSTANCE_ATTR(ue_count
, S_IRUGO
, instance_ue_count_show
, NULL
);
345 /* list of edac_dev 'instance' attributes */
346 static struct instance_attribute
*device_instance_attr
[] = {
347 &attr_instance_ce_count
,
348 &attr_instance_ue_count
,
352 /* The 'ktype' for each edac_dev 'instance' */
353 static struct kobj_type ktype_instance_ctrl
= {
354 .release
= edac_device_ctrl_instance_release
,
355 .sysfs_ops
= &device_instance_ops
,
356 .default_attrs
= (struct attribute
**)device_instance_attr
,
359 /*************** edac_dev -> instance -> block information *********/
362 * Set of low-level block attribute show functions
364 static ssize_t
block_ue_count_show(struct edac_device_block
*block
, char *data
)
366 return sprintf(data
, "%u\n", block
->counters
.ue_count
);
369 static ssize_t
block_ce_count_show(struct edac_device_block
*block
, char *data
)
371 return sprintf(data
, "%u\n", block
->counters
.ce_count
);
374 #define to_block(k) container_of(k, struct edac_device_block, kobj)
375 #define to_block_attr(a) container_of(a,struct block_attribute,attr)
377 /* DEVICE block kobject release() function */
378 static void edac_device_ctrl_block_release(struct kobject
*kobj
)
380 struct edac_device_block
*block
;
382 debugf1("%s()\n", __func__
);
384 block
= to_block(kobj
);
385 complete(&block
->kobj_complete
);
388 /* block specific attribute structure */
389 struct block_attribute
{
390 struct attribute attr
;
391 ssize_t(*show
) (struct edac_device_block
*, char *);
392 ssize_t(*store
) (struct edac_device_block
*, const char *, size_t);
395 /* Function to 'show' fields from the edac_dev 'block' structure */
396 static ssize_t
edac_dev_block_show(struct kobject
*kobj
,
397 struct attribute
*attr
, char *buffer
)
399 struct edac_device_block
*block
= to_block(kobj
);
400 struct block_attribute
*block_attr
= to_block_attr(attr
);
402 if (block_attr
->show
)
403 return block_attr
->show(block
, buffer
);
407 /* Function to 'store' fields into the edac_dev 'block' structure */
408 static ssize_t
edac_dev_block_store(struct kobject
*kobj
,
409 struct attribute
*attr
,
410 const char *buffer
, size_t count
)
412 struct edac_device_block
*block
= to_block(kobj
);
413 struct block_attribute
*block_attr
= to_block_attr(attr
);
415 if (block_attr
->store
)
416 return block_attr
->store(block
, buffer
, count
);
420 /* edac_dev file operations for a 'block' */
421 static struct sysfs_ops device_block_ops
= {
422 .show
= edac_dev_block_show
,
423 .store
= edac_dev_block_store
426 #define BLOCK_ATTR(_name,_mode,_show,_store) \
427 static struct block_attribute attr_block_##_name = { \
428 .attr = {.name = __stringify(_name), .mode = _mode }, \
433 BLOCK_ATTR(ce_count
, S_IRUGO
, block_ce_count_show
, NULL
);
434 BLOCK_ATTR(ue_count
, S_IRUGO
, block_ue_count_show
, NULL
);
436 /* list of edac_dev 'block' attributes */
437 static struct block_attribute
*device_block_attr
[] = {
438 &attr_block_ce_count
,
439 &attr_block_ue_count
,
443 /* The 'ktype' for each edac_dev 'block' */
444 static struct kobj_type ktype_block_ctrl
= {
445 .release
= edac_device_ctrl_block_release
,
446 .sysfs_ops
= &device_block_ops
,
447 .default_attrs
= (struct attribute
**)device_block_attr
,
450 /************** block ctor/dtor code ************/
453 * edac_device_create_block
455 static int edac_device_create_block(struct edac_device_ctl_info
*edac_dev
,
456 struct edac_device_instance
*instance
,
460 struct edac_device_block
*block
;
462 block
= &instance
->blocks
[idx
];
464 debugf1("%s() Instance '%s' block[%d] '%s'\n",
465 __func__
, instance
->name
, idx
, block
->name
);
467 /* init this block's kobject */
468 memset(&block
->kobj
, 0, sizeof(struct kobject
));
469 block
->kobj
.parent
= &instance
->kobj
;
470 block
->kobj
.ktype
= &ktype_block_ctrl
;
472 err
= kobject_set_name(&block
->kobj
, "%s", block
->name
);
476 err
= kobject_register(&block
->kobj
);
478 debugf1("%s()Failed to register instance '%s'\n",
479 __func__
, block
->name
);
487 * edac_device_delete_block(edac_dev,j);
489 static void edac_device_delete_block(struct edac_device_ctl_info
*edac_dev
,
490 struct edac_device_instance
*instance
,
493 struct edac_device_block
*block
;
495 block
= &instance
->blocks
[idx
];
497 /* unregister this block's kobject */
498 init_completion(&block
->kobj_complete
);
499 kobject_unregister(&block
->kobj
);
500 wait_for_completion(&block
->kobj_complete
);
503 /************** instance ctor/dtor code ************/
506 * edac_device_create_instance
507 * create just one instance of an edac_device 'instance'
509 static int edac_device_create_instance(struct edac_device_ctl_info
*edac_dev
,
514 struct edac_device_instance
*instance
;
516 instance
= &edac_dev
->instances
[idx
];
518 /* Init the instance's kobject */
519 memset(&instance
->kobj
, 0, sizeof(struct kobject
));
521 /* set this new device under the edac_device main kobject */
522 instance
->kobj
.parent
= &edac_dev
->kobj
;
523 instance
->kobj
.ktype
= &ktype_instance_ctrl
;
525 err
= kobject_set_name(&instance
->kobj
, "%s", instance
->name
);
529 err
= kobject_register(&instance
->kobj
);
531 debugf2("%s() Failed to register instance '%s'\n",
532 __func__
, instance
->name
);
536 debugf1("%s() now register '%d' blocks for instance %d\n",
537 __func__
, instance
->nr_blocks
, idx
);
539 /* register all blocks of this instance */
540 for (i
= 0; i
< instance
->nr_blocks
; i
++) {
541 err
= edac_device_create_block(edac_dev
, instance
, i
);
543 for (j
= 0; j
< i
; j
++) {
544 edac_device_delete_block(edac_dev
, instance
, j
);
550 debugf1("%s() Registered instance %d '%s' kobject\n",
551 __func__
, idx
, instance
->name
);
557 * edac_device_remove_instance
558 * remove an edac_device instance
560 static void edac_device_delete_instance(struct edac_device_ctl_info
*edac_dev
,
564 struct edac_device_instance
*instance
;
566 instance
= &edac_dev
->instances
[idx
];
568 /* unregister all blocks in this instance */
569 for (i
= 0; i
< instance
->nr_blocks
; i
++) {
570 edac_device_delete_block(edac_dev
, instance
, i
);
573 /* unregister this instance's kobject */
574 init_completion(&instance
->kobj_complete
);
575 kobject_unregister(&instance
->kobj
);
576 wait_for_completion(&instance
->kobj_complete
);
580 * edac_device_create_instances
581 * create the first level of 'instances' for this device
582 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
584 static int edac_device_create_instances(struct edac_device_ctl_info
*edac_dev
)
589 debugf0("%s()\n", __func__
);
591 /* iterate over creation of the instances */
592 for (i
= 0; i
< edac_dev
->nr_instances
; i
++) {
593 err
= edac_device_create_instance(edac_dev
, i
);
595 /* unwind previous instances on error */
596 for (j
= 0; j
< i
; j
++) {
597 edac_device_delete_instance(edac_dev
, j
);
607 * edac_device_delete_instances(edac_dev);
608 * unregister all the kobjects of the instances
610 static void edac_device_delete_instances(struct edac_device_ctl_info
*edac_dev
)
614 /* iterate over creation of the instances */
615 for (i
= 0; i
< edac_dev
->nr_instances
; i
++) {
616 edac_device_delete_instance(edac_dev
, i
);
620 /******************* edac_dev sysfs ctor/dtor code *************/
623 * edac_device_add_sysfs_attributes
624 * add some attributes to this instance's main kobject
626 static int edac_device_add_sysfs_attributes(
627 struct edac_device_ctl_info
*edac_dev
)
630 struct edac_dev_sysfs_attribute
*sysfs_attrib
;
632 /* point to the start of the array and iterate over it
633 * adding each attribute listed to this mci instance's kobject
635 sysfs_attrib
= edac_dev
->sysfs_attributes
;
637 while (sysfs_attrib
->attr
.name
!= NULL
) {
638 err
= sysfs_create_file(&edac_dev
->kobj
,
639 (struct attribute
*) sysfs_attrib
);
651 * edac_device_create_sysfs() Constructor
653 * Create a new edac_device kobject instance,
659 int edac_device_create_sysfs(struct edac_device_ctl_info
*edac_dev
)
662 struct kobject
*edac_kobj
= &edac_dev
->kobj
;
664 /* register this instance's main kobj with the edac class kobj */
665 err
= edac_device_register_main_kobj(edac_dev
);
669 debugf0("%s() idx=%d\n", __func__
, edac_dev
->dev_idx
);
671 /* If the low level driver requests some sysfs entries
672 * then go create them here
674 if (edac_dev
->sysfs_attributes
!= NULL
) {
675 err
= edac_device_add_sysfs_attributes(edac_dev
);
677 debugf0("%s() failed to add sysfs attribs\n",
679 goto err_unreg_object
;
683 /* create a symlink from the edac device
684 * to the platform 'device' being used for this
686 err
= sysfs_create_link(edac_kobj
,
687 &edac_dev
->dev
->kobj
, EDAC_DEVICE_SYMLINK
);
689 debugf0("%s() sysfs_create_link() returned err= %d\n",
691 goto err_unreg_object
;
694 debugf0("%s() calling create-instances, idx=%d\n",
695 __func__
, edac_dev
->dev_idx
);
697 /* Create the first level instance directories */
698 err
= edac_device_create_instances(edac_dev
);
700 goto err_remove_link
;
705 /* Error unwind stack */
707 /* remove the sym link */
708 sysfs_remove_link(&edac_dev
->kobj
, EDAC_DEVICE_SYMLINK
);
711 edac_device_unregister_main_kobj(edac_dev
);
717 * edac_device_remove_sysfs() destructor
719 * remove a edac_device instance
721 void edac_device_remove_sysfs(struct edac_device_ctl_info
*edac_dev
)
723 debugf0("%s()\n", __func__
);
725 edac_device_delete_instances(edac_dev
);
727 /* remove the sym link */
728 sysfs_remove_link(&edac_dev
->kobj
, EDAC_DEVICE_SYMLINK
);
730 /* unregister the instance's main kobj */
731 edac_device_unregister_main_kobj(edac_dev
);