RDMA/core: Introduce and use ib_setup_port_attrs()
[linux-block.git] / drivers / infiniband / core / device.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
2a1d9b7f 3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
1da177e4
LT
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
1da177e4
LT
32 */
33
34#include <linux/module.h>
35#include <linux/string.h>
36#include <linux/errno.h>
9a6b090c 37#include <linux/kernel.h>
1da177e4
LT
38#include <linux/slab.h>
39#include <linux/init.h>
9268f72d 40#include <linux/netdevice.h>
8f408ab6
DJ
41#include <linux/security.h>
42#include <linux/notifier.h>
b2cbae2c 43#include <rdma/rdma_netlink.h>
03db3a2d
MB
44#include <rdma/ib_addr.h>
45#include <rdma/ib_cache.h>
1da177e4
LT
46
47#include "core_priv.h"
48
49MODULE_AUTHOR("Roland Dreier");
50MODULE_DESCRIPTION("core kernel InfiniBand API");
51MODULE_LICENSE("Dual BSD/GPL");
52
14d3a3b2 53struct workqueue_struct *ib_comp_wq;
f794809a 54struct workqueue_struct *ib_comp_unbound_wq;
f0626710
TH
55struct workqueue_struct *ib_wq;
56EXPORT_SYMBOL_GPL(ib_wq);
57
921eab11
JG
58/*
59 * Each of the three rwsem locks (devices, clients, client_data) protects the
60 * xarray of the same name. Specifically it allows the caller to assert that
61 * the MARK will/will not be changing under the lock, and for devices and
62 * clients, that the value in the xarray is still a valid pointer. Change of
63 * the MARK is linked to the object state, so holding the lock and testing the
64 * MARK also asserts that the contained object is in a certain state.
65 *
66 * This is used to build a two stage register/unregister flow where objects
67 * can continue to be in the xarray even though they are still in progress to
68 * register/unregister.
69 *
70 * The xarray itself provides additional locking, and restartable iteration,
71 * which is also relied on.
72 *
73 * Locks should not be nested, with the exception of client_data, which is
74 * allowed to nest under the read side of the other two locks.
75 *
76 * The devices_rwsem also protects the device name list, any change or
77 * assignment of device name must also hold the write side to guarantee unique
78 * names.
79 */
80
0df91bb6
JG
81/*
82 * devices contains devices that have had their names assigned. The
83 * devices may not be registered. Users that care about the registration
84 * status need to call ib_device_try_get() on the device to ensure it is
85 * registered, and keep it registered, for the required duration.
86 *
87 */
88static DEFINE_XARRAY_FLAGS(devices, XA_FLAGS_ALLOC);
921eab11 89static DECLARE_RWSEM(devices_rwsem);
0df91bb6
JG
90#define DEVICE_REGISTERED XA_MARK_1
91
1da177e4 92static LIST_HEAD(client_list);
e59178d8
JG
93#define CLIENT_REGISTERED XA_MARK_1
94static DEFINE_XARRAY_FLAGS(clients, XA_FLAGS_ALLOC);
921eab11 95static DECLARE_RWSEM(clients_rwsem);
1da177e4
LT
96
97/*
0df91bb6
JG
98 * If client_data is registered then the corresponding client must also still
99 * be registered.
100 */
101#define CLIENT_DATA_REGISTERED XA_MARK_1
102/*
103 * xarray has this behavior where it won't iterate over NULL values stored in
104 * allocated arrays. So we need our own iterator to see all values stored in
105 * the array. This does the same thing as xa_for_each except that it also
106 * returns NULL valued entries if the array is allocating. Simplified to only
107 * work on simple xarrays.
108 */
109static void *xan_find_marked(struct xarray *xa, unsigned long *indexp,
110 xa_mark_t filter)
111{
112 XA_STATE(xas, xa, *indexp);
113 void *entry;
114
115 rcu_read_lock();
116 do {
117 entry = xas_find_marked(&xas, ULONG_MAX, filter);
118 if (xa_is_zero(entry))
119 break;
120 } while (xas_retry(&xas, entry));
121 rcu_read_unlock();
122
123 if (entry) {
124 *indexp = xas.xa_index;
125 if (xa_is_zero(entry))
126 return NULL;
127 return entry;
128 }
129 return XA_ERROR(-ENOENT);
130}
131#define xan_for_each_marked(xa, index, entry, filter) \
132 for (index = 0, entry = xan_find_marked(xa, &(index), filter); \
133 !xa_is_err(entry); \
134 (index)++, entry = xan_find_marked(xa, &(index), filter))
135
8f408ab6
DJ
136static int ib_security_change(struct notifier_block *nb, unsigned long event,
137 void *lsm_data);
138static void ib_policy_change_task(struct work_struct *work);
139static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
140
141static struct notifier_block ibdev_lsm_nb = {
142 .notifier_call = ib_security_change,
143};
1da177e4
LT
144
145static int ib_device_check_mandatory(struct ib_device *device)
146{
3023a1e9 147#define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x }
1da177e4
LT
148 static const struct {
149 size_t offset;
150 char *name;
151 } mandatory_table[] = {
152 IB_MANDATORY_FUNC(query_device),
153 IB_MANDATORY_FUNC(query_port),
154 IB_MANDATORY_FUNC(query_pkey),
1da177e4
LT
155 IB_MANDATORY_FUNC(alloc_pd),
156 IB_MANDATORY_FUNC(dealloc_pd),
1da177e4
LT
157 IB_MANDATORY_FUNC(create_qp),
158 IB_MANDATORY_FUNC(modify_qp),
159 IB_MANDATORY_FUNC(destroy_qp),
160 IB_MANDATORY_FUNC(post_send),
161 IB_MANDATORY_FUNC(post_recv),
162 IB_MANDATORY_FUNC(create_cq),
163 IB_MANDATORY_FUNC(destroy_cq),
164 IB_MANDATORY_FUNC(poll_cq),
165 IB_MANDATORY_FUNC(req_notify_cq),
166 IB_MANDATORY_FUNC(get_dma_mr),
7738613e
IW
167 IB_MANDATORY_FUNC(dereg_mr),
168 IB_MANDATORY_FUNC(get_port_immutable)
1da177e4
LT
169 };
170 int i;
171
6780c4fa 172 device->kverbs_provider = true;
9a6b090c 173 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
3023a1e9
KH
174 if (!*(void **) ((void *) &device->ops +
175 mandatory_table[i].offset)) {
6780c4fa
GP
176 device->kverbs_provider = false;
177 break;
1da177e4
LT
178 }
179 }
180
181 return 0;
182}
183
f8978bd9 184/*
01b67117
PP
185 * Caller must perform ib_device_put() to return the device reference count
186 * when ib_device_get_by_index() returns valid device pointer.
f8978bd9
LR
187 */
188struct ib_device *ib_device_get_by_index(u32 index)
189{
190 struct ib_device *device;
191
921eab11 192 down_read(&devices_rwsem);
0df91bb6 193 device = xa_load(&devices, index);
01b67117 194 if (device) {
d79af724 195 if (!ib_device_try_get(device))
01b67117
PP
196 device = NULL;
197 }
921eab11 198 up_read(&devices_rwsem);
f8978bd9
LR
199 return device;
200}
201
d79af724
JG
202/**
203 * ib_device_put - Release IB device reference
204 * @device: device whose reference to be released
205 *
206 * ib_device_put() releases reference to the IB device to allow it to be
207 * unregistered and eventually free.
208 */
01b67117
PP
209void ib_device_put(struct ib_device *device)
210{
211 if (refcount_dec_and_test(&device->refcount))
212 complete(&device->unreg_completion);
213}
d79af724 214EXPORT_SYMBOL(ib_device_put);
01b67117 215
1da177e4
LT
216static struct ib_device *__ib_device_get_by_name(const char *name)
217{
218 struct ib_device *device;
0df91bb6 219 unsigned long index;
1da177e4 220
0df91bb6 221 xa_for_each (&devices, index, device)
896de009 222 if (!strcmp(name, dev_name(&device->dev)))
1da177e4
LT
223 return device;
224
225 return NULL;
226}
227
d21943dd
LR
228int ib_device_rename(struct ib_device *ibdev, const char *name)
229{
e3593b56 230 int ret;
d21943dd 231
921eab11 232 down_write(&devices_rwsem);
e3593b56
JG
233 if (!strcmp(name, dev_name(&ibdev->dev))) {
234 ret = 0;
235 goto out;
236 }
237
344684e6
JG
238 if (__ib_device_get_by_name(name)) {
239 ret = -EEXIST;
240 goto out;
d21943dd
LR
241 }
242
243 ret = device_rename(&ibdev->dev, name);
244 if (ret)
245 goto out;
246 strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
247out:
921eab11 248 up_write(&devices_rwsem);
d21943dd
LR
249 return ret;
250}
251
e349f858 252static int alloc_name(struct ib_device *ibdev, const char *name)
1da177e4 253{
1da177e4 254 struct ib_device *device;
0df91bb6 255 unsigned long index;
3b88afd3
JG
256 struct ida inuse;
257 int rc;
1da177e4
LT
258 int i;
259
921eab11 260 lockdep_assert_held_exclusive(&devices_rwsem);
3b88afd3 261 ida_init(&inuse);
0df91bb6 262 xa_for_each (&devices, index, device) {
e349f858
JG
263 char buf[IB_DEVICE_NAME_MAX];
264
896de009 265 if (sscanf(dev_name(&device->dev), name, &i) != 1)
1da177e4 266 continue;
3b88afd3 267 if (i < 0 || i >= INT_MAX)
1da177e4
LT
268 continue;
269 snprintf(buf, sizeof buf, name, i);
3b88afd3
JG
270 if (strcmp(buf, dev_name(&device->dev)) != 0)
271 continue;
272
273 rc = ida_alloc_range(&inuse, i, i, GFP_KERNEL);
274 if (rc < 0)
275 goto out;
1da177e4
LT
276 }
277
3b88afd3
JG
278 rc = ida_alloc(&inuse, GFP_KERNEL);
279 if (rc < 0)
280 goto out;
1da177e4 281
3b88afd3
JG
282 rc = dev_set_name(&ibdev->dev, name, rc);
283out:
284 ida_destroy(&inuse);
285 return rc;
1da177e4
LT
286}
287
55aeed06
JG
288static void ib_device_release(struct device *device)
289{
290 struct ib_device *dev = container_of(device, struct ib_device, dev);
291
652432f3 292 WARN_ON(refcount_read(&dev->refcount));
d45f89d5 293 ib_cache_release_one(dev);
b34b269a
JG
294 ib_security_release_port_pkey_list(dev);
295 kfree(dev->port_pkey_list);
d45f89d5 296 kfree(dev->port_immutable);
0df91bb6 297 xa_destroy(&dev->client_data);
55aeed06
JG
298 kfree(dev);
299}
300
301static int ib_device_uevent(struct device *device,
302 struct kobj_uevent_env *env)
303{
896de009 304 if (add_uevent_var(env, "NAME=%s", dev_name(device)))
55aeed06
JG
305 return -ENOMEM;
306
307 /*
308 * It would be nice to pass the node GUID with the event...
309 */
310
311 return 0;
312}
313
314static struct class ib_class = {
315 .name = "infiniband",
316 .dev_release = ib_device_release,
317 .dev_uevent = ib_device_uevent,
318};
319
1da177e4 320/**
459cc69f 321 * _ib_alloc_device - allocate an IB device struct
1da177e4
LT
322 * @size:size of structure to allocate
323 *
324 * Low-level drivers should use ib_alloc_device() to allocate &struct
325 * ib_device. @size is the size of the structure to be allocated,
326 * including any private data used by the low-level driver.
327 * ib_dealloc_device() must be used to free structures allocated with
328 * ib_alloc_device().
329 */
459cc69f 330struct ib_device *_ib_alloc_device(size_t size)
1da177e4 331{
55aeed06
JG
332 struct ib_device *device;
333
334 if (WARN_ON(size < sizeof(struct ib_device)))
335 return NULL;
336
337 device = kzalloc(size, GFP_KERNEL);
338 if (!device)
339 return NULL;
340
0ad699c0 341 rdma_restrack_init(device);
02d8883f 342
55aeed06
JG
343 device->dev.class = &ib_class;
344 device_initialize(&device->dev);
345
55aeed06
JG
346 INIT_LIST_HEAD(&device->event_handler_list);
347 spin_lock_init(&device->event_handler_lock);
0df91bb6
JG
348 /*
349 * client_data needs to be alloc because we don't want our mark to be
350 * destroyed if the user stores NULL in the client data.
351 */
352 xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
921eab11 353 init_rwsem(&device->client_data_rwsem);
55aeed06 354 INIT_LIST_HEAD(&device->port_list);
01b67117 355 init_completion(&device->unreg_completion);
1da177e4 356
55aeed06 357 return device;
1da177e4 358}
459cc69f 359EXPORT_SYMBOL(_ib_alloc_device);
1da177e4
LT
360
361/**
362 * ib_dealloc_device - free an IB device struct
363 * @device:structure to free
364 *
365 * Free a structure allocated with ib_alloc_device().
366 */
367void ib_dealloc_device(struct ib_device *device)
368{
0df91bb6 369 WARN_ON(!xa_empty(&device->client_data));
652432f3 370 WARN_ON(refcount_read(&device->refcount));
0ad699c0 371 rdma_restrack_clean(device);
e155755e 372 /* Balances with device_initialize */
924b8900 373 put_device(&device->dev);
1da177e4
LT
374}
375EXPORT_SYMBOL(ib_dealloc_device);
376
921eab11
JG
377/*
378 * add_client_context() and remove_client_context() must be safe against
379 * parallel calls on the same device - registration/unregistration of both the
380 * device and client can be occurring in parallel.
381 *
382 * The routines need to be a fence, any caller must not return until the add
383 * or remove is fully completed.
384 */
385static int add_client_context(struct ib_device *device,
386 struct ib_client *client)
1da177e4 387{
921eab11 388 int ret = 0;
1da177e4 389
6780c4fa 390 if (!device->kverbs_provider && !client->no_kverbs_req)
921eab11
JG
391 return 0;
392
393 down_write(&device->client_data_rwsem);
394 /*
395 * Another caller to add_client_context got here first and has already
396 * completely initialized context.
397 */
398 if (xa_get_mark(&device->client_data, client->client_id,
399 CLIENT_DATA_REGISTERED))
400 goto out;
401
402 ret = xa_err(xa_store(&device->client_data, client->client_id, NULL,
403 GFP_KERNEL));
404 if (ret)
405 goto out;
406 downgrade_write(&device->client_data_rwsem);
407 if (client->add)
408 client->add(device);
409
410 /* Readers shall not see a client until add has been completed */
411 xa_set_mark(&device->client_data, client->client_id,
412 CLIENT_DATA_REGISTERED);
413 up_read(&device->client_data_rwsem);
414 return 0;
415
416out:
417 up_write(&device->client_data_rwsem);
418 return ret;
419}
420
421static void remove_client_context(struct ib_device *device,
422 unsigned int client_id)
423{
424 struct ib_client *client;
425 void *client_data;
6780c4fa 426
921eab11
JG
427 down_write(&device->client_data_rwsem);
428 if (!xa_get_mark(&device->client_data, client_id,
429 CLIENT_DATA_REGISTERED)) {
430 up_write(&device->client_data_rwsem);
431 return;
432 }
433 client_data = xa_load(&device->client_data, client_id);
434 xa_clear_mark(&device->client_data, client_id, CLIENT_DATA_REGISTERED);
435 client = xa_load(&clients, client_id);
436 downgrade_write(&device->client_data_rwsem);
1da177e4 437
921eab11
JG
438 /*
439 * Notice we cannot be holding any exclusive locks when calling the
440 * remove callback as the remove callback can recurse back into any
441 * public functions in this module and thus try for any locks those
442 * functions take.
443 *
444 * For this reason clients and drivers should not call the
445 * unregistration functions will holdling any locks.
446 *
447 * It tempting to drop the client_data_rwsem too, but this is required
448 * to ensure that unregister_client does not return until all clients
449 * are completely unregistered, which is required to avoid module
450 * unloading races.
451 */
452 if (client->remove)
453 client->remove(device, client_data);
454
455 xa_erase(&device->client_data, client_id);
456 up_read(&device->client_data_rwsem);
1da177e4
LT
457}
458
337877a4
IW
459static int verify_immutable(const struct ib_device *dev, u8 port)
460{
461 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
462 rdma_max_mad_size(dev, port) != 0);
463}
464
7738613e 465static int read_port_immutable(struct ib_device *device)
5eb620c8 466{
55aeed06 467 int ret;
7738613e
IW
468 u8 start_port = rdma_start_port(device);
469 u8 end_port = rdma_end_port(device);
470 u8 port;
471
472 /**
473 * device->port_immutable is indexed directly by the port number to make
474 * access to this data as efficient as possible.
475 *
476 * Therefore port_immutable is declared as a 1 based array with
477 * potential empty slots at the beginning.
478 */
6396bb22
KC
479 device->port_immutable = kcalloc(end_port + 1,
480 sizeof(*device->port_immutable),
7738613e
IW
481 GFP_KERNEL);
482 if (!device->port_immutable)
55aeed06 483 return -ENOMEM;
5eb620c8 484
7738613e 485 for (port = start_port; port <= end_port; ++port) {
3023a1e9
KH
486 ret = device->ops.get_port_immutable(
487 device, port, &device->port_immutable[port]);
5eb620c8 488 if (ret)
55aeed06 489 return ret;
337877a4 490
55aeed06
JG
491 if (verify_immutable(device, port))
492 return -EINVAL;
5eb620c8 493 }
55aeed06 494 return 0;
5eb620c8
YE
495}
496
9abb0d1b 497void ib_get_device_fw_str(struct ib_device *dev, char *str)
5fa76c20 498{
3023a1e9
KH
499 if (dev->ops.get_dev_fw_str)
500 dev->ops.get_dev_fw_str(dev, str);
5fa76c20
IW
501 else
502 str[0] = '\0';
503}
504EXPORT_SYMBOL(ib_get_device_fw_str);
505
d291f1a6
DJ
506static int setup_port_pkey_list(struct ib_device *device)
507{
508 int i;
509
510 /**
511 * device->port_pkey_list is indexed directly by the port number,
512 * Therefore it is declared as a 1 based array with potential empty
513 * slots at the beginning.
514 */
515 device->port_pkey_list = kcalloc(rdma_end_port(device) + 1,
516 sizeof(*device->port_pkey_list),
517 GFP_KERNEL);
518
519 if (!device->port_pkey_list)
520 return -ENOMEM;
521
522 for (i = 0; i < (rdma_end_port(device) + 1); i++) {
523 spin_lock_init(&device->port_pkey_list[i].list_lock);
524 INIT_LIST_HEAD(&device->port_pkey_list[i].pkey_list);
525 }
526
527 return 0;
528}
529
8f408ab6
DJ
530static void ib_policy_change_task(struct work_struct *work)
531{
532 struct ib_device *dev;
0df91bb6 533 unsigned long index;
8f408ab6 534
921eab11 535 down_read(&devices_rwsem);
0df91bb6 536 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
8f408ab6
DJ
537 int i;
538
539 for (i = rdma_start_port(dev); i <= rdma_end_port(dev); i++) {
540 u64 sp;
541 int ret = ib_get_cached_subnet_prefix(dev,
542 i,
543 &sp);
544
545 WARN_ONCE(ret,
546 "ib_get_cached_subnet_prefix err: %d, this should never happen here\n",
547 ret);
a750cfde
DJ
548 if (!ret)
549 ib_security_cache_change(dev, i, sp);
8f408ab6
DJ
550 }
551 }
921eab11 552 up_read(&devices_rwsem);
8f408ab6
DJ
553}
554
555static int ib_security_change(struct notifier_block *nb, unsigned long event,
556 void *lsm_data)
557{
558 if (event != LSM_POLICY_CHANGE)
559 return NOTIFY_DONE;
560
561 schedule_work(&ib_policy_change_work);
c66f6741 562 ib_mad_agent_security_change();
8f408ab6
DJ
563
564 return NOTIFY_OK;
565}
566
0df91bb6
JG
567/*
568 * Assign the unique string device name and the unique device index.
ecc82c53 569 */
0df91bb6 570static int assign_name(struct ib_device *device, const char *name)
ecc82c53 571{
0df91bb6
JG
572 static u32 last_id;
573 int ret;
ecc82c53 574
921eab11 575 down_write(&devices_rwsem);
0df91bb6
JG
576 /* Assign a unique name to the device */
577 if (strchr(name, '%'))
578 ret = alloc_name(device, name);
579 else
580 ret = dev_set_name(&device->dev, name);
581 if (ret)
582 goto out;
583
584 if (__ib_device_get_by_name(dev_name(&device->dev))) {
585 ret = -ENFILE;
586 goto out;
587 }
588 strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
ecc82c53 589
0df91bb6
JG
590 /* Cyclically allocate a user visible ID for the device */
591 device->index = last_id;
592 ret = xa_alloc(&devices, &device->index, INT_MAX, device, GFP_KERNEL);
593 if (ret == -ENOSPC) {
594 device->index = 0;
595 ret = xa_alloc(&devices, &device->index, INT_MAX, device,
596 GFP_KERNEL);
ecc82c53 597 }
0df91bb6
JG
598 if (ret)
599 goto out;
600 last_id = device->index + 1;
601
602 ret = 0;
921eab11 603
0df91bb6 604out:
921eab11 605 up_write(&devices_rwsem);
0df91bb6
JG
606 return ret;
607}
608
609static void release_name(struct ib_device *device)
610{
921eab11 611 down_write(&devices_rwsem);
0df91bb6 612 xa_erase(&devices, device->index);
921eab11 613 up_write(&devices_rwsem);
ecc82c53
LR
614}
615
548cb4fb 616static void setup_dma_device(struct ib_device *device)
1da177e4 617{
99db9494
BVA
618 struct device *parent = device->dev.parent;
619
0957c29f
BVA
620 WARN_ON_ONCE(device->dma_device);
621 if (device->dev.dma_ops) {
622 /*
623 * The caller provided custom DMA operations. Copy the
624 * DMA-related fields that are used by e.g. dma_alloc_coherent()
625 * into device->dev.
626 */
627 device->dma_device = &device->dev;
02ee9da3
BVA
628 if (!device->dev.dma_mask) {
629 if (parent)
630 device->dev.dma_mask = parent->dma_mask;
631 else
632 WARN_ON_ONCE(true);
633 }
634 if (!device->dev.coherent_dma_mask) {
635 if (parent)
636 device->dev.coherent_dma_mask =
637 parent->coherent_dma_mask;
638 else
639 WARN_ON_ONCE(true);
640 }
0957c29f
BVA
641 } else {
642 /*
643 * The caller did not provide custom DMA operations. Use the
644 * DMA mapping operations of the parent device.
645 */
02ee9da3 646 WARN_ON_ONCE(!parent);
0957c29f
BVA
647 device->dma_device = parent;
648 }
548cb4fb 649}
1da177e4 650
921eab11
JG
651/*
652 * setup_device() allocates memory and sets up data that requires calling the
653 * device ops, this is the only reason these actions are not done during
654 * ib_alloc_device. It is undone by ib_dealloc_device().
655 */
548cb4fb
PP
656static int setup_device(struct ib_device *device)
657{
658 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
659 int ret;
1da177e4 660
921eab11
JG
661 setup_dma_device(device);
662
548cb4fb
PP
663 ret = ib_device_check_mandatory(device);
664 if (ret)
665 return ret;
1da177e4 666
7738613e 667 ret = read_port_immutable(device);
5eb620c8 668 if (ret) {
43c7c851
JG
669 dev_warn(&device->dev,
670 "Couldn't create per port immutable data\n");
548cb4fb
PP
671 return ret;
672 }
673
674 memset(&device->attrs, 0, sizeof(device->attrs));
3023a1e9 675 ret = device->ops.query_device(device, &device->attrs, &uhw);
548cb4fb
PP
676 if (ret) {
677 dev_warn(&device->dev,
678 "Couldn't query the device attributes\n");
d45f89d5 679 return ret;
5eb620c8
YE
680 }
681
d291f1a6
DJ
682 ret = setup_port_pkey_list(device);
683 if (ret) {
43c7c851 684 dev_warn(&device->dev, "Couldn't create per port_pkey_list\n");
b34b269a 685 return ret;
03db3a2d 686 }
548cb4fb 687
d45f89d5 688 return 0;
548cb4fb
PP
689}
690
921eab11
JG
691static void disable_device(struct ib_device *device)
692{
693 struct ib_client *client;
694
695 WARN_ON(!refcount_read(&device->refcount));
696
697 down_write(&devices_rwsem);
698 xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
699 up_write(&devices_rwsem);
700
701 down_read(&clients_rwsem);
702 list_for_each_entry_reverse(client, &client_list, list)
703 remove_client_context(device, client->client_id);
704 up_read(&clients_rwsem);
705
706 /* Pairs with refcount_set in enable_device */
707 ib_device_put(device);
708 wait_for_completion(&device->unreg_completion);
709}
710
711/*
712 * An enabled device is visible to all clients and to all the public facing
713 * APIs that return a device pointer.
714 */
715static int enable_device(struct ib_device *device)
716{
717 struct ib_client *client;
718 unsigned long index;
719 int ret;
720
721 refcount_set(&device->refcount, 1);
722 down_write(&devices_rwsem);
723 xa_set_mark(&devices, device->index, DEVICE_REGISTERED);
724 up_write(&devices_rwsem);
725
726 down_read(&clients_rwsem);
727 xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
728 ret = add_client_context(device, client);
729 if (ret) {
730 up_read(&clients_rwsem);
731 disable_device(device);
732 return ret;
733 }
734 }
735 up_read(&clients_rwsem);
736 return 0;
737}
738
548cb4fb
PP
739/**
740 * ib_register_device - Register an IB device with IB core
741 * @device:Device to register
742 *
743 * Low-level drivers use ib_register_device() to register their
744 * devices with the IB core. All registered clients will receive a
745 * callback for each device that is added. @device must be allocated
746 * with ib_alloc_device().
747 */
ea4baf7f 748int ib_register_device(struct ib_device *device, const char *name)
548cb4fb
PP
749{
750 int ret;
548cb4fb 751
0df91bb6
JG
752 ret = assign_name(device, name);
753 if (ret)
921eab11 754 return ret;
548cb4fb
PP
755
756 ret = setup_device(device);
757 if (ret)
921eab11 758 goto out;
03db3a2d 759
d45f89d5
JG
760 ret = ib_cache_setup_one(device);
761 if (ret) {
762 dev_warn(&device->dev,
763 "Couldn't set up InfiniBand P_Key/GID cache\n");
921eab11 764 goto out;
d45f89d5
JG
765 }
766
7527a7b1 767 ib_device_register_rdmacg(device);
3e153a93 768
ea4baf7f 769 ret = ib_device_register_sysfs(device);
1da177e4 770 if (ret) {
43c7c851
JG
771 dev_warn(&device->dev,
772 "Couldn't register device with driver model\n");
2fb4f4ea 773 goto cg_cleanup;
1da177e4
LT
774 }
775
921eab11
JG
776 ret = enable_device(device);
777 if (ret)
778 goto sysfs_cleanup;
1da177e4 779
4be3a4fa
PP
780 return 0;
781
921eab11
JG
782sysfs_cleanup:
783 ib_device_unregister_sysfs(device);
2fb4f4ea
PP
784cg_cleanup:
785 ib_device_unregister_rdmacg(device);
d45f89d5 786 ib_cache_cleanup_one(device);
5aa44bb9 787out:
921eab11 788 release_name(device);
1da177e4
LT
789 return ret;
790}
791EXPORT_SYMBOL(ib_register_device);
792
793/**
794 * ib_unregister_device - Unregister an IB device
795 * @device:Device to unregister
796 *
797 * Unregister an IB device. All clients will receive a remove callback.
798 */
799void ib_unregister_device(struct ib_device *device)
800{
921eab11 801 disable_device(device);
9206dff1 802 ib_device_unregister_sysfs(device);
c715a395 803 ib_device_unregister_rdmacg(device);
03db3a2d 804 ib_cache_cleanup_one(device);
921eab11 805 release_name(device);
1da177e4
LT
806}
807EXPORT_SYMBOL(ib_unregister_device);
808
e59178d8
JG
809static int assign_client_id(struct ib_client *client)
810{
811 int ret;
812
921eab11 813 down_write(&clients_rwsem);
e59178d8
JG
814 /*
815 * The add/remove callbacks must be called in FIFO/LIFO order. To
816 * achieve this we assign client_ids so they are sorted in
817 * registration order, and retain a linked list we can reverse iterate
818 * to get the LIFO order. The extra linked list can go away if xarray
819 * learns to reverse iterate.
820 */
821 if (list_empty(&client_list))
822 client->client_id = 0;
823 else
824 client->client_id =
825 list_last_entry(&client_list, struct ib_client, list)
826 ->client_id;
827 ret = xa_alloc(&clients, &client->client_id, INT_MAX, client,
828 GFP_KERNEL);
829 if (ret)
830 goto out;
831
921eab11
JG
832 xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED);
833 list_add_tail(&client->list, &client_list);
834
e59178d8 835out:
921eab11 836 up_write(&clients_rwsem);
e59178d8
JG
837 return ret;
838}
839
1da177e4
LT
840/**
841 * ib_register_client - Register an IB client
842 * @client:Client to register
843 *
844 * Upper level users of the IB drivers can use ib_register_client() to
845 * register callbacks for IB device addition and removal. When an IB
846 * device is added, each registered client's add method will be called
847 * (in the order the clients were registered), and when a device is
848 * removed, each client's remove method will be called (in the reverse
849 * order that clients were registered). In addition, when
850 * ib_register_client() is called, the client will receive an add
851 * callback for all devices already registered.
852 */
853int ib_register_client(struct ib_client *client)
854{
855 struct ib_device *device;
0df91bb6 856 unsigned long index;
e59178d8 857 int ret;
1da177e4 858
e59178d8 859 ret = assign_client_id(client);
921eab11 860 if (ret)
e59178d8 861 return ret;
1da177e4 862
921eab11
JG
863 down_read(&devices_rwsem);
864 xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) {
865 ret = add_client_context(device, client);
866 if (ret) {
867 up_read(&devices_rwsem);
868 ib_unregister_client(client);
869 return ret;
870 }
871 }
872 up_read(&devices_rwsem);
1da177e4
LT
873 return 0;
874}
875EXPORT_SYMBOL(ib_register_client);
876
877/**
878 * ib_unregister_client - Unregister an IB client
879 * @client:Client to unregister
880 *
881 * Upper level users use ib_unregister_client() to remove their client
882 * registration. When ib_unregister_client() is called, the client
883 * will receive a remove callback for each IB device still registered.
921eab11
JG
884 *
885 * This is a full fence, once it returns no client callbacks will be called,
886 * or are running in another thread.
1da177e4
LT
887 */
888void ib_unregister_client(struct ib_client *client)
889{
1da177e4 890 struct ib_device *device;
0df91bb6 891 unsigned long index;
1da177e4 892
921eab11 893 down_write(&clients_rwsem);
e59178d8 894 xa_clear_mark(&clients, client->client_id, CLIENT_REGISTERED);
921eab11
JG
895 up_write(&clients_rwsem);
896 /*
897 * Every device still known must be serialized to make sure we are
898 * done with the client callbacks before we return.
899 */
900 down_read(&devices_rwsem);
901 xa_for_each (&devices, index, device)
902 remove_client_context(device, client->client_id);
903 up_read(&devices_rwsem);
1da177e4 904
921eab11 905 down_write(&clients_rwsem);
e59178d8
JG
906 list_del(&client->list);
907 xa_erase(&clients, client->client_id);
921eab11 908 up_write(&clients_rwsem);
1da177e4
LT
909}
910EXPORT_SYMBOL(ib_unregister_client);
911
1da177e4 912/**
9cd330d3 913 * ib_set_client_data - Set IB client context
1da177e4
LT
914 * @device:Device to set context for
915 * @client:Client to set context for
916 * @data:Context to set
917 *
0df91bb6
JG
918 * ib_set_client_data() sets client context data that can be retrieved with
919 * ib_get_client_data(). This can only be called while the client is
920 * registered to the device, once the ib_client remove() callback returns this
921 * cannot be called.
1da177e4
LT
922 */
923void ib_set_client_data(struct ib_device *device, struct ib_client *client,
924 void *data)
925{
0df91bb6 926 void *rc;
1da177e4 927
0df91bb6
JG
928 if (WARN_ON(IS_ERR(data)))
929 data = NULL;
1da177e4 930
0df91bb6
JG
931 rc = xa_store(&device->client_data, client->client_id, data,
932 GFP_KERNEL);
933 WARN_ON(xa_is_err(rc));
1da177e4
LT
934}
935EXPORT_SYMBOL(ib_set_client_data);
936
937/**
938 * ib_register_event_handler - Register an IB event handler
939 * @event_handler:Handler to register
940 *
941 * ib_register_event_handler() registers an event handler that will be
942 * called back when asynchronous IB events occur (as defined in
943 * chapter 11 of the InfiniBand Architecture Specification). This
944 * callback may occur in interrupt context.
945 */
dcc9881e 946void ib_register_event_handler(struct ib_event_handler *event_handler)
1da177e4
LT
947{
948 unsigned long flags;
949
950 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
951 list_add_tail(&event_handler->list,
952 &event_handler->device->event_handler_list);
953 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
1da177e4
LT
954}
955EXPORT_SYMBOL(ib_register_event_handler);
956
957/**
958 * ib_unregister_event_handler - Unregister an event handler
959 * @event_handler:Handler to unregister
960 *
961 * Unregister an event handler registered with
962 * ib_register_event_handler().
963 */
dcc9881e 964void ib_unregister_event_handler(struct ib_event_handler *event_handler)
1da177e4
LT
965{
966 unsigned long flags;
967
968 spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
969 list_del(&event_handler->list);
970 spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
1da177e4
LT
971}
972EXPORT_SYMBOL(ib_unregister_event_handler);
973
974/**
975 * ib_dispatch_event - Dispatch an asynchronous event
976 * @event:Event to dispatch
977 *
978 * Low-level drivers must call ib_dispatch_event() to dispatch the
979 * event to all registered event handlers when an asynchronous event
980 * occurs.
981 */
982void ib_dispatch_event(struct ib_event *event)
983{
984 unsigned long flags;
985 struct ib_event_handler *handler;
986
987 spin_lock_irqsave(&event->device->event_handler_lock, flags);
988
989 list_for_each_entry(handler, &event->device->event_handler_list, list)
990 handler->handler(handler, event);
991
992 spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
993}
994EXPORT_SYMBOL(ib_dispatch_event);
995
1da177e4
LT
996/**
997 * ib_query_port - Query IB port attributes
998 * @device:Device to query
999 * @port_num:Port number to query
1000 * @port_attr:Port attributes
1001 *
1002 * ib_query_port() returns the attributes of a port through the
1003 * @port_attr pointer.
1004 */
1005int ib_query_port(struct ib_device *device,
1006 u8 port_num,
1007 struct ib_port_attr *port_attr)
1008{
fad61ad4
EC
1009 union ib_gid gid;
1010 int err;
1011
24dc831b 1012 if (!rdma_is_port_valid(device, port_num))
116c0074
RD
1013 return -EINVAL;
1014
fad61ad4 1015 memset(port_attr, 0, sizeof(*port_attr));
3023a1e9 1016 err = device->ops.query_port(device, port_num, port_attr);
fad61ad4
EC
1017 if (err || port_attr->subnet_prefix)
1018 return err;
1019
d7012467
EC
1020 if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
1021 return 0;
1022
3023a1e9 1023 err = device->ops.query_gid(device, port_num, 0, &gid);
fad61ad4
EC
1024 if (err)
1025 return err;
1026
1027 port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
1028 return 0;
1da177e4
LT
1029}
1030EXPORT_SYMBOL(ib_query_port);
1031
03db3a2d
MB
1032/**
1033 * ib_enum_roce_netdev - enumerate all RoCE ports
1034 * @ib_dev : IB device we want to query
1035 * @filter: Should we call the callback?
1036 * @filter_cookie: Cookie passed to filter
1037 * @cb: Callback to call for each found RoCE ports
1038 * @cookie: Cookie passed back to the callback
1039 *
1040 * Enumerates all of the physical RoCE ports of ib_dev
1041 * which are related to netdevice and calls callback() on each
1042 * device for which filter() function returns non zero.
1043 */
1044void ib_enum_roce_netdev(struct ib_device *ib_dev,
1045 roce_netdev_filter filter,
1046 void *filter_cookie,
1047 roce_netdev_callback cb,
1048 void *cookie)
1049{
1050 u8 port;
1051
1052 for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
1053 port++)
1054 if (rdma_protocol_roce(ib_dev, port)) {
1055 struct net_device *idev = NULL;
1056
3023a1e9
KH
1057 if (ib_dev->ops.get_netdev)
1058 idev = ib_dev->ops.get_netdev(ib_dev, port);
03db3a2d
MB
1059
1060 if (idev &&
1061 idev->reg_state >= NETREG_UNREGISTERED) {
1062 dev_put(idev);
1063 idev = NULL;
1064 }
1065
1066 if (filter(ib_dev, port, idev, filter_cookie))
1067 cb(ib_dev, port, idev, cookie);
1068
1069 if (idev)
1070 dev_put(idev);
1071 }
1072}
1073
1074/**
1075 * ib_enum_all_roce_netdevs - enumerate all RoCE devices
1076 * @filter: Should we call the callback?
1077 * @filter_cookie: Cookie passed to filter
1078 * @cb: Callback to call for each found RoCE ports
1079 * @cookie: Cookie passed back to the callback
1080 *
1081 * Enumerates all RoCE devices' physical ports which are related
1082 * to netdevices and calls callback() on each device for which
1083 * filter() function returns non zero.
1084 */
1085void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
1086 void *filter_cookie,
1087 roce_netdev_callback cb,
1088 void *cookie)
1089{
1090 struct ib_device *dev;
0df91bb6 1091 unsigned long index;
03db3a2d 1092
921eab11 1093 down_read(&devices_rwsem);
0df91bb6 1094 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED)
03db3a2d 1095 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
921eab11 1096 up_read(&devices_rwsem);
8030c835
LR
1097}
1098
1099/**
1100 * ib_enum_all_devs - enumerate all ib_devices
1101 * @cb: Callback to call for each found ib_device
1102 *
1103 * Enumerates all ib_devices and calls callback() on each device.
1104 */
1105int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
1106 struct netlink_callback *cb)
1107{
0df91bb6 1108 unsigned long index;
8030c835
LR
1109 struct ib_device *dev;
1110 unsigned int idx = 0;
1111 int ret = 0;
1112
921eab11 1113 down_read(&devices_rwsem);
0df91bb6 1114 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
8030c835
LR
1115 ret = nldev_cb(dev, skb, cb, idx);
1116 if (ret)
1117 break;
1118 idx++;
1119 }
921eab11 1120 up_read(&devices_rwsem);
8030c835 1121 return ret;
03db3a2d
MB
1122}
1123
1da177e4
LT
1124/**
1125 * ib_query_pkey - Get P_Key table entry
1126 * @device:Device to query
1127 * @port_num:Port number to query
1128 * @index:P_Key table index to query
1129 * @pkey:Returned P_Key
1130 *
1131 * ib_query_pkey() fetches the specified P_Key table entry.
1132 */
1133int ib_query_pkey(struct ib_device *device,
1134 u8 port_num, u16 index, u16 *pkey)
1135{
9af3f5cf
YS
1136 if (!rdma_is_port_valid(device, port_num))
1137 return -EINVAL;
1138
3023a1e9 1139 return device->ops.query_pkey(device, port_num, index, pkey);
1da177e4
LT
1140}
1141EXPORT_SYMBOL(ib_query_pkey);
1142
1143/**
1144 * ib_modify_device - Change IB device attributes
1145 * @device:Device to modify
1146 * @device_modify_mask:Mask of attributes to change
1147 * @device_modify:New attribute values
1148 *
1149 * ib_modify_device() changes a device's attributes as specified by
1150 * the @device_modify_mask and @device_modify structure.
1151 */
1152int ib_modify_device(struct ib_device *device,
1153 int device_modify_mask,
1154 struct ib_device_modify *device_modify)
1155{
3023a1e9 1156 if (!device->ops.modify_device)
10e1b54b
BVA
1157 return -ENOSYS;
1158
3023a1e9
KH
1159 return device->ops.modify_device(device, device_modify_mask,
1160 device_modify);
1da177e4
LT
1161}
1162EXPORT_SYMBOL(ib_modify_device);
1163
1164/**
1165 * ib_modify_port - Modifies the attributes for the specified port.
1166 * @device: The device to modify.
1167 * @port_num: The number of the port to modify.
1168 * @port_modify_mask: Mask used to specify which attributes of the port
1169 * to change.
1170 * @port_modify: New attribute values for the port.
1171 *
1172 * ib_modify_port() changes a port's attributes as specified by the
1173 * @port_modify_mask and @port_modify structure.
1174 */
1175int ib_modify_port(struct ib_device *device,
1176 u8 port_num, int port_modify_mask,
1177 struct ib_port_modify *port_modify)
1178{
61e0962d 1179 int rc;
10e1b54b 1180
24dc831b 1181 if (!rdma_is_port_valid(device, port_num))
116c0074
RD
1182 return -EINVAL;
1183
3023a1e9
KH
1184 if (device->ops.modify_port)
1185 rc = device->ops.modify_port(device, port_num,
1186 port_modify_mask,
1187 port_modify);
61e0962d
SX
1188 else
1189 rc = rdma_protocol_roce(device, port_num) ? 0 : -ENOSYS;
1190 return rc;
1da177e4
LT
1191}
1192EXPORT_SYMBOL(ib_modify_port);
1193
5eb620c8
YE
1194/**
1195 * ib_find_gid - Returns the port number and GID table index where
dbb12562 1196 * a specified GID value occurs. Its searches only for IB link layer.
5eb620c8
YE
1197 * @device: The device to query.
1198 * @gid: The GID value to search for.
1199 * @port_num: The port number of the device where the GID value was found.
1200 * @index: The index into the GID table where the GID was found. This
1201 * parameter may be NULL.
1202 */
1203int ib_find_gid(struct ib_device *device, union ib_gid *gid,
b26c4a11 1204 u8 *port_num, u16 *index)
5eb620c8
YE
1205{
1206 union ib_gid tmp_gid;
1207 int ret, port, i;
1208
0cf18d77 1209 for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
22d24f75 1210 if (!rdma_protocol_ib(device, port))
b39ffa1d
MB
1211 continue;
1212
7738613e 1213 for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
1dfce294 1214 ret = rdma_query_gid(device, port, i, &tmp_gid);
5eb620c8
YE
1215 if (ret)
1216 return ret;
1217 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
1218 *port_num = port;
1219 if (index)
1220 *index = i;
1221 return 0;
1222 }
1223 }
1224 }
1225
1226 return -ENOENT;
1227}
1228EXPORT_SYMBOL(ib_find_gid);
1229
1230/**
1231 * ib_find_pkey - Returns the PKey table index where a specified
1232 * PKey value occurs.
1233 * @device: The device to query.
1234 * @port_num: The port number of the device to search for the PKey.
1235 * @pkey: The PKey value to search for.
1236 * @index: The index into the PKey table where the PKey was found.
1237 */
1238int ib_find_pkey(struct ib_device *device,
1239 u8 port_num, u16 pkey, u16 *index)
1240{
1241 int ret, i;
1242 u16 tmp_pkey;
ff7166c4 1243 int partial_ix = -1;
5eb620c8 1244
7738613e 1245 for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
5eb620c8
YE
1246 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
1247 if (ret)
1248 return ret;
36026ecc 1249 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
ff7166c4
JM
1250 /* if there is full-member pkey take it.*/
1251 if (tmp_pkey & 0x8000) {
1252 *index = i;
1253 return 0;
1254 }
1255 if (partial_ix < 0)
1256 partial_ix = i;
5eb620c8
YE
1257 }
1258 }
1259
ff7166c4
JM
1260 /*no full-member, if exists take the limited*/
1261 if (partial_ix >= 0) {
1262 *index = partial_ix;
1263 return 0;
1264 }
5eb620c8
YE
1265 return -ENOENT;
1266}
1267EXPORT_SYMBOL(ib_find_pkey);
1268
9268f72d
YK
1269/**
1270 * ib_get_net_dev_by_params() - Return the appropriate net_dev
1271 * for a received CM request
1272 * @dev: An RDMA device on which the request has been received.
1273 * @port: Port number on the RDMA device.
1274 * @pkey: The Pkey the request came on.
1275 * @gid: A GID that the net_dev uses to communicate.
1276 * @addr: Contains the IP address that the request specified as its
1277 * destination.
921eab11 1278 *
9268f72d
YK
1279 */
1280struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
1281 u8 port,
1282 u16 pkey,
1283 const union ib_gid *gid,
1284 const struct sockaddr *addr)
1285{
1286 struct net_device *net_dev = NULL;
0df91bb6
JG
1287 unsigned long index;
1288 void *client_data;
9268f72d
YK
1289
1290 if (!rdma_protocol_ib(dev, port))
1291 return NULL;
1292
921eab11
JG
1293 /*
1294 * Holding the read side guarantees that the client will not become
1295 * unregistered while we are calling get_net_dev_by_params()
1296 */
1297 down_read(&dev->client_data_rwsem);
0df91bb6
JG
1298 xan_for_each_marked (&dev->client_data, index, client_data,
1299 CLIENT_DATA_REGISTERED) {
1300 struct ib_client *client = xa_load(&clients, index);
9268f72d 1301
0df91bb6 1302 if (!client || !client->get_net_dev_by_params)
9268f72d
YK
1303 continue;
1304
0df91bb6
JG
1305 net_dev = client->get_net_dev_by_params(dev, port, pkey, gid,
1306 addr, client_data);
1307 if (net_dev)
1308 break;
9268f72d 1309 }
921eab11 1310 up_read(&dev->client_data_rwsem);
9268f72d
YK
1311
1312 return net_dev;
1313}
1314EXPORT_SYMBOL(ib_get_net_dev_by_params);
1315
521ed0d9
KH
1316void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
1317{
3023a1e9 1318 struct ib_device_ops *dev_ops = &dev->ops;
521ed0d9
KH
1319#define SET_DEVICE_OP(ptr, name) \
1320 do { \
1321 if (ops->name) \
1322 if (!((ptr)->name)) \
1323 (ptr)->name = ops->name; \
1324 } while (0)
1325
30471d4b
LR
1326#define SET_OBJ_SIZE(ptr, name) SET_DEVICE_OP(ptr, size_##name)
1327
3023a1e9 1328 SET_DEVICE_OP(dev_ops, add_gid);
2f1927b0 1329 SET_DEVICE_OP(dev_ops, advise_mr);
3023a1e9
KH
1330 SET_DEVICE_OP(dev_ops, alloc_dm);
1331 SET_DEVICE_OP(dev_ops, alloc_fmr);
1332 SET_DEVICE_OP(dev_ops, alloc_hw_stats);
1333 SET_DEVICE_OP(dev_ops, alloc_mr);
1334 SET_DEVICE_OP(dev_ops, alloc_mw);
1335 SET_DEVICE_OP(dev_ops, alloc_pd);
1336 SET_DEVICE_OP(dev_ops, alloc_rdma_netdev);
1337 SET_DEVICE_OP(dev_ops, alloc_ucontext);
1338 SET_DEVICE_OP(dev_ops, alloc_xrcd);
1339 SET_DEVICE_OP(dev_ops, attach_mcast);
1340 SET_DEVICE_OP(dev_ops, check_mr_status);
1341 SET_DEVICE_OP(dev_ops, create_ah);
1342 SET_DEVICE_OP(dev_ops, create_counters);
1343 SET_DEVICE_OP(dev_ops, create_cq);
1344 SET_DEVICE_OP(dev_ops, create_flow);
1345 SET_DEVICE_OP(dev_ops, create_flow_action_esp);
1346 SET_DEVICE_OP(dev_ops, create_qp);
1347 SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
1348 SET_DEVICE_OP(dev_ops, create_srq);
1349 SET_DEVICE_OP(dev_ops, create_wq);
1350 SET_DEVICE_OP(dev_ops, dealloc_dm);
1351 SET_DEVICE_OP(dev_ops, dealloc_fmr);
1352 SET_DEVICE_OP(dev_ops, dealloc_mw);
1353 SET_DEVICE_OP(dev_ops, dealloc_pd);
1354 SET_DEVICE_OP(dev_ops, dealloc_ucontext);
1355 SET_DEVICE_OP(dev_ops, dealloc_xrcd);
1356 SET_DEVICE_OP(dev_ops, del_gid);
1357 SET_DEVICE_OP(dev_ops, dereg_mr);
1358 SET_DEVICE_OP(dev_ops, destroy_ah);
1359 SET_DEVICE_OP(dev_ops, destroy_counters);
1360 SET_DEVICE_OP(dev_ops, destroy_cq);
1361 SET_DEVICE_OP(dev_ops, destroy_flow);
1362 SET_DEVICE_OP(dev_ops, destroy_flow_action);
1363 SET_DEVICE_OP(dev_ops, destroy_qp);
1364 SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table);
1365 SET_DEVICE_OP(dev_ops, destroy_srq);
1366 SET_DEVICE_OP(dev_ops, destroy_wq);
1367 SET_DEVICE_OP(dev_ops, detach_mcast);
1368 SET_DEVICE_OP(dev_ops, disassociate_ucontext);
1369 SET_DEVICE_OP(dev_ops, drain_rq);
1370 SET_DEVICE_OP(dev_ops, drain_sq);
02da3750 1371 SET_DEVICE_OP(dev_ops, fill_res_entry);
3023a1e9
KH
1372 SET_DEVICE_OP(dev_ops, get_dev_fw_str);
1373 SET_DEVICE_OP(dev_ops, get_dma_mr);
1374 SET_DEVICE_OP(dev_ops, get_hw_stats);
1375 SET_DEVICE_OP(dev_ops, get_link_layer);
1376 SET_DEVICE_OP(dev_ops, get_netdev);
1377 SET_DEVICE_OP(dev_ops, get_port_immutable);
1378 SET_DEVICE_OP(dev_ops, get_vector_affinity);
1379 SET_DEVICE_OP(dev_ops, get_vf_config);
1380 SET_DEVICE_OP(dev_ops, get_vf_stats);
ea4baf7f 1381 SET_DEVICE_OP(dev_ops, init_port);
3023a1e9
KH
1382 SET_DEVICE_OP(dev_ops, map_mr_sg);
1383 SET_DEVICE_OP(dev_ops, map_phys_fmr);
1384 SET_DEVICE_OP(dev_ops, mmap);
1385 SET_DEVICE_OP(dev_ops, modify_ah);
1386 SET_DEVICE_OP(dev_ops, modify_cq);
1387 SET_DEVICE_OP(dev_ops, modify_device);
1388 SET_DEVICE_OP(dev_ops, modify_flow_action_esp);
1389 SET_DEVICE_OP(dev_ops, modify_port);
1390 SET_DEVICE_OP(dev_ops, modify_qp);
1391 SET_DEVICE_OP(dev_ops, modify_srq);
1392 SET_DEVICE_OP(dev_ops, modify_wq);
1393 SET_DEVICE_OP(dev_ops, peek_cq);
1394 SET_DEVICE_OP(dev_ops, poll_cq);
1395 SET_DEVICE_OP(dev_ops, post_recv);
1396 SET_DEVICE_OP(dev_ops, post_send);
1397 SET_DEVICE_OP(dev_ops, post_srq_recv);
1398 SET_DEVICE_OP(dev_ops, process_mad);
1399 SET_DEVICE_OP(dev_ops, query_ah);
1400 SET_DEVICE_OP(dev_ops, query_device);
1401 SET_DEVICE_OP(dev_ops, query_gid);
1402 SET_DEVICE_OP(dev_ops, query_pkey);
1403 SET_DEVICE_OP(dev_ops, query_port);
1404 SET_DEVICE_OP(dev_ops, query_qp);
1405 SET_DEVICE_OP(dev_ops, query_srq);
1406 SET_DEVICE_OP(dev_ops, rdma_netdev_get_params);
1407 SET_DEVICE_OP(dev_ops, read_counters);
1408 SET_DEVICE_OP(dev_ops, reg_dm_mr);
1409 SET_DEVICE_OP(dev_ops, reg_user_mr);
1410 SET_DEVICE_OP(dev_ops, req_ncomp_notif);
1411 SET_DEVICE_OP(dev_ops, req_notify_cq);
1412 SET_DEVICE_OP(dev_ops, rereg_user_mr);
1413 SET_DEVICE_OP(dev_ops, resize_cq);
1414 SET_DEVICE_OP(dev_ops, set_vf_guid);
1415 SET_DEVICE_OP(dev_ops, set_vf_link_state);
1416 SET_DEVICE_OP(dev_ops, unmap_fmr);
21a428a0
LR
1417
1418 SET_OBJ_SIZE(dev_ops, ib_pd);
521ed0d9
KH
1419}
1420EXPORT_SYMBOL(ib_set_device_ops);
1421
d0e312fe 1422static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
735c631a 1423 [RDMA_NL_LS_OP_RESOLVE] = {
647c75ac 1424 .doit = ib_nl_handle_resolve_resp,
e3a2b93d
LR
1425 .flags = RDMA_NL_ADMIN_PERM,
1426 },
735c631a 1427 [RDMA_NL_LS_OP_SET_TIMEOUT] = {
647c75ac 1428 .doit = ib_nl_handle_set_timeout,
e3a2b93d
LR
1429 .flags = RDMA_NL_ADMIN_PERM,
1430 },
ae43f828 1431 [RDMA_NL_LS_OP_IP_RESOLVE] = {
647c75ac 1432 .doit = ib_nl_handle_ip_res_resp,
e3a2b93d
LR
1433 .flags = RDMA_NL_ADMIN_PERM,
1434 },
735c631a
MB
1435};
1436
1da177e4
LT
1437static int __init ib_core_init(void)
1438{
1439 int ret;
1440
f0626710
TH
1441 ib_wq = alloc_workqueue("infiniband", 0, 0);
1442 if (!ib_wq)
1443 return -ENOMEM;
1444
14d3a3b2 1445 ib_comp_wq = alloc_workqueue("ib-comp-wq",
b7363e67 1446 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
14d3a3b2
CH
1447 if (!ib_comp_wq) {
1448 ret = -ENOMEM;
1449 goto err;
1450 }
1451
f794809a
JM
1452 ib_comp_unbound_wq =
1453 alloc_workqueue("ib-comp-unb-wq",
1454 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM |
1455 WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE);
1456 if (!ib_comp_unbound_wq) {
1457 ret = -ENOMEM;
1458 goto err_comp;
1459 }
1460
55aeed06 1461 ret = class_register(&ib_class);
fd75c789 1462 if (ret) {
aba25a3e 1463 pr_warn("Couldn't create InfiniBand device class\n");
f794809a 1464 goto err_comp_unbound;
fd75c789 1465 }
1da177e4 1466
c9901724 1467 ret = rdma_nl_init();
b2cbae2c 1468 if (ret) {
c9901724 1469 pr_warn("Couldn't init IB netlink interface: err %d\n", ret);
b2cbae2c
RD
1470 goto err_sysfs;
1471 }
1472
e3f20f02
LR
1473 ret = addr_init();
1474 if (ret) {
1475 pr_warn("Could't init IB address resolution\n");
1476 goto err_ibnl;
1477 }
1478
4c2cb422
MB
1479 ret = ib_mad_init();
1480 if (ret) {
1481 pr_warn("Couldn't init IB MAD\n");
1482 goto err_addr;
1483 }
1484
c2e49c92
MB
1485 ret = ib_sa_init();
1486 if (ret) {
1487 pr_warn("Couldn't init SA\n");
1488 goto err_mad;
1489 }
1490
8f408ab6
DJ
1491 ret = register_lsm_notifier(&ibdev_lsm_nb);
1492 if (ret) {
1493 pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
c9901724 1494 goto err_sa;
8f408ab6
DJ
1495 }
1496
6c80b41a 1497 nldev_init();
c9901724 1498 rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
5ef8c0c1 1499 roce_gid_mgmt_init();
1da177e4 1500
fd75c789
NM
1501 return 0;
1502
735c631a
MB
1503err_sa:
1504 ib_sa_cleanup();
c2e49c92
MB
1505err_mad:
1506 ib_mad_cleanup();
4c2cb422
MB
1507err_addr:
1508 addr_cleanup();
e3f20f02 1509err_ibnl:
c9901724 1510 rdma_nl_exit();
fd75c789 1511err_sysfs:
55aeed06 1512 class_unregister(&ib_class);
f794809a
JM
1513err_comp_unbound:
1514 destroy_workqueue(ib_comp_unbound_wq);
14d3a3b2
CH
1515err_comp:
1516 destroy_workqueue(ib_comp_wq);
fd75c789
NM
1517err:
1518 destroy_workqueue(ib_wq);
1da177e4
LT
1519 return ret;
1520}
1521
1522static void __exit ib_core_cleanup(void)
1523{
5ef8c0c1 1524 roce_gid_mgmt_cleanup();
6c80b41a 1525 nldev_exit();
c9901724
LR
1526 rdma_nl_unregister(RDMA_NL_LS);
1527 unregister_lsm_notifier(&ibdev_lsm_nb);
c2e49c92 1528 ib_sa_cleanup();
4c2cb422 1529 ib_mad_cleanup();
e3f20f02 1530 addr_cleanup();
c9901724 1531 rdma_nl_exit();
55aeed06 1532 class_unregister(&ib_class);
f794809a 1533 destroy_workqueue(ib_comp_unbound_wq);
14d3a3b2 1534 destroy_workqueue(ib_comp_wq);
f7c6a7b5 1535 /* Make sure that any pending umem accounting work is done. */
f0626710 1536 destroy_workqueue(ib_wq);
e59178d8 1537 WARN_ON(!xa_empty(&clients));
0df91bb6 1538 WARN_ON(!xa_empty(&devices));
1da177e4
LT
1539}
1540
e3bf14bd
JG
1541MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
1542
a9cd1a67 1543subsys_initcall(ib_core_init);
1da177e4 1544module_exit(ib_core_cleanup);