drivers/edac: i5000 code tidying
[deliverable/linux.git] / drivers / edac / edac_device.c
CommitLineData
e27e3dac
DT
1
2/*
3 * edac_device.c
4 * (C) 2007 www.douglaskthompson.com
5 *
6 * This file may be distributed under the terms of the
7 * GNU General Public License.
8 *
9 * Written by Doug Thompson <norsk5@xmission.com>
10 *
11 * edac_device API implementation
12 * 19 Jan 2007
13 */
14
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>
29#include <asm/page.h>
30
31#include "edac_core.h"
32#include "edac_module.h"
33
34/* lock to memory controller's control array */
35static DECLARE_MUTEX(device_ctls_mutex);
36static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
37
e27e3dac
DT
38static inline void lock_device_list(void)
39{
40 down(&device_ctls_mutex);
41}
42
43static inline void unlock_device_list(void)
44{
45 up(&device_ctls_mutex);
46}
47
e27e3dac
DT
48#ifdef CONFIG_EDAC_DEBUG
49static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
50{
079708b9 51 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
e27e3dac
DT
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);
57}
079708b9 58#endif /* CONFIG_EDAC_DEBUG */
e27e3dac
DT
59
60/*
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.
64 */
65struct edac_device_ctl_info *edac_device_alloc_ctl_info(
66 unsigned sz_private,
67 char *edac_device_name,
68 unsigned nr_instances,
69 char *edac_block_name,
70 unsigned nr_blocks,
71 unsigned offset_value,
079708b9
DT
72 struct edac_attrib_spec
73 *attrib_spec,
e27e3dac
DT
74 unsigned nr_attribs)
75{
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;
80 unsigned total_size;
81 unsigned count;
82 unsigned instance, block, attr;
83 void *pvt;
84
85 debugf1("%s() instances=%d blocks=%d\n",
079708b9 86 __func__, nr_instances, nr_blocks);
e27e3dac
DT
87
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.
92 */
079708b9 93 dev_ctl = (struct edac_device_ctl_info *)0;
e27e3dac
DT
94
95 /* Calc the 'end' offset past the ctl_info structure */
96 dev_inst = (struct edac_device_instance *)
052dfb45 97 edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
e27e3dac
DT
98
99 /* Calc the 'end' offset past the instance array */
100 dev_blk = (struct edac_device_block *)
052dfb45 101 edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
e27e3dac
DT
102
103 /* Calc the 'end' offset past the dev_blk array */
104 count = nr_instances * nr_blocks;
105 dev_attrib = (struct edac_attrib *)
052dfb45 106 edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
e27e3dac
DT
107
108 /* Check for case of NO attributes specified */
109 if (nr_attribs > 0)
110 count *= nr_attribs;
111
112 /* Calc the 'end' offset past the attributes array */
079708b9
DT
113 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
114 total_size = ((unsigned long)pvt) + sz_private;
e27e3dac
DT
115
116 /* Allocate the amount of memory for the set of control structures */
117 if ((dev_ctl = kmalloc(total_size, GFP_KERNEL)) == NULL)
118 return NULL;
119
120 /* Adjust pointers so they point within the memory we just allocated
121 * rather than an imaginary chunk of memory located at address 0.
122 */
123 dev_inst = (struct edac_device_instance *)
052dfb45 124 (((char *)dev_ctl) + ((unsigned long)dev_inst));
e27e3dac 125 dev_blk = (struct edac_device_block *)
052dfb45 126 (((char *)dev_ctl) + ((unsigned long)dev_blk));
e27e3dac 127 dev_attrib = (struct edac_attrib *)
052dfb45 128 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
079708b9 129 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
e27e3dac 130
079708b9 131 memset(dev_ctl, 0, total_size); /* clear all fields */
e27e3dac
DT
132 dev_ctl->nr_instances = nr_instances;
133 dev_ctl->instances = dev_inst;
134 dev_ctl->pvt_info = pvt;
135
136 /* Name of this edac device, ensure null terminated */
079708b9
DT
137 snprintf(dev_ctl->name, sizeof(dev_ctl->name), "%s", edac_device_name);
138 dev_ctl->name[sizeof(dev_ctl->name) - 1] = '\0';
e27e3dac
DT
139
140 /* Initialize every Instance */
141 for (instance = 0; instance < nr_instances; instance++) {
142 inst = &dev_inst[instance];
143 inst->ctl = dev_ctl;
144 inst->nr_blocks = nr_blocks;
145 blk_p = &dev_blk[instance * nr_blocks];
146 inst->blocks = blk_p;
147
148 /* name of this instance */
149 snprintf(inst->name, sizeof(inst->name),
079708b9
DT
150 "%s%u", edac_device_name, instance);
151 inst->name[sizeof(inst->name) - 1] = '\0';
e27e3dac
DT
152
153 /* Initialize every block in each instance */
079708b9 154 for (block = 0; block < nr_blocks; block++) {
e27e3dac
DT
155 blk = &blk_p[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),
d391a7b8 161 "%s%d", edac_block_name, block+offset_value);
079708b9 162 blk->name[sizeof(blk->name) - 1] = '\0';
e27e3dac
DT
163
164 debugf1("%s() instance=%d block=%d name=%s\n",
079708b9 165 __func__, instance, block, blk->name);
e27e3dac
DT
166
167 if (attrib_spec != NULL) {
168 /* when there is an attrib_spec passed int then
169 * Initialize every attrib of each block
170 */
171 for (attr = 0; attr < nr_attribs; attr++) {
172 attrib = &attrib_p[attr];
173 attrib->block = blk;
174
175 /* Link each attribute to the caller's
176 * spec entry, for name and type
079708b9 177 */
e27e3dac
DT
178 attrib->spec = &attrib_spec[attr];
179 }
180 }
181 }
182 }
183
184 /* Mark this instance as merely ALLOCATED */
185 dev_ctl->op_state = OP_ALLOC;
186
187 return dev_ctl;
188}
079708b9 189
e27e3dac
DT
190EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
191
192/*
193 * edac_device_free_ctl_info()
194 * frees the memory allocated by the edac_device_alloc_ctl_info()
195 * function
196 */
079708b9
DT
197void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
198{
e27e3dac
DT
199 kfree(ctl_info);
200}
e27e3dac 201
079708b9 202EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
e27e3dac
DT
203
204/*
205 * find_edac_device_by_dev
206 * scans the edac_device list for a specific 'struct device *'
207 */
079708b9 208static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
e27e3dac
DT
209{
210 struct edac_device_ctl_info *edac_dev;
211 struct list_head *item;
212
213 debugf3("%s()\n", __func__);
214
215 list_for_each(item, &edac_device_list) {
216 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
217
218 if (edac_dev->dev == dev)
219 return edac_dev;
220 }
221
222 return NULL;
223}
224
225/*
226 * add_edac_dev_to_global_list
227 * Before calling this function, caller must
228 * assign a unique value to edac_dev->dev_idx.
229 * Return:
230 * 0 on success
231 * 1 on failure.
232 */
079708b9 233static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
e27e3dac
DT
234{
235 struct list_head *item, *insert_before;
236 struct edac_device_ctl_info *rover;
237
238 insert_before = &edac_device_list;
239
240 /* Determine if already on the list */
241 if (unlikely((rover = find_edac_device_by_dev(edac_dev->dev)) != NULL))
242 goto fail0;
243
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);
247
248 if (rover->dev_idx >= edac_dev->dev_idx) {
249 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
250 goto fail1;
251
252 insert_before = item;
253 break;
254 }
255 }
256
257 list_add_tail_rcu(&edac_dev->link, insert_before);
258 return 0;
259
052dfb45 260fail0:
e27e3dac 261 edac_printk(KERN_WARNING, EDAC_MC,
052dfb45
DT
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);
e27e3dac
DT
265 return 1;
266
052dfb45 267fail1:
e27e3dac 268 edac_printk(KERN_WARNING, EDAC_MC,
052dfb45
DT
269 "bug in low-level driver: attempt to assign\n"
270 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
271 __func__);
e27e3dac
DT
272 return 1;
273}
274
275/*
276 * complete_edac_device_list_del
277 */
278static void complete_edac_device_list_del(struct rcu_head *head)
279{
280 struct edac_device_ctl_info *edac_dev;
281
282 edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
283 INIT_LIST_HEAD(&edac_dev->link);
284 complete(&edac_dev->complete);
285}
286
287/*
288 * del_edac_device_from_global_list
289 */
079708b9 290static void del_edac_device_from_global_list(struct edac_device_ctl_info
052dfb45 291 *edac_device)
e27e3dac
DT
292{
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);
297}
298
299/**
300 * edac_device_find
301 * Search for a edac_device_ctl_info structure whose index is 'idx'.
302 *
303 * If found, return a pointer to the structure.
304 * Else return NULL.
305 *
306 * Caller must hold device_ctls_mutex.
307 */
079708b9 308struct edac_device_ctl_info *edac_device_find(int idx)
e27e3dac
DT
309{
310 struct list_head *item;
311 struct edac_device_ctl_info *edac_dev;
312
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);
316
317 if (edac_dev->dev_idx >= idx) {
318 if (edac_dev->dev_idx == idx)
319 return edac_dev;
320
321 /* not on list, so terminate early */
322 break;
323 }
324 }
325
326 return NULL;
327}
e27e3dac 328
079708b9 329EXPORT_SYMBOL(edac_device_find);
e27e3dac
DT
330
331/*
81d87cb1 332 * edac_device_workq_function
e27e3dac
DT
333 * performs the operation scheduled by a workq request
334 */
81d87cb1 335static void edac_device_workq_function(struct work_struct *work_req)
e27e3dac 336{
079708b9
DT
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);
e27e3dac
DT
339
340 //debugf0("%s() here and running\n", __func__);
341 lock_device_list();
342
343 /* Only poll controllers that are running polled and have a check */
344 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
052dfb45
DT
345 (edac_dev->edac_check != NULL)) {
346 edac_dev->edac_check(edac_dev);
e27e3dac
DT
347 }
348
349 unlock_device_list();
350
351 /* Reschedule */
079708b9 352 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
e27e3dac
DT
353}
354
355/*
81d87cb1 356 * edac_device_workq_setup
e27e3dac
DT
357 * initialize a workq item for this edac_device instance
358 * passing in the new delay period in msec
359 */
81d87cb1 360void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
052dfb45 361 unsigned msec)
e27e3dac
DT
362{
363 debugf0("%s()\n", __func__);
364
365 edac_dev->poll_msec = msec;
81d87cb1 366 edac_calc_delay(edac_dev); /* Calc delay jiffies */
e27e3dac 367
81d87cb1 368 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
81d87cb1 369 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
e27e3dac
DT
370}
371
372/*
81d87cb1 373 * edac_device_workq_teardown
e27e3dac
DT
374 * stop the workq processing on this edac_dev
375 */
81d87cb1 376void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
e27e3dac
DT
377{
378 int status;
379
380 status = cancel_delayed_work(&edac_dev->work);
381 if (status == 0) {
382 /* workq instance might be running, wait for it */
383 flush_workqueue(edac_workqueue);
384 }
385}
386
387/*
388 * edac_device_reset_delay_period
389 */
390
079708b9 391void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
052dfb45 392 unsigned long value)
e27e3dac
DT
393{
394 lock_device_list();
395
396 /* cancel the current workq request */
81d87cb1 397 edac_device_workq_teardown(edac_dev);
e27e3dac
DT
398
399 /* restart the workq request, with new delay value */
81d87cb1 400 edac_device_workq_setup(edac_dev, value);
e27e3dac
DT
401
402 unlock_device_list();
403}
404
e27e3dac
DT
405/**
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.
412 *
413 * Return:
414 * 0 Success
415 * !0 Failure
416 */
417int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
418{
419 debugf0("%s()\n", __func__);
420
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);
425#endif
426 lock_device_list();
427
428 if (add_edac_dev_to_global_list(edac_dev))
429 goto fail0;
430
431 /* set load time so that error rate can be tracked */
432 edac_dev->start_time = jiffies;
433
434 /* create this instance's sysfs entries */
435 if (edac_device_create_sysfs(edac_dev)) {
436 edac_device_printk(edac_dev, KERN_WARNING,
052dfb45 437 "failed to create sysfs device\n");
e27e3dac
DT
438 goto fail1;
439 }
440
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;
445
81d87cb1
DJ
446 /*
447 * enable workq processing on this instance,
448 * default = 1000 msec
449 */
450 edac_device_workq_setup(edac_dev, 1000);
e27e3dac
DT
451 } else {
452 edac_dev->op_state = OP_RUNNING_INTERRUPT;
453 }
454
e27e3dac
DT
455 /* Report action taken */
456 edac_device_printk(edac_dev, KERN_INFO,
052dfb45
DT
457 "Giving out device to module '%s' controller "
458 "'%s': DEV '%s' (%s)\n",
459 edac_dev->mod_name,
460 edac_dev->ctl_name,
461 dev_name(edac_dev),
462 edac_op_state_toString(edac_dev->op_state));
e27e3dac
DT
463
464 unlock_device_list();
465 return 0;
466
052dfb45 467fail1:
e27e3dac
DT
468 /* Some error, so remove the entry from the lsit */
469 del_edac_device_from_global_list(edac_dev);
470
052dfb45 471fail0:
e27e3dac
DT
472 unlock_device_list();
473 return 1;
474}
079708b9 475
e27e3dac
DT
476EXPORT_SYMBOL_GPL(edac_device_add_device);
477
478/**
479 * edac_device_del_device:
480 * Remove sysfs entries for specified edac_device structure and
481 * then remove edac_device structure from global list
482 *
483 * @pdev:
484 * Pointer to 'struct device' representing edac_device
485 * structure to remove.
486 *
487 * Return:
488 * Pointer to removed edac_device structure,
489 * OR NULL if device not found.
490 */
079708b9 491struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
e27e3dac
DT
492{
493 struct edac_device_ctl_info *edac_dev;
494
495 debugf0("MC: %s()\n", __func__);
496
497 lock_device_list();
498
499 if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
500 unlock_device_list();
501 return NULL;
502 }
503
504 /* mark this instance as OFFLINE */
505 edac_dev->op_state = OP_OFFLINE;
506
507 /* clear workq processing on this instance */
81d87cb1 508 edac_device_workq_teardown(edac_dev);
e27e3dac
DT
509
510 /* Tear down the sysfs entries for this instance */
511 edac_device_remove_sysfs(edac_dev);
512
513 /* deregister from global list */
514 del_edac_device_from_global_list(edac_dev);
515
516 unlock_device_list();
517
518 edac_printk(KERN_INFO, EDAC_MC,
052dfb45
DT
519 "Removed device %d for %s %s: DEV %s\n",
520 edac_dev->dev_idx,
521 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
e27e3dac
DT
522
523 return edac_dev;
524}
e27e3dac 525
079708b9 526EXPORT_SYMBOL_GPL(edac_device_del_device);
e27e3dac
DT
527
528static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
529{
530 return edac_dev->log_ce;
531}
532
533static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
534{
535 return edac_dev->log_ue;
536}
537
079708b9 538static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
052dfb45 539 *edac_dev)
e27e3dac
DT
540{
541 return edac_dev->panic_on_ue;
542}
543
544/*
545 * edac_device_handle_ce
546 * perform a common output and handling of an 'edac_dev' CE event
547 */
548void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
052dfb45 549 int inst_nr, int block_nr, const char *msg)
e27e3dac
DT
550{
551 struct edac_device_instance *instance;
552 struct edac_device_block *block = NULL;
553
554 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
555 edac_device_printk(edac_dev, KERN_ERR,
052dfb45
DT
556 "INTERNAL ERROR: 'instance' out of range "
557 "(%d >= %d)\n", inst_nr,
558 edac_dev->nr_instances);
e27e3dac
DT
559 return;
560 }
561
562 instance = edac_dev->instances + inst_nr;
563
564 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
565 edac_device_printk(edac_dev, KERN_ERR,
052dfb45
DT
566 "INTERNAL ERROR: instance %d 'block' "
567 "out of range (%d >= %d)\n",
568 inst_nr, block_nr,
569 instance->nr_blocks);
e27e3dac
DT
570 return;
571 }
572
573 if (instance->nr_blocks > 0) {
574 block = instance->blocks + block_nr;
575 block->counters.ce_count++;
576 }
577
578 /* Propogate the count up the 'totals' tree */
579 instance->counters.ce_count++;
580 edac_dev->counters.ce_count++;
581
582 if (edac_device_get_log_ce(edac_dev))
583 edac_device_printk(edac_dev, KERN_WARNING,
052dfb45
DT
584 "CE: %s instance: %s block: %s '%s'\n",
585 edac_dev->ctl_name, instance->name,
586 block ? block->name : "N/A", msg);
e27e3dac 587}
079708b9 588
e27e3dac
DT
589EXPORT_SYMBOL_GPL(edac_device_handle_ce);
590
591/*
592 * edac_device_handle_ue
593 * perform a common output and handling of an 'edac_dev' UE event
594 */
595void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
052dfb45 596 int inst_nr, int block_nr, const char *msg)
e27e3dac
DT
597{
598 struct edac_device_instance *instance;
599 struct edac_device_block *block = NULL;
600
601 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
602 edac_device_printk(edac_dev, KERN_ERR,
052dfb45
DT
603 "INTERNAL ERROR: 'instance' out of range "
604 "(%d >= %d)\n", inst_nr,
605 edac_dev->nr_instances);
e27e3dac
DT
606 return;
607 }
608
609 instance = edac_dev->instances + inst_nr;
610
611 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
612 edac_device_printk(edac_dev, KERN_ERR,
052dfb45
DT
613 "INTERNAL ERROR: instance %d 'block' "
614 "out of range (%d >= %d)\n",
615 inst_nr, block_nr,
616 instance->nr_blocks);
e27e3dac
DT
617 return;
618 }
619
620 if (instance->nr_blocks > 0) {
621 block = instance->blocks + block_nr;
622 block->counters.ue_count++;
623 }
624
625 /* Propogate the count up the 'totals' tree */
626 instance->counters.ue_count++;
627 edac_dev->counters.ue_count++;
628
629 if (edac_device_get_log_ue(edac_dev))
630 edac_device_printk(edac_dev, KERN_EMERG,
052dfb45
DT
631 "UE: %s instance: %s block: %s '%s'\n",
632 edac_dev->ctl_name, instance->name,
633 block ? block->name : "N/A", msg);
e27e3dac
DT
634
635 if (edac_device_get_panic_on_ue(edac_dev))
d391a7b8 636 panic("EDAC %s: UE instance: %s block %s '%s'\n",
052dfb45
DT
637 edac_dev->ctl_name, instance->name,
638 block ? block->name : "N/A", msg);
e27e3dac 639}
e27e3dac 640
079708b9 641EXPORT_SYMBOL_GPL(edac_device_handle_ue);
This page took 0.053619 seconds and 5 git commands to generate.