blob: 3ccadda3e7233bcb8ab4cc4a781f170cec4dc9b9 [file] [log] [blame]
Douglas Thompsone27e3da2007-07-19 01:49:36 -07001
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>
Douglas Thompson52490c82007-07-19 01:50:20 -070023#include <linux/jiffies.h>
Douglas Thompsone27e3da2007-07-19 01:49:36 -070024#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
Douglas Thompson52490c82007-07-19 01:50:20 -070035/* lock to memory controller's control array 'edac_device_list' */
Douglas Thompsone27e3da2007-07-19 01:49:36 -070036static DECLARE_MUTEX(device_ctls_mutex);
37static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
38
Douglas Thompsone27e3da2007-07-19 01:49:36 -070039#ifdef CONFIG_EDAC_DEBUG
40static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
41{
Douglas Thompson079708b2007-07-19 01:49:58 -070042 debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070043 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}
Douglas Thompson079708b2007-07-19 01:49:58 -070049#endif /* CONFIG_EDAC_DEBUG */
Douglas Thompsone27e3da2007-07-19 01:49:36 -070050
51/*
Douglas Thompson52490c82007-07-19 01:50:20 -070052 * 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
Douglas Thompsone27e3da2007-07-19 01:49:36 -070064 */
65struct edac_device_ctl_info *edac_device_alloc_ctl_info(
66 unsigned sz_private,
Douglas Thompson52490c82007-07-19 01:50:20 -070067 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)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070071{
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",
Douglas Thompson079708b2007-07-19 01:49:58 -070082 __func__, nr_instances, nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070083
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 */
Douglas Thompson52490c82007-07-19 01:50:20 -070089 dev_ctl = (struct edac_device_ctl_info *)NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070090
91 /* Calc the 'end' offset past the ctl_info structure */
92 dev_inst = (struct edac_device_instance *)
Douglas Thompson052dfb42007-07-19 01:50:13 -070093 edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -070094
95 /* Calc the 'end' offset past the instance array */
96 dev_blk = (struct edac_device_block *)
Douglas Thompson052dfb42007-07-19 01:50:13 -070097 edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
Douglas Thompsone27e3da2007-07-19 01:49:36 -070098
99 /* Calc the 'end' offset past the dev_blk array */
100 count = nr_instances * nr_blocks;
101 dev_attrib = (struct edac_attrib *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700102 edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700103
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 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700109 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
110 total_size = ((unsigned long)pvt) + sz_private;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700111
112 /* Allocate the amount of memory for the set of control structures */
Douglas Thompson52490c82007-07-19 01:50:20 -0700113 dev_ctl = kzalloc(total_size, GFP_KERNEL);
114 if (dev_ctl == NULL)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700115 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 *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700121 (((char *)dev_ctl) + ((unsigned long)dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700122 dev_blk = (struct edac_device_block *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700123 (((char *)dev_ctl) + ((unsigned long)dev_blk));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700124 dev_attrib = (struct edac_attrib *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700125 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
Douglas Thompson079708b2007-07-19 01:49:58 -0700126 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700127
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700128 dev_ctl->nr_instances = nr_instances;
129 dev_ctl->instances = dev_inst;
130 dev_ctl->pvt_info = pvt;
131
Douglas Thompson52490c82007-07-19 01:50:20 -0700132 /* Name of this edac device */
133 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700134
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),
Douglas Thompson079708b2007-07-19 01:49:58 -0700145 "%s%u", edac_device_name, instance);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700146
147 /* Initialize every block in each instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700148 for (block = 0; block < nr_blocks; block++) {
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700149 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),
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700155 "%s%d", edac_block_name, block+offset_value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700156
157 debugf1("%s() instance=%d block=%d name=%s\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700158 __func__, instance, block, blk->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700159
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
Douglas Thompson079708b2007-07-19 01:49:58 -0700170 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700171 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}
182EXPORT_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 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700189void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
190{
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700191 kfree(ctl_info);
192}
193EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
194
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700195/*
196 * find_edac_device_by_dev
197 * scans the edac_device list for a specific 'struct device *'
Douglas Thompson52490c82007-07-19 01:50:20 -0700198 *
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
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700204 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700205static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700206{
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.
Douglas Thompson52490c82007-07-19 01:50:20 -0700226 *
227 * lock to be held prior to call: device_ctls_mutex
228 *
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700229 * Return:
230 * 0 on success
231 * 1 on failure.
232 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700233static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700234{
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 */
Douglas Thompson52490c82007-07-19 01:50:20 -0700241 rover = find_edac_device_by_dev(edac_dev->dev);
242 if (unlikely(rover != NULL))
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700243 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
Douglas Thompson052dfb42007-07-19 01:50:13 -0700261fail0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700262 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700263 "%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);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700266 return 1;
267
Douglas Thompson052dfb42007-07-19 01:50:13 -0700268fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700269 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700270 "bug in low-level driver: attempt to assign\n"
271 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
272 __func__);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700273 return 1;
274}
275
276/*
277 * complete_edac_device_list_del
Douglas Thompson52490c82007-07-19 01:50:20 -0700278 *
279 * callback function when reference count is zero
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700280 */
281static 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
Douglas Thompson52490c82007-07-19 01:50:20 -0700292 *
293 * remove the RCU, setup for a callback call, then wait for the
294 * callback to occur
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700295 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700296static void del_edac_device_from_global_list(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700297 *edac_device)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700298{
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 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700314struct edac_device_ctl_info *edac_device_find(int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700315{
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}
Douglas Thompson52490c82007-07-19 01:50:20 -0700334EXPORT_SYMBOL_GPL(edac_device_find);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700335
336/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700337 * edac_device_workq_function
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700338 * performs the operation scheduled by a workq request
339 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700340static void edac_device_workq_function(struct work_struct *work_req)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700341{
Douglas Thompson079708b2007-07-19 01:49:58 -0700342 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);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700344
345 //debugf0("%s() here and running\n", __func__);
Douglas Thompson52490c82007-07-19 01:50:20 -0700346 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700347
348 /* Only poll controllers that are running polled and have a check */
349 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
Douglas Thompson052dfb42007-07-19 01:50:13 -0700350 (edac_dev->edac_check != NULL)) {
351 edac_dev->edac_check(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700352 }
353
Douglas Thompson52490c82007-07-19 01:50:20 -0700354 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700355
356 /* Reschedule */
Douglas Thompson079708b2007-07-19 01:49:58 -0700357 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700358}
359
360/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700361 * edac_device_workq_setup
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700362 * initialize a workq item for this edac_device instance
363 * passing in the new delay period in msec
364 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700365void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700366 unsigned msec)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700367{
368 debugf0("%s()\n", __func__);
369
370 edac_dev->poll_msec = msec;
Douglas Thompson52490c82007-07-19 01:50:20 -0700371 edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700372
Dave Jiang81d87cb2007-07-19 01:49:52 -0700373 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
Dave Jiang81d87cb2007-07-19 01:49:52 -0700374 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700375}
376
377/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700378 * edac_device_workq_teardown
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700379 * stop the workq processing on this edac_dev
380 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700381void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700382{
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
Douglas Thompson079708b2007-07-19 01:49:58 -0700396void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700397 unsigned long value)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700398{
Douglas Thompson52490c82007-07-19 01:50:20 -0700399 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700400
401 /* cancel the current workq request */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700402 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700403
404 /* restart the workq request, with new delay value */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700405 edac_device_workq_setup(edac_dev, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700406
Douglas Thompson52490c82007-07-19 01:50:20 -0700407 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700408}
409
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700410/**
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 */
422int 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
Douglas Thompson52490c82007-07-19 01:50:20 -0700431 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700432
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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700442 "failed to create sysfs device\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700443 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
Dave Jiang81d87cb2007-07-19 01:49:52 -0700451 /*
452 * enable workq processing on this instance,
453 * default = 1000 msec
454 */
455 edac_device_workq_setup(edac_dev, 1000);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700456 } else {
457 edac_dev->op_state = OP_RUNNING_INTERRUPT;
458 }
459
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700460 /* Report action taken */
461 edac_device_printk(edac_dev, KERN_INFO,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700462 "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));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700468
Douglas Thompson52490c82007-07-19 01:50:20 -0700469 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700470 return 0;
471
Douglas Thompson052dfb42007-07-19 01:50:13 -0700472fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700473 /* Some error, so remove the entry from the lsit */
474 del_edac_device_from_global_list(edac_dev);
475
Douglas Thompson052dfb42007-07-19 01:50:13 -0700476fail0:
Douglas Thompson52490c82007-07-19 01:50:20 -0700477 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700478 return 1;
479}
480EXPORT_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 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700495struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700496{
497 struct edac_device_ctl_info *edac_dev;
498
499 debugf0("MC: %s()\n", __func__);
500
Douglas Thompson52490c82007-07-19 01:50:20 -0700501 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700502
Douglas Thompson52490c82007-07-19 01:50:20 -0700503 /* 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);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700507 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 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700514 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700515
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
Douglas Thompson52490c82007-07-19 01:50:20 -0700522 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700523
524 edac_printk(KERN_INFO, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700525 "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));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700528
529 return edac_dev;
530}
Douglas Thompson079708b2007-07-19 01:49:58 -0700531EXPORT_SYMBOL_GPL(edac_device_del_device);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700532
533static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
534{
535 return edac_dev->log_ce;
536}
537
538static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
539{
540 return edac_dev->log_ue;
541}
542
Douglas Thompson079708b2007-07-19 01:49:58 -0700543static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700544 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700545{
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 */
553void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700554 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700555{
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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700561 "INTERNAL ERROR: 'instance' out of range "
562 "(%d >= %d)\n", inst_nr,
563 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700564 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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700571 "INTERNAL ERROR: instance %d 'block' "
572 "out of range (%d >= %d)\n",
573 inst_nr, block_nr,
574 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700575 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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700589 "CE: %s instance: %s block: %s '%s'\n",
590 edac_dev->ctl_name, instance->name,
591 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700592}
593EXPORT_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 */
599void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700600 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700601{
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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700607 "INTERNAL ERROR: 'instance' out of range "
608 "(%d >= %d)\n", inst_nr,
609 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700610 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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700617 "INTERNAL ERROR: instance %d 'block' "
618 "out of range (%d >= %d)\n",
619 inst_nr, block_nr,
620 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700621 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,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700635 "UE: %s instance: %s block: %s '%s'\n",
636 edac_dev->ctl_name, instance->name,
637 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700638
639 if (edac_device_get_panic_on_ue(edac_dev))
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700640 panic("EDAC %s: UE instance: %s block %s '%s'\n",
Douglas Thompson052dfb42007-07-19 01:50:13 -0700641 edac_dev->ctl_name, instance->name,
642 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700643}
Douglas Thompson079708b2007-07-19 01:49:58 -0700644EXPORT_SYMBOL_GPL(edac_device_handle_ue);