blob: 568592e31b443c979928b77a77edfe61ce1ce253 [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
Tejun Heof0626712010-10-19 15:24:36 +000061struct workqueue_struct *ib_wq;
62EXPORT_SYMBOL_GPL(ib_wq);
63
Haggai Eran5aa44bb2015-07-30 17:50:13 +030064/* The device_list and client_list contain devices and clients after their
65 * registration has completed, and the devices and clients are removed
66 * during unregistration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static LIST_HEAD(device_list);
68static LIST_HEAD(client_list);
69
70/*
Haggai Eran5aa44bb2015-07-30 17:50:13 +030071 * device_mutex and lists_rwsem protect access to both device_list and
72 * client_list. device_mutex protects writer access by device and client
73 * registration / de-registration. lists_rwsem protects reader access to
74 * these lists. Iterators of these lists must lock it for read, while updates
75 * to the lists must be done with a write lock. A special case is when the
76 * device_mutex is locked. In this case locking the lists for read access is
77 * not necessary as the device_mutex implies it.
Haggai Eran7c1eb452015-07-30 17:50:14 +030078 *
79 * lists_rwsem also protects access to the client data list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Ingo Molnar95ed6442006-01-13 14:51:39 -080081static DEFINE_MUTEX(device_mutex);
Haggai Eran5aa44bb2015-07-30 17:50:13 +030082static DECLARE_RWSEM(lists_rwsem);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static int ib_device_check_mandatory(struct ib_device *device)
86{
87#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
88 static const struct {
89 size_t offset;
90 char *name;
91 } mandatory_table[] = {
92 IB_MANDATORY_FUNC(query_device),
93 IB_MANDATORY_FUNC(query_port),
94 IB_MANDATORY_FUNC(query_pkey),
95 IB_MANDATORY_FUNC(query_gid),
96 IB_MANDATORY_FUNC(alloc_pd),
97 IB_MANDATORY_FUNC(dealloc_pd),
98 IB_MANDATORY_FUNC(create_ah),
99 IB_MANDATORY_FUNC(destroy_ah),
100 IB_MANDATORY_FUNC(create_qp),
101 IB_MANDATORY_FUNC(modify_qp),
102 IB_MANDATORY_FUNC(destroy_qp),
103 IB_MANDATORY_FUNC(post_send),
104 IB_MANDATORY_FUNC(post_recv),
105 IB_MANDATORY_FUNC(create_cq),
106 IB_MANDATORY_FUNC(destroy_cq),
107 IB_MANDATORY_FUNC(poll_cq),
108 IB_MANDATORY_FUNC(req_notify_cq),
109 IB_MANDATORY_FUNC(get_dma_mr),
Ira Weiny77386132015-05-13 20:02:58 -0400110 IB_MANDATORY_FUNC(dereg_mr),
111 IB_MANDATORY_FUNC(get_port_immutable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 };
113 int i;
114
Ahmed S. Darwish9a6b0902007-02-06 18:07:25 +0200115 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
117 printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
118 device->name, mandatory_table[i].name);
119 return -EINVAL;
120 }
121 }
122
123 return 0;
124}
125
126static struct ib_device *__ib_device_get_by_name(const char *name)
127{
128 struct ib_device *device;
129
130 list_for_each_entry(device, &device_list, core_list)
131 if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
132 return device;
133
134 return NULL;
135}
136
137
138static int alloc_name(char *name)
139{
Roland Dreier65d470b2007-10-09 19:59:04 -0700140 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 char buf[IB_DEVICE_NAME_MAX];
142 struct ib_device *device;
143 int i;
144
Roland Dreier65d470b2007-10-09 19:59:04 -0700145 inuse = (unsigned long *) get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!inuse)
147 return -ENOMEM;
148
149 list_for_each_entry(device, &device_list, core_list) {
150 if (!sscanf(device->name, name, &i))
151 continue;
152 if (i < 0 || i >= PAGE_SIZE * 8)
153 continue;
154 snprintf(buf, sizeof buf, name, i);
155 if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
156 set_bit(i, inuse);
157 }
158
159 i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
160 free_page((unsigned long) inuse);
161 snprintf(buf, sizeof buf, name, i);
162
163 if (__ib_device_get_by_name(buf))
164 return -ENFILE;
165
166 strlcpy(name, buf, IB_DEVICE_NAME_MAX);
167 return 0;
168}
169
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600170static void ib_device_release(struct device *device)
171{
172 struct ib_device *dev = container_of(device, struct ib_device, dev);
173
Matan Barak03db3a22015-07-30 18:33:26 +0300174 ib_cache_release_one(dev);
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600175 kfree(dev->port_immutable);
176 kfree(dev);
177}
178
179static int ib_device_uevent(struct device *device,
180 struct kobj_uevent_env *env)
181{
182 struct ib_device *dev = container_of(device, struct ib_device, dev);
183
184 if (add_uevent_var(env, "NAME=%s", dev->name))
185 return -ENOMEM;
186
187 /*
188 * It would be nice to pass the node GUID with the event...
189 */
190
191 return 0;
192}
193
194static struct class ib_class = {
195 .name = "infiniband",
196 .dev_release = ib_device_release,
197 .dev_uevent = ib_device_uevent,
198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/**
201 * ib_alloc_device - allocate an IB device struct
202 * @size:size of structure to allocate
203 *
204 * Low-level drivers should use ib_alloc_device() to allocate &struct
205 * ib_device. @size is the size of the structure to be allocated,
206 * including any private data used by the low-level driver.
207 * ib_dealloc_device() must be used to free structures allocated with
208 * ib_alloc_device().
209 */
210struct ib_device *ib_alloc_device(size_t size)
211{
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600212 struct ib_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600214 if (WARN_ON(size < sizeof(struct ib_device)))
215 return NULL;
216
217 device = kzalloc(size, GFP_KERNEL);
218 if (!device)
219 return NULL;
220
221 device->dev.class = &ib_class;
222 device_initialize(&device->dev);
223
224 dev_set_drvdata(&device->dev, device);
225
226 INIT_LIST_HEAD(&device->event_handler_list);
227 spin_lock_init(&device->event_handler_lock);
228 spin_lock_init(&device->client_data_lock);
229 INIT_LIST_HEAD(&device->client_data_list);
230 INIT_LIST_HEAD(&device->port_list);
231
232 return device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234EXPORT_SYMBOL(ib_alloc_device);
235
236/**
237 * ib_dealloc_device - free an IB device struct
238 * @device:structure to free
239 *
240 * Free a structure allocated with ib_alloc_device().
241 */
242void ib_dealloc_device(struct ib_device *device)
243{
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600244 WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
245 device->reg_state != IB_DEV_UNINITIALIZED);
Roland Dreier9206dff2009-02-25 13:27:46 -0800246 kobject_put(&device->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248EXPORT_SYMBOL(ib_dealloc_device);
249
250static int add_client_context(struct ib_device *device, struct ib_client *client)
251{
252 struct ib_client_data *context;
253 unsigned long flags;
254
255 context = kmalloc(sizeof *context, GFP_KERNEL);
256 if (!context) {
257 printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
258 device->name, client->name);
259 return -ENOMEM;
260 }
261
262 context->client = client;
263 context->data = NULL;
Haggai Eran7c1eb452015-07-30 17:50:14 +0300264 context->going_down = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Haggai Eran7c1eb452015-07-30 17:50:14 +0300266 down_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_lock_irqsave(&device->client_data_lock, flags);
268 list_add(&context->list, &device->client_data_list);
269 spin_unlock_irqrestore(&device->client_data_lock, flags);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300270 up_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return 0;
273}
274
Ira Weiny337877a2015-06-06 14:38:29 -0400275static int verify_immutable(const struct ib_device *dev, u8 port)
276{
277 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
278 rdma_max_mad_size(dev, port) != 0);
279}
280
Ira Weiny77386132015-05-13 20:02:58 -0400281static int read_port_immutable(struct ib_device *device)
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300282{
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600283 int ret;
Ira Weiny77386132015-05-13 20:02:58 -0400284 u8 start_port = rdma_start_port(device);
285 u8 end_port = rdma_end_port(device);
286 u8 port;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300287
Ira Weiny77386132015-05-13 20:02:58 -0400288 /**
289 * device->port_immutable is indexed directly by the port number to make
290 * access to this data as efficient as possible.
291 *
292 * Therefore port_immutable is declared as a 1 based array with
293 * potential empty slots at the beginning.
294 */
295 device->port_immutable = kzalloc(sizeof(*device->port_immutable)
296 * (end_port + 1),
297 GFP_KERNEL);
298 if (!device->port_immutable)
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600299 return -ENOMEM;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300300
Ira Weiny77386132015-05-13 20:02:58 -0400301 for (port = start_port; port <= end_port; ++port) {
302 ret = device->get_port_immutable(device, port,
303 &device->port_immutable[port]);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300304 if (ret)
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600305 return ret;
Ira Weiny337877a2015-06-06 14:38:29 -0400306
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600307 if (verify_immutable(device, port))
308 return -EINVAL;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300309 }
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600310 return 0;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/**
314 * ib_register_device - Register an IB device with IB core
315 * @device:Device to register
316 *
317 * Low-level drivers use ib_register_device() to register their
318 * devices with the IB core. All registered clients will receive a
319 * callback for each device that is added. @device must be allocated
320 * with ib_alloc_device().
321 */
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700322int ib_register_device(struct ib_device *device,
323 int (*port_callback)(struct ib_device *,
324 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int ret;
Doug Ledfordb8071ad2015-08-15 10:16:14 -0400327 struct ib_client *client;
Ira Weiny3e153a92015-12-18 10:59:44 +0200328 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
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
Ira Weiny3e153a92015-12-18 10:59:44 +0200356 memset(&device->attrs, 0, sizeof(device->attrs));
357 ret = device->query_device(device, &device->attrs, &uhw);
358 if (ret) {
359 printk(KERN_WARNING "Couldn't query the device attributes\n");
360 goto out;
361 }
362
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700363 ret = ib_device_register_sysfs(device, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (ret) {
365 printk(KERN_WARNING "Couldn't register device %s with driver model\n",
366 device->name);
Matan Barak03db3a22015-07-30 18:33:26 +0300367 ib_cache_cleanup_one(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto out;
369 }
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 device->reg_state = IB_DEV_REGISTERED;
372
Doug Ledfordb8071ad2015-08-15 10:16:14 -0400373 list_for_each_entry(client, &client_list, list)
374 if (client->add && !add_client_context(device, client))
375 client->add(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300377 down_write(&lists_rwsem);
378 list_add_tail(&device->core_list, &device_list);
379 up_write(&lists_rwsem);
380out:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800381 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return ret;
383}
384EXPORT_SYMBOL(ib_register_device);
385
386/**
387 * ib_unregister_device - Unregister an IB device
388 * @device:Device to unregister
389 *
390 * Unregister an IB device. All clients will receive a remove callback.
391 */
392void ib_unregister_device(struct ib_device *device)
393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct ib_client_data *context, *tmp;
395 unsigned long flags;
396
Ingo Molnar95ed6442006-01-13 14:51:39 -0800397 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300399 down_write(&lists_rwsem);
400 list_del(&device->core_list);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300401 spin_lock_irqsave(&device->client_data_lock, flags);
402 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
403 context->going_down = true;
404 spin_unlock_irqrestore(&device->client_data_lock, flags);
405 downgrade_write(&lists_rwsem);
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300406
Haggai Eran7c1eb452015-07-30 17:50:14 +0300407 list_for_each_entry_safe(context, tmp, &device->client_data_list,
408 list) {
409 if (context->client->remove)
410 context->client->remove(device, context->data);
411 }
412 up_read(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Ingo Molnar95ed6442006-01-13 14:51:39 -0800414 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Roland Dreier9206dff2009-02-25 13:27:46 -0800416 ib_device_unregister_sysfs(device);
Matan Barak03db3a22015-07-30 18:33:26 +0300417 ib_cache_cleanup_one(device);
Roland Dreier9206dff2009-02-25 13:27:46 -0800418
Haggai Eran7c1eb452015-07-30 17:50:14 +0300419 down_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_lock_irqsave(&device->client_data_lock, flags);
421 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
422 kfree(context);
423 spin_unlock_irqrestore(&device->client_data_lock, flags);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300424 up_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 device->reg_state = IB_DEV_UNREGISTERED;
427}
428EXPORT_SYMBOL(ib_unregister_device);
429
430/**
431 * ib_register_client - Register an IB client
432 * @client:Client to register
433 *
434 * Upper level users of the IB drivers can use ib_register_client() to
435 * register callbacks for IB device addition and removal. When an IB
436 * device is added, each registered client's add method will be called
437 * (in the order the clients were registered), and when a device is
438 * removed, each client's remove method will be called (in the reverse
439 * order that clients were registered). In addition, when
440 * ib_register_client() is called, the client will receive an add
441 * callback for all devices already registered.
442 */
443int ib_register_client(struct ib_client *client)
444{
445 struct ib_device *device;
446
Ingo Molnar95ed6442006-01-13 14:51:39 -0800447 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 list_for_each_entry(device, &device_list, core_list)
450 if (client->add && !add_client_context(device, client))
451 client->add(device);
452
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300453 down_write(&lists_rwsem);
454 list_add_tail(&client->list, &client_list);
455 up_write(&lists_rwsem);
456
Ingo Molnar95ed6442006-01-13 14:51:39 -0800457 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 return 0;
460}
461EXPORT_SYMBOL(ib_register_client);
462
463/**
464 * ib_unregister_client - Unregister an IB client
465 * @client:Client to unregister
466 *
467 * Upper level users use ib_unregister_client() to remove their client
468 * registration. When ib_unregister_client() is called, the client
469 * will receive a remove callback for each IB device still registered.
470 */
471void ib_unregister_client(struct ib_client *client)
472{
473 struct ib_client_data *context, *tmp;
474 struct ib_device *device;
475 unsigned long flags;
476
Ingo Molnar95ed6442006-01-13 14:51:39 -0800477 mutex_lock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Haggai Eran5aa44bb2015-07-30 17:50:13 +0300479 down_write(&lists_rwsem);
480 list_del(&client->list);
481 up_write(&lists_rwsem);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 list_for_each_entry(device, &device_list, core_list) {
Haggai Eran7c1eb452015-07-30 17:50:14 +0300484 struct ib_client_data *found_context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Haggai Eran7c1eb452015-07-30 17:50:14 +0300486 down_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 spin_lock_irqsave(&device->client_data_lock, flags);
488 list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
489 if (context->client == client) {
Haggai Eran7c1eb452015-07-30 17:50:14 +0300490 context->going_down = true;
491 found_context = context;
492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 spin_unlock_irqrestore(&device->client_data_lock, flags);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300495 up_write(&lists_rwsem);
496
497 if (client->remove)
498 client->remove(device, found_context ?
499 found_context->data : NULL);
500
501 if (!found_context) {
502 pr_warn("No client context found for %s/%s\n",
503 device->name, client->name);
504 continue;
505 }
506
507 down_write(&lists_rwsem);
508 spin_lock_irqsave(&device->client_data_lock, flags);
509 list_del(&found_context->list);
510 kfree(found_context);
511 spin_unlock_irqrestore(&device->client_data_lock, flags);
512 up_write(&lists_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ingo Molnar95ed6442006-01-13 14:51:39 -0800515 mutex_unlock(&device_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517EXPORT_SYMBOL(ib_unregister_client);
518
519/**
520 * ib_get_client_data - Get IB client context
521 * @device:Device to get context for
522 * @client:Client to get context for
523 *
524 * ib_get_client_data() returns client context set with
525 * ib_set_client_data().
526 */
527void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
528{
529 struct ib_client_data *context;
530 void *ret = NULL;
531 unsigned long flags;
532
533 spin_lock_irqsave(&device->client_data_lock, flags);
534 list_for_each_entry(context, &device->client_data_list, list)
535 if (context->client == client) {
536 ret = context->data;
537 break;
538 }
539 spin_unlock_irqrestore(&device->client_data_lock, flags);
540
541 return ret;
542}
543EXPORT_SYMBOL(ib_get_client_data);
544
545/**
Krishna Kumar9cd330d2006-09-22 15:22:58 -0700546 * ib_set_client_data - Set IB client context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * @device:Device to set context for
548 * @client:Client to set context for
549 * @data:Context to set
550 *
551 * ib_set_client_data() sets client context that can be retrieved with
552 * ib_get_client_data().
553 */
554void ib_set_client_data(struct ib_device *device, struct ib_client *client,
555 void *data)
556{
557 struct ib_client_data *context;
558 unsigned long flags;
559
560 spin_lock_irqsave(&device->client_data_lock, flags);
561 list_for_each_entry(context, &device->client_data_list, list)
562 if (context->client == client) {
563 context->data = data;
564 goto out;
565 }
566
567 printk(KERN_WARNING "No client context found for %s/%s\n",
568 device->name, client->name);
569
570out:
571 spin_unlock_irqrestore(&device->client_data_lock, flags);
572}
573EXPORT_SYMBOL(ib_set_client_data);
574
575/**
576 * ib_register_event_handler - Register an IB event handler
577 * @event_handler:Handler to register
578 *
579 * ib_register_event_handler() registers an event handler that will be
580 * called back when asynchronous IB events occur (as defined in
581 * chapter 11 of the InfiniBand Architecture Specification). This
582 * callback may occur in interrupt context.
583 */
584int ib_register_event_handler (struct ib_event_handler *event_handler)
585{
586 unsigned long flags;
587
588 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
589 list_add_tail(&event_handler->list,
590 &event_handler->device->event_handler_list);
591 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
592
593 return 0;
594}
595EXPORT_SYMBOL(ib_register_event_handler);
596
597/**
598 * ib_unregister_event_handler - Unregister an event handler
599 * @event_handler:Handler to unregister
600 *
601 * Unregister an event handler registered with
602 * ib_register_event_handler().
603 */
604int ib_unregister_event_handler(struct ib_event_handler *event_handler)
605{
606 unsigned long flags;
607
608 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
609 list_del(&event_handler->list);
610 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
611
612 return 0;
613}
614EXPORT_SYMBOL(ib_unregister_event_handler);
615
616/**
617 * ib_dispatch_event - Dispatch an asynchronous event
618 * @event:Event to dispatch
619 *
620 * Low-level drivers must call ib_dispatch_event() to dispatch the
621 * event to all registered event handlers when an asynchronous event
622 * occurs.
623 */
624void ib_dispatch_event(struct ib_event *event)
625{
626 unsigned long flags;
627 struct ib_event_handler *handler;
628
629 spin_lock_irqsave(&event->device->event_handler_lock, flags);
630
631 list_for_each_entry(handler, &event->device->event_handler_list, list)
632 handler->handler(handler, event);
633
634 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
635}
636EXPORT_SYMBOL(ib_dispatch_event);
637
638/**
639 * ib_query_device - Query IB device attributes
640 * @device:Device to query
641 * @device_attr:Device attributes
642 *
643 * ib_query_device() returns the attributes of a device through the
644 * @device_attr pointer.
645 */
646int ib_query_device(struct ib_device *device,
647 struct ib_device_attr *device_attr)
648{
Matan Barak2528e332015-06-11 16:35:25 +0300649 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
650
Matan Barak24306dc2015-06-11 16:35:24 +0300651 memset(device_attr, 0, sizeof(*device_attr));
652
Matan Barak2528e332015-06-11 16:35:25 +0300653 return device->query_device(device, device_attr, &uhw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655EXPORT_SYMBOL(ib_query_device);
656
657/**
658 * ib_query_port - Query IB port attributes
659 * @device:Device to query
660 * @port_num:Port number to query
661 * @port_attr:Port attributes
662 *
663 * ib_query_port() returns the attributes of a port through the
664 * @port_attr pointer.
665 */
666int ib_query_port(struct ib_device *device,
667 u8 port_num,
668 struct ib_port_attr *port_attr)
669{
Ira Weiny0cf18d72015-05-13 20:02:55 -0400670 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700671 return -EINVAL;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return device->query_port(device, port_num, port_attr);
674}
675EXPORT_SYMBOL(ib_query_port);
676
677/**
678 * ib_query_gid - Get GID table entry
679 * @device:Device to query
680 * @port_num:Port number to query
681 * @index:GID table index to query
682 * @gid:Returned GID
Matan Barak55ee3ab2015-10-15 18:38:45 +0300683 * @attr: Returned GID attributes related to this GID index (only in RoCE).
684 * NULL means ignore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
686 * ib_query_gid() fetches the specified GID table entry.
687 */
688int ib_query_gid(struct ib_device *device,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300689 u8 port_num, int index, union ib_gid *gid,
690 struct ib_gid_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Matan Barak03db3a22015-07-30 18:33:26 +0300692 if (rdma_cap_roce_gid_table(device, port_num))
Matan Barak55ee3ab2015-10-15 18:38:45 +0300693 return ib_get_cached_gid(device, port_num, index, gid, attr);
694
695 if (attr)
696 return -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return device->query_gid(device, port_num, index, gid);
699}
700EXPORT_SYMBOL(ib_query_gid);
701
702/**
Matan Barak03db3a22015-07-30 18:33:26 +0300703 * ib_enum_roce_netdev - enumerate all RoCE ports
704 * @ib_dev : IB device we want to query
705 * @filter: Should we call the callback?
706 * @filter_cookie: Cookie passed to filter
707 * @cb: Callback to call for each found RoCE ports
708 * @cookie: Cookie passed back to the callback
709 *
710 * Enumerates all of the physical RoCE ports of ib_dev
711 * which are related to netdevice and calls callback() on each
712 * device for which filter() function returns non zero.
713 */
714void ib_enum_roce_netdev(struct ib_device *ib_dev,
715 roce_netdev_filter filter,
716 void *filter_cookie,
717 roce_netdev_callback cb,
718 void *cookie)
719{
720 u8 port;
721
722 for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
723 port++)
724 if (rdma_protocol_roce(ib_dev, port)) {
725 struct net_device *idev = NULL;
726
727 if (ib_dev->get_netdev)
728 idev = ib_dev->get_netdev(ib_dev, port);
729
730 if (idev &&
731 idev->reg_state >= NETREG_UNREGISTERED) {
732 dev_put(idev);
733 idev = NULL;
734 }
735
736 if (filter(ib_dev, port, idev, filter_cookie))
737 cb(ib_dev, port, idev, cookie);
738
739 if (idev)
740 dev_put(idev);
741 }
742}
743
744/**
745 * ib_enum_all_roce_netdevs - enumerate all RoCE devices
746 * @filter: Should we call the callback?
747 * @filter_cookie: Cookie passed to filter
748 * @cb: Callback to call for each found RoCE ports
749 * @cookie: Cookie passed back to the callback
750 *
751 * Enumerates all RoCE devices' physical ports which are related
752 * to netdevices and calls callback() on each device for which
753 * filter() function returns non zero.
754 */
755void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
756 void *filter_cookie,
757 roce_netdev_callback cb,
758 void *cookie)
759{
760 struct ib_device *dev;
761
762 down_read(&lists_rwsem);
763 list_for_each_entry(dev, &device_list, core_list)
764 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
765 up_read(&lists_rwsem);
766}
767
768/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 * ib_query_pkey - Get P_Key table entry
770 * @device:Device to query
771 * @port_num:Port number to query
772 * @index:P_Key table index to query
773 * @pkey:Returned P_Key
774 *
775 * ib_query_pkey() fetches the specified P_Key table entry.
776 */
777int ib_query_pkey(struct ib_device *device,
778 u8 port_num, u16 index, u16 *pkey)
779{
780 return device->query_pkey(device, port_num, index, pkey);
781}
782EXPORT_SYMBOL(ib_query_pkey);
783
784/**
785 * ib_modify_device - Change IB device attributes
786 * @device:Device to modify
787 * @device_modify_mask:Mask of attributes to change
788 * @device_modify:New attribute values
789 *
790 * ib_modify_device() changes a device's attributes as specified by
791 * the @device_modify_mask and @device_modify structure.
792 */
793int ib_modify_device(struct ib_device *device,
794 int device_modify_mask,
795 struct ib_device_modify *device_modify)
796{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000797 if (!device->modify_device)
798 return -ENOSYS;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return device->modify_device(device, device_modify_mask,
801 device_modify);
802}
803EXPORT_SYMBOL(ib_modify_device);
804
805/**
806 * ib_modify_port - Modifies the attributes for the specified port.
807 * @device: The device to modify.
808 * @port_num: The number of the port to modify.
809 * @port_modify_mask: Mask used to specify which attributes of the port
810 * to change.
811 * @port_modify: New attribute values for the port.
812 *
813 * ib_modify_port() changes a port's attributes as specified by the
814 * @port_modify_mask and @port_modify structure.
815 */
816int ib_modify_port(struct ib_device *device,
817 u8 port_num, int port_modify_mask,
818 struct ib_port_modify *port_modify)
819{
Bart Van Assche10e1b542011-06-18 16:35:42 +0000820 if (!device->modify_port)
821 return -ENOSYS;
822
Ira Weiny0cf18d72015-05-13 20:02:55 -0400823 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Roland Dreier116c0072005-10-03 09:32:33 -0700824 return -EINVAL;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return device->modify_port(device, port_num, port_modify_mask,
827 port_modify);
828}
829EXPORT_SYMBOL(ib_modify_port);
830
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300831/**
832 * ib_find_gid - Returns the port number and GID table index where
833 * a specified GID value occurs.
834 * @device: The device to query.
835 * @gid: The GID value to search for.
Matan Barak55ee3ab2015-10-15 18:38:45 +0300836 * @ndev: The ndev related to the GID to search for.
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300837 * @port_num: The port number of the device where the GID value was found.
838 * @index: The index into the GID table where the GID was found. This
839 * parameter may be NULL.
840 */
841int ib_find_gid(struct ib_device *device, union ib_gid *gid,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300842 struct net_device *ndev, u8 *port_num, u16 *index)
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300843{
844 union ib_gid tmp_gid;
845 int ret, port, i;
846
Ira Weiny0cf18d72015-05-13 20:02:55 -0400847 for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
Matan Barak03db3a22015-07-30 18:33:26 +0300848 if (rdma_cap_roce_gid_table(device, port)) {
Matan Barakd300ec52015-10-15 18:38:46 +0300849 if (!ib_find_cached_gid_by_port(device, gid, port,
850 ndev, index)) {
Matan Barak03db3a22015-07-30 18:33:26 +0300851 *port_num = port;
852 return 0;
Dan Carpenter98d25af2015-08-18 12:22:10 +0300853 }
Matan Barak03db3a22015-07-30 18:33:26 +0300854 }
855
Ira Weiny77386132015-05-13 20:02:58 -0400856 for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
Matan Barak55ee3ab2015-10-15 18:38:45 +0300857 ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300858 if (ret)
859 return ret;
860 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
861 *port_num = port;
862 if (index)
863 *index = i;
864 return 0;
865 }
866 }
867 }
868
869 return -ENOENT;
870}
871EXPORT_SYMBOL(ib_find_gid);
872
873/**
874 * ib_find_pkey - Returns the PKey table index where a specified
875 * PKey value occurs.
876 * @device: The device to query.
877 * @port_num: The port number of the device to search for the PKey.
878 * @pkey: The PKey value to search for.
879 * @index: The index into the PKey table where the PKey was found.
880 */
881int ib_find_pkey(struct ib_device *device,
882 u8 port_num, u16 pkey, u16 *index)
883{
884 int ret, i;
885 u16 tmp_pkey;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000886 int partial_ix = -1;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300887
Ira Weiny77386132015-05-13 20:02:58 -0400888 for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300889 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
890 if (ret)
891 return ret;
Moni Shoua36026ec2007-07-23 10:07:42 +0300892 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000893 /* if there is full-member pkey take it.*/
894 if (tmp_pkey & 0x8000) {
895 *index = i;
896 return 0;
897 }
898 if (partial_ix < 0)
899 partial_ix = i;
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300900 }
901 }
902
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000903 /*no full-member, if exists take the limited*/
904 if (partial_ix >= 0) {
905 *index = partial_ix;
906 return 0;
907 }
Yosef Etigin5eb620c2007-05-14 07:26:51 +0300908 return -ENOENT;
909}
910EXPORT_SYMBOL(ib_find_pkey);
911
Yotam Kenneth9268f722015-07-30 17:50:15 +0300912/**
913 * ib_get_net_dev_by_params() - Return the appropriate net_dev
914 * for a received CM request
915 * @dev: An RDMA device on which the request has been received.
916 * @port: Port number on the RDMA device.
917 * @pkey: The Pkey the request came on.
918 * @gid: A GID that the net_dev uses to communicate.
919 * @addr: Contains the IP address that the request specified as its
920 * destination.
921 */
922struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
923 u8 port,
924 u16 pkey,
925 const union ib_gid *gid,
926 const struct sockaddr *addr)
927{
928 struct net_device *net_dev = NULL;
929 struct ib_client_data *context;
930
931 if (!rdma_protocol_ib(dev, port))
932 return NULL;
933
934 down_read(&lists_rwsem);
935
936 list_for_each_entry(context, &dev->client_data_list, list) {
937 struct ib_client *client = context->client;
938
939 if (context->going_down)
940 continue;
941
942 if (client->get_net_dev_by_params) {
943 net_dev = client->get_net_dev_by_params(dev, port, pkey,
944 gid, addr,
945 context->data);
946 if (net_dev)
947 break;
948 }
949 }
950
951 up_read(&lists_rwsem);
952
953 return net_dev;
954}
955EXPORT_SYMBOL(ib_get_net_dev_by_params);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957static int __init ib_core_init(void)
958{
959 int ret;
960
Tejun Heof0626712010-10-19 15:24:36 +0000961 ib_wq = alloc_workqueue("infiniband", 0, 0);
962 if (!ib_wq)
963 return -ENOMEM;
964
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600965 ret = class_register(&ib_class);
Nir Muchtarfd75c782011-05-20 11:46:10 -0700966 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700968 goto err;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Roland Dreierb2cbae22011-05-20 11:46:11 -0700971 ret = ibnl_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ret) {
Roland Dreierb2cbae22011-05-20 11:46:11 -0700973 printk(KERN_WARNING "Couldn't init IB netlink interface\n");
Nir Muchtarfd75c782011-05-20 11:46:10 -0700974 goto err_sysfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Matan Barak03db3a22015-07-30 18:33:26 +0300977 ib_cache_setup();
Roland Dreierb2cbae22011-05-20 11:46:11 -0700978
Nir Muchtarfd75c782011-05-20 11:46:10 -0700979 return 0;
980
981err_sysfs:
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600982 class_unregister(&ib_class);
Nir Muchtarfd75c782011-05-20 11:46:10 -0700983
984err:
985 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return ret;
987}
988
989static void __exit ib_core_cleanup(void)
990{
991 ib_cache_cleanup();
Roland Dreierb2cbae22011-05-20 11:46:11 -0700992 ibnl_cleanup();
Jason Gunthorpe55aeed02015-08-04 15:23:34 -0600993 class_unregister(&ib_class);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800994 /* Make sure that any pending umem accounting work is done. */
Tejun Heof0626712010-10-19 15:24:36 +0000995 destroy_workqueue(ib_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998module_init(ib_core_init);
999module_exit(ib_core_cleanup);