blob: 6020da68cbef9a9f90512579c651b6e2deb4360e [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
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 */
Doug Thompsond45e7822007-07-19 01:50:27 -070070 struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
71 int device_index)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070072{
73 struct edac_device_ctl_info *dev_ctl;
74 struct edac_device_instance *dev_inst, *inst;
75 struct edac_device_block *dev_blk, *blk_p, *blk;
Douglas Thompsonfd309a92007-07-19 01:50:25 -070076 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070077 unsigned total_size;
78 unsigned count;
79 unsigned instance, block, attr;
80 void *pvt;
81
82 debugf1("%s() instances=%d blocks=%d\n",
Douglas Thompson079708b2007-07-19 01:49:58 -070083 __func__, nr_instances, nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070084
Douglas Thompsonfd309a92007-07-19 01:50:25 -070085 /* Calculate the size of memory we need to allocate AND
86 * determine the offsets of the various item arrays
87 * (instance,block,attrib) from the start of an allocated structure.
88 * We want the alignment of each item (instance,block,attrib)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070089 * to be at least as stringent as what the compiler would
90 * provide if we could simply hardcode everything into a single struct.
91 */
Douglas Thompson52490c82007-07-19 01:50:20 -070092 dev_ctl = (struct edac_device_ctl_info *)NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070093
Douglas Thompsonfd309a92007-07-19 01:50:25 -070094 /* Calc the 'end' offset past end of ONE ctl_info structure
95 * which will become the start of the 'instance' array
96 */
Douglas Thompson7391c6d2007-07-19 01:50:21 -070097 dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -070098
Douglas Thompsonfd309a92007-07-19 01:50:25 -070099 /* Calc the 'end' offset past the instance array within the ctl_info
100 * which will become the start of the block array
101 */
Douglas Thompson7391c6d2007-07-19 01:50:21 -0700102 dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700103
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700104 /* Calc the 'end' offset past the dev_blk array
105 * which will become the start of the attrib array, if any.
106 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700107 count = nr_instances * nr_blocks;
Douglas Thompson7391c6d2007-07-19 01:50:21 -0700108 dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700109
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700110 /* Check for case of when an attribute array is specified */
111 if (nr_attrib > 0) {
112 /* calc how many nr_attrib we need */
113 count *= nr_attrib;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700114
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700115 /* Calc the 'end' offset past the attributes array */
116 pvt = edac_align_ptr(&dev_attrib[count], sz_private);
117 } else {
118 /* no attribute array specificed */
119 pvt = edac_align_ptr(dev_attrib, sz_private);
120 }
121
122 /* 'pvt' now points to where the private data area is.
123 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
124 * is baselined at ZERO
125 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700126 total_size = ((unsigned long)pvt) + sz_private;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700127
128 /* Allocate the amount of memory for the set of control structures */
Douglas Thompson52490c82007-07-19 01:50:20 -0700129 dev_ctl = kzalloc(total_size, GFP_KERNEL);
130 if (dev_ctl == NULL)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700131 return NULL;
132
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700133 /* Adjust pointers so they point within the actual memory we
134 * just allocated rather than an imaginary chunk of memory
135 * located at address 0.
136 * 'dev_ctl' points to REAL memory, while the others are
137 * ZERO based and thus need to be adjusted to point within
138 * the allocated memory.
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700139 */
140 dev_inst = (struct edac_device_instance *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700141 (((char *)dev_ctl) + ((unsigned long)dev_inst));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700142 dev_blk = (struct edac_device_block *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700143 (((char *)dev_ctl) + ((unsigned long)dev_blk));
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700144 dev_attrib = (struct edac_dev_sysfs_block_attribute *)
Douglas Thompson052dfb42007-07-19 01:50:13 -0700145 (((char *)dev_ctl) + ((unsigned long)dev_attrib));
Douglas Thompson079708b2007-07-19 01:49:58 -0700146 pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700147
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700148 /* Begin storing the information into the control info structure */
Doug Thompsond45e7822007-07-19 01:50:27 -0700149 dev_ctl->dev_idx = device_index;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700150 dev_ctl->nr_instances = nr_instances;
151 dev_ctl->instances = dev_inst;
152 dev_ctl->pvt_info = pvt;
153
Douglas Thompson52490c82007-07-19 01:50:20 -0700154 /* Name of this edac device */
155 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700156
157 /* Initialize every Instance */
158 for (instance = 0; instance < nr_instances; instance++) {
159 inst = &dev_inst[instance];
160 inst->ctl = dev_ctl;
161 inst->nr_blocks = nr_blocks;
162 blk_p = &dev_blk[instance * nr_blocks];
163 inst->blocks = blk_p;
164
165 /* name of this instance */
166 snprintf(inst->name, sizeof(inst->name),
Douglas Thompson079708b2007-07-19 01:49:58 -0700167 "%s%u", edac_device_name, instance);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700168
169 /* Initialize every block in each instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700170 for (block = 0; block < nr_blocks; block++) {
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700171 blk = &blk_p[block];
172 blk->instance = inst;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700173 snprintf(blk->name, sizeof(blk->name),
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700174 "%s%d", edac_block_name, block+offset_value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700175
176 debugf1("%s() instance=%d block=%d name=%s\n",
Douglas Thompson079708b2007-07-19 01:49:58 -0700177 __func__, instance, block, blk->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700178
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700179 /* if there are NO attributes OR no attribute pointer
180 * then continue on to next block iteration
181 */
182 if ((nr_attrib == 0) || (attrib_spec == NULL))
183 continue;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700184
Douglas Thompsonfd309a92007-07-19 01:50:25 -0700185 /* setup the attribute array for this block */
186 blk->nr_attribs = nr_attrib;
187 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
188 blk->block_attributes = attrib_p;
189
190 /* Initialize every user specified attribute in this
191 * block with the data the caller passed in
192 */
193 for (attr = 0; attr < nr_attrib; attr++) {
194 attrib = &attrib_p[attr];
195 attrib->attr = attrib_spec->attr;
196 attrib->show = attrib_spec->show;
197 attrib->store = attrib_spec->store;
198
199 /* up reference this block */
200 attrib->block = blk;
201
202 /* bump the attrib_spec */
203 attrib_spec++;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700204 }
205 }
206 }
207
208 /* Mark this instance as merely ALLOCATED */
209 dev_ctl->op_state = OP_ALLOC;
210
211 return dev_ctl;
212}
213EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
214
215/*
216 * edac_device_free_ctl_info()
217 * frees the memory allocated by the edac_device_alloc_ctl_info()
218 * function
219 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700220void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
221{
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700222 kfree(ctl_info);
223}
224EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
225
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700226/*
227 * find_edac_device_by_dev
228 * scans the edac_device list for a specific 'struct device *'
Douglas Thompson52490c82007-07-19 01:50:20 -0700229 *
230 * lock to be held prior to call: device_ctls_mutex
231 *
232 * Return:
233 * pointer to control structure managing 'dev'
234 * NULL if not found on list
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700235 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700236static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700237{
238 struct edac_device_ctl_info *edac_dev;
239 struct list_head *item;
240
241 debugf3("%s()\n", __func__);
242
243 list_for_each(item, &edac_device_list) {
244 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
245
246 if (edac_dev->dev == dev)
247 return edac_dev;
248 }
249
250 return NULL;
251}
252
253/*
254 * add_edac_dev_to_global_list
255 * Before calling this function, caller must
256 * assign a unique value to edac_dev->dev_idx.
Douglas Thompson52490c82007-07-19 01:50:20 -0700257 *
258 * lock to be held prior to call: device_ctls_mutex
259 *
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700260 * Return:
261 * 0 on success
262 * 1 on failure.
263 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700264static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700265{
266 struct list_head *item, *insert_before;
267 struct edac_device_ctl_info *rover;
268
269 insert_before = &edac_device_list;
270
271 /* Determine if already on the list */
Douglas Thompson52490c82007-07-19 01:50:20 -0700272 rover = find_edac_device_by_dev(edac_dev->dev);
273 if (unlikely(rover != NULL))
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700274 goto fail0;
275
276 /* Insert in ascending order by 'dev_idx', so find position */
277 list_for_each(item, &edac_device_list) {
278 rover = list_entry(item, struct edac_device_ctl_info, link);
279
280 if (rover->dev_idx >= edac_dev->dev_idx) {
281 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
282 goto fail1;
283
284 insert_before = item;
285 break;
286 }
287 }
288
289 list_add_tail_rcu(&edac_dev->link, insert_before);
290 return 0;
291
Douglas Thompson052dfb42007-07-19 01:50:13 -0700292fail0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700293 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700294 "%s (%s) %s %s already assigned %d\n",
295 rover->dev->bus_id, dev_name(rover),
296 rover->mod_name, rover->ctl_name, rover->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700297 return 1;
298
Douglas Thompson052dfb42007-07-19 01:50:13 -0700299fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700300 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700301 "bug in low-level driver: attempt to assign\n"
302 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
303 __func__);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700304 return 1;
305}
306
307/*
308 * complete_edac_device_list_del
Douglas Thompson52490c82007-07-19 01:50:20 -0700309 *
310 * callback function when reference count is zero
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700311 */
312static void complete_edac_device_list_del(struct rcu_head *head)
313{
314 struct edac_device_ctl_info *edac_dev;
315
316 edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
317 INIT_LIST_HEAD(&edac_dev->link);
318 complete(&edac_dev->complete);
319}
320
321/*
322 * del_edac_device_from_global_list
Douglas Thompson52490c82007-07-19 01:50:20 -0700323 *
324 * remove the RCU, setup for a callback call, then wait for the
325 * callback to occur
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700326 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700327static void del_edac_device_from_global_list(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700328 *edac_device)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700329{
330 list_del_rcu(&edac_device->link);
331 init_completion(&edac_device->complete);
332 call_rcu(&edac_device->rcu, complete_edac_device_list_del);
333 wait_for_completion(&edac_device->complete);
334}
335
336/**
337 * edac_device_find
338 * Search for a edac_device_ctl_info structure whose index is 'idx'.
339 *
340 * If found, return a pointer to the structure.
341 * Else return NULL.
342 *
343 * Caller must hold device_ctls_mutex.
344 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700345struct edac_device_ctl_info *edac_device_find(int idx)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700346{
347 struct list_head *item;
348 struct edac_device_ctl_info *edac_dev;
349
350 /* Iterate over list, looking for exact match of ID */
351 list_for_each(item, &edac_device_list) {
352 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
353
354 if (edac_dev->dev_idx >= idx) {
355 if (edac_dev->dev_idx == idx)
356 return edac_dev;
357
358 /* not on list, so terminate early */
359 break;
360 }
361 }
362
363 return NULL;
364}
Douglas Thompson52490c82007-07-19 01:50:20 -0700365EXPORT_SYMBOL_GPL(edac_device_find);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700366
367/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700368 * edac_device_workq_function
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700369 * performs the operation scheduled by a workq request
370 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700371static void edac_device_workq_function(struct work_struct *work_req)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700372{
Douglas Thompson079708b2007-07-19 01:49:58 -0700373 struct delayed_work *d_work = (struct delayed_work *)work_req;
374 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700375
376 //debugf0("%s() here and running\n", __func__);
Doug Thompson0ca84762007-07-19 01:50:22 -0700377 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700378
379 /* Only poll controllers that are running polled and have a check */
380 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
Douglas Thompson052dfb42007-07-19 01:50:13 -0700381 (edac_dev->edac_check != NULL)) {
382 edac_dev->edac_check(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700383 }
384
Doug Thompson0ca84762007-07-19 01:50:22 -0700385 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700386
387 /* Reschedule */
Douglas Thompson079708b2007-07-19 01:49:58 -0700388 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700389}
390
391/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700392 * edac_device_workq_setup
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700393 * initialize a workq item for this edac_device instance
394 * passing in the new delay period in msec
395 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700396void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700397 unsigned msec)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700398{
399 debugf0("%s()\n", __func__);
400
401 edac_dev->poll_msec = msec;
Douglas Thompson52490c82007-07-19 01:50:20 -0700402 edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700403
Dave Jiang81d87cb2007-07-19 01:49:52 -0700404 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
Dave Jiang81d87cb2007-07-19 01:49:52 -0700405 queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700406}
407
408/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700409 * edac_device_workq_teardown
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700410 * stop the workq processing on this edac_dev
411 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700412void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700413{
414 int status;
415
416 status = cancel_delayed_work(&edac_dev->work);
417 if (status == 0) {
418 /* workq instance might be running, wait for it */
419 flush_workqueue(edac_workqueue);
420 }
421}
422
423/*
424 * edac_device_reset_delay_period
425 */
426
Douglas Thompson079708b2007-07-19 01:49:58 -0700427void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700428 unsigned long value)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700429{
Doug Thompson0ca84762007-07-19 01:50:22 -0700430 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700431
432 /* cancel the current workq request */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700433 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700434
435 /* restart the workq request, with new delay value */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700436 edac_device_workq_setup(edac_dev, value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700437
Doug Thompson0ca84762007-07-19 01:50:22 -0700438 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700439}
440
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700441/**
442 * edac_device_add_device: Insert the 'edac_dev' structure into the
443 * edac_device global list and create sysfs entries associated with
444 * edac_device structure.
445 * @edac_device: pointer to the edac_device structure to be added to the list
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700446 * 'edac_device' structure.
447 *
448 * Return:
449 * 0 Success
450 * !0 Failure
451 */
Doug Thompsond45e7822007-07-19 01:50:27 -0700452int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700453{
454 debugf0("%s()\n", __func__);
455
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700456#ifdef CONFIG_EDAC_DEBUG
457 if (edac_debug_level >= 3)
458 edac_device_dump_device(edac_dev);
459#endif
Doug Thompson0ca84762007-07-19 01:50:22 -0700460 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700461
462 if (add_edac_dev_to_global_list(edac_dev))
463 goto fail0;
464
465 /* set load time so that error rate can be tracked */
466 edac_dev->start_time = jiffies;
467
468 /* create this instance's sysfs entries */
469 if (edac_device_create_sysfs(edac_dev)) {
470 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700471 "failed to create sysfs device\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700472 goto fail1;
473 }
474
475 /* If there IS a check routine, then we are running POLLED */
476 if (edac_dev->edac_check != NULL) {
477 /* This instance is NOW RUNNING */
478 edac_dev->op_state = OP_RUNNING_POLL;
479
Dave Jiang81d87cb2007-07-19 01:49:52 -0700480 /*
481 * enable workq processing on this instance,
482 * default = 1000 msec
483 */
484 edac_device_workq_setup(edac_dev, 1000);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700485 } else {
486 edac_dev->op_state = OP_RUNNING_INTERRUPT;
487 }
488
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700489 /* Report action taken */
490 edac_device_printk(edac_dev, KERN_INFO,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700491 "Giving out device to module '%s' controller "
492 "'%s': DEV '%s' (%s)\n",
493 edac_dev->mod_name,
494 edac_dev->ctl_name,
495 dev_name(edac_dev),
Douglas Thompson494d0d52007-07-19 01:50:21 -0700496 edac_op_state_to_string(edac_dev->op_state));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700497
Doug Thompson0ca84762007-07-19 01:50:22 -0700498 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700499 return 0;
500
Douglas Thompson052dfb42007-07-19 01:50:13 -0700501fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700502 /* Some error, so remove the entry from the lsit */
503 del_edac_device_from_global_list(edac_dev);
504
Douglas Thompson052dfb42007-07-19 01:50:13 -0700505fail0:
Doug Thompson0ca84762007-07-19 01:50:22 -0700506 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700507 return 1;
508}
509EXPORT_SYMBOL_GPL(edac_device_add_device);
510
511/**
512 * edac_device_del_device:
513 * Remove sysfs entries for specified edac_device structure and
514 * then remove edac_device structure from global list
515 *
516 * @pdev:
517 * Pointer to 'struct device' representing edac_device
518 * structure to remove.
519 *
520 * Return:
521 * Pointer to removed edac_device structure,
522 * OR NULL if device not found.
523 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700524struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700525{
526 struct edac_device_ctl_info *edac_dev;
527
528 debugf0("MC: %s()\n", __func__);
529
Doug Thompson0ca84762007-07-19 01:50:22 -0700530 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700531
Douglas Thompson52490c82007-07-19 01:50:20 -0700532 /* Find the structure on the list, if not there, then leave */
533 edac_dev = find_edac_device_by_dev(dev);
534 if (edac_dev == NULL) {
Doug Thompson0ca84762007-07-19 01:50:22 -0700535 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700536 return NULL;
537 }
538
539 /* mark this instance as OFFLINE */
540 edac_dev->op_state = OP_OFFLINE;
541
542 /* clear workq processing on this instance */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700543 edac_device_workq_teardown(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700544
545 /* Tear down the sysfs entries for this instance */
546 edac_device_remove_sysfs(edac_dev);
547
548 /* deregister from global list */
549 del_edac_device_from_global_list(edac_dev);
550
Doug Thompson0ca84762007-07-19 01:50:22 -0700551 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700552
553 edac_printk(KERN_INFO, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700554 "Removed device %d for %s %s: DEV %s\n",
555 edac_dev->dev_idx,
556 edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700557
558 return edac_dev;
559}
Douglas Thompson079708b2007-07-19 01:49:58 -0700560EXPORT_SYMBOL_GPL(edac_device_del_device);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700561
562static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
563{
564 return edac_dev->log_ce;
565}
566
567static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
568{
569 return edac_dev->log_ue;
570}
571
Douglas Thompson079708b2007-07-19 01:49:58 -0700572static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700573 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700574{
575 return edac_dev->panic_on_ue;
576}
577
578/*
579 * edac_device_handle_ce
580 * perform a common output and handling of an 'edac_dev' CE event
581 */
582void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700583 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700584{
585 struct edac_device_instance *instance;
586 struct edac_device_block *block = NULL;
587
588 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
589 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700590 "INTERNAL ERROR: 'instance' out of range "
591 "(%d >= %d)\n", inst_nr,
592 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700593 return;
594 }
595
596 instance = edac_dev->instances + inst_nr;
597
598 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
599 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700600 "INTERNAL ERROR: instance %d 'block' "
601 "out of range (%d >= %d)\n",
602 inst_nr, block_nr,
603 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700604 return;
605 }
606
607 if (instance->nr_blocks > 0) {
608 block = instance->blocks + block_nr;
609 block->counters.ce_count++;
610 }
611
612 /* Propogate the count up the 'totals' tree */
613 instance->counters.ce_count++;
614 edac_dev->counters.ce_count++;
615
616 if (edac_device_get_log_ce(edac_dev))
617 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700618 "CE: %s instance: %s block: %s '%s'\n",
619 edac_dev->ctl_name, instance->name,
620 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700621}
622EXPORT_SYMBOL_GPL(edac_device_handle_ce);
623
624/*
625 * edac_device_handle_ue
626 * perform a common output and handling of an 'edac_dev' UE event
627 */
628void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700629 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700630{
631 struct edac_device_instance *instance;
632 struct edac_device_block *block = NULL;
633
634 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
635 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700636 "INTERNAL ERROR: 'instance' out of range "
637 "(%d >= %d)\n", inst_nr,
638 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700639 return;
640 }
641
642 instance = edac_dev->instances + inst_nr;
643
644 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
645 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700646 "INTERNAL ERROR: instance %d 'block' "
647 "out of range (%d >= %d)\n",
648 inst_nr, block_nr,
649 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700650 return;
651 }
652
653 if (instance->nr_blocks > 0) {
654 block = instance->blocks + block_nr;
655 block->counters.ue_count++;
656 }
657
658 /* Propogate the count up the 'totals' tree */
659 instance->counters.ue_count++;
660 edac_dev->counters.ue_count++;
661
662 if (edac_device_get_log_ue(edac_dev))
663 edac_device_printk(edac_dev, KERN_EMERG,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700664 "UE: %s instance: %s block: %s '%s'\n",
665 edac_dev->ctl_name, instance->name,
666 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700667
668 if (edac_device_get_panic_on_ue(edac_dev))
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700669 panic("EDAC %s: UE instance: %s block %s '%s'\n",
Douglas Thompson052dfb42007-07-19 01:50:13 -0700670 edac_dev->ctl_name, instance->name,
671 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700672}
Douglas Thompson079708b2007-07-19 01:49:58 -0700673EXPORT_SYMBOL_GPL(edac_device_handle_ue);