1 /*
2 * file for managing the edac_device subsystem of devices for EDAC
3 *
4 * (C) 2007 SoftwareBitMaker
5 *
6 * This file may be distributed under the terms of the
7 * GNU General Public License.
8 *
9 * Written Doug Thompson <norsk5@xmission.com>
10 *
11 */
12
13 #include <linux/ctype.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/edac.h>
17
18 #include "edac_device.h"
19 #include "edac_module.h"
20
21 #define EDAC_DEVICE_SYMLINK "device"
22
23 #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
24
25 /*
26 * Set of edac_device_ctl_info attribute store/show functions
27 */
28
29 /* 'log_ue' */
edac_device_ctl_log_ue_show(struct edac_device_ctl_info * ctl_info,char * data)30 static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
31 *ctl_info, char *data)
32 {
33 return sprintf(data, "%u\n", ctl_info->log_ue);
34 }
35
edac_device_ctl_log_ue_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)36 static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
37 *ctl_info, const char *data,
38 size_t count)
39 {
40 /* if parameter is zero, turn off flag, if non-zero turn on flag */
41 ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
42
43 return count;
44 }
45
46 /* 'log_ce' */
edac_device_ctl_log_ce_show(struct edac_device_ctl_info * ctl_info,char * data)47 static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
48 *ctl_info, char *data)
49 {
50 return sprintf(data, "%u\n", ctl_info->log_ce);
51 }
52
edac_device_ctl_log_ce_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)53 static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
54 *ctl_info, const char *data,
55 size_t count)
56 {
57 /* if parameter is zero, turn off flag, if non-zero turn on flag */
58 ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
59
60 return count;
61 }
62
63 /* 'panic_on_ue' */
edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info * ctl_info,char * data)64 static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
65 *ctl_info, char *data)
66 {
67 return sprintf(data, "%u\n", ctl_info->panic_on_ue);
68 }
69
edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)70 static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
71 *ctl_info, const char *data,
72 size_t count)
73 {
74 /* if parameter is zero, turn off flag, if non-zero turn on flag */
75 ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
76
77 return count;
78 }
79
80 /* 'poll_msec' show and store functions*/
edac_device_ctl_poll_msec_show(struct edac_device_ctl_info * ctl_info,char * data)81 static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
82 *ctl_info, char *data)
83 {
84 return sprintf(data, "%u\n", ctl_info->poll_msec);
85 }
86
edac_device_ctl_poll_msec_store(struct edac_device_ctl_info * ctl_info,const char * data,size_t count)87 static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info *ctl_info,
88 const char *data, size_t count)
89 {
90 unsigned int value;
91 int ret;
92
93 /*
94 * Get the value, make sure it is non-zero, must be at least one millisecond
95 * for the delay period between scans.
96 */
97 ret = kstrtouint(data, 0, &value);
98 if (ret < 0)
99 return ret;
100
101 if (value < 1)
102 return -EINVAL;
103
104 edac_device_reset_delay_period(ctl_info, value);
105
106 return count;
107 }
108
109 /* edac_device_ctl_info specific attribute structure */
110 struct ctl_info_attribute {
111 struct attribute attr;
112 ssize_t(*show) (struct edac_device_ctl_info *, char *);
113 ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
114 };
115
116 #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
117 #define to_ctl_info_attr(a) container_of_const(a, struct ctl_info_attribute, attr)
118
119 /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
edac_dev_ctl_info_show(struct kobject * kobj,struct attribute * attr,char * buffer)120 static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
121 struct attribute *attr, char *buffer)
122 {
123 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
124 const struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
125
126 if (ctl_info_attr->show)
127 return ctl_info_attr->show(edac_dev, buffer);
128 return -EIO;
129 }
130
131 /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
edac_dev_ctl_info_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t count)132 static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
133 struct attribute *attr,
134 const char *buffer, size_t count)
135 {
136 struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
137 const struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
138
139 if (ctl_info_attr->store)
140 return ctl_info_attr->store(edac_dev, buffer, count);
141 return -EIO;
142 }
143
144 /* edac_dev file operations for an 'ctl_info' */
145 static const struct sysfs_ops device_ctl_info_ops = {
146 .show = edac_dev_ctl_info_show,
147 .store = edac_dev_ctl_info_store
148 };
149
150 #define CTL_INFO_ATTR(_name,_mode,_show,_store) \
151 static const struct ctl_info_attribute attr_ctl_info_##_name = { \
152 .attr = {.name = __stringify(_name), .mode = _mode }, \
153 .show = _show, \
154 .store = _store, \
155 };
156
157 /* Declare the various ctl_info attributes here and their respective ops */
158 CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
159 edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
160 CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
161 edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
162 CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
163 edac_device_ctl_panic_on_ue_show,
164 edac_device_ctl_panic_on_ue_store);
165 CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
166 edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
167
168 /* Base Attributes of the EDAC_DEVICE ECC object */
169 static const struct attribute *const device_ctrl_attrs[] = {
170 &attr_ctl_info_panic_on_ue.attr,
171 &attr_ctl_info_log_ue.attr,
172 &attr_ctl_info_log_ce.attr,
173 &attr_ctl_info_poll_msec.attr,
174 NULL,
175 };
176 ATTRIBUTE_GROUPS(device_ctrl);
177
178 /*
179 * edac_device_ctrl_master_release
180 *
181 * called when the reference count for the 'main' kobj
182 * for a edac_device control struct reaches zero
183 *
184 * Reference count model:
185 * One 'main' kobject for each control structure allocated.
186 * That main kobj is initially set to one AND
187 * the reference count for the EDAC 'core' module is
188 * bumped by one, thus added 'keep in memory' dependency.
189 *
190 * Each new internal kobj (in instances and blocks) then
191 * bumps the 'main' kobject.
192 *
193 * When they are released their release functions decrement
194 * the 'main' kobj.
195 *
196 * When the main kobj reaches zero (0) then THIS function
197 * is called which then decrements the EDAC 'core' module.
198 * When the module reference count reaches zero then the
199 * module no longer has dependency on keeping the release
200 * function code in memory and module can be unloaded.
201 *
202 * This will support several control objects as well, each
203 * with its own 'main' kobj.
204 */
edac_device_ctrl_master_release(struct kobject * kobj)205 static void edac_device_ctrl_master_release(struct kobject *kobj)
206 {
207 struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
208
209 edac_dbg(4, "control index=%d\n", edac_dev->dev_idx);
210
211 /* decrement the EDAC CORE module ref count */
212 module_put(edac_dev->owner);
213
214 __edac_device_free_ctl_info(edac_dev);
215 }
216
217 /* ktype for the main (master) kobject */
218 static struct kobj_type ktype_device_ctrl = {
219 .release = edac_device_ctrl_master_release,
220 .sysfs_ops = &device_ctl_info_ops,
221 .default_groups = device_ctrl_groups,
222 };
223
224 /*
225 * edac_device_register_sysfs_main_kobj
226 *
227 * perform the high level setup for the new edac_device instance
228 *
229 * Return: 0 SUCCESS
230 * !0 FAILURE
231 */
edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info * edac_dev)232 int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
233 {
234 struct device *dev_root;
235 const struct bus_type *edac_subsys;
236 int err = -ENODEV;
237
238 edac_dbg(1, "\n");
239
240 /* get the /sys/devices/system/edac reference */
241 edac_subsys = edac_get_sysfs_subsys();
242
243 /* Point to the 'edac_subsys' this instance 'reports' to */
244 edac_dev->edac_subsys = edac_subsys;
245
246 /* Init the devices's kobject */
247 memset(&edac_dev->kobj, 0, sizeof(struct kobject));
248
249 /* Record which module 'owns' this control structure
250 * and bump the ref count of the module
251 */
252 edac_dev->owner = THIS_MODULE;
253
254 if (!try_module_get(edac_dev->owner))
255 goto err_out;
256
257 /* register */
258 dev_root = bus_get_dev_root(edac_subsys);
259 if (dev_root) {
260 err = kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl,
261 &dev_root->kobj, "%s", edac_dev->name);
262 put_device(dev_root);
263 }
264 if (err) {
265 edac_dbg(1, "Failed to register '.../edac/%s'\n",
266 edac_dev->name);
267 goto err_kobj_reg;
268 }
269 kobject_uevent(&edac_dev->kobj, KOBJ_ADD);
270
271 /* At this point, to 'free' the control struct,
272 * edac_device_unregister_sysfs_main_kobj() must be used
273 */
274
275 edac_dbg(4, "Registered '.../edac/%s' kobject\n", edac_dev->name);
276
277 return 0;
278
279 /* Error exit stack */
280 err_kobj_reg:
281 kobject_put(&edac_dev->kobj);
282 module_put(edac_dev->owner);
283
284 err_out:
285 return err;
286 }
287
288 /*
289 * edac_device_unregister_sysfs_main_kobj:
290 * the '..../edac/<name>' kobject
291 */
edac_device_unregister_sysfs_main_kobj(struct edac_device_ctl_info * dev)292 void edac_device_unregister_sysfs_main_kobj(struct edac_device_ctl_info *dev)
293 {
294 edac_dbg(0, "\n");
295 edac_dbg(4, "name of kobject is: %s\n", kobject_name(&dev->kobj));
296
297 /*
298 * Unregister the edac device's kobject and
299 * allow for reference count to reach 0 at which point
300 * the callback will be called to:
301 * a) module_put() this module
302 * b) 'kfree' the memory
303 */
304 kobject_put(&dev->kobj);
305 }
306
307 /* edac_dev -> instance information */
308
309 /*
310 * Set of low-level instance attribute show functions
311 */
instance_ue_count_show(struct edac_device_instance * instance,char * data)312 static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
313 char *data)
314 {
315 return sprintf(data, "%u\n", instance->counters.ue_count);
316 }
317
instance_ce_count_show(struct edac_device_instance * instance,char * data)318 static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
319 char *data)
320 {
321 return sprintf(data, "%u\n", instance->counters.ce_count);
322 }
323
324 #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
325 #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
326
327 /* DEVICE instance kobject release() function */
edac_device_ctrl_instance_release(struct kobject * kobj)328 static void edac_device_ctrl_instance_release(struct kobject *kobj)
329 {
330 struct edac_device_instance *instance;
331
332 edac_dbg(1, "\n");
333
334 /* map from this kobj to the main control struct
335 * and then dec the main kobj count
336 */
337 instance = to_instance(kobj);
338 kobject_put(&instance->ctl->kobj);
339 }
340
341 /* instance specific attribute structure */
342 struct instance_attribute {
343 struct attribute attr;
344 ssize_t(*show) (struct edac_device_instance *, char *);
345 ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
346 };
347
348 /* Function to 'show' fields from the edac_dev 'instance' structure */
edac_dev_instance_show(struct kobject * kobj,struct attribute * attr,char * buffer)349 static ssize_t edac_dev_instance_show(struct kobject *kobj,
350 struct attribute *attr, char *buffer)
351 {
352 struct edac_device_instance *instance = to_instance(kobj);
353 struct instance_attribute *instance_attr = to_instance_attr(attr);
354
355 if (instance_attr->show)
356 return instance_attr->show(instance, buffer);
357 return -EIO;
358 }
359
360 /* Function to 'store' fields into the edac_dev 'instance' structure */
edac_dev_instance_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t count)361 static ssize_t edac_dev_instance_store(struct kobject *kobj,
362 struct attribute *attr,
363 const char *buffer, size_t count)
364 {
365 struct edac_device_instance *instance = to_instance(kobj);
366 struct instance_attribute *instance_attr = to_instance_attr(attr);
367
368 if (instance_attr->store)
369 return instance_attr->store(instance, buffer, count);
370 return -EIO;
371 }
372
373 /* edac_dev file operations for an 'instance' */
374 static const struct sysfs_ops device_instance_ops = {
375 .show = edac_dev_instance_show,
376 .store = edac_dev_instance_store
377 };
378
379 #define INSTANCE_ATTR(_name,_mode,_show,_store) \
380 static struct instance_attribute attr_instance_##_name = { \
381 .attr = {.name = __stringify(_name), .mode = _mode }, \
382 .show = _show, \
383 .store = _store, \
384 };
385
386 /*
387 * Define attributes visible for the edac_device instance object
388 * Each contains a pointer to a show and an optional set
389 * function pointer that does the low level output/input
390 */
391 INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
392 INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
393
394 /* list of edac_dev 'instance' attributes */
395 static struct attribute *device_instance_attrs[] = {
396 &attr_instance_ce_count.attr,
397 &attr_instance_ue_count.attr,
398 NULL,
399 };
400 ATTRIBUTE_GROUPS(device_instance);
401
402 /* The 'ktype' for each edac_dev 'instance' */
403 static struct kobj_type ktype_instance_ctrl = {
404 .release = edac_device_ctrl_instance_release,
405 .sysfs_ops = &device_instance_ops,
406 .default_groups = device_instance_groups,
407 };
408
409 /* edac_dev -> instance -> block information */
410
411 #define to_block(k) container_of(k, struct edac_device_block, kobj)
412 #define to_block_attr(a) \
413 container_of(a, struct edac_dev_sysfs_block_attribute, attr)
414
415 /*
416 * Set of low-level block attribute show functions
417 */
block_ue_count_show(struct kobject * kobj,struct attribute * attr,char * data)418 static ssize_t block_ue_count_show(struct kobject *kobj,
419 struct attribute *attr, char *data)
420 {
421 struct edac_device_block *block = to_block(kobj);
422
423 return sprintf(data, "%u\n", block->counters.ue_count);
424 }
425
block_ce_count_show(struct kobject * kobj,struct attribute * attr,char * data)426 static ssize_t block_ce_count_show(struct kobject *kobj,
427 struct attribute *attr, char *data)
428 {
429 struct edac_device_block *block = to_block(kobj);
430
431 return sprintf(data, "%u\n", block->counters.ce_count);
432 }
433
434 /* DEVICE block kobject release() function */
edac_device_ctrl_block_release(struct kobject * kobj)435 static void edac_device_ctrl_block_release(struct kobject *kobj)
436 {
437 struct edac_device_block *block;
438
439 edac_dbg(1, "\n");
440
441 /* get the container of the kobj */
442 block = to_block(kobj);
443
444 /* map from 'block kobj' to 'block->instance->controller->main_kobj'
445 * now 'release' the block kobject
446 */
447 kobject_put(&block->instance->ctl->kobj);
448 }
449
450
451 /* Function to 'show' fields from the edac_dev 'block' structure */
edac_dev_block_show(struct kobject * kobj,struct attribute * attr,char * buffer)452 static ssize_t edac_dev_block_show(struct kobject *kobj,
453 struct attribute *attr, char *buffer)
454 {
455 struct edac_dev_sysfs_block_attribute *block_attr =
456 to_block_attr(attr);
457
458 if (block_attr->show)
459 return block_attr->show(kobj, attr, buffer);
460 return -EIO;
461 }
462
463 /* edac_dev file operations for a 'block' */
464 static const struct sysfs_ops device_block_ops = {
465 .show = edac_dev_block_show,
466 };
467
468 #define BLOCK_ATTR(_name,_mode,_show) \
469 static struct edac_dev_sysfs_block_attribute attr_block_##_name = { \
470 .attr = {.name = __stringify(_name), .mode = _mode }, \
471 .show = _show, \
472 };
473
474 BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show);
475 BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show);
476
477 /* list of edac_dev 'block' attributes */
478 static struct attribute *device_block_attrs[] = {
479 &attr_block_ce_count.attr,
480 &attr_block_ue_count.attr,
481 NULL,
482 };
483 ATTRIBUTE_GROUPS(device_block);
484
485 /* The 'ktype' for each edac_dev 'block' */
486 static struct kobj_type ktype_block_ctrl = {
487 .release = edac_device_ctrl_block_release,
488 .sysfs_ops = &device_block_ops,
489 .default_groups = device_block_groups,
490 };
491
492 /* block ctor/dtor code */
493
494 /*
495 * edac_device_create_block
496 */
edac_device_create_block(struct edac_device_ctl_info * edac_dev,struct edac_device_instance * instance,struct edac_device_block * block)497 static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
498 struct edac_device_instance *instance,
499 struct edac_device_block *block)
500 {
501 int i;
502 int err;
503 struct edac_dev_sysfs_block_attribute *sysfs_attrib;
504 struct kobject *main_kobj;
505
506 edac_dbg(4, "Instance '%s' inst_p=%p block '%s' block_p=%p\n",
507 instance->name, instance, block->name, block);
508 edac_dbg(4, "block kobj=%p block kobj->parent=%p\n",
509 &block->kobj, &block->kobj.parent);
510
511 /* init this block's kobject */
512 memset(&block->kobj, 0, sizeof(struct kobject));
513
514 /* bump the main kobject's reference count for this controller
515 * and this instance is dependent on the main
516 */
517 main_kobj = kobject_get(&edac_dev->kobj);
518 if (!main_kobj) {
519 err = -ENODEV;
520 goto err_out;
521 }
522
523 /* Add this block's kobject */
524 err = kobject_init_and_add(&block->kobj, &ktype_block_ctrl,
525 &instance->kobj,
526 "%s", block->name);
527 if (err) {
528 edac_dbg(1, "Failed to register instance '%s'\n", block->name);
529 kobject_put(main_kobj);
530 err = -ENODEV;
531 goto err_out;
532 }
533
534 /* If there are driver level block attributes, then added them
535 * to the block kobject
536 */
537 sysfs_attrib = block->block_attributes;
538 if (sysfs_attrib && block->nr_attribs) {
539 for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
540
541 edac_dbg(4, "creating block attrib='%s' attrib->%p to kobj=%p\n",
542 sysfs_attrib->attr.name,
543 sysfs_attrib, &block->kobj);
544
545 /* Create each block_attribute file */
546 err = sysfs_create_file(&block->kobj,
547 &sysfs_attrib->attr);
548 if (err)
549 goto err_on_attrib;
550 }
551 }
552 kobject_uevent(&block->kobj, KOBJ_ADD);
553
554 return 0;
555
556 /* Error unwind stack */
557 err_on_attrib:
558 kobject_put(&block->kobj);
559
560 err_out:
561 return err;
562 }
563
564 /*
565 * edac_device_delete_block(edac_dev,block);
566 */
edac_device_delete_block(struct edac_device_ctl_info * edac_dev,struct edac_device_block * block)567 static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
568 struct edac_device_block *block)
569 {
570 struct edac_dev_sysfs_block_attribute *sysfs_attrib;
571 int i;
572
573 /* if this block has 'attributes' then we need to iterate over the list
574 * and 'remove' the attributes on this block
575 */
576 sysfs_attrib = block->block_attributes;
577 if (sysfs_attrib && block->nr_attribs) {
578 for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
579
580 /* remove each block_attrib file */
581 sysfs_remove_file(&block->kobj, &sysfs_attrib->attr);
582 }
583 }
584
585 /* unregister this block's kobject, SEE:
586 * edac_device_ctrl_block_release() callback operation
587 */
588 kobject_put(&block->kobj);
589 }
590
591 /* instance ctor/dtor code */
592
593 /*
594 * edac_device_create_instance
595 * create just one instance of an edac_device 'instance'
596 */
edac_device_create_instance(struct edac_device_ctl_info * edac_dev,int idx)597 static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
598 int idx)
599 {
600 int i, j;
601 int err;
602 struct edac_device_instance *instance;
603 struct kobject *main_kobj;
604
605 instance = &edac_dev->instances[idx];
606
607 /* Init the instance's kobject */
608 memset(&instance->kobj, 0, sizeof(struct kobject));
609
610 instance->ctl = edac_dev;
611
612 /* bump the main kobject's reference count for this controller
613 * and this instance is dependent on the main
614 */
615 main_kobj = kobject_get(&edac_dev->kobj);
616 if (!main_kobj) {
617 err = -ENODEV;
618 goto err_out;
619 }
620
621 /* Formally register this instance's kobject under the edac_device */
622 err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
623 &edac_dev->kobj, "%s", instance->name);
624 if (err != 0) {
625 edac_dbg(2, "Failed to register instance '%s'\n",
626 instance->name);
627 kobject_put(main_kobj);
628 goto err_out;
629 }
630
631 edac_dbg(4, "now register '%d' blocks for instance %d\n",
632 instance->nr_blocks, idx);
633
634 /* register all blocks of this instance */
635 for (i = 0; i < instance->nr_blocks; i++) {
636 err = edac_device_create_block(edac_dev, instance,
637 &instance->blocks[i]);
638 if (err) {
639 /* If any fail, remove all previous ones */
640 for (j = 0; j < i; j++)
641 edac_device_delete_block(edac_dev,
642 &instance->blocks[j]);
643 goto err_release_instance_kobj;
644 }
645 }
646 kobject_uevent(&instance->kobj, KOBJ_ADD);
647
648 edac_dbg(4, "Registered instance %d '%s' kobject\n",
649 idx, instance->name);
650
651 return 0;
652
653 /* error unwind stack */
654 err_release_instance_kobj:
655 kobject_put(&instance->kobj);
656
657 err_out:
658 return err;
659 }
660
661 /*
662 * edac_device_remove_instance
663 * remove an edac_device instance
664 */
edac_device_delete_instance(struct edac_device_ctl_info * edac_dev,int idx)665 static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
666 int idx)
667 {
668 struct edac_device_instance *instance;
669 int i;
670
671 instance = &edac_dev->instances[idx];
672
673 /* unregister all blocks in this instance */
674 for (i = 0; i < instance->nr_blocks; i++)
675 edac_device_delete_block(edac_dev, &instance->blocks[i]);
676
677 /* unregister this instance's kobject, SEE:
678 * edac_device_ctrl_instance_release() for callback operation
679 */
680 kobject_put(&instance->kobj);
681 }
682
683 /*
684 * edac_device_create_instances
685 * create the first level of 'instances' for this device
686 * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
687 */
edac_device_create_instances(struct edac_device_ctl_info * edac_dev)688 static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
689 {
690 int i, j;
691 int err;
692
693 edac_dbg(0, "\n");
694
695 /* iterate over creation of the instances */
696 for (i = 0; i < edac_dev->nr_instances; i++) {
697 err = edac_device_create_instance(edac_dev, i);
698 if (err) {
699 /* unwind previous instances on error */
700 for (j = 0; j < i; j++)
701 edac_device_delete_instance(edac_dev, j);
702 return err;
703 }
704 }
705
706 return 0;
707 }
708
709 /*
710 * edac_device_delete_instances(edac_dev);
711 * unregister all the kobjects of the instances
712 */
edac_device_delete_instances(struct edac_device_ctl_info * edac_dev)713 static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
714 {
715 int i;
716
717 /* iterate over creation of the instances */
718 for (i = 0; i < edac_dev->nr_instances; i++)
719 edac_device_delete_instance(edac_dev, i);
720 }
721
722 /* edac_dev sysfs ctor/dtor code */
723
724 /*
725 * edac_device_add_main_sysfs_attributes
726 * add some attributes to this instance's main kobject
727 */
edac_device_add_main_sysfs_attributes(struct edac_device_ctl_info * edac_dev)728 static int edac_device_add_main_sysfs_attributes(
729 struct edac_device_ctl_info *edac_dev)
730 {
731 const struct edac_dev_sysfs_attribute *sysfs_attrib;
732 int err = 0;
733
734 sysfs_attrib = edac_dev->sysfs_attributes;
735 if (sysfs_attrib) {
736 /* iterate over the array and create an attribute for each
737 * entry in the list
738 */
739 while (sysfs_attrib->attr.name != NULL) {
740 err = sysfs_create_file(&edac_dev->kobj,
741 &sysfs_attrib->attr);
742 if (err)
743 goto err_out;
744
745 sysfs_attrib++;
746 }
747 }
748
749 err_out:
750 return err;
751 }
752
753 /*
754 * edac_device_remove_main_sysfs_attributes
755 * remove any attributes to this instance's main kobject
756 */
edac_device_remove_main_sysfs_attributes(struct edac_device_ctl_info * edac_dev)757 static void edac_device_remove_main_sysfs_attributes(
758 struct edac_device_ctl_info *edac_dev)
759 {
760 const struct edac_dev_sysfs_attribute *sysfs_attrib;
761
762 /* if there are main attributes, defined, remove them. First,
763 * point to the start of the array and iterate over it
764 * removing each attribute listed from this device's instance's kobject
765 */
766 sysfs_attrib = edac_dev->sysfs_attributes;
767 if (sysfs_attrib) {
768 while (sysfs_attrib->attr.name != NULL) {
769 sysfs_remove_file(&edac_dev->kobj, &sysfs_attrib->attr);
770 sysfs_attrib++;
771 }
772 }
773 }
774
775 /*
776 * edac_device_create_sysfs() Constructor
777 *
778 * accept a created edac_device control structure
779 * and 'export' it to sysfs. The 'main' kobj should already have been
780 * created. 'instance' and 'block' kobjects should be registered
781 * along with any 'block' attributes from the low driver. In addition,
782 * the main attributes (if any) are connected to the main kobject of
783 * the control structure.
784 *
785 * Return:
786 * 0 Success
787 * !0 Failure
788 */
edac_device_create_sysfs(struct edac_device_ctl_info * edac_dev)789 int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
790 {
791 int err;
792 struct kobject *edac_kobj = &edac_dev->kobj;
793
794 edac_dbg(0, "idx=%d\n", edac_dev->dev_idx);
795
796 /* go create any main attributes callers wants */
797 err = edac_device_add_main_sysfs_attributes(edac_dev);
798 if (err) {
799 edac_dbg(0, "failed to add sysfs attribs\n");
800 goto err_out;
801 }
802
803 /* create a symlink from the edac device
804 * to the platform 'device' being used for this
805 */
806 err = sysfs_create_link(edac_kobj,
807 &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
808 if (err) {
809 edac_dbg(0, "sysfs_create_link() returned err= %d\n", err);
810 goto err_remove_main_attribs;
811 }
812
813 /* Create the first level instance directories
814 * In turn, the nested blocks beneath the instances will
815 * be registered as well
816 */
817 err = edac_device_create_instances(edac_dev);
818 if (err) {
819 edac_dbg(0, "edac_device_create_instances() returned err= %d\n",
820 err);
821 goto err_remove_link;
822 }
823
824
825 edac_dbg(4, "create-instances done, idx=%d\n", edac_dev->dev_idx);
826
827 return 0;
828
829 /* Error unwind stack */
830 err_remove_link:
831 /* remove the sym link */
832 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
833
834 err_remove_main_attribs:
835 edac_device_remove_main_sysfs_attributes(edac_dev);
836
837 err_out:
838 return err;
839 }
840
841 /*
842 * edac_device_remove_sysfs() destructor
843 *
844 * given an edac_device struct, tear down the kobject resources
845 */
edac_device_remove_sysfs(struct edac_device_ctl_info * edac_dev)846 void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
847 {
848 edac_dbg(0, "\n");
849
850 /* remove any main attributes for this device */
851 edac_device_remove_main_sysfs_attributes(edac_dev);
852
853 /* remove the device sym link */
854 sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
855
856 /* walk the instance/block kobject tree, deconstructing it */
857 edac_device_delete_instances(edac_dev);
858 }
859