blob: 65cf2b9355c47126a5f589efc3abc207edb4b773 [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
Douglas Thompsone27e3da2007-07-19 01:49:36 -070015#include <asm/page.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Mauro Carvalho Chehab6d8ef242016-10-29 10:01:41 -020017#include <linux/ctype.h>
18#include <linux/highmem.h>
19#include <linux/init.h>
20#include <linux/jiffies.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/smp.h>
24#include <linux/spinlock.h>
25#include <linux/sysctl.h>
26#include <linux/timer.h>
Douglas Thompsone27e3da2007-07-19 01:49:36 -070027
Mauro Carvalho Chehab6d8ef242016-10-29 10:01:41 -020028#include "edac_device.h"
Douglas Thompsone27e3da2007-07-19 01:49:36 -070029#include "edac_module.h"
30
Doug Thompsonbf52fa42007-07-19 01:50:30 -070031/* lock for the list: 'edac_device_list', manipulation of this list
32 * is protected by the 'device_ctls_mutex' lock
33 */
Doug Thompson0ca84762007-07-19 01:50:22 -070034static DEFINE_MUTEX(device_ctls_mutex);
Robert P. J. Dayff6ac2a2008-04-29 01:03:17 -070035static LIST_HEAD(edac_device_list);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070036
Douglas Thompsone27e3da2007-07-19 01:49:36 -070037#ifdef CONFIG_EDAC_DEBUG
38static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
39{
Joe Perches956b9ba2012-04-29 17:08:39 -030040 edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n",
41 edac_dev, edac_dev->dev_idx);
42 edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
43 edac_dbg(3, "\tdev = %p\n", edac_dev->dev);
44 edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
45 edac_dev->mod_name, edac_dev->ctl_name);
46 edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070047}
Douglas Thompson079708b2007-07-19 01:49:58 -070048#endif /* CONFIG_EDAC_DEBUG */
Douglas Thompsone27e3da2007-07-19 01:49:36 -070049
Douglas Thompsone27e3da2007-07-19 01:49:36 -070050struct edac_device_ctl_info *edac_device_alloc_ctl_info(
51 unsigned sz_private,
Douglas Thompson52490c82007-07-19 01:50:20 -070052 char *edac_device_name, unsigned nr_instances,
53 char *edac_block_name, unsigned nr_blocks,
54 unsigned offset_value, /* zero, 1, or other based offset */
Doug Thompsond45e7822007-07-19 01:50:27 -070055 struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
56 int device_index)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070057{
58 struct edac_device_ctl_info *dev_ctl;
59 struct edac_device_instance *dev_inst, *inst;
60 struct edac_device_block *dev_blk, *blk_p, *blk;
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070061 struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070062 unsigned total_size;
63 unsigned count;
64 unsigned instance, block, attr;
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030065 void *pvt, *p;
Douglas Thompson1c3631f2007-07-19 01:50:29 -070066 int err;
Douglas Thompsone27e3da2007-07-19 01:49:36 -070067
Joe Perches956b9ba2012-04-29 17:08:39 -030068 edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070069
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070070 /* Calculate the size of memory we need to allocate AND
71 * determine the offsets of the various item arrays
72 * (instance,block,attrib) from the start of an allocated structure.
73 * We want the alignment of each item (instance,block,attrib)
Douglas Thompsone27e3da2007-07-19 01:49:36 -070074 * to be at least as stringent as what the compiler would
75 * provide if we could simply hardcode everything into a single struct.
76 */
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030077 p = NULL;
78 dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070079
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070080 /* Calc the 'end' offset past end of ONE ctl_info structure
81 * which will become the start of the 'instance' array
82 */
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030083 dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070084
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070085 /* Calc the 'end' offset past the instance array within the ctl_info
86 * which will become the start of the block array
87 */
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030088 count = nr_instances * nr_blocks;
89 dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070090
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070091 /* Calc the 'end' offset past the dev_blk array
92 * which will become the start of the attrib array, if any.
93 */
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030094 /* calc how many nr_attrib we need */
95 if (nr_attrib > 0)
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -070096 count *= nr_attrib;
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030097 dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
Douglas Thompsone27e3da2007-07-19 01:49:36 -070098
Mauro Carvalho Chehab93e4fe62012-04-16 10:18:12 -030099 /* Calc the 'end' offset past the attributes array */
100 pvt = edac_align_ptr(&p, sz_private, 1);
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700101
102 /* 'pvt' now points to where the private data area is.
103 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
104 * is baselined at ZERO
105 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700106 total_size = ((unsigned long)pvt) + sz_private;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700107
108 /* Allocate the amount of memory for the set of control structures */
Douglas Thompson52490c82007-07-19 01:50:20 -0700109 dev_ctl = kzalloc(total_size, GFP_KERNEL);
110 if (dev_ctl == NULL)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700111 return NULL;
112
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700113 /* Adjust pointers so they point within the actual memory we
114 * just allocated rather than an imaginary chunk of memory
115 * located at address 0.
116 * 'dev_ctl' points to REAL memory, while the others are
117 * ZERO based and thus need to be adjusted to point within
118 * the allocated memory.
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700119 */
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 Thompsonfd309a9d2007-07-19 01:50:25 -0700124 dev_attrib = (struct edac_dev_sysfs_block_attribute *)
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 Thompsonfd309a9d2007-07-19 01:50:25 -0700128 /* Begin storing the information into the control info structure */
Doug Thompsond45e7822007-07-19 01:50:27 -0700129 dev_ctl->dev_idx = device_index;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700130 dev_ctl->nr_instances = nr_instances;
131 dev_ctl->instances = dev_inst;
132 dev_ctl->pvt_info = pvt;
133
Doug Thompson56e61a92008-02-07 00:14:51 -0800134 /* Default logging of CEs and UEs */
135 dev_ctl->log_ce = 1;
136 dev_ctl->log_ue = 1;
137
Douglas Thompson52490c82007-07-19 01:50:20 -0700138 /* Name of this edac device */
139 snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700140
Joe Perches956b9ba2012-04-29 17:08:39 -0300141 edac_dbg(4, "edac_dev=%p next after end=%p\n",
142 dev_ctl, pvt + sz_private);
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700143
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700144 /* Initialize every Instance */
145 for (instance = 0; instance < nr_instances; instance++) {
146 inst = &dev_inst[instance];
147 inst->ctl = dev_ctl;
148 inst->nr_blocks = nr_blocks;
149 blk_p = &dev_blk[instance * nr_blocks];
150 inst->blocks = blk_p;
151
152 /* name of this instance */
153 snprintf(inst->name, sizeof(inst->name),
Douglas Thompson079708b2007-07-19 01:49:58 -0700154 "%s%u", edac_device_name, instance);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700155
156 /* Initialize every block in each instance */
Douglas Thompson079708b2007-07-19 01:49:58 -0700157 for (block = 0; block < nr_blocks; block++) {
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700158 blk = &blk_p[block];
159 blk->instance = inst;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700160 snprintf(blk->name, sizeof(blk->name),
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700161 "%s%d", edac_block_name, block+offset_value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700162
Joe Perches956b9ba2012-04-29 17:08:39 -0300163 edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
164 instance, inst, block, blk, blk->name);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700165
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700166 /* if there are NO attributes OR no attribute pointer
167 * then continue on to next block iteration
168 */
169 if ((nr_attrib == 0) || (attrib_spec == NULL))
170 continue;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700171
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700172 /* setup the attribute array for this block */
173 blk->nr_attribs = nr_attrib;
174 attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
175 blk->block_attributes = attrib_p;
176
Joe Perches956b9ba2012-04-29 17:08:39 -0300177 edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
178 blk->block_attributes);
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700179
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700180 /* Initialize every user specified attribute in this
181 * block with the data the caller passed in
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700182 * Each block gets its own copy of pointers,
183 * and its unique 'value'
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700184 */
185 for (attr = 0; attr < nr_attrib; attr++) {
186 attrib = &attrib_p[attr];
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700187
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700188 /* populate the unique per attrib
189 * with the code pointers and info
190 */
191 attrib->attr = attrib_spec[attr].attr;
192 attrib->show = attrib_spec[attr].show;
193 attrib->store = attrib_spec[attr].store;
Douglas Thompsonfd309a9d2007-07-19 01:50:25 -0700194
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700195 attrib->block = blk; /* up link */
196
Joe Perches956b9ba2012-04-29 17:08:39 -0300197 edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
198 attrib, attrib->attr.name,
199 &attrib_spec[attr],
200 attrib_spec[attr].attr.name
Doug Thompsonb2a4ac02007-07-19 01:50:33 -0700201 );
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700202 }
203 }
204 }
205
206 /* Mark this instance as merely ALLOCATED */
207 dev_ctl->op_state = OP_ALLOC;
208
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700209 /*
210 * Initialize the 'root' kobj for the edac_device controller
211 */
212 err = edac_device_register_sysfs_main_kobj(dev_ctl);
213 if (err) {
214 kfree(dev_ctl);
215 return NULL;
216 }
217
218 /* at this point, the root kobj is valid, and in order to
219 * 'free' the object, then the function:
220 * edac_device_unregister_sysfs_main_kobj() must be called
221 * which will perform kobj unregistration and the actual free
222 * will occur during the kobject callback operation
223 */
224
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700225 return dev_ctl;
226}
227EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
228
Douglas Thompson079708b2007-07-19 01:49:58 -0700229void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
230{
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700231 edac_device_unregister_sysfs_main_kobj(ctl_info);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700232}
233EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
234
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700235/*
236 * find_edac_device_by_dev
237 * scans the edac_device list for a specific 'struct device *'
Douglas Thompson52490c82007-07-19 01:50:20 -0700238 *
239 * lock to be held prior to call: device_ctls_mutex
240 *
241 * Return:
242 * pointer to control structure managing 'dev'
243 * NULL if not found on list
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700244 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700245static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700246{
247 struct edac_device_ctl_info *edac_dev;
248 struct list_head *item;
249
Joe Perches956b9ba2012-04-29 17:08:39 -0300250 edac_dbg(0, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700251
252 list_for_each(item, &edac_device_list) {
253 edac_dev = list_entry(item, struct edac_device_ctl_info, link);
254
255 if (edac_dev->dev == dev)
256 return edac_dev;
257 }
258
259 return NULL;
260}
261
262/*
263 * add_edac_dev_to_global_list
264 * Before calling this function, caller must
265 * assign a unique value to edac_dev->dev_idx.
Douglas Thompson52490c82007-07-19 01:50:20 -0700266 *
267 * lock to be held prior to call: device_ctls_mutex
268 *
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700269 * Return:
270 * 0 on success
271 * 1 on failure.
272 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700273static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700274{
275 struct list_head *item, *insert_before;
276 struct edac_device_ctl_info *rover;
277
278 insert_before = &edac_device_list;
279
280 /* Determine if already on the list */
Douglas Thompson52490c82007-07-19 01:50:20 -0700281 rover = find_edac_device_by_dev(edac_dev->dev);
282 if (unlikely(rover != NULL))
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700283 goto fail0;
284
285 /* Insert in ascending order by 'dev_idx', so find position */
286 list_for_each(item, &edac_device_list) {
287 rover = list_entry(item, struct edac_device_ctl_info, link);
288
289 if (rover->dev_idx >= edac_dev->dev_idx) {
290 if (unlikely(rover->dev_idx == edac_dev->dev_idx))
291 goto fail1;
292
293 insert_before = item;
294 break;
295 }
296 }
297
298 list_add_tail_rcu(&edac_dev->link, insert_before);
299 return 0;
300
Douglas Thompson052dfb42007-07-19 01:50:13 -0700301fail0:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700302 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700303 "%s (%s) %s %s already assigned %d\n",
Kay Sievers281efb12009-01-06 14:42:57 -0800304 dev_name(rover->dev), edac_dev_name(rover),
Douglas Thompson052dfb42007-07-19 01:50:13 -0700305 rover->mod_name, rover->ctl_name, rover->dev_idx);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700306 return 1;
307
Douglas Thompson052dfb42007-07-19 01:50:13 -0700308fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700309 edac_printk(KERN_WARNING, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700310 "bug in low-level driver: attempt to assign\n"
311 " duplicate dev_idx %d in %s()\n", rover->dev_idx,
312 __func__);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700313 return 1;
314}
315
316/*
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700317 * del_edac_device_from_global_list
318 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700319static void del_edac_device_from_global_list(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700320 *edac_device)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700321{
322 list_del_rcu(&edac_device->link);
Lai Jiangshane2e77092011-05-26 16:25:58 -0700323
324 /* these are for safe removal of devices from global list while
325 * NMI handlers may be traversing list
326 */
327 synchronize_rcu();
328 INIT_LIST_HEAD(&edac_device->link);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700329}
330
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700331/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700332 * edac_device_workq_function
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700333 * performs the operation scheduled by a workq request
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700334 *
335 * this workq is embedded within an edac_device_ctl_info
336 * structure, that needs to be polled for possible error events.
337 *
338 * This operation is to acquire the list mutex lock
Jiri Kosinaf70d4a92012-05-22 10:56:21 +0200339 * (thus preventing insertation or deletion)
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700340 * and then call the device's poll function IFF this device is
341 * running polled and there is a poll function defined.
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700342 */
Dave Jiang81d87cb2007-07-19 01:49:52 -0700343static void edac_device_workq_function(struct work_struct *work_req)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700344{
Jean Delvarefbeb4382009-04-13 14:40:21 -0700345 struct delayed_work *d_work = to_delayed_work(work_req);
Douglas Thompson079708b2007-07-19 01:49:58 -0700346 struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700347
Doug Thompson0ca84762007-07-19 01:50:22 -0700348 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700349
Harry Ciaod519c8d2008-12-23 13:57:16 -0800350 /* If we are being removed, bail out immediately */
351 if (edac_dev->op_state == OP_OFFLINE) {
352 mutex_unlock(&device_ctls_mutex);
353 return;
354 }
355
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700356 /* Only poll controllers that are running polled and have a check */
357 if ((edac_dev->op_state == OP_RUNNING_POLL) &&
Douglas Thompson052dfb42007-07-19 01:50:13 -0700358 (edac_dev->edac_check != NULL)) {
359 edac_dev->edac_check(edac_dev);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700360 }
361
Doug Thompson0ca84762007-07-19 01:50:22 -0700362 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700363
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700364 /* Reschedule the workq for the next time period to start again
365 * if the number of msec is for 1 sec, then adjust to the next
David Mackey15ed1032012-04-17 11:30:52 -0700366 * whole one second to save timers firing all over the period
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700367 * between integral seconds
368 */
369 if (edac_dev->poll_msec == 1000)
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100370 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700371 else
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100372 edac_queue_work(&edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700373}
374
375/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700376 * edac_device_workq_setup
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700377 * initialize a workq item for this edac_device instance
378 * passing in the new delay period in msec
379 */
Borislav Petkove136fa02015-11-30 15:07:28 +0100380static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
381 unsigned msec)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700382{
Joe Perches956b9ba2012-04-29 17:08:39 -0300383 edac_dbg(0, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700384
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700385 /* take the arg 'msec' and set it into the control structure
386 * to used in the time period calculation
387 * then calc the number of jiffies that represents
388 */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700389 edac_dev->poll_msec = msec;
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700390 edac_dev->delay = msecs_to_jiffies(msec);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700391
Dave Jiang81d87cb2007-07-19 01:49:52 -0700392 INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700393
394 /* optimize here for the 1 second case, which will be normal value, to
395 * fire ON the 1 second time event. This helps reduce all sorts of
396 * timers firing on sub-second basis, while they are happy
397 * to fire together on the 1 second exactly
398 */
399 if (edac_dev->poll_msec == 1000)
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100400 edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700401 else
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100402 edac_queue_work(&edac_dev->work, edac_dev->delay);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700403}
404
405/*
Dave Jiang81d87cb2007-07-19 01:49:52 -0700406 * edac_device_workq_teardown
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700407 * stop the workq processing on this edac_dev
408 */
Borislav Petkove136fa02015-11-30 15:07:28 +0100409static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700410{
Stephen Boyd881f0fc2013-12-30 12:14:12 -0800411 if (!edac_dev->edac_check)
412 return;
413
Borislav Petkovfcd5c4d2015-11-27 10:38:38 +0100414 edac_dev->op_state = OP_OFFLINE;
415
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100416 edac_stop_work(&edac_dev->work);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700417}
418
419/*
420 * edac_device_reset_delay_period
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700421 *
422 * need to stop any outstanding workq queued up at this time
423 * because we will be resetting the sleep time.
424 * Then restart the workq on the new delay
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700425 */
Douglas Thompson079708b2007-07-19 01:49:58 -0700426void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700427 unsigned long value)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700428{
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100429 unsigned long jiffs = msecs_to_jiffies(value);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700430
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100431 if (value == 1000)
432 jiffs = round_jiffies_relative(value);
Doug Thompsonbf52fa42007-07-19 01:50:30 -0700433
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100434 edac_dev->poll_msec = value;
435 edac_dev->delay = jiffs;
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700436
Borislav Petkovc4cf3b42015-11-30 19:02:01 +0100437 edac_mod_work(&edac_dev->work, jiffs);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700438}
439
Harry Ciao1dc9b702009-06-17 16:27:59 -0700440int edac_device_alloc_index(void)
441{
442 static atomic_t device_indexes = ATOMIC_INIT(0);
443
444 return atomic_inc_return(&device_indexes) - 1;
445}
446EXPORT_SYMBOL_GPL(edac_device_alloc_index);
447
Doug Thompsond45e7822007-07-19 01:50:27 -0700448int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700449{
Joe Perches956b9ba2012-04-29 17:08:39 -0300450 edac_dbg(0, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700451
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700452#ifdef CONFIG_EDAC_DEBUG
453 if (edac_debug_level >= 3)
454 edac_device_dump_device(edac_dev);
455#endif
Doug Thompson0ca84762007-07-19 01:50:22 -0700456 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700457
458 if (add_edac_dev_to_global_list(edac_dev))
459 goto fail0;
460
461 /* set load time so that error rate can be tracked */
462 edac_dev->start_time = jiffies;
463
464 /* create this instance's sysfs entries */
465 if (edac_device_create_sysfs(edac_dev)) {
466 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700467 "failed to create sysfs device\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700468 goto fail1;
469 }
470
471 /* If there IS a check routine, then we are running POLLED */
472 if (edac_dev->edac_check != NULL) {
473 /* This instance is NOW RUNNING */
474 edac_dev->op_state = OP_RUNNING_POLL;
475
Dave Jiang81d87cb2007-07-19 01:49:52 -0700476 /*
477 * enable workq processing on this instance,
478 * default = 1000 msec
479 */
480 edac_device_workq_setup(edac_dev, 1000);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700481 } else {
482 edac_dev->op_state = OP_RUNNING_INTERRUPT;
483 }
484
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700485 /* Report action taken */
486 edac_device_printk(edac_dev, KERN_INFO,
Robert Richter7270a602013-10-10 18:22:36 +0200487 "Giving out device to module %s controller %s: DEV %s (%s)\n",
488 edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
489 edac_op_state_to_string(edac_dev->op_state));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700490
Doug Thompson0ca84762007-07-19 01:50:22 -0700491 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700492 return 0;
493
Douglas Thompson052dfb42007-07-19 01:50:13 -0700494fail1:
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700495 /* Some error, so remove the entry from the lsit */
496 del_edac_device_from_global_list(edac_dev);
497
Douglas Thompson052dfb42007-07-19 01:50:13 -0700498fail0:
Doug Thompson0ca84762007-07-19 01:50:22 -0700499 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700500 return 1;
501}
502EXPORT_SYMBOL_GPL(edac_device_add_device);
503
Douglas Thompson079708b2007-07-19 01:49:58 -0700504struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700505{
506 struct edac_device_ctl_info *edac_dev;
507
Joe Perches956b9ba2012-04-29 17:08:39 -0300508 edac_dbg(0, "\n");
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700509
Doug Thompson0ca84762007-07-19 01:50:22 -0700510 mutex_lock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700511
Douglas Thompson52490c82007-07-19 01:50:20 -0700512 /* Find the structure on the list, if not there, then leave */
513 edac_dev = find_edac_device_by_dev(dev);
514 if (edac_dev == NULL) {
Doug Thompson0ca84762007-07-19 01:50:22 -0700515 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700516 return NULL;
517 }
518
519 /* mark this instance as OFFLINE */
520 edac_dev->op_state = OP_OFFLINE;
521
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700522 /* deregister from global list */
523 del_edac_device_from_global_list(edac_dev);
524
Doug Thompson0ca84762007-07-19 01:50:22 -0700525 mutex_unlock(&device_ctls_mutex);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700526
Harry Ciaod519c8d2008-12-23 13:57:16 -0800527 /* clear workq processing on this instance */
528 edac_device_workq_teardown(edac_dev);
529
Douglas Thompson1c3631f2007-07-19 01:50:29 -0700530 /* Tear down the sysfs entries for this instance */
531 edac_device_remove_sysfs(edac_dev);
532
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700533 edac_printk(KERN_INFO, EDAC_MC,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700534 "Removed device %d for %s %s: DEV %s\n",
535 edac_dev->dev_idx,
Stephen Rothwell17aa7e02008-05-05 13:54:19 +1000536 edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700537
538 return edac_dev;
539}
Douglas Thompson079708b2007-07-19 01:49:58 -0700540EXPORT_SYMBOL_GPL(edac_device_del_device);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700541
542static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
543{
544 return edac_dev->log_ce;
545}
546
547static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
548{
549 return edac_dev->log_ue;
550}
551
Douglas Thompson079708b2007-07-19 01:49:58 -0700552static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
Douglas Thompson052dfb42007-07-19 01:50:13 -0700553 *edac_dev)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700554{
555 return edac_dev->panic_on_ue;
556}
557
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700558void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700559 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700560{
561 struct edac_device_instance *instance;
562 struct edac_device_block *block = NULL;
563
564 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
565 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700566 "INTERNAL ERROR: 'instance' out of range "
567 "(%d >= %d)\n", inst_nr,
568 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700569 return;
570 }
571
572 instance = edac_dev->instances + inst_nr;
573
574 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
575 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700576 "INTERNAL ERROR: instance %d 'block' "
577 "out of range (%d >= %d)\n",
578 inst_nr, block_nr,
579 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700580 return;
581 }
582
583 if (instance->nr_blocks > 0) {
584 block = instance->blocks + block_nr;
585 block->counters.ce_count++;
586 }
587
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300588 /* Propagate the count up the 'totals' tree */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700589 instance->counters.ce_count++;
590 edac_dev->counters.ce_count++;
591
592 if (edac_device_get_log_ce(edac_dev))
593 edac_device_printk(edac_dev, KERN_WARNING,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700594 "CE: %s instance: %s block: %s '%s'\n",
595 edac_dev->ctl_name, instance->name,
596 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700597}
598EXPORT_SYMBOL_GPL(edac_device_handle_ce);
599
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700600void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700601 int inst_nr, int block_nr, const char *msg)
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700602{
603 struct edac_device_instance *instance;
604 struct edac_device_block *block = NULL;
605
606 if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
607 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700608 "INTERNAL ERROR: 'instance' out of range "
609 "(%d >= %d)\n", inst_nr,
610 edac_dev->nr_instances);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700611 return;
612 }
613
614 instance = edac_dev->instances + inst_nr;
615
616 if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
617 edac_device_printk(edac_dev, KERN_ERR,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700618 "INTERNAL ERROR: instance %d 'block' "
619 "out of range (%d >= %d)\n",
620 inst_nr, block_nr,
621 instance->nr_blocks);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700622 return;
623 }
624
625 if (instance->nr_blocks > 0) {
626 block = instance->blocks + block_nr;
627 block->counters.ue_count++;
628 }
629
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300630 /* Propagate the count up the 'totals' tree */
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700631 instance->counters.ue_count++;
632 edac_dev->counters.ue_count++;
633
634 if (edac_device_get_log_ue(edac_dev))
635 edac_device_printk(edac_dev, KERN_EMERG,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700636 "UE: %s instance: %s block: %s '%s'\n",
637 edac_dev->ctl_name, instance->name,
638 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700639
640 if (edac_device_get_panic_on_ue(edac_dev))
Douglas Thompsond391a7b2007-07-19 01:50:11 -0700641 panic("EDAC %s: UE instance: %s block %s '%s'\n",
Douglas Thompson052dfb42007-07-19 01:50:13 -0700642 edac_dev->ctl_name, instance->name,
643 block ? block->name : "N/A", msg);
Douglas Thompsone27e3da2007-07-19 01:49:36 -0700644}
Douglas Thompson079708b2007-07-19 01:49:58 -0700645EXPORT_SYMBOL_GPL(edac_device_handle_ue);