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/kernel.h>
40 #include <linux/slab.h>
41 #include <linux/init.h>
42 #include <linux/mutex.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_mutex 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 DEFINE_MUTEX(device_mutex
);
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
< ARRAY_SIZE(mandatory_table
); ++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
)
164 BUG_ON(size
< sizeof (struct ib_device
));
166 return kzalloc(size
, GFP_KERNEL
);
168 EXPORT_SYMBOL(ib_alloc_device
);
171 * ib_dealloc_device - free an IB device struct
172 * @device:structure to free
174 * Free a structure allocated with ib_alloc_device().
176 void ib_dealloc_device(struct ib_device
*device
)
178 if (device
->reg_state
== IB_DEV_UNINITIALIZED
) {
183 BUG_ON(device
->reg_state
!= IB_DEV_UNREGISTERED
);
185 ib_device_unregister_sysfs(device
);
187 EXPORT_SYMBOL(ib_dealloc_device
);
189 static int add_client_context(struct ib_device
*device
, struct ib_client
*client
)
191 struct ib_client_data
*context
;
194 context
= kmalloc(sizeof *context
, GFP_KERNEL
);
196 printk(KERN_WARNING
"Couldn't allocate client context for %s/%s\n",
197 device
->name
, client
->name
);
201 context
->client
= client
;
202 context
->data
= NULL
;
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
);
212 * ib_register_device - Register an IB device with IB core
213 * @device:Device to register
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().
220 int ib_register_device(struct ib_device
*device
)
224 mutex_lock(&device_mutex
);
226 if (strchr(device
->name
, '%')) {
227 ret
= alloc_name(device
->name
);
232 if (ib_device_check_mandatory(device
)) {
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
);
242 ret
= ib_device_register_sysfs(device
);
244 printk(KERN_WARNING
"Couldn't register device %s with driver model\n",
249 list_add_tail(&device
->core_list
, &device_list
);
251 device
->reg_state
= IB_DEV_REGISTERED
;
254 struct ib_client
*client
;
256 list_for_each_entry(client
, &client_list
, list
)
257 if (client
->add
&& !add_client_context(device
, client
))
262 mutex_unlock(&device_mutex
);
265 EXPORT_SYMBOL(ib_register_device
);
268 * ib_unregister_device - Unregister an IB device
269 * @device:Device to unregister
271 * Unregister an IB device. All clients will receive a remove callback.
273 void ib_unregister_device(struct ib_device
*device
)
275 struct ib_client
*client
;
276 struct ib_client_data
*context
, *tmp
;
279 mutex_lock(&device_mutex
);
281 list_for_each_entry_reverse(client
, &client_list
, list
)
283 client
->remove(device
);
285 list_del(&device
->core_list
);
287 mutex_unlock(&device_mutex
);
289 spin_lock_irqsave(&device
->client_data_lock
, flags
);
290 list_for_each_entry_safe(context
, tmp
, &device
->client_data_list
, list
)
292 spin_unlock_irqrestore(&device
->client_data_lock
, flags
);
294 device
->reg_state
= IB_DEV_UNREGISTERED
;
296 EXPORT_SYMBOL(ib_unregister_device
);
299 * ib_register_client - Register an IB client
300 * @client:Client to register
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.
311 int ib_register_client(struct ib_client
*client
)
313 struct ib_device
*device
;
315 mutex_lock(&device_mutex
);
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
))
322 mutex_unlock(&device_mutex
);
326 EXPORT_SYMBOL(ib_register_client
);
329 * ib_unregister_client - Unregister an IB client
330 * @client:Client to unregister
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.
336 void ib_unregister_client(struct ib_client
*client
)
338 struct ib_client_data
*context
, *tmp
;
339 struct ib_device
*device
;
342 mutex_lock(&device_mutex
);
344 list_for_each_entry(device
, &device_list
, core_list
) {
346 client
->remove(device
);
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
);
354 spin_unlock_irqrestore(&device
->client_data_lock
, flags
);
356 list_del(&client
->list
);
358 mutex_unlock(&device_mutex
);
360 EXPORT_SYMBOL(ib_unregister_client
);
363 * ib_get_client_data - Get IB client context
364 * @device:Device to get context for
365 * @client:Client to get context for
367 * ib_get_client_data() returns client context set with
368 * ib_set_client_data().
370 void *ib_get_client_data(struct ib_device
*device
, struct ib_client
*client
)
372 struct ib_client_data
*context
;
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
) {
382 spin_unlock_irqrestore(&device
->client_data_lock
, flags
);
386 EXPORT_SYMBOL(ib_get_client_data
);
389 * ib_set_client_data - Set IB client context
390 * @device:Device to set context for
391 * @client:Client to set context for
392 * @data:Context to set
394 * ib_set_client_data() sets client context that can be retrieved with
395 * ib_get_client_data().
397 void ib_set_client_data(struct ib_device
*device
, struct ib_client
*client
,
400 struct ib_client_data
*context
;
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
;
410 printk(KERN_WARNING
"No client context found for %s/%s\n",
411 device
->name
, client
->name
);
414 spin_unlock_irqrestore(&device
->client_data_lock
, flags
);
416 EXPORT_SYMBOL(ib_set_client_data
);
419 * ib_register_event_handler - Register an IB event handler
420 * @event_handler:Handler to register
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.
427 int ib_register_event_handler (struct ib_event_handler
*event_handler
)
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
);
438 EXPORT_SYMBOL(ib_register_event_handler
);
441 * ib_unregister_event_handler - Unregister an event handler
442 * @event_handler:Handler to unregister
444 * Unregister an event handler registered with
445 * ib_register_event_handler().
447 int ib_unregister_event_handler(struct ib_event_handler
*event_handler
)
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
);
457 EXPORT_SYMBOL(ib_unregister_event_handler
);
460 * ib_dispatch_event - Dispatch an asynchronous event
461 * @event:Event to dispatch
463 * Low-level drivers must call ib_dispatch_event() to dispatch the
464 * event to all registered event handlers when an asynchronous event
467 void ib_dispatch_event(struct ib_event
*event
)
470 struct ib_event_handler
*handler
;
472 spin_lock_irqsave(&event
->device
->event_handler_lock
, flags
);
474 list_for_each_entry(handler
, &event
->device
->event_handler_list
, list
)
475 handler
->handler(handler
, event
);
477 spin_unlock_irqrestore(&event
->device
->event_handler_lock
, flags
);
479 EXPORT_SYMBOL(ib_dispatch_event
);
482 * ib_query_device - Query IB device attributes
483 * @device:Device to query
484 * @device_attr:Device attributes
486 * ib_query_device() returns the attributes of a device through the
487 * @device_attr pointer.
489 int ib_query_device(struct ib_device
*device
,
490 struct ib_device_attr
*device_attr
)
492 return device
->query_device(device
, device_attr
);
494 EXPORT_SYMBOL(ib_query_device
);
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
502 * ib_query_port() returns the attributes of a port through the
503 * @port_attr pointer.
505 int ib_query_port(struct ib_device
*device
,
507 struct ib_port_attr
*port_attr
)
509 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
512 } else if (port_num
< 1 || port_num
> device
->phys_port_cnt
)
515 return device
->query_port(device
, port_num
, port_attr
);
517 EXPORT_SYMBOL(ib_query_port
);
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
526 * ib_query_gid() fetches the specified GID table entry.
528 int ib_query_gid(struct ib_device
*device
,
529 u8 port_num
, int index
, union ib_gid
*gid
)
531 return device
->query_gid(device
, port_num
, index
, gid
);
533 EXPORT_SYMBOL(ib_query_gid
);
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
542 * ib_query_pkey() fetches the specified P_Key table entry.
544 int ib_query_pkey(struct ib_device
*device
,
545 u8 port_num
, u16 index
, u16
*pkey
)
547 return device
->query_pkey(device
, port_num
, index
, pkey
);
549 EXPORT_SYMBOL(ib_query_pkey
);
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
557 * ib_modify_device() changes a device's attributes as specified by
558 * the @device_modify_mask and @device_modify structure.
560 int ib_modify_device(struct ib_device
*device
,
561 int device_modify_mask
,
562 struct ib_device_modify
*device_modify
)
564 return device
->modify_device(device
, device_modify_mask
,
567 EXPORT_SYMBOL(ib_modify_device
);
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
575 * @port_modify: New attribute values for the port.
577 * ib_modify_port() changes a port's attributes as specified by the
578 * @port_modify_mask and @port_modify structure.
580 int ib_modify_port(struct ib_device
*device
,
581 u8 port_num
, int port_modify_mask
,
582 struct ib_port_modify
*port_modify
)
584 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
587 } else if (port_num
< 1 || port_num
> device
->phys_port_cnt
)
590 return device
->modify_port(device
, port_num
, port_modify_mask
,
593 EXPORT_SYMBOL(ib_modify_port
);
595 static int __init
ib_core_init(void)
599 ret
= ib_sysfs_setup();
601 printk(KERN_WARNING
"Couldn't create InfiniBand device class\n");
603 ret
= ib_cache_setup();
605 printk(KERN_WARNING
"Couldn't set up InfiniBand P_Key/GID cache\n");
612 static void __exit
ib_core_cleanup(void)
616 /* Make sure that any pending umem accounting work is done. */
617 flush_scheduled_work();
620 module_init(ib_core_init
);
621 module_exit(ib_core_cleanup
);