2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <rdma/uverbs_std_types.h>
34 #include <rdma/ib_user_verbs.h>
35 #include <rdma/ib_verbs.h>
36 #include <linux/bug.h>
37 #include <linux/file.h>
38 #include <rdma/restrack.h>
39 #include "rdma_core.h"
42 static int uverbs_free_ah(struct ib_uobject *uobject,
43 enum rdma_remove_reason why)
45 return rdma_destroy_ah((struct ib_ah *)uobject->object);
48 static int uverbs_free_flow(struct ib_uobject *uobject,
49 enum rdma_remove_reason why)
52 struct ib_flow *flow = (struct ib_flow *)uobject->object;
53 struct ib_uflow_object *uflow =
54 container_of(uobject, struct ib_uflow_object, uobject);
56 ret = ib_destroy_flow(flow);
58 ib_uverbs_flow_resources_free(uflow->resources);
63 static int uverbs_free_mw(struct ib_uobject *uobject,
64 enum rdma_remove_reason why)
66 return uverbs_dealloc_mw((struct ib_mw *)uobject->object);
69 static int uverbs_free_qp(struct ib_uobject *uobject,
70 enum rdma_remove_reason why)
72 struct ib_qp *qp = uobject->object;
73 struct ib_uqp_object *uqp =
74 container_of(uobject, struct ib_uqp_object, uevent.uobject);
77 if (why == RDMA_REMOVE_DESTROY) {
78 if (!list_empty(&uqp->mcast_list))
80 } else if (qp == qp->real_qp) {
81 ib_uverbs_detach_umcast(qp, uqp);
84 ret = ib_destroy_qp(qp);
85 if (ret && why == RDMA_REMOVE_DESTROY)
89 atomic_dec(&uqp->uxrcd->refcnt);
91 ib_uverbs_release_uevent(uobject->context->ufile, &uqp->uevent);
95 static int uverbs_free_rwq_ind_tbl(struct ib_uobject *uobject,
96 enum rdma_remove_reason why)
98 struct ib_rwq_ind_table *rwq_ind_tbl = uobject->object;
99 struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
102 ret = ib_destroy_rwq_ind_table(rwq_ind_tbl);
103 if (!ret || why != RDMA_REMOVE_DESTROY)
108 static int uverbs_free_wq(struct ib_uobject *uobject,
109 enum rdma_remove_reason why)
111 struct ib_wq *wq = uobject->object;
112 struct ib_uwq_object *uwq =
113 container_of(uobject, struct ib_uwq_object, uevent.uobject);
116 ret = ib_destroy_wq(wq);
117 if (!ret || why != RDMA_REMOVE_DESTROY)
118 ib_uverbs_release_uevent(uobject->context->ufile, &uwq->uevent);
122 static int uverbs_free_srq(struct ib_uobject *uobject,
123 enum rdma_remove_reason why)
125 struct ib_srq *srq = uobject->object;
126 struct ib_uevent_object *uevent =
127 container_of(uobject, struct ib_uevent_object, uobject);
128 enum ib_srq_type srq_type = srq->srq_type;
131 ret = ib_destroy_srq(srq);
133 if (ret && why == RDMA_REMOVE_DESTROY)
136 if (srq_type == IB_SRQT_XRC) {
137 struct ib_usrq_object *us =
138 container_of(uevent, struct ib_usrq_object, uevent);
140 atomic_dec(&us->uxrcd->refcnt);
143 ib_uverbs_release_uevent(uobject->context->ufile, uevent);
147 static int uverbs_free_xrcd(struct ib_uobject *uobject,
148 enum rdma_remove_reason why)
150 struct ib_xrcd *xrcd = uobject->object;
151 struct ib_uxrcd_object *uxrcd =
152 container_of(uobject, struct ib_uxrcd_object, uobject);
155 mutex_lock(&uobject->context->ufile->device->xrcd_tree_mutex);
156 if (why == RDMA_REMOVE_DESTROY && atomic_read(&uxrcd->refcnt))
159 ret = ib_uverbs_dealloc_xrcd(uobject->context->ufile->device,
161 mutex_unlock(&uobject->context->ufile->device->xrcd_tree_mutex);
166 static int uverbs_free_pd(struct ib_uobject *uobject,
167 enum rdma_remove_reason why)
169 struct ib_pd *pd = uobject->object;
171 if (why == RDMA_REMOVE_DESTROY && atomic_read(&pd->usecnt))
174 ib_dealloc_pd((struct ib_pd *)uobject->object);
178 static int uverbs_hot_unplug_completion_event_file(struct ib_uobject_file *uobj_file,
179 enum rdma_remove_reason why)
181 struct ib_uverbs_completion_event_file *comp_event_file =
182 container_of(uobj_file, struct ib_uverbs_completion_event_file,
184 struct ib_uverbs_event_queue *event_queue = &comp_event_file->ev_queue;
186 spin_lock_irq(&event_queue->lock);
187 event_queue->is_closed = 1;
188 spin_unlock_irq(&event_queue->lock);
190 if (why == RDMA_REMOVE_DRIVER_REMOVE) {
191 wake_up_interruptible(&event_queue->poll_wait);
192 kill_fasync(&event_queue->async_queue, SIGIO, POLL_IN);
197 int uverbs_destroy_def_handler(struct ib_device *ib_dev,
198 struct ib_uverbs_file *file,
199 struct uverbs_attr_bundle *attrs)
205 * This spec is used in order to pass information to the hardware driver in a
206 * legacy way. Every verb that could get driver specific data should get this
209 const struct uverbs_attr_def uverbs_uhw_compat_in =
210 UVERBS_ATTR_PTR_IN_SZ(UVERBS_ATTR_UHW_IN, UVERBS_ATTR_SIZE(0, USHRT_MAX),
211 UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO));
212 const struct uverbs_attr_def uverbs_uhw_compat_out =
213 UVERBS_ATTR_PTR_OUT_SZ(UVERBS_ATTR_UHW_OUT, UVERBS_ATTR_SIZE(0, USHRT_MAX),
214 UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO));
216 void create_udata(struct uverbs_attr_bundle *ctx, struct ib_udata *udata)
219 * This is for ease of conversion. The purpose is to convert all drivers
220 * to use uverbs_attr_bundle instead of ib_udata.
221 * Assume attr == 0 is input and attr == 1 is output.
223 const struct uverbs_attr *uhw_in =
224 uverbs_attr_get(ctx, UVERBS_ATTR_UHW_IN);
225 const struct uverbs_attr *uhw_out =
226 uverbs_attr_get(ctx, UVERBS_ATTR_UHW_OUT);
228 if (!IS_ERR(uhw_in)) {
229 udata->inlen = uhw_in->ptr_attr.len;
230 if (uverbs_attr_ptr_is_inline(uhw_in))
231 udata->inbuf = &uhw_in->uattr->data;
233 udata->inbuf = u64_to_user_ptr(uhw_in->ptr_attr.data);
239 if (!IS_ERR(uhw_out)) {
240 udata->outbuf = u64_to_user_ptr(uhw_out->ptr_attr.data);
241 udata->outlen = uhw_out->ptr_attr.len;
243 udata->outbuf = NULL;
248 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_COMP_CHANNEL,
249 &UVERBS_TYPE_ALLOC_FD(0,
250 sizeof(struct ib_uverbs_completion_event_file),
251 uverbs_hot_unplug_completion_event_file,
253 "[infinibandevent]", O_RDONLY));
255 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_QP,
256 &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uqp_object), 0,
259 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MW,
260 &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_mw));
262 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_SRQ,
263 &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_usrq_object), 0,
266 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_AH,
267 &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_ah));
269 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_FLOW,
270 &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uflow_object),
271 0, uverbs_free_flow));
273 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_WQ,
274 &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uwq_object), 0,
277 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL,
278 &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_rwq_ind_tbl));
280 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_XRCD,
281 &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uxrcd_object), 0,
284 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_PD,
285 /* 2 is used in order to free the PD after MRs */
286 &UVERBS_TYPE_ALLOC_IDR(2, uverbs_free_pd));
288 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DEVICE, NULL);
290 static DECLARE_UVERBS_OBJECT_TREE(uverbs_default_objects,
291 &UVERBS_OBJECT(UVERBS_OBJECT_DEVICE),
292 &UVERBS_OBJECT(UVERBS_OBJECT_PD),
293 &UVERBS_OBJECT(UVERBS_OBJECT_MR),
294 &UVERBS_OBJECT(UVERBS_OBJECT_COMP_CHANNEL),
295 &UVERBS_OBJECT(UVERBS_OBJECT_CQ),
296 &UVERBS_OBJECT(UVERBS_OBJECT_QP),
297 &UVERBS_OBJECT(UVERBS_OBJECT_AH),
298 &UVERBS_OBJECT(UVERBS_OBJECT_MW),
299 &UVERBS_OBJECT(UVERBS_OBJECT_SRQ),
300 &UVERBS_OBJECT(UVERBS_OBJECT_FLOW),
301 &UVERBS_OBJECT(UVERBS_OBJECT_WQ),
302 &UVERBS_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL),
303 &UVERBS_OBJECT(UVERBS_OBJECT_XRCD),
304 &UVERBS_OBJECT(UVERBS_OBJECT_FLOW_ACTION),
305 &UVERBS_OBJECT(UVERBS_OBJECT_DM),
306 &UVERBS_OBJECT(UVERBS_OBJECT_COUNTERS));
308 const struct uverbs_object_tree_def *uverbs_default_get_objects(void)
310 return &uverbs_default_objects;
312 EXPORT_SYMBOL_GPL(uverbs_default_get_objects);