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),
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_device_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_device_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_device_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);
367 * edac_device_workq_setup
368 * initialize a workq item for this edac_device instance
369 * passing in the new delay period in msec
371 void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
374 debugf0("%s()\n", __func__);
376 edac_dev->poll_msec = msec;
377 edac_calc_delay(edac_dev); /* Calc delay jiffies */
379 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
380 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
382 INIT_WORK(&edac_dev->work, edac_device_workq_function, edac_dev);
384 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
388 * edac_device_workq_teardown
389 * stop the workq processing on this edac_dev
391 void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
395 status = cancel_delayed_work(&edac_dev->work);
397 /* workq instance might be running, wait for it */
398 flush_workqueue(edac_workqueue);
403 * edac_device_reset_delay_period
406 void edac_device_reset_delay_period(
407 struct edac_device_ctl_info *edac_dev,
412 /* cancel the current workq request */
413 edac_device_workq_teardown(edac_dev);
415 /* restart the workq request, with new delay value */
416 edac_device_workq_setup(edac_dev, value);
418 unlock_device_list();
422 * edac_op_state_toString(edac_dev)
424 static char *edac_op_state_toString(struct edac_device_ctl_info *edac_dev)
426 int opstate = edac_dev->op_state;
428 if (opstate == OP_RUNNING_POLL)
430 else if (opstate == OP_RUNNING_INTERRUPT)
432 else if (opstate == OP_RUNNING_POLL_INTR)
434 else if (opstate == OP_ALLOC)
436 else if (opstate == OP_OFFLINE)
443 * edac_device_add_device: Insert the 'edac_dev' structure into the
444 * edac_device global list and create sysfs entries associated with
445 * edac_device structure.
446 * @edac_device: pointer to the edac_device structure to be added to the list
447 * @edac_idx: A unique numeric identifier to be assigned to the
448 * 'edac_device' structure.
454 int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
456 debugf0("%s()\n", __func__);
458 edac_dev->dev_idx = edac_idx;
459 #ifdef CONFIG_EDAC_DEBUG
460 if (edac_debug_level >= 3)
461 edac_device_dump_device(edac_dev);
465 if (add_edac_dev_to_global_list(edac_dev))
468 /* set load time so that error rate can be tracked */
469 edac_dev->start_time = jiffies;
471 /* create this instance's sysfs entries */
472 if (edac_device_create_sysfs(edac_dev)) {
473 edac_device_printk(edac_dev, KERN_WARNING,
474 "failed to create sysfs device\n");
478 /* If there IS a check routine, then we are running POLLED */
479 if (edac_dev->edac_check != NULL) {
480 /* This instance is NOW RUNNING */
481 edac_dev->op_state = OP_RUNNING_POLL;
484 * enable workq processing on this instance,
485 * default = 1000 msec
487 edac_device_workq_setup(edac_dev, 1000);
489 edac_dev->op_state = OP_RUNNING_INTERRUPT;
493 /* Report action taken */
494 edac_device_printk(edac_dev, KERN_INFO,
495 "Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
499 edac_op_state_toString(edac_dev)
502 unlock_device_list();
506 /* Some error, so remove the entry from the lsit */
507 del_edac_device_from_global_list(edac_dev);
510 unlock_device_list();
513 EXPORT_SYMBOL_GPL(edac_device_add_device);
516 * edac_device_del_device:
517 * Remove sysfs entries for specified edac_device structure and
518 * then remove edac_device structure from global list
521 * Pointer to 'struct device' representing edac_device
522 * structure to remove.
525 * Pointer to removed edac_device structure,
526 * OR NULL if device not found.
528 struct edac_device_ctl_info * edac_device_del_device(struct device *dev)
530 struct edac_device_ctl_info *edac_dev;
532 debugf0("MC: %s()\n", __func__);
536 if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
537 unlock_device_list();
541 /* mark this instance as OFFLINE */
542 edac_dev->op_state = OP_OFFLINE;
544 /* clear workq processing on this instance */
545 edac_device_workq_teardown(edac_dev);
547 /* Tear down the sysfs entries for this instance */
548 edac_device_remove_sysfs(edac_dev);
550 /* deregister from global list */
551 del_edac_device_from_global_list(edac_dev);
553 unlock_device_list();
555 edac_printk(KERN_INFO, EDAC_MC,
556 "Removed device %d for %s %s: DEV %s\n",
564 EXPORT_SYMBOL_GPL(edac_device_del_device);
567 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
569 return edac_dev->log_ce;
572 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
574 return edac_dev->log_ue;
577 static inline int edac_device_get_panic_on_ue(
578 struct edac_device_ctl_info *edac_dev)
580 return edac_dev->panic_on_ue;
584 * edac_device_handle_ce
585 * perform a common output and handling of an 'edac_dev' CE event
587 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
588 int inst_nr, int block_nr, const char *msg)
590 struct edac_device_instance *instance;
591 struct edac_device_block *block = NULL;
593 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
594 edac_device_printk(edac_dev, KERN_ERR,
595 "INTERNAL ERROR: 'instance' out of range "
596 "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
600 instance = edac_dev->instances + inst_nr;
602 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
603 edac_device_printk(edac_dev, KERN_ERR,
604 "INTERNAL ERROR: instance %d 'block' out of range "
605 "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
609 if (instance->nr_blocks > 0) {
610 block = instance->blocks + block_nr;
611 block->counters.ce_count++;
614 /* Propogate the count up the 'totals' tree */
615 instance->counters.ce_count++;
616 edac_dev->counters.ce_count++;
618 if (edac_device_get_log_ce(edac_dev))
619 edac_device_printk(edac_dev, KERN_WARNING,
620 "CE ctl: %s, instance: %s, block: %s: %s\n",
621 edac_dev->ctl_name, instance->name,
622 block ? block->name : "N/A", msg);
624 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
627 * edac_device_handle_ue
628 * perform a common output and handling of an 'edac_dev' UE event
630 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
631 int inst_nr, int block_nr, const char *msg)
633 struct edac_device_instance *instance;
634 struct edac_device_block *block = NULL;
636 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
637 edac_device_printk(edac_dev, KERN_ERR,
638 "INTERNAL ERROR: 'instance' out of range "
639 "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
643 instance = edac_dev->instances + inst_nr;
645 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
646 edac_device_printk(edac_dev, KERN_ERR,
647 "INTERNAL ERROR: instance %d 'block' out of range "
648 "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
652 if (instance->nr_blocks > 0) {
653 block = instance->blocks + block_nr;
654 block->counters.ue_count++;
657 /* Propogate the count up the 'totals' tree */
658 instance->counters.ue_count++;
659 edac_dev->counters.ue_count++;
661 if (edac_device_get_log_ue(edac_dev))
662 edac_device_printk(edac_dev, KERN_EMERG,
663 "UE ctl: %s, instance: %s, block: %s: %s\n",
664 edac_dev->ctl_name, instance->name,
665 block ? block->name : "N/A", msg);
667 if (edac_device_get_panic_on_ue(edac_dev))
668 panic("EDAC %s: UE instance: %s, block %s: %s\n",
669 edac_dev->ctl_name, instance->name,
670 block ? block->name : "N/A", msg);
672 EXPORT_SYMBOL_GPL(edac_device_handle_ue);