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