]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/edac/edac_device.c
drivers/edac: cleanup workq ifdefs
[mirror_ubuntu-bionic-kernel.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 static inline void lock_device_list(void)
39 {
40 down(&device_ctls_mutex);
41 }
42
43 static inline void unlock_device_list(void)
44 {
45 up(&device_ctls_mutex);
46 }
47
48 #ifdef CONFIG_EDAC_DEBUG
49 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
50 {
51 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
52 debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
53 debugf3("\tdev = %p\n", edac_dev->dev);
54 debugf3("\tmod_name:ctl_name = %s:%s\n",
55 edac_dev->mod_name, edac_dev->ctl_name);
56 debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
57 }
58 #endif /* CONFIG_EDAC_DEBUG */
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 */
65 struct 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,
72 struct edac_attrib_spec
73 *attrib_spec,
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",
86 __func__, nr_instances, nr_blocks);
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 */
93 dev_ctl = (struct edac_device_ctl_info *)0;
94
95 /* Calc the 'end' offset past the ctl_info structure */
96 dev_inst = (struct edac_device_instance *)
97 edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
98
99 /* Calc the 'end' offset past the instance array */
100 dev_blk = (struct edac_device_block *)
101 edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
102
103 /* Calc the 'end' offset past the dev_blk array */
104 count = nr_instances * nr_blocks;
105 dev_attrib = (struct edac_attrib *)
106 edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
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 */
113 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
114 total_size = ((unsigned long)pvt) + sz_private;
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 *)
124 (((char *)dev_ctl) + ((unsigned long)dev_inst));
125 dev_blk = (struct edac_device_block *)
126 (((char *)dev_ctl) + ((unsigned long)dev_blk));
127 dev_attrib = (struct edac_attrib *)
128 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
129 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
130
131 memset(dev_ctl, 0, total_size); /* clear all fields */
132 dev_ctl->nr_instances = nr_instances;
133 dev_ctl->instances = dev_inst;
134 dev_ctl->pvt_info = pvt;
135
136 /* Name of this edac device, ensure null terminated */
137 snprintf(dev_ctl->name, sizeof(dev_ctl->name), "%s", edac_device_name);
138 dev_ctl->name[sizeof(dev_ctl->name) - 1] = '\0';
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),
150 "%s%u", edac_device_name, instance);
151 inst->name[sizeof(inst->name) - 1] = '\0';
152
153 /* Initialize every block in each instance */
154 for (block = 0; block < nr_blocks; block++) {
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),
161 "%s%d", edac_block_name, block + 1);
162 blk->name[sizeof(blk->name) - 1] = '\0';
163
164 debugf1("%s() instance=%d block=%d name=%s\n",
165 __func__, instance, block, blk->name);
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
177 */
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 }
189
190 EXPORT_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 */
197 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
198 {
199 kfree(ctl_info);
200 }
201
202 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
203
204 /*
205 * find_edac_device_by_dev
206 * scans the edac_device list for a specific 'struct device *'
207 */
208 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
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 */
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 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
260 fail0:
261 edac_printk(KERN_WARNING, EDAC_MC,
262 "%s (%s) %s %s already assigned %d\n",
263 rover->dev->bus_id, dev_name(rover),
264 rover->mod_name, rover->ctl_name, rover->dev_idx);
265 return 1;
266
267 fail1:
268 edac_printk(KERN_WARNING, EDAC_MC,
269 "bug in low-level driver: attempt to assign\n"
270 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
271 __func__);
272 return 1;
273 }
274
275 /*
276 * complete_edac_device_list_del
277 */
278 static 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 */
290 static void del_edac_device_from_global_list(struct edac_device_ctl_info
291 *edac_device)
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 */
308 struct edac_device_ctl_info *edac_device_find(int idx)
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 }
328
329 EXPORT_SYMBOL(edac_device_find);
330
331 /*
332 * edac_device_workq_function
333 * performs the operation scheduled by a workq request
334 */
335 static void edac_device_workq_function(struct work_struct *work_req)
336 {
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);
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) &&
345 (edac_dev->edac_check != NULL)) {
346 edac_dev->edac_check(edac_dev);
347 }
348
349 unlock_device_list();
350
351 /* Reschedule */
352 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
353 }
354
355 /*
356 * edac_device_workq_setup
357 * initialize a workq item for this edac_device instance
358 * passing in the new delay period in msec
359 */
360 void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
361 unsigned msec)
362 {
363 debugf0("%s()\n", __func__);
364
365 edac_dev->poll_msec = msec;
366 edac_calc_delay(edac_dev); /* Calc delay jiffies */
367
368 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
369 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
370 }
371
372 /*
373 * edac_device_workq_teardown
374 * stop the workq processing on this edac_dev
375 */
376 void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
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
391 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
392 unsigned long value)
393 {
394 lock_device_list();
395
396 /* cancel the current workq request */
397 edac_device_workq_teardown(edac_dev);
398
399 /* restart the workq request, with new delay value */
400 edac_device_workq_setup(edac_dev, value);
401
402 unlock_device_list();
403 }
404
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 */
417 int 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,
437 "failed to create sysfs device\n");
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
446 /*
447 * enable workq processing on this instance,
448 * default = 1000 msec
449 */
450 edac_device_workq_setup(edac_dev, 1000);
451 } else {
452 edac_dev->op_state = OP_RUNNING_INTERRUPT;
453 }
454
455 /* Report action taken */
456 edac_device_printk(edac_dev, KERN_INFO,
457 "Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
458 edac_dev->mod_name,
459 edac_dev->ctl_name,
460 dev_name(edac_dev),
461 edac_op_state_toString(edac_dev->op_state)
462 );
463
464 unlock_device_list();
465 return 0;
466
467 fail1:
468 /* Some error, so remove the entry from the lsit */
469 del_edac_device_from_global_list(edac_dev);
470
471 fail0:
472 unlock_device_list();
473 return 1;
474 }
475
476 EXPORT_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 */
491 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
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 */
508 edac_device_workq_teardown(edac_dev);
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,
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));
522
523 return edac_dev;
524 }
525
526 EXPORT_SYMBOL_GPL(edac_device_del_device);
527
528 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
529 {
530 return edac_dev->log_ce;
531 }
532
533 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
534 {
535 return edac_dev->log_ue;
536 }
537
538 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
539 *edac_dev)
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 */
548 void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
549 int inst_nr, int block_nr, const char *msg)
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,
556 "INTERNAL ERROR: 'instance' out of range "
557 "(%d >= %d)\n", inst_nr,
558 edac_dev->nr_instances);
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,
566 "INTERNAL ERROR: instance %d 'block' out of range "
567 "(%d >= %d)\n", inst_nr, block_nr,
568 instance->nr_blocks);
569 return;
570 }
571
572 if (instance->nr_blocks > 0) {
573 block = instance->blocks + block_nr;
574 block->counters.ce_count++;
575 }
576
577 /* Propogate the count up the 'totals' tree */
578 instance->counters.ce_count++;
579 edac_dev->counters.ce_count++;
580
581 if (edac_device_get_log_ce(edac_dev))
582 edac_device_printk(edac_dev, KERN_WARNING,
583 "CE ctl: %s, instance: %s, block: %s: %s\n",
584 edac_dev->ctl_name, instance->name,
585 block ? block->name : "N/A", msg);
586 }
587
588 EXPORT_SYMBOL_GPL(edac_device_handle_ce);
589
590 /*
591 * edac_device_handle_ue
592 * perform a common output and handling of an 'edac_dev' UE event
593 */
594 void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
595 int inst_nr, int block_nr, const char *msg)
596 {
597 struct edac_device_instance *instance;
598 struct edac_device_block *block = NULL;
599
600 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
601 edac_device_printk(edac_dev, KERN_ERR,
602 "INTERNAL ERROR: 'instance' out of range "
603 "(%d >= %d)\n", inst_nr,
604 edac_dev->nr_instances);
605 return;
606 }
607
608 instance = edac_dev->instances + inst_nr;
609
610 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
611 edac_device_printk(edac_dev, KERN_ERR,
612 "INTERNAL ERROR: instance %d 'block' out of range "
613 "(%d >= %d)\n", inst_nr, block_nr,
614 instance->nr_blocks);
615 return;
616 }
617
618 if (instance->nr_blocks > 0) {
619 block = instance->blocks + block_nr;
620 block->counters.ue_count++;
621 }
622
623 /* Propogate the count up the 'totals' tree */
624 instance->counters.ue_count++;
625 edac_dev->counters.ue_count++;
626
627 if (edac_device_get_log_ue(edac_dev))
628 edac_device_printk(edac_dev, KERN_EMERG,
629 "UE ctl: %s, instance: %s, block: %s: %s\n",
630 edac_dev->ctl_name, instance->name,
631 block ? block->name : "N/A", msg);
632
633 if (edac_device_get_panic_on_ue(edac_dev))
634 panic("EDAC %s: UE instance: %s, block %s: %s\n",
635 edac_dev->ctl_name, instance->name,
636 block ? block->name : "N/A", msg);
637 }
638
639 EXPORT_SYMBOL_GPL(edac_device_handle_ue);