⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 device.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2004 Topspin Communications.  All rights reserved. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses.  You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * *     Redistribution and use in source and binary forms, with or *     without modification, are permitted provided that the following *     conditions are met: * *      - Redistributions of source code must retain the above *        copyright notice, this list of conditions and the following *        disclaimer. * *      - Redistributions in binary form must reproduce the above *        copyright notice, this list of conditions and the following *        disclaimer in the documentation and/or other materials *        provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * $Id: device.c 1349 2004-12-16 21:09:43Z roland $ */#include <linux/module.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/init.h>#include <asm/semaphore.h>#include "core_priv.h"MODULE_AUTHOR("Roland Dreier");MODULE_DESCRIPTION("core kernel InfiniBand API");MODULE_LICENSE("Dual BSD/GPL");struct ib_client_data {	struct list_head  list;	struct ib_client *client;	void *            data;};static LIST_HEAD(device_list);static LIST_HEAD(client_list);/* * device_sem protects access to both device_list and client_list. * There's no real point to using multiple locks or something fancier * like an rwsem: we always access both lists, and we're always * modifying one list or the other list.  In any case this is not a * hot path so there's no point in trying to optimize. */static DECLARE_MUTEX(device_sem);static int ib_device_check_mandatory(struct ib_device *device){#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }	static const struct {		size_t offset;		char  *name;	} mandatory_table[] = {		IB_MANDATORY_FUNC(query_device),		IB_MANDATORY_FUNC(query_port),		IB_MANDATORY_FUNC(query_pkey),		IB_MANDATORY_FUNC(query_gid),		IB_MANDATORY_FUNC(alloc_pd),		IB_MANDATORY_FUNC(dealloc_pd),		IB_MANDATORY_FUNC(create_ah),		IB_MANDATORY_FUNC(destroy_ah),		IB_MANDATORY_FUNC(create_qp),		IB_MANDATORY_FUNC(modify_qp),		IB_MANDATORY_FUNC(destroy_qp),		IB_MANDATORY_FUNC(post_send),		IB_MANDATORY_FUNC(post_recv),		IB_MANDATORY_FUNC(create_cq),		IB_MANDATORY_FUNC(destroy_cq),		IB_MANDATORY_FUNC(poll_cq),		IB_MANDATORY_FUNC(req_notify_cq),		IB_MANDATORY_FUNC(get_dma_mr),		IB_MANDATORY_FUNC(dereg_mr)	};	int i;	for (i = 0; i < sizeof mandatory_table / sizeof mandatory_table[0]; ++i) {		if (!*(void **) ((void *) device + mandatory_table[i].offset)) {			printk(KERN_WARNING "Device %s is missing mandatory function %s\n",			       device->name, mandatory_table[i].name);			return -EINVAL;		}	}	return 0;}static struct ib_device *__ib_device_get_by_name(const char *name){	struct ib_device *device;	list_for_each_entry(device, &device_list, core_list)		if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))			return device;	return NULL;}static int alloc_name(char *name){	long *inuse;	char buf[IB_DEVICE_NAME_MAX];	struct ib_device *device;	int i;	inuse = (long *) get_zeroed_page(GFP_KERNEL);	if (!inuse)		return -ENOMEM;	list_for_each_entry(device, &device_list, core_list) {		if (!sscanf(device->name, name, &i))			continue;		if (i < 0 || i >= PAGE_SIZE * 8)			continue;		snprintf(buf, sizeof buf, name, i);		if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))			set_bit(i, inuse);	}	i = find_first_zero_bit(inuse, PAGE_SIZE * 8);	free_page((unsigned long) inuse);	snprintf(buf, sizeof buf, name, i);	if (__ib_device_get_by_name(buf))		return -ENFILE;	strlcpy(name, buf, IB_DEVICE_NAME_MAX);	return 0;}/** * ib_alloc_device - allocate an IB device struct * @size:size of structure to allocate * * Low-level drivers should use ib_alloc_device() to allocate &struct * ib_device.  @size is the size of the structure to be allocated, * including any private data used by the low-level driver. * ib_dealloc_device() must be used to free structures allocated with * ib_alloc_device(). */struct ib_device *ib_alloc_device(size_t size){	BUG_ON(size < sizeof (struct ib_device));	return kzalloc(size, GFP_KERNEL);}EXPORT_SYMBOL(ib_alloc_device);/** * ib_dealloc_device - free an IB device struct * @device:structure to free * * Free a structure allocated with ib_alloc_device(). */void ib_dealloc_device(struct ib_device *device){	if (device->reg_state == IB_DEV_UNINITIALIZED) {		kfree(device);		return;	}	BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);	ib_device_unregister_sysfs(device);}EXPORT_SYMBOL(ib_dealloc_device);static int add_client_context(struct ib_device *device, struct ib_client *client){	struct ib_client_data *context;	unsigned long flags;	context = kmalloc(sizeof *context, GFP_KERNEL);	if (!context) {		printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",		       device->name, client->name);		return -ENOMEM;	}	context->client = client;	context->data   = NULL;	spin_lock_irqsave(&device->client_data_lock, flags);	list_add(&context->list, &device->client_data_list);	spin_unlock_irqrestore(&device->client_data_lock, flags);	return 0;}/** * ib_register_device - Register an IB device with IB core * @device:Device to register * * Low-level drivers use ib_register_device() to register their * devices with the IB core.  All registered clients will receive a * callback for each device that is added. @device must be allocated * with ib_alloc_device(). */int ib_register_device(struct ib_device *device){	int ret;	down(&device_sem);	if (strchr(device->name, '%')) {		ret = alloc_name(device->name);		if (ret)			goto out;	}	if (ib_device_check_mandatory(device)) {		ret = -EINVAL;		goto out;	}	INIT_LIST_HEAD(&device->event_handler_list);	INIT_LIST_HEAD(&device->client_data_list);	spin_lock_init(&device->event_handler_lock);	spin_lock_init(&device->client_data_lock);	ret = ib_device_register_sysfs(device);	if (ret) {		printk(KERN_WARNING "Couldn't register device %s with driver model\n",		       device->name);		goto out;	}	list_add_tail(&device->core_list, &device_list);	device->reg_state = IB_DEV_REGISTERED;	{		struct ib_client *client;		list_for_each_entry(client, &client_list, list)			if (client->add && !add_client_context(device, client))				client->add(device);	} out:	up(&device_sem);	return ret;}EXPORT_SYMBOL(ib_register_device);/** * ib_unregister_device - Unregister an IB device * @device:Device to unregister * * Unregister an IB device.  All clients will receive a remove callback. */void ib_unregister_device(struct ib_device *device){	struct ib_client *client;	struct ib_client_data *context, *tmp;	unsigned long flags;	down(&device_sem);	list_for_each_entry_reverse(client, &client_list, list)		if (client->remove)			client->remove(device);	list_del(&device->core_list);	up(&device_sem);	spin_lock_irqsave(&device->client_data_lock, flags);	list_for_each_entry_safe(context, tmp, &device->client_data_list, list)		kfree(context);	spin_unlock_irqrestore(&device->client_data_lock, flags);	device->reg_state = IB_DEV_UNREGISTERED;}EXPORT_SYMBOL(ib_unregister_device);/** * ib_register_client - Register an IB client * @client:Client to register * * Upper level users of the IB drivers can use ib_register_client() to * register callbacks for IB device addition and removal.  When an IB * device is added, each registered client's add method will be called * (in the order the clients were registered), and when a device is * removed, each client's remove method will be called (in the reverse * order that clients were registered).  In addition, when * ib_register_client() is called, the client will receive an add * callback for all devices already registered. */

⌨️ 快捷键说明

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