This source file includes following definitions.
- ib_client_put
- rdma_dev_access_netns
- xan_find_marked
- __ibdev_printk
- ibdev_printk
- ib_device_check_mandatory
- ib_device_get_by_index
- ib_device_put
- __ib_device_get_by_name
- ib_device_get_by_name
- rename_compat_devs
- ib_device_rename
- ib_device_set_dim
- alloc_name
- ib_device_release
- ib_device_uevent
- net_namespace
- rdma_init_coredev
- _ib_alloc_device
- ib_dealloc_device
- add_client_context
- remove_client_context
- alloc_port_data
- verify_immutable
- setup_port_data
- ib_get_device_fw_str
- ib_policy_change_task
- ib_security_change
- compatdev_release
- add_one_compat_dev
- remove_one_compat_dev
- remove_compat_devs
- add_compat_devs
- remove_all_compat_devs
- add_all_compat_devs
- rdma_compatdev_set
- rdma_dev_exit_net
- rdma_dev_init_net
- assign_name
- setup_dma_device
- setup_device
- disable_device
- enable_device_and_get
- ib_register_device
- __ib_unregister_device
- ib_unregister_device
- ib_unregister_device_and_put
- ib_unregister_driver
- ib_unregister_work
- ib_unregister_device_queued
- rdma_dev_change_netns
- ib_device_set_netns_put
- assign_client_id
- remove_client_id
- ib_register_client
- ib_unregister_client
- __ib_get_global_client_nl_info
- __ib_get_client_nl_info
- ib_get_client_nl_info
- ib_set_client_data
- ib_register_event_handler
- ib_unregister_event_handler
- ib_dispatch_event_clients
- iw_query_port
- __ib_query_port
- ib_query_port
- add_ndev_hash
- ib_device_set_netdev
- free_netdevs
- ib_device_get_netdev
- ib_device_get_by_netdev
- ib_enum_roce_netdev
- ib_enum_all_roce_netdevs
- ib_enum_all_devs
- ib_query_pkey
- ib_modify_device
- ib_modify_port
- ib_find_gid
- ib_find_pkey
- ib_get_net_dev_by_params
- ib_set_device_ops
- ib_core_init
- ib_core_cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 static DEFINE_XARRAY_FLAGS(devices, XA_FLAGS_ALLOC);
93 static DECLARE_RWSEM(devices_rwsem);
94 #define DEVICE_REGISTERED XA_MARK_1
95
96 static u32 highest_client_id;
97 #define CLIENT_REGISTERED XA_MARK_1
98 static DEFINE_XARRAY_FLAGS(clients, XA_FLAGS_ALLOC);
99 static DECLARE_RWSEM(clients_rwsem);
100
101 static void ib_client_put(struct ib_client *client)
102 {
103 if (refcount_dec_and_test(&client->uses))
104 complete(&client->uses_zero);
105 }
106
107
108
109
110
111 #define CLIENT_DATA_REGISTERED XA_MARK_1
112
113 unsigned int rdma_dev_net_id;
114
115
116
117
118
119
120 static DEFINE_XARRAY_FLAGS(rdma_nets, XA_FLAGS_ALLOC);
121
122
123
124 static DECLARE_RWSEM(rdma_nets_rwsem);
125
126 bool ib_devices_shared_netns = true;
127 module_param_named(netns_mode, ib_devices_shared_netns, bool, 0444);
128 MODULE_PARM_DESC(netns_mode,
129 "Share device among net namespaces; default=1 (shared)");
130
131
132
133
134
135
136
137
138
139
140
141
142
143 bool rdma_dev_access_netns(const struct ib_device *dev, const struct net *net)
144 {
145 return (ib_devices_shared_netns ||
146 net_eq(read_pnet(&dev->coredev.rdma_net), net));
147 }
148 EXPORT_SYMBOL(rdma_dev_access_netns);
149
150
151
152
153
154
155
156
157 static void *xan_find_marked(struct xarray *xa, unsigned long *indexp,
158 xa_mark_t filter)
159 {
160 XA_STATE(xas, xa, *indexp);
161 void *entry;
162
163 rcu_read_lock();
164 do {
165 entry = xas_find_marked(&xas, ULONG_MAX, filter);
166 if (xa_is_zero(entry))
167 break;
168 } while (xas_retry(&xas, entry));
169 rcu_read_unlock();
170
171 if (entry) {
172 *indexp = xas.xa_index;
173 if (xa_is_zero(entry))
174 return NULL;
175 return entry;
176 }
177 return XA_ERROR(-ENOENT);
178 }
179 #define xan_for_each_marked(xa, index, entry, filter) \
180 for (index = 0, entry = xan_find_marked(xa, &(index), filter); \
181 !xa_is_err(entry); \
182 (index)++, entry = xan_find_marked(xa, &(index), filter))
183
184
185 static DEFINE_SPINLOCK(ndev_hash_lock);
186 static DECLARE_HASHTABLE(ndev_hash, 5);
187
188 static void free_netdevs(struct ib_device *ib_dev);
189 static void ib_unregister_work(struct work_struct *work);
190 static void __ib_unregister_device(struct ib_device *device);
191 static int ib_security_change(struct notifier_block *nb, unsigned long event,
192 void *lsm_data);
193 static void ib_policy_change_task(struct work_struct *work);
194 static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
195
196 static void __ibdev_printk(const char *level, const struct ib_device *ibdev,
197 struct va_format *vaf)
198 {
199 if (ibdev && ibdev->dev.parent)
200 dev_printk_emit(level[1] - '0',
201 ibdev->dev.parent,
202 "%s %s %s: %pV",
203 dev_driver_string(ibdev->dev.parent),
204 dev_name(ibdev->dev.parent),
205 dev_name(&ibdev->dev),
206 vaf);
207 else if (ibdev)
208 printk("%s%s: %pV",
209 level, dev_name(&ibdev->dev), vaf);
210 else
211 printk("%s(NULL ib_device): %pV", level, vaf);
212 }
213
214 void ibdev_printk(const char *level, const struct ib_device *ibdev,
215 const char *format, ...)
216 {
217 struct va_format vaf;
218 va_list args;
219
220 va_start(args, format);
221
222 vaf.fmt = format;
223 vaf.va = &args;
224
225 __ibdev_printk(level, ibdev, &vaf);
226
227 va_end(args);
228 }
229 EXPORT_SYMBOL(ibdev_printk);
230
231 #define define_ibdev_printk_level(func, level) \
232 void func(const struct ib_device *ibdev, const char *fmt, ...) \
233 { \
234 struct va_format vaf; \
235 va_list args; \
236 \
237 va_start(args, fmt); \
238 \
239 vaf.fmt = fmt; \
240 vaf.va = &args; \
241 \
242 __ibdev_printk(level, ibdev, &vaf); \
243 \
244 va_end(args); \
245 } \
246 EXPORT_SYMBOL(func);
247
248 define_ibdev_printk_level(ibdev_emerg, KERN_EMERG);
249 define_ibdev_printk_level(ibdev_alert, KERN_ALERT);
250 define_ibdev_printk_level(ibdev_crit, KERN_CRIT);
251 define_ibdev_printk_level(ibdev_err, KERN_ERR);
252 define_ibdev_printk_level(ibdev_warn, KERN_WARNING);
253 define_ibdev_printk_level(ibdev_notice, KERN_NOTICE);
254 define_ibdev_printk_level(ibdev_info, KERN_INFO);
255
256 static struct notifier_block ibdev_lsm_nb = {
257 .notifier_call = ib_security_change,
258 };
259
260 static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
261 struct net *net);
262
263
264 struct ib_port_data_rcu {
265 struct rcu_head rcu_head;
266 struct ib_port_data pdata[];
267 };
268
269 static void ib_device_check_mandatory(struct ib_device *device)
270 {
271 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x }
272 static const struct {
273 size_t offset;
274 char *name;
275 } mandatory_table[] = {
276 IB_MANDATORY_FUNC(query_device),
277 IB_MANDATORY_FUNC(query_port),
278 IB_MANDATORY_FUNC(query_pkey),
279 IB_MANDATORY_FUNC(alloc_pd),
280 IB_MANDATORY_FUNC(dealloc_pd),
281 IB_MANDATORY_FUNC(create_qp),
282 IB_MANDATORY_FUNC(modify_qp),
283 IB_MANDATORY_FUNC(destroy_qp),
284 IB_MANDATORY_FUNC(post_send),
285 IB_MANDATORY_FUNC(post_recv),
286 IB_MANDATORY_FUNC(create_cq),
287 IB_MANDATORY_FUNC(destroy_cq),
288 IB_MANDATORY_FUNC(poll_cq),
289 IB_MANDATORY_FUNC(req_notify_cq),
290 IB_MANDATORY_FUNC(get_dma_mr),
291 IB_MANDATORY_FUNC(dereg_mr),
292 IB_MANDATORY_FUNC(get_port_immutable)
293 };
294 int i;
295
296 device->kverbs_provider = true;
297 for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
298 if (!*(void **) ((void *) &device->ops +
299 mandatory_table[i].offset)) {
300 device->kverbs_provider = false;
301 break;
302 }
303 }
304 }
305
306
307
308
309
310 struct ib_device *ib_device_get_by_index(const struct net *net, u32 index)
311 {
312 struct ib_device *device;
313
314 down_read(&devices_rwsem);
315 device = xa_load(&devices, index);
316 if (device) {
317 if (!rdma_dev_access_netns(device, net)) {
318 device = NULL;
319 goto out;
320 }
321
322 if (!ib_device_try_get(device))
323 device = NULL;
324 }
325 out:
326 up_read(&devices_rwsem);
327 return device;
328 }
329
330
331
332
333
334
335
336
337 void ib_device_put(struct ib_device *device)
338 {
339 if (refcount_dec_and_test(&device->refcount))
340 complete(&device->unreg_completion);
341 }
342 EXPORT_SYMBOL(ib_device_put);
343
344 static struct ib_device *__ib_device_get_by_name(const char *name)
345 {
346 struct ib_device *device;
347 unsigned long index;
348
349 xa_for_each (&devices, index, device)
350 if (!strcmp(name, dev_name(&device->dev)))
351 return device;
352
353 return NULL;
354 }
355
356
357
358
359
360
361
362
363
364 struct ib_device *ib_device_get_by_name(const char *name,
365 enum rdma_driver_id driver_id)
366 {
367 struct ib_device *device;
368
369 down_read(&devices_rwsem);
370 device = __ib_device_get_by_name(name);
371 if (device && driver_id != RDMA_DRIVER_UNKNOWN &&
372 device->ops.driver_id != driver_id)
373 device = NULL;
374
375 if (device) {
376 if (!ib_device_try_get(device))
377 device = NULL;
378 }
379 up_read(&devices_rwsem);
380 return device;
381 }
382 EXPORT_SYMBOL(ib_device_get_by_name);
383
384 static int rename_compat_devs(struct ib_device *device)
385 {
386 struct ib_core_device *cdev;
387 unsigned long index;
388 int ret = 0;
389
390 mutex_lock(&device->compat_devs_mutex);
391 xa_for_each (&device->compat_devs, index, cdev) {
392 ret = device_rename(&cdev->dev, dev_name(&device->dev));
393 if (ret) {
394 dev_warn(&cdev->dev,
395 "Fail to rename compatdev to new name %s\n",
396 dev_name(&device->dev));
397 break;
398 }
399 }
400 mutex_unlock(&device->compat_devs_mutex);
401 return ret;
402 }
403
404 int ib_device_rename(struct ib_device *ibdev, const char *name)
405 {
406 unsigned long index;
407 void *client_data;
408 int ret;
409
410 down_write(&devices_rwsem);
411 if (!strcmp(name, dev_name(&ibdev->dev))) {
412 up_write(&devices_rwsem);
413 return 0;
414 }
415
416 if (__ib_device_get_by_name(name)) {
417 up_write(&devices_rwsem);
418 return -EEXIST;
419 }
420
421 ret = device_rename(&ibdev->dev, name);
422 if (ret) {
423 up_write(&devices_rwsem);
424 return ret;
425 }
426
427 strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
428 ret = rename_compat_devs(ibdev);
429
430 downgrade_write(&devices_rwsem);
431 down_read(&ibdev->client_data_rwsem);
432 xan_for_each_marked(&ibdev->client_data, index, client_data,
433 CLIENT_DATA_REGISTERED) {
434 struct ib_client *client = xa_load(&clients, index);
435
436 if (!client || !client->rename)
437 continue;
438
439 client->rename(ibdev, client_data);
440 }
441 up_read(&ibdev->client_data_rwsem);
442 up_read(&devices_rwsem);
443 return 0;
444 }
445
446 int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim)
447 {
448 if (use_dim > 1)
449 return -EINVAL;
450 ibdev->use_cq_dim = use_dim;
451
452 return 0;
453 }
454
455 static int alloc_name(struct ib_device *ibdev, const char *name)
456 {
457 struct ib_device *device;
458 unsigned long index;
459 struct ida inuse;
460 int rc;
461 int i;
462
463 lockdep_assert_held_write(&devices_rwsem);
464 ida_init(&inuse);
465 xa_for_each (&devices, index, device) {
466 char buf[IB_DEVICE_NAME_MAX];
467
468 if (sscanf(dev_name(&device->dev), name, &i) != 1)
469 continue;
470 if (i < 0 || i >= INT_MAX)
471 continue;
472 snprintf(buf, sizeof buf, name, i);
473 if (strcmp(buf, dev_name(&device->dev)) != 0)
474 continue;
475
476 rc = ida_alloc_range(&inuse, i, i, GFP_KERNEL);
477 if (rc < 0)
478 goto out;
479 }
480
481 rc = ida_alloc(&inuse, GFP_KERNEL);
482 if (rc < 0)
483 goto out;
484
485 rc = dev_set_name(&ibdev->dev, name, rc);
486 out:
487 ida_destroy(&inuse);
488 return rc;
489 }
490
491 static void ib_device_release(struct device *device)
492 {
493 struct ib_device *dev = container_of(device, struct ib_device, dev);
494
495 free_netdevs(dev);
496 WARN_ON(refcount_read(&dev->refcount));
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->unregistration_lock);
507 mutex_destroy(&dev->compat_devs_mutex);
508
509 xa_destroy(&dev->compat_devs);
510 xa_destroy(&dev->client_data);
511 kfree_rcu(dev, rcu_head);
512 }
513
514 static int ib_device_uevent(struct device *device,
515 struct kobj_uevent_env *env)
516 {
517 if (add_uevent_var(env, "NAME=%s", dev_name(device)))
518 return -ENOMEM;
519
520
521
522
523
524 return 0;
525 }
526
527 static const void *net_namespace(struct device *d)
528 {
529 struct ib_core_device *coredev =
530 container_of(d, struct ib_core_device, dev);
531
532 return read_pnet(&coredev->rdma_net);
533 }
534
535 static struct class ib_class = {
536 .name = "infiniband",
537 .dev_release = ib_device_release,
538 .dev_uevent = ib_device_uevent,
539 .ns_type = &net_ns_type_operations,
540 .namespace = net_namespace,
541 };
542
543 static void rdma_init_coredev(struct ib_core_device *coredev,
544 struct ib_device *dev, struct net *net)
545 {
546
547
548
549
550
551
552 BUILD_BUG_ON(offsetof(struct ib_device, coredev.dev) !=
553 offsetof(struct ib_device, dev));
554
555 coredev->dev.class = &ib_class;
556 coredev->dev.groups = dev->groups;
557 device_initialize(&coredev->dev);
558 coredev->owner = dev;
559 INIT_LIST_HEAD(&coredev->port_list);
560 write_pnet(&coredev->rdma_net, net);
561 }
562
563
564
565
566
567
568
569
570
571
572
573 struct ib_device *_ib_alloc_device(size_t size)
574 {
575 struct ib_device *device;
576
577 if (WARN_ON(size < sizeof(struct ib_device)))
578 return NULL;
579
580 device = kzalloc(size, GFP_KERNEL);
581 if (!device)
582 return NULL;
583
584 if (rdma_restrack_init(device)) {
585 kfree(device);
586 return NULL;
587 }
588
589 device->groups[0] = &ib_dev_attr_group;
590 rdma_init_coredev(&device->coredev, device, &init_net);
591
592 INIT_LIST_HEAD(&device->event_handler_list);
593 spin_lock_init(&device->event_handler_lock);
594 init_rwsem(&device->event_handler_rwsem);
595 mutex_init(&device->unregistration_lock);
596
597
598
599
600 xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
601 init_rwsem(&device->client_data_rwsem);
602 xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC);
603 mutex_init(&device->compat_devs_mutex);
604 init_completion(&device->unreg_completion);
605 INIT_WORK(&device->unregistration_work, ib_unregister_work);
606
607 return device;
608 }
609 EXPORT_SYMBOL(_ib_alloc_device);
610
611
612
613
614
615
616
617 void ib_dealloc_device(struct ib_device *device)
618 {
619 if (device->ops.dealloc_driver)
620 device->ops.dealloc_driver(device);
621
622
623
624
625
626
627
628 down_write(&devices_rwsem);
629 if (xa_load(&devices, device->index) == device)
630 xa_erase(&devices, device->index);
631 up_write(&devices_rwsem);
632
633
634 free_netdevs(device);
635
636 WARN_ON(!xa_empty(&device->compat_devs));
637 WARN_ON(!xa_empty(&device->client_data));
638 WARN_ON(refcount_read(&device->refcount));
639 rdma_restrack_clean(device);
640
641 put_device(&device->dev);
642 }
643 EXPORT_SYMBOL(ib_dealloc_device);
644
645
646
647
648
649
650
651
652
653 static int add_client_context(struct ib_device *device,
654 struct ib_client *client)
655 {
656 int ret = 0;
657
658 if (!device->kverbs_provider && !client->no_kverbs_req)
659 return 0;
660
661 down_write(&device->client_data_rwsem);
662
663
664
665
666 if (!refcount_inc_not_zero(&client->uses))
667 goto out_unlock;
668 refcount_inc(&device->refcount);
669
670
671
672
673
674 if (xa_get_mark(&device->client_data, client->client_id,
675 CLIENT_DATA_REGISTERED))
676 goto out;
677
678 ret = xa_err(xa_store(&device->client_data, client->client_id, NULL,
679 GFP_KERNEL));
680 if (ret)
681 goto out;
682 downgrade_write(&device->client_data_rwsem);
683 if (client->add)
684 client->add(device);
685
686
687 xa_set_mark(&device->client_data, client->client_id,
688 CLIENT_DATA_REGISTERED);
689 up_read(&device->client_data_rwsem);
690 return 0;
691
692 out:
693 ib_device_put(device);
694 ib_client_put(client);
695 out_unlock:
696 up_write(&device->client_data_rwsem);
697 return ret;
698 }
699
700 static void remove_client_context(struct ib_device *device,
701 unsigned int client_id)
702 {
703 struct ib_client *client;
704 void *client_data;
705
706 down_write(&device->client_data_rwsem);
707 if (!xa_get_mark(&device->client_data, client_id,
708 CLIENT_DATA_REGISTERED)) {
709 up_write(&device->client_data_rwsem);
710 return;
711 }
712 client_data = xa_load(&device->client_data, client_id);
713 xa_clear_mark(&device->client_data, client_id, CLIENT_DATA_REGISTERED);
714 client = xa_load(&clients, client_id);
715 up_write(&device->client_data_rwsem);
716
717
718
719
720
721
722
723
724
725
726 if (client->remove)
727 client->remove(device, client_data);
728
729 xa_erase(&device->client_data, client_id);
730 ib_device_put(device);
731 ib_client_put(client);
732 }
733
734 static int alloc_port_data(struct ib_device *device)
735 {
736 struct ib_port_data_rcu *pdata_rcu;
737 unsigned int port;
738
739 if (device->port_data)
740 return 0;
741
742
743 if (WARN_ON(!device->phys_port_cnt))
744 return -EINVAL;
745
746
747
748
749
750
751
752
753 pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata,
754 rdma_end_port(device) + 1),
755 GFP_KERNEL);
756 if (!pdata_rcu)
757 return -ENOMEM;
758
759
760
761
762
763 device->port_data = pdata_rcu->pdata;
764
765 rdma_for_each_port (device, port) {
766 struct ib_port_data *pdata = &device->port_data[port];
767
768 pdata->ib_dev = device;
769 spin_lock_init(&pdata->pkey_list_lock);
770 INIT_LIST_HEAD(&pdata->pkey_list);
771 spin_lock_init(&pdata->netdev_lock);
772 INIT_HLIST_NODE(&pdata->ndev_hash_link);
773 }
774 return 0;
775 }
776
777 static int verify_immutable(const struct ib_device *dev, u8 port)
778 {
779 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
780 rdma_max_mad_size(dev, port) != 0);
781 }
782
783 static int setup_port_data(struct ib_device *device)
784 {
785 unsigned int port;
786 int ret;
787
788 ret = alloc_port_data(device);
789 if (ret)
790 return ret;
791
792 rdma_for_each_port (device, port) {
793 struct ib_port_data *pdata = &device->port_data[port];
794
795 ret = device->ops.get_port_immutable(device, port,
796 &pdata->immutable);
797 if (ret)
798 return ret;
799
800 if (verify_immutable(device, port))
801 return -EINVAL;
802 }
803 return 0;
804 }
805
806 void ib_get_device_fw_str(struct ib_device *dev, char *str)
807 {
808 if (dev->ops.get_dev_fw_str)
809 dev->ops.get_dev_fw_str(dev, str);
810 else
811 str[0] = '\0';
812 }
813 EXPORT_SYMBOL(ib_get_device_fw_str);
814
815 static void ib_policy_change_task(struct work_struct *work)
816 {
817 struct ib_device *dev;
818 unsigned long index;
819
820 down_read(&devices_rwsem);
821 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
822 unsigned int i;
823
824 rdma_for_each_port (dev, i) {
825 u64 sp;
826 int ret = ib_get_cached_subnet_prefix(dev,
827 i,
828 &sp);
829
830 WARN_ONCE(ret,
831 "ib_get_cached_subnet_prefix err: %d, this should never happen here\n",
832 ret);
833 if (!ret)
834 ib_security_cache_change(dev, i, sp);
835 }
836 }
837 up_read(&devices_rwsem);
838 }
839
840 static int ib_security_change(struct notifier_block *nb, unsigned long event,
841 void *lsm_data)
842 {
843 if (event != LSM_POLICY_CHANGE)
844 return NOTIFY_DONE;
845
846 schedule_work(&ib_policy_change_work);
847 ib_mad_agent_security_change();
848
849 return NOTIFY_OK;
850 }
851
852 static void compatdev_release(struct device *dev)
853 {
854 struct ib_core_device *cdev =
855 container_of(dev, struct ib_core_device, dev);
856
857 kfree(cdev);
858 }
859
860 static int add_one_compat_dev(struct ib_device *device,
861 struct rdma_dev_net *rnet)
862 {
863 struct ib_core_device *cdev;
864 int ret;
865
866 lockdep_assert_held(&rdma_nets_rwsem);
867 if (!ib_devices_shared_netns)
868 return 0;
869
870
871
872
873
874 if (net_eq(read_pnet(&rnet->net),
875 read_pnet(&device->coredev.rdma_net)))
876 return 0;
877
878
879
880
881
882
883 mutex_lock(&device->compat_devs_mutex);
884 cdev = xa_load(&device->compat_devs, rnet->id);
885 if (cdev) {
886 ret = 0;
887 goto done;
888 }
889 ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
890 if (ret)
891 goto done;
892
893 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
894 if (!cdev) {
895 ret = -ENOMEM;
896 goto cdev_err;
897 }
898
899 cdev->dev.parent = device->dev.parent;
900 rdma_init_coredev(cdev, device, read_pnet(&rnet->net));
901 cdev->dev.release = compatdev_release;
902 ret = dev_set_name(&cdev->dev, "%s", dev_name(&device->dev));
903 if (ret)
904 goto add_err;
905
906 ret = device_add(&cdev->dev);
907 if (ret)
908 goto add_err;
909 ret = ib_setup_port_attrs(cdev);
910 if (ret)
911 goto port_err;
912
913 ret = xa_err(xa_store(&device->compat_devs, rnet->id,
914 cdev, GFP_KERNEL));
915 if (ret)
916 goto insert_err;
917
918 mutex_unlock(&device->compat_devs_mutex);
919 return 0;
920
921 insert_err:
922 ib_free_port_attrs(cdev);
923 port_err:
924 device_del(&cdev->dev);
925 add_err:
926 put_device(&cdev->dev);
927 cdev_err:
928 xa_release(&device->compat_devs, rnet->id);
929 done:
930 mutex_unlock(&device->compat_devs_mutex);
931 return ret;
932 }
933
934 static void remove_one_compat_dev(struct ib_device *device, u32 id)
935 {
936 struct ib_core_device *cdev;
937
938 mutex_lock(&device->compat_devs_mutex);
939 cdev = xa_erase(&device->compat_devs, id);
940 mutex_unlock(&device->compat_devs_mutex);
941 if (cdev) {
942 ib_free_port_attrs(cdev);
943 device_del(&cdev->dev);
944 put_device(&cdev->dev);
945 }
946 }
947
948 static void remove_compat_devs(struct ib_device *device)
949 {
950 struct ib_core_device *cdev;
951 unsigned long index;
952
953 xa_for_each (&device->compat_devs, index, cdev)
954 remove_one_compat_dev(device, index);
955 }
956
957 static int add_compat_devs(struct ib_device *device)
958 {
959 struct rdma_dev_net *rnet;
960 unsigned long index;
961 int ret = 0;
962
963 lockdep_assert_held(&devices_rwsem);
964
965 down_read(&rdma_nets_rwsem);
966 xa_for_each (&rdma_nets, index, rnet) {
967 ret = add_one_compat_dev(device, rnet);
968 if (ret)
969 break;
970 }
971 up_read(&rdma_nets_rwsem);
972 return ret;
973 }
974
975 static void remove_all_compat_devs(void)
976 {
977 struct ib_compat_device *cdev;
978 struct ib_device *dev;
979 unsigned long index;
980
981 down_read(&devices_rwsem);
982 xa_for_each (&devices, index, dev) {
983 unsigned long c_index = 0;
984
985
986
987
988 down_read(&rdma_nets_rwsem);
989 xa_for_each (&dev->compat_devs, c_index, cdev)
990 remove_one_compat_dev(dev, c_index);
991 up_read(&rdma_nets_rwsem);
992 }
993 up_read(&devices_rwsem);
994 }
995
996 static int add_all_compat_devs(void)
997 {
998 struct rdma_dev_net *rnet;
999 struct ib_device *dev;
1000 unsigned long index;
1001 int ret = 0;
1002
1003 down_read(&devices_rwsem);
1004 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1005 unsigned long net_index = 0;
1006
1007
1008
1009
1010 down_read(&rdma_nets_rwsem);
1011 xa_for_each (&rdma_nets, net_index, rnet) {
1012 ret = add_one_compat_dev(dev, rnet);
1013 if (ret)
1014 break;
1015 }
1016 up_read(&rdma_nets_rwsem);
1017 }
1018 up_read(&devices_rwsem);
1019 if (ret)
1020 remove_all_compat_devs();
1021 return ret;
1022 }
1023
1024 int rdma_compatdev_set(u8 enable)
1025 {
1026 struct rdma_dev_net *rnet;
1027 unsigned long index;
1028 int ret = 0;
1029
1030 down_write(&rdma_nets_rwsem);
1031 if (ib_devices_shared_netns == enable) {
1032 up_write(&rdma_nets_rwsem);
1033 return 0;
1034 }
1035
1036
1037
1038
1039 xa_for_each (&rdma_nets, index, rnet) {
1040 ret++;
1041 break;
1042 }
1043 if (!ret)
1044 ib_devices_shared_netns = enable;
1045 up_write(&rdma_nets_rwsem);
1046 if (ret)
1047 return -EBUSY;
1048
1049 if (enable)
1050 ret = add_all_compat_devs();
1051 else
1052 remove_all_compat_devs();
1053 return ret;
1054 }
1055
1056 static void rdma_dev_exit_net(struct net *net)
1057 {
1058 struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1059 struct ib_device *dev;
1060 unsigned long index;
1061 int ret;
1062
1063 down_write(&rdma_nets_rwsem);
1064
1065
1066
1067 ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL));
1068 WARN_ON(ret);
1069 up_write(&rdma_nets_rwsem);
1070
1071 down_read(&devices_rwsem);
1072 xa_for_each (&devices, index, dev) {
1073 get_device(&dev->dev);
1074
1075
1076
1077
1078 up_read(&devices_rwsem);
1079
1080 remove_one_compat_dev(dev, rnet->id);
1081
1082
1083
1084
1085 rdma_dev_change_netns(dev, net, &init_net);
1086
1087 put_device(&dev->dev);
1088 down_read(&devices_rwsem);
1089 }
1090 up_read(&devices_rwsem);
1091
1092 rdma_nl_net_exit(rnet);
1093 xa_erase(&rdma_nets, rnet->id);
1094 }
1095
1096 static __net_init int rdma_dev_init_net(struct net *net)
1097 {
1098 struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
1099 unsigned long index;
1100 struct ib_device *dev;
1101 int ret;
1102
1103 write_pnet(&rnet->net, net);
1104
1105 ret = rdma_nl_net_init(rnet);
1106 if (ret)
1107 return ret;
1108
1109
1110 if (net_eq(net, &init_net))
1111 return 0;
1112
1113 ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
1114 if (ret) {
1115 rdma_nl_net_exit(rnet);
1116 return ret;
1117 }
1118
1119 down_read(&devices_rwsem);
1120 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1121
1122
1123
1124 down_read(&rdma_nets_rwsem);
1125 ret = add_one_compat_dev(dev, rnet);
1126 up_read(&rdma_nets_rwsem);
1127 if (ret)
1128 break;
1129 }
1130 up_read(&devices_rwsem);
1131
1132 if (ret)
1133 rdma_dev_exit_net(net);
1134
1135 return ret;
1136 }
1137
1138
1139
1140
1141
1142 static int assign_name(struct ib_device *device, const char *name)
1143 {
1144 static u32 last_id;
1145 int ret;
1146
1147 down_write(&devices_rwsem);
1148
1149 if (strchr(name, '%'))
1150 ret = alloc_name(device, name);
1151 else
1152 ret = dev_set_name(&device->dev, name);
1153 if (ret)
1154 goto out;
1155
1156 if (__ib_device_get_by_name(dev_name(&device->dev))) {
1157 ret = -ENFILE;
1158 goto out;
1159 }
1160 strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
1161
1162 ret = xa_alloc_cyclic(&devices, &device->index, device, xa_limit_31b,
1163 &last_id, GFP_KERNEL);
1164 if (ret > 0)
1165 ret = 0;
1166
1167 out:
1168 up_write(&devices_rwsem);
1169 return ret;
1170 }
1171
1172 static void setup_dma_device(struct ib_device *device)
1173 {
1174 struct device *parent = device->dev.parent;
1175
1176 WARN_ON_ONCE(device->dma_device);
1177 if (device->dev.dma_ops) {
1178
1179
1180
1181
1182
1183 device->dma_device = &device->dev;
1184 if (!device->dev.dma_mask) {
1185 if (parent)
1186 device->dev.dma_mask = parent->dma_mask;
1187 else
1188 WARN_ON_ONCE(true);
1189 }
1190 if (!device->dev.coherent_dma_mask) {
1191 if (parent)
1192 device->dev.coherent_dma_mask =
1193 parent->coherent_dma_mask;
1194 else
1195 WARN_ON_ONCE(true);
1196 }
1197 } else {
1198
1199
1200
1201
1202 WARN_ON_ONCE(!parent);
1203 device->dma_device = parent;
1204 }
1205
1206 if (!device->dev.dma_parms) {
1207 if (parent) {
1208
1209
1210
1211
1212
1213
1214 device->dev.dma_parms = parent->dma_parms;
1215 dma_set_max_seg_size(device->dma_device, SZ_2G);
1216 } else {
1217 WARN_ON_ONCE(true);
1218 }
1219 }
1220 }
1221
1222
1223
1224
1225
1226
1227 static int setup_device(struct ib_device *device)
1228 {
1229 struct ib_udata uhw = {.outlen = 0, .inlen = 0};
1230 int ret;
1231
1232 setup_dma_device(device);
1233 ib_device_check_mandatory(device);
1234
1235 ret = setup_port_data(device);
1236 if (ret) {
1237 dev_warn(&device->dev, "Couldn't create per-port data\n");
1238 return ret;
1239 }
1240
1241 memset(&device->attrs, 0, sizeof(device->attrs));
1242 ret = device->ops.query_device(device, &device->attrs, &uhw);
1243 if (ret) {
1244 dev_warn(&device->dev,
1245 "Couldn't query the device attributes\n");
1246 return ret;
1247 }
1248
1249 return 0;
1250 }
1251
1252 static void disable_device(struct ib_device *device)
1253 {
1254 u32 cid;
1255
1256 WARN_ON(!refcount_read(&device->refcount));
1257
1258 down_write(&devices_rwsem);
1259 xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
1260 up_write(&devices_rwsem);
1261
1262
1263
1264
1265
1266
1267
1268 down_read(&clients_rwsem);
1269 cid = highest_client_id;
1270 up_read(&clients_rwsem);
1271 while (cid) {
1272 cid--;
1273 remove_client_context(device, cid);
1274 }
1275
1276
1277 ib_device_put(device);
1278 wait_for_completion(&device->unreg_completion);
1279
1280
1281
1282
1283
1284
1285 remove_compat_devs(device);
1286 }
1287
1288
1289
1290
1291
1292
1293 static int enable_device_and_get(struct ib_device *device)
1294 {
1295 struct ib_client *client;
1296 unsigned long index;
1297 int ret = 0;
1298
1299
1300
1301
1302
1303 refcount_set(&device->refcount, 2);
1304 down_write(&devices_rwsem);
1305 xa_set_mark(&devices, device->index, DEVICE_REGISTERED);
1306
1307
1308
1309
1310
1311 downgrade_write(&devices_rwsem);
1312
1313 if (device->ops.enable_driver) {
1314 ret = device->ops.enable_driver(device);
1315 if (ret)
1316 goto out;
1317 }
1318
1319 down_read(&clients_rwsem);
1320 xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1321 ret = add_client_context(device, client);
1322 if (ret)
1323 break;
1324 }
1325 up_read(&clients_rwsem);
1326 if (!ret)
1327 ret = add_compat_devs(device);
1328 out:
1329 up_read(&devices_rwsem);
1330 return ret;
1331 }
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346 int ib_register_device(struct ib_device *device, const char *name)
1347 {
1348 int ret;
1349
1350 ret = assign_name(device, name);
1351 if (ret)
1352 return ret;
1353
1354 ret = setup_device(device);
1355 if (ret)
1356 return ret;
1357
1358 ret = ib_cache_setup_one(device);
1359 if (ret) {
1360 dev_warn(&device->dev,
1361 "Couldn't set up InfiniBand P_Key/GID cache\n");
1362 return ret;
1363 }
1364
1365 ib_device_register_rdmacg(device);
1366
1367 rdma_counter_init(device);
1368
1369
1370
1371
1372
1373 dev_set_uevent_suppress(&device->dev, true);
1374 ret = device_add(&device->dev);
1375 if (ret)
1376 goto cg_cleanup;
1377
1378 ret = ib_device_register_sysfs(device);
1379 if (ret) {
1380 dev_warn(&device->dev,
1381 "Couldn't register device with driver model\n");
1382 goto dev_cleanup;
1383 }
1384
1385 ret = enable_device_and_get(device);
1386 dev_set_uevent_suppress(&device->dev, false);
1387
1388 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1389 if (ret) {
1390 void (*dealloc_fn)(struct ib_device *);
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403 dealloc_fn = device->ops.dealloc_driver;
1404 device->ops.dealloc_driver = NULL;
1405 ib_device_put(device);
1406 __ib_unregister_device(device);
1407 device->ops.dealloc_driver = dealloc_fn;
1408 return ret;
1409 }
1410 ib_device_put(device);
1411
1412 return 0;
1413
1414 dev_cleanup:
1415 device_del(&device->dev);
1416 cg_cleanup:
1417 dev_set_uevent_suppress(&device->dev, false);
1418 ib_device_unregister_rdmacg(device);
1419 ib_cache_cleanup_one(device);
1420 return ret;
1421 }
1422 EXPORT_SYMBOL(ib_register_device);
1423
1424
1425 static void __ib_unregister_device(struct ib_device *ib_dev)
1426 {
1427
1428
1429
1430
1431
1432
1433
1434 mutex_lock(&ib_dev->unregistration_lock);
1435 if (!refcount_read(&ib_dev->refcount))
1436 goto out;
1437
1438 disable_device(ib_dev);
1439
1440
1441 free_netdevs(ib_dev);
1442
1443 ib_device_unregister_sysfs(ib_dev);
1444 device_del(&ib_dev->dev);
1445 ib_device_unregister_rdmacg(ib_dev);
1446 ib_cache_cleanup_one(ib_dev);
1447
1448
1449
1450
1451
1452 if (ib_dev->ops.dealloc_driver) {
1453 WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
1454 ib_dealloc_device(ib_dev);
1455 }
1456 out:
1457 mutex_unlock(&ib_dev->unregistration_lock);
1458 }
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474 void ib_unregister_device(struct ib_device *ib_dev)
1475 {
1476 get_device(&ib_dev->dev);
1477 __ib_unregister_device(ib_dev);
1478 put_device(&ib_dev->dev);
1479 }
1480 EXPORT_SYMBOL(ib_unregister_device);
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496 void ib_unregister_device_and_put(struct ib_device *ib_dev)
1497 {
1498 WARN_ON(!ib_dev->ops.dealloc_driver);
1499 get_device(&ib_dev->dev);
1500 ib_device_put(ib_dev);
1501 __ib_unregister_device(ib_dev);
1502 put_device(&ib_dev->dev);
1503 }
1504 EXPORT_SYMBOL(ib_unregister_device_and_put);
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520 void ib_unregister_driver(enum rdma_driver_id driver_id)
1521 {
1522 struct ib_device *ib_dev;
1523 unsigned long index;
1524
1525 down_read(&devices_rwsem);
1526 xa_for_each (&devices, index, ib_dev) {
1527 if (ib_dev->ops.driver_id != driver_id)
1528 continue;
1529
1530 get_device(&ib_dev->dev);
1531 up_read(&devices_rwsem);
1532
1533 WARN_ON(!ib_dev->ops.dealloc_driver);
1534 __ib_unregister_device(ib_dev);
1535
1536 put_device(&ib_dev->dev);
1537 down_read(&devices_rwsem);
1538 }
1539 up_read(&devices_rwsem);
1540 }
1541 EXPORT_SYMBOL(ib_unregister_driver);
1542
1543 static void ib_unregister_work(struct work_struct *work)
1544 {
1545 struct ib_device *ib_dev =
1546 container_of(work, struct ib_device, unregistration_work);
1547
1548 __ib_unregister_device(ib_dev);
1549 put_device(&ib_dev->dev);
1550 }
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563 void ib_unregister_device_queued(struct ib_device *ib_dev)
1564 {
1565 WARN_ON(!refcount_read(&ib_dev->refcount));
1566 WARN_ON(!ib_dev->ops.dealloc_driver);
1567 get_device(&ib_dev->dev);
1568 if (!queue_work(system_unbound_wq, &ib_dev->unregistration_work))
1569 put_device(&ib_dev->dev);
1570 }
1571 EXPORT_SYMBOL(ib_unregister_device_queued);
1572
1573
1574
1575
1576
1577
1578 static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
1579 struct net *net)
1580 {
1581 int ret2 = -EINVAL;
1582 int ret;
1583
1584 mutex_lock(&device->unregistration_lock);
1585
1586
1587
1588
1589
1590
1591 if (refcount_read(&device->refcount) == 0 ||
1592 !net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) {
1593 ret = -ENODEV;
1594 goto out;
1595 }
1596
1597 kobject_uevent(&device->dev.kobj, KOBJ_REMOVE);
1598 disable_device(device);
1599
1600
1601
1602
1603
1604 write_pnet(&device->coredev.rdma_net, net);
1605
1606 down_read(&devices_rwsem);
1607
1608
1609
1610
1611
1612 ret = device_rename(&device->dev, dev_name(&device->dev));
1613 up_read(&devices_rwsem);
1614 if (ret) {
1615 dev_warn(&device->dev,
1616 "%s: Couldn't rename device after namespace change\n",
1617 __func__);
1618
1619 write_pnet(&device->coredev.rdma_net, cur_net);
1620 }
1621
1622 ret2 = enable_device_and_get(device);
1623 if (ret2) {
1624
1625
1626
1627
1628 dev_warn(&device->dev,
1629 "%s: Couldn't re-enable device after namespace change\n",
1630 __func__);
1631 }
1632 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1633
1634 ib_device_put(device);
1635 out:
1636 mutex_unlock(&device->unregistration_lock);
1637 if (ret)
1638 return ret;
1639 return ret2;
1640 }
1641
1642 int ib_device_set_netns_put(struct sk_buff *skb,
1643 struct ib_device *dev, u32 ns_fd)
1644 {
1645 struct net *net;
1646 int ret;
1647
1648 net = get_net_ns_by_fd(ns_fd);
1649 if (IS_ERR(net)) {
1650 ret = PTR_ERR(net);
1651 goto net_err;
1652 }
1653
1654 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1655 ret = -EPERM;
1656 goto ns_err;
1657 }
1658
1659
1660
1661
1662
1663
1664
1665 if (!dev->ops.disassociate_ucontext || dev->ops.init_port ||
1666 ib_devices_shared_netns) {
1667 ret = -EOPNOTSUPP;
1668 goto ns_err;
1669 }
1670
1671 get_device(&dev->dev);
1672 ib_device_put(dev);
1673 ret = rdma_dev_change_netns(dev, current->nsproxy->net_ns, net);
1674 put_device(&dev->dev);
1675
1676 put_net(net);
1677 return ret;
1678
1679 ns_err:
1680 put_net(net);
1681 net_err:
1682 ib_device_put(dev);
1683 return ret;
1684 }
1685
1686 static struct pernet_operations rdma_dev_net_ops = {
1687 .init = rdma_dev_init_net,
1688 .exit = rdma_dev_exit_net,
1689 .id = &rdma_dev_net_id,
1690 .size = sizeof(struct rdma_dev_net),
1691 };
1692
1693 static int assign_client_id(struct ib_client *client)
1694 {
1695 int ret;
1696
1697 down_write(&clients_rwsem);
1698
1699
1700
1701
1702
1703 client->client_id = highest_client_id;
1704 ret = xa_insert(&clients, client->client_id, client, GFP_KERNEL);
1705 if (ret)
1706 goto out;
1707
1708 highest_client_id++;
1709 xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED);
1710
1711 out:
1712 up_write(&clients_rwsem);
1713 return ret;
1714 }
1715
1716 static void remove_client_id(struct ib_client *client)
1717 {
1718 down_write(&clients_rwsem);
1719 xa_erase(&clients, client->client_id);
1720 for (; highest_client_id; highest_client_id--)
1721 if (xa_load(&clients, highest_client_id - 1))
1722 break;
1723 up_write(&clients_rwsem);
1724 }
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739 int ib_register_client(struct ib_client *client)
1740 {
1741 struct ib_device *device;
1742 unsigned long index;
1743 int ret;
1744
1745 refcount_set(&client->uses, 1);
1746 init_completion(&client->uses_zero);
1747 ret = assign_client_id(client);
1748 if (ret)
1749 return ret;
1750
1751 down_read(&devices_rwsem);
1752 xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) {
1753 ret = add_client_context(device, client);
1754 if (ret) {
1755 up_read(&devices_rwsem);
1756 ib_unregister_client(client);
1757 return ret;
1758 }
1759 }
1760 up_read(&devices_rwsem);
1761 return 0;
1762 }
1763 EXPORT_SYMBOL(ib_register_client);
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776 void ib_unregister_client(struct ib_client *client)
1777 {
1778 struct ib_device *device;
1779 unsigned long index;
1780
1781 down_write(&clients_rwsem);
1782 ib_client_put(client);
1783 xa_clear_mark(&clients, client->client_id, CLIENT_REGISTERED);
1784 up_write(&clients_rwsem);
1785
1786
1787 rcu_read_lock();
1788 xa_for_each (&devices, index, device) {
1789 if (!ib_device_try_get(device))
1790 continue;
1791 rcu_read_unlock();
1792
1793 remove_client_context(device, client->client_id);
1794
1795 ib_device_put(device);
1796 rcu_read_lock();
1797 }
1798 rcu_read_unlock();
1799
1800
1801
1802
1803
1804 wait_for_completion(&client->uses_zero);
1805 remove_client_id(client);
1806 }
1807 EXPORT_SYMBOL(ib_unregister_client);
1808
1809 static int __ib_get_global_client_nl_info(const char *client_name,
1810 struct ib_client_nl_info *res)
1811 {
1812 struct ib_client *client;
1813 unsigned long index;
1814 int ret = -ENOENT;
1815
1816 down_read(&clients_rwsem);
1817 xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1818 if (strcmp(client->name, client_name) != 0)
1819 continue;
1820 if (!client->get_global_nl_info) {
1821 ret = -EOPNOTSUPP;
1822 break;
1823 }
1824 ret = client->get_global_nl_info(res);
1825 if (WARN_ON(ret == -ENOENT))
1826 ret = -EINVAL;
1827 if (!ret && res->cdev)
1828 get_device(res->cdev);
1829 break;
1830 }
1831 up_read(&clients_rwsem);
1832 return ret;
1833 }
1834
1835 static int __ib_get_client_nl_info(struct ib_device *ibdev,
1836 const char *client_name,
1837 struct ib_client_nl_info *res)
1838 {
1839 unsigned long index;
1840 void *client_data;
1841 int ret = -ENOENT;
1842
1843 down_read(&ibdev->client_data_rwsem);
1844 xan_for_each_marked (&ibdev->client_data, index, client_data,
1845 CLIENT_DATA_REGISTERED) {
1846 struct ib_client *client = xa_load(&clients, index);
1847
1848 if (!client || strcmp(client->name, client_name) != 0)
1849 continue;
1850 if (!client->get_nl_info) {
1851 ret = -EOPNOTSUPP;
1852 break;
1853 }
1854 ret = client->get_nl_info(ibdev, client_data, res);
1855 if (WARN_ON(ret == -ENOENT))
1856 ret = -EINVAL;
1857
1858
1859
1860
1861
1862
1863 if (!ret && res->cdev)
1864 get_device(res->cdev);
1865 break;
1866 }
1867 up_read(&ibdev->client_data_rwsem);
1868
1869 return ret;
1870 }
1871
1872
1873
1874
1875
1876
1877
1878 int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
1879 struct ib_client_nl_info *res)
1880 {
1881 int ret;
1882
1883 if (ibdev)
1884 ret = __ib_get_client_nl_info(ibdev, client_name, res);
1885 else
1886 ret = __ib_get_global_client_nl_info(client_name, res);
1887 #ifdef CONFIG_MODULES
1888 if (ret == -ENOENT) {
1889 request_module("rdma-client-%s", client_name);
1890 if (ibdev)
1891 ret = __ib_get_client_nl_info(ibdev, client_name, res);
1892 else
1893 ret = __ib_get_global_client_nl_info(client_name, res);
1894 }
1895 #endif
1896 if (ret) {
1897 if (ret == -ENOENT)
1898 return -EOPNOTSUPP;
1899 return ret;
1900 }
1901
1902 if (WARN_ON(!res->cdev))
1903 return -EINVAL;
1904 return 0;
1905 }
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
1919 void *data)
1920 {
1921 void *rc;
1922
1923 if (WARN_ON(IS_ERR(data)))
1924 data = NULL;
1925
1926 rc = xa_store(&device->client_data, client->client_id, data,
1927 GFP_KERNEL);
1928 WARN_ON(xa_is_err(rc));
1929 }
1930 EXPORT_SYMBOL(ib_set_client_data);
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 void ib_register_event_handler(struct ib_event_handler *event_handler)
1942 {
1943 down_write(&event_handler->device->event_handler_rwsem);
1944 list_add_tail(&event_handler->list,
1945 &event_handler->device->event_handler_list);
1946 up_write(&event_handler->device->event_handler_rwsem);
1947 }
1948 EXPORT_SYMBOL(ib_register_event_handler);
1949
1950
1951
1952
1953
1954
1955
1956
1957 void ib_unregister_event_handler(struct ib_event_handler *event_handler)
1958 {
1959 down_write(&event_handler->device->event_handler_rwsem);
1960 list_del(&event_handler->list);
1961 up_write(&event_handler->device->event_handler_rwsem);
1962 }
1963 EXPORT_SYMBOL(ib_unregister_event_handler);
1964
1965 void ib_dispatch_event_clients(struct ib_event *event)
1966 {
1967 struct ib_event_handler *handler;
1968
1969 down_read(&event->device->event_handler_rwsem);
1970
1971 list_for_each_entry(handler, &event->device->event_handler_list, list)
1972 handler->handler(handler, event);
1973
1974 up_read(&event->device->event_handler_rwsem);
1975 }
1976
1977 static int iw_query_port(struct ib_device *device,
1978 u8 port_num,
1979 struct ib_port_attr *port_attr)
1980 {
1981 struct in_device *inetdev;
1982 struct net_device *netdev;
1983 int err;
1984
1985 memset(port_attr, 0, sizeof(*port_attr));
1986
1987 netdev = ib_device_get_netdev(device, port_num);
1988 if (!netdev)
1989 return -ENODEV;
1990
1991 port_attr->max_mtu = IB_MTU_4096;
1992 port_attr->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
1993
1994 if (!netif_carrier_ok(netdev)) {
1995 port_attr->state = IB_PORT_DOWN;
1996 port_attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
1997 } else {
1998 rcu_read_lock();
1999 inetdev = __in_dev_get_rcu(netdev);
2000
2001 if (inetdev && inetdev->ifa_list) {
2002 port_attr->state = IB_PORT_ACTIVE;
2003 port_attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
2004 } else {
2005 port_attr->state = IB_PORT_INIT;
2006 port_attr->phys_state =
2007 IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING;
2008 }
2009
2010 rcu_read_unlock();
2011 }
2012
2013 dev_put(netdev);
2014 err = device->ops.query_port(device, port_num, port_attr);
2015 if (err)
2016 return err;
2017
2018 return 0;
2019 }
2020
2021 static int __ib_query_port(struct ib_device *device,
2022 u8 port_num,
2023 struct ib_port_attr *port_attr)
2024 {
2025 union ib_gid gid = {};
2026 int err;
2027
2028 memset(port_attr, 0, sizeof(*port_attr));
2029
2030 err = device->ops.query_port(device, port_num, port_attr);
2031 if (err || port_attr->subnet_prefix)
2032 return err;
2033
2034 if (rdma_port_get_link_layer(device, port_num) !=
2035 IB_LINK_LAYER_INFINIBAND)
2036 return 0;
2037
2038 err = device->ops.query_gid(device, port_num, 0, &gid);
2039 if (err)
2040 return err;
2041
2042 port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
2043 return 0;
2044 }
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055 int ib_query_port(struct ib_device *device,
2056 u8 port_num,
2057 struct ib_port_attr *port_attr)
2058 {
2059 if (!rdma_is_port_valid(device, port_num))
2060 return -EINVAL;
2061
2062 if (rdma_protocol_iwarp(device, port_num))
2063 return iw_query_port(device, port_num, port_attr);
2064 else
2065 return __ib_query_port(device, port_num, port_attr);
2066 }
2067 EXPORT_SYMBOL(ib_query_port);
2068
2069 static void add_ndev_hash(struct ib_port_data *pdata)
2070 {
2071 unsigned long flags;
2072
2073 might_sleep();
2074
2075 spin_lock_irqsave(&ndev_hash_lock, flags);
2076 if (hash_hashed(&pdata->ndev_hash_link)) {
2077 hash_del_rcu(&pdata->ndev_hash_link);
2078 spin_unlock_irqrestore(&ndev_hash_lock, flags);
2079
2080
2081
2082
2083 synchronize_rcu();
2084 spin_lock_irqsave(&ndev_hash_lock, flags);
2085 }
2086 if (pdata->netdev)
2087 hash_add_rcu(ndev_hash, &pdata->ndev_hash_link,
2088 (uintptr_t)pdata->netdev);
2089 spin_unlock_irqrestore(&ndev_hash_lock, flags);
2090 }
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107 int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
2108 unsigned int port)
2109 {
2110 struct net_device *old_ndev;
2111 struct ib_port_data *pdata;
2112 unsigned long flags;
2113 int ret;
2114
2115
2116
2117
2118
2119 ret = alloc_port_data(ib_dev);
2120 if (ret)
2121 return ret;
2122
2123 if (!rdma_is_port_valid(ib_dev, port))
2124 return -EINVAL;
2125
2126 pdata = &ib_dev->port_data[port];
2127 spin_lock_irqsave(&pdata->netdev_lock, flags);
2128 old_ndev = rcu_dereference_protected(
2129 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
2130 if (old_ndev == ndev) {
2131 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
2132 return 0;
2133 }
2134
2135 if (ndev)
2136 dev_hold(ndev);
2137 rcu_assign_pointer(pdata->netdev, ndev);
2138 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
2139
2140 add_ndev_hash(pdata);
2141 if (old_ndev)
2142 dev_put(old_ndev);
2143
2144 return 0;
2145 }
2146 EXPORT_SYMBOL(ib_device_set_netdev);
2147
2148 static void free_netdevs(struct ib_device *ib_dev)
2149 {
2150 unsigned long flags;
2151 unsigned int port;
2152
2153 if (!ib_dev->port_data)
2154 return;
2155
2156 rdma_for_each_port (ib_dev, port) {
2157 struct ib_port_data *pdata = &ib_dev->port_data[port];
2158 struct net_device *ndev;
2159
2160 spin_lock_irqsave(&pdata->netdev_lock, flags);
2161 ndev = rcu_dereference_protected(
2162 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
2163 if (ndev) {
2164 spin_lock(&ndev_hash_lock);
2165 hash_del_rcu(&pdata->ndev_hash_link);
2166 spin_unlock(&ndev_hash_lock);
2167
2168
2169
2170
2171
2172
2173
2174 rcu_assign_pointer(pdata->netdev, NULL);
2175 dev_put(ndev);
2176 }
2177 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
2178 }
2179 }
2180
2181 struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
2182 unsigned int port)
2183 {
2184 struct ib_port_data *pdata;
2185 struct net_device *res;
2186
2187 if (!rdma_is_port_valid(ib_dev, port))
2188 return NULL;
2189
2190 pdata = &ib_dev->port_data[port];
2191
2192
2193
2194
2195
2196 if (ib_dev->ops.get_netdev)
2197 res = ib_dev->ops.get_netdev(ib_dev, port);
2198 else {
2199 spin_lock(&pdata->netdev_lock);
2200 res = rcu_dereference_protected(
2201 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
2202 if (res)
2203 dev_hold(res);
2204 spin_unlock(&pdata->netdev_lock);
2205 }
2206
2207
2208
2209
2210
2211 if (res && res->reg_state != NETREG_REGISTERED) {
2212 dev_put(res);
2213 return NULL;
2214 }
2215
2216 return res;
2217 }
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228 struct ib_device *ib_device_get_by_netdev(struct net_device *ndev,
2229 enum rdma_driver_id driver_id)
2230 {
2231 struct ib_device *res = NULL;
2232 struct ib_port_data *cur;
2233
2234 rcu_read_lock();
2235 hash_for_each_possible_rcu (ndev_hash, cur, ndev_hash_link,
2236 (uintptr_t)ndev) {
2237 if (rcu_access_pointer(cur->netdev) == ndev &&
2238 (driver_id == RDMA_DRIVER_UNKNOWN ||
2239 cur->ib_dev->ops.driver_id == driver_id) &&
2240 ib_device_try_get(cur->ib_dev)) {
2241 res = cur->ib_dev;
2242 break;
2243 }
2244 }
2245 rcu_read_unlock();
2246
2247 return res;
2248 }
2249 EXPORT_SYMBOL(ib_device_get_by_netdev);
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 void ib_enum_roce_netdev(struct ib_device *ib_dev,
2264 roce_netdev_filter filter,
2265 void *filter_cookie,
2266 roce_netdev_callback cb,
2267 void *cookie)
2268 {
2269 unsigned int port;
2270
2271 rdma_for_each_port (ib_dev, port)
2272 if (rdma_protocol_roce(ib_dev, port)) {
2273 struct net_device *idev =
2274 ib_device_get_netdev(ib_dev, port);
2275
2276 if (filter(ib_dev, port, idev, filter_cookie))
2277 cb(ib_dev, port, idev, cookie);
2278
2279 if (idev)
2280 dev_put(idev);
2281 }
2282 }
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
2296 void *filter_cookie,
2297 roce_netdev_callback cb,
2298 void *cookie)
2299 {
2300 struct ib_device *dev;
2301 unsigned long index;
2302
2303 down_read(&devices_rwsem);
2304 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED)
2305 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
2306 up_read(&devices_rwsem);
2307 }
2308
2309
2310
2311
2312
2313
2314
2315 int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
2316 struct netlink_callback *cb)
2317 {
2318 unsigned long index;
2319 struct ib_device *dev;
2320 unsigned int idx = 0;
2321 int ret = 0;
2322
2323 down_read(&devices_rwsem);
2324 xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
2325 if (!rdma_dev_access_netns(dev, sock_net(skb->sk)))
2326 continue;
2327
2328 ret = nldev_cb(dev, skb, cb, idx);
2329 if (ret)
2330 break;
2331 idx++;
2332 }
2333 up_read(&devices_rwsem);
2334 return ret;
2335 }
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346 int ib_query_pkey(struct ib_device *device,
2347 u8 port_num, u16 index, u16 *pkey)
2348 {
2349 if (!rdma_is_port_valid(device, port_num))
2350 return -EINVAL;
2351
2352 return device->ops.query_pkey(device, port_num, index, pkey);
2353 }
2354 EXPORT_SYMBOL(ib_query_pkey);
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365 int ib_modify_device(struct ib_device *device,
2366 int device_modify_mask,
2367 struct ib_device_modify *device_modify)
2368 {
2369 if (!device->ops.modify_device)
2370 return -ENOSYS;
2371
2372 return device->ops.modify_device(device, device_modify_mask,
2373 device_modify);
2374 }
2375 EXPORT_SYMBOL(ib_modify_device);
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388 int ib_modify_port(struct ib_device *device,
2389 u8 port_num, int port_modify_mask,
2390 struct ib_port_modify *port_modify)
2391 {
2392 int rc;
2393
2394 if (!rdma_is_port_valid(device, port_num))
2395 return -EINVAL;
2396
2397 if (device->ops.modify_port)
2398 rc = device->ops.modify_port(device, port_num,
2399 port_modify_mask,
2400 port_modify);
2401 else if (rdma_protocol_roce(device, port_num) &&
2402 ((port_modify->set_port_cap_mask & ~IB_PORT_CM_SUP) == 0 ||
2403 (port_modify->clr_port_cap_mask & ~IB_PORT_CM_SUP) == 0))
2404 rc = 0;
2405 else
2406 rc = -EOPNOTSUPP;
2407 return rc;
2408 }
2409 EXPORT_SYMBOL(ib_modify_port);
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
2421 u8 *port_num, u16 *index)
2422 {
2423 union ib_gid tmp_gid;
2424 unsigned int port;
2425 int ret, i;
2426
2427 rdma_for_each_port (device, port) {
2428 if (!rdma_protocol_ib(device, port))
2429 continue;
2430
2431 for (i = 0; i < device->port_data[port].immutable.gid_tbl_len;
2432 ++i) {
2433 ret = rdma_query_gid(device, port, i, &tmp_gid);
2434 if (ret)
2435 return ret;
2436 if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
2437 *port_num = port;
2438 if (index)
2439 *index = i;
2440 return 0;
2441 }
2442 }
2443 }
2444
2445 return -ENOENT;
2446 }
2447 EXPORT_SYMBOL(ib_find_gid);
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457 int ib_find_pkey(struct ib_device *device,
2458 u8 port_num, u16 pkey, u16 *index)
2459 {
2460 int ret, i;
2461 u16 tmp_pkey;
2462 int partial_ix = -1;
2463
2464 for (i = 0; i < device->port_data[port_num].immutable.pkey_tbl_len;
2465 ++i) {
2466 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
2467 if (ret)
2468 return ret;
2469 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
2470
2471 if (tmp_pkey & 0x8000) {
2472 *index = i;
2473 return 0;
2474 }
2475 if (partial_ix < 0)
2476 partial_ix = i;
2477 }
2478 }
2479
2480
2481 if (partial_ix >= 0) {
2482 *index = partial_ix;
2483 return 0;
2484 }
2485 return -ENOENT;
2486 }
2487 EXPORT_SYMBOL(ib_find_pkey);
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500 struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
2501 u8 port,
2502 u16 pkey,
2503 const union ib_gid *gid,
2504 const struct sockaddr *addr)
2505 {
2506 struct net_device *net_dev = NULL;
2507 unsigned long index;
2508 void *client_data;
2509
2510 if (!rdma_protocol_ib(dev, port))
2511 return NULL;
2512
2513
2514
2515
2516
2517 down_read(&dev->client_data_rwsem);
2518 xan_for_each_marked (&dev->client_data, index, client_data,
2519 CLIENT_DATA_REGISTERED) {
2520 struct ib_client *client = xa_load(&clients, index);
2521
2522 if (!client || !client->get_net_dev_by_params)
2523 continue;
2524
2525 net_dev = client->get_net_dev_by_params(dev, port, pkey, gid,
2526 addr, client_data);
2527 if (net_dev)
2528 break;
2529 }
2530 up_read(&dev->client_data_rwsem);
2531
2532 return net_dev;
2533 }
2534 EXPORT_SYMBOL(ib_get_net_dev_by_params);
2535
2536 void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
2537 {
2538 struct ib_device_ops *dev_ops = &dev->ops;
2539 #define SET_DEVICE_OP(ptr, name) \
2540 do { \
2541 if (ops->name) \
2542 if (!((ptr)->name)) \
2543 (ptr)->name = ops->name; \
2544 } while (0)
2545
2546 #define SET_OBJ_SIZE(ptr, name) SET_DEVICE_OP(ptr, size_##name)
2547
2548 if (ops->driver_id != RDMA_DRIVER_UNKNOWN) {
2549 WARN_ON(dev_ops->driver_id != RDMA_DRIVER_UNKNOWN &&
2550 dev_ops->driver_id != ops->driver_id);
2551 dev_ops->driver_id = ops->driver_id;
2552 }
2553 if (ops->owner) {
2554 WARN_ON(dev_ops->owner && dev_ops->owner != ops->owner);
2555 dev_ops->owner = ops->owner;
2556 }
2557 if (ops->uverbs_abi_ver)
2558 dev_ops->uverbs_abi_ver = ops->uverbs_abi_ver;
2559
2560 dev_ops->uverbs_no_driver_id_binding |=
2561 ops->uverbs_no_driver_id_binding;
2562
2563 SET_DEVICE_OP(dev_ops, add_gid);
2564 SET_DEVICE_OP(dev_ops, advise_mr);
2565 SET_DEVICE_OP(dev_ops, alloc_dm);
2566 SET_DEVICE_OP(dev_ops, alloc_fmr);
2567 SET_DEVICE_OP(dev_ops, alloc_hw_stats);
2568 SET_DEVICE_OP(dev_ops, alloc_mr);
2569 SET_DEVICE_OP(dev_ops, alloc_mr_integrity);
2570 SET_DEVICE_OP(dev_ops, alloc_mw);
2571 SET_DEVICE_OP(dev_ops, alloc_pd);
2572 SET_DEVICE_OP(dev_ops, alloc_rdma_netdev);
2573 SET_DEVICE_OP(dev_ops, alloc_ucontext);
2574 SET_DEVICE_OP(dev_ops, alloc_xrcd);
2575 SET_DEVICE_OP(dev_ops, attach_mcast);
2576 SET_DEVICE_OP(dev_ops, check_mr_status);
2577 SET_DEVICE_OP(dev_ops, counter_alloc_stats);
2578 SET_DEVICE_OP(dev_ops, counter_bind_qp);
2579 SET_DEVICE_OP(dev_ops, counter_dealloc);
2580 SET_DEVICE_OP(dev_ops, counter_unbind_qp);
2581 SET_DEVICE_OP(dev_ops, counter_update_stats);
2582 SET_DEVICE_OP(dev_ops, create_ah);
2583 SET_DEVICE_OP(dev_ops, create_counters);
2584 SET_DEVICE_OP(dev_ops, create_cq);
2585 SET_DEVICE_OP(dev_ops, create_flow);
2586 SET_DEVICE_OP(dev_ops, create_flow_action_esp);
2587 SET_DEVICE_OP(dev_ops, create_qp);
2588 SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
2589 SET_DEVICE_OP(dev_ops, create_srq);
2590 SET_DEVICE_OP(dev_ops, create_wq);
2591 SET_DEVICE_OP(dev_ops, dealloc_dm);
2592 SET_DEVICE_OP(dev_ops, dealloc_driver);
2593 SET_DEVICE_OP(dev_ops, dealloc_fmr);
2594 SET_DEVICE_OP(dev_ops, dealloc_mw);
2595 SET_DEVICE_OP(dev_ops, dealloc_pd);
2596 SET_DEVICE_OP(dev_ops, dealloc_ucontext);
2597 SET_DEVICE_OP(dev_ops, dealloc_xrcd);
2598 SET_DEVICE_OP(dev_ops, del_gid);
2599 SET_DEVICE_OP(dev_ops, dereg_mr);
2600 SET_DEVICE_OP(dev_ops, destroy_ah);
2601 SET_DEVICE_OP(dev_ops, destroy_counters);
2602 SET_DEVICE_OP(dev_ops, destroy_cq);
2603 SET_DEVICE_OP(dev_ops, destroy_flow);
2604 SET_DEVICE_OP(dev_ops, destroy_flow_action);
2605 SET_DEVICE_OP(dev_ops, destroy_qp);
2606 SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table);
2607 SET_DEVICE_OP(dev_ops, destroy_srq);
2608 SET_DEVICE_OP(dev_ops, destroy_wq);
2609 SET_DEVICE_OP(dev_ops, detach_mcast);
2610 SET_DEVICE_OP(dev_ops, disassociate_ucontext);
2611 SET_DEVICE_OP(dev_ops, drain_rq);
2612 SET_DEVICE_OP(dev_ops, drain_sq);
2613 SET_DEVICE_OP(dev_ops, enable_driver);
2614 SET_DEVICE_OP(dev_ops, fill_res_entry);
2615 SET_DEVICE_OP(dev_ops, get_dev_fw_str);
2616 SET_DEVICE_OP(dev_ops, get_dma_mr);
2617 SET_DEVICE_OP(dev_ops, get_hw_stats);
2618 SET_DEVICE_OP(dev_ops, get_link_layer);
2619 SET_DEVICE_OP(dev_ops, get_netdev);
2620 SET_DEVICE_OP(dev_ops, get_port_immutable);
2621 SET_DEVICE_OP(dev_ops, get_vector_affinity);
2622 SET_DEVICE_OP(dev_ops, get_vf_config);
2623 SET_DEVICE_OP(dev_ops, get_vf_stats);
2624 SET_DEVICE_OP(dev_ops, init_port);
2625 SET_DEVICE_OP(dev_ops, invalidate_range);
2626 SET_DEVICE_OP(dev_ops, iw_accept);
2627 SET_DEVICE_OP(dev_ops, iw_add_ref);
2628 SET_DEVICE_OP(dev_ops, iw_connect);
2629 SET_DEVICE_OP(dev_ops, iw_create_listen);
2630 SET_DEVICE_OP(dev_ops, iw_destroy_listen);
2631 SET_DEVICE_OP(dev_ops, iw_get_qp);
2632 SET_DEVICE_OP(dev_ops, iw_reject);
2633 SET_DEVICE_OP(dev_ops, iw_rem_ref);
2634 SET_DEVICE_OP(dev_ops, map_mr_sg);
2635 SET_DEVICE_OP(dev_ops, map_mr_sg_pi);
2636 SET_DEVICE_OP(dev_ops, map_phys_fmr);
2637 SET_DEVICE_OP(dev_ops, mmap);
2638 SET_DEVICE_OP(dev_ops, modify_ah);
2639 SET_DEVICE_OP(dev_ops, modify_cq);
2640 SET_DEVICE_OP(dev_ops, modify_device);
2641 SET_DEVICE_OP(dev_ops, modify_flow_action_esp);
2642 SET_DEVICE_OP(dev_ops, modify_port);
2643 SET_DEVICE_OP(dev_ops, modify_qp);
2644 SET_DEVICE_OP(dev_ops, modify_srq);
2645 SET_DEVICE_OP(dev_ops, modify_wq);
2646 SET_DEVICE_OP(dev_ops, peek_cq);
2647 SET_DEVICE_OP(dev_ops, poll_cq);
2648 SET_DEVICE_OP(dev_ops, post_recv);
2649 SET_DEVICE_OP(dev_ops, post_send);
2650 SET_DEVICE_OP(dev_ops, post_srq_recv);
2651 SET_DEVICE_OP(dev_ops, process_mad);
2652 SET_DEVICE_OP(dev_ops, query_ah);
2653 SET_DEVICE_OP(dev_ops, query_device);
2654 SET_DEVICE_OP(dev_ops, query_gid);
2655 SET_DEVICE_OP(dev_ops, query_pkey);
2656 SET_DEVICE_OP(dev_ops, query_port);
2657 SET_DEVICE_OP(dev_ops, query_qp);
2658 SET_DEVICE_OP(dev_ops, query_srq);
2659 SET_DEVICE_OP(dev_ops, rdma_netdev_get_params);
2660 SET_DEVICE_OP(dev_ops, read_counters);
2661 SET_DEVICE_OP(dev_ops, reg_dm_mr);
2662 SET_DEVICE_OP(dev_ops, reg_user_mr);
2663 SET_DEVICE_OP(dev_ops, req_ncomp_notif);
2664 SET_DEVICE_OP(dev_ops, req_notify_cq);
2665 SET_DEVICE_OP(dev_ops, rereg_user_mr);
2666 SET_DEVICE_OP(dev_ops, resize_cq);
2667 SET_DEVICE_OP(dev_ops, set_vf_guid);
2668 SET_DEVICE_OP(dev_ops, set_vf_link_state);
2669 SET_DEVICE_OP(dev_ops, unmap_fmr);
2670
2671 SET_OBJ_SIZE(dev_ops, ib_ah);
2672 SET_OBJ_SIZE(dev_ops, ib_cq);
2673 SET_OBJ_SIZE(dev_ops, ib_pd);
2674 SET_OBJ_SIZE(dev_ops, ib_srq);
2675 SET_OBJ_SIZE(dev_ops, ib_ucontext);
2676 }
2677 EXPORT_SYMBOL(ib_set_device_ops);
2678
2679 static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
2680 [RDMA_NL_LS_OP_RESOLVE] = {
2681 .doit = ib_nl_handle_resolve_resp,
2682 .flags = RDMA_NL_ADMIN_PERM,
2683 },
2684 [RDMA_NL_LS_OP_SET_TIMEOUT] = {
2685 .doit = ib_nl_handle_set_timeout,
2686 .flags = RDMA_NL_ADMIN_PERM,
2687 },
2688 [RDMA_NL_LS_OP_IP_RESOLVE] = {
2689 .doit = ib_nl_handle_ip_res_resp,
2690 .flags = RDMA_NL_ADMIN_PERM,
2691 },
2692 };
2693
2694 static int __init ib_core_init(void)
2695 {
2696 int ret;
2697
2698 ib_wq = alloc_workqueue("infiniband", 0, 0);
2699 if (!ib_wq)
2700 return -ENOMEM;
2701
2702 ib_comp_wq = alloc_workqueue("ib-comp-wq",
2703 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2704 if (!ib_comp_wq) {
2705 ret = -ENOMEM;
2706 goto err;
2707 }
2708
2709 ib_comp_unbound_wq =
2710 alloc_workqueue("ib-comp-unb-wq",
2711 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM |
2712 WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE);
2713 if (!ib_comp_unbound_wq) {
2714 ret = -ENOMEM;
2715 goto err_comp;
2716 }
2717
2718 ret = class_register(&ib_class);
2719 if (ret) {
2720 pr_warn("Couldn't create InfiniBand device class\n");
2721 goto err_comp_unbound;
2722 }
2723
2724 rdma_nl_init();
2725
2726 ret = addr_init();
2727 if (ret) {
2728 pr_warn("Could't init IB address resolution\n");
2729 goto err_ibnl;
2730 }
2731
2732 ret = ib_mad_init();
2733 if (ret) {
2734 pr_warn("Couldn't init IB MAD\n");
2735 goto err_addr;
2736 }
2737
2738 ret = ib_sa_init();
2739 if (ret) {
2740 pr_warn("Couldn't init SA\n");
2741 goto err_mad;
2742 }
2743
2744 ret = register_blocking_lsm_notifier(&ibdev_lsm_nb);
2745 if (ret) {
2746 pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
2747 goto err_sa;
2748 }
2749
2750 ret = register_pernet_device(&rdma_dev_net_ops);
2751 if (ret) {
2752 pr_warn("Couldn't init compat dev. ret %d\n", ret);
2753 goto err_compat;
2754 }
2755
2756 nldev_init();
2757 rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
2758 roce_gid_mgmt_init();
2759
2760 return 0;
2761
2762 err_compat:
2763 unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
2764 err_sa:
2765 ib_sa_cleanup();
2766 err_mad:
2767 ib_mad_cleanup();
2768 err_addr:
2769 addr_cleanup();
2770 err_ibnl:
2771 class_unregister(&ib_class);
2772 err_comp_unbound:
2773 destroy_workqueue(ib_comp_unbound_wq);
2774 err_comp:
2775 destroy_workqueue(ib_comp_wq);
2776 err:
2777 destroy_workqueue(ib_wq);
2778 return ret;
2779 }
2780
2781 static void __exit ib_core_cleanup(void)
2782 {
2783 roce_gid_mgmt_cleanup();
2784 nldev_exit();
2785 rdma_nl_unregister(RDMA_NL_LS);
2786 unregister_pernet_device(&rdma_dev_net_ops);
2787 unregister_blocking_lsm_notifier(&ibdev_lsm_nb);
2788 ib_sa_cleanup();
2789 ib_mad_cleanup();
2790 addr_cleanup();
2791 rdma_nl_exit();
2792 class_unregister(&ib_class);
2793 destroy_workqueue(ib_comp_unbound_wq);
2794 destroy_workqueue(ib_comp_wq);
2795
2796 destroy_workqueue(ib_wq);
2797 flush_workqueue(system_unbound_wq);
2798 WARN_ON(!xa_empty(&clients));
2799 WARN_ON(!xa_empty(&devices));
2800 }
2801
2802 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
2803
2804
2805
2806
2807 fs_initcall(ib_core_init);
2808 module_exit(ib_core_cleanup);