1e27e3dacSDouglas Thompson /*
2fe5ff8b8SKay Sievers * file for managing the edac_device subsystem of devices for EDAC
3e27e3dacSDouglas Thompson *
4631dd1a8SJustin P. Mattock * (C) 2007 SoftwareBitMaker
51c3631ffSDouglas Thompson *
6e27e3dacSDouglas Thompson * This file may be distributed under the terms of the
7e27e3dacSDouglas Thompson * GNU General Public License.
8e27e3dacSDouglas Thompson *
9e27e3dacSDouglas Thompson * Written Doug Thompson <norsk5@xmission.com>
10e27e3dacSDouglas Thompson *
11e27e3dacSDouglas Thompson */
12e27e3dacSDouglas Thompson
13e27e3dacSDouglas Thompson #include <linux/ctype.h>
141c3631ffSDouglas Thompson #include <linux/module.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
1630e1f7a8SBorislav Petkov #include <linux/edac.h>
17e27e3dacSDouglas Thompson
186d8ef247SMauro Carvalho Chehab #include "edac_device.h"
19e27e3dacSDouglas Thompson #include "edac_module.h"
20e27e3dacSDouglas Thompson
21e27e3dacSDouglas Thompson #define EDAC_DEVICE_SYMLINK "device"
22e27e3dacSDouglas Thompson
23e27e3dacSDouglas Thompson #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
24e27e3dacSDouglas Thompson #define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
25e27e3dacSDouglas Thompson
26e27e3dacSDouglas Thompson
27e27e3dacSDouglas Thompson /*
28e27e3dacSDouglas Thompson * Set of edac_device_ctl_info attribute store/show functions
29e27e3dacSDouglas Thompson */
30e27e3dacSDouglas Thompson
31e27e3dacSDouglas Thompson /* 'log_ue' */
edac_device_ctl_log_ue_show(struct edac_device_ctl_info * ctl_info,char * data)32079708b9SDouglas Thompson static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
33079708b9SDouglas Thompson *ctl_info, char *data)
34e27e3dacSDouglas Thompson {
35e27e3dacSDouglas Thompson return sprintf(data, "%u\n", ctl_info->log_ue);
36e27e3dacSDouglas Thompson }
37e27e3dacSDouglas Thompson
edac_device_ctl_log_ue_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)38079708b9SDouglas Thompson static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
39079708b9SDouglas Thompson *ctl_info, const char *data,
40079708b9SDouglas Thompson size_t count)
41e27e3dacSDouglas Thompson {
42e27e3dacSDouglas Thompson /* if parameter is zero, turn off flag, if non-zero turn on flag */
43e27e3dacSDouglas Thompson ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
44e27e3dacSDouglas Thompson
45e27e3dacSDouglas Thompson return count;
46e27e3dacSDouglas Thompson }
47e27e3dacSDouglas Thompson
48e27e3dacSDouglas Thompson /* 'log_ce' */
edac_device_ctl_log_ce_show(struct edac_device_ctl_info * ctl_info,char * data)49079708b9SDouglas Thompson static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
50079708b9SDouglas Thompson *ctl_info, char *data)
51e27e3dacSDouglas Thompson {
52e27e3dacSDouglas Thompson return sprintf(data, "%u\n", ctl_info->log_ce);
53e27e3dacSDouglas Thompson }
54e27e3dacSDouglas Thompson
edac_device_ctl_log_ce_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)55079708b9SDouglas Thompson static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
56079708b9SDouglas Thompson *ctl_info, const char *data,
57079708b9SDouglas Thompson size_t count)
58e27e3dacSDouglas Thompson {
59e27e3dacSDouglas Thompson /* if parameter is zero, turn off flag, if non-zero turn on flag */
60e27e3dacSDouglas Thompson ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
61e27e3dacSDouglas Thompson
62e27e3dacSDouglas Thompson return count;
63e27e3dacSDouglas Thompson }
64e27e3dacSDouglas Thompson
65e27e3dacSDouglas Thompson /* 'panic_on_ue' */
edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info * ctl_info,char * data)66079708b9SDouglas Thompson static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
67079708b9SDouglas Thompson *ctl_info, char *data)
68e27e3dacSDouglas Thompson {
69e27e3dacSDouglas Thompson return sprintf(data, "%u\n", ctl_info->panic_on_ue);
70e27e3dacSDouglas Thompson }
71e27e3dacSDouglas Thompson
edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)72079708b9SDouglas Thompson static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
73079708b9SDouglas Thompson *ctl_info, const char *data,
74079708b9SDouglas Thompson size_t count)
75e27e3dacSDouglas Thompson {
76e27e3dacSDouglas Thompson /* if parameter is zero, turn off flag, if non-zero turn on flag */
77e27e3dacSDouglas Thompson ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
78e27e3dacSDouglas Thompson
79e27e3dacSDouglas Thompson return count;
80e27e3dacSDouglas Thompson }
81e27e3dacSDouglas Thompson
82e27e3dacSDouglas Thompson /* 'poll_msec' show and store functions*/
edac_device_ctl_poll_msec_show(struct edac_device_ctl_info * ctl_info,char * data)83079708b9SDouglas Thompson static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
84079708b9SDouglas Thompson *ctl_info, char *data)
85e27e3dacSDouglas Thompson {
86e27e3dacSDouglas Thompson return sprintf(data, "%u\n", ctl_info->poll_msec);
87e27e3dacSDouglas Thompson }
88e27e3dacSDouglas Thompson
edac_device_ctl_poll_msec_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)89079708b9SDouglas Thompson static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
90079708b9SDouglas Thompson *ctl_info, const char *data,
91079708b9SDouglas Thompson size_t count)
92e27e3dacSDouglas Thompson {
93e27e3dacSDouglas Thompson unsigned long value;
94e27e3dacSDouglas Thompson
95e27e3dacSDouglas Thompson /* get the value and enforce that it is non-zero, must be at least
96e27e3dacSDouglas Thompson * one millisecond for the delay period, between scans
97e27e3dacSDouglas Thompson * Then cancel last outstanding delay for the work request
98e27e3dacSDouglas Thompson * and set a new one.
99e27e3dacSDouglas Thompson */
100e27e3dacSDouglas Thompson value = simple_strtoul(data, NULL, 0);
101e27e3dacSDouglas Thompson edac_device_reset_delay_period(ctl_info, value);
102e27e3dacSDouglas Thompson
103e27e3dacSDouglas Thompson return count;
104e27e3dacSDouglas Thompson }
105e27e3dacSDouglas Thompson
106e27e3dacSDouglas Thompson /* edac_device_ctl_info specific attribute structure */
107e27e3dacSDouglas Thompson struct ctl_info_attribute {
108e27e3dacSDouglas Thompson struct attribute attr;
109e27e3dacSDouglas Thompson ssize_t(*show) (struct edac_device_ctl_info *, char *);
110e27e3dacSDouglas Thompson ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
111e27e3dacSDouglas Thompson };
112e27e3dacSDouglas Thompson
113e27e3dacSDouglas Thompson #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
114e27e3dacSDouglas Thompson #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
115e27e3dacSDouglas Thompson
116e27e3dacSDouglas Thompson /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
edac_dev_ctl_info_show(struct kobject * kobj,struct attribute * attr,char * buffer)117e27e3dacSDouglas Thompson static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
118079708b9SDouglas Thompson struct attribute *attr, char *buffer)
119e27e3dacSDouglas Thompson {
120e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
121e27e3dacSDouglas Thompson struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
122e27e3dacSDouglas Thompson
123e27e3dacSDouglas Thompson if (ctl_info_attr->show)
124e27e3dacSDouglas Thompson return ctl_info_attr->show(edac_dev, buffer);
125e27e3dacSDouglas Thompson return -EIO;
126e27e3dacSDouglas Thompson }
127e27e3dacSDouglas Thompson
128e27e3dacSDouglas Thompson /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
edac_dev_ctl_info_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t count)129e27e3dacSDouglas Thompson static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
130e27e3dacSDouglas Thompson struct attribute *attr,
131e27e3dacSDouglas Thompson const char *buffer, size_t count)
132e27e3dacSDouglas Thompson {
133e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
134e27e3dacSDouglas Thompson struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
135e27e3dacSDouglas Thompson
136e27e3dacSDouglas Thompson if (ctl_info_attr->store)
137e27e3dacSDouglas Thompson return ctl_info_attr->store(edac_dev, buffer, count);
138e27e3dacSDouglas Thompson return -EIO;
139e27e3dacSDouglas Thompson }
140e27e3dacSDouglas Thompson
141e27e3dacSDouglas Thompson /* edac_dev file operations for an 'ctl_info' */
14252cf25d0SEmese Revfy static const struct sysfs_ops device_ctl_info_ops = {
143e27e3dacSDouglas Thompson .show = edac_dev_ctl_info_show,
144e27e3dacSDouglas Thompson .store = edac_dev_ctl_info_store
145e27e3dacSDouglas Thompson };
146e27e3dacSDouglas Thompson
147e27e3dacSDouglas Thompson #define CTL_INFO_ATTR(_name,_mode,_show,_store) \
148e27e3dacSDouglas Thompson static struct ctl_info_attribute attr_ctl_info_##_name = { \
149e27e3dacSDouglas Thompson .attr = {.name = __stringify(_name), .mode = _mode }, \
150e27e3dacSDouglas Thompson .show = _show, \
151e27e3dacSDouglas Thompson .store = _store, \
152e27e3dacSDouglas Thompson };
153e27e3dacSDouglas Thompson
154e27e3dacSDouglas Thompson /* Declare the various ctl_info attributes here and their respective ops */
155e27e3dacSDouglas Thompson CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
156079708b9SDouglas Thompson edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
157e27e3dacSDouglas Thompson CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
158079708b9SDouglas Thompson edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
159e27e3dacSDouglas Thompson CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
160e27e3dacSDouglas Thompson edac_device_ctl_panic_on_ue_show,
161e27e3dacSDouglas Thompson edac_device_ctl_panic_on_ue_store);
162e27e3dacSDouglas Thompson CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
163079708b9SDouglas Thompson edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
164e27e3dacSDouglas Thompson
165e27e3dacSDouglas Thompson /* Base Attributes of the EDAC_DEVICE ECC object */
166625c6b55SGreg Kroah-Hartman static struct attribute *device_ctrl_attrs[] = {
16711413893SGreg Kroah-Hartman &attr_ctl_info_panic_on_ue.attr,
16811413893SGreg Kroah-Hartman &attr_ctl_info_log_ue.attr,
16911413893SGreg Kroah-Hartman &attr_ctl_info_log_ce.attr,
17011413893SGreg Kroah-Hartman &attr_ctl_info_poll_msec.attr,
171e27e3dacSDouglas Thompson NULL,
172e27e3dacSDouglas Thompson };
173625c6b55SGreg Kroah-Hartman ATTRIBUTE_GROUPS(device_ctrl);
174e27e3dacSDouglas Thompson
1751c3631ffSDouglas Thompson /*
1761c3631ffSDouglas Thompson * edac_device_ctrl_master_release
1771c3631ffSDouglas Thompson *
1781c3631ffSDouglas Thompson * called when the reference count for the 'main' kobj
1791c3631ffSDouglas Thompson * for a edac_device control struct reaches zero
1801c3631ffSDouglas Thompson *
1811c3631ffSDouglas Thompson * Reference count model:
1821c3631ffSDouglas Thompson * One 'main' kobject for each control structure allocated.
1831c3631ffSDouglas Thompson * That main kobj is initially set to one AND
1841c3631ffSDouglas Thompson * the reference count for the EDAC 'core' module is
1851c3631ffSDouglas Thompson * bumped by one, thus added 'keep in memory' dependency.
1861c3631ffSDouglas Thompson *
1871c3631ffSDouglas Thompson * Each new internal kobj (in instances and blocks) then
1881c3631ffSDouglas Thompson * bumps the 'main' kobject.
1891c3631ffSDouglas Thompson *
1901c3631ffSDouglas Thompson * When they are released their release functions decrement
1911c3631ffSDouglas Thompson * the 'main' kobj.
1921c3631ffSDouglas Thompson *
1931c3631ffSDouglas Thompson * When the main kobj reaches zero (0) then THIS function
1941c3631ffSDouglas Thompson * is called which then decrements the EDAC 'core' module.
1951c3631ffSDouglas Thompson * When the module reference count reaches zero then the
1961c3631ffSDouglas Thompson * module no longer has dependency on keeping the release
1971c3631ffSDouglas Thompson * function code in memory and module can be unloaded.
1981c3631ffSDouglas Thompson *
1991c3631ffSDouglas Thompson * This will support several control objects as well, each
2001c3631ffSDouglas Thompson * with its own 'main' kobj.
2011c3631ffSDouglas Thompson */
edac_device_ctrl_master_release(struct kobject * kobj)202e27e3dacSDouglas Thompson static void edac_device_ctrl_master_release(struct kobject *kobj)
203e27e3dacSDouglas Thompson {
2041c3631ffSDouglas Thompson struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
205e27e3dacSDouglas Thompson
206956b9ba1SJoe Perches edac_dbg(4, "control index=%d\n", edac_dev->dev_idx);
207e27e3dacSDouglas Thompson
2081c3631ffSDouglas Thompson /* decrement the EDAC CORE module ref count */
2091c3631ffSDouglas Thompson module_put(edac_dev->owner);
2101c3631ffSDouglas Thompson
2119fb9ce39SBorislav Petkov __edac_device_free_ctl_info(edac_dev);
212e27e3dacSDouglas Thompson }
213e27e3dacSDouglas Thompson
2141c3631ffSDouglas Thompson /* ktype for the main (master) kobject */
215e27e3dacSDouglas Thompson static struct kobj_type ktype_device_ctrl = {
216e27e3dacSDouglas Thompson .release = edac_device_ctrl_master_release,
217e27e3dacSDouglas Thompson .sysfs_ops = &device_ctl_info_ops,
218625c6b55SGreg Kroah-Hartman .default_groups = device_ctrl_groups,
219e27e3dacSDouglas Thompson };
220e27e3dacSDouglas Thompson
221e27e3dacSDouglas Thompson /*
2221c3631ffSDouglas Thompson * edac_device_register_sysfs_main_kobj
223e27e3dacSDouglas Thompson *
224e27e3dacSDouglas Thompson * perform the high level setup for the new edac_device instance
225e27e3dacSDouglas Thompson *
226e27e3dacSDouglas Thompson * Return: 0 SUCCESS
227e27e3dacSDouglas Thompson * !0 FAILURE
228e27e3dacSDouglas Thompson */
edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info * edac_dev)2291c3631ffSDouglas Thompson int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
230e27e3dacSDouglas Thompson {
231cb4a0becSGreg Kroah-Hartman struct device *dev_root;
232f36be9ceSGreg Kroah-Hartman const struct bus_type *edac_subsys;
233cb4a0becSGreg Kroah-Hartman int err = -ENODEV;
234e27e3dacSDouglas Thompson
235956b9ba1SJoe Perches edac_dbg(1, "\n");
236e27e3dacSDouglas Thompson
237e27e3dacSDouglas Thompson /* get the /sys/devices/system/edac reference */
238fe5ff8b8SKay Sievers edac_subsys = edac_get_sysfs_subsys();
239e27e3dacSDouglas Thompson
240fe5ff8b8SKay Sievers /* Point to the 'edac_subsys' this instance 'reports' to */
241fe5ff8b8SKay Sievers edac_dev->edac_subsys = edac_subsys;
242e27e3dacSDouglas Thompson
243e27e3dacSDouglas Thompson /* Init the devices's kobject */
244e27e3dacSDouglas Thompson memset(&edac_dev->kobj, 0, sizeof(struct kobject));
2451c3631ffSDouglas Thompson
2461c3631ffSDouglas Thompson /* Record which module 'owns' this control structure
2471c3631ffSDouglas Thompson * and bump the ref count of the module
2481c3631ffSDouglas Thompson */
2491c3631ffSDouglas Thompson edac_dev->owner = THIS_MODULE;
2501c3631ffSDouglas Thompson
251cb4a0becSGreg Kroah-Hartman if (!try_module_get(edac_dev->owner))
252733476cfSBorislav Petkov goto err_out;
2531c3631ffSDouglas Thompson
2541c3631ffSDouglas Thompson /* register */
255cb4a0becSGreg Kroah-Hartman dev_root = bus_get_dev_root(edac_subsys);
256cb4a0becSGreg Kroah-Hartman if (dev_root) {
257b2ed215aSGreg Kroah-Hartman err = kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl,
258cb4a0becSGreg Kroah-Hartman &dev_root->kobj, "%s", edac_dev->name);
259cb4a0becSGreg Kroah-Hartman put_device(dev_root);
260cb4a0becSGreg Kroah-Hartman }
261e27e3dacSDouglas Thompson if (err) {
262956b9ba1SJoe Perches edac_dbg(1, "Failed to register '.../edac/%s'\n",
263dd23cd6eSMauro Carvalho Chehab edac_dev->name);
2641c3631ffSDouglas Thompson goto err_kobj_reg;
265e27e3dacSDouglas Thompson }
266b2ed215aSGreg Kroah-Hartman kobject_uevent(&edac_dev->kobj, KOBJ_ADD);
267e27e3dacSDouglas Thompson
2681c3631ffSDouglas Thompson /* At this point, to 'free' the control struct,
2691c3631ffSDouglas Thompson * edac_device_unregister_sysfs_main_kobj() must be used
2701c3631ffSDouglas Thompson */
2711c3631ffSDouglas Thompson
272956b9ba1SJoe Perches edac_dbg(4, "Registered '.../edac/%s' kobject\n", edac_dev->name);
273e27e3dacSDouglas Thompson
274e27e3dacSDouglas Thompson return 0;
2751c3631ffSDouglas Thompson
2761c3631ffSDouglas Thompson /* Error exit stack */
2771c3631ffSDouglas Thompson err_kobj_reg:
27817ed808aSQiushi Wu kobject_put(&edac_dev->kobj);
2791c3631ffSDouglas Thompson module_put(edac_dev->owner);
2801c3631ffSDouglas Thompson
2811c3631ffSDouglas Thompson err_out:
2821c3631ffSDouglas Thompson return err;
283e27e3dacSDouglas Thompson }
284e27e3dacSDouglas Thompson
285e27e3dacSDouglas Thompson /*
2861c3631ffSDouglas Thompson * edac_device_unregister_sysfs_main_kobj:
287e27e3dacSDouglas Thompson * the '..../edac/<name>' kobject
288e27e3dacSDouglas Thompson */
edac_device_unregister_sysfs_main_kobj(struct edac_device_ctl_info * dev)28930e1f7a8SBorislav Petkov void edac_device_unregister_sysfs_main_kobj(struct edac_device_ctl_info *dev)
290e27e3dacSDouglas Thompson {
291956b9ba1SJoe Perches edac_dbg(0, "\n");
292956b9ba1SJoe Perches edac_dbg(4, "name of kobject is: %s\n", kobject_name(&dev->kobj));
293e27e3dacSDouglas Thompson
294e27e3dacSDouglas Thompson /*
295e27e3dacSDouglas Thompson * Unregister the edac device's kobject and
2961c3631ffSDouglas Thompson * allow for reference count to reach 0 at which point
2971c3631ffSDouglas Thompson * the callback will be called to:
2981c3631ffSDouglas Thompson * a) module_put() this module
2991c3631ffSDouglas Thompson * b) 'kfree' the memory
300e27e3dacSDouglas Thompson */
30130e1f7a8SBorislav Petkov kobject_put(&dev->kobj);
302e27e3dacSDouglas Thompson }
303e27e3dacSDouglas Thompson
3041c3631ffSDouglas Thompson /* edac_dev -> instance information */
305e27e3dacSDouglas Thompson
306e27e3dacSDouglas Thompson /*
307e27e3dacSDouglas Thompson * Set of low-level instance attribute show functions
308e27e3dacSDouglas Thompson */
instance_ue_count_show(struct edac_device_instance * instance,char * data)309079708b9SDouglas Thompson static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
310079708b9SDouglas Thompson char *data)
311e27e3dacSDouglas Thompson {
312e27e3dacSDouglas Thompson return sprintf(data, "%u\n", instance->counters.ue_count);
313e27e3dacSDouglas Thompson }
314e27e3dacSDouglas Thompson
instance_ce_count_show(struct edac_device_instance * instance,char * data)315079708b9SDouglas Thompson static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
316079708b9SDouglas Thompson char *data)
317e27e3dacSDouglas Thompson {
318e27e3dacSDouglas Thompson return sprintf(data, "%u\n", instance->counters.ce_count);
319e27e3dacSDouglas Thompson }
320e27e3dacSDouglas Thompson
321e27e3dacSDouglas Thompson #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
322e27e3dacSDouglas Thompson #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
323e27e3dacSDouglas Thompson
324e27e3dacSDouglas Thompson /* DEVICE instance kobject release() function */
edac_device_ctrl_instance_release(struct kobject * kobj)325e27e3dacSDouglas Thompson static void edac_device_ctrl_instance_release(struct kobject *kobj)
326e27e3dacSDouglas Thompson {
327e27e3dacSDouglas Thompson struct edac_device_instance *instance;
328e27e3dacSDouglas Thompson
329956b9ba1SJoe Perches edac_dbg(1, "\n");
330e27e3dacSDouglas Thompson
3311c3631ffSDouglas Thompson /* map from this kobj to the main control struct
3321c3631ffSDouglas Thompson * and then dec the main kobj count
3331c3631ffSDouglas Thompson */
334e27e3dacSDouglas Thompson instance = to_instance(kobj);
3351c3631ffSDouglas Thompson kobject_put(&instance->ctl->kobj);
336e27e3dacSDouglas Thompson }
337e27e3dacSDouglas Thompson
338e27e3dacSDouglas Thompson /* instance specific attribute structure */
339e27e3dacSDouglas Thompson struct instance_attribute {
340e27e3dacSDouglas Thompson struct attribute attr;
341e27e3dacSDouglas Thompson ssize_t(*show) (struct edac_device_instance *, char *);
342e27e3dacSDouglas Thompson ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
343e27e3dacSDouglas Thompson };
344e27e3dacSDouglas Thompson
345e27e3dacSDouglas Thompson /* Function to 'show' fields from the edac_dev 'instance' structure */
edac_dev_instance_show(struct kobject * kobj,struct attribute * attr,char * buffer)346e27e3dacSDouglas Thompson static ssize_t edac_dev_instance_show(struct kobject *kobj,
347079708b9SDouglas Thompson struct attribute *attr, char *buffer)
348e27e3dacSDouglas Thompson {
349e27e3dacSDouglas Thompson struct edac_device_instance *instance = to_instance(kobj);
350e27e3dacSDouglas Thompson struct instance_attribute *instance_attr = to_instance_attr(attr);
351e27e3dacSDouglas Thompson
352e27e3dacSDouglas Thompson if (instance_attr->show)
353e27e3dacSDouglas Thompson return instance_attr->show(instance, buffer);
354e27e3dacSDouglas Thompson return -EIO;
355e27e3dacSDouglas Thompson }
356e27e3dacSDouglas Thompson
357e27e3dacSDouglas Thompson /* Function to 'store' fields into the edac_dev 'instance' structure */
edac_dev_instance_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t count)358e27e3dacSDouglas Thompson static ssize_t edac_dev_instance_store(struct kobject *kobj,
359e27e3dacSDouglas Thompson struct attribute *attr,
360e27e3dacSDouglas Thompson const char *buffer, size_t count)
361e27e3dacSDouglas Thompson {
362e27e3dacSDouglas Thompson struct edac_device_instance *instance = to_instance(kobj);
363e27e3dacSDouglas Thompson struct instance_attribute *instance_attr = to_instance_attr(attr);
364e27e3dacSDouglas Thompson
365e27e3dacSDouglas Thompson if (instance_attr->store)
366e27e3dacSDouglas Thompson return instance_attr->store(instance, buffer, count);
367e27e3dacSDouglas Thompson return -EIO;
368e27e3dacSDouglas Thompson }
369e27e3dacSDouglas Thompson
370e27e3dacSDouglas Thompson /* edac_dev file operations for an 'instance' */
37152cf25d0SEmese Revfy static const struct sysfs_ops device_instance_ops = {
372e27e3dacSDouglas Thompson .show = edac_dev_instance_show,
373e27e3dacSDouglas Thompson .store = edac_dev_instance_store
374e27e3dacSDouglas Thompson };
375e27e3dacSDouglas Thompson
376e27e3dacSDouglas Thompson #define INSTANCE_ATTR(_name,_mode,_show,_store) \
377e27e3dacSDouglas Thompson static struct instance_attribute attr_instance_##_name = { \
378e27e3dacSDouglas Thompson .attr = {.name = __stringify(_name), .mode = _mode }, \
379e27e3dacSDouglas Thompson .show = _show, \
380e27e3dacSDouglas Thompson .store = _store, \
381e27e3dacSDouglas Thompson };
382e27e3dacSDouglas Thompson
383e27e3dacSDouglas Thompson /*
384e27e3dacSDouglas Thompson * Define attributes visible for the edac_device instance object
385e27e3dacSDouglas Thompson * Each contains a pointer to a show and an optional set
386e27e3dacSDouglas Thompson * function pointer that does the low level output/input
387e27e3dacSDouglas Thompson */
388e27e3dacSDouglas Thompson INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
389e27e3dacSDouglas Thompson INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
390e27e3dacSDouglas Thompson
391e27e3dacSDouglas Thompson /* list of edac_dev 'instance' attributes */
392625c6b55SGreg Kroah-Hartman static struct attribute *device_instance_attrs[] = {
39311413893SGreg Kroah-Hartman &attr_instance_ce_count.attr,
39411413893SGreg Kroah-Hartman &attr_instance_ue_count.attr,
395e27e3dacSDouglas Thompson NULL,
396e27e3dacSDouglas Thompson };
397625c6b55SGreg Kroah-Hartman ATTRIBUTE_GROUPS(device_instance);
398e27e3dacSDouglas Thompson
399e27e3dacSDouglas Thompson /* The 'ktype' for each edac_dev 'instance' */
400e27e3dacSDouglas Thompson static struct kobj_type ktype_instance_ctrl = {
401e27e3dacSDouglas Thompson .release = edac_device_ctrl_instance_release,
402e27e3dacSDouglas Thompson .sysfs_ops = &device_instance_ops,
403625c6b55SGreg Kroah-Hartman .default_groups = device_instance_groups,
404e27e3dacSDouglas Thompson };
405e27e3dacSDouglas Thompson
4061c3631ffSDouglas Thompson /* edac_dev -> instance -> block information */
407e27e3dacSDouglas Thompson
408b2a4ac0cSDoug Thompson #define to_block(k) container_of(k, struct edac_device_block, kobj)
409b2a4ac0cSDoug Thompson #define to_block_attr(a) \
410b2a4ac0cSDoug Thompson container_of(a, struct edac_dev_sysfs_block_attribute, attr)
411b2a4ac0cSDoug Thompson
412e27e3dacSDouglas Thompson /*
413e27e3dacSDouglas Thompson * Set of low-level block attribute show functions
414e27e3dacSDouglas Thompson */
block_ue_count_show(struct kobject * kobj,struct attribute * attr,char * data)415b2a4ac0cSDoug Thompson static ssize_t block_ue_count_show(struct kobject *kobj,
416b2a4ac0cSDoug Thompson struct attribute *attr, char *data)
417e27e3dacSDouglas Thompson {
418b2a4ac0cSDoug Thompson struct edac_device_block *block = to_block(kobj);
419b2a4ac0cSDoug Thompson
420e27e3dacSDouglas Thompson return sprintf(data, "%u\n", block->counters.ue_count);
421e27e3dacSDouglas Thompson }
422e27e3dacSDouglas Thompson
block_ce_count_show(struct kobject * kobj,struct attribute * attr,char * data)423b2a4ac0cSDoug Thompson static ssize_t block_ce_count_show(struct kobject *kobj,
424b2a4ac0cSDoug Thompson struct attribute *attr, char *data)
425e27e3dacSDouglas Thompson {
426b2a4ac0cSDoug Thompson struct edac_device_block *block = to_block(kobj);
427b2a4ac0cSDoug Thompson
428e27e3dacSDouglas Thompson return sprintf(data, "%u\n", block->counters.ce_count);
429e27e3dacSDouglas Thompson }
430e27e3dacSDouglas Thompson
431e27e3dacSDouglas Thompson /* DEVICE block kobject release() function */
edac_device_ctrl_block_release(struct kobject * kobj)432e27e3dacSDouglas Thompson static void edac_device_ctrl_block_release(struct kobject *kobj)
433e27e3dacSDouglas Thompson {
434e27e3dacSDouglas Thompson struct edac_device_block *block;
435e27e3dacSDouglas Thompson
436956b9ba1SJoe Perches edac_dbg(1, "\n");
437e27e3dacSDouglas Thompson
4381c3631ffSDouglas Thompson /* get the container of the kobj */
439e27e3dacSDouglas Thompson block = to_block(kobj);
4401c3631ffSDouglas Thompson
4411c3631ffSDouglas Thompson /* map from 'block kobj' to 'block->instance->controller->main_kobj'
4421c3631ffSDouglas Thompson * now 'release' the block kobject
4431c3631ffSDouglas Thompson */
4441c3631ffSDouglas Thompson kobject_put(&block->instance->ctl->kobj);
445e27e3dacSDouglas Thompson }
446e27e3dacSDouglas Thompson
447e27e3dacSDouglas Thompson
448e27e3dacSDouglas Thompson /* Function to 'show' fields from the edac_dev 'block' structure */
edac_dev_block_show(struct kobject * kobj,struct attribute * attr,char * buffer)449e27e3dacSDouglas Thompson static ssize_t edac_dev_block_show(struct kobject *kobj,
450079708b9SDouglas Thompson struct attribute *attr, char *buffer)
451e27e3dacSDouglas Thompson {
452b2a4ac0cSDoug Thompson struct edac_dev_sysfs_block_attribute *block_attr =
453b2a4ac0cSDoug Thompson to_block_attr(attr);
454e27e3dacSDouglas Thompson
455e27e3dacSDouglas Thompson if (block_attr->show)
456b2a4ac0cSDoug Thompson return block_attr->show(kobj, attr, buffer);
457e27e3dacSDouglas Thompson return -EIO;
458e27e3dacSDouglas Thompson }
459e27e3dacSDouglas Thompson
460e27e3dacSDouglas Thompson /* edac_dev file operations for a 'block' */
46152cf25d0SEmese Revfy static const struct sysfs_ops device_block_ops = {
462e27e3dacSDouglas Thompson .show = edac_dev_block_show,
463e27e3dacSDouglas Thompson };
464e27e3dacSDouglas Thompson
465*9186695eSJiri Slaby (SUSE) #define BLOCK_ATTR(_name,_mode,_show) \
466b2a4ac0cSDoug Thompson static struct edac_dev_sysfs_block_attribute attr_block_##_name = { \
467e27e3dacSDouglas Thompson .attr = {.name = __stringify(_name), .mode = _mode }, \
468e27e3dacSDouglas Thompson .show = _show, \
469e27e3dacSDouglas Thompson };
470e27e3dacSDouglas Thompson
471*9186695eSJiri Slaby (SUSE) BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show);
472*9186695eSJiri Slaby (SUSE) BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show);
473e27e3dacSDouglas Thompson
474e27e3dacSDouglas Thompson /* list of edac_dev 'block' attributes */
475625c6b55SGreg Kroah-Hartman static struct attribute *device_block_attrs[] = {
47611413893SGreg Kroah-Hartman &attr_block_ce_count.attr,
47711413893SGreg Kroah-Hartman &attr_block_ue_count.attr,
478e27e3dacSDouglas Thompson NULL,
479e27e3dacSDouglas Thompson };
480625c6b55SGreg Kroah-Hartman ATTRIBUTE_GROUPS(device_block);
481e27e3dacSDouglas Thompson
482e27e3dacSDouglas Thompson /* The 'ktype' for each edac_dev 'block' */
483e27e3dacSDouglas Thompson static struct kobj_type ktype_block_ctrl = {
484e27e3dacSDouglas Thompson .release = edac_device_ctrl_block_release,
485e27e3dacSDouglas Thompson .sysfs_ops = &device_block_ops,
486625c6b55SGreg Kroah-Hartman .default_groups = device_block_groups,
487e27e3dacSDouglas Thompson };
488e27e3dacSDouglas Thompson
4891c3631ffSDouglas Thompson /* block ctor/dtor code */
490e27e3dacSDouglas Thompson
491e27e3dacSDouglas Thompson /*
492e27e3dacSDouglas Thompson * edac_device_create_block
493e27e3dacSDouglas Thompson */
edac_device_create_block(struct edac_device_ctl_info * edac_dev,struct edac_device_instance * instance,struct edac_device_block * block)494079708b9SDouglas Thompson static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
495e27e3dacSDouglas Thompson struct edac_device_instance *instance,
4961c3631ffSDouglas Thompson struct edac_device_block *block)
497e27e3dacSDouglas Thompson {
498fd309a9dSDouglas Thompson int i;
499e27e3dacSDouglas Thompson int err;
500fd309a9dSDouglas Thompson struct edac_dev_sysfs_block_attribute *sysfs_attrib;
5011c3631ffSDouglas Thompson struct kobject *main_kobj;
502e27e3dacSDouglas Thompson
503956b9ba1SJoe Perches edac_dbg(4, "Instance '%s' inst_p=%p block '%s' block_p=%p\n",
504dd23cd6eSMauro Carvalho Chehab instance->name, instance, block->name, block);
505956b9ba1SJoe Perches edac_dbg(4, "block kobj=%p block kobj->parent=%p\n",
506dd23cd6eSMauro Carvalho Chehab &block->kobj, &block->kobj.parent);
507e27e3dacSDouglas Thompson
508e27e3dacSDouglas Thompson /* init this block's kobject */
509e27e3dacSDouglas Thompson memset(&block->kobj, 0, sizeof(struct kobject));
510e27e3dacSDouglas Thompson
5111c3631ffSDouglas Thompson /* bump the main kobject's reference count for this controller
51225985edcSLucas De Marchi * and this instance is dependent on the main
5131c3631ffSDouglas Thompson */
5141c3631ffSDouglas Thompson main_kobj = kobject_get(&edac_dev->kobj);
5151c3631ffSDouglas Thompson if (!main_kobj) {
5161c3631ffSDouglas Thompson err = -ENODEV;
5171c3631ffSDouglas Thompson goto err_out;
5181c3631ffSDouglas Thompson }
5191c3631ffSDouglas Thompson
5201c3631ffSDouglas Thompson /* Add this block's kobject */
521b2ed215aSGreg Kroah-Hartman err = kobject_init_and_add(&block->kobj, &ktype_block_ctrl,
522b2ed215aSGreg Kroah-Hartman &instance->kobj,
523b2ed215aSGreg Kroah-Hartman "%s", block->name);
524e27e3dacSDouglas Thompson if (err) {
525956b9ba1SJoe Perches edac_dbg(1, "Failed to register instance '%s'\n", block->name);
5261c3631ffSDouglas Thompson kobject_put(main_kobj);
5271c3631ffSDouglas Thompson err = -ENODEV;
5281c3631ffSDouglas Thompson goto err_out;
529e27e3dacSDouglas Thompson }
530e27e3dacSDouglas Thompson
531fd309a9dSDouglas Thompson /* If there are driver level block attributes, then added them
532fd309a9dSDouglas Thompson * to the block kobject
533fd309a9dSDouglas Thompson */
534fd309a9dSDouglas Thompson sysfs_attrib = block->block_attributes;
535b2a4ac0cSDoug Thompson if (sysfs_attrib && block->nr_attribs) {
536b2a4ac0cSDoug Thompson for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
537b2a4ac0cSDoug Thompson
538956b9ba1SJoe Perches edac_dbg(4, "creating block attrib='%s' attrib->%p to kobj=%p\n",
539b2a4ac0cSDoug Thompson sysfs_attrib->attr.name,
540b2a4ac0cSDoug Thompson sysfs_attrib, &block->kobj);
541b2a4ac0cSDoug Thompson
542b2a4ac0cSDoug Thompson /* Create each block_attribute file */
543fd309a9dSDouglas Thompson err = sysfs_create_file(&block->kobj,
544b2a4ac0cSDoug Thompson &sysfs_attrib->attr);
545fd309a9dSDouglas Thompson if (err)
546fd309a9dSDouglas Thompson goto err_on_attrib;
547fd309a9dSDouglas Thompson }
548fd309a9dSDouglas Thompson }
549b2ed215aSGreg Kroah-Hartman kobject_uevent(&block->kobj, KOBJ_ADD);
550fd309a9dSDouglas Thompson
551e27e3dacSDouglas Thompson return 0;
552fd309a9dSDouglas Thompson
5531c3631ffSDouglas Thompson /* Error unwind stack */
554fd309a9dSDouglas Thompson err_on_attrib:
555c10997f6SGreg Kroah-Hartman kobject_put(&block->kobj);
556fd309a9dSDouglas Thompson
5571c3631ffSDouglas Thompson err_out:
558fd309a9dSDouglas Thompson return err;
559e27e3dacSDouglas Thompson }
560e27e3dacSDouglas Thompson
561e27e3dacSDouglas Thompson /*
5621c3631ffSDouglas Thompson * edac_device_delete_block(edac_dev,block);
563e27e3dacSDouglas Thompson */
edac_device_delete_block(struct edac_device_ctl_info * edac_dev,struct edac_device_block * block)564079708b9SDouglas Thompson static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
5651c3631ffSDouglas Thompson struct edac_device_block *block)
566e27e3dacSDouglas Thompson {
5671c3631ffSDouglas Thompson struct edac_dev_sysfs_block_attribute *sysfs_attrib;
5681c3631ffSDouglas Thompson int i;
569e27e3dacSDouglas Thompson
5701c3631ffSDouglas Thompson /* if this block has 'attributes' then we need to iterate over the list
5711c3631ffSDouglas Thompson * and 'remove' the attributes on this block
5721c3631ffSDouglas Thompson */
5731c3631ffSDouglas Thompson sysfs_attrib = block->block_attributes;
5741c3631ffSDouglas Thompson if (sysfs_attrib && block->nr_attribs) {
575b2a4ac0cSDoug Thompson for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
576b2a4ac0cSDoug Thompson
577b2a4ac0cSDoug Thompson /* remove each block_attrib file */
5781c3631ffSDouglas Thompson sysfs_remove_file(&block->kobj,
5791c3631ffSDouglas Thompson (struct attribute *) sysfs_attrib);
5801c3631ffSDouglas Thompson }
581e27e3dacSDouglas Thompson }
582e27e3dacSDouglas Thompson
5831c3631ffSDouglas Thompson /* unregister this block's kobject, SEE:
5841c3631ffSDouglas Thompson * edac_device_ctrl_block_release() callback operation
5851c3631ffSDouglas Thompson */
586c10997f6SGreg Kroah-Hartman kobject_put(&block->kobj);
5871c3631ffSDouglas Thompson }
5881c3631ffSDouglas Thompson
5891c3631ffSDouglas Thompson /* instance ctor/dtor code */
590e27e3dacSDouglas Thompson
591e27e3dacSDouglas Thompson /*
592e27e3dacSDouglas Thompson * edac_device_create_instance
593e27e3dacSDouglas Thompson * create just one instance of an edac_device 'instance'
594e27e3dacSDouglas Thompson */
edac_device_create_instance(struct edac_device_ctl_info * edac_dev,int idx)595079708b9SDouglas Thompson static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
596079708b9SDouglas Thompson int idx)
597e27e3dacSDouglas Thompson {
598e27e3dacSDouglas Thompson int i, j;
599e27e3dacSDouglas Thompson int err;
600e27e3dacSDouglas Thompson struct edac_device_instance *instance;
6011c3631ffSDouglas Thompson struct kobject *main_kobj;
602e27e3dacSDouglas Thompson
603e27e3dacSDouglas Thompson instance = &edac_dev->instances[idx];
604e27e3dacSDouglas Thompson
605e27e3dacSDouglas Thompson /* Init the instance's kobject */
606e27e3dacSDouglas Thompson memset(&instance->kobj, 0, sizeof(struct kobject));
607e27e3dacSDouglas Thompson
6081c3631ffSDouglas Thompson instance->ctl = edac_dev;
609e27e3dacSDouglas Thompson
6101c3631ffSDouglas Thompson /* bump the main kobject's reference count for this controller
61125985edcSLucas De Marchi * and this instance is dependent on the main
6121c3631ffSDouglas Thompson */
6131c3631ffSDouglas Thompson main_kobj = kobject_get(&edac_dev->kobj);
6141c3631ffSDouglas Thompson if (!main_kobj) {
6151c3631ffSDouglas Thompson err = -ENODEV;
6161c3631ffSDouglas Thompson goto err_out;
6171c3631ffSDouglas Thompson }
6181c3631ffSDouglas Thompson
619b2ed215aSGreg Kroah-Hartman /* Formally register this instance's kobject under the edac_device */
620b2ed215aSGreg Kroah-Hartman err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
621b2ed215aSGreg Kroah-Hartman &edac_dev->kobj, "%s", instance->name);
622e27e3dacSDouglas Thompson if (err != 0) {
623956b9ba1SJoe Perches edac_dbg(2, "Failed to register instance '%s'\n",
624dd23cd6eSMauro Carvalho Chehab instance->name);
6251c3631ffSDouglas Thompson kobject_put(main_kobj);
6261c3631ffSDouglas Thompson goto err_out;
627e27e3dacSDouglas Thompson }
628e27e3dacSDouglas Thompson
629956b9ba1SJoe Perches edac_dbg(4, "now register '%d' blocks for instance %d\n",
630dd23cd6eSMauro Carvalho Chehab instance->nr_blocks, idx);
631e27e3dacSDouglas Thompson
632e27e3dacSDouglas Thompson /* register all blocks of this instance */
633e27e3dacSDouglas Thompson for (i = 0; i < instance->nr_blocks; i++) {
6341c3631ffSDouglas Thompson err = edac_device_create_block(edac_dev, instance,
6351c3631ffSDouglas Thompson &instance->blocks[i]);
636e27e3dacSDouglas Thompson if (err) {
6371c3631ffSDouglas Thompson /* If any fail, remove all previous ones */
63852490c8dSDouglas Thompson for (j = 0; j < i; j++)
6391c3631ffSDouglas Thompson edac_device_delete_block(edac_dev,
6401c3631ffSDouglas Thompson &instance->blocks[j]);
6411c3631ffSDouglas Thompson goto err_release_instance_kobj;
642e27e3dacSDouglas Thompson }
643e27e3dacSDouglas Thompson }
644b2ed215aSGreg Kroah-Hartman kobject_uevent(&instance->kobj, KOBJ_ADD);
645e27e3dacSDouglas Thompson
646956b9ba1SJoe Perches edac_dbg(4, "Registered instance %d '%s' kobject\n",
647dd23cd6eSMauro Carvalho Chehab idx, instance->name);
648e27e3dacSDouglas Thompson
649e27e3dacSDouglas Thompson return 0;
6501c3631ffSDouglas Thompson
6511c3631ffSDouglas Thompson /* error unwind stack */
6521c3631ffSDouglas Thompson err_release_instance_kobj:
653c10997f6SGreg Kroah-Hartman kobject_put(&instance->kobj);
6541c3631ffSDouglas Thompson
6551c3631ffSDouglas Thompson err_out:
6561c3631ffSDouglas Thompson return err;
657e27e3dacSDouglas Thompson }
658e27e3dacSDouglas Thompson
659e27e3dacSDouglas Thompson /*
660e27e3dacSDouglas Thompson * edac_device_remove_instance
661e27e3dacSDouglas Thompson * remove an edac_device instance
662e27e3dacSDouglas Thompson */
edac_device_delete_instance(struct edac_device_ctl_info * edac_dev,int idx)663079708b9SDouglas Thompson static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
664079708b9SDouglas Thompson int idx)
665e27e3dacSDouglas Thompson {
666e27e3dacSDouglas Thompson struct edac_device_instance *instance;
6671c3631ffSDouglas Thompson int i;
668e27e3dacSDouglas Thompson
669e27e3dacSDouglas Thompson instance = &edac_dev->instances[idx];
670e27e3dacSDouglas Thompson
671e27e3dacSDouglas Thompson /* unregister all blocks in this instance */
67252490c8dSDouglas Thompson for (i = 0; i < instance->nr_blocks; i++)
6731c3631ffSDouglas Thompson edac_device_delete_block(edac_dev, &instance->blocks[i]);
674e27e3dacSDouglas Thompson
6751c3631ffSDouglas Thompson /* unregister this instance's kobject, SEE:
6761c3631ffSDouglas Thompson * edac_device_ctrl_instance_release() for callback operation
6771c3631ffSDouglas Thompson */
678c10997f6SGreg Kroah-Hartman kobject_put(&instance->kobj);
679e27e3dacSDouglas Thompson }
680e27e3dacSDouglas Thompson
681e27e3dacSDouglas Thompson /*
682e27e3dacSDouglas Thompson * edac_device_create_instances
683e27e3dacSDouglas Thompson * create the first level of 'instances' for this device
684e27e3dacSDouglas Thompson * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
685e27e3dacSDouglas Thompson */
edac_device_create_instances(struct edac_device_ctl_info * edac_dev)686e27e3dacSDouglas Thompson static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
687e27e3dacSDouglas Thompson {
688e27e3dacSDouglas Thompson int i, j;
689e27e3dacSDouglas Thompson int err;
690e27e3dacSDouglas Thompson
691956b9ba1SJoe Perches edac_dbg(0, "\n");
692e27e3dacSDouglas Thompson
693e27e3dacSDouglas Thompson /* iterate over creation of the instances */
694e27e3dacSDouglas Thompson for (i = 0; i < edac_dev->nr_instances; i++) {
695e27e3dacSDouglas Thompson err = edac_device_create_instance(edac_dev, i);
696e27e3dacSDouglas Thompson if (err) {
697e27e3dacSDouglas Thompson /* unwind previous instances on error */
69852490c8dSDouglas Thompson for (j = 0; j < i; j++)
699e27e3dacSDouglas Thompson edac_device_delete_instance(edac_dev, j);
700e27e3dacSDouglas Thompson return err;
701e27e3dacSDouglas Thompson }
702e27e3dacSDouglas Thompson }
703e27e3dacSDouglas Thompson
704e27e3dacSDouglas Thompson return 0;
705e27e3dacSDouglas Thompson }
706e27e3dacSDouglas Thompson
707e27e3dacSDouglas Thompson /*
708e27e3dacSDouglas Thompson * edac_device_delete_instances(edac_dev);
709e27e3dacSDouglas Thompson * unregister all the kobjects of the instances
710e27e3dacSDouglas Thompson */
edac_device_delete_instances(struct edac_device_ctl_info * edac_dev)711e27e3dacSDouglas Thompson static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
712e27e3dacSDouglas Thompson {
713e27e3dacSDouglas Thompson int i;
714e27e3dacSDouglas Thompson
715e27e3dacSDouglas Thompson /* iterate over creation of the instances */
71652490c8dSDouglas Thompson for (i = 0; i < edac_dev->nr_instances; i++)
717e27e3dacSDouglas Thompson edac_device_delete_instance(edac_dev, i);
718e27e3dacSDouglas Thompson }
719e27e3dacSDouglas Thompson
7201c3631ffSDouglas Thompson /* edac_dev sysfs ctor/dtor code */
721e27e3dacSDouglas Thompson
722e27e3dacSDouglas Thompson /*
7231c3631ffSDouglas Thompson * edac_device_add_main_sysfs_attributes
72442a8e397SDouglas Thompson * add some attributes to this instance's main kobject
72542a8e397SDouglas Thompson */
edac_device_add_main_sysfs_attributes(struct edac_device_ctl_info * edac_dev)7261c3631ffSDouglas Thompson static int edac_device_add_main_sysfs_attributes(
72742a8e397SDouglas Thompson struct edac_device_ctl_info *edac_dev)
72842a8e397SDouglas Thompson {
72942a8e397SDouglas Thompson struct edac_dev_sysfs_attribute *sysfs_attrib;
7301c3631ffSDouglas Thompson int err = 0;
73142a8e397SDouglas Thompson
73242a8e397SDouglas Thompson sysfs_attrib = edac_dev->sysfs_attributes;
7331c3631ffSDouglas Thompson if (sysfs_attrib) {
7341c3631ffSDouglas Thompson /* iterate over the array and create an attribute for each
7351c3631ffSDouglas Thompson * entry in the list
7361c3631ffSDouglas Thompson */
73742a8e397SDouglas Thompson while (sysfs_attrib->attr.name != NULL) {
73842a8e397SDouglas Thompson err = sysfs_create_file(&edac_dev->kobj,
73942a8e397SDouglas Thompson (struct attribute*) sysfs_attrib);
74052490c8dSDouglas Thompson if (err)
7411c3631ffSDouglas Thompson goto err_out;
74242a8e397SDouglas Thompson
74342a8e397SDouglas Thompson sysfs_attrib++;
74442a8e397SDouglas Thompson }
7451c3631ffSDouglas Thompson }
74642a8e397SDouglas Thompson
7471c3631ffSDouglas Thompson err_out:
7481c3631ffSDouglas Thompson return err;
7491c3631ffSDouglas Thompson }
7501c3631ffSDouglas Thompson
7511c3631ffSDouglas Thompson /*
7521c3631ffSDouglas Thompson * edac_device_remove_main_sysfs_attributes
7531c3631ffSDouglas Thompson * remove any attributes to this instance's main kobject
7541c3631ffSDouglas Thompson */
edac_device_remove_main_sysfs_attributes(struct edac_device_ctl_info * edac_dev)7551c3631ffSDouglas Thompson static void edac_device_remove_main_sysfs_attributes(
7561c3631ffSDouglas Thompson struct edac_device_ctl_info *edac_dev)
7571c3631ffSDouglas Thompson {
7581c3631ffSDouglas Thompson struct edac_dev_sysfs_attribute *sysfs_attrib;
7591c3631ffSDouglas Thompson
7601c3631ffSDouglas Thompson /* if there are main attributes, defined, remove them. First,
7611c3631ffSDouglas Thompson * point to the start of the array and iterate over it
7621c3631ffSDouglas Thompson * removing each attribute listed from this device's instance's kobject
7631c3631ffSDouglas Thompson */
7641c3631ffSDouglas Thompson sysfs_attrib = edac_dev->sysfs_attributes;
7651c3631ffSDouglas Thompson if (sysfs_attrib) {
7661c3631ffSDouglas Thompson while (sysfs_attrib->attr.name != NULL) {
7671c3631ffSDouglas Thompson sysfs_remove_file(&edac_dev->kobj,
7681c3631ffSDouglas Thompson (struct attribute *) sysfs_attrib);
7691c3631ffSDouglas Thompson sysfs_attrib++;
7701c3631ffSDouglas Thompson }
7711c3631ffSDouglas Thompson }
77242a8e397SDouglas Thompson }
77342a8e397SDouglas Thompson
77442a8e397SDouglas Thompson /*
775e27e3dacSDouglas Thompson * edac_device_create_sysfs() Constructor
776e27e3dacSDouglas Thompson *
7771c3631ffSDouglas Thompson * accept a created edac_device control structure
7781c3631ffSDouglas Thompson * and 'export' it to sysfs. The 'main' kobj should already have been
7791c3631ffSDouglas Thompson * created. 'instance' and 'block' kobjects should be registered
7801c3631ffSDouglas Thompson * along with any 'block' attributes from the low driver. In addition,
7811c3631ffSDouglas Thompson * the main attributes (if any) are connected to the main kobject of
7821c3631ffSDouglas Thompson * the control structure.
783e27e3dacSDouglas Thompson *
784e27e3dacSDouglas Thompson * Return:
785e27e3dacSDouglas Thompson * 0 Success
786e27e3dacSDouglas Thompson * !0 Failure
787e27e3dacSDouglas Thompson */
edac_device_create_sysfs(struct edac_device_ctl_info * edac_dev)788e27e3dacSDouglas Thompson int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
789e27e3dacSDouglas Thompson {
790e27e3dacSDouglas Thompson int err;
791e27e3dacSDouglas Thompson struct kobject *edac_kobj = &edac_dev->kobj;
792e27e3dacSDouglas Thompson
793956b9ba1SJoe Perches edac_dbg(0, "idx=%d\n", edac_dev->dev_idx);
794e27e3dacSDouglas Thompson
7951c3631ffSDouglas Thompson /* go create any main attributes callers wants */
7961c3631ffSDouglas Thompson err = edac_device_add_main_sysfs_attributes(edac_dev);
79742a8e397SDouglas Thompson if (err) {
798956b9ba1SJoe Perches edac_dbg(0, "failed to add sysfs attribs\n");
7991c3631ffSDouglas Thompson goto err_out;
80042a8e397SDouglas Thompson }
80142a8e397SDouglas Thompson
802e27e3dacSDouglas Thompson /* create a symlink from the edac device
803e27e3dacSDouglas Thompson * to the platform 'device' being used for this
804e27e3dacSDouglas Thompson */
805e27e3dacSDouglas Thompson err = sysfs_create_link(edac_kobj,
806079708b9SDouglas Thompson &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
807e27e3dacSDouglas Thompson if (err) {
808956b9ba1SJoe Perches edac_dbg(0, "sysfs_create_link() returned err= %d\n", err);
8091c3631ffSDouglas Thompson goto err_remove_main_attribs;
810e27e3dacSDouglas Thompson }
811e27e3dacSDouglas Thompson
8121c3631ffSDouglas Thompson /* Create the first level instance directories
8131c3631ffSDouglas Thompson * In turn, the nested blocks beneath the instances will
8141c3631ffSDouglas Thompson * be registered as well
8151c3631ffSDouglas Thompson */
8161c3631ffSDouglas Thompson err = edac_device_create_instances(edac_dev);
8171c3631ffSDouglas Thompson if (err) {
818956b9ba1SJoe Perches edac_dbg(0, "edac_device_create_instances() returned err= %d\n",
819956b9ba1SJoe Perches err);
8201c3631ffSDouglas Thompson goto err_remove_link;
8211c3631ffSDouglas Thompson }
8221c3631ffSDouglas Thompson
8231c3631ffSDouglas Thompson
824956b9ba1SJoe Perches edac_dbg(4, "create-instances done, idx=%d\n", edac_dev->dev_idx);
825e27e3dacSDouglas Thompson
826e27e3dacSDouglas Thompson return 0;
827e27e3dacSDouglas Thompson
828e27e3dacSDouglas Thompson /* Error unwind stack */
82942a8e397SDouglas Thompson err_remove_link:
83042a8e397SDouglas Thompson /* remove the sym link */
83142a8e397SDouglas Thompson sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
832e27e3dacSDouglas Thompson
8331c3631ffSDouglas Thompson err_remove_main_attribs:
8341c3631ffSDouglas Thompson edac_device_remove_main_sysfs_attributes(edac_dev);
835e27e3dacSDouglas Thompson
8361c3631ffSDouglas Thompson err_out:
837e27e3dacSDouglas Thompson return err;
838e27e3dacSDouglas Thompson }
839e27e3dacSDouglas Thompson
840e27e3dacSDouglas Thompson /*
841e27e3dacSDouglas Thompson * edac_device_remove_sysfs() destructor
842e27e3dacSDouglas Thompson *
8431c3631ffSDouglas Thompson * given an edac_device struct, tear down the kobject resources
844e27e3dacSDouglas Thompson */
edac_device_remove_sysfs(struct edac_device_ctl_info * edac_dev)845e27e3dacSDouglas Thompson void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
846e27e3dacSDouglas Thompson {
847956b9ba1SJoe Perches edac_dbg(0, "\n");
848e27e3dacSDouglas Thompson
8491c3631ffSDouglas Thompson /* remove any main attributes for this device */
8501c3631ffSDouglas Thompson edac_device_remove_main_sysfs_attributes(edac_dev);
851e27e3dacSDouglas Thompson
8521c3631ffSDouglas Thompson /* remove the device sym link */
853e27e3dacSDouglas Thompson sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
854e27e3dacSDouglas Thompson
8551c3631ffSDouglas Thompson /* walk the instance/block kobject tree, deconstructing it */
8561c3631ffSDouglas Thompson edac_device_delete_instances(edac_dev);
857e27e3dacSDouglas Thompson }
858