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