blob: 568cb41be904e88bb74efd6b70a3798e9a1e9f66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
34#include <linux/module.h>
35#include <linux/string.h>
36#include <linux/errno.h>
Ahmed S. Darwish9a6b0902007-02-06 18:07:25 +020037#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/slab.h>
39#include <linux/init.h>
Ingo Molnar95ed6442006-01-13 14:51:39 -080040#include <linux/mutex.h>
Roland Dreierb2cbae22011-05-20 11:46:11 -070041#include <rdma/rdma_netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "core_priv.h"
44
45MODULE_AUTHOR("Roland Dreier");
46MODULE_DESCRIPTION("core kernel InfiniBand API");
47MODULE_LICENSE("Dual BSD/GPL");
48
49struct ib_client_data {
50 struct list_head list;
51 struct ib_client *client;
52 void * data;
53};
54
Tejun Heof0626712010-10-19 15:24:36 +000055struct workqueue_struct *ib_wq;
56EXPORT_SYMBOL_GPL(ib_wq);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static LIST_HEAD(device_list);
59static LIST_HEAD(client_list);
60
61/*
Ingo Molnar95ed6442006-01-13 14:51:39 -080062 * device_mutex protects access to both device_list and client_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * There's no real point to using multiple locks or something fancier
64 * like an rwsem: we always access both lists, and we're always
65 * modifying one list or the other list. In any case this is not a
66 * hot path so there's no point in trying to optimize.
67 */
Ingo Molnar95ed6442006-01-13 14:51:39 -080068static DEFINE_MUTEX(device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static int ib_device_check_mandatory(struct ib_device *device)
71{
72#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
73 static const struct {
74 size_t offset;
75 char *name;
76 } mandatory_table[] = {
77 IB_MANDATORY_FUNC(query_device),
78 IB_MANDATORY_FUNC(query_port),
79 IB_MANDATORY_FUNC(query_pkey),
80 IB_MANDATORY_FUNC(query_gid),
81 IB_MANDATORY_FUNC(alloc_pd),
82 IB_MANDATORY_FUNC(dealloc_pd),
83 IB_MANDATORY_FUNC(create_ah),
84 IB_MANDATORY_FUNC(destroy_ah),
85 IB_MANDATORY_FUNC(create_qp),
86 IB_MANDATORY_FUNC(modify_qp),
87 IB_MANDATORY_FUNC(destroy_qp),
88 IB_MANDATORY_FUNC(post_send),
89 IB_MANDATORY_FUNC(post_recv),
90 IB_MANDATORY_FUNC(create_cq),
91 IB_MANDATORY_FUNC(destroy_cq),
92 IB_MANDATORY_FUNC(poll_cq),
93 IB_MANDATORY_FUNC(req_notify_cq),
94 IB_MANDATORY_FUNC(get_dma_mr),
Ira Weiny77386132015-05-13 20:02:58 -040095 IB_MANDATORY_FUNC(dereg_mr),
96 IB_MANDATORY_FUNC(get_port_immutable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 };
98 int i;
99
Ahmed S. Darwish9a6b0902007-02-06 18:07:25 +0200100 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
102 printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
103 device->name, mandatory_table[i].name);
104 return -EINVAL;
105 }
106 }
107
108 return 0;
109}
110
111static struct ib_device *__ib_device_get_by_name(const char *name)
112{
113 struct ib_device *device;
114
115 list_for_each_entry(device, &device_list, core_list)
116 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
117 return device;
118
119 return NULL;
120}
121
122
123static int alloc_name(char *name)
124{
Roland Dreier65d470b2007-10-09 19:59:04 -0700125 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char buf[IB_DEVICE_NAME_MAX];
127 struct ib_device *device;
128 int i;
129
Roland Dreier65d470b2007-10-09 19:59:04 -0700130 inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (!inuse)
132 return -ENOMEM;
133
134 list_for_each_entry(device, &device_list, core_list) {
135 if (!sscanf(device->name, name, &i))
136 continue;
137 if (i < 0 || i >= PAGE_SIZE * 8)
138 continue;
139 snprintf(buf, sizeof buf, name, i);
140 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
141 set_bit(i, inuse);
142 }
143
144 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
145 free_page((unsigned long) inuse);
146 snprintf(buf, sizeof buf, name, i);
147
148 if (__ib_device_get_by_name(buf))
149 return -ENFILE;
150
151 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
152 return 0;
153}
154
155/**
156 * ib_alloc_device - allocate an IB device struct
157 * @size:size of structure to allocate
158 *
159 * Low-level drivers should use ib_alloc_device() to allocate &struct
160 * ib_device. @size is the size of the structure to be allocated,
161 * including any private data used by the low-level driver.
162 * ib_dealloc_device() must be used to free structures allocated with
163 * ib_alloc_device().
164 */
165struct ib_device *ib_alloc_device(size_t size)
166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 BUG_ON(size < sizeof (struct ib_device));
168
Roland Dreierde6eb662005-11-02 07:23:14 -0800169 return kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171EXPORT_SYMBOL(ib_alloc_device);
172
173/**
174 * ib_dealloc_device - free an IB device struct
175 * @device:structure to free
176 *
177 * Free a structure allocated with ib_alloc_device().
178 */
179void ib_dealloc_device(struct ib_device *device)
180{
181 if (device->reg_state == IB_DEV_UNINITIALIZED) {
182 kfree(device);
183 return;
184 }
185
186 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
187
Roland Dreier9206dff2009-02-25 13:27:46 -0800188 kobject_put(&device->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190EXPORT_SYMBOL(ib_dealloc_device);
191
192static int add_client_context(struct ib_device *device, struct ib_client *client)
193{
194 struct ib_client_data *context;
195 unsigned long flags;
196
197 context = kmalloc(sizeof *context, GFP_KERNEL);
198 if (!context) {
199 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
200 device->name, client->name);
201 return -ENOMEM;
202 }
203
204 context->client = client;
205 context->data = NULL;
206
207 spin_lock_irqsave(&device->client_data_lock, flags);
208 list_add(&context->list, &device->client_data_list);
209 spin_unlock_irqrestore(&device->client_data_lock, flags);
210
211 return 0;
212}
213
Ira Weiny77386132015-05-13 20:02:58 -0400214static int read_port_immutable(struct ib_device *device)
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300215{
Ira Weiny77386132015-05-13 20:02:58 -0400216 int ret = -ENOMEM;
217 u8 start_port = rdma_start_port(device);
218 u8 end_port = rdma_end_port(device);
219 u8 port;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300220
Ira Weiny77386132015-05-13 20:02:58 -0400221 /**
222 * device->port_immutable is indexed directly by the port number to make
223 * access to this data as efficient as possible.
224 *
225 * Therefore port_immutable is declared as a 1 based array with
226 * potential empty slots at the beginning.
227 */
228 device->port_immutable = kzalloc(sizeof(*device->port_immutable)
229 * (end_port + 1),
230 GFP_KERNEL);
231 if (!device->port_immutable)
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300232 goto err;
233
Ira Weiny77386132015-05-13 20:02:58 -0400234 for (port = start_port; port <= end_port; ++port) {
235 ret = device->get_port_immutable(device, port,
236 &device->port_immutable[port]);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300237 if (ret)
238 goto err;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300239 }
240
241 ret = 0;
242 goto out;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300243err:
Ira Weiny77386132015-05-13 20:02:58 -0400244 kfree(device->port_immutable);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300245out:
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300246 return ret;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/**
250 * ib_register_device - Register an IB device with IB core
251 * @device:Device to register
252 *
253 * Low-level drivers use ib_register_device() to register their
254 * devices with the IB core. All registered clients will receive a
255 * callback for each device that is added. @device must be allocated
256 * with ib_alloc_device().
257 */
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700258int ib_register_device(struct ib_device *device,
259 int (*port_callback)(struct ib_device *,
260 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int ret;
263
Ingo Molnar95ed6442006-01-13 14:51:39 -0800264 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (strchr(device->name, '%')) {
267 ret = alloc_name(device->name);
268 if (ret)
269 goto out;
270 }
271
272 if (ib_device_check_mandatory(device)) {
273 ret = -EINVAL;
274 goto out;
275 }
276
277 INIT_LIST_HEAD(&device->event_handler_list);
278 INIT_LIST_HEAD(&device->client_data_list);
279 spin_lock_init(&device->event_handler_lock);
280 spin_lock_init(&device->client_data_lock);
281
Ira Weiny77386132015-05-13 20:02:58 -0400282 ret = read_port_immutable(device);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300283 if (ret) {
Ira Weiny77386132015-05-13 20:02:58 -0400284 printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300285 device->name);
286 goto out;
287 }
288
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700289 ret = ib_device_register_sysfs(device, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (ret) {
291 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
292 device->name);
Ira Weiny77386132015-05-13 20:02:58 -0400293 kfree(device->port_immutable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 goto out;
295 }
296
297 list_add_tail(&device->core_list, &device_list);
298
299 device->reg_state = IB_DEV_REGISTERED;
300
301 {
302 struct ib_client *client;
303
304 list_for_each_entry(client, &client_list, list)
305 if (client->add && !add_client_context(device, client))
306 client->add(device);
307 }
308
309 out:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800310 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return ret;
312}
313EXPORT_SYMBOL(ib_register_device);
314
315/**
316 * ib_unregister_device - Unregister an IB device
317 * @device:Device to unregister
318 *
319 * Unregister an IB device. All clients will receive a remove callback.
320 */
321void ib_unregister_device(struct ib_device *device)
322{
323 struct ib_client *client;
324 struct ib_client_data *context, *tmp;
325 unsigned long flags;
326
Ingo Molnar95ed6442006-01-13 14:51:39 -0800327 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 list_for_each_entry_reverse(client, &client_list, list)
330 if (client->remove)
331 client->remove(device);
332
333 list_del(&device->core_list);
334
Ingo Molnar95ed6442006-01-13 14:51:39 -0800335 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Roland Dreier9206dff2009-02-25 13:27:46 -0800337 ib_device_unregister_sysfs(device);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_lock_irqsave(&device->client_data_lock, flags);
340 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
341 kfree(context);
342 spin_unlock_irqrestore(&device->client_data_lock, flags);
343
344 device->reg_state = IB_DEV_UNREGISTERED;
345}
346EXPORT_SYMBOL(ib_unregister_device);
347
348/**
349 * ib_register_client - Register an IB client
350 * @client:Client to register
351 *
352 * Upper level users of the IB drivers can use ib_register_client() to
353 * register callbacks for IB device addition and removal. When an IB
354 * device is added, each registered client's add method will be called
355 * (in the order the clients were registered), and when a device is
356 * removed, each client's remove method will be called (in the reverse
357 * order that clients were registered). In addition, when
358 * ib_register_client() is called, the client will receive an add
359 * callback for all devices already registered.
360 */
361int ib_register_client(struct ib_client *client)
362{
363 struct ib_device *device;
364
Ingo Molnar95ed6442006-01-13 14:51:39 -0800365 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 list_add_tail(&client->list, &client_list);
368 list_for_each_entry(device, &device_list, core_list)
369 if (client->add && !add_client_context(device, client))
370 client->add(device);
371
Ingo Molnar95ed6442006-01-13 14:51:39 -0800372 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 return 0;
375}
376EXPORT_SYMBOL(ib_register_client);
377
378/**
379 * ib_unregister_client - Unregister an IB client
380 * @client:Client to unregister
381 *
382 * Upper level users use ib_unregister_client() to remove their client
383 * registration. When ib_unregister_client() is called, the client
384 * will receive a remove callback for each IB device still registered.
385 */
386void ib_unregister_client(struct ib_client *client)
387{
388 struct ib_client_data *context, *tmp;
389 struct ib_device *device;
390 unsigned long flags;
391
Ingo Molnar95ed6442006-01-13 14:51:39 -0800392 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 list_for_each_entry(device, &device_list, core_list) {
395 if (client->remove)
396 client->remove(device);
397
398 spin_lock_irqsave(&device->client_data_lock, flags);
399 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
400 if (context->client == client) {
401 list_del(&context->list);
402 kfree(context);
403 }
404 spin_unlock_irqrestore(&device->client_data_lock, flags);
405 }
406 list_del(&client->list);
407
Ingo Molnar95ed6442006-01-13 14:51:39 -0800408 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410EXPORT_SYMBOL(ib_unregister_client);
411
412/**
413 * ib_get_client_data - Get IB client context
414 * @device:Device to get context for
415 * @client:Client to get context for
416 *
417 * ib_get_client_data() returns client context set with
418 * ib_set_client_data().
419 */
420void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
421{
422 struct ib_client_data *context;
423 void *ret = NULL;
424 unsigned long flags;
425
426 spin_lock_irqsave(&device->client_data_lock, flags);
427 list_for_each_entry(context, &device->client_data_list, list)
428 if (context->client == client) {
429 ret = context->data;
430 break;
431 }
432 spin_unlock_irqrestore(&device->client_data_lock, flags);
433
434 return ret;
435}
436EXPORT_SYMBOL(ib_get_client_data);
437
438/**
Krishna Kumar9cd330d2006-09-22 15:22:58 -0700439 * ib_set_client_data - Set IB client context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * @device:Device to set context for
441 * @client:Client to set context for
442 * @data:Context to set
443 *
444 * ib_set_client_data() sets client context that can be retrieved with
445 * ib_get_client_data().
446 */
447void ib_set_client_data(struct ib_device *device, struct ib_client *client,
448 void *data)
449{
450 struct ib_client_data *context;
451 unsigned long flags;
452
453 spin_lock_irqsave(&device->client_data_lock, flags);
454 list_for_each_entry(context, &device->client_data_list, list)
455 if (context->client == client) {
456 context->data = data;
457 goto out;
458 }
459
460 printk(KERN_WARNING "No client context found for %s/%s\n",
461 device->name, client->name);
462
463out:
464 spin_unlock_irqrestore(&device->client_data_lock, flags);
465}
466EXPORT_SYMBOL(ib_set_client_data);
467
468/**
469 * ib_register_event_handler - Register an IB event handler
470 * @event_handler:Handler to register
471 *
472 * ib_register_event_handler() registers an event handler that will be
473 * called back when asynchronous IB events occur (as defined in
474 * chapter 11 of the InfiniBand Architecture Specification). This
475 * callback may occur in interrupt context.
476 */
477int ib_register_event_handler (struct ib_event_handler *event_handler)
478{
479 unsigned long flags;
480
481 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
482 list_add_tail(&event_handler->list,
483 &event_handler->device->event_handler_list);
484 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
485
486 return 0;
487}
488EXPORT_SYMBOL(ib_register_event_handler);
489
490/**
491 * ib_unregister_event_handler - Unregister an event handler
492 * @event_handler:Handler to unregister
493 *
494 * Unregister an event handler registered with
495 * ib_register_event_handler().
496 */
497int ib_unregister_event_handler(struct ib_event_handler *event_handler)
498{
499 unsigned long flags;
500
501 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
502 list_del(&event_handler->list);
503 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
504
505 return 0;
506}
507EXPORT_SYMBOL(ib_unregister_event_handler);
508
509/**
510 * ib_dispatch_event - Dispatch an asynchronous event
511 * @event:Event to dispatch
512 *
513 * Low-level drivers must call ib_dispatch_event() to dispatch the
514 * event to all registered event handlers when an asynchronous event
515 * occurs.
516 */
517void ib_dispatch_event(struct ib_event *event)
518{
519 unsigned long flags;
520 struct ib_event_handler *handler;
521
522 spin_lock_irqsave(&event->device->event_handler_lock, flags);
523
524 list_for_each_entry(handler, &event->device->event_handler_list, list)
525 handler->handler(handler, event);
526
527 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
528}
529EXPORT_SYMBOL(ib_dispatch_event);
530
531/**
532 * ib_query_device - Query IB device attributes
533 * @device:Device to query
534 * @device_attr:Device attributes
535 *
536 * ib_query_device() returns the attributes of a device through the
537 * @device_attr pointer.
538 */
539int ib_query_device(struct ib_device *device,
540 struct ib_device_attr *device_attr)
541{
Matan Barak24306dc2015-06-11 16:35:24 +0300542 memset(device_attr, 0, sizeof(*device_attr));
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return device->query_device(device, device_attr);
545}
546EXPORT_SYMBOL(ib_query_device);
547
548/**
549 * ib_query_port - Query IB port attributes
550 * @device:Device to query
551 * @port_num:Port number to query
552 * @port_attr:Port attributes
553 *
554 * ib_query_port() returns the attributes of a port through the
555 * @port_attr pointer.
556 */
557int ib_query_port(struct ib_device *device,
558 u8 port_num,
559 struct ib_port_attr *port_attr)
560{
Ira Weiny0cf18d72015-05-13 20:02:55 -0400561 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700562 return -EINVAL;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return device->query_port(device, port_num, port_attr);
565}
566EXPORT_SYMBOL(ib_query_port);
567
568/**
569 * ib_query_gid - Get GID table entry
570 * @device:Device to query
571 * @port_num:Port number to query
572 * @index:GID table index to query
573 * @gid:Returned GID
574 *
575 * ib_query_gid() fetches the specified GID table entry.
576 */
577int ib_query_gid(struct ib_device *device,
578 u8 port_num, int index, union ib_gid *gid)
579{
580 return device->query_gid(device, port_num, index, gid);
581}
582EXPORT_SYMBOL(ib_query_gid);
583
584/**
585 * ib_query_pkey - Get P_Key table entry
586 * @device:Device to query
587 * @port_num:Port number to query
588 * @index:P_Key table index to query
589 * @pkey:Returned P_Key
590 *
591 * ib_query_pkey() fetches the specified P_Key table entry.
592 */
593int ib_query_pkey(struct ib_device *device,
594 u8 port_num, u16 index, u16 *pkey)
595{
596 return device->query_pkey(device, port_num, index, pkey);
597}
598EXPORT_SYMBOL(ib_query_pkey);
599
600/**
601 * ib_modify_device - Change IB device attributes
602 * @device:Device to modify
603 * @device_modify_mask:Mask of attributes to change
604 * @device_modify:New attribute values
605 *
606 * ib_modify_device() changes a device's attributes as specified by
607 * the @device_modify_mask and @device_modify structure.
608 */
609int ib_modify_device(struct ib_device *device,
610 int device_modify_mask,
611 struct ib_device_modify *device_modify)
612{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000613 if (!device->modify_device)
614 return -ENOSYS;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return device->modify_device(device, device_modify_mask,
617 device_modify);
618}
619EXPORT_SYMBOL(ib_modify_device);
620
621/**
622 * ib_modify_port - Modifies the attributes for the specified port.
623 * @device: The device to modify.
624 * @port_num: The number of the port to modify.
625 * @port_modify_mask: Mask used to specify which attributes of the port
626 * to change.
627 * @port_modify: New attribute values for the port.
628 *
629 * ib_modify_port() changes a port's attributes as specified by the
630 * @port_modify_mask and @port_modify structure.
631 */
632int ib_modify_port(struct ib_device *device,
633 u8 port_num, int port_modify_mask,
634 struct ib_port_modify *port_modify)
635{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000636 if (!device->modify_port)
637 return -ENOSYS;
638
Ira Weiny0cf18d72015-05-13 20:02:55 -0400639 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700640 return -EINVAL;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return device->modify_port(device, port_num, port_modify_mask,
643 port_modify);
644}
645EXPORT_SYMBOL(ib_modify_port);
646
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300647/**
648 * ib_find_gid - Returns the port number and GID table index where
649 * a specified GID value occurs.
650 * @device: The device to query.
651 * @gid: The GID value to search for.
652 * @port_num: The port number of the device where the GID value was found.
653 * @index: The index into the GID table where the GID was found. This
654 * parameter may be NULL.
655 */
656int ib_find_gid(struct ib_device *device, union ib_gid *gid,
657 u8 *port_num, u16 *index)
658{
659 union ib_gid tmp_gid;
660 int ret, port, i;
661
Ira Weiny0cf18d72015-05-13 20:02:55 -0400662 for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
Ira Weiny77386132015-05-13 20:02:58 -0400663 for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300664 ret = ib_query_gid(device, port, i, &tmp_gid);
665 if (ret)
666 return ret;
667 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
668 *port_num = port;
669 if (index)
670 *index = i;
671 return 0;
672 }
673 }
674 }
675
676 return -ENOENT;
677}
678EXPORT_SYMBOL(ib_find_gid);
679
680/**
681 * ib_find_pkey - Returns the PKey table index where a specified
682 * PKey value occurs.
683 * @device: The device to query.
684 * @port_num: The port number of the device to search for the PKey.
685 * @pkey: The PKey value to search for.
686 * @index: The index into the PKey table where the PKey was found.
687 */
688int ib_find_pkey(struct ib_device *device,
689 u8 port_num, u16 pkey, u16 *index)
690{
691 int ret, i;
692 u16 tmp_pkey;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000693 int partial_ix = -1;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300694
Ira Weiny77386132015-05-13 20:02:58 -0400695 for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300696 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
697 if (ret)
698 return ret;
Moni Shoua36026ec2007-07-23 10:07:42 +0300699 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000700 /* if there is full-member pkey take it.*/
701 if (tmp_pkey & 0x8000) {
702 *index = i;
703 return 0;
704 }
705 if (partial_ix < 0)
706 partial_ix = i;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300707 }
708 }
709
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000710 /*no full-member, if exists take the limited*/
711 if (partial_ix >= 0) {
712 *index = partial_ix;
713 return 0;
714 }
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300715 return -ENOENT;
716}
717EXPORT_SYMBOL(ib_find_pkey);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static int __init ib_core_init(void)
720{
721 int ret;
722
Tejun Heof0626712010-10-19 15:24:36 +0000723 ib_wq = alloc_workqueue("infiniband", 0, 0);
724 if (!ib_wq)
725 return -ENOMEM;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ret = ib_sysfs_setup();
Nir Muchtarfd75c782011-05-20 11:46:10 -0700728 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700730 goto err;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Roland Dreierb2cbae22011-05-20 11:46:11 -0700733 ret = ibnl_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (ret) {
Roland Dreierb2cbae22011-05-20 11:46:11 -0700735 printk(KERN_WARNING "Couldn't init IB netlink interface\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700736 goto err_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Roland Dreierb2cbae22011-05-20 11:46:11 -0700739 ret = ib_cache_setup();
740 if (ret) {
741 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
742 goto err_nl;
743 }
744
Nir Muchtarfd75c782011-05-20 11:46:10 -0700745 return 0;
746
Roland Dreierb2cbae22011-05-20 11:46:11 -0700747err_nl:
748 ibnl_cleanup();
749
Nir Muchtarfd75c782011-05-20 11:46:10 -0700750err_sysfs:
751 ib_sysfs_cleanup();
752
753err:
754 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return ret;
756}
757
758static void __exit ib_core_cleanup(void)
759{
760 ib_cache_cleanup();
Roland Dreierb2cbae22011-05-20 11:46:11 -0700761 ibnl_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ib_sysfs_cleanup();
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800763 /* Make sure that any pending umem accounting work is done. */
Tejun Heof0626712010-10-19 15:24:36 +0000764 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767module_init(ib_core_init);
768module_exit(ib_core_cleanup);