uverbs_std_types_cq.c 6.8 KB

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