blob: 6421d2317b6f138d759da053b495f58a37b81684 [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>
Yotam Kenneth9268f722015-07-30 17:50:15 +030041#include <linux/netdevice.h>
Roland Dreierb2cbae22011-05-20 11:46:11 -070042#include <rdma/rdma_netlink.h>
Matan Barak03db3a22015-07-30 18:33:26 +030043#include <rdma/ib_addr.h>
44#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
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;
Haggai Eran7c1eb452015-07-30 17:50:14 +030056 /* The device or client is going down. Do not call client or device
57 * callbacks other than remove(). */
58 bool going_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -080061struct workqueue_struct *ib_comp_wq;
Tejun Heof0626712010-10-19 15:24:36 +000062struct workqueue_struct *ib_wq;
63EXPORT_SYMBOL_GPL(ib_wq);
64
Haggai Eran5aa44bb2015-07-30 17:50:13 +030065/* The device_list and client_list contain devices and clients after their
66 * registration has completed, and the devices and clients are removed
67 * during unregistration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static LIST_HEAD(device_list);
69static LIST_HEAD(client_list);
70
71/*
Haggai Eran5aa44bb2015-07-30 17:50:13 +030072 * device_mutex and lists_rwsem protect access to both device_list and
73 * client_list. device_mutex protects writer access by device and client
74 * registration / de-registration. lists_rwsem protects reader access to
75 * these lists. Iterators of these lists must lock it for read, while updates
76 * to the lists must be done with a write lock. A special case is when the
77 * device_mutex is locked. In this case locking the lists for read access is
78 * not necessary as the device_mutex implies it.
Haggai Eran7c1eb452015-07-30 17:50:14 +030079 *
80 * lists_rwsem also protects access to the client data list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
Ingo Molnar95ed6442006-01-13 14:51:39 -080082static DEFINE_MUTEX(device_mutex);
Haggai Eran5aa44bb2015-07-30 17:50:13 +030083static DECLARE_RWSEM(lists_rwsem);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86static int ib_device_check_mandatory(struct ib_device *device)
87{
88#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
89 static const struct {
90 size_t offset;
91 char *name;
92 } mandatory_table[] = {
93 IB_MANDATORY_FUNC(query_device),
94 IB_MANDATORY_FUNC(query_port),
95 IB_MANDATORY_FUNC(query_pkey),
96 IB_MANDATORY_FUNC(query_gid),
97 IB_MANDATORY_FUNC(alloc_pd),
98 IB_MANDATORY_FUNC(dealloc_pd),
99 IB_MANDATORY_FUNC(create_ah),
100 IB_MANDATORY_FUNC(destroy_ah),
101 IB_MANDATORY_FUNC(create_qp),
102 IB_MANDATORY_FUNC(modify_qp),
103 IB_MANDATORY_FUNC(destroy_qp),
104 IB_MANDATORY_FUNC(post_send),
105 IB_MANDATORY_FUNC(post_recv),
106 IB_MANDATORY_FUNC(create_cq),
107 IB_MANDATORY_FUNC(destroy_cq),
108 IB_MANDATORY_FUNC(poll_cq),
109 IB_MANDATORY_FUNC(req_notify_cq),
110 IB_MANDATORY_FUNC(get_dma_mr),
Ira Weiny77386132015-05-13 20:02:58 -0400111 IB_MANDATORY_FUNC(dereg_mr),
112 IB_MANDATORY_FUNC(get_port_immutable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 };
114 int i;
115
Ahmed S. Darwish9a6b0902007-02-06 18:07:25 +0200116 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
118 printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
119 device->name, mandatory_table[i].name);
120 return -EINVAL;
121 }
122 }
123
124 return 0;
125}
126
127static struct ib_device *__ib_device_get_by_name(const char *name)
128{
129 struct ib_device *device;
130
131 list_for_each_entry(device, &device_list, core_list)
132 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
133 return device;
134
135 return NULL;
136}
137
138
139static int alloc_name(char *name)
140{
Roland Dreier65d470b2007-10-09 19:59:04 -0700141 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 char buf[IB_DEVICE_NAME_MAX];
143 struct ib_device *device;
144 int i;
145
Roland Dreier65d470b2007-10-09 19:59:04 -0700146 inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!inuse)
148 return -ENOMEM;
149
150 list_for_each_entry(device, &device_list, core_list) {
151 if (!sscanf(device->name, name, &i))
152 continue;
153 if (i < 0 || i >= PAGE_SIZE * 8)
154 continue;
155 snprintf(buf, sizeof buf, name, i);
156 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
157 set_bit(i, inuse);
158 }
159
160 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
161 free_page((unsigned long) inuse);
162 snprintf(buf, sizeof buf, name, i);
163
164 if (__ib_device_get_by_name(buf))
165 return -ENFILE;
166
167 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
168 return 0;
169}
170
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600171static void ib_device_release(struct device *device)
172{
173 struct ib_device *dev = container_of(device, struct ib_device, dev);
174
Matan Barak03db3a22015-07-30 18:33:26 +0300175 ib_cache_release_one(dev);
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600176 kfree(dev->port_immutable);
177 kfree(dev);
178}
179
180static int ib_device_uevent(struct device *device,
181 struct kobj_uevent_env *env)
182{
183 struct ib_device *dev = container_of(device, struct ib_device, dev);
184
185 if (add_uevent_var(env, "NAME=%s", dev->name))
186 return -ENOMEM;
187
188 /*
189 * It would be nice to pass the node GUID with the event...
190 */
191
192 return 0;
193}
194
195static struct class ib_class = {
196 .name = "infiniband",
197 .dev_release = ib_device_release,
198 .dev_uevent = ib_device_uevent,
199};
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/**
202 * ib_alloc_device - allocate an IB device struct
203 * @size:size of structure to allocate
204 *
205 * Low-level drivers should use ib_alloc_device() to allocate &struct
206 * ib_device. @size is the size of the structure to be allocated,
207 * including any private data used by the low-level driver.
208 * ib_dealloc_device() must be used to free structures allocated with
209 * ib_alloc_device().
210 */
211struct ib_device *ib_alloc_device(size_t size)
212{
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600213 struct ib_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600215 if (WARN_ON(size < sizeof(struct ib_device)))
216 return NULL;
217
218 device = kzalloc(size, GFP_KERNEL);
219 if (!device)
220 return NULL;
221
222 device->dev.class = &ib_class;
223 device_initialize(&device->dev);
224
225 dev_set_drvdata(&device->dev, device);
226
227 INIT_LIST_HEAD(&device->event_handler_list);
228 spin_lock_init(&device->event_handler_lock);
229 spin_lock_init(&device->client_data_lock);
230 INIT_LIST_HEAD(&device->client_data_list);
231 INIT_LIST_HEAD(&device->port_list);
232
233 return device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235EXPORT_SYMBOL(ib_alloc_device);
236
237/**
238 * ib_dealloc_device - free an IB device struct
239 * @device:structure to free
240 *
241 * Free a structure allocated with ib_alloc_device().
242 */
243void ib_dealloc_device(struct ib_device *device)
244{
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600245 WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
246 device->reg_state != IB_DEV_UNINITIALIZED);
Roland Dreier9206dff2009-02-25 13:27:46 -0800247 kobject_put(&device->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249EXPORT_SYMBOL(ib_dealloc_device);
250
251static int add_client_context(struct ib_device *device, struct ib_client *client)
252{
253 struct ib_client_data *context;
254 unsigned long flags;
255
256 context = kmalloc(sizeof *context, GFP_KERNEL);
257 if (!context) {
258 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
259 device->name, client->name);
260 return -ENOMEM;
261 }
262
263 context->client = client;
264 context->data = NULL;
Haggai Eran7c1eb452015-07-30 17:50:14 +0300265 context->going_down = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Haggai Eran7c1eb452015-07-30 17:50:14 +0300267 down_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_lock_irqsave(&device->client_data_lock, flags);
269 list_add(&context->list, &device->client_data_list);
270 spin_unlock_irqrestore(&device->client_data_lock, flags);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300271 up_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 return 0;
274}
275
Ira Weiny337877a2015-06-06 14:38:29 -0400276static int verify_immutable(const struct ib_device *dev, u8 port)
277{
278 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
279 rdma_max_mad_size(dev, port) != 0);
280}
281
Ira Weiny77386132015-05-13 20:02:58 -0400282static int read_port_immutable(struct ib_device *device)
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300283{
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600284 int ret;
Ira Weiny77386132015-05-13 20:02:58 -0400285 u8 start_port = rdma_start_port(device);
286 u8 end_port = rdma_end_port(device);
287 u8 port;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300288
Ira Weiny77386132015-05-13 20:02:58 -0400289 /**
290 * device->port_immutable is indexed directly by the port number to make
291 * access to this data as efficient as possible.
292 *
293 * Therefore port_immutable is declared as a 1 based array with
294 * potential empty slots at the beginning.
295 */
296 device->port_immutable = kzalloc(sizeof(*device->port_immutable)
297 * (end_port + 1),
298 GFP_KERNEL);
299 if (!device->port_immutable)
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600300 return -ENOMEM;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300301
Ira Weiny77386132015-05-13 20:02:58 -0400302 for (port = start_port; port <= end_port; ++port) {
303 ret = device->get_port_immutable(device, port,
304 &device->port_immutable[port]);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300305 if (ret)
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600306 return ret;
Ira Weiny337877a2015-06-06 14:38:29 -0400307
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600308 if (verify_immutable(device, port))
309 return -EINVAL;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300310 }
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600311 return 0;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/**
315 * ib_register_device - Register an IB device with IB core
316 * @device:Device to register
317 *
318 * Low-level drivers use ib_register_device() to register their
319 * devices with the IB core. All registered clients will receive a
320 * callback for each device that is added. @device must be allocated
321 * with ib_alloc_device().
322 */
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700323int ib_register_device(struct ib_device *device,
324 int (*port_callback)(struct ib_device *,
325 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int ret;
Doug Ledfordb8071ad2015-08-15 10:16:14 -0400328 struct ib_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Ingo Molnar95ed6442006-01-13 14:51:39 -0800330 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (strchr(device->name, '%')) {
333 ret = alloc_name(device->name);
334 if (ret)
335 goto out;
336 }
337
338 if (ib_device_check_mandatory(device)) {
339 ret = -EINVAL;
340 goto out;
341 }
342
Ira Weiny77386132015-05-13 20:02:58 -0400343 ret = read_port_immutable(device);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300344 if (ret) {
Ira Weiny77386132015-05-13 20:02:58 -0400345 printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300346 device->name);
347 goto out;
348 }
349
Matan Barak03db3a22015-07-30 18:33:26 +0300350 ret = ib_cache_setup_one(device);
351 if (ret) {
352 printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
353 goto out;
354 }
355
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700356 ret = ib_device_register_sysfs(device, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (ret) {
358 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
359 device->name);
Matan Barak03db3a22015-07-30 18:33:26 +0300360 ib_cache_cleanup_one(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto out;
362 }
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 device->reg_state = IB_DEV_REGISTERED;
365
Doug Ledfordb8071ad2015-08-15 10:16:14 -0400366 list_for_each_entry(client, &client_list, list)
367 if (client->add && !add_client_context(device, client))
368 client->add(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300370 down_write(&lists_rwsem);
371 list_add_tail(&device->core_list, &device_list);
372 up_write(&lists_rwsem);
373out:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800374 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return ret;
376}
377EXPORT_SYMBOL(ib_register_device);
378
379/**
380 * ib_unregister_device - Unregister an IB device
381 * @device:Device to unregister
382 *
383 * Unregister an IB device. All clients will receive a remove callback.
384 */
385void ib_unregister_device(struct ib_device *device)
386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct ib_client_data *context, *tmp;
388 unsigned long flags;
389
Ingo Molnar95ed6442006-01-13 14:51:39 -0800390 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300392 down_write(&lists_rwsem);
393 list_del(&device->core_list);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300394 spin_lock_irqsave(&device->client_data_lock, flags);
395 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
396 context->going_down = true;
397 spin_unlock_irqrestore(&device->client_data_lock, flags);
398 downgrade_write(&lists_rwsem);
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300399
Haggai Eran7c1eb452015-07-30 17:50:14 +0300400 list_for_each_entry_safe(context, tmp, &device->client_data_list,
401 list) {
402 if (context->client->remove)
403 context->client->remove(device, context->data);
404 }
405 up_read(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Ingo Molnar95ed6442006-01-13 14:51:39 -0800407 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Roland Dreier9206dff2009-02-25 13:27:46 -0800409 ib_device_unregister_sysfs(device);
Matan Barak03db3a22015-07-30 18:33:26 +0300410 ib_cache_cleanup_one(device);
Roland Dreier9206dff2009-02-25 13:27:46 -0800411
Haggai Eran7c1eb452015-07-30 17:50:14 +0300412 down_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 spin_lock_irqsave(&device->client_data_lock, flags);
414 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
415 kfree(context);
416 spin_unlock_irqrestore(&device->client_data_lock, flags);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300417 up_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 device->reg_state = IB_DEV_UNREGISTERED;
420}
421EXPORT_SYMBOL(ib_unregister_device);
422
423/**
424 * ib_register_client - Register an IB client
425 * @client:Client to register
426 *
427 * Upper level users of the IB drivers can use ib_register_client() to
428 * register callbacks for IB device addition and removal. When an IB
429 * device is added, each registered client's add method will be called
430 * (in the order the clients were registered), and when a device is
431 * removed, each client's remove method will be called (in the reverse
432 * order that clients were registered). In addition, when
433 * ib_register_client() is called, the client will receive an add
434 * callback for all devices already registered.
435 */
436int ib_register_client(struct ib_client *client)
437{
438 struct ib_device *device;
439
Ingo Molnar95ed6442006-01-13 14:51:39 -0800440 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 list_for_each_entry(device, &device_list, core_list)
443 if (client->add && !add_client_context(device, client))
444 client->add(device);
445
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300446 down_write(&lists_rwsem);
447 list_add_tail(&client->list, &client_list);
448 up_write(&lists_rwsem);
449
Ingo Molnar95ed6442006-01-13 14:51:39 -0800450 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return 0;
453}
454EXPORT_SYMBOL(ib_register_client);
455
456/**
457 * ib_unregister_client - Unregister an IB client
458 * @client:Client to unregister
459 *
460 * Upper level users use ib_unregister_client() to remove their client
461 * registration. When ib_unregister_client() is called, the client
462 * will receive a remove callback for each IB device still registered.
463 */
464void ib_unregister_client(struct ib_client *client)
465{
466 struct ib_client_data *context, *tmp;
467 struct ib_device *device;
468 unsigned long flags;
469
Ingo Molnar95ed6442006-01-13 14:51:39 -0800470 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300472 down_write(&lists_rwsem);
473 list_del(&client->list);
474 up_write(&lists_rwsem);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 list_for_each_entry(device, &device_list, core_list) {
Haggai Eran7c1eb452015-07-30 17:50:14 +0300477 struct ib_client_data *found_context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Haggai Eran7c1eb452015-07-30 17:50:14 +0300479 down_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 spin_lock_irqsave(&device->client_data_lock, flags);
481 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
482 if (context->client == client) {
Haggai Eran7c1eb452015-07-30 17:50:14 +0300483 context->going_down = true;
484 found_context = context;
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 spin_unlock_irqrestore(&device->client_data_lock, flags);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300488 up_write(&lists_rwsem);
489
490 if (client->remove)
491 client->remove(device, found_context ?
492 found_context->data : NULL);
493
494 if (!found_context) {
495 pr_warn("No client context found for %s/%s\n",
496 device->name, client->name);
497 continue;
498 }
499
500 down_write(&lists_rwsem);
501 spin_lock_irqsave(&device->client_data_lock, flags);
502 list_del(&found_context->list);
503 kfree(found_context);
504 spin_unlock_irqrestore(&device->client_data_lock, flags);
505 up_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ingo Molnar95ed6442006-01-13 14:51:39 -0800508 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510EXPORT_SYMBOL(ib_unregister_client);
511
512/**
513 * ib_get_client_data - Get IB client context
514 * @device:Device to get context for
515 * @client:Client to get context for
516 *
517 * ib_get_client_data() returns client context set with
518 * ib_set_client_data().
519 */
520void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
521{
522 struct ib_client_data *context;
523 void *ret = NULL;
524 unsigned long flags;
525
526 spin_lock_irqsave(&device->client_data_lock, flags);
527 list_for_each_entry(context, &device->client_data_list, list)
528 if (context->client == client) {
529 ret = context->data;
530 break;
531 }
532 spin_unlock_irqrestore(&device->client_data_lock, flags);
533
534 return ret;
535}
536EXPORT_SYMBOL(ib_get_client_data);
537
538/**
Krishna Kumar9cd330d2006-09-22 15:22:58 -0700539 * ib_set_client_data - Set IB client context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * @device:Device to set context for
541 * @client:Client to set context for
542 * @data:Context to set
543 *
544 * ib_set_client_data() sets client context that can be retrieved with
545 * ib_get_client_data().
546 */
547void ib_set_client_data(struct ib_device *device, struct ib_client *client,
548 void *data)
549{
550 struct ib_client_data *context;
551 unsigned long flags;
552
553 spin_lock_irqsave(&device->client_data_lock, flags);
554 list_for_each_entry(context, &device->client_data_list, list)
555 if (context->client == client) {
556 context->data = data;
557 goto out;
558 }
559
560 printk(KERN_WARNING "No client context found for %s/%s\n",
561 device->name, client->name);
562
563out:
564 spin_unlock_irqrestore(&device->client_data_lock, flags);
565}
566EXPORT_SYMBOL(ib_set_client_data);
567
568/**
569 * ib_register_event_handler - Register an IB event handler
570 * @event_handler:Handler to register
571 *
572 * ib_register_event_handler() registers an event handler that will be
573 * called back when asynchronous IB events occur (as defined in
574 * chapter 11 of the InfiniBand Architecture Specification). This
575 * callback may occur in interrupt context.
576 */
577int ib_register_event_handler (struct ib_event_handler *event_handler)
578{
579 unsigned long flags;
580
581 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
582 list_add_tail(&event_handler->list,
583 &event_handler->device->event_handler_list);
584 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
585
586 return 0;
587}
588EXPORT_SYMBOL(ib_register_event_handler);
589
590/**
591 * ib_unregister_event_handler - Unregister an event handler
592 * @event_handler:Handler to unregister
593 *
594 * Unregister an event handler registered with
595 * ib_register_event_handler().
596 */
597int ib_unregister_event_handler(struct ib_event_handler *event_handler)
598{
599 unsigned long flags;
600
601 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
602 list_del(&event_handler->list);
603 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
604
605 return 0;
606}
607EXPORT_SYMBOL(ib_unregister_event_handler);
608
609/**
610 * ib_dispatch_event - Dispatch an asynchronous event
611 * @event:Event to dispatch
612 *
613 * Low-level drivers must call ib_dispatch_event() to dispatch the
614 * event to all registered event handlers when an asynchronous event
615 * occurs.
616 */
617void ib_dispatch_event(struct ib_event *event)
618{
619 unsigned long flags;
620 struct ib_event_handler *handler;
621
622 spin_lock_irqsave(&event->device->event_handler_lock, flags);
623
624 list_for_each_entry(handler, &event->device->event_handler_list, list)
625 handler->handler(handler, event);
626
627 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
628}
629EXPORT_SYMBOL(ib_dispatch_event);
630
631/**
632 * ib_query_device - Query IB device attributes
633 * @device:Device to query
634 * @device_attr:Device attributes
635 *
636 * ib_query_device() returns the attributes of a device through the
637 * @device_attr pointer.
638 */
639int ib_query_device(struct ib_device *device,
640 struct ib_device_attr *device_attr)
641{
Matan Barak2528e332015-06-11 16:35:25 +0300642 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
643
Matan Barak24306dc2015-06-11 16:35:24 +0300644 memset(device_attr, 0, sizeof(*device_attr));
645
Matan Barak2528e332015-06-11 16:35:25 +0300646 return device->query_device(device, device_attr, &uhw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648EXPORT_SYMBOL(ib_query_device);
649
650/**
651 * ib_query_port - Query IB port attributes
652 * @device:Device to query
653 * @port_num:Port number to query
654 * @port_attr:Port attributes
655 *
656 * ib_query_port() returns the attributes of a port through the
657 * @port_attr pointer.
658 */
659int ib_query_port(struct ib_device *device,
660 u8 port_num,
661 struct ib_port_attr *port_attr)
662{
Ira Weiny0cf18d72015-05-13 20:02:55 -0400663 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700664 return -EINVAL;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return device->query_port(device, port_num, port_attr);
667}
668EXPORT_SYMBOL(ib_query_port);
669
670/**
671 * ib_query_gid - Get GID table entry
672 * @device:Device to query
673 * @port_num:Port number to query
674 * @index:GID table index to query
675 * @gid:Returned GID
Matan Barak55ee3ab2015-10-15 18:38:45 +0300676 * @attr: Returned GID attributes related to this GID index (only in RoCE).
677 * NULL means ignore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *
679 * ib_query_gid() fetches the specified GID table entry.
680 */
681int ib_query_gid(struct ib_device *device,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300682 u8 port_num, int index, union ib_gid *gid,
683 struct ib_gid_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Matan Barak03db3a22015-07-30 18:33:26 +0300685 if (rdma_cap_roce_gid_table(device, port_num))
Matan Barak55ee3ab2015-10-15 18:38:45 +0300686 return ib_get_cached_gid(device, port_num, index, gid, attr);
687
688 if (attr)
689 return -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return device->query_gid(device, port_num, index, gid);
692}
693EXPORT_SYMBOL(ib_query_gid);
694
695/**
Matan Barak03db3a22015-07-30 18:33:26 +0300696 * ib_enum_roce_netdev - enumerate all RoCE ports
697 * @ib_dev : IB device we want to query
698 * @filter: Should we call the callback?
699 * @filter_cookie: Cookie passed to filter
700 * @cb: Callback to call for each found RoCE ports
701 * @cookie: Cookie passed back to the callback
702 *
703 * Enumerates all of the physical RoCE ports of ib_dev
704 * which are related to netdevice and calls callback() on each
705 * device for which filter() function returns non zero.
706 */
707void ib_enum_roce_netdev(struct ib_device *ib_dev,
708 roce_netdev_filter filter,
709 void *filter_cookie,
710 roce_netdev_callback cb,
711 void *cookie)
712{
713 u8 port;
714
715 for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
716 port++)
717 if (rdma_protocol_roce(ib_dev, port)) {
718 struct net_device *idev = NULL;
719
720 if (ib_dev->get_netdev)
721 idev = ib_dev->get_netdev(ib_dev, port);
722
723 if (idev &&
724 idev->reg_state >= NETREG_UNREGISTERED) {
725 dev_put(idev);
726 idev = NULL;
727 }
728
729 if (filter(ib_dev, port, idev, filter_cookie))
730 cb(ib_dev, port, idev, cookie);
731
732 if (idev)
733 dev_put(idev);
734 }
735}
736
737/**
738 * ib_enum_all_roce_netdevs - enumerate all RoCE devices
739 * @filter: Should we call the callback?
740 * @filter_cookie: Cookie passed to filter
741 * @cb: Callback to call for each found RoCE ports
742 * @cookie: Cookie passed back to the callback
743 *
744 * Enumerates all RoCE devices' physical ports which are related
745 * to netdevices and calls callback() on each device for which
746 * filter() function returns non zero.
747 */
748void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
749 void *filter_cookie,
750 roce_netdev_callback cb,
751 void *cookie)
752{
753 struct ib_device *dev;
754
755 down_read(&lists_rwsem);
756 list_for_each_entry(dev, &device_list, core_list)
757 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
758 up_read(&lists_rwsem);
759}
760
761/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * ib_query_pkey - Get P_Key table entry
763 * @device:Device to query
764 * @port_num:Port number to query
765 * @index:P_Key table index to query
766 * @pkey:Returned P_Key
767 *
768 * ib_query_pkey() fetches the specified P_Key table entry.
769 */
770int ib_query_pkey(struct ib_device *device,
771 u8 port_num, u16 index, u16 *pkey)
772{
773 return device->query_pkey(device, port_num, index, pkey);
774}
775EXPORT_SYMBOL(ib_query_pkey);
776
777/**
778 * ib_modify_device - Change IB device attributes
779 * @device:Device to modify
780 * @device_modify_mask:Mask of attributes to change
781 * @device_modify:New attribute values
782 *
783 * ib_modify_device() changes a device's attributes as specified by
784 * the @device_modify_mask and @device_modify structure.
785 */
786int ib_modify_device(struct ib_device *device,
787 int device_modify_mask,
788 struct ib_device_modify *device_modify)
789{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000790 if (!device->modify_device)
791 return -ENOSYS;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return device->modify_device(device, device_modify_mask,
794 device_modify);
795}
796EXPORT_SYMBOL(ib_modify_device);
797
798/**
799 * ib_modify_port - Modifies the attributes for the specified port.
800 * @device: The device to modify.
801 * @port_num: The number of the port to modify.
802 * @port_modify_mask: Mask used to specify which attributes of the port
803 * to change.
804 * @port_modify: New attribute values for the port.
805 *
806 * ib_modify_port() changes a port's attributes as specified by the
807 * @port_modify_mask and @port_modify structure.
808 */
809int ib_modify_port(struct ib_device *device,
810 u8 port_num, int port_modify_mask,
811 struct ib_port_modify *port_modify)
812{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000813 if (!device->modify_port)
814 return -ENOSYS;
815
Ira Weiny0cf18d72015-05-13 20:02:55 -0400816 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700817 return -EINVAL;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return device->modify_port(device, port_num, port_modify_mask,
820 port_modify);
821}
822EXPORT_SYMBOL(ib_modify_port);
823
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300824/**
825 * ib_find_gid - Returns the port number and GID table index where
826 * a specified GID value occurs.
827 * @device: The device to query.
828 * @gid: The GID value to search for.
Matan Barak55ee3ab2015-10-15 18:38:45 +0300829 * @ndev: The ndev related to the GID to search for.
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300830 * @port_num: The port number of the device where the GID value was found.
831 * @index: The index into the GID table where the GID was found. This
832 * parameter may be NULL.
833 */
834int ib_find_gid(struct ib_device *device, union ib_gid *gid,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300835 struct net_device *ndev, u8 *port_num, u16 *index)
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300836{
837 union ib_gid tmp_gid;
838 int ret, port, i;
839
Ira Weiny0cf18d72015-05-13 20:02:55 -0400840 for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
Matan Barak03db3a22015-07-30 18:33:26 +0300841 if (rdma_cap_roce_gid_table(device, port)) {
Matan Barakd300ec52015-10-15 18:38:46 +0300842 if (!ib_find_cached_gid_by_port(device, gid, port,
843 ndev, index)) {
Matan Barak03db3a22015-07-30 18:33:26 +0300844 *port_num = port;
845 return 0;
Dan Carpenter98d25af2015-08-18 12:22:10 +0300846 }
Matan Barak03db3a22015-07-30 18:33:26 +0300847 }
848
Ira Weiny77386132015-05-13 20:02:58 -0400849 for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
Matan Barak55ee3ab2015-10-15 18:38:45 +0300850 ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300851 if (ret)
852 return ret;
853 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
854 *port_num = port;
855 if (index)
856 *index = i;
857 return 0;
858 }
859 }
860 }
861
862 return -ENOENT;
863}
864EXPORT_SYMBOL(ib_find_gid);
865
866/**
867 * ib_find_pkey - Returns the PKey table index where a specified
868 * PKey value occurs.
869 * @device: The device to query.
870 * @port_num: The port number of the device to search for the PKey.
871 * @pkey: The PKey value to search for.
872 * @index: The index into the PKey table where the PKey was found.
873 */
874int ib_find_pkey(struct ib_device *device,
875 u8 port_num, u16 pkey, u16 *index)
876{
877 int ret, i;
878 u16 tmp_pkey;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000879 int partial_ix = -1;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300880
Ira Weiny77386132015-05-13 20:02:58 -0400881 for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300882 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
883 if (ret)
884 return ret;
Moni Shoua36026ec2007-07-23 10:07:42 +0300885 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000886 /* if there is full-member pkey take it.*/
887 if (tmp_pkey & 0x8000) {
888 *index = i;
889 return 0;
890 }
891 if (partial_ix < 0)
892 partial_ix = i;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300893 }
894 }
895
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000896 /*no full-member, if exists take the limited*/
897 if (partial_ix >= 0) {
898 *index = partial_ix;
899 return 0;
900 }
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300901 return -ENOENT;
902}
903EXPORT_SYMBOL(ib_find_pkey);
904
Yotam Kenneth9268f722015-07-30 17:50:15 +0300905/**
906 * ib_get_net_dev_by_params() - Return the appropriate net_dev
907 * for a received CM request
908 * @dev: An RDMA device on which the request has been received.
909 * @port: Port number on the RDMA device.
910 * @pkey: The Pkey the request came on.
911 * @gid: A GID that the net_dev uses to communicate.
912 * @addr: Contains the IP address that the request specified as its
913 * destination.
914 */
915struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
916 u8 port,
917 u16 pkey,
918 const union ib_gid *gid,
919 const struct sockaddr *addr)
920{
921 struct net_device *net_dev = NULL;
922 struct ib_client_data *context;
923
924 if (!rdma_protocol_ib(dev, port))
925 return NULL;
926
927 down_read(&lists_rwsem);
928
929 list_for_each_entry(context, &dev->client_data_list, list) {
930 struct ib_client *client = context->client;
931
932 if (context->going_down)
933 continue;
934
935 if (client->get_net_dev_by_params) {
936 net_dev = client->get_net_dev_by_params(dev, port, pkey,
937 gid, addr,
938 context->data);
939 if (net_dev)
940 break;
941 }
942 }
943
944 up_read(&lists_rwsem);
945
946 return net_dev;
947}
948EXPORT_SYMBOL(ib_get_net_dev_by_params);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int __init ib_core_init(void)
951{
952 int ret;
953
Tejun Heof0626712010-10-19 15:24:36 +0000954 ib_wq = alloc_workqueue("infiniband", 0, 0);
955 if (!ib_wq)
956 return -ENOMEM;
957
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800958 ib_comp_wq = alloc_workqueue("ib-comp-wq",
959 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
960 WQ_UNBOUND_MAX_ACTIVE);
961 if (!ib_comp_wq) {
962 ret = -ENOMEM;
963 goto err;
964 }
965
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600966 ret = class_register(&ib_class);
Nir Muchtarfd75c782011-05-20 11:46:10 -0700967 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800969 goto err_comp;
Nir Muchtarfd75c782011-05-20 11:46:10 -0700970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Roland Dreierb2cbae22011-05-20 11:46:11 -0700972 ret = ibnl_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (ret) {
Roland Dreierb2cbae22011-05-20 11:46:11 -0700974 printk(KERN_WARNING "Couldn't init IB netlink interface\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700975 goto err_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Matan Barak03db3a22015-07-30 18:33:26 +0300978 ib_cache_setup();
Roland Dreierb2cbae22011-05-20 11:46:11 -0700979
Nir Muchtarfd75c782011-05-20 11:46:10 -0700980 return 0;
981
982err_sysfs:
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600983 class_unregister(&ib_class);
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800984err_comp:
985 destroy_workqueue(ib_comp_wq);
Nir Muchtarfd75c782011-05-20 11:46:10 -0700986err:
987 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return ret;
989}
990
991static void __exit ib_core_cleanup(void)
992{
993 ib_cache_cleanup();
Roland Dreierb2cbae22011-05-20 11:46:11 -0700994 ibnl_cleanup();
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600995 class_unregister(&ib_class);
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800996 destroy_workqueue(ib_comp_wq);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800997 /* Make sure that any pending umem accounting work is done. */
Tejun Heof0626712010-10-19 15:24:36 +0000998 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001module_init(ib_core_init);
1002module_exit(ib_core_cleanup);