Detach sched.h from mm.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / infiniband / core / device.c
blobbcecf4ddbf00a1e77f41d8fddecbf78ec9813432
1 /*
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
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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
31 * SOFTWARE.
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>
43 #include <linux/workqueue.h>
45 #include "core_priv.h"
47 MODULE_AUTHOR("Roland Dreier");
48 MODULE_DESCRIPTION("core kernel InfiniBand API");
49 MODULE_LICENSE("Dual BSD/GPL");
51 struct ib_client_data {
52 struct list_head list;
53 struct ib_client *client;
54 void * data;
57 static LIST_HEAD(device_list);
58 static LIST_HEAD(client_list);
61 * device_mutex protects access to both device_list and client_list.
62 * There's no real point to using multiple locks or something fancier
63 * like an rwsem: we always access both lists, and we're always
64 * modifying one list or the other list. In any case this is not a
65 * hot path so there's no point in trying to optimize.
67 static DEFINE_MUTEX(device_mutex);
69 static int ib_device_check_mandatory(struct ib_device *device)
71 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
72 static const struct {
73 size_t offset;
74 char *name;
75 } mandatory_table[] = {
76 IB_MANDATORY_FUNC(query_device),
77 IB_MANDATORY_FUNC(query_port),
78 IB_MANDATORY_FUNC(query_pkey),
79 IB_MANDATORY_FUNC(query_gid),
80 IB_MANDATORY_FUNC(alloc_pd),
81 IB_MANDATORY_FUNC(dealloc_pd),
82 IB_MANDATORY_FUNC(create_ah),
83 IB_MANDATORY_FUNC(destroy_ah),
84 IB_MANDATORY_FUNC(create_qp),
85 IB_MANDATORY_FUNC(modify_qp),
86 IB_MANDATORY_FUNC(destroy_qp),
87 IB_MANDATORY_FUNC(post_send),
88 IB_MANDATORY_FUNC(post_recv),
89 IB_MANDATORY_FUNC(create_cq),
90 IB_MANDATORY_FUNC(destroy_cq),
91 IB_MANDATORY_FUNC(poll_cq),
92 IB_MANDATORY_FUNC(req_notify_cq),
93 IB_MANDATORY_FUNC(get_dma_mr),
94 IB_MANDATORY_FUNC(dereg_mr)
96 int i;
98 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
99 if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
100 printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
101 device->name, mandatory_table[i].name);
102 return -EINVAL;
106 return 0;
109 static struct ib_device *__ib_device_get_by_name(const char *name)
111 struct ib_device *device;
113 list_for_each_entry(device, &device_list, core_list)
114 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
115 return device;
117 return NULL;
121 static int alloc_name(char *name)
123 long *inuse;
124 char buf[IB_DEVICE_NAME_MAX];
125 struct ib_device *device;
126 int i;
128 inuse = (long *) get_zeroed_page(GFP_KERNEL);
129 if (!inuse)
130 return -ENOMEM;
132 list_for_each_entry(device, &device_list, core_list) {
133 if (!sscanf(device->name, name, &i))
134 continue;
135 if (i < 0 || i >= PAGE_SIZE * 8)
136 continue;
137 snprintf(buf, sizeof buf, name, i);
138 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
139 set_bit(i, inuse);
142 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
143 free_page((unsigned long) inuse);
144 snprintf(buf, sizeof buf, name, i);
146 if (__ib_device_get_by_name(buf))
147 return -ENFILE;
149 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
150 return 0;
154 * ib_alloc_device - allocate an IB device struct
155 * @size:size of structure to allocate
157 * Low-level drivers should use ib_alloc_device() to allocate &struct
158 * ib_device. @size is the size of the structure to be allocated,
159 * including any private data used by the low-level driver.
160 * ib_dealloc_device() must be used to free structures allocated with
161 * ib_alloc_device().
163 struct ib_device *ib_alloc_device(size_t size)
165 BUG_ON(size < sizeof (struct ib_device));
167 return kzalloc(size, GFP_KERNEL);
169 EXPORT_SYMBOL(ib_alloc_device);
172 * ib_dealloc_device - free an IB device struct
173 * @device:structure to free
175 * Free a structure allocated with ib_alloc_device().
177 void ib_dealloc_device(struct ib_device *device)
179 if (device->reg_state == IB_DEV_UNINITIALIZED) {
180 kfree(device);
181 return;
184 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
186 ib_device_unregister_sysfs(device);
188 EXPORT_SYMBOL(ib_dealloc_device);
190 static int add_client_context(struct ib_device *device, struct ib_client *client)
192 struct ib_client_data *context;
193 unsigned long flags;
195 context = kmalloc(sizeof *context, GFP_KERNEL);
196 if (!context) {
197 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
198 device->name, client->name);
199 return -ENOMEM;
202 context->client = client;
203 context->data = NULL;
205 spin_lock_irqsave(&device->client_data_lock, flags);
206 list_add(&context->list, &device->client_data_list);
207 spin_unlock_irqrestore(&device->client_data_lock, flags);
209 return 0;
213 * ib_register_device - Register an IB device with IB core
214 * @device:Device to register
216 * Low-level drivers use ib_register_device() to register their
217 * devices with the IB core. All registered clients will receive a
218 * callback for each device that is added. @device must be allocated
219 * with ib_alloc_device().
221 int ib_register_device(struct ib_device *device)
223 int ret;
225 mutex_lock(&device_mutex);
227 if (strchr(device->name, '%')) {
228 ret = alloc_name(device->name);
229 if (ret)
230 goto out;
233 if (ib_device_check_mandatory(device)) {
234 ret = -EINVAL;
235 goto out;
238 INIT_LIST_HEAD(&device->event_handler_list);
239 INIT_LIST_HEAD(&device->client_data_list);
240 spin_lock_init(&device->event_handler_lock);
241 spin_lock_init(&device->client_data_lock);
243 ret = ib_device_register_sysfs(device);
244 if (ret) {
245 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
246 device->name);
247 goto out;
250 list_add_tail(&device->core_list, &device_list);
252 device->reg_state = IB_DEV_REGISTERED;
255 struct ib_client *client;
257 list_for_each_entry(client, &client_list, list)
258 if (client->add && !add_client_context(device, client))
259 client->add(device);
262 out:
263 mutex_unlock(&device_mutex);
264 return ret;
266 EXPORT_SYMBOL(ib_register_device);
269 * ib_unregister_device - Unregister an IB device
270 * @device:Device to unregister
272 * Unregister an IB device. All clients will receive a remove callback.
274 void ib_unregister_device(struct ib_device *device)
276 struct ib_client *client;
277 struct ib_client_data *context, *tmp;
278 unsigned long flags;
280 mutex_lock(&device_mutex);
282 list_for_each_entry_reverse(client, &client_list, list)
283 if (client->remove)
284 client->remove(device);
286 list_del(&device->core_list);
288 mutex_unlock(&device_mutex);
290 spin_lock_irqsave(&device->client_data_lock, flags);
291 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
292 kfree(context);
293 spin_unlock_irqrestore(&device->client_data_lock, flags);
295 device->reg_state = IB_DEV_UNREGISTERED;
297 EXPORT_SYMBOL(ib_unregister_device);
300 * ib_register_client - Register an IB client
301 * @client:Client to register
303 * Upper level users of the IB drivers can use ib_register_client() to
304 * register callbacks for IB device addition and removal. When an IB
305 * device is added, each registered client's add method will be called
306 * (in the order the clients were registered), and when a device is
307 * removed, each client's remove method will be called (in the reverse
308 * order that clients were registered). In addition, when
309 * ib_register_client() is called, the client will receive an add
310 * callback for all devices already registered.
312 int ib_register_client(struct ib_client *client)
314 struct ib_device *device;
316 mutex_lock(&device_mutex);
318 list_add_tail(&client->list, &client_list);
319 list_for_each_entry(device, &device_list, core_list)
320 if (client->add && !add_client_context(device, client))
321 client->add(device);
323 mutex_unlock(&device_mutex);
325 return 0;
327 EXPORT_SYMBOL(ib_register_client);
330 * ib_unregister_client - Unregister an IB client
331 * @client:Client to unregister
333 * Upper level users use ib_unregister_client() to remove their client
334 * registration. When ib_unregister_client() is called, the client
335 * will receive a remove callback for each IB device still registered.
337 void ib_unregister_client(struct ib_client *client)
339 struct ib_client_data *context, *tmp;
340 struct ib_device *device;
341 unsigned long flags;
343 mutex_lock(&device_mutex);
345 list_for_each_entry(device, &device_list, core_list) {
346 if (client->remove)
347 client->remove(device);
349 spin_lock_irqsave(&device->client_data_lock, flags);
350 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
351 if (context->client == client) {
352 list_del(&context->list);
353 kfree(context);
355 spin_unlock_irqrestore(&device->client_data_lock, flags);
357 list_del(&client->list);
359 mutex_unlock(&device_mutex);
361 EXPORT_SYMBOL(ib_unregister_client);
364 * ib_get_client_data - Get IB client context
365 * @device:Device to get context for
366 * @client:Client to get context for
368 * ib_get_client_data() returns client context set with
369 * ib_set_client_data().
371 void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
373 struct ib_client_data *context;
374 void *ret = NULL;
375 unsigned long flags;
377 spin_lock_irqsave(&device->client_data_lock, flags);
378 list_for_each_entry(context, &device->client_data_list, list)
379 if (context->client == client) {
380 ret = context->data;
381 break;
383 spin_unlock_irqrestore(&device->client_data_lock, flags);
385 return ret;
387 EXPORT_SYMBOL(ib_get_client_data);
390 * ib_set_client_data - Set IB client context
391 * @device:Device to set context for
392 * @client:Client to set context for
393 * @data:Context to set
395 * ib_set_client_data() sets client context that can be retrieved with
396 * ib_get_client_data().
398 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
399 void *data)
401 struct ib_client_data *context;
402 unsigned long flags;
404 spin_lock_irqsave(&device->client_data_lock, flags);
405 list_for_each_entry(context, &device->client_data_list, list)
406 if (context->client == client) {
407 context->data = data;
408 goto out;
411 printk(KERN_WARNING "No client context found for %s/%s\n",
412 device->name, client->name);
414 out:
415 spin_unlock_irqrestore(&device->client_data_lock, flags);
417 EXPORT_SYMBOL(ib_set_client_data);
420 * ib_register_event_handler - Register an IB event handler
421 * @event_handler:Handler to register
423 * ib_register_event_handler() registers an event handler that will be
424 * called back when asynchronous IB events occur (as defined in
425 * chapter 11 of the InfiniBand Architecture Specification). This
426 * callback may occur in interrupt context.
428 int ib_register_event_handler (struct ib_event_handler *event_handler)
430 unsigned long flags;
432 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
433 list_add_tail(&event_handler->list,
434 &event_handler->device->event_handler_list);
435 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
437 return 0;
439 EXPORT_SYMBOL(ib_register_event_handler);
442 * ib_unregister_event_handler - Unregister an event handler
443 * @event_handler:Handler to unregister
445 * Unregister an event handler registered with
446 * ib_register_event_handler().
448 int ib_unregister_event_handler(struct ib_event_handler *event_handler)
450 unsigned long flags;
452 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
453 list_del(&event_handler->list);
454 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
456 return 0;
458 EXPORT_SYMBOL(ib_unregister_event_handler);
461 * ib_dispatch_event - Dispatch an asynchronous event
462 * @event:Event to dispatch
464 * Low-level drivers must call ib_dispatch_event() to dispatch the
465 * event to all registered event handlers when an asynchronous event
466 * occurs.
468 void ib_dispatch_event(struct ib_event *event)
470 unsigned long flags;
471 struct ib_event_handler *handler;
473 spin_lock_irqsave(&event->device->event_handler_lock, flags);
475 list_for_each_entry(handler, &event->device->event_handler_list, list)
476 handler->handler(handler, event);
478 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
480 EXPORT_SYMBOL(ib_dispatch_event);
483 * ib_query_device - Query IB device attributes
484 * @device:Device to query
485 * @device_attr:Device attributes
487 * ib_query_device() returns the attributes of a device through the
488 * @device_attr pointer.
490 int ib_query_device(struct ib_device *device,
491 struct ib_device_attr *device_attr)
493 return device->query_device(device, device_attr);
495 EXPORT_SYMBOL(ib_query_device);
498 * ib_query_port - Query IB port attributes
499 * @device:Device to query
500 * @port_num:Port number to query
501 * @port_attr:Port attributes
503 * ib_query_port() returns the attributes of a port through the
504 * @port_attr pointer.
506 int ib_query_port(struct ib_device *device,
507 u8 port_num,
508 struct ib_port_attr *port_attr)
510 if (device->node_type == RDMA_NODE_IB_SWITCH) {
511 if (port_num)
512 return -EINVAL;
513 } else if (port_num < 1 || port_num > device->phys_port_cnt)
514 return -EINVAL;
516 return device->query_port(device, port_num, port_attr);
518 EXPORT_SYMBOL(ib_query_port);
521 * ib_query_gid - Get GID table entry
522 * @device:Device to query
523 * @port_num:Port number to query
524 * @index:GID table index to query
525 * @gid:Returned GID
527 * ib_query_gid() fetches the specified GID table entry.
529 int ib_query_gid(struct ib_device *device,
530 u8 port_num, int index, union ib_gid *gid)
532 return device->query_gid(device, port_num, index, gid);
534 EXPORT_SYMBOL(ib_query_gid);
537 * ib_query_pkey - Get P_Key table entry
538 * @device:Device to query
539 * @port_num:Port number to query
540 * @index:P_Key table index to query
541 * @pkey:Returned P_Key
543 * ib_query_pkey() fetches the specified P_Key table entry.
545 int ib_query_pkey(struct ib_device *device,
546 u8 port_num, u16 index, u16 *pkey)
548 return device->query_pkey(device, port_num, index, pkey);
550 EXPORT_SYMBOL(ib_query_pkey);
553 * ib_modify_device - Change IB device attributes
554 * @device:Device to modify
555 * @device_modify_mask:Mask of attributes to change
556 * @device_modify:New attribute values
558 * ib_modify_device() changes a device's attributes as specified by
559 * the @device_modify_mask and @device_modify structure.
561 int ib_modify_device(struct ib_device *device,
562 int device_modify_mask,
563 struct ib_device_modify *device_modify)
565 return device->modify_device(device, device_modify_mask,
566 device_modify);
568 EXPORT_SYMBOL(ib_modify_device);
571 * ib_modify_port - Modifies the attributes for the specified port.
572 * @device: The device to modify.
573 * @port_num: The number of the port to modify.
574 * @port_modify_mask: Mask used to specify which attributes of the port
575 * to change.
576 * @port_modify: New attribute values for the port.
578 * ib_modify_port() changes a port's attributes as specified by the
579 * @port_modify_mask and @port_modify structure.
581 int ib_modify_port(struct ib_device *device,
582 u8 port_num, int port_modify_mask,
583 struct ib_port_modify *port_modify)
585 if (device->node_type == RDMA_NODE_IB_SWITCH) {
586 if (port_num)
587 return -EINVAL;
588 } else if (port_num < 1 || port_num > device->phys_port_cnt)
589 return -EINVAL;
591 return device->modify_port(device, port_num, port_modify_mask,
592 port_modify);
594 EXPORT_SYMBOL(ib_modify_port);
596 static int __init ib_core_init(void)
598 int ret;
600 ret = ib_sysfs_setup();
601 if (ret)
602 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
604 ret = ib_cache_setup();
605 if (ret) {
606 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
607 ib_sysfs_cleanup();
610 return ret;
613 static void __exit ib_core_cleanup(void)
615 ib_cache_cleanup();
616 ib_sysfs_cleanup();
617 /* Make sure that any pending umem accounting work is done. */
618 flush_scheduled_work();
621 module_init(ib_core_init);
622 module_exit(ib_core_cleanup);