edac_device.c

来自「linux 内核源代码」· C语言 代码 · 共 747 行 · 第 1/2 页

C
747
字号
/* * edac_device.c * (C) 2007 www.douglaskthompson.com * * This file may be distributed under the terms of the * GNU General Public License. * * Written by Doug Thompson <norsk5@xmission.com> * * edac_device API implementation * 19 Jan 2007 */#include <linux/module.h>#include <linux/types.h>#include <linux/smp.h>#include <linux/init.h>#include <linux/sysctl.h>#include <linux/highmem.h>#include <linux/timer.h>#include <linux/slab.h>#include <linux/jiffies.h>#include <linux/spinlock.h>#include <linux/list.h>#include <linux/sysdev.h>#include <linux/ctype.h>#include <linux/workqueue.h>#include <asm/uaccess.h>#include <asm/page.h>#include "edac_core.h"#include "edac_module.h"/* lock for the list: 'edac_device_list', manipulation of this list * is protected by the 'device_ctls_mutex' lock */static DEFINE_MUTEX(device_ctls_mutex);static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);#ifdef CONFIG_EDAC_DEBUGstatic void edac_device_dump_device(struct edac_device_ctl_info *edac_dev){	debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);	debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);	debugf3("\tdev = %p\n", edac_dev->dev);	debugf3("\tmod_name:ctl_name = %s:%s\n",		edac_dev->mod_name, edac_dev->ctl_name);	debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);}#endif				/* CONFIG_EDAC_DEBUG *//* * edac_device_alloc_ctl_info() *	Allocate a new edac device control info structure * *	The control structure is allocated in complete chunk *	from the OS. It is in turn sub allocated to the *	various objects that compose the struture * *	The structure has a 'nr_instance' array within itself. *	Each instance represents a major component *		Example:  L1 cache and L2 cache are 2 instance components * *	Within each instance is an array of 'nr_blocks' blockoffsets */struct edac_device_ctl_info *edac_device_alloc_ctl_info(	unsigned sz_private,	char *edac_device_name, unsigned nr_instances,	char *edac_block_name, unsigned nr_blocks,	unsigned offset_value,		/* zero, 1, or other based offset */	struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,	int device_index){	struct edac_device_ctl_info *dev_ctl;	struct edac_device_instance *dev_inst, *inst;	struct edac_device_block *dev_blk, *blk_p, *blk;	struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;	unsigned total_size;	unsigned count;	unsigned instance, block, attr;	void *pvt;	int err;	debugf4("%s() instances=%d blocks=%d\n",		__func__, nr_instances, nr_blocks);	/* Calculate the size of memory we need to allocate AND	 * determine the offsets of the various item arrays	 * (instance,block,attrib) from the start of an  allocated structure.	 * We want the alignment of each item  (instance,block,attrib)	 * to be at least as stringent as what the compiler would	 * provide if we could simply hardcode everything into a single struct.	 */	dev_ctl = (struct edac_device_ctl_info *)NULL;	/* Calc the 'end' offset past end of ONE ctl_info structure	 * which will become the start of the 'instance' array	 */	dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));	/* Calc the 'end' offset past the instance array within the ctl_info	 * which will become the start of the block array	 */	dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));	/* Calc the 'end' offset past the dev_blk array	 * which will become the start of the attrib array, if any.	 */	count = nr_instances * nr_blocks;	dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));	/* Check for case of when an attribute array is specified */	if (nr_attrib > 0) {		/* calc how many nr_attrib we need */		count *= nr_attrib;		/* Calc the 'end' offset past the attributes array */		pvt = edac_align_ptr(&dev_attrib[count], sz_private);	} else {		/* no attribute array specificed */		pvt = edac_align_ptr(dev_attrib, sz_private);	}	/* 'pvt' now points to where the private data area is.	 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)	 * is baselined at ZERO	 */	total_size = ((unsigned long)pvt) + sz_private;	/* Allocate the amount of memory for the set of control structures */	dev_ctl = kzalloc(total_size, GFP_KERNEL);	if (dev_ctl == NULL)		return NULL;	/* Adjust pointers so they point within the actual memory we	 * just allocated rather than an imaginary chunk of memory	 * located at address 0.	 * 'dev_ctl' points to REAL memory, while the others are	 * ZERO based and thus need to be adjusted to point within	 * the allocated memory.	 */	dev_inst = (struct edac_device_instance *)		(((char *)dev_ctl) + ((unsigned long)dev_inst));	dev_blk = (struct edac_device_block *)		(((char *)dev_ctl) + ((unsigned long)dev_blk));	dev_attrib = (struct edac_dev_sysfs_block_attribute *)		(((char *)dev_ctl) + ((unsigned long)dev_attrib));	pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;	/* Begin storing the information into the control info structure */	dev_ctl->dev_idx = device_index;	dev_ctl->nr_instances = nr_instances;	dev_ctl->instances = dev_inst;	dev_ctl->pvt_info = pvt;	/* Name of this edac device */	snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);	debugf4("%s() edac_dev=%p next after end=%p\n",		__func__, dev_ctl, pvt + sz_private );	/* Initialize every Instance */	for (instance = 0; instance < nr_instances; instance++) {		inst = &dev_inst[instance];		inst->ctl = dev_ctl;		inst->nr_blocks = nr_blocks;		blk_p = &dev_blk[instance * nr_blocks];		inst->blocks = blk_p;		/* name of this instance */		snprintf(inst->name, sizeof(inst->name),			 "%s%u", edac_device_name, instance);		/* Initialize every block in each instance */		for (block = 0; block < nr_blocks; block++) {			blk = &blk_p[block];			blk->instance = inst;			snprintf(blk->name, sizeof(blk->name),				 "%s%d", edac_block_name, block+offset_value);			debugf4("%s() instance=%d inst_p=%p block=#%d "				"block_p=%p name='%s'\n",				__func__, instance, inst, block,				blk, blk->name);			/* if there are NO attributes OR no attribute pointer			 * then continue on to next block iteration			 */			if ((nr_attrib == 0) || (attrib_spec == NULL))				continue;			/* setup the attribute array for this block */			blk->nr_attribs = nr_attrib;			attrib_p = &dev_attrib[block*nr_instances*nr_attrib];			blk->block_attributes = attrib_p;			debugf4("%s() THIS BLOCK_ATTRIB=%p\n",				__func__, blk->block_attributes);			/* Initialize every user specified attribute in this			 * block with the data the caller passed in			 * Each block gets its own copy of pointers,			 * and its unique 'value'			 */			for (attr = 0; attr < nr_attrib; attr++) {				attrib = &attrib_p[attr];				/* populate the unique per attrib				 * with the code pointers and info				 */				attrib->attr = attrib_spec[attr].attr;				attrib->show = attrib_spec[attr].show;				attrib->store = attrib_spec[attr].store;				attrib->block = blk;	/* up link */				debugf4("%s() alloc-attrib=%p attrib_name='%s' "					"attrib-spec=%p spec-name=%s\n",					__func__, attrib, attrib->attr.name,					&attrib_spec[attr],					attrib_spec[attr].attr.name					);			}		}	}	/* Mark this instance as merely ALLOCATED */	dev_ctl->op_state = OP_ALLOC;	/*	 * Initialize the 'root' kobj for the edac_device controller	 */	err = edac_device_register_sysfs_main_kobj(dev_ctl);	if (err) {		kfree(dev_ctl);		return NULL;	}	/* at this point, the root kobj is valid, and in order to	 * 'free' the object, then the function:	 *	edac_device_unregister_sysfs_main_kobj() must be called	 * which will perform kobj unregistration and the actual free	 * will occur during the kobject callback operation	 */	return dev_ctl;}EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);/* * edac_device_free_ctl_info() *	frees the memory allocated by the edac_device_alloc_ctl_info() *	function */void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info){	edac_device_unregister_sysfs_main_kobj(ctl_info);}EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);/* * find_edac_device_by_dev *	scans the edac_device list for a specific 'struct device *' * *	lock to be held prior to call:	device_ctls_mutex * *	Return: *		pointer to control structure managing 'dev' *		NULL if not found on list */static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev){	struct edac_device_ctl_info *edac_dev;	struct list_head *item;	debugf0("%s()\n", __func__);	list_for_each(item, &edac_device_list) {		edac_dev = list_entry(item, struct edac_device_ctl_info, link);		if (edac_dev->dev == dev)			return edac_dev;	}	return NULL;}/* * add_edac_dev_to_global_list *	Before calling this function, caller must *	assign a unique value to edac_dev->dev_idx. * *	lock to be held prior to call:	device_ctls_mutex * *	Return: *		0 on success *		1 on failure. */static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev){	struct list_head *item, *insert_before;	struct edac_device_ctl_info *rover;	insert_before = &edac_device_list;	/* Determine if already on the list */	rover = find_edac_device_by_dev(edac_dev->dev);	if (unlikely(rover != NULL))		goto fail0;	/* Insert in ascending order by 'dev_idx', so find position */	list_for_each(item, &edac_device_list) {		rover = list_entry(item, struct edac_device_ctl_info, link);		if (rover->dev_idx >= edac_dev->dev_idx) {			if (unlikely(rover->dev_idx == edac_dev->dev_idx))				goto fail1;			insert_before = item;			break;		}	}	list_add_tail_rcu(&edac_dev->link, insert_before);	return 0;fail0:	edac_printk(KERN_WARNING, EDAC_MC,			"%s (%s) %s %s already assigned %d\n",			rover->dev->bus_id, dev_name(rover),			rover->mod_name, rover->ctl_name, rover->dev_idx);	return 1;fail1:	edac_printk(KERN_WARNING, EDAC_MC,			"bug in low-level driver: attempt to assign\n"			"    duplicate dev_idx %d in %s()\n", rover->dev_idx,			__func__);	return 1;}/* * complete_edac_device_list_del * *	callback function when reference count is zero */static void complete_edac_device_list_del(struct rcu_head *head){	struct edac_device_ctl_info *edac_dev;	edac_dev = container_of(head, struct edac_device_ctl_info, rcu);	INIT_LIST_HEAD(&edac_dev->link);	complete(&edac_dev->removal_complete);}/* * del_edac_device_from_global_list * *	remove the RCU, setup for a callback call, *	then wait for the callback to occur */static void del_edac_device_from_global_list(struct edac_device_ctl_info						*edac_device){	list_del_rcu(&edac_device->link);	init_completion(&edac_device->removal_complete);	call_rcu(&edac_device->rcu, complete_edac_device_list_del);	wait_for_completion(&edac_device->removal_complete);}/**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?