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