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 */
51 * @off_val: zero, 1, or other based offset
53 struct edac_device_ctl_info *
54 edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instances,
55 char *blk_name, unsigned nr_blocks, unsigned off_val,
56 struct edac_dev_sysfs_block_attribute *attrib_spec,
57 unsigned nr_attrib, int device_index)
59 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
60 struct edac_device_block *dev_blk, *blk_p, *blk;
61 struct edac_device_instance *dev_inst, *inst;
62 struct edac_device_ctl_info *dev_ctl;
63 unsigned instance, block, attr;
67 edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
69 dev_ctl = kzalloc(sizeof(struct edac_device_ctl_info), GFP_KERNEL);
73 dev_inst = kcalloc(nr_instances, sizeof(struct edac_device_instance), GFP_KERNEL);
77 dev_ctl->instances = dev_inst;
79 dev_blk = kcalloc(nr_instances * nr_blocks, sizeof(struct edac_device_block), GFP_KERNEL);
83 dev_ctl->blocks = dev_blk;
86 dev_attrib = kcalloc(nr_attrib, sizeof(struct edac_dev_sysfs_block_attribute),
91 dev_ctl->attribs = dev_attrib;
95 pvt = kzalloc(pvt_sz, GFP_KERNEL);
99 dev_ctl->pvt_info = pvt;
102 dev_ctl->dev_idx = device_index;
103 dev_ctl->nr_instances = nr_instances;
105 /* Default logging of CEs and UEs */
109 /* Name of this edac device */
110 snprintf(dev_ctl->name, sizeof(dev_ctl->name),"%s", dev_name);
112 /* Initialize every Instance */
113 for (instance = 0; instance < nr_instances; instance++) {
114 inst = &dev_inst[instance];
116 inst->nr_blocks = nr_blocks;
117 blk_p = &dev_blk[instance * nr_blocks];
118 inst->blocks = blk_p;
120 /* name of this instance */
121 snprintf(inst->name, sizeof(inst->name), "%s%u", dev_name, instance);
123 /* Initialize every block in each instance */
124 for (block = 0; block < nr_blocks; block++) {
126 blk->instance = inst;
127 snprintf(blk->name, sizeof(blk->name),
128 "%s%d", blk_name, block + off_val);
130 edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
131 instance, inst, block, blk, blk->name);
133 /* if there are NO attributes OR no attribute pointer
134 * then continue on to next block iteration
136 if ((nr_attrib == 0) || (attrib_spec == NULL))
139 /* setup the attribute array for this block */
140 blk->nr_attribs = nr_attrib;
141 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
142 blk->block_attributes = attrib_p;
144 edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
145 blk->block_attributes);
147 /* Initialize every user specified attribute in this
148 * block with the data the caller passed in
149 * Each block gets its own copy of pointers,
150 * and its unique 'value'
152 for (attr = 0; attr < nr_attrib; attr++) {
153 attrib = &attrib_p[attr];
155 /* populate the unique per attrib
156 * with the code pointers and info
158 attrib->attr = attrib_spec[attr].attr;
159 attrib->show = attrib_spec[attr].show;
160 attrib->store = attrib_spec[attr].store;
162 attrib->block = blk; /* up link */
164 edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
165 attrib, attrib->attr.name,
167 attrib_spec[attr].attr.name
173 /* Mark this instance as merely ALLOCATED */
174 dev_ctl->op_state = OP_ALLOC;
177 * Initialize the 'root' kobj for the edac_device controller
179 err = edac_device_register_sysfs_main_kobj(dev_ctl);
183 /* at this point, the root kobj is valid, and in order to
184 * 'free' the object, then the function:
185 * edac_device_unregister_sysfs_main_kobj() must be called
186 * which will perform kobj unregistration and the actual free
187 * will occur during the kobject callback operation
193 __edac_device_free_ctl_info(dev_ctl);
197 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
199 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
201 edac_device_unregister_sysfs_main_kobj(ctl_info);
203 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
206 * find_edac_device_by_dev
207 * scans the edac_device list for a specific 'struct device *'
209 * lock to be held prior to call: device_ctls_mutex
212 * pointer to control structure managing 'dev'
213 * NULL if not found on list
215 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
217 struct edac_device_ctl_info *edac_dev;
218 struct list_head *item;
222 list_for_each(item, &edac_device_list) {
223 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
225 if (edac_dev->dev == dev)
233 * add_edac_dev_to_global_list
234 * Before calling this function, caller must
235 * assign a unique value to edac_dev->dev_idx.
237 * lock to be held prior to call: device_ctls_mutex
243 static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
245 struct list_head *item, *insert_before;
246 struct edac_device_ctl_info *rover;
248 insert_before = &edac_device_list;
250 /* Determine if already on the list */
251 rover = find_edac_device_by_dev(edac_dev->dev);
252 if (unlikely(rover != NULL))
255 /* Insert in ascending order by 'dev_idx', so find position */
256 list_for_each(item, &edac_device_list) {
257 rover = list_entry(item, struct edac_device_ctl_info, link);
259 if (rover->dev_idx >= edac_dev->dev_idx) {
260 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
263 insert_before = item;
268 list_add_tail_rcu(&edac_dev->link, insert_before);
272 edac_printk(KERN_WARNING, EDAC_MC,
273 "%s (%s) %s %s already assigned %d\n",
274 dev_name(rover->dev), edac_dev_name(rover),
275 rover->mod_name, rover->ctl_name, rover->dev_idx);
279 edac_printk(KERN_WARNING, EDAC_MC,
280 "bug in low-level driver: attempt to assign\n"
281 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
287 * del_edac_device_from_global_list
289 static void del_edac_device_from_global_list(struct edac_device_ctl_info
292 list_del_rcu(&edac_device->link);
294 /* these are for safe removal of devices from global list while
295 * NMI handlers may be traversing list
298 INIT_LIST_HEAD(&edac_device->link);
302 * edac_device_workq_function
303 * performs the operation scheduled by a workq request
305 * this workq is embedded within an edac_device_ctl_info
306 * structure, that needs to be polled for possible error events.
308 * This operation is to acquire the list mutex lock
309 * (thus preventing insertation or deletion)
310 * and then call the device's poll function IFF this device is
311 * running polled and there is a poll function defined.
313 static void edac_device_workq_function(struct work_struct *work_req)
315 struct delayed_work *d_work = to_delayed_work(work_req);
316 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
318 mutex_lock(&device_ctls_mutex);
320 /* If we are being removed, bail out immediately */
321 if (edac_dev->op_state == OP_OFFLINE) {
322 mutex_unlock(&device_ctls_mutex);
326 /* Only poll controllers that are running polled and have a check */
327 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
328 (edac_dev->edac_check != NULL)) {
329 edac_dev->edac_check(edac_dev);
332 mutex_unlock(&device_ctls_mutex);
334 /* Reschedule the workq for the next time period to start again
335 * if the number of msec is for 1 sec, then adjust to the next
336 * whole one second to save timers firing all over the period
337 * between integral seconds
339 if (edac_dev->poll_msec == 1000)
340 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
342 edac_queue_work(&edac_dev->work, edac_dev->delay);
346 * edac_device_workq_setup
347 * initialize a workq item for this edac_device instance
348 * passing in the new delay period in msec
350 static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
355 /* take the arg 'msec' and set it into the control structure
356 * to used in the time period calculation
357 * then calc the number of jiffies that represents
359 edac_dev->poll_msec = msec;
360 edac_dev->delay = msecs_to_jiffies(msec);
362 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
364 /* optimize here for the 1 second case, which will be normal value, to
365 * fire ON the 1 second time event. This helps reduce all sorts of
366 * timers firing on sub-second basis, while they are happy
367 * to fire together on the 1 second exactly
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_teardown
377 * stop the workq processing on this edac_dev
379 static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
381 if (!edac_dev->edac_check)
384 edac_dev->op_state = OP_OFFLINE;
386 edac_stop_work(&edac_dev->work);
390 * edac_device_reset_delay_period
392 * need to stop any outstanding workq queued up at this time
393 * because we will be resetting the sleep time.
394 * Then restart the workq on the new delay
396 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
399 unsigned long jiffs = msecs_to_jiffies(value);
402 jiffs = round_jiffies_relative(value);
404 edac_dev->poll_msec = value;
405 edac_dev->delay = jiffs;
407 edac_mod_work(&edac_dev->work, jiffs);
410 int edac_device_alloc_index(void)
412 static atomic_t device_indexes = ATOMIC_INIT(0);
414 return atomic_inc_return(&device_indexes) - 1;
416 EXPORT_SYMBOL_GPL(edac_device_alloc_index);
418 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
422 #ifdef CONFIG_EDAC_DEBUG
423 if (edac_debug_level >= 3)
424 edac_device_dump_device(edac_dev);
426 mutex_lock(&device_ctls_mutex);
428 if (add_edac_dev_to_global_list(edac_dev))
431 /* set load time so that error rate can be tracked */
432 edac_dev->start_time = jiffies;
434 /* create this instance's sysfs entries */
435 if (edac_device_create_sysfs(edac_dev)) {
436 edac_device_printk(edac_dev, KERN_WARNING,
437 "failed to create sysfs device\n");
441 /* If there IS a check routine, then we are running POLLED */
442 if (edac_dev->edac_check != NULL) {
443 /* This instance is NOW RUNNING */
444 edac_dev->op_state = OP_RUNNING_POLL;
447 * enable workq processing on this instance,
448 * default = 1000 msec
450 edac_device_workq_setup(edac_dev, 1000);
452 edac_dev->op_state = OP_RUNNING_INTERRUPT;
455 /* Report action taken */
456 edac_device_printk(edac_dev, KERN_INFO,
457 "Giving out device to module %s controller %s: DEV %s (%s)\n",
458 edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
459 edac_op_state_to_string(edac_dev->op_state));
461 mutex_unlock(&device_ctls_mutex);
465 /* Some error, so remove the entry from the lsit */
466 del_edac_device_from_global_list(edac_dev);
469 mutex_unlock(&device_ctls_mutex);
472 EXPORT_SYMBOL_GPL(edac_device_add_device);
474 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
476 struct edac_device_ctl_info *edac_dev;
480 mutex_lock(&device_ctls_mutex);
482 /* Find the structure on the list, if not there, then leave */
483 edac_dev = find_edac_device_by_dev(dev);
484 if (edac_dev == NULL) {
485 mutex_unlock(&device_ctls_mutex);
489 /* mark this instance as OFFLINE */
490 edac_dev->op_state = OP_OFFLINE;
492 /* deregister from global list */
493 del_edac_device_from_global_list(edac_dev);
495 mutex_unlock(&device_ctls_mutex);
497 /* clear workq processing on this instance */
498 edac_device_workq_teardown(edac_dev);
500 /* Tear down the sysfs entries for this instance */
501 edac_device_remove_sysfs(edac_dev);
503 edac_printk(KERN_INFO, EDAC_MC,
504 "Removed device %d for %s %s: DEV %s\n",
506 edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
510 EXPORT_SYMBOL_GPL(edac_device_del_device);
512 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
514 return edac_dev->log_ce;
517 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
519 return edac_dev->log_ue;
522 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
525 return edac_dev->panic_on_ue;
528 void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
529 unsigned int count, int inst_nr, int block_nr,
532 struct edac_device_instance *instance;
533 struct edac_device_block *block = NULL;
538 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
539 edac_device_printk(edac_dev, KERN_ERR,
540 "INTERNAL ERROR: 'instance' out of range "
541 "(%d >= %d)\n", inst_nr,
542 edac_dev->nr_instances);
546 instance = edac_dev->instances + inst_nr;
548 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
549 edac_device_printk(edac_dev, KERN_ERR,
550 "INTERNAL ERROR: instance %d 'block' "
551 "out of range (%d >= %d)\n",
553 instance->nr_blocks);
557 if (instance->nr_blocks > 0) {
558 block = instance->blocks + block_nr;
559 block->counters.ce_count += count;
562 /* Propagate the count up the 'totals' tree */
563 instance->counters.ce_count += count;
564 edac_dev->counters.ce_count += count;
566 if (edac_device_get_log_ce(edac_dev))
567 edac_device_printk(edac_dev, KERN_WARNING,
568 "CE: %s instance: %s block: %s count: %d '%s'\n",
569 edac_dev->ctl_name, instance->name,
570 block ? block->name : "N/A", count, msg);
572 EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
574 void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
575 unsigned int count, int inst_nr, int block_nr,
578 struct edac_device_instance *instance;
579 struct edac_device_block *block = NULL;
584 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
585 edac_device_printk(edac_dev, KERN_ERR,
586 "INTERNAL ERROR: 'instance' out of range "
587 "(%d >= %d)\n", inst_nr,
588 edac_dev->nr_instances);
592 instance = edac_dev->instances + inst_nr;
594 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
595 edac_device_printk(edac_dev, KERN_ERR,
596 "INTERNAL ERROR: instance %d 'block' "
597 "out of range (%d >= %d)\n",
599 instance->nr_blocks);
603 if (instance->nr_blocks > 0) {
604 block = instance->blocks + block_nr;
605 block->counters.ue_count += count;
608 /* Propagate the count up the 'totals' tree */
609 instance->counters.ue_count += count;
610 edac_dev->counters.ue_count += count;
612 if (edac_device_get_log_ue(edac_dev))
613 edac_device_printk(edac_dev, KERN_EMERG,
614 "UE: %s instance: %s block: %s count: %d '%s'\n",
615 edac_dev->ctl_name, instance->name,
616 block ? block->name : "N/A", count, msg);
618 if (edac_device_get_panic_on_ue(edac_dev))
619 panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
620 edac_dev->ctl_name, instance->name,
621 block ? block->name : "N/A", count, msg);
623 EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);