blob: 849d569dd2ce4c2976025703eb58d4a0ba31a8e2 [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
12#include <linux/module.h>
13#include <linux/sysdev.h>
14#include <linux/ctype.h>
15
16#include "edac_core.h"
17#include "edac_module.h"
18
Douglas Thompsone27e3da2007-07-19 01:49:36 -070019#define EDAC_DEVICE_SYMLINK "device"
20
21#define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
22#define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
23
Douglas Thompsone27e3da2007-07-19 01:49:36 -070024/************************** edac_device sysfs code and data **************/
25
26/*
27 * Set of edac_device_ctl_info attribute store/show functions
28 */
29
30/* 'log_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -070031static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
32 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070033{
Douglas Thompson079708b2007-07-19 01:49:58 -070034 return sprintf(data, "%u\n", ctl_info->log_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070035}
36
Douglas Thompson079708b2007-07-19 01:49:58 -070037static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
38 *ctl_info, const char *data,
39 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070040{
41 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070042 ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070043
Douglas Thompson079708b2007-07-19 01:49:58 -070044 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070045}
46
47/* 'log_ce' */
Douglas Thompson079708b2007-07-19 01:49:58 -070048static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
49 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070050{
Douglas Thompson079708b2007-07-19 01:49:58 -070051 return sprintf(data, "%u\n", ctl_info->log_ce);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070052}
53
Douglas Thompson079708b2007-07-19 01:49:58 -070054static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
55 *ctl_info, const char *data,
56 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070057{
58 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070059 ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070060
Douglas Thompson079708b2007-07-19 01:49:58 -070061 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070062}
63
Douglas Thompsone27e3da2007-07-19 01:49:36 -070064/* 'panic_on_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -070065static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
66 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070067{
Douglas Thompson079708b2007-07-19 01:49:58 -070068 return sprintf(data, "%u\n", ctl_info->panic_on_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070069}
70
Douglas Thompson079708b2007-07-19 01:49:58 -070071static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
72 *ctl_info, const char *data,
73 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070074{
75 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -070076 ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070077
78 return count;
79}
80
81/* 'poll_msec' show and store functions*/
Douglas Thompson079708b2007-07-19 01:49:58 -070082static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
83 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070084{
Douglas Thompson079708b2007-07-19 01:49:58 -070085 return sprintf(data, "%u\n", ctl_info->poll_msec);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070086}
87
Douglas Thompson079708b2007-07-19 01:49:58 -070088static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
89 *ctl_info, const char *data,
90 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070091{
92 unsigned long value;
93
94 /* get the value and enforce that it is non-zero, must be at least
95 * one millisecond for the delay period, between scans
96 * Then cancel last outstanding delay for the work request
97 * and set a new one.
98 */
Douglas Thompson079708b2007-07-19 01:49:58 -070099 value = simple_strtoul(data, NULL, 0);
100 edac_device_reset_delay_period(ctl_info, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700101
Douglas Thompson079708b2007-07-19 01:49:58 -0700102 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700103}
104
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700105/* edac_device_ctl_info specific attribute structure */
106struct ctl_info_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700107 struct attribute attr;
108 ssize_t(*show) (struct edac_device_ctl_info *, char *);
109 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700110};
111
112#define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
113#define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
114
115/* Function to 'show' fields from the edac_dev 'ctl_info' structure */
116static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700117 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700118{
Douglas Thompson079708b2007-07-19 01:49:58 -0700119 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
120 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700121
Douglas Thompson079708b2007-07-19 01:49:58 -0700122 if (ctl_info_attr->show)
123 return ctl_info_attr->show(edac_dev, buffer);
124 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700125}
126
127/* Function to 'store' fields into the edac_dev 'ctl_info' structure */
128static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700129 struct attribute *attr,
130 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700131{
Douglas Thompson079708b2007-07-19 01:49:58 -0700132 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
133 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700134
Douglas Thompson079708b2007-07-19 01:49:58 -0700135 if (ctl_info_attr->store)
136 return ctl_info_attr->store(edac_dev, buffer, count);
137 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700138}
139
140/* edac_dev file operations for an 'ctl_info' */
141static struct sysfs_ops device_ctl_info_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700142 .show = edac_dev_ctl_info_show,
143 .store = edac_dev_ctl_info_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700144};
145
146#define CTL_INFO_ATTR(_name,_mode,_show,_store) \
147static struct ctl_info_attribute attr_ctl_info_##_name = { \
148 .attr = {.name = __stringify(_name), .mode = _mode }, \
149 .show = _show, \
150 .store = _store, \
151};
152
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700153/* Declare the various ctl_info attributes here and their respective ops */
Douglas Thompson079708b2007-07-19 01:49:58 -0700154CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
155 edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
156CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
157 edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
158CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
159 edac_device_ctl_panic_on_ue_show,
160 edac_device_ctl_panic_on_ue_store);
161CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
162 edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700163
164/* Base Attributes of the EDAC_DEVICE ECC object */
165static struct ctl_info_attribute *device_ctrl_attr[] = {
166 &attr_ctl_info_panic_on_ue,
167 &attr_ctl_info_log_ue,
168 &attr_ctl_info_log_ce,
169 &attr_ctl_info_poll_msec,
170 NULL,
171};
172
173/* Main DEVICE kobject release() function */
174static void edac_device_ctrl_master_release(struct kobject *kobj)
175{
176 struct edac_device_ctl_info *edac_dev;
177
178 edac_dev = to_edacdev(kobj);
179
180 debugf1("%s()\n", __func__);
181 complete(&edac_dev->kobj_complete);
182}
183
184static struct kobj_type ktype_device_ctrl = {
185 .release = edac_device_ctrl_master_release,
186 .sysfs_ops = &device_ctl_info_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700187 .default_attrs = (struct attribute **)device_ctrl_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700188};
189
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700190/**************** edac_device main kobj ctor/dtor code *********************/
191
192/*
193 * edac_device_register_main_kobj
194 *
195 * perform the high level setup for the new edac_device instance
196 *
197 * Return: 0 SUCCESS
198 * !0 FAILURE
199 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700200static int edac_device_register_main_kobj(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700201{
202 int err = 0;
203 struct sysdev_class *edac_class;
204
205 debugf1("%s()\n", __func__);
206
Douglas Thompson079708b2007-07-19 01:49:58 -0700207 /* get the /sys/devices/system/edac reference */
208 edac_class = edac_get_edac_class();
209 if (edac_class == NULL) {
210 debugf1("%s() no edac_class error=%d\n", __func__, err);
211 return err;
212 }
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700213
214 /* Point to the 'edac_class' this instance 'reports' to */
215 edac_dev->edac_class = edac_class;
216
217 /* Init the devices's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700218 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700219 edac_dev->kobj.ktype = &ktype_device_ctrl;
220
221 /* set this new device under the edac_class kobject */
222 edac_dev->kobj.parent = &edac_class->kset.kobj;
223
224 /* generate sysfs "..../edac/<name>" */
Douglas Thompson079708b2007-07-19 01:49:58 -0700225 debugf1("%s() set name of kobject to: %s\n", __func__, edac_dev->name);
226 err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700227 if (err)
228 return err;
229 err = kobject_register(&edac_dev->kobj);
230 if (err) {
231 debugf1("%s()Failed to register '.../edac/%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700232 __func__, edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700233 return err;
234 }
235
236 debugf1("%s() Registered '.../edac/%s' kobject\n",
237 __func__, edac_dev->name);
238
239 return 0;
240}
241
242/*
243 * edac_device_unregister_main_kobj:
244 * the '..../edac/<name>' kobject
245 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700246static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
247 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700248{
249 debugf0("%s()\n", __func__);
250 debugf1("%s() name of kobject is: %s\n",
251 __func__, kobject_name(&edac_dev->kobj));
252
253 init_completion(&edac_dev->kobj_complete);
254
255 /*
256 * Unregister the edac device's kobject and
257 * wait for reference count to reach 0.
258 */
259 kobject_unregister(&edac_dev->kobj);
260 wait_for_completion(&edac_dev->kobj_complete);
261}
262
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700263/*************** edac_dev -> instance information ***********/
264
265/*
266 * Set of low-level instance attribute show functions
267 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700268static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
269 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700270{
Douglas Thompson079708b2007-07-19 01:49:58 -0700271 return sprintf(data, "%u\n", instance->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700272}
273
Douglas Thompson079708b2007-07-19 01:49:58 -0700274static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
275 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700276{
Douglas Thompson079708b2007-07-19 01:49:58 -0700277 return sprintf(data, "%u\n", instance->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700278}
279
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700280#define to_instance(k) container_of(k, struct edac_device_instance, kobj)
281#define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
282
283/* DEVICE instance kobject release() function */
284static void edac_device_ctrl_instance_release(struct kobject *kobj)
285{
286 struct edac_device_instance *instance;
287
288 debugf1("%s()\n", __func__);
289
290 instance = to_instance(kobj);
291 complete(&instance->kobj_complete);
292}
293
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700294/* instance specific attribute structure */
295struct instance_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700296 struct attribute attr;
297 ssize_t(*show) (struct edac_device_instance *, char *);
298 ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700299};
300
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700301/* Function to 'show' fields from the edac_dev 'instance' structure */
302static ssize_t edac_dev_instance_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700303 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700304{
Douglas Thompson079708b2007-07-19 01:49:58 -0700305 struct edac_device_instance *instance = to_instance(kobj);
306 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700307
Douglas Thompson079708b2007-07-19 01:49:58 -0700308 if (instance_attr->show)
309 return instance_attr->show(instance, buffer);
310 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700311}
312
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700313/* Function to 'store' fields into the edac_dev 'instance' structure */
314static ssize_t edac_dev_instance_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700315 struct attribute *attr,
316 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700317{
Douglas Thompson079708b2007-07-19 01:49:58 -0700318 struct edac_device_instance *instance = to_instance(kobj);
319 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700320
Douglas Thompson079708b2007-07-19 01:49:58 -0700321 if (instance_attr->store)
322 return instance_attr->store(instance, buffer, count);
323 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700324}
325
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700326/* edac_dev file operations for an 'instance' */
327static struct sysfs_ops device_instance_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700328 .show = edac_dev_instance_show,
329 .store = edac_dev_instance_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700330};
331
332#define INSTANCE_ATTR(_name,_mode,_show,_store) \
333static struct instance_attribute attr_instance_##_name = { \
334 .attr = {.name = __stringify(_name), .mode = _mode }, \
335 .show = _show, \
336 .store = _store, \
337};
338
339/*
340 * Define attributes visible for the edac_device instance object
341 * Each contains a pointer to a show and an optional set
342 * function pointer that does the low level output/input
343 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700344INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
345INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700346
347/* list of edac_dev 'instance' attributes */
348static struct instance_attribute *device_instance_attr[] = {
349 &attr_instance_ce_count,
350 &attr_instance_ue_count,
351 NULL,
352};
353
354/* The 'ktype' for each edac_dev 'instance' */
355static struct kobj_type ktype_instance_ctrl = {
356 .release = edac_device_ctrl_instance_release,
357 .sysfs_ops = &device_instance_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700358 .default_attrs = (struct attribute **)device_instance_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700359};
360
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700361/*************** edac_dev -> instance -> block information *********/
362
363/*
364 * Set of low-level block attribute show functions
365 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700366static ssize_t block_ue_count_show(struct edac_device_block *block, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700367{
Douglas Thompson079708b2007-07-19 01:49:58 -0700368 return sprintf(data, "%u\n", block->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700369}
370
Douglas Thompson079708b2007-07-19 01:49:58 -0700371static ssize_t block_ce_count_show(struct edac_device_block *block, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700372{
Douglas Thompson079708b2007-07-19 01:49:58 -0700373 return sprintf(data, "%u\n", block->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700374}
375
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700376#define to_block(k) container_of(k, struct edac_device_block, kobj)
377#define to_block_attr(a) container_of(a,struct block_attribute,attr)
378
379/* DEVICE block kobject release() function */
380static void edac_device_ctrl_block_release(struct kobject *kobj)
381{
382 struct edac_device_block *block;
383
384 debugf1("%s()\n", __func__);
385
386 block = to_block(kobj);
387 complete(&block->kobj_complete);
388}
389
390/* block specific attribute structure */
391struct block_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700392 struct attribute attr;
393 ssize_t(*show) (struct edac_device_block *, char *);
394 ssize_t(*store) (struct edac_device_block *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700395};
396
397/* Function to 'show' fields from the edac_dev 'block' structure */
398static ssize_t edac_dev_block_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700399 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700400{
Douglas Thompson079708b2007-07-19 01:49:58 -0700401 struct edac_device_block *block = to_block(kobj);
402 struct block_attribute *block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700403
Douglas Thompson079708b2007-07-19 01:49:58 -0700404 if (block_attr->show)
405 return block_attr->show(block, buffer);
406 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700407}
408
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700409/* Function to 'store' fields into the edac_dev 'block' structure */
410static ssize_t edac_dev_block_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700411 struct attribute *attr,
412 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700413{
Douglas Thompson079708b2007-07-19 01:49:58 -0700414 struct edac_device_block *block = to_block(kobj);
415 struct block_attribute *block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700416
Douglas Thompson079708b2007-07-19 01:49:58 -0700417 if (block_attr->store)
418 return block_attr->store(block, buffer, count);
419 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700420}
421
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700422/* edac_dev file operations for a 'block' */
423static struct sysfs_ops device_block_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700424 .show = edac_dev_block_show,
425 .store = edac_dev_block_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700426};
427
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700428#define BLOCK_ATTR(_name,_mode,_show,_store) \
429static struct block_attribute attr_block_##_name = { \
430 .attr = {.name = __stringify(_name), .mode = _mode }, \
431 .show = _show, \
432 .store = _store, \
433};
434
Douglas Thompson079708b2007-07-19 01:49:58 -0700435BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
436BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700437
438/* list of edac_dev 'block' attributes */
439static struct block_attribute *device_block_attr[] = {
440 &attr_block_ce_count,
441 &attr_block_ue_count,
442 NULL,
443};
444
445/* The 'ktype' for each edac_dev 'block' */
446static struct kobj_type ktype_block_ctrl = {
447 .release = edac_device_ctrl_block_release,
448 .sysfs_ops = &device_block_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700449 .default_attrs = (struct attribute **)device_block_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700450};
451
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700452/************** block ctor/dtor code ************/
453
454/*
455 * edac_device_create_block
456 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700457static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
458 struct edac_device_instance *instance,
459 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700460{
461 int err;
462 struct edac_device_block *block;
463
464 block = &instance->blocks[idx];
465
466 debugf1("%s() Instance '%s' block[%d] '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700467 __func__, instance->name, idx, block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700468
469 /* init this block's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700470 memset(&block->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700471 block->kobj.parent = &instance->kobj;
472 block->kobj.ktype = &ktype_block_ctrl;
473
Douglas Thompson079708b2007-07-19 01:49:58 -0700474 err = kobject_set_name(&block->kobj, "%s", block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700475 if (err)
476 return err;
477
478 err = kobject_register(&block->kobj);
479 if (err) {
480 debugf1("%s()Failed to register instance '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700481 __func__, block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700482 return err;
483 }
484
485 return 0;
486}
487
488/*
489 * edac_device_delete_block(edac_dev,j);
490 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700491static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
492 struct edac_device_instance *instance,
493 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700494{
495 struct edac_device_block *block;
496
497 block = &instance->blocks[idx];
498
499 /* unregister this block's kobject */
500 init_completion(&block->kobj_complete);
501 kobject_unregister(&block->kobj);
502 wait_for_completion(&block->kobj_complete);
503}
504
505/************** instance ctor/dtor code ************/
506
507/*
508 * edac_device_create_instance
509 * create just one instance of an edac_device 'instance'
510 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700511static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
512 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700513{
514 int i, j;
515 int err;
516 struct edac_device_instance *instance;
517
518 instance = &edac_dev->instances[idx];
519
520 /* Init the instance's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700521 memset(&instance->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700522
523 /* set this new device under the edac_device main kobject */
524 instance->kobj.parent = &edac_dev->kobj;
525 instance->kobj.ktype = &ktype_instance_ctrl;
526
Douglas Thompson079708b2007-07-19 01:49:58 -0700527 err = kobject_set_name(&instance->kobj, "%s", instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700528 if (err)
529 return err;
530
531 err = kobject_register(&instance->kobj);
532 if (err != 0) {
533 debugf2("%s() Failed to register instance '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700534 __func__, instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700535 return err;
536 }
537
538 debugf1("%s() now register '%d' blocks for instance %d\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700539 __func__, instance->nr_blocks, idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700540
541 /* register all blocks of this instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700542 for (i = 0; i < instance->nr_blocks; i++) {
543 err = edac_device_create_block(edac_dev, instance, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700544 if (err) {
545 for (j = 0; j < i; j++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700546 edac_device_delete_block(edac_dev, instance, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700547 }
548 return err;
549 }
550 }
551
552 debugf1("%s() Registered instance %d '%s' kobject\n",
553 __func__, idx, instance->name);
554
555 return 0;
556}
557
558/*
559 * edac_device_remove_instance
560 * remove an edac_device instance
561 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700562static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
563 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700564{
565 int i;
566 struct edac_device_instance *instance;
567
568 instance = &edac_dev->instances[idx];
569
570 /* unregister all blocks in this instance */
571 for (i = 0; i < instance->nr_blocks; i++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700572 edac_device_delete_block(edac_dev, instance, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700573 }
574
575 /* unregister this instance's kobject */
576 init_completion(&instance->kobj_complete);
577 kobject_unregister(&instance->kobj);
578 wait_for_completion(&instance->kobj_complete);
579}
580
581/*
582 * edac_device_create_instances
583 * create the first level of 'instances' for this device
584 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
585 */
586static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
587{
588 int i, j;
589 int err;
590
591 debugf0("%s()\n", __func__);
592
593 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700594 for (i = 0; i < edac_dev->nr_instances; i++) {
595 err = edac_device_create_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700596 if (err) {
597 /* unwind previous instances on error */
598 for (j = 0; j < i; j++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700599 edac_device_delete_instance(edac_dev, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700600 }
601 return err;
602 }
603 }
604
605 return 0;
606}
607
608/*
609 * edac_device_delete_instances(edac_dev);
610 * unregister all the kobjects of the instances
611 */
612static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
613{
614 int i;
615
616 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700617 for (i = 0; i < edac_dev->nr_instances; i++) {
618 edac_device_delete_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700619 }
620}
621
622/******************* edac_dev sysfs ctor/dtor code *************/
623
624/*
625 * edac_device_create_sysfs() Constructor
626 *
627 * Create a new edac_device kobject instance,
628 *
629 * Return:
630 * 0 Success
631 * !0 Failure
632 */
633int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
634{
635 int err;
Douglas Thompson079708b2007-07-19 01:49:58 -0700636 struct kobject *edac_kobj = &edac_dev->kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700637
638 /* register this instance's main kobj with the edac class kobj */
639 err = edac_device_register_main_kobj(edac_dev);
640 if (err)
641 return err;
642
643 debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
644
645 /* create a symlink from the edac device
646 * to the platform 'device' being used for this
647 */
648 err = sysfs_create_link(edac_kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700649 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700650 if (err) {
651 debugf0("%s() sysfs_create_link() returned err= %d\n",
652 __func__, err);
653 return err;
654 }
655
656 debugf0("%s() calling create-instances, idx=%d\n",
657 __func__, edac_dev->dev_idx);
658
659 /* Create the first level instance directories */
660 err = edac_device_create_instances(edac_dev);
661 if (err) {
662 goto error0;
663 }
664
665 return 0;
666
667 /* Error unwind stack */
668
Douglas Thompson079708b2007-07-19 01:49:58 -0700669 error0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700670 edac_device_unregister_main_kobj(edac_dev);
671
672 return err;
673}
674
675/*
676 * edac_device_remove_sysfs() destructor
677 *
678 * remove a edac_device instance
679 */
680void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
681{
682 debugf0("%s()\n", __func__);
683
684 edac_device_delete_instances(edac_dev);
685
686 /* remove the sym link */
687 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
688
689 /* unregister the instance's main kobj */
690 edac_device_unregister_main_kobj(edac_dev);
691}