blob: 69305e6378100d6b2e88674e6d9561eedeef5ba8 [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
24#ifdef DKT
25
26static ssize_t edac_dev_ue_count_show(struct edac_device_ctl_info *edac_dev,
Douglas Thompson079708b2007-07-19 01:49:58 -070027 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070028{
Douglas Thompson079708b2007-07-19 01:49:58 -070029 return sprintf(data, "%d\n", edac_dev->ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070030}
31
32static ssize_t edac_dev_ce_count_show(struct edac_device_ctl_info *edac_dev,
Douglas Thompson079708b2007-07-19 01:49:58 -070033 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070034{
Douglas Thompson079708b2007-07-19 01:49:58 -070035 return sprintf(data, "%d\n", edac_dev->ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070036}
37
38static ssize_t edac_dev_seconds_show(struct edac_device_ctl_info *edac_dev,
Douglas Thompson079708b2007-07-19 01:49:58 -070039 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070040{
Douglas Thompson079708b2007-07-19 01:49:58 -070041 return sprintf(data, "%ld\n", (jiffies - edac_dev->start_time) / HZ);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070042}
43
44static ssize_t edac_dev_ctl_name_show(struct edac_device_ctl_info *edac_dev,
Douglas Thompson079708b2007-07-19 01:49:58 -070045 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070046{
Douglas Thompson079708b2007-07-19 01:49:58 -070047 return sprintf(data, "%s\n", edac_dev->ctl_name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070048}
49
Douglas Thompsone27e3da2007-07-19 01:49:36 -070050struct edacdev_attribute {
51 struct attribute attr;
Douglas Thompson079708b2007-07-19 01:49:58 -070052 ssize_t(*show) (struct edac_device_ctl_info *, char *);
53 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070054};
55
Douglas Thompsone27e3da2007-07-19 01:49:36 -070056/* EDAC DEVICE show/store functions for top most object */
57static ssize_t edacdev_show(struct kobject *kobj, struct attribute *attr,
Douglas Thompson079708b2007-07-19 01:49:58 -070058 char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070059{
60 struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
Douglas Thompson079708b2007-07-19 01:49:58 -070061 struct edacdev_attribute *edacdev_attr = to_edacdev_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070062
63 if (edacdev_attr->show)
64 return edacdev_attr->show(edac_dev, buffer);
65
66 return -EIO;
67}
68
69static ssize_t edacdev_store(struct kobject *kobj, struct attribute *attr,
Douglas Thompson079708b2007-07-19 01:49:58 -070070 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070071{
72 struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
Douglas Thompson079708b2007-07-19 01:49:58 -070073 struct edacdev_attribute *edacdev_attr = to_edacdev_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070074
75 if (edacdev_attr->store)
76 return edacdev_attr->store(edac_dev, buffer, count);
77
78 return -EIO;
79}
80
81static struct sysfs_ops edac_dev_ops = {
82 .show = edacdev_show,
83 .store = edacdev_store
84};
85
86#define EDACDEV_ATTR(_name,_mode,_show,_store) \
87static struct edacdev_attribute edac_dev_attr_##_name = { \
88 .attr = {.name = __stringify(_name), .mode = _mode }, \
89 .show = _show, \
90 .store = _store, \
91};
92
93/* default Control file */
Douglas Thompson079708b2007-07-19 01:49:58 -070094EDACDEV_ATTR(reset_counters, S_IWUSR, NULL, edac_dev_reset_counters_store);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070095
96/* default Attribute files */
Douglas Thompson079708b2007-07-19 01:49:58 -070097EDACDEV_ATTR(mc_name, S_IRUGO, edac_dev_ctl_name_show, NULL);
98EDACDEV_ATTR(seconds_since_reset, S_IRUGO, edac_dev_seconds_show, NULL);
99EDACDEV_ATTR(ue_count, S_IRUGO, edac_dev_ue_count_show, NULL);
100EDACDEV_ATTR(ce_count, S_IRUGO, edac_dev_ce_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700101
102static struct edacdev_attribute *edacdev_attr[] = {
103 &edacdev_attr_reset_counters,
104 &edacdev_attr_mc_name,
105 &edacdev_attr_seconds_since_reset,
106 &edacdev_attr_ue_count,
107 &edacdev_attr_ce_count,
108 NULL
109};
110
111/*
112 * Release of a Edac Device controlling instance
113 */
114static void edac_dev_instance_release(struct kobject *kobj)
115{
116 struct edac_device_ctl_info *edac_dev;
117
118 edac_dev = to_edacdev(kobj);
119 debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
120 complete(&edac_dev->kobj_complete);
121}
122
123static struct kobj_type ktype_device = {
124 .release = edac_dev_instance_release,
125 .sysfs_ops = &edacdev_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700126 .default_attrs = (struct attribute **)edacdev_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700127};
128
129#endif
130
131/************************** edac_device sysfs code and data **************/
132
133/*
134 * Set of edac_device_ctl_info attribute store/show functions
135 */
136
137/* 'log_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -0700138static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
139 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700140{
Douglas Thompson079708b2007-07-19 01:49:58 -0700141 return sprintf(data, "%u\n", ctl_info->log_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700142}
143
Douglas Thompson079708b2007-07-19 01:49:58 -0700144static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
145 *ctl_info, const char *data,
146 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700147{
148 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -0700149 ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700150
Douglas Thompson079708b2007-07-19 01:49:58 -0700151 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700152}
153
154/* 'log_ce' */
Douglas Thompson079708b2007-07-19 01:49:58 -0700155static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
156 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700157{
Douglas Thompson079708b2007-07-19 01:49:58 -0700158 return sprintf(data, "%u\n", ctl_info->log_ce);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700159}
160
Douglas Thompson079708b2007-07-19 01:49:58 -0700161static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
162 *ctl_info, const char *data,
163 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700164{
165 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -0700166 ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700167
Douglas Thompson079708b2007-07-19 01:49:58 -0700168 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700169}
170
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700171/* 'panic_on_ue' */
Douglas Thompson079708b2007-07-19 01:49:58 -0700172static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
173 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700174{
Douglas Thompson079708b2007-07-19 01:49:58 -0700175 return sprintf(data, "%u\n", ctl_info->panic_on_ue);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700176}
177
Douglas Thompson079708b2007-07-19 01:49:58 -0700178static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
179 *ctl_info, const char *data,
180 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700181{
182 /* if parameter is zero, turn off flag, if non-zero turn on flag */
Douglas Thompson079708b2007-07-19 01:49:58 -0700183 ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700184
185 return count;
186}
187
188/* 'poll_msec' show and store functions*/
Douglas Thompson079708b2007-07-19 01:49:58 -0700189static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
190 *ctl_info, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700191{
Douglas Thompson079708b2007-07-19 01:49:58 -0700192 return sprintf(data, "%u\n", ctl_info->poll_msec);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700193}
194
Douglas Thompson079708b2007-07-19 01:49:58 -0700195static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
196 *ctl_info, const char *data,
197 size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700198{
199 unsigned long value;
200
201 /* get the value and enforce that it is non-zero, must be at least
202 * one millisecond for the delay period, between scans
203 * Then cancel last outstanding delay for the work request
204 * and set a new one.
205 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700206 value = simple_strtoul(data, NULL, 0);
207 edac_device_reset_delay_period(ctl_info, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700208
Douglas Thompson079708b2007-07-19 01:49:58 -0700209 return count;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700210}
211
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700212/* edac_device_ctl_info specific attribute structure */
213struct ctl_info_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700214 struct attribute attr;
215 ssize_t(*show) (struct edac_device_ctl_info *, char *);
216 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700217};
218
219#define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
220#define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
221
222/* Function to 'show' fields from the edac_dev 'ctl_info' structure */
223static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700224 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700225{
Douglas Thompson079708b2007-07-19 01:49:58 -0700226 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
227 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700228
Douglas Thompson079708b2007-07-19 01:49:58 -0700229 if (ctl_info_attr->show)
230 return ctl_info_attr->show(edac_dev, buffer);
231 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700232}
233
234/* Function to 'store' fields into the edac_dev 'ctl_info' structure */
235static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700236 struct attribute *attr,
237 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700238{
Douglas Thompson079708b2007-07-19 01:49:58 -0700239 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
240 struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700241
Douglas Thompson079708b2007-07-19 01:49:58 -0700242 if (ctl_info_attr->store)
243 return ctl_info_attr->store(edac_dev, buffer, count);
244 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700245}
246
247/* edac_dev file operations for an 'ctl_info' */
248static struct sysfs_ops device_ctl_info_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700249 .show = edac_dev_ctl_info_show,
250 .store = edac_dev_ctl_info_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700251};
252
253#define CTL_INFO_ATTR(_name,_mode,_show,_store) \
254static struct ctl_info_attribute attr_ctl_info_##_name = { \
255 .attr = {.name = __stringify(_name), .mode = _mode }, \
256 .show = _show, \
257 .store = _store, \
258};
259
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700260/* Declare the various ctl_info attributes here and their respective ops */
Douglas Thompson079708b2007-07-19 01:49:58 -0700261CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
262 edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
263CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
264 edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
265CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
266 edac_device_ctl_panic_on_ue_show,
267 edac_device_ctl_panic_on_ue_store);
268CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
269 edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700270
271/* Base Attributes of the EDAC_DEVICE ECC object */
272static struct ctl_info_attribute *device_ctrl_attr[] = {
273 &attr_ctl_info_panic_on_ue,
274 &attr_ctl_info_log_ue,
275 &attr_ctl_info_log_ce,
276 &attr_ctl_info_poll_msec,
277 NULL,
278};
279
280/* Main DEVICE kobject release() function */
281static void edac_device_ctrl_master_release(struct kobject *kobj)
282{
283 struct edac_device_ctl_info *edac_dev;
284
285 edac_dev = to_edacdev(kobj);
286
287 debugf1("%s()\n", __func__);
288 complete(&edac_dev->kobj_complete);
289}
290
291static struct kobj_type ktype_device_ctrl = {
292 .release = edac_device_ctrl_master_release,
293 .sysfs_ops = &device_ctl_info_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700294 .default_attrs = (struct attribute **)device_ctrl_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700295};
296
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700297/**************** edac_device main kobj ctor/dtor code *********************/
298
299/*
300 * edac_device_register_main_kobj
301 *
302 * perform the high level setup for the new edac_device instance
303 *
304 * Return: 0 SUCCESS
305 * !0 FAILURE
306 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700307static int edac_device_register_main_kobj(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700308{
309 int err = 0;
310 struct sysdev_class *edac_class;
311
312 debugf1("%s()\n", __func__);
313
Douglas Thompson079708b2007-07-19 01:49:58 -0700314 /* get the /sys/devices/system/edac reference */
315 edac_class = edac_get_edac_class();
316 if (edac_class == NULL) {
317 debugf1("%s() no edac_class error=%d\n", __func__, err);
318 return err;
319 }
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700320
321 /* Point to the 'edac_class' this instance 'reports' to */
322 edac_dev->edac_class = edac_class;
323
324 /* Init the devices's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700325 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700326 edac_dev->kobj.ktype = &ktype_device_ctrl;
327
328 /* set this new device under the edac_class kobject */
329 edac_dev->kobj.parent = &edac_class->kset.kobj;
330
331 /* generate sysfs "..../edac/<name>" */
Douglas Thompson079708b2007-07-19 01:49:58 -0700332 debugf1("%s() set name of kobject to: %s\n", __func__, edac_dev->name);
333 err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700334 if (err)
335 return err;
336 err = kobject_register(&edac_dev->kobj);
337 if (err) {
338 debugf1("%s()Failed to register '.../edac/%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700339 __func__, edac_dev->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700340 return err;
341 }
342
343 debugf1("%s() Registered '.../edac/%s' kobject\n",
344 __func__, edac_dev->name);
345
346 return 0;
347}
348
349/*
350 * edac_device_unregister_main_kobj:
351 * the '..../edac/<name>' kobject
352 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700353static void edac_device_unregister_main_kobj(struct edac_device_ctl_info
354 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700355{
356 debugf0("%s()\n", __func__);
357 debugf1("%s() name of kobject is: %s\n",
358 __func__, kobject_name(&edac_dev->kobj));
359
360 init_completion(&edac_dev->kobj_complete);
361
362 /*
363 * Unregister the edac device's kobject and
364 * wait for reference count to reach 0.
365 */
366 kobject_unregister(&edac_dev->kobj);
367 wait_for_completion(&edac_dev->kobj_complete);
368}
369
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700370/*************** edac_dev -> instance information ***********/
371
372/*
373 * Set of low-level instance attribute show functions
374 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700375static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
376 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700377{
Douglas Thompson079708b2007-07-19 01:49:58 -0700378 return sprintf(data, "%u\n", instance->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700379}
380
Douglas Thompson079708b2007-07-19 01:49:58 -0700381static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
382 char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700383{
Douglas Thompson079708b2007-07-19 01:49:58 -0700384 return sprintf(data, "%u\n", instance->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700385}
386
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700387#define to_instance(k) container_of(k, struct edac_device_instance, kobj)
388#define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
389
390/* DEVICE instance kobject release() function */
391static void edac_device_ctrl_instance_release(struct kobject *kobj)
392{
393 struct edac_device_instance *instance;
394
395 debugf1("%s()\n", __func__);
396
397 instance = to_instance(kobj);
398 complete(&instance->kobj_complete);
399}
400
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700401/* instance specific attribute structure */
402struct instance_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700403 struct attribute attr;
404 ssize_t(*show) (struct edac_device_instance *, char *);
405 ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700406};
407
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700408/* Function to 'show' fields from the edac_dev 'instance' structure */
409static ssize_t edac_dev_instance_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700410 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700411{
Douglas Thompson079708b2007-07-19 01:49:58 -0700412 struct edac_device_instance *instance = to_instance(kobj);
413 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700414
Douglas Thompson079708b2007-07-19 01:49:58 -0700415 if (instance_attr->show)
416 return instance_attr->show(instance, buffer);
417 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700418}
419
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700420/* Function to 'store' fields into the edac_dev 'instance' structure */
421static ssize_t edac_dev_instance_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700422 struct attribute *attr,
423 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700424{
Douglas Thompson079708b2007-07-19 01:49:58 -0700425 struct edac_device_instance *instance = to_instance(kobj);
426 struct instance_attribute *instance_attr = to_instance_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700427
Douglas Thompson079708b2007-07-19 01:49:58 -0700428 if (instance_attr->store)
429 return instance_attr->store(instance, buffer, count);
430 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700431}
432
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700433/* edac_dev file operations for an 'instance' */
434static struct sysfs_ops device_instance_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700435 .show = edac_dev_instance_show,
436 .store = edac_dev_instance_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700437};
438
439#define INSTANCE_ATTR(_name,_mode,_show,_store) \
440static struct instance_attribute attr_instance_##_name = { \
441 .attr = {.name = __stringify(_name), .mode = _mode }, \
442 .show = _show, \
443 .store = _store, \
444};
445
446/*
447 * Define attributes visible for the edac_device instance object
448 * Each contains a pointer to a show and an optional set
449 * function pointer that does the low level output/input
450 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700451INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
452INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700453
454/* list of edac_dev 'instance' attributes */
455static struct instance_attribute *device_instance_attr[] = {
456 &attr_instance_ce_count,
457 &attr_instance_ue_count,
458 NULL,
459};
460
461/* The 'ktype' for each edac_dev 'instance' */
462static struct kobj_type ktype_instance_ctrl = {
463 .release = edac_device_ctrl_instance_release,
464 .sysfs_ops = &device_instance_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700465 .default_attrs = (struct attribute **)device_instance_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700466};
467
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700468/*************** edac_dev -> instance -> block information *********/
469
470/*
471 * Set of low-level block attribute show functions
472 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700473static ssize_t block_ue_count_show(struct edac_device_block *block, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700474{
Douglas Thompson079708b2007-07-19 01:49:58 -0700475 return sprintf(data, "%u\n", block->counters.ue_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700476}
477
Douglas Thompson079708b2007-07-19 01:49:58 -0700478static ssize_t block_ce_count_show(struct edac_device_block *block, char *data)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700479{
Douglas Thompson079708b2007-07-19 01:49:58 -0700480 return sprintf(data, "%u\n", block->counters.ce_count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700481}
482
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700483#define to_block(k) container_of(k, struct edac_device_block, kobj)
484#define to_block_attr(a) container_of(a,struct block_attribute,attr)
485
486/* DEVICE block kobject release() function */
487static void edac_device_ctrl_block_release(struct kobject *kobj)
488{
489 struct edac_device_block *block;
490
491 debugf1("%s()\n", __func__);
492
493 block = to_block(kobj);
494 complete(&block->kobj_complete);
495}
496
497/* block specific attribute structure */
498struct block_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -0700499 struct attribute attr;
500 ssize_t(*show) (struct edac_device_block *, char *);
501 ssize_t(*store) (struct edac_device_block *, const char *, size_t);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700502};
503
504/* Function to 'show' fields from the edac_dev 'block' structure */
505static ssize_t edac_dev_block_show(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700506 struct attribute *attr, char *buffer)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700507{
Douglas Thompson079708b2007-07-19 01:49:58 -0700508 struct edac_device_block *block = to_block(kobj);
509 struct block_attribute *block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700510
Douglas Thompson079708b2007-07-19 01:49:58 -0700511 if (block_attr->show)
512 return block_attr->show(block, buffer);
513 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700514}
515
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700516/* Function to 'store' fields into the edac_dev 'block' structure */
517static ssize_t edac_dev_block_store(struct kobject *kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700518 struct attribute *attr,
519 const char *buffer, size_t count)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700520{
Douglas Thompson079708b2007-07-19 01:49:58 -0700521 struct edac_device_block *block = to_block(kobj);
522 struct block_attribute *block_attr = to_block_attr(attr);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700523
Douglas Thompson079708b2007-07-19 01:49:58 -0700524 if (block_attr->store)
525 return block_attr->store(block, buffer, count);
526 return -EIO;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700527}
528
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700529/* edac_dev file operations for a 'block' */
530static struct sysfs_ops device_block_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700531 .show = edac_dev_block_show,
532 .store = edac_dev_block_store
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700533};
534
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700535#define BLOCK_ATTR(_name,_mode,_show,_store) \
536static struct block_attribute attr_block_##_name = { \
537 .attr = {.name = __stringify(_name), .mode = _mode }, \
538 .show = _show, \
539 .store = _store, \
540};
541
Douglas Thompson079708b2007-07-19 01:49:58 -0700542BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
543BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700544
545/* list of edac_dev 'block' attributes */
546static struct block_attribute *device_block_attr[] = {
547 &attr_block_ce_count,
548 &attr_block_ue_count,
549 NULL,
550};
551
552/* The 'ktype' for each edac_dev 'block' */
553static struct kobj_type ktype_block_ctrl = {
554 .release = edac_device_ctrl_block_release,
555 .sysfs_ops = &device_block_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700556 .default_attrs = (struct attribute **)device_block_attr,
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700557};
558
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700559/************** block ctor/dtor code ************/
560
561/*
562 * edac_device_create_block
563 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700564static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
565 struct edac_device_instance *instance,
566 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700567{
568 int err;
569 struct edac_device_block *block;
570
571 block = &instance->blocks[idx];
572
573 debugf1("%s() Instance '%s' block[%d] '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700574 __func__, instance->name, idx, block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700575
576 /* init this block's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700577 memset(&block->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700578 block->kobj.parent = &instance->kobj;
579 block->kobj.ktype = &ktype_block_ctrl;
580
Douglas Thompson079708b2007-07-19 01:49:58 -0700581 err = kobject_set_name(&block->kobj, "%s", block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700582 if (err)
583 return err;
584
585 err = kobject_register(&block->kobj);
586 if (err) {
587 debugf1("%s()Failed to register instance '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700588 __func__, block->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700589 return err;
590 }
591
592 return 0;
593}
594
595/*
596 * edac_device_delete_block(edac_dev,j);
597 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700598static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
599 struct edac_device_instance *instance,
600 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700601{
602 struct edac_device_block *block;
603
604 block = &instance->blocks[idx];
605
606 /* unregister this block's kobject */
607 init_completion(&block->kobj_complete);
608 kobject_unregister(&block->kobj);
609 wait_for_completion(&block->kobj_complete);
610}
611
612/************** instance ctor/dtor code ************/
613
614/*
615 * edac_device_create_instance
616 * create just one instance of an edac_device 'instance'
617 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700618static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
619 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700620{
621 int i, j;
622 int err;
623 struct edac_device_instance *instance;
624
625 instance = &edac_dev->instances[idx];
626
627 /* Init the instance's kobject */
Douglas Thompson079708b2007-07-19 01:49:58 -0700628 memset(&instance->kobj, 0, sizeof(struct kobject));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700629
630 /* set this new device under the edac_device main kobject */
631 instance->kobj.parent = &edac_dev->kobj;
632 instance->kobj.ktype = &ktype_instance_ctrl;
633
Douglas Thompson079708b2007-07-19 01:49:58 -0700634 err = kobject_set_name(&instance->kobj, "%s", instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700635 if (err)
636 return err;
637
638 err = kobject_register(&instance->kobj);
639 if (err != 0) {
640 debugf2("%s() Failed to register instance '%s'\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700641 __func__, instance->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700642 return err;
643 }
644
645 debugf1("%s() now register '%d' blocks for instance %d\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700646 __func__, instance->nr_blocks, idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700647
648 /* register all blocks of this instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700649 for (i = 0; i < instance->nr_blocks; i++) {
650 err = edac_device_create_block(edac_dev, instance, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700651 if (err) {
652 for (j = 0; j < i; j++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700653 edac_device_delete_block(edac_dev, instance, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700654 }
655 return err;
656 }
657 }
658
659 debugf1("%s() Registered instance %d '%s' kobject\n",
660 __func__, idx, instance->name);
661
662 return 0;
663}
664
665/*
666 * edac_device_remove_instance
667 * remove an edac_device instance
668 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700669static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
670 int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700671{
672 int i;
673 struct edac_device_instance *instance;
674
675 instance = &edac_dev->instances[idx];
676
677 /* unregister all blocks in this instance */
678 for (i = 0; i < instance->nr_blocks; i++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700679 edac_device_delete_block(edac_dev, instance, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700680 }
681
682 /* unregister this instance's kobject */
683 init_completion(&instance->kobj_complete);
684 kobject_unregister(&instance->kobj);
685 wait_for_completion(&instance->kobj_complete);
686}
687
688/*
689 * edac_device_create_instances
690 * create the first level of 'instances' for this device
691 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
692 */
693static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
694{
695 int i, j;
696 int err;
697
698 debugf0("%s()\n", __func__);
699
700 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700701 for (i = 0; i < edac_dev->nr_instances; i++) {
702 err = edac_device_create_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700703 if (err) {
704 /* unwind previous instances on error */
705 for (j = 0; j < i; j++) {
Douglas Thompson079708b2007-07-19 01:49:58 -0700706 edac_device_delete_instance(edac_dev, j);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700707 }
708 return err;
709 }
710 }
711
712 return 0;
713}
714
715/*
716 * edac_device_delete_instances(edac_dev);
717 * unregister all the kobjects of the instances
718 */
719static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
720{
721 int i;
722
723 /* iterate over creation of the instances */
Douglas Thompson079708b2007-07-19 01:49:58 -0700724 for (i = 0; i < edac_dev->nr_instances; i++) {
725 edac_device_delete_instance(edac_dev, i);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700726 }
727}
728
729/******************* edac_dev sysfs ctor/dtor code *************/
730
731/*
732 * edac_device_create_sysfs() Constructor
733 *
734 * Create a new edac_device kobject instance,
735 *
736 * Return:
737 * 0 Success
738 * !0 Failure
739 */
740int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
741{
742 int err;
Douglas Thompson079708b2007-07-19 01:49:58 -0700743 struct kobject *edac_kobj = &edac_dev->kobj;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700744
745 /* register this instance's main kobj with the edac class kobj */
746 err = edac_device_register_main_kobj(edac_dev);
747 if (err)
748 return err;
749
750 debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
751
752 /* create a symlink from the edac device
753 * to the platform 'device' being used for this
754 */
755 err = sysfs_create_link(edac_kobj,
Douglas Thompson079708b2007-07-19 01:49:58 -0700756 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700757 if (err) {
758 debugf0("%s() sysfs_create_link() returned err= %d\n",
759 __func__, err);
760 return err;
761 }
762
763 debugf0("%s() calling create-instances, idx=%d\n",
764 __func__, edac_dev->dev_idx);
765
766 /* Create the first level instance directories */
767 err = edac_device_create_instances(edac_dev);
768 if (err) {
769 goto error0;
770 }
771
772 return 0;
773
774 /* Error unwind stack */
775
Douglas Thompson079708b2007-07-19 01:49:58 -0700776 error0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700777 edac_device_unregister_main_kobj(edac_dev);
778
779 return err;
780}
781
782/*
783 * edac_device_remove_sysfs() destructor
784 *
785 * remove a edac_device instance
786 */
787void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
788{
789 debugf0("%s()\n", __func__);
790
791 edac_device_delete_instances(edac_dev);
792
793 /* remove the sym link */
794 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
795
796 /* unregister the instance's main kobj */
797 edac_device_unregister_main_kobj(edac_dev);
798}