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