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/spinlock.h>
24 #include <linux/list.h>
25 #include <linux/sysdev.h>
26 #include <linux/ctype.h>
27 #include <linux/workqueue.h>
28 #include <asm/uaccess.h>
31 #include "edac_core.h"
32 #include "edac_module.h"
34 /* lock to memory controller's control array */
35 static DECLARE_MUTEX(device_ctls_mutex
);
36 static struct list_head edac_device_list
= LIST_HEAD_INIT(edac_device_list
);
38 static inline void lock_device_list(void)
40 down(&device_ctls_mutex
);
43 static inline void unlock_device_list(void)
45 up(&device_ctls_mutex
);
48 #ifdef CONFIG_EDAC_DEBUG
49 static void edac_device_dump_device(struct edac_device_ctl_info
*edac_dev
)
51 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev
, edac_dev
->dev_idx
);
52 debugf4("\tedac_dev->edac_check = %p\n", edac_dev
->edac_check
);
53 debugf3("\tdev = %p\n", edac_dev
->dev
);
54 debugf3("\tmod_name:ctl_name = %s:%s\n",
55 edac_dev
->mod_name
, edac_dev
->ctl_name
);
56 debugf3("\tpvt_info = %p\n\n", edac_dev
->pvt_info
);
58 #endif /* CONFIG_EDAC_DEBUG */
61 * The alloc() and free() functions for the 'edac_device' control info
62 * structure. A MC driver will allocate one of these for each edac_device
63 * it is going to control/register with the EDAC CORE.
65 struct edac_device_ctl_info
*edac_device_alloc_ctl_info(
67 char *edac_device_name
,
68 unsigned nr_instances
,
69 char *edac_block_name
,
71 unsigned offset_value
,
72 struct edac_attrib_spec
76 struct edac_device_ctl_info
*dev_ctl
;
77 struct edac_device_instance
*dev_inst
, *inst
;
78 struct edac_device_block
*dev_blk
, *blk_p
, *blk
;
79 struct edac_attrib
*dev_attrib
, *attrib_p
, *attrib
;
82 unsigned instance
, block
, attr
;
85 debugf1("%s() instances=%d blocks=%d\n",
86 __func__
, nr_instances
, nr_blocks
);
88 /* Figure out the offsets of the various items from the start of an
89 * ctl_info structure. We want the alignment of each item
90 * to be at least as stringent as what the compiler would
91 * provide if we could simply hardcode everything into a single struct.
93 dev_ctl
= (struct edac_device_ctl_info
*)0;
95 /* Calc the 'end' offset past the ctl_info structure */
96 dev_inst
= (struct edac_device_instance
*)
97 edac_align_ptr(&dev_ctl
[1], sizeof(*dev_inst
));
99 /* Calc the 'end' offset past the instance array */
100 dev_blk
= (struct edac_device_block
*)
101 edac_align_ptr(&dev_inst
[nr_instances
], sizeof(*dev_blk
));
103 /* Calc the 'end' offset past the dev_blk array */
104 count
= nr_instances
* nr_blocks
;
105 dev_attrib
= (struct edac_attrib
*)
106 edac_align_ptr(&dev_blk
[count
], sizeof(*dev_attrib
));
108 /* Check for case of NO attributes specified */
112 /* Calc the 'end' offset past the attributes array */
113 pvt
= edac_align_ptr(&dev_attrib
[count
], sz_private
);
114 total_size
= ((unsigned long)pvt
) + sz_private
;
116 /* Allocate the amount of memory for the set of control structures */
117 if ((dev_ctl
= kmalloc(total_size
, GFP_KERNEL
)) == NULL
)
120 /* Adjust pointers so they point within the memory we just allocated
121 * rather than an imaginary chunk of memory located at address 0.
123 dev_inst
= (struct edac_device_instance
*)
124 (((char *)dev_ctl
) + ((unsigned long)dev_inst
));
125 dev_blk
= (struct edac_device_block
*)
126 (((char *)dev_ctl
) + ((unsigned long)dev_blk
));
127 dev_attrib
= (struct edac_attrib
*)
128 (((char *)dev_ctl
) + ((unsigned long)dev_attrib
));
129 pvt
= sz_private
? (((char *)dev_ctl
) + ((unsigned long)pvt
)) : NULL
;
131 memset(dev_ctl
, 0, total_size
); /* clear all fields */
132 dev_ctl
->nr_instances
= nr_instances
;
133 dev_ctl
->instances
= dev_inst
;
134 dev_ctl
->pvt_info
= pvt
;
136 /* Name of this edac device, ensure null terminated */
137 snprintf(dev_ctl
->name
, sizeof(dev_ctl
->name
), "%s", edac_device_name
);
138 dev_ctl
->name
[sizeof(dev_ctl
->name
) - 1] = '\0';
140 /* Initialize every Instance */
141 for (instance
= 0; instance
< nr_instances
; instance
++) {
142 inst
= &dev_inst
[instance
];
144 inst
->nr_blocks
= nr_blocks
;
145 blk_p
= &dev_blk
[instance
* nr_blocks
];
146 inst
->blocks
= blk_p
;
148 /* name of this instance */
149 snprintf(inst
->name
, sizeof(inst
->name
),
150 "%s%u", edac_device_name
, instance
);
151 inst
->name
[sizeof(inst
->name
) - 1] = '\0';
153 /* Initialize every block in each instance */
154 for (block
= 0; block
< nr_blocks
; block
++) {
156 blk
->instance
= inst
;
157 blk
->nr_attribs
= nr_attribs
;
158 attrib_p
= &dev_attrib
[block
* nr_attribs
];
159 blk
->attribs
= attrib_p
;
160 snprintf(blk
->name
, sizeof(blk
->name
),
161 "%s%d", edac_block_name
, block
+offset_value
);
162 blk
->name
[sizeof(blk
->name
) - 1] = '\0';
164 debugf1("%s() instance=%d block=%d name=%s\n",
165 __func__
, instance
, block
, blk
->name
);
167 if (attrib_spec
!= NULL
) {
168 /* when there is an attrib_spec passed int then
169 * Initialize every attrib of each block
171 for (attr
= 0; attr
< nr_attribs
; attr
++) {
172 attrib
= &attrib_p
[attr
];
175 /* Link each attribute to the caller's
176 * spec entry, for name and type
178 attrib
->spec
= &attrib_spec
[attr
];
184 /* Mark this instance as merely ALLOCATED */
185 dev_ctl
->op_state
= OP_ALLOC
;
190 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info
);
193 * edac_device_free_ctl_info()
194 * frees the memory allocated by the edac_device_alloc_ctl_info()
197 void edac_device_free_ctl_info(struct edac_device_ctl_info
*ctl_info
)
202 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info
);
205 * find_edac_device_by_dev
206 * scans the edac_device list for a specific 'struct device *'
208 static struct edac_device_ctl_info
*find_edac_device_by_dev(struct device
*dev
)
210 struct edac_device_ctl_info
*edac_dev
;
211 struct list_head
*item
;
213 debugf3("%s()\n", __func__
);
215 list_for_each(item
, &edac_device_list
) {
216 edac_dev
= list_entry(item
, struct edac_device_ctl_info
, link
);
218 if (edac_dev
->dev
== dev
)
226 * add_edac_dev_to_global_list
227 * Before calling this function, caller must
228 * assign a unique value to edac_dev->dev_idx.
233 static int add_edac_dev_to_global_list(struct edac_device_ctl_info
*edac_dev
)
235 struct list_head
*item
, *insert_before
;
236 struct edac_device_ctl_info
*rover
;
238 insert_before
= &edac_device_list
;
240 /* Determine if already on the list */
241 if (unlikely((rover
= find_edac_device_by_dev(edac_dev
->dev
)) != NULL
))
244 /* Insert in ascending order by 'dev_idx', so find position */
245 list_for_each(item
, &edac_device_list
) {
246 rover
= list_entry(item
, struct edac_device_ctl_info
, link
);
248 if (rover
->dev_idx
>= edac_dev
->dev_idx
) {
249 if (unlikely(rover
->dev_idx
== edac_dev
->dev_idx
))
252 insert_before
= item
;
257 list_add_tail_rcu(&edac_dev
->link
, insert_before
);
261 edac_printk(KERN_WARNING
, EDAC_MC
,
262 "%s (%s) %s %s already assigned %d\n",
263 rover
->dev
->bus_id
, dev_name(rover
),
264 rover
->mod_name
, rover
->ctl_name
, rover
->dev_idx
);
268 edac_printk(KERN_WARNING
, EDAC_MC
,
269 "bug in low-level driver: attempt to assign\n"
270 " duplicate dev_idx %d in %s()\n", rover
->dev_idx
,
276 * complete_edac_device_list_del
278 static void complete_edac_device_list_del(struct rcu_head
*head
)
280 struct edac_device_ctl_info
*edac_dev
;
282 edac_dev
= container_of(head
, struct edac_device_ctl_info
, rcu
);
283 INIT_LIST_HEAD(&edac_dev
->link
);
284 complete(&edac_dev
->complete
);
288 * del_edac_device_from_global_list
290 static void del_edac_device_from_global_list(struct edac_device_ctl_info
293 list_del_rcu(&edac_device
->link
);
294 init_completion(&edac_device
->complete
);
295 call_rcu(&edac_device
->rcu
, complete_edac_device_list_del
);
296 wait_for_completion(&edac_device
->complete
);
301 * Search for a edac_device_ctl_info structure whose index is 'idx'.
303 * If found, return a pointer to the structure.
306 * Caller must hold device_ctls_mutex.
308 struct edac_device_ctl_info
*edac_device_find(int idx
)
310 struct list_head
*item
;
311 struct edac_device_ctl_info
*edac_dev
;
313 /* Iterate over list, looking for exact match of ID */
314 list_for_each(item
, &edac_device_list
) {
315 edac_dev
= list_entry(item
, struct edac_device_ctl_info
, link
);
317 if (edac_dev
->dev_idx
>= idx
) {
318 if (edac_dev
->dev_idx
== idx
)
321 /* not on list, so terminate early */
329 EXPORT_SYMBOL(edac_device_find
);
332 * edac_device_workq_function
333 * performs the operation scheduled by a workq request
335 static void edac_device_workq_function(struct work_struct
*work_req
)
337 struct delayed_work
*d_work
= (struct delayed_work
*)work_req
;
338 struct edac_device_ctl_info
*edac_dev
= to_edac_device_ctl_work(d_work
);
340 //debugf0("%s() here and running\n", __func__);
343 /* Only poll controllers that are running polled and have a check */
344 if ((edac_dev
->op_state
== OP_RUNNING_POLL
) &&
345 (edac_dev
->edac_check
!= NULL
)) {
346 edac_dev
->edac_check(edac_dev
);
349 unlock_device_list();
352 queue_delayed_work(edac_workqueue
, &edac_dev
->work
, edac_dev
->delay
);
356 * edac_device_workq_setup
357 * initialize a workq item for this edac_device instance
358 * passing in the new delay period in msec
360 void edac_device_workq_setup(struct edac_device_ctl_info
*edac_dev
,
363 debugf0("%s()\n", __func__
);
365 edac_dev
->poll_msec
= msec
;
366 edac_calc_delay(edac_dev
); /* Calc delay jiffies */
368 INIT_DELAYED_WORK(&edac_dev
->work
, edac_device_workq_function
);
369 queue_delayed_work(edac_workqueue
, &edac_dev
->work
, edac_dev
->delay
);
373 * edac_device_workq_teardown
374 * stop the workq processing on this edac_dev
376 void edac_device_workq_teardown(struct edac_device_ctl_info
*edac_dev
)
380 status
= cancel_delayed_work(&edac_dev
->work
);
382 /* workq instance might be running, wait for it */
383 flush_workqueue(edac_workqueue
);
388 * edac_device_reset_delay_period
391 void edac_device_reset_delay_period(struct edac_device_ctl_info
*edac_dev
,
396 /* cancel the current workq request */
397 edac_device_workq_teardown(edac_dev
);
399 /* restart the workq request, with new delay value */
400 edac_device_workq_setup(edac_dev
, value
);
402 unlock_device_list();
406 * edac_device_add_device: Insert the 'edac_dev' structure into the
407 * edac_device global list and create sysfs entries associated with
408 * edac_device structure.
409 * @edac_device: pointer to the edac_device structure to be added to the list
410 * @edac_idx: A unique numeric identifier to be assigned to the
411 * 'edac_device' structure.
417 int edac_device_add_device(struct edac_device_ctl_info
*edac_dev
, int edac_idx
)
419 debugf0("%s()\n", __func__
);
421 edac_dev
->dev_idx
= edac_idx
;
422 #ifdef CONFIG_EDAC_DEBUG
423 if (edac_debug_level
>= 3)
424 edac_device_dump_device(edac_dev
);
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 "
458 "'%s': DEV '%s' (%s)\n",
462 edac_op_state_toString(edac_dev
->op_state
));
464 unlock_device_list();
468 /* Some error, so remove the entry from the lsit */
469 del_edac_device_from_global_list(edac_dev
);
472 unlock_device_list();
476 EXPORT_SYMBOL_GPL(edac_device_add_device
);
479 * edac_device_del_device:
480 * Remove sysfs entries for specified edac_device structure and
481 * then remove edac_device structure from global list
484 * Pointer to 'struct device' representing edac_device
485 * structure to remove.
488 * Pointer to removed edac_device structure,
489 * OR NULL if device not found.
491 struct edac_device_ctl_info
*edac_device_del_device(struct device
*dev
)
493 struct edac_device_ctl_info
*edac_dev
;
495 debugf0("MC: %s()\n", __func__
);
499 if ((edac_dev
= find_edac_device_by_dev(dev
)) == NULL
) {
500 unlock_device_list();
504 /* mark this instance as OFFLINE */
505 edac_dev
->op_state
= OP_OFFLINE
;
507 /* clear workq processing on this instance */
508 edac_device_workq_teardown(edac_dev
);
510 /* Tear down the sysfs entries for this instance */
511 edac_device_remove_sysfs(edac_dev
);
513 /* deregister from global list */
514 del_edac_device_from_global_list(edac_dev
);
516 unlock_device_list();
518 edac_printk(KERN_INFO
, EDAC_MC
,
519 "Removed device %d for %s %s: DEV %s\n",
521 edac_dev
->mod_name
, edac_dev
->ctl_name
, dev_name(edac_dev
));
526 EXPORT_SYMBOL_GPL(edac_device_del_device
);
528 static inline int edac_device_get_log_ce(struct edac_device_ctl_info
*edac_dev
)
530 return edac_dev
->log_ce
;
533 static inline int edac_device_get_log_ue(struct edac_device_ctl_info
*edac_dev
)
535 return edac_dev
->log_ue
;
538 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
541 return edac_dev
->panic_on_ue
;
545 * edac_device_handle_ce
546 * perform a common output and handling of an 'edac_dev' CE event
548 void edac_device_handle_ce(struct edac_device_ctl_info
*edac_dev
,
549 int inst_nr
, int block_nr
, const char *msg
)
551 struct edac_device_instance
*instance
;
552 struct edac_device_block
*block
= NULL
;
554 if ((inst_nr
>= edac_dev
->nr_instances
) || (inst_nr
< 0)) {
555 edac_device_printk(edac_dev
, KERN_ERR
,
556 "INTERNAL ERROR: 'instance' out of range "
557 "(%d >= %d)\n", inst_nr
,
558 edac_dev
->nr_instances
);
562 instance
= edac_dev
->instances
+ inst_nr
;
564 if ((block_nr
>= instance
->nr_blocks
) || (block_nr
< 0)) {
565 edac_device_printk(edac_dev
, KERN_ERR
,
566 "INTERNAL ERROR: instance %d 'block' "
567 "out of range (%d >= %d)\n",
569 instance
->nr_blocks
);
573 if (instance
->nr_blocks
> 0) {
574 block
= instance
->blocks
+ block_nr
;
575 block
->counters
.ce_count
++;
578 /* Propogate the count up the 'totals' tree */
579 instance
->counters
.ce_count
++;
580 edac_dev
->counters
.ce_count
++;
582 if (edac_device_get_log_ce(edac_dev
))
583 edac_device_printk(edac_dev
, KERN_WARNING
,
584 "CE: %s instance: %s block: %s '%s'\n",
585 edac_dev
->ctl_name
, instance
->name
,
586 block
? block
->name
: "N/A", msg
);
589 EXPORT_SYMBOL_GPL(edac_device_handle_ce
);
592 * edac_device_handle_ue
593 * perform a common output and handling of an 'edac_dev' UE event
595 void edac_device_handle_ue(struct edac_device_ctl_info
*edac_dev
,
596 int inst_nr
, int block_nr
, const char *msg
)
598 struct edac_device_instance
*instance
;
599 struct edac_device_block
*block
= NULL
;
601 if ((inst_nr
>= edac_dev
->nr_instances
) || (inst_nr
< 0)) {
602 edac_device_printk(edac_dev
, KERN_ERR
,
603 "INTERNAL ERROR: 'instance' out of range "
604 "(%d >= %d)\n", inst_nr
,
605 edac_dev
->nr_instances
);
609 instance
= edac_dev
->instances
+ inst_nr
;
611 if ((block_nr
>= instance
->nr_blocks
) || (block_nr
< 0)) {
612 edac_device_printk(edac_dev
, KERN_ERR
,
613 "INTERNAL ERROR: instance %d 'block' "
614 "out of range (%d >= %d)\n",
616 instance
->nr_blocks
);
620 if (instance
->nr_blocks
> 0) {
621 block
= instance
->blocks
+ block_nr
;
622 block
->counters
.ue_count
++;
625 /* Propogate the count up the 'totals' tree */
626 instance
->counters
.ue_count
++;
627 edac_dev
->counters
.ue_count
++;
629 if (edac_device_get_log_ue(edac_dev
))
630 edac_device_printk(edac_dev
, KERN_EMERG
,
631 "UE: %s instance: %s block: %s '%s'\n",
632 edac_dev
->ctl_name
, instance
->name
,
633 block
? block
->name
: "N/A", msg
);
635 if (edac_device_get_panic_on_ue(edac_dev
))
636 panic("EDAC %s: UE instance: %s block %s '%s'\n",
637 edac_dev
->ctl_name
, instance
->name
,
638 block
? block
->name
: "N/A", msg
);
641 EXPORT_SYMBOL_GPL(edac_device_handle_ue
);