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)
26 static ssize_t edac_dev_ue_count_show(struct edac_device_ctl_info *edac_dev,
29 return sprintf(data, "%d\n", edac_dev->ue_count);
32 static ssize_t edac_dev_ce_count_show(struct edac_device_ctl_info *edac_dev,
35 return sprintf(data, "%d\n", edac_dev->ce_count);
38 static ssize_t edac_dev_seconds_show(struct edac_device_ctl_info *edac_dev,
41 return sprintf(data, "%ld\n", (jiffies - edac_dev->start_time) / HZ);
44 static ssize_t edac_dev_ctl_name_show(struct edac_device_ctl_info *edac_dev,
47 return sprintf(data, "%s\n", edac_dev->ctl_name);
50 struct edacdev_attribute {
51 struct attribute attr;
52 ssize_t(*show) (struct edac_device_ctl_info *, char *);
53 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
56 /* EDAC DEVICE show/store functions for top most object */
57 static ssize_t edacdev_show(struct kobject *kobj, struct attribute *attr,
60 struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
61 struct edacdev_attribute *edacdev_attr = to_edacdev_attr(attr);
63 if (edacdev_attr->show)
64 return edacdev_attr->show(edac_dev, buffer);
69 static ssize_t edacdev_store(struct kobject *kobj, struct attribute *attr,
70 const char *buffer, size_t count)
72 struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
73 struct edacdev_attribute *edacdev_attr = to_edacdev_attr(attr);
75 if (edacdev_attr->store)
76 return edacdev_attr->store(edac_dev, buffer, count);
81 static struct sysfs_ops edac_dev_ops = {
83 .store = edacdev_store
86 #define EDACDEV_ATTR(_name,_mode,_show,_store) \
87 static struct edacdev_attribute edac_dev_attr_##_name = { \
88 .attr = {.name = __stringify(_name), .mode = _mode }, \
93 /* default Control file */
94 EDACDEV_ATTR(reset_counters, S_IWUSR, NULL, edac_dev_reset_counters_store);
96 /* default Attribute files */
97 EDACDEV_ATTR(mc_name, S_IRUGO, edac_dev_ctl_name_show, NULL);
98 EDACDEV_ATTR(seconds_since_reset, S_IRUGO, edac_dev_seconds_show, NULL);
99 EDACDEV_ATTR(ue_count, S_IRUGO, edac_dev_ue_count_show, NULL);
100 EDACDEV_ATTR(ce_count, S_IRUGO, edac_dev_ce_count_show, NULL);
102 static struct edacdev_attribute *edacdev_attr[] = {
103 &edacdev_attr_reset_counters,
104 &edacdev_attr_mc_name,
105 &edacdev_attr_seconds_since_reset,
106 &edacdev_attr_ue_count,
107 &edacdev_attr_ce_count,
112 * Release of a Edac Device controlling instance
114 static void edac_dev_instance_release(struct kobject *kobj)
116 struct edac_device_ctl_info *edac_dev;
118 edac_dev = to_edacdev(kobj);
119 debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
120 complete(&edac_dev->kobj_complete);
123 static struct kobj_type ktype_device = {
124 .release = edac_dev_instance_release,
125 .sysfs_ops = &edacdev_ops,
126 .default_attrs = (struct attribute **)edacdev_attr,
131 /************************** edac_device sysfs code and data **************/
134 * Set of edac_device_ctl_info attribute store/show functions
138 static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
139 *ctl_info, char *data)
141 return sprintf(data, "%u\n", ctl_info->log_ue);
144 static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
145 *ctl_info, const char *data,
148 /* if parameter is zero, turn off flag, if non-zero turn on flag */
149 ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
155 static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
156 *ctl_info, char *data)
158 return sprintf(data, "%u\n", ctl_info->log_ce);
161 static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
162 *ctl_info, const char *data,
165 /* if parameter is zero, turn off flag, if non-zero turn on flag */
166 ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
172 static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
173 *ctl_info, char *data)
175 return sprintf(data, "%u\n", ctl_info->panic_on_ue);
178 static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
179 *ctl_info, const char *data,
182 /* if parameter is zero, turn off flag, if non-zero turn on flag */
183 ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
188 /* 'poll_msec' show and store functions*/
189 static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
190 *ctl_info, char *data)
192 return sprintf(data, "%u\n", ctl_info->poll_msec);
195 static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
196 *ctl_info, const char *data,
201 /* get the value and enforce that it is non-zero, must be at least
202 * one millisecond for the delay period, between scans
203 * Then cancel last outstanding delay for the work request
206 value = simple_strtoul(data, NULL, 0);
207 edac_device_reset_delay_period(ctl_info, value);
212 /* edac_device_ctl_info specific attribute structure */
213 struct ctl_info_attribute {
214 struct attribute attr;
215 ssize_t(*show) (struct edac_device_ctl_info *, char *);
216 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
219 #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
220 #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
222 /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
223 static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
224 struct attribute *attr, char *buffer)
226 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
227 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
229 if (ctl_info_attr->show)
230 return ctl_info_attr->show(edac_dev, buffer);
234 /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
235 static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
236 struct attribute *attr,
237 const char *buffer, size_t count)
239 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
240 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
242 if (ctl_info_attr->store)
243 return ctl_info_attr->store(edac_dev, buffer, count);
247 /* edac_dev file operations for an 'ctl_info' */
248 static struct sysfs_ops device_ctl_info_ops = {
249 .show = edac_dev_ctl_info_show,
250 .store = edac_dev_ctl_info_store
253 #define CTL_INFO_ATTR(_name,_mode,_show,_store) \
254 static struct ctl_info_attribute attr_ctl_info_##_name = { \
255 .attr = {.name = __stringify(_name), .mode = _mode }, \
260 /* Declare the various ctl_info attributes here and their respective ops */
261 CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
262 edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
263 CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
264 edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
265 CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
266 edac_device_ctl_panic_on_ue_show,
267 edac_device_ctl_panic_on_ue_store);
268 CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
269 edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
271 /* Base Attributes of the EDAC_DEVICE ECC object */
272 static struct ctl_info_attribute *device_ctrl_attr[] = {
273 &attr_ctl_info_panic_on_ue,
274 &attr_ctl_info_log_ue,
275 &attr_ctl_info_log_ce,
276 &attr_ctl_info_poll_msec,
280 /* Main DEVICE kobject release() function */
281 static void edac_device_ctrl_master_release(struct kobject *kobj)
283 struct edac_device_ctl_info *edac_dev;
285 edac_dev = to_edacdev(kobj);
287 debugf1("%s()\n", __func__);
288 complete(&edac_dev->kobj_complete);
291 static struct kobj_type ktype_device_ctrl = {
292 .release = edac_device_ctrl_master_release,
293 .sysfs_ops = &device_ctl_info_ops,
294 .default_attrs = (struct attribute **)device_ctrl_attr,
297 /**************** edac_device main kobj ctor/dtor code *********************/
300 * edac_device_register_main_kobj
302 * perform the high level setup for the new edac_device instance
307 static int edac_device_register_main_kobj(struct edac_device_ctl_info *edac_dev)
310 struct sysdev_class *edac_class;
312 debugf1("%s()\n", __func__);
314 /* get the /sys/devices/system/edac reference */
315 edac_class = edac_get_edac_class();
316 if (edac_class == NULL) {
317 debugf1("%s() no edac_class error=%d\n", __func__, err);
321 /* Point to the 'edac_class' this instance 'reports' to */
322 edac_dev->edac_class = edac_class;
324 /* Init the devices's kobject */
325 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
326 edac_dev->kobj.ktype = &ktype_device_ctrl;
328 /* set this new device under the edac_class kobject */
329 edac_dev->kobj.parent = &edac_class->kset.kobj;
331 /* generate sysfs "..../edac/<name>" */
332 debugf1("%s() set name of kobject to: %s\n", __func__, edac_dev->name);
333 err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name);
336 err = kobject_register(&edac_dev->kobj);
338 debugf1("%s()Failed to register '.../edac/%s'\n",
339 __func__, edac_dev->name);
343 debugf1("%s() Registered '.../edac/%s' kobject\n",
344 __func__, edac_dev->name);
350 * edac_device_unregister_main_kobj:
351 * the '..../edac/<name>' kobject
353 static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
356 debugf0("%s()\n", __func__);
357 debugf1("%s() name of kobject is: %s\n",
358 __func__, kobject_name(&edac_dev->kobj));
360 init_completion(&edac_dev->kobj_complete);
363 * Unregister the edac device's kobject and
364 * wait for reference count to reach 0.
366 kobject_unregister(&edac_dev->kobj);
367 wait_for_completion(&edac_dev->kobj_complete);
370 /*************** edac_dev -> instance information ***********/
373 * Set of low-level instance attribute show functions
375 static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
378 return sprintf(data, "%u\n", instance->counters.ue_count);
381 static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
384 return sprintf(data, "%u\n", instance->counters.ce_count);
387 #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
388 #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
390 /* DEVICE instance kobject release() function */
391 static void edac_device_ctrl_instance_release(struct kobject *kobj)
393 struct edac_device_instance *instance;
395 debugf1("%s()\n", __func__);
397 instance = to_instance(kobj);
398 complete(&instance->kobj_complete);
401 /* instance specific attribute structure */
402 struct instance_attribute {
403 struct attribute attr;
404 ssize_t(*show) (struct edac_device_instance *, char *);
405 ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
408 /* Function to 'show' fields from the edac_dev 'instance' structure */
409 static ssize_t edac_dev_instance_show(struct kobject *kobj,
410 struct attribute *attr, char *buffer)
412 struct edac_device_instance *instance = to_instance(kobj);
413 struct instance_attribute *instance_attr = to_instance_attr(attr);
415 if (instance_attr->show)
416 return instance_attr->show(instance, buffer);
420 /* Function to 'store' fields into the edac_dev 'instance' structure */
421 static ssize_t edac_dev_instance_store(struct kobject *kobj,
422 struct attribute *attr,
423 const char *buffer, size_t count)
425 struct edac_device_instance *instance = to_instance(kobj);
426 struct instance_attribute *instance_attr = to_instance_attr(attr);
428 if (instance_attr->store)
429 return instance_attr->store(instance, buffer, count);
433 /* edac_dev file operations for an 'instance' */
434 static struct sysfs_ops device_instance_ops = {
435 .show = edac_dev_instance_show,
436 .store = edac_dev_instance_store
439 #define INSTANCE_ATTR(_name,_mode,_show,_store) \
440 static struct instance_attribute attr_instance_##_name = { \
441 .attr = {.name = __stringify(_name), .mode = _mode }, \
447 * Define attributes visible for the edac_device instance object
448 * Each contains a pointer to a show and an optional set
449 * function pointer that does the low level output/input
451 INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
452 INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
454 /* list of edac_dev 'instance' attributes */
455 static struct instance_attribute *device_instance_attr[] = {
456 &attr_instance_ce_count,
457 &attr_instance_ue_count,
461 /* The 'ktype' for each edac_dev 'instance' */
462 static struct kobj_type ktype_instance_ctrl = {
463 .release = edac_device_ctrl_instance_release,
464 .sysfs_ops = &device_instance_ops,
465 .default_attrs = (struct attribute **)device_instance_attr,
468 /*************** edac_dev -> instance -> block information *********/
471 * Set of low-level block attribute show functions
473 static ssize_t block_ue_count_show(struct edac_device_block *block, char *data)
475 return sprintf(data, "%u\n", block->counters.ue_count);
478 static ssize_t block_ce_count_show(struct edac_device_block *block, char *data)
480 return sprintf(data, "%u\n", block->counters.ce_count);
483 #define to_block(k) container_of(k, struct edac_device_block, kobj)
484 #define to_block_attr(a) container_of(a,struct block_attribute,attr)
486 /* DEVICE block kobject release() function */
487 static void edac_device_ctrl_block_release(struct kobject *kobj)
489 struct edac_device_block *block;
491 debugf1("%s()\n", __func__);
493 block = to_block(kobj);
494 complete(&block->kobj_complete);
497 /* block specific attribute structure */
498 struct block_attribute {
499 struct attribute attr;
500 ssize_t(*show) (struct edac_device_block *, char *);
501 ssize_t(*store) (struct edac_device_block *, const char *, size_t);
504 /* Function to 'show' fields from the edac_dev 'block' structure */
505 static ssize_t edac_dev_block_show(struct kobject *kobj,
506 struct attribute *attr, char *buffer)
508 struct edac_device_block *block = to_block(kobj);
509 struct block_attribute *block_attr = to_block_attr(attr);
511 if (block_attr->show)
512 return block_attr->show(block, buffer);
516 /* Function to 'store' fields into the edac_dev 'block' structure */
517 static ssize_t edac_dev_block_store(struct kobject *kobj,
518 struct attribute *attr,
519 const char *buffer, size_t count)
521 struct edac_device_block *block = to_block(kobj);
522 struct block_attribute *block_attr = to_block_attr(attr);
524 if (block_attr->store)
525 return block_attr->store(block, buffer, count);
529 /* edac_dev file operations for a 'block' */
530 static struct sysfs_ops device_block_ops = {
531 .show = edac_dev_block_show,
532 .store = edac_dev_block_store
535 #define BLOCK_ATTR(_name,_mode,_show,_store) \
536 static struct block_attribute attr_block_##_name = { \
537 .attr = {.name = __stringify(_name), .mode = _mode }, \
542 BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
543 BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
545 /* list of edac_dev 'block' attributes */
546 static struct block_attribute *device_block_attr[] = {
547 &attr_block_ce_count,
548 &attr_block_ue_count,
552 /* The 'ktype' for each edac_dev 'block' */
553 static struct kobj_type ktype_block_ctrl = {
554 .release = edac_device_ctrl_block_release,
555 .sysfs_ops = &device_block_ops,
556 .default_attrs = (struct attribute **)device_block_attr,
559 /************** block ctor/dtor code ************/
562 * edac_device_create_block
564 static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
565 struct edac_device_instance *instance,
569 struct edac_device_block *block;
571 block = &instance->blocks[idx];
573 debugf1("%s() Instance '%s' block[%d] '%s'\n",
574 __func__, instance->name, idx, block->name);
576 /* init this block's kobject */
577 memset(&block->kobj, 0, sizeof(struct kobject));
578 block->kobj.parent = &instance->kobj;
579 block->kobj.ktype = &ktype_block_ctrl;
581 err = kobject_set_name(&block->kobj, "%s", block->name);
585 err = kobject_register(&block->kobj);
587 debugf1("%s()Failed to register instance '%s'\n",
588 __func__, block->name);
596 * edac_device_delete_block(edac_dev,j);
598 static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
599 struct edac_device_instance *instance,
602 struct edac_device_block *block;
604 block = &instance->blocks[idx];
606 /* unregister this block's kobject */
607 init_completion(&block->kobj_complete);
608 kobject_unregister(&block->kobj);
609 wait_for_completion(&block->kobj_complete);
612 /************** instance ctor/dtor code ************/
615 * edac_device_create_instance
616 * create just one instance of an edac_device 'instance'
618 static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
623 struct edac_device_instance *instance;
625 instance = &edac_dev->instances[idx];
627 /* Init the instance's kobject */
628 memset(&instance->kobj, 0, sizeof(struct kobject));
630 /* set this new device under the edac_device main kobject */
631 instance->kobj.parent = &edac_dev->kobj;
632 instance->kobj.ktype = &ktype_instance_ctrl;
634 err = kobject_set_name(&instance->kobj, "%s", instance->name);
638 err = kobject_register(&instance->kobj);
640 debugf2("%s() Failed to register instance '%s'\n",
641 __func__, instance->name);
645 debugf1("%s() now register '%d' blocks for instance %d\n",
646 __func__, instance->nr_blocks, idx);
648 /* register all blocks of this instance */
649 for (i = 0; i < instance->nr_blocks; i++) {
650 err = edac_device_create_block(edac_dev, instance, i);
652 for (j = 0; j < i; j++) {
653 edac_device_delete_block(edac_dev, instance, j);
659 debugf1("%s() Registered instance %d '%s' kobject\n",
660 __func__, idx, instance->name);
666 * edac_device_remove_instance
667 * remove an edac_device instance
669 static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
673 struct edac_device_instance *instance;
675 instance = &edac_dev->instances[idx];
677 /* unregister all blocks in this instance */
678 for (i = 0; i < instance->nr_blocks; i++) {
679 edac_device_delete_block(edac_dev, instance, i);
682 /* unregister this instance's kobject */
683 init_completion(&instance->kobj_complete);
684 kobject_unregister(&instance->kobj);
685 wait_for_completion(&instance->kobj_complete);
689 * edac_device_create_instances
690 * create the first level of 'instances' for this device
691 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
693 static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
698 debugf0("%s()\n", __func__);
700 /* iterate over creation of the instances */
701 for (i = 0; i < edac_dev->nr_instances; i++) {
702 err = edac_device_create_instance(edac_dev, i);
704 /* unwind previous instances on error */
705 for (j = 0; j < i; j++) {
706 edac_device_delete_instance(edac_dev, j);
716 * edac_device_delete_instances(edac_dev);
717 * unregister all the kobjects of the instances
719 static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
723 /* iterate over creation of the instances */
724 for (i = 0; i < edac_dev->nr_instances; i++) {
725 edac_device_delete_instance(edac_dev, i);
729 /******************* edac_dev sysfs ctor/dtor code *************/
732 * edac_device_create_sysfs() Constructor
734 * Create a new edac_device kobject instance,
740 int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
743 struct kobject *edac_kobj = &edac_dev->kobj;
745 /* register this instance's main kobj with the edac class kobj */
746 err = edac_device_register_main_kobj(edac_dev);
750 debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
752 /* create a symlink from the edac device
753 * to the platform 'device' being used for this
755 err = sysfs_create_link(edac_kobj,
756 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
758 debugf0("%s() sysfs_create_link() returned err= %d\n",
763 debugf0("%s() calling create-instances, idx=%d\n",
764 __func__, edac_dev->dev_idx);
766 /* Create the first level instance directories */
767 err = edac_device_create_instances(edac_dev);
774 /* Error unwind stack */
777 edac_device_unregister_main_kobj(edac_dev);
783 * edac_device_remove_sysfs() destructor
785 * remove a edac_device instance
787 void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
789 debugf0("%s()\n", __func__);
791 edac_device_delete_instances(edac_dev);
793 /* remove the sym link */
794 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
796 /* unregister the instance's main kobj */
797 edac_device_unregister_main_kobj(edac_dev);