blob: 173f4ba0f7c8101285c4a1eb0f03da53127a3eea [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' */
Doug Thompson0ca84762007-07-19 01:50:22 -070036static DEFINE_MUTEX(device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070037static 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
Douglas Thompson1c3631f2007-07-19 01:50:29 -070051
Douglas Thompsone27e3da2007-07-19 01:49:36 -070052/*
Douglas Thompson52490c82007-07-19 01:50:20 -070053 * edac_device_alloc_ctl_info()
54 * Allocate a new edac device control info structure
55 *
56 * The control structure is allocated in complete chunk
57 * from the OS. It is in turn sub allocated to the
58 * various objects that compose the struture
59 *
60 * The structure has a 'nr_instance' array within itself.
61 * Each instance represents a major component
62 * Example: L1 cache and L2 cache are 2 instance components
63 *
64 * Within each instance is an array of 'nr_blocks' blockoffsets
Douglas Thompsone27e3da2007-07-19 01:49:36 -070065 */
66struct edac_device_ctl_info *edac_device_alloc_ctl_info(
67 unsigned sz_private,
Douglas Thompson52490c82007-07-19 01:50:20 -070068 char *edac_device_name, unsigned nr_instances,
69 char *edac_block_name, unsigned nr_blocks,
70 unsigned offset_value, /* zero, 1, or other based offset */
Doug Thompsond45e7822007-07-19 01:50:27 -070071 struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
72 int device_index)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070073{
74 struct edac_device_ctl_info *dev_ctl;
75 struct edac_device_instance *dev_inst, *inst;
76 struct edac_device_block *dev_blk, *blk_p, *blk;
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070077 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070078 unsigned total_size;
79 unsigned count;
80 unsigned instance, block, attr;
81 void *pvt;
Douglas Thompson1c3631f2007-07-19 01:50:29 -070082 int err;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070083
84 debugf1("%s() instances=%d blocks=%d\n",
Douglas Thompson079708b2007-07-19 01:49:58 -070085 __func__, nr_instances, nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070086
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070087 /* Calculate the size of memory we need to allocate AND
88 * determine the offsets of the various item arrays
89 * (instance,block,attrib) from the start of an allocated structure.
90 * We want the alignment of each item (instance,block,attrib)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070091 * to be at least as stringent as what the compiler would
92 * provide if we could simply hardcode everything into a single struct.
93 */
Douglas Thompson52490c82007-07-19 01:50:20 -070094 dev_ctl = (struct edac_device_ctl_info *)NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070095
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070096 /* Calc the 'end' offset past end of ONE ctl_info structure
97 * which will become the start of the 'instance' array
98 */
Douglas Thompson7391c6d2007-07-19 01:50:21 -070099 dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700100
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700101 /* Calc the 'end' offset past the instance array within the ctl_info
102 * which will become the start of the block array
103 */
Douglas Thompson7391c6d2007-07-19 01:50:21 -0700104 dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700105
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700106 /* Calc the 'end' offset past the dev_blk array
107 * which will become the start of the attrib array, if any.
108 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700109 count = nr_instances * nr_blocks;
Douglas Thompson7391c6d2007-07-19 01:50:21 -0700110 dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700111
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700112 /* Check for case of when an attribute array is specified */
113 if (nr_attrib > 0) {
114 /* calc how many nr_attrib we need */
115 count *= nr_attrib;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700116
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700117 /* Calc the 'end' offset past the attributes array */
118 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
119 } else {
120 /* no attribute array specificed */
121 pvt = edac_align_ptr(dev_attrib, sz_private);
122 }
123
124 /* 'pvt' now points to where the private data area is.
125 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
126 * is baselined at ZERO
127 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700128 total_size = ((unsigned long)pvt) + sz_private;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700129
130 /* Allocate the amount of memory for the set of control structures */
Douglas Thompson52490c82007-07-19 01:50:20 -0700131 dev_ctl = kzalloc(total_size, GFP_KERNEL);
132 if (dev_ctl == NULL)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700133 return NULL;
134
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700135 /* Adjust pointers so they point within the actual memory we
136 * just allocated rather than an imaginary chunk of memory
137 * located at address 0.
138 * 'dev_ctl' points to REAL memory, while the others are
139 * ZERO based and thus need to be adjusted to point within
140 * the allocated memory.
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700141 */
142 dev_inst = (struct edac_device_instance *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700143 (((char *)dev_ctl) + ((unsigned long)dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700144 dev_blk = (struct edac_device_block *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700145 (((char *)dev_ctl) + ((unsigned long)dev_blk));
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700146 dev_attrib = (struct edac_dev_sysfs_block_attribute *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700147 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
Douglas Thompson079708b2007-07-19 01:49:58 -0700148 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700149
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700150 /* Begin storing the information into the control info structure */
Doug Thompsond45e7822007-07-19 01:50:27 -0700151 dev_ctl->dev_idx = device_index;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700152 dev_ctl->nr_instances = nr_instances;
153 dev_ctl->instances = dev_inst;
154 dev_ctl->pvt_info = pvt;
155
Douglas Thompson52490c82007-07-19 01:50:20 -0700156 /* Name of this edac device */
157 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700158
159 /* Initialize every Instance */
160 for (instance = 0; instance < nr_instances; instance++) {
161 inst = &dev_inst[instance];
162 inst->ctl = dev_ctl;
163 inst->nr_blocks = nr_blocks;
164 blk_p = &dev_blk[instance * nr_blocks];
165 inst->blocks = blk_p;
166
167 /* name of this instance */
168 snprintf(inst->name, sizeof(inst->name),
Douglas Thompson079708b2007-07-19 01:49:58 -0700169 "%s%u", edac_device_name, instance);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700170
171 /* Initialize every block in each instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700172 for (block = 0; block < nr_blocks; block++) {
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700173 blk = &blk_p[block];
174 blk->instance = inst;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700175 snprintf(blk->name, sizeof(blk->name),
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700176 "%s%d", edac_block_name, block+offset_value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700177
178 debugf1("%s() instance=%d block=%d name=%s\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700179 __func__, instance, block, blk->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700180
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700181 /* if there are NO attributes OR no attribute pointer
182 * then continue on to next block iteration
183 */
184 if ((nr_attrib == 0) || (attrib_spec == NULL))
185 continue;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700186
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700187 /* setup the attribute array for this block */
188 blk->nr_attribs = nr_attrib;
189 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
190 blk->block_attributes = attrib_p;
191
192 /* Initialize every user specified attribute in this
193 * block with the data the caller passed in
194 */
195 for (attr = 0; attr < nr_attrib; attr++) {
196 attrib = &attrib_p[attr];
197 attrib->attr = attrib_spec->attr;
198 attrib->show = attrib_spec->show;
199 attrib->store = attrib_spec->store;
200
201 /* up reference this block */
202 attrib->block = blk;
203
204 /* bump the attrib_spec */
205 attrib_spec++;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700206 }
207 }
208 }
209
210 /* Mark this instance as merely ALLOCATED */
211 dev_ctl->op_state = OP_ALLOC;
212
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700213 /*
214 * Initialize the 'root' kobj for the edac_device controller
215 */
216 err = edac_device_register_sysfs_main_kobj(dev_ctl);
217 if (err) {
218 kfree(dev_ctl);
219 return NULL;
220 }
221
222 /* at this point, the root kobj is valid, and in order to
223 * 'free' the object, then the function:
224 * edac_device_unregister_sysfs_main_kobj() must be called
225 * which will perform kobj unregistration and the actual free
226 * will occur during the kobject callback operation
227 */
228
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700229 return dev_ctl;
230}
231EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
232
233/*
234 * edac_device_free_ctl_info()
235 * frees the memory allocated by the edac_device_alloc_ctl_info()
236 * function
237 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700238void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
239{
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700240 edac_device_unregister_sysfs_main_kobj(ctl_info);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700241}
242EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
243
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700244/*
245 * find_edac_device_by_dev
246 * scans the edac_device list for a specific 'struct device *'
Douglas Thompson52490c82007-07-19 01:50:20 -0700247 *
248 * lock to be held prior to call: device_ctls_mutex
249 *
250 * Return:
251 * pointer to control structure managing 'dev'
252 * NULL if not found on list
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700253 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700254static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700255{
256 struct edac_device_ctl_info *edac_dev;
257 struct list_head *item;
258
259 debugf3("%s()\n", __func__);
260
261 list_for_each(item, &edac_device_list) {
262 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
263
264 if (edac_dev->dev == dev)
265 return edac_dev;
266 }
267
268 return NULL;
269}
270
271/*
272 * add_edac_dev_to_global_list
273 * Before calling this function, caller must
274 * assign a unique value to edac_dev->dev_idx.
Douglas Thompson52490c82007-07-19 01:50:20 -0700275 *
276 * lock to be held prior to call: device_ctls_mutex
277 *
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700278 * Return:
279 * 0 on success
280 * 1 on failure.
281 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700282static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700283{
284 struct list_head *item, *insert_before;
285 struct edac_device_ctl_info *rover;
286
287 insert_before = &edac_device_list;
288
289 /* Determine if already on the list */
Douglas Thompson52490c82007-07-19 01:50:20 -0700290 rover = find_edac_device_by_dev(edac_dev->dev);
291 if (unlikely(rover != NULL))
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700292 goto fail0;
293
294 /* Insert in ascending order by 'dev_idx', so find position */
295 list_for_each(item, &edac_device_list) {
296 rover = list_entry(item, struct edac_device_ctl_info, link);
297
298 if (rover->dev_idx >= edac_dev->dev_idx) {
299 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
300 goto fail1;
301
302 insert_before = item;
303 break;
304 }
305 }
306
307 list_add_tail_rcu(&edac_dev->link, insert_before);
308 return 0;
309
Douglas Thompson052dfb42007-07-19 01:50:13 -0700310fail0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700311 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700312 "%s (%s) %s %s already assigned %d\n",
313 rover->dev->bus_id, dev_name(rover),
314 rover->mod_name, rover->ctl_name, rover->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700315 return 1;
316
Douglas Thompson052dfb42007-07-19 01:50:13 -0700317fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700318 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700319 "bug in low-level driver: attempt to assign\n"
320 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
321 __func__);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700322 return 1;
323}
324
325/*
326 * complete_edac_device_list_del
Douglas Thompson52490c82007-07-19 01:50:20 -0700327 *
328 * callback function when reference count is zero
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700329 */
330static void complete_edac_device_list_del(struct rcu_head *head)
331{
332 struct edac_device_ctl_info *edac_dev;
333
334 edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
335 INIT_LIST_HEAD(&edac_dev->link);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700336 complete(&edac_dev->removal_complete);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700337}
338
339/*
340 * del_edac_device_from_global_list
Douglas Thompson52490c82007-07-19 01:50:20 -0700341 *
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700342 * remove the RCU, setup for a callback call,
343 * then wait for the callback to occur
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700344 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700345static void del_edac_device_from_global_list(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700346 *edac_device)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700347{
348 list_del_rcu(&edac_device->link);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700349
350 init_completion(&edac_device->removal_complete);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700351 call_rcu(&edac_device->rcu, complete_edac_device_list_del);
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700352 wait_for_completion(&edac_device->removal_complete);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700353}
354
355/**
356 * edac_device_find
357 * Search for a edac_device_ctl_info structure whose index is 'idx'.
358 *
359 * If found, return a pointer to the structure.
360 * Else return NULL.
361 *
362 * Caller must hold device_ctls_mutex.
363 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700364struct edac_device_ctl_info *edac_device_find(int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700365{
366 struct list_head *item;
367 struct edac_device_ctl_info *edac_dev;
368
369 /* Iterate over list, looking for exact match of ID */
370 list_for_each(item, &edac_device_list) {
371 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
372
373 if (edac_dev->dev_idx >= idx) {
374 if (edac_dev->dev_idx == idx)
375 return edac_dev;
376
377 /* not on list, so terminate early */
378 break;
379 }
380 }
381
382 return NULL;
383}
Douglas Thompson52490c82007-07-19 01:50:20 -0700384EXPORT_SYMBOL_GPL(edac_device_find);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700385
386/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700387 * edac_device_workq_function
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700388 * performs the operation scheduled by a workq request
389 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700390static void edac_device_workq_function(struct work_struct *work_req)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700391{
Douglas Thompson079708b2007-07-19 01:49:58 -0700392 struct delayed_work *d_work = (struct delayed_work *)work_req;
393 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700394
395 //debugf0("%s() here and running\n", __func__);
Doug Thompson0ca84762007-07-19 01:50:22 -0700396 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700397
398 /* Only poll controllers that are running polled and have a check */
399 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
Douglas Thompson052dfb42007-07-19 01:50:13 -0700400 (edac_dev->edac_check != NULL)) {
401 edac_dev->edac_check(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700402 }
403
Doug Thompson0ca84762007-07-19 01:50:22 -0700404 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700405
406 /* Reschedule */
Douglas Thompson079708b2007-07-19 01:49:58 -0700407 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700408}
409
410/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700411 * edac_device_workq_setup
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700412 * initialize a workq item for this edac_device instance
413 * passing in the new delay period in msec
414 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700415void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700416 unsigned msec)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700417{
418 debugf0("%s()\n", __func__);
419
420 edac_dev->poll_msec = msec;
Douglas Thompson52490c82007-07-19 01:50:20 -0700421 edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700422
Dave Jiang81d87cb2007-07-19 01:49:52 -0700423 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
Dave Jiang81d87cb2007-07-19 01:49:52 -0700424 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700425}
426
427/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700428 * edac_device_workq_teardown
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700429 * stop the workq processing on this edac_dev
430 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700431void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700432{
433 int status;
434
435 status = cancel_delayed_work(&edac_dev->work);
436 if (status == 0) {
437 /* workq instance might be running, wait for it */
438 flush_workqueue(edac_workqueue);
439 }
440}
441
442/*
443 * edac_device_reset_delay_period
444 */
445
Douglas Thompson079708b2007-07-19 01:49:58 -0700446void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700447 unsigned long value)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700448{
Doug Thompson0ca84762007-07-19 01:50:22 -0700449 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700450
451 /* cancel the current workq request */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700452 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700453
454 /* restart the workq request, with new delay value */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700455 edac_device_workq_setup(edac_dev, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700456
Doug Thompson0ca84762007-07-19 01:50:22 -0700457 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700458}
459
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700460/**
461 * edac_device_add_device: Insert the 'edac_dev' structure into the
462 * edac_device global list and create sysfs entries associated with
463 * edac_device structure.
464 * @edac_device: pointer to the edac_device structure to be added to the list
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700465 * 'edac_device' structure.
466 *
467 * Return:
468 * 0 Success
469 * !0 Failure
470 */
Doug Thompsond45e7822007-07-19 01:50:27 -0700471int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700472{
473 debugf0("%s()\n", __func__);
474
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700475#ifdef CONFIG_EDAC_DEBUG
476 if (edac_debug_level >= 3)
477 edac_device_dump_device(edac_dev);
478#endif
Doug Thompson0ca84762007-07-19 01:50:22 -0700479 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700480
481 if (add_edac_dev_to_global_list(edac_dev))
482 goto fail0;
483
484 /* set load time so that error rate can be tracked */
485 edac_dev->start_time = jiffies;
486
487 /* create this instance's sysfs entries */
488 if (edac_device_create_sysfs(edac_dev)) {
489 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700490 "failed to create sysfs device\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700491 goto fail1;
492 }
493
494 /* If there IS a check routine, then we are running POLLED */
495 if (edac_dev->edac_check != NULL) {
496 /* This instance is NOW RUNNING */
497 edac_dev->op_state = OP_RUNNING_POLL;
498
Dave Jiang81d87cb2007-07-19 01:49:52 -0700499 /*
500 * enable workq processing on this instance,
501 * default = 1000 msec
502 */
503 edac_device_workq_setup(edac_dev, 1000);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700504 } else {
505 edac_dev->op_state = OP_RUNNING_INTERRUPT;
506 }
507
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700508 /* Report action taken */
509 edac_device_printk(edac_dev, KERN_INFO,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700510 "Giving out device to module '%s' controller "
511 "'%s': DEV '%s' (%s)\n",
512 edac_dev->mod_name,
513 edac_dev->ctl_name,
514 dev_name(edac_dev),
Douglas Thompson494d0d52007-07-19 01:50:21 -0700515 edac_op_state_to_string(edac_dev->op_state));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700516
Doug Thompson0ca84762007-07-19 01:50:22 -0700517 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700518 return 0;
519
Douglas Thompson052dfb42007-07-19 01:50:13 -0700520fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700521 /* Some error, so remove the entry from the lsit */
522 del_edac_device_from_global_list(edac_dev);
523
Douglas Thompson052dfb42007-07-19 01:50:13 -0700524fail0:
Doug Thompson0ca84762007-07-19 01:50:22 -0700525 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700526 return 1;
527}
528EXPORT_SYMBOL_GPL(edac_device_add_device);
529
530/**
531 * edac_device_del_device:
532 * Remove sysfs entries for specified edac_device structure and
533 * then remove edac_device structure from global list
534 *
535 * @pdev:
536 * Pointer to 'struct device' representing edac_device
537 * structure to remove.
538 *
539 * Return:
540 * Pointer to removed edac_device structure,
541 * OR NULL if device not found.
542 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700543struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700544{
545 struct edac_device_ctl_info *edac_dev;
546
547 debugf0("MC: %s()\n", __func__);
548
Doug Thompson0ca84762007-07-19 01:50:22 -0700549 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700550
Douglas Thompson52490c82007-07-19 01:50:20 -0700551 /* Find the structure on the list, if not there, then leave */
552 edac_dev = find_edac_device_by_dev(dev);
553 if (edac_dev == NULL) {
Doug Thompson0ca84762007-07-19 01:50:22 -0700554 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700555 return NULL;
556 }
557
558 /* mark this instance as OFFLINE */
559 edac_dev->op_state = OP_OFFLINE;
560
561 /* clear workq processing on this instance */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700562 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700563
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700564 /* deregister from global list */
565 del_edac_device_from_global_list(edac_dev);
566
Doug Thompson0ca84762007-07-19 01:50:22 -0700567 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700568
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700569 /* Tear down the sysfs entries for this instance */
570 edac_device_remove_sysfs(edac_dev);
571
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700572 edac_printk(KERN_INFO, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700573 "Removed device %d for %s %s: DEV %s\n",
574 edac_dev->dev_idx,
575 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700576
577 return edac_dev;
578}
Douglas Thompson079708b2007-07-19 01:49:58 -0700579EXPORT_SYMBOL_GPL(edac_device_del_device);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700580
581static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
582{
583 return edac_dev->log_ce;
584}
585
586static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
587{
588 return edac_dev->log_ue;
589}
590
Douglas Thompson079708b2007-07-19 01:49:58 -0700591static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700592 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700593{
594 return edac_dev->panic_on_ue;
595}
596
597/*
598 * edac_device_handle_ce
599 * perform a common output and handling of an 'edac_dev' CE event
600 */
601void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700602 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700603{
604 struct edac_device_instance *instance;
605 struct edac_device_block *block = NULL;
606
607 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
608 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700609 "INTERNAL ERROR: 'instance' out of range "
610 "(%d >= %d)\n", inst_nr,
611 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700612 return;
613 }
614
615 instance = edac_dev->instances + inst_nr;
616
617 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
618 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700619 "INTERNAL ERROR: instance %d 'block' "
620 "out of range (%d >= %d)\n",
621 inst_nr, block_nr,
622 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700623 return;
624 }
625
626 if (instance->nr_blocks > 0) {
627 block = instance->blocks + block_nr;
628 block->counters.ce_count++;
629 }
630
631 /* Propogate the count up the 'totals' tree */
632 instance->counters.ce_count++;
633 edac_dev->counters.ce_count++;
634
635 if (edac_device_get_log_ce(edac_dev))
636 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700637 "CE: %s instance: %s block: %s '%s'\n",
638 edac_dev->ctl_name, instance->name,
639 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700640}
641EXPORT_SYMBOL_GPL(edac_device_handle_ce);
642
643/*
644 * edac_device_handle_ue
645 * perform a common output and handling of an 'edac_dev' UE event
646 */
647void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700648 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700649{
650 struct edac_device_instance *instance;
651 struct edac_device_block *block = NULL;
652
653 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
654 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700655 "INTERNAL ERROR: 'instance' out of range "
656 "(%d >= %d)\n", inst_nr,
657 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700658 return;
659 }
660
661 instance = edac_dev->instances + inst_nr;
662
663 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
664 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700665 "INTERNAL ERROR: instance %d 'block' "
666 "out of range (%d >= %d)\n",
667 inst_nr, block_nr,
668 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700669 return;
670 }
671
672 if (instance->nr_blocks > 0) {
673 block = instance->blocks + block_nr;
674 block->counters.ue_count++;
675 }
676
677 /* Propogate the count up the 'totals' tree */
678 instance->counters.ue_count++;
679 edac_dev->counters.ue_count++;
680
681 if (edac_device_get_log_ue(edac_dev))
682 edac_device_printk(edac_dev, KERN_EMERG,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700683 "UE: %s instance: %s block: %s '%s'\n",
684 edac_dev->ctl_name, instance->name,
685 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700686
687 if (edac_device_get_panic_on_ue(edac_dev))
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700688 panic("EDAC %s: UE instance: %s block %s '%s'\n",
Douglas Thompson052dfb42007-07-19 01:50:13 -0700689 edac_dev->ctl_name, instance->name,
690 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700691}
Douglas Thompson079708b2007-07-19 01:49:58 -0700692EXPORT_SYMBOL_GPL(edac_device_handle_ue);