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
16 #include <linux/uaccess.h>
17 #include <linux/ctype.h>
18 #include <linux/highmem.h>
19 #include <linux/init.h>
20 #include <linux/jiffies.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <linux/spinlock.h>
25 #include <linux/sysctl.h>
26 #include <linux/timer.h>
28 #include "edac_device.h"
29 #include "edac_module.h"
31 /* lock for the list: 'edac_device_list', manipulation of this list
32 * is protected by the 'device_ctls_mutex' lock
34 static DEFINE_MUTEX(device_ctls_mutex);
35 static LIST_HEAD(edac_device_list);
37 #ifdef CONFIG_EDAC_DEBUG
38 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
40 edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n",
41 edac_dev, edac_dev->dev_idx);
42 edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
43 edac_dbg(3, "\tdev = %p\n", edac_dev->dev);
44 edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
45 edac_dev->mod_name, edac_dev->ctl_name);
46 edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info);
48 #endif /* CONFIG_EDAC_DEBUG */
50 struct edac_device_ctl_info *edac_device_alloc_ctl_info(
52 char *edac_device_name, unsigned nr_instances,
53 char *edac_block_name, unsigned nr_blocks,
54 unsigned offset_value, /* zero, 1, or other based offset */
55 struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
58 struct edac_device_ctl_info *dev_ctl;
59 struct edac_device_instance *dev_inst, *inst;
60 struct edac_device_block *dev_blk, *blk_p, *blk;
61 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
64 unsigned instance, block, attr;
68 edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
70 /* Calculate the size of memory we need to allocate AND
71 * determine the offsets of the various item arrays
72 * (instance,block,attrib) from the start of an allocated structure.
73 * We want the alignment of each item (instance,block,attrib)
74 * to be at least as stringent as what the compiler would
75 * provide if we could simply hardcode everything into a single struct.
78 dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
80 /* Calc the 'end' offset past end of ONE ctl_info structure
81 * which will become the start of the 'instance' array
83 dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
85 /* Calc the 'end' offset past the instance array within the ctl_info
86 * which will become the start of the block array
88 count = nr_instances * nr_blocks;
89 dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
91 /* Calc the 'end' offset past the dev_blk array
92 * which will become the start of the attrib array, if any.
94 /* calc how many nr_attrib we need */
97 dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
99 /* Calc the 'end' offset past the attributes array */
100 pvt = edac_align_ptr(&p, sz_private, 1);
102 /* 'pvt' now points to where the private data area is.
103 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
104 * is baselined at ZERO
106 total_size = ((unsigned long)pvt) + sz_private;
108 /* Allocate the amount of memory for the set of control structures */
109 dev_ctl = kzalloc(total_size, GFP_KERNEL);
113 /* Adjust pointers so they point within the actual memory we
114 * just allocated rather than an imaginary chunk of memory
115 * located at address 0.
116 * 'dev_ctl' points to REAL memory, while the others are
117 * ZERO based and thus need to be adjusted to point within
118 * the allocated memory.
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_dev_sysfs_block_attribute *)
125 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
126 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
128 /* Begin storing the information into the control info structure */
129 dev_ctl->dev_idx = device_index;
130 dev_ctl->nr_instances = nr_instances;
131 dev_ctl->instances = dev_inst;
132 dev_ctl->pvt_info = pvt;
134 /* Default logging of CEs and UEs */
138 /* Name of this edac device */
139 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
141 edac_dbg(4, "edac_dev=%p next after end=%p\n",
142 dev_ctl, pvt + sz_private);
144 /* Initialize every Instance */
145 for (instance = 0; instance < nr_instances; instance++) {
146 inst = &dev_inst[instance];
148 inst->nr_blocks = nr_blocks;
149 blk_p = &dev_blk[instance * nr_blocks];
150 inst->blocks = blk_p;
152 /* name of this instance */
153 snprintf(inst->name, sizeof(inst->name),
154 "%s%u", edac_device_name, instance);
156 /* Initialize every block in each instance */
157 for (block = 0; block < nr_blocks; block++) {
159 blk->instance = inst;
160 snprintf(blk->name, sizeof(blk->name),
161 "%s%d", edac_block_name, block+offset_value);
163 edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
164 instance, inst, block, blk, blk->name);
166 /* if there are NO attributes OR no attribute pointer
167 * then continue on to next block iteration
169 if ((nr_attrib == 0) || (attrib_spec == NULL))
172 /* setup the attribute array for this block */
173 blk->nr_attribs = nr_attrib;
174 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
175 blk->block_attributes = attrib_p;
177 edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
178 blk->block_attributes);
180 /* Initialize every user specified attribute in this
181 * block with the data the caller passed in
182 * Each block gets its own copy of pointers,
183 * and its unique 'value'
185 for (attr = 0; attr < nr_attrib; attr++) {
186 attrib = &attrib_p[attr];
188 /* populate the unique per attrib
189 * with the code pointers and info
191 attrib->attr = attrib_spec[attr].attr;
192 attrib->show = attrib_spec[attr].show;
193 attrib->store = attrib_spec[attr].store;
195 attrib->block = blk; /* up link */
197 edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
198 attrib, attrib->attr.name,
200 attrib_spec[attr].attr.name
206 /* Mark this instance as merely ALLOCATED */
207 dev_ctl->op_state = OP_ALLOC;
210 * Initialize the 'root' kobj for the edac_device controller
212 err = edac_device_register_sysfs_main_kobj(dev_ctl);
218 /* at this point, the root kobj is valid, and in order to
219 * 'free' the object, then the function:
220 * edac_device_unregister_sysfs_main_kobj() must be called
221 * which will perform kobj unregistration and the actual free
222 * will occur during the kobject callback operation
227 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
229 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
231 edac_device_unregister_sysfs_main_kobj(ctl_info);
233 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
236 * find_edac_device_by_dev
237 * scans the edac_device list for a specific 'struct device *'
239 * lock to be held prior to call: device_ctls_mutex
242 * pointer to control structure managing 'dev'
243 * NULL if not found on list
245 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
247 struct edac_device_ctl_info *edac_dev;
248 struct list_head *item;
252 list_for_each(item, &edac_device_list) {
253 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
255 if (edac_dev->dev == dev)
263 * add_edac_dev_to_global_list
264 * Before calling this function, caller must
265 * assign a unique value to edac_dev->dev_idx.
267 * lock to be held prior to call: device_ctls_mutex
273 static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
275 struct list_head *item, *insert_before;
276 struct edac_device_ctl_info *rover;
278 insert_before = &edac_device_list;
280 /* Determine if already on the list */
281 rover = find_edac_device_by_dev(edac_dev->dev);
282 if (unlikely(rover != NULL))
285 /* Insert in ascending order by 'dev_idx', so find position */
286 list_for_each(item, &edac_device_list) {
287 rover = list_entry(item, struct edac_device_ctl_info, link);
289 if (rover->dev_idx >= edac_dev->dev_idx) {
290 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
293 insert_before = item;
298 list_add_tail_rcu(&edac_dev->link, insert_before);
302 edac_printk(KERN_WARNING, EDAC_MC,
303 "%s (%s) %s %s already assigned %d\n",
304 dev_name(rover->dev), edac_dev_name(rover),
305 rover->mod_name, rover->ctl_name, rover->dev_idx);
309 edac_printk(KERN_WARNING, EDAC_MC,
310 "bug in low-level driver: attempt to assign\n"
311 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
317 * del_edac_device_from_global_list
319 static void del_edac_device_from_global_list(struct edac_device_ctl_info
322 list_del_rcu(&edac_device->link);
324 /* these are for safe removal of devices from global list while
325 * NMI handlers may be traversing list
328 INIT_LIST_HEAD(&edac_device->link);
332 * edac_device_workq_function
333 * performs the operation scheduled by a workq request
335 * this workq is embedded within an edac_device_ctl_info
336 * structure, that needs to be polled for possible error events.
338 * This operation is to acquire the list mutex lock
339 * (thus preventing insertation or deletion)
340 * and then call the device's poll function IFF this device is
341 * running polled and there is a poll function defined.
343 static void edac_device_workq_function(struct work_struct *work_req)
345 struct delayed_work *d_work = to_delayed_work(work_req);
346 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
348 mutex_lock(&device_ctls_mutex);
350 /* If we are being removed, bail out immediately */
351 if (edac_dev->op_state == OP_OFFLINE) {
352 mutex_unlock(&device_ctls_mutex);
356 /* Only poll controllers that are running polled and have a check */
357 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
358 (edac_dev->edac_check != NULL)) {
359 edac_dev->edac_check(edac_dev);
362 mutex_unlock(&device_ctls_mutex);
364 /* Reschedule the workq for the next time period to start again
365 * if the number of msec is for 1 sec, then adjust to the next
366 * whole one second to save timers firing all over the period
367 * between integral seconds
369 if (edac_dev->poll_msec == 1000)
370 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
372 edac_queue_work(&edac_dev->work, edac_dev->delay);
376 * edac_device_workq_setup
377 * initialize a workq item for this edac_device instance
378 * passing in the new delay period in msec
380 static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
385 /* take the arg 'msec' and set it into the control structure
386 * to used in the time period calculation
387 * then calc the number of jiffies that represents
389 edac_dev->poll_msec = msec;
390 edac_dev->delay = msecs_to_jiffies(msec);
392 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
394 /* optimize here for the 1 second case, which will be normal value, to
395 * fire ON the 1 second time event. This helps reduce all sorts of
396 * timers firing on sub-second basis, while they are happy
397 * to fire together on the 1 second exactly
399 if (edac_dev->poll_msec == 1000)
400 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
402 edac_queue_work(&edac_dev->work, edac_dev->delay);
406 * edac_device_workq_teardown
407 * stop the workq processing on this edac_dev
409 static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
411 if (!edac_dev->edac_check)
414 edac_dev->op_state = OP_OFFLINE;
416 edac_stop_work(&edac_dev->work);
420 * edac_device_reset_delay_period
422 * need to stop any outstanding workq queued up at this time
423 * because we will be resetting the sleep time.
424 * Then restart the workq on the new delay
426 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
429 unsigned long jiffs = msecs_to_jiffies(value);
432 jiffs = round_jiffies_relative(value);
434 edac_dev->poll_msec = value;
435 edac_dev->delay = jiffs;
437 edac_mod_work(&edac_dev->work, jiffs);
440 int edac_device_alloc_index(void)
442 static atomic_t device_indexes = ATOMIC_INIT(0);
444 return atomic_inc_return(&device_indexes) - 1;
446 EXPORT_SYMBOL_GPL(edac_device_alloc_index);
448 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
452 #ifdef CONFIG_EDAC_DEBUG
453 if (edac_debug_level >= 3)
454 edac_device_dump_device(edac_dev);
456 mutex_lock(&device_ctls_mutex);
458 if (add_edac_dev_to_global_list(edac_dev))
461 /* set load time so that error rate can be tracked */
462 edac_dev->start_time = jiffies;
464 /* create this instance's sysfs entries */
465 if (edac_device_create_sysfs(edac_dev)) {
466 edac_device_printk(edac_dev, KERN_WARNING,
467 "failed to create sysfs device\n");
471 /* If there IS a check routine, then we are running POLLED */
472 if (edac_dev->edac_check != NULL) {
473 /* This instance is NOW RUNNING */
474 edac_dev->op_state = OP_RUNNING_POLL;
477 * enable workq processing on this instance,
478 * default = 1000 msec
480 edac_device_workq_setup(edac_dev, 1000);
482 edac_dev->op_state = OP_RUNNING_INTERRUPT;
485 /* Report action taken */
486 edac_device_printk(edac_dev, KERN_INFO,
487 "Giving out device to module %s controller %s: DEV %s (%s)\n",
488 edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
489 edac_op_state_to_string(edac_dev->op_state));
491 mutex_unlock(&device_ctls_mutex);
495 /* Some error, so remove the entry from the lsit */
496 del_edac_device_from_global_list(edac_dev);
499 mutex_unlock(&device_ctls_mutex);
502 EXPORT_SYMBOL_GPL(edac_device_add_device);
504 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
506 struct edac_device_ctl_info *edac_dev;
510 mutex_lock(&device_ctls_mutex);
512 /* Find the structure on the list, if not there, then leave */
513 edac_dev = find_edac_device_by_dev(dev);
514 if (edac_dev == NULL) {
515 mutex_unlock(&device_ctls_mutex);
519 /* mark this instance as OFFLINE */
520 edac_dev->op_state = OP_OFFLINE;
522 /* deregister from global list */
523 del_edac_device_from_global_list(edac_dev);
525 mutex_unlock(&device_ctls_mutex);
527 /* clear workq processing on this instance */
528 edac_device_workq_teardown(edac_dev);
530 /* Tear down the sysfs entries for this instance */
531 edac_device_remove_sysfs(edac_dev);
533 edac_printk(KERN_INFO, EDAC_MC,
534 "Removed device %d for %s %s: DEV %s\n",
536 edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
540 EXPORT_SYMBOL_GPL(edac_device_del_device);
542 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
544 return edac_dev->log_ce;
547 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
549 return edac_dev->log_ue;
552 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
555 return edac_dev->panic_on_ue;
558 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
559 int inst_nr, int block_nr, const char *msg)
561 struct edac_device_instance *instance;
562 struct edac_device_block *block = NULL;
564 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
565 edac_device_printk(edac_dev, KERN_ERR,
566 "INTERNAL ERROR: 'instance' out of range "
567 "(%d >= %d)\n", inst_nr,
568 edac_dev->nr_instances);
572 instance = edac_dev->instances + inst_nr;
574 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
575 edac_device_printk(edac_dev, KERN_ERR,
576 "INTERNAL ERROR: instance %d 'block' "
577 "out of range (%d >= %d)\n",
579 instance->nr_blocks);
583 if (instance->nr_blocks > 0) {
584 block = instance->blocks + block_nr;
585 block->counters.ce_count++;
588 /* Propagate 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: %s instance: %s block: %s '%s'\n",
595 edac_dev->ctl_name, instance->name,
596 block ? block->name : "N/A", msg);
598 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
600 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
601 int inst_nr, int block_nr, const char *msg)
603 struct edac_device_instance *instance;
604 struct edac_device_block *block = NULL;
606 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
607 edac_device_printk(edac_dev, KERN_ERR,
608 "INTERNAL ERROR: 'instance' out of range "
609 "(%d >= %d)\n", inst_nr,
610 edac_dev->nr_instances);
614 instance = edac_dev->instances + inst_nr;
616 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
617 edac_device_printk(edac_dev, KERN_ERR,
618 "INTERNAL ERROR: instance %d 'block' "
619 "out of range (%d >= %d)\n",
621 instance->nr_blocks);
625 if (instance->nr_blocks > 0) {
626 block = instance->blocks + block_nr;
627 block->counters.ue_count++;
630 /* Propagate the count up the 'totals' tree */
631 instance->counters.ue_count++;
632 edac_dev->counters.ue_count++;
634 if (edac_device_get_log_ue(edac_dev))
635 edac_device_printk(edac_dev, KERN_EMERG,
636 "UE: %s instance: %s block: %s '%s'\n",
637 edac_dev->ctl_name, instance->name,
638 block ? block->name : "N/A", msg);
640 if (edac_device_get_panic_on_ue(edac_dev))
641 panic("EDAC %s: UE instance: %s block %s '%s'\n",
642 edac_dev->ctl_name, instance->name,
643 block ? block->name : "N/A", msg);
645 EXPORT_SYMBOL_GPL(edac_device_handle_ue);