uverbs_types.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifndef _UVERBS_TYPES_
  33. #define _UVERBS_TYPES_
  34. #include <linux/kernel.h>
  35. #include <rdma/ib_verbs.h>
  36. struct uverbs_obj_type;
  37. struct uverbs_api_object;
  38. enum rdma_lookup_mode {
  39. UVERBS_LOOKUP_READ,
  40. UVERBS_LOOKUP_WRITE,
  41. /*
  42. * Destroy is like LOOKUP_WRITE, except that the uobject is not
  43. * locked. uobj_destroy is used to convert a LOOKUP_DESTROY lock into
  44. * a LOOKUP_WRITE lock.
  45. */
  46. UVERBS_LOOKUP_DESTROY,
  47. };
  48. /*
  49. * The following sequences are valid:
  50. * Success flow:
  51. * alloc_begin
  52. * alloc_commit
  53. * [..]
  54. * Access flow:
  55. * lookup_get(exclusive=false) & uverbs_try_lock_object
  56. * lookup_put(exclusive=false) via rdma_lookup_put_uobject
  57. * Destruction flow:
  58. * lookup_get(exclusive=true) & uverbs_try_lock_object
  59. * remove_commit
  60. * remove_handle (optional)
  61. * lookup_put(exclusive=true) via rdma_lookup_put_uobject
  62. *
  63. * Allocate Error flow #1
  64. * alloc_begin
  65. * alloc_abort
  66. * Allocate Error flow #2
  67. * alloc_begin
  68. * remove_commit
  69. * alloc_abort
  70. * Allocate Error flow #3
  71. * alloc_begin
  72. * alloc_commit (fails)
  73. * remove_commit
  74. * alloc_abort
  75. *
  76. * In all cases the caller must hold the ufile kref until alloc_commit or
  77. * alloc_abort returns.
  78. */
  79. struct uverbs_obj_type_class {
  80. struct ib_uobject *(*alloc_begin)(const struct uverbs_api_object *obj,
  81. struct ib_uverbs_file *ufile);
  82. /* This consumes the kref on uobj */
  83. int (*alloc_commit)(struct ib_uobject *uobj);
  84. /* This does not consume the kref on uobj */
  85. void (*alloc_abort)(struct ib_uobject *uobj);
  86. struct ib_uobject *(*lookup_get)(const struct uverbs_api_object *obj,
  87. struct ib_uverbs_file *ufile, s64 id,
  88. enum rdma_lookup_mode mode);
  89. void (*lookup_put)(struct ib_uobject *uobj, enum rdma_lookup_mode mode);
  90. /* This does not consume the kref on uobj */
  91. int __must_check (*destroy_hw)(struct ib_uobject *uobj,
  92. enum rdma_remove_reason why);
  93. void (*remove_handle)(struct ib_uobject *uobj);
  94. u8 needs_kfree_rcu;
  95. };
  96. struct uverbs_obj_type {
  97. const struct uverbs_obj_type_class * const type_class;
  98. size_t obj_size;
  99. };
  100. /*
  101. * Objects type classes which support a detach state (object is still alive but
  102. * it's not attached to any context need to make sure:
  103. * (a) no call through to a driver after a detach is called
  104. * (b) detach isn't called concurrently with context_cleanup
  105. */
  106. struct uverbs_obj_idr_type {
  107. /*
  108. * In idr based objects, uverbs_obj_type_class points to a generic
  109. * idr operations. In order to specialize the underlying types (e.g. CQ,
  110. * QPs, etc.), we add destroy_object specific callbacks.
  111. */
  112. struct uverbs_obj_type type;
  113. /* Free driver resources from the uobject, make the driver uncallable,
  114. * and move the uobject to the detached state. If the object was
  115. * destroyed by the user's request, a failure should leave the uobject
  116. * completely unchanged.
  117. */
  118. int __must_check (*destroy_object)(struct ib_uobject *uobj,
  119. enum rdma_remove_reason why);
  120. };
  121. struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_api_object *obj,
  122. struct ib_uverbs_file *ufile, s64 id,
  123. enum rdma_lookup_mode mode);
  124. void rdma_lookup_put_uobject(struct ib_uobject *uobj,
  125. enum rdma_lookup_mode mode);
  126. struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_api_object *obj,
  127. struct ib_uverbs_file *ufile);
  128. void rdma_alloc_abort_uobject(struct ib_uobject *uobj);
  129. int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj);
  130. struct uverbs_obj_fd_type {
  131. /*
  132. * In fd based objects, uverbs_obj_type_ops points to generic
  133. * fd operations. In order to specialize the underlying types (e.g.
  134. * completion_channel), we use fops, name and flags for fd creation.
  135. * context_closed is called when the context is closed either when
  136. * the driver is removed or the process terminated.
  137. */
  138. struct uverbs_obj_type type;
  139. int (*context_closed)(struct ib_uobject *uobj,
  140. enum rdma_remove_reason why);
  141. const struct file_operations *fops;
  142. const char *name;
  143. int flags;
  144. };
  145. extern const struct uverbs_obj_type_class uverbs_idr_class;
  146. extern const struct uverbs_obj_type_class uverbs_fd_class;
  147. #define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \
  148. sizeof(char))
  149. #define UVERBS_TYPE_ALLOC_FD(_obj_size, _context_closed, _fops, _name, _flags)\
  150. ((&((const struct uverbs_obj_fd_type) \
  151. {.type = { \
  152. .type_class = &uverbs_fd_class, \
  153. .obj_size = (_obj_size) + \
  154. UVERBS_BUILD_BUG_ON((_obj_size) < \
  155. sizeof(struct ib_uobject)), \
  156. }, \
  157. .context_closed = _context_closed, \
  158. .fops = _fops, \
  159. .name = _name, \
  160. .flags = _flags}))->type)
  161. #define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _destroy_object) \
  162. ((&((const struct uverbs_obj_idr_type) \
  163. {.type = { \
  164. .type_class = &uverbs_idr_class, \
  165. .obj_size = (_size) + \
  166. UVERBS_BUILD_BUG_ON((_size) < \
  167. sizeof(struct ib_uobject)) \
  168. }, \
  169. .destroy_object = _destroy_object,}))->type)
  170. #define UVERBS_TYPE_ALLOC_IDR(_destroy_object) \
  171. UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject), \
  172. _destroy_object)
  173. #endif