3f4c8a28154a3d5da5d2c54d27d6d8fa531b5e44
[powerpc.git] / 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_op_state_toString(edac_dev)
423  */
424 static char *edac_op_state_toString(struct edac_device_ctl_info *edac_dev)
425 {
426         int opstate = edac_dev->op_state;
427
428         if (opstate == OP_RUNNING_POLL)
429                 return "POLLED";
430         else if (opstate == OP_RUNNING_INTERRUPT)
431                 return "INTERRUPT";
432         else if (opstate == OP_RUNNING_POLL_INTR)
433                 return "POLL-INTR";
434         else if (opstate == OP_ALLOC)
435                 return "ALLOC";
436         else if (opstate == OP_OFFLINE)
437                 return "OFFLINE";
438
439         return "UNKNOWN";
440 }
441
442 /**
443  * edac_device_add_device: Insert the 'edac_dev' structure into the
444  * edac_device global list and create sysfs entries associated with
445  * edac_device structure.
446  * @edac_device: pointer to the edac_device structure to be added to the list
447  * @edac_idx: A unique numeric identifier to be assigned to the
448  * 'edac_device' structure.
449  *
450  * Return:
451  *      0       Success
452  *      !0      Failure
453  */
454 int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
455 {
456         debugf0("%s()\n", __func__);
457
458         edac_dev->dev_idx = edac_idx;
459 #ifdef CONFIG_EDAC_DEBUG
460         if (edac_debug_level >= 3)
461                 edac_device_dump_device(edac_dev);
462 #endif
463         lock_device_list();
464
465         if (add_edac_dev_to_global_list(edac_dev))
466                 goto fail0;
467
468         /* set load time so that error rate can be tracked */
469         edac_dev->start_time = jiffies;
470
471         /* create this instance's sysfs entries */
472         if (edac_device_create_sysfs(edac_dev)) {
473                 edac_device_printk(edac_dev, KERN_WARNING,
474                         "failed to create sysfs device\n");
475                 goto fail1;
476         }
477
478         /* If there IS a check routine, then we are running POLLED */
479         if (edac_dev->edac_check != NULL) {
480                 /* This instance is NOW RUNNING */
481                 edac_dev->op_state = OP_RUNNING_POLL;
482
483                 /*
484                  * enable workq processing on this instance,
485                  * default = 1000 msec
486                  */
487                 edac_device_workq_setup(edac_dev, 1000);
488         } else {
489                 edac_dev->op_state = OP_RUNNING_INTERRUPT;
490         }
491
492
493         /* Report action taken */
494         edac_device_printk(edac_dev, KERN_INFO,
495                 "Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
496                 edac_dev->mod_name,
497                 edac_dev->ctl_name,
498                 dev_name(edac_dev),
499                 edac_op_state_toString(edac_dev)
500                 );
501
502         unlock_device_list();
503         return 0;
504
505 fail1:
506         /* Some error, so remove the entry from the lsit */
507         del_edac_device_from_global_list(edac_dev);
508
509 fail0:
510         unlock_device_list();
511         return 1;
512 }
513 EXPORT_SYMBOL_GPL(edac_device_add_device);
514
515 /**
516  * edac_device_del_device:
517  *      Remove sysfs entries for specified edac_device structure and
518  *      then remove edac_device structure from global list
519  *
520  * @pdev:
521  *      Pointer to 'struct device' representing edac_device
522  *      structure to remove.
523  *
524  * Return:
525  *      Pointer to removed edac_device structure,
526  *      OR NULL if device not found.
527  */
528 struct edac_device_ctl_info * edac_device_del_device(struct device *dev)
529 {
530         struct edac_device_ctl_info *edac_dev;
531
532         debugf0("MC: %s()\n", __func__);
533
534         lock_device_list();
535
536         if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
537                 unlock_device_list();
538                 return NULL;
539         }
540
541         /* mark this instance as OFFLINE */
542         edac_dev->op_state = OP_OFFLINE;
543
544         /* clear workq processing on this instance */
545         edac_device_workq_teardown(edac_dev);
546
547         /* Tear down the sysfs entries for this instance */
548         edac_device_remove_sysfs(edac_dev);
549
550         /* deregister from global list */
551         del_edac_device_from_global_list(edac_dev);
552
553         unlock_device_list();
554
555         edac_printk(KERN_INFO, EDAC_MC,
556                 "Removed device %d for %s %s: DEV %s\n",
557                 edac_dev->dev_idx,
558                 edac_dev->mod_name,
559                 edac_dev->ctl_name,
560                 dev_name(edac_dev));
561
562         return edac_dev;
563 }
564 EXPORT_SYMBOL_GPL(edac_device_del_device);
565
566
567 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
568 {
569         return edac_dev->log_ce;
570 }
571
572 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
573 {
574         return edac_dev->log_ue;
575 }
576
577 static inline int edac_device_get_panic_on_ue(
578                 struct edac_device_ctl_info *edac_dev)
579 {
580         return edac_dev->panic_on_ue;
581 }
582
583 /*
584  * edac_device_handle_ce
585  *      perform a common output and handling of an 'edac_dev' CE event
586  */
587 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
588                 int inst_nr, int block_nr, const char *msg)
589 {
590         struct edac_device_instance *instance;
591         struct edac_device_block *block = NULL;
592
593         if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
594                 edac_device_printk(edac_dev, KERN_ERR,
595                         "INTERNAL ERROR: 'instance' out of range "
596                         "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
597                 return;
598         }
599
600         instance = edac_dev->instances + inst_nr;
601
602         if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
603                 edac_device_printk(edac_dev, KERN_ERR,
604                         "INTERNAL ERROR: instance %d 'block' out of range "
605                         "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
606                 return;
607         }
608
609         if (instance->nr_blocks > 0) {
610                 block = instance->blocks + block_nr;
611                 block->counters.ce_count++;
612         }
613
614         /* Propogate the count up the 'totals' tree */
615         instance->counters.ce_count++;
616         edac_dev->counters.ce_count++;
617
618         if (edac_device_get_log_ce(edac_dev))
619                 edac_device_printk(edac_dev, KERN_WARNING,
620                 "CE ctl: %s, instance: %s, block: %s: %s\n",
621                 edac_dev->ctl_name, instance->name,
622                 block ? block->name : "N/A", msg);
623 }
624 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
625
626 /*
627  * edac_device_handle_ue
628  *      perform a common output and handling of an 'edac_dev' UE event
629  */
630 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
631                 int inst_nr, int block_nr, const char *msg)
632 {
633         struct edac_device_instance *instance;
634         struct edac_device_block *block = NULL;
635
636         if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
637                 edac_device_printk(edac_dev, KERN_ERR,
638                         "INTERNAL ERROR: 'instance' out of range "
639                         "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
640                 return;
641         }
642
643         instance = edac_dev->instances + inst_nr;
644
645         if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
646                 edac_device_printk(edac_dev, KERN_ERR,
647                         "INTERNAL ERROR: instance %d 'block' out of range "
648                         "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
649                 return;
650         }
651
652         if (instance->nr_blocks > 0) {
653                 block = instance->blocks + block_nr;
654                 block->counters.ue_count++;
655         }
656
657         /* Propogate the count up the 'totals' tree */
658         instance->counters.ue_count++;
659         edac_dev->counters.ue_count++;
660
661         if (edac_device_get_log_ue(edac_dev))
662                 edac_device_printk(edac_dev, KERN_EMERG,
663                 "UE ctl: %s, instance: %s, block: %s: %s\n",
664                 edac_dev->ctl_name, instance->name,
665                 block ? block->name : "N/A", msg);
666
667         if (edac_device_get_panic_on_ue(edac_dev))
668                 panic("EDAC %s: UE instance: %s, block %s: %s\n",
669                         edac_dev->ctl_name, instance->name,
670                         block ? block->name : "N/A", msg);
671 }
672 EXPORT_SYMBOL_GPL(edac_device_handle_ue);
673