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