RDMA/core: Fix reported speed and width
[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                           struct uverbs_attr_bundle *attrs)
45 {
46         return rdma_destroy_ah_user((struct ib_ah *)uobject->object,
47                                     RDMA_DESTROY_AH_SLEEPABLE,
48                                     &attrs->driver_udata);
49 }
50
51 static int uverbs_free_flow(struct ib_uobject *uobject,
52                             enum rdma_remove_reason why,
53                             struct uverbs_attr_bundle *attrs)
54 {
55         struct ib_flow *flow = (struct ib_flow *)uobject->object;
56         struct ib_uflow_object *uflow =
57                 container_of(uobject, struct ib_uflow_object, uobject);
58         struct ib_qp *qp = flow->qp;
59         int ret;
60
61         ret = flow->device->ops.destroy_flow(flow);
62         if (!ret) {
63                 if (qp)
64                         atomic_dec(&qp->usecnt);
65                 ib_uverbs_flow_resources_free(uflow->resources);
66         }
67
68         return ret;
69 }
70
71 static int uverbs_free_mw(struct ib_uobject *uobject,
72                           enum rdma_remove_reason why,
73                           struct uverbs_attr_bundle *attrs)
74 {
75         return uverbs_dealloc_mw((struct ib_mw *)uobject->object);
76 }
77
78 static int uverbs_free_rwq_ind_tbl(struct ib_uobject *uobject,
79                                    enum rdma_remove_reason why,
80                                    struct uverbs_attr_bundle *attrs)
81 {
82         struct ib_rwq_ind_table *rwq_ind_tbl = uobject->object;
83         struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl;
84         int ret;
85
86         ret = ib_destroy_rwq_ind_table(rwq_ind_tbl);
87         if (ib_is_destroy_retryable(ret, why, uobject))
88                 return ret;
89
90         kfree(ind_tbl);
91         return ret;
92 }
93
94 static int uverbs_free_xrcd(struct ib_uobject *uobject,
95                             enum rdma_remove_reason why,
96                             struct uverbs_attr_bundle *attrs)
97 {
98         struct ib_xrcd *xrcd = uobject->object;
99         struct ib_uxrcd_object *uxrcd =
100                 container_of(uobject, struct ib_uxrcd_object, uobject);
101         int ret;
102
103         ret = ib_destroy_usecnt(&uxrcd->refcnt, why, uobject);
104         if (ret)
105                 return ret;
106
107         mutex_lock(&attrs->ufile->device->xrcd_tree_mutex);
108         ret = ib_uverbs_dealloc_xrcd(uobject, xrcd, why, attrs);
109         mutex_unlock(&attrs->ufile->device->xrcd_tree_mutex);
110
111         return ret;
112 }
113
114 static int uverbs_free_pd(struct ib_uobject *uobject,
115                           enum rdma_remove_reason why,
116                           struct uverbs_attr_bundle *attrs)
117 {
118         struct ib_pd *pd = uobject->object;
119         int ret;
120
121         ret = ib_destroy_usecnt(&pd->usecnt, why, uobject);
122         if (ret)
123                 return ret;
124
125         ib_dealloc_pd_user(pd, &attrs->driver_udata);
126         return 0;
127 }
128
129 void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue)
130 {
131         struct ib_uverbs_event *entry, *tmp;
132
133         spin_lock_irq(&event_queue->lock);
134         /*
135          * The user must ensure that no new items are added to the event_list
136          * once is_closed is set.
137          */
138         event_queue->is_closed = 1;
139         spin_unlock_irq(&event_queue->lock);
140         wake_up_interruptible(&event_queue->poll_wait);
141         kill_fasync(&event_queue->async_queue, SIGIO, POLL_IN);
142
143         spin_lock_irq(&event_queue->lock);
144         list_for_each_entry_safe(entry, tmp, &event_queue->event_list, list) {
145                 if (entry->counter)
146                         list_del(&entry->obj_list);
147                 list_del(&entry->list);
148                 kfree(entry);
149         }
150         spin_unlock_irq(&event_queue->lock);
151 }
152
153 static int
154 uverbs_completion_event_file_destroy_uobj(struct ib_uobject *uobj,
155                                           enum rdma_remove_reason why)
156 {
157         struct ib_uverbs_completion_event_file *file =
158                 container_of(uobj, struct ib_uverbs_completion_event_file,
159                              uobj);
160
161         ib_uverbs_free_event_queue(&file->ev_queue);
162         return 0;
163 }
164
165 int uverbs_destroy_def_handler(struct uverbs_attr_bundle *attrs)
166 {
167         return 0;
168 }
169 EXPORT_SYMBOL(uverbs_destroy_def_handler);
170
171 DECLARE_UVERBS_NAMED_OBJECT(
172         UVERBS_OBJECT_COMP_CHANNEL,
173         UVERBS_TYPE_ALLOC_FD(sizeof(struct ib_uverbs_completion_event_file),
174                              uverbs_completion_event_file_destroy_uobj,
175                              &uverbs_event_fops,
176                              "[infinibandevent]",
177                              O_RDONLY));
178
179 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
180         UVERBS_METHOD_MW_DESTROY,
181         UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_MW_HANDLE,
182                         UVERBS_OBJECT_MW,
183                         UVERBS_ACCESS_DESTROY,
184                         UA_MANDATORY));
185
186 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_MW,
187                             UVERBS_TYPE_ALLOC_IDR(uverbs_free_mw),
188                             &UVERBS_METHOD(UVERBS_METHOD_MW_DESTROY));
189
190 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
191         UVERBS_METHOD_AH_DESTROY,
192         UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_AH_HANDLE,
193                         UVERBS_OBJECT_AH,
194                         UVERBS_ACCESS_DESTROY,
195                         UA_MANDATORY));
196
197 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_AH,
198                             UVERBS_TYPE_ALLOC_IDR(uverbs_free_ah),
199                             &UVERBS_METHOD(UVERBS_METHOD_AH_DESTROY));
200
201 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
202         UVERBS_METHOD_FLOW_DESTROY,
203         UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_FLOW_HANDLE,
204                         UVERBS_OBJECT_FLOW,
205                         UVERBS_ACCESS_DESTROY,
206                         UA_MANDATORY));
207
208 DECLARE_UVERBS_NAMED_OBJECT(
209         UVERBS_OBJECT_FLOW,
210         UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uflow_object),
211                                  uverbs_free_flow),
212                             &UVERBS_METHOD(UVERBS_METHOD_FLOW_DESTROY));
213
214 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
215         UVERBS_METHOD_RWQ_IND_TBL_DESTROY,
216         UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_RWQ_IND_TBL_HANDLE,
217                         UVERBS_OBJECT_RWQ_IND_TBL,
218                         UVERBS_ACCESS_DESTROY,
219                         UA_MANDATORY));
220
221 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_RWQ_IND_TBL,
222                             UVERBS_TYPE_ALLOC_IDR(uverbs_free_rwq_ind_tbl),
223                             &UVERBS_METHOD(UVERBS_METHOD_RWQ_IND_TBL_DESTROY));
224
225 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
226         UVERBS_METHOD_XRCD_DESTROY,
227         UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_XRCD_HANDLE,
228                         UVERBS_OBJECT_XRCD,
229                         UVERBS_ACCESS_DESTROY,
230                         UA_MANDATORY));
231
232 DECLARE_UVERBS_NAMED_OBJECT(
233         UVERBS_OBJECT_XRCD,
234         UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uxrcd_object),
235                                  uverbs_free_xrcd),
236                             &UVERBS_METHOD(UVERBS_METHOD_XRCD_DESTROY));
237
238 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
239         UVERBS_METHOD_PD_DESTROY,
240         UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_PD_HANDLE,
241                         UVERBS_OBJECT_PD,
242                         UVERBS_ACCESS_DESTROY,
243                         UA_MANDATORY));
244
245 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_PD,
246                             UVERBS_TYPE_ALLOC_IDR(uverbs_free_pd),
247                             &UVERBS_METHOD(UVERBS_METHOD_PD_DESTROY));
248
249 const struct uapi_definition uverbs_def_obj_intf[] = {
250         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_PD,
251                                       UAPI_DEF_OBJ_NEEDS_FN(dealloc_pd)),
252         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_COMP_CHANNEL,
253                                       UAPI_DEF_OBJ_NEEDS_FN(dealloc_pd)),
254         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_AH,
255                                       UAPI_DEF_OBJ_NEEDS_FN(destroy_ah)),
256         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_MW,
257                                       UAPI_DEF_OBJ_NEEDS_FN(dealloc_mw)),
258         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_FLOW,
259                                       UAPI_DEF_OBJ_NEEDS_FN(destroy_flow)),
260         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(
261                 UVERBS_OBJECT_RWQ_IND_TBL,
262                 UAPI_DEF_OBJ_NEEDS_FN(destroy_rwq_ind_table)),
263         UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_XRCD,
264                                       UAPI_DEF_OBJ_NEEDS_FN(dealloc_xrcd)),
265         {}
266 };