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_core.h"
35 #include "uverbs.h"
36 
37 static int uverbs_free_cq(struct ib_uobject *uobject,
38 			  enum rdma_remove_reason why)
39 {
40 	struct ib_cq *cq = uobject->object;
41 	struct ib_uverbs_event_queue *ev_queue = cq->cq_context;
42 	struct ib_ucq_object *ucq =
43 		container_of(uobject, struct ib_ucq_object, uobject);
44 	int ret;
45 
46 	ret = ib_destroy_cq(cq);
47 	if (!ret || why != RDMA_REMOVE_DESTROY)
48 		ib_uverbs_release_ucq(uobject->context->ufile, ev_queue ?
49 				      container_of(ev_queue,
50 						   struct ib_uverbs_completion_event_file,
51 						   ev_queue) : NULL,
52 				      ucq);
53 	return ret;
54 }
55 
56 static int UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE)(struct ib_device *ib_dev,
57 						   struct ib_uverbs_file *file,
58 						   struct uverbs_attr_bundle *attrs)
59 {
60 	struct ib_ucontext *ucontext = file->ucontext;
61 	struct ib_ucq_object           *obj;
62 	struct ib_udata uhw;
63 	int ret;
64 	u64 user_handle;
65 	struct ib_cq_init_attr attr = {};
66 	struct ib_cq                   *cq;
67 	struct ib_uverbs_completion_event_file    *ev_file = NULL;
68 	struct ib_uobject *ev_file_uobj;
69 
70 	if (!(ib_dev->uverbs_cmd_mask & 1ULL << IB_USER_VERBS_CMD_CREATE_CQ))
71 		return -EOPNOTSUPP;
72 
73 	ret = uverbs_copy_from(&attr.comp_vector, attrs,
74 			       UVERBS_ATTR_CREATE_CQ_COMP_VECTOR);
75 	if (!ret)
76 		ret = uverbs_copy_from(&attr.cqe, attrs,
77 				       UVERBS_ATTR_CREATE_CQ_CQE);
78 	if (!ret)
79 		ret = uverbs_copy_from(&user_handle, attrs,
80 				       UVERBS_ATTR_CREATE_CQ_USER_HANDLE);
81 	if (ret)
82 		return ret;
83 
84 	/* Optional param, if it doesn't exist, we get -ENOENT and skip it */
85 	if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&attr.flags, attrs,
86 						UVERBS_ATTR_CREATE_CQ_FLAGS)))
87 		return -EFAULT;
88 
89 	ev_file_uobj = uverbs_attr_get_uobject(attrs, UVERBS_ATTR_CREATE_CQ_COMP_CHANNEL);
90 	if (!IS_ERR(ev_file_uobj)) {
91 		ev_file = container_of(ev_file_uobj,
92 				       struct ib_uverbs_completion_event_file,
93 				       uobj_file.uobj);
94 		uverbs_uobject_get(ev_file_uobj);
95 	}
96 
97 	if (attr.comp_vector >= ucontext->ufile->device->num_comp_vectors) {
98 		ret = -EINVAL;
99 		goto err_event_file;
100 	}
101 
102 	obj = container_of(uverbs_attr_get_uobject(attrs,
103 						   UVERBS_ATTR_CREATE_CQ_HANDLE),
104 			   typeof(*obj), uobject);
105 	obj->uverbs_file	   = ucontext->ufile;
106 	obj->comp_events_reported  = 0;
107 	obj->async_events_reported = 0;
108 	INIT_LIST_HEAD(&obj->comp_list);
109 	INIT_LIST_HEAD(&obj->async_list);
110 
111 	/* Temporary, only until drivers get the new uverbs_attr_bundle */
112 	create_udata(attrs, &uhw);
113 
114 	cq = ib_dev->create_cq(ib_dev, &attr, ucontext, &uhw);
115 	if (IS_ERR(cq)) {
116 		ret = PTR_ERR(cq);
117 		goto err_event_file;
118 	}
119 
120 	cq->device        = ib_dev;
121 	cq->uobject       = &obj->uobject;
122 	cq->comp_handler  = ib_uverbs_comp_handler;
123 	cq->event_handler = ib_uverbs_cq_event_handler;
124 	cq->cq_context    = ev_file ? &ev_file->ev_queue : NULL;
125 	obj->uobject.object = cq;
126 	obj->uobject.user_handle = user_handle;
127 	atomic_set(&cq->usecnt, 0);
128 	cq->res.type = RDMA_RESTRACK_CQ;
129 	rdma_restrack_add(&cq->res);
130 
131 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_CREATE_CQ_RESP_CQE, &cq->cqe,
132 			     sizeof(cq->cqe));
133 	if (ret)
134 		goto err_cq;
135 
136 	return 0;
137 err_cq:
138 	ib_destroy_cq(cq);
139 
140 err_event_file:
141 	if (ev_file)
142 		uverbs_uobject_put(ev_file_uobj);
143 	return ret;
144 };
145 
146 static DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_CQ_CREATE,
147 	&UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_CQ_HANDLE, UVERBS_OBJECT_CQ,
148 			 UVERBS_ACCESS_NEW,
149 			 UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
150 	&UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_CQE,
151 			    UVERBS_ATTR_TYPE(u32),
152 			    UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
153 	&UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_USER_HANDLE,
154 			    UVERBS_ATTR_TYPE(u64),
155 			    UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
156 	&UVERBS_ATTR_FD(UVERBS_ATTR_CREATE_CQ_COMP_CHANNEL,
157 			UVERBS_OBJECT_COMP_CHANNEL,
158 			UVERBS_ACCESS_READ),
159 	&UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_COMP_VECTOR, UVERBS_ATTR_TYPE(u32),
160 			    UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
161 	&UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_FLAGS, UVERBS_ATTR_TYPE(u32)),
162 	&UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_CREATE_CQ_RESP_CQE, UVERBS_ATTR_TYPE(u32),
163 			     UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
164 	&uverbs_uhw_compat_in, &uverbs_uhw_compat_out);
165 
166 static int UVERBS_HANDLER(UVERBS_METHOD_CQ_DESTROY)(struct ib_device *ib_dev,
167 						    struct ib_uverbs_file *file,
168 						    struct uverbs_attr_bundle *attrs)
169 {
170 	struct ib_uobject *uobj =
171 		uverbs_attr_get_uobject(attrs, UVERBS_ATTR_DESTROY_CQ_HANDLE);
172 	struct ib_uverbs_destroy_cq_resp resp;
173 	struct ib_ucq_object *obj;
174 	int ret;
175 
176 	if (IS_ERR(uobj))
177 		return PTR_ERR(uobj);
178 
179 	obj = container_of(uobj, struct ib_ucq_object, uobject);
180 
181 	if (!(ib_dev->uverbs_cmd_mask & 1ULL << IB_USER_VERBS_CMD_DESTROY_CQ))
182 		return -EOPNOTSUPP;
183 
184 	ret = rdma_explicit_destroy(uobj);
185 	if (ret)
186 		return ret;
187 
188 	resp.comp_events_reported  = obj->comp_events_reported;
189 	resp.async_events_reported = obj->async_events_reported;
190 
191 	return uverbs_copy_to(attrs, UVERBS_ATTR_DESTROY_CQ_RESP, &resp,
192 			      sizeof(resp));
193 }
194 
195 static DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_CQ_DESTROY,
196 	&UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_CQ_HANDLE, UVERBS_OBJECT_CQ,
197 			 UVERBS_ACCESS_DESTROY,
198 			 UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
199 	&UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_DESTROY_CQ_RESP,
200 			     UVERBS_ATTR_TYPE(struct ib_uverbs_destroy_cq_resp),
201 			     UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
202 
203 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_CQ,
204 			    &UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_ucq_object), 0,
205 						      uverbs_free_cq),
206 #if IS_ENABLED(CONFIG_INFINIBAND_EXP_LEGACY_VERBS_NEW_UAPI)
207 			    &UVERBS_METHOD(UVERBS_METHOD_CQ_CREATE),
208 			    &UVERBS_METHOD(UVERBS_METHOD_CQ_DESTROY)
209 #endif
210 			   );
211 
212