2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
33 * $Id: device.c 1349 2004-12-16 21:09:43Z roland $
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>
42 #include <asm/semaphore.h>
44 #include "core_priv.h"
46 MODULE_AUTHOR("Roland Dreier");
47 MODULE_DESCRIPTION("core kernel InfiniBand API");
48 MODULE_LICENSE("Dual BSD/GPL");
50 struct ib_client_data {
51 struct list_head list;
52 struct ib_client *client;
56 static LIST_HEAD(device_list);
57 static LIST_HEAD(client_list);
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.
66 static DECLARE_MUTEX(device_sem);
68 static int ib_device_check_mandatory(struct ib_device *device)
70 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
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)
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);
108 static struct ib_device *__ib_device_get_by_name(const char *name)
110 struct ib_device *device;
112 list_for_each_entry(device, &device_list, core_list)
113 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
120 static int alloc_name(char *name)
123 char buf[IB_DEVICE_NAME_MAX];
124 struct ib_device *device;
127 inuse = (long *) get_zeroed_page(GFP_KERNEL);
131 list_for_each_entry(device, &device_list, core_list) {
132 if (!sscanf(device->name, name, &i))
134 if (i < 0 || i >= PAGE_SIZE * 8)
136 snprintf(buf, sizeof buf, name, i);
137 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
141 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
142 free_page((unsigned long) inuse);
143 snprintf(buf, sizeof buf, name, i);
145 if (__ib_device_get_by_name(buf))
148 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
153 * ib_alloc_device - allocate an IB device struct
154 * @size:size of structure to allocate
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
162 struct ib_device *ib_alloc_device(size_t size)
166 BUG_ON(size < sizeof (struct ib_device));
168 dev = kmalloc(size, GFP_KERNEL);
172 memset(dev, 0, size);
176 EXPORT_SYMBOL(ib_alloc_device);
179 * ib_dealloc_device - free an IB device struct
180 * @device:structure to free
182 * Free a structure allocated with ib_alloc_device().
184 void ib_dealloc_device(struct ib_device *device)
186 if (device->reg_state == IB_DEV_UNINITIALIZED) {
191 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
193 ib_device_unregister_sysfs(device);
195 EXPORT_SYMBOL(ib_dealloc_device);
197 static int add_client_context(struct ib_device *device, struct ib_client *client)
199 struct ib_client_data *context;
202 context = kmalloc(sizeof *context, GFP_KERNEL);
204 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
205 device->name, client->name);
209 context->client = client;
210 context->data = NULL;
212 spin_lock_irqsave(&device->client_data_lock, flags);
213 list_add(&context->list, &device->client_data_list);
214 spin_unlock_irqrestore(&device->client_data_lock, flags);
220 * ib_register_device - Register an IB device with IB core
221 * @device:Device to register
223 * Low-level drivers use ib_register_device() to register their
224 * devices with the IB core. All registered clients will receive a
225 * callback for each device that is added. @device must be allocated
226 * with ib_alloc_device().
228 int ib_register_device(struct ib_device *device)
234 if (strchr(device->name, '%')) {
235 ret = alloc_name(device->name);
240 if (ib_device_check_mandatory(device)) {
245 INIT_LIST_HEAD(&device->event_handler_list);
246 INIT_LIST_HEAD(&device->client_data_list);
247 spin_lock_init(&device->event_handler_lock);
248 spin_lock_init(&device->client_data_lock);
250 ret = ib_device_register_sysfs(device);
252 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
257 list_add_tail(&device->core_list, &device_list);
259 device->reg_state = IB_DEV_REGISTERED;
262 struct ib_client *client;
264 list_for_each_entry(client, &client_list, list)
265 if (client->add && !add_client_context(device, client))
273 EXPORT_SYMBOL(ib_register_device);
276 * ib_unregister_device - Unregister an IB device
277 * @device:Device to unregister
279 * Unregister an IB device. All clients will receive a remove callback.
281 void ib_unregister_device(struct ib_device *device)
283 struct ib_client *client;
284 struct ib_client_data *context, *tmp;
289 list_for_each_entry_reverse(client, &client_list, list)
291 client->remove(device);
293 list_del(&device->core_list);
297 spin_lock_irqsave(&device->client_data_lock, flags);
298 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
300 spin_unlock_irqrestore(&device->client_data_lock, flags);
302 device->reg_state = IB_DEV_UNREGISTERED;
304 EXPORT_SYMBOL(ib_unregister_device);
307 * ib_register_client - Register an IB client
308 * @client:Client to register
310 * Upper level users of the IB drivers can use ib_register_client() to
311 * register callbacks for IB device addition and removal. When an IB
312 * device is added, each registered client's add method will be called
313 * (in the order the clients were registered), and when a device is
314 * removed, each client's remove method will be called (in the reverse
315 * order that clients were registered). In addition, when
316 * ib_register_client() is called, the client will receive an add
317 * callback for all devices already registered.
319 int ib_register_client(struct ib_client *client)
321 struct ib_device *device;
325 list_add_tail(&client->list, &client_list);
326 list_for_each_entry(device, &device_list, core_list)
327 if (client->add && !add_client_context(device, client))
334 EXPORT_SYMBOL(ib_register_client);
337 * ib_unregister_client - Unregister an IB client
338 * @client:Client to unregister
340 * Upper level users use ib_unregister_client() to remove their client
341 * registration. When ib_unregister_client() is called, the client
342 * will receive a remove callback for each IB device still registered.
344 void ib_unregister_client(struct ib_client *client)
346 struct ib_client_data *context, *tmp;
347 struct ib_device *device;
352 list_for_each_entry(device, &device_list, core_list) {
354 client->remove(device);
356 spin_lock_irqsave(&device->client_data_lock, flags);
357 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
358 if (context->client == client) {
359 list_del(&context->list);
362 spin_unlock_irqrestore(&device->client_data_lock, flags);
364 list_del(&client->list);
368 EXPORT_SYMBOL(ib_unregister_client);
371 * ib_get_client_data - Get IB client context
372 * @device:Device to get context for
373 * @client:Client to get context for
375 * ib_get_client_data() returns client context set with
376 * ib_set_client_data().
378 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
380 struct ib_client_data *context;
384 spin_lock_irqsave(&device->client_data_lock, flags);
385 list_for_each_entry(context, &device->client_data_list, list)
386 if (context->client == client) {
390 spin_unlock_irqrestore(&device->client_data_lock, flags);
394 EXPORT_SYMBOL(ib_get_client_data);
397 * ib_set_client_data - Get IB client context
398 * @device:Device to set context for
399 * @client:Client to set context for
400 * @data:Context to set
402 * ib_set_client_data() sets client context that can be retrieved with
403 * ib_get_client_data().
405 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
408 struct ib_client_data *context;
411 spin_lock_irqsave(&device->client_data_lock, flags);
412 list_for_each_entry(context, &device->client_data_list, list)
413 if (context->client == client) {
414 context->data = data;
418 printk(KERN_WARNING "No client context found for %s/%s\n",
419 device->name, client->name);
422 spin_unlock_irqrestore(&device->client_data_lock, flags);
424 EXPORT_SYMBOL(ib_set_client_data);
427 * ib_register_event_handler - Register an IB event handler
428 * @event_handler:Handler to register
430 * ib_register_event_handler() registers an event handler that will be
431 * called back when asynchronous IB events occur (as defined in
432 * chapter 11 of the InfiniBand Architecture Specification). This
433 * callback may occur in interrupt context.
435 int ib_register_event_handler (struct ib_event_handler *event_handler)
439 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
440 list_add_tail(&event_handler->list,
441 &event_handler->device->event_handler_list);
442 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
446 EXPORT_SYMBOL(ib_register_event_handler);
449 * ib_unregister_event_handler - Unregister an event handler
450 * @event_handler:Handler to unregister
452 * Unregister an event handler registered with
453 * ib_register_event_handler().
455 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
459 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
460 list_del(&event_handler->list);
461 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
465 EXPORT_SYMBOL(ib_unregister_event_handler);
468 * ib_dispatch_event - Dispatch an asynchronous event
469 * @event:Event to dispatch
471 * Low-level drivers must call ib_dispatch_event() to dispatch the
472 * event to all registered event handlers when an asynchronous event
475 void ib_dispatch_event(struct ib_event *event)
478 struct ib_event_handler *handler;
480 spin_lock_irqsave(&event->device->event_handler_lock, flags);
482 list_for_each_entry(handler, &event->device->event_handler_list, list)
483 handler->handler(handler, event);
485 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
487 EXPORT_SYMBOL(ib_dispatch_event);
490 * ib_query_device - Query IB device attributes
491 * @device:Device to query
492 * @device_attr:Device attributes
494 * ib_query_device() returns the attributes of a device through the
495 * @device_attr pointer.
497 int ib_query_device(struct ib_device *device,
498 struct ib_device_attr *device_attr)
500 return device->query_device(device, device_attr);
502 EXPORT_SYMBOL(ib_query_device);
505 * ib_query_port - Query IB port attributes
506 * @device:Device to query
507 * @port_num:Port number to query
508 * @port_attr:Port attributes
510 * ib_query_port() returns the attributes of a port through the
511 * @port_attr pointer.
513 int ib_query_port(struct ib_device *device,
515 struct ib_port_attr *port_attr)
517 if (device->node_type == IB_NODE_SWITCH) {
520 } else if (port_num < 1 || port_num > device->phys_port_cnt)
523 return device->query_port(device, port_num, port_attr);
525 EXPORT_SYMBOL(ib_query_port);
528 * ib_query_gid - Get GID table entry
529 * @device:Device to query
530 * @port_num:Port number to query
531 * @index:GID table index to query
534 * ib_query_gid() fetches the specified GID table entry.
536 int ib_query_gid(struct ib_device *device,
537 u8 port_num, int index, union ib_gid *gid)
539 return device->query_gid(device, port_num, index, gid);
541 EXPORT_SYMBOL(ib_query_gid);
544 * ib_query_pkey - Get P_Key table entry
545 * @device:Device to query
546 * @port_num:Port number to query
547 * @index:P_Key table index to query
548 * @pkey:Returned P_Key
550 * ib_query_pkey() fetches the specified P_Key table entry.
552 int ib_query_pkey(struct ib_device *device,
553 u8 port_num, u16 index, u16 *pkey)
555 return device->query_pkey(device, port_num, index, pkey);
557 EXPORT_SYMBOL(ib_query_pkey);
560 * ib_modify_device - Change IB device attributes
561 * @device:Device to modify
562 * @device_modify_mask:Mask of attributes to change
563 * @device_modify:New attribute values
565 * ib_modify_device() changes a device's attributes as specified by
566 * the @device_modify_mask and @device_modify structure.
568 int ib_modify_device(struct ib_device *device,
569 int device_modify_mask,
570 struct ib_device_modify *device_modify)
572 return device->modify_device(device, device_modify_mask,
575 EXPORT_SYMBOL(ib_modify_device);
578 * ib_modify_port - Modifies the attributes for the specified port.
579 * @device: The device to modify.
580 * @port_num: The number of the port to modify.
581 * @port_modify_mask: Mask used to specify which attributes of the port
583 * @port_modify: New attribute values for the port.
585 * ib_modify_port() changes a port's attributes as specified by the
586 * @port_modify_mask and @port_modify structure.
588 int ib_modify_port(struct ib_device *device,
589 u8 port_num, int port_modify_mask,
590 struct ib_port_modify *port_modify)
592 if (device->node_type == IB_NODE_SWITCH) {
595 } else if (port_num < 1 || port_num > device->phys_port_cnt)
598 return device->modify_port(device, port_num, port_modify_mask,
601 EXPORT_SYMBOL(ib_modify_port);
603 static int __init ib_core_init(void)
607 ret = ib_sysfs_setup();
609 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
611 ret = ib_cache_setup();
613 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
620 static void __exit ib_core_cleanup(void)
626 module_init(ib_core_init);
627 module_exit(ib_core_cleanup);