Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * file for managing the edac_device subsystem of devices for EDAC
0003  *
0004  * (C) 2007 SoftwareBitMaker
0005  *
0006  * This file may be distributed under the terms of the
0007  * GNU General Public License.
0008  *
0009  * Written Doug Thompson <norsk5@xmission.com>
0010  *
0011  */
0012 
0013 #include <linux/ctype.h>
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016 #include <linux/edac.h>
0017 
0018 #include "edac_device.h"
0019 #include "edac_module.h"
0020 
0021 #define EDAC_DEVICE_SYMLINK "device"
0022 
0023 #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
0024 #define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
0025 
0026 
0027 /*
0028  * Set of edac_device_ctl_info attribute store/show functions
0029  */
0030 
0031 /* 'log_ue' */
0032 static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
0033                     *ctl_info, char *data)
0034 {
0035     return sprintf(data, "%u\n", ctl_info->log_ue);
0036 }
0037 
0038 static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
0039                     *ctl_info, const char *data,
0040                     size_t count)
0041 {
0042     /* if parameter is zero, turn off flag, if non-zero turn on flag */
0043     ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
0044 
0045     return count;
0046 }
0047 
0048 /* 'log_ce' */
0049 static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
0050                     *ctl_info, char *data)
0051 {
0052     return sprintf(data, "%u\n", ctl_info->log_ce);
0053 }
0054 
0055 static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
0056                     *ctl_info, const char *data,
0057                     size_t count)
0058 {
0059     /* if parameter is zero, turn off flag, if non-zero turn on flag */
0060     ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
0061 
0062     return count;
0063 }
0064 
0065 /* 'panic_on_ue' */
0066 static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
0067                         *ctl_info, char *data)
0068 {
0069     return sprintf(data, "%u\n", ctl_info->panic_on_ue);
0070 }
0071 
0072 static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
0073                          *ctl_info, const char *data,
0074                          size_t count)
0075 {
0076     /* if parameter is zero, turn off flag, if non-zero turn on flag */
0077     ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
0078 
0079     return count;
0080 }
0081 
0082 /* 'poll_msec' show and store functions*/
0083 static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
0084                     *ctl_info, char *data)
0085 {
0086     return sprintf(data, "%u\n", ctl_info->poll_msec);
0087 }
0088 
0089 static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
0090                     *ctl_info, const char *data,
0091                     size_t count)
0092 {
0093     unsigned long value;
0094 
0095     /* get the value and enforce that it is non-zero, must be at least
0096      * one millisecond for the delay period, between scans
0097      * Then cancel last outstanding delay for the work request
0098      * and set a new one.
0099      */
0100     value = simple_strtoul(data, NULL, 0);
0101     edac_device_reset_delay_period(ctl_info, value);
0102 
0103     return count;
0104 }
0105 
0106 /* edac_device_ctl_info specific attribute structure */
0107 struct ctl_info_attribute {
0108     struct attribute attr;
0109     ssize_t(*show) (struct edac_device_ctl_info *, char *);
0110     ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
0111 };
0112 
0113 #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
0114 #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
0115 
0116 /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
0117 static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
0118                 struct attribute *attr, char *buffer)
0119 {
0120     struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
0121     struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
0122 
0123     if (ctl_info_attr->show)
0124         return ctl_info_attr->show(edac_dev, buffer);
0125     return -EIO;
0126 }
0127 
0128 /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
0129 static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
0130                 struct attribute *attr,
0131                 const char *buffer, size_t count)
0132 {
0133     struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
0134     struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
0135 
0136     if (ctl_info_attr->store)
0137         return ctl_info_attr->store(edac_dev, buffer, count);
0138     return -EIO;
0139 }
0140 
0141 /* edac_dev file operations for an 'ctl_info' */
0142 static const struct sysfs_ops device_ctl_info_ops = {
0143     .show = edac_dev_ctl_info_show,
0144     .store = edac_dev_ctl_info_store
0145 };
0146 
0147 #define CTL_INFO_ATTR(_name,_mode,_show,_store)        \
0148 static struct ctl_info_attribute attr_ctl_info_##_name = {      \
0149     .attr = {.name = __stringify(_name), .mode = _mode },   \
0150     .show   = _show,                                        \
0151     .store  = _store,                                       \
0152 };
0153 
0154 /* Declare the various ctl_info attributes here and their respective ops */
0155 CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
0156     edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
0157 CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
0158     edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
0159 CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
0160     edac_device_ctl_panic_on_ue_show,
0161     edac_device_ctl_panic_on_ue_store);
0162 CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
0163     edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
0164 
0165 /* Base Attributes of the EDAC_DEVICE ECC object */
0166 static struct attribute *device_ctrl_attrs[] = {
0167     &attr_ctl_info_panic_on_ue.attr,
0168     &attr_ctl_info_log_ue.attr,
0169     &attr_ctl_info_log_ce.attr,
0170     &attr_ctl_info_poll_msec.attr,
0171     NULL,
0172 };
0173 ATTRIBUTE_GROUPS(device_ctrl);
0174 
0175 /*
0176  * edac_device_ctrl_master_release
0177  *
0178  *  called when the reference count for the 'main' kobj
0179  *  for a edac_device control struct reaches zero
0180  *
0181  *  Reference count model:
0182  *      One 'main' kobject for each control structure allocated.
0183  *      That main kobj is initially set to one AND
0184  *      the reference count for the EDAC 'core' module is
0185  *      bumped by one, thus added 'keep in memory' dependency.
0186  *
0187  *      Each new internal kobj (in instances and blocks) then
0188  *      bumps the 'main' kobject.
0189  *
0190  *      When they are released their release functions decrement
0191  *      the 'main' kobj.
0192  *
0193  *      When the main kobj reaches zero (0) then THIS function
0194  *      is called which then decrements the EDAC 'core' module.
0195  *      When the module reference count reaches zero then the
0196  *      module no longer has dependency on keeping the release
0197  *      function code in memory and module can be unloaded.
0198  *
0199  *      This will support several control objects as well, each
0200  *      with its own 'main' kobj.
0201  */
0202 static void edac_device_ctrl_master_release(struct kobject *kobj)
0203 {
0204     struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
0205 
0206     edac_dbg(4, "control index=%d\n", edac_dev->dev_idx);
0207 
0208     /* decrement the EDAC CORE module ref count */
0209     module_put(edac_dev->owner);
0210 
0211     __edac_device_free_ctl_info(edac_dev);
0212 }
0213 
0214 /* ktype for the main (master) kobject */
0215 static struct kobj_type ktype_device_ctrl = {
0216     .release = edac_device_ctrl_master_release,
0217     .sysfs_ops = &device_ctl_info_ops,
0218     .default_groups = device_ctrl_groups,
0219 };
0220 
0221 /*
0222  * edac_device_register_sysfs_main_kobj
0223  *
0224  *  perform the high level setup for the new edac_device instance
0225  *
0226  * Return:  0 SUCCESS
0227  *         !0 FAILURE
0228  */
0229 int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
0230 {
0231     struct bus_type *edac_subsys;
0232     int err;
0233 
0234     edac_dbg(1, "\n");
0235 
0236     /* get the /sys/devices/system/edac reference */
0237     edac_subsys = edac_get_sysfs_subsys();
0238 
0239     /* Point to the 'edac_subsys' this instance 'reports' to */
0240     edac_dev->edac_subsys = edac_subsys;
0241 
0242     /* Init the devices's kobject */
0243     memset(&edac_dev->kobj, 0, sizeof(struct kobject));
0244 
0245     /* Record which module 'owns' this control structure
0246      * and bump the ref count of the module
0247      */
0248     edac_dev->owner = THIS_MODULE;
0249 
0250     if (!try_module_get(edac_dev->owner)) {
0251         err = -ENODEV;
0252         goto err_out;
0253     }
0254 
0255     /* register */
0256     err = kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl,
0257                    &edac_subsys->dev_root->kobj,
0258                    "%s", edac_dev->name);
0259     if (err) {
0260         edac_dbg(1, "Failed to register '.../edac/%s'\n",
0261              edac_dev->name);
0262         goto err_kobj_reg;
0263     }
0264     kobject_uevent(&edac_dev->kobj, KOBJ_ADD);
0265 
0266     /* At this point, to 'free' the control struct,
0267      * edac_device_unregister_sysfs_main_kobj() must be used
0268      */
0269 
0270     edac_dbg(4, "Registered '.../edac/%s' kobject\n", edac_dev->name);
0271 
0272     return 0;
0273 
0274     /* Error exit stack */
0275 err_kobj_reg:
0276     kobject_put(&edac_dev->kobj);
0277     module_put(edac_dev->owner);
0278 
0279 err_out:
0280     return err;
0281 }
0282 
0283 /*
0284  * edac_device_unregister_sysfs_main_kobj:
0285  *  the '..../edac/<name>' kobject
0286  */
0287 void edac_device_unregister_sysfs_main_kobj(struct edac_device_ctl_info *dev)
0288 {
0289     edac_dbg(0, "\n");
0290     edac_dbg(4, "name of kobject is: %s\n", kobject_name(&dev->kobj));
0291 
0292     /*
0293      * Unregister the edac device's kobject and
0294      * allow for reference count to reach 0 at which point
0295      * the callback will be called to:
0296      *   a) module_put() this module
0297      *   b) 'kfree' the memory
0298      */
0299     kobject_put(&dev->kobj);
0300 }
0301 
0302 /* edac_dev -> instance information */
0303 
0304 /*
0305  * Set of low-level instance attribute show functions
0306  */
0307 static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
0308                 char *data)
0309 {
0310     return sprintf(data, "%u\n", instance->counters.ue_count);
0311 }
0312 
0313 static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
0314                 char *data)
0315 {
0316     return sprintf(data, "%u\n", instance->counters.ce_count);
0317 }
0318 
0319 #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
0320 #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
0321 
0322 /* DEVICE instance kobject release() function */
0323 static void edac_device_ctrl_instance_release(struct kobject *kobj)
0324 {
0325     struct edac_device_instance *instance;
0326 
0327     edac_dbg(1, "\n");
0328 
0329     /* map from this kobj to the main control struct
0330      * and then dec the main kobj count
0331      */
0332     instance = to_instance(kobj);
0333     kobject_put(&instance->ctl->kobj);
0334 }
0335 
0336 /* instance specific attribute structure */
0337 struct instance_attribute {
0338     struct attribute attr;
0339     ssize_t(*show) (struct edac_device_instance *, char *);
0340     ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
0341 };
0342 
0343 /* Function to 'show' fields from the edac_dev 'instance' structure */
0344 static ssize_t edac_dev_instance_show(struct kobject *kobj,
0345                 struct attribute *attr, char *buffer)
0346 {
0347     struct edac_device_instance *instance = to_instance(kobj);
0348     struct instance_attribute *instance_attr = to_instance_attr(attr);
0349 
0350     if (instance_attr->show)
0351         return instance_attr->show(instance, buffer);
0352     return -EIO;
0353 }
0354 
0355 /* Function to 'store' fields into the edac_dev 'instance' structure */
0356 static ssize_t edac_dev_instance_store(struct kobject *kobj,
0357                 struct attribute *attr,
0358                 const char *buffer, size_t count)
0359 {
0360     struct edac_device_instance *instance = to_instance(kobj);
0361     struct instance_attribute *instance_attr = to_instance_attr(attr);
0362 
0363     if (instance_attr->store)
0364         return instance_attr->store(instance, buffer, count);
0365     return -EIO;
0366 }
0367 
0368 /* edac_dev file operations for an 'instance' */
0369 static const struct sysfs_ops device_instance_ops = {
0370     .show = edac_dev_instance_show,
0371     .store = edac_dev_instance_store
0372 };
0373 
0374 #define INSTANCE_ATTR(_name,_mode,_show,_store)        \
0375 static struct instance_attribute attr_instance_##_name = {      \
0376     .attr = {.name = __stringify(_name), .mode = _mode },   \
0377     .show   = _show,                                        \
0378     .store  = _store,                                       \
0379 };
0380 
0381 /*
0382  * Define attributes visible for the edac_device instance object
0383  *  Each contains a pointer to a show and an optional set
0384  *  function pointer that does the low level output/input
0385  */
0386 INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
0387 INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
0388 
0389 /* list of edac_dev 'instance' attributes */
0390 static struct attribute *device_instance_attrs[] = {
0391     &attr_instance_ce_count.attr,
0392     &attr_instance_ue_count.attr,
0393     NULL,
0394 };
0395 ATTRIBUTE_GROUPS(device_instance);
0396 
0397 /* The 'ktype' for each edac_dev 'instance' */
0398 static struct kobj_type ktype_instance_ctrl = {
0399     .release = edac_device_ctrl_instance_release,
0400     .sysfs_ops = &device_instance_ops,
0401     .default_groups = device_instance_groups,
0402 };
0403 
0404 /* edac_dev -> instance -> block information */
0405 
0406 #define to_block(k) container_of(k, struct edac_device_block, kobj)
0407 #define to_block_attr(a) \
0408     container_of(a, struct edac_dev_sysfs_block_attribute, attr)
0409 
0410 /*
0411  * Set of low-level block attribute show functions
0412  */
0413 static ssize_t block_ue_count_show(struct kobject *kobj,
0414                     struct attribute *attr, char *data)
0415 {
0416     struct edac_device_block *block = to_block(kobj);
0417 
0418     return sprintf(data, "%u\n", block->counters.ue_count);
0419 }
0420 
0421 static ssize_t block_ce_count_show(struct kobject *kobj,
0422                     struct attribute *attr, char *data)
0423 {
0424     struct edac_device_block *block = to_block(kobj);
0425 
0426     return sprintf(data, "%u\n", block->counters.ce_count);
0427 }
0428 
0429 /* DEVICE block kobject release() function */
0430 static void edac_device_ctrl_block_release(struct kobject *kobj)
0431 {
0432     struct edac_device_block *block;
0433 
0434     edac_dbg(1, "\n");
0435 
0436     /* get the container of the kobj */
0437     block = to_block(kobj);
0438 
0439     /* map from 'block kobj' to 'block->instance->controller->main_kobj'
0440      * now 'release' the block kobject
0441      */
0442     kobject_put(&block->instance->ctl->kobj);
0443 }
0444 
0445 
0446 /* Function to 'show' fields from the edac_dev 'block' structure */
0447 static ssize_t edac_dev_block_show(struct kobject *kobj,
0448                 struct attribute *attr, char *buffer)
0449 {
0450     struct edac_dev_sysfs_block_attribute *block_attr =
0451                         to_block_attr(attr);
0452 
0453     if (block_attr->show)
0454         return block_attr->show(kobj, attr, buffer);
0455     return -EIO;
0456 }
0457 
0458 /* Function to 'store' fields into the edac_dev 'block' structure */
0459 static ssize_t edac_dev_block_store(struct kobject *kobj,
0460                 struct attribute *attr,
0461                 const char *buffer, size_t count)
0462 {
0463     struct edac_dev_sysfs_block_attribute *block_attr;
0464 
0465     block_attr = to_block_attr(attr);
0466 
0467     if (block_attr->store)
0468         return block_attr->store(kobj, attr, buffer, count);
0469     return -EIO;
0470 }
0471 
0472 /* edac_dev file operations for a 'block' */
0473 static const struct sysfs_ops device_block_ops = {
0474     .show = edac_dev_block_show,
0475     .store = edac_dev_block_store
0476 };
0477 
0478 #define BLOCK_ATTR(_name,_mode,_show,_store)        \
0479 static struct edac_dev_sysfs_block_attribute attr_block_##_name = { \
0480     .attr = {.name = __stringify(_name), .mode = _mode },   \
0481     .show   = _show,                                        \
0482     .store  = _store,                                       \
0483 };
0484 
0485 BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
0486 BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
0487 
0488 /* list of edac_dev 'block' attributes */
0489 static struct attribute *device_block_attrs[] = {
0490     &attr_block_ce_count.attr,
0491     &attr_block_ue_count.attr,
0492     NULL,
0493 };
0494 ATTRIBUTE_GROUPS(device_block);
0495 
0496 /* The 'ktype' for each edac_dev 'block' */
0497 static struct kobj_type ktype_block_ctrl = {
0498     .release = edac_device_ctrl_block_release,
0499     .sysfs_ops = &device_block_ops,
0500     .default_groups = device_block_groups,
0501 };
0502 
0503 /* block ctor/dtor  code */
0504 
0505 /*
0506  * edac_device_create_block
0507  */
0508 static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
0509                 struct edac_device_instance *instance,
0510                 struct edac_device_block *block)
0511 {
0512     int i;
0513     int err;
0514     struct edac_dev_sysfs_block_attribute *sysfs_attrib;
0515     struct kobject *main_kobj;
0516 
0517     edac_dbg(4, "Instance '%s' inst_p=%p  block '%s'  block_p=%p\n",
0518          instance->name, instance, block->name, block);
0519     edac_dbg(4, "block kobj=%p  block kobj->parent=%p\n",
0520          &block->kobj, &block->kobj.parent);
0521 
0522     /* init this block's kobject */
0523     memset(&block->kobj, 0, sizeof(struct kobject));
0524 
0525     /* bump the main kobject's reference count for this controller
0526      * and this instance is dependent on the main
0527      */
0528     main_kobj = kobject_get(&edac_dev->kobj);
0529     if (!main_kobj) {
0530         err = -ENODEV;
0531         goto err_out;
0532     }
0533 
0534     /* Add this block's kobject */
0535     err = kobject_init_and_add(&block->kobj, &ktype_block_ctrl,
0536                    &instance->kobj,
0537                    "%s", block->name);
0538     if (err) {
0539         edac_dbg(1, "Failed to register instance '%s'\n", block->name);
0540         kobject_put(main_kobj);
0541         err = -ENODEV;
0542         goto err_out;
0543     }
0544 
0545     /* If there are driver level block attributes, then added them
0546      * to the block kobject
0547      */
0548     sysfs_attrib = block->block_attributes;
0549     if (sysfs_attrib && block->nr_attribs) {
0550         for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
0551 
0552             edac_dbg(4, "creating block attrib='%s' attrib->%p to kobj=%p\n",
0553                  sysfs_attrib->attr.name,
0554                  sysfs_attrib, &block->kobj);
0555 
0556             /* Create each block_attribute file */
0557             err = sysfs_create_file(&block->kobj,
0558                 &sysfs_attrib->attr);
0559             if (err)
0560                 goto err_on_attrib;
0561         }
0562     }
0563     kobject_uevent(&block->kobj, KOBJ_ADD);
0564 
0565     return 0;
0566 
0567     /* Error unwind stack */
0568 err_on_attrib:
0569     kobject_put(&block->kobj);
0570 
0571 err_out:
0572     return err;
0573 }
0574 
0575 /*
0576  * edac_device_delete_block(edac_dev,block);
0577  */
0578 static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
0579                 struct edac_device_block *block)
0580 {
0581     struct edac_dev_sysfs_block_attribute *sysfs_attrib;
0582     int i;
0583 
0584     /* if this block has 'attributes' then we need to iterate over the list
0585      * and 'remove' the attributes on this block
0586      */
0587     sysfs_attrib = block->block_attributes;
0588     if (sysfs_attrib && block->nr_attribs) {
0589         for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
0590 
0591             /* remove each block_attrib file */
0592             sysfs_remove_file(&block->kobj,
0593                 (struct attribute *) sysfs_attrib);
0594         }
0595     }
0596 
0597     /* unregister this block's kobject, SEE:
0598      *  edac_device_ctrl_block_release() callback operation
0599      */
0600     kobject_put(&block->kobj);
0601 }
0602 
0603 /* instance ctor/dtor code */
0604 
0605 /*
0606  * edac_device_create_instance
0607  *  create just one instance of an edac_device 'instance'
0608  */
0609 static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
0610                 int idx)
0611 {
0612     int i, j;
0613     int err;
0614     struct edac_device_instance *instance;
0615     struct kobject *main_kobj;
0616 
0617     instance = &edac_dev->instances[idx];
0618 
0619     /* Init the instance's kobject */
0620     memset(&instance->kobj, 0, sizeof(struct kobject));
0621 
0622     instance->ctl = edac_dev;
0623 
0624     /* bump the main kobject's reference count for this controller
0625      * and this instance is dependent on the main
0626      */
0627     main_kobj = kobject_get(&edac_dev->kobj);
0628     if (!main_kobj) {
0629         err = -ENODEV;
0630         goto err_out;
0631     }
0632 
0633     /* Formally register this instance's kobject under the edac_device */
0634     err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
0635                    &edac_dev->kobj, "%s", instance->name);
0636     if (err != 0) {
0637         edac_dbg(2, "Failed to register instance '%s'\n",
0638              instance->name);
0639         kobject_put(main_kobj);
0640         goto err_out;
0641     }
0642 
0643     edac_dbg(4, "now register '%d' blocks for instance %d\n",
0644          instance->nr_blocks, idx);
0645 
0646     /* register all blocks of this instance */
0647     for (i = 0; i < instance->nr_blocks; i++) {
0648         err = edac_device_create_block(edac_dev, instance,
0649                         &instance->blocks[i]);
0650         if (err) {
0651             /* If any fail, remove all previous ones */
0652             for (j = 0; j < i; j++)
0653                 edac_device_delete_block(edac_dev,
0654                             &instance->blocks[j]);
0655             goto err_release_instance_kobj;
0656         }
0657     }
0658     kobject_uevent(&instance->kobj, KOBJ_ADD);
0659 
0660     edac_dbg(4, "Registered instance %d '%s' kobject\n",
0661          idx, instance->name);
0662 
0663     return 0;
0664 
0665     /* error unwind stack */
0666 err_release_instance_kobj:
0667     kobject_put(&instance->kobj);
0668 
0669 err_out:
0670     return err;
0671 }
0672 
0673 /*
0674  * edac_device_remove_instance
0675  *  remove an edac_device instance
0676  */
0677 static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
0678                     int idx)
0679 {
0680     struct edac_device_instance *instance;
0681     int i;
0682 
0683     instance = &edac_dev->instances[idx];
0684 
0685     /* unregister all blocks in this instance */
0686     for (i = 0; i < instance->nr_blocks; i++)
0687         edac_device_delete_block(edac_dev, &instance->blocks[i]);
0688 
0689     /* unregister this instance's kobject, SEE:
0690      *  edac_device_ctrl_instance_release() for callback operation
0691      */
0692     kobject_put(&instance->kobj);
0693 }
0694 
0695 /*
0696  * edac_device_create_instances
0697  *  create the first level of 'instances' for this device
0698  *  (ie  'cache' might have 'cache0', 'cache1', 'cache2', etc
0699  */
0700 static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
0701 {
0702     int i, j;
0703     int err;
0704 
0705     edac_dbg(0, "\n");
0706 
0707     /* iterate over creation of the instances */
0708     for (i = 0; i < edac_dev->nr_instances; i++) {
0709         err = edac_device_create_instance(edac_dev, i);
0710         if (err) {
0711             /* unwind previous instances on error */
0712             for (j = 0; j < i; j++)
0713                 edac_device_delete_instance(edac_dev, j);
0714             return err;
0715         }
0716     }
0717 
0718     return 0;
0719 }
0720 
0721 /*
0722  * edac_device_delete_instances(edac_dev);
0723  *  unregister all the kobjects of the instances
0724  */
0725 static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
0726 {
0727     int i;
0728 
0729     /* iterate over creation of the instances */
0730     for (i = 0; i < edac_dev->nr_instances; i++)
0731         edac_device_delete_instance(edac_dev, i);
0732 }
0733 
0734 /* edac_dev sysfs ctor/dtor  code */
0735 
0736 /*
0737  * edac_device_add_main_sysfs_attributes
0738  *  add some attributes to this instance's main kobject
0739  */
0740 static int edac_device_add_main_sysfs_attributes(
0741             struct edac_device_ctl_info *edac_dev)
0742 {
0743     struct edac_dev_sysfs_attribute *sysfs_attrib;
0744     int err = 0;
0745 
0746     sysfs_attrib = edac_dev->sysfs_attributes;
0747     if (sysfs_attrib) {
0748         /* iterate over the array and create an attribute for each
0749          * entry in the list
0750          */
0751         while (sysfs_attrib->attr.name != NULL) {
0752             err = sysfs_create_file(&edac_dev->kobj,
0753                 (struct attribute*) sysfs_attrib);
0754             if (err)
0755                 goto err_out;
0756 
0757             sysfs_attrib++;
0758         }
0759     }
0760 
0761 err_out:
0762     return err;
0763 }
0764 
0765 /*
0766  * edac_device_remove_main_sysfs_attributes
0767  *  remove any attributes to this instance's main kobject
0768  */
0769 static void edac_device_remove_main_sysfs_attributes(
0770             struct edac_device_ctl_info *edac_dev)
0771 {
0772     struct edac_dev_sysfs_attribute *sysfs_attrib;
0773 
0774     /* if there are main attributes, defined, remove them. First,
0775      * point to the start of the array and iterate over it
0776      * removing each attribute listed from this device's instance's kobject
0777      */
0778     sysfs_attrib = edac_dev->sysfs_attributes;
0779     if (sysfs_attrib) {
0780         while (sysfs_attrib->attr.name != NULL) {
0781             sysfs_remove_file(&edac_dev->kobj,
0782                     (struct attribute *) sysfs_attrib);
0783             sysfs_attrib++;
0784         }
0785     }
0786 }
0787 
0788 /*
0789  * edac_device_create_sysfs() Constructor
0790  *
0791  * accept a created edac_device control structure
0792  * and 'export' it to sysfs. The 'main' kobj should already have been
0793  * created. 'instance' and 'block' kobjects should be registered
0794  * along with any 'block' attributes from the low driver. In addition,
0795  * the main attributes (if any) are connected to the main kobject of
0796  * the control structure.
0797  *
0798  * Return:
0799  *  0   Success
0800  *  !0  Failure
0801  */
0802 int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
0803 {
0804     int err;
0805     struct kobject *edac_kobj = &edac_dev->kobj;
0806 
0807     edac_dbg(0, "idx=%d\n", edac_dev->dev_idx);
0808 
0809     /*  go create any main attributes callers wants */
0810     err = edac_device_add_main_sysfs_attributes(edac_dev);
0811     if (err) {
0812         edac_dbg(0, "failed to add sysfs attribs\n");
0813         goto err_out;
0814     }
0815 
0816     /* create a symlink from the edac device
0817      * to the platform 'device' being used for this
0818      */
0819     err = sysfs_create_link(edac_kobj,
0820                 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
0821     if (err) {
0822         edac_dbg(0, "sysfs_create_link() returned err= %d\n", err);
0823         goto err_remove_main_attribs;
0824     }
0825 
0826     /* Create the first level instance directories
0827      * In turn, the nested blocks beneath the instances will
0828      * be registered as well
0829      */
0830     err = edac_device_create_instances(edac_dev);
0831     if (err) {
0832         edac_dbg(0, "edac_device_create_instances() returned err= %d\n",
0833              err);
0834         goto err_remove_link;
0835     }
0836 
0837 
0838     edac_dbg(4, "create-instances done, idx=%d\n", edac_dev->dev_idx);
0839 
0840     return 0;
0841 
0842     /* Error unwind stack */
0843 err_remove_link:
0844     /* remove the sym link */
0845     sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
0846 
0847 err_remove_main_attribs:
0848     edac_device_remove_main_sysfs_attributes(edac_dev);
0849 
0850 err_out:
0851     return err;
0852 }
0853 
0854 /*
0855  * edac_device_remove_sysfs() destructor
0856  *
0857  * given an edac_device struct, tear down the kobject resources
0858  */
0859 void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
0860 {
0861     edac_dbg(0, "\n");
0862 
0863     /* remove any main attributes for this device */
0864     edac_device_remove_main_sysfs_attributes(edac_dev);
0865 
0866     /* remove the device sym link */
0867     sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
0868 
0869     /* walk the instance/block kobject tree, deconstructing it */
0870     edac_device_delete_instances(edac_dev);
0871 }