1b633e08cSHans Petter Selasky /*
2b633e08cSHans Petter Selasky  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
3b633e08cSHans Petter Selasky  *
4b633e08cSHans Petter Selasky  * This software is available to you under a choice of one of two
5b633e08cSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
6b633e08cSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
7b633e08cSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
8b633e08cSHans Petter Selasky  * OpenIB.org BSD license below:
9b633e08cSHans Petter Selasky  *
10b633e08cSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
11b633e08cSHans Petter Selasky  *     without modification, are permitted provided that the following
12b633e08cSHans Petter Selasky  *     conditions are met:
13b633e08cSHans Petter Selasky  *
14b633e08cSHans Petter Selasky  *      - Redistributions of source code must retain the above
15b633e08cSHans Petter Selasky  *        copyright notice, this list of conditions and the following
16b633e08cSHans Petter Selasky  *        disclaimer.
17b633e08cSHans Petter Selasky  *
18b633e08cSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
19b633e08cSHans Petter Selasky  *        copyright notice, this list of conditions and the following
20b633e08cSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
21b633e08cSHans Petter Selasky  *        provided with the distribution.
22b633e08cSHans Petter Selasky  *
23b633e08cSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24b633e08cSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25b633e08cSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26b633e08cSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27b633e08cSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28b633e08cSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29b633e08cSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30b633e08cSHans Petter Selasky  * SOFTWARE.
31b633e08cSHans Petter Selasky  */
32b633e08cSHans Petter Selasky 
33b633e08cSHans Petter Selasky #include <rdma/uverbs_std_types.h>
34b633e08cSHans Petter Selasky #include "rdma_core.h"
35b633e08cSHans Petter Selasky #include "uverbs.h"
36b633e08cSHans Petter Selasky 
uverbs_free_cq(struct ib_uobject * uobject,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)37b633e08cSHans Petter Selasky static int uverbs_free_cq(struct ib_uobject *uobject,
38b633e08cSHans Petter Selasky 			  enum rdma_remove_reason why,
39b633e08cSHans Petter Selasky 			  struct uverbs_attr_bundle *attrs)
40b633e08cSHans Petter Selasky {
41b633e08cSHans Petter Selasky 	struct ib_cq *cq = uobject->object;
42b633e08cSHans Petter Selasky 	struct ib_uverbs_event_queue *ev_queue = cq->cq_context;
43b633e08cSHans Petter Selasky 	struct ib_ucq_object *ucq =
44b633e08cSHans Petter Selasky 		container_of(uobject, struct ib_ucq_object, uevent.uobject);
45b633e08cSHans Petter Selasky 	int ret;
46b633e08cSHans Petter Selasky 
47b633e08cSHans Petter Selasky 	ret = ib_destroy_cq_user(cq, &attrs->driver_udata);
48b633e08cSHans Petter Selasky 	if (ib_is_destroy_retryable(ret, why, uobject))
49b633e08cSHans Petter Selasky 		return ret;
50b633e08cSHans Petter Selasky 
51b633e08cSHans Petter Selasky 	ib_uverbs_release_ucq(
52b633e08cSHans Petter Selasky 		ev_queue ? container_of(ev_queue,
53b633e08cSHans Petter Selasky 					struct ib_uverbs_completion_event_file,
54b633e08cSHans Petter Selasky 					ev_queue) :
55b633e08cSHans Petter Selasky 			   NULL,
56b633e08cSHans Petter Selasky 		ucq);
57b633e08cSHans Petter Selasky 	return ret;
58b633e08cSHans Petter Selasky }
59b633e08cSHans Petter Selasky 
UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE)60b633e08cSHans Petter Selasky static int UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE)(
61b633e08cSHans Petter Selasky 	struct uverbs_attr_bundle *attrs)
62b633e08cSHans Petter Selasky {
63b633e08cSHans Petter Selasky 	struct ib_ucq_object *obj = container_of(
64b633e08cSHans Petter Selasky 		uverbs_attr_get_uobject(attrs, UVERBS_ATTR_CREATE_CQ_HANDLE),
65b633e08cSHans Petter Selasky 		typeof(*obj), uevent.uobject);
66b633e08cSHans Petter Selasky 	struct ib_device *ib_dev = attrs->context->device;
67b633e08cSHans Petter Selasky 	int ret;
68b633e08cSHans Petter Selasky 	u64 user_handle;
69b633e08cSHans Petter Selasky 	struct ib_cq_init_attr attr = {};
70b633e08cSHans Petter Selasky 	struct ib_cq                   *cq;
71b633e08cSHans Petter Selasky 	struct ib_uverbs_completion_event_file    *ev_file = NULL;
72b633e08cSHans Petter Selasky 	struct ib_uobject *ev_file_uobj;
73b633e08cSHans Petter Selasky 
74b633e08cSHans Petter Selasky 	if (!ib_dev->create_cq || !ib_dev->destroy_cq)
75b633e08cSHans Petter Selasky 		return -EOPNOTSUPP;
76b633e08cSHans Petter Selasky 
77b633e08cSHans Petter Selasky 	ret = uverbs_copy_from(&attr.comp_vector, attrs,
78b633e08cSHans Petter Selasky 			       UVERBS_ATTR_CREATE_CQ_COMP_VECTOR);
79b633e08cSHans Petter Selasky 	if (!ret)
80b633e08cSHans Petter Selasky 		ret = uverbs_copy_from(&attr.cqe, attrs,
81b633e08cSHans Petter Selasky 				       UVERBS_ATTR_CREATE_CQ_CQE);
82b633e08cSHans Petter Selasky 	if (!ret)
83b633e08cSHans Petter Selasky 		ret = uverbs_copy_from(&user_handle, attrs,
84b633e08cSHans Petter Selasky 				       UVERBS_ATTR_CREATE_CQ_USER_HANDLE);
85b633e08cSHans Petter Selasky 	if (ret)
86b633e08cSHans Petter Selasky 		return ret;
87b633e08cSHans Petter Selasky 
88b633e08cSHans Petter Selasky 	ret = uverbs_get_flags32(&attr.flags, attrs,
89b633e08cSHans Petter Selasky 				 UVERBS_ATTR_CREATE_CQ_FLAGS,
90b633e08cSHans Petter Selasky 				 IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION |
91b633e08cSHans Petter Selasky 					 IB_UVERBS_CQ_FLAGS_IGNORE_OVERRUN);
92b633e08cSHans Petter Selasky 	if (ret)
93b633e08cSHans Petter Selasky 		return ret;
94b633e08cSHans Petter Selasky 
95b633e08cSHans Petter Selasky 	ev_file_uobj = uverbs_attr_get_uobject(attrs, UVERBS_ATTR_CREATE_CQ_COMP_CHANNEL);
96b633e08cSHans Petter Selasky 	if (!IS_ERR(ev_file_uobj)) {
97b633e08cSHans Petter Selasky 		ev_file = container_of(ev_file_uobj,
98b633e08cSHans Petter Selasky 				       struct ib_uverbs_completion_event_file,
99b633e08cSHans Petter Selasky 				       uobj);
100b633e08cSHans Petter Selasky 		uverbs_uobject_get(ev_file_uobj);
101b633e08cSHans Petter Selasky 	}
102b633e08cSHans Petter Selasky 
103b633e08cSHans Petter Selasky 	if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) {
104b633e08cSHans Petter Selasky 		ret = -EINVAL;
105b633e08cSHans Petter Selasky 		goto err_event_file;
106b633e08cSHans Petter Selasky 	}
107b633e08cSHans Petter Selasky 
108b633e08cSHans Petter Selasky 	INIT_LIST_HEAD(&obj->comp_list);
109b633e08cSHans Petter Selasky 	INIT_LIST_HEAD(&obj->uevent.event_list);
110b633e08cSHans Petter Selasky 
111b633e08cSHans Petter Selasky 	cq = rdma_zalloc_drv_obj(ib_dev, ib_cq);
112b633e08cSHans Petter Selasky 	if (!cq) {
113b633e08cSHans Petter Selasky 		ret = -ENOMEM;
114b633e08cSHans Petter Selasky 		goto err_event_file;
115b633e08cSHans Petter Selasky 	}
116b633e08cSHans Petter Selasky 
117b633e08cSHans Petter Selasky 	cq->device        = ib_dev;
118b633e08cSHans Petter Selasky 	cq->uobject       = obj;
119b633e08cSHans Petter Selasky 	cq->comp_handler  = ib_uverbs_comp_handler;
120b633e08cSHans Petter Selasky 	cq->event_handler = ib_uverbs_cq_event_handler;
121b633e08cSHans Petter Selasky 	cq->cq_context    = ev_file ? &ev_file->ev_queue : NULL;
122b633e08cSHans Petter Selasky 	atomic_set(&cq->usecnt, 0);
123b633e08cSHans Petter Selasky 
124b633e08cSHans Petter Selasky 	ret = ib_dev->create_cq(cq, &attr, &attrs->driver_udata);
125b633e08cSHans Petter Selasky 	if (ret)
126b633e08cSHans Petter Selasky 		goto err_free;
127b633e08cSHans Petter Selasky 
128b633e08cSHans Petter Selasky 	obj->uevent.uobject.object = cq;
129b633e08cSHans Petter Selasky 	obj->uevent.uobject.user_handle = user_handle;
130b633e08cSHans Petter Selasky 
131b633e08cSHans Petter Selasky 	ret = uverbs_copy_to(attrs, UVERBS_ATTR_CREATE_CQ_RESP_CQE, &cq->cqe,
132b633e08cSHans Petter Selasky 			     sizeof(cq->cqe));
133b633e08cSHans Petter Selasky 	if (ret)
134b633e08cSHans Petter Selasky 		goto err_cq;
135b633e08cSHans Petter Selasky 
136b633e08cSHans Petter Selasky 	return 0;
137b633e08cSHans Petter Selasky err_cq:
138b633e08cSHans Petter Selasky 	ib_destroy_cq_user(cq, uverbs_get_cleared_udata(attrs));
139b633e08cSHans Petter Selasky 	cq = NULL;
140b633e08cSHans Petter Selasky err_free:
141b633e08cSHans Petter Selasky 	kfree(cq);
142b633e08cSHans Petter Selasky err_event_file:
143b633e08cSHans Petter Selasky 	if (ev_file)
144b633e08cSHans Petter Selasky 		uverbs_uobject_put(ev_file_uobj);
145b633e08cSHans Petter Selasky 	return ret;
146b633e08cSHans Petter Selasky };
147b633e08cSHans Petter Selasky 
148b633e08cSHans Petter Selasky DECLARE_UVERBS_NAMED_METHOD(
149b633e08cSHans Petter Selasky 	UVERBS_METHOD_CQ_CREATE,
150b633e08cSHans Petter Selasky 	UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_CQ_HANDLE,
151b633e08cSHans Petter Selasky 			UVERBS_OBJECT_CQ,
152b633e08cSHans Petter Selasky 			UVERBS_ACCESS_NEW,
153b633e08cSHans Petter Selasky 			UA_MANDATORY),
154b633e08cSHans Petter Selasky 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_CQE,
155b633e08cSHans Petter Selasky 			   UVERBS_ATTR_TYPE(u32),
156b633e08cSHans Petter Selasky 			   UA_MANDATORY),
157b633e08cSHans Petter Selasky 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_USER_HANDLE,
158b633e08cSHans Petter Selasky 			   UVERBS_ATTR_TYPE(u64),
159b633e08cSHans Petter Selasky 			   UA_MANDATORY),
160b633e08cSHans Petter Selasky 	UVERBS_ATTR_FD(UVERBS_ATTR_CREATE_CQ_COMP_CHANNEL,
161b633e08cSHans Petter Selasky 		       UVERBS_OBJECT_COMP_CHANNEL,
162b633e08cSHans Petter Selasky 		       UVERBS_ACCESS_READ,
163b633e08cSHans Petter Selasky 		       UA_OPTIONAL),
164b633e08cSHans Petter Selasky 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_COMP_VECTOR,
165b633e08cSHans Petter Selasky 			   UVERBS_ATTR_TYPE(u32),
166b633e08cSHans Petter Selasky 			   UA_MANDATORY),
167b633e08cSHans Petter Selasky 	UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_CREATE_CQ_FLAGS,
168b633e08cSHans Petter Selasky 			     enum ib_uverbs_ex_create_cq_flags),
169b633e08cSHans Petter Selasky 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_CREATE_CQ_RESP_CQE,
170b633e08cSHans Petter Selasky 			    UVERBS_ATTR_TYPE(u32),
171b633e08cSHans Petter Selasky 			    UA_MANDATORY),
172b633e08cSHans Petter Selasky 	UVERBS_ATTR_UHW());
173b633e08cSHans Petter Selasky 
UVERBS_HANDLER(UVERBS_METHOD_CQ_DESTROY)174b633e08cSHans Petter Selasky static int UVERBS_HANDLER(UVERBS_METHOD_CQ_DESTROY)(
175b633e08cSHans Petter Selasky 	struct uverbs_attr_bundle *attrs)
176b633e08cSHans Petter Selasky {
177b633e08cSHans Petter Selasky 	struct ib_uobject *uobj =
178b633e08cSHans Petter Selasky 		uverbs_attr_get_uobject(attrs, UVERBS_ATTR_DESTROY_CQ_HANDLE);
179b633e08cSHans Petter Selasky 	struct ib_ucq_object *obj =
180b633e08cSHans Petter Selasky 		container_of(uobj, struct ib_ucq_object, uevent.uobject);
181b633e08cSHans Petter Selasky 	struct ib_uverbs_destroy_cq_resp resp = {
182b633e08cSHans Petter Selasky 		.comp_events_reported = obj->comp_events_reported,
183b633e08cSHans Petter Selasky 		.async_events_reported = obj->uevent.events_reported
184b633e08cSHans Petter Selasky 	};
185b633e08cSHans Petter Selasky 
186b633e08cSHans Petter Selasky 	return uverbs_copy_to(attrs, UVERBS_ATTR_DESTROY_CQ_RESP, &resp,
187b633e08cSHans Petter Selasky 			      sizeof(resp));
188b633e08cSHans Petter Selasky }
189b633e08cSHans Petter Selasky 
190b633e08cSHans Petter Selasky DECLARE_UVERBS_NAMED_METHOD(
191b633e08cSHans Petter Selasky 	UVERBS_METHOD_CQ_DESTROY,
192b633e08cSHans Petter Selasky 	UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_CQ_HANDLE,
193b633e08cSHans Petter Selasky 			UVERBS_OBJECT_CQ,
194b633e08cSHans Petter Selasky 			UVERBS_ACCESS_DESTROY,
195b633e08cSHans Petter Selasky 			UA_MANDATORY),
196b633e08cSHans Petter Selasky 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_DESTROY_CQ_RESP,
197b633e08cSHans Petter Selasky 			    UVERBS_ATTR_TYPE(struct ib_uverbs_destroy_cq_resp),
198b633e08cSHans Petter Selasky 			    UA_MANDATORY));
199b633e08cSHans Petter Selasky 
200b633e08cSHans Petter Selasky DECLARE_UVERBS_NAMED_OBJECT(
201b633e08cSHans Petter Selasky 	UVERBS_OBJECT_CQ,
202b633e08cSHans Petter Selasky 	UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_ucq_object), uverbs_free_cq),
203b633e08cSHans Petter Selasky 
204b633e08cSHans Petter Selasky #if 1 /* CONFIG_INFINIBAND_EXP_LEGACY_VERBS_NEW_UAPI */
205b633e08cSHans Petter Selasky 	&UVERBS_METHOD(UVERBS_METHOD_CQ_CREATE),
206b633e08cSHans Petter Selasky 	&UVERBS_METHOD(UVERBS_METHOD_CQ_DESTROY)
207b633e08cSHans Petter Selasky #endif
208b633e08cSHans Petter Selasky );
209b633e08cSHans Petter Selasky 
210b633e08cSHans Petter Selasky const struct uapi_definition uverbs_def_obj_cq[] = {
211b633e08cSHans Petter Selasky 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_CQ,
212b633e08cSHans Petter Selasky 				      UAPI_DEF_OBJ_NEEDS_FN(destroy_cq)),
213b633e08cSHans Petter Selasky 	{}
214b633e08cSHans Petter Selasky };
215