blob: 1d2eb20304f6104d22480d82ce3ea30491c1c860 [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 */
Douglas Thompson7391c6d2007-07-19 01:50:21 -070092 dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -070093
94 /* Calc the 'end' offset past the instance array */
Douglas Thompson7391c6d2007-07-19 01:50:21 -070095 dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
Douglas Thompsone27e3da2007-07-19 01:49:36 -070096
97 /* Calc the 'end' offset past the dev_blk array */
98 count = nr_instances * nr_blocks;
Douglas Thompson7391c6d2007-07-19 01:50:21 -070099 dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700100
101 /* Check for case of NO attributes specified */
102 if (nr_attribs > 0)
103 count *= nr_attribs;
104
105 /* Calc the 'end' offset past the attributes array */
Douglas Thompson079708b2007-07-19 01:49:58 -0700106 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
107 total_size = ((unsigned long)pvt) + sz_private;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700108
109 /* Allocate the amount of memory for the set of control structures */
Douglas Thompson52490c82007-07-19 01:50:20 -0700110 dev_ctl = kzalloc(total_size, GFP_KERNEL);
111 if (dev_ctl == NULL)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700112 return NULL;
113
114 /* Adjust pointers so they point within the memory we just allocated
115 * rather than an imaginary chunk of memory located at address 0.
116 */
117 dev_inst = (struct edac_device_instance *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700118 (((char *)dev_ctl) + ((unsigned long)dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700119 dev_blk = (struct edac_device_block *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700120 (((char *)dev_ctl) + ((unsigned long)dev_blk));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700121 dev_attrib = (struct edac_attrib *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700122 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
Douglas Thompson079708b2007-07-19 01:49:58 -0700123 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700124
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700125 dev_ctl->nr_instances = nr_instances;
126 dev_ctl->instances = dev_inst;
127 dev_ctl->pvt_info = pvt;
128
Douglas Thompson52490c82007-07-19 01:50:20 -0700129 /* Name of this edac device */
130 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700131
132 /* Initialize every Instance */
133 for (instance = 0; instance < nr_instances; instance++) {
134 inst = &dev_inst[instance];
135 inst->ctl = dev_ctl;
136 inst->nr_blocks = nr_blocks;
137 blk_p = &dev_blk[instance * nr_blocks];
138 inst->blocks = blk_p;
139
140 /* name of this instance */
141 snprintf(inst->name, sizeof(inst->name),
Douglas Thompson079708b2007-07-19 01:49:58 -0700142 "%s%u", edac_device_name, instance);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700143
144 /* Initialize every block in each instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700145 for (block = 0; block < nr_blocks; block++) {
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700146 blk = &blk_p[block];
147 blk->instance = inst;
148 blk->nr_attribs = nr_attribs;
149 attrib_p = &dev_attrib[block * nr_attribs];
150 blk->attribs = attrib_p;
151 snprintf(blk->name, sizeof(blk->name),
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700152 "%s%d", edac_block_name, block+offset_value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700153
154 debugf1("%s() instance=%d block=%d name=%s\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700155 __func__, instance, block, blk->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700156
157 if (attrib_spec != NULL) {
158 /* when there is an attrib_spec passed int then
159 * Initialize every attrib of each block
160 */
161 for (attr = 0; attr < nr_attribs; attr++) {
162 attrib = &attrib_p[attr];
163 attrib->block = blk;
164
165 /* Link each attribute to the caller's
166 * spec entry, for name and type
Douglas Thompson079708b2007-07-19 01:49:58 -0700167 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700168 attrib->spec = &attrib_spec[attr];
169 }
170 }
171 }
172 }
173
174 /* Mark this instance as merely ALLOCATED */
175 dev_ctl->op_state = OP_ALLOC;
176
177 return dev_ctl;
178}
179EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
180
181/*
182 * edac_device_free_ctl_info()
183 * frees the memory allocated by the edac_device_alloc_ctl_info()
184 * function
185 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700186void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
187{
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700188 kfree(ctl_info);
189}
190EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
191
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700192/*
193 * find_edac_device_by_dev
194 * scans the edac_device list for a specific 'struct device *'
Douglas Thompson52490c82007-07-19 01:50:20 -0700195 *
196 * lock to be held prior to call: device_ctls_mutex
197 *
198 * Return:
199 * pointer to control structure managing 'dev'
200 * NULL if not found on list
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700201 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700202static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700203{
204 struct edac_device_ctl_info *edac_dev;
205 struct list_head *item;
206
207 debugf3("%s()\n", __func__);
208
209 list_for_each(item, &edac_device_list) {
210 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
211
212 if (edac_dev->dev == dev)
213 return edac_dev;
214 }
215
216 return NULL;
217}
218
219/*
220 * add_edac_dev_to_global_list
221 * Before calling this function, caller must
222 * assign a unique value to edac_dev->dev_idx.
Douglas Thompson52490c82007-07-19 01:50:20 -0700223 *
224 * lock to be held prior to call: device_ctls_mutex
225 *
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700226 * Return:
227 * 0 on success
228 * 1 on failure.
229 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700230static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700231{
232 struct list_head *item, *insert_before;
233 struct edac_device_ctl_info *rover;
234
235 insert_before = &edac_device_list;
236
237 /* Determine if already on the list */
Douglas Thompson52490c82007-07-19 01:50:20 -0700238 rover = find_edac_device_by_dev(edac_dev->dev);
239 if (unlikely(rover != NULL))
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700240 goto fail0;
241
242 /* Insert in ascending order by 'dev_idx', so find position */
243 list_for_each(item, &edac_device_list) {
244 rover = list_entry(item, struct edac_device_ctl_info, link);
245
246 if (rover->dev_idx >= edac_dev->dev_idx) {
247 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
248 goto fail1;
249
250 insert_before = item;
251 break;
252 }
253 }
254
255 list_add_tail_rcu(&edac_dev->link, insert_before);
256 return 0;
257
Douglas Thompson052dfb42007-07-19 01:50:13 -0700258fail0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700259 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700260 "%s (%s) %s %s already assigned %d\n",
261 rover->dev->bus_id, dev_name(rover),
262 rover->mod_name, rover->ctl_name, rover->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700263 return 1;
264
Douglas Thompson052dfb42007-07-19 01:50:13 -0700265fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700266 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700267 "bug in low-level driver: attempt to assign\n"
268 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
269 __func__);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700270 return 1;
271}
272
273/*
274 * complete_edac_device_list_del
Douglas Thompson52490c82007-07-19 01:50:20 -0700275 *
276 * callback function when reference count is zero
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700277 */
278static 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
Douglas Thompson52490c82007-07-19 01:50:20 -0700289 *
290 * remove the RCU, setup for a callback call, then wait for the
291 * callback to occur
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700292 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700293static void del_edac_device_from_global_list(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700294 *edac_device)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700295{
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 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700311struct edac_device_ctl_info *edac_device_find(int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700312{
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}
Douglas Thompson52490c82007-07-19 01:50:20 -0700331EXPORT_SYMBOL_GPL(edac_device_find);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700332
333/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700334 * edac_device_workq_function
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700335 * performs the operation scheduled by a workq request
336 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700337static void edac_device_workq_function(struct work_struct *work_req)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700338{
Douglas Thompson079708b2007-07-19 01:49:58 -0700339 struct delayed_work *d_work = (struct delayed_work *)work_req;
340 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700341
342 //debugf0("%s() here and running\n", __func__);
Douglas Thompson52490c82007-07-19 01:50:20 -0700343 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700344
345 /* Only poll controllers that are running polled and have a check */
346 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
Douglas Thompson052dfb42007-07-19 01:50:13 -0700347 (edac_dev->edac_check != NULL)) {
348 edac_dev->edac_check(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700349 }
350
Douglas Thompson52490c82007-07-19 01:50:20 -0700351 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700352
353 /* Reschedule */
Douglas Thompson079708b2007-07-19 01:49:58 -0700354 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700355}
356
357/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700358 * edac_device_workq_setup
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700359 * initialize a workq item for this edac_device instance
360 * passing in the new delay period in msec
361 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700362void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700363 unsigned msec)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700364{
365 debugf0("%s()\n", __func__);
366
367 edac_dev->poll_msec = msec;
Douglas Thompson52490c82007-07-19 01:50:20 -0700368 edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700369
Dave Jiang81d87cb2007-07-19 01:49:52 -0700370 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
Dave Jiang81d87cb2007-07-19 01:49:52 -0700371 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700372}
373
374/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700375 * edac_device_workq_teardown
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700376 * stop the workq processing on this edac_dev
377 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700378void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700379{
380 int status;
381
382 status = cancel_delayed_work(&edac_dev->work);
383 if (status == 0) {
384 /* workq instance might be running, wait for it */
385 flush_workqueue(edac_workqueue);
386 }
387}
388
389/*
390 * edac_device_reset_delay_period
391 */
392
Douglas Thompson079708b2007-07-19 01:49:58 -0700393void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700394 unsigned long value)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700395{
Douglas Thompson52490c82007-07-19 01:50:20 -0700396 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700397
398 /* cancel the current workq request */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700399 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700400
401 /* restart the workq request, with new delay value */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700402 edac_device_workq_setup(edac_dev, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700403
Douglas Thompson52490c82007-07-19 01:50:20 -0700404 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700405}
406
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700407/**
408 * edac_device_add_device: Insert the 'edac_dev' structure into the
409 * edac_device global list and create sysfs entries associated with
410 * edac_device structure.
411 * @edac_device: pointer to the edac_device structure to be added to the list
412 * @edac_idx: A unique numeric identifier to be assigned to the
413 * 'edac_device' structure.
414 *
415 * Return:
416 * 0 Success
417 * !0 Failure
418 */
419int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
420{
421 debugf0("%s()\n", __func__);
422
423 edac_dev->dev_idx = edac_idx;
424#ifdef CONFIG_EDAC_DEBUG
425 if (edac_debug_level >= 3)
426 edac_device_dump_device(edac_dev);
427#endif
Douglas Thompson52490c82007-07-19 01:50:20 -0700428 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700429
430 if (add_edac_dev_to_global_list(edac_dev))
431 goto fail0;
432
433 /* set load time so that error rate can be tracked */
434 edac_dev->start_time = jiffies;
435
436 /* create this instance's sysfs entries */
437 if (edac_device_create_sysfs(edac_dev)) {
438 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700439 "failed to create sysfs device\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700440 goto fail1;
441 }
442
443 /* If there IS a check routine, then we are running POLLED */
444 if (edac_dev->edac_check != NULL) {
445 /* This instance is NOW RUNNING */
446 edac_dev->op_state = OP_RUNNING_POLL;
447
Dave Jiang81d87cb2007-07-19 01:49:52 -0700448 /*
449 * enable workq processing on this instance,
450 * default = 1000 msec
451 */
452 edac_device_workq_setup(edac_dev, 1000);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700453 } else {
454 edac_dev->op_state = OP_RUNNING_INTERRUPT;
455 }
456
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700457 /* Report action taken */
458 edac_device_printk(edac_dev, KERN_INFO,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700459 "Giving out device to module '%s' controller "
460 "'%s': DEV '%s' (%s)\n",
461 edac_dev->mod_name,
462 edac_dev->ctl_name,
463 dev_name(edac_dev),
Douglas Thompson494d0d52007-07-19 01:50:21 -0700464 edac_op_state_to_string(edac_dev->op_state));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700465
Douglas Thompson52490c82007-07-19 01:50:20 -0700466 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700467 return 0;
468
Douglas Thompson052dfb42007-07-19 01:50:13 -0700469fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700470 /* Some error, so remove the entry from the lsit */
471 del_edac_device_from_global_list(edac_dev);
472
Douglas Thompson052dfb42007-07-19 01:50:13 -0700473fail0:
Douglas Thompson52490c82007-07-19 01:50:20 -0700474 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700475 return 1;
476}
477EXPORT_SYMBOL_GPL(edac_device_add_device);
478
479/**
480 * edac_device_del_device:
481 * Remove sysfs entries for specified edac_device structure and
482 * then remove edac_device structure from global list
483 *
484 * @pdev:
485 * Pointer to 'struct device' representing edac_device
486 * structure to remove.
487 *
488 * Return:
489 * Pointer to removed edac_device structure,
490 * OR NULL if device not found.
491 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700492struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700493{
494 struct edac_device_ctl_info *edac_dev;
495
496 debugf0("MC: %s()\n", __func__);
497
Douglas Thompson52490c82007-07-19 01:50:20 -0700498 down(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700499
Douglas Thompson52490c82007-07-19 01:50:20 -0700500 /* Find the structure on the list, if not there, then leave */
501 edac_dev = find_edac_device_by_dev(dev);
502 if (edac_dev == NULL) {
503 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700504 return NULL;
505 }
506
507 /* mark this instance as OFFLINE */
508 edac_dev->op_state = OP_OFFLINE;
509
510 /* clear workq processing on this instance */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700511 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700512
513 /* Tear down the sysfs entries for this instance */
514 edac_device_remove_sysfs(edac_dev);
515
516 /* deregister from global list */
517 del_edac_device_from_global_list(edac_dev);
518
Douglas Thompson52490c82007-07-19 01:50:20 -0700519 up(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700520
521 edac_printk(KERN_INFO, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700522 "Removed device %d for %s %s: DEV %s\n",
523 edac_dev->dev_idx,
524 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700525
526 return edac_dev;
527}
Douglas Thompson079708b2007-07-19 01:49:58 -0700528EXPORT_SYMBOL_GPL(edac_device_del_device);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700529
530static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
531{
532 return edac_dev->log_ce;
533}
534
535static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
536{
537 return edac_dev->log_ue;
538}
539
Douglas Thompson079708b2007-07-19 01:49:58 -0700540static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700541 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700542{
543 return edac_dev->panic_on_ue;
544}
545
546/*
547 * edac_device_handle_ce
548 * perform a common output and handling of an 'edac_dev' CE event
549 */
550void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700551 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700552{
553 struct edac_device_instance *instance;
554 struct edac_device_block *block = NULL;
555
556 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
557 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700558 "INTERNAL ERROR: 'instance' out of range "
559 "(%d >= %d)\n", inst_nr,
560 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700561 return;
562 }
563
564 instance = edac_dev->instances + inst_nr;
565
566 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
567 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700568 "INTERNAL ERROR: instance %d 'block' "
569 "out of range (%d >= %d)\n",
570 inst_nr, block_nr,
571 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700572 return;
573 }
574
575 if (instance->nr_blocks > 0) {
576 block = instance->blocks + block_nr;
577 block->counters.ce_count++;
578 }
579
580 /* Propogate the count up the 'totals' tree */
581 instance->counters.ce_count++;
582 edac_dev->counters.ce_count++;
583
584 if (edac_device_get_log_ce(edac_dev))
585 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700586 "CE: %s instance: %s block: %s '%s'\n",
587 edac_dev->ctl_name, instance->name,
588 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700589}
590EXPORT_SYMBOL_GPL(edac_device_handle_ce);
591
592/*
593 * edac_device_handle_ue
594 * perform a common output and handling of an 'edac_dev' UE event
595 */
596void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700597 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700598{
599 struct edac_device_instance *instance;
600 struct edac_device_block *block = NULL;
601
602 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
603 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700604 "INTERNAL ERROR: 'instance' out of range "
605 "(%d >= %d)\n", inst_nr,
606 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700607 return;
608 }
609
610 instance = edac_dev->instances + inst_nr;
611
612 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
613 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700614 "INTERNAL ERROR: instance %d 'block' "
615 "out of range (%d >= %d)\n",
616 inst_nr, block_nr,
617 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700618 return;
619 }
620
621 if (instance->nr_blocks > 0) {
622 block = instance->blocks + block_nr;
623 block->counters.ue_count++;
624 }
625
626 /* Propogate the count up the 'totals' tree */
627 instance->counters.ue_count++;
628 edac_dev->counters.ue_count++;
629
630 if (edac_device_get_log_ue(edac_dev))
631 edac_device_printk(edac_dev, KERN_EMERG,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700632 "UE: %s instance: %s block: %s '%s'\n",
633 edac_dev->ctl_name, instance->name,
634 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700635
636 if (edac_device_get_panic_on_ue(edac_dev))
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700637 panic("EDAC %s: UE instance: %s block %s '%s'\n",
Douglas Thompson052dfb42007-07-19 01:50:13 -0700638 edac_dev->ctl_name, instance->name,
639 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700640}
Douglas Thompson079708b2007-07-19 01:49:58 -0700641EXPORT_SYMBOL_GPL(edac_device_handle_ue);