Merge tag 'gvt-next-2018-09-04' of https://github.com/intel/gvt-linux into drm-intel...
[linux-2.6-microblaze.git] / drivers / infiniband / core / uverbs_std_types.c
1 /*
2  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
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"
40 #include "uverbs.h"
41
42 static int uverbs_free_ah(struct ib_uobject *uobject,
43                           enum rdma_remove_reason why)
44 {
45         return rdma_destroy_ah((struct ib_ah *)uobject->object);
46 }
47
48 static int uverbs_free_flow(struct ib_uobject *uobject,
49                             enum rdma_remove_reason why)
50 {
51         int ret;
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);
55
56         ret = ib_destroy_flow(flow);
57         if (!ret)
58                 ib_uverbs_flow_resources_free(uflow->resources);
59
60         return ret;
61 }
62
63 static int uverbs_free_mw(struct ib_uobject *uobject,
64                           enum rdma_remove_reason why)
65 {
66         return uverbs_dealloc_mw((struct ib_mw *)uobject->object);
67 }
68
69 static int uverbs_free_qp(struct ib_uobject *uobject,
70                           enum rdma_remove_reason why)
71 {
72         struct ib_qp *qp = uobject->object;
73         struct ib_uqp_object *uqp =
74                 container_of(uobject, struct ib_uqp_object, uevent.uobject);
75         int ret;
76
77         if (why == RDMA_REMOVE_DESTROY) {
78                 if (!list_empty(&uqp->mcast_list))
79                         return -EBUSY;
80         } else if (qp == qp->real_qp) {
81                 ib_uverbs_detach_umcast(qp, uqp);
82         }
83
84         ret = ib_destroy_qp(qp);
85         if (ret && why == RDMA_REMOVE_DESTROY)
86                 return ret;
87
88         if (uqp->uxrcd)
89                 atomic_dec(&uqp->uxrcd->refcnt);
90
91         ib_uverbs_release_uevent(uobject->context->ufile, &uqp->uevent);
92         return ret;
93 }
94
95 static int uverbs_free_rwq_ind_tbl(struct ib_uobject *uobject,
96                                    enum rdma_remove_reason why)
97 {
98         struct ib_rwq_ind_table *rwq_ind_tbl = uobject->object;
99         struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
100         int ret;
101
102         ret = ib_destroy_rwq_ind_table(rwq_ind_tbl);
103         if (!ret || why != RDMA_REMOVE_DESTROY)
104                 kfree(ind_tbl);
105         return ret;
106 }
107
108 static int uverbs_free_wq(struct ib_uobject *uobject,
109                           enum rdma_remove_reason why)
110 {
111         struct ib_wq *wq = uobject->object;
112         struct ib_uwq_object *uwq =
113                 container_of(uobject, struct ib_uwq_object, uevent.uobject);
114         int ret;
115
116         ret = ib_destroy_wq(wq);
117         if (!ret || why != RDMA_REMOVE_DESTROY)
118                 ib_uverbs_release_uevent(uobject->context->ufile, &uwq->uevent);
119         return ret;
120 }
121
122 static int uverbs_free_srq(struct ib_uobject *uobject,
123                            enum rdma_remove_reason why)
124 {
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;
129         int ret;
130
131         ret = ib_destroy_srq(srq);
132
133         if (ret && why == RDMA_REMOVE_DESTROY)
134                 return ret;
135
136         if (srq_type == IB_SRQT_XRC) {
137                 struct ib_usrq_object *us =
138                         container_of(uevent, struct ib_usrq_object, uevent);
139
140                 atomic_dec(&us->uxrcd->refcnt);
141         }
142
143         ib_uverbs_release_uevent(uobject->context->ufile, uevent);
144         return ret;
145 }
146
147 static int uverbs_free_xrcd(struct ib_uobject *uobject,
148                             enum rdma_remove_reason why)
149 {
150         struct ib_xrcd *xrcd = uobject->object;
151         struct ib_uxrcd_object *uxrcd =
152                 container_of(uobject, struct ib_uxrcd_object, uobject);
153         int ret;
154
155         mutex_lock(&uobject->context->ufile->device->xrcd_tree_mutex);
156         if (why == RDMA_REMOVE_DESTROY && atomic_read(&uxrcd->refcnt))
157                 ret = -EBUSY;
158         else
159                 ret = ib_uverbs_dealloc_xrcd(uobject->context->ufile->device,
160                                              xrcd, why);
161         mutex_unlock(&uobject->context->ufile->device->xrcd_tree_mutex);
162
163         return ret;
164 }
165
166 static int uverbs_free_pd(struct ib_uobject *uobject,
167                           enum rdma_remove_reason why)
168 {
169         struct ib_pd *pd = uobject->object;
170
171         if (why == RDMA_REMOVE_DESTROY && atomic_read(&pd->usecnt))
172                 return -EBUSY;
173
174         ib_dealloc_pd((struct ib_pd *)uobject->object);
175         return 0;
176 }
177
178 static int uverbs_hot_unplug_completion_event_file(struct ib_uobject_file *uobj_file,
179                                                    enum rdma_remove_reason why)
180 {
181         struct ib_uverbs_completion_event_file *comp_event_file =
182                 container_of(uobj_file, struct ib_uverbs_completion_event_file,
183                              uobj_file);
184         struct ib_uverbs_event_queue *event_queue = &comp_event_file->ev_queue;
185
186         spin_lock_irq(&event_queue->lock);
187         event_queue->is_closed = 1;
188         spin_unlock_irq(&event_queue->lock);
189
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);
193         }
194         return 0;
195 };
196
197 int uverbs_destroy_def_handler(struct ib_device *ib_dev,
198                                struct ib_uverbs_file *file,
199                                struct uverbs_attr_bundle *attrs)
200 {
201         return 0;
202 }
203
204 /*
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
207  * spec.
208  */
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));
215
216 void create_udata(struct uverbs_attr_bundle *ctx, struct ib_udata *udata)
217 {
218         /*
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.
222          */
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);
227
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;
232                 else
233                         udata->inbuf = u64_to_user_ptr(uhw_in->ptr_attr.data);
234         } else {
235                 udata->inbuf = NULL;
236                 udata->inlen = 0;
237         }
238
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;
242         } else {
243                 udata->outbuf = NULL;
244                 udata->outlen = 0;
245         }
246 }
247
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,
252                                                   &uverbs_event_fops,
253                                                   "[infinibandevent]", O_RDONLY));
254
255 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_QP,
256                             &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uqp_object), 0,
257                                                       uverbs_free_qp));
258
259 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MW,
260                             &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_mw));
261
262 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_SRQ,
263                             &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_usrq_object), 0,
264                                                       uverbs_free_srq));
265
266 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_AH,
267                             &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_ah));
268
269 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_FLOW,
270                             &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uflow_object),
271                                                       0, uverbs_free_flow));
272
273 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_WQ,
274                             &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uwq_object), 0,
275                                                       uverbs_free_wq));
276
277 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL,
278                             &UVERBS_TYPE_ALLOC_IDR(0, uverbs_free_rwq_ind_tbl));
279
280 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_XRCD,
281                             &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uxrcd_object), 0,
282                                                       uverbs_free_xrcd));
283
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));
287
288 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DEVICE, NULL);
289
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));
307
308 const struct uverbs_object_tree_def *uverbs_default_get_objects(void)
309 {
310         return &uverbs_default_objects;
311 }
312 EXPORT_SYMBOL_GPL(uverbs_default_get_objects);