4 * (C) 2007 www.douglaskthompson.com
6 * This file may be distributed under the terms of the
7 * GNU General Public License.
9 * Written by Doug Thompson <norsk5@xmission.com>
11 * edac_device API implementation
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/spinlock.h>
24 #include <linux/list.h>
25 #include <linux/sysdev.h>
26 #include <linux/ctype.h>
27 #include <linux/workqueue.h>
28 #include <asm/uaccess.h>
31 #include "edac_core.h"
32 #include "edac_module.h"
34 /* lock to memory controller's control array */
35 static DECLARE_MUTEX(device_ctls_mutex);
36 static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
39 static inline void lock_device_list(void)
41 down(&device_ctls_mutex);
44 static inline void unlock_device_list(void)
46 up(&device_ctls_mutex);
50 #ifdef CONFIG_EDAC_DEBUG
51 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
53 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev,edac_dev->dev_idx);
54 debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
55 debugf3("\tdev = %p\n", edac_dev->dev);
56 debugf3("\tmod_name:ctl_name = %s:%s\n",
57 edac_dev->mod_name, edac_dev->ctl_name);
58 debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
60 #endif /* CONFIG_EDAC_DEBUG */
63 * The alloc() and free() functions for the 'edac_device' control info
64 * structure. A MC driver will allocate one of these for each edac_device
65 * it is going to control/register with the EDAC CORE.
67 struct edac_device_ctl_info *edac_device_alloc_ctl_info(
69 char *edac_device_name,
70 unsigned nr_instances,
71 char *edac_block_name,
73 unsigned offset_value,
74 struct edac_attrib_spec *attrib_spec,
77 struct edac_device_ctl_info *dev_ctl;
78 struct edac_device_instance *dev_inst, *inst;
79 struct edac_device_block *dev_blk, *blk_p, *blk;
80 struct edac_attrib *dev_attrib, *attrib_p, *attrib;
83 unsigned instance, block, attr;
86 debugf1("%s() instances=%d blocks=%d\n",
87 __func__,nr_instances,nr_blocks);
89 /* Figure out the offsets of the various items from the start of an
90 * ctl_info structure. We want the alignment of each item
91 * to be at least as stringent as what the compiler would
92 * provide if we could simply hardcode everything into a single struct.
94 dev_ctl = (struct edac_device_ctl_info *) 0;
96 /* Calc the 'end' offset past the ctl_info structure */
97 dev_inst = (struct edac_device_instance *)
98 edac_align_ptr(&dev_ctl[1],sizeof(*dev_inst));
100 /* Calc the 'end' offset past the instance array */
101 dev_blk = (struct edac_device_block *)
102 edac_align_ptr(&dev_inst[nr_instances],sizeof(*dev_blk));
104 /* Calc the 'end' offset past the dev_blk array */
105 count = nr_instances * nr_blocks;
106 dev_attrib = (struct edac_attrib *)
107 edac_align_ptr(&dev_blk[count],sizeof(*dev_attrib));
109 /* Check for case of NO attributes specified */
113 /* Calc the 'end' offset past the attributes array */
114 pvt = edac_align_ptr(&dev_attrib[count],sz_private);
115 total_size = ((unsigned long) pvt) + sz_private;
117 /* Allocate the amount of memory for the set of control structures */
118 if ((dev_ctl = kmalloc(total_size, GFP_KERNEL)) == NULL)
121 /* Adjust pointers so they point within the memory we just allocated
122 * rather than an imaginary chunk of memory located at address 0.
124 dev_inst = (struct edac_device_instance *)
125 (((char *) dev_ctl) + ((unsigned long) dev_inst));
126 dev_blk = (struct edac_device_block *)
127 (((char *) dev_ctl) + ((unsigned long) dev_blk));
128 dev_attrib = (struct edac_attrib *)
129 (((char *) dev_ctl) + ((unsigned long) dev_attrib));
131 (((char *) dev_ctl) + ((unsigned long) pvt)) : NULL;
133 memset(dev_ctl, 0, total_size); /* clear all fields */
134 dev_ctl->nr_instances = nr_instances;
135 dev_ctl->instances = dev_inst;
136 dev_ctl->pvt_info = pvt;
138 /* Name of this edac device, ensure null terminated */
139 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s", edac_device_name);
140 dev_ctl->name[sizeof(dev_ctl->name)-1] = '\0';
142 /* Initialize every Instance */
143 for (instance = 0; instance < nr_instances; instance++) {
144 inst = &dev_inst[instance];
146 inst->nr_blocks = nr_blocks;
147 blk_p = &dev_blk[instance * nr_blocks];
148 inst->blocks = blk_p;
150 /* name of this instance */
151 snprintf(inst->name, sizeof(inst->name),
152 "%s%u", edac_device_name, instance);
153 inst->name[sizeof(inst->name)-1] = '\0';
155 /* Initialize every block in each instance */
160 blk->instance = inst;
161 blk->nr_attribs = nr_attribs;
162 attrib_p = &dev_attrib[block * nr_attribs];
163 blk->attribs = attrib_p;
164 snprintf(blk->name, sizeof(blk->name),
165 "%s%d", edac_block_name,block+1);
166 blk->name[sizeof(blk->name)-1] = '\0';
168 debugf1("%s() instance=%d block=%d name=%s\n",
169 __func__, instance,block,blk->name);
171 if (attrib_spec != NULL) {
172 /* when there is an attrib_spec passed int then
173 * Initialize every attrib of each block
175 for (attr = 0; attr < nr_attribs; attr++) {
176 attrib = &attrib_p[attr];
179 /* Link each attribute to the caller's
180 * spec entry, for name and type
182 attrib->spec = &attrib_spec[attr];
188 /* Mark this instance as merely ALLOCATED */
189 dev_ctl->op_state = OP_ALLOC;
193 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
196 * edac_device_free_ctl_info()
197 * frees the memory allocated by the edac_device_alloc_ctl_info()
200 void edac_device_free_ctl_info( struct edac_device_ctl_info *ctl_info) {
203 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
208 * find_edac_device_by_dev
209 * scans the edac_device list for a specific 'struct device *'
211 static struct edac_device_ctl_info *
212 find_edac_device_by_dev(struct device *dev)
214 struct edac_device_ctl_info *edac_dev;
215 struct list_head *item;
217 debugf3("%s()\n", __func__);
219 list_for_each(item, &edac_device_list) {
220 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
222 if (edac_dev->dev == dev)
230 * add_edac_dev_to_global_list
231 * Before calling this function, caller must
232 * assign a unique value to edac_dev->dev_idx.
237 static int add_edac_dev_to_global_list (struct edac_device_ctl_info *edac_dev)
239 struct list_head *item, *insert_before;
240 struct edac_device_ctl_info *rover;
242 insert_before = &edac_device_list;
244 /* Determine if already on the list */
245 if (unlikely((rover = find_edac_device_by_dev(edac_dev->dev)) != NULL))
248 /* Insert in ascending order by 'dev_idx', so find position */
249 list_for_each(item, &edac_device_list) {
250 rover = list_entry(item, struct edac_device_ctl_info, link);
252 if (rover->dev_idx >= edac_dev->dev_idx) {
253 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
256 insert_before = item;
261 list_add_tail_rcu(&edac_dev->link, insert_before);
265 edac_printk(KERN_WARNING, EDAC_MC,
266 "%s (%s) %s %s already assigned %d\n",
267 rover->dev->bus_id, dev_name(rover->dev),
268 rover->mod_name, rover->ctl_name, rover->dev_idx);
272 edac_printk(KERN_WARNING, EDAC_MC,
273 "bug in low-level driver: attempt to assign\n"
274 " duplicate dev_idx %d in %s()\n", rover->dev_idx, __func__);
279 * complete_edac_device_list_del
281 static void complete_edac_device_list_del(struct rcu_head *head)
283 struct edac_device_ctl_info *edac_dev;
285 edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
286 INIT_LIST_HEAD(&edac_dev->link);
287 complete(&edac_dev->complete);
291 * del_edac_device_from_global_list
293 static void del_edac_device_from_global_list(
294 struct edac_device_ctl_info *edac_device)
296 list_del_rcu(&edac_device->link);
297 init_completion(&edac_device->complete);
298 call_rcu(&edac_device->rcu, complete_edac_device_list_del);
299 wait_for_completion(&edac_device->complete);
304 * Search for a edac_device_ctl_info structure whose index is 'idx'.
306 * If found, return a pointer to the structure.
309 * Caller must hold device_ctls_mutex.
311 struct edac_device_ctl_info * edac_device_find(int idx)
313 struct list_head *item;
314 struct edac_device_ctl_info *edac_dev;
316 /* Iterate over list, looking for exact match of ID */
317 list_for_each(item, &edac_device_list) {
318 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
320 if (edac_dev->dev_idx >= idx) {
321 if (edac_dev->dev_idx == idx)
324 /* not on list, so terminate early */
331 EXPORT_SYMBOL(edac_device_find);
335 * edac_workq_function
336 * performs the operation scheduled by a workq request
338 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
339 static void edac_workq_function(struct work_struct *work_req)
341 struct delayed_work *d_work = (struct delayed_work*) work_req;
342 struct edac_device_ctl_info *edac_dev =
343 to_edac_device_ctl_work(d_work);
345 static void edac_workq_function(void *ptr)
347 struct edac_device_ctl_info *edac_dev =
348 (struct edac_device_ctl_info *) ptr;
351 //debugf0("%s() here and running\n", __func__);
354 /* Only poll controllers that are running polled and have a check */
355 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
356 (edac_dev->edac_check != NULL)) {
357 edac_dev->edac_check(edac_dev);
360 unlock_device_list();
363 queue_delayed_work(edac_workqueue,&edac_dev->work, edac_dev->delay);
368 * initialize a workq item for this edac_device instance
369 * passing in the new delay period in msec
371 void edac_workq_setup(struct edac_device_ctl_info *edac_dev, unsigned msec)
373 debugf0("%s()\n", __func__);
375 edac_dev->poll_msec = msec;
376 edac_device_calc_delay(edac_dev); /* Calc delay jiffies */
378 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
379 INIT_DELAYED_WORK(&edac_dev->work,edac_workq_function);
381 INIT_WORK(&edac_dev->work,edac_workq_function,edac_dev);
383 queue_delayed_work(edac_workqueue,&edac_dev->work, edac_dev->delay);
387 * edac_workq_teardown
388 * stop the workq processing on this edac_dev
390 void edac_workq_teardown(struct edac_device_ctl_info *edac_dev)
394 status = cancel_delayed_work(&edac_dev->work);
396 /* workq instance might be running, wait for it */
397 flush_workqueue(edac_workqueue);
402 * edac_device_reset_delay_period
405 void edac_device_reset_delay_period(
406 struct edac_device_ctl_info *edac_dev,
411 /* cancel the current workq request */
412 edac_workq_teardown(edac_dev);
414 /* restart the workq request, with new delay value */
415 edac_workq_setup(edac_dev, value);
417 unlock_device_list();
421 * edac_op_state_toString(edac_dev)
423 static char *edac_op_state_toString(struct edac_device_ctl_info *edac_dev)
425 int opstate = edac_dev->op_state;
427 if (opstate == OP_RUNNING_POLL)
429 else if (opstate == OP_RUNNING_INTERRUPT)
431 else if (opstate == OP_RUNNING_POLL_INTR)
433 else if (opstate == OP_ALLOC)
435 else if (opstate == OP_OFFLINE)
442 * edac_device_add_device: Insert the 'edac_dev' structure into the
443 * edac_device global list and create sysfs entries associated with
444 * edac_device structure.
445 * @edac_device: pointer to the edac_device structure to be added to the list
446 * @edac_idx: A unique numeric identifier to be assigned to the
447 * 'edac_device' structure.
453 int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
455 debugf0("%s()\n", __func__);
457 edac_dev->dev_idx = edac_idx;
458 #ifdef CONFIG_EDAC_DEBUG
459 if (edac_debug_level >= 3)
460 edac_device_dump_device(edac_dev);
464 if (add_edac_dev_to_global_list(edac_dev))
467 /* set load time so that error rate can be tracked */
468 edac_dev->start_time = jiffies;
470 /* create this instance's sysfs entries */
471 if (edac_device_create_sysfs(edac_dev)) {
472 edac_device_printk(edac_dev, KERN_WARNING,
473 "failed to create sysfs device\n");
477 /* If there IS a check routine, then we are running POLLED */
478 if (edac_dev->edac_check != NULL) {
479 /* This instance is NOW RUNNING */
480 edac_dev->op_state = OP_RUNNING_POLL;
482 /* enable workq processing on this instance, default = 1000 msec */
483 edac_workq_setup(edac_dev, 1000);
485 edac_dev->op_state = OP_RUNNING_INTERRUPT;
489 /* Report action taken */
490 edac_device_printk(edac_dev, KERN_INFO,
491 "Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
494 dev_name(edac_dev->dev),
495 edac_op_state_toString(edac_dev)
498 unlock_device_list();
502 /* Some error, so remove the entry from the lsit */
503 del_edac_device_from_global_list(edac_dev);
506 unlock_device_list();
509 EXPORT_SYMBOL_GPL(edac_device_add_device);
512 * edac_device_del_device:
513 * Remove sysfs entries for specified edac_device structure and
514 * then remove edac_device structure from global list
517 * Pointer to 'struct device' representing edac_device
518 * structure to remove.
521 * Pointer to removed edac_device structure,
522 * OR NULL if device not found.
524 struct edac_device_ctl_info * edac_device_del_device(struct device *dev)
526 struct edac_device_ctl_info *edac_dev;
528 debugf0("MC: %s()\n", __func__);
532 if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
533 unlock_device_list();
537 /* mark this instance as OFFLINE */
538 edac_dev->op_state = OP_OFFLINE;
540 /* clear workq processing on this instance */
541 edac_workq_teardown(edac_dev);
543 /* Tear down the sysfs entries for this instance */
544 edac_device_remove_sysfs(edac_dev);
546 /* deregister from global list */
547 del_edac_device_from_global_list(edac_dev);
549 unlock_device_list();
551 edac_printk(KERN_INFO, EDAC_MC,
552 "Removed device %d for %s %s: DEV %s\n",
556 dev_name(edac_dev->dev));
560 EXPORT_SYMBOL_GPL(edac_device_del_device);
563 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
565 return edac_dev->log_ce;
568 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
570 return edac_dev->log_ue;
573 static inline int edac_device_get_panic_on_ue(
574 struct edac_device_ctl_info *edac_dev)
576 return edac_dev->panic_on_ue;
580 * edac_device_handle_ce
581 * perform a common output and handling of an 'edac_dev' CE event
583 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
584 int inst_nr, int block_nr, const char *msg)
586 struct edac_device_instance *instance;
587 struct edac_device_block *block = NULL;
589 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
590 edac_device_printk(edac_dev, KERN_ERR,
591 "INTERNAL ERROR: 'instance' out of range "
592 "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
596 instance = edac_dev->instances + inst_nr;
598 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
599 edac_device_printk(edac_dev, KERN_ERR,
600 "INTERNAL ERROR: instance %d 'block' out of range "
601 "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
605 if (instance->nr_blocks > 0) {
606 block = instance->blocks + block_nr;
607 block->counters.ce_count++;
610 /* Propogate the count up the 'totals' tree */
611 instance->counters.ce_count++;
612 edac_dev->counters.ce_count++;
614 if (edac_device_get_log_ce(edac_dev))
615 edac_device_printk(edac_dev, KERN_WARNING,
616 "CE ctl: %s, instance: %s, block: %s: %s\n",
617 edac_dev->ctl_name, instance->name,
618 block ? block->name : "N/A", msg);
620 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
623 * edac_device_handle_ue
624 * perform a common output and handling of an 'edac_dev' UE event
626 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
627 int inst_nr, int block_nr, const char *msg)
629 struct edac_device_instance *instance;
630 struct edac_device_block *block = NULL;
632 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
633 edac_device_printk(edac_dev, KERN_ERR,
634 "INTERNAL ERROR: 'instance' out of range "
635 "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
639 instance = edac_dev->instances + inst_nr;
641 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
642 edac_device_printk(edac_dev, KERN_ERR,
643 "INTERNAL ERROR: instance %d 'block' out of range "
644 "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
648 if (instance->nr_blocks > 0) {
649 block = instance->blocks + block_nr;
650 block->counters.ue_count++;
653 /* Propogate the count up the 'totals' tree */
654 instance->counters.ue_count++;
655 edac_dev->counters.ue_count++;
657 if (edac_device_get_log_ue(edac_dev))
658 edac_device_printk(edac_dev, KERN_EMERG,
659 "UE ctl: %s, instance: %s, block: %s: %s\n",
660 edac_dev->ctl_name, instance->name,
661 block ? block->name : "N/A", msg);
663 if (edac_device_get_panic_on_ue(edac_dev))
664 panic("EDAC %s: UE instance: %s, block %s: %s\n",
665 edac_dev->ctl_name, instance->name,
666 block ? block->name : "N/A", msg);
668 EXPORT_SYMBOL_GPL(edac_device_handle_ue);