Path: blob/master/drivers/infiniband/core/device.c
37212 views
/*1* Copyright (c) 2004 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/module.h>34#include <linux/string.h>35#include <linux/errno.h>36#include <linux/kernel.h>37#include <linux/slab.h>38#include <linux/init.h>39#include <linux/mutex.h>40#include <rdma/rdma_netlink.h>4142#include "core_priv.h"4344MODULE_AUTHOR("Roland Dreier");45MODULE_DESCRIPTION("core kernel InfiniBand API");46MODULE_LICENSE("Dual BSD/GPL");4748struct ib_client_data {49struct list_head list;50struct ib_client *client;51void * data;52};5354struct workqueue_struct *ib_wq;55EXPORT_SYMBOL_GPL(ib_wq);5657static LIST_HEAD(device_list);58static LIST_HEAD(client_list);5960/*61* device_mutex protects access to both device_list and client_list.62* There's no real point to using multiple locks or something fancier63* like an rwsem: we always access both lists, and we're always64* modifying one list or the other list. In any case this is not a65* hot path so there's no point in trying to optimize.66*/67static DEFINE_MUTEX(device_mutex);6869static int ib_device_check_mandatory(struct ib_device *device)70{71#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }72static const struct {73size_t offset;74char *name;75} mandatory_table[] = {76IB_MANDATORY_FUNC(query_device),77IB_MANDATORY_FUNC(query_port),78IB_MANDATORY_FUNC(query_pkey),79IB_MANDATORY_FUNC(query_gid),80IB_MANDATORY_FUNC(alloc_pd),81IB_MANDATORY_FUNC(dealloc_pd),82IB_MANDATORY_FUNC(create_ah),83IB_MANDATORY_FUNC(destroy_ah),84IB_MANDATORY_FUNC(create_qp),85IB_MANDATORY_FUNC(modify_qp),86IB_MANDATORY_FUNC(destroy_qp),87IB_MANDATORY_FUNC(post_send),88IB_MANDATORY_FUNC(post_recv),89IB_MANDATORY_FUNC(create_cq),90IB_MANDATORY_FUNC(destroy_cq),91IB_MANDATORY_FUNC(poll_cq),92IB_MANDATORY_FUNC(req_notify_cq),93IB_MANDATORY_FUNC(get_dma_mr),94IB_MANDATORY_FUNC(dereg_mr)95};96int i;9798for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {99if (!*(void **) ((void *) device + mandatory_table[i].offset)) {100printk(KERN_WARNING "Device %s is missing mandatory function %s\n",101device->name, mandatory_table[i].name);102return -EINVAL;103}104}105106return 0;107}108109static struct ib_device *__ib_device_get_by_name(const char *name)110{111struct ib_device *device;112113list_for_each_entry(device, &device_list, core_list)114if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))115return device;116117return NULL;118}119120121static int alloc_name(char *name)122{123unsigned long *inuse;124char buf[IB_DEVICE_NAME_MAX];125struct ib_device *device;126int i;127128inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);129if (!inuse)130return -ENOMEM;131132list_for_each_entry(device, &device_list, core_list) {133if (!sscanf(device->name, name, &i))134continue;135if (i < 0 || i >= PAGE_SIZE * 8)136continue;137snprintf(buf, sizeof buf, name, i);138if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))139set_bit(i, inuse);140}141142i = find_first_zero_bit(inuse, PAGE_SIZE * 8);143free_page((unsigned long) inuse);144snprintf(buf, sizeof buf, name, i);145146if (__ib_device_get_by_name(buf))147return -ENFILE;148149strlcpy(name, buf, IB_DEVICE_NAME_MAX);150return 0;151}152153static int start_port(struct ib_device *device)154{155return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;156}157158159static int end_port(struct ib_device *device)160{161return (device->node_type == RDMA_NODE_IB_SWITCH) ?1620 : device->phys_port_cnt;163}164165/**166* ib_alloc_device - allocate an IB device struct167* @size:size of structure to allocate168*169* Low-level drivers should use ib_alloc_device() to allocate &struct170* ib_device. @size is the size of the structure to be allocated,171* including any private data used by the low-level driver.172* ib_dealloc_device() must be used to free structures allocated with173* ib_alloc_device().174*/175struct ib_device *ib_alloc_device(size_t size)176{177BUG_ON(size < sizeof (struct ib_device));178179return kzalloc(size, GFP_KERNEL);180}181EXPORT_SYMBOL(ib_alloc_device);182183/**184* ib_dealloc_device - free an IB device struct185* @device:structure to free186*187* Free a structure allocated with ib_alloc_device().188*/189void ib_dealloc_device(struct ib_device *device)190{191if (device->reg_state == IB_DEV_UNINITIALIZED) {192kfree(device);193return;194}195196BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);197198kobject_put(&device->dev.kobj);199}200EXPORT_SYMBOL(ib_dealloc_device);201202static int add_client_context(struct ib_device *device, struct ib_client *client)203{204struct ib_client_data *context;205unsigned long flags;206207context = kmalloc(sizeof *context, GFP_KERNEL);208if (!context) {209printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",210device->name, client->name);211return -ENOMEM;212}213214context->client = client;215context->data = NULL;216217spin_lock_irqsave(&device->client_data_lock, flags);218list_add(&context->list, &device->client_data_list);219spin_unlock_irqrestore(&device->client_data_lock, flags);220221return 0;222}223224static int read_port_table_lengths(struct ib_device *device)225{226struct ib_port_attr *tprops = NULL;227int num_ports, ret = -ENOMEM;228u8 port_index;229230tprops = kmalloc(sizeof *tprops, GFP_KERNEL);231if (!tprops)232goto out;233234num_ports = end_port(device) - start_port(device) + 1;235236device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,237GFP_KERNEL);238device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,239GFP_KERNEL);240if (!device->pkey_tbl_len || !device->gid_tbl_len)241goto err;242243for (port_index = 0; port_index < num_ports; ++port_index) {244ret = ib_query_port(device, port_index + start_port(device),245tprops);246if (ret)247goto err;248device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;249device->gid_tbl_len[port_index] = tprops->gid_tbl_len;250}251252ret = 0;253goto out;254255err:256kfree(device->gid_tbl_len);257kfree(device->pkey_tbl_len);258out:259kfree(tprops);260return ret;261}262263/**264* ib_register_device - Register an IB device with IB core265* @device:Device to register266*267* Low-level drivers use ib_register_device() to register their268* devices with the IB core. All registered clients will receive a269* callback for each device that is added. @device must be allocated270* with ib_alloc_device().271*/272int ib_register_device(struct ib_device *device,273int (*port_callback)(struct ib_device *,274u8, struct kobject *))275{276int ret;277278mutex_lock(&device_mutex);279280if (strchr(device->name, '%')) {281ret = alloc_name(device->name);282if (ret)283goto out;284}285286if (ib_device_check_mandatory(device)) {287ret = -EINVAL;288goto out;289}290291INIT_LIST_HEAD(&device->event_handler_list);292INIT_LIST_HEAD(&device->client_data_list);293spin_lock_init(&device->event_handler_lock);294spin_lock_init(&device->client_data_lock);295296ret = read_port_table_lengths(device);297if (ret) {298printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",299device->name);300goto out;301}302303ret = ib_device_register_sysfs(device, port_callback);304if (ret) {305printk(KERN_WARNING "Couldn't register device %s with driver model\n",306device->name);307kfree(device->gid_tbl_len);308kfree(device->pkey_tbl_len);309goto out;310}311312list_add_tail(&device->core_list, &device_list);313314device->reg_state = IB_DEV_REGISTERED;315316{317struct ib_client *client;318319list_for_each_entry(client, &client_list, list)320if (client->add && !add_client_context(device, client))321client->add(device);322}323324out:325mutex_unlock(&device_mutex);326return ret;327}328EXPORT_SYMBOL(ib_register_device);329330/**331* ib_unregister_device - Unregister an IB device332* @device:Device to unregister333*334* Unregister an IB device. All clients will receive a remove callback.335*/336void ib_unregister_device(struct ib_device *device)337{338struct ib_client *client;339struct ib_client_data *context, *tmp;340unsigned long flags;341342mutex_lock(&device_mutex);343344list_for_each_entry_reverse(client, &client_list, list)345if (client->remove)346client->remove(device);347348list_del(&device->core_list);349350kfree(device->gid_tbl_len);351kfree(device->pkey_tbl_len);352353mutex_unlock(&device_mutex);354355ib_device_unregister_sysfs(device);356357spin_lock_irqsave(&device->client_data_lock, flags);358list_for_each_entry_safe(context, tmp, &device->client_data_list, list)359kfree(context);360spin_unlock_irqrestore(&device->client_data_lock, flags);361362device->reg_state = IB_DEV_UNREGISTERED;363}364EXPORT_SYMBOL(ib_unregister_device);365366/**367* ib_register_client - Register an IB client368* @client:Client to register369*370* Upper level users of the IB drivers can use ib_register_client() to371* register callbacks for IB device addition and removal. When an IB372* device is added, each registered client's add method will be called373* (in the order the clients were registered), and when a device is374* removed, each client's remove method will be called (in the reverse375* order that clients were registered). In addition, when376* ib_register_client() is called, the client will receive an add377* callback for all devices already registered.378*/379int ib_register_client(struct ib_client *client)380{381struct ib_device *device;382383mutex_lock(&device_mutex);384385list_add_tail(&client->list, &client_list);386list_for_each_entry(device, &device_list, core_list)387if (client->add && !add_client_context(device, client))388client->add(device);389390mutex_unlock(&device_mutex);391392return 0;393}394EXPORT_SYMBOL(ib_register_client);395396/**397* ib_unregister_client - Unregister an IB client398* @client:Client to unregister399*400* Upper level users use ib_unregister_client() to remove their client401* registration. When ib_unregister_client() is called, the client402* will receive a remove callback for each IB device still registered.403*/404void ib_unregister_client(struct ib_client *client)405{406struct ib_client_data *context, *tmp;407struct ib_device *device;408unsigned long flags;409410mutex_lock(&device_mutex);411412list_for_each_entry(device, &device_list, core_list) {413if (client->remove)414client->remove(device);415416spin_lock_irqsave(&device->client_data_lock, flags);417list_for_each_entry_safe(context, tmp, &device->client_data_list, list)418if (context->client == client) {419list_del(&context->list);420kfree(context);421}422spin_unlock_irqrestore(&device->client_data_lock, flags);423}424list_del(&client->list);425426mutex_unlock(&device_mutex);427}428EXPORT_SYMBOL(ib_unregister_client);429430/**431* ib_get_client_data - Get IB client context432* @device:Device to get context for433* @client:Client to get context for434*435* ib_get_client_data() returns client context set with436* ib_set_client_data().437*/438void *ib_get_client_data(struct ib_device *device, struct ib_client *client)439{440struct ib_client_data *context;441void *ret = NULL;442unsigned long flags;443444spin_lock_irqsave(&device->client_data_lock, flags);445list_for_each_entry(context, &device->client_data_list, list)446if (context->client == client) {447ret = context->data;448break;449}450spin_unlock_irqrestore(&device->client_data_lock, flags);451452return ret;453}454EXPORT_SYMBOL(ib_get_client_data);455456/**457* ib_set_client_data - Set IB client context458* @device:Device to set context for459* @client:Client to set context for460* @data:Context to set461*462* ib_set_client_data() sets client context that can be retrieved with463* ib_get_client_data().464*/465void ib_set_client_data(struct ib_device *device, struct ib_client *client,466void *data)467{468struct ib_client_data *context;469unsigned long flags;470471spin_lock_irqsave(&device->client_data_lock, flags);472list_for_each_entry(context, &device->client_data_list, list)473if (context->client == client) {474context->data = data;475goto out;476}477478printk(KERN_WARNING "No client context found for %s/%s\n",479device->name, client->name);480481out:482spin_unlock_irqrestore(&device->client_data_lock, flags);483}484EXPORT_SYMBOL(ib_set_client_data);485486/**487* ib_register_event_handler - Register an IB event handler488* @event_handler:Handler to register489*490* ib_register_event_handler() registers an event handler that will be491* called back when asynchronous IB events occur (as defined in492* chapter 11 of the InfiniBand Architecture Specification). This493* callback may occur in interrupt context.494*/495int ib_register_event_handler (struct ib_event_handler *event_handler)496{497unsigned long flags;498499spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);500list_add_tail(&event_handler->list,501&event_handler->device->event_handler_list);502spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);503504return 0;505}506EXPORT_SYMBOL(ib_register_event_handler);507508/**509* ib_unregister_event_handler - Unregister an event handler510* @event_handler:Handler to unregister511*512* Unregister an event handler registered with513* ib_register_event_handler().514*/515int ib_unregister_event_handler(struct ib_event_handler *event_handler)516{517unsigned long flags;518519spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);520list_del(&event_handler->list);521spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);522523return 0;524}525EXPORT_SYMBOL(ib_unregister_event_handler);526527/**528* ib_dispatch_event - Dispatch an asynchronous event529* @event:Event to dispatch530*531* Low-level drivers must call ib_dispatch_event() to dispatch the532* event to all registered event handlers when an asynchronous event533* occurs.534*/535void ib_dispatch_event(struct ib_event *event)536{537unsigned long flags;538struct ib_event_handler *handler;539540spin_lock_irqsave(&event->device->event_handler_lock, flags);541542list_for_each_entry(handler, &event->device->event_handler_list, list)543handler->handler(handler, event);544545spin_unlock_irqrestore(&event->device->event_handler_lock, flags);546}547EXPORT_SYMBOL(ib_dispatch_event);548549/**550* ib_query_device - Query IB device attributes551* @device:Device to query552* @device_attr:Device attributes553*554* ib_query_device() returns the attributes of a device through the555* @device_attr pointer.556*/557int ib_query_device(struct ib_device *device,558struct ib_device_attr *device_attr)559{560return device->query_device(device, device_attr);561}562EXPORT_SYMBOL(ib_query_device);563564/**565* ib_query_port - Query IB port attributes566* @device:Device to query567* @port_num:Port number to query568* @port_attr:Port attributes569*570* ib_query_port() returns the attributes of a port through the571* @port_attr pointer.572*/573int ib_query_port(struct ib_device *device,574u8 port_num,575struct ib_port_attr *port_attr)576{577if (port_num < start_port(device) || port_num > end_port(device))578return -EINVAL;579580return device->query_port(device, port_num, port_attr);581}582EXPORT_SYMBOL(ib_query_port);583584/**585* ib_query_gid - Get GID table entry586* @device:Device to query587* @port_num:Port number to query588* @index:GID table index to query589* @gid:Returned GID590*591* ib_query_gid() fetches the specified GID table entry.592*/593int ib_query_gid(struct ib_device *device,594u8 port_num, int index, union ib_gid *gid)595{596return device->query_gid(device, port_num, index, gid);597}598EXPORT_SYMBOL(ib_query_gid);599600/**601* ib_query_pkey - Get P_Key table entry602* @device:Device to query603* @port_num:Port number to query604* @index:P_Key table index to query605* @pkey:Returned P_Key606*607* ib_query_pkey() fetches the specified P_Key table entry.608*/609int ib_query_pkey(struct ib_device *device,610u8 port_num, u16 index, u16 *pkey)611{612return device->query_pkey(device, port_num, index, pkey);613}614EXPORT_SYMBOL(ib_query_pkey);615616/**617* ib_modify_device - Change IB device attributes618* @device:Device to modify619* @device_modify_mask:Mask of attributes to change620* @device_modify:New attribute values621*622* ib_modify_device() changes a device's attributes as specified by623* the @device_modify_mask and @device_modify structure.624*/625int ib_modify_device(struct ib_device *device,626int device_modify_mask,627struct ib_device_modify *device_modify)628{629return device->modify_device(device, device_modify_mask,630device_modify);631}632EXPORT_SYMBOL(ib_modify_device);633634/**635* ib_modify_port - Modifies the attributes for the specified port.636* @device: The device to modify.637* @port_num: The number of the port to modify.638* @port_modify_mask: Mask used to specify which attributes of the port639* to change.640* @port_modify: New attribute values for the port.641*642* ib_modify_port() changes a port's attributes as specified by the643* @port_modify_mask and @port_modify structure.644*/645int ib_modify_port(struct ib_device *device,646u8 port_num, int port_modify_mask,647struct ib_port_modify *port_modify)648{649if (port_num < start_port(device) || port_num > end_port(device))650return -EINVAL;651652return device->modify_port(device, port_num, port_modify_mask,653port_modify);654}655EXPORT_SYMBOL(ib_modify_port);656657/**658* ib_find_gid - Returns the port number and GID table index where659* a specified GID value occurs.660* @device: The device to query.661* @gid: The GID value to search for.662* @port_num: The port number of the device where the GID value was found.663* @index: The index into the GID table where the GID was found. This664* parameter may be NULL.665*/666int ib_find_gid(struct ib_device *device, union ib_gid *gid,667u8 *port_num, u16 *index)668{669union ib_gid tmp_gid;670int ret, port, i;671672for (port = start_port(device); port <= end_port(device); ++port) {673for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {674ret = ib_query_gid(device, port, i, &tmp_gid);675if (ret)676return ret;677if (!memcmp(&tmp_gid, gid, sizeof *gid)) {678*port_num = port;679if (index)680*index = i;681return 0;682}683}684}685686return -ENOENT;687}688EXPORT_SYMBOL(ib_find_gid);689690/**691* ib_find_pkey - Returns the PKey table index where a specified692* PKey value occurs.693* @device: The device to query.694* @port_num: The port number of the device to search for the PKey.695* @pkey: The PKey value to search for.696* @index: The index into the PKey table where the PKey was found.697*/698int ib_find_pkey(struct ib_device *device,699u8 port_num, u16 pkey, u16 *index)700{701int ret, i;702u16 tmp_pkey;703704for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {705ret = ib_query_pkey(device, port_num, i, &tmp_pkey);706if (ret)707return ret;708709if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {710*index = i;711return 0;712}713}714715return -ENOENT;716}717EXPORT_SYMBOL(ib_find_pkey);718719static int __init ib_core_init(void)720{721int ret;722723ib_wq = alloc_workqueue("infiniband", 0, 0);724if (!ib_wq)725return -ENOMEM;726727ret = ib_sysfs_setup();728if (ret) {729printk(KERN_WARNING "Couldn't create InfiniBand device class\n");730goto err;731}732733ret = ibnl_init();734if (ret) {735printk(KERN_WARNING "Couldn't init IB netlink interface\n");736goto err_sysfs;737}738739ret = ib_cache_setup();740if (ret) {741printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");742goto err_nl;743}744745return 0;746747err_nl:748ibnl_cleanup();749750err_sysfs:751ib_sysfs_cleanup();752753err:754destroy_workqueue(ib_wq);755return ret;756}757758static void __exit ib_core_cleanup(void)759{760ib_cache_cleanup();761ibnl_cleanup();762ib_sysfs_cleanup();763/* Make sure that any pending umem accounting work is done. */764destroy_workqueue(ib_wq);765}766767module_init(ib_core_init);768module_exit(ib_core_cleanup);769770771