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/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>
32 #include "edac_core.h"
33 #include "edac_module.h"
35 /* lock to memory controller's control array 'edac_device_list' */
36 static DECLARE_MUTEX(device_ctls_mutex);
37 static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
39 #ifdef CONFIG_EDAC_DEBUG
40 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
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);
49 #endif /* CONFIG_EDAC_DEBUG */
52 * edac_device_alloc_ctl_info()
53 * Allocate a new edac device control info structure
55 * The control structure is allocated in complete chunk
56 * from the OS. It is in turn sub allocated to the
57 * various objects that compose the struture
59 * The structure has a 'nr_instance' array within itself.
60 * Each instance represents a major component
61 * Example: L1 cache and L2 cache are 2 instance components
63 * Within each instance is an array of 'nr_blocks' blockoffsets
65 struct edac_device_ctl_info *edac_device_alloc_ctl_info(
67 char *edac_device_name, unsigned nr_instances,
68 char *edac_block_name, unsigned nr_blocks,
69 unsigned offset_value, /* zero, 1, or other based offset */
70 struct edac_attrib_spec *attrib_spec, unsigned nr_attribs)
72 struct edac_device_ctl_info *dev_ctl;
73 struct edac_device_instance *dev_inst, *inst;
74 struct edac_device_block *dev_blk, *blk_p, *blk;
75 struct edac_attrib *dev_attrib, *attrib_p, *attrib;
78 unsigned instance, block, attr;
81 debugf1("%s() instances=%d blocks=%d\n",
82 __func__, nr_instances, nr_blocks);
84 /* Figure out the offsets of the various items from the start of an
85 * ctl_info structure. We want the alignment of each item
86 * to be at least as stringent as what the compiler would
87 * provide if we could simply hardcode everything into a single struct.
89 dev_ctl = (struct edac_device_ctl_info *)NULL;
91 /* Calc the 'end' offset past the ctl_info structure */
92 dev_inst = (struct edac_device_instance *)
93 edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
95 /* Calc the 'end' offset past the instance array */
96 dev_blk = (struct edac_device_block *)
97 edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
99 /* Calc the 'end' offset past the dev_blk array */
100 count = nr_instances * nr_blocks;
101 dev_attrib = (struct edac_attrib *)
102 edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
104 /* Check for case of NO attributes specified */
108 /* Calc the 'end' offset past the attributes array */
109 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
110 total_size = ((unsigned long)pvt) + sz_private;
112 /* Allocate the amount of memory for the set of control structures */
113 dev_ctl = kzalloc(total_size, GFP_KERNEL);
117 /* Adjust pointers so they point within the memory we just allocated
118 * rather than an imaginary chunk of memory located at address 0.
120 dev_inst = (struct edac_device_instance *)
121 (((char *)dev_ctl) + ((unsigned long)dev_inst));
122 dev_blk = (struct edac_device_block *)
123 (((char *)dev_ctl) + ((unsigned long)dev_blk));
124 dev_attrib = (struct edac_attrib *)
125 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
126 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
128 dev_ctl->nr_instances = nr_instances;
129 dev_ctl->instances = dev_inst;
130 dev_ctl->pvt_info = pvt;
132 /* Name of this edac device */
133 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
135 /* Initialize every Instance */
136 for (instance = 0; instance < nr_instances; instance++) {
137 inst = &dev_inst[instance];
139 inst->nr_blocks = nr_blocks;
140 blk_p = &dev_blk[instance * nr_blocks];
141 inst->blocks = blk_p;
143 /* name of this instance */
144 snprintf(inst->name, sizeof(inst->name),
145 "%s%u", edac_device_name, instance);
147 /* Initialize every block in each instance */
148 for (block = 0; block < nr_blocks; block++) {
150 blk->instance = inst;
151 blk->nr_attribs = nr_attribs;
152 attrib_p = &dev_attrib[block * nr_attribs];
153 blk->attribs = attrib_p;
154 snprintf(blk->name, sizeof(blk->name),
155 "%s%d", edac_block_name, block+offset_value);
157 debugf1("%s() instance=%d block=%d name=%s\n",
158 __func__, instance, block, blk->name);
160 if (attrib_spec != NULL) {
161 /* when there is an attrib_spec passed int then
162 * Initialize every attrib of each block
164 for (attr = 0; attr < nr_attribs; attr++) {
165 attrib = &attrib_p[attr];
168 /* Link each attribute to the caller's
169 * spec entry, for name and type
171 attrib->spec = &attrib_spec[attr];
177 /* Mark this instance as merely ALLOCATED */
178 dev_ctl->op_state = OP_ALLOC;
182 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
185 * edac_device_free_ctl_info()
186 * frees the memory allocated by the edac_device_alloc_ctl_info()
189 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
193 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
196 * find_edac_device_by_dev
197 * scans the edac_device list for a specific 'struct device *'
199 * lock to be held prior to call: device_ctls_mutex
202 * pointer to control structure managing 'dev'
203 * NULL if not found on list
205 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
207 struct edac_device_ctl_info *edac_dev;
208 struct list_head *item;
210 debugf3("%s()\n", __func__);
212 list_for_each(item, &edac_device_list) {
213 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
215 if (edac_dev->dev == dev)
223 * add_edac_dev_to_global_list
224 * Before calling this function, caller must
225 * assign a unique value to edac_dev->dev_idx.
227 * lock to be held prior to call: device_ctls_mutex
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 rover = find_edac_device_by_dev(edac_dev->dev);
242 if (unlikely(rover != NULL))
245 /* Insert in ascending order by 'dev_idx', so find position */
246 list_for_each(item, &edac_device_list) {
247 rover = list_entry(item, struct edac_device_ctl_info, link);
249 if (rover->dev_idx >= edac_dev->dev_idx) {
250 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
253 insert_before = item;
258 list_add_tail_rcu(&edac_dev->link, insert_before);
262 edac_printk(KERN_WARNING, EDAC_MC,
263 "%s (%s) %s %s already assigned %d\n",
264 rover->dev->bus_id, dev_name(rover),
265 rover->mod_name, rover->ctl_name, rover->dev_idx);
269 edac_printk(KERN_WARNING, EDAC_MC,
270 "bug in low-level driver: attempt to assign\n"
271 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
277 * complete_edac_device_list_del
279 * callback function when reference count is zero
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 * remove the RCU, setup for a callback call, then wait for the
296 static void del_edac_device_from_global_list(struct edac_device_ctl_info
299 list_del_rcu(&edac_device->link);
300 init_completion(&edac_device->complete);
301 call_rcu(&edac_device->rcu, complete_edac_device_list_del);
302 wait_for_completion(&edac_device->complete);
307 * Search for a edac_device_ctl_info structure whose index is 'idx'.
309 * If found, return a pointer to the structure.
312 * Caller must hold device_ctls_mutex.
314 struct edac_device_ctl_info *edac_device_find(int idx)
316 struct list_head *item;
317 struct edac_device_ctl_info *edac_dev;
319 /* Iterate over list, looking for exact match of ID */
320 list_for_each(item, &edac_device_list) {
321 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
323 if (edac_dev->dev_idx >= idx) {
324 if (edac_dev->dev_idx == idx)
327 /* not on list, so terminate early */
334 EXPORT_SYMBOL_GPL(edac_device_find);
337 * edac_device_workq_function
338 * performs the operation scheduled by a workq request
340 static void edac_device_workq_function(struct work_struct *work_req)
342 struct delayed_work *d_work = (struct delayed_work *)work_req;
343 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
345 //debugf0("%s() here and running\n", __func__);
346 down(&device_ctls_mutex);
348 /* Only poll controllers that are running polled and have a check */
349 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
350 (edac_dev->edac_check != NULL)) {
351 edac_dev->edac_check(edac_dev);
354 up(&device_ctls_mutex);
357 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
361 * edac_device_workq_setup
362 * initialize a workq item for this edac_device instance
363 * passing in the new delay period in msec
365 void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
368 debugf0("%s()\n", __func__);
370 edac_dev->poll_msec = msec;
371 edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
373 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
374 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
378 * edac_device_workq_teardown
379 * stop the workq processing on this edac_dev
381 void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
385 status = cancel_delayed_work(&edac_dev->work);
387 /* workq instance might be running, wait for it */
388 flush_workqueue(edac_workqueue);
393 * edac_device_reset_delay_period
396 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
399 down(&device_ctls_mutex);
401 /* cancel the current workq request */
402 edac_device_workq_teardown(edac_dev);
404 /* restart the workq request, with new delay value */
405 edac_device_workq_setup(edac_dev, value);
407 up(&device_ctls_mutex);
411 * edac_device_add_device: Insert the 'edac_dev' structure into the
412 * edac_device global list and create sysfs entries associated with
413 * edac_device structure.
414 * @edac_device: pointer to the edac_device structure to be added to the list
415 * @edac_idx: A unique numeric identifier to be assigned to the
416 * 'edac_device' structure.
422 int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
424 debugf0("%s()\n", __func__);
426 edac_dev->dev_idx = edac_idx;
427 #ifdef CONFIG_EDAC_DEBUG
428 if (edac_debug_level >= 3)
429 edac_device_dump_device(edac_dev);
431 down(&device_ctls_mutex);
433 if (add_edac_dev_to_global_list(edac_dev))
436 /* set load time so that error rate can be tracked */
437 edac_dev->start_time = jiffies;
439 /* create this instance's sysfs entries */
440 if (edac_device_create_sysfs(edac_dev)) {
441 edac_device_printk(edac_dev, KERN_WARNING,
442 "failed to create sysfs device\n");
446 /* If there IS a check routine, then we are running POLLED */
447 if (edac_dev->edac_check != NULL) {
448 /* This instance is NOW RUNNING */
449 edac_dev->op_state = OP_RUNNING_POLL;
452 * enable workq processing on this instance,
453 * default = 1000 msec
455 edac_device_workq_setup(edac_dev, 1000);
457 edac_dev->op_state = OP_RUNNING_INTERRUPT;
460 /* Report action taken */
461 edac_device_printk(edac_dev, KERN_INFO,
462 "Giving out device to module '%s' controller "
463 "'%s': DEV '%s' (%s)\n",
467 edac_op_state_toString(edac_dev->op_state));
469 up(&device_ctls_mutex);
473 /* Some error, so remove the entry from the lsit */
474 del_edac_device_from_global_list(edac_dev);
477 up(&device_ctls_mutex);
480 EXPORT_SYMBOL_GPL(edac_device_add_device);
483 * edac_device_del_device:
484 * Remove sysfs entries for specified edac_device structure and
485 * then remove edac_device structure from global list
488 * Pointer to 'struct device' representing edac_device
489 * structure to remove.
492 * Pointer to removed edac_device structure,
493 * OR NULL if device not found.
495 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
497 struct edac_device_ctl_info *edac_dev;
499 debugf0("MC: %s()\n", __func__);
501 down(&device_ctls_mutex);
503 /* Find the structure on the list, if not there, then leave */
504 edac_dev = find_edac_device_by_dev(dev);
505 if (edac_dev == NULL) {
506 up(&device_ctls_mutex);
510 /* mark this instance as OFFLINE */
511 edac_dev->op_state = OP_OFFLINE;
513 /* clear workq processing on this instance */
514 edac_device_workq_teardown(edac_dev);
516 /* Tear down the sysfs entries for this instance */
517 edac_device_remove_sysfs(edac_dev);
519 /* deregister from global list */
520 del_edac_device_from_global_list(edac_dev);
522 up(&device_ctls_mutex);
524 edac_printk(KERN_INFO, EDAC_MC,
525 "Removed device %d for %s %s: DEV %s\n",
527 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
531 EXPORT_SYMBOL_GPL(edac_device_del_device);
533 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
535 return edac_dev->log_ce;
538 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
540 return edac_dev->log_ue;
543 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
546 return edac_dev->panic_on_ue;
550 * edac_device_handle_ce
551 * perform a common output and handling of an 'edac_dev' CE event
553 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
554 int inst_nr, int block_nr, const char *msg)
556 struct edac_device_instance *instance;
557 struct edac_device_block *block = NULL;
559 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
560 edac_device_printk(edac_dev, KERN_ERR,
561 "INTERNAL ERROR: 'instance' out of range "
562 "(%d >= %d)\n", inst_nr,
563 edac_dev->nr_instances);
567 instance = edac_dev->instances + inst_nr;
569 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
570 edac_device_printk(edac_dev, KERN_ERR,
571 "INTERNAL ERROR: instance %d 'block' "
572 "out of range (%d >= %d)\n",
574 instance->nr_blocks);
578 if (instance->nr_blocks > 0) {
579 block = instance->blocks + block_nr;
580 block->counters.ce_count++;
583 /* Propogate the count up the 'totals' tree */
584 instance->counters.ce_count++;
585 edac_dev->counters.ce_count++;
587 if (edac_device_get_log_ce(edac_dev))
588 edac_device_printk(edac_dev, KERN_WARNING,
589 "CE: %s instance: %s block: %s '%s'\n",
590 edac_dev->ctl_name, instance->name,
591 block ? block->name : "N/A", msg);
593 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
596 * edac_device_handle_ue
597 * perform a common output and handling of an 'edac_dev' UE event
599 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
600 int inst_nr, int block_nr, const char *msg)
602 struct edac_device_instance *instance;
603 struct edac_device_block *block = NULL;
605 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
606 edac_device_printk(edac_dev, KERN_ERR,
607 "INTERNAL ERROR: 'instance' out of range "
608 "(%d >= %d)\n", inst_nr,
609 edac_dev->nr_instances);
613 instance = edac_dev->instances + inst_nr;
615 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
616 edac_device_printk(edac_dev, KERN_ERR,
617 "INTERNAL ERROR: instance %d 'block' "
618 "out of range (%d >= %d)\n",
620 instance->nr_blocks);
624 if (instance->nr_blocks > 0) {
625 block = instance->blocks + block_nr;
626 block->counters.ue_count++;
629 /* Propogate the count up the 'totals' tree */
630 instance->counters.ue_count++;
631 edac_dev->counters.ue_count++;
633 if (edac_device_get_log_ue(edac_dev))
634 edac_device_printk(edac_dev, KERN_EMERG,
635 "UE: %s instance: %s block: %s '%s'\n",
636 edac_dev->ctl_name, instance->name,
637 block ? block->name : "N/A", msg);
639 if (edac_device_get_panic_on_ue(edac_dev))
640 panic("EDAC %s: UE instance: %s block %s '%s'\n",
641 edac_dev->ctl_name, instance->name,
642 block ? block->name : "N/A", msg);
644 EXPORT_SYMBOL_GPL(edac_device_handle_ue);