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