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 struct uverbs_attr_bundle *attrs) 40 { 41 struct ib_cq *cq = uobject->object; 42 struct ib_uverbs_event_queue *ev_queue = cq->cq_context; 43 struct ib_ucq_object *ucq = 44 container_of(uobject, struct ib_ucq_object, uevent.uobject); 45 int ret; 46 47 ret = ib_destroy_cq_user(cq, &attrs->driver_udata); 48 if (ib_is_destroy_retryable(ret, why, uobject)) 49 return ret; 50 51 ib_uverbs_release_ucq( 52 ev_queue ? container_of(ev_queue, 53 struct ib_uverbs_completion_event_file, 54 ev_queue) : 55 NULL, 56 ucq); 57 return ret; 58 } 59 60 static int UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE)( 61 struct uverbs_attr_bundle *attrs) 62 { 63 struct ib_ucq_object *obj = container_of( 64 uverbs_attr_get_uobject(attrs, UVERBS_ATTR_CREATE_CQ_HANDLE), 65 typeof(*obj), uevent.uobject); 66 struct ib_device *ib_dev = attrs->context->device; 67 int ret; 68 u64 user_handle; 69 struct ib_cq_init_attr attr = {}; 70 struct ib_cq *cq; 71 struct ib_uverbs_completion_event_file *ev_file = NULL; 72 struct ib_uobject *ev_file_uobj; 73 74 if (!ib_dev->ops.create_cq || !ib_dev->ops.destroy_cq) 75 return -EOPNOTSUPP; 76 77 ret = uverbs_copy_from(&attr.comp_vector, attrs, 78 UVERBS_ATTR_CREATE_CQ_COMP_VECTOR); 79 if (!ret) 80 ret = uverbs_copy_from(&attr.cqe, attrs, 81 UVERBS_ATTR_CREATE_CQ_CQE); 82 if (!ret) 83 ret = uverbs_copy_from(&user_handle, attrs, 84 UVERBS_ATTR_CREATE_CQ_USER_HANDLE); 85 if (ret) 86 return ret; 87 88 ret = uverbs_get_flags32(&attr.flags, attrs, 89 UVERBS_ATTR_CREATE_CQ_FLAGS, 90 IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION | 91 IB_UVERBS_CQ_FLAGS_IGNORE_OVERRUN); 92 if (ret) 93 return ret; 94 95 ev_file_uobj = uverbs_attr_get_uobject(attrs, UVERBS_ATTR_CREATE_CQ_COMP_CHANNEL); 96 if (!IS_ERR(ev_file_uobj)) { 97 ev_file = container_of(ev_file_uobj, 98 struct ib_uverbs_completion_event_file, 99 uobj); 100 uverbs_uobject_get(ev_file_uobj); 101 } 102 103 if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) { 104 ret = -EINVAL; 105 goto err_event_file; 106 } 107 108 INIT_LIST_HEAD(&obj->comp_list); 109 INIT_LIST_HEAD(&obj->uevent.event_list); 110 111 cq = rdma_zalloc_drv_obj(ib_dev, ib_cq); 112 if (!cq) { 113 ret = -ENOMEM; 114 goto err_event_file; 115 } 116 117 cq->device = ib_dev; 118 cq->uobject = obj; 119 cq->comp_handler = ib_uverbs_comp_handler; 120 cq->event_handler = ib_uverbs_cq_event_handler; 121 cq->cq_context = ev_file ? &ev_file->ev_queue : NULL; 122 atomic_set(&cq->usecnt, 0); 123 cq->res.type = RDMA_RESTRACK_CQ; 124 125 ret = ib_dev->ops.create_cq(cq, &attr, &attrs->driver_udata); 126 if (ret) 127 goto err_free; 128 129 obj->uevent.uobject.object = cq; 130 obj->uevent.uobject.user_handle = user_handle; 131 rdma_restrack_uadd(&cq->res); 132 133 ret = uverbs_copy_to(attrs, UVERBS_ATTR_CREATE_CQ_RESP_CQE, &cq->cqe, 134 sizeof(cq->cqe)); 135 if (ret) 136 goto err_cq; 137 138 return 0; 139 err_cq: 140 ib_destroy_cq_user(cq, uverbs_get_cleared_udata(attrs)); 141 cq = NULL; 142 err_free: 143 kfree(cq); 144 err_event_file: 145 if (ev_file) 146 uverbs_uobject_put(ev_file_uobj); 147 return ret; 148 }; 149 150 DECLARE_UVERBS_NAMED_METHOD( 151 UVERBS_METHOD_CQ_CREATE, 152 UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_CQ_HANDLE, 153 UVERBS_OBJECT_CQ, 154 UVERBS_ACCESS_NEW, 155 UA_MANDATORY), 156 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_CQE, 157 UVERBS_ATTR_TYPE(u32), 158 UA_MANDATORY), 159 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_USER_HANDLE, 160 UVERBS_ATTR_TYPE(u64), 161 UA_MANDATORY), 162 UVERBS_ATTR_FD(UVERBS_ATTR_CREATE_CQ_COMP_CHANNEL, 163 UVERBS_OBJECT_COMP_CHANNEL, 164 UVERBS_ACCESS_READ, 165 UA_OPTIONAL), 166 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_COMP_VECTOR, 167 UVERBS_ATTR_TYPE(u32), 168 UA_MANDATORY), 169 UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_CREATE_CQ_FLAGS, 170 enum ib_uverbs_ex_create_cq_flags), 171 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_CREATE_CQ_RESP_CQE, 172 UVERBS_ATTR_TYPE(u32), 173 UA_MANDATORY), 174 UVERBS_ATTR_UHW()); 175 176 static int UVERBS_HANDLER(UVERBS_METHOD_CQ_DESTROY)( 177 struct uverbs_attr_bundle *attrs) 178 { 179 struct ib_uobject *uobj = 180 uverbs_attr_get_uobject(attrs, UVERBS_ATTR_DESTROY_CQ_HANDLE); 181 struct ib_ucq_object *obj = 182 container_of(uobj, struct ib_ucq_object, uevent.uobject); 183 struct ib_uverbs_destroy_cq_resp resp = { 184 .comp_events_reported = obj->comp_events_reported, 185 .async_events_reported = obj->uevent.events_reported 186 }; 187 188 return uverbs_copy_to(attrs, UVERBS_ATTR_DESTROY_CQ_RESP, &resp, 189 sizeof(resp)); 190 } 191 192 DECLARE_UVERBS_NAMED_METHOD( 193 UVERBS_METHOD_CQ_DESTROY, 194 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_CQ_HANDLE, 195 UVERBS_OBJECT_CQ, 196 UVERBS_ACCESS_DESTROY, 197 UA_MANDATORY), 198 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_DESTROY_CQ_RESP, 199 UVERBS_ATTR_TYPE(struct ib_uverbs_destroy_cq_resp), 200 UA_MANDATORY)); 201 202 DECLARE_UVERBS_NAMED_OBJECT( 203 UVERBS_OBJECT_CQ, 204 UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_ucq_object), uverbs_free_cq), 205 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 const struct uapi_definition uverbs_def_obj_cq[] = { 213 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_CQ, 214 UAPI_DEF_OBJ_NEEDS_FN(destroy_cq)), 215 {} 216 }; 217