blob: b360350a0b205ffd6cfd6ef3be472295dbe84821 [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),
Michael Wang6b90a6d2015-05-05 14:50:18 +020079 IB_MANDATORY_FUNC(query_protocol),
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 IB_MANDATORY_FUNC(query_pkey),
81 IB_MANDATORY_FUNC(query_gid),
82 IB_MANDATORY_FUNC(alloc_pd),
83 IB_MANDATORY_FUNC(dealloc_pd),
84 IB_MANDATORY_FUNC(create_ah),
85 IB_MANDATORY_FUNC(destroy_ah),
86 IB_MANDATORY_FUNC(create_qp),
87 IB_MANDATORY_FUNC(modify_qp),
88 IB_MANDATORY_FUNC(destroy_qp),
89 IB_MANDATORY_FUNC(post_send),
90 IB_MANDATORY_FUNC(post_recv),
91 IB_MANDATORY_FUNC(create_cq),
92 IB_MANDATORY_FUNC(destroy_cq),
93 IB_MANDATORY_FUNC(poll_cq),
94 IB_MANDATORY_FUNC(req_notify_cq),
95 IB_MANDATORY_FUNC(get_dma_mr),
96 IB_MANDATORY_FUNC(dereg_mr)
97 };
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
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300155static int start_port(struct ib_device *device)
156{
157 return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
158}
159
160
161static int end_port(struct ib_device *device)
162{
163 return (device->node_type == RDMA_NODE_IB_SWITCH) ?
164 0 : device->phys_port_cnt;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/**
168 * ib_alloc_device - allocate an IB device struct
169 * @size:size of structure to allocate
170 *
171 * Low-level drivers should use ib_alloc_device() to allocate &struct
172 * ib_device. @size is the size of the structure to be allocated,
173 * including any private data used by the low-level driver.
174 * ib_dealloc_device() must be used to free structures allocated with
175 * ib_alloc_device().
176 */
177struct ib_device *ib_alloc_device(size_t size)
178{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 BUG_ON(size < sizeof (struct ib_device));
180
Roland Dreierde6eb662005-11-02 07:23:14 -0800181 return kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183EXPORT_SYMBOL(ib_alloc_device);
184
185/**
186 * ib_dealloc_device - free an IB device struct
187 * @device:structure to free
188 *
189 * Free a structure allocated with ib_alloc_device().
190 */
191void ib_dealloc_device(struct ib_device *device)
192{
193 if (device->reg_state == IB_DEV_UNINITIALIZED) {
194 kfree(device);
195 return;
196 }
197
198 BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
199
Roland Dreier9206dff2009-02-25 13:27:46 -0800200 kobject_put(&device->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202EXPORT_SYMBOL(ib_dealloc_device);
203
204static int add_client_context(struct ib_device *device, struct ib_client *client)
205{
206 struct ib_client_data *context;
207 unsigned long flags;
208
209 context = kmalloc(sizeof *context, GFP_KERNEL);
210 if (!context) {
211 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
212 device->name, client->name);
213 return -ENOMEM;
214 }
215
216 context->client = client;
217 context->data = NULL;
218
219 spin_lock_irqsave(&device->client_data_lock, flags);
220 list_add(&context->list, &device->client_data_list);
221 spin_unlock_irqrestore(&device->client_data_lock, flags);
222
223 return 0;
224}
225
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300226static int read_port_table_lengths(struct ib_device *device)
227{
228 struct ib_port_attr *tprops = NULL;
229 int num_ports, ret = -ENOMEM;
230 u8 port_index;
231
232 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
233 if (!tprops)
234 goto out;
235
236 num_ports = end_port(device) - start_port(device) + 1;
237
238 device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
239 GFP_KERNEL);
240 device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
241 GFP_KERNEL);
242 if (!device->pkey_tbl_len || !device->gid_tbl_len)
243 goto err;
244
245 for (port_index = 0; port_index < num_ports; ++port_index) {
246 ret = ib_query_port(device, port_index + start_port(device),
247 tprops);
248 if (ret)
249 goto err;
250 device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
251 device->gid_tbl_len[port_index] = tprops->gid_tbl_len;
252 }
253
254 ret = 0;
255 goto out;
256
257err:
258 kfree(device->gid_tbl_len);
259 kfree(device->pkey_tbl_len);
260out:
261 kfree(tprops);
262 return ret;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
266 * ib_register_device - Register an IB device with IB core
267 * @device:Device to register
268 *
269 * Low-level drivers use ib_register_device() to register their
270 * devices with the IB core. All registered clients will receive a
271 * callback for each device that is added. @device must be allocated
272 * with ib_alloc_device().
273 */
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700274int ib_register_device(struct ib_device *device,
275 int (*port_callback)(struct ib_device *,
276 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int ret;
279
Ingo Molnar95ed6442006-01-13 14:51:39 -0800280 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (strchr(device->name, '%')) {
283 ret = alloc_name(device->name);
284 if (ret)
285 goto out;
286 }
287
288 if (ib_device_check_mandatory(device)) {
289 ret = -EINVAL;
290 goto out;
291 }
292
293 INIT_LIST_HEAD(&device->event_handler_list);
294 INIT_LIST_HEAD(&device->client_data_list);
295 spin_lock_init(&device->event_handler_lock);
296 spin_lock_init(&device->client_data_lock);
297
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300298 ret = read_port_table_lengths(device);
299 if (ret) {
300 printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
301 device->name);
302 goto out;
303 }
304
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700305 ret = ib_device_register_sysfs(device, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (ret) {
307 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
308 device->name);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300309 kfree(device->gid_tbl_len);
310 kfree(device->pkey_tbl_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
312 }
313
314 list_add_tail(&device->core_list, &device_list);
315
316 device->reg_state = IB_DEV_REGISTERED;
317
318 {
319 struct ib_client *client;
320
321 list_for_each_entry(client, &client_list, list)
322 if (client->add && !add_client_context(device, client))
323 client->add(device);
324 }
325
326 out:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800327 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return ret;
329}
330EXPORT_SYMBOL(ib_register_device);
331
332/**
333 * ib_unregister_device - Unregister an IB device
334 * @device:Device to unregister
335 *
336 * Unregister an IB device. All clients will receive a remove callback.
337 */
338void ib_unregister_device(struct ib_device *device)
339{
340 struct ib_client *client;
341 struct ib_client_data *context, *tmp;
342 unsigned long flags;
343
Ingo Molnar95ed6442006-01-13 14:51:39 -0800344 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 list_for_each_entry_reverse(client, &client_list, list)
347 if (client->remove)
348 client->remove(device);
349
350 list_del(&device->core_list);
351
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300352 kfree(device->gid_tbl_len);
353 kfree(device->pkey_tbl_len);
354
Ingo Molnar95ed6442006-01-13 14:51:39 -0800355 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Roland Dreier9206dff2009-02-25 13:27:46 -0800357 ib_device_unregister_sysfs(device);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 spin_lock_irqsave(&device->client_data_lock, flags);
360 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
361 kfree(context);
362 spin_unlock_irqrestore(&device->client_data_lock, flags);
363
364 device->reg_state = IB_DEV_UNREGISTERED;
365}
366EXPORT_SYMBOL(ib_unregister_device);
367
368/**
369 * ib_register_client - Register an IB client
370 * @client:Client to register
371 *
372 * Upper level users of the IB drivers can use ib_register_client() to
373 * register callbacks for IB device addition and removal. When an IB
374 * device is added, each registered client's add method will be called
375 * (in the order the clients were registered), and when a device is
376 * removed, each client's remove method will be called (in the reverse
377 * order that clients were registered). In addition, when
378 * ib_register_client() is called, the client will receive an add
379 * callback for all devices already registered.
380 */
381int ib_register_client(struct ib_client *client)
382{
383 struct ib_device *device;
384
Ingo Molnar95ed6442006-01-13 14:51:39 -0800385 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 list_add_tail(&client->list, &client_list);
388 list_for_each_entry(device, &device_list, core_list)
389 if (client->add && !add_client_context(device, client))
390 client->add(device);
391
Ingo Molnar95ed6442006-01-13 14:51:39 -0800392 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return 0;
395}
396EXPORT_SYMBOL(ib_register_client);
397
398/**
399 * ib_unregister_client - Unregister an IB client
400 * @client:Client to unregister
401 *
402 * Upper level users use ib_unregister_client() to remove their client
403 * registration. When ib_unregister_client() is called, the client
404 * will receive a remove callback for each IB device still registered.
405 */
406void ib_unregister_client(struct ib_client *client)
407{
408 struct ib_client_data *context, *tmp;
409 struct ib_device *device;
410 unsigned long flags;
411
Ingo Molnar95ed6442006-01-13 14:51:39 -0800412 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 list_for_each_entry(device, &device_list, core_list) {
415 if (client->remove)
416 client->remove(device);
417
418 spin_lock_irqsave(&device->client_data_lock, flags);
419 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
420 if (context->client == client) {
421 list_del(&context->list);
422 kfree(context);
423 }
424 spin_unlock_irqrestore(&device->client_data_lock, flags);
425 }
426 list_del(&client->list);
427
Ingo Molnar95ed6442006-01-13 14:51:39 -0800428 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430EXPORT_SYMBOL(ib_unregister_client);
431
432/**
433 * ib_get_client_data - Get IB client context
434 * @device:Device to get context for
435 * @client:Client to get context for
436 *
437 * ib_get_client_data() returns client context set with
438 * ib_set_client_data().
439 */
440void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
441{
442 struct ib_client_data *context;
443 void *ret = NULL;
444 unsigned long flags;
445
446 spin_lock_irqsave(&device->client_data_lock, flags);
447 list_for_each_entry(context, &device->client_data_list, list)
448 if (context->client == client) {
449 ret = context->data;
450 break;
451 }
452 spin_unlock_irqrestore(&device->client_data_lock, flags);
453
454 return ret;
455}
456EXPORT_SYMBOL(ib_get_client_data);
457
458/**
Krishna Kumar9cd330d2006-09-22 15:22:58 -0700459 * ib_set_client_data - Set IB client context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * @device:Device to set context for
461 * @client:Client to set context for
462 * @data:Context to set
463 *
464 * ib_set_client_data() sets client context that can be retrieved with
465 * ib_get_client_data().
466 */
467void ib_set_client_data(struct ib_device *device, struct ib_client *client,
468 void *data)
469{
470 struct ib_client_data *context;
471 unsigned long flags;
472
473 spin_lock_irqsave(&device->client_data_lock, flags);
474 list_for_each_entry(context, &device->client_data_list, list)
475 if (context->client == client) {
476 context->data = data;
477 goto out;
478 }
479
480 printk(KERN_WARNING "No client context found for %s/%s\n",
481 device->name, client->name);
482
483out:
484 spin_unlock_irqrestore(&device->client_data_lock, flags);
485}
486EXPORT_SYMBOL(ib_set_client_data);
487
488/**
489 * ib_register_event_handler - Register an IB event handler
490 * @event_handler:Handler to register
491 *
492 * ib_register_event_handler() registers an event handler that will be
493 * called back when asynchronous IB events occur (as defined in
494 * chapter 11 of the InfiniBand Architecture Specification). This
495 * callback may occur in interrupt context.
496 */
497int ib_register_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_add_tail(&event_handler->list,
503 &event_handler->device->event_handler_list);
504 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
505
506 return 0;
507}
508EXPORT_SYMBOL(ib_register_event_handler);
509
510/**
511 * ib_unregister_event_handler - Unregister an event handler
512 * @event_handler:Handler to unregister
513 *
514 * Unregister an event handler registered with
515 * ib_register_event_handler().
516 */
517int ib_unregister_event_handler(struct ib_event_handler *event_handler)
518{
519 unsigned long flags;
520
521 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
522 list_del(&event_handler->list);
523 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
524
525 return 0;
526}
527EXPORT_SYMBOL(ib_unregister_event_handler);
528
529/**
530 * ib_dispatch_event - Dispatch an asynchronous event
531 * @event:Event to dispatch
532 *
533 * Low-level drivers must call ib_dispatch_event() to dispatch the
534 * event to all registered event handlers when an asynchronous event
535 * occurs.
536 */
537void ib_dispatch_event(struct ib_event *event)
538{
539 unsigned long flags;
540 struct ib_event_handler *handler;
541
542 spin_lock_irqsave(&event->device->event_handler_lock, flags);
543
544 list_for_each_entry(handler, &event->device->event_handler_list, list)
545 handler->handler(handler, event);
546
547 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
548}
549EXPORT_SYMBOL(ib_dispatch_event);
550
551/**
552 * ib_query_device - Query IB device attributes
553 * @device:Device to query
554 * @device_attr:Device attributes
555 *
556 * ib_query_device() returns the attributes of a device through the
557 * @device_attr pointer.
558 */
559int ib_query_device(struct ib_device *device,
560 struct ib_device_attr *device_attr)
561{
562 return device->query_device(device, device_attr);
563}
564EXPORT_SYMBOL(ib_query_device);
565
566/**
567 * ib_query_port - Query IB port attributes
568 * @device:Device to query
569 * @port_num:Port number to query
570 * @port_attr:Port attributes
571 *
572 * ib_query_port() returns the attributes of a port through the
573 * @port_attr pointer.
574 */
575int ib_query_port(struct ib_device *device,
576 u8 port_num,
577 struct ib_port_attr *port_attr)
578{
Roland Dreier1af4c432007-05-19 08:51:54 -0700579 if (port_num < start_port(device) || port_num > end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700580 return -EINVAL;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return device->query_port(device, port_num, port_attr);
583}
584EXPORT_SYMBOL(ib_query_port);
585
586/**
587 * ib_query_gid - Get GID table entry
588 * @device:Device to query
589 * @port_num:Port number to query
590 * @index:GID table index to query
591 * @gid:Returned GID
592 *
593 * ib_query_gid() fetches the specified GID table entry.
594 */
595int ib_query_gid(struct ib_device *device,
596 u8 port_num, int index, union ib_gid *gid)
597{
598 return device->query_gid(device, port_num, index, gid);
599}
600EXPORT_SYMBOL(ib_query_gid);
601
602/**
603 * ib_query_pkey - Get P_Key table entry
604 * @device:Device to query
605 * @port_num:Port number to query
606 * @index:P_Key table index to query
607 * @pkey:Returned P_Key
608 *
609 * ib_query_pkey() fetches the specified P_Key table entry.
610 */
611int ib_query_pkey(struct ib_device *device,
612 u8 port_num, u16 index, u16 *pkey)
613{
614 return device->query_pkey(device, port_num, index, pkey);
615}
616EXPORT_SYMBOL(ib_query_pkey);
617
618/**
619 * ib_modify_device - Change IB device attributes
620 * @device:Device to modify
621 * @device_modify_mask:Mask of attributes to change
622 * @device_modify:New attribute values
623 *
624 * ib_modify_device() changes a device's attributes as specified by
625 * the @device_modify_mask and @device_modify structure.
626 */
627int ib_modify_device(struct ib_device *device,
628 int device_modify_mask,
629 struct ib_device_modify *device_modify)
630{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000631 if (!device->modify_device)
632 return -ENOSYS;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return device->modify_device(device, device_modify_mask,
635 device_modify);
636}
637EXPORT_SYMBOL(ib_modify_device);
638
639/**
640 * ib_modify_port - Modifies the attributes for the specified port.
641 * @device: The device to modify.
642 * @port_num: The number of the port to modify.
643 * @port_modify_mask: Mask used to specify which attributes of the port
644 * to change.
645 * @port_modify: New attribute values for the port.
646 *
647 * ib_modify_port() changes a port's attributes as specified by the
648 * @port_modify_mask and @port_modify structure.
649 */
650int ib_modify_port(struct ib_device *device,
651 u8 port_num, int port_modify_mask,
652 struct ib_port_modify *port_modify)
653{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000654 if (!device->modify_port)
655 return -ENOSYS;
656
Roland Dreier1af4c432007-05-19 08:51:54 -0700657 if (port_num < start_port(device) || port_num > end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700658 return -EINVAL;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return device->modify_port(device, port_num, port_modify_mask,
661 port_modify);
662}
663EXPORT_SYMBOL(ib_modify_port);
664
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300665/**
666 * ib_find_gid - Returns the port number and GID table index where
667 * a specified GID value occurs.
668 * @device: The device to query.
669 * @gid: The GID value to search for.
670 * @port_num: The port number of the device where the GID value was found.
671 * @index: The index into the GID table where the GID was found. This
672 * parameter may be NULL.
673 */
674int ib_find_gid(struct ib_device *device, union ib_gid *gid,
675 u8 *port_num, u16 *index)
676{
677 union ib_gid tmp_gid;
678 int ret, port, i;
679
680 for (port = start_port(device); port <= end_port(device); ++port) {
681 for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
682 ret = ib_query_gid(device, port, i, &tmp_gid);
683 if (ret)
684 return ret;
685 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
686 *port_num = port;
687 if (index)
688 *index = i;
689 return 0;
690 }
691 }
692 }
693
694 return -ENOENT;
695}
696EXPORT_SYMBOL(ib_find_gid);
697
698/**
699 * ib_find_pkey - Returns the PKey table index where a specified
700 * PKey value occurs.
701 * @device: The device to query.
702 * @port_num: The port number of the device to search for the PKey.
703 * @pkey: The PKey value to search for.
704 * @index: The index into the PKey table where the PKey was found.
705 */
706int ib_find_pkey(struct ib_device *device,
707 u8 port_num, u16 pkey, u16 *index)
708{
709 int ret, i;
710 u16 tmp_pkey;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000711 int partial_ix = -1;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300712
713 for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
714 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
715 if (ret)
716 return ret;
Moni Shoua36026ec2007-07-23 10:07:42 +0300717 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000718 /* if there is full-member pkey take it.*/
719 if (tmp_pkey & 0x8000) {
720 *index = i;
721 return 0;
722 }
723 if (partial_ix < 0)
724 partial_ix = i;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300725 }
726 }
727
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000728 /*no full-member, if exists take the limited*/
729 if (partial_ix >= 0) {
730 *index = partial_ix;
731 return 0;
732 }
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300733 return -ENOENT;
734}
735EXPORT_SYMBOL(ib_find_pkey);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static int __init ib_core_init(void)
738{
739 int ret;
740
Tejun Heof0626712010-10-19 15:24:36 +0000741 ib_wq = alloc_workqueue("infiniband", 0, 0);
742 if (!ib_wq)
743 return -ENOMEM;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ret = ib_sysfs_setup();
Nir Muchtarfd75c782011-05-20 11:46:10 -0700746 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700748 goto err;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Roland Dreierb2cbae22011-05-20 11:46:11 -0700751 ret = ibnl_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (ret) {
Roland Dreierb2cbae22011-05-20 11:46:11 -0700753 printk(KERN_WARNING "Couldn't init IB netlink interface\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700754 goto err_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Roland Dreierb2cbae22011-05-20 11:46:11 -0700757 ret = ib_cache_setup();
758 if (ret) {
759 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
760 goto err_nl;
761 }
762
Nir Muchtarfd75c782011-05-20 11:46:10 -0700763 return 0;
764
Roland Dreierb2cbae22011-05-20 11:46:11 -0700765err_nl:
766 ibnl_cleanup();
767
Nir Muchtarfd75c782011-05-20 11:46:10 -0700768err_sysfs:
769 ib_sysfs_cleanup();
770
771err:
772 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return ret;
774}
775
776static void __exit ib_core_cleanup(void)
777{
778 ib_cache_cleanup();
Roland Dreierb2cbae22011-05-20 11:46:11 -0700779 ibnl_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ib_sysfs_cleanup();
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800781 /* Make sure that any pending umem accounting work is done. */
Tejun Heof0626712010-10-19 15:24:36 +0000782 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785module_init(ib_core_init);
786module_exit(ib_core_cleanup);