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 <asm/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 */
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 structure
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_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
73 struct edac_device_ctl_info *dev_ctl;
74 struct edac_device_instance *dev_inst, *inst;
75 struct edac_device_block *dev_blk, *blk_p, *blk;
76 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
79 unsigned instance, block, attr;
83 edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
85 /* Calculate the size of memory we need to allocate AND
86 * determine the offsets of the various item arrays
87 * (instance,block,attrib) from the start of an allocated structure.
88 * We want the alignment of each item (instance,block,attrib)
89 * to be at least as stringent as what the compiler would
90 * provide if we could simply hardcode everything into a single struct.
93 dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
95 /* Calc the 'end' offset past end of ONE ctl_info structure
96 * which will become the start of the 'instance' array
98 dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
100 /* Calc the 'end' offset past the instance array within the ctl_info
101 * which will become the start of the block array
103 count = nr_instances * nr_blocks;
104 dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
106 /* Calc the 'end' offset past the dev_blk array
107 * which will become the start of the attrib array, if any.
109 /* calc how many nr_attrib we need */
112 dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
114 /* Calc the 'end' offset past the attributes array */
115 pvt = edac_align_ptr(&p, sz_private, 1);
117 /* 'pvt' now points to where the private data area is.
118 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
119 * is baselined at ZERO
121 total_size = ((unsigned long)pvt) + sz_private;
123 /* Allocate the amount of memory for the set of control structures */
124 dev_ctl = kzalloc(total_size, GFP_KERNEL);
128 /* Adjust pointers so they point within the actual memory we
129 * just allocated rather than an imaginary chunk of memory
130 * located at address 0.
131 * 'dev_ctl' points to REAL memory, while the others are
132 * ZERO based and thus need to be adjusted to point within
133 * the allocated memory.
135 dev_inst = (struct edac_device_instance *)
136 (((char *)dev_ctl) + ((unsigned long)dev_inst));
137 dev_blk = (struct edac_device_block *)
138 (((char *)dev_ctl) + ((unsigned long)dev_blk));
139 dev_attrib = (struct edac_dev_sysfs_block_attribute *)
140 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
141 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
143 /* Begin storing the information into the control info structure */
144 dev_ctl->dev_idx = device_index;
145 dev_ctl->nr_instances = nr_instances;
146 dev_ctl->instances = dev_inst;
147 dev_ctl->pvt_info = pvt;
149 /* Default logging of CEs and UEs */
153 /* Name of this edac device */
154 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
156 edac_dbg(4, "edac_dev=%p next after end=%p\n",
157 dev_ctl, pvt + sz_private);
159 /* Initialize every Instance */
160 for (instance = 0; instance < nr_instances; instance++) {
161 inst = &dev_inst[instance];
163 inst->nr_blocks = nr_blocks;
164 blk_p = &dev_blk[instance * nr_blocks];
165 inst->blocks = blk_p;
167 /* name of this instance */
168 snprintf(inst->name, sizeof(inst->name),
169 "%s%u", edac_device_name, instance);
171 /* Initialize every block in each instance */
172 for (block = 0; block < nr_blocks; block++) {
174 blk->instance = inst;
175 snprintf(blk->name, sizeof(blk->name),
176 "%s%d", edac_block_name, block+offset_value);
178 edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
179 instance, inst, block, blk, blk->name);
181 /* if there are NO attributes OR no attribute pointer
182 * then continue on to next block iteration
184 if ((nr_attrib == 0) || (attrib_spec == NULL))
187 /* setup the attribute array for this block */
188 blk->nr_attribs = nr_attrib;
189 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
190 blk->block_attributes = attrib_p;
192 edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
193 blk->block_attributes);
195 /* Initialize every user specified attribute in this
196 * block with the data the caller passed in
197 * Each block gets its own copy of pointers,
198 * and its unique 'value'
200 for (attr = 0; attr < nr_attrib; attr++) {
201 attrib = &attrib_p[attr];
203 /* populate the unique per attrib
204 * with the code pointers and info
206 attrib->attr = attrib_spec[attr].attr;
207 attrib->show = attrib_spec[attr].show;
208 attrib->store = attrib_spec[attr].store;
210 attrib->block = blk; /* up link */
212 edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
213 attrib, attrib->attr.name,
215 attrib_spec[attr].attr.name
221 /* Mark this instance as merely ALLOCATED */
222 dev_ctl->op_state = OP_ALLOC;
225 * Initialize the 'root' kobj for the edac_device controller
227 err = edac_device_register_sysfs_main_kobj(dev_ctl);
233 /* at this point, the root kobj is valid, and in order to
234 * 'free' the object, then the function:
235 * edac_device_unregister_sysfs_main_kobj() must be called
236 * which will perform kobj unregistration and the actual free
237 * will occur during the kobject callback operation
242 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
245 * edac_device_free_ctl_info()
246 * frees the memory allocated by the edac_device_alloc_ctl_info()
249 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
251 edac_device_unregister_sysfs_main_kobj(ctl_info);
253 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
256 * find_edac_device_by_dev
257 * scans the edac_device list for a specific 'struct device *'
259 * lock to be held prior to call: device_ctls_mutex
262 * pointer to control structure managing 'dev'
263 * NULL if not found on list
265 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
267 struct edac_device_ctl_info *edac_dev;
268 struct list_head *item;
272 list_for_each(item, &edac_device_list) {
273 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
275 if (edac_dev->dev == dev)
283 * add_edac_dev_to_global_list
284 * Before calling this function, caller must
285 * assign a unique value to edac_dev->dev_idx.
287 * lock to be held prior to call: device_ctls_mutex
293 static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
295 struct list_head *item, *insert_before;
296 struct edac_device_ctl_info *rover;
298 insert_before = &edac_device_list;
300 /* Determine if already on the list */
301 rover = find_edac_device_by_dev(edac_dev->dev);
302 if (unlikely(rover != NULL))
305 /* Insert in ascending order by 'dev_idx', so find position */
306 list_for_each(item, &edac_device_list) {
307 rover = list_entry(item, struct edac_device_ctl_info, link);
309 if (rover->dev_idx >= edac_dev->dev_idx) {
310 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
313 insert_before = item;
318 list_add_tail_rcu(&edac_dev->link, insert_before);
322 edac_printk(KERN_WARNING, EDAC_MC,
323 "%s (%s) %s %s already assigned %d\n",
324 dev_name(rover->dev), edac_dev_name(rover),
325 rover->mod_name, rover->ctl_name, rover->dev_idx);
329 edac_printk(KERN_WARNING, EDAC_MC,
330 "bug in low-level driver: attempt to assign\n"
331 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
337 * del_edac_device_from_global_list
339 static void del_edac_device_from_global_list(struct edac_device_ctl_info
342 list_del_rcu(&edac_device->link);
344 /* these are for safe removal of devices from global list while
345 * NMI handlers may be traversing list
348 INIT_LIST_HEAD(&edac_device->link);
352 * edac_device_workq_function
353 * performs the operation scheduled by a workq request
355 * this workq is embedded within an edac_device_ctl_info
356 * structure, that needs to be polled for possible error events.
358 * This operation is to acquire the list mutex lock
359 * (thus preventing insertation or deletion)
360 * and then call the device's poll function IFF this device is
361 * running polled and there is a poll function defined.
363 static void edac_device_workq_function(struct work_struct *work_req)
365 struct delayed_work *d_work = to_delayed_work(work_req);
366 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
368 mutex_lock(&device_ctls_mutex);
370 /* If we are being removed, bail out immediately */
371 if (edac_dev->op_state == OP_OFFLINE) {
372 mutex_unlock(&device_ctls_mutex);
376 /* Only poll controllers that are running polled and have a check */
377 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
378 (edac_dev->edac_check != NULL)) {
379 edac_dev->edac_check(edac_dev);
382 mutex_unlock(&device_ctls_mutex);
384 /* Reschedule the workq for the next time period to start again
385 * if the number of msec is for 1 sec, then adjust to the next
386 * whole one second to save timers firing all over the period
387 * between integral seconds
389 if (edac_dev->poll_msec == 1000)
390 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
392 edac_queue_work(&edac_dev->work, edac_dev->delay);
396 * edac_device_workq_setup
397 * initialize a workq item for this edac_device instance
398 * passing in the new delay period in msec
400 static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
405 /* take the arg 'msec' and set it into the control structure
406 * to used in the time period calculation
407 * then calc the number of jiffies that represents
409 edac_dev->poll_msec = msec;
410 edac_dev->delay = msecs_to_jiffies(msec);
412 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
414 /* optimize here for the 1 second case, which will be normal value, to
415 * fire ON the 1 second time event. This helps reduce all sorts of
416 * timers firing on sub-second basis, while they are happy
417 * to fire together on the 1 second exactly
419 if (edac_dev->poll_msec == 1000)
420 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
422 edac_queue_work(&edac_dev->work, edac_dev->delay);
426 * edac_device_workq_teardown
427 * stop the workq processing on this edac_dev
429 static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
431 if (!edac_dev->edac_check)
434 edac_dev->op_state = OP_OFFLINE;
436 edac_stop_work(&edac_dev->work);
440 * edac_device_reset_delay_period
442 * need to stop any outstanding workq queued up at this time
443 * because we will be resetting the sleep time.
444 * Then restart the workq on the new delay
446 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
449 unsigned long jiffs = msecs_to_jiffies(value);
452 jiffs = round_jiffies_relative(value);
454 edac_dev->poll_msec = value;
455 edac_dev->delay = jiffs;
457 edac_mod_work(&edac_dev->work, jiffs);
461 * edac_device_alloc_index: Allocate a unique device index number
464 * allocated index number
466 int edac_device_alloc_index(void)
468 static atomic_t device_indexes = ATOMIC_INIT(0);
470 return atomic_inc_return(&device_indexes) - 1;
472 EXPORT_SYMBOL_GPL(edac_device_alloc_index);
475 * edac_device_add_device: Insert the 'edac_dev' structure into the
476 * edac_device global list and create sysfs entries associated with
477 * edac_device structure.
478 * @edac_device: pointer to the edac_device structure to be added to the list
479 * 'edac_device' structure.
485 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
489 #ifdef CONFIG_EDAC_DEBUG
490 if (edac_debug_level >= 3)
491 edac_device_dump_device(edac_dev);
493 mutex_lock(&device_ctls_mutex);
495 if (add_edac_dev_to_global_list(edac_dev))
498 /* set load time so that error rate can be tracked */
499 edac_dev->start_time = jiffies;
501 /* create this instance's sysfs entries */
502 if (edac_device_create_sysfs(edac_dev)) {
503 edac_device_printk(edac_dev, KERN_WARNING,
504 "failed to create sysfs device\n");
508 /* If there IS a check routine, then we are running POLLED */
509 if (edac_dev->edac_check != NULL) {
510 /* This instance is NOW RUNNING */
511 edac_dev->op_state = OP_RUNNING_POLL;
514 * enable workq processing on this instance,
515 * default = 1000 msec
517 edac_device_workq_setup(edac_dev, 1000);
519 edac_dev->op_state = OP_RUNNING_INTERRUPT;
522 /* Report action taken */
523 edac_device_printk(edac_dev, KERN_INFO,
524 "Giving out device to module %s controller %s: DEV %s (%s)\n",
525 edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
526 edac_op_state_to_string(edac_dev->op_state));
528 mutex_unlock(&device_ctls_mutex);
532 /* Some error, so remove the entry from the lsit */
533 del_edac_device_from_global_list(edac_dev);
536 mutex_unlock(&device_ctls_mutex);
539 EXPORT_SYMBOL_GPL(edac_device_add_device);
542 * edac_device_del_device:
543 * Remove sysfs entries for specified edac_device structure and
544 * then remove edac_device structure from global list
547 * Pointer to 'struct device' representing edac_device
548 * structure to remove.
551 * Pointer to removed edac_device structure,
552 * OR NULL if device not found.
554 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
556 struct edac_device_ctl_info *edac_dev;
560 mutex_lock(&device_ctls_mutex);
562 /* Find the structure on the list, if not there, then leave */
563 edac_dev = find_edac_device_by_dev(dev);
564 if (edac_dev == NULL) {
565 mutex_unlock(&device_ctls_mutex);
569 /* mark this instance as OFFLINE */
570 edac_dev->op_state = OP_OFFLINE;
572 /* deregister from global list */
573 del_edac_device_from_global_list(edac_dev);
575 mutex_unlock(&device_ctls_mutex);
577 /* clear workq processing on this instance */
578 edac_device_workq_teardown(edac_dev);
580 /* Tear down the sysfs entries for this instance */
581 edac_device_remove_sysfs(edac_dev);
583 edac_printk(KERN_INFO, EDAC_MC,
584 "Removed device %d for %s %s: DEV %s\n",
586 edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
590 EXPORT_SYMBOL_GPL(edac_device_del_device);
592 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
594 return edac_dev->log_ce;
597 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
599 return edac_dev->log_ue;
602 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
605 return edac_dev->panic_on_ue;
609 * edac_device_handle_ce
610 * perform a common output and handling of an 'edac_dev' CE event
612 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
613 int inst_nr, int block_nr, const char *msg)
615 struct edac_device_instance *instance;
616 struct edac_device_block *block = NULL;
618 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
619 edac_device_printk(edac_dev, KERN_ERR,
620 "INTERNAL ERROR: 'instance' out of range "
621 "(%d >= %d)\n", inst_nr,
622 edac_dev->nr_instances);
626 instance = edac_dev->instances + inst_nr;
628 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
629 edac_device_printk(edac_dev, KERN_ERR,
630 "INTERNAL ERROR: instance %d 'block' "
631 "out of range (%d >= %d)\n",
633 instance->nr_blocks);
637 if (instance->nr_blocks > 0) {
638 block = instance->blocks + block_nr;
639 block->counters.ce_count++;
642 /* Propagate the count up the 'totals' tree */
643 instance->counters.ce_count++;
644 edac_dev->counters.ce_count++;
646 if (edac_device_get_log_ce(edac_dev))
647 edac_device_printk(edac_dev, KERN_WARNING,
648 "CE: %s instance: %s block: %s '%s'\n",
649 edac_dev->ctl_name, instance->name,
650 block ? block->name : "N/A", msg);
652 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
655 * edac_device_handle_ue
656 * perform a common output and handling of an 'edac_dev' UE event
658 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
659 int inst_nr, int block_nr, const char *msg)
661 struct edac_device_instance *instance;
662 struct edac_device_block *block = NULL;
664 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
665 edac_device_printk(edac_dev, KERN_ERR,
666 "INTERNAL ERROR: 'instance' out of range "
667 "(%d >= %d)\n", inst_nr,
668 edac_dev->nr_instances);
672 instance = edac_dev->instances + inst_nr;
674 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
675 edac_device_printk(edac_dev, KERN_ERR,
676 "INTERNAL ERROR: instance %d 'block' "
677 "out of range (%d >= %d)\n",
679 instance->nr_blocks);
683 if (instance->nr_blocks > 0) {
684 block = instance->blocks + block_nr;
685 block->counters.ue_count++;
688 /* Propagate the count up the 'totals' tree */
689 instance->counters.ue_count++;
690 edac_dev->counters.ue_count++;
692 if (edac_device_get_log_ue(edac_dev))
693 edac_device_printk(edac_dev, KERN_EMERG,
694 "UE: %s instance: %s block: %s '%s'\n",
695 edac_dev->ctl_name, instance->name,
696 block ? block->name : "N/A", msg);
698 if (edac_device_get_panic_on_ue(edac_dev))
699 panic("EDAC %s: UE instance: %s block %s '%s'\n",
700 edac_dev->ctl_name, instance->name,
701 block ? block->name : "N/A", msg);
703 EXPORT_SYMBOL_GPL(edac_device_handle_ue);