drivers/edac: fix edac_device sysfs completion code
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / edac / edac_device.c
1
2 /*
3 * edac_device.c
4 * (C) 2007 www.douglaskthompson.com
5 *
6 * This file may be distributed under the terms of the
7 * GNU General Public License.
8 *
9 * Written by Doug Thompson <norsk5@xmission.com>
10 *
11 * edac_device API implementation
12 * 19 Jan 2007
13 */
14
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/smp.h>
18 #include <linux/init.h>
19 #include <linux/sysctl.h>
20 #include <linux/highmem.h>
21 #include <linux/timer.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/spinlock.h>
25 #include <linux/list.h>
26 #include <linux/sysdev.h>
27 #include <linux/ctype.h>
28 #include <linux/workqueue.h>
29 #include <asm/uaccess.h>
30 #include <asm/page.h>
31
32 #include "edac_core.h"
33 #include "edac_module.h"
34
35 /* lock to memory controller's control array 'edac_device_list' */
36 static DEFINE_MUTEX(device_ctls_mutex);
37 static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
38
39 #ifdef CONFIG_EDAC_DEBUG
40 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
41 {
42 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
43 debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
44 debugf3("\tdev = %p\n", edac_dev->dev);
45 debugf3("\tmod_name:ctl_name = %s:%s\n",
46 edac_dev->mod_name, edac_dev->ctl_name);
47 debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
48 }
49 #endif /* CONFIG_EDAC_DEBUG */
50
51
52 /*
53 * edac_device_alloc_ctl_info()
54 * Allocate a new edac device control info structure
55 *
56 * The control structure is allocated in complete chunk
57 * from the OS. It is in turn sub allocated to the
58 * various objects that compose the struture
59 *
60 * The structure has a 'nr_instance' array within itself.
61 * Each instance represents a major component
62 * Example: L1 cache and L2 cache are 2 instance components
63 *
64 * Within each instance is an array of 'nr_blocks' blockoffsets
65 */
66 struct edac_device_ctl_info *edac_device_alloc_ctl_info(
67 unsigned sz_private,
68 char *edac_device_name, unsigned nr_instances,
69 char *edac_block_name, unsigned nr_blocks,
70 unsigned offset_value, /* zero, 1, or other based offset */
71 struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
72 int device_index)
73 {
74 struct edac_device_ctl_info *dev_ctl;
75 struct edac_device_instance *dev_inst, *inst;
76 struct edac_device_block *dev_blk, *blk_p, *blk;
77 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
78 unsigned total_size;
79 unsigned count;
80 unsigned instance, block, attr;
81 void *pvt;
82 int err;
83
84 debugf1("%s() instances=%d blocks=%d\n",
85 __func__, nr_instances, nr_blocks);
86
87 /* Calculate the size of memory we need to allocate AND
88 * determine the offsets of the various item arrays
89 * (instance,block,attrib) from the start of an allocated structure.
90 * We want the alignment of each item (instance,block,attrib)
91 * to be at least as stringent as what the compiler would
92 * provide if we could simply hardcode everything into a single struct.
93 */
94 dev_ctl = (struct edac_device_ctl_info *)NULL;
95
96 /* Calc the 'end' offset past end of ONE ctl_info structure
97 * which will become the start of the 'instance' array
98 */
99 dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
100
101 /* Calc the 'end' offset past the instance array within the ctl_info
102 * which will become the start of the block array
103 */
104 dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
105
106 /* Calc the 'end' offset past the dev_blk array
107 * which will become the start of the attrib array, if any.
108 */
109 count = nr_instances * nr_blocks;
110 dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
111
112 /* Check for case of when an attribute array is specified */
113 if (nr_attrib > 0) {
114 /* calc how many nr_attrib we need */
115 count *= nr_attrib;
116
117 /* Calc the 'end' offset past the attributes array */
118 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
119 } else {
120 /* no attribute array specificed */
121 pvt = edac_align_ptr(dev_attrib, sz_private);
122 }
123
124 /* 'pvt' now points to where the private data area is.
125 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
126 * is baselined at ZERO
127 */
128 total_size = ((unsigned long)pvt) + sz_private;
129
130 /* Allocate the amount of memory for the set of control structures */
131 dev_ctl = kzalloc(total_size, GFP_KERNEL);
132 if (dev_ctl == NULL)
133 return NULL;
134
135 /* Adjust pointers so they point within the actual memory we
136 * just allocated rather than an imaginary chunk of memory
137 * located at address 0.
138 * 'dev_ctl' points to REAL memory, while the others are
139 * ZERO based and thus need to be adjusted to point within
140 * the allocated memory.
141 */
142 dev_inst = (struct edac_device_instance *)
143 (((char *)dev_ctl) + ((unsigned long)dev_inst));
144 dev_blk = (struct edac_device_block *)
145 (((char *)dev_ctl) + ((unsigned long)dev_blk));
146 dev_attrib = (struct edac_dev_sysfs_block_attribute *)
147 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
148 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
149
150 /* Begin storing the information into the control info structure */
151 dev_ctl->dev_idx = device_index;
152 dev_ctl->nr_instances = nr_instances;
153 dev_ctl->instances = dev_inst;
154 dev_ctl->pvt_info = pvt;
155
156 /* Name of this edac device */
157 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
158
159 /* Initialize every Instance */
160 for (instance = 0; instance < nr_instances; instance++) {
161 inst = &dev_inst[instance];
162 inst->ctl = dev_ctl;
163 inst->nr_blocks = nr_blocks;
164 blk_p = &dev_blk[instance * nr_blocks];
165 inst->blocks = blk_p;
166
167 /* name of this instance */
168 snprintf(inst->name, sizeof(inst->name),
169 "%s%u", edac_device_name, instance);
170
171 /* Initialize every block in each instance */
172 for (block = 0; block < nr_blocks; block++) {
173 blk = &blk_p[block];
174 blk->instance = inst;
175 snprintf(blk->name, sizeof(blk->name),
176 "%s%d", edac_block_name, block+offset_value);
177
178 debugf1("%s() instance=%d block=%d name=%s\n",
179 __func__, instance, block, blk->name);
180
181 /* if there are NO attributes OR no attribute pointer
182 * then continue on to next block iteration
183 */
184 if ((nr_attrib == 0) || (attrib_spec == NULL))
185 continue;
186
187 /* setup the attribute array for this block */
188 blk->nr_attribs = nr_attrib;
189 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
190 blk->block_attributes = attrib_p;
191
192 /* Initialize every user specified attribute in this
193 * block with the data the caller passed in
194 */
195 for (attr = 0; attr < nr_attrib; attr++) {
196 attrib = &attrib_p[attr];
197 attrib->attr = attrib_spec->attr;
198 attrib->show = attrib_spec->show;
199 attrib->store = attrib_spec->store;
200
201 /* up reference this block */
202 attrib->block = blk;
203
204 /* bump the attrib_spec */
205 attrib_spec++;
206 }
207 }
208 }
209
210 /* Mark this instance as merely ALLOCATED */
211 dev_ctl->op_state = OP_ALLOC;
212
213 /*
214 * Initialize the 'root' kobj for the edac_device controller
215 */
216 err = edac_device_register_sysfs_main_kobj(dev_ctl);
217 if (err) {
218 kfree(dev_ctl);
219 return NULL;
220 }
221
222 /* at this point, the root kobj is valid, and in order to
223 * 'free' the object, then the function:
224 * edac_device_unregister_sysfs_main_kobj() must be called
225 * which will perform kobj unregistration and the actual free
226 * will occur during the kobject callback operation
227 */
228
229 return dev_ctl;
230 }
231 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
232
233 /*
234 * edac_device_free_ctl_info()
235 * frees the memory allocated by the edac_device_alloc_ctl_info()
236 * function
237 */
238 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
239 {
240 edac_device_unregister_sysfs_main_kobj(ctl_info);
241 }
242 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
243
244 /*
245 * find_edac_device_by_dev
246 * scans the edac_device list for a specific 'struct device *'
247 *
248 * lock to be held prior to call: device_ctls_mutex
249 *
250 * Return:
251 * pointer to control structure managing 'dev'
252 * NULL if not found on list
253 */
254 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
255 {
256 struct edac_device_ctl_info *edac_dev;
257 struct list_head *item;
258
259 debugf3("%s()\n", __func__);
260
261 list_for_each(item, &edac_device_list) {
262 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
263
264 if (edac_dev->dev == dev)
265 return edac_dev;
266 }
267
268 return NULL;
269 }
270
271 /*
272 * add_edac_dev_to_global_list
273 * Before calling this function, caller must
274 * assign a unique value to edac_dev->dev_idx.
275 *
276 * lock to be held prior to call: device_ctls_mutex
277 *
278 * Return:
279 * 0 on success
280 * 1 on failure.
281 */
282 static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
283 {
284 struct list_head *item, *insert_before;
285 struct edac_device_ctl_info *rover;
286
287 insert_before = &edac_device_list;
288
289 /* Determine if already on the list */
290 rover = find_edac_device_by_dev(edac_dev->dev);
291 if (unlikely(rover != NULL))
292 goto fail0;
293
294 /* Insert in ascending order by 'dev_idx', so find position */
295 list_for_each(item, &edac_device_list) {
296 rover = list_entry(item, struct edac_device_ctl_info, link);
297
298 if (rover->dev_idx >= edac_dev->dev_idx) {
299 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
300 goto fail1;
301
302 insert_before = item;
303 break;
304 }
305 }
306
307 list_add_tail_rcu(&edac_dev->link, insert_before);
308 return 0;
309
310 fail0:
311 edac_printk(KERN_WARNING, EDAC_MC,
312 "%s (%s) %s %s already assigned %d\n",
313 rover->dev->bus_id, dev_name(rover),
314 rover->mod_name, rover->ctl_name, rover->dev_idx);
315 return 1;
316
317 fail1:
318 edac_printk(KERN_WARNING, EDAC_MC,
319 "bug in low-level driver: attempt to assign\n"
320 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
321 __func__);
322 return 1;
323 }
324
325 /*
326 * complete_edac_device_list_del
327 *
328 * callback function when reference count is zero
329 */
330 static void complete_edac_device_list_del(struct rcu_head *head)
331 {
332 struct edac_device_ctl_info *edac_dev;
333
334 edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
335 INIT_LIST_HEAD(&edac_dev->link);
336 complete(&edac_dev->removal_complete);
337 }
338
339 /*
340 * del_edac_device_from_global_list
341 *
342 * remove the RCU, setup for a callback call,
343 * then wait for the callback to occur
344 */
345 static void del_edac_device_from_global_list(struct edac_device_ctl_info
346 *edac_device)
347 {
348 list_del_rcu(&edac_device->link);
349
350 init_completion(&edac_device->removal_complete);
351 call_rcu(&edac_device->rcu, complete_edac_device_list_del);
352 wait_for_completion(&edac_device->removal_complete);
353 }
354
355 /**
356 * edac_device_find
357 * Search for a edac_device_ctl_info structure whose index is 'idx'.
358 *
359 * If found, return a pointer to the structure.
360 * Else return NULL.
361 *
362 * Caller must hold device_ctls_mutex.
363 */
364 struct edac_device_ctl_info *edac_device_find(int idx)
365 {
366 struct list_head *item;
367 struct edac_device_ctl_info *edac_dev;
368
369 /* Iterate over list, looking for exact match of ID */
370 list_for_each(item, &edac_device_list) {
371 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
372
373 if (edac_dev->dev_idx >= idx) {
374 if (edac_dev->dev_idx == idx)
375 return edac_dev;
376
377 /* not on list, so terminate early */
378 break;
379 }
380 }
381
382 return NULL;
383 }
384 EXPORT_SYMBOL_GPL(edac_device_find);
385
386 /*
387 * edac_device_workq_function
388 * performs the operation scheduled by a workq request
389 */
390 static void edac_device_workq_function(struct work_struct *work_req)
391 {
392 struct delayed_work *d_work = (struct delayed_work *)work_req;
393 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
394
395 //debugf0("%s() here and running\n", __func__);
396 mutex_lock(&device_ctls_mutex);
397
398 /* Only poll controllers that are running polled and have a check */
399 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
400 (edac_dev->edac_check != NULL)) {
401 edac_dev->edac_check(edac_dev);
402 }
403
404 mutex_unlock(&device_ctls_mutex);
405
406 /* Reschedule */
407 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
408 }
409
410 /*
411 * edac_device_workq_setup
412 * initialize a workq item for this edac_device instance
413 * passing in the new delay period in msec
414 */
415 void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
416 unsigned msec)
417 {
418 debugf0("%s()\n", __func__);
419
420 edac_dev->poll_msec = msec;
421 edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
422
423 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
424 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
425 }
426
427 /*
428 * edac_device_workq_teardown
429 * stop the workq processing on this edac_dev
430 */
431 void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
432 {
433 int status;
434
435 status = cancel_delayed_work(&edac_dev->work);
436 if (status == 0) {
437 /* workq instance might be running, wait for it */
438 flush_workqueue(edac_workqueue);
439 }
440 }
441
442 /*
443 * edac_device_reset_delay_period
444 */
445
446 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
447 unsigned long value)
448 {
449 mutex_lock(&device_ctls_mutex);
450
451 /* cancel the current workq request */
452 edac_device_workq_teardown(edac_dev);
453
454 /* restart the workq request, with new delay value */
455 edac_device_workq_setup(edac_dev, value);
456
457 mutex_unlock(&device_ctls_mutex);
458 }
459
460 /**
461 * edac_device_add_device: Insert the 'edac_dev' structure into the
462 * edac_device global list and create sysfs entries associated with
463 * edac_device structure.
464 * @edac_device: pointer to the edac_device structure to be added to the list
465 * 'edac_device' structure.
466 *
467 * Return:
468 * 0 Success
469 * !0 Failure
470 */
471 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
472 {
473 debugf0("%s()\n", __func__);
474
475 #ifdef CONFIG_EDAC_DEBUG
476 if (edac_debug_level >= 3)
477 edac_device_dump_device(edac_dev);
478 #endif
479 mutex_lock(&device_ctls_mutex);
480
481 if (add_edac_dev_to_global_list(edac_dev))
482 goto fail0;
483
484 /* set load time so that error rate can be tracked */
485 edac_dev->start_time = jiffies;
486
487 /* create this instance's sysfs entries */
488 if (edac_device_create_sysfs(edac_dev)) {
489 edac_device_printk(edac_dev, KERN_WARNING,
490 "failed to create sysfs device\n");
491 goto fail1;
492 }
493
494 /* If there IS a check routine, then we are running POLLED */
495 if (edac_dev->edac_check != NULL) {
496 /* This instance is NOW RUNNING */
497 edac_dev->op_state = OP_RUNNING_POLL;
498
499 /*
500 * enable workq processing on this instance,
501 * default = 1000 msec
502 */
503 edac_device_workq_setup(edac_dev, 1000);
504 } else {
505 edac_dev->op_state = OP_RUNNING_INTERRUPT;
506 }
507
508 /* Report action taken */
509 edac_device_printk(edac_dev, KERN_INFO,
510 "Giving out device to module '%s' controller "
511 "'%s': DEV '%s' (%s)\n",
512 edac_dev->mod_name,
513 edac_dev->ctl_name,
514 dev_name(edac_dev),
515 edac_op_state_to_string(edac_dev->op_state));
516
517 mutex_unlock(&device_ctls_mutex);
518 return 0;
519
520 fail1:
521 /* Some error, so remove the entry from the lsit */
522 del_edac_device_from_global_list(edac_dev);
523
524 fail0:
525 mutex_unlock(&device_ctls_mutex);
526 return 1;
527 }
528 EXPORT_SYMBOL_GPL(edac_device_add_device);
529
530 /**
531 * edac_device_del_device:
532 * Remove sysfs entries for specified edac_device structure and
533 * then remove edac_device structure from global list
534 *
535 * @pdev:
536 * Pointer to 'struct device' representing edac_device
537 * structure to remove.
538 *
539 * Return:
540 * Pointer to removed edac_device structure,
541 * OR NULL if device not found.
542 */
543 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
544 {
545 struct edac_device_ctl_info *edac_dev;
546
547 debugf0("MC: %s()\n", __func__);
548
549 mutex_lock(&device_ctls_mutex);
550
551 /* Find the structure on the list, if not there, then leave */
552 edac_dev = find_edac_device_by_dev(dev);
553 if (edac_dev == NULL) {
554 mutex_unlock(&device_ctls_mutex);
555 return NULL;
556 }
557
558 /* mark this instance as OFFLINE */
559 edac_dev->op_state = OP_OFFLINE;
560
561 /* clear workq processing on this instance */
562 edac_device_workq_teardown(edac_dev);
563
564 /* deregister from global list */
565 del_edac_device_from_global_list(edac_dev);
566
567 mutex_unlock(&device_ctls_mutex);
568
569 /* Tear down the sysfs entries for this instance */
570 edac_device_remove_sysfs(edac_dev);
571
572 edac_printk(KERN_INFO, EDAC_MC,
573 "Removed device %d for %s %s: DEV %s\n",
574 edac_dev->dev_idx,
575 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
576
577 return edac_dev;
578 }
579 EXPORT_SYMBOL_GPL(edac_device_del_device);
580
581 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
582 {
583 return edac_dev->log_ce;
584 }
585
586 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
587 {
588 return edac_dev->log_ue;
589 }
590
591 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
592 *edac_dev)
593 {
594 return edac_dev->panic_on_ue;
595 }
596
597 /*
598 * edac_device_handle_ce
599 * perform a common output and handling of an 'edac_dev' CE event
600 */
601 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
602 int inst_nr, int block_nr, const char *msg)
603 {
604 struct edac_device_instance *instance;
605 struct edac_device_block *block = NULL;
606
607 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
608 edac_device_printk(edac_dev, KERN_ERR,
609 "INTERNAL ERROR: 'instance' out of range "
610 "(%d >= %d)\n", inst_nr,
611 edac_dev->nr_instances);
612 return;
613 }
614
615 instance = edac_dev->instances + inst_nr;
616
617 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
618 edac_device_printk(edac_dev, KERN_ERR,
619 "INTERNAL ERROR: instance %d 'block' "
620 "out of range (%d >= %d)\n",
621 inst_nr, block_nr,
622 instance->nr_blocks);
623 return;
624 }
625
626 if (instance->nr_blocks > 0) {
627 block = instance->blocks + block_nr;
628 block->counters.ce_count++;
629 }
630
631 /* Propogate the count up the 'totals' tree */
632 instance->counters.ce_count++;
633 edac_dev->counters.ce_count++;
634
635 if (edac_device_get_log_ce(edac_dev))
636 edac_device_printk(edac_dev, KERN_WARNING,
637 "CE: %s instance: %s block: %s '%s'\n",
638 edac_dev->ctl_name, instance->name,
639 block ? block->name : "N/A", msg);
640 }
641 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
642
643 /*
644 * edac_device_handle_ue
645 * perform a common output and handling of an 'edac_dev' UE event
646 */
647 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
648 int inst_nr, int block_nr, const char *msg)
649 {
650 struct edac_device_instance *instance;
651 struct edac_device_block *block = NULL;
652
653 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
654 edac_device_printk(edac_dev, KERN_ERR,
655 "INTERNAL ERROR: 'instance' out of range "
656 "(%d >= %d)\n", inst_nr,
657 edac_dev->nr_instances);
658 return;
659 }
660
661 instance = edac_dev->instances + inst_nr;
662
663 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
664 edac_device_printk(edac_dev, KERN_ERR,
665 "INTERNAL ERROR: instance %d 'block' "
666 "out of range (%d >= %d)\n",
667 inst_nr, block_nr,
668 instance->nr_blocks);
669 return;
670 }
671
672 if (instance->nr_blocks > 0) {
673 block = instance->blocks + block_nr;
674 block->counters.ue_count++;
675 }
676
677 /* Propogate the count up the 'totals' tree */
678 instance->counters.ue_count++;
679 edac_dev->counters.ue_count++;
680
681 if (edac_device_get_log_ue(edac_dev))
682 edac_device_printk(edac_dev, KERN_EMERG,
683 "UE: %s instance: %s block: %s '%s'\n",
684 edac_dev->ctl_name, instance->name,
685 block ? block->name : "N/A", msg);
686
687 if (edac_device_get_panic_on_ue(edac_dev))
688 panic("EDAC %s: UE instance: %s block %s '%s'\n",
689 edac_dev->ctl_name, instance->name,
690 block ? block->name : "N/A", msg);
691 }
692 EXPORT_SYMBOL_GPL(edac_device_handle_ue);