]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/infiniband/core/device.c
ebc0b0e58eca2d69e65046a10da31555de825fea
[linux.git] / drivers / infiniband / core / device.c
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 <net/netns/generic.h>
43 #include <linux/security.h>
44 #include <linux/notifier.h>
45 #include <linux/hashtable.h>
46 #include <rdma/rdma_netlink.h>
47 #include <rdma/ib_addr.h>
48 #include <rdma/ib_cache.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  * Each of the three rwsem locks (devices, clients, client_data) protects the
64  * xarray of the same name. Specifically it allows the caller to assert that
65  * the MARK will/will not be changing under the lock, and for devices and
66  * clients, that the value in the xarray is still a valid pointer. Change of
67  * the MARK is linked to the object state, so holding the lock and testing the
68  * MARK also asserts that the contained object is in a certain state.
69  *
70  * This is used to build a two stage register/unregister flow where objects
71  * can continue to be in the xarray even though they are still in progress to
72  * register/unregister.
73  *
74  * The xarray itself provides additional locking, and restartable iteration,
75  * which is also relied on.
76  *
77  * Locks should not be nested, with the exception of client_data, which is
78  * allowed to nest under the read side of the other two locks.
79  *
80  * The devices_rwsem also protects the device name list, any change or
81  * assignment of device name must also hold the write side to guarantee unique
82  * names.
83  */
84
85 /*
86  * devices contains devices that have had their names assigned. The
87  * devices may not be registered. Users that care about the registration
88  * status need to call ib_device_try_get() on the device to ensure it is
89  * registered, and keep it registered, for the required duration.
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 LIST_HEAD(client_list);
97 #define CLIENT_REGISTERED XA_MARK_1
98 static DEFINE_XARRAY_FLAGS(clients, XA_FLAGS_ALLOC);
99 static DECLARE_RWSEM(clients_rwsem);
100
101 /*
102  * If client_data is registered then the corresponding client must also still
103  * be registered.
104  */
105 #define CLIENT_DATA_REGISTERED XA_MARK_1
106
107 /**
108  * struct rdma_dev_net - rdma net namespace metadata for a net
109  * @net:        Pointer to owner net namespace
110  * @id:         xarray id to identify the net namespace.
111  */
112 struct rdma_dev_net {
113         possible_net_t net;
114         u32 id;
115 };
116
117 static unsigned int rdma_dev_net_id;
118
119 /*
120  * A list of net namespaces is maintained in an xarray. This is necessary
121  * because we can't get the locking right using the existing net ns list. We
122  * would require a init_net callback after the list is updated.
123  */
124 static DEFINE_XARRAY_FLAGS(rdma_nets, XA_FLAGS_ALLOC);
125 /*
126  * rwsem to protect accessing the rdma_nets xarray entries.
127  */
128 static DECLARE_RWSEM(rdma_nets_rwsem);
129
130 static bool ib_devices_shared_netns = true;
131 module_param_named(netns_mode, ib_devices_shared_netns, bool, 0444);
132 MODULE_PARM_DESC(netns_mode,
133                  "Share device among net namespaces; default=1 (shared)");
134 /*
135  * xarray has this behavior where it won't iterate over NULL values stored in
136  * allocated arrays.  So we need our own iterator to see all values stored in
137  * the array. This does the same thing as xa_for_each except that it also
138  * returns NULL valued entries if the array is allocating. Simplified to only
139  * work on simple xarrays.
140  */
141 static void *xan_find_marked(struct xarray *xa, unsigned long *indexp,
142                              xa_mark_t filter)
143 {
144         XA_STATE(xas, xa, *indexp);
145         void *entry;
146
147         rcu_read_lock();
148         do {
149                 entry = xas_find_marked(&xas, ULONG_MAX, filter);
150                 if (xa_is_zero(entry))
151                         break;
152         } while (xas_retry(&xas, entry));
153         rcu_read_unlock();
154
155         if (entry) {
156                 *indexp = xas.xa_index;
157                 if (xa_is_zero(entry))
158                         return NULL;
159                 return entry;
160         }
161         return XA_ERROR(-ENOENT);
162 }
163 #define xan_for_each_marked(xa, index, entry, filter)                          \
164         for (index = 0, entry = xan_find_marked(xa, &(index), filter);         \
165              !xa_is_err(entry);                                                \
166              (index)++, entry = xan_find_marked(xa, &(index), filter))
167
168 /* RCU hash table mapping netdevice pointers to struct ib_port_data */
169 static DEFINE_SPINLOCK(ndev_hash_lock);
170 static DECLARE_HASHTABLE(ndev_hash, 5);
171
172 static void free_netdevs(struct ib_device *ib_dev);
173 static void ib_unregister_work(struct work_struct *work);
174 static void __ib_unregister_device(struct ib_device *device);
175 static int ib_security_change(struct notifier_block *nb, unsigned long event,
176                               void *lsm_data);
177 static void ib_policy_change_task(struct work_struct *work);
178 static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
179
180 static struct notifier_block ibdev_lsm_nb = {
181         .notifier_call = ib_security_change,
182 };
183
184 /* Pointer to the RCU head at the start of the ib_port_data array */
185 struct ib_port_data_rcu {
186         struct rcu_head rcu_head;
187         struct ib_port_data pdata[];
188 };
189
190 static int ib_device_check_mandatory(struct ib_device *device)
191 {
192 #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x }
193         static const struct {
194                 size_t offset;
195                 char  *name;
196         } mandatory_table[] = {
197                 IB_MANDATORY_FUNC(query_device),
198                 IB_MANDATORY_FUNC(query_port),
199                 IB_MANDATORY_FUNC(query_pkey),
200                 IB_MANDATORY_FUNC(alloc_pd),
201                 IB_MANDATORY_FUNC(dealloc_pd),
202                 IB_MANDATORY_FUNC(create_qp),
203                 IB_MANDATORY_FUNC(modify_qp),
204                 IB_MANDATORY_FUNC(destroy_qp),
205                 IB_MANDATORY_FUNC(post_send),
206                 IB_MANDATORY_FUNC(post_recv),
207                 IB_MANDATORY_FUNC(create_cq),
208                 IB_MANDATORY_FUNC(destroy_cq),
209                 IB_MANDATORY_FUNC(poll_cq),
210                 IB_MANDATORY_FUNC(req_notify_cq),
211                 IB_MANDATORY_FUNC(get_dma_mr),
212                 IB_MANDATORY_FUNC(dereg_mr),
213                 IB_MANDATORY_FUNC(get_port_immutable)
214         };
215         int i;
216
217         device->kverbs_provider = true;
218         for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
219                 if (!*(void **) ((void *) &device->ops +
220                                  mandatory_table[i].offset)) {
221                         device->kverbs_provider = false;
222                         break;
223                 }
224         }
225
226         return 0;
227 }
228
229 /*
230  * Caller must perform ib_device_put() to return the device reference count
231  * when ib_device_get_by_index() returns valid device pointer.
232  */
233 struct ib_device *ib_device_get_by_index(u32 index)
234 {
235         struct ib_device *device;
236
237         down_read(&devices_rwsem);
238         device = xa_load(&devices, index);
239         if (device) {
240                 if (!ib_device_try_get(device))
241                         device = NULL;
242         }
243         up_read(&devices_rwsem);
244         return device;
245 }
246
247 /**
248  * ib_device_put - Release IB device reference
249  * @device: device whose reference to be released
250  *
251  * ib_device_put() releases reference to the IB device to allow it to be
252  * unregistered and eventually free.
253  */
254 void ib_device_put(struct ib_device *device)
255 {
256         if (refcount_dec_and_test(&device->refcount))
257                 complete(&device->unreg_completion);
258 }
259 EXPORT_SYMBOL(ib_device_put);
260
261 static struct ib_device *__ib_device_get_by_name(const char *name)
262 {
263         struct ib_device *device;
264         unsigned long index;
265
266         xa_for_each (&devices, index, device)
267                 if (!strcmp(name, dev_name(&device->dev)))
268                         return device;
269
270         return NULL;
271 }
272
273 /**
274  * ib_device_get_by_name - Find an IB device by name
275  * @name: The name to look for
276  * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all)
277  *
278  * Find and hold an ib_device by its name. The caller must call
279  * ib_device_put() on the returned pointer.
280  */
281 struct ib_device *ib_device_get_by_name(const char *name,
282                                         enum rdma_driver_id driver_id)
283 {
284         struct ib_device *device;
285
286         down_read(&devices_rwsem);
287         device = __ib_device_get_by_name(name);
288         if (device && driver_id != RDMA_DRIVER_UNKNOWN &&
289             device->driver_id != driver_id)
290                 device = NULL;
291
292         if (device) {
293                 if (!ib_device_try_get(device))
294                         device = NULL;
295         }
296         up_read(&devices_rwsem);
297         return device;
298 }
299 EXPORT_SYMBOL(ib_device_get_by_name);
300
301 static int rename_compat_devs(struct ib_device *device)
302 {
303         struct ib_core_device *cdev;
304         unsigned long index;
305         int ret = 0;
306
307         mutex_lock(&device->compat_devs_mutex);
308         xa_for_each (&device->compat_devs, index, cdev) {
309                 ret = device_rename(&cdev->dev, dev_name(&device->dev));
310                 if (ret) {
311                         dev_warn(&cdev->dev,
312                                  "Fail to rename compatdev to new name %s\n",
313                                  dev_name(&device->dev));
314                         break;
315                 }
316         }
317         mutex_unlock(&device->compat_devs_mutex);
318         return ret;
319 }
320
321 int ib_device_rename(struct ib_device *ibdev, const char *name)
322 {
323         int ret;
324
325         down_write(&devices_rwsem);
326         if (!strcmp(name, dev_name(&ibdev->dev))) {
327                 ret = 0;
328                 goto out;
329         }
330
331         if (__ib_device_get_by_name(name)) {
332                 ret = -EEXIST;
333                 goto out;
334         }
335
336         ret = device_rename(&ibdev->dev, name);
337         if (ret)
338                 goto out;
339         strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
340         ret = rename_compat_devs(ibdev);
341 out:
342         up_write(&devices_rwsem);
343         return ret;
344 }
345
346 static int alloc_name(struct ib_device *ibdev, const char *name)
347 {
348         struct ib_device *device;
349         unsigned long index;
350         struct ida inuse;
351         int rc;
352         int i;
353
354         lockdep_assert_held_exclusive(&devices_rwsem);
355         ida_init(&inuse);
356         xa_for_each (&devices, index, device) {
357                 char buf[IB_DEVICE_NAME_MAX];
358
359                 if (sscanf(dev_name(&device->dev), name, &i) != 1)
360                         continue;
361                 if (i < 0 || i >= INT_MAX)
362                         continue;
363                 snprintf(buf, sizeof buf, name, i);
364                 if (strcmp(buf, dev_name(&device->dev)) != 0)
365                         continue;
366
367                 rc = ida_alloc_range(&inuse, i, i, GFP_KERNEL);
368                 if (rc < 0)
369                         goto out;
370         }
371
372         rc = ida_alloc(&inuse, GFP_KERNEL);
373         if (rc < 0)
374                 goto out;
375
376         rc = dev_set_name(&ibdev->dev, name, rc);
377 out:
378         ida_destroy(&inuse);
379         return rc;
380 }
381
382 static void ib_device_release(struct device *device)
383 {
384         struct ib_device *dev = container_of(device, struct ib_device, dev);
385
386         free_netdevs(dev);
387         WARN_ON(refcount_read(&dev->refcount));
388         ib_cache_release_one(dev);
389         ib_security_release_port_pkey_list(dev);
390         xa_destroy(&dev->compat_devs);
391         xa_destroy(&dev->client_data);
392         if (dev->port_data)
393                 kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu,
394                                        pdata[0]),
395                           rcu_head);
396         kfree_rcu(dev, rcu_head);
397 }
398
399 static int ib_device_uevent(struct device *device,
400                             struct kobj_uevent_env *env)
401 {
402         if (add_uevent_var(env, "NAME=%s", dev_name(device)))
403                 return -ENOMEM;
404
405         /*
406          * It would be nice to pass the node GUID with the event...
407          */
408
409         return 0;
410 }
411
412 static const void *net_namespace(struct device *d)
413 {
414         struct ib_core_device *coredev =
415                         container_of(d, struct ib_core_device, dev);
416
417         return read_pnet(&coredev->rdma_net);
418 }
419
420 static struct class ib_class = {
421         .name    = "infiniband",
422         .dev_release = ib_device_release,
423         .dev_uevent = ib_device_uevent,
424         .ns_type = &net_ns_type_operations,
425         .namespace = net_namespace,
426 };
427
428 static void rdma_init_coredev(struct ib_core_device *coredev,
429                               struct ib_device *dev, struct net *net)
430 {
431         /* This BUILD_BUG_ON is intended to catch layout change
432          * of union of ib_core_device and device.
433          * dev must be the first element as ib_core and providers
434          * driver uses it. Adding anything in ib_core_device before
435          * device will break this assumption.
436          */
437         BUILD_BUG_ON(offsetof(struct ib_device, coredev.dev) !=
438                      offsetof(struct ib_device, dev));
439
440         coredev->dev.class = &ib_class;
441         coredev->dev.groups = dev->groups;
442         device_initialize(&coredev->dev);
443         coredev->owner = dev;
444         INIT_LIST_HEAD(&coredev->port_list);
445         write_pnet(&coredev->rdma_net, net);
446 }
447
448 /**
449  * _ib_alloc_device - allocate an IB device struct
450  * @size:size of structure to allocate
451  *
452  * Low-level drivers should use ib_alloc_device() to allocate &struct
453  * ib_device.  @size is the size of the structure to be allocated,
454  * including any private data used by the low-level driver.
455  * ib_dealloc_device() must be used to free structures allocated with
456  * ib_alloc_device().
457  */
458 struct ib_device *_ib_alloc_device(size_t size)
459 {
460         struct ib_device *device;
461
462         if (WARN_ON(size < sizeof(struct ib_device)))
463                 return NULL;
464
465         device = kzalloc(size, GFP_KERNEL);
466         if (!device)
467                 return NULL;
468
469         if (rdma_restrack_init(device)) {
470                 kfree(device);
471                 return NULL;
472         }
473
474         device->groups[0] = &ib_dev_attr_group;
475         rdma_init_coredev(&device->coredev, device, &init_net);
476
477         INIT_LIST_HEAD(&device->event_handler_list);
478         spin_lock_init(&device->event_handler_lock);
479         mutex_init(&device->unregistration_lock);
480         /*
481          * client_data needs to be alloc because we don't want our mark to be
482          * destroyed if the user stores NULL in the client data.
483          */
484         xa_init_flags(&device->client_data, XA_FLAGS_ALLOC);
485         init_rwsem(&device->client_data_rwsem);
486         xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC);
487         mutex_init(&device->compat_devs_mutex);
488         init_completion(&device->unreg_completion);
489         INIT_WORK(&device->unregistration_work, ib_unregister_work);
490
491         return device;
492 }
493 EXPORT_SYMBOL(_ib_alloc_device);
494
495 /**
496  * ib_dealloc_device - free an IB device struct
497  * @device:structure to free
498  *
499  * Free a structure allocated with ib_alloc_device().
500  */
501 void ib_dealloc_device(struct ib_device *device)
502 {
503         if (device->ops.dealloc_driver)
504                 device->ops.dealloc_driver(device);
505
506         /*
507          * ib_unregister_driver() requires all devices to remain in the xarray
508          * while their ops are callable. The last op we call is dealloc_driver
509          * above.  This is needed to create a fence on op callbacks prior to
510          * allowing the driver module to unload.
511          */
512         down_write(&devices_rwsem);
513         if (xa_load(&devices, device->index) == device)
514                 xa_erase(&devices, device->index);
515         up_write(&devices_rwsem);
516
517         /* Expedite releasing netdev references */
518         free_netdevs(device);
519
520         WARN_ON(!xa_empty(&device->compat_devs));
521         WARN_ON(!xa_empty(&device->client_data));
522         WARN_ON(refcount_read(&device->refcount));
523         rdma_restrack_clean(device);
524         /* Balances with device_initialize */
525         put_device(&device->dev);
526 }
527 EXPORT_SYMBOL(ib_dealloc_device);
528
529 /*
530  * add_client_context() and remove_client_context() must be safe against
531  * parallel calls on the same device - registration/unregistration of both the
532  * device and client can be occurring in parallel.
533  *
534  * The routines need to be a fence, any caller must not return until the add
535  * or remove is fully completed.
536  */
537 static int add_client_context(struct ib_device *device,
538                               struct ib_client *client)
539 {
540         int ret = 0;
541
542         if (!device->kverbs_provider && !client->no_kverbs_req)
543                 return 0;
544
545         down_write(&device->client_data_rwsem);
546         /*
547          * Another caller to add_client_context got here first and has already
548          * completely initialized context.
549          */
550         if (xa_get_mark(&device->client_data, client->client_id,
551                     CLIENT_DATA_REGISTERED))
552                 goto out;
553
554         ret = xa_err(xa_store(&device->client_data, client->client_id, NULL,
555                               GFP_KERNEL));
556         if (ret)
557                 goto out;
558         downgrade_write(&device->client_data_rwsem);
559         if (client->add)
560                 client->add(device);
561
562         /* Readers shall not see a client until add has been completed */
563         xa_set_mark(&device->client_data, client->client_id,
564                     CLIENT_DATA_REGISTERED);
565         up_read(&device->client_data_rwsem);
566         return 0;
567
568 out:
569         up_write(&device->client_data_rwsem);
570         return ret;
571 }
572
573 static void remove_client_context(struct ib_device *device,
574                                   unsigned int client_id)
575 {
576         struct ib_client *client;
577         void *client_data;
578
579         down_write(&device->client_data_rwsem);
580         if (!xa_get_mark(&device->client_data, client_id,
581                          CLIENT_DATA_REGISTERED)) {
582                 up_write(&device->client_data_rwsem);
583                 return;
584         }
585         client_data = xa_load(&device->client_data, client_id);
586         xa_clear_mark(&device->client_data, client_id, CLIENT_DATA_REGISTERED);
587         client = xa_load(&clients, client_id);
588         downgrade_write(&device->client_data_rwsem);
589
590         /*
591          * Notice we cannot be holding any exclusive locks when calling the
592          * remove callback as the remove callback can recurse back into any
593          * public functions in this module and thus try for any locks those
594          * functions take.
595          *
596          * For this reason clients and drivers should not call the
597          * unregistration functions will holdling any locks.
598          *
599          * It tempting to drop the client_data_rwsem too, but this is required
600          * to ensure that unregister_client does not return until all clients
601          * are completely unregistered, which is required to avoid module
602          * unloading races.
603          */
604         if (client->remove)
605                 client->remove(device, client_data);
606
607         xa_erase(&device->client_data, client_id);
608         up_read(&device->client_data_rwsem);
609 }
610
611 static int alloc_port_data(struct ib_device *device)
612 {
613         struct ib_port_data_rcu *pdata_rcu;
614         unsigned int port;
615
616         if (device->port_data)
617                 return 0;
618
619         /* This can only be called once the physical port range is defined */
620         if (WARN_ON(!device->phys_port_cnt))
621                 return -EINVAL;
622
623         /*
624          * device->port_data is indexed directly by the port number to make
625          * access to this data as efficient as possible.
626          *
627          * Therefore port_data is declared as a 1 based array with potential
628          * empty slots at the beginning.
629          */
630         pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata,
631                                         rdma_end_port(device) + 1),
632                             GFP_KERNEL);
633         if (!pdata_rcu)
634                 return -ENOMEM;
635         /*
636          * The rcu_head is put in front of the port data array and the stored
637          * pointer is adjusted since we never need to see that member until
638          * kfree_rcu.
639          */
640         device->port_data = pdata_rcu->pdata;
641
642         rdma_for_each_port (device, port) {
643                 struct ib_port_data *pdata = &device->port_data[port];
644
645                 pdata->ib_dev = device;
646                 spin_lock_init(&pdata->pkey_list_lock);
647                 INIT_LIST_HEAD(&pdata->pkey_list);
648                 spin_lock_init(&pdata->netdev_lock);
649                 INIT_HLIST_NODE(&pdata->ndev_hash_link);
650         }
651         return 0;
652 }
653
654 static int verify_immutable(const struct ib_device *dev, u8 port)
655 {
656         return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
657                             rdma_max_mad_size(dev, port) != 0);
658 }
659
660 static int setup_port_data(struct ib_device *device)
661 {
662         unsigned int port;
663         int ret;
664
665         ret = alloc_port_data(device);
666         if (ret)
667                 return ret;
668
669         rdma_for_each_port (device, port) {
670                 struct ib_port_data *pdata = &device->port_data[port];
671
672                 ret = device->ops.get_port_immutable(device, port,
673                                                      &pdata->immutable);
674                 if (ret)
675                         return ret;
676
677                 if (verify_immutable(device, port))
678                         return -EINVAL;
679         }
680         return 0;
681 }
682
683 void ib_get_device_fw_str(struct ib_device *dev, char *str)
684 {
685         if (dev->ops.get_dev_fw_str)
686                 dev->ops.get_dev_fw_str(dev, str);
687         else
688                 str[0] = '\0';
689 }
690 EXPORT_SYMBOL(ib_get_device_fw_str);
691
692 static void ib_policy_change_task(struct work_struct *work)
693 {
694         struct ib_device *dev;
695         unsigned long index;
696
697         down_read(&devices_rwsem);
698         xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
699                 unsigned int i;
700
701                 rdma_for_each_port (dev, i) {
702                         u64 sp;
703                         int ret = ib_get_cached_subnet_prefix(dev,
704                                                               i,
705                                                               &sp);
706
707                         WARN_ONCE(ret,
708                                   "ib_get_cached_subnet_prefix err: %d, this should never happen here\n",
709                                   ret);
710                         if (!ret)
711                                 ib_security_cache_change(dev, i, sp);
712                 }
713         }
714         up_read(&devices_rwsem);
715 }
716
717 static int ib_security_change(struct notifier_block *nb, unsigned long event,
718                               void *lsm_data)
719 {
720         if (event != LSM_POLICY_CHANGE)
721                 return NOTIFY_DONE;
722
723         schedule_work(&ib_policy_change_work);
724         ib_mad_agent_security_change();
725
726         return NOTIFY_OK;
727 }
728
729 static void compatdev_release(struct device *dev)
730 {
731         struct ib_core_device *cdev =
732                 container_of(dev, struct ib_core_device, dev);
733
734         kfree(cdev);
735 }
736
737 static int add_one_compat_dev(struct ib_device *device,
738                               struct rdma_dev_net *rnet)
739 {
740         struct ib_core_device *cdev;
741         int ret;
742
743         if (!ib_devices_shared_netns)
744                 return 0;
745
746         /*
747          * Create and add compat device in all namespaces other than where it
748          * is currently bound to.
749          */
750         if (net_eq(read_pnet(&rnet->net),
751                    read_pnet(&device->coredev.rdma_net)))
752                 return 0;
753
754         /*
755          * The first of init_net() or ib_register_device() to take the
756          * compat_devs_mutex wins and gets to add the device. Others will wait
757          * for completion here.
758          */
759         mutex_lock(&device->compat_devs_mutex);
760         cdev = xa_load(&device->compat_devs, rnet->id);
761         if (cdev) {
762                 ret = 0;
763                 goto done;
764         }
765         ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL);
766         if (ret)
767                 goto done;
768
769         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
770         if (!cdev) {
771                 ret = -ENOMEM;
772                 goto cdev_err;
773         }
774
775         cdev->dev.parent = device->dev.parent;
776         rdma_init_coredev(cdev, device, read_pnet(&rnet->net));
777         cdev->dev.release = compatdev_release;
778         dev_set_name(&cdev->dev, "%s", dev_name(&device->dev));
779
780         ret = device_add(&cdev->dev);
781         if (ret)
782                 goto add_err;
783         ret = ib_setup_port_attrs(cdev, false);
784         if (ret)
785                 goto port_err;
786
787         ret = xa_err(xa_store(&device->compat_devs, rnet->id,
788                               cdev, GFP_KERNEL));
789         if (ret)
790                 goto insert_err;
791
792         mutex_unlock(&device->compat_devs_mutex);
793         return 0;
794
795 insert_err:
796         ib_free_port_attrs(cdev);
797 port_err:
798         device_del(&cdev->dev);
799 add_err:
800         put_device(&cdev->dev);
801 cdev_err:
802         xa_release(&device->compat_devs, rnet->id);
803 done:
804         mutex_unlock(&device->compat_devs_mutex);
805         return ret;
806 }
807
808 static void remove_one_compat_dev(struct ib_device *device, u32 id)
809 {
810         struct ib_core_device *cdev;
811
812         mutex_lock(&device->compat_devs_mutex);
813         cdev = xa_erase(&device->compat_devs, id);
814         mutex_unlock(&device->compat_devs_mutex);
815         if (cdev) {
816                 ib_free_port_attrs(cdev);
817                 device_del(&cdev->dev);
818                 put_device(&cdev->dev);
819         }
820 }
821
822 static void remove_compat_devs(struct ib_device *device)
823 {
824         struct ib_core_device *cdev;
825         unsigned long index;
826
827         xa_for_each (&device->compat_devs, index, cdev)
828                 remove_one_compat_dev(device, index);
829 }
830
831 static int add_compat_devs(struct ib_device *device)
832 {
833         struct rdma_dev_net *rnet;
834         unsigned long index;
835         int ret = 0;
836
837         down_read(&rdma_nets_rwsem);
838         xa_for_each (&rdma_nets, index, rnet) {
839                 ret = add_one_compat_dev(device, rnet);
840                 if (ret)
841                         break;
842         }
843         up_read(&rdma_nets_rwsem);
844         return ret;
845 }
846
847 static void rdma_dev_exit_net(struct net *net)
848 {
849         struct rdma_dev_net *rnet = net_generic(net, rdma_dev_net_id);
850         struct ib_device *dev;
851         unsigned long index;
852         int ret;
853
854         down_write(&rdma_nets_rwsem);
855         /*
856          * Prevent the ID from being re-used and hide the id from xa_for_each.
857          */
858         ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL));
859         WARN_ON(ret);
860         up_write(&rdma_nets_rwsem);
861
862         down_read(&devices_rwsem);
863         xa_for_each (&devices, index, dev) {
864                 get_device(&dev->dev);
865                 /*
866                  * Release the devices_rwsem so that pontentially blocking
867                  * device_del, doesn't hold the devices_rwsem for too long.
868                  */
869                 up_read(&devices_rwsem);
870
871                 remove_one_compat_dev(dev, rnet->id);
872
873                 put_device(&dev->dev);
874                 down_read(&devices_rwsem);
875         }
876         up_read(&devices_rwsem);
877
878         xa_erase(&rdma_nets, rnet->id);
879 }
880
881 static __net_init int rdma_dev_init_net(struct net *net)
882 {
883         struct rdma_dev_net *rnet = net_generic(net, rdma_dev_net_id);
884         unsigned long index;
885         struct ib_device *dev;
886         int ret;
887
888         /* No need to create any compat devices in default init_net. */
889         if (net_eq(net, &init_net))
890                 return 0;
891
892         write_pnet(&rnet->net, net);
893
894         ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL);
895         if (ret)
896                 return ret;
897
898         down_read(&devices_rwsem);
899         xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
900                 ret = add_one_compat_dev(dev, rnet);
901                 if (ret)
902                         break;
903         }
904         up_read(&devices_rwsem);
905
906         if (ret)
907                 rdma_dev_exit_net(net);
908
909         return ret;
910 }
911
912 /*
913  * Assign the unique string device name and the unique device index. This is
914  * undone by ib_dealloc_device.
915  */
916 static int assign_name(struct ib_device *device, const char *name)
917 {
918         static u32 last_id;
919         int ret;
920
921         down_write(&devices_rwsem);
922         /* Assign a unique name to the device */
923         if (strchr(name, '%'))
924                 ret = alloc_name(device, name);
925         else
926                 ret = dev_set_name(&device->dev, name);
927         if (ret)
928                 goto out;
929
930         if (__ib_device_get_by_name(dev_name(&device->dev))) {
931                 ret = -ENFILE;
932                 goto out;
933         }
934         strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
935
936         ret = xa_alloc_cyclic(&devices, &device->index, device, xa_limit_31b,
937                         &last_id, GFP_KERNEL);
938         if (ret > 0)
939                 ret = 0;
940
941 out:
942         up_write(&devices_rwsem);
943         return ret;
944 }
945
946 static void setup_dma_device(struct ib_device *device)
947 {
948         struct device *parent = device->dev.parent;
949
950         WARN_ON_ONCE(device->dma_device);
951         if (device->dev.dma_ops) {
952                 /*
953                  * The caller provided custom DMA operations. Copy the
954                  * DMA-related fields that are used by e.g. dma_alloc_coherent()
955                  * into device->dev.
956                  */
957                 device->dma_device = &device->dev;
958                 if (!device->dev.dma_mask) {
959                         if (parent)
960                                 device->dev.dma_mask = parent->dma_mask;
961                         else
962                                 WARN_ON_ONCE(true);
963                 }
964                 if (!device->dev.coherent_dma_mask) {
965                         if (parent)
966                                 device->dev.coherent_dma_mask =
967                                         parent->coherent_dma_mask;
968                         else
969                                 WARN_ON_ONCE(true);
970                 }
971         } else {
972                 /*
973                  * The caller did not provide custom DMA operations. Use the
974                  * DMA mapping operations of the parent device.
975                  */
976                 WARN_ON_ONCE(!parent);
977                 device->dma_device = parent;
978         }
979 }
980
981 /*
982  * setup_device() allocates memory and sets up data that requires calling the
983  * device ops, this is the only reason these actions are not done during
984  * ib_alloc_device. It is undone by ib_dealloc_device().
985  */
986 static int setup_device(struct ib_device *device)
987 {
988         struct ib_udata uhw = {.outlen = 0, .inlen = 0};
989         int ret;
990
991         setup_dma_device(device);
992
993         ret = ib_device_check_mandatory(device);
994         if (ret)
995                 return ret;
996
997         ret = setup_port_data(device);
998         if (ret) {
999                 dev_warn(&device->dev, "Couldn't create per-port data\n");
1000                 return ret;
1001         }
1002
1003         memset(&device->attrs, 0, sizeof(device->attrs));
1004         ret = device->ops.query_device(device, &device->attrs, &uhw);
1005         if (ret) {
1006                 dev_warn(&device->dev,
1007                          "Couldn't query the device attributes\n");
1008                 return ret;
1009         }
1010
1011         return 0;
1012 }
1013
1014 static void disable_device(struct ib_device *device)
1015 {
1016         struct ib_client *client;
1017
1018         WARN_ON(!refcount_read(&device->refcount));
1019
1020         down_write(&devices_rwsem);
1021         xa_clear_mark(&devices, device->index, DEVICE_REGISTERED);
1022         up_write(&devices_rwsem);
1023
1024         down_read(&clients_rwsem);
1025         list_for_each_entry_reverse(client, &client_list, list)
1026                 remove_client_context(device, client->client_id);
1027         up_read(&clients_rwsem);
1028
1029         /* Pairs with refcount_set in enable_device */
1030         ib_device_put(device);
1031         wait_for_completion(&device->unreg_completion);
1032
1033         /*
1034          * compat devices must be removed after device refcount drops to zero.
1035          * Otherwise init_net() may add more compatdevs after removing compat
1036          * devices and before device is disabled.
1037          */
1038         remove_compat_devs(device);
1039
1040         /* Expedite removing unregistered pointers from the hash table */
1041         free_netdevs(device);
1042 }
1043
1044 /*
1045  * An enabled device is visible to all clients and to all the public facing
1046  * APIs that return a device pointer. This always returns with a new get, even
1047  * if it fails.
1048  */
1049 static int enable_device_and_get(struct ib_device *device)
1050 {
1051         struct ib_client *client;
1052         unsigned long index;
1053         int ret = 0;
1054
1055         /*
1056          * One ref belongs to the xa and the other belongs to this
1057          * thread. This is needed to guard against parallel unregistration.
1058          */
1059         refcount_set(&device->refcount, 2);
1060         down_write(&devices_rwsem);
1061         xa_set_mark(&devices, device->index, DEVICE_REGISTERED);
1062
1063         /*
1064          * By using downgrade_write() we ensure that no other thread can clear
1065          * DEVICE_REGISTERED while we are completing the client setup.
1066          */
1067         downgrade_write(&devices_rwsem);
1068
1069         if (device->ops.enable_driver) {
1070                 ret = device->ops.enable_driver(device);
1071                 if (ret)
1072                         goto out;
1073         }
1074
1075         down_read(&clients_rwsem);
1076         xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1077                 ret = add_client_context(device, client);
1078                 if (ret)
1079                         break;
1080         }
1081         up_read(&clients_rwsem);
1082         if (!ret)
1083                 ret = add_compat_devs(device);
1084 out:
1085         up_read(&devices_rwsem);
1086         return ret;
1087 }
1088
1089 /**
1090  * ib_register_device - Register an IB device with IB core
1091  * @device:Device to register
1092  *
1093  * Low-level drivers use ib_register_device() to register their
1094  * devices with the IB core.  All registered clients will receive a
1095  * callback for each device that is added. @device must be allocated
1096  * with ib_alloc_device().
1097  *
1098  * If the driver uses ops.dealloc_driver and calls any ib_unregister_device()
1099  * asynchronously then the device pointer may become freed as soon as this
1100  * function returns.
1101  */
1102 int ib_register_device(struct ib_device *device, const char *name)
1103 {
1104         int ret;
1105
1106         ret = assign_name(device, name);
1107         if (ret)
1108                 return ret;
1109
1110         ret = setup_device(device);
1111         if (ret)
1112                 return ret;
1113
1114         ret = ib_cache_setup_one(device);
1115         if (ret) {
1116                 dev_warn(&device->dev,
1117                          "Couldn't set up InfiniBand P_Key/GID cache\n");
1118                 return ret;
1119         }
1120
1121         ib_device_register_rdmacg(device);
1122
1123         ret = device_add(&device->dev);
1124         if (ret)
1125                 goto cg_cleanup;
1126
1127         ret = ib_device_register_sysfs(device);
1128         if (ret) {
1129                 dev_warn(&device->dev,
1130                          "Couldn't register device with driver model\n");
1131                 goto dev_cleanup;
1132         }
1133
1134         ret = enable_device_and_get(device);
1135         if (ret) {
1136                 void (*dealloc_fn)(struct ib_device *);
1137
1138                 /*
1139                  * If we hit this error flow then we don't want to
1140                  * automatically dealloc the device since the caller is
1141                  * expected to call ib_dealloc_device() after
1142                  * ib_register_device() fails. This is tricky due to the
1143                  * possibility for a parallel unregistration along with this
1144                  * error flow. Since we have a refcount here we know any
1145                  * parallel flow is stopped in disable_device and will see the
1146                  * NULL pointers, causing the responsibility to
1147                  * ib_dealloc_device() to revert back to this thread.
1148                  */
1149                 dealloc_fn = device->ops.dealloc_driver;
1150                 device->ops.dealloc_driver = NULL;
1151                 ib_device_put(device);
1152                 __ib_unregister_device(device);
1153                 device->ops.dealloc_driver = dealloc_fn;
1154                 return ret;
1155         }
1156         ib_device_put(device);
1157
1158         return 0;
1159
1160 dev_cleanup:
1161         device_del(&device->dev);
1162 cg_cleanup:
1163         ib_device_unregister_rdmacg(device);
1164         ib_cache_cleanup_one(device);
1165         return ret;
1166 }
1167 EXPORT_SYMBOL(ib_register_device);
1168
1169 /* Callers must hold a get on the device. */
1170 static void __ib_unregister_device(struct ib_device *ib_dev)
1171 {
1172         /*
1173          * We have a registration lock so that all the calls to unregister are
1174          * fully fenced, once any unregister returns the device is truely
1175          * unregistered even if multiple callers are unregistering it at the
1176          * same time. This also interacts with the registration flow and
1177          * provides sane semantics if register and unregister are racing.
1178          */
1179         mutex_lock(&ib_dev->unregistration_lock);
1180         if (!refcount_read(&ib_dev->refcount))
1181                 goto out;
1182
1183         disable_device(ib_dev);
1184         ib_device_unregister_sysfs(ib_dev);
1185         device_del(&ib_dev->dev);
1186         ib_device_unregister_rdmacg(ib_dev);
1187         ib_cache_cleanup_one(ib_dev);
1188
1189         /*
1190          * Drivers using the new flow may not call ib_dealloc_device except
1191          * in error unwind prior to registration success.
1192          */
1193         if (ib_dev->ops.dealloc_driver) {
1194                 WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1);
1195                 ib_dealloc_device(ib_dev);
1196         }
1197 out:
1198         mutex_unlock(&ib_dev->unregistration_lock);
1199 }
1200
1201 /**
1202  * ib_unregister_device - Unregister an IB device
1203  * @device: The device to unregister
1204  *
1205  * Unregister an IB device.  All clients will receive a remove callback.
1206  *
1207  * Callers should call this routine only once, and protect against races with
1208  * registration. Typically it should only be called as part of a remove
1209  * callback in an implementation of driver core's struct device_driver and
1210  * related.
1211  *
1212  * If ops.dealloc_driver is used then ib_dev will be freed upon return from
1213  * this function.
1214  */
1215 void ib_unregister_device(struct ib_device *ib_dev)
1216 {
1217         get_device(&ib_dev->dev);
1218         __ib_unregister_device(ib_dev);
1219         put_device(&ib_dev->dev);
1220 }
1221 EXPORT_SYMBOL(ib_unregister_device);
1222
1223 /**
1224  * ib_unregister_device_and_put - Unregister a device while holding a 'get'
1225  * device: The device to unregister
1226  *
1227  * This is the same as ib_unregister_device(), except it includes an internal
1228  * ib_device_put() that should match a 'get' obtained by the caller.
1229  *
1230  * It is safe to call this routine concurrently from multiple threads while
1231  * holding the 'get'. When the function returns the device is fully
1232  * unregistered.
1233  *
1234  * Drivers using this flow MUST use the driver_unregister callback to clean up
1235  * their resources associated with the device and dealloc it.
1236  */
1237 void ib_unregister_device_and_put(struct ib_device *ib_dev)
1238 {
1239         WARN_ON(!ib_dev->ops.dealloc_driver);
1240         get_device(&ib_dev->dev);
1241         ib_device_put(ib_dev);
1242         __ib_unregister_device(ib_dev);
1243         put_device(&ib_dev->dev);
1244 }
1245 EXPORT_SYMBOL(ib_unregister_device_and_put);
1246
1247 /**
1248  * ib_unregister_driver - Unregister all IB devices for a driver
1249  * @driver_id: The driver to unregister
1250  *
1251  * This implements a fence for device unregistration. It only returns once all
1252  * devices associated with the driver_id have fully completed their
1253  * unregistration and returned from ib_unregister_device*().
1254  *
1255  * If device's are not yet unregistered it goes ahead and starts unregistering
1256  * them.
1257  *
1258  * This does not block creation of new devices with the given driver_id, that
1259  * is the responsibility of the caller.
1260  */
1261 void ib_unregister_driver(enum rdma_driver_id driver_id)
1262 {
1263         struct ib_device *ib_dev;
1264         unsigned long index;
1265
1266         down_read(&devices_rwsem);
1267         xa_for_each (&devices, index, ib_dev) {
1268                 if (ib_dev->driver_id != driver_id)
1269                         continue;
1270
1271                 get_device(&ib_dev->dev);
1272                 up_read(&devices_rwsem);
1273
1274                 WARN_ON(!ib_dev->ops.dealloc_driver);
1275                 __ib_unregister_device(ib_dev);
1276
1277                 put_device(&ib_dev->dev);
1278                 down_read(&devices_rwsem);
1279         }
1280         up_read(&devices_rwsem);
1281 }
1282 EXPORT_SYMBOL(ib_unregister_driver);
1283
1284 static void ib_unregister_work(struct work_struct *work)
1285 {
1286         struct ib_device *ib_dev =
1287                 container_of(work, struct ib_device, unregistration_work);
1288
1289         __ib_unregister_device(ib_dev);
1290         put_device(&ib_dev->dev);
1291 }
1292
1293 /**
1294  * ib_unregister_device_queued - Unregister a device using a work queue
1295  * device: The device to unregister
1296  *
1297  * This schedules an asynchronous unregistration using a WQ for the device. A
1298  * driver should use this to avoid holding locks while doing unregistration,
1299  * such as holding the RTNL lock.
1300  *
1301  * Drivers using this API must use ib_unregister_driver before module unload
1302  * to ensure that all scheduled unregistrations have completed.
1303  */
1304 void ib_unregister_device_queued(struct ib_device *ib_dev)
1305 {
1306         WARN_ON(!refcount_read(&ib_dev->refcount));
1307         WARN_ON(!ib_dev->ops.dealloc_driver);
1308         get_device(&ib_dev->dev);
1309         if (!queue_work(system_unbound_wq, &ib_dev->unregistration_work))
1310                 put_device(&ib_dev->dev);
1311 }
1312 EXPORT_SYMBOL(ib_unregister_device_queued);
1313
1314 static struct pernet_operations rdma_dev_net_ops = {
1315         .init = rdma_dev_init_net,
1316         .exit = rdma_dev_exit_net,
1317         .id = &rdma_dev_net_id,
1318         .size = sizeof(struct rdma_dev_net),
1319 };
1320
1321 static int assign_client_id(struct ib_client *client)
1322 {
1323         int ret;
1324
1325         down_write(&clients_rwsem);
1326         /*
1327          * The add/remove callbacks must be called in FIFO/LIFO order. To
1328          * achieve this we assign client_ids so they are sorted in
1329          * registration order, and retain a linked list we can reverse iterate
1330          * to get the LIFO order. The extra linked list can go away if xarray
1331          * learns to reverse iterate.
1332          */
1333         if (list_empty(&client_list)) {
1334                 client->client_id = 0;
1335         } else {
1336                 struct ib_client *last;
1337
1338                 last = list_last_entry(&client_list, struct ib_client, list);
1339                 client->client_id = last->client_id + 1;
1340         }
1341         ret = xa_insert(&clients, client->client_id, client, GFP_KERNEL);
1342         if (ret)
1343                 goto out;
1344
1345         xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED);
1346         list_add_tail(&client->list, &client_list);
1347
1348 out:
1349         up_write(&clients_rwsem);
1350         return ret;
1351 }
1352
1353 /**
1354  * ib_register_client - Register an IB client
1355  * @client:Client to register
1356  *
1357  * Upper level users of the IB drivers can use ib_register_client() to
1358  * register callbacks for IB device addition and removal.  When an IB
1359  * device is added, each registered client's add method will be called
1360  * (in the order the clients were registered), and when a device is
1361  * removed, each client's remove method will be called (in the reverse
1362  * order that clients were registered).  In addition, when
1363  * ib_register_client() is called, the client will receive an add
1364  * callback for all devices already registered.
1365  */
1366 int ib_register_client(struct ib_client *client)
1367 {
1368         struct ib_device *device;
1369         unsigned long index;
1370         int ret;
1371
1372         ret = assign_client_id(client);
1373         if (ret)
1374                 return ret;
1375
1376         down_read(&devices_rwsem);
1377         xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) {
1378                 ret = add_client_context(device, client);
1379                 if (ret) {
1380                         up_read(&devices_rwsem);
1381                         ib_unregister_client(client);
1382                         return ret;
1383                 }
1384         }
1385         up_read(&devices_rwsem);
1386         return 0;
1387 }
1388 EXPORT_SYMBOL(ib_register_client);
1389
1390 /**
1391  * ib_unregister_client - Unregister an IB client
1392  * @client:Client to unregister
1393  *
1394  * Upper level users use ib_unregister_client() to remove their client
1395  * registration.  When ib_unregister_client() is called, the client
1396  * will receive a remove callback for each IB device still registered.
1397  *
1398  * This is a full fence, once it returns no client callbacks will be called,
1399  * or are running in another thread.
1400  */
1401 void ib_unregister_client(struct ib_client *client)
1402 {
1403         struct ib_device *device;
1404         unsigned long index;
1405
1406         down_write(&clients_rwsem);
1407         xa_clear_mark(&clients, client->client_id, CLIENT_REGISTERED);
1408         up_write(&clients_rwsem);
1409         /*
1410          * Every device still known must be serialized to make sure we are
1411          * done with the client callbacks before we return.
1412          */
1413         down_read(&devices_rwsem);
1414         xa_for_each (&devices, index, device)
1415                 remove_client_context(device, client->client_id);
1416         up_read(&devices_rwsem);
1417
1418         down_write(&clients_rwsem);
1419         list_del(&client->list);
1420         xa_erase(&clients, client->client_id);
1421         up_write(&clients_rwsem);
1422 }
1423 EXPORT_SYMBOL(ib_unregister_client);
1424
1425 /**
1426  * ib_set_client_data - Set IB client context
1427  * @device:Device to set context for
1428  * @client:Client to set context for
1429  * @data:Context to set
1430  *
1431  * ib_set_client_data() sets client context data that can be retrieved with
1432  * ib_get_client_data(). This can only be called while the client is
1433  * registered to the device, once the ib_client remove() callback returns this
1434  * cannot be called.
1435  */
1436 void ib_set_client_data(struct ib_device *device, struct ib_client *client,
1437                         void *data)
1438 {
1439         void *rc;
1440
1441         if (WARN_ON(IS_ERR(data)))
1442                 data = NULL;
1443
1444         rc = xa_store(&device->client_data, client->client_id, data,
1445                       GFP_KERNEL);
1446         WARN_ON(xa_is_err(rc));
1447 }
1448 EXPORT_SYMBOL(ib_set_client_data);
1449
1450 /**
1451  * ib_register_event_handler - Register an IB event handler
1452  * @event_handler:Handler to register
1453  *
1454  * ib_register_event_handler() registers an event handler that will be
1455  * called back when asynchronous IB events occur (as defined in
1456  * chapter 11 of the InfiniBand Architecture Specification).  This
1457  * callback may occur in interrupt context.
1458  */
1459 void ib_register_event_handler(struct ib_event_handler *event_handler)
1460 {
1461         unsigned long flags;
1462
1463         spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
1464         list_add_tail(&event_handler->list,
1465                       &event_handler->device->event_handler_list);
1466         spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
1467 }
1468 EXPORT_SYMBOL(ib_register_event_handler);
1469
1470 /**
1471  * ib_unregister_event_handler - Unregister an event handler
1472  * @event_handler:Handler to unregister
1473  *
1474  * Unregister an event handler registered with
1475  * ib_register_event_handler().
1476  */
1477 void ib_unregister_event_handler(struct ib_event_handler *event_handler)
1478 {
1479         unsigned long flags;
1480
1481         spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
1482         list_del(&event_handler->list);
1483         spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
1484 }
1485 EXPORT_SYMBOL(ib_unregister_event_handler);
1486
1487 /**
1488  * ib_dispatch_event - Dispatch an asynchronous event
1489  * @event:Event to dispatch
1490  *
1491  * Low-level drivers must call ib_dispatch_event() to dispatch the
1492  * event to all registered event handlers when an asynchronous event
1493  * occurs.
1494  */
1495 void ib_dispatch_event(struct ib_event *event)
1496 {
1497         unsigned long flags;
1498         struct ib_event_handler *handler;
1499
1500         spin_lock_irqsave(&event->device->event_handler_lock, flags);
1501
1502         list_for_each_entry(handler, &event->device->event_handler_list, list)
1503                 handler->handler(handler, event);
1504
1505         spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
1506 }
1507 EXPORT_SYMBOL(ib_dispatch_event);
1508
1509 /**
1510  * ib_query_port - Query IB port attributes
1511  * @device:Device to query
1512  * @port_num:Port number to query
1513  * @port_attr:Port attributes
1514  *
1515  * ib_query_port() returns the attributes of a port through the
1516  * @port_attr pointer.
1517  */
1518 int ib_query_port(struct ib_device *device,
1519                   u8 port_num,
1520                   struct ib_port_attr *port_attr)
1521 {
1522         union ib_gid gid;
1523         int err;
1524
1525         if (!rdma_is_port_valid(device, port_num))
1526                 return -EINVAL;
1527
1528         memset(port_attr, 0, sizeof(*port_attr));
1529         err = device->ops.query_port(device, port_num, port_attr);
1530         if (err || port_attr->subnet_prefix)
1531                 return err;
1532
1533         if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
1534                 return 0;
1535
1536         err = device->ops.query_gid(device, port_num, 0, &gid);
1537         if (err)
1538                 return err;
1539
1540         port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
1541         return 0;
1542 }
1543 EXPORT_SYMBOL(ib_query_port);
1544
1545 static void add_ndev_hash(struct ib_port_data *pdata)
1546 {
1547         unsigned long flags;
1548
1549         might_sleep();
1550
1551         spin_lock_irqsave(&ndev_hash_lock, flags);
1552         if (hash_hashed(&pdata->ndev_hash_link)) {
1553                 hash_del_rcu(&pdata->ndev_hash_link);
1554                 spin_unlock_irqrestore(&ndev_hash_lock, flags);
1555                 /*
1556                  * We cannot do hash_add_rcu after a hash_del_rcu until the
1557                  * grace period
1558                  */
1559                 synchronize_rcu();
1560                 spin_lock_irqsave(&ndev_hash_lock, flags);
1561         }
1562         if (pdata->netdev)
1563                 hash_add_rcu(ndev_hash, &pdata->ndev_hash_link,
1564                              (uintptr_t)pdata->netdev);
1565         spin_unlock_irqrestore(&ndev_hash_lock, flags);
1566 }
1567
1568 /**
1569  * ib_device_set_netdev - Associate the ib_dev with an underlying net_device
1570  * @ib_dev: Device to modify
1571  * @ndev: net_device to affiliate, may be NULL
1572  * @port: IB port the net_device is connected to
1573  *
1574  * Drivers should use this to link the ib_device to a netdev so the netdev
1575  * shows up in interfaces like ib_enum_roce_netdev. Only one netdev may be
1576  * affiliated with any port.
1577  *
1578  * The caller must ensure that the given ndev is not unregistered or
1579  * unregistering, and that either the ib_device is unregistered or
1580  * ib_device_set_netdev() is called with NULL when the ndev sends a
1581  * NETDEV_UNREGISTER event.
1582  */
1583 int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
1584                          unsigned int port)
1585 {
1586         struct net_device *old_ndev;
1587         struct ib_port_data *pdata;
1588         unsigned long flags;
1589         int ret;
1590
1591         /*
1592          * Drivers wish to call this before ib_register_driver, so we have to
1593          * setup the port data early.
1594          */
1595         ret = alloc_port_data(ib_dev);
1596         if (ret)
1597                 return ret;
1598
1599         if (!rdma_is_port_valid(ib_dev, port))
1600                 return -EINVAL;
1601
1602         pdata = &ib_dev->port_data[port];
1603         spin_lock_irqsave(&pdata->netdev_lock, flags);
1604         old_ndev = rcu_dereference_protected(
1605                 pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
1606         if (old_ndev == ndev) {
1607                 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
1608                 return 0;
1609         }
1610
1611         if (ndev)
1612                 dev_hold(ndev);
1613         rcu_assign_pointer(pdata->netdev, ndev);
1614         spin_unlock_irqrestore(&pdata->netdev_lock, flags);
1615
1616         add_ndev_hash(pdata);
1617         if (old_ndev)
1618                 dev_put(old_ndev);
1619
1620         return 0;
1621 }
1622 EXPORT_SYMBOL(ib_device_set_netdev);
1623
1624 static void free_netdevs(struct ib_device *ib_dev)
1625 {
1626         unsigned long flags;
1627         unsigned int port;
1628
1629         rdma_for_each_port (ib_dev, port) {
1630                 struct ib_port_data *pdata = &ib_dev->port_data[port];
1631                 struct net_device *ndev;
1632
1633                 spin_lock_irqsave(&pdata->netdev_lock, flags);
1634                 ndev = rcu_dereference_protected(
1635                         pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
1636                 if (ndev) {
1637                         spin_lock(&ndev_hash_lock);
1638                         hash_del_rcu(&pdata->ndev_hash_link);
1639                         spin_unlock(&ndev_hash_lock);
1640
1641                         /*
1642                          * If this is the last dev_put there is still a
1643                          * synchronize_rcu before the netdev is kfreed, so we
1644                          * can continue to rely on unlocked pointer
1645                          * comparisons after the put
1646                          */
1647                         rcu_assign_pointer(pdata->netdev, NULL);
1648                         dev_put(ndev);
1649                 }
1650                 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
1651         }
1652 }
1653
1654 struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
1655                                         unsigned int port)
1656 {
1657         struct ib_port_data *pdata;
1658         struct net_device *res;
1659
1660         if (!rdma_is_port_valid(ib_dev, port))
1661                 return NULL;
1662
1663         pdata = &ib_dev->port_data[port];
1664
1665         /*
1666          * New drivers should use ib_device_set_netdev() not the legacy
1667          * get_netdev().
1668          */
1669         if (ib_dev->ops.get_netdev)
1670                 res = ib_dev->ops.get_netdev(ib_dev, port);
1671         else {
1672                 spin_lock(&pdata->netdev_lock);
1673                 res = rcu_dereference_protected(
1674                         pdata->netdev, lockdep_is_held(&pdata->netdev_lock));
1675                 if (res)
1676                         dev_hold(res);
1677                 spin_unlock(&pdata->netdev_lock);
1678         }
1679
1680         /*
1681          * If we are starting to unregister expedite things by preventing
1682          * propagation of an unregistering netdev.
1683          */
1684         if (res && res->reg_state != NETREG_REGISTERED) {
1685                 dev_put(res);
1686                 return NULL;
1687         }
1688
1689         return res;
1690 }
1691
1692 /**
1693  * ib_device_get_by_netdev - Find an IB device associated with a netdev
1694  * @ndev: netdev to locate
1695  * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all)
1696  *
1697  * Find and hold an ib_device that is associated with a netdev via
1698  * ib_device_set_netdev(). The caller must call ib_device_put() on the
1699  * returned pointer.
1700  */
1701 struct ib_device *ib_device_get_by_netdev(struct net_device *ndev,
1702                                           enum rdma_driver_id driver_id)
1703 {
1704         struct ib_device *res = NULL;
1705         struct ib_port_data *cur;
1706
1707         rcu_read_lock();
1708         hash_for_each_possible_rcu (ndev_hash, cur, ndev_hash_link,
1709                                     (uintptr_t)ndev) {
1710                 if (rcu_access_pointer(cur->netdev) == ndev &&
1711                     (driver_id == RDMA_DRIVER_UNKNOWN ||
1712                      cur->ib_dev->driver_id == driver_id) &&
1713                     ib_device_try_get(cur->ib_dev)) {
1714                         res = cur->ib_dev;
1715                         break;
1716                 }
1717         }
1718         rcu_read_unlock();
1719
1720         return res;
1721 }
1722 EXPORT_SYMBOL(ib_device_get_by_netdev);
1723
1724 /**
1725  * ib_enum_roce_netdev - enumerate all RoCE ports
1726  * @ib_dev : IB device we want to query
1727  * @filter: Should we call the callback?
1728  * @filter_cookie: Cookie passed to filter
1729  * @cb: Callback to call for each found RoCE ports
1730  * @cookie: Cookie passed back to the callback
1731  *
1732  * Enumerates all of the physical RoCE ports of ib_dev
1733  * which are related to netdevice and calls callback() on each
1734  * device for which filter() function returns non zero.
1735  */
1736 void ib_enum_roce_netdev(struct ib_device *ib_dev,
1737                          roce_netdev_filter filter,
1738                          void *filter_cookie,
1739                          roce_netdev_callback cb,
1740                          void *cookie)
1741 {
1742         unsigned int port;
1743
1744         rdma_for_each_port (ib_dev, port)
1745                 if (rdma_protocol_roce(ib_dev, port)) {
1746                         struct net_device *idev =
1747                                 ib_device_get_netdev(ib_dev, port);
1748
1749                         if (filter(ib_dev, port, idev, filter_cookie))
1750                                 cb(ib_dev, port, idev, cookie);
1751
1752                         if (idev)
1753                                 dev_put(idev);
1754                 }
1755 }
1756
1757 /**
1758  * ib_enum_all_roce_netdevs - enumerate all RoCE devices
1759  * @filter: Should we call the callback?
1760  * @filter_cookie: Cookie passed to filter
1761  * @cb: Callback to call for each found RoCE ports
1762  * @cookie: Cookie passed back to the callback
1763  *
1764  * Enumerates all RoCE devices' physical ports which are related
1765  * to netdevices and calls callback() on each device for which
1766  * filter() function returns non zero.
1767  */
1768 void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
1769                               void *filter_cookie,
1770                               roce_netdev_callback cb,
1771                               void *cookie)
1772 {
1773         struct ib_device *dev;
1774         unsigned long index;
1775
1776         down_read(&devices_rwsem);
1777         xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED)
1778                 ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
1779         up_read(&devices_rwsem);
1780 }
1781
1782 /**
1783  * ib_enum_all_devs - enumerate all ib_devices
1784  * @cb: Callback to call for each found ib_device
1785  *
1786  * Enumerates all ib_devices and calls callback() on each device.
1787  */
1788 int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
1789                      struct netlink_callback *cb)
1790 {
1791         unsigned long index;
1792         struct ib_device *dev;
1793         unsigned int idx = 0;
1794         int ret = 0;
1795
1796         down_read(&devices_rwsem);
1797         xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
1798                 ret = nldev_cb(dev, skb, cb, idx);
1799                 if (ret)
1800                         break;
1801                 idx++;
1802         }
1803         up_read(&devices_rwsem);
1804         return ret;
1805 }
1806
1807 /**
1808  * ib_query_pkey - Get P_Key table entry
1809  * @device:Device to query
1810  * @port_num:Port number to query
1811  * @index:P_Key table index to query
1812  * @pkey:Returned P_Key
1813  *
1814  * ib_query_pkey() fetches the specified P_Key table entry.
1815  */
1816 int ib_query_pkey(struct ib_device *device,
1817                   u8 port_num, u16 index, u16 *pkey)
1818 {
1819         if (!rdma_is_port_valid(device, port_num))
1820                 return -EINVAL;
1821
1822         return device->ops.query_pkey(device, port_num, index, pkey);
1823 }
1824 EXPORT_SYMBOL(ib_query_pkey);
1825
1826 /**
1827  * ib_modify_device - Change IB device attributes
1828  * @device:Device to modify
1829  * @device_modify_mask:Mask of attributes to change
1830  * @device_modify:New attribute values
1831  *
1832  * ib_modify_device() changes a device's attributes as specified by
1833  * the @device_modify_mask and @device_modify structure.
1834  */
1835 int ib_modify_device(struct ib_device *device,
1836                      int device_modify_mask,
1837                      struct ib_device_modify *device_modify)
1838 {
1839         if (!device->ops.modify_device)
1840                 return -ENOSYS;
1841
1842         return device->ops.modify_device(device, device_modify_mask,
1843                                          device_modify);
1844 }
1845 EXPORT_SYMBOL(ib_modify_device);
1846
1847 /**
1848  * ib_modify_port - Modifies the attributes for the specified port.
1849  * @device: The device to modify.
1850  * @port_num: The number of the port to modify.
1851  * @port_modify_mask: Mask used to specify which attributes of the port
1852  *   to change.
1853  * @port_modify: New attribute values for the port.
1854  *
1855  * ib_modify_port() changes a port's attributes as specified by the
1856  * @port_modify_mask and @port_modify structure.
1857  */
1858 int ib_modify_port(struct ib_device *device,
1859                    u8 port_num, int port_modify_mask,
1860                    struct ib_port_modify *port_modify)
1861 {
1862         int rc;
1863
1864         if (!rdma_is_port_valid(device, port_num))
1865                 return -EINVAL;
1866
1867         if (device->ops.modify_port)
1868                 rc = device->ops.modify_port(device, port_num,
1869                                              port_modify_mask,
1870                                              port_modify);
1871         else
1872                 rc = rdma_protocol_roce(device, port_num) ? 0 : -ENOSYS;
1873         return rc;
1874 }
1875 EXPORT_SYMBOL(ib_modify_port);
1876
1877 /**
1878  * ib_find_gid - Returns the port number and GID table index where
1879  *   a specified GID value occurs. Its searches only for IB link layer.
1880  * @device: The device to query.
1881  * @gid: The GID value to search for.
1882  * @port_num: The port number of the device where the GID value was found.
1883  * @index: The index into the GID table where the GID was found.  This
1884  *   parameter may be NULL.
1885  */
1886 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
1887                 u8 *port_num, u16 *index)
1888 {
1889         union ib_gid tmp_gid;
1890         unsigned int port;
1891         int ret, i;
1892
1893         rdma_for_each_port (device, port) {
1894                 if (!rdma_protocol_ib(device, port))
1895                         continue;
1896
1897                 for (i = 0; i < device->port_data[port].immutable.gid_tbl_len;
1898                      ++i) {
1899                         ret = rdma_query_gid(device, port, i, &tmp_gid);
1900                         if (ret)
1901                                 return ret;
1902                         if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
1903                                 *port_num = port;
1904                                 if (index)
1905                                         *index = i;
1906                                 return 0;
1907                         }
1908                 }
1909         }
1910
1911         return -ENOENT;
1912 }
1913 EXPORT_SYMBOL(ib_find_gid);
1914
1915 /**
1916  * ib_find_pkey - Returns the PKey table index where a specified
1917  *   PKey value occurs.
1918  * @device: The device to query.
1919  * @port_num: The port number of the device to search for the PKey.
1920  * @pkey: The PKey value to search for.
1921  * @index: The index into the PKey table where the PKey was found.
1922  */
1923 int ib_find_pkey(struct ib_device *device,
1924                  u8 port_num, u16 pkey, u16 *index)
1925 {
1926         int ret, i;
1927         u16 tmp_pkey;
1928         int partial_ix = -1;
1929
1930         for (i = 0; i < device->port_data[port_num].immutable.pkey_tbl_len;
1931              ++i) {
1932                 ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
1933                 if (ret)
1934                         return ret;
1935                 if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
1936                         /* if there is full-member pkey take it.*/
1937                         if (tmp_pkey & 0x8000) {
1938                                 *index = i;
1939                                 return 0;
1940                         }
1941                         if (partial_ix < 0)
1942                                 partial_ix = i;
1943                 }
1944         }
1945
1946         /*no full-member, if exists take the limited*/
1947         if (partial_ix >= 0) {
1948                 *index = partial_ix;
1949                 return 0;
1950         }
1951         return -ENOENT;
1952 }
1953 EXPORT_SYMBOL(ib_find_pkey);
1954
1955 /**
1956  * ib_get_net_dev_by_params() - Return the appropriate net_dev
1957  * for a received CM request
1958  * @dev:        An RDMA device on which the request has been received.
1959  * @port:       Port number on the RDMA device.
1960  * @pkey:       The Pkey the request came on.
1961  * @gid:        A GID that the net_dev uses to communicate.
1962  * @addr:       Contains the IP address that the request specified as its
1963  *              destination.
1964  *
1965  */
1966 struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
1967                                             u8 port,
1968                                             u16 pkey,
1969                                             const union ib_gid *gid,
1970                                             const struct sockaddr *addr)
1971 {
1972         struct net_device *net_dev = NULL;
1973         unsigned long index;
1974         void *client_data;
1975
1976         if (!rdma_protocol_ib(dev, port))
1977                 return NULL;
1978
1979         /*
1980          * Holding the read side guarantees that the client will not become
1981          * unregistered while we are calling get_net_dev_by_params()
1982          */
1983         down_read(&dev->client_data_rwsem);
1984         xan_for_each_marked (&dev->client_data, index, client_data,
1985                              CLIENT_DATA_REGISTERED) {
1986                 struct ib_client *client = xa_load(&clients, index);
1987
1988                 if (!client || !client->get_net_dev_by_params)
1989                         continue;
1990
1991                 net_dev = client->get_net_dev_by_params(dev, port, pkey, gid,
1992                                                         addr, client_data);
1993                 if (net_dev)
1994                         break;
1995         }
1996         up_read(&dev->client_data_rwsem);
1997
1998         return net_dev;
1999 }
2000 EXPORT_SYMBOL(ib_get_net_dev_by_params);
2001
2002 void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops)
2003 {
2004         struct ib_device_ops *dev_ops = &dev->ops;
2005 #define SET_DEVICE_OP(ptr, name)                                               \
2006         do {                                                                   \
2007                 if (ops->name)                                                 \
2008                         if (!((ptr)->name))                                    \
2009                                 (ptr)->name = ops->name;                       \
2010         } while (0)
2011
2012 #define SET_OBJ_SIZE(ptr, name) SET_DEVICE_OP(ptr, size_##name)
2013
2014         SET_DEVICE_OP(dev_ops, add_gid);
2015         SET_DEVICE_OP(dev_ops, advise_mr);
2016         SET_DEVICE_OP(dev_ops, alloc_dm);
2017         SET_DEVICE_OP(dev_ops, alloc_fmr);
2018         SET_DEVICE_OP(dev_ops, alloc_hw_stats);
2019         SET_DEVICE_OP(dev_ops, alloc_mr);
2020         SET_DEVICE_OP(dev_ops, alloc_mw);
2021         SET_DEVICE_OP(dev_ops, alloc_pd);
2022         SET_DEVICE_OP(dev_ops, alloc_rdma_netdev);
2023         SET_DEVICE_OP(dev_ops, alloc_ucontext);
2024         SET_DEVICE_OP(dev_ops, alloc_xrcd);
2025         SET_DEVICE_OP(dev_ops, attach_mcast);
2026         SET_DEVICE_OP(dev_ops, check_mr_status);
2027         SET_DEVICE_OP(dev_ops, create_ah);
2028         SET_DEVICE_OP(dev_ops, create_counters);
2029         SET_DEVICE_OP(dev_ops, create_cq);
2030         SET_DEVICE_OP(dev_ops, create_flow);
2031         SET_DEVICE_OP(dev_ops, create_flow_action_esp);
2032         SET_DEVICE_OP(dev_ops, create_qp);
2033         SET_DEVICE_OP(dev_ops, create_rwq_ind_table);
2034         SET_DEVICE_OP(dev_ops, create_srq);
2035         SET_DEVICE_OP(dev_ops, create_wq);
2036         SET_DEVICE_OP(dev_ops, dealloc_dm);
2037         SET_DEVICE_OP(dev_ops, dealloc_driver);
2038         SET_DEVICE_OP(dev_ops, dealloc_fmr);
2039         SET_DEVICE_OP(dev_ops, dealloc_mw);
2040         SET_DEVICE_OP(dev_ops, dealloc_pd);
2041         SET_DEVICE_OP(dev_ops, dealloc_ucontext);
2042         SET_DEVICE_OP(dev_ops, dealloc_xrcd);
2043         SET_DEVICE_OP(dev_ops, del_gid);
2044         SET_DEVICE_OP(dev_ops, dereg_mr);
2045         SET_DEVICE_OP(dev_ops, destroy_ah);
2046         SET_DEVICE_OP(dev_ops, destroy_counters);
2047         SET_DEVICE_OP(dev_ops, destroy_cq);
2048         SET_DEVICE_OP(dev_ops, destroy_flow);
2049         SET_DEVICE_OP(dev_ops, destroy_flow_action);
2050         SET_DEVICE_OP(dev_ops, destroy_qp);
2051         SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table);
2052         SET_DEVICE_OP(dev_ops, destroy_srq);
2053         SET_DEVICE_OP(dev_ops, destroy_wq);
2054         SET_DEVICE_OP(dev_ops, detach_mcast);
2055         SET_DEVICE_OP(dev_ops, disassociate_ucontext);
2056         SET_DEVICE_OP(dev_ops, drain_rq);
2057         SET_DEVICE_OP(dev_ops, drain_sq);
2058         SET_DEVICE_OP(dev_ops, enable_driver);
2059         SET_DEVICE_OP(dev_ops, fill_res_entry);
2060         SET_DEVICE_OP(dev_ops, get_dev_fw_str);
2061         SET_DEVICE_OP(dev_ops, get_dma_mr);
2062         SET_DEVICE_OP(dev_ops, get_hw_stats);
2063         SET_DEVICE_OP(dev_ops, get_link_layer);
2064         SET_DEVICE_OP(dev_ops, get_netdev);
2065         SET_DEVICE_OP(dev_ops, get_port_immutable);
2066         SET_DEVICE_OP(dev_ops, get_vector_affinity);
2067         SET_DEVICE_OP(dev_ops, get_vf_config);
2068         SET_DEVICE_OP(dev_ops, get_vf_stats);
2069         SET_DEVICE_OP(dev_ops, init_port);
2070         SET_DEVICE_OP(dev_ops, map_mr_sg);
2071         SET_DEVICE_OP(dev_ops, map_phys_fmr);
2072         SET_DEVICE_OP(dev_ops, mmap);
2073         SET_DEVICE_OP(dev_ops, modify_ah);
2074         SET_DEVICE_OP(dev_ops, modify_cq);
2075         SET_DEVICE_OP(dev_ops, modify_device);
2076         SET_DEVICE_OP(dev_ops, modify_flow_action_esp);
2077         SET_DEVICE_OP(dev_ops, modify_port);
2078         SET_DEVICE_OP(dev_ops, modify_qp);
2079         SET_DEVICE_OP(dev_ops, modify_srq);
2080         SET_DEVICE_OP(dev_ops, modify_wq);
2081         SET_DEVICE_OP(dev_ops, peek_cq);
2082         SET_DEVICE_OP(dev_ops, poll_cq);
2083         SET_DEVICE_OP(dev_ops, post_recv);
2084         SET_DEVICE_OP(dev_ops, post_send);
2085         SET_DEVICE_OP(dev_ops, post_srq_recv);
2086         SET_DEVICE_OP(dev_ops, process_mad);
2087         SET_DEVICE_OP(dev_ops, query_ah);
2088         SET_DEVICE_OP(dev_ops, query_device);
2089         SET_DEVICE_OP(dev_ops, query_gid);
2090         SET_DEVICE_OP(dev_ops, query_pkey);
2091         SET_DEVICE_OP(dev_ops, query_port);
2092         SET_DEVICE_OP(dev_ops, query_qp);
2093         SET_DEVICE_OP(dev_ops, query_srq);
2094         SET_DEVICE_OP(dev_ops, rdma_netdev_get_params);
2095         SET_DEVICE_OP(dev_ops, read_counters);
2096         SET_DEVICE_OP(dev_ops, reg_dm_mr);
2097         SET_DEVICE_OP(dev_ops, reg_user_mr);
2098         SET_DEVICE_OP(dev_ops, req_ncomp_notif);
2099         SET_DEVICE_OP(dev_ops, req_notify_cq);
2100         SET_DEVICE_OP(dev_ops, rereg_user_mr);
2101         SET_DEVICE_OP(dev_ops, resize_cq);
2102         SET_DEVICE_OP(dev_ops, set_vf_guid);
2103         SET_DEVICE_OP(dev_ops, set_vf_link_state);
2104         SET_DEVICE_OP(dev_ops, unmap_fmr);
2105
2106         SET_OBJ_SIZE(dev_ops, ib_pd);
2107         SET_OBJ_SIZE(dev_ops, ib_ucontext);
2108 }
2109 EXPORT_SYMBOL(ib_set_device_ops);
2110
2111 static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
2112         [RDMA_NL_LS_OP_RESOLVE] = {
2113                 .doit = ib_nl_handle_resolve_resp,
2114                 .flags = RDMA_NL_ADMIN_PERM,
2115         },
2116         [RDMA_NL_LS_OP_SET_TIMEOUT] = {
2117                 .doit = ib_nl_handle_set_timeout,
2118                 .flags = RDMA_NL_ADMIN_PERM,
2119         },
2120         [RDMA_NL_LS_OP_IP_RESOLVE] = {
2121                 .doit = ib_nl_handle_ip_res_resp,
2122                 .flags = RDMA_NL_ADMIN_PERM,
2123         },
2124 };
2125
2126 static int __init ib_core_init(void)
2127 {
2128         int ret;
2129
2130         ib_wq = alloc_workqueue("infiniband", 0, 0);
2131         if (!ib_wq)
2132                 return -ENOMEM;
2133
2134         ib_comp_wq = alloc_workqueue("ib-comp-wq",
2135                         WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2136         if (!ib_comp_wq) {
2137                 ret = -ENOMEM;
2138                 goto err;
2139         }
2140
2141         ib_comp_unbound_wq =
2142                 alloc_workqueue("ib-comp-unb-wq",
2143                                 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM |
2144                                 WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE);
2145         if (!ib_comp_unbound_wq) {
2146                 ret = -ENOMEM;
2147                 goto err_comp;
2148         }
2149
2150         ret = class_register(&ib_class);
2151         if (ret) {
2152                 pr_warn("Couldn't create InfiniBand device class\n");
2153                 goto err_comp_unbound;
2154         }
2155
2156         ret = rdma_nl_init();
2157         if (ret) {
2158                 pr_warn("Couldn't init IB netlink interface: err %d\n", ret);
2159                 goto err_sysfs;
2160         }
2161
2162         ret = addr_init();
2163         if (ret) {
2164                 pr_warn("Could't init IB address resolution\n");
2165                 goto err_ibnl;
2166         }
2167
2168         ret = ib_mad_init();
2169         if (ret) {
2170                 pr_warn("Couldn't init IB MAD\n");
2171                 goto err_addr;
2172         }
2173
2174         ret = ib_sa_init();
2175         if (ret) {
2176                 pr_warn("Couldn't init SA\n");
2177                 goto err_mad;
2178         }
2179
2180         ret = register_lsm_notifier(&ibdev_lsm_nb);
2181         if (ret) {
2182                 pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
2183                 goto err_sa;
2184         }
2185
2186         ret = register_pernet_device(&rdma_dev_net_ops);
2187         if (ret) {
2188                 pr_warn("Couldn't init compat dev. ret %d\n", ret);
2189                 goto err_compat;
2190         }
2191
2192         nldev_init();
2193         rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
2194         roce_gid_mgmt_init();
2195
2196         return 0;
2197
2198 err_compat:
2199         unregister_lsm_notifier(&ibdev_lsm_nb);
2200 err_sa:
2201         ib_sa_cleanup();
2202 err_mad:
2203         ib_mad_cleanup();
2204 err_addr:
2205         addr_cleanup();
2206 err_ibnl:
2207         rdma_nl_exit();
2208 err_sysfs:
2209         class_unregister(&ib_class);
2210 err_comp_unbound:
2211         destroy_workqueue(ib_comp_unbound_wq);
2212 err_comp:
2213         destroy_workqueue(ib_comp_wq);
2214 err:
2215         destroy_workqueue(ib_wq);
2216         return ret;
2217 }
2218
2219 static void __exit ib_core_cleanup(void)
2220 {
2221         roce_gid_mgmt_cleanup();
2222         nldev_exit();
2223         rdma_nl_unregister(RDMA_NL_LS);
2224         unregister_pernet_device(&rdma_dev_net_ops);
2225         unregister_lsm_notifier(&ibdev_lsm_nb);
2226         ib_sa_cleanup();
2227         ib_mad_cleanup();
2228         addr_cleanup();
2229         rdma_nl_exit();
2230         class_unregister(&ib_class);
2231         destroy_workqueue(ib_comp_unbound_wq);
2232         destroy_workqueue(ib_comp_wq);
2233         /* Make sure that any pending umem accounting work is done. */
2234         destroy_workqueue(ib_wq);
2235         flush_workqueue(system_unbound_wq);
2236         WARN_ON(!xa_empty(&clients));
2237         WARN_ON(!xa_empty(&devices));
2238 }
2239
2240 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
2241
2242 /* ib core relies on netdev stack to first register net_ns_type_operations
2243  * ns kobject type before ib_core initialization.
2244  */
2245 fs_initcall(ib_core_init);
2246 module_exit(ib_core_cleanup);