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);
38 static inline void lock_device_list(void)
40 down(&device_ctls_mutex);
43 static inline void unlock_device_list(void)
45 up(&device_ctls_mutex);
48 #ifdef CONFIG_EDAC_DEBUG
49 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
51 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
52 debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
53 debugf3("\tdev = %p\n", edac_dev->dev);
54 debugf3("\tmod_name:ctl_name = %s:%s\n",
55 edac_dev->mod_name, edac_dev->ctl_name);
56 debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
58 #endif /* CONFIG_EDAC_DEBUG */
61 * The alloc() and free() functions for the 'edac_device' control info
62 * structure. A MC driver will allocate one of these for each edac_device
63 * it is going to control/register with the EDAC CORE.
65 struct edac_device_ctl_info *edac_device_alloc_ctl_info(
67 char *edac_device_name,
68 unsigned nr_instances,
69 char *edac_block_name,
71 unsigned offset_value,
72 struct edac_attrib_spec
76 struct edac_device_ctl_info *dev_ctl;
77 struct edac_device_instance *dev_inst, *inst;
78 struct edac_device_block *dev_blk, *blk_p, *blk;
79 struct edac_attrib *dev_attrib, *attrib_p, *attrib;
82 unsigned instance, block, attr;
85 debugf1("%s() instances=%d blocks=%d\n",
86 __func__, nr_instances, nr_blocks);
88 /* Figure out the offsets of the various items from the start of an
89 * ctl_info structure. We want the alignment of each item
90 * to be at least as stringent as what the compiler would
91 * provide if we could simply hardcode everything into a single struct.
93 dev_ctl = (struct edac_device_ctl_info *)0;
95 /* Calc the 'end' offset past the ctl_info structure */
96 dev_inst = (struct edac_device_instance *)
97 edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
99 /* Calc the 'end' offset past the instance array */
100 dev_blk = (struct edac_device_block *)
101 edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
103 /* Calc the 'end' offset past the dev_blk array */
104 count = nr_instances * nr_blocks;
105 dev_attrib = (struct edac_attrib *)
106 edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
108 /* Check for case of NO attributes specified */
112 /* Calc the 'end' offset past the attributes array */
113 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
114 total_size = ((unsigned long)pvt) + sz_private;
116 /* Allocate the amount of memory for the set of control structures */
117 if ((dev_ctl = kmalloc(total_size, GFP_KERNEL)) == NULL)
120 /* Adjust pointers so they point within the memory we just allocated
121 * rather than an imaginary chunk of memory located at address 0.
123 dev_inst = (struct edac_device_instance *)
124 (((char *)dev_ctl) + ((unsigned long)dev_inst));
125 dev_blk = (struct edac_device_block *)
126 (((char *)dev_ctl) + ((unsigned long)dev_blk));
127 dev_attrib = (struct edac_attrib *)
128 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
129 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
131 memset(dev_ctl, 0, total_size); /* clear all fields */
132 dev_ctl->nr_instances = nr_instances;
133 dev_ctl->instances = dev_inst;
134 dev_ctl->pvt_info = pvt;
136 /* Name of this edac device, ensure null terminated */
137 snprintf(dev_ctl->name, sizeof(dev_ctl->name), "%s", edac_device_name);
138 dev_ctl->name[sizeof(dev_ctl->name) - 1] = '\0';
140 /* Initialize every Instance */
141 for (instance = 0; instance < nr_instances; instance++) {
142 inst = &dev_inst[instance];
144 inst->nr_blocks = nr_blocks;
145 blk_p = &dev_blk[instance * nr_blocks];
146 inst->blocks = blk_p;
148 /* name of this instance */
149 snprintf(inst->name, sizeof(inst->name),
150 "%s%u", edac_device_name, instance);
151 inst->name[sizeof(inst->name) - 1] = '\0';
153 /* Initialize every block in each instance */
154 for (block = 0; block < nr_blocks; block++) {
156 blk->instance = inst;
157 blk->nr_attribs = nr_attribs;
158 attrib_p = &dev_attrib[block * nr_attribs];
159 blk->attribs = attrib_p;
160 snprintf(blk->name, sizeof(blk->name),
161 "%s%d", edac_block_name, block + 1);
162 blk->name[sizeof(blk->name) - 1] = '\0';
164 debugf1("%s() instance=%d block=%d name=%s\n",
165 __func__, instance, block, blk->name);
167 if (attrib_spec != NULL) {
168 /* when there is an attrib_spec passed int then
169 * Initialize every attrib of each block
171 for (attr = 0; attr < nr_attribs; attr++) {
172 attrib = &attrib_p[attr];
175 /* Link each attribute to the caller's
176 * spec entry, for name and type
178 attrib->spec = &attrib_spec[attr];
184 /* Mark this instance as merely ALLOCATED */
185 dev_ctl->op_state = OP_ALLOC;
190 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
193 * edac_device_free_ctl_info()
194 * frees the memory allocated by the edac_device_alloc_ctl_info()
197 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
202 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
205 * find_edac_device_by_dev
206 * scans the edac_device list for a specific 'struct device *'
208 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
210 struct edac_device_ctl_info *edac_dev;
211 struct list_head *item;
213 debugf3("%s()\n", __func__);
215 list_for_each(item, &edac_device_list) {
216 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
218 if (edac_dev->dev == dev)
226 * add_edac_dev_to_global_list
227 * Before calling this function, caller must
228 * assign a unique value to edac_dev->dev_idx.
233 static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
235 struct list_head *item, *insert_before;
236 struct edac_device_ctl_info *rover;
238 insert_before = &edac_device_list;
240 /* Determine if already on the list */
241 if (unlikely((rover = find_edac_device_by_dev(edac_dev->dev)) != NULL))
244 /* Insert in ascending order by 'dev_idx', so find position */
245 list_for_each(item, &edac_device_list) {
246 rover = list_entry(item, struct edac_device_ctl_info, link);
248 if (rover->dev_idx >= edac_dev->dev_idx) {
249 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
252 insert_before = item;
257 list_add_tail_rcu(&edac_dev->link, insert_before);
261 edac_printk(KERN_WARNING, EDAC_MC,
262 "%s (%s) %s %s already assigned %d\n",
263 rover->dev->bus_id, dev_name(rover),
264 rover->mod_name, rover->ctl_name, rover->dev_idx);
268 edac_printk(KERN_WARNING, EDAC_MC,
269 "bug in low-level driver: attempt to assign\n"
270 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
276 * complete_edac_device_list_del
278 static void complete_edac_device_list_del(struct rcu_head *head)
280 struct edac_device_ctl_info *edac_dev;
282 edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
283 INIT_LIST_HEAD(&edac_dev->link);
284 complete(&edac_dev->complete);
288 * del_edac_device_from_global_list
290 static void del_edac_device_from_global_list(struct edac_device_ctl_info
293 list_del_rcu(&edac_device->link);
294 init_completion(&edac_device->complete);
295 call_rcu(&edac_device->rcu, complete_edac_device_list_del);
296 wait_for_completion(&edac_device->complete);
301 * Search for a edac_device_ctl_info structure whose index is 'idx'.
303 * If found, return a pointer to the structure.
306 * Caller must hold device_ctls_mutex.
308 struct edac_device_ctl_info *edac_device_find(int idx)
310 struct list_head *item;
311 struct edac_device_ctl_info *edac_dev;
313 /* Iterate over list, looking for exact match of ID */
314 list_for_each(item, &edac_device_list) {
315 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
317 if (edac_dev->dev_idx >= idx) {
318 if (edac_dev->dev_idx == idx)
321 /* not on list, so terminate early */
329 EXPORT_SYMBOL(edac_device_find);
332 * edac_device_workq_function
333 * performs the operation scheduled by a workq request
335 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
336 static void edac_device_workq_function(struct work_struct *work_req)
338 struct delayed_work *d_work = (struct delayed_work *)work_req;
339 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
341 static void edac_device_workq_function(void *ptr)
343 struct edac_device_ctl_info *edac_dev =
344 (struct edac_device_ctl_info *)ptr;
347 //debugf0("%s() here and running\n", __func__);
350 /* Only poll controllers that are running polled and have a check */
351 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
352 (edac_dev->edac_check != NULL)) {
353 edac_dev->edac_check(edac_dev);
356 unlock_device_list();
359 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
363 * edac_device_workq_setup
364 * initialize a workq item for this edac_device instance
365 * passing in the new delay period in msec
367 void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
370 debugf0("%s()\n", __func__);
372 edac_dev->poll_msec = msec;
373 edac_calc_delay(edac_dev); /* Calc delay jiffies */
375 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
376 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
378 INIT_WORK(&edac_dev->work, edac_device_workq_function, edac_dev);
380 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
384 * edac_device_workq_teardown
385 * stop the workq processing on this edac_dev
387 void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
391 status = cancel_delayed_work(&edac_dev->work);
393 /* workq instance might be running, wait for it */
394 flush_workqueue(edac_workqueue);
399 * edac_device_reset_delay_period
402 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
407 /* cancel the current workq request */
408 edac_device_workq_teardown(edac_dev);
410 /* restart the workq request, with new delay value */
411 edac_device_workq_setup(edac_dev, value);
413 unlock_device_list();
417 * edac_device_add_device: Insert the 'edac_dev' structure into the
418 * edac_device global list and create sysfs entries associated with
419 * edac_device structure.
420 * @edac_device: pointer to the edac_device structure to be added to the list
421 * @edac_idx: A unique numeric identifier to be assigned to the
422 * 'edac_device' structure.
428 int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
430 debugf0("%s()\n", __func__);
432 edac_dev->dev_idx = edac_idx;
433 #ifdef CONFIG_EDAC_DEBUG
434 if (edac_debug_level >= 3)
435 edac_device_dump_device(edac_dev);
439 if (add_edac_dev_to_global_list(edac_dev))
442 /* set load time so that error rate can be tracked */
443 edac_dev->start_time = jiffies;
445 /* create this instance's sysfs entries */
446 if (edac_device_create_sysfs(edac_dev)) {
447 edac_device_printk(edac_dev, KERN_WARNING,
448 "failed to create sysfs device\n");
452 /* If there IS a check routine, then we are running POLLED */
453 if (edac_dev->edac_check != NULL) {
454 /* This instance is NOW RUNNING */
455 edac_dev->op_state = OP_RUNNING_POLL;
458 * enable workq processing on this instance,
459 * default = 1000 msec
461 edac_device_workq_setup(edac_dev, 1000);
463 edac_dev->op_state = OP_RUNNING_INTERRUPT;
466 /* Report action taken */
467 edac_device_printk(edac_dev, KERN_INFO,
468 "Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
472 edac_op_state_toString(edac_dev->op_state)
475 unlock_device_list();
479 /* Some error, so remove the entry from the lsit */
480 del_edac_device_from_global_list(edac_dev);
483 unlock_device_list();
487 EXPORT_SYMBOL_GPL(edac_device_add_device);
490 * edac_device_del_device:
491 * Remove sysfs entries for specified edac_device structure and
492 * then remove edac_device structure from global list
495 * Pointer to 'struct device' representing edac_device
496 * structure to remove.
499 * Pointer to removed edac_device structure,
500 * OR NULL if device not found.
502 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
504 struct edac_device_ctl_info *edac_dev;
506 debugf0("MC: %s()\n", __func__);
510 if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
511 unlock_device_list();
515 /* mark this instance as OFFLINE */
516 edac_dev->op_state = OP_OFFLINE;
518 /* clear workq processing on this instance */
519 edac_device_workq_teardown(edac_dev);
521 /* Tear down the sysfs entries for this instance */
522 edac_device_remove_sysfs(edac_dev);
524 /* deregister from global list */
525 del_edac_device_from_global_list(edac_dev);
527 unlock_device_list();
529 edac_printk(KERN_INFO, EDAC_MC,
530 "Removed device %d for %s %s: DEV %s\n",
532 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
537 EXPORT_SYMBOL_GPL(edac_device_del_device);
539 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
541 return edac_dev->log_ce;
544 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
546 return edac_dev->log_ue;
549 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
552 return edac_dev->panic_on_ue;
556 * edac_device_handle_ce
557 * perform a common output and handling of an 'edac_dev' CE event
559 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
560 int inst_nr, int block_nr, const char *msg)
562 struct edac_device_instance *instance;
563 struct edac_device_block *block = NULL;
565 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
566 edac_device_printk(edac_dev, KERN_ERR,
567 "INTERNAL ERROR: 'instance' out of range "
568 "(%d >= %d)\n", inst_nr,
569 edac_dev->nr_instances);
573 instance = edac_dev->instances + inst_nr;
575 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
576 edac_device_printk(edac_dev, KERN_ERR,
577 "INTERNAL ERROR: instance %d 'block' out of range "
578 "(%d >= %d)\n", inst_nr, block_nr,
579 instance->nr_blocks);
583 if (instance->nr_blocks > 0) {
584 block = instance->blocks + block_nr;
585 block->counters.ce_count++;
588 /* Propogate the count up the 'totals' tree */
589 instance->counters.ce_count++;
590 edac_dev->counters.ce_count++;
592 if (edac_device_get_log_ce(edac_dev))
593 edac_device_printk(edac_dev, KERN_WARNING,
594 "CE ctl: %s, instance: %s, block: %s: %s\n",
595 edac_dev->ctl_name, instance->name,
596 block ? block->name : "N/A", msg);
599 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
602 * edac_device_handle_ue
603 * perform a common output and handling of an 'edac_dev' UE event
605 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
606 int inst_nr, int block_nr, const char *msg)
608 struct edac_device_instance *instance;
609 struct edac_device_block *block = NULL;
611 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
612 edac_device_printk(edac_dev, KERN_ERR,
613 "INTERNAL ERROR: 'instance' out of range "
614 "(%d >= %d)\n", inst_nr,
615 edac_dev->nr_instances);
619 instance = edac_dev->instances + inst_nr;
621 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
622 edac_device_printk(edac_dev, KERN_ERR,
623 "INTERNAL ERROR: instance %d 'block' out of range "
624 "(%d >= %d)\n", inst_nr, block_nr,
625 instance->nr_blocks);
629 if (instance->nr_blocks > 0) {
630 block = instance->blocks + block_nr;
631 block->counters.ue_count++;
634 /* Propogate the count up the 'totals' tree */
635 instance->counters.ue_count++;
636 edac_dev->counters.ue_count++;
638 if (edac_device_get_log_ue(edac_dev))
639 edac_device_printk(edac_dev, KERN_EMERG,
640 "UE ctl: %s, instance: %s, block: %s: %s\n",
641 edac_dev->ctl_name, instance->name,
642 block ? block->name : "N/A", msg);
644 if (edac_device_get_panic_on_ue(edac_dev))
645 panic("EDAC %s: UE instance: %s, block %s: %s\n",
646 edac_dev->ctl_name, instance->name,
647 block ? block->name : "N/A", msg);
650 EXPORT_SYMBOL_GPL(edac_device_handle_ue);