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 edac_dev->poll_msec = msec;
400 edac_dev->delay = msecs_to_jiffies(msec);
402 /* See comment in edac_device_workq_setup() above */
403 if (edac_dev->poll_msec == 1000)
404 edac_mod_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
406 edac_mod_work(&edac_dev->work, edac_dev->delay);
409 int edac_device_alloc_index(void)
411 static atomic_t device_indexes = ATOMIC_INIT(0);
413 return atomic_inc_return(&device_indexes) - 1;
415 EXPORT_SYMBOL_GPL(edac_device_alloc_index);
417 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
421 #ifdef CONFIG_EDAC_DEBUG
422 if (edac_debug_level >= 3)
423 edac_device_dump_device(edac_dev);
425 mutex_lock(&device_ctls_mutex);
427 if (add_edac_dev_to_global_list(edac_dev))
430 /* set load time so that error rate can be tracked */
431 edac_dev->start_time = jiffies;
433 /* create this instance's sysfs entries */
434 if (edac_device_create_sysfs(edac_dev)) {
435 edac_device_printk(edac_dev, KERN_WARNING,
436 "failed to create sysfs device\n");
440 /* If there IS a check routine, then we are running POLLED */
441 if (edac_dev->edac_check != NULL) {
442 /* This instance is NOW RUNNING */
443 edac_dev->op_state = OP_RUNNING_POLL;
446 * enable workq processing on this instance,
447 * default = 1000 msec
449 edac_device_workq_setup(edac_dev, 1000);
451 edac_dev->op_state = OP_RUNNING_INTERRUPT;
454 /* Report action taken */
455 edac_device_printk(edac_dev, KERN_INFO,
456 "Giving out device to module %s controller %s: DEV %s (%s)\n",
457 edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
458 edac_op_state_to_string(edac_dev->op_state));
460 mutex_unlock(&device_ctls_mutex);
464 /* Some error, so remove the entry from the lsit */
465 del_edac_device_from_global_list(edac_dev);
468 mutex_unlock(&device_ctls_mutex);
471 EXPORT_SYMBOL_GPL(edac_device_add_device);
473 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
475 struct edac_device_ctl_info *edac_dev;
479 mutex_lock(&device_ctls_mutex);
481 /* Find the structure on the list, if not there, then leave */
482 edac_dev = find_edac_device_by_dev(dev);
483 if (edac_dev == NULL) {
484 mutex_unlock(&device_ctls_mutex);
488 /* mark this instance as OFFLINE */
489 edac_dev->op_state = OP_OFFLINE;
491 /* deregister from global list */
492 del_edac_device_from_global_list(edac_dev);
494 mutex_unlock(&device_ctls_mutex);
496 /* clear workq processing on this instance */
497 edac_device_workq_teardown(edac_dev);
499 /* Tear down the sysfs entries for this instance */
500 edac_device_remove_sysfs(edac_dev);
502 edac_printk(KERN_INFO, EDAC_MC,
503 "Removed device %d for %s %s: DEV %s\n",
505 edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
509 EXPORT_SYMBOL_GPL(edac_device_del_device);
511 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
513 return edac_dev->log_ce;
516 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
518 return edac_dev->log_ue;
521 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
524 return edac_dev->panic_on_ue;
527 void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
528 unsigned int count, int inst_nr, int block_nr,
531 struct edac_device_instance *instance;
532 struct edac_device_block *block = NULL;
537 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
538 edac_device_printk(edac_dev, KERN_ERR,
539 "INTERNAL ERROR: 'instance' out of range "
540 "(%d >= %d)\n", inst_nr,
541 edac_dev->nr_instances);
545 instance = edac_dev->instances + inst_nr;
547 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
548 edac_device_printk(edac_dev, KERN_ERR,
549 "INTERNAL ERROR: instance %d 'block' "
550 "out of range (%d >= %d)\n",
552 instance->nr_blocks);
556 if (instance->nr_blocks > 0) {
557 block = instance->blocks + block_nr;
558 block->counters.ce_count += count;
561 /* Propagate the count up the 'totals' tree */
562 instance->counters.ce_count += count;
563 edac_dev->counters.ce_count += count;
565 if (edac_device_get_log_ce(edac_dev))
566 edac_device_printk(edac_dev, KERN_WARNING,
567 "CE: %s instance: %s block: %s count: %d '%s'\n",
568 edac_dev->ctl_name, instance->name,
569 block ? block->name : "N/A", count, msg);
571 EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
573 void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
574 unsigned int count, int inst_nr, int block_nr,
577 struct edac_device_instance *instance;
578 struct edac_device_block *block = NULL;
583 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
584 edac_device_printk(edac_dev, KERN_ERR,
585 "INTERNAL ERROR: 'instance' out of range "
586 "(%d >= %d)\n", inst_nr,
587 edac_dev->nr_instances);
591 instance = edac_dev->instances + inst_nr;
593 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
594 edac_device_printk(edac_dev, KERN_ERR,
595 "INTERNAL ERROR: instance %d 'block' "
596 "out of range (%d >= %d)\n",
598 instance->nr_blocks);
602 if (instance->nr_blocks > 0) {
603 block = instance->blocks + block_nr;
604 block->counters.ue_count += count;
607 /* Propagate the count up the 'totals' tree */
608 instance->counters.ue_count += count;
609 edac_dev->counters.ue_count += count;
611 if (edac_device_get_log_ue(edac_dev))
612 edac_device_printk(edac_dev, KERN_EMERG,
613 "UE: %s instance: %s block: %s count: %d '%s'\n",
614 edac_dev->ctl_name, instance->name,
615 block ? block->name : "N/A", count, msg);
617 if (edac_device_get_panic_on_ue(edac_dev))
618 panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
619 edac_dev->ctl_name, instance->name,
620 block ? block->name : "N/A", count, msg);
622 EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);