1e27e3dacSDouglas Thompson 2e27e3dacSDouglas Thompson /* 3e27e3dacSDouglas Thompson * edac_device.c 4e27e3dacSDouglas Thompson * (C) 2007 www.douglaskthompson.com 5e27e3dacSDouglas Thompson * 6e27e3dacSDouglas Thompson * This file may be distributed under the terms of the 7e27e3dacSDouglas Thompson * GNU General Public License. 8e27e3dacSDouglas Thompson * 9e27e3dacSDouglas Thompson * Written by Doug Thompson <norsk5@xmission.com> 10e27e3dacSDouglas Thompson * 11e27e3dacSDouglas Thompson * edac_device API implementation 12e27e3dacSDouglas Thompson * 19 Jan 2007 13e27e3dacSDouglas Thompson */ 14e27e3dacSDouglas Thompson 15e27e3dacSDouglas Thompson #include <linux/module.h> 16e27e3dacSDouglas Thompson #include <linux/types.h> 17e27e3dacSDouglas Thompson #include <linux/smp.h> 18e27e3dacSDouglas Thompson #include <linux/init.h> 19e27e3dacSDouglas Thompson #include <linux/sysctl.h> 20e27e3dacSDouglas Thompson #include <linux/highmem.h> 21e27e3dacSDouglas Thompson #include <linux/timer.h> 22e27e3dacSDouglas Thompson #include <linux/slab.h> 2352490c8dSDouglas Thompson #include <linux/jiffies.h> 24e27e3dacSDouglas Thompson #include <linux/spinlock.h> 25e27e3dacSDouglas Thompson #include <linux/list.h> 26e27e3dacSDouglas Thompson #include <linux/ctype.h> 27e27e3dacSDouglas Thompson #include <linux/workqueue.h> 28e27e3dacSDouglas Thompson #include <asm/uaccess.h> 29e27e3dacSDouglas Thompson #include <asm/page.h> 30e27e3dacSDouglas Thompson 31e27e3dacSDouglas Thompson #include "edac_core.h" 32e27e3dacSDouglas Thompson #include "edac_module.h" 33e27e3dacSDouglas Thompson 34bf52fa4aSDoug Thompson /* lock for the list: 'edac_device_list', manipulation of this list 35bf52fa4aSDoug Thompson * is protected by the 'device_ctls_mutex' lock 36bf52fa4aSDoug Thompson */ 370ca84761SDoug Thompson static DEFINE_MUTEX(device_ctls_mutex); 38ff6ac2a6SRobert P. J. Day static LIST_HEAD(edac_device_list); 39e27e3dacSDouglas Thompson 40e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG 41e27e3dacSDouglas Thompson static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev) 42e27e3dacSDouglas Thompson { 43956b9ba1SJoe Perches edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n", 44956b9ba1SJoe Perches edac_dev, edac_dev->dev_idx); 45956b9ba1SJoe Perches edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check); 46956b9ba1SJoe Perches edac_dbg(3, "\tdev = %p\n", edac_dev->dev); 47956b9ba1SJoe Perches edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n", 48e27e3dacSDouglas Thompson edac_dev->mod_name, edac_dev->ctl_name); 49956b9ba1SJoe Perches edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info); 50e27e3dacSDouglas Thompson } 51e27e3dacSDouglas Thompson #endif /* CONFIG_EDAC_DEBUG */ 52e27e3dacSDouglas Thompson 531c3631ffSDouglas Thompson 54e27e3dacSDouglas Thompson /* 5552490c8dSDouglas Thompson * edac_device_alloc_ctl_info() 5652490c8dSDouglas Thompson * Allocate a new edac device control info structure 5752490c8dSDouglas Thompson * 5852490c8dSDouglas Thompson * The control structure is allocated in complete chunk 5952490c8dSDouglas Thompson * from the OS. It is in turn sub allocated to the 6015ed103aSDavid Mackey * various objects that compose the structure 6152490c8dSDouglas Thompson * 6252490c8dSDouglas Thompson * The structure has a 'nr_instance' array within itself. 6352490c8dSDouglas Thompson * Each instance represents a major component 6452490c8dSDouglas Thompson * Example: L1 cache and L2 cache are 2 instance components 6552490c8dSDouglas Thompson * 6652490c8dSDouglas Thompson * Within each instance is an array of 'nr_blocks' blockoffsets 67e27e3dacSDouglas Thompson */ 68e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_alloc_ctl_info( 69e27e3dacSDouglas Thompson unsigned sz_private, 7052490c8dSDouglas Thompson char *edac_device_name, unsigned nr_instances, 7152490c8dSDouglas Thompson char *edac_block_name, unsigned nr_blocks, 7252490c8dSDouglas Thompson unsigned offset_value, /* zero, 1, or other based offset */ 73d45e7823SDoug Thompson struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib, 74d45e7823SDoug Thompson int device_index) 75e27e3dacSDouglas Thompson { 76e27e3dacSDouglas Thompson struct edac_device_ctl_info *dev_ctl; 77e27e3dacSDouglas Thompson struct edac_device_instance *dev_inst, *inst; 78e27e3dacSDouglas Thompson struct edac_device_block *dev_blk, *blk_p, *blk; 79fd309a9dSDouglas Thompson struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib; 80e27e3dacSDouglas Thompson unsigned total_size; 81e27e3dacSDouglas Thompson unsigned count; 82e27e3dacSDouglas Thompson unsigned instance, block, attr; 8393e4fe64SMauro Carvalho Chehab void *pvt, *p; 841c3631ffSDouglas Thompson int err; 85e27e3dacSDouglas Thompson 86956b9ba1SJoe Perches edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks); 87e27e3dacSDouglas Thompson 88fd309a9dSDouglas Thompson /* Calculate the size of memory we need to allocate AND 89fd309a9dSDouglas Thompson * determine the offsets of the various item arrays 90fd309a9dSDouglas Thompson * (instance,block,attrib) from the start of an allocated structure. 91fd309a9dSDouglas Thompson * We want the alignment of each item (instance,block,attrib) 92e27e3dacSDouglas Thompson * to be at least as stringent as what the compiler would 93e27e3dacSDouglas Thompson * provide if we could simply hardcode everything into a single struct. 94e27e3dacSDouglas Thompson */ 9593e4fe64SMauro Carvalho Chehab p = NULL; 9693e4fe64SMauro Carvalho Chehab dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1); 97e27e3dacSDouglas Thompson 98fd309a9dSDouglas Thompson /* Calc the 'end' offset past end of ONE ctl_info structure 99fd309a9dSDouglas Thompson * which will become the start of the 'instance' array 100fd309a9dSDouglas Thompson */ 10193e4fe64SMauro Carvalho Chehab dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances); 102e27e3dacSDouglas Thompson 103fd309a9dSDouglas Thompson /* Calc the 'end' offset past the instance array within the ctl_info 104fd309a9dSDouglas Thompson * which will become the start of the block array 105fd309a9dSDouglas Thompson */ 10693e4fe64SMauro Carvalho Chehab count = nr_instances * nr_blocks; 10793e4fe64SMauro Carvalho Chehab dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count); 108e27e3dacSDouglas Thompson 109fd309a9dSDouglas Thompson /* Calc the 'end' offset past the dev_blk array 110fd309a9dSDouglas Thompson * which will become the start of the attrib array, if any. 111fd309a9dSDouglas Thompson */ 112fd309a9dSDouglas Thompson /* calc how many nr_attrib we need */ 11393e4fe64SMauro Carvalho Chehab if (nr_attrib > 0) 114fd309a9dSDouglas Thompson count *= nr_attrib; 11593e4fe64SMauro Carvalho Chehab dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count); 116e27e3dacSDouglas Thompson 117e27e3dacSDouglas Thompson /* Calc the 'end' offset past the attributes array */ 11893e4fe64SMauro Carvalho Chehab pvt = edac_align_ptr(&p, sz_private, 1); 119fd309a9dSDouglas Thompson 120fd309a9dSDouglas Thompson /* 'pvt' now points to where the private data area is. 121fd309a9dSDouglas Thompson * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib) 122fd309a9dSDouglas Thompson * is baselined at ZERO 123fd309a9dSDouglas Thompson */ 124e27e3dacSDouglas Thompson total_size = ((unsigned long)pvt) + sz_private; 125e27e3dacSDouglas Thompson 126e27e3dacSDouglas Thompson /* Allocate the amount of memory for the set of control structures */ 12752490c8dSDouglas Thompson dev_ctl = kzalloc(total_size, GFP_KERNEL); 12852490c8dSDouglas Thompson if (dev_ctl == NULL) 129e27e3dacSDouglas Thompson return NULL; 130e27e3dacSDouglas Thompson 131fd309a9dSDouglas Thompson /* Adjust pointers so they point within the actual memory we 132fd309a9dSDouglas Thompson * just allocated rather than an imaginary chunk of memory 133fd309a9dSDouglas Thompson * located at address 0. 134fd309a9dSDouglas Thompson * 'dev_ctl' points to REAL memory, while the others are 135fd309a9dSDouglas Thompson * ZERO based and thus need to be adjusted to point within 136fd309a9dSDouglas Thompson * the allocated memory. 137e27e3dacSDouglas Thompson */ 138e27e3dacSDouglas Thompson dev_inst = (struct edac_device_instance *) 139e27e3dacSDouglas Thompson (((char *)dev_ctl) + ((unsigned long)dev_inst)); 140e27e3dacSDouglas Thompson dev_blk = (struct edac_device_block *) 141e27e3dacSDouglas Thompson (((char *)dev_ctl) + ((unsigned long)dev_blk)); 142fd309a9dSDouglas Thompson dev_attrib = (struct edac_dev_sysfs_block_attribute *) 143e27e3dacSDouglas Thompson (((char *)dev_ctl) + ((unsigned long)dev_attrib)); 144079708b9SDouglas Thompson pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL; 145e27e3dacSDouglas Thompson 146fd309a9dSDouglas Thompson /* Begin storing the information into the control info structure */ 147d45e7823SDoug Thompson dev_ctl->dev_idx = device_index; 148e27e3dacSDouglas Thompson dev_ctl->nr_instances = nr_instances; 149e27e3dacSDouglas Thompson dev_ctl->instances = dev_inst; 150e27e3dacSDouglas Thompson dev_ctl->pvt_info = pvt; 151e27e3dacSDouglas Thompson 15256e61a9cSDoug Thompson /* Default logging of CEs and UEs */ 15356e61a9cSDoug Thompson dev_ctl->log_ce = 1; 15456e61a9cSDoug Thompson dev_ctl->log_ue = 1; 15556e61a9cSDoug Thompson 15652490c8dSDouglas Thompson /* Name of this edac device */ 157e27e3dacSDouglas Thompson snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name); 158e27e3dacSDouglas Thompson 159956b9ba1SJoe Perches edac_dbg(4, "edac_dev=%p next after end=%p\n", 160dd23cd6eSMauro Carvalho Chehab dev_ctl, pvt + sz_private); 161b2a4ac0cSDoug Thompson 162e27e3dacSDouglas Thompson /* Initialize every Instance */ 163e27e3dacSDouglas Thompson for (instance = 0; instance < nr_instances; instance++) { 164e27e3dacSDouglas Thompson inst = &dev_inst[instance]; 165e27e3dacSDouglas Thompson inst->ctl = dev_ctl; 166e27e3dacSDouglas Thompson inst->nr_blocks = nr_blocks; 167e27e3dacSDouglas Thompson blk_p = &dev_blk[instance * nr_blocks]; 168e27e3dacSDouglas Thompson inst->blocks = blk_p; 169e27e3dacSDouglas Thompson 170e27e3dacSDouglas Thompson /* name of this instance */ 171e27e3dacSDouglas Thompson snprintf(inst->name, sizeof(inst->name), 172e27e3dacSDouglas Thompson "%s%u", edac_device_name, instance); 173e27e3dacSDouglas Thompson 174e27e3dacSDouglas Thompson /* Initialize every block in each instance */ 175079708b9SDouglas Thompson for (block = 0; block < nr_blocks; block++) { 176e27e3dacSDouglas Thompson blk = &blk_p[block]; 177e27e3dacSDouglas Thompson blk->instance = inst; 178e27e3dacSDouglas Thompson snprintf(blk->name, sizeof(blk->name), 179d391a7b8SDouglas Thompson "%s%d", edac_block_name, block+offset_value); 180e27e3dacSDouglas Thompson 181956b9ba1SJoe Perches edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n", 182956b9ba1SJoe Perches instance, inst, block, blk, blk->name); 183e27e3dacSDouglas Thompson 184fd309a9dSDouglas Thompson /* if there are NO attributes OR no attribute pointer 185fd309a9dSDouglas Thompson * then continue on to next block iteration 186e27e3dacSDouglas Thompson */ 187fd309a9dSDouglas Thompson if ((nr_attrib == 0) || (attrib_spec == NULL)) 188fd309a9dSDouglas Thompson continue; 189fd309a9dSDouglas Thompson 190fd309a9dSDouglas Thompson /* setup the attribute array for this block */ 191fd309a9dSDouglas Thompson blk->nr_attribs = nr_attrib; 192fd309a9dSDouglas Thompson attrib_p = &dev_attrib[block*nr_instances*nr_attrib]; 193fd309a9dSDouglas Thompson blk->block_attributes = attrib_p; 194fd309a9dSDouglas Thompson 195956b9ba1SJoe Perches edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n", 196dd23cd6eSMauro Carvalho Chehab blk->block_attributes); 197b2a4ac0cSDoug Thompson 198fd309a9dSDouglas Thompson /* Initialize every user specified attribute in this 199fd309a9dSDouglas Thompson * block with the data the caller passed in 200b2a4ac0cSDoug Thompson * Each block gets its own copy of pointers, 201b2a4ac0cSDoug Thompson * and its unique 'value' 202fd309a9dSDouglas Thompson */ 203fd309a9dSDouglas Thompson for (attr = 0; attr < nr_attrib; attr++) { 204e27e3dacSDouglas Thompson attrib = &attrib_p[attr]; 205fd309a9dSDouglas Thompson 206b2a4ac0cSDoug Thompson /* populate the unique per attrib 207b2a4ac0cSDoug Thompson * with the code pointers and info 208b2a4ac0cSDoug Thompson */ 209b2a4ac0cSDoug Thompson attrib->attr = attrib_spec[attr].attr; 210b2a4ac0cSDoug Thompson attrib->show = attrib_spec[attr].show; 211b2a4ac0cSDoug Thompson attrib->store = attrib_spec[attr].store; 212e27e3dacSDouglas Thompson 213b2a4ac0cSDoug Thompson attrib->block = blk; /* up link */ 214b2a4ac0cSDoug Thompson 215956b9ba1SJoe Perches edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n", 216dd23cd6eSMauro Carvalho Chehab attrib, attrib->attr.name, 217b2a4ac0cSDoug Thompson &attrib_spec[attr], 218b2a4ac0cSDoug Thompson attrib_spec[attr].attr.name 219b2a4ac0cSDoug Thompson ); 220e27e3dacSDouglas Thompson } 221e27e3dacSDouglas Thompson } 222e27e3dacSDouglas Thompson } 223e27e3dacSDouglas Thompson 224e27e3dacSDouglas Thompson /* Mark this instance as merely ALLOCATED */ 225e27e3dacSDouglas Thompson dev_ctl->op_state = OP_ALLOC; 226e27e3dacSDouglas Thompson 2271c3631ffSDouglas Thompson /* 2281c3631ffSDouglas Thompson * Initialize the 'root' kobj for the edac_device controller 2291c3631ffSDouglas Thompson */ 2301c3631ffSDouglas Thompson err = edac_device_register_sysfs_main_kobj(dev_ctl); 2311c3631ffSDouglas Thompson if (err) { 2321c3631ffSDouglas Thompson kfree(dev_ctl); 2331c3631ffSDouglas Thompson return NULL; 2341c3631ffSDouglas Thompson } 2351c3631ffSDouglas Thompson 2361c3631ffSDouglas Thompson /* at this point, the root kobj is valid, and in order to 2371c3631ffSDouglas Thompson * 'free' the object, then the function: 2381c3631ffSDouglas Thompson * edac_device_unregister_sysfs_main_kobj() must be called 2391c3631ffSDouglas Thompson * which will perform kobj unregistration and the actual free 2401c3631ffSDouglas Thompson * will occur during the kobject callback operation 2411c3631ffSDouglas Thompson */ 2421c3631ffSDouglas Thompson 243e27e3dacSDouglas Thompson return dev_ctl; 244e27e3dacSDouglas Thompson } 245e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info); 246e27e3dacSDouglas Thompson 247e27e3dacSDouglas Thompson /* 248e27e3dacSDouglas Thompson * edac_device_free_ctl_info() 249e27e3dacSDouglas Thompson * frees the memory allocated by the edac_device_alloc_ctl_info() 250e27e3dacSDouglas Thompson * function 251e27e3dacSDouglas Thompson */ 252079708b9SDouglas Thompson void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info) 253079708b9SDouglas Thompson { 2541c3631ffSDouglas Thompson edac_device_unregister_sysfs_main_kobj(ctl_info); 255e27e3dacSDouglas Thompson } 256e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_free_ctl_info); 257e27e3dacSDouglas Thompson 258e27e3dacSDouglas Thompson /* 259e27e3dacSDouglas Thompson * find_edac_device_by_dev 260e27e3dacSDouglas Thompson * scans the edac_device list for a specific 'struct device *' 26152490c8dSDouglas Thompson * 26252490c8dSDouglas Thompson * lock to be held prior to call: device_ctls_mutex 26352490c8dSDouglas Thompson * 26452490c8dSDouglas Thompson * Return: 26552490c8dSDouglas Thompson * pointer to control structure managing 'dev' 26652490c8dSDouglas Thompson * NULL if not found on list 267e27e3dacSDouglas Thompson */ 268079708b9SDouglas Thompson static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev) 269e27e3dacSDouglas Thompson { 270e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_dev; 271e27e3dacSDouglas Thompson struct list_head *item; 272e27e3dacSDouglas Thompson 273956b9ba1SJoe Perches edac_dbg(0, "\n"); 274e27e3dacSDouglas Thompson 275e27e3dacSDouglas Thompson list_for_each(item, &edac_device_list) { 276e27e3dacSDouglas Thompson edac_dev = list_entry(item, struct edac_device_ctl_info, link); 277e27e3dacSDouglas Thompson 278e27e3dacSDouglas Thompson if (edac_dev->dev == dev) 279e27e3dacSDouglas Thompson return edac_dev; 280e27e3dacSDouglas Thompson } 281e27e3dacSDouglas Thompson 282e27e3dacSDouglas Thompson return NULL; 283e27e3dacSDouglas Thompson } 284e27e3dacSDouglas Thompson 285e27e3dacSDouglas Thompson /* 286e27e3dacSDouglas Thompson * add_edac_dev_to_global_list 287e27e3dacSDouglas Thompson * Before calling this function, caller must 288e27e3dacSDouglas Thompson * assign a unique value to edac_dev->dev_idx. 28952490c8dSDouglas Thompson * 29052490c8dSDouglas Thompson * lock to be held prior to call: device_ctls_mutex 29152490c8dSDouglas Thompson * 292e27e3dacSDouglas Thompson * Return: 293e27e3dacSDouglas Thompson * 0 on success 294e27e3dacSDouglas Thompson * 1 on failure. 295e27e3dacSDouglas Thompson */ 296e27e3dacSDouglas Thompson static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev) 297e27e3dacSDouglas Thompson { 298e27e3dacSDouglas Thompson struct list_head *item, *insert_before; 299e27e3dacSDouglas Thompson struct edac_device_ctl_info *rover; 300e27e3dacSDouglas Thompson 301e27e3dacSDouglas Thompson insert_before = &edac_device_list; 302e27e3dacSDouglas Thompson 303e27e3dacSDouglas Thompson /* Determine if already on the list */ 30452490c8dSDouglas Thompson rover = find_edac_device_by_dev(edac_dev->dev); 30552490c8dSDouglas Thompson if (unlikely(rover != NULL)) 306e27e3dacSDouglas Thompson goto fail0; 307e27e3dacSDouglas Thompson 308e27e3dacSDouglas Thompson /* Insert in ascending order by 'dev_idx', so find position */ 309e27e3dacSDouglas Thompson list_for_each(item, &edac_device_list) { 310e27e3dacSDouglas Thompson rover = list_entry(item, struct edac_device_ctl_info, link); 311e27e3dacSDouglas Thompson 312e27e3dacSDouglas Thompson if (rover->dev_idx >= edac_dev->dev_idx) { 313e27e3dacSDouglas Thompson if (unlikely(rover->dev_idx == edac_dev->dev_idx)) 314e27e3dacSDouglas Thompson goto fail1; 315e27e3dacSDouglas Thompson 316e27e3dacSDouglas Thompson insert_before = item; 317e27e3dacSDouglas Thompson break; 318e27e3dacSDouglas Thompson } 319e27e3dacSDouglas Thompson } 320e27e3dacSDouglas Thompson 321e27e3dacSDouglas Thompson list_add_tail_rcu(&edac_dev->link, insert_before); 322e27e3dacSDouglas Thompson return 0; 323e27e3dacSDouglas Thompson 324e27e3dacSDouglas Thompson fail0: 325e27e3dacSDouglas Thompson edac_printk(KERN_WARNING, EDAC_MC, 326e27e3dacSDouglas Thompson "%s (%s) %s %s already assigned %d\n", 327281efb17SKay Sievers dev_name(rover->dev), edac_dev_name(rover), 328e27e3dacSDouglas Thompson rover->mod_name, rover->ctl_name, rover->dev_idx); 329e27e3dacSDouglas Thompson return 1; 330e27e3dacSDouglas Thompson 331e27e3dacSDouglas Thompson fail1: 332e27e3dacSDouglas Thompson edac_printk(KERN_WARNING, EDAC_MC, 333e27e3dacSDouglas Thompson "bug in low-level driver: attempt to assign\n" 334079708b9SDouglas Thompson " duplicate dev_idx %d in %s()\n", rover->dev_idx, 335079708b9SDouglas Thompson __func__); 336e27e3dacSDouglas Thompson return 1; 337e27e3dacSDouglas Thompson } 338e27e3dacSDouglas Thompson 339e27e3dacSDouglas Thompson /* 340e27e3dacSDouglas Thompson * del_edac_device_from_global_list 341e27e3dacSDouglas Thompson */ 342079708b9SDouglas Thompson static void del_edac_device_from_global_list(struct edac_device_ctl_info 343079708b9SDouglas Thompson *edac_device) 344e27e3dacSDouglas Thompson { 345e27e3dacSDouglas Thompson list_del_rcu(&edac_device->link); 346e2e77098SLai Jiangshan 347e2e77098SLai Jiangshan /* these are for safe removal of devices from global list while 348e2e77098SLai Jiangshan * NMI handlers may be traversing list 349e2e77098SLai Jiangshan */ 350e2e77098SLai Jiangshan synchronize_rcu(); 351e2e77098SLai Jiangshan INIT_LIST_HEAD(&edac_device->link); 352e27e3dacSDouglas Thompson } 353e27e3dacSDouglas Thompson 354e27e3dacSDouglas Thompson /* 35581d87cb1SDave Jiang * edac_device_workq_function 356e27e3dacSDouglas Thompson * performs the operation scheduled by a workq request 357bf52fa4aSDoug Thompson * 358bf52fa4aSDoug Thompson * this workq is embedded within an edac_device_ctl_info 359bf52fa4aSDoug Thompson * structure, that needs to be polled for possible error events. 360bf52fa4aSDoug Thompson * 361bf52fa4aSDoug Thompson * This operation is to acquire the list mutex lock 362f70d4a95SJiri Kosina * (thus preventing insertation or deletion) 363bf52fa4aSDoug Thompson * and then call the device's poll function IFF this device is 364bf52fa4aSDoug Thompson * running polled and there is a poll function defined. 365e27e3dacSDouglas Thompson */ 36681d87cb1SDave Jiang static void edac_device_workq_function(struct work_struct *work_req) 367e27e3dacSDouglas Thompson { 368fbeb4384SJean Delvare struct delayed_work *d_work = to_delayed_work(work_req); 369079708b9SDouglas Thompson struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work); 370e27e3dacSDouglas Thompson 3710ca84761SDoug Thompson mutex_lock(&device_ctls_mutex); 372e27e3dacSDouglas Thompson 373d519c8d9SHarry Ciao /* If we are being removed, bail out immediately */ 374d519c8d9SHarry Ciao if (edac_dev->op_state == OP_OFFLINE) { 375d519c8d9SHarry Ciao mutex_unlock(&device_ctls_mutex); 376d519c8d9SHarry Ciao return; 377d519c8d9SHarry Ciao } 378d519c8d9SHarry Ciao 379e27e3dacSDouglas Thompson /* Only poll controllers that are running polled and have a check */ 380e27e3dacSDouglas Thompson if ((edac_dev->op_state == OP_RUNNING_POLL) && 381e27e3dacSDouglas Thompson (edac_dev->edac_check != NULL)) { 382e27e3dacSDouglas Thompson edac_dev->edac_check(edac_dev); 383e27e3dacSDouglas Thompson } 384e27e3dacSDouglas Thompson 3850ca84761SDoug Thompson mutex_unlock(&device_ctls_mutex); 386e27e3dacSDouglas Thompson 387bf52fa4aSDoug Thompson /* Reschedule the workq for the next time period to start again 388bf52fa4aSDoug Thompson * if the number of msec is for 1 sec, then adjust to the next 38915ed103aSDavid Mackey * whole one second to save timers firing all over the period 390bf52fa4aSDoug Thompson * between integral seconds 391bf52fa4aSDoug Thompson */ 392bf52fa4aSDoug Thompson if (edac_dev->poll_msec == 1000) 393bf52fa4aSDoug Thompson queue_delayed_work(edac_workqueue, &edac_dev->work, 394c2ae24cfSAnton Blanchard round_jiffies_relative(edac_dev->delay)); 395bf52fa4aSDoug Thompson else 396bf52fa4aSDoug Thompson queue_delayed_work(edac_workqueue, &edac_dev->work, 397bf52fa4aSDoug Thompson edac_dev->delay); 398e27e3dacSDouglas Thompson } 399e27e3dacSDouglas Thompson 400e27e3dacSDouglas Thompson /* 40181d87cb1SDave Jiang * edac_device_workq_setup 402e27e3dacSDouglas Thompson * initialize a workq item for this edac_device instance 403e27e3dacSDouglas Thompson * passing in the new delay period in msec 404e27e3dacSDouglas Thompson */ 40581d87cb1SDave Jiang void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev, 40681d87cb1SDave Jiang unsigned msec) 407e27e3dacSDouglas Thompson { 408956b9ba1SJoe Perches edac_dbg(0, "\n"); 409e27e3dacSDouglas Thompson 410bf52fa4aSDoug Thompson /* take the arg 'msec' and set it into the control structure 411bf52fa4aSDoug Thompson * to used in the time period calculation 412bf52fa4aSDoug Thompson * then calc the number of jiffies that represents 413bf52fa4aSDoug Thompson */ 414e27e3dacSDouglas Thompson edac_dev->poll_msec = msec; 415bf52fa4aSDoug Thompson edac_dev->delay = msecs_to_jiffies(msec); 416e27e3dacSDouglas Thompson 41781d87cb1SDave Jiang INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function); 418bf52fa4aSDoug Thompson 419bf52fa4aSDoug Thompson /* optimize here for the 1 second case, which will be normal value, to 420bf52fa4aSDoug Thompson * fire ON the 1 second time event. This helps reduce all sorts of 421bf52fa4aSDoug Thompson * timers firing on sub-second basis, while they are happy 422bf52fa4aSDoug Thompson * to fire together on the 1 second exactly 423bf52fa4aSDoug Thompson */ 424bf52fa4aSDoug Thompson if (edac_dev->poll_msec == 1000) 425bf52fa4aSDoug Thompson queue_delayed_work(edac_workqueue, &edac_dev->work, 426c2ae24cfSAnton Blanchard round_jiffies_relative(edac_dev->delay)); 427bf52fa4aSDoug Thompson else 428bf52fa4aSDoug Thompson queue_delayed_work(edac_workqueue, &edac_dev->work, 429bf52fa4aSDoug Thompson edac_dev->delay); 430e27e3dacSDouglas Thompson } 431e27e3dacSDouglas Thompson 432e27e3dacSDouglas Thompson /* 43381d87cb1SDave Jiang * edac_device_workq_teardown 434e27e3dacSDouglas Thompson * stop the workq processing on this edac_dev 435e27e3dacSDouglas Thompson */ 43681d87cb1SDave Jiang void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev) 437e27e3dacSDouglas Thompson { 438881f0fceSStephen Boyd if (!edac_dev->edac_check) 439881f0fceSStephen Boyd return; 440881f0fceSStephen Boyd 441*fcd5c4ddSBorislav Petkov edac_dev->op_state = OP_OFFLINE; 442*fcd5c4ddSBorislav Petkov 443*fcd5c4ddSBorislav Petkov cancel_delayed_work_sync(&edac_dev->work); 444e27e3dacSDouglas Thompson flush_workqueue(edac_workqueue); 445e27e3dacSDouglas Thompson } 446e27e3dacSDouglas Thompson 447e27e3dacSDouglas Thompson /* 448e27e3dacSDouglas Thompson * edac_device_reset_delay_period 449bf52fa4aSDoug Thompson * 450bf52fa4aSDoug Thompson * need to stop any outstanding workq queued up at this time 451bf52fa4aSDoug Thompson * because we will be resetting the sleep time. 452bf52fa4aSDoug Thompson * Then restart the workq on the new delay 453e27e3dacSDouglas Thompson */ 454079708b9SDouglas Thompson void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev, 455e27e3dacSDouglas Thompson unsigned long value) 456e27e3dacSDouglas Thompson { 457bf52fa4aSDoug Thompson /* cancel the current workq request, without the mutex lock */ 45881d87cb1SDave Jiang edac_device_workq_teardown(edac_dev); 459e27e3dacSDouglas Thompson 460bf52fa4aSDoug Thompson /* acquire the mutex before doing the workq setup */ 461bf52fa4aSDoug Thompson mutex_lock(&device_ctls_mutex); 462bf52fa4aSDoug Thompson 463e27e3dacSDouglas Thompson /* restart the workq request, with new delay value */ 46481d87cb1SDave Jiang edac_device_workq_setup(edac_dev, value); 465e27e3dacSDouglas Thompson 4660ca84761SDoug Thompson mutex_unlock(&device_ctls_mutex); 467e27e3dacSDouglas Thompson } 468e27e3dacSDouglas Thompson 4691dc9b70dSHarry Ciao /* 4701dc9b70dSHarry Ciao * edac_device_alloc_index: Allocate a unique device index number 4711dc9b70dSHarry Ciao * 4721dc9b70dSHarry Ciao * Return: 4731dc9b70dSHarry Ciao * allocated index number 4741dc9b70dSHarry Ciao */ 4751dc9b70dSHarry Ciao int edac_device_alloc_index(void) 4761dc9b70dSHarry Ciao { 4771dc9b70dSHarry Ciao static atomic_t device_indexes = ATOMIC_INIT(0); 4781dc9b70dSHarry Ciao 4791dc9b70dSHarry Ciao return atomic_inc_return(&device_indexes) - 1; 4801dc9b70dSHarry Ciao } 4811dc9b70dSHarry Ciao EXPORT_SYMBOL_GPL(edac_device_alloc_index); 4821dc9b70dSHarry Ciao 483e27e3dacSDouglas Thompson /** 484e27e3dacSDouglas Thompson * edac_device_add_device: Insert the 'edac_dev' structure into the 485e27e3dacSDouglas Thompson * edac_device global list and create sysfs entries associated with 486e27e3dacSDouglas Thompson * edac_device structure. 487e27e3dacSDouglas Thompson * @edac_device: pointer to the edac_device structure to be added to the list 488e27e3dacSDouglas Thompson * 'edac_device' structure. 489e27e3dacSDouglas Thompson * 490e27e3dacSDouglas Thompson * Return: 491e27e3dacSDouglas Thompson * 0 Success 492e27e3dacSDouglas Thompson * !0 Failure 493e27e3dacSDouglas Thompson */ 494d45e7823SDoug Thompson int edac_device_add_device(struct edac_device_ctl_info *edac_dev) 495e27e3dacSDouglas Thompson { 496956b9ba1SJoe Perches edac_dbg(0, "\n"); 497e27e3dacSDouglas Thompson 498e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG 499e27e3dacSDouglas Thompson if (edac_debug_level >= 3) 500e27e3dacSDouglas Thompson edac_device_dump_device(edac_dev); 501e27e3dacSDouglas Thompson #endif 5020ca84761SDoug Thompson mutex_lock(&device_ctls_mutex); 503e27e3dacSDouglas Thompson 504e27e3dacSDouglas Thompson if (add_edac_dev_to_global_list(edac_dev)) 505e27e3dacSDouglas Thompson goto fail0; 506e27e3dacSDouglas Thompson 507e27e3dacSDouglas Thompson /* set load time so that error rate can be tracked */ 508e27e3dacSDouglas Thompson edac_dev->start_time = jiffies; 509e27e3dacSDouglas Thompson 510e27e3dacSDouglas Thompson /* create this instance's sysfs entries */ 511e27e3dacSDouglas Thompson if (edac_device_create_sysfs(edac_dev)) { 512e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_WARNING, 513e27e3dacSDouglas Thompson "failed to create sysfs device\n"); 514e27e3dacSDouglas Thompson goto fail1; 515e27e3dacSDouglas Thompson } 516e27e3dacSDouglas Thompson 517e27e3dacSDouglas Thompson /* If there IS a check routine, then we are running POLLED */ 518e27e3dacSDouglas Thompson if (edac_dev->edac_check != NULL) { 519e27e3dacSDouglas Thompson /* This instance is NOW RUNNING */ 520e27e3dacSDouglas Thompson edac_dev->op_state = OP_RUNNING_POLL; 521e27e3dacSDouglas Thompson 52281d87cb1SDave Jiang /* 52381d87cb1SDave Jiang * enable workq processing on this instance, 52481d87cb1SDave Jiang * default = 1000 msec 52581d87cb1SDave Jiang */ 52681d87cb1SDave Jiang edac_device_workq_setup(edac_dev, 1000); 527e27e3dacSDouglas Thompson } else { 528e27e3dacSDouglas Thompson edac_dev->op_state = OP_RUNNING_INTERRUPT; 529e27e3dacSDouglas Thompson } 530e27e3dacSDouglas Thompson 531e27e3dacSDouglas Thompson /* Report action taken */ 532e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_INFO, 5337270a608SRobert Richter "Giving out device to module %s controller %s: DEV %s (%s)\n", 5347270a608SRobert Richter edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name, 535494d0d55SDouglas Thompson edac_op_state_to_string(edac_dev->op_state)); 536e27e3dacSDouglas Thompson 5370ca84761SDoug Thompson mutex_unlock(&device_ctls_mutex); 538e27e3dacSDouglas Thompson return 0; 539e27e3dacSDouglas Thompson 540e27e3dacSDouglas Thompson fail1: 541e27e3dacSDouglas Thompson /* Some error, so remove the entry from the lsit */ 542e27e3dacSDouglas Thompson del_edac_device_from_global_list(edac_dev); 543e27e3dacSDouglas Thompson 544e27e3dacSDouglas Thompson fail0: 5450ca84761SDoug Thompson mutex_unlock(&device_ctls_mutex); 546e27e3dacSDouglas Thompson return 1; 547e27e3dacSDouglas Thompson } 548e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_add_device); 549e27e3dacSDouglas Thompson 550e27e3dacSDouglas Thompson /** 551e27e3dacSDouglas Thompson * edac_device_del_device: 552e27e3dacSDouglas Thompson * Remove sysfs entries for specified edac_device structure and 553e27e3dacSDouglas Thompson * then remove edac_device structure from global list 554e27e3dacSDouglas Thompson * 55515ed103aSDavid Mackey * @dev: 556e27e3dacSDouglas Thompson * Pointer to 'struct device' representing edac_device 557e27e3dacSDouglas Thompson * structure to remove. 558e27e3dacSDouglas Thompson * 559e27e3dacSDouglas Thompson * Return: 560e27e3dacSDouglas Thompson * Pointer to removed edac_device structure, 561e27e3dacSDouglas Thompson * OR NULL if device not found. 562e27e3dacSDouglas Thompson */ 563e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_del_device(struct device *dev) 564e27e3dacSDouglas Thompson { 565e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_dev; 566e27e3dacSDouglas Thompson 567956b9ba1SJoe Perches edac_dbg(0, "\n"); 568e27e3dacSDouglas Thompson 5690ca84761SDoug Thompson mutex_lock(&device_ctls_mutex); 570e27e3dacSDouglas Thompson 57152490c8dSDouglas Thompson /* Find the structure on the list, if not there, then leave */ 57252490c8dSDouglas Thompson edac_dev = find_edac_device_by_dev(dev); 57352490c8dSDouglas Thompson if (edac_dev == NULL) { 5740ca84761SDoug Thompson mutex_unlock(&device_ctls_mutex); 575e27e3dacSDouglas Thompson return NULL; 576e27e3dacSDouglas Thompson } 577e27e3dacSDouglas Thompson 578e27e3dacSDouglas Thompson /* mark this instance as OFFLINE */ 579e27e3dacSDouglas Thompson edac_dev->op_state = OP_OFFLINE; 580e27e3dacSDouglas Thompson 581e27e3dacSDouglas Thompson /* deregister from global list */ 582e27e3dacSDouglas Thompson del_edac_device_from_global_list(edac_dev); 583e27e3dacSDouglas Thompson 5840ca84761SDoug Thompson mutex_unlock(&device_ctls_mutex); 585e27e3dacSDouglas Thompson 586d519c8d9SHarry Ciao /* clear workq processing on this instance */ 587d519c8d9SHarry Ciao edac_device_workq_teardown(edac_dev); 588d519c8d9SHarry Ciao 5891c3631ffSDouglas Thompson /* Tear down the sysfs entries for this instance */ 5901c3631ffSDouglas Thompson edac_device_remove_sysfs(edac_dev); 5911c3631ffSDouglas Thompson 592e27e3dacSDouglas Thompson edac_printk(KERN_INFO, EDAC_MC, 593e27e3dacSDouglas Thompson "Removed device %d for %s %s: DEV %s\n", 594e27e3dacSDouglas Thompson edac_dev->dev_idx, 59517aa7e03SStephen Rothwell edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev)); 596e27e3dacSDouglas Thompson 597e27e3dacSDouglas Thompson return edac_dev; 598e27e3dacSDouglas Thompson } 599079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_del_device); 600e27e3dacSDouglas Thompson 601e27e3dacSDouglas Thompson static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev) 602e27e3dacSDouglas Thompson { 603e27e3dacSDouglas Thompson return edac_dev->log_ce; 604e27e3dacSDouglas Thompson } 605e27e3dacSDouglas Thompson 606e27e3dacSDouglas Thompson static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev) 607e27e3dacSDouglas Thompson { 608e27e3dacSDouglas Thompson return edac_dev->log_ue; 609e27e3dacSDouglas Thompson } 610e27e3dacSDouglas Thompson 611079708b9SDouglas Thompson static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info 612079708b9SDouglas Thompson *edac_dev) 613e27e3dacSDouglas Thompson { 614e27e3dacSDouglas Thompson return edac_dev->panic_on_ue; 615e27e3dacSDouglas Thompson } 616e27e3dacSDouglas Thompson 617e27e3dacSDouglas Thompson /* 618e27e3dacSDouglas Thompson * edac_device_handle_ce 619e27e3dacSDouglas Thompson * perform a common output and handling of an 'edac_dev' CE event 620e27e3dacSDouglas Thompson */ 621e27e3dacSDouglas Thompson void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev, 622e27e3dacSDouglas Thompson int inst_nr, int block_nr, const char *msg) 623e27e3dacSDouglas Thompson { 624e27e3dacSDouglas Thompson struct edac_device_instance *instance; 625e27e3dacSDouglas Thompson struct edac_device_block *block = NULL; 626e27e3dacSDouglas Thompson 627e27e3dacSDouglas Thompson if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) { 628e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_ERR, 629e27e3dacSDouglas Thompson "INTERNAL ERROR: 'instance' out of range " 630079708b9SDouglas Thompson "(%d >= %d)\n", inst_nr, 631079708b9SDouglas Thompson edac_dev->nr_instances); 632e27e3dacSDouglas Thompson return; 633e27e3dacSDouglas Thompson } 634e27e3dacSDouglas Thompson 635e27e3dacSDouglas Thompson instance = edac_dev->instances + inst_nr; 636e27e3dacSDouglas Thompson 637e27e3dacSDouglas Thompson if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) { 638e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_ERR, 639052dfb45SDouglas Thompson "INTERNAL ERROR: instance %d 'block' " 640052dfb45SDouglas Thompson "out of range (%d >= %d)\n", 641052dfb45SDouglas Thompson inst_nr, block_nr, 642079708b9SDouglas Thompson instance->nr_blocks); 643e27e3dacSDouglas Thompson return; 644e27e3dacSDouglas Thompson } 645e27e3dacSDouglas Thompson 646e27e3dacSDouglas Thompson if (instance->nr_blocks > 0) { 647e27e3dacSDouglas Thompson block = instance->blocks + block_nr; 648e27e3dacSDouglas Thompson block->counters.ce_count++; 649e27e3dacSDouglas Thompson } 650e27e3dacSDouglas Thompson 65125985edcSLucas De Marchi /* Propagate the count up the 'totals' tree */ 652e27e3dacSDouglas Thompson instance->counters.ce_count++; 653e27e3dacSDouglas Thompson edac_dev->counters.ce_count++; 654e27e3dacSDouglas Thompson 655e27e3dacSDouglas Thompson if (edac_device_get_log_ce(edac_dev)) 656e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_WARNING, 657d391a7b8SDouglas Thompson "CE: %s instance: %s block: %s '%s'\n", 658e27e3dacSDouglas Thompson edac_dev->ctl_name, instance->name, 659e27e3dacSDouglas Thompson block ? block->name : "N/A", msg); 660e27e3dacSDouglas Thompson } 661e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ce); 662e27e3dacSDouglas Thompson 663e27e3dacSDouglas Thompson /* 664e27e3dacSDouglas Thompson * edac_device_handle_ue 665e27e3dacSDouglas Thompson * perform a common output and handling of an 'edac_dev' UE event 666e27e3dacSDouglas Thompson */ 667e27e3dacSDouglas Thompson void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev, 668e27e3dacSDouglas Thompson int inst_nr, int block_nr, const char *msg) 669e27e3dacSDouglas Thompson { 670e27e3dacSDouglas Thompson struct edac_device_instance *instance; 671e27e3dacSDouglas Thompson struct edac_device_block *block = NULL; 672e27e3dacSDouglas Thompson 673e27e3dacSDouglas Thompson if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) { 674e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_ERR, 675e27e3dacSDouglas Thompson "INTERNAL ERROR: 'instance' out of range " 676079708b9SDouglas Thompson "(%d >= %d)\n", inst_nr, 677079708b9SDouglas Thompson edac_dev->nr_instances); 678e27e3dacSDouglas Thompson return; 679e27e3dacSDouglas Thompson } 680e27e3dacSDouglas Thompson 681e27e3dacSDouglas Thompson instance = edac_dev->instances + inst_nr; 682e27e3dacSDouglas Thompson 683e27e3dacSDouglas Thompson if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) { 684e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_ERR, 685052dfb45SDouglas Thompson "INTERNAL ERROR: instance %d 'block' " 686052dfb45SDouglas Thompson "out of range (%d >= %d)\n", 687052dfb45SDouglas Thompson inst_nr, block_nr, 688079708b9SDouglas Thompson instance->nr_blocks); 689e27e3dacSDouglas Thompson return; 690e27e3dacSDouglas Thompson } 691e27e3dacSDouglas Thompson 692e27e3dacSDouglas Thompson if (instance->nr_blocks > 0) { 693e27e3dacSDouglas Thompson block = instance->blocks + block_nr; 694e27e3dacSDouglas Thompson block->counters.ue_count++; 695e27e3dacSDouglas Thompson } 696e27e3dacSDouglas Thompson 69725985edcSLucas De Marchi /* Propagate the count up the 'totals' tree */ 698e27e3dacSDouglas Thompson instance->counters.ue_count++; 699e27e3dacSDouglas Thompson edac_dev->counters.ue_count++; 700e27e3dacSDouglas Thompson 701e27e3dacSDouglas Thompson if (edac_device_get_log_ue(edac_dev)) 702e27e3dacSDouglas Thompson edac_device_printk(edac_dev, KERN_EMERG, 703d391a7b8SDouglas Thompson "UE: %s instance: %s block: %s '%s'\n", 704e27e3dacSDouglas Thompson edac_dev->ctl_name, instance->name, 705e27e3dacSDouglas Thompson block ? block->name : "N/A", msg); 706e27e3dacSDouglas Thompson 707e27e3dacSDouglas Thompson if (edac_device_get_panic_on_ue(edac_dev)) 708d391a7b8SDouglas Thompson panic("EDAC %s: UE instance: %s block %s '%s'\n", 709e27e3dacSDouglas Thompson edac_dev->ctl_name, instance->name, 710e27e3dacSDouglas Thompson block ? block->name : "N/A", msg); 711e27e3dacSDouglas Thompson } 712079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ue); 713