Detach sched.h from mm.h
[deliverable/linux.git] / drivers / infiniband / core / device.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
2a1d9b7f 3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
1da177e4
LT
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>
9a6b090c 39#include <linux/kernel.h>
1da177e4
LT
40#include <linux/slab.h>
41#include <linux/init.h>
95ed644f 42#include <linux/mutex.h>
e8edc6e0 43#include <linux/workqueue.h>
1da177e4
LT
44
45#include "core_priv.h"
46
47MODULE_AUTHOR("Roland Dreier");
48MODULE_DESCRIPTION("core kernel InfiniBand API");
49MODULE_LICENSE("Dual BSD/GPL");
50
51struct ib_client_data {
52 struct list_head list;
53 struct ib_client *client;
54 void * data;
55};
56
57static LIST_HEAD(device_list);
58static LIST_HEAD(client_list);
59
60/*
95ed644f 61 * device_mutex protects access to both device_list and client_list.
1da177e4
LT
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.
66 */
95ed644f 67static DEFINE_MUTEX(device_mutex);
1da177e4
LT
68
69static int ib_device_check_mandatory(struct ib_device *device)
70{
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)
95 };
96 int i;
97
9a6b090c 98 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
1da177e4
LT
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;
103 }
104 }
105
106 return 0;
107}
108
109static struct ib_device *__ib_device_get_by_name(const char *name)
110{
111 struct ib_device *device;
112
113 list_for_each_entry(device, &device_list, core_list)
114 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
115 return device;
116
117 return NULL;
118}
119
120
121static int alloc_name(char *name)
122{
123 long *inuse;
124 char buf[IB_DEVICE_NAME_MAX];
125 struct ib_device *device;
126 int i;
127
128 inuse = (long *) get_zeroed_page(GFP_KERNEL);
129 if (!inuse)
130 return -ENOMEM;
131
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);
140 }
141
142 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
143 free_page((unsigned long) inuse);
144 snprintf(buf, sizeof buf, name, i);
145
146 if (__ib_device_get_by_name(buf))
147 return -ENFILE;
148
149 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
150 return 0;
151}
152
153/**
154 * ib_alloc_device - allocate an IB device struct
155 * @size:size of structure to allocate
156 *
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().
162 */
163struct ib_device *ib_alloc_device(size_t size)
164{
1da177e4
LT
165 BUG_ON(size < sizeof (struct ib_device));
166
de6eb66b 167 return kzalloc(size, GFP_KERNEL);
1da177e4
LT
168}
169EXPORT_SYMBOL(ib_alloc_device);
170
171/**
172 * ib_dealloc_device - free an IB device struct
173 * @device:structure to free
174 *
175 * Free a structure allocated with ib_alloc_device().
176 */
177void ib_dealloc_device(struct ib_device *device)
178{
179 if (device->reg_state == IB_DEV_UNINITIALIZED) {
180 kfree(device);
181 return;
182 }
183
184 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
185
186 ib_device_unregister_sysfs(device);
187}
188EXPORT_SYMBOL(ib_dealloc_device);
189
190static int add_client_context(struct ib_device *device, struct ib_client *client)
191{
192 struct ib_client_data *context;
193 unsigned long flags;
194
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;
200 }
201
202 context->client = client;
203 context->data = NULL;
204
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);
208
209 return 0;
210}
211
212/**
213 * ib_register_device - Register an IB device with IB core
214 * @device:Device to register
215 *
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().
220 */
221int ib_register_device(struct ib_device *device)
222{
223 int ret;
224
95ed644f 225 mutex_lock(&device_mutex);
1da177e4
LT
226
227 if (strchr(device->name, '%')) {
228 ret = alloc_name(device->name);
229 if (ret)
230 goto out;
231 }
232
233 if (ib_device_check_mandatory(device)) {
234 ret = -EINVAL;
235 goto out;
236 }
237
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);
242
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;
248 }
249
250 list_add_tail(&device->core_list, &device_list);
251
252 device->reg_state = IB_DEV_REGISTERED;
253
254 {
255 struct ib_client *client;
256
257 list_for_each_entry(client, &client_list, list)
258 if (client->add && !add_client_context(device, client))
259 client->add(device);
260 }
261
262 out:
95ed644f 263 mutex_unlock(&device_mutex);
1da177e4
LT
264 return ret;
265}
266EXPORT_SYMBOL(ib_register_device);
267
268/**
269 * ib_unregister_device - Unregister an IB device
270 * @device:Device to unregister
271 *
272 * Unregister an IB device. All clients will receive a remove callback.
273 */
274void ib_unregister_device(struct ib_device *device)
275{
276 struct ib_client *client;
277 struct ib_client_data *context, *tmp;
278 unsigned long flags;
279
95ed644f 280 mutex_lock(&device_mutex);
1da177e4
LT
281
282 list_for_each_entry_reverse(client, &client_list, list)
283 if (client->remove)
284 client->remove(device);
285
286 list_del(&device->core_list);
287
95ed644f 288 mutex_unlock(&device_mutex);
1da177e4
LT
289
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);
294
295 device->reg_state = IB_DEV_UNREGISTERED;
296}
297EXPORT_SYMBOL(ib_unregister_device);
298
299/**
300 * ib_register_client - Register an IB client
301 * @client:Client to register
302 *
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.
311 */
312int ib_register_client(struct ib_client *client)
313{
314 struct ib_device *device;
315
95ed644f 316 mutex_lock(&device_mutex);
1da177e4
LT
317
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);
322
95ed644f 323 mutex_unlock(&device_mutex);
1da177e4
LT
324
325 return 0;
326}
327EXPORT_SYMBOL(ib_register_client);
328
329/**
330 * ib_unregister_client - Unregister an IB client
331 * @client:Client to unregister
332 *
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.
336 */
337void ib_unregister_client(struct ib_client *client)
338{
339 struct ib_client_data *context, *tmp;
340 struct ib_device *device;
341 unsigned long flags;
342
95ed644f 343 mutex_lock(&device_mutex);
1da177e4
LT
344
345 list_for_each_entry(device, &device_list, core_list) {
346 if (client->remove)
347 client->remove(device);
348
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);
354 }
355 spin_unlock_irqrestore(&device->client_data_lock, flags);
356 }
357 list_del(&client->list);
358
95ed644f 359 mutex_unlock(&device_mutex);
1da177e4
LT
360}
361EXPORT_SYMBOL(ib_unregister_client);
362
363/**
364 * ib_get_client_data - Get IB client context
365 * @device:Device to get context for
366 * @client:Client to get context for
367 *
368 * ib_get_client_data() returns client context set with
369 * ib_set_client_data().
370 */
371void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
372{
373 struct ib_client_data *context;
374 void *ret = NULL;
375 unsigned long flags;
376
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;
382 }
383 spin_unlock_irqrestore(&device->client_data_lock, flags);
384
385 return ret;
386}
387EXPORT_SYMBOL(ib_get_client_data);
388
389/**
9cd330d3 390 * ib_set_client_data - Set IB client context
1da177e4
LT
391 * @device:Device to set context for
392 * @client:Client to set context for
393 * @data:Context to set
394 *
395 * ib_set_client_data() sets client context that can be retrieved with
396 * ib_get_client_data().
397 */
398void ib_set_client_data(struct ib_device *device, struct ib_client *client,
399 void *data)
400{
401 struct ib_client_data *context;
402 unsigned long flags;
403
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;
409 }
410
411 printk(KERN_WARNING "No client context found for %s/%s\n",
412 device->name, client->name);
413
414out:
415 spin_unlock_irqrestore(&device->client_data_lock, flags);
416}
417EXPORT_SYMBOL(ib_set_client_data);
418
419/**
420 * ib_register_event_handler - Register an IB event handler
421 * @event_handler:Handler to register
422 *
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.
427 */
428int ib_register_event_handler (struct ib_event_handler *event_handler)
429{
430 unsigned long flags;
431
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);
436
437 return 0;
438}
439EXPORT_SYMBOL(ib_register_event_handler);
440
441/**
442 * ib_unregister_event_handler - Unregister an event handler
443 * @event_handler:Handler to unregister
444 *
445 * Unregister an event handler registered with
446 * ib_register_event_handler().
447 */
448int ib_unregister_event_handler(struct ib_event_handler *event_handler)
449{
450 unsigned long flags;
451
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);
455
456 return 0;
457}
458EXPORT_SYMBOL(ib_unregister_event_handler);
459
460/**
461 * ib_dispatch_event - Dispatch an asynchronous event
462 * @event:Event to dispatch
463 *
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.
467 */
468void ib_dispatch_event(struct ib_event *event)
469{
470 unsigned long flags;
471 struct ib_event_handler *handler;
472
473 spin_lock_irqsave(&event->device->event_handler_lock, flags);
474
475 list_for_each_entry(handler, &event->device->event_handler_list, list)
476 handler->handler(handler, event);
477
478 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
479}
480EXPORT_SYMBOL(ib_dispatch_event);
481
482/**
483 * ib_query_device - Query IB device attributes
484 * @device:Device to query
485 * @device_attr:Device attributes
486 *
487 * ib_query_device() returns the attributes of a device through the
488 * @device_attr pointer.
489 */
490int ib_query_device(struct ib_device *device,
491 struct ib_device_attr *device_attr)
492{
493 return device->query_device(device, device_attr);
494}
495EXPORT_SYMBOL(ib_query_device);
496
497/**
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
502 *
503 * ib_query_port() returns the attributes of a port through the
504 * @port_attr pointer.
505 */
506int ib_query_port(struct ib_device *device,
507 u8 port_num,
508 struct ib_port_attr *port_attr)
509{
07ebafba 510 if (device->node_type == RDMA_NODE_IB_SWITCH) {
116c0074
RD
511 if (port_num)
512 return -EINVAL;
513 } else if (port_num < 1 || port_num > device->phys_port_cnt)
514 return -EINVAL;
515
1da177e4
LT
516 return device->query_port(device, port_num, port_attr);
517}
518EXPORT_SYMBOL(ib_query_port);
519
520/**
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
526 *
527 * ib_query_gid() fetches the specified GID table entry.
528 */
529int ib_query_gid(struct ib_device *device,
530 u8 port_num, int index, union ib_gid *gid)
531{
532 return device->query_gid(device, port_num, index, gid);
533}
534EXPORT_SYMBOL(ib_query_gid);
535
536/**
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
542 *
543 * ib_query_pkey() fetches the specified P_Key table entry.
544 */
545int ib_query_pkey(struct ib_device *device,
546 u8 port_num, u16 index, u16 *pkey)
547{
548 return device->query_pkey(device, port_num, index, pkey);
549}
550EXPORT_SYMBOL(ib_query_pkey);
551
552/**
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
557 *
558 * ib_modify_device() changes a device's attributes as specified by
559 * the @device_modify_mask and @device_modify structure.
560 */
561int ib_modify_device(struct ib_device *device,
562 int device_modify_mask,
563 struct ib_device_modify *device_modify)
564{
565 return device->modify_device(device, device_modify_mask,
566 device_modify);
567}
568EXPORT_SYMBOL(ib_modify_device);
569
570/**
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.
577 *
578 * ib_modify_port() changes a port's attributes as specified by the
579 * @port_modify_mask and @port_modify structure.
580 */
581int ib_modify_port(struct ib_device *device,
582 u8 port_num, int port_modify_mask,
583 struct ib_port_modify *port_modify)
584{
07ebafba 585 if (device->node_type == RDMA_NODE_IB_SWITCH) {
116c0074
RD
586 if (port_num)
587 return -EINVAL;
588 } else if (port_num < 1 || port_num > device->phys_port_cnt)
589 return -EINVAL;
590
1da177e4
LT
591 return device->modify_port(device, port_num, port_modify_mask,
592 port_modify);
593}
594EXPORT_SYMBOL(ib_modify_port);
595
596static int __init ib_core_init(void)
597{
598 int ret;
599
600 ret = ib_sysfs_setup();
601 if (ret)
602 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
603
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();
608 }
609
610 return ret;
611}
612
613static void __exit ib_core_cleanup(void)
614{
615 ib_cache_cleanup();
616 ib_sysfs_cleanup();
f7c6a7b5
RD
617 /* Make sure that any pending umem accounting work is done. */
618 flush_scheduled_work();
1da177e4
LT
619}
620
621module_init(ib_core_init);
622module_exit(ib_core_cleanup);
This page took 0.222631 seconds and 5 git commands to generate.