1 /*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
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.
32 */
33
34 #include <linux/module.h>
35 #include <linux/string.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/slab.h>
39 #include <linux/init.h>
40 #include <linux/netdevice.h>
41 #include <net/net_namespace.h>
42 #include <linux/security.h>
43 #include <linux/notifier.h>
44 #include <linux/hashtable.h>
45 #include <rdma/rdma_netlink.h>
46 #include <rdma/ib_addr.h>
47 #include <rdma/ib_cache.h>
48 #include <rdma/rdma_counter.h>
49
50 #include "core_priv.h"
51 #include "restrack.h"
52
53 MODULE_AUTHOR("Roland Dreier");
54 MODULE_DESCRIPTION("core kernel InfiniBand API");
55 MODULE_LICENSE("Dual BSD/GPL");
56
57 struct workqueue_struct *ib_comp_wq;
58 struct workqueue_struct *ib_comp_unbound_wq;
59 struct workqueue_struct *ib_wq;
60 EXPORT_SYMBOL_GPL(ib_wq);
61 static struct workqueue_struct *ib_unreg_wq;
62
63 /*
64 * Each of the three rwsem locks (devices, clients, client_data) protects the
65 * xarray of the same name. Specifically it allows the caller to assert that
66 * the MARK will/will not be changing under the lock, and for devices and
67 * clients, that the value in the xarray is still a valid pointer. Change of
68 * the MARK is linked to the object state, so holding the lock and testing the
69 * MARK also asserts that the contained object is in a certain state.
70 *
71 * This is used to build a two stage register/unregister flow where objects
72 * can continue to be in the xarray even though they are still in progress to
73 * register/unregister.
74 *
75 * The xarray itself provides additional locking, and restartable iteration,
76 * which is also relied on.
77 *
78 * Locks should not be nested, with the exception of client_data, which is
79 * allowed to nest under the read side of the other two locks.
80 *
81 * The devices_rwsem also protects the device name list, any change or
82 * assignment of device name must also hold the write side to guarantee unique
83 * names.
84 */
85
86 /*
87 * devices contains devices that have had their names assigned. The
88 * devices may not be registered. Users that care about the registration
89 * status need to call ib_device_try_get() on the device to ensure it is
90 * registered, and keep it registered, for the required duration.
91 *
92 */
93 static DEFINE_XARRAY_FLAGS(devices, XA_FLAGS_ALLOC);
94 static DECLARE_RWSEM(devices_rwsem);
95 #define DEVICE_REGISTERED XA_MARK_1
96
97 static u32 highest_client_id;
98 #define CLIENT_REGISTERED XA_MARK_1
99 static DEFINE_XARRAY_FLAGS(clients, XA_FLAGS_ALLOC);
100 static DECLARE_RWSEM(clients_rwsem);
101
ib_client_put(struct ib_client * client)102 static void ib_client_put(struct ib_client *client)
103 {
104 if (refcount_dec_and_test(&client->uses))
105 complete(&client->uses_zero);
106 }
107
108 /*
109 * If client_data is registered then the corresponding client must also still
110 * be registered.
111 */
112 #define CLIENT_DATA_REGISTERED XA_MARK_1
113
114 unsigned int rdma_dev_net_id;
115
116 /*
117 * A list of net namespaces is maintained in an xarray. This is necessary
118 * because we can't get the locking right using the existing net ns list. We
119 * would require a init_net callback after the list is updated.
120 */
121 static DEFINE_XARRAY_FLAGS(rdma_nets, XA_FLAGS_ALLOC);
122 /*
123 * rwsem to protect accessing the rdma_nets xarray entries.
124 */
125 static DECLARE_RWSEM(rdma_nets_rwsem);
126
127 bool ib_devices_shared_netns = true;
128 module_param_named(netns_mode, ib_devices_shared_netns, bool, 0444);
129 MODULE_PARM_DESC(netns_mode,
130 "Share device among net namespaces; default=1 (shared)");
131 /**
132 * rdma_dev_access_netns() - Return whether an rdma device can be accessed
133 * from a specified net namespace or not.
134 * @dev: Pointer to rdma device which needs to be checked
135 * @net: Pointer to net namesapce for which access to be checked
136 *
137 * When the rdma device is in shared mode, it ignores the net namespace.
138 * When the rdma device is exclusive to a net namespace, rdma device net
139 * namespace is checked against the specified one.
140 */
rdma_dev_access_netns(const struct ib_device * dev,const struct net * net)141 bool rdma_dev_access_netns(const struct ib_device *dev, const struct net *net)
142 {
143 return (ib_devices_shared_netns ||
144 net_eq(read_pnet(&dev->coredev.rdma_net), net));
145 }
146 EXPORT_SYMBOL(rdma_dev_access_netns);
147
148 /*
149 * xarray has this behavior where it won't iterate over NULL values stored in
150 * allocated arrays. So we need our own iterator to see all values stored in
151 * the array. This does the same thing as xa_for_each except that it also
152 * returns NULL valued entries if the array is allocating. Simplified to only
153 * work on simple xarrays.
154 */
xan_find_marked(struct xarray * xa,unsigned long * indexp,xa_mark_t filter)155 static void *xan_find_marked(struct xarray *xa, unsigned long *indexp,
156 xa_mark_t filter)
157 {
158 XA_STATE(xas, xa, *indexp);
159 void *entry;
160
161 rcu_read_lock();
162 do {
163 entry = xas_find_marked(&xas, ULONG_MAX, filter);
164 if (xa_is_zero(entry))
165 break;
166 } while (xas_retry(&xas, entry));
167 rcu_read_unlock();
168
169 if (entry) {
170 *indexp = xas.xa_index;
171 if (xa_is_zero(entry))
172 return NULL;
173 return entry;
174 }
175 return XA_ERROR(-ENOENT);
176 }
177 #define xan_for_each_marked(xa, index, entry, filter) \
178 for (index = 0, entry = xan_find_marked(xa, &(index), filter); \
179 !xa_is_err(entry); \
180 (index)++, entry = xan_find_marked(xa, &(index), filter))
181
182 /* RCU hash table mapping netdevice pointers to struct ib_port_data */
183 static DEFINE_SPINLOCK(ndev_hash_lock);
184 static DECLARE_HASHTABLE(ndev_hash, 5);
185
186 static void free_netdevs(struct ib_device *ib_dev);
187 static void ib_unregister_work(struct work_struct *work);
188 static void __ib_unregister_device(struct ib_device *device);
189 static int ib_security_change(struct notifier_block *nb, unsigned long event,
190 void *lsm_data);
191 static void ib_policy_change_task(struct work_struct *work);
192 static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
193
__ibdev_printk(const char * level,const struct ib_device * ibdev,struct va_format * vaf)194 static void __ibdev_printk(const char *level, const struct ib_device *ibdev,
195 struct va_format *vaf)
196 {
197 if (ibdev && ibdev->dev.parent)
198 dev_printk_emit(level[1] - '0',
199 ibdev->dev.parent,
200 "%s %s %s: %pV",
201 dev_driver_string(ibdev->dev.parent),
202 dev_name(ibdev->dev.parent),
203 dev_name(&ibdev->dev),
204 vaf);
205 else if (ibdev)
206 printk("%s%s: %pV",
207 level, dev_name(&ibdev->dev), vaf);
208 else
209 printk("%s(NULL ib_device): %pV", level, vaf);
210 }
211
ibdev_printk(const char * level,const struct ib_device * ibdev,const char * format,...)212 void ibdev_printk(const char *level, const struct ib_device *ibdev,
213 const char *format, ...)
214 {
215 struct va_format vaf;
216 va_list args;
217
218 va_start(args, format);
219
220 vaf.fmt = format;
221 vaf.va = &args;
222
223 __ibdev_printk(level, ibdev, &vaf);
224
225 va_end(args);
226 }
227 EXPORT_SYMBOL(ibdev_printk);
228
229 #define define_ibdev_printk_level(func, level) \
230 void func(const struct ib_device *ibdev, const char *fmt, ...) \
231 { \
232 struct va_format vaf; \
233 va_list args; \
234 \
235 va_start(args, fmt); \
236 \
237 vaf.fmt = fmt; \
238 vaf.va = &args; \
239 \
240 __ibdev_printk(level, ibdev, &vaf); \
241 \
242 va_end(args); \
243 } \
244 EXPORT_SYMBOL(func);
245
246 define_ibdev_printk_level(ibdev_emerg, KERN_EMERG);
247 define_ibdev_printk_level(ibdev_alert, KERN_ALERT);
248 define_ibdev_printk_level(ibdev_crit, KERN_CRIT);
249 define_ibdev_printk_level(ibdev_err, KERN_ERR);
250 define_ibdev_printk_level(ibdev_warn, KERN_WARNING);
251 define_ibdev_printk_level(ibdev_notice, KERN_NOTICE);
252 define_ibdev_printk_level(ibdev_info, KERN_INFO);
253
254 static struct notifier_block ibdev_lsm_nb = {
255 .notifier_call = ib_security_change,
256 };
257
258 static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
259 struct net *net);
260
261 /* Pointer to the RCU head at the start of the ib_port_data array */
262 struct ib_port_data_rcu {
263 struct rcu_head rcu_head;
264 struct ib_port_data pdata[];
265 };
266
ib_device_check_mandatory(struct ib_device * device)267 static void ib_device_check_mandatory(struct ib_device *device)
268 {
269 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x }
270 static const struct {
271 size_t offset;
272 char *name;
273 } mandatory_table[] = {
274 IB_MANDATORY_FUNC(query_device),
275 IB_MANDATORY_FUNC(query_port),
276 IB_MANDATORY_FUNC(alloc_pd),
277 IB_MANDATORY_FUNC(dealloc_pd),
278 IB_MANDATORY_FUNC(create_qp),
279 IB_MANDATORY_FUNC(modify_qp),
280 IB_MANDATORY_FUNC(destroy_qp),
281 IB_MANDATORY_FUNC(post_send),
282 IB_MANDATORY_FUNC(post_recv),
283 IB_MANDATORY_FUNC(create_cq),
284 IB_MANDATORY_FUNC(destroy_cq),
285 IB_MANDATORY_FUNC(poll_cq),
286 IB_MANDATORY_FUNC(req_notify_cq),
287 IB_MANDATORY_FUNC(get_dma_mr),
288 IB_MANDATORY_FUNC(reg_user_mr),
289 IB_MANDATORY_FUNC(dereg_mr),
290 IB_MANDATORY_FUNC(get_port_immutable)
291 };
292 int i;
293
294 device->kverbs_provider = true;
295 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
296 if (!*(void **) ((void *) &device->ops +
297 mandatory_table[i].offset)) {
298 device->kverbs_provider = false;
299 break;
300 }
301 }
302 }
303
304 /*
305 * Caller must perform ib_device_put() to return the device reference count
306 * when ib_device_get_by_index() returns valid device pointer.
307 */
ib_device_get_by_index(const struct net * net,u32 index)308 struct ib_device *ib_device_get_by_index(const struct net *net, u32 index)
309 {
310 struct ib_device *device;
311
312 down_read(&devices_rwsem);
313 device = xa_load(&devices, index);
314 if (device) {
315 if (!rdma_dev_access_netns(device, net)) {
316 device = NULL;
317 goto out;
318 }
319
320 if (!ib_device_try_get(device))
321 device = NULL;
322 }
323 out:
324 up_read(&devices_rwsem);
325 return device;
326 }
327
328 /**
329 * ib_device_put - Release IB device reference
330 * @device: device whose reference to be released
331 *
332 * ib_device_put() releases reference to the IB device to allow it to be
333 * unregistered and eventually free.
334 */
ib_device_put(struct ib_device * device)335 void ib_device_put(struct ib_device *device)
336 {
337 if (refcount_dec_and_test(&device->refcount))
338 complete(&device->unreg_completion);
339 }
340 EXPORT_SYMBOL(ib_device_put);
341
__ib_device_get_by_name(const char * name)342 static struct ib_device *__ib_device_get_by_name(const char *name)
343 {
344 struct ib_device *device;
345 unsigned long index;
346
347 xa_for_each (&devices, index, device)
348 if (!strcmp(name, dev_name(&device->dev)))
349 return device;
350
351 return NULL;
352 }
353
354 /**
355 * ib_device_get_by_name - Find an IB device by name
356 * @name: The name to look for
357 * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all)
358 *
359 * Find and hold an ib_device by its name. The caller must call
360 * ib_device_put() on the returned pointer.
361 */
ib_device_get_by_name(const char * name,enum rdma_driver_id driver_id)362 struct ib_device *ib_device_get_by_name(const char *name,
363 enum rdma_driver_id driver_id)
364 {
365 struct ib_device *device;
366
367 down_read(&devices_rwsem);
368 device = __ib_device_get_by_name(name);
369 if (device && driver_id != RDMA_DRIVER_UNKNOWN &&
370 device->ops.driver_id != driver_id)
371 device = NULL;
372
373 if (device) {
374 if (!ib_device_try_get(device))
375 device = NULL;
376 }
377 up_read(&devices_rwsem);
378 return device;
379 }
380 EXPORT_SYMBOL(ib_device_get_by_name);
381
rename_compat_devs(struct ib_device * device)382 static int rename_compat_devs(struct ib_device *device)
383 {
384 struct ib_core_device *cdev;
385 unsigned long index;
386 int ret = 0;
387
388 mutex_lock(&device->compat_devs_mutex);
389 xa_for_each (&device->compat_devs, index, cdev) {
390 ret = device_rename(&cdev->dev, dev_name(&device->dev));
391 if (ret) {
392 dev_warn(&cdev->dev,
393 "Fail to rename compatdev to new name %s\n",
394 dev_name(&device->dev));
395 break;
396 }
397 }
398 mutex_unlock(&device->compat_devs_mutex);
399 return ret;
400 }
401
ib_device_rename(struct ib_device * ibdev,const char * name)402 int ib_device_rename(struct ib_device *ibdev, const char *name)
403 {
404 unsigned long index;
405 void *client_data;
406 int ret;
407
408 down_write(&devices_rwsem);
409 if (!strcmp(name, dev_name(&ibdev->dev))) {
410 up_write(&devices_rwsem);
411 return 0;
412 }
413
414 if (__ib_device_get_by_name(name)) {
415 up_write(&devices_rwsem);
416 return -EEXIST;
417 }
418
419 ret = device_rename(&ibdev->dev, name);
420 if (ret) {
421 up_write(&devices_rwsem);
422 return ret;
423 }
424
425 strscpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
426 ret = rename_compat_devs(ibdev);
427
428 downgrade_write(&devices_rwsem);
429 down_read(&ibdev->client_data_rwsem);
430 xan_for_each_marked(&ibdev->client_data, index, client_data,
431 CLIENT_DATA_REGISTERED) {
432 struct ib_client *client = xa_load(&clients, index);
433
434 if (!client || !client->rename)
435 continue;
436
437 client->rename(ibdev, client_data);
438 }
439 up_read(&ibdev->client_data_rwsem);
440 up_read(&devices_rwsem);
441 return 0;
442 }
443
ib_device_set_dim(struct ib_device * ibdev,u8 use_dim)444 int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim)
445 {
446 if (use_dim > 1)
447 return -EINVAL;
448 ibdev->use_cq_dim = use_dim;
449
450 return 0;
451 }
452
alloc_name(struct ib_device * ibdev,const char * name)453 static int alloc_name(struct ib_device *ibdev, const char *name)
454 {
455 struct ib_device *device;
456 unsigned long index;
457 struct ida inuse;
458 int rc;
459 int i;
460
461 lockdep_assert_held_write(&devices_rwsem);
462 ida_init(&inuse);
463 xa_for_each (&devices, index, device) {
464 char buf[IB_DEVICE_NAME_MAX];
465
466 if (sscanf(dev_name(&device->dev), name, &i) != 1)
467 continue;
468 if (i < 0 || i >= INT_MAX)
469 continue;
470 snprintf(buf, sizeof buf, name, i);
471 if (strcmp(buf, dev_name(&device->dev)) != 0)
472 continue;
473
474 rc = ida_alloc_range(&inuse, i, i, GFP_KERNEL);
475 if (rc < 0)
476 goto out;
477 }
478
479 rc = ida_alloc(&inuse, GFP_KERNEL);
480 if (rc < 0)
481 goto out;
482
483 rc = dev_set_name(&ibdev->dev, name, rc);
484 out:
485 ida_destroy(&inuse);
486 return rc;
487 }
488
ib_device_release(struct device * device)489 static void ib_device_release(struct device *device)
490 {
491 struct ib_device *dev = container_of(device, struct ib_device, dev);
492
493 free_netdevs(dev);
494 WARN_ON(refcount_read(&dev->refcount));
495 if (dev->hw_stats_data)
496 ib_device_release_hw_stats(dev->hw_stats_data);
497 if (dev->port_data) {
498 ib_cache_release_one(dev);
499 ib_security_release_port_pkey_list(dev);
500 rdma_counter_release(dev);
501 kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu,
502 pdata[0]),
503 rcu_head);
504 }
505
506 mutex_destroy(&dev->subdev_lock);
507 mutex_destroy(&dev->unregistration_lock);
508 mutex_destroy(&dev->compat_devs_mutex);
509
510 xa_destroy(&dev->compat_devs);
511 xa_destroy(&dev->client_data);
512 kfree_rcu(dev, rcu_head);
513 }
514
ib_device_uevent(const struct device * device,struct kobj_uevent_env * env)515 static int ib_device_uevent(const struct device *device,
516 struct kobj_uevent_env *env)
517 {
518 if (add_uevent_var(env, "NAME=%s", dev_name(device)))
519 return -ENOMEM;
520
521 /*
522 * It would be nice to pass the node GUID with the event...
523 */
524
525 return 0;
526 }
527
net_namespace(const struct device * d)528 static const void *net_namespace(const struct device *d)
529 {
530 const struct ib_core_device *coredev =
531 container_of(d, struct ib_core_device, dev);
532
533 return read_pnet(&coredev->rdma_net);
534 }
535
536 static struct class ib_class = {
537 .name = "infiniband",
538 .dev_release = ib_device_release,
539 .dev_uevent = ib_device_uevent,
540 .ns_type = &net_ns_type_operations,
541 .namespace = net_namespace,
542 };
543
rdma_init_coredev(struct ib_core_device * coredev,struct ib_device * dev,struct net * net)544 static void rdma_init_coredev(struct ib_core_device *coredev,
545 struct ib_device *dev, struct net *net)
546 {
547 bool is_full_dev = &dev->coredev == coredev;
548
549 /* This BUILD_BUG_ON is intended to catch layout change
550 * of union of ib_core_device and device.
551 * dev must be the first element as ib_core and providers
552 * driver uses it. Adding anything in ib_core_device before
553 * device will break this assumption.
554 */
555 BUILD_BUG_ON(offsetof(struct ib_device, coredev.dev) !=
556 offsetof(struct ib_device, dev));
557
558 coredev->dev.class = &ib_class;
559 coredev->dev.groups = dev->groups;
560
561 /*
562 * Don't expose hw counters outside of the init namespace.
563 */
564 if (!is_full_dev && dev->hw_stats_attr_index)
565 coredev->dev.groups[dev->hw_stats_attr_index] = NULL;
566
567 device_initialize(&coredev->dev);
568 coredev->owner = dev;
569 INIT_LIST_HEAD(&coredev->port_list);
570 write_pnet(&coredev->rdma_net, net);
571 }
572
573 /**
574 * _ib_alloc_device - allocate an IB device struct
575 * @size:size of structure to allocate
576 *
577 * Low-level drivers should use ib_alloc_device() to allocate &struct
578 * ib_device. @size is the size of the structure to be allocated,
579 * including any private data used by the low-level driver.
580 * ib_dealloc_device() must be used to free structures allocated with
581 * ib_alloc_device().
582 */
_ib_alloc_device(size_t size)583 struct ib_device *_ib_alloc_device(size_t size)
584 {
585 struct ib_device *device;
586 unsigned int i;
587
588 if (WARN_ON(size < sizeof(struct ib_device)))
589 return NULL;
590
591 device = kzalloc(size, GFP_KERNEL);
592 if (!device)
593 return NULL;
594
595 if (rdma_restrack_init(device)) {
596 kfree(device);
597 return NULL;
598 }
599
600 rdma_init_coredev(&device->coredev, device, &init_net);
601
602 INIT_LIST_HEAD(&device->event_handler_list);
603 spin_lock_init(&device->qp_open_list_lock);
604 init_rwsem(&device->event_handler_rwsem);
605 mutex_init(&device->unregistration_lock);
606 /*
607 * client_data needs to be alloc because we don't want our mark to be
608 * destroyed if the user stores NULL in the client data.
609 */
610 xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
611 init_rwsem(&device->client_data_rwsem);
612 xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC);
613 mutex_init(&device->compat_devs_mutex);
614 init_completion(&device->unreg_completion);
615 INIT_WORK(&device->unregistration_work, ib_unregister_work);
616
617 spin_lock_init(&device->cq_pools_lock);
618 for (i = 0; i < ARRAY_SIZE(device->cq_pools); i++)
619 INIT_LIST_HEAD(&device->cq_pools[i]);
620
621 rwlock_init(&device->cache_lock);
622
623 device->uverbs_cmd_mask =
624 BIT_ULL(IB_USER_VERBS_CMD_ALLOC_MW) |
625 BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD) |
626 BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST) |
627 BIT_ULL(IB_USER_VERBS_CMD_CLOSE_XRCD) |
628 BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH) |
629 BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
630 BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ) |
631 BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP) |
632 BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ) |
633 BIT_ULL(IB_USER_VERBS_CMD_CREATE_XSRQ) |
634 BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_MW) |
635 BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD) |
636 BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR) |
637 BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH) |
638 BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ) |
639 BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP) |
640 BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ) |
641 BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST) |
642 BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT) |
643 BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP) |
644 BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ) |
645 BIT_ULL(IB_USER_VERBS_CMD_OPEN_QP) |
646 BIT_ULL(IB_USER_VERBS_CMD_OPEN_XRCD) |
647 BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE) |
648 BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT) |
649 BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP) |
650 BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ) |
651 BIT_ULL(IB_USER_VERBS_CMD_REG_MR) |
652 BIT_ULL(IB_USER_VERBS_CMD_REREG_MR) |
653 BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ);
654
655 mutex_init(&device->subdev_lock);
656 INIT_LIST_HEAD(&device->subdev_list_head);
657 INIT_LIST_HEAD(&device->subdev_list);
658
659 return device;
660 }
661 EXPORT_SYMBOL(_ib_alloc_device);
662
663 /**
664 * ib_dealloc_device - free an IB device struct
665 * @device:structure to free
666 *
667 * Free a structure allocated with ib_alloc_device().
668 */
ib_dealloc_device(struct ib_device * device)669 void ib_dealloc_device(struct ib_device *device)
670 {
671 if (device->ops.dealloc_driver)
672 device->ops.dealloc_driver(device);
673
674 /*
675 * ib_unregister_driver() requires all devices to remain in the xarray
676 * while their ops are callable. The last op we call is dealloc_driver
677 * above. This is needed to create a fence on op callbacks prior to
678 * allowing the driver module to unload.
679 */
680 down_write(&devices_rwsem);
681 if (xa_load(&devices, device->index) == device)
682 xa_erase(&devices, device->index);
683 up_write(&devices_rwsem);
684
685 /* Expedite releasing netdev references */
686 free_netdevs(device);
687
688 WARN_ON(!xa_empty(&device->compat_devs));
689 WARN_ON(!xa_empty(&device->client_data));
690 WARN_ON(refcount_read(&device->refcount));
691 rdma_restrack_clean(device);
692 /* Balances with device_initialize */
693 put_device(&device->dev);
694 }
695 EXPORT_SYMBOL(ib_dealloc_device);
696
697 /*
698 * add_client_context() and remove_client_context() must be safe against
699 * parallel calls on the same device - registration/unregistration of both the
700 * device and client can be occurring in parallel.
701 *
702 * The routines need to be a fence, any caller must not return until the add
703 * or remove is fully completed.
704 */
add_client_context(struct ib_device * device,struct ib_client * client)705 static int add_client_context(struct ib_device *device,
706 struct ib_client *client)
707 {
708 int ret = 0;
709
710 if (!device->kverbs_provider && !client->no_kverbs_req)
711 return 0;
712
713 down_write(&device->client_data_rwsem);
714 /*
715 * So long as the client is registered hold both the client and device
716 * unregistration locks.
717 */
718 if (!refcount_inc_not_zero(&client->uses))
719 goto out_unlock;
720 refcount_inc(&device->refcount);
721
722 /*
723 * Another caller to add_client_context got here first and has already
724 * completely initialized context.
725 */
726 if (xa_get_mark(&device->client_data, client->client_id,
727 CLIENT_DATA_REGISTERED))
728 goto out;
729
730 ret = xa_err(xa_store(&device->client_data, client->client_id, NULL,
731 GFP_KERNEL));
732 if (ret)
733 goto out;
734 downgrade_write(&device->client_data_rwsem);
735 if (client->add) {
736 if (client->add(device)) {
737 /*
738 * If a client fails to add then the error code is
739 * ignored, but we won't call any more ops on this
740 * client.
741 */
742 xa_erase(&device->client_data, client->client_id);
743 up_read(&device->client_data_rwsem);
744 ib_device_put(device);
745 ib_client_put(client);
746 return 0;
747 }
748 }
749
750 /* Readers shall not see a client until add has been completed */
751 xa_set_mark(&device->client_data, client->client_id,
752 CLIENT_DATA_REGISTERED);
753 up_read(&device->client_data_rwsem);
754 return 0;
755
756 out:
757 ib_device_put(device);
758 ib_client_put(client);
759 out_unlock:
760 up_write(&device->client_data_rwsem);
761 return ret;
762 }
763
remove_client_context(struct ib_device * device,unsigned int client_id)764 static void remove_client_context(struct ib_device *device,
765 unsigned int client_id)
766 {
767 struct ib_client *client;
768 void *client_data;
769
770 down_write(&device->client_data_rwsem);
771 if (!xa_get_mark(&device->client_data, client_id,
772 CLIENT_DATA_REGISTERED)) {
773 up_write(&device->client_data_rwsem);
774 return;
775 }
776 client_data = xa_load(&device->client_data, client_id);
777 xa_clear_mark(&device->client_data, client_id, CLIENT_DATA_REGISTERED);
778 client = xa_load(&clients, client_id);
779 up_write(&device->client_data_rwsem);
780
781 /*
782 * Notice we cannot be holding any exclusive locks when calling the
783 * remove callback as the remove callback can recurse back into any
784 * public functions in this module and thus try for any locks those
785 * functions take.
786 *
787 * For this reason clients and drivers should not call the
788 * unregistration functions will holdling any locks.
789 */
790 if (client->remove)
791 client->remove(device, client_data);
792
793 xa_erase(&device->client_data, client_id);
794 ib_device_put(device);
795 ib_client_put(client);
796 }
797
alloc_port_data(struct ib_device * device)798 static int alloc_port_data(struct ib_device *device)
799 {
800 struct ib_port_data_rcu *pdata_rcu;
801 u32 port;
802
803 if (device->port_data)
804 return 0;
805
806 /* This can only be called once the physical port range is defined */
807 if (WARN_ON(!device->phys_port_cnt))
808 return -EINVAL;
809
810 /* Reserve U32_MAX so the logic to go over all the ports is sane */
811 if (WARN_ON(device->phys_port_cnt == U32_MAX))
812 return -EINVAL;
813
814 /*
815 * device->port_data is indexed directly by the port number to make
816 * access to this data as efficient as possible.
817 *
818 * Therefore port_data is declared as a 1 based array with potential
819 * empty slots at the beginning.
820 */
821 pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata,
822 size_add(rdma_end_port(device), 1)),
823 GFP_KERNEL);
824 if (!pdata_rcu)
825 return -ENOMEM;
826 /*
827 * The rcu_head is put in front of the port data array and the stored
828 * pointer is adjusted since we never need to see that member until
829 * kfree_rcu.
830 */
831 device->port_data = pdata_rcu->pdata;
832
833 rdma_for_each_port (device, port) {
834 struct ib_port_data *pdata = &device->port_data[port];
835
836 pdata->ib_dev = device;
837 spin_lock_init(&pdata->pkey_list_lock);
838 INIT_LIST_HEAD(&pdata->pkey_list);
839 spin_lock_init(&pdata->netdev_lock);
840 INIT_HLIST_NODE(&pdata->ndev_hash_link);
841 }
842 return 0;
843 }
844
verify_immutable(const struct ib_device * dev,u32 port)845 static int verify_immutable(const struct ib_device *dev, u32 port)
846 {
847 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
848 rdma_max_mad_size(dev, port) != 0);
849 }
850
setup_port_data(struct ib_device * device)851 static int setup_port_data(struct ib_device *device)
852 {
853 u32 port;
854 int ret;
855
856 ret = alloc_port_data(device);
857 if (ret)
858 return ret;
859
860 rdma_for_each_port (device, port) {
861 struct ib_port_data *pdata = &device->port_data[port];
862
863 ret = device->ops.get_port_immutable(device, port,
864 &pdata->immutable);
865 if (ret)
866 return ret;
867
868 if (verify_immutable(device, port))
869 return -EINVAL;
870 }
871 return 0;
872 }
873
874 /**
875 * ib_port_immutable_read() - Read rdma port's immutable data
876 * @dev: IB device
877 * @port: port number whose immutable data to read. It starts with index 1 and
878 * valid upto including rdma_end_port().
879 */
880 const struct ib_port_immutable*
ib_port_immutable_read(struct ib_device * dev,unsigned int port)881 ib_port_immutable_read(struct ib_device *dev, unsigned int port)
882 {
883 WARN_ON(!rdma_is_port_valid(dev, port));
884 return &dev->port_data[port].immutable;
885 }
886 EXPORT_SYMBOL(ib_port_immutable_read);
887
ib_get_device_fw_str(struct ib_device * dev,char * str)888 void ib_get_device_fw_str(struct ib_device *dev, char *str)
889 {
890 if (dev->ops.get_dev_fw_str)
891 dev->ops.get_dev_fw_str(dev, str);
892 else
893 str[0] = '\0';
894 }
895 EXPORT_SYMBOL(ib_get_device_fw_str);
896
ib_policy_change_task(struct work_struct * work)897 static void ib_policy_change_task(struct work_struct *work)
898 {
899 struct ib_device *dev;
900 unsigned long index;
901
902 down_read(&devices_rwsem);
903 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
904 unsigned int i;
905
906 rdma_for_each_port (dev, i) {
907 u64 sp;
908 ib_get_cached_subnet_prefix(dev, i, &sp);
909 ib_security_cache_change(dev, i, sp);
910 }
911 }
912 up_read(&devices_rwsem);
913 }
914
ib_security_change(struct notifier_block * nb,unsigned long event,void * lsm_data)915 static int ib_security_change(struct notifier_block *nb, unsigned long event,
916 void *lsm_data)
917 {
918 if (event != LSM_POLICY_CHANGE)
919 return NOTIFY_DONE;
920
921 schedule_work(&ib_policy_change_work);
922 ib_mad_agent_security_change();
923
924 return NOTIFY_OK;
925 }
926
compatdev_release(struct device * dev)927 static void compatdev_release(struct device *dev)
928 {
929 struct ib_core_device *cdev =
930 container_of(dev, struct ib_core_device, dev);
931
932 kfree(cdev);
933 }
934
add_one_compat_dev(struct ib_device * device,struct rdma_dev_net * rnet)935 static int add_one_compat_dev(struct ib_device *device,
936 struct rdma_dev_net *rnet)
937 {
938 struct ib_core_device *cdev;
939 int ret;
940
941 lockdep_assert_held(&rdma_nets_rwsem);
942 if (!ib_devices_shared_netns)
943 return 0;
944
945 /*
946 * Create and add compat device in all namespaces other than where it
947 * is currently bound to.
948 */
949 if (net_eq(read_pnet(&rnet->net),
950 read_pnet(&device->coredev.rdma_net)))
951 return 0;
952
953 /*
954 * The first of init_net() or ib_register_device() to take the
955 * compat_devs_mutex wins and gets to add the device. Others will wait
956 * for completion here.
957 */
958 mutex_lock(&device->compat_devs_mutex);
959 cdev = xa_load(&device->compat_devs, rnet->id);
960 if (cdev) {
961 ret = 0;
962 goto done;
963 }
964 ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
965 if (ret)
966 goto done;
967
968 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
969 if (!cdev) {
970 ret = -ENOMEM;
971 goto cdev_err;
972 }
973
974 cdev->dev.parent = device->dev.parent;
975 rdma_init_coredev(cdev, device, read_pnet(&rnet->net));
976 cdev->dev.release = compatdev_release;
977 ret = dev_set_name(&cdev->dev, "%s", dev_name(&device->dev));
978 if (ret)
979 goto add_err;
980
981 ret = device_add(&cdev->dev);
982 if (ret)
983 goto add_err;
984 ret = ib_setup_port_attrs(cdev);
985 if (ret)
986 goto port_err;
987
988 ret = xa_err(xa_store(&device->compat_devs, rnet->id,
989 cdev, GFP_KERNEL));
990 if (ret)
991 goto insert_err;
992
993 mutex_unlock(&device->compat_devs_mutex);
994 return 0;
995
996 insert_err:
997 ib_free_port_attrs(cdev);
998 port_err:
999 device_del(&cdev->dev);
1000 add_err:
1001 put_device(&cdev->dev);
1002 cdev_err:
1003 xa_release(&device->compat_devs, rnet->id);
1004 done:
1005 mutex_unlock(&device->compat_devs_mutex);
1006 return ret;
1007 }
1008
remove_one_compat_dev(struct ib_device * device,u32 id)1009 static void remove_one_compat_dev(struct ib_device *device, u32 id)
1010 {
1011 struct ib_core_device *cdev;
1012
1013 mutex_lock(&device->compat_devs_mutex);
1014 cdev = xa_erase(&device->compat_devs, id);
1015 mutex_unlock(&device->compat_devs_mutex);
1016 if (cdev) {
1017 ib_free_port_attrs(cdev);
1018 device_del(&cdev->dev);
1019 put_device(&cdev->dev);
1020 }
1021 }
1022
remove_compat_devs(struct ib_device * device)1023 static void remove_compat_devs(struct ib_device *device)
1024 {
1025 struct ib_core_device *cdev;
1026 unsigned long index;
1027
1028 xa_for_each (&device->compat_devs, index, cdev)
1029 remove_one_compat_dev(device, index);
1030 }
1031
add_compat_devs(struct ib_device * device)1032 static int add_compat_devs(struct ib_device *device)
1033 {
1034 struct rdma_dev_net *rnet;
1035 unsigned long index;
1036 int ret = 0;
1037
1038 lockdep_assert_held(&devices_rwsem);
1039
1040 down_read(&rdma_nets_rwsem);
1041 xa_for_each (&rdma_nets, index, rnet) {
1042 ret = add_one_compat_dev(device, rnet);
1043 if (ret)
1044 break;
1045 }
1046 up_read(&rdma_nets_rwsem);
1047 return ret;
1048 }
1049
remove_all_compat_devs(void)1050 static void remove_all_compat_devs(void)
1051 {
1052 struct ib_compat_device *cdev;
1053 struct ib_device *dev;
1054 unsigned long index;
1055
1056 down_read(&devices_rwsem);
1057 xa_for_each (&devices, index, dev) {
1058 unsigned long c_index = 0;
1059
1060 /* Hold nets_rwsem so that any other thread modifying this
1061 * system param can sync with this thread.
1062 */
1063 down_read(&rdma_nets_rwsem);
1064 xa_for_each (&dev->compat_devs, c_index, cdev)
1065 remove_one_compat_dev(dev, c_index);
1066 up_read(&rdma_nets_rwsem);
1067 }
1068 up_read(&devices_rwsem);
1069 }
1070
add_all_compat_devs(void)1071 static int add_all_compat_devs(void)
1072 {
1073 struct rdma_dev_net *rnet;
1074 struct ib_device *dev;
1075 unsigned long index;
1076 int ret = 0;
1077
1078 down_read(&devices_rwsem);
1079 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1080 unsigned long net_index = 0;
1081
1082 /* Hold nets_rwsem so that any other thread modifying this
1083 * system param can sync with this thread.
1084 */
1085 down_read(&rdma_nets_rwsem);
1086 xa_for_each (&rdma_nets, net_index, rnet) {
1087 ret = add_one_compat_dev(dev, rnet);
1088 if (ret)
1089 break;
1090 }
1091 up_read(&rdma_nets_rwsem);
1092 }
1093 up_read(&devices_rwsem);
1094 if (ret)
1095 remove_all_compat_devs();
1096 return ret;
1097 }
1098
rdma_compatdev_set(u8 enable)1099 int rdma_compatdev_set(u8 enable)
1100 {
1101 struct rdma_dev_net *rnet;
1102 unsigned long index;
1103 int ret = 0;
1104
1105 down_write(&rdma_nets_rwsem);
1106 if (ib_devices_shared_netns == enable) {
1107 up_write(&rdma_nets_rwsem);
1108 return 0;
1109 }
1110
1111 /* enable/disable of compat devices is not supported
1112 * when more than default init_net exists.
1113 */
1114 xa_for_each (&rdma_nets, index, rnet) {
1115 ret++;
1116 break;
1117 }
1118 if (!ret)
1119 ib_devices_shared_netns = enable;
1120 up_write(&rdma_nets_rwsem);
1121 if (ret)
1122 return -EBUSY;
1123
1124 if (enable)
1125 ret = add_all_compat_devs();
1126 else
1127 remove_all_compat_devs();
1128 return ret;
1129 }
1130
rdma_dev_exit_net(struct net * net)1131 static void rdma_dev_exit_net(struct net *net)
1132 {
1133 struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1134 struct ib_device *dev;
1135 unsigned long index;
1136 int ret;
1137
1138 down_write(&rdma_nets_rwsem);
1139 /*
1140 * Prevent the ID from being re-used and hide the id from xa_for_each.
1141 */
1142 ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL));
1143 WARN_ON(ret);
1144 up_write(&rdma_nets_rwsem);
1145
1146 down_read(&devices_rwsem);
1147 xa_for_each (&devices, index, dev) {
1148 get_device(&dev->dev);
1149 /*
1150 * Release the devices_rwsem so that pontentially blocking
1151 * device_del, doesn't hold the devices_rwsem for too long.
1152 */
1153 up_read(&devices_rwsem);
1154
1155 remove_one_compat_dev(dev, rnet->id);
1156
1157 /*
1158 * If the real device is in the NS then move it back to init.
1159 */
1160 rdma_dev_change_netns(dev, net, &init_net);
1161
1162 put_device(&dev->dev);
1163 down_read(&devices_rwsem);
1164 }
1165 up_read(&devices_rwsem);
1166
1167 rdma_nl_net_exit(rnet);
1168 xa_erase(&rdma_nets, rnet->id);
1169 }
1170
rdma_dev_init_net(struct net * net)1171 static __net_init int rdma_dev_init_net(struct net *net)
1172 {
1173 struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1174 unsigned long index;
1175 struct ib_device *dev;
1176 int ret;
1177
1178 write_pnet(&rnet->net, net);
1179
1180 ret = rdma_nl_net_init(rnet);
1181 if (ret)
1182 return ret;
1183
1184 /* No need to create any compat devices in default init_net. */
1185 if (net_eq(net, &init_net))
1186 return 0;
1187
1188 ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
1189 if (ret) {
1190 rdma_nl_net_exit(rnet);
1191 return ret;
1192 }
1193
1194 down_read(&devices_rwsem);
1195 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1196 /* Hold nets_rwsem so that netlink command cannot change
1197 * system configuration for device sharing mode.
1198 */
1199 down_read(&rdma_nets_rwsem);
1200 ret = add_one_compat_dev(dev, rnet);
1201 up_read(&rdma_nets_rwsem);
1202 if (ret)
1203 break;
1204 }
1205 up_read(&devices_rwsem);
1206
1207 if (ret)
1208 rdma_dev_exit_net(net);
1209
1210 return ret;
1211 }
1212
1213 /*
1214 * Assign the unique string device name and the unique device index. This is
1215 * undone by ib_dealloc_device.
1216 */
assign_name(struct ib_device * device,const char * name)1217 static int assign_name(struct ib_device *device, const char *name)
1218 {
1219 static u32 last_id;
1220 int ret;
1221
1222 down_write(&devices_rwsem);
1223 /* Assign a unique name to the device */
1224 if (strchr(name, '%'))
1225 ret = alloc_name(device, name);
1226 else
1227 ret = dev_set_name(&device->dev, name);
1228 if (ret)
1229 goto out;
1230
1231 if (__ib_device_get_by_name(dev_name(&device->dev))) {
1232 ret = -ENFILE;
1233 goto out;
1234 }
1235 strscpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
1236
1237 ret = xa_alloc_cyclic(&devices, &device->index, device, xa_limit_31b,
1238 &last_id, GFP_KERNEL);
1239 if (ret > 0)
1240 ret = 0;
1241
1242 out:
1243 up_write(&devices_rwsem);
1244 return ret;
1245 }
1246
1247 /*
1248 * setup_device() allocates memory and sets up data that requires calling the
1249 * device ops, this is the only reason these actions are not done during
1250 * ib_alloc_device. It is undone by ib_dealloc_device().
1251 */
setup_device(struct ib_device * device)1252 static int setup_device(struct ib_device *device)
1253 {
1254 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
1255 int ret;
1256
1257 ib_device_check_mandatory(device);
1258
1259 ret = setup_port_data(device);
1260 if (ret) {
1261 dev_warn(&device->dev, "Couldn't create per-port data\n");
1262 return ret;
1263 }
1264
1265 memset(&device->attrs, 0, sizeof(device->attrs));
1266 ret = device->ops.query_device(device, &device->attrs, &uhw);
1267 if (ret) {
1268 dev_warn(&device->dev,
1269 "Couldn't query the device attributes\n");
1270 return ret;
1271 }
1272
1273 return 0;
1274 }
1275
disable_device(struct ib_device * device)1276 static void disable_device(struct ib_device *device)
1277 {
1278 u32 cid;
1279
1280 WARN_ON(!refcount_read(&device->refcount));
1281
1282 down_write(&devices_rwsem);
1283 xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
1284 up_write(&devices_rwsem);
1285
1286 /*
1287 * Remove clients in LIFO order, see assign_client_id. This could be
1288 * more efficient if xarray learns to reverse iterate. Since no new
1289 * clients can be added to this ib_device past this point we only need
1290 * the maximum possible client_id value here.
1291 */
1292 down_read(&clients_rwsem);
1293 cid = highest_client_id;
1294 up_read(&clients_rwsem);
1295 while (cid) {
1296 cid--;
1297 remove_client_context(device, cid);
1298 }
1299
1300 ib_cq_pool_cleanup(device);
1301
1302 /* Pairs with refcount_set in enable_device */
1303 ib_device_put(device);
1304 wait_for_completion(&device->unreg_completion);
1305
1306 /*
1307 * compat devices must be removed after device refcount drops to zero.
1308 * Otherwise init_net() may add more compatdevs after removing compat
1309 * devices and before device is disabled.
1310 */
1311 remove_compat_devs(device);
1312 }
1313
1314 /*
1315 * An enabled device is visible to all clients and to all the public facing
1316 * APIs that return a device pointer. This always returns with a new get, even
1317 * if it fails.
1318 */
enable_device_and_get(struct ib_device * device)1319 static int enable_device_and_get(struct ib_device *device)
1320 {
1321 struct ib_client *client;
1322 unsigned long index;
1323 int ret = 0;
1324
1325 /*
1326 * One ref belongs to the xa and the other belongs to this
1327 * thread. This is needed to guard against parallel unregistration.
1328 */
1329 refcount_set(&device->refcount, 2);
1330 down_write(&devices_rwsem);
1331 xa_set_mark(&devices, device->index, DEVICE_REGISTERED);
1332
1333 /*
1334 * By using downgrade_write() we ensure that no other thread can clear
1335 * DEVICE_REGISTERED while we are completing the client setup.
1336 */
1337 downgrade_write(&devices_rwsem);
1338
1339 if (device->ops.enable_driver) {
1340 ret = device->ops.enable_driver(device);
1341 if (ret)
1342 goto out;
1343 }
1344
1345 down_read(&clients_rwsem);
1346 xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1347 ret = add_client_context(device, client);
1348 if (ret)
1349 break;
1350 }
1351 up_read(&clients_rwsem);
1352 if (!ret)
1353 ret = add_compat_devs(device);
1354 out:
1355 up_read(&devices_rwsem);
1356 return ret;
1357 }
1358
prevent_dealloc_device(struct ib_device * ib_dev)1359 static void prevent_dealloc_device(struct ib_device *ib_dev)
1360 {
1361 }
1362
ib_device_notify_register(struct ib_device * device)1363 static void ib_device_notify_register(struct ib_device *device)
1364 {
1365 struct net_device *netdev;
1366 u32 port;
1367 int ret;
1368
1369 down_read(&devices_rwsem);
1370
1371 /* Mark for userspace that device is ready */
1372 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1373
1374 ret = rdma_nl_notify_event(device, 0, RDMA_REGISTER_EVENT);
1375 if (ret)
1376 goto out;
1377
1378 rdma_for_each_port(device, port) {
1379 netdev = ib_device_get_netdev(device, port);
1380 if (!netdev)
1381 continue;
1382
1383 ret = rdma_nl_notify_event(device, port,
1384 RDMA_NETDEV_ATTACH_EVENT);
1385 dev_put(netdev);
1386 if (ret)
1387 goto out;
1388 }
1389
1390 out:
1391 up_read(&devices_rwsem);
1392 }
1393
1394 /**
1395 * ib_register_device - Register an IB device with IB core
1396 * @device: Device to register
1397 * @name: unique string device name. This may include a '%' which will
1398 * cause a unique index to be added to the passed device name.
1399 * @dma_device: pointer to a DMA-capable device. If %NULL, then the IB
1400 * device will be used. In this case the caller should fully
1401 * setup the ibdev for DMA. This usually means using dma_virt_ops.
1402 *
1403 * Low-level drivers use ib_register_device() to register their
1404 * devices with the IB core. All registered clients will receive a
1405 * callback for each device that is added. @device must be allocated
1406 * with ib_alloc_device().
1407 *
1408 * If the driver uses ops.dealloc_driver and calls any ib_unregister_device()
1409 * asynchronously then the device pointer may become freed as soon as this
1410 * function returns.
1411 */
ib_register_device(struct ib_device * device,const char * name,struct device * dma_device)1412 int ib_register_device(struct ib_device *device, const char *name,
1413 struct device *dma_device)
1414 {
1415 int ret;
1416
1417 ret = assign_name(device, name);
1418 if (ret)
1419 return ret;
1420
1421 /*
1422 * If the caller does not provide a DMA capable device then the IB core
1423 * will set up ib_sge and scatterlist structures that stash the kernel
1424 * virtual address into the address field.
1425 */
1426 WARN_ON(dma_device && !dma_device->dma_parms);
1427 device->dma_device = dma_device;
1428
1429 ret = setup_device(device);
1430 if (ret)
1431 return ret;
1432
1433 ret = ib_cache_setup_one(device);
1434 if (ret) {
1435 dev_warn(&device->dev,
1436 "Couldn't set up InfiniBand P_Key/GID cache\n");
1437 return ret;
1438 }
1439
1440 device->groups[0] = &ib_dev_attr_group;
1441 device->groups[1] = device->ops.device_group;
1442 ret = ib_setup_device_attrs(device);
1443 if (ret)
1444 goto cache_cleanup;
1445
1446 ib_device_register_rdmacg(device);
1447
1448 rdma_counter_init(device);
1449
1450 /*
1451 * Ensure that ADD uevent is not fired because it
1452 * is too early amd device is not initialized yet.
1453 */
1454 dev_set_uevent_suppress(&device->dev, true);
1455 ret = device_add(&device->dev);
1456 if (ret)
1457 goto cg_cleanup;
1458
1459 ret = ib_setup_port_attrs(&device->coredev);
1460 if (ret) {
1461 dev_warn(&device->dev,
1462 "Couldn't register device with driver model\n");
1463 goto dev_cleanup;
1464 }
1465
1466 ret = enable_device_and_get(device);
1467 if (ret) {
1468 void (*dealloc_fn)(struct ib_device *);
1469
1470 /*
1471 * If we hit this error flow then we don't want to
1472 * automatically dealloc the device since the caller is
1473 * expected to call ib_dealloc_device() after
1474 * ib_register_device() fails. This is tricky due to the
1475 * possibility for a parallel unregistration along with this
1476 * error flow. Since we have a refcount here we know any
1477 * parallel flow is stopped in disable_device and will see the
1478 * special dealloc_driver pointer, causing the responsibility to
1479 * ib_dealloc_device() to revert back to this thread.
1480 */
1481 dealloc_fn = device->ops.dealloc_driver;
1482 device->ops.dealloc_driver = prevent_dealloc_device;
1483 ib_device_put(device);
1484 __ib_unregister_device(device);
1485 device->ops.dealloc_driver = dealloc_fn;
1486 dev_set_uevent_suppress(&device->dev, false);
1487 return ret;
1488 }
1489 dev_set_uevent_suppress(&device->dev, false);
1490
1491 ib_device_notify_register(device);
1492
1493 ib_device_put(device);
1494
1495 return 0;
1496
1497 dev_cleanup:
1498 device_del(&device->dev);
1499 cg_cleanup:
1500 dev_set_uevent_suppress(&device->dev, false);
1501 ib_device_unregister_rdmacg(device);
1502 cache_cleanup:
1503 ib_cache_cleanup_one(device);
1504 return ret;
1505 }
1506 EXPORT_SYMBOL(ib_register_device);
1507
1508 /* Callers must hold a get on the device. */
__ib_unregister_device(struct ib_device * ib_dev)1509 static void __ib_unregister_device(struct ib_device *ib_dev)
1510 {
1511 struct ib_device *sub, *tmp;
1512
1513 mutex_lock(&ib_dev->subdev_lock);
1514 list_for_each_entry_safe_reverse(sub, tmp,
1515 &ib_dev->subdev_list_head,
1516 subdev_list) {
1517 list_del(&sub->subdev_list);
1518 ib_dev->ops.del_sub_dev(sub);
1519 ib_device_put(ib_dev);
1520 }
1521 mutex_unlock(&ib_dev->subdev_lock);
1522
1523 /*
1524 * We have a registration lock so that all the calls to unregister are
1525 * fully fenced, once any unregister returns the device is truely
1526 * unregistered even if multiple callers are unregistering it at the
1527 * same time. This also interacts with the registration flow and
1528 * provides sane semantics if register and unregister are racing.
1529 */
1530 mutex_lock(&ib_dev->unregistration_lock);
1531 if (!refcount_read(&ib_dev->refcount))
1532 goto out;
1533
1534 disable_device(ib_dev);
1535 rdma_nl_notify_event(ib_dev, 0, RDMA_UNREGISTER_EVENT);
1536
1537 /* Expedite removing unregistered pointers from the hash table */
1538 free_netdevs(ib_dev);
1539
1540 ib_free_port_attrs(&ib_dev->coredev);
1541 device_del(&ib_dev->dev);
1542 ib_device_unregister_rdmacg(ib_dev);
1543 ib_cache_cleanup_one(ib_dev);
1544
1545 /*
1546 * Drivers using the new flow may not call ib_dealloc_device except
1547 * in error unwind prior to registration success.
1548 */
1549 if (ib_dev->ops.dealloc_driver &&
1550 ib_dev->ops.dealloc_driver != prevent_dealloc_device) {
1551 WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
1552 ib_dealloc_device(ib_dev);
1553 }
1554 out:
1555 mutex_unlock(&ib_dev->unregistration_lock);
1556 }
1557
1558 /**
1559 * ib_unregister_device - Unregister an IB device
1560 * @ib_dev: The device to unregister
1561 *
1562 * Unregister an IB device. All clients will receive a remove callback.
1563 *
1564 * Callers should call this routine only once, and protect against races with
1565 * registration. Typically it should only be called as part of a remove
1566 * callback in an implementation of driver core's struct device_driver and
1567 * related.
1568 *
1569 * If ops.dealloc_driver is used then ib_dev will be freed upon return from
1570 * this function.
1571 */
ib_unregister_device(struct ib_device * ib_dev)1572 void ib_unregister_device(struct ib_device *ib_dev)
1573 {
1574 get_device(&ib_dev->dev);
1575 __ib_unregister_device(ib_dev);
1576 put_device(&ib_dev->dev);
1577 }
1578 EXPORT_SYMBOL(ib_unregister_device);
1579
1580 /**
1581 * ib_unregister_device_and_put - Unregister a device while holding a 'get'
1582 * @ib_dev: The device to unregister
1583 *
1584 * This is the same as ib_unregister_device(), except it includes an internal
1585 * ib_device_put() that should match a 'get' obtained by the caller.
1586 *
1587 * It is safe to call this routine concurrently from multiple threads while
1588 * holding the 'get'. When the function returns the device is fully
1589 * unregistered.
1590 *
1591 * Drivers using this flow MUST use the driver_unregister callback to clean up
1592 * their resources associated with the device and dealloc it.
1593 */
ib_unregister_device_and_put(struct ib_device * ib_dev)1594 void ib_unregister_device_and_put(struct ib_device *ib_dev)
1595 {
1596 WARN_ON(!ib_dev->ops.dealloc_driver);
1597 get_device(&ib_dev->dev);
1598 ib_device_put(ib_dev);
1599 __ib_unregister_device(ib_dev);
1600 put_device(&ib_dev->dev);
1601 }
1602 EXPORT_SYMBOL(ib_unregister_device_and_put);
1603
1604 /**
1605 * ib_unregister_driver - Unregister all IB devices for a driver
1606 * @driver_id: The driver to unregister
1607 *
1608 * This implements a fence for device unregistration. It only returns once all
1609 * devices associated with the driver_id have fully completed their
1610 * unregistration and returned from ib_unregister_device*().
1611 *
1612 * If device's are not yet unregistered it goes ahead and starts unregistering
1613 * them.
1614 *
1615 * This does not block creation of new devices with the given driver_id, that
1616 * is the responsibility of the caller.
1617 */
ib_unregister_driver(enum rdma_driver_id driver_id)1618 void ib_unregister_driver(enum rdma_driver_id driver_id)
1619 {
1620 struct ib_device *ib_dev;
1621 unsigned long index;
1622
1623 down_read(&devices_rwsem);
1624 xa_for_each (&devices, index, ib_dev) {
1625 if (ib_dev->ops.driver_id != driver_id)
1626 continue;
1627
1628 get_device(&ib_dev->dev);
1629 up_read(&devices_rwsem);
1630
1631 WARN_ON(!ib_dev->ops.dealloc_driver);
1632 __ib_unregister_device(ib_dev);
1633
1634 put_device(&ib_dev->dev);
1635 down_read(&devices_rwsem);
1636 }
1637 up_read(&devices_rwsem);
1638 }
1639 EXPORT_SYMBOL(ib_unregister_driver);
1640
ib_unregister_work(struct work_struct * work)1641 static void ib_unregister_work(struct work_struct *work)
1642 {
1643 struct ib_device *ib_dev =
1644 container_of(work, struct ib_device, unregistration_work);
1645
1646 __ib_unregister_device(ib_dev);
1647 put_device(&ib_dev->dev);
1648 }
1649
1650 /**
1651 * ib_unregister_device_queued - Unregister a device using a work queue
1652 * @ib_dev: The device to unregister
1653 *
1654 * This schedules an asynchronous unregistration using a WQ for the device. A
1655 * driver should use this to avoid holding locks while doing unregistration,
1656 * such as holding the RTNL lock.
1657 *
1658 * Drivers using this API must use ib_unregister_driver before module unload
1659 * to ensure that all scheduled unregistrations have completed.
1660 */
ib_unregister_device_queued(struct ib_device * ib_dev)1661 void ib_unregister_device_queued(struct ib_device *ib_dev)
1662 {
1663 WARN_ON(!refcount_read(&ib_dev->refcount));
1664 WARN_ON(!ib_dev->ops.dealloc_driver);
1665 get_device(&ib_dev->dev);
1666 if (!queue_work(ib_unreg_wq, &ib_dev->unregistration_work))
1667 put_device(&ib_dev->dev);
1668 }
1669 EXPORT_SYMBOL(ib_unregister_device_queued);
1670
1671 /*
1672 * The caller must pass in a device that has the kref held and the refcount
1673 * released. If the device is in cur_net and still registered then it is moved
1674 * into net.
1675 */
rdma_dev_change_netns(struct ib_device * device,struct net * cur_net,struct net * net)1676 static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
1677 struct net *net)
1678 {
1679 int ret2 = -EINVAL;
1680 int ret;
1681
1682 mutex_lock(&device->unregistration_lock);
1683
1684 /*
1685 * If a device not under ib_device_get() or if the unregistration_lock
1686 * is not held, the namespace can be changed, or it can be unregistered.
1687 * Check again under the lock.
1688 */
1689 if (refcount_read(&device->refcount) == 0 ||
1690 !net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) {
1691 ret = -ENODEV;
1692 goto out;
1693 }
1694
1695 kobject_uevent(&device->dev.kobj, KOBJ_REMOVE);
1696 disable_device(device);
1697
1698 /*
1699 * At this point no one can be using the device, so it is safe to
1700 * change the namespace.
1701 */
1702 write_pnet(&device->coredev.rdma_net, net);
1703
1704 down_read(&devices_rwsem);
1705 /*
1706 * Currently rdma devices are system wide unique. So the device name
1707 * is guaranteed free in the new namespace. Publish the new namespace
1708 * at the sysfs level.
1709 */
1710 ret = device_rename(&device->dev, dev_name(&device->dev));
1711 up_read(&devices_rwsem);
1712 if (ret) {
1713 dev_warn(&device->dev,
1714 "%s: Couldn't rename device after namespace change\n",
1715 __func__);
1716 /* Try and put things back and re-enable the device */
1717 write_pnet(&device->coredev.rdma_net, cur_net);
1718 }
1719
1720 ret2 = enable_device_and_get(device);
1721 if (ret2) {
1722 /*
1723 * This shouldn't really happen, but if it does, let the user
1724 * retry at later point. So don't disable the device.
1725 */
1726 dev_warn(&device->dev,
1727 "%s: Couldn't re-enable device after namespace change\n",
1728 __func__);
1729 }
1730 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1731
1732 ib_device_put(device);
1733 out:
1734 mutex_unlock(&device->unregistration_lock);
1735 if (ret)
1736 return ret;
1737 return ret2;
1738 }
1739
ib_device_set_netns_put(struct sk_buff * skb,struct ib_device * dev,u32 ns_fd)1740 int ib_device_set_netns_put(struct sk_buff *skb,
1741 struct ib_device *dev, u32 ns_fd)
1742 {
1743 struct net *net;
1744 int ret;
1745
1746 net = get_net_ns_by_fd(ns_fd);
1747 if (IS_ERR(net)) {
1748 ret = PTR_ERR(net);
1749 goto net_err;
1750 }
1751
1752 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1753 ret = -EPERM;
1754 goto ns_err;
1755 }
1756
1757 /*
1758 * All the ib_clients, including uverbs, are reset when the namespace is
1759 * changed and this cannot be blocked waiting for userspace to do
1760 * something, so disassociation is mandatory.
1761 */
1762 if (!dev->ops.disassociate_ucontext || ib_devices_shared_netns) {
1763 ret = -EOPNOTSUPP;
1764 goto ns_err;
1765 }
1766
1767 get_device(&dev->dev);
1768 ib_device_put(dev);
1769 ret = rdma_dev_change_netns(dev, current->nsproxy->net_ns, net);
1770 put_device(&dev->dev);
1771
1772 put_net(net);
1773 return ret;
1774
1775 ns_err:
1776 put_net(net);
1777 net_err:
1778 ib_device_put(dev);
1779 return ret;
1780 }
1781
1782 static struct pernet_operations rdma_dev_net_ops = {
1783 .init = rdma_dev_init_net,
1784 .exit = rdma_dev_exit_net,
1785 .id = &rdma_dev_net_id,
1786 .size = sizeof(struct rdma_dev_net),
1787 };
1788
assign_client_id(struct ib_client * client)1789 static int assign_client_id(struct ib_client *client)
1790 {
1791 int ret;
1792
1793 lockdep_assert_held(&clients_rwsem);
1794 /*
1795 * The add/remove callbacks must be called in FIFO/LIFO order. To
1796 * achieve this we assign client_ids so they are sorted in
1797 * registration order.
1798 */
1799 client->client_id = highest_client_id;
1800 ret = xa_insert(&clients, client->client_id, client, GFP_KERNEL);
1801 if (ret)
1802 return ret;
1803
1804 highest_client_id++;
1805 xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED);
1806 return 0;
1807 }
1808
remove_client_id(struct ib_client * client)1809 static void remove_client_id(struct ib_client *client)
1810 {
1811 down_write(&clients_rwsem);
1812 xa_erase(&clients, client->client_id);
1813 for (; highest_client_id; highest_client_id--)
1814 if (xa_load(&clients, highest_client_id - 1))
1815 break;
1816 up_write(&clients_rwsem);
1817 }
1818
1819 /**
1820 * ib_register_client - Register an IB client
1821 * @client:Client to register
1822 *
1823 * Upper level users of the IB drivers can use ib_register_client() to
1824 * register callbacks for IB device addition and removal. When an IB
1825 * device is added, each registered client's add method will be called
1826 * (in the order the clients were registered), and when a device is
1827 * removed, each client's remove method will be called (in the reverse
1828 * order that clients were registered). In addition, when
1829 * ib_register_client() is called, the client will receive an add
1830 * callback for all devices already registered.
1831 */
ib_register_client(struct ib_client * client)1832 int ib_register_client(struct ib_client *client)
1833 {
1834 struct ib_device *device;
1835 unsigned long index;
1836 bool need_unreg = false;
1837 int ret;
1838
1839 refcount_set(&client->uses, 1);
1840 init_completion(&client->uses_zero);
1841
1842 /*
1843 * The devices_rwsem is held in write mode to ensure that a racing
1844 * ib_register_device() sees a consisent view of clients and devices.
1845 */
1846 down_write(&devices_rwsem);
1847 down_write(&clients_rwsem);
1848 ret = assign_client_id(client);
1849 if (ret)
1850 goto out;
1851
1852 need_unreg = true;
1853 xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) {
1854 ret = add_client_context(device, client);
1855 if (ret)
1856 goto out;
1857 }
1858 ret = 0;
1859 out:
1860 up_write(&clients_rwsem);
1861 up_write(&devices_rwsem);
1862 if (need_unreg && ret)
1863 ib_unregister_client(client);
1864 return ret;
1865 }
1866 EXPORT_SYMBOL(ib_register_client);
1867
1868 /**
1869 * ib_unregister_client - Unregister an IB client
1870 * @client:Client to unregister
1871 *
1872 * Upper level users use ib_unregister_client() to remove their client
1873 * registration. When ib_unregister_client() is called, the client
1874 * will receive a remove callback for each IB device still registered.
1875 *
1876 * This is a full fence, once it returns no client callbacks will be called,
1877 * or are running in another thread.
1878 */
ib_unregister_client(struct ib_client * client)1879 void ib_unregister_client(struct ib_client *client)
1880 {
1881 struct ib_device *device;
1882 unsigned long index;
1883
1884 down_write(&clients_rwsem);
1885 ib_client_put(client);
1886 xa_clear_mark(&clients, client->client_id, CLIENT_REGISTERED);
1887 up_write(&clients_rwsem);
1888
1889 /* We do not want to have locks while calling client->remove() */
1890 rcu_read_lock();
1891 xa_for_each (&devices, index, device) {
1892 if (!ib_device_try_get(device))
1893 continue;
1894 rcu_read_unlock();
1895
1896 remove_client_context(device, client->client_id);
1897
1898 ib_device_put(device);
1899 rcu_read_lock();
1900 }
1901 rcu_read_unlock();
1902
1903 /*
1904 * remove_client_context() is not a fence, it can return even though a
1905 * removal is ongoing. Wait until all removals are completed.
1906 */
1907 wait_for_completion(&client->uses_zero);
1908 remove_client_id(client);
1909 }
1910 EXPORT_SYMBOL(ib_unregister_client);
1911
__ib_get_global_client_nl_info(const char * client_name,struct ib_client_nl_info * res)1912 static int __ib_get_global_client_nl_info(const char *client_name,
1913 struct ib_client_nl_info *res)
1914 {
1915 struct ib_client *client;
1916 unsigned long index;
1917 int ret = -ENOENT;
1918
1919 down_read(&clients_rwsem);
1920 xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1921 if (strcmp(client->name, client_name) != 0)
1922 continue;
1923 if (!client->get_global_nl_info) {
1924 ret = -EOPNOTSUPP;
1925 break;
1926 }
1927 ret = client->get_global_nl_info(res);
1928 if (WARN_ON(ret == -ENOENT))
1929 ret = -EINVAL;
1930 if (!ret && res->cdev)
1931 get_device(res->cdev);
1932 break;
1933 }
1934 up_read(&clients_rwsem);
1935 return ret;
1936 }
1937
__ib_get_client_nl_info(struct ib_device * ibdev,const char * client_name,struct ib_client_nl_info * res)1938 static int __ib_get_client_nl_info(struct ib_device *ibdev,
1939 const char *client_name,
1940 struct ib_client_nl_info *res)
1941 {
1942 unsigned long index;
1943 void *client_data;
1944 int ret = -ENOENT;
1945
1946 down_read(&ibdev->client_data_rwsem);
1947 xan_for_each_marked (&ibdev->client_data, index, client_data,
1948 CLIENT_DATA_REGISTERED) {
1949 struct ib_client *client = xa_load(&clients, index);
1950
1951 if (!client || strcmp(client->name, client_name) != 0)
1952 continue;
1953 if (!client->get_nl_info) {
1954 ret = -EOPNOTSUPP;
1955 break;
1956 }
1957 ret = client->get_nl_info(ibdev, client_data, res);
1958 if (WARN_ON(ret == -ENOENT))
1959 ret = -EINVAL;
1960
1961 /*
1962 * The cdev is guaranteed valid as long as we are inside the
1963 * client_data_rwsem as remove_one can't be called. Keep it
1964 * valid for the caller.
1965 */
1966 if (!ret && res->cdev)
1967 get_device(res->cdev);
1968 break;
1969 }
1970 up_read(&ibdev->client_data_rwsem);
1971
1972 return ret;
1973 }
1974
1975 /**
1976 * ib_get_client_nl_info - Fetch the nl_info from a client
1977 * @ibdev: IB device
1978 * @client_name: Name of the client
1979 * @res: Result of the query
1980 */
ib_get_client_nl_info(struct ib_device * ibdev,const char * client_name,struct ib_client_nl_info * res)1981 int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
1982 struct ib_client_nl_info *res)
1983 {
1984 int ret;
1985
1986 if (ibdev)
1987 ret = __ib_get_client_nl_info(ibdev, client_name, res);
1988 else
1989 ret = __ib_get_global_client_nl_info(client_name, res);
1990 #ifdef CONFIG_MODULES
1991 if (ret == -ENOENT) {
1992 request_module("rdma-client-%s", client_name);
1993 if (ibdev)
1994 ret = __ib_get_client_nl_info(ibdev, client_name, res);
1995 else
1996 ret = __ib_get_global_client_nl_info(client_name, res);
1997 }
1998 #endif
1999 if (ret) {
2000 if (ret == -ENOENT)
2001 return -EOPNOTSUPP;
2002 return ret;
2003 }
2004
2005 if (WARN_ON(!res->cdev))
2006 return -EINVAL;
2007 return 0;
2008 }
2009
2010 /**
2011 * ib_set_client_data - Set IB client context
2012 * @device:Device to set context for
2013 * @client:Client to set context for
2014 * @data:Context to set
2015 *
2016 * ib_set_client_data() sets client context data that can be retrieved with
2017 * ib_get_client_data(). This can only be called while the client is
2018 * registered to the device, once the ib_client remove() callback returns this
2019 * cannot be called.
2020 */
ib_set_client_data(struct ib_device * device,struct ib_client * client,void * data)2021 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
2022 void *data)
2023 {
2024 void *rc;
2025
2026 if (WARN_ON(IS_ERR(data)))
2027 data = NULL;
2028
2029 rc = xa_store(&device->client_data, client->client_id, data,
2030 GFP_KERNEL);
2031 WARN_ON(xa_is_err(rc));
2032 }
2033 EXPORT_SYMBOL(ib_set_client_data);
2034
2035 /**
2036 * ib_register_event_handler - Register an IB event handler
2037 * @event_handler:Handler to register
2038 *
2039 * ib_register_event_handler() registers an event handler that will be
2040 * called back when asynchronous IB events occur (as defined in
2041 * chapter 11 of the InfiniBand Architecture Specification). This
2042 * callback occurs in workqueue context.
2043 */
ib_register_event_handler(struct ib_event_handler * event_handler)2044 void ib_register_event_handler(struct ib_event_handler *event_handler)
2045 {
2046 down_write(&event_handler->device->event_handler_rwsem);
2047 list_add_tail(&event_handler->list,
2048 &event_handler->device->event_handler_list);
2049 up_write(&event_handler->device->event_handler_rwsem);
2050 }
2051 EXPORT_SYMBOL(ib_register_event_handler);
2052
2053 /**
2054 * ib_unregister_event_handler - Unregister an event handler
2055 * @event_handler:Handler to unregister
2056 *
2057 * Unregister an event handler registered with
2058 * ib_register_event_handler().
2059 */
ib_unregister_event_handler(struct ib_event_handler * event_handler)2060 void ib_unregister_event_handler(struct ib_event_handler *event_handler)
2061 {
2062 down_write(&event_handler->device->event_handler_rwsem);
2063 list_del(&event_handler->list);
2064 up_write(&event_handler->device->event_handler_rwsem);
2065 }
2066 EXPORT_SYMBOL(ib_unregister_event_handler);
2067
ib_dispatch_event_clients(struct ib_event * event)2068 void ib_dispatch_event_clients(struct ib_event *event)
2069 {
2070 struct ib_event_handler *handler;
2071
2072 down_read(&event->device->event_handler_rwsem);
2073
2074 list_for_each_entry(handler, &event->device->event_handler_list, list)
2075 handler->handler(handler, event);
2076
2077 up_read(&event->device->event_handler_rwsem);
2078 }
2079
iw_query_port(struct ib_device * device,u32 port_num,struct ib_port_attr * port_attr)2080 static int iw_query_port(struct ib_device *device,
2081 u32 port_num,
2082 struct ib_port_attr *port_attr)
2083 {
2084 struct in_device *inetdev;
2085 struct net_device *netdev;
2086
2087 memset(port_attr, 0, sizeof(*port_attr));
2088
2089 netdev = ib_device_get_netdev(device, port_num);
2090 if (!netdev)
2091 return -ENODEV;
2092
2093 port_attr->max_mtu = IB_MTU_4096;
2094 port_attr->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
2095
2096 if (!netif_carrier_ok(netdev)) {
2097 port_attr->state = IB_PORT_DOWN;
2098 port_attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
2099 } else {
2100 rcu_read_lock();
2101 inetdev = __in_dev_get_rcu(netdev);
2102
2103 if (inetdev && inetdev->ifa_list) {
2104 port_attr->state = IB_PORT_ACTIVE;
2105 port_attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
2106 } else {
2107 port_attr->state = IB_PORT_INIT;
2108 port_attr->phys_state =
2109 IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING;
2110 }
2111
2112 rcu_read_unlock();
2113 }
2114
2115 dev_put(netdev);
2116 return device->ops.query_port(device, port_num, port_attr);
2117 }
2118
__ib_query_port(struct ib_device * device,u32 port_num,struct ib_port_attr * port_attr)2119 static int __ib_query_port(struct ib_device *device,
2120 u32 port_num,
2121 struct ib_port_attr *port_attr)
2122 {
2123 int err;
2124
2125 memset(port_attr, 0, sizeof(*port_attr));
2126
2127 err = device->ops.query_port(device, port_num, port_attr);
2128 if (err || port_attr->subnet_prefix)
2129 return err;
2130
2131 if (rdma_port_get_link_layer(device, port_num) !=
2132 IB_LINK_LAYER_INFINIBAND)
2133 return 0;
2134
2135 ib_get_cached_subnet_prefix(device, port_num,
2136 &port_attr->subnet_prefix);
2137 return 0;
2138 }
2139
2140 /**
2141 * ib_query_port - Query IB port attributes
2142 * @device:Device to query
2143 * @port_num:Port number to query
2144 * @port_attr:Port attributes
2145 *
2146 * ib_query_port() returns the attributes of a port through the
2147 * @port_attr pointer.
2148 */
ib_query_port(struct ib_device * device,u32 port_num,struct ib_port_attr * port_attr)2149 int ib_query_port(struct ib_device *device,
2150 u32 port_num,
2151 struct ib_port_attr *port_attr)
2152 {
2153 if (!rdma_is_port_valid(device, port_num))
2154 return -EINVAL;
2155
2156 if (rdma_protocol_iwarp(device, port_num))
2157 return iw_query_port(device, port_num, port_attr);
2158 else
2159 return __ib_query_port(device, port_num, port_attr);
2160 }
2161 EXPORT_SYMBOL(ib_query_port);
2162
add_ndev_hash(struct ib_port_data * pdata)2163 static void add_ndev_hash(struct ib_port_data *pdata)
2164 {
2165 unsigned long flags;
2166
2167 might_sleep();
2168
2169 spin_lock_irqsave(&ndev_hash_lock, flags);
2170 if (hash_hashed(&pdata->ndev_hash_link)) {
2171 hash_del_rcu(&pdata->ndev_hash_link);
2172 spin_unlock_irqrestore(&ndev_hash_lock, flags);
2173 /*
2174 * We cannot do hash_add_rcu after a hash_del_rcu until the
2175 * grace period
2176 */
2177 synchronize_rcu();
2178 spin_lock_irqsave(&ndev_hash_lock, flags);
2179 }
2180 if (pdata->netdev)
2181 hash_add_rcu(ndev_hash, &pdata->ndev_hash_link,
2182 (uintptr_t)pdata->netdev);
2183 spin_unlock_irqrestore(&ndev_hash_lock, flags);
2184 }
2185
2186 /**
2187 * ib_device_set_netdev - Associate the ib_dev with an underlying net_device
2188 * @ib_dev: Device to modify
2189 * @ndev: net_device to affiliate, may be NULL
2190 * @port: IB port the net_device is connected to
2191 *
2192 * Drivers should use this to link the ib_device to a netdev so the netdev
2193 * shows up in interfaces like ib_enum_roce_netdev. Only one netdev may be
2194 * affiliated with any port.
2195 *
2196 * The caller must ensure that the given ndev is not unregistered or
2197 * unregistering, and that either the ib_device is unregistered or
2198 * ib_device_set_netdev() is called with NULL when the ndev sends a
2199 * NETDEV_UNREGISTER event.
2200 */
ib_device_set_netdev(struct ib_device * ib_dev,struct net_device * ndev,u32 port)2201 int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
2202 u32 port)
2203 {
2204 enum rdma_nl_notify_event_type etype;
2205 struct net_device *old_ndev;
2206 struct ib_port_data *pdata;
2207 unsigned long flags;
2208 int ret;
2209
2210 if (!rdma_is_port_valid(ib_dev, port))
2211 return -EINVAL;
2212
2213 /*
2214 * Drivers wish to call this before ib_register_driver, so we have to
2215 * setup the port data early.
2216 */
2217 ret = alloc_port_data(ib_dev);
2218 if (ret)
2219 return ret;
2220
2221 pdata = &ib_dev->port_data[port];
2222 spin_lock_irqsave(&pdata->netdev_lock, flags);
2223 old_ndev = rcu_dereference_protected(
2224 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
2225 if (old_ndev == ndev) {
2226 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
2227 return 0;
2228 }
2229
2230 rcu_assign_pointer(pdata->netdev, ndev);
2231 netdev_put(old_ndev, &pdata->netdev_tracker);
2232 netdev_hold(ndev, &pdata->netdev_tracker, GFP_ATOMIC);
2233 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
2234
2235 add_ndev_hash(pdata);
2236
2237 /* Make sure that the device is registered before we send events */
2238 if (xa_load(&devices, ib_dev->index) != ib_dev)
2239 return 0;
2240
2241 etype = ndev ? RDMA_NETDEV_ATTACH_EVENT : RDMA_NETDEV_DETACH_EVENT;
2242 rdma_nl_notify_event(ib_dev, port, etype);
2243
2244 return 0;
2245 }
2246 EXPORT_SYMBOL(ib_device_set_netdev);
2247
free_netdevs(struct ib_device * ib_dev)2248 static void free_netdevs(struct ib_device *ib_dev)
2249 {
2250 unsigned long flags;
2251 u32 port;
2252
2253 if (!ib_dev->port_data)
2254 return;
2255
2256 rdma_for_each_port (ib_dev, port) {
2257 struct ib_port_data *pdata = &ib_dev->port_data[port];
2258 struct net_device *ndev;
2259
2260 spin_lock_irqsave(&pdata->netdev_lock, flags);
2261 ndev = rcu_dereference_protected(
2262 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
2263 if (ndev) {
2264 spin_lock(&ndev_hash_lock);
2265 hash_del_rcu(&pdata->ndev_hash_link);
2266 spin_unlock(&ndev_hash_lock);
2267
2268 /*
2269 * If this is the last dev_put there is still a
2270 * synchronize_rcu before the netdev is kfreed, so we
2271 * can continue to rely on unlocked pointer
2272 * comparisons after the put
2273 */
2274 rcu_assign_pointer(pdata->netdev, NULL);
2275 netdev_put(ndev, &pdata->netdev_tracker);
2276 }
2277 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
2278 }
2279 }
2280
ib_device_get_netdev(struct ib_device * ib_dev,u32 port)2281 struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
2282 u32 port)
2283 {
2284 struct ib_port_data *pdata;
2285 struct net_device *res;
2286
2287 if (!rdma_is_port_valid(ib_dev, port))
2288 return NULL;
2289
2290 if (!ib_dev->port_data)
2291 return NULL;
2292
2293 pdata = &ib_dev->port_data[port];
2294
2295 /*
2296 * New drivers should use ib_device_set_netdev() not the legacy
2297 * get_netdev().
2298 */
2299 if (ib_dev->ops.get_netdev)
2300 res = ib_dev->ops.get_netdev(ib_dev, port);
2301 else {
2302 spin_lock(&pdata->netdev_lock);
2303 res = rcu_dereference_protected(
2304 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
2305 dev_hold(res);
2306 spin_unlock(&pdata->netdev_lock);
2307 }
2308
2309 return res;
2310 }
2311 EXPORT_SYMBOL(ib_device_get_netdev);
2312
2313 /**
2314 * ib_device_get_by_netdev - Find an IB device associated with a netdev
2315 * @ndev: netdev to locate
2316 * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all)
2317 *
2318 * Find and hold an ib_device that is associated with a netdev via
2319 * ib_device_set_netdev(). The caller must call ib_device_put() on the
2320 * returned pointer.
2321 */
ib_device_get_by_netdev(struct net_device * ndev,enum rdma_driver_id driver_id)2322 struct ib_device *ib_device_get_by_netdev(struct net_device *ndev,
2323 enum rdma_driver_id driver_id)
2324 {
2325 struct ib_device *res = NULL;
2326 struct ib_port_data *cur;
2327
2328 rcu_read_lock();
2329 hash_for_each_possible_rcu (ndev_hash, cur, ndev_hash_link,
2330 (uintptr_t)ndev) {
2331 if (rcu_access_pointer(cur->netdev) == ndev &&
2332 (driver_id == RDMA_DRIVER_UNKNOWN ||
2333 cur->ib_dev->ops.driver_id == driver_id) &&
2334 ib_device_try_get(cur->ib_dev)) {
2335 res = cur->ib_dev;
2336 break;
2337 }
2338 }
2339 rcu_read_unlock();
2340
2341 return res;
2342 }
2343 EXPORT_SYMBOL(ib_device_get_by_netdev);
2344
2345 /**
2346 * ib_enum_roce_netdev - enumerate all RoCE ports
2347 * @ib_dev : IB device we want to query
2348 * @filter: Should we call the callback?
2349 * @filter_cookie: Cookie passed to filter
2350 * @cb: Callback to call for each found RoCE ports
2351 * @cookie: Cookie passed back to the callback
2352 *
2353 * Enumerates all of the physical RoCE ports of ib_dev
2354 * which are related to netdevice and calls callback() on each
2355 * device for which filter() function returns non zero.
2356 */
ib_enum_roce_netdev(struct ib_device * ib_dev,roce_netdev_filter filter,void * filter_cookie,roce_netdev_callback cb,void * cookie)2357 void ib_enum_roce_netdev(struct ib_device *ib_dev,
2358 roce_netdev_filter filter,
2359 void *filter_cookie,
2360 roce_netdev_callback cb,
2361 void *cookie)
2362 {
2363 u32 port;
2364
2365 rdma_for_each_port (ib_dev, port)
2366 if (rdma_protocol_roce(ib_dev, port)) {
2367 struct net_device *idev =
2368 ib_device_get_netdev(ib_dev, port);
2369
2370 if (filter(ib_dev, port, idev, filter_cookie))
2371 cb(ib_dev, port, idev, cookie);
2372 dev_put(idev);
2373 }
2374 }
2375
2376 /**
2377 * ib_enum_all_roce_netdevs - enumerate all RoCE devices
2378 * @filter: Should we call the callback?
2379 * @filter_cookie: Cookie passed to filter
2380 * @cb: Callback to call for each found RoCE ports
2381 * @cookie: Cookie passed back to the callback
2382 *
2383 * Enumerates all RoCE devices' physical ports which are related
2384 * to netdevices and calls callback() on each device for which
2385 * filter() function returns non zero.
2386 */
ib_enum_all_roce_netdevs(roce_netdev_filter filter,void * filter_cookie,roce_netdev_callback cb,void * cookie)2387 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
2388 void *filter_cookie,
2389 roce_netdev_callback cb,
2390 void *cookie)
2391 {
2392 struct ib_device *dev;
2393 unsigned long index;
2394
2395 down_read(&devices_rwsem);
2396 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED)
2397 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
2398 up_read(&devices_rwsem);
2399 }
2400
2401 /*
2402 * ib_enum_all_devs - enumerate all ib_devices
2403 * @cb: Callback to call for each found ib_device
2404 *
2405 * Enumerates all ib_devices and calls callback() on each device.
2406 */
ib_enum_all_devs(nldev_callback nldev_cb,struct sk_buff * skb,struct netlink_callback * cb)2407 int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
2408 struct netlink_callback *cb)
2409 {
2410 unsigned long index;
2411 struct ib_device *dev;
2412 unsigned int idx = 0;
2413 int ret = 0;
2414
2415 down_read(&devices_rwsem);
2416 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
2417 if (!rdma_dev_access_netns(dev, sock_net(skb->sk)))
2418 continue;
2419
2420 ret = nldev_cb(dev, skb, cb, idx);
2421 if (ret)
2422 break;
2423 idx++;
2424 }
2425 up_read(&devices_rwsem);
2426 return ret;
2427 }
2428
2429 /**
2430 * ib_query_pkey - Get P_Key table entry
2431 * @device:Device to query
2432 * @port_num:Port number to query
2433 * @index:P_Key table index to query
2434 * @pkey:Returned P_Key
2435 *
2436 * ib_query_pkey() fetches the specified P_Key table entry.
2437 */
ib_query_pkey(struct ib_device * device,u32 port_num,u16 index,u16 * pkey)2438 int ib_query_pkey(struct ib_device *device,
2439 u32 port_num, u16 index, u16 *pkey)
2440 {
2441 if (!rdma_is_port_valid(device, port_num))
2442 return -EINVAL;
2443
2444 if (!device->ops.query_pkey)
2445 return -EOPNOTSUPP;
2446
2447 return device->ops.query_pkey(device, port_num, index, pkey);
2448 }
2449 EXPORT_SYMBOL(ib_query_pkey);
2450
2451 /**
2452 * ib_modify_device - Change IB device attributes
2453 * @device:Device to modify
2454 * @device_modify_mask:Mask of attributes to change
2455 * @device_modify:New attribute values
2456 *
2457 * ib_modify_device() changes a device's attributes as specified by
2458 * the @device_modify_mask and @device_modify structure.
2459 */
ib_modify_device(struct ib_device * device,int device_modify_mask,struct ib_device_modify * device_modify)2460 int ib_modify_device(struct ib_device *device,
2461 int device_modify_mask,
2462 struct ib_device_modify *device_modify)
2463 {
2464 if (!device->ops.modify_device)
2465 return -EOPNOTSUPP;
2466
2467 return device->ops.modify_device(device, device_modify_mask,
2468 device_modify);
2469 }
2470 EXPORT_SYMBOL(ib_modify_device);
2471
2472 /**
2473 * ib_modify_port - Modifies the attributes for the specified port.
2474 * @device: The device to modify.
2475 * @port_num: The number of the port to modify.
2476 * @port_modify_mask: Mask used to specify which attributes of the port
2477 * to change.
2478 * @port_modify: New attribute values for the port.
2479 *
2480 * ib_modify_port() changes a port's attributes as specified by the
2481 * @port_modify_mask and @port_modify structure.
2482 */
ib_modify_port(struct ib_device * device,u32 port_num,int port_modify_mask,struct ib_port_modify * port_modify)2483 int ib_modify_port(struct ib_device *device,
2484 u32 port_num, int port_modify_mask,
2485 struct ib_port_modify *port_modify)
2486 {
2487 int rc;
2488
2489 if (!rdma_is_port_valid(device, port_num))
2490 return -EINVAL;
2491
2492 if (device->ops.modify_port)
2493 rc = device->ops.modify_port(device, port_num,
2494 port_modify_mask,
2495 port_modify);
2496 else if (rdma_protocol_roce(device, port_num) &&
2497 ((port_modify->set_port_cap_mask & ~IB_PORT_CM_SUP) == 0 ||
2498 (port_modify->clr_port_cap_mask & ~IB_PORT_CM_SUP) == 0))
2499 rc = 0;
2500 else
2501 rc = -EOPNOTSUPP;
2502 return rc;
2503 }
2504 EXPORT_SYMBOL(ib_modify_port);
2505
2506 /**
2507 * ib_find_gid - Returns the port number and GID table index where
2508 * a specified GID value occurs. Its searches only for IB link layer.
2509 * @device: The device to query.
2510 * @gid: The GID value to search for.
2511 * @port_num: The port number of the device where the GID value was found.
2512 * @index: The index into the GID table where the GID was found. This
2513 * parameter may be NULL.
2514 */
ib_find_gid(struct ib_device * device,union ib_gid * gid,u32 * port_num,u16 * index)2515 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
2516 u32 *port_num, u16 *index)
2517 {
2518 union ib_gid tmp_gid;
2519 u32 port;
2520 int ret, i;
2521
2522 rdma_for_each_port (device, port) {
2523 if (!rdma_protocol_ib(device, port))
2524 continue;
2525
2526 for (i = 0; i < device->port_data[port].immutable.gid_tbl_len;
2527 ++i) {
2528 ret = rdma_query_gid(device, port, i, &tmp_gid);
2529 if (ret)
2530 continue;
2531
2532 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
2533 *port_num = port;
2534 if (index)
2535 *index = i;
2536 return 0;
2537 }
2538 }
2539 }
2540
2541 return -ENOENT;
2542 }
2543 EXPORT_SYMBOL(ib_find_gid);
2544
2545 /**
2546 * ib_find_pkey - Returns the PKey table index where a specified
2547 * PKey value occurs.
2548 * @device: The device to query.
2549 * @port_num: The port number of the device to search for the PKey.
2550 * @pkey: The PKey value to search for.
2551 * @index: The index into the PKey table where the PKey was found.
2552 */
ib_find_pkey(struct ib_device * device,u32 port_num,u16 pkey,u16 * index)2553 int ib_find_pkey(struct ib_device *device,
2554 u32 port_num, u16 pkey, u16 *index)
2555 {
2556 int ret, i;
2557 u16 tmp_pkey;
2558 int partial_ix = -1;
2559
2560 for (i = 0; i < device->port_data[port_num].immutable.pkey_tbl_len;
2561 ++i) {
2562 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
2563 if (ret)
2564 return ret;
2565 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
2566 /* if there is full-member pkey take it.*/
2567 if (tmp_pkey & 0x8000) {
2568 *index = i;
2569 return 0;
2570 }
2571 if (partial_ix < 0)
2572 partial_ix = i;
2573 }
2574 }
2575
2576 /*no full-member, if exists take the limited*/
2577 if (partial_ix >= 0) {
2578 *index = partial_ix;
2579 return 0;
2580 }
2581 return -ENOENT;
2582 }
2583 EXPORT_SYMBOL(ib_find_pkey);
2584
2585 /**
2586 * ib_get_net_dev_by_params() - Return the appropriate net_dev
2587 * for a received CM request
2588 * @dev: An RDMA device on which the request has been received.
2589 * @port: Port number on the RDMA device.
2590 * @pkey: The Pkey the request came on.
2591 * @gid: A GID that the net_dev uses to communicate.
2592 * @addr: Contains the IP address that the request specified as its
2593 * destination.
2594 *
2595 */
ib_get_net_dev_by_params(struct ib_device * dev,u32 port,u16 pkey,const union ib_gid * gid,const struct sockaddr * addr)2596 struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
2597 u32 port,
2598 u16 pkey,
2599 const union ib_gid *gid,
2600 const struct sockaddr *addr)
2601 {
2602 struct net_device *net_dev = NULL;
2603 unsigned long index;
2604 void *client_data;
2605
2606 if (!rdma_protocol_ib(dev, port))
2607 return NULL;
2608
2609 /*
2610 * Holding the read side guarantees that the client will not become
2611 * unregistered while we are calling get_net_dev_by_params()
2612 */
2613 down_read(&dev->client_data_rwsem);
2614 xan_for_each_marked (&dev->client_data, index, client_data,
2615 CLIENT_DATA_REGISTERED) {
2616 struct ib_client *client = xa_load(&clients, index);
2617
2618 if (!client || !client->get_net_dev_by_params)
2619 continue;
2620
2621 net_dev = client->get_net_dev_by_params(dev, port, pkey, gid,
2622 addr, client_data);
2623 if (net_dev)
2624 break;
2625 }
2626 up_read(&dev->client_data_rwsem);
2627
2628 return net_dev;
2629 }
2630 EXPORT_SYMBOL(ib_get_net_dev_by_params);
2631
ib_set_device_ops(struct ib_device * dev,const struct ib_device_ops * ops)2632 void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
2633 {
2634 struct ib_device_ops *dev_ops = &dev->ops;
2635 #define SET_DEVICE_OP(ptr, name) \
2636 do { \
2637 if (ops->name) \
2638 if (!((ptr)->name)) \
2639 (ptr)->name = ops->name; \
2640 } while (0)
2641
2642 #define SET_OBJ_SIZE(ptr, name) SET_DEVICE_OP(ptr, size_##name)
2643
2644 if (ops->driver_id != RDMA_DRIVER_UNKNOWN) {
2645 WARN_ON(dev_ops->driver_id != RDMA_DRIVER_UNKNOWN &&
2646 dev_ops->driver_id != ops->driver_id);
2647 dev_ops->driver_id = ops->driver_id;
2648 }
2649 if (ops->owner) {
2650 WARN_ON(dev_ops->owner && dev_ops->owner != ops->owner);
2651 dev_ops->owner = ops->owner;
2652 }
2653 if (ops->uverbs_abi_ver)
2654 dev_ops->uverbs_abi_ver = ops->uverbs_abi_ver;
2655
2656 dev_ops->uverbs_no_driver_id_binding |=
2657 ops->uverbs_no_driver_id_binding;
2658
2659 SET_DEVICE_OP(dev_ops, add_gid);
2660 SET_DEVICE_OP(dev_ops, add_sub_dev);
2661 SET_DEVICE_OP(dev_ops, advise_mr);
2662 SET_DEVICE_OP(dev_ops, alloc_dm);
2663 SET_DEVICE_OP(dev_ops, alloc_hw_device_stats);
2664 SET_DEVICE_OP(dev_ops, alloc_hw_port_stats);
2665 SET_DEVICE_OP(dev_ops, alloc_mr);
2666 SET_DEVICE_OP(dev_ops, alloc_mr_integrity);
2667 SET_DEVICE_OP(dev_ops, alloc_mw);
2668 SET_DEVICE_OP(dev_ops, alloc_pd);
2669 SET_DEVICE_OP(dev_ops, alloc_rdma_netdev);
2670 SET_DEVICE_OP(dev_ops, alloc_ucontext);
2671 SET_DEVICE_OP(dev_ops, alloc_xrcd);
2672 SET_DEVICE_OP(dev_ops, attach_mcast);
2673 SET_DEVICE_OP(dev_ops, check_mr_status);
2674 SET_DEVICE_OP(dev_ops, counter_alloc_stats);
2675 SET_DEVICE_OP(dev_ops, counter_bind_qp);
2676 SET_DEVICE_OP(dev_ops, counter_dealloc);
2677 SET_DEVICE_OP(dev_ops, counter_unbind_qp);
2678 SET_DEVICE_OP(dev_ops, counter_update_stats);
2679 SET_DEVICE_OP(dev_ops, create_ah);
2680 SET_DEVICE_OP(dev_ops, create_counters);
2681 SET_DEVICE_OP(dev_ops, create_cq);
2682 SET_DEVICE_OP(dev_ops, create_flow);
2683 SET_DEVICE_OP(dev_ops, create_qp);
2684 SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
2685 SET_DEVICE_OP(dev_ops, create_srq);
2686 SET_DEVICE_OP(dev_ops, create_user_ah);
2687 SET_DEVICE_OP(dev_ops, create_wq);
2688 SET_DEVICE_OP(dev_ops, dealloc_dm);
2689 SET_DEVICE_OP(dev_ops, dealloc_driver);
2690 SET_DEVICE_OP(dev_ops, dealloc_mw);
2691 SET_DEVICE_OP(dev_ops, dealloc_pd);
2692 SET_DEVICE_OP(dev_ops, dealloc_ucontext);
2693 SET_DEVICE_OP(dev_ops, dealloc_xrcd);
2694 SET_DEVICE_OP(dev_ops, del_gid);
2695 SET_DEVICE_OP(dev_ops, del_sub_dev);
2696 SET_DEVICE_OP(dev_ops, dereg_mr);
2697 SET_DEVICE_OP(dev_ops, destroy_ah);
2698 SET_DEVICE_OP(dev_ops, destroy_counters);
2699 SET_DEVICE_OP(dev_ops, destroy_cq);
2700 SET_DEVICE_OP(dev_ops, destroy_flow);
2701 SET_DEVICE_OP(dev_ops, destroy_flow_action);
2702 SET_DEVICE_OP(dev_ops, destroy_qp);
2703 SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table);
2704 SET_DEVICE_OP(dev_ops, destroy_srq);
2705 SET_DEVICE_OP(dev_ops, destroy_wq);
2706 SET_DEVICE_OP(dev_ops, device_group);
2707 SET_DEVICE_OP(dev_ops, detach_mcast);
2708 SET_DEVICE_OP(dev_ops, disassociate_ucontext);
2709 SET_DEVICE_OP(dev_ops, drain_rq);
2710 SET_DEVICE_OP(dev_ops, drain_sq);
2711 SET_DEVICE_OP(dev_ops, enable_driver);
2712 SET_DEVICE_OP(dev_ops, fill_res_cm_id_entry);
2713 SET_DEVICE_OP(dev_ops, fill_res_cq_entry);
2714 SET_DEVICE_OP(dev_ops, fill_res_cq_entry_raw);
2715 SET_DEVICE_OP(dev_ops, fill_res_mr_entry);
2716 SET_DEVICE_OP(dev_ops, fill_res_mr_entry_raw);
2717 SET_DEVICE_OP(dev_ops, fill_res_qp_entry);
2718 SET_DEVICE_OP(dev_ops, fill_res_qp_entry_raw);
2719 SET_DEVICE_OP(dev_ops, fill_res_srq_entry);
2720 SET_DEVICE_OP(dev_ops, fill_res_srq_entry_raw);
2721 SET_DEVICE_OP(dev_ops, fill_stat_mr_entry);
2722 SET_DEVICE_OP(dev_ops, get_dev_fw_str);
2723 SET_DEVICE_OP(dev_ops, get_dma_mr);
2724 SET_DEVICE_OP(dev_ops, get_hw_stats);
2725 SET_DEVICE_OP(dev_ops, get_link_layer);
2726 SET_DEVICE_OP(dev_ops, get_netdev);
2727 SET_DEVICE_OP(dev_ops, get_numa_node);
2728 SET_DEVICE_OP(dev_ops, get_port_immutable);
2729 SET_DEVICE_OP(dev_ops, get_vector_affinity);
2730 SET_DEVICE_OP(dev_ops, get_vf_config);
2731 SET_DEVICE_OP(dev_ops, get_vf_guid);
2732 SET_DEVICE_OP(dev_ops, get_vf_stats);
2733 SET_DEVICE_OP(dev_ops, iw_accept);
2734 SET_DEVICE_OP(dev_ops, iw_add_ref);
2735 SET_DEVICE_OP(dev_ops, iw_connect);
2736 SET_DEVICE_OP(dev_ops, iw_create_listen);
2737 SET_DEVICE_OP(dev_ops, iw_destroy_listen);
2738 SET_DEVICE_OP(dev_ops, iw_get_qp);
2739 SET_DEVICE_OP(dev_ops, iw_reject);
2740 SET_DEVICE_OP(dev_ops, iw_rem_ref);
2741 SET_DEVICE_OP(dev_ops, map_mr_sg);
2742 SET_DEVICE_OP(dev_ops, map_mr_sg_pi);
2743 SET_DEVICE_OP(dev_ops, mmap);
2744 SET_DEVICE_OP(dev_ops, mmap_free);
2745 SET_DEVICE_OP(dev_ops, modify_ah);
2746 SET_DEVICE_OP(dev_ops, modify_cq);
2747 SET_DEVICE_OP(dev_ops, modify_device);
2748 SET_DEVICE_OP(dev_ops, modify_hw_stat);
2749 SET_DEVICE_OP(dev_ops, modify_port);
2750 SET_DEVICE_OP(dev_ops, modify_qp);
2751 SET_DEVICE_OP(dev_ops, modify_srq);
2752 SET_DEVICE_OP(dev_ops, modify_wq);
2753 SET_DEVICE_OP(dev_ops, peek_cq);
2754 SET_DEVICE_OP(dev_ops, poll_cq);
2755 SET_DEVICE_OP(dev_ops, port_groups);
2756 SET_DEVICE_OP(dev_ops, post_recv);
2757 SET_DEVICE_OP(dev_ops, post_send);
2758 SET_DEVICE_OP(dev_ops, post_srq_recv);
2759 SET_DEVICE_OP(dev_ops, process_mad);
2760 SET_DEVICE_OP(dev_ops, query_ah);
2761 SET_DEVICE_OP(dev_ops, query_device);
2762 SET_DEVICE_OP(dev_ops, query_gid);
2763 SET_DEVICE_OP(dev_ops, query_pkey);
2764 SET_DEVICE_OP(dev_ops, query_port);
2765 SET_DEVICE_OP(dev_ops, query_qp);
2766 SET_DEVICE_OP(dev_ops, query_srq);
2767 SET_DEVICE_OP(dev_ops, query_ucontext);
2768 SET_DEVICE_OP(dev_ops, rdma_netdev_get_params);
2769 SET_DEVICE_OP(dev_ops, read_counters);
2770 SET_DEVICE_OP(dev_ops, reg_dm_mr);
2771 SET_DEVICE_OP(dev_ops, reg_user_mr);
2772 SET_DEVICE_OP(dev_ops, reg_user_mr_dmabuf);
2773 SET_DEVICE_OP(dev_ops, req_notify_cq);
2774 SET_DEVICE_OP(dev_ops, rereg_user_mr);
2775 SET_DEVICE_OP(dev_ops, resize_cq);
2776 SET_DEVICE_OP(dev_ops, set_vf_guid);
2777 SET_DEVICE_OP(dev_ops, set_vf_link_state);
2778
2779 SET_OBJ_SIZE(dev_ops, ib_ah);
2780 SET_OBJ_SIZE(dev_ops, ib_counters);
2781 SET_OBJ_SIZE(dev_ops, ib_cq);
2782 SET_OBJ_SIZE(dev_ops, ib_mw);
2783 SET_OBJ_SIZE(dev_ops, ib_pd);
2784 SET_OBJ_SIZE(dev_ops, ib_qp);
2785 SET_OBJ_SIZE(dev_ops, ib_rwq_ind_table);
2786 SET_OBJ_SIZE(dev_ops, ib_srq);
2787 SET_OBJ_SIZE(dev_ops, ib_ucontext);
2788 SET_OBJ_SIZE(dev_ops, ib_xrcd);
2789 }
2790 EXPORT_SYMBOL(ib_set_device_ops);
2791
ib_add_sub_device(struct ib_device * parent,enum rdma_nl_dev_type type,const char * name)2792 int ib_add_sub_device(struct ib_device *parent,
2793 enum rdma_nl_dev_type type,
2794 const char *name)
2795 {
2796 struct ib_device *sub;
2797 int ret = 0;
2798
2799 if (!parent->ops.add_sub_dev || !parent->ops.del_sub_dev)
2800 return -EOPNOTSUPP;
2801
2802 if (!ib_device_try_get(parent))
2803 return -EINVAL;
2804
2805 sub = parent->ops.add_sub_dev(parent, type, name);
2806 if (IS_ERR(sub)) {
2807 ib_device_put(parent);
2808 return PTR_ERR(sub);
2809 }
2810
2811 sub->type = type;
2812 sub->parent = parent;
2813
2814 mutex_lock(&parent->subdev_lock);
2815 list_add_tail(&parent->subdev_list_head, &sub->subdev_list);
2816 mutex_unlock(&parent->subdev_lock);
2817
2818 return ret;
2819 }
2820 EXPORT_SYMBOL(ib_add_sub_device);
2821
ib_del_sub_device_and_put(struct ib_device * sub)2822 int ib_del_sub_device_and_put(struct ib_device *sub)
2823 {
2824 struct ib_device *parent = sub->parent;
2825
2826 if (!parent)
2827 return -EOPNOTSUPP;
2828
2829 mutex_lock(&parent->subdev_lock);
2830 list_del(&sub->subdev_list);
2831 mutex_unlock(&parent->subdev_lock);
2832
2833 ib_device_put(sub);
2834 parent->ops.del_sub_dev(sub);
2835 ib_device_put(parent);
2836
2837 return 0;
2838 }
2839 EXPORT_SYMBOL(ib_del_sub_device_and_put);
2840
2841 #ifdef CONFIG_INFINIBAND_VIRT_DMA
ib_dma_virt_map_sg(struct ib_device * dev,struct scatterlist * sg,int nents)2842 int ib_dma_virt_map_sg(struct ib_device *dev, struct scatterlist *sg, int nents)
2843 {
2844 struct scatterlist *s;
2845 int i;
2846
2847 for_each_sg(sg, s, nents, i) {
2848 sg_dma_address(s) = (uintptr_t)sg_virt(s);
2849 sg_dma_len(s) = s->length;
2850 }
2851 return nents;
2852 }
2853 EXPORT_SYMBOL(ib_dma_virt_map_sg);
2854 #endif /* CONFIG_INFINIBAND_VIRT_DMA */
2855
2856 static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
2857 [RDMA_NL_LS_OP_RESOLVE] = {
2858 .doit = ib_nl_handle_resolve_resp,
2859 .flags = RDMA_NL_ADMIN_PERM,
2860 },
2861 [RDMA_NL_LS_OP_SET_TIMEOUT] = {
2862 .doit = ib_nl_handle_set_timeout,
2863 .flags = RDMA_NL_ADMIN_PERM,
2864 },
2865 [RDMA_NL_LS_OP_IP_RESOLVE] = {
2866 .doit = ib_nl_handle_ip_res_resp,
2867 .flags = RDMA_NL_ADMIN_PERM,
2868 },
2869 };
2870
ib_core_init(void)2871 static int __init ib_core_init(void)
2872 {
2873 int ret = -ENOMEM;
2874
2875 ib_wq = alloc_workqueue("infiniband", 0, 0);
2876 if (!ib_wq)
2877 return -ENOMEM;
2878
2879 ib_unreg_wq = alloc_workqueue("ib-unreg-wq", WQ_UNBOUND,
2880 WQ_UNBOUND_MAX_ACTIVE);
2881 if (!ib_unreg_wq)
2882 goto err;
2883
2884 ib_comp_wq = alloc_workqueue("ib-comp-wq",
2885 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2886 if (!ib_comp_wq)
2887 goto err_unbound;
2888
2889 ib_comp_unbound_wq =
2890 alloc_workqueue("ib-comp-unb-wq",
2891 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM |
2892 WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE);
2893 if (!ib_comp_unbound_wq)
2894 goto err_comp;
2895
2896 ret = class_register(&ib_class);
2897 if (ret) {
2898 pr_warn("Couldn't create InfiniBand device class\n");
2899 goto err_comp_unbound;
2900 }
2901
2902 rdma_nl_init();
2903
2904 ret = addr_init();
2905 if (ret) {
2906 pr_warn("Couldn't init IB address resolution\n");
2907 goto err_ibnl;
2908 }
2909
2910 ret = ib_mad_init();
2911 if (ret) {
2912 pr_warn("Couldn't init IB MAD\n");
2913 goto err_addr;
2914 }
2915
2916 ret = ib_sa_init();
2917 if (ret) {
2918 pr_warn("Couldn't init SA\n");
2919 goto err_mad;
2920 }
2921
2922 ret = register_blocking_lsm_notifier(&ibdev_lsm_nb);
2923 if (ret) {
2924 pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
2925 goto err_sa;
2926 }
2927
2928 ret = register_pernet_device(&rdma_dev_net_ops);
2929 if (ret) {
2930 pr_warn("Couldn't init compat dev. ret %d\n", ret);
2931 goto err_compat;
2932 }
2933
2934 nldev_init();
2935 rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
2936 ret = roce_gid_mgmt_init();
2937 if (ret) {
2938 pr_warn("Couldn't init RoCE GID management\n");
2939 goto err_parent;
2940 }
2941
2942 return 0;
2943
2944 err_parent:
2945 rdma_nl_unregister(RDMA_NL_LS);
2946 nldev_exit();
2947 unregister_pernet_device(&rdma_dev_net_ops);
2948 err_compat:
2949 unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
2950 err_sa:
2951 ib_sa_cleanup();
2952 err_mad:
2953 ib_mad_cleanup();
2954 err_addr:
2955 addr_cleanup();
2956 err_ibnl:
2957 class_unregister(&ib_class);
2958 err_comp_unbound:
2959 destroy_workqueue(ib_comp_unbound_wq);
2960 err_comp:
2961 destroy_workqueue(ib_comp_wq);
2962 err_unbound:
2963 destroy_workqueue(ib_unreg_wq);
2964 err:
2965 destroy_workqueue(ib_wq);
2966 return ret;
2967 }
2968
ib_core_cleanup(void)2969 static void __exit ib_core_cleanup(void)
2970 {
2971 roce_gid_mgmt_cleanup();
2972 rdma_nl_unregister(RDMA_NL_LS);
2973 nldev_exit();
2974 unregister_pernet_device(&rdma_dev_net_ops);
2975 unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
2976 ib_sa_cleanup();
2977 ib_mad_cleanup();
2978 addr_cleanup();
2979 rdma_nl_exit();
2980 class_unregister(&ib_class);
2981 destroy_workqueue(ib_comp_unbound_wq);
2982 destroy_workqueue(ib_comp_wq);
2983 /* Make sure that any pending umem accounting work is done. */
2984 destroy_workqueue(ib_wq);
2985 destroy_workqueue(ib_unreg_wq);
2986 WARN_ON(!xa_empty(&clients));
2987 WARN_ON(!xa_empty(&devices));
2988 }
2989
2990 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
2991
2992 /* ib core relies on netdev stack to first register net_ns_type_operations
2993 * ns kobject type before ib_core initialization.
2994 */
2995 fs_initcall(ib_core_init);
2996 module_exit(ib_core_cleanup);
2997