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 DEFINE_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 */
53 * edac_device_alloc_ctl_info()
54 * Allocate a new edac device control info structure
56 * The control structure is allocated in complete chunk
57 * from the OS. It is in turn sub allocated to the
58 * various objects that compose the struture
60 * The structure has a 'nr_instance' array within itself.
61 * Each instance represents a major component
62 * Example: L1 cache and L2 cache are 2 instance components
64 * Within each instance is an array of 'nr_blocks' blockoffsets
66 struct edac_device_ctl_info
*edac_device_alloc_ctl_info(
68 char *edac_device_name
, unsigned nr_instances
,
69 char *edac_block_name
, unsigned nr_blocks
,
70 unsigned offset_value
, /* zero, 1, or other based offset */
71 struct edac_dev_sysfs_block_attribute
*attrib_spec
, unsigned nr_attrib
,
74 struct edac_device_ctl_info
*dev_ctl
;
75 struct edac_device_instance
*dev_inst
, *inst
;
76 struct edac_device_block
*dev_blk
, *blk_p
, *blk
;
77 struct edac_dev_sysfs_block_attribute
*dev_attrib
, *attrib_p
, *attrib
;
80 unsigned instance
, block
, attr
;
84 debugf1("%s() instances=%d blocks=%d\n",
85 __func__
, nr_instances
, nr_blocks
);
87 /* Calculate the size of memory we need to allocate AND
88 * determine the offsets of the various item arrays
89 * (instance,block,attrib) from the start of an allocated structure.
90 * We want the alignment of each item (instance,block,attrib)
91 * to be at least as stringent as what the compiler would
92 * provide if we could simply hardcode everything into a single struct.
94 dev_ctl
= (struct edac_device_ctl_info
*)NULL
;
96 /* Calc the 'end' offset past end of ONE ctl_info structure
97 * which will become the start of the 'instance' array
99 dev_inst
= edac_align_ptr(&dev_ctl
[1], sizeof(*dev_inst
));
101 /* Calc the 'end' offset past the instance array within the ctl_info
102 * which will become the start of the block array
104 dev_blk
= edac_align_ptr(&dev_inst
[nr_instances
], sizeof(*dev_blk
));
106 /* Calc the 'end' offset past the dev_blk array
107 * which will become the start of the attrib array, if any.
109 count
= nr_instances
* nr_blocks
;
110 dev_attrib
= edac_align_ptr(&dev_blk
[count
], sizeof(*dev_attrib
));
112 /* Check for case of when an attribute array is specified */
114 /* calc how many nr_attrib we need */
117 /* Calc the 'end' offset past the attributes array */
118 pvt
= edac_align_ptr(&dev_attrib
[count
], sz_private
);
120 /* no attribute array specificed */
121 pvt
= edac_align_ptr(dev_attrib
, sz_private
);
124 /* 'pvt' now points to where the private data area is.
125 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
126 * is baselined at ZERO
128 total_size
= ((unsigned long)pvt
) + sz_private
;
130 /* Allocate the amount of memory for the set of control structures */
131 dev_ctl
= kzalloc(total_size
, GFP_KERNEL
);
135 /* Adjust pointers so they point within the actual memory we
136 * just allocated rather than an imaginary chunk of memory
137 * located at address 0.
138 * 'dev_ctl' points to REAL memory, while the others are
139 * ZERO based and thus need to be adjusted to point within
140 * the allocated memory.
142 dev_inst
= (struct edac_device_instance
*)
143 (((char *)dev_ctl
) + ((unsigned long)dev_inst
));
144 dev_blk
= (struct edac_device_block
*)
145 (((char *)dev_ctl
) + ((unsigned long)dev_blk
));
146 dev_attrib
= (struct edac_dev_sysfs_block_attribute
*)
147 (((char *)dev_ctl
) + ((unsigned long)dev_attrib
));
148 pvt
= sz_private
? (((char *)dev_ctl
) + ((unsigned long)pvt
)) : NULL
;
150 /* Begin storing the information into the control info structure */
151 dev_ctl
->dev_idx
= device_index
;
152 dev_ctl
->nr_instances
= nr_instances
;
153 dev_ctl
->instances
= dev_inst
;
154 dev_ctl
->pvt_info
= pvt
;
156 /* Name of this edac device */
157 snprintf(dev_ctl
->name
,sizeof(dev_ctl
->name
),"%s",edac_device_name
);
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 debugf1("%s() instance=%d block=%d name=%s\n",
179 __func__
, instance
, block
, 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 /* Initialize every user specified attribute in this
193 * block with the data the caller passed in
195 for (attr
= 0; attr
< nr_attrib
; attr
++) {
196 attrib
= &attrib_p
[attr
];
197 attrib
->attr
= attrib_spec
->attr
;
198 attrib
->show
= attrib_spec
->show
;
199 attrib
->store
= attrib_spec
->store
;
201 /* up reference this block */
204 /* bump the attrib_spec */
210 /* Mark this instance as merely ALLOCATED */
211 dev_ctl
->op_state
= OP_ALLOC
;
214 * Initialize the 'root' kobj for the edac_device controller
216 err
= edac_device_register_sysfs_main_kobj(dev_ctl
);
222 /* at this point, the root kobj is valid, and in order to
223 * 'free' the object, then the function:
224 * edac_device_unregister_sysfs_main_kobj() must be called
225 * which will perform kobj unregistration and the actual free
226 * will occur during the kobject callback operation
231 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info
);
234 * edac_device_free_ctl_info()
235 * frees the memory allocated by the edac_device_alloc_ctl_info()
238 void edac_device_free_ctl_info(struct edac_device_ctl_info
*ctl_info
)
240 edac_device_unregister_sysfs_main_kobj(ctl_info
);
242 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info
);
245 * find_edac_device_by_dev
246 * scans the edac_device list for a specific 'struct device *'
248 * lock to be held prior to call: device_ctls_mutex
251 * pointer to control structure managing 'dev'
252 * NULL if not found on list
254 static struct edac_device_ctl_info
*find_edac_device_by_dev(struct device
*dev
)
256 struct edac_device_ctl_info
*edac_dev
;
257 struct list_head
*item
;
259 debugf3("%s()\n", __func__
);
261 list_for_each(item
, &edac_device_list
) {
262 edac_dev
= list_entry(item
, struct edac_device_ctl_info
, link
);
264 if (edac_dev
->dev
== dev
)
272 * add_edac_dev_to_global_list
273 * Before calling this function, caller must
274 * assign a unique value to edac_dev->dev_idx.
276 * lock to be held prior to call: device_ctls_mutex
282 static int add_edac_dev_to_global_list(struct edac_device_ctl_info
*edac_dev
)
284 struct list_head
*item
, *insert_before
;
285 struct edac_device_ctl_info
*rover
;
287 insert_before
= &edac_device_list
;
289 /* Determine if already on the list */
290 rover
= find_edac_device_by_dev(edac_dev
->dev
);
291 if (unlikely(rover
!= NULL
))
294 /* Insert in ascending order by 'dev_idx', so find position */
295 list_for_each(item
, &edac_device_list
) {
296 rover
= list_entry(item
, struct edac_device_ctl_info
, link
);
298 if (rover
->dev_idx
>= edac_dev
->dev_idx
) {
299 if (unlikely(rover
->dev_idx
== edac_dev
->dev_idx
))
302 insert_before
= item
;
307 list_add_tail_rcu(&edac_dev
->link
, insert_before
);
311 edac_printk(KERN_WARNING
, EDAC_MC
,
312 "%s (%s) %s %s already assigned %d\n",
313 rover
->dev
->bus_id
, dev_name(rover
),
314 rover
->mod_name
, rover
->ctl_name
, rover
->dev_idx
);
318 edac_printk(KERN_WARNING
, EDAC_MC
,
319 "bug in low-level driver: attempt to assign\n"
320 " duplicate dev_idx %d in %s()\n", rover
->dev_idx
,
326 * complete_edac_device_list_del
328 * callback function when reference count is zero
330 static void complete_edac_device_list_del(struct rcu_head
*head
)
332 struct edac_device_ctl_info
*edac_dev
;
334 edac_dev
= container_of(head
, struct edac_device_ctl_info
, rcu
);
335 INIT_LIST_HEAD(&edac_dev
->link
);
336 complete(&edac_dev
->removal_complete
);
340 * del_edac_device_from_global_list
342 * remove the RCU, setup for a callback call,
343 * then wait for the callback to occur
345 static void del_edac_device_from_global_list(struct edac_device_ctl_info
348 list_del_rcu(&edac_device
->link
);
350 init_completion(&edac_device
->removal_complete
);
351 call_rcu(&edac_device
->rcu
, complete_edac_device_list_del
);
352 wait_for_completion(&edac_device
->removal_complete
);
357 * Search for a edac_device_ctl_info structure whose index is 'idx'.
359 * If found, return a pointer to the structure.
362 * Caller must hold device_ctls_mutex.
364 struct edac_device_ctl_info
*edac_device_find(int idx
)
366 struct list_head
*item
;
367 struct edac_device_ctl_info
*edac_dev
;
369 /* Iterate over list, looking for exact match of ID */
370 list_for_each(item
, &edac_device_list
) {
371 edac_dev
= list_entry(item
, struct edac_device_ctl_info
, link
);
373 if (edac_dev
->dev_idx
>= idx
) {
374 if (edac_dev
->dev_idx
== idx
)
377 /* not on list, so terminate early */
384 EXPORT_SYMBOL_GPL(edac_device_find
);
387 * edac_device_workq_function
388 * performs the operation scheduled by a workq request
390 static void edac_device_workq_function(struct work_struct
*work_req
)
392 struct delayed_work
*d_work
= (struct delayed_work
*)work_req
;
393 struct edac_device_ctl_info
*edac_dev
= to_edac_device_ctl_work(d_work
);
395 //debugf0("%s() here and running\n", __func__);
396 mutex_lock(&device_ctls_mutex
);
398 /* Only poll controllers that are running polled and have a check */
399 if ((edac_dev
->op_state
== OP_RUNNING_POLL
) &&
400 (edac_dev
->edac_check
!= NULL
)) {
401 edac_dev
->edac_check(edac_dev
);
404 mutex_unlock(&device_ctls_mutex
);
407 queue_delayed_work(edac_workqueue
, &edac_dev
->work
, edac_dev
->delay
);
411 * edac_device_workq_setup
412 * initialize a workq item for this edac_device instance
413 * passing in the new delay period in msec
415 void edac_device_workq_setup(struct edac_device_ctl_info
*edac_dev
,
418 debugf0("%s()\n", __func__
);
420 edac_dev
->poll_msec
= msec
;
421 edac_dev
->delay
= msecs_to_jiffies(msec
); /* Calc delay jiffies */
423 INIT_DELAYED_WORK(&edac_dev
->work
, edac_device_workq_function
);
424 queue_delayed_work(edac_workqueue
, &edac_dev
->work
, edac_dev
->delay
);
428 * edac_device_workq_teardown
429 * stop the workq processing on this edac_dev
431 void edac_device_workq_teardown(struct edac_device_ctl_info
*edac_dev
)
435 status
= cancel_delayed_work(&edac_dev
->work
);
437 /* workq instance might be running, wait for it */
438 flush_workqueue(edac_workqueue
);
443 * edac_device_reset_delay_period
446 void edac_device_reset_delay_period(struct edac_device_ctl_info
*edac_dev
,
449 mutex_lock(&device_ctls_mutex
);
451 /* cancel the current workq request */
452 edac_device_workq_teardown(edac_dev
);
454 /* restart the workq request, with new delay value */
455 edac_device_workq_setup(edac_dev
, value
);
457 mutex_unlock(&device_ctls_mutex
);
461 * edac_device_add_device: Insert the 'edac_dev' structure into the
462 * edac_device global list and create sysfs entries associated with
463 * edac_device structure.
464 * @edac_device: pointer to the edac_device structure to be added to the list
465 * 'edac_device' structure.
471 int edac_device_add_device(struct edac_device_ctl_info
*edac_dev
)
473 debugf0("%s()\n", __func__
);
475 #ifdef CONFIG_EDAC_DEBUG
476 if (edac_debug_level
>= 3)
477 edac_device_dump_device(edac_dev
);
479 mutex_lock(&device_ctls_mutex
);
481 if (add_edac_dev_to_global_list(edac_dev
))
484 /* set load time so that error rate can be tracked */
485 edac_dev
->start_time
= jiffies
;
487 /* create this instance's sysfs entries */
488 if (edac_device_create_sysfs(edac_dev
)) {
489 edac_device_printk(edac_dev
, KERN_WARNING
,
490 "failed to create sysfs device\n");
494 /* If there IS a check routine, then we are running POLLED */
495 if (edac_dev
->edac_check
!= NULL
) {
496 /* This instance is NOW RUNNING */
497 edac_dev
->op_state
= OP_RUNNING_POLL
;
500 * enable workq processing on this instance,
501 * default = 1000 msec
503 edac_device_workq_setup(edac_dev
, 1000);
505 edac_dev
->op_state
= OP_RUNNING_INTERRUPT
;
508 /* Report action taken */
509 edac_device_printk(edac_dev
, KERN_INFO
,
510 "Giving out device to module '%s' controller "
511 "'%s': DEV '%s' (%s)\n",
515 edac_op_state_to_string(edac_dev
->op_state
));
517 mutex_unlock(&device_ctls_mutex
);
521 /* Some error, so remove the entry from the lsit */
522 del_edac_device_from_global_list(edac_dev
);
525 mutex_unlock(&device_ctls_mutex
);
528 EXPORT_SYMBOL_GPL(edac_device_add_device
);
531 * edac_device_del_device:
532 * Remove sysfs entries for specified edac_device structure and
533 * then remove edac_device structure from global list
536 * Pointer to 'struct device' representing edac_device
537 * structure to remove.
540 * Pointer to removed edac_device structure,
541 * OR NULL if device not found.
543 struct edac_device_ctl_info
*edac_device_del_device(struct device
*dev
)
545 struct edac_device_ctl_info
*edac_dev
;
547 debugf0("MC: %s()\n", __func__
);
549 mutex_lock(&device_ctls_mutex
);
551 /* Find the structure on the list, if not there, then leave */
552 edac_dev
= find_edac_device_by_dev(dev
);
553 if (edac_dev
== NULL
) {
554 mutex_unlock(&device_ctls_mutex
);
558 /* mark this instance as OFFLINE */
559 edac_dev
->op_state
= OP_OFFLINE
;
561 /* clear workq processing on this instance */
562 edac_device_workq_teardown(edac_dev
);
564 /* deregister from global list */
565 del_edac_device_from_global_list(edac_dev
);
567 mutex_unlock(&device_ctls_mutex
);
569 /* Tear down the sysfs entries for this instance */
570 edac_device_remove_sysfs(edac_dev
);
572 edac_printk(KERN_INFO
, EDAC_MC
,
573 "Removed device %d for %s %s: DEV %s\n",
575 edac_dev
->mod_name
, edac_dev
->ctl_name
, dev_name(edac_dev
));
579 EXPORT_SYMBOL_GPL(edac_device_del_device
);
581 static inline int edac_device_get_log_ce(struct edac_device_ctl_info
*edac_dev
)
583 return edac_dev
->log_ce
;
586 static inline int edac_device_get_log_ue(struct edac_device_ctl_info
*edac_dev
)
588 return edac_dev
->log_ue
;
591 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
594 return edac_dev
->panic_on_ue
;
598 * edac_device_handle_ce
599 * perform a common output and handling of an 'edac_dev' CE event
601 void edac_device_handle_ce(struct edac_device_ctl_info
*edac_dev
,
602 int inst_nr
, int block_nr
, const char *msg
)
604 struct edac_device_instance
*instance
;
605 struct edac_device_block
*block
= NULL
;
607 if ((inst_nr
>= edac_dev
->nr_instances
) || (inst_nr
< 0)) {
608 edac_device_printk(edac_dev
, KERN_ERR
,
609 "INTERNAL ERROR: 'instance' out of range "
610 "(%d >= %d)\n", inst_nr
,
611 edac_dev
->nr_instances
);
615 instance
= edac_dev
->instances
+ inst_nr
;
617 if ((block_nr
>= instance
->nr_blocks
) || (block_nr
< 0)) {
618 edac_device_printk(edac_dev
, KERN_ERR
,
619 "INTERNAL ERROR: instance %d 'block' "
620 "out of range (%d >= %d)\n",
622 instance
->nr_blocks
);
626 if (instance
->nr_blocks
> 0) {
627 block
= instance
->blocks
+ block_nr
;
628 block
->counters
.ce_count
++;
631 /* Propogate the count up the 'totals' tree */
632 instance
->counters
.ce_count
++;
633 edac_dev
->counters
.ce_count
++;
635 if (edac_device_get_log_ce(edac_dev
))
636 edac_device_printk(edac_dev
, KERN_WARNING
,
637 "CE: %s instance: %s block: %s '%s'\n",
638 edac_dev
->ctl_name
, instance
->name
,
639 block
? block
->name
: "N/A", msg
);
641 EXPORT_SYMBOL_GPL(edac_device_handle_ce
);
644 * edac_device_handle_ue
645 * perform a common output and handling of an 'edac_dev' UE event
647 void edac_device_handle_ue(struct edac_device_ctl_info
*edac_dev
,
648 int inst_nr
, int block_nr
, const char *msg
)
650 struct edac_device_instance
*instance
;
651 struct edac_device_block
*block
= NULL
;
653 if ((inst_nr
>= edac_dev
->nr_instances
) || (inst_nr
< 0)) {
654 edac_device_printk(edac_dev
, KERN_ERR
,
655 "INTERNAL ERROR: 'instance' out of range "
656 "(%d >= %d)\n", inst_nr
,
657 edac_dev
->nr_instances
);
661 instance
= edac_dev
->instances
+ inst_nr
;
663 if ((block_nr
>= instance
->nr_blocks
) || (block_nr
< 0)) {
664 edac_device_printk(edac_dev
, KERN_ERR
,
665 "INTERNAL ERROR: instance %d 'block' "
666 "out of range (%d >= %d)\n",
668 instance
->nr_blocks
);
672 if (instance
->nr_blocks
> 0) {
673 block
= instance
->blocks
+ block_nr
;
674 block
->counters
.ue_count
++;
677 /* Propogate the count up the 'totals' tree */
678 instance
->counters
.ue_count
++;
679 edac_dev
->counters
.ue_count
++;
681 if (edac_device_get_log_ue(edac_dev
))
682 edac_device_printk(edac_dev
, KERN_EMERG
,
683 "UE: %s instance: %s block: %s '%s'\n",
684 edac_dev
->ctl_name
, instance
->name
,
685 block
? block
->name
: "N/A", msg
);
687 if (edac_device_get_panic_on_ue(edac_dev
))
688 panic("EDAC %s: UE instance: %s block %s '%s'\n",
689 edac_dev
->ctl_name
, instance
->name
,
690 block
? block
->name
: "N/A", msg
);
692 EXPORT_SYMBOL_GPL(edac_device_handle_ue
);