1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <asm/page.h>
16#include <linux/uaccess.h>
17#include <linux/ctype.h>
18#include <linux/highmem.h>
19#include <linux/init.h>
20#include <linux/jiffies.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/smp.h>
24#include <linux/spinlock.h>
25#include <linux/sysctl.h>
26#include <linux/timer.h>
27
28#include "edac_device.h"
29#include "edac_module.h"
30
31
32
33
34static DEFINE_MUTEX(device_ctls_mutex);
35static LIST_HEAD(edac_device_list);
36
37#ifdef CONFIG_EDAC_DEBUG
38static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
39{
40 edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n",
41 edac_dev, edac_dev->dev_idx);
42 edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
43 edac_dbg(3, "\tdev = %p\n", edac_dev->dev);
44 edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
45 edac_dev->mod_name, edac_dev->ctl_name);
46 edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info);
47}
48#endif
49
50struct edac_device_ctl_info *edac_device_alloc_ctl_info(
51 unsigned sz_private,
52 char *edac_device_name, unsigned nr_instances,
53 char *edac_block_name, unsigned nr_blocks,
54 unsigned offset_value,
55 struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
56 int device_index)
57{
58 struct edac_device_ctl_info *dev_ctl;
59 struct edac_device_instance *dev_inst, *inst;
60 struct edac_device_block *dev_blk, *blk_p, *blk;
61 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
62 unsigned total_size;
63 unsigned count;
64 unsigned instance, block, attr;
65 void *pvt, *p;
66 int err;
67
68 edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
69
70
71
72
73
74
75
76
77 p = NULL;
78 dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
79
80
81
82
83 dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
84
85
86
87
88 count = nr_instances * nr_blocks;
89 dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
90
91
92
93
94
95 if (nr_attrib > 0)
96 count *= nr_attrib;
97 dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
98
99
100 pvt = edac_align_ptr(&p, sz_private, 1);
101
102
103
104
105
106 total_size = ((unsigned long)pvt) + sz_private;
107
108
109 dev_ctl = kzalloc(total_size, GFP_KERNEL);
110 if (dev_ctl == NULL)
111 return NULL;
112
113
114
115
116
117
118
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_dev_sysfs_block_attribute *)
125 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
126 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
127
128
129 dev_ctl->dev_idx = device_index;
130 dev_ctl->nr_instances = nr_instances;
131 dev_ctl->instances = dev_inst;
132 dev_ctl->pvt_info = pvt;
133
134
135 dev_ctl->log_ce = 1;
136 dev_ctl->log_ue = 1;
137
138
139 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
140
141 edac_dbg(4, "edac_dev=%p next after end=%p\n",
142 dev_ctl, pvt + sz_private);
143
144
145 for (instance = 0; instance < nr_instances; instance++) {
146 inst = &dev_inst[instance];
147 inst->ctl = dev_ctl;
148 inst->nr_blocks = nr_blocks;
149 blk_p = &dev_blk[instance * nr_blocks];
150 inst->blocks = blk_p;
151
152
153 snprintf(inst->name, sizeof(inst->name),
154 "%s%u", edac_device_name, instance);
155
156
157 for (block = 0; block < nr_blocks; block++) {
158 blk = &blk_p[block];
159 blk->instance = inst;
160 snprintf(blk->name, sizeof(blk->name),
161 "%s%d", edac_block_name, block+offset_value);
162
163 edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
164 instance, inst, block, blk, blk->name);
165
166
167
168
169 if ((nr_attrib == 0) || (attrib_spec == NULL))
170 continue;
171
172
173 blk->nr_attribs = nr_attrib;
174 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
175 blk->block_attributes = attrib_p;
176
177 edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
178 blk->block_attributes);
179
180
181
182
183
184
185 for (attr = 0; attr < nr_attrib; attr++) {
186 attrib = &attrib_p[attr];
187
188
189
190
191 attrib->attr = attrib_spec[attr].attr;
192 attrib->show = attrib_spec[attr].show;
193 attrib->store = attrib_spec[attr].store;
194
195 attrib->block = blk;
196
197 edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
198 attrib, attrib->attr.name,
199 &attrib_spec[attr],
200 attrib_spec[attr].attr.name
201 );
202 }
203 }
204 }
205
206
207 dev_ctl->op_state = OP_ALLOC;
208
209
210
211
212 err = edac_device_register_sysfs_main_kobj(dev_ctl);
213 if (err) {
214 kfree(dev_ctl);
215 return NULL;
216 }
217
218
219
220
221
222
223
224
225 return dev_ctl;
226}
227EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
228
229void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
230{
231 edac_device_unregister_sysfs_main_kobj(ctl_info);
232}
233EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
234
235
236
237
238
239
240
241
242
243
244
245static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
246{
247 struct edac_device_ctl_info *edac_dev;
248 struct list_head *item;
249
250 edac_dbg(0, "\n");
251
252 list_for_each(item, &edac_device_list) {
253 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
254
255 if (edac_dev->dev == dev)
256 return edac_dev;
257 }
258
259 return NULL;
260}
261
262
263
264
265
266
267
268
269
270
271
272
273static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
274{
275 struct list_head *item, *insert_before;
276 struct edac_device_ctl_info *rover;
277
278 insert_before = &edac_device_list;
279
280
281 rover = find_edac_device_by_dev(edac_dev->dev);
282 if (unlikely(rover != NULL))
283 goto fail0;
284
285
286 list_for_each(item, &edac_device_list) {
287 rover = list_entry(item, struct edac_device_ctl_info, link);
288
289 if (rover->dev_idx >= edac_dev->dev_idx) {
290 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
291 goto fail1;
292
293 insert_before = item;
294 break;
295 }
296 }
297
298 list_add_tail_rcu(&edac_dev->link, insert_before);
299 return 0;
300
301fail0:
302 edac_printk(KERN_WARNING, EDAC_MC,
303 "%s (%s) %s %s already assigned %d\n",
304 dev_name(rover->dev), edac_dev_name(rover),
305 rover->mod_name, rover->ctl_name, rover->dev_idx);
306 return 1;
307
308fail1:
309 edac_printk(KERN_WARNING, EDAC_MC,
310 "bug in low-level driver: attempt to assign\n"
311 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
312 __func__);
313 return 1;
314}
315
316
317
318
319static void del_edac_device_from_global_list(struct edac_device_ctl_info
320 *edac_device)
321{
322 list_del_rcu(&edac_device->link);
323
324
325
326
327 synchronize_rcu();
328 INIT_LIST_HEAD(&edac_device->link);
329}
330
331
332
333
334
335
336
337
338
339
340
341
342
343static void edac_device_workq_function(struct work_struct *work_req)
344{
345 struct delayed_work *d_work = to_delayed_work(work_req);
346 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
347
348 mutex_lock(&device_ctls_mutex);
349
350
351 if (edac_dev->op_state == OP_OFFLINE) {
352 mutex_unlock(&device_ctls_mutex);
353 return;
354 }
355
356
357 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
358 (edac_dev->edac_check != NULL)) {
359 edac_dev->edac_check(edac_dev);
360 }
361
362 mutex_unlock(&device_ctls_mutex);
363
364
365
366
367
368
369 if (edac_dev->poll_msec == 1000)
370 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
371 else
372 edac_queue_work(&edac_dev->work, edac_dev->delay);
373}
374
375
376
377
378
379
380static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
381 unsigned msec)
382{
383 edac_dbg(0, "\n");
384
385
386
387
388
389 edac_dev->poll_msec = msec;
390 edac_dev->delay = msecs_to_jiffies(msec);
391
392 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
393
394
395
396
397
398
399 if (edac_dev->poll_msec == 1000)
400 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
401 else
402 edac_queue_work(&edac_dev->work, edac_dev->delay);
403}
404
405
406
407
408
409static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
410{
411 if (!edac_dev->edac_check)
412 return;
413
414 edac_dev->op_state = OP_OFFLINE;
415
416 edac_stop_work(&edac_dev->work);
417}
418
419
420
421
422
423
424
425
426void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
427 unsigned long value)
428{
429 unsigned long jiffs = msecs_to_jiffies(value);
430
431 if (value == 1000)
432 jiffs = round_jiffies_relative(value);
433
434 edac_dev->poll_msec = value;
435 edac_dev->delay = jiffs;
436
437 edac_mod_work(&edac_dev->work, jiffs);
438}
439
440int edac_device_alloc_index(void)
441{
442 static atomic_t device_indexes = ATOMIC_INIT(0);
443
444 return atomic_inc_return(&device_indexes) - 1;
445}
446EXPORT_SYMBOL_GPL(edac_device_alloc_index);
447
448int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
449{
450 edac_dbg(0, "\n");
451
452#ifdef CONFIG_EDAC_DEBUG
453 if (edac_debug_level >= 3)
454 edac_device_dump_device(edac_dev);
455#endif
456 mutex_lock(&device_ctls_mutex);
457
458 if (add_edac_dev_to_global_list(edac_dev))
459 goto fail0;
460
461
462 edac_dev->start_time = jiffies;
463
464
465 if (edac_device_create_sysfs(edac_dev)) {
466 edac_device_printk(edac_dev, KERN_WARNING,
467 "failed to create sysfs device\n");
468 goto fail1;
469 }
470
471
472 if (edac_dev->edac_check != NULL) {
473
474 edac_dev->op_state = OP_RUNNING_POLL;
475
476
477
478
479
480 edac_device_workq_setup(edac_dev, 1000);
481 } else {
482 edac_dev->op_state = OP_RUNNING_INTERRUPT;
483 }
484
485
486 edac_device_printk(edac_dev, KERN_INFO,
487 "Giving out device to module %s controller %s: DEV %s (%s)\n",
488 edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
489 edac_op_state_to_string(edac_dev->op_state));
490
491 mutex_unlock(&device_ctls_mutex);
492 return 0;
493
494fail1:
495
496 del_edac_device_from_global_list(edac_dev);
497
498fail0:
499 mutex_unlock(&device_ctls_mutex);
500 return 1;
501}
502EXPORT_SYMBOL_GPL(edac_device_add_device);
503
504struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
505{
506 struct edac_device_ctl_info *edac_dev;
507
508 edac_dbg(0, "\n");
509
510 mutex_lock(&device_ctls_mutex);
511
512
513 edac_dev = find_edac_device_by_dev(dev);
514 if (edac_dev == NULL) {
515 mutex_unlock(&device_ctls_mutex);
516 return NULL;
517 }
518
519
520 edac_dev->op_state = OP_OFFLINE;
521
522
523 del_edac_device_from_global_list(edac_dev);
524
525 mutex_unlock(&device_ctls_mutex);
526
527
528 edac_device_workq_teardown(edac_dev);
529
530
531 edac_device_remove_sysfs(edac_dev);
532
533 edac_printk(KERN_INFO, EDAC_MC,
534 "Removed device %d for %s %s: DEV %s\n",
535 edac_dev->dev_idx,
536 edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
537
538 return edac_dev;
539}
540EXPORT_SYMBOL_GPL(edac_device_del_device);
541
542static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
543{
544 return edac_dev->log_ce;
545}
546
547static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
548{
549 return edac_dev->log_ue;
550}
551
552static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
553 *edac_dev)
554{
555 return edac_dev->panic_on_ue;
556}
557
558void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
559 unsigned int count, int inst_nr, int block_nr,
560 const char *msg)
561{
562 struct edac_device_instance *instance;
563 struct edac_device_block *block = NULL;
564
565 if (!count)
566 return;
567
568 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
569 edac_device_printk(edac_dev, KERN_ERR,
570 "INTERNAL ERROR: 'instance' out of range "
571 "(%d >= %d)\n", inst_nr,
572 edac_dev->nr_instances);
573 return;
574 }
575
576 instance = edac_dev->instances + inst_nr;
577
578 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
579 edac_device_printk(edac_dev, KERN_ERR,
580 "INTERNAL ERROR: instance %d 'block' "
581 "out of range (%d >= %d)\n",
582 inst_nr, block_nr,
583 instance->nr_blocks);
584 return;
585 }
586
587 if (instance->nr_blocks > 0) {
588 block = instance->blocks + block_nr;
589 block->counters.ce_count += count;
590 }
591
592
593 instance->counters.ce_count += count;
594 edac_dev->counters.ce_count += count;
595
596 if (edac_device_get_log_ce(edac_dev))
597 edac_device_printk(edac_dev, KERN_WARNING,
598 "CE: %s instance: %s block: %s count: %d '%s'\n",
599 edac_dev->ctl_name, instance->name,
600 block ? block->name : "N/A", count, msg);
601}
602EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
603
604void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
605 unsigned int count, int inst_nr, int block_nr,
606 const char *msg)
607{
608 struct edac_device_instance *instance;
609 struct edac_device_block *block = NULL;
610
611 if (!count)
612 return;
613
614 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
615 edac_device_printk(edac_dev, KERN_ERR,
616 "INTERNAL ERROR: 'instance' out of range "
617 "(%d >= %d)\n", inst_nr,
618 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' "
627 "out of range (%d >= %d)\n",
628 inst_nr, block_nr,
629 instance->nr_blocks);
630 return;
631 }
632
633 if (instance->nr_blocks > 0) {
634 block = instance->blocks + block_nr;
635 block->counters.ue_count += count;
636 }
637
638
639 instance->counters.ue_count += count;
640 edac_dev->counters.ue_count += count;
641
642 if (edac_device_get_log_ue(edac_dev))
643 edac_device_printk(edac_dev, KERN_EMERG,
644 "UE: %s instance: %s block: %s count: %d '%s'\n",
645 edac_dev->ctl_name, instance->name,
646 block ? block->name : "N/A", count, msg);
647
648 if (edac_device_get_panic_on_ue(edac_dev))
649 panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
650 edac_dev->ctl_name, instance->name,
651 block ? block->name : "N/A", count, msg);
652}
653EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);
654