Commit | Line | Data |
---|---|---|
a0aa309c 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_IOCTL_ | |
34 | #define _UVERBS_IOCTL_ | |
35 | ||
36 | #include <rdma/uverbs_types.h> | |
35410306 MB |
37 | #include <linux/uaccess.h> |
38 | #include <rdma/rdma_user_ioctl.h> | |
d70724f1 | 39 | #include <rdma/ib_user_ioctl_verbs.h> |
1f7ff9d5 | 40 | #include <rdma/ib_user_ioctl_cmds.h> |
a0aa309c MB |
41 | |
42 | /* | |
43 | * ======================================= | |
44 | * Verbs action specifications | |
45 | * ======================================= | |
46 | */ | |
47 | ||
f43dbebf MB |
48 | enum uverbs_attr_type { |
49 | UVERBS_ATTR_TYPE_NA, | |
fac9658c MB |
50 | UVERBS_ATTR_TYPE_PTR_IN, |
51 | UVERBS_ATTR_TYPE_PTR_OUT, | |
f43dbebf MB |
52 | UVERBS_ATTR_TYPE_IDR, |
53 | UVERBS_ATTR_TYPE_FD, | |
494c5580 | 54 | UVERBS_ATTR_TYPE_ENUM_IN, |
f43dbebf MB |
55 | }; |
56 | ||
a0aa309c MB |
57 | enum uverbs_obj_access { |
58 | UVERBS_ACCESS_READ, | |
59 | UVERBS_ACCESS_WRITE, | |
60 | UVERBS_ACCESS_NEW, | |
61 | UVERBS_ACCESS_DESTROY | |
62 | }; | |
63 | ||
1f07e08f | 64 | /* Specification of a single attribute inside the ioctl message */ |
83bb4442 | 65 | /* good size 16 */ |
f43dbebf | 66 | struct uverbs_attr_spec { |
d108dac0 | 67 | u8 type; |
83bb4442 JG |
68 | |
69 | /* | |
422e3d37 JG |
70 | * Support extending attributes by length. Allow the user to provide |
71 | * more bytes than ptr.len, but check that everything after is zero'd | |
72 | * by the user. | |
83bb4442 | 73 | */ |
422e3d37 | 74 | u8 zero_trailing:1; |
83bb4442 JG |
75 | /* |
76 | * Valid only for PTR_IN. Allocate and copy the data inside | |
77 | * the parser | |
78 | */ | |
79 | u8 alloc_and_copy:1; | |
80 | u8 mandatory:1; | |
d108dac0 | 81 | |
fac9658c | 82 | union { |
1f07e08f | 83 | struct { |
c66db311 | 84 | /* Current known size to kernel */ |
d108dac0 | 85 | u16 len; |
c66db311 | 86 | /* User isn't allowed to provide something < min_len */ |
d108dac0 | 87 | u16 min_len; |
1f07e08f | 88 | } ptr; |
d108dac0 | 89 | |
1f07e08f | 90 | struct { |
fac9658c MB |
91 | /* |
92 | * higher bits mean the namespace and lower bits mean | |
93 | * the type id within the namespace. | |
94 | */ | |
d108dac0 JG |
95 | u16 obj_type; |
96 | u8 access; | |
fac9658c | 97 | } obj; |
d108dac0 JG |
98 | |
99 | struct { | |
100 | u8 num_elems; | |
101 | } enum_def; | |
102 | } u; | |
103 | ||
104 | /* This weird split of the enum lets us remove some padding */ | |
105 | union { | |
494c5580 | 106 | struct { |
494c5580 MB |
107 | /* |
108 | * The enum attribute can select one of the attributes | |
109 | * contained in the ids array. Currently only PTR_IN | |
110 | * attributes are supported in the ids array. | |
111 | */ | |
d108dac0 | 112 | const struct uverbs_attr_spec *ids; |
494c5580 | 113 | } enum_def; |
d108dac0 | 114 | } u2; |
f43dbebf MB |
115 | }; |
116 | ||
117 | struct uverbs_attr_spec_hash { | |
118 | size_t num_attrs; | |
fac9658c | 119 | unsigned long *mandatory_attrs_bitmask; |
f43dbebf MB |
120 | struct uverbs_attr_spec attrs[0]; |
121 | }; | |
122 | ||
fac9658c MB |
123 | struct uverbs_attr_bundle; |
124 | struct ib_uverbs_file; | |
125 | ||
fac9658c MB |
126 | struct uverbs_method_spec { |
127 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ | |
128 | u32 flags; | |
129 | size_t num_buckets; | |
130 | size_t num_child_attrs; | |
e83f0ecd | 131 | int (*handler)(struct ib_uverbs_file *ufile, |
fac9658c MB |
132 | struct uverbs_attr_bundle *ctx); |
133 | struct uverbs_attr_spec_hash *attr_buckets[0]; | |
134 | }; | |
135 | ||
136 | struct uverbs_method_spec_hash { | |
137 | size_t num_methods; | |
138 | struct uverbs_method_spec *methods[0]; | |
139 | }; | |
140 | ||
141 | struct uverbs_object_spec { | |
142 | const struct uverbs_obj_type *type_attrs; | |
143 | size_t num_buckets; | |
144 | struct uverbs_method_spec_hash *method_buckets[0]; | |
145 | }; | |
146 | ||
147 | struct uverbs_object_spec_hash { | |
148 | size_t num_objects; | |
149 | struct uverbs_object_spec *objects[0]; | |
150 | }; | |
151 | ||
152 | struct uverbs_root_spec { | |
153 | size_t num_buckets; | |
154 | struct uverbs_object_spec_hash *object_buckets[0]; | |
155 | }; | |
156 | ||
9ed3e5f4 JG |
157 | /* |
158 | * Information about the API is loaded into a radix tree. For IOCTL we start | |
159 | * with a tuple of: | |
160 | * object_id, attr_id, method_id | |
161 | * | |
162 | * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based | |
163 | * on the current kernel support this is compressed into 16 bit key for the | |
164 | * radix tree. Since this compression is entirely internal to the kernel the | |
165 | * below limits can be revised if the kernel gains additional data. | |
166 | * | |
167 | * With 64 leafs per node this is a 3 level radix tree. | |
168 | * | |
169 | * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns | |
170 | * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. | |
171 | */ | |
172 | enum uapi_radix_data { | |
173 | UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, | |
174 | ||
175 | UVERBS_API_ATTR_KEY_BITS = 6, | |
176 | UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), | |
177 | UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, | |
178 | ||
179 | UVERBS_API_METHOD_KEY_BITS = 5, | |
180 | UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, | |
181 | UVERBS_API_METHOD_KEY_NUM_CORE = 24, | |
182 | UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) - | |
183 | UVERBS_API_METHOD_KEY_NUM_CORE, | |
184 | UVERBS_API_METHOD_KEY_MASK = GENMASK( | |
185 | UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, | |
186 | UVERBS_API_METHOD_KEY_SHIFT), | |
187 | ||
188 | UVERBS_API_OBJ_KEY_BITS = 5, | |
189 | UVERBS_API_OBJ_KEY_SHIFT = | |
190 | UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, | |
191 | UVERBS_API_OBJ_KEY_NUM_CORE = 24, | |
192 | UVERBS_API_OBJ_KEY_NUM_DRIVER = | |
193 | (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, | |
194 | UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), | |
195 | ||
196 | /* This id guaranteed to not exist in the radix tree */ | |
197 | UVERBS_API_KEY_ERR = 0xFFFFFFFF, | |
198 | }; | |
199 | ||
200 | static inline __attribute_const__ u32 uapi_key_obj(u32 id) | |
201 | { | |
202 | if (id & UVERBS_API_NS_FLAG) { | |
203 | id &= ~UVERBS_API_NS_FLAG; | |
204 | if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) | |
205 | return UVERBS_API_KEY_ERR; | |
206 | id = id + UVERBS_API_OBJ_KEY_NUM_CORE; | |
207 | } else { | |
208 | if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) | |
209 | return UVERBS_API_KEY_ERR; | |
210 | } | |
211 | ||
212 | return id << UVERBS_API_OBJ_KEY_SHIFT; | |
213 | } | |
214 | ||
215 | static inline __attribute_const__ bool uapi_key_is_object(u32 key) | |
216 | { | |
217 | return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; | |
218 | } | |
219 | ||
220 | static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) | |
221 | { | |
222 | if (id & UVERBS_API_NS_FLAG) { | |
223 | id &= ~UVERBS_API_NS_FLAG; | |
224 | if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) | |
225 | return UVERBS_API_KEY_ERR; | |
226 | id = id + UVERBS_API_METHOD_KEY_NUM_CORE; | |
227 | } else { | |
228 | id++; | |
229 | if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) | |
230 | return UVERBS_API_KEY_ERR; | |
231 | } | |
232 | ||
233 | return id << UVERBS_API_METHOD_KEY_SHIFT; | |
234 | } | |
235 | ||
236 | static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key) | |
237 | { | |
238 | return attr_key & | |
239 | (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); | |
240 | } | |
241 | ||
242 | static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) | |
243 | { | |
244 | return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && | |
245 | (key & UVERBS_API_ATTR_KEY_MASK) == 0; | |
246 | } | |
247 | ||
248 | static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) | |
249 | { | |
250 | /* 0 is the method slot itself */ | |
251 | return ioctl_method_key + 1; | |
252 | } | |
253 | ||
254 | static inline __attribute_const__ u32 uapi_key_attr(u32 id) | |
255 | { | |
256 | /* | |
257 | * The attr is designed to fit in the typical single radix tree node | |
258 | * of 64 entries. Since allmost all methods have driver attributes we | |
259 | * organize things so that the driver and core attributes interleave to | |
260 | * reduce the length of the attributes array in typical cases. | |
261 | */ | |
262 | if (id & UVERBS_API_NS_FLAG) { | |
263 | id &= ~UVERBS_API_NS_FLAG; | |
264 | id++; | |
265 | if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) | |
266 | return UVERBS_API_KEY_ERR; | |
267 | id = (id << 1) | 0; | |
268 | } else { | |
269 | if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) | |
270 | return UVERBS_API_KEY_ERR; | |
271 | id = (id << 1) | 1; | |
272 | } | |
273 | ||
274 | return id; | |
275 | } | |
276 | ||
277 | static inline __attribute_const__ bool uapi_key_is_attr(u32 key) | |
278 | { | |
279 | return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && | |
280 | (key & UVERBS_API_ATTR_KEY_MASK) != 0; | |
281 | } | |
282 | ||
283 | /* | |
284 | * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), | |
285 | * basically it undoes the reservation of 0 in the ID numbering. attr_key | |
286 | * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of | |
287 | * uapi_key_attr(). | |
288 | */ | |
289 | static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) | |
290 | { | |
291 | return attr_key - 1; | |
292 | } | |
293 | ||
5009010f MB |
294 | /* |
295 | * ======================================= | |
296 | * Verbs definitions | |
297 | * ======================================= | |
298 | */ | |
299 | ||
09e3ebf8 MB |
300 | struct uverbs_attr_def { |
301 | u16 id; | |
302 | struct uverbs_attr_spec attr; | |
303 | }; | |
304 | ||
305 | struct uverbs_method_def { | |
306 | u16 id; | |
307 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ | |
308 | u32 flags; | |
309 | size_t num_attrs; | |
310 | const struct uverbs_attr_def * const (*attrs)[]; | |
e83f0ecd | 311 | int (*handler)(struct ib_uverbs_file *ufile, |
09e3ebf8 MB |
312 | struct uverbs_attr_bundle *ctx); |
313 | }; | |
314 | ||
5009010f | 315 | struct uverbs_object_def { |
09e3ebf8 | 316 | u16 id; |
5009010f | 317 | const struct uverbs_obj_type *type_attrs; |
09e3ebf8 MB |
318 | size_t num_methods; |
319 | const struct uverbs_method_def * const (*methods)[]; | |
320 | }; | |
321 | ||
322 | struct uverbs_object_tree_def { | |
323 | size_t num_objects; | |
324 | const struct uverbs_object_def * const (*objects)[]; | |
5009010f MB |
325 | }; |
326 | ||
d108dac0 JG |
327 | /* |
328 | * ======================================= | |
329 | * Attribute Specifications | |
330 | * ======================================= | |
331 | */ | |
c66db311 | 332 | |
c66db311 | 333 | #define UVERBS_ATTR_SIZE(_min_len, _len) \ |
d108dac0 | 334 | .u.ptr.min_len = _min_len, .u.ptr.len = _len |
422e3d37 | 335 | |
fd44e385 YH |
336 | #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) |
337 | ||
422e3d37 JG |
338 | /* |
339 | * Specifies a uapi structure that cannot be extended. The user must always | |
340 | * supply the whole structure and nothing more. The structure must be declared | |
341 | * in a header under include/uapi/rdma. | |
342 | */ | |
343 | #define UVERBS_ATTR_TYPE(_type) \ | |
344 | .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) | |
345 | /* | |
346 | * Specifies a uapi structure where the user must provide at least up to | |
347 | * member 'last'. Anything after last and up until the end of the structure | |
348 | * can be non-zero, anything longer than the end of the structure must be | |
349 | * zero. The structure must be declared in a header under include/uapi/rdma. | |
350 | */ | |
351 | #define UVERBS_ATTR_STRUCT(_type, _last) \ | |
352 | .zero_trailing = 1, \ | |
353 | UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ | |
354 | sizeof(_type)) | |
540cd692 JG |
355 | /* |
356 | * Specifies at least min_len bytes must be passed in, but the amount can be | |
357 | * larger, up to the protocol maximum size. No check for zeroing is done. | |
358 | */ | |
359 | #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) | |
c66db311 | 360 | |
d108dac0 | 361 | /* Must be used in the '...' of any UVERBS_ATTR */ |
83bb4442 JG |
362 | #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 |
363 | #define UA_MANDATORY .mandatory = 1 | |
83bb4442 | 364 | #define UA_OPTIONAL .mandatory = 0 |
d108dac0 JG |
365 | |
366 | #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ | |
9a119cd5 | 367 | (&(const struct uverbs_attr_def){ \ |
d108dac0 JG |
368 | .id = _attr_id, \ |
369 | .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ | |
370 | .u.obj.obj_type = _idr_type, \ | |
371 | .u.obj.access = _access, \ | |
372 | __VA_ARGS__ } }) | |
373 | ||
374 | #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ | |
9a119cd5 | 375 | (&(const struct uverbs_attr_def){ \ |
d108dac0 JG |
376 | .id = (_attr_id) + \ |
377 | BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ | |
378 | (_access) != UVERBS_ACCESS_READ), \ | |
379 | .attr = { .type = UVERBS_ATTR_TYPE_FD, \ | |
380 | .u.obj.obj_type = _fd_type, \ | |
381 | .u.obj.access = _access, \ | |
382 | __VA_ARGS__ } }) | |
383 | ||
384 | #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ | |
9a119cd5 | 385 | (&(const struct uverbs_attr_def){ \ |
d108dac0 JG |
386 | .id = _attr_id, \ |
387 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ | |
388 | _type, \ | |
389 | __VA_ARGS__ } }) | |
390 | ||
391 | #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ | |
9a119cd5 | 392 | (&(const struct uverbs_attr_def){ \ |
d108dac0 JG |
393 | .id = _attr_id, \ |
394 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ | |
395 | _type, \ | |
396 | __VA_ARGS__ } }) | |
397 | ||
398 | /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ | |
399 | #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ | |
9a119cd5 | 400 | (&(const struct uverbs_attr_def){ \ |
d108dac0 JG |
401 | .id = _attr_id, \ |
402 | .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ | |
403 | .u2.enum_def.ids = _enum_arr, \ | |
404 | .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ | |
405 | __VA_ARGS__ }, \ | |
406 | }) | |
35410306 | 407 | |
bccd0622 JG |
408 | /* |
409 | * An input value that is a bitwise combination of values of _enum_type. | |
410 | * This permits the flag value to be passed as either a u32 or u64, it must | |
411 | * be retrieved via uverbs_get_flag(). | |
412 | */ | |
413 | #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ | |
414 | UVERBS_ATTR_PTR_IN( \ | |
415 | _attr_id, \ | |
416 | UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ | |
417 | !sizeof(_enum_type *)), \ | |
418 | sizeof(u64)), \ | |
419 | __VA_ARGS__) | |
420 | ||
6c61d2a5 JG |
421 | /* |
422 | * This spec is used in order to pass information to the hardware driver in a | |
423 | * legacy way. Every verb that could get driver specific data should get this | |
424 | * spec. | |
425 | */ | |
426 | #define UVERBS_ATTR_UHW() \ | |
9a119cd5 | 427 | UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ |
540cd692 JG |
428 | UVERBS_ATTR_MIN_SIZE(0), \ |
429 | UA_OPTIONAL), \ | |
9a119cd5 | 430 | UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ |
540cd692 JG |
431 | UVERBS_ATTR_MIN_SIZE(0), \ |
432 | UA_OPTIONAL) | |
6c61d2a5 | 433 | |
35410306 | 434 | /* |
d108dac0 JG |
435 | * ======================================= |
436 | * Declaration helpers | |
437 | * ======================================= | |
35410306 | 438 | */ |
6c61d2a5 JG |
439 | |
440 | #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ | |
441 | static const struct uverbs_object_def *const _name##_ptr[] = { \ | |
442 | __VA_ARGS__, \ | |
443 | }; \ | |
444 | static const struct uverbs_object_tree_def _name = { \ | |
445 | .num_objects = ARRAY_SIZE(_name##_ptr), \ | |
446 | .objects = &_name##_ptr, \ | |
447 | } | |
09e3ebf8 | 448 | |
fac9658c MB |
449 | /* ================================================= |
450 | * Parsing infrastructure | |
451 | * ================================================= | |
452 | */ | |
453 | ||
454 | struct uverbs_ptr_attr { | |
8762d149 MB |
455 | /* |
456 | * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is | |
457 | * used. | |
458 | */ | |
459 | union { | |
460 | void *ptr; | |
461 | u64 data; | |
462 | }; | |
fac9658c MB |
463 | u16 len; |
464 | /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ | |
465 | u16 flags; | |
494c5580 | 466 | u8 enum_id; |
fac9658c MB |
467 | }; |
468 | ||
f43dbebf MB |
469 | struct uverbs_obj_attr { |
470 | struct ib_uobject *uobject; | |
471 | }; | |
472 | ||
473 | struct uverbs_attr { | |
fac9658c MB |
474 | /* |
475 | * pointer to the user-space given attribute, in order to write the | |
476 | * new uobject's id or update flags. | |
477 | */ | |
478 | struct ib_uverbs_attr __user *uattr; | |
479 | union { | |
480 | struct uverbs_ptr_attr ptr_attr; | |
481 | struct uverbs_obj_attr obj_attr; | |
482 | }; | |
f43dbebf MB |
483 | }; |
484 | ||
485 | struct uverbs_attr_bundle_hash { | |
486 | /* if bit i is set, it means attrs[i] contains valid information */ | |
487 | unsigned long *valid_bitmap; | |
488 | size_t num_attrs; | |
489 | /* | |
490 | * arrays of attributes, each element corresponds to the specification | |
491 | * of the attribute in the same index. | |
492 | */ | |
493 | struct uverbs_attr *attrs; | |
494 | }; | |
495 | ||
496 | struct uverbs_attr_bundle { | |
497 | size_t num_buckets; | |
498 | struct uverbs_attr_bundle_hash hash[]; | |
499 | }; | |
500 | ||
501 | static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, | |
502 | unsigned int idx) | |
503 | { | |
504 | return test_bit(idx, attrs_hash->valid_bitmap); | |
505 | } | |
506 | ||
35410306 MB |
507 | static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, |
508 | unsigned int idx) | |
509 | { | |
510 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; | |
511 | ||
512 | if (attrs_bundle->num_buckets <= idx_bucket) | |
513 | return false; | |
514 | ||
515 | return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], | |
516 | idx & ~UVERBS_ID_NS_MASK); | |
517 | } | |
518 | ||
41b2a71f MB |
519 | #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) |
520 | ||
d70724f1 MB |
521 | static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, |
522 | u16 idx) | |
523 | { | |
524 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; | |
525 | ||
526 | if (!uverbs_attr_is_valid(attrs_bundle, idx)) | |
527 | return ERR_PTR(-ENOENT); | |
528 | ||
529 | return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; | |
530 | } | |
531 | ||
494c5580 MB |
532 | static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, |
533 | u16 idx) | |
534 | { | |
535 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
536 | ||
537 | if (IS_ERR(attr)) | |
538 | return PTR_ERR(attr); | |
539 | ||
540 | return attr->ptr_attr.enum_id; | |
541 | } | |
542 | ||
be934cca AL |
543 | static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, |
544 | u16 idx) | |
545 | { | |
f4602cbb | 546 | const struct uverbs_attr *attr; |
be934cca | 547 | |
f4602cbb JG |
548 | attr = uverbs_attr_get(attrs_bundle, idx); |
549 | if (IS_ERR(attr)) | |
550 | return ERR_CAST(attr); | |
be934cca | 551 | |
f4602cbb | 552 | return attr->obj_attr.uobject->object; |
be934cca AL |
553 | } |
554 | ||
3efa3812 MB |
555 | static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, |
556 | u16 idx) | |
557 | { | |
558 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
559 | ||
560 | if (IS_ERR(attr)) | |
561 | return ERR_CAST(attr); | |
562 | ||
563 | return attr->obj_attr.uobject; | |
564 | } | |
565 | ||
8762d149 MB |
566 | static inline int |
567 | uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) | |
568 | { | |
569 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
570 | ||
571 | if (IS_ERR(attr)) | |
572 | return PTR_ERR(attr); | |
573 | ||
574 | return attr->ptr_attr.len; | |
575 | } | |
576 | ||
d70724f1 | 577 | static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, |
89d9e8d3 | 578 | size_t idx, const void *from, size_t size) |
d70724f1 MB |
579 | { |
580 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
581 | u16 flags; | |
89d9e8d3 | 582 | size_t min_size; |
d70724f1 MB |
583 | |
584 | if (IS_ERR(attr)) | |
585 | return PTR_ERR(attr); | |
586 | ||
89d9e8d3 | 587 | min_size = min_t(size_t, attr->ptr_attr.len, size); |
2f36028c | 588 | if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) |
89d9e8d3 MB |
589 | return -EFAULT; |
590 | ||
d70724f1 | 591 | flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; |
89d9e8d3 MB |
592 | if (put_user(flags, &attr->uattr->flags)) |
593 | return -EFAULT; | |
594 | ||
595 | return 0; | |
d70724f1 MB |
596 | } |
597 | ||
89d9e8d3 MB |
598 | static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) |
599 | { | |
600 | return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); | |
601 | } | |
602 | ||
8762d149 MB |
603 | static inline void *uverbs_attr_get_alloced_ptr( |
604 | const struct uverbs_attr_bundle *attrs_bundle, u16 idx) | |
605 | { | |
606 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
607 | ||
608 | if (IS_ERR(attr)) | |
609 | return (void *)attr; | |
610 | ||
611 | return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : | |
612 | attr->ptr_attr.ptr; | |
613 | } | |
614 | ||
89d9e8d3 | 615 | static inline int _uverbs_copy_from(void *to, |
d70724f1 | 616 | const struct uverbs_attr_bundle *attrs_bundle, |
89d9e8d3 MB |
617 | size_t idx, |
618 | size_t size) | |
d70724f1 MB |
619 | { |
620 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
621 | ||
622 | if (IS_ERR(attr)) | |
623 | return PTR_ERR(attr); | |
624 | ||
89d9e8d3 MB |
625 | /* |
626 | * Validation ensures attr->ptr_attr.len >= size. If the caller is | |
c66db311 MB |
627 | * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call |
628 | * uverbs_copy_from_or_zero. | |
89d9e8d3 MB |
629 | */ |
630 | if (unlikely(size < attr->ptr_attr.len)) | |
631 | return -EINVAL; | |
632 | ||
633 | if (uverbs_attr_ptr_is_inline(attr)) | |
d70724f1 | 634 | memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); |
2f36028c JG |
635 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
636 | attr->ptr_attr.len)) | |
d70724f1 MB |
637 | return -EFAULT; |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
c66db311 MB |
642 | static inline int _uverbs_copy_from_or_zero(void *to, |
643 | const struct uverbs_attr_bundle *attrs_bundle, | |
644 | size_t idx, | |
645 | size_t size) | |
646 | { | |
647 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); | |
648 | size_t min_size; | |
649 | ||
650 | if (IS_ERR(attr)) | |
651 | return PTR_ERR(attr); | |
652 | ||
653 | min_size = min_t(size_t, size, attr->ptr_attr.len); | |
654 | ||
655 | if (uverbs_attr_ptr_is_inline(attr)) | |
656 | memcpy(to, &attr->ptr_attr.data, min_size); | |
657 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), | |
658 | min_size)) | |
659 | return -EFAULT; | |
660 | ||
661 | if (size > min_size) | |
662 | memset(to + min_size, 0, size - min_size); | |
663 | ||
664 | return 0; | |
665 | } | |
666 | ||
d70724f1 | 667 | #define uverbs_copy_from(to, attrs_bundle, idx) \ |
89d9e8d3 | 668 | _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) |
d70724f1 | 669 | |
c66db311 MB |
670 | #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ |
671 | _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) | |
672 | ||
bccd0622 JG |
673 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
674 | int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, | |
675 | size_t idx, u64 allowed_bits); | |
676 | int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, | |
677 | size_t idx, u64 allowed_bits); | |
678 | #else | |
679 | static inline int | |
680 | uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, | |
681 | size_t idx, u64 allowed_bits) | |
682 | { | |
683 | return -EINVAL; | |
684 | } | |
685 | static inline int | |
686 | uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, | |
687 | size_t idx, u64 allowed_bits) | |
688 | { | |
689 | return -EINVAL; | |
690 | } | |
691 | #endif | |
692 | ||
118620d3 MB |
693 | /* ================================================= |
694 | * Definitions -> Specs infrastructure | |
695 | * ================================================= | |
696 | */ | |
697 | ||
698 | /* | |
699 | * uverbs_alloc_spec_tree - Merges different common and driver specific feature | |
700 | * into one parsing tree that every uverbs command will be parsed upon. | |
701 | * | |
702 | * @num_trees: Number of trees in the array @trees. | |
703 | * @trees: Array of pointers to tree root definitions to merge. Each such tree | |
704 | * possibly contains objects, methods and attributes definitions. | |
705 | * | |
706 | * Returns: | |
707 | * uverbs_root_spec *: The root of the merged parsing tree. | |
708 | * On error, we return an error code. Error is checked via IS_ERR. | |
709 | * | |
710 | * The following merges could take place: | |
711 | * a. Two trees representing the same method with different handler | |
712 | * -> We take the handler of the tree that its handler != NULL | |
713 | * and its index in the trees array is greater. The incentive for that | |
714 | * is that developers are expected to first merge common trees and then | |
715 | * merge trees that gives specialized the behaviour. | |
716 | * b. Two trees representing the same object with different | |
717 | * type_attrs (struct uverbs_obj_type): | |
718 | * -> We take the type_attrs of the tree that its type_attr != NULL | |
719 | * and its index in the trees array is greater. This could be used | |
720 | * in order to override the free function, allocation size, etc. | |
721 | * c. Two trees representing the same method attribute (same id but possibly | |
722 | * different attributes): | |
723 | * -> ERROR (-ENOENT), we believe that's not the programmer's intent. | |
724 | * | |
725 | * An object without any methods is considered invalid and will abort the | |
726 | * function with -ENOENT error. | |
727 | */ | |
52427112 | 728 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
118620d3 MB |
729 | struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
730 | const struct uverbs_object_tree_def **trees); | |
731 | void uverbs_free_spec_tree(struct uverbs_root_spec *root); | |
52427112 MB |
732 | #else |
733 | static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, | |
734 | const struct uverbs_object_tree_def **trees) | |
735 | { | |
736 | return NULL; | |
737 | } | |
738 | ||
739 | static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) | |
740 | { | |
741 | } | |
742 | #endif | |
a0aa309c | 743 | |
118620d3 | 744 | #endif |