1 /* 2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 * 33 * $Id: device.c 1349 2004-12-16 21:09:43Z roland $ 34 */ 35 36 #include <linux/module.h> 37 #include <linux/string.h> 38 #include <linux/errno.h> 39 #include <linux/slab.h> 40 #include <linux/init.h> 41 42 #include <asm/semaphore.h> 43 44 #include "core_priv.h" 45 46 MODULE_AUTHOR("Roland Dreier"); 47 MODULE_DESCRIPTION("core kernel InfiniBand API"); 48 MODULE_LICENSE("Dual BSD/GPL"); 49 50 struct ib_client_data { 51 struct list_head list; 52 struct ib_client *client; 53 void * data; 54 }; 55 56 static LIST_HEAD(device_list); 57 static LIST_HEAD(client_list); 58 59 /* 60 * device_sem protects access to both device_list and client_list. 61 * There's no real point to using multiple locks or something fancier 62 * like an rwsem: we always access both lists, and we're always 63 * modifying one list or the other list. In any case this is not a 64 * hot path so there's no point in trying to optimize. 65 */ 66 static DECLARE_MUTEX(device_sem); 67 68 static int ib_device_check_mandatory(struct ib_device *device) 69 { 70 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x } 71 static const struct { 72 size_t offset; 73 char *name; 74 } mandatory_table[] = { 75 IB_MANDATORY_FUNC(query_device), 76 IB_MANDATORY_FUNC(query_port), 77 IB_MANDATORY_FUNC(query_pkey), 78 IB_MANDATORY_FUNC(query_gid), 79 IB_MANDATORY_FUNC(alloc_pd), 80 IB_MANDATORY_FUNC(dealloc_pd), 81 IB_MANDATORY_FUNC(create_ah), 82 IB_MANDATORY_FUNC(destroy_ah), 83 IB_MANDATORY_FUNC(create_qp), 84 IB_MANDATORY_FUNC(modify_qp), 85 IB_MANDATORY_FUNC(destroy_qp), 86 IB_MANDATORY_FUNC(post_send), 87 IB_MANDATORY_FUNC(post_recv), 88 IB_MANDATORY_FUNC(create_cq), 89 IB_MANDATORY_FUNC(destroy_cq), 90 IB_MANDATORY_FUNC(poll_cq), 91 IB_MANDATORY_FUNC(req_notify_cq), 92 IB_MANDATORY_FUNC(get_dma_mr), 93 IB_MANDATORY_FUNC(dereg_mr) 94 }; 95 int i; 96 97 for (i = 0; i < sizeof mandatory_table / sizeof mandatory_table[0]; ++i) { 98 if (!*(void **) ((void *) device + mandatory_table[i].offset)) { 99 printk(KERN_WARNING "Device %s is missing mandatory function %s\n", 100 device->name, mandatory_table[i].name); 101 return -EINVAL; 102 } 103 } 104 105 return 0; 106 } 107 108 static struct ib_device *__ib_device_get_by_name(const char *name) 109 { 110 struct ib_device *device; 111 112 list_for_each_entry(device, &device_list, core_list) 113 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX)) 114 return device; 115 116 return NULL; 117 } 118 119 120 static int alloc_name(char *name) 121 { 122 long *inuse; 123 char buf[IB_DEVICE_NAME_MAX]; 124 struct ib_device *device; 125 int i; 126 127 inuse = (long *) get_zeroed_page(GFP_KERNEL); 128 if (!inuse) 129 return -ENOMEM; 130 131 list_for_each_entry(device, &device_list, core_list) { 132 if (!sscanf(device->name, name, &i)) 133 continue; 134 if (i < 0 || i >= PAGE_SIZE * 8) 135 continue; 136 snprintf(buf, sizeof buf, name, i); 137 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX)) 138 set_bit(i, inuse); 139 } 140 141 i = find_first_zero_bit(inuse, PAGE_SIZE * 8); 142 free_page((unsigned long) inuse); 143 snprintf(buf, sizeof buf, name, i); 144 145 if (__ib_device_get_by_name(buf)) 146 return -ENFILE; 147 148 strlcpy(name, buf, IB_DEVICE_NAME_MAX); 149 return 0; 150 } 151 152 /** 153 * ib_alloc_device - allocate an IB device struct 154 * @size:size of structure to allocate 155 * 156 * Low-level drivers should use ib_alloc_device() to allocate &struct 157 * ib_device. @size is the size of the structure to be allocated, 158 * including any private data used by the low-level driver. 159 * ib_dealloc_device() must be used to free structures allocated with 160 * ib_alloc_device(). 161 */ 162 struct ib_device *ib_alloc_device(size_t size) 163 { 164 BUG_ON(size < sizeof (struct ib_device)); 165 166 return kzalloc(size, GFP_KERNEL); 167 } 168 EXPORT_SYMBOL(ib_alloc_device); 169 170 /** 171 * ib_dealloc_device - free an IB device struct 172 * @device:structure to free 173 * 174 * Free a structure allocated with ib_alloc_device(). 175 */ 176 void ib_dealloc_device(struct ib_device *device) 177 { 178 if (device->reg_state == IB_DEV_UNINITIALIZED) { 179 kfree(device); 180 return; 181 } 182 183 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED); 184 185 ib_device_unregister_sysfs(device); 186 } 187 EXPORT_SYMBOL(ib_dealloc_device); 188 189 static int add_client_context(struct ib_device *device, struct ib_client *client) 190 { 191 struct ib_client_data *context; 192 unsigned long flags; 193 194 context = kmalloc(sizeof *context, GFP_KERNEL); 195 if (!context) { 196 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n", 197 device->name, client->name); 198 return -ENOMEM; 199 } 200 201 context->client = client; 202 context->data = NULL; 203 204 spin_lock_irqsave(&device->client_data_lock, flags); 205 list_add(&context->list, &device->client_data_list); 206 spin_unlock_irqrestore(&device->client_data_lock, flags); 207 208 return 0; 209 } 210 211 /** 212 * ib_register_device - Register an IB device with IB core 213 * @device:Device to register 214 * 215 * Low-level drivers use ib_register_device() to register their 216 * devices with the IB core. All registered clients will receive a 217 * callback for each device that is added. @device must be allocated 218 * with ib_alloc_device(). 219 */ 220 int ib_register_device(struct ib_device *device) 221 { 222 int ret; 223 224 down(&device_sem); 225 226 if (strchr(device->name, '%')) { 227 ret = alloc_name(device->name); 228 if (ret) 229 goto out; 230 } 231 232 if (ib_device_check_mandatory(device)) { 233 ret = -EINVAL; 234 goto out; 235 } 236 237 INIT_LIST_HEAD(&device->event_handler_list); 238 INIT_LIST_HEAD(&device->client_data_list); 239 spin_lock_init(&device->event_handler_lock); 240 spin_lock_init(&device->client_data_lock); 241 242 ret = ib_device_register_sysfs(device); 243 if (ret) { 244 printk(KERN_WARNING "Couldn't register device %s with driver model\n", 245 device->name); 246 goto out; 247 } 248 249 list_add_tail(&device->core_list, &device_list); 250 251 device->reg_state = IB_DEV_REGISTERED; 252 253 { 254 struct ib_client *client; 255 256 list_for_each_entry(client, &client_list, list) 257 if (client->add && !add_client_context(device, client)) 258 client->add(device); 259 } 260 261 out: 262 up(&device_sem); 263 return ret; 264 } 265 EXPORT_SYMBOL(ib_register_device); 266 267 /** 268 * ib_unregister_device - Unregister an IB device 269 * @device:Device to unregister 270 * 271 * Unregister an IB device. All clients will receive a remove callback. 272 */ 273 void ib_unregister_device(struct ib_device *device) 274 { 275 struct ib_client *client; 276 struct ib_client_data *context, *tmp; 277 unsigned long flags; 278 279 down(&device_sem); 280 281 list_for_each_entry_reverse(client, &client_list, list) 282 if (client->remove) 283 client->remove(device); 284 285 list_del(&device->core_list); 286 287 up(&device_sem); 288 289 spin_lock_irqsave(&device->client_data_lock, flags); 290 list_for_each_entry_safe(context, tmp, &device->client_data_list, list) 291 kfree(context); 292 spin_unlock_irqrestore(&device->client_data_lock, flags); 293 294 device->reg_state = IB_DEV_UNREGISTERED; 295 } 296 EXPORT_SYMBOL(ib_unregister_device); 297 298 /** 299 * ib_register_client - Register an IB client 300 * @client:Client to register 301 * 302 * Upper level users of the IB drivers can use ib_register_client() to 303 * register callbacks for IB device addition and removal. When an IB 304 * device is added, each registered client's add method will be called 305 * (in the order the clients were registered), and when a device is 306 * removed, each client's remove method will be called (in the reverse 307 * order that clients were registered). In addition, when 308 * ib_register_client() is called, the client will receive an add 309 * callback for all devices already registered. 310 */ 311 int ib_register_client(struct ib_client *client) 312 { 313 struct ib_device *device; 314 315 down(&device_sem); 316 317 list_add_tail(&client->list, &client_list); 318 list_for_each_entry(device, &device_list, core_list) 319 if (client->add && !add_client_context(device, client)) 320 client->add(device); 321 322 up(&device_sem); 323 324 return 0; 325 } 326 EXPORT_SYMBOL(ib_register_client); 327 328 /** 329 * ib_unregister_client - Unregister an IB client 330 * @client:Client to unregister 331 * 332 * Upper level users use ib_unregister_client() to remove their client 333 * registration. When ib_unregister_client() is called, the client 334 * will receive a remove callback for each IB device still registered. 335 */ 336 void ib_unregister_client(struct ib_client *client) 337 { 338 struct ib_client_data *context, *tmp; 339 struct ib_device *device; 340 unsigned long flags; 341 342 down(&device_sem); 343 344 list_for_each_entry(device, &device_list, core_list) { 345 if (client->remove) 346 client->remove(device); 347 348 spin_lock_irqsave(&device->client_data_lock, flags); 349 list_for_each_entry_safe(context, tmp, &device->client_data_list, list) 350 if (context->client == client) { 351 list_del(&context->list); 352 kfree(context); 353 } 354 spin_unlock_irqrestore(&device->client_data_lock, flags); 355 } 356 list_del(&client->list); 357 358 up(&device_sem); 359 } 360 EXPORT_SYMBOL(ib_unregister_client); 361 362 /** 363 * ib_get_client_data - Get IB client context 364 * @device:Device to get context for 365 * @client:Client to get context for 366 * 367 * ib_get_client_data() returns client context set with 368 * ib_set_client_data(). 369 */ 370 void *ib_get_client_data(struct ib_device *device, struct ib_client *client) 371 { 372 struct ib_client_data *context; 373 void *ret = NULL; 374 unsigned long flags; 375 376 spin_lock_irqsave(&device->client_data_lock, flags); 377 list_for_each_entry(context, &device->client_data_list, list) 378 if (context->client == client) { 379 ret = context->data; 380 break; 381 } 382 spin_unlock_irqrestore(&device->client_data_lock, flags); 383 384 return ret; 385 } 386 EXPORT_SYMBOL(ib_get_client_data); 387 388 /** 389 * ib_set_client_data - Get IB client context 390 * @device:Device to set context for 391 * @client:Client to set context for 392 * @data:Context to set 393 * 394 * ib_set_client_data() sets client context that can be retrieved with 395 * ib_get_client_data(). 396 */ 397 void ib_set_client_data(struct ib_device *device, struct ib_client *client, 398 void *data) 399 { 400 struct ib_client_data *context; 401 unsigned long flags; 402 403 spin_lock_irqsave(&device->client_data_lock, flags); 404 list_for_each_entry(context, &device->client_data_list, list) 405 if (context->client == client) { 406 context->data = data; 407 goto out; 408 } 409 410 printk(KERN_WARNING "No client context found for %s/%s\n", 411 device->name, client->name); 412 413 out: 414 spin_unlock_irqrestore(&device->client_data_lock, flags); 415 } 416 EXPORT_SYMBOL(ib_set_client_data); 417 418 /** 419 * ib_register_event_handler - Register an IB event handler 420 * @event_handler:Handler to register 421 * 422 * ib_register_event_handler() registers an event handler that will be 423 * called back when asynchronous IB events occur (as defined in 424 * chapter 11 of the InfiniBand Architecture Specification). This 425 * callback may occur in interrupt context. 426 */ 427 int ib_register_event_handler (struct ib_event_handler *event_handler) 428 { 429 unsigned long flags; 430 431 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags); 432 list_add_tail(&event_handler->list, 433 &event_handler->device->event_handler_list); 434 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags); 435 436 return 0; 437 } 438 EXPORT_SYMBOL(ib_register_event_handler); 439 440 /** 441 * ib_unregister_event_handler - Unregister an event handler 442 * @event_handler:Handler to unregister 443 * 444 * Unregister an event handler registered with 445 * ib_register_event_handler(). 446 */ 447 int ib_unregister_event_handler(struct ib_event_handler *event_handler) 448 { 449 unsigned long flags; 450 451 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags); 452 list_del(&event_handler->list); 453 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags); 454 455 return 0; 456 } 457 EXPORT_SYMBOL(ib_unregister_event_handler); 458 459 /** 460 * ib_dispatch_event - Dispatch an asynchronous event 461 * @event:Event to dispatch 462 * 463 * Low-level drivers must call ib_dispatch_event() to dispatch the 464 * event to all registered event handlers when an asynchronous event 465 * occurs. 466 */ 467 void ib_dispatch_event(struct ib_event *event) 468 { 469 unsigned long flags; 470 struct ib_event_handler *handler; 471 472 spin_lock_irqsave(&event->device->event_handler_lock, flags); 473 474 list_for_each_entry(handler, &event->device->event_handler_list, list) 475 handler->handler(handler, event); 476 477 spin_unlock_irqrestore(&event->device->event_handler_lock, flags); 478 } 479 EXPORT_SYMBOL(ib_dispatch_event); 480 481 /** 482 * ib_query_device - Query IB device attributes 483 * @device:Device to query 484 * @device_attr:Device attributes 485 * 486 * ib_query_device() returns the attributes of a device through the 487 * @device_attr pointer. 488 */ 489 int ib_query_device(struct ib_device *device, 490 struct ib_device_attr *device_attr) 491 { 492 return device->query_device(device, device_attr); 493 } 494 EXPORT_SYMBOL(ib_query_device); 495 496 /** 497 * ib_query_port - Query IB port attributes 498 * @device:Device to query 499 * @port_num:Port number to query 500 * @port_attr:Port attributes 501 * 502 * ib_query_port() returns the attributes of a port through the 503 * @port_attr pointer. 504 */ 505 int ib_query_port(struct ib_device *device, 506 u8 port_num, 507 struct ib_port_attr *port_attr) 508 { 509 if (device->node_type == IB_NODE_SWITCH) { 510 if (port_num) 511 return -EINVAL; 512 } else if (port_num < 1 || port_num > device->phys_port_cnt) 513 return -EINVAL; 514 515 return device->query_port(device, port_num, port_attr); 516 } 517 EXPORT_SYMBOL(ib_query_port); 518 519 /** 520 * ib_query_gid - Get GID table entry 521 * @device:Device to query 522 * @port_num:Port number to query 523 * @index:GID table index to query 524 * @gid:Returned GID 525 * 526 * ib_query_gid() fetches the specified GID table entry. 527 */ 528 int ib_query_gid(struct ib_device *device, 529 u8 port_num, int index, union ib_gid *gid) 530 { 531 return device->query_gid(device, port_num, index, gid); 532 } 533 EXPORT_SYMBOL(ib_query_gid); 534 535 /** 536 * ib_query_pkey - Get P_Key table entry 537 * @device:Device to query 538 * @port_num:Port number to query 539 * @index:P_Key table index to query 540 * @pkey:Returned P_Key 541 * 542 * ib_query_pkey() fetches the specified P_Key table entry. 543 */ 544 int ib_query_pkey(struct ib_device *device, 545 u8 port_num, u16 index, u16 *pkey) 546 { 547 return device->query_pkey(device, port_num, index, pkey); 548 } 549 EXPORT_SYMBOL(ib_query_pkey); 550 551 /** 552 * ib_modify_device - Change IB device attributes 553 * @device:Device to modify 554 * @device_modify_mask:Mask of attributes to change 555 * @device_modify:New attribute values 556 * 557 * ib_modify_device() changes a device's attributes as specified by 558 * the @device_modify_mask and @device_modify structure. 559 */ 560 int ib_modify_device(struct ib_device *device, 561 int device_modify_mask, 562 struct ib_device_modify *device_modify) 563 { 564 return device->modify_device(device, device_modify_mask, 565 device_modify); 566 } 567 EXPORT_SYMBOL(ib_modify_device); 568 569 /** 570 * ib_modify_port - Modifies the attributes for the specified port. 571 * @device: The device to modify. 572 * @port_num: The number of the port to modify. 573 * @port_modify_mask: Mask used to specify which attributes of the port 574 * to change. 575 * @port_modify: New attribute values for the port. 576 * 577 * ib_modify_port() changes a port's attributes as specified by the 578 * @port_modify_mask and @port_modify structure. 579 */ 580 int ib_modify_port(struct ib_device *device, 581 u8 port_num, int port_modify_mask, 582 struct ib_port_modify *port_modify) 583 { 584 if (device->node_type == IB_NODE_SWITCH) { 585 if (port_num) 586 return -EINVAL; 587 } else if (port_num < 1 || port_num > device->phys_port_cnt) 588 return -EINVAL; 589 590 return device->modify_port(device, port_num, port_modify_mask, 591 port_modify); 592 } 593 EXPORT_SYMBOL(ib_modify_port); 594 595 static int __init ib_core_init(void) 596 { 597 int ret; 598 599 ret = ib_sysfs_setup(); 600 if (ret) 601 printk(KERN_WARNING "Couldn't create InfiniBand device class\n"); 602 603 ret = ib_cache_setup(); 604 if (ret) { 605 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n"); 606 ib_sysfs_cleanup(); 607 } 608 609 return ret; 610 } 611 612 static void __exit ib_core_cleanup(void) 613 { 614 ib_cache_cleanup(); 615 ib_sysfs_cleanup(); 616 } 617 618 module_init(ib_core_init); 619 module_exit(ib_core_cleanup); 620