RDMA/ocrdma: Incorporate the moving of GID Table mgmt to IB/Core
[linux-block.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.
1da177e4
LT
32 */
33
34#include <linux/module.h>
35#include <linux/string.h>
36#include <linux/errno.h>
9a6b090c 37#include <linux/kernel.h>
1da177e4
LT
38#include <linux/slab.h>
39#include <linux/init.h>
95ed644f 40#include <linux/mutex.h>
9268f72d 41#include <linux/netdevice.h>
b2cbae2c 42#include <rdma/rdma_netlink.h>
03db3a2d
MB
43#include <rdma/ib_addr.h>
44#include <rdma/ib_cache.h>
1da177e4
LT
45
46#include "core_priv.h"
47
48MODULE_AUTHOR("Roland Dreier");
49MODULE_DESCRIPTION("core kernel InfiniBand API");
50MODULE_LICENSE("Dual BSD/GPL");
51
52struct ib_client_data {
53 struct list_head list;
54 struct ib_client *client;
55 void * data;
7c1eb45a
HE
56 /* The device or client is going down. Do not call client or device
57 * callbacks other than remove(). */
58 bool going_down;
1da177e4
LT
59};
60
f0626710
TH
61struct workqueue_struct *ib_wq;
62EXPORT_SYMBOL_GPL(ib_wq);
63
5aa44bb9
HE
64/* The device_list and client_list contain devices and clients after their
65 * registration has completed, and the devices and clients are removed
66 * during unregistration. */
1da177e4
LT
67static LIST_HEAD(device_list);
68static LIST_HEAD(client_list);
69
70/*
5aa44bb9
HE
71 * device_mutex and lists_rwsem protect access to both device_list and
72 * client_list. device_mutex protects writer access by device and client
73 * registration / de-registration. lists_rwsem protects reader access to
74 * these lists. Iterators of these lists must lock it for read, while updates
75 * to the lists must be done with a write lock. A special case is when the
76 * device_mutex is locked. In this case locking the lists for read access is
77 * not necessary as the device_mutex implies it.
7c1eb45a
HE
78 *
79 * lists_rwsem also protects access to the client data list.
1da177e4 80 */
95ed644f 81static DEFINE_MUTEX(device_mutex);
5aa44bb9
HE
82static DECLARE_RWSEM(lists_rwsem);
83
1da177e4
LT
84
85static int ib_device_check_mandatory(struct ib_device *device)
86{
87#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
88 static const struct {
89 size_t offset;
90 char *name;
91 } mandatory_table[] = {
92 IB_MANDATORY_FUNC(query_device),
93 IB_MANDATORY_FUNC(query_port),
94 IB_MANDATORY_FUNC(query_pkey),
95 IB_MANDATORY_FUNC(query_gid),
96 IB_MANDATORY_FUNC(alloc_pd),
97 IB_MANDATORY_FUNC(dealloc_pd),
98 IB_MANDATORY_FUNC(create_ah),
99 IB_MANDATORY_FUNC(destroy_ah),
100 IB_MANDATORY_FUNC(create_qp),
101 IB_MANDATORY_FUNC(modify_qp),
102 IB_MANDATORY_FUNC(destroy_qp),
103 IB_MANDATORY_FUNC(post_send),
104 IB_MANDATORY_FUNC(post_recv),
105 IB_MANDATORY_FUNC(create_cq),
106 IB_MANDATORY_FUNC(destroy_cq),
107 IB_MANDATORY_FUNC(poll_cq),
108 IB_MANDATORY_FUNC(req_notify_cq),
109 IB_MANDATORY_FUNC(get_dma_mr),
7738613e
IW
110 IB_MANDATORY_FUNC(dereg_mr),
111 IB_MANDATORY_FUNC(get_port_immutable)
1da177e4
LT
112 };
113 int i;
114
9a6b090c 115 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
1da177e4
LT
116 if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
117 printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
118 device->name, mandatory_table[i].name);
119 return -EINVAL;
120 }
121 }
122
123 return 0;
124}
125
126static struct ib_device *__ib_device_get_by_name(const char *name)
127{
128 struct ib_device *device;
129
130 list_for_each_entry(device, &device_list, core_list)
131 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
132 return device;
133
134 return NULL;
135}
136
137
138static int alloc_name(char *name)
139{
65d470b3 140 unsigned long *inuse;
1da177e4
LT
141 char buf[IB_DEVICE_NAME_MAX];
142 struct ib_device *device;
143 int i;
144
65d470b3 145 inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
1da177e4
LT
146 if (!inuse)
147 return -ENOMEM;
148
149 list_for_each_entry(device, &device_list, core_list) {
150 if (!sscanf(device->name, name, &i))
151 continue;
152 if (i < 0 || i >= PAGE_SIZE * 8)
153 continue;
154 snprintf(buf, sizeof buf, name, i);
155 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
156 set_bit(i, inuse);
157 }
158
159 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
160 free_page((unsigned long) inuse);
161 snprintf(buf, sizeof buf, name, i);
162
163 if (__ib_device_get_by_name(buf))
164 return -ENFILE;
165
166 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
167 return 0;
168}
169
55aeed06
JG
170static void ib_device_release(struct device *device)
171{
172 struct ib_device *dev = container_of(device, struct ib_device, dev);
173
03db3a2d 174 ib_cache_release_one(dev);
55aeed06
JG
175 kfree(dev->port_immutable);
176 kfree(dev);
177}
178
179static int ib_device_uevent(struct device *device,
180 struct kobj_uevent_env *env)
181{
182 struct ib_device *dev = container_of(device, struct ib_device, dev);
183
184 if (add_uevent_var(env, "NAME=%s", dev->name))
185 return -ENOMEM;
186
187 /*
188 * It would be nice to pass the node GUID with the event...
189 */
190
191 return 0;
192}
193
194static struct class ib_class = {
195 .name = "infiniband",
196 .dev_release = ib_device_release,
197 .dev_uevent = ib_device_uevent,
198};
199
1da177e4
LT
200/**
201 * ib_alloc_device - allocate an IB device struct
202 * @size:size of structure to allocate
203 *
204 * Low-level drivers should use ib_alloc_device() to allocate &struct
205 * ib_device. @size is the size of the structure to be allocated,
206 * including any private data used by the low-level driver.
207 * ib_dealloc_device() must be used to free structures allocated with
208 * ib_alloc_device().
209 */
210struct ib_device *ib_alloc_device(size_t size)
211{
55aeed06
JG
212 struct ib_device *device;
213
214 if (WARN_ON(size < sizeof(struct ib_device)))
215 return NULL;
216
217 device = kzalloc(size, GFP_KERNEL);
218 if (!device)
219 return NULL;
220
221 device->dev.class = &ib_class;
222 device_initialize(&device->dev);
223
224 dev_set_drvdata(&device->dev, device);
225
226 INIT_LIST_HEAD(&device->event_handler_list);
227 spin_lock_init(&device->event_handler_lock);
228 spin_lock_init(&device->client_data_lock);
229 INIT_LIST_HEAD(&device->client_data_list);
230 INIT_LIST_HEAD(&device->port_list);
1da177e4 231
55aeed06 232 return device;
1da177e4
LT
233}
234EXPORT_SYMBOL(ib_alloc_device);
235
236/**
237 * ib_dealloc_device - free an IB device struct
238 * @device:structure to free
239 *
240 * Free a structure allocated with ib_alloc_device().
241 */
242void ib_dealloc_device(struct ib_device *device)
243{
55aeed06
JG
244 WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
245 device->reg_state != IB_DEV_UNINITIALIZED);
9206dff1 246 kobject_put(&device->dev.kobj);
1da177e4
LT
247}
248EXPORT_SYMBOL(ib_dealloc_device);
249
250static int add_client_context(struct ib_device *device, struct ib_client *client)
251{
252 struct ib_client_data *context;
253 unsigned long flags;
254
255 context = kmalloc(sizeof *context, GFP_KERNEL);
256 if (!context) {
257 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
258 device->name, client->name);
259 return -ENOMEM;
260 }
261
262 context->client = client;
263 context->data = NULL;
7c1eb45a 264 context->going_down = false;
1da177e4 265
7c1eb45a 266 down_write(&lists_rwsem);
1da177e4
LT
267 spin_lock_irqsave(&device->client_data_lock, flags);
268 list_add(&context->list, &device->client_data_list);
269 spin_unlock_irqrestore(&device->client_data_lock, flags);
7c1eb45a 270 up_write(&lists_rwsem);
1da177e4
LT
271
272 return 0;
273}
274
337877a4
IW
275static int verify_immutable(const struct ib_device *dev, u8 port)
276{
277 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
278 rdma_max_mad_size(dev, port) != 0);
279}
280
7738613e 281static int read_port_immutable(struct ib_device *device)
5eb620c8 282{
55aeed06 283 int ret;
7738613e
IW
284 u8 start_port = rdma_start_port(device);
285 u8 end_port = rdma_end_port(device);
286 u8 port;
287
288 /**
289 * device->port_immutable is indexed directly by the port number to make
290 * access to this data as efficient as possible.
291 *
292 * Therefore port_immutable is declared as a 1 based array with
293 * potential empty slots at the beginning.
294 */
295 device->port_immutable = kzalloc(sizeof(*device->port_immutable)
296 * (end_port + 1),
297 GFP_KERNEL);
298 if (!device->port_immutable)
55aeed06 299 return -ENOMEM;
5eb620c8 300
7738613e
IW
301 for (port = start_port; port <= end_port; ++port) {
302 ret = device->get_port_immutable(device, port,
303 &device->port_immutable[port]);
5eb620c8 304 if (ret)
55aeed06 305 return ret;
337877a4 306
55aeed06
JG
307 if (verify_immutable(device, port))
308 return -EINVAL;
5eb620c8 309 }
55aeed06 310 return 0;
5eb620c8
YE
311}
312
1da177e4
LT
313/**
314 * ib_register_device - Register an IB device with IB core
315 * @device:Device to register
316 *
317 * Low-level drivers use ib_register_device() to register their
318 * devices with the IB core. All registered clients will receive a
319 * callback for each device that is added. @device must be allocated
320 * with ib_alloc_device().
321 */
9a6edb60
RC
322int ib_register_device(struct ib_device *device,
323 int (*port_callback)(struct ib_device *,
324 u8, struct kobject *))
1da177e4
LT
325{
326 int ret;
327
95ed644f 328 mutex_lock(&device_mutex);
1da177e4
LT
329
330 if (strchr(device->name, '%')) {
331 ret = alloc_name(device->name);
332 if (ret)
333 goto out;
334 }
335
336 if (ib_device_check_mandatory(device)) {
337 ret = -EINVAL;
338 goto out;
339 }
340
7738613e 341 ret = read_port_immutable(device);
5eb620c8 342 if (ret) {
7738613e 343 printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
5eb620c8
YE
344 device->name);
345 goto out;
346 }
347
03db3a2d
MB
348 ret = ib_cache_setup_one(device);
349 if (ret) {
350 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
351 goto out;
352 }
353
9a6edb60 354 ret = ib_device_register_sysfs(device, port_callback);
1da177e4
LT
355 if (ret) {
356 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
357 device->name);
03db3a2d 358 ib_cache_cleanup_one(device);
1da177e4
LT
359 goto out;
360 }
361
1da177e4
LT
362 device->reg_state = IB_DEV_REGISTERED;
363
364 {
365 struct ib_client *client;
366
367 list_for_each_entry(client, &client_list, list)
368 if (client->add && !add_client_context(device, client))
369 client->add(device);
370 }
371
5aa44bb9
HE
372 down_write(&lists_rwsem);
373 list_add_tail(&device->core_list, &device_list);
374 up_write(&lists_rwsem);
375out:
95ed644f 376 mutex_unlock(&device_mutex);
1da177e4
LT
377 return ret;
378}
379EXPORT_SYMBOL(ib_register_device);
380
381/**
382 * ib_unregister_device - Unregister an IB device
383 * @device:Device to unregister
384 *
385 * Unregister an IB device. All clients will receive a remove callback.
386 */
387void ib_unregister_device(struct ib_device *device)
388{
1da177e4
LT
389 struct ib_client_data *context, *tmp;
390 unsigned long flags;
391
95ed644f 392 mutex_lock(&device_mutex);
1da177e4 393
5aa44bb9
HE
394 down_write(&lists_rwsem);
395 list_del(&device->core_list);
7c1eb45a
HE
396 spin_lock_irqsave(&device->client_data_lock, flags);
397 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
398 context->going_down = true;
399 spin_unlock_irqrestore(&device->client_data_lock, flags);
400 downgrade_write(&lists_rwsem);
5aa44bb9 401
7c1eb45a
HE
402 list_for_each_entry_safe(context, tmp, &device->client_data_list,
403 list) {
404 if (context->client->remove)
405 context->client->remove(device, context->data);
406 }
407 up_read(&lists_rwsem);
1da177e4 408
95ed644f 409 mutex_unlock(&device_mutex);
1da177e4 410
9206dff1 411 ib_device_unregister_sysfs(device);
03db3a2d 412 ib_cache_cleanup_one(device);
9206dff1 413
7c1eb45a 414 down_write(&lists_rwsem);
1da177e4
LT
415 spin_lock_irqsave(&device->client_data_lock, flags);
416 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
417 kfree(context);
418 spin_unlock_irqrestore(&device->client_data_lock, flags);
7c1eb45a 419 up_write(&lists_rwsem);
1da177e4
LT
420
421 device->reg_state = IB_DEV_UNREGISTERED;
422}
423EXPORT_SYMBOL(ib_unregister_device);
424
425/**
426 * ib_register_client - Register an IB client
427 * @client:Client to register
428 *
429 * Upper level users of the IB drivers can use ib_register_client() to
430 * register callbacks for IB device addition and removal. When an IB
431 * device is added, each registered client's add method will be called
432 * (in the order the clients were registered), and when a device is
433 * removed, each client's remove method will be called (in the reverse
434 * order that clients were registered). In addition, when
435 * ib_register_client() is called, the client will receive an add
436 * callback for all devices already registered.
437 */
438int ib_register_client(struct ib_client *client)
439{
440 struct ib_device *device;
441
95ed644f 442 mutex_lock(&device_mutex);
1da177e4 443
1da177e4
LT
444 list_for_each_entry(device, &device_list, core_list)
445 if (client->add && !add_client_context(device, client))
446 client->add(device);
447
5aa44bb9
HE
448 down_write(&lists_rwsem);
449 list_add_tail(&client->list, &client_list);
450 up_write(&lists_rwsem);
451
95ed644f 452 mutex_unlock(&device_mutex);
1da177e4
LT
453
454 return 0;
455}
456EXPORT_SYMBOL(ib_register_client);
457
458/**
459 * ib_unregister_client - Unregister an IB client
460 * @client:Client to unregister
461 *
462 * Upper level users use ib_unregister_client() to remove their client
463 * registration. When ib_unregister_client() is called, the client
464 * will receive a remove callback for each IB device still registered.
465 */
466void ib_unregister_client(struct ib_client *client)
467{
468 struct ib_client_data *context, *tmp;
469 struct ib_device *device;
470 unsigned long flags;
471
95ed644f 472 mutex_lock(&device_mutex);
1da177e4 473
5aa44bb9
HE
474 down_write(&lists_rwsem);
475 list_del(&client->list);
476 up_write(&lists_rwsem);
477
1da177e4 478 list_for_each_entry(device, &device_list, core_list) {
7c1eb45a 479 struct ib_client_data *found_context = NULL;
1da177e4 480
7c1eb45a 481 down_write(&lists_rwsem);
1da177e4
LT
482 spin_lock_irqsave(&device->client_data_lock, flags);
483 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
484 if (context->client == client) {
7c1eb45a
HE
485 context->going_down = true;
486 found_context = context;
487 break;
1da177e4
LT
488 }
489 spin_unlock_irqrestore(&device->client_data_lock, flags);
7c1eb45a
HE
490 up_write(&lists_rwsem);
491
492 if (client->remove)
493 client->remove(device, found_context ?
494 found_context->data : NULL);
495
496 if (!found_context) {
497 pr_warn("No client context found for %s/%s\n",
498 device->name, client->name);
499 continue;
500 }
501
502 down_write(&lists_rwsem);
503 spin_lock_irqsave(&device->client_data_lock, flags);
504 list_del(&found_context->list);
505 kfree(found_context);
506 spin_unlock_irqrestore(&device->client_data_lock, flags);
507 up_write(&lists_rwsem);
1da177e4 508 }
1da177e4 509
95ed644f 510 mutex_unlock(&device_mutex);
1da177e4
LT
511}
512EXPORT_SYMBOL(ib_unregister_client);
513
514/**
515 * ib_get_client_data - Get IB client context
516 * @device:Device to get context for
517 * @client:Client to get context for
518 *
519 * ib_get_client_data() returns client context set with
520 * ib_set_client_data().
521 */
522void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
523{
524 struct ib_client_data *context;
525 void *ret = NULL;
526 unsigned long flags;
527
528 spin_lock_irqsave(&device->client_data_lock, flags);
529 list_for_each_entry(context, &device->client_data_list, list)
530 if (context->client == client) {
531 ret = context->data;
532 break;
533 }
534 spin_unlock_irqrestore(&device->client_data_lock, flags);
535
536 return ret;
537}
538EXPORT_SYMBOL(ib_get_client_data);
539
540/**
9cd330d3 541 * ib_set_client_data - Set IB client context
1da177e4
LT
542 * @device:Device to set context for
543 * @client:Client to set context for
544 * @data:Context to set
545 *
546 * ib_set_client_data() sets client context that can be retrieved with
547 * ib_get_client_data().
548 */
549void ib_set_client_data(struct ib_device *device, struct ib_client *client,
550 void *data)
551{
552 struct ib_client_data *context;
553 unsigned long flags;
554
555 spin_lock_irqsave(&device->client_data_lock, flags);
556 list_for_each_entry(context, &device->client_data_list, list)
557 if (context->client == client) {
558 context->data = data;
559 goto out;
560 }
561
562 printk(KERN_WARNING "No client context found for %s/%s\n",
563 device->name, client->name);
564
565out:
566 spin_unlock_irqrestore(&device->client_data_lock, flags);
567}
568EXPORT_SYMBOL(ib_set_client_data);
569
570/**
571 * ib_register_event_handler - Register an IB event handler
572 * @event_handler:Handler to register
573 *
574 * ib_register_event_handler() registers an event handler that will be
575 * called back when asynchronous IB events occur (as defined in
576 * chapter 11 of the InfiniBand Architecture Specification). This
577 * callback may occur in interrupt context.
578 */
579int ib_register_event_handler (struct ib_event_handler *event_handler)
580{
581 unsigned long flags;
582
583 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
584 list_add_tail(&event_handler->list,
585 &event_handler->device->event_handler_list);
586 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
587
588 return 0;
589}
590EXPORT_SYMBOL(ib_register_event_handler);
591
592/**
593 * ib_unregister_event_handler - Unregister an event handler
594 * @event_handler:Handler to unregister
595 *
596 * Unregister an event handler registered with
597 * ib_register_event_handler().
598 */
599int ib_unregister_event_handler(struct ib_event_handler *event_handler)
600{
601 unsigned long flags;
602
603 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
604 list_del(&event_handler->list);
605 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
606
607 return 0;
608}
609EXPORT_SYMBOL(ib_unregister_event_handler);
610
611/**
612 * ib_dispatch_event - Dispatch an asynchronous event
613 * @event:Event to dispatch
614 *
615 * Low-level drivers must call ib_dispatch_event() to dispatch the
616 * event to all registered event handlers when an asynchronous event
617 * occurs.
618 */
619void ib_dispatch_event(struct ib_event *event)
620{
621 unsigned long flags;
622 struct ib_event_handler *handler;
623
624 spin_lock_irqsave(&event->device->event_handler_lock, flags);
625
626 list_for_each_entry(handler, &event->device->event_handler_list, list)
627 handler->handler(handler, event);
628
629 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
630}
631EXPORT_SYMBOL(ib_dispatch_event);
632
633/**
634 * ib_query_device - Query IB device attributes
635 * @device:Device to query
636 * @device_attr:Device attributes
637 *
638 * ib_query_device() returns the attributes of a device through the
639 * @device_attr pointer.
640 */
641int ib_query_device(struct ib_device *device,
642 struct ib_device_attr *device_attr)
643{
2528e33e
MB
644 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
645
24306dc6
MB
646 memset(device_attr, 0, sizeof(*device_attr));
647
2528e33e 648 return device->query_device(device, device_attr, &uhw);
1da177e4
LT
649}
650EXPORT_SYMBOL(ib_query_device);
651
652/**
653 * ib_query_port - Query IB port attributes
654 * @device:Device to query
655 * @port_num:Port number to query
656 * @port_attr:Port attributes
657 *
658 * ib_query_port() returns the attributes of a port through the
659 * @port_attr pointer.
660 */
661int ib_query_port(struct ib_device *device,
662 u8 port_num,
663 struct ib_port_attr *port_attr)
664{
0cf18d77 665 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
116c0074
RD
666 return -EINVAL;
667
1da177e4
LT
668 return device->query_port(device, port_num, port_attr);
669}
670EXPORT_SYMBOL(ib_query_port);
671
672/**
673 * ib_query_gid - Get GID table entry
674 * @device:Device to query
675 * @port_num:Port number to query
676 * @index:GID table index to query
677 * @gid:Returned GID
678 *
679 * ib_query_gid() fetches the specified GID table entry.
680 */
681int ib_query_gid(struct ib_device *device,
682 u8 port_num, int index, union ib_gid *gid)
683{
03db3a2d
MB
684 if (rdma_cap_roce_gid_table(device, port_num))
685 return ib_get_cached_gid(device, port_num, index, gid);
686
1da177e4
LT
687 return device->query_gid(device, port_num, index, gid);
688}
689EXPORT_SYMBOL(ib_query_gid);
690
03db3a2d
MB
691/**
692 * ib_enum_roce_netdev - enumerate all RoCE ports
693 * @ib_dev : IB device we want to query
694 * @filter: Should we call the callback?
695 * @filter_cookie: Cookie passed to filter
696 * @cb: Callback to call for each found RoCE ports
697 * @cookie: Cookie passed back to the callback
698 *
699 * Enumerates all of the physical RoCE ports of ib_dev
700 * which are related to netdevice and calls callback() on each
701 * device for which filter() function returns non zero.
702 */
703void ib_enum_roce_netdev(struct ib_device *ib_dev,
704 roce_netdev_filter filter,
705 void *filter_cookie,
706 roce_netdev_callback cb,
707 void *cookie)
708{
709 u8 port;
710
711 for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
712 port++)
713 if (rdma_protocol_roce(ib_dev, port)) {
714 struct net_device *idev = NULL;
715
716 if (ib_dev->get_netdev)
717 idev = ib_dev->get_netdev(ib_dev, port);
718
719 if (idev &&
720 idev->reg_state >= NETREG_UNREGISTERED) {
721 dev_put(idev);
722 idev = NULL;
723 }
724
725 if (filter(ib_dev, port, idev, filter_cookie))
726 cb(ib_dev, port, idev, cookie);
727
728 if (idev)
729 dev_put(idev);
730 }
731}
732
733/**
734 * ib_enum_all_roce_netdevs - enumerate all RoCE devices
735 * @filter: Should we call the callback?
736 * @filter_cookie: Cookie passed to filter
737 * @cb: Callback to call for each found RoCE ports
738 * @cookie: Cookie passed back to the callback
739 *
740 * Enumerates all RoCE devices' physical ports which are related
741 * to netdevices and calls callback() on each device for which
742 * filter() function returns non zero.
743 */
744void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
745 void *filter_cookie,
746 roce_netdev_callback cb,
747 void *cookie)
748{
749 struct ib_device *dev;
750
751 down_read(&lists_rwsem);
752 list_for_each_entry(dev, &device_list, core_list)
753 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
754 up_read(&lists_rwsem);
755}
756
1da177e4
LT
757/**
758 * ib_query_pkey - Get P_Key table entry
759 * @device:Device to query
760 * @port_num:Port number to query
761 * @index:P_Key table index to query
762 * @pkey:Returned P_Key
763 *
764 * ib_query_pkey() fetches the specified P_Key table entry.
765 */
766int ib_query_pkey(struct ib_device *device,
767 u8 port_num, u16 index, u16 *pkey)
768{
769 return device->query_pkey(device, port_num, index, pkey);
770}
771EXPORT_SYMBOL(ib_query_pkey);
772
773/**
774 * ib_modify_device - Change IB device attributes
775 * @device:Device to modify
776 * @device_modify_mask:Mask of attributes to change
777 * @device_modify:New attribute values
778 *
779 * ib_modify_device() changes a device's attributes as specified by
780 * the @device_modify_mask and @device_modify structure.
781 */
782int ib_modify_device(struct ib_device *device,
783 int device_modify_mask,
784 struct ib_device_modify *device_modify)
785{
10e1b54b
BVA
786 if (!device->modify_device)
787 return -ENOSYS;
788
1da177e4
LT
789 return device->modify_device(device, device_modify_mask,
790 device_modify);
791}
792EXPORT_SYMBOL(ib_modify_device);
793
794/**
795 * ib_modify_port - Modifies the attributes for the specified port.
796 * @device: The device to modify.
797 * @port_num: The number of the port to modify.
798 * @port_modify_mask: Mask used to specify which attributes of the port
799 * to change.
800 * @port_modify: New attribute values for the port.
801 *
802 * ib_modify_port() changes a port's attributes as specified by the
803 * @port_modify_mask and @port_modify structure.
804 */
805int ib_modify_port(struct ib_device *device,
806 u8 port_num, int port_modify_mask,
807 struct ib_port_modify *port_modify)
808{
10e1b54b
BVA
809 if (!device->modify_port)
810 return -ENOSYS;
811
0cf18d77 812 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
116c0074
RD
813 return -EINVAL;
814
1da177e4
LT
815 return device->modify_port(device, port_num, port_modify_mask,
816 port_modify);
817}
818EXPORT_SYMBOL(ib_modify_port);
819
5eb620c8
YE
820/**
821 * ib_find_gid - Returns the port number and GID table index where
822 * a specified GID value occurs.
823 * @device: The device to query.
824 * @gid: The GID value to search for.
825 * @port_num: The port number of the device where the GID value was found.
826 * @index: The index into the GID table where the GID was found. This
827 * parameter may be NULL.
828 */
829int ib_find_gid(struct ib_device *device, union ib_gid *gid,
830 u8 *port_num, u16 *index)
831{
832 union ib_gid tmp_gid;
833 int ret, port, i;
834
0cf18d77 835 for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
03db3a2d
MB
836 if (rdma_cap_roce_gid_table(device, port)) {
837 if (!ib_cache_gid_find_by_port(device, gid, port,
98d25afa 838 NULL, index)) {
03db3a2d
MB
839 *port_num = port;
840 return 0;
98d25afa 841 }
03db3a2d
MB
842 }
843
7738613e 844 for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
5eb620c8
YE
845 ret = ib_query_gid(device, port, i, &tmp_gid);
846 if (ret)
847 return ret;
848 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
849 *port_num = port;
850 if (index)
851 *index = i;
852 return 0;
853 }
854 }
855 }
856
857 return -ENOENT;
858}
859EXPORT_SYMBOL(ib_find_gid);
860
861/**
862 * ib_find_pkey - Returns the PKey table index where a specified
863 * PKey value occurs.
864 * @device: The device to query.
865 * @port_num: The port number of the device to search for the PKey.
866 * @pkey: The PKey value to search for.
867 * @index: The index into the PKey table where the PKey was found.
868 */
869int ib_find_pkey(struct ib_device *device,
870 u8 port_num, u16 pkey, u16 *index)
871{
872 int ret, i;
873 u16 tmp_pkey;
ff7166c4 874 int partial_ix = -1;
5eb620c8 875
7738613e 876 for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
5eb620c8
YE
877 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
878 if (ret)
879 return ret;
36026ecc 880 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
ff7166c4
JM
881 /* if there is full-member pkey take it.*/
882 if (tmp_pkey & 0x8000) {
883 *index = i;
884 return 0;
885 }
886 if (partial_ix < 0)
887 partial_ix = i;
5eb620c8
YE
888 }
889 }
890
ff7166c4
JM
891 /*no full-member, if exists take the limited*/
892 if (partial_ix >= 0) {
893 *index = partial_ix;
894 return 0;
895 }
5eb620c8
YE
896 return -ENOENT;
897}
898EXPORT_SYMBOL(ib_find_pkey);
899
9268f72d
YK
900/**
901 * ib_get_net_dev_by_params() - Return the appropriate net_dev
902 * for a received CM request
903 * @dev: An RDMA device on which the request has been received.
904 * @port: Port number on the RDMA device.
905 * @pkey: The Pkey the request came on.
906 * @gid: A GID that the net_dev uses to communicate.
907 * @addr: Contains the IP address that the request specified as its
908 * destination.
909 */
910struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
911 u8 port,
912 u16 pkey,
913 const union ib_gid *gid,
914 const struct sockaddr *addr)
915{
916 struct net_device *net_dev = NULL;
917 struct ib_client_data *context;
918
919 if (!rdma_protocol_ib(dev, port))
920 return NULL;
921
922 down_read(&lists_rwsem);
923
924 list_for_each_entry(context, &dev->client_data_list, list) {
925 struct ib_client *client = context->client;
926
927 if (context->going_down)
928 continue;
929
930 if (client->get_net_dev_by_params) {
931 net_dev = client->get_net_dev_by_params(dev, port, pkey,
932 gid, addr,
933 context->data);
934 if (net_dev)
935 break;
936 }
937 }
938
939 up_read(&lists_rwsem);
940
941 return net_dev;
942}
943EXPORT_SYMBOL(ib_get_net_dev_by_params);
944
1da177e4
LT
945static int __init ib_core_init(void)
946{
947 int ret;
948
f0626710
TH
949 ib_wq = alloc_workqueue("infiniband", 0, 0);
950 if (!ib_wq)
951 return -ENOMEM;
952
55aeed06 953 ret = class_register(&ib_class);
fd75c789 954 if (ret) {
1da177e4 955 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
fd75c789
NM
956 goto err;
957 }
1da177e4 958
b2cbae2c
RD
959 ret = ibnl_init();
960 if (ret) {
961 printk(KERN_WARNING "Couldn't init IB netlink interface\n");
962 goto err_sysfs;
963 }
964
03db3a2d 965 ib_cache_setup();
1da177e4 966
fd75c789
NM
967 return 0;
968
969err_sysfs:
55aeed06 970 class_unregister(&ib_class);
fd75c789
NM
971
972err:
973 destroy_workqueue(ib_wq);
1da177e4
LT
974 return ret;
975}
976
977static void __exit ib_core_cleanup(void)
978{
979 ib_cache_cleanup();
b2cbae2c 980 ibnl_cleanup();
55aeed06 981 class_unregister(&ib_class);
f7c6a7b5 982 /* Make sure that any pending umem accounting work is done. */
f0626710 983 destroy_workqueue(ib_wq);
1da177e4
LT
984}
985
986module_init(ib_core_init);
987module_exit(ib_core_cleanup);