blob: 32b2a8e53dc7758da006cdf30204759ff5251846 [file] [log] [blame]
Douglas Thompsone27e3da2007-07-19 01:49:36 -07001/*
2 * file for managing the edac_device class of devices for EDAC
3 *
4 * (C) 2007 SoftwareBitMaker(http://www.softwarebitmaker.com)
5 * This file may be distributed under the terms of the
6 * GNU General Public License.
7 *
8 * Written Doug Thompson <norsk5@xmission.com>
9 *
10 */
11
Douglas Thompsone27e3da2007-07-19 01:49:36 -070012#include <linux/ctype.h>
13
14#include "edac_core.h"
15#include "edac_module.h"
16
Douglas Thompsone27e3da2007-07-19 01:49:36 -070017#define EDAC_DEVICE_SYMLINK "device"
18
19#define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
20#define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
21
Douglas Thompsone27e3da2007-07-19 01:49:36 -070022/************************** edac_device sysfs code and data **************/
23
24/*
25 * Set of edac_device_ctl_info attribute store/show functions
26 */
27
28/* 'log_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -070029static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
30 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070031{
Douglas Thompson079708b2007-07-19 01:49:58 -070032 return sprintf(data, "%u\n", ctl_info->log_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070033}
34
Douglas Thompson079708b2007-07-19 01:49:58 -070035static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
36 *ctl_info, const char *data,
37 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070038{
39 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070040 ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070041
Douglas Thompson079708b2007-07-19 01:49:58 -070042 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070043}
44
45/* 'log_ce' */
Douglas Thompson079708b2007-07-19 01:49:58 -070046static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
47 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070048{
Douglas Thompson079708b2007-07-19 01:49:58 -070049 return sprintf(data, "%u\n", ctl_info->log_ce);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070050}
51
Douglas Thompson079708b2007-07-19 01:49:58 -070052static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
53 *ctl_info, const char *data,
54 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070055{
56 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070057 ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070058
Douglas Thompson079708b2007-07-19 01:49:58 -070059 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070060}
61
Douglas Thompsone27e3da2007-07-19 01:49:36 -070062/* 'panic_on_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -070063static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
64 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070065{
Douglas Thompson079708b2007-07-19 01:49:58 -070066 return sprintf(data, "%u\n", ctl_info->panic_on_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070067}
68
Douglas Thompson079708b2007-07-19 01:49:58 -070069static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
70 *ctl_info, const char *data,
71 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070072{
73 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070074 ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070075
76 return count;
77}
78
79/* 'poll_msec' show and store functions*/
Douglas Thompson079708b2007-07-19 01:49:58 -070080static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
81 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070082{
Douglas Thompson079708b2007-07-19 01:49:58 -070083 return sprintf(data, "%u\n", ctl_info->poll_msec);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070084}
85
Douglas Thompson079708b2007-07-19 01:49:58 -070086static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
87 *ctl_info, const char *data,
88 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070089{
90 unsigned long value;
91
92 /* get the value and enforce that it is non-zero, must be at least
93 * one millisecond for the delay period, between scans
94 * Then cancel last outstanding delay for the work request
95 * and set a new one.
96 */
Douglas Thompson079708b2007-07-19 01:49:58 -070097 value = simple_strtoul(data, NULL, 0);
98 edac_device_reset_delay_period(ctl_info, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070099
Douglas Thompson079708b2007-07-19 01:49:58 -0700100 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700101}
102
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700103/* edac_device_ctl_info specific attribute structure */
104struct ctl_info_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700105 struct attribute attr;
106 ssize_t(*show) (struct edac_device_ctl_info *, char *);
107 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700108};
109
110#define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
111#define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
112
113/* Function to 'show' fields from the edac_dev 'ctl_info' structure */
114static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700115 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700116{
Douglas Thompson079708b2007-07-19 01:49:58 -0700117 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
118 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700119
Douglas Thompson079708b2007-07-19 01:49:58 -0700120 if (ctl_info_attr->show)
121 return ctl_info_attr->show(edac_dev, buffer);
122 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700123}
124
125/* Function to 'store' fields into the edac_dev 'ctl_info' structure */
126static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700127 struct attribute *attr,
128 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700129{
Douglas Thompson079708b2007-07-19 01:49:58 -0700130 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
131 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700132
Douglas Thompson079708b2007-07-19 01:49:58 -0700133 if (ctl_info_attr->store)
134 return ctl_info_attr->store(edac_dev, buffer, count);
135 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700136}
137
138/* edac_dev file operations for an 'ctl_info' */
139static struct sysfs_ops device_ctl_info_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700140 .show = edac_dev_ctl_info_show,
141 .store = edac_dev_ctl_info_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700142};
143
144#define CTL_INFO_ATTR(_name,_mode,_show,_store) \
145static struct ctl_info_attribute attr_ctl_info_##_name = { \
146 .attr = {.name = __stringify(_name), .mode = _mode }, \
147 .show = _show, \
148 .store = _store, \
149};
150
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700151/* Declare the various ctl_info attributes here and their respective ops */
Douglas Thompson079708b2007-07-19 01:49:58 -0700152CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
153 edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
154CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
155 edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
156CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
157 edac_device_ctl_panic_on_ue_show,
158 edac_device_ctl_panic_on_ue_store);
159CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
160 edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700161
162/* Base Attributes of the EDAC_DEVICE ECC object */
163static struct ctl_info_attribute *device_ctrl_attr[] = {
164 &attr_ctl_info_panic_on_ue,
165 &attr_ctl_info_log_ue,
166 &attr_ctl_info_log_ce,
167 &attr_ctl_info_poll_msec,
168 NULL,
169};
170
171/* Main DEVICE kobject release() function */
172static void edac_device_ctrl_master_release(struct kobject *kobj)
173{
174 struct edac_device_ctl_info *edac_dev;
175
176 edac_dev = to_edacdev(kobj);
177
178 debugf1("%s()\n", __func__);
179 complete(&edac_dev->kobj_complete);
180}
181
182static struct kobj_type ktype_device_ctrl = {
183 .release = edac_device_ctrl_master_release,
184 .sysfs_ops = &device_ctl_info_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700185 .default_attrs = (struct attribute **)device_ctrl_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700186};
187
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700188/**************** edac_device main kobj ctor/dtor code *********************/
189
190/*
191 * edac_device_register_main_kobj
192 *
193 * perform the high level setup for the new edac_device instance
194 *
195 * Return: 0 SUCCESS
196 * !0 FAILURE
197 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700198static int edac_device_register_main_kobj(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700199{
200 int err = 0;
201 struct sysdev_class *edac_class;
202
203 debugf1("%s()\n", __func__);
204
Douglas Thompson079708b2007-07-19 01:49:58 -0700205 /* get the /sys/devices/system/edac reference */
206 edac_class = edac_get_edac_class();
207 if (edac_class == NULL) {
208 debugf1("%s() no edac_class error=%d\n", __func__, err);
209 return err;
210 }
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700211
212 /* Point to the 'edac_class' this instance 'reports' to */
213 edac_dev->edac_class = edac_class;
214
215 /* Init the devices's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700216 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700217 edac_dev->kobj.ktype = &ktype_device_ctrl;
218
219 /* set this new device under the edac_class kobject */
220 edac_dev->kobj.parent = &edac_class->kset.kobj;
221
222 /* generate sysfs "..../edac/<name>" */
Douglas Thompson079708b2007-07-19 01:49:58 -0700223 debugf1("%s() set name of kobject to: %s\n", __func__, edac_dev->name);
224 err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700225 if (err)
226 return err;
227 err = kobject_register(&edac_dev->kobj);
228 if (err) {
229 debugf1("%s()Failed to register '.../edac/%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700230 __func__, edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700231 return err;
232 }
233
234 debugf1("%s() Registered '.../edac/%s' kobject\n",
235 __func__, edac_dev->name);
236
237 return 0;
238}
239
240/*
241 * edac_device_unregister_main_kobj:
242 * the '..../edac/<name>' kobject
243 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700244static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
245 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700246{
247 debugf0("%s()\n", __func__);
248 debugf1("%s() name of kobject is: %s\n",
249 __func__, kobject_name(&edac_dev->kobj));
250
251 init_completion(&edac_dev->kobj_complete);
252
253 /*
254 * Unregister the edac device's kobject and
255 * wait for reference count to reach 0.
256 */
257 kobject_unregister(&edac_dev->kobj);
258 wait_for_completion(&edac_dev->kobj_complete);
259}
260
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700261/*************** edac_dev -> instance information ***********/
262
263/*
264 * Set of low-level instance attribute show functions
265 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700266static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
267 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700268{
Douglas Thompson079708b2007-07-19 01:49:58 -0700269 return sprintf(data, "%u\n", instance->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700270}
271
Douglas Thompson079708b2007-07-19 01:49:58 -0700272static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
273 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700274{
Douglas Thompson079708b2007-07-19 01:49:58 -0700275 return sprintf(data, "%u\n", instance->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700276}
277
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700278#define to_instance(k) container_of(k, struct edac_device_instance, kobj)
279#define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
280
281/* DEVICE instance kobject release() function */
282static void edac_device_ctrl_instance_release(struct kobject *kobj)
283{
284 struct edac_device_instance *instance;
285
286 debugf1("%s()\n", __func__);
287
288 instance = to_instance(kobj);
289 complete(&instance->kobj_complete);
290}
291
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700292/* instance specific attribute structure */
293struct instance_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700294 struct attribute attr;
295 ssize_t(*show) (struct edac_device_instance *, char *);
296 ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700297};
298
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700299/* Function to 'show' fields from the edac_dev 'instance' structure */
300static ssize_t edac_dev_instance_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700301 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700302{
Douglas Thompson079708b2007-07-19 01:49:58 -0700303 struct edac_device_instance *instance = to_instance(kobj);
304 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700305
Douglas Thompson079708b2007-07-19 01:49:58 -0700306 if (instance_attr->show)
307 return instance_attr->show(instance, buffer);
308 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700309}
310
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700311/* Function to 'store' fields into the edac_dev 'instance' structure */
312static ssize_t edac_dev_instance_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700313 struct attribute *attr,
314 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700315{
Douglas Thompson079708b2007-07-19 01:49:58 -0700316 struct edac_device_instance *instance = to_instance(kobj);
317 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700318
Douglas Thompson079708b2007-07-19 01:49:58 -0700319 if (instance_attr->store)
320 return instance_attr->store(instance, buffer, count);
321 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700322}
323
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700324/* edac_dev file operations for an 'instance' */
325static struct sysfs_ops device_instance_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700326 .show = edac_dev_instance_show,
327 .store = edac_dev_instance_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700328};
329
330#define INSTANCE_ATTR(_name,_mode,_show,_store) \
331static struct instance_attribute attr_instance_##_name = { \
332 .attr = {.name = __stringify(_name), .mode = _mode }, \
333 .show = _show, \
334 .store = _store, \
335};
336
337/*
338 * Define attributes visible for the edac_device instance object
339 * Each contains a pointer to a show and an optional set
340 * function pointer that does the low level output/input
341 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700342INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
343INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700344
345/* list of edac_dev 'instance' attributes */
346static struct instance_attribute *device_instance_attr[] = {
347 &attr_instance_ce_count,
348 &attr_instance_ue_count,
349 NULL,
350};
351
352/* The 'ktype' for each edac_dev 'instance' */
353static struct kobj_type ktype_instance_ctrl = {
354 .release = edac_device_ctrl_instance_release,
355 .sysfs_ops = &device_instance_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700356 .default_attrs = (struct attribute **)device_instance_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700357};
358
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700359/*************** edac_dev -> instance -> block information *********/
360
361/*
362 * Set of low-level block attribute show functions
363 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700364static ssize_t block_ue_count_show(struct edac_device_block *block, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700365{
Douglas Thompson079708b2007-07-19 01:49:58 -0700366 return sprintf(data, "%u\n", block->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700367}
368
Douglas Thompson079708b2007-07-19 01:49:58 -0700369static ssize_t block_ce_count_show(struct edac_device_block *block, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700370{
Douglas Thompson079708b2007-07-19 01:49:58 -0700371 return sprintf(data, "%u\n", block->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700372}
373
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700374#define to_block(k) container_of(k, struct edac_device_block, kobj)
375#define to_block_attr(a) container_of(a,struct block_attribute,attr)
376
377/* DEVICE block kobject release() function */
378static void edac_device_ctrl_block_release(struct kobject *kobj)
379{
380 struct edac_device_block *block;
381
382 debugf1("%s()\n", __func__);
383
384 block = to_block(kobj);
385 complete(&block->kobj_complete);
386}
387
388/* block specific attribute structure */
389struct block_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700390 struct attribute attr;
391 ssize_t(*show) (struct edac_device_block *, char *);
392 ssize_t(*store) (struct edac_device_block *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700393};
394
395/* Function to 'show' fields from the edac_dev 'block' structure */
396static ssize_t edac_dev_block_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700397 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700398{
Douglas Thompson079708b2007-07-19 01:49:58 -0700399 struct edac_device_block *block = to_block(kobj);
400 struct block_attribute *block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700401
Douglas Thompson079708b2007-07-19 01:49:58 -0700402 if (block_attr->show)
403 return block_attr->show(block, buffer);
404 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700405}
406
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700407/* Function to 'store' fields into the edac_dev 'block' structure */
408static ssize_t edac_dev_block_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700409 struct attribute *attr,
410 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700411{
Douglas Thompson079708b2007-07-19 01:49:58 -0700412 struct edac_device_block *block = to_block(kobj);
413 struct block_attribute *block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700414
Douglas Thompson079708b2007-07-19 01:49:58 -0700415 if (block_attr->store)
416 return block_attr->store(block, buffer, count);
417 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700418}
419
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700420/* edac_dev file operations for a 'block' */
421static struct sysfs_ops device_block_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700422 .show = edac_dev_block_show,
423 .store = edac_dev_block_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700424};
425
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700426#define BLOCK_ATTR(_name,_mode,_show,_store) \
427static struct block_attribute attr_block_##_name = { \
428 .attr = {.name = __stringify(_name), .mode = _mode }, \
429 .show = _show, \
430 .store = _store, \
431};
432
Douglas Thompson079708b2007-07-19 01:49:58 -0700433BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
434BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700435
436/* list of edac_dev 'block' attributes */
437static struct block_attribute *device_block_attr[] = {
438 &attr_block_ce_count,
439 &attr_block_ue_count,
440 NULL,
441};
442
443/* The 'ktype' for each edac_dev 'block' */
444static struct kobj_type ktype_block_ctrl = {
445 .release = edac_device_ctrl_block_release,
446 .sysfs_ops = &device_block_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700447 .default_attrs = (struct attribute **)device_block_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700448};
449
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700450/************** block ctor/dtor code ************/
451
452/*
453 * edac_device_create_block
454 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700455static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
456 struct edac_device_instance *instance,
457 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700458{
459 int err;
460 struct edac_device_block *block;
461
462 block = &instance->blocks[idx];
463
464 debugf1("%s() Instance '%s' block[%d] '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700465 __func__, instance->name, idx, block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700466
467 /* init this block's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700468 memset(&block->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700469 block->kobj.parent = &instance->kobj;
470 block->kobj.ktype = &ktype_block_ctrl;
471
Douglas Thompson079708b2007-07-19 01:49:58 -0700472 err = kobject_set_name(&block->kobj, "%s", block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700473 if (err)
474 return err;
475
476 err = kobject_register(&block->kobj);
477 if (err) {
478 debugf1("%s()Failed to register instance '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700479 __func__, block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700480 return err;
481 }
482
483 return 0;
484}
485
486/*
487 * edac_device_delete_block(edac_dev,j);
488 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700489static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
490 struct edac_device_instance *instance,
491 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700492{
493 struct edac_device_block *block;
494
495 block = &instance->blocks[idx];
496
497 /* unregister this block's kobject */
498 init_completion(&block->kobj_complete);
499 kobject_unregister(&block->kobj);
500 wait_for_completion(&block->kobj_complete);
501}
502
503/************** instance ctor/dtor code ************/
504
505/*
506 * edac_device_create_instance
507 * create just one instance of an edac_device 'instance'
508 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700509static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
510 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700511{
512 int i, j;
513 int err;
514 struct edac_device_instance *instance;
515
516 instance = &edac_dev->instances[idx];
517
518 /* Init the instance's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700519 memset(&instance->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700520
521 /* set this new device under the edac_device main kobject */
522 instance->kobj.parent = &edac_dev->kobj;
523 instance->kobj.ktype = &ktype_instance_ctrl;
524
Douglas Thompson079708b2007-07-19 01:49:58 -0700525 err = kobject_set_name(&instance->kobj, "%s", instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700526 if (err)
527 return err;
528
529 err = kobject_register(&instance->kobj);
530 if (err != 0) {
531 debugf2("%s() Failed to register instance '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700532 __func__, instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700533 return err;
534 }
535
536 debugf1("%s() now register '%d' blocks for instance %d\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700537 __func__, instance->nr_blocks, idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700538
539 /* register all blocks of this instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700540 for (i = 0; i < instance->nr_blocks; i++) {
541 err = edac_device_create_block(edac_dev, instance, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700542 if (err) {
543 for (j = 0; j < i; j++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700544 edac_device_delete_block(edac_dev, instance, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700545 }
546 return err;
547 }
548 }
549
550 debugf1("%s() Registered instance %d '%s' kobject\n",
551 __func__, idx, instance->name);
552
553 return 0;
554}
555
556/*
557 * edac_device_remove_instance
558 * remove an edac_device instance
559 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700560static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
561 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700562{
563 int i;
564 struct edac_device_instance *instance;
565
566 instance = &edac_dev->instances[idx];
567
568 /* unregister all blocks in this instance */
569 for (i = 0; i < instance->nr_blocks; i++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700570 edac_device_delete_block(edac_dev, instance, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700571 }
572
573 /* unregister this instance's kobject */
574 init_completion(&instance->kobj_complete);
575 kobject_unregister(&instance->kobj);
576 wait_for_completion(&instance->kobj_complete);
577}
578
579/*
580 * edac_device_create_instances
581 * create the first level of 'instances' for this device
582 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
583 */
584static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
585{
586 int i, j;
587 int err;
588
589 debugf0("%s()\n", __func__);
590
591 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700592 for (i = 0; i < edac_dev->nr_instances; i++) {
593 err = edac_device_create_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700594 if (err) {
595 /* unwind previous instances on error */
596 for (j = 0; j < i; j++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700597 edac_device_delete_instance(edac_dev, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700598 }
599 return err;
600 }
601 }
602
603 return 0;
604}
605
606/*
607 * edac_device_delete_instances(edac_dev);
608 * unregister all the kobjects of the instances
609 */
610static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
611{
612 int i;
613
614 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700615 for (i = 0; i < edac_dev->nr_instances; i++) {
616 edac_device_delete_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700617 }
618}
619
620/******************* edac_dev sysfs ctor/dtor code *************/
621
622/*
Douglas Thompson42a8e392007-07-19 01:50:10 -0700623 * edac_device_add_sysfs_attributes
624 * add some attributes to this instance's main kobject
625 */
626static int edac_device_add_sysfs_attributes(
627 struct edac_device_ctl_info *edac_dev)
628{
629 int err;
630 struct edac_dev_sysfs_attribute *sysfs_attrib;
631
632 /* point to the start of the array and iterate over it
633 * adding each attribute listed to this mci instance's kobject
634 */
635 sysfs_attrib = edac_dev->sysfs_attributes;
636
637 while (sysfs_attrib->attr.name != NULL) {
638 err = sysfs_create_file(&edac_dev->kobj,
639 (struct attribute*) sysfs_attrib);
640 if (err) {
641 return err;
642 }
643
644 sysfs_attrib++;
645 }
646
647 return 0;
648}
649
650/*
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700651 * edac_device_create_sysfs() Constructor
652 *
653 * Create a new edac_device kobject instance,
654 *
655 * Return:
656 * 0 Success
657 * !0 Failure
658 */
659int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
660{
661 int err;
Douglas Thompson079708b2007-07-19 01:49:58 -0700662 struct kobject *edac_kobj = &edac_dev->kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700663
664 /* register this instance's main kobj with the edac class kobj */
665 err = edac_device_register_main_kobj(edac_dev);
666 if (err)
667 return err;
668
669 debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
670
Douglas Thompson42a8e392007-07-19 01:50:10 -0700671 /* If the low level driver requests some sysfs entries
672 * then go create them here
673 */
674 if (edac_dev->sysfs_attributes != NULL) {
675 err = edac_device_add_sysfs_attributes(edac_dev);
676 if (err) {
677 debugf0("%s() failed to add sysfs attribs\n",
678 __func__);
679 goto err_unreg_object;
680 }
681 }
682
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700683 /* create a symlink from the edac device
684 * to the platform 'device' being used for this
685 */
686 err = sysfs_create_link(edac_kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700687 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700688 if (err) {
689 debugf0("%s() sysfs_create_link() returned err= %d\n",
690 __func__, err);
Douglas Thompson42a8e392007-07-19 01:50:10 -0700691 goto err_unreg_object;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700692 }
693
694 debugf0("%s() calling create-instances, idx=%d\n",
695 __func__, edac_dev->dev_idx);
696
697 /* Create the first level instance directories */
698 err = edac_device_create_instances(edac_dev);
699 if (err) {
Douglas Thompson42a8e392007-07-19 01:50:10 -0700700 goto err_remove_link;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700701 }
702
703 return 0;
704
705 /* Error unwind stack */
Douglas Thompson42a8e392007-07-19 01:50:10 -0700706err_remove_link:
707 /* remove the sym link */
708 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700709
Douglas Thompson42a8e392007-07-19 01:50:10 -0700710err_unreg_object:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700711 edac_device_unregister_main_kobj(edac_dev);
712
713 return err;
714}
715
716/*
717 * edac_device_remove_sysfs() destructor
718 *
719 * remove a edac_device instance
720 */
721void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
722{
723 debugf0("%s()\n", __func__);
724
725 edac_device_delete_instances(edac_dev);
726
727 /* remove the sym link */
728 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
729
730 /* unregister the instance's main kobj */
731 edac_device_unregister_main_kobj(edac_dev);
732}