blob: b69eaf62e657458ff1dcc8fc6bf602ac433c702e [file] [log] [blame]
Douglas Thompsone27e3da2007-07-19 01:49:36 -07001/*
Kay Sieversfe5ff8b2011-12-14 15:21:07 -08002 * file for managing the edac_device subsystem of devices for EDAC
Douglas Thompsone27e3da2007-07-19 01:49:36 -07003 *
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02004 * (C) 2007 SoftwareBitMaker
Douglas Thompson1c3631f2007-07-19 01:50:29 -07005 *
Douglas Thompsone27e3da2007-07-19 01:49:36 -07006 * This file may be distributed under the terms of the
7 * GNU General Public License.
8 *
9 * Written Doug Thompson <norsk5@xmission.com>
10 *
11 */
12
Douglas Thompsone27e3da2007-07-19 01:49:36 -070013#include <linux/ctype.h>
Douglas Thompson1c3631f2007-07-19 01:50:29 -070014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Borislav Petkov30e1f7a2010-09-02 17:26:48 +020016#include <linux/edac.h>
Douglas Thompsone27e3da2007-07-19 01:49:36 -070017
18#include "edac_core.h"
19#include "edac_module.h"
20
Douglas Thompsone27e3da2007-07-19 01:49:36 -070021#define EDAC_DEVICE_SYMLINK "device"
22
23#define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
24#define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
25
Douglas Thompsone27e3da2007-07-19 01:49:36 -070026
27/*
28 * Set of edac_device_ctl_info attribute store/show functions
29 */
30
31/* 'log_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -070032static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -070033 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070034{
Douglas Thompson079708b2007-07-19 01:49:58 -070035 return sprintf(data, "%u\n", ctl_info->log_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070036}
37
Douglas Thompson079708b2007-07-19 01:49:58 -070038static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -070039 *ctl_info, const char *data,
40 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070041{
42 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070043 ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070044
Douglas Thompson079708b2007-07-19 01:49:58 -070045 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070046}
47
48/* 'log_ce' */
Douglas Thompson079708b2007-07-19 01:49:58 -070049static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -070050 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070051{
Douglas Thompson079708b2007-07-19 01:49:58 -070052 return sprintf(data, "%u\n", ctl_info->log_ce);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070053}
54
Douglas Thompson079708b2007-07-19 01:49:58 -070055static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -070056 *ctl_info, const char *data,
57 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070058{
59 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070060 ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070061
Douglas Thompson079708b2007-07-19 01:49:58 -070062 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070063}
64
Stepan Moskovchenkoa8349c12014-06-10 19:24:50 -070065/* 'panic_on_ce' */
66static ssize_t edac_device_ctl_panic_on_ce_show(struct edac_device_ctl_info
67 *ctl_info, char *data)
68{
69 return snprintf(data, PAGE_SIZE, "%u\n", ctl_info->panic_on_ce);
70}
71
Douglas Thompsone27e3da2007-07-19 01:49:36 -070072/* 'panic_on_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -070073static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
74 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070075{
Douglas Thompson079708b2007-07-19 01:49:58 -070076 return sprintf(data, "%u\n", ctl_info->panic_on_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070077}
78
Stepan Moskovchenkoa8349c12014-06-10 19:24:50 -070079static ssize_t edac_device_ctl_panic_on_ce_store(struct edac_device_ctl_info
80 *ctl_info, const char *data,
81 size_t count)
82{
83 unsigned long val;
84
85 /* if parameter is zero, turn off flag, if non-zero turn on flag */
86 if (kstrtoul(data, 0, &val) < 0)
87 return -EINVAL;
88
89 ctl_info->panic_on_ce = !!val;
90
91 return count;
92}
93
Douglas Thompson079708b2007-07-19 01:49:58 -070094static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
95 *ctl_info, const char *data,
96 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070097{
98 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070099 ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700100
101 return count;
102}
103
104/* 'poll_msec' show and store functions*/
Douglas Thompson079708b2007-07-19 01:49:58 -0700105static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700106 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700107{
Douglas Thompson079708b2007-07-19 01:49:58 -0700108 return sprintf(data, "%u\n", ctl_info->poll_msec);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700109}
110
Douglas Thompson079708b2007-07-19 01:49:58 -0700111static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700112 *ctl_info, const char *data,
113 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700114{
115 unsigned long value;
116
117 /* get the value and enforce that it is non-zero, must be at least
118 * one millisecond for the delay period, between scans
119 * Then cancel last outstanding delay for the work request
120 * and set a new one.
121 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700122 value = simple_strtoul(data, NULL, 0);
123 edac_device_reset_delay_period(ctl_info, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700124
Douglas Thompson079708b2007-07-19 01:49:58 -0700125 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700126}
127
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700128/* edac_device_ctl_info specific attribute structure */
129struct ctl_info_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700130 struct attribute attr;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700131 ssize_t(*show) (struct edac_device_ctl_info *, char *);
132 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700133};
134
135#define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
136#define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
137
138/* Function to 'show' fields from the edac_dev 'ctl_info' structure */
139static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700140 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700141{
Douglas Thompson079708b2007-07-19 01:49:58 -0700142 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
143 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700144
Douglas Thompson079708b2007-07-19 01:49:58 -0700145 if (ctl_info_attr->show)
146 return ctl_info_attr->show(edac_dev, buffer);
147 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700148}
149
150/* Function to 'store' fields into the edac_dev 'ctl_info' structure */
151static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700152 struct attribute *attr,
153 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700154{
Douglas Thompson079708b2007-07-19 01:49:58 -0700155 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
156 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700157
Douglas Thompson079708b2007-07-19 01:49:58 -0700158 if (ctl_info_attr->store)
159 return ctl_info_attr->store(edac_dev, buffer, count);
160 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700161}
162
163/* edac_dev file operations for an 'ctl_info' */
Emese Revfy52cf25d2010-01-19 02:58:23 +0100164static const struct sysfs_ops device_ctl_info_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700165 .show = edac_dev_ctl_info_show,
166 .store = edac_dev_ctl_info_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700167};
168
169#define CTL_INFO_ATTR(_name,_mode,_show,_store) \
170static struct ctl_info_attribute attr_ctl_info_##_name = { \
Douglas Thompson052dfb42007-07-19 01:50:13 -0700171 .attr = {.name = __stringify(_name), .mode = _mode }, \
172 .show = _show, \
173 .store = _store, \
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700174};
175
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700176/* Declare the various ctl_info attributes here and their respective ops */
Douglas Thompson079708b2007-07-19 01:49:58 -0700177CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700178 edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
Douglas Thompson079708b2007-07-19 01:49:58 -0700179CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700180 edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
Stepan Moskovchenkoa8349c12014-06-10 19:24:50 -0700181CTL_INFO_ATTR(panic_on_ce, S_IRUGO | S_IWUSR,
182 edac_device_ctl_panic_on_ce_show,
183 edac_device_ctl_panic_on_ce_store);
Douglas Thompson079708b2007-07-19 01:49:58 -0700184CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700185 edac_device_ctl_panic_on_ue_show,
186 edac_device_ctl_panic_on_ue_store);
Douglas Thompson079708b2007-07-19 01:49:58 -0700187CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700188 edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700189
190/* Base Attributes of the EDAC_DEVICE ECC object */
191static struct ctl_info_attribute *device_ctrl_attr[] = {
Stepan Moskovchenkoa8349c12014-06-10 19:24:50 -0700192 &attr_ctl_info_panic_on_ce,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700193 &attr_ctl_info_panic_on_ue,
194 &attr_ctl_info_log_ue,
195 &attr_ctl_info_log_ce,
196 &attr_ctl_info_poll_msec,
197 NULL,
198};
199
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700200/*
201 * edac_device_ctrl_master_release
202 *
203 * called when the reference count for the 'main' kobj
204 * for a edac_device control struct reaches zero
205 *
206 * Reference count model:
207 * One 'main' kobject for each control structure allocated.
208 * That main kobj is initially set to one AND
209 * the reference count for the EDAC 'core' module is
210 * bumped by one, thus added 'keep in memory' dependency.
211 *
212 * Each new internal kobj (in instances and blocks) then
213 * bumps the 'main' kobject.
214 *
215 * When they are released their release functions decrement
216 * the 'main' kobj.
217 *
218 * When the main kobj reaches zero (0) then THIS function
219 * is called which then decrements the EDAC 'core' module.
220 * When the module reference count reaches zero then the
221 * module no longer has dependency on keeping the release
222 * function code in memory and module can be unloaded.
223 *
224 * This will support several control objects as well, each
225 * with its own 'main' kobj.
226 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700227static void edac_device_ctrl_master_release(struct kobject *kobj)
228{
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700229 struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700230
Joe Perches956b9ba2012-04-29 17:08:39 -0300231 edac_dbg(4, "control index=%d\n", edac_dev->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700232
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700233 /* decrement the EDAC CORE module ref count */
234 module_put(edac_dev->owner);
235
236 /* free the control struct containing the 'main' kobj
237 * passed in to this routine
238 */
239 kfree(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700240}
241
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700242/* ktype for the main (master) kobject */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700243static struct kobj_type ktype_device_ctrl = {
244 .release = edac_device_ctrl_master_release,
245 .sysfs_ops = &device_ctl_info_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700246 .default_attrs = (struct attribute **)device_ctrl_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700247};
248
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700249/*
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700250 * edac_device_register_sysfs_main_kobj
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700251 *
252 * perform the high level setup for the new edac_device instance
253 *
254 * Return: 0 SUCCESS
255 * !0 FAILURE
256 */
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700257int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700258{
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800259 struct bus_type *edac_subsys;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700260 int err;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700261
Joe Perches956b9ba2012-04-29 17:08:39 -0300262 edac_dbg(1, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700263
Douglas Thompson079708b2007-07-19 01:49:58 -0700264 /* get the /sys/devices/system/edac reference */
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800265 edac_subsys = edac_get_sysfs_subsys();
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700266
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800267 /* Point to the 'edac_subsys' this instance 'reports' to */
268 edac_dev->edac_subsys = edac_subsys;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700269
270 /* Init the devices's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700271 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700272
273 /* Record which module 'owns' this control structure
274 * and bump the ref count of the module
275 */
276 edac_dev->owner = THIS_MODULE;
277
278 if (!try_module_get(edac_dev->owner)) {
279 err = -ENODEV;
Borislav Petkov733476c2015-11-27 11:40:43 +0100280 goto err_out;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700281 }
282
283 /* register */
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400284 err = kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl,
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800285 &edac_subsys->dev_root->kobj,
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400286 "%s", edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700287 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300288 edac_dbg(1, "Failed to register '.../edac/%s'\n",
289 edac_dev->name);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700290 goto err_kobj_reg;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700291 }
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400292 kobject_uevent(&edac_dev->kobj, KOBJ_ADD);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700293
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700294 /* At this point, to 'free' the control struct,
295 * edac_device_unregister_sysfs_main_kobj() must be used
296 */
297
Joe Perches956b9ba2012-04-29 17:08:39 -0300298 edac_dbg(4, "Registered '.../edac/%s' kobject\n", edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700299
300 return 0;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700301
302 /* Error exit stack */
303err_kobj_reg:
304 module_put(edac_dev->owner);
305
306err_out:
307 return err;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700308}
309
310/*
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700311 * edac_device_unregister_sysfs_main_kobj:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700312 * the '..../edac/<name>' kobject
313 */
Borislav Petkov30e1f7a2010-09-02 17:26:48 +0200314void edac_device_unregister_sysfs_main_kobj(struct edac_device_ctl_info *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700315{
Joe Perches956b9ba2012-04-29 17:08:39 -0300316 edac_dbg(0, "\n");
317 edac_dbg(4, "name of kobject is: %s\n", kobject_name(&dev->kobj));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700318
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700319 /*
320 * Unregister the edac device's kobject and
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700321 * allow for reference count to reach 0 at which point
322 * the callback will be called to:
323 * a) module_put() this module
324 * b) 'kfree' the memory
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700325 */
Borislav Petkov30e1f7a2010-09-02 17:26:48 +0200326 kobject_put(&dev->kobj);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700327}
328
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700329/* edac_dev -> instance information */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700330
331/*
332 * Set of low-level instance attribute show functions
333 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700334static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700335 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700336{
Douglas Thompson079708b2007-07-19 01:49:58 -0700337 return sprintf(data, "%u\n", instance->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700338}
339
Douglas Thompson079708b2007-07-19 01:49:58 -0700340static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700341 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700342{
Douglas Thompson079708b2007-07-19 01:49:58 -0700343 return sprintf(data, "%u\n", instance->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700344}
345
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700346#define to_instance(k) container_of(k, struct edac_device_instance, kobj)
347#define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
348
349/* DEVICE instance kobject release() function */
350static void edac_device_ctrl_instance_release(struct kobject *kobj)
351{
352 struct edac_device_instance *instance;
353
Joe Perches956b9ba2012-04-29 17:08:39 -0300354 edac_dbg(1, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700355
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700356 /* map from this kobj to the main control struct
357 * and then dec the main kobj count
358 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700359 instance = to_instance(kobj);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700360 kobject_put(&instance->ctl->kobj);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700361}
362
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700363/* instance specific attribute structure */
364struct instance_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700365 struct attribute attr;
Douglas Thompson52490c82007-07-19 01:50:20 -0700366 ssize_t(*show) (struct edac_device_instance *, char *);
367 ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700368};
369
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700370/* Function to 'show' fields from the edac_dev 'instance' structure */
371static ssize_t edac_dev_instance_show(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700372 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700373{
Douglas Thompson079708b2007-07-19 01:49:58 -0700374 struct edac_device_instance *instance = to_instance(kobj);
375 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700376
Douglas Thompson079708b2007-07-19 01:49:58 -0700377 if (instance_attr->show)
378 return instance_attr->show(instance, buffer);
379 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700380}
381
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700382/* Function to 'store' fields into the edac_dev 'instance' structure */
383static ssize_t edac_dev_instance_store(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700384 struct attribute *attr,
385 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700386{
Douglas Thompson079708b2007-07-19 01:49:58 -0700387 struct edac_device_instance *instance = to_instance(kobj);
388 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700389
Douglas Thompson079708b2007-07-19 01:49:58 -0700390 if (instance_attr->store)
391 return instance_attr->store(instance, buffer, count);
392 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700393}
394
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700395/* edac_dev file operations for an 'instance' */
Emese Revfy52cf25d2010-01-19 02:58:23 +0100396static const struct sysfs_ops device_instance_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700397 .show = edac_dev_instance_show,
398 .store = edac_dev_instance_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700399};
400
401#define INSTANCE_ATTR(_name,_mode,_show,_store) \
402static struct instance_attribute attr_instance_##_name = { \
Douglas Thompson052dfb42007-07-19 01:50:13 -0700403 .attr = {.name = __stringify(_name), .mode = _mode }, \
404 .show = _show, \
405 .store = _store, \
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700406};
407
408/*
409 * Define attributes visible for the edac_device instance object
410 * Each contains a pointer to a show and an optional set
411 * function pointer that does the low level output/input
412 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700413INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
414INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700415
416/* list of edac_dev 'instance' attributes */
417static struct instance_attribute *device_instance_attr[] = {
418 &attr_instance_ce_count,
419 &attr_instance_ue_count,
420 NULL,
421};
422
423/* The 'ktype' for each edac_dev 'instance' */
424static struct kobj_type ktype_instance_ctrl = {
425 .release = edac_device_ctrl_instance_release,
426 .sysfs_ops = &device_instance_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700427 .default_attrs = (struct attribute **)device_instance_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700428};
429
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700430/* edac_dev -> instance -> block information */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700431
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700432#define to_block(k) container_of(k, struct edac_device_block, kobj)
433#define to_block_attr(a) \
434 container_of(a, struct edac_dev_sysfs_block_attribute, attr)
435
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700436/*
437 * Set of low-level block attribute show functions
438 */
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700439static ssize_t block_ue_count_show(struct kobject *kobj,
440 struct attribute *attr, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700441{
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700442 struct edac_device_block *block = to_block(kobj);
443
Douglas Thompson079708b2007-07-19 01:49:58 -0700444 return sprintf(data, "%u\n", block->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700445}
446
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700447static ssize_t block_ce_count_show(struct kobject *kobj,
448 struct attribute *attr, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700449{
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700450 struct edac_device_block *block = to_block(kobj);
451
Douglas Thompson079708b2007-07-19 01:49:58 -0700452 return sprintf(data, "%u\n", block->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700453}
454
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700455/* DEVICE block kobject release() function */
456static void edac_device_ctrl_block_release(struct kobject *kobj)
457{
458 struct edac_device_block *block;
459
Joe Perches956b9ba2012-04-29 17:08:39 -0300460 edac_dbg(1, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700461
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700462 /* get the container of the kobj */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700463 block = to_block(kobj);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700464
465 /* map from 'block kobj' to 'block->instance->controller->main_kobj'
466 * now 'release' the block kobject
467 */
468 kobject_put(&block->instance->ctl->kobj);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700469}
470
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700471
472/* Function to 'show' fields from the edac_dev 'block' structure */
473static ssize_t edac_dev_block_show(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700474 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700475{
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700476 struct edac_dev_sysfs_block_attribute *block_attr =
477 to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700478
Douglas Thompson079708b2007-07-19 01:49:58 -0700479 if (block_attr->show)
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700480 return block_attr->show(kobj, attr, buffer);
Douglas Thompson079708b2007-07-19 01:49:58 -0700481 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700482}
483
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700484/* Function to 'store' fields into the edac_dev 'block' structure */
485static ssize_t edac_dev_block_store(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700486 struct attribute *attr,
487 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700488{
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700489 struct edac_dev_sysfs_block_attribute *block_attr;
490
491 block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700492
Douglas Thompson079708b2007-07-19 01:49:58 -0700493 if (block_attr->store)
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700494 return block_attr->store(kobj, attr, buffer, count);
Douglas Thompson079708b2007-07-19 01:49:58 -0700495 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700496}
497
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700498/* edac_dev file operations for a 'block' */
Emese Revfy52cf25d2010-01-19 02:58:23 +0100499static const struct sysfs_ops device_block_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700500 .show = edac_dev_block_show,
501 .store = edac_dev_block_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700502};
503
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700504#define BLOCK_ATTR(_name,_mode,_show,_store) \
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700505static struct edac_dev_sysfs_block_attribute attr_block_##_name = { \
Douglas Thompson052dfb42007-07-19 01:50:13 -0700506 .attr = {.name = __stringify(_name), .mode = _mode }, \
507 .show = _show, \
508 .store = _store, \
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700509};
510
Douglas Thompson079708b2007-07-19 01:49:58 -0700511BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
512BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700513
514/* list of edac_dev 'block' attributes */
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700515static struct edac_dev_sysfs_block_attribute *device_block_attr[] = {
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700516 &attr_block_ce_count,
517 &attr_block_ue_count,
518 NULL,
519};
520
521/* The 'ktype' for each edac_dev 'block' */
522static struct kobj_type ktype_block_ctrl = {
523 .release = edac_device_ctrl_block_release,
524 .sysfs_ops = &device_block_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700525 .default_attrs = (struct attribute **)device_block_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700526};
527
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700528/* block ctor/dtor code */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700529
530/*
531 * edac_device_create_block
532 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700533static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700534 struct edac_device_instance *instance,
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700535 struct edac_device_block *block)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700536{
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700537 int i;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700538 int err;
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700539 struct edac_dev_sysfs_block_attribute *sysfs_attrib;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700540 struct kobject *main_kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700541
Joe Perches956b9ba2012-04-29 17:08:39 -0300542 edac_dbg(4, "Instance '%s' inst_p=%p block '%s' block_p=%p\n",
543 instance->name, instance, block->name, block);
544 edac_dbg(4, "block kobj=%p block kobj->parent=%p\n",
545 &block->kobj, &block->kobj.parent);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700546
547 /* init this block's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700548 memset(&block->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700549
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700550 /* bump the main kobject's reference count for this controller
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300551 * and this instance is dependent on the main
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700552 */
553 main_kobj = kobject_get(&edac_dev->kobj);
554 if (!main_kobj) {
555 err = -ENODEV;
556 goto err_out;
557 }
558
559 /* Add this block's kobject */
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400560 err = kobject_init_and_add(&block->kobj, &ktype_block_ctrl,
561 &instance->kobj,
562 "%s", block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700563 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300564 edac_dbg(1, "Failed to register instance '%s'\n", block->name);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700565 kobject_put(main_kobj);
566 err = -ENODEV;
567 goto err_out;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700568 }
569
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700570 /* If there are driver level block attributes, then added them
571 * to the block kobject
572 */
573 sysfs_attrib = block->block_attributes;
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700574 if (sysfs_attrib && block->nr_attribs) {
575 for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
576
Joe Perches956b9ba2012-04-29 17:08:39 -0300577 edac_dbg(4, "creating block attrib='%s' attrib->%p to kobj=%p\n",
578 sysfs_attrib->attr.name,
579 sysfs_attrib, &block->kobj);
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700580
581 /* Create each block_attribute file */
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700582 err = sysfs_create_file(&block->kobj,
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700583 &sysfs_attrib->attr);
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700584 if (err)
585 goto err_on_attrib;
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700586 }
587 }
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400588 kobject_uevent(&block->kobj, KOBJ_ADD);
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700589
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700590 return 0;
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700591
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700592 /* Error unwind stack */
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700593err_on_attrib:
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800594 kobject_put(&block->kobj);
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700595
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700596err_out:
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700597 return err;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700598}
599
600/*
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700601 * edac_device_delete_block(edac_dev,block);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700602 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700603static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700604 struct edac_device_block *block)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700605{
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700606 struct edac_dev_sysfs_block_attribute *sysfs_attrib;
607 int i;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700608
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700609 /* if this block has 'attributes' then we need to iterate over the list
610 * and 'remove' the attributes on this block
611 */
612 sysfs_attrib = block->block_attributes;
613 if (sysfs_attrib && block->nr_attribs) {
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700614 for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
615
616 /* remove each block_attrib file */
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700617 sysfs_remove_file(&block->kobj,
618 (struct attribute *) sysfs_attrib);
619 }
620 }
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700621
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700622 /* unregister this block's kobject, SEE:
623 * edac_device_ctrl_block_release() callback operation
624 */
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800625 kobject_put(&block->kobj);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700626}
627
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700628/* instance ctor/dtor code */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700629
630/*
631 * edac_device_create_instance
632 * create just one instance of an edac_device 'instance'
633 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700634static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700635 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700636{
637 int i, j;
638 int err;
639 struct edac_device_instance *instance;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700640 struct kobject *main_kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700641
642 instance = &edac_dev->instances[idx];
643
644 /* Init the instance's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700645 memset(&instance->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700646
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700647 instance->ctl = edac_dev;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700648
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700649 /* bump the main kobject's reference count for this controller
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300650 * and this instance is dependent on the main
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700651 */
652 main_kobj = kobject_get(&edac_dev->kobj);
653 if (!main_kobj) {
654 err = -ENODEV;
655 goto err_out;
656 }
657
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400658 /* Formally register this instance's kobject under the edac_device */
659 err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
660 &edac_dev->kobj, "%s", instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700661 if (err != 0) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300662 edac_dbg(2, "Failed to register instance '%s'\n",
663 instance->name);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700664 kobject_put(main_kobj);
665 goto err_out;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700666 }
667
Joe Perches956b9ba2012-04-29 17:08:39 -0300668 edac_dbg(4, "now register '%d' blocks for instance %d\n",
669 instance->nr_blocks, idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700670
671 /* register all blocks of this instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700672 for (i = 0; i < instance->nr_blocks; i++) {
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700673 err = edac_device_create_block(edac_dev, instance,
674 &instance->blocks[i]);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700675 if (err) {
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700676 /* If any fail, remove all previous ones */
Douglas Thompson52490c82007-07-19 01:50:20 -0700677 for (j = 0; j < i; j++)
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700678 edac_device_delete_block(edac_dev,
679 &instance->blocks[j]);
680 goto err_release_instance_kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700681 }
682 }
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400683 kobject_uevent(&instance->kobj, KOBJ_ADD);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700684
Joe Perches956b9ba2012-04-29 17:08:39 -0300685 edac_dbg(4, "Registered instance %d '%s' kobject\n",
686 idx, instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700687
688 return 0;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700689
690 /* error unwind stack */
691err_release_instance_kobj:
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800692 kobject_put(&instance->kobj);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700693
694err_out:
695 return err;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700696}
697
698/*
699 * edac_device_remove_instance
700 * remove an edac_device instance
701 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700702static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
703 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700704{
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700705 struct edac_device_instance *instance;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700706 int i;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700707
708 instance = &edac_dev->instances[idx];
709
710 /* unregister all blocks in this instance */
Douglas Thompson52490c82007-07-19 01:50:20 -0700711 for (i = 0; i < instance->nr_blocks; i++)
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700712 edac_device_delete_block(edac_dev, &instance->blocks[i]);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700713
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700714 /* unregister this instance's kobject, SEE:
715 * edac_device_ctrl_instance_release() for callback operation
716 */
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800717 kobject_put(&instance->kobj);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700718}
719
720/*
721 * edac_device_create_instances
722 * create the first level of 'instances' for this device
723 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
724 */
725static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
726{
727 int i, j;
728 int err;
729
Joe Perches956b9ba2012-04-29 17:08:39 -0300730 edac_dbg(0, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700731
732 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700733 for (i = 0; i < edac_dev->nr_instances; i++) {
734 err = edac_device_create_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700735 if (err) {
736 /* unwind previous instances on error */
Douglas Thompson52490c82007-07-19 01:50:20 -0700737 for (j = 0; j < i; j++)
Douglas Thompson079708b2007-07-19 01:49:58 -0700738 edac_device_delete_instance(edac_dev, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700739 return err;
740 }
741 }
742
743 return 0;
744}
745
746/*
747 * edac_device_delete_instances(edac_dev);
748 * unregister all the kobjects of the instances
749 */
750static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
751{
752 int i;
753
754 /* iterate over creation of the instances */
Douglas Thompson52490c82007-07-19 01:50:20 -0700755 for (i = 0; i < edac_dev->nr_instances; i++)
Douglas Thompson079708b2007-07-19 01:49:58 -0700756 edac_device_delete_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700757}
758
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700759/* edac_dev sysfs ctor/dtor code */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700760
761/*
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700762 * edac_device_add_main_sysfs_attributes
Douglas Thompson42a8e392007-07-19 01:50:10 -0700763 * add some attributes to this instance's main kobject
764 */
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700765static int edac_device_add_main_sysfs_attributes(
Douglas Thompson42a8e392007-07-19 01:50:10 -0700766 struct edac_device_ctl_info *edac_dev)
767{
Douglas Thompson052dfb42007-07-19 01:50:13 -0700768 struct edac_dev_sysfs_attribute *sysfs_attrib;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700769 int err = 0;
Douglas Thompson42a8e392007-07-19 01:50:10 -0700770
Douglas Thompson052dfb42007-07-19 01:50:13 -0700771 sysfs_attrib = edac_dev->sysfs_attributes;
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700772 if (sysfs_attrib) {
773 /* iterate over the array and create an attribute for each
774 * entry in the list
775 */
776 while (sysfs_attrib->attr.name != NULL) {
777 err = sysfs_create_file(&edac_dev->kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700778 (struct attribute*) sysfs_attrib);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700779 if (err)
780 goto err_out;
Douglas Thompson42a8e392007-07-19 01:50:10 -0700781
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700782 sysfs_attrib++;
783 }
Douglas Thompson052dfb42007-07-19 01:50:13 -0700784 }
Douglas Thompson42a8e392007-07-19 01:50:10 -0700785
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700786err_out:
787 return err;
788}
789
790/*
791 * edac_device_remove_main_sysfs_attributes
792 * remove any attributes to this instance's main kobject
793 */
794static void edac_device_remove_main_sysfs_attributes(
795 struct edac_device_ctl_info *edac_dev)
796{
797 struct edac_dev_sysfs_attribute *sysfs_attrib;
798
799 /* if there are main attributes, defined, remove them. First,
800 * point to the start of the array and iterate over it
801 * removing each attribute listed from this device's instance's kobject
802 */
803 sysfs_attrib = edac_dev->sysfs_attributes;
804 if (sysfs_attrib) {
805 while (sysfs_attrib->attr.name != NULL) {
806 sysfs_remove_file(&edac_dev->kobj,
807 (struct attribute *) sysfs_attrib);
808 sysfs_attrib++;
809 }
810 }
Douglas Thompson42a8e392007-07-19 01:50:10 -0700811}
812
813/*
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700814 * edac_device_create_sysfs() Constructor
815 *
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700816 * accept a created edac_device control structure
817 * and 'export' it to sysfs. The 'main' kobj should already have been
818 * created. 'instance' and 'block' kobjects should be registered
819 * along with any 'block' attributes from the low driver. In addition,
820 * the main attributes (if any) are connected to the main kobject of
821 * the control structure.
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700822 *
823 * Return:
824 * 0 Success
825 * !0 Failure
826 */
827int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
828{
829 int err;
Douglas Thompson079708b2007-07-19 01:49:58 -0700830 struct kobject *edac_kobj = &edac_dev->kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700831
Joe Perches956b9ba2012-04-29 17:08:39 -0300832 edac_dbg(0, "idx=%d\n", edac_dev->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700833
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700834 /* go create any main attributes callers wants */
835 err = edac_device_add_main_sysfs_attributes(edac_dev);
836 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300837 edac_dbg(0, "failed to add sysfs attribs\n");
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700838 goto err_out;
Douglas Thompson42a8e392007-07-19 01:50:10 -0700839 }
840
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700841 /* create a symlink from the edac device
842 * to the platform 'device' being used for this
843 */
844 err = sysfs_create_link(edac_kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700845 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700846 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300847 edac_dbg(0, "sysfs_create_link() returned err= %d\n", err);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700848 goto err_remove_main_attribs;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700849 }
850
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700851 /* Create the first level instance directories
852 * In turn, the nested blocks beneath the instances will
853 * be registered as well
854 */
855 err = edac_device_create_instances(edac_dev);
856 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300857 edac_dbg(0, "edac_device_create_instances() returned err= %d\n",
858 err);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700859 goto err_remove_link;
860 }
861
862
Joe Perches956b9ba2012-04-29 17:08:39 -0300863 edac_dbg(4, "create-instances done, idx=%d\n", edac_dev->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700864
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700865 return 0;
866
867 /* Error unwind stack */
Douglas Thompson42a8e392007-07-19 01:50:10 -0700868err_remove_link:
869 /* remove the sym link */
870 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700871
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700872err_remove_main_attribs:
873 edac_device_remove_main_sysfs_attributes(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700874
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700875err_out:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700876 return err;
877}
878
879/*
880 * edac_device_remove_sysfs() destructor
881 *
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700882 * given an edac_device struct, tear down the kobject resources
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700883 */
884void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
885{
Joe Perches956b9ba2012-04-29 17:08:39 -0300886 edac_dbg(0, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700887
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700888 /* remove any main attributes for this device */
889 edac_device_remove_main_sysfs_attributes(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700890
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700891 /* remove the device sym link */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700892 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
893
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700894 /* walk the instance/block kobject tree, deconstructing it */
895 edac_device_delete_instances(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700896}