device.c revision 219820
1219820Sjeff/* 2219820Sjeff * Copyright (c) 2004 Topspin Communications. All rights reserved. 3219820Sjeff * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 4219820Sjeff * 5219820Sjeff * This software is available to you under a choice of one of two 6219820Sjeff * licenses. You may choose to be licensed under the terms of the GNU 7219820Sjeff * General Public License (GPL) Version 2, available from the file 8219820Sjeff * COPYING in the main directory of this source tree, or the 9219820Sjeff * OpenIB.org BSD license below: 10219820Sjeff * 11219820Sjeff * Redistribution and use in source and binary forms, with or 12219820Sjeff * without modification, are permitted provided that the following 13219820Sjeff * conditions are met: 14219820Sjeff * 15219820Sjeff * - Redistributions of source code must retain the above 16219820Sjeff * copyright notice, this list of conditions and the following 17219820Sjeff * disclaimer. 18219820Sjeff * 19219820Sjeff * - Redistributions in binary form must reproduce the above 20219820Sjeff * copyright notice, this list of conditions and the following 21219820Sjeff * disclaimer in the documentation and/or other materials 22219820Sjeff * provided with the distribution. 23219820Sjeff * 24219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31219820Sjeff * SOFTWARE. 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#include <linux/module.h> 35219820Sjeff#include <linux/string.h> 36219820Sjeff#include <linux/errno.h> 37219820Sjeff#include <linux/kernel.h> 38219820Sjeff#include <linux/slab.h> 39219820Sjeff#include <linux/init.h> 40219820Sjeff#include <linux/mutex.h> 41219820Sjeff#include <linux/workqueue.h> 42219820Sjeff 43219820Sjeff#include "core_priv.h" 44219820Sjeff 45219820SjeffMODULE_AUTHOR("Roland Dreier"); 46219820SjeffMODULE_DESCRIPTION("core kernel InfiniBand API"); 47219820SjeffMODULE_LICENSE("Dual BSD/GPL"); 48219820Sjeff 49219820Sjeff#ifdef __ia64__ 50219820Sjeff/* workaround for a bug in hp chipset that would cause kernel 51219820Sjeff panic when dma resources are exhaused */ 52219820Sjeffint dma_map_sg_hp_wa = 0; 53219820Sjeff#endif 54219820Sjeff 55219820Sjeffstruct ib_client_data { 56219820Sjeff struct list_head list; 57219820Sjeff struct ib_client *client; 58219820Sjeff void * data; 59219820Sjeff}; 60219820Sjeff 61219820Sjeffstatic LIST_HEAD(device_list); 62219820Sjeffstatic LIST_HEAD(client_list); 63219820Sjeff 64219820Sjeff/* 65219820Sjeff * device_mutex protects access to both device_list and client_list. 66219820Sjeff * There's no real point to using multiple locks or something fancier 67219820Sjeff * like an rwsem: we always access both lists, and we're always 68219820Sjeff * modifying one list or the other list. In any case this is not a 69219820Sjeff * hot path so there's no point in trying to optimize. 70219820Sjeff */ 71219820Sjeffstatic DEFINE_MUTEX(device_mutex); 72219820Sjeff 73219820Sjeffstatic int ib_device_check_mandatory(struct ib_device *device) 74219820Sjeff{ 75219820Sjeff#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x } 76219820Sjeff static const struct { 77219820Sjeff size_t offset; 78219820Sjeff char *name; 79219820Sjeff } mandatory_table[] = { 80219820Sjeff IB_MANDATORY_FUNC(query_device), 81219820Sjeff IB_MANDATORY_FUNC(query_port), 82219820Sjeff IB_MANDATORY_FUNC(query_pkey), 83219820Sjeff IB_MANDATORY_FUNC(query_gid), 84219820Sjeff IB_MANDATORY_FUNC(alloc_pd), 85219820Sjeff IB_MANDATORY_FUNC(dealloc_pd), 86219820Sjeff IB_MANDATORY_FUNC(create_ah), 87219820Sjeff IB_MANDATORY_FUNC(destroy_ah), 88219820Sjeff IB_MANDATORY_FUNC(create_qp), 89219820Sjeff IB_MANDATORY_FUNC(modify_qp), 90219820Sjeff IB_MANDATORY_FUNC(destroy_qp), 91219820Sjeff IB_MANDATORY_FUNC(post_send), 92219820Sjeff IB_MANDATORY_FUNC(post_recv), 93219820Sjeff IB_MANDATORY_FUNC(create_cq), 94219820Sjeff IB_MANDATORY_FUNC(destroy_cq), 95219820Sjeff IB_MANDATORY_FUNC(poll_cq), 96219820Sjeff IB_MANDATORY_FUNC(req_notify_cq), 97219820Sjeff IB_MANDATORY_FUNC(get_dma_mr), 98219820Sjeff IB_MANDATORY_FUNC(dereg_mr) 99219820Sjeff }; 100219820Sjeff int i; 101219820Sjeff 102219820Sjeff for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) { 103219820Sjeff if (!*(void **) ((u_char *) device + mandatory_table[i].offset)) { 104219820Sjeff printk(KERN_WARNING "Device %s is missing mandatory function %s\n", 105219820Sjeff device->name, mandatory_table[i].name); 106219820Sjeff return -EINVAL; 107219820Sjeff } 108219820Sjeff } 109219820Sjeff 110219820Sjeff return 0; 111219820Sjeff} 112219820Sjeff 113219820Sjeffstatic struct ib_device *__ib_device_get_by_name(const char *name) 114219820Sjeff{ 115219820Sjeff struct ib_device *device; 116219820Sjeff 117219820Sjeff list_for_each_entry(device, &device_list, core_list) 118219820Sjeff if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX)) 119219820Sjeff return device; 120219820Sjeff 121219820Sjeff return NULL; 122219820Sjeff} 123219820Sjeff 124219820Sjeff 125219820Sjeffstatic int alloc_name(char *name) 126219820Sjeff{ 127219820Sjeff unsigned long *inuse; 128219820Sjeff char buf[IB_DEVICE_NAME_MAX]; 129219820Sjeff struct ib_device *device; 130219820Sjeff int i; 131219820Sjeff 132219820Sjeff inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL); 133219820Sjeff if (!inuse) 134219820Sjeff return -ENOMEM; 135219820Sjeff 136219820Sjeff list_for_each_entry(device, &device_list, core_list) { 137219820Sjeff if (!sscanf(device->name, name, &i)) 138219820Sjeff continue; 139219820Sjeff if (i < 0 || i >= PAGE_SIZE * 8) 140219820Sjeff continue; 141219820Sjeff snprintf(buf, sizeof buf, name, i); 142219820Sjeff if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX)) 143219820Sjeff set_bit(i, inuse); 144219820Sjeff } 145219820Sjeff 146219820Sjeff i = find_first_zero_bit(inuse, PAGE_SIZE * 8); 147219820Sjeff free_page((unsigned long) inuse); 148219820Sjeff snprintf(buf, sizeof buf, name, i); 149219820Sjeff 150219820Sjeff if (__ib_device_get_by_name(buf)) 151219820Sjeff return -ENFILE; 152219820Sjeff 153219820Sjeff strlcpy(name, buf, IB_DEVICE_NAME_MAX); 154219820Sjeff return 0; 155219820Sjeff} 156219820Sjeff 157219820Sjeffstatic int start_port(struct ib_device *device) 158219820Sjeff{ 159219820Sjeff return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1; 160219820Sjeff} 161219820Sjeff 162219820Sjeff 163219820Sjeffstatic int end_port(struct ib_device *device) 164219820Sjeff{ 165219820Sjeff return (device->node_type == RDMA_NODE_IB_SWITCH) ? 166219820Sjeff 0 : device->phys_port_cnt; 167219820Sjeff} 168219820Sjeff 169219820Sjeff/** 170219820Sjeff * ib_alloc_device - allocate an IB device struct 171219820Sjeff * @size:size of structure to allocate 172219820Sjeff * 173219820Sjeff * Low-level drivers should use ib_alloc_device() to allocate &struct 174219820Sjeff * ib_device. @size is the size of the structure to be allocated, 175219820Sjeff * including any private data used by the low-level driver. 176219820Sjeff * ib_dealloc_device() must be used to free structures allocated with 177219820Sjeff * ib_alloc_device(). 178219820Sjeff */ 179219820Sjeffstruct ib_device *ib_alloc_device(size_t size) 180219820Sjeff{ 181219820Sjeff BUG_ON(size < sizeof (struct ib_device)); 182219820Sjeff 183219820Sjeff return kzalloc(size, GFP_KERNEL); 184219820Sjeff} 185219820SjeffEXPORT_SYMBOL(ib_alloc_device); 186219820Sjeff 187219820Sjeff/** 188219820Sjeff * ib_dealloc_device - free an IB device struct 189219820Sjeff * @device:structure to free 190219820Sjeff * 191219820Sjeff * Free a structure allocated with ib_alloc_device(). 192219820Sjeff */ 193219820Sjeffvoid ib_dealloc_device(struct ib_device *device) 194219820Sjeff{ 195219820Sjeff if (device->reg_state == IB_DEV_UNINITIALIZED) { 196219820Sjeff kfree(device); 197219820Sjeff return; 198219820Sjeff } 199219820Sjeff 200219820Sjeff BUG_ON(device->reg_state != IB_DEV_UNREGISTERED); 201219820Sjeff 202219820Sjeff kobject_put(&device->dev.kobj); 203219820Sjeff} 204219820SjeffEXPORT_SYMBOL(ib_dealloc_device); 205219820Sjeff 206219820Sjeffstatic int add_client_context(struct ib_device *device, struct ib_client *client) 207219820Sjeff{ 208219820Sjeff struct ib_client_data *context; 209219820Sjeff unsigned long flags; 210219820Sjeff 211219820Sjeff context = kmalloc(sizeof *context, GFP_KERNEL); 212219820Sjeff if (!context) { 213219820Sjeff printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n", 214219820Sjeff device->name, client->name); 215219820Sjeff return -ENOMEM; 216219820Sjeff } 217219820Sjeff 218219820Sjeff context->client = client; 219219820Sjeff context->data = NULL; 220219820Sjeff 221219820Sjeff spin_lock_irqsave(&device->client_data_lock, flags); 222219820Sjeff list_add(&context->list, &device->client_data_list); 223219820Sjeff spin_unlock_irqrestore(&device->client_data_lock, flags); 224219820Sjeff 225219820Sjeff return 0; 226219820Sjeff} 227219820Sjeff 228219820Sjeffstatic int read_port_table_lengths(struct ib_device *device) 229219820Sjeff{ 230219820Sjeff struct ib_port_attr *tprops = NULL; 231219820Sjeff int num_ports, ret = -ENOMEM; 232219820Sjeff u8 port_index; 233219820Sjeff 234219820Sjeff tprops = kmalloc(sizeof *tprops, GFP_KERNEL); 235219820Sjeff if (!tprops) 236219820Sjeff goto out; 237219820Sjeff 238219820Sjeff num_ports = end_port(device) - start_port(device) + 1; 239219820Sjeff 240219820Sjeff device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports, 241219820Sjeff GFP_KERNEL); 242219820Sjeff device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports, 243219820Sjeff GFP_KERNEL); 244219820Sjeff if (!device->pkey_tbl_len || !device->gid_tbl_len) 245219820Sjeff goto err; 246219820Sjeff 247219820Sjeff for (port_index = 0; port_index < num_ports; ++port_index) { 248219820Sjeff ret = ib_query_port(device, port_index + start_port(device), 249219820Sjeff tprops); 250219820Sjeff if (ret) 251219820Sjeff goto err; 252219820Sjeff device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len; 253219820Sjeff device->gid_tbl_len[port_index] = tprops->gid_tbl_len; 254219820Sjeff } 255219820Sjeff 256219820Sjeff ret = 0; 257219820Sjeff goto out; 258219820Sjeff 259219820Sjefferr: 260219820Sjeff kfree(device->gid_tbl_len); 261219820Sjeff kfree(device->pkey_tbl_len); 262219820Sjeffout: 263219820Sjeff kfree(tprops); 264219820Sjeff return ret; 265219820Sjeff} 266219820Sjeff 267219820Sjeff/** 268219820Sjeff * ib_register_device - Register an IB device with IB core 269219820Sjeff * @device:Device to register 270219820Sjeff * 271219820Sjeff * Low-level drivers use ib_register_device() to register their 272219820Sjeff * devices with the IB core. All registered clients will receive a 273219820Sjeff * callback for each device that is added. @device must be allocated 274219820Sjeff * with ib_alloc_device(). 275219820Sjeff */ 276219820Sjeffint ib_register_device(struct ib_device *device) 277219820Sjeff{ 278219820Sjeff int ret; 279219820Sjeff 280219820Sjeff mutex_lock(&device_mutex); 281219820Sjeff 282219820Sjeff if (strchr(device->name, '%')) { 283219820Sjeff ret = alloc_name(device->name); 284219820Sjeff if (ret) 285219820Sjeff goto out; 286219820Sjeff } 287219820Sjeff 288219820Sjeff if (ib_device_check_mandatory(device)) { 289219820Sjeff ret = -EINVAL; 290219820Sjeff goto out; 291219820Sjeff } 292219820Sjeff 293219820Sjeff INIT_LIST_HEAD(&device->event_handler_list); 294219820Sjeff INIT_LIST_HEAD(&device->client_data_list); 295219820Sjeff spin_lock_init(&device->event_handler_lock); 296219820Sjeff spin_lock_init(&device->client_data_lock); 297219820Sjeff device->ib_uverbs_xrcd_table = RB_ROOT; 298219820Sjeff mutex_init(&device->xrcd_table_mutex); 299219820Sjeff 300219820Sjeff ret = read_port_table_lengths(device); 301219820Sjeff if (ret) { 302219820Sjeff printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n", 303219820Sjeff device->name); 304219820Sjeff goto out; 305219820Sjeff } 306219820Sjeff 307219820Sjeff ret = ib_device_register_sysfs(device); 308219820Sjeff if (ret) { 309219820Sjeff printk(KERN_WARNING "Couldn't register device %s with driver model\n", 310219820Sjeff device->name); 311219820Sjeff kfree(device->gid_tbl_len); 312219820Sjeff kfree(device->pkey_tbl_len); 313219820Sjeff goto out; 314219820Sjeff } 315219820Sjeff 316219820Sjeff list_add_tail(&device->core_list, &device_list); 317219820Sjeff 318219820Sjeff device->reg_state = IB_DEV_REGISTERED; 319219820Sjeff 320219820Sjeff { 321219820Sjeff struct ib_client *client; 322219820Sjeff 323219820Sjeff list_for_each_entry(client, &client_list, list) 324219820Sjeff if (client->add && !add_client_context(device, client)) 325219820Sjeff client->add(device); 326219820Sjeff } 327219820Sjeff 328219820Sjeff out: 329219820Sjeff mutex_unlock(&device_mutex); 330219820Sjeff return ret; 331219820Sjeff} 332219820SjeffEXPORT_SYMBOL(ib_register_device); 333219820Sjeff 334219820Sjeff/** 335219820Sjeff * ib_unregister_device - Unregister an IB device 336219820Sjeff * @device:Device to unregister 337219820Sjeff * 338219820Sjeff * Unregister an IB device. All clients will receive a remove callback. 339219820Sjeff */ 340219820Sjeffvoid ib_unregister_device(struct ib_device *device) 341219820Sjeff{ 342219820Sjeff struct ib_client *client; 343219820Sjeff struct ib_client_data *context, *tmp; 344219820Sjeff unsigned long flags; 345219820Sjeff 346219820Sjeff mutex_lock(&device_mutex); 347219820Sjeff 348219820Sjeff list_for_each_entry_reverse(client, &client_list, list) 349219820Sjeff if (client->remove) 350219820Sjeff client->remove(device); 351219820Sjeff 352219820Sjeff list_del(&device->core_list); 353219820Sjeff 354219820Sjeff kfree(device->gid_tbl_len); 355219820Sjeff kfree(device->pkey_tbl_len); 356219820Sjeff 357219820Sjeff mutex_unlock(&device_mutex); 358219820Sjeff 359219820Sjeff ib_device_unregister_sysfs(device); 360219820Sjeff 361219820Sjeff spin_lock_irqsave(&device->client_data_lock, flags); 362219820Sjeff list_for_each_entry_safe(context, tmp, &device->client_data_list, list) 363219820Sjeff kfree(context); 364219820Sjeff spin_unlock_irqrestore(&device->client_data_lock, flags); 365219820Sjeff 366219820Sjeff device->reg_state = IB_DEV_UNREGISTERED; 367219820Sjeff} 368219820SjeffEXPORT_SYMBOL(ib_unregister_device); 369219820Sjeff 370219820Sjeff/** 371219820Sjeff * ib_register_client - Register an IB client 372219820Sjeff * @client:Client to register 373219820Sjeff * 374219820Sjeff * Upper level users of the IB drivers can use ib_register_client() to 375219820Sjeff * register callbacks for IB device addition and removal. When an IB 376219820Sjeff * device is added, each registered client's add method will be called 377219820Sjeff * (in the order the clients were registered), and when a device is 378219820Sjeff * removed, each client's remove method will be called (in the reverse 379219820Sjeff * order that clients were registered). In addition, when 380219820Sjeff * ib_register_client() is called, the client will receive an add 381219820Sjeff * callback for all devices already registered. 382219820Sjeff */ 383219820Sjeffint ib_register_client(struct ib_client *client) 384219820Sjeff{ 385219820Sjeff struct ib_device *device; 386219820Sjeff 387219820Sjeff mutex_lock(&device_mutex); 388219820Sjeff 389219820Sjeff list_add_tail(&client->list, &client_list); 390219820Sjeff list_for_each_entry(device, &device_list, core_list) 391219820Sjeff if (client->add && !add_client_context(device, client)) 392219820Sjeff client->add(device); 393219820Sjeff 394219820Sjeff mutex_unlock(&device_mutex); 395219820Sjeff 396219820Sjeff return 0; 397219820Sjeff} 398219820SjeffEXPORT_SYMBOL(ib_register_client); 399219820Sjeff 400219820Sjeff/** 401219820Sjeff * ib_unregister_client - Unregister an IB client 402219820Sjeff * @client:Client to unregister 403219820Sjeff * 404219820Sjeff * Upper level users use ib_unregister_client() to remove their client 405219820Sjeff * registration. When ib_unregister_client() is called, the client 406219820Sjeff * will receive a remove callback for each IB device still registered. 407219820Sjeff */ 408219820Sjeffvoid ib_unregister_client(struct ib_client *client) 409219820Sjeff{ 410219820Sjeff struct ib_client_data *context, *tmp; 411219820Sjeff struct ib_device *device; 412219820Sjeff unsigned long flags; 413219820Sjeff 414219820Sjeff mutex_lock(&device_mutex); 415219820Sjeff 416219820Sjeff list_for_each_entry(device, &device_list, core_list) { 417219820Sjeff if (client->remove) 418219820Sjeff client->remove(device); 419219820Sjeff 420219820Sjeff spin_lock_irqsave(&device->client_data_lock, flags); 421219820Sjeff list_for_each_entry_safe(context, tmp, &device->client_data_list, list) 422219820Sjeff if (context->client == client) { 423219820Sjeff list_del(&context->list); 424219820Sjeff kfree(context); 425219820Sjeff } 426219820Sjeff spin_unlock_irqrestore(&device->client_data_lock, flags); 427219820Sjeff } 428219820Sjeff list_del(&client->list); 429219820Sjeff 430219820Sjeff mutex_unlock(&device_mutex); 431219820Sjeff} 432219820SjeffEXPORT_SYMBOL(ib_unregister_client); 433219820Sjeff 434219820Sjeff/** 435219820Sjeff * ib_get_client_data - Get IB client context 436219820Sjeff * @device:Device to get context for 437219820Sjeff * @client:Client to get context for 438219820Sjeff * 439219820Sjeff * ib_get_client_data() returns client context set with 440219820Sjeff * ib_set_client_data(). 441219820Sjeff */ 442219820Sjeffvoid *ib_get_client_data(struct ib_device *device, struct ib_client *client) 443219820Sjeff{ 444219820Sjeff struct ib_client_data *context; 445219820Sjeff void *ret = NULL; 446219820Sjeff unsigned long flags; 447219820Sjeff 448219820Sjeff spin_lock_irqsave(&device->client_data_lock, flags); 449219820Sjeff list_for_each_entry(context, &device->client_data_list, list) 450219820Sjeff if (context->client == client) { 451219820Sjeff ret = context->data; 452219820Sjeff break; 453219820Sjeff } 454219820Sjeff spin_unlock_irqrestore(&device->client_data_lock, flags); 455219820Sjeff 456219820Sjeff return ret; 457219820Sjeff} 458219820SjeffEXPORT_SYMBOL(ib_get_client_data); 459219820Sjeff 460219820Sjeff/** 461219820Sjeff * ib_set_client_data - Set IB client context 462219820Sjeff * @device:Device to set context for 463219820Sjeff * @client:Client to set context for 464219820Sjeff * @data:Context to set 465219820Sjeff * 466219820Sjeff * ib_set_client_data() sets client context that can be retrieved with 467219820Sjeff * ib_get_client_data(). 468219820Sjeff */ 469219820Sjeffvoid ib_set_client_data(struct ib_device *device, struct ib_client *client, 470219820Sjeff void *data) 471219820Sjeff{ 472219820Sjeff struct ib_client_data *context; 473219820Sjeff unsigned long flags; 474219820Sjeff 475219820Sjeff spin_lock_irqsave(&device->client_data_lock, flags); 476219820Sjeff list_for_each_entry(context, &device->client_data_list, list) 477219820Sjeff if (context->client == client) { 478219820Sjeff context->data = data; 479219820Sjeff goto out; 480219820Sjeff } 481219820Sjeff 482219820Sjeff printk(KERN_WARNING "No client context found for %s/%s\n", 483219820Sjeff device->name, client->name); 484219820Sjeff 485219820Sjeffout: 486219820Sjeff spin_unlock_irqrestore(&device->client_data_lock, flags); 487219820Sjeff} 488219820SjeffEXPORT_SYMBOL(ib_set_client_data); 489219820Sjeff 490219820Sjeff/** 491219820Sjeff * ib_register_event_handler - Register an IB event handler 492219820Sjeff * @event_handler:Handler to register 493219820Sjeff * 494219820Sjeff * ib_register_event_handler() registers an event handler that will be 495219820Sjeff * called back when asynchronous IB events occur (as defined in 496219820Sjeff * chapter 11 of the InfiniBand Architecture Specification). This 497219820Sjeff * callback may occur in interrupt context. 498219820Sjeff */ 499219820Sjeffint ib_register_event_handler (struct ib_event_handler *event_handler) 500219820Sjeff{ 501219820Sjeff unsigned long flags; 502219820Sjeff 503219820Sjeff spin_lock_irqsave(&event_handler->device->event_handler_lock, flags); 504219820Sjeff list_add_tail(&event_handler->list, 505219820Sjeff &event_handler->device->event_handler_list); 506219820Sjeff spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags); 507219820Sjeff 508219820Sjeff return 0; 509219820Sjeff} 510219820SjeffEXPORT_SYMBOL(ib_register_event_handler); 511219820Sjeff 512219820Sjeff/** 513219820Sjeff * ib_unregister_event_handler - Unregister an event handler 514219820Sjeff * @event_handler:Handler to unregister 515219820Sjeff * 516219820Sjeff * Unregister an event handler registered with 517219820Sjeff * ib_register_event_handler(). 518219820Sjeff */ 519219820Sjeffint ib_unregister_event_handler(struct ib_event_handler *event_handler) 520219820Sjeff{ 521219820Sjeff unsigned long flags; 522219820Sjeff 523219820Sjeff spin_lock_irqsave(&event_handler->device->event_handler_lock, flags); 524219820Sjeff list_del(&event_handler->list); 525219820Sjeff spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags); 526219820Sjeff 527219820Sjeff return 0; 528219820Sjeff} 529219820SjeffEXPORT_SYMBOL(ib_unregister_event_handler); 530219820Sjeff 531219820Sjeff/** 532219820Sjeff * ib_dispatch_event - Dispatch an asynchronous event 533219820Sjeff * @event:Event to dispatch 534219820Sjeff * 535219820Sjeff * Low-level drivers must call ib_dispatch_event() to dispatch the 536219820Sjeff * event to all registered event handlers when an asynchronous event 537219820Sjeff * occurs. 538219820Sjeff */ 539219820Sjeffvoid ib_dispatch_event(struct ib_event *event) 540219820Sjeff{ 541219820Sjeff unsigned long flags; 542219820Sjeff struct ib_event_handler *handler; 543219820Sjeff 544219820Sjeff spin_lock_irqsave(&event->device->event_handler_lock, flags); 545219820Sjeff 546219820Sjeff list_for_each_entry(handler, &event->device->event_handler_list, list) 547219820Sjeff handler->handler(handler, event); 548219820Sjeff 549219820Sjeff spin_unlock_irqrestore(&event->device->event_handler_lock, flags); 550219820Sjeff} 551219820SjeffEXPORT_SYMBOL(ib_dispatch_event); 552219820Sjeff 553219820Sjeff/** 554219820Sjeff * ib_query_device - Query IB device attributes 555219820Sjeff * @device:Device to query 556219820Sjeff * @device_attr:Device attributes 557219820Sjeff * 558219820Sjeff * ib_query_device() returns the attributes of a device through the 559219820Sjeff * @device_attr pointer. 560219820Sjeff */ 561219820Sjeffint ib_query_device(struct ib_device *device, 562219820Sjeff struct ib_device_attr *device_attr) 563219820Sjeff{ 564219820Sjeff return device->query_device(device, device_attr); 565219820Sjeff} 566219820SjeffEXPORT_SYMBOL(ib_query_device); 567219820Sjeff 568219820Sjeff/** 569219820Sjeff * ib_query_port - Query IB port attributes 570219820Sjeff * @device:Device to query 571219820Sjeff * @port_num:Port number to query 572219820Sjeff * @port_attr:Port attributes 573219820Sjeff * 574219820Sjeff * ib_query_port() returns the attributes of a port through the 575219820Sjeff * @port_attr pointer. 576219820Sjeff */ 577219820Sjeffint ib_query_port(struct ib_device *device, 578219820Sjeff u8 port_num, 579219820Sjeff struct ib_port_attr *port_attr) 580219820Sjeff{ 581219820Sjeff if (port_num < start_port(device) || port_num > end_port(device)) 582219820Sjeff return -EINVAL; 583219820Sjeff 584219820Sjeff return device->query_port(device, port_num, port_attr); 585219820Sjeff} 586219820SjeffEXPORT_SYMBOL(ib_query_port); 587219820Sjeff 588219820Sjeff/** 589219820Sjeff * ib_query_gid - Get GID table entry 590219820Sjeff * @device:Device to query 591219820Sjeff * @port_num:Port number to query 592219820Sjeff * @index:GID table index to query 593219820Sjeff * @gid:Returned GID 594219820Sjeff * 595219820Sjeff * ib_query_gid() fetches the specified GID table entry. 596219820Sjeff */ 597219820Sjeffint ib_query_gid(struct ib_device *device, 598219820Sjeff u8 port_num, int index, union ib_gid *gid) 599219820Sjeff{ 600219820Sjeff return device->query_gid(device, port_num, index, gid); 601219820Sjeff} 602219820SjeffEXPORT_SYMBOL(ib_query_gid); 603219820Sjeff 604219820Sjeff/** 605219820Sjeff * ib_query_pkey - Get P_Key table entry 606219820Sjeff * @device:Device to query 607219820Sjeff * @port_num:Port number to query 608219820Sjeff * @index:P_Key table index to query 609219820Sjeff * @pkey:Returned P_Key 610219820Sjeff * 611219820Sjeff * ib_query_pkey() fetches the specified P_Key table entry. 612219820Sjeff */ 613219820Sjeffint ib_query_pkey(struct ib_device *device, 614219820Sjeff u8 port_num, u16 index, u16 *pkey) 615219820Sjeff{ 616219820Sjeff return device->query_pkey(device, port_num, index, pkey); 617219820Sjeff} 618219820SjeffEXPORT_SYMBOL(ib_query_pkey); 619219820Sjeff 620219820Sjeff/** 621219820Sjeff * ib_modify_device - Change IB device attributes 622219820Sjeff * @device:Device to modify 623219820Sjeff * @device_modify_mask:Mask of attributes to change 624219820Sjeff * @device_modify:New attribute values 625219820Sjeff * 626219820Sjeff * ib_modify_device() changes a device's attributes as specified by 627219820Sjeff * the @device_modify_mask and @device_modify structure. 628219820Sjeff */ 629219820Sjeffint ib_modify_device(struct ib_device *device, 630219820Sjeff int device_modify_mask, 631219820Sjeff struct ib_device_modify *device_modify) 632219820Sjeff{ 633219820Sjeff return device->modify_device(device, device_modify_mask, 634219820Sjeff device_modify); 635219820Sjeff} 636219820SjeffEXPORT_SYMBOL(ib_modify_device); 637219820Sjeff 638219820Sjeff/** 639219820Sjeff * ib_modify_port - Modifies the attributes for the specified port. 640219820Sjeff * @device: The device to modify. 641219820Sjeff * @port_num: The number of the port to modify. 642219820Sjeff * @port_modify_mask: Mask used to specify which attributes of the port 643219820Sjeff * to change. 644219820Sjeff * @port_modify: New attribute values for the port. 645219820Sjeff * 646219820Sjeff * ib_modify_port() changes a port's attributes as specified by the 647219820Sjeff * @port_modify_mask and @port_modify structure. 648219820Sjeff */ 649219820Sjeffint ib_modify_port(struct ib_device *device, 650219820Sjeff u8 port_num, int port_modify_mask, 651219820Sjeff struct ib_port_modify *port_modify) 652219820Sjeff{ 653219820Sjeff if (port_num < start_port(device) || port_num > end_port(device)) 654219820Sjeff return -EINVAL; 655219820Sjeff 656219820Sjeff return device->modify_port(device, port_num, port_modify_mask, 657219820Sjeff port_modify); 658219820Sjeff} 659219820SjeffEXPORT_SYMBOL(ib_modify_port); 660219820Sjeff 661219820Sjeff/** 662219820Sjeff * ib_find_gid - Returns the port number and GID table index where 663219820Sjeff * a specified GID value occurs. 664219820Sjeff * @device: The device to query. 665219820Sjeff * @gid: The GID value to search for. 666219820Sjeff * @port_num: The port number of the device where the GID value was found. 667219820Sjeff * @index: The index into the GID table where the GID was found. This 668219820Sjeff * parameter may be NULL. 669219820Sjeff */ 670219820Sjeffint ib_find_gid(struct ib_device *device, union ib_gid *gid, 671219820Sjeff u8 *port_num, u16 *index) 672219820Sjeff{ 673219820Sjeff union ib_gid tmp_gid; 674219820Sjeff int ret, port, i; 675219820Sjeff 676219820Sjeff for (port = start_port(device); port <= end_port(device); ++port) { 677219820Sjeff for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) { 678219820Sjeff ret = ib_query_gid(device, port, i, &tmp_gid); 679219820Sjeff if (ret) 680219820Sjeff return ret; 681219820Sjeff if (!memcmp(&tmp_gid, gid, sizeof *gid)) { 682219820Sjeff *port_num = port; 683219820Sjeff if (index) 684219820Sjeff *index = i; 685219820Sjeff return 0; 686219820Sjeff } 687219820Sjeff } 688219820Sjeff } 689219820Sjeff 690219820Sjeff return -ENOENT; 691219820Sjeff} 692219820SjeffEXPORT_SYMBOL(ib_find_gid); 693219820Sjeff 694219820Sjeff/** 695219820Sjeff * ib_find_pkey - Returns the PKey table index where a specified 696219820Sjeff * PKey value occurs. 697219820Sjeff * @device: The device to query. 698219820Sjeff * @port_num: The port number of the device to search for the PKey. 699219820Sjeff * @pkey: The PKey value to search for. 700219820Sjeff * @index: The index into the PKey table where the PKey was found. 701219820Sjeff */ 702219820Sjeffint ib_find_pkey(struct ib_device *device, 703219820Sjeff u8 port_num, u16 pkey, u16 *index) 704219820Sjeff{ 705219820Sjeff int ret, i; 706219820Sjeff u16 tmp_pkey; 707219820Sjeff 708219820Sjeff for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) { 709219820Sjeff ret = ib_query_pkey(device, port_num, i, &tmp_pkey); 710219820Sjeff if (ret) 711219820Sjeff return ret; 712219820Sjeff 713219820Sjeff if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) { 714219820Sjeff *index = i; 715219820Sjeff return 0; 716219820Sjeff } 717219820Sjeff } 718219820Sjeff 719219820Sjeff return -ENOENT; 720219820Sjeff} 721219820SjeffEXPORT_SYMBOL(ib_find_pkey); 722219820Sjeff 723219820Sjeffstatic int __init ib_core_init(void) 724219820Sjeff{ 725219820Sjeff int ret; 726219820Sjeff 727219820Sjeff#ifdef __ia64__ 728219820Sjeff if (ia64_platform_is("hpzx1")) 729219820Sjeff dma_map_sg_hp_wa = 1; 730219820Sjeff#endif 731219820Sjeff 732219820Sjeff ret = ib_sysfs_setup(); 733219820Sjeff if (ret) 734219820Sjeff printk(KERN_WARNING "Couldn't create InfiniBand device class\n"); 735219820Sjeff 736219820Sjeff ret = ib_cache_setup(); 737219820Sjeff if (ret) { 738219820Sjeff printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n"); 739219820Sjeff ib_sysfs_cleanup(); 740219820Sjeff } 741219820Sjeff 742219820Sjeff return ret; 743219820Sjeff} 744219820Sjeff 745219820Sjeffstatic void __exit ib_core_cleanup(void) 746219820Sjeff{ 747219820Sjeff ib_cache_cleanup(); 748219820Sjeff ib_sysfs_cleanup(); 749219820Sjeff /* Make sure that any pending umem accounting work is done. */ 750219820Sjeff flush_scheduled_work(); 751219820Sjeff} 752219820Sjeff 753219820Sjeffmodule_init(ib_core_init); 754219820Sjeffmodule_exit(ib_core_cleanup); 755