]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/rdma/uverbs_ioctl.h
RDMA/uverbs: Use uverbs_attr_bundle to pass udata for write
[mirror_ubuntu-jammy-kernel.git] / include / rdma / uverbs_ioctl.h
CommitLineData
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
48enum 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,
70cd20ae 55 UVERBS_ATTR_TYPE_IDRS_ARRAY,
f43dbebf
MB
56};
57
a0aa309c
MB
58enum uverbs_obj_access {
59 UVERBS_ACCESS_READ,
60 UVERBS_ACCESS_WRITE,
61 UVERBS_ACCESS_NEW,
62 UVERBS_ACCESS_DESTROY
63};
64
1f07e08f 65/* Specification of a single attribute inside the ioctl message */
83bb4442 66/* good size 16 */
f43dbebf 67struct uverbs_attr_spec {
d108dac0 68 u8 type;
83bb4442
JG
69
70 /*
422e3d37
JG
71 * Support extending attributes by length. Allow the user to provide
72 * more bytes than ptr.len, but check that everything after is zero'd
73 * by the user.
83bb4442 74 */
422e3d37 75 u8 zero_trailing:1;
83bb4442
JG
76 /*
77 * Valid only for PTR_IN. Allocate and copy the data inside
78 * the parser
79 */
80 u8 alloc_and_copy:1;
81 u8 mandatory:1;
d108dac0 82
fac9658c 83 union {
1f07e08f 84 struct {
c66db311 85 /* Current known size to kernel */
d108dac0 86 u16 len;
c66db311 87 /* User isn't allowed to provide something < min_len */
d108dac0 88 u16 min_len;
1f07e08f 89 } ptr;
d108dac0 90
1f07e08f 91 struct {
fac9658c
MB
92 /*
93 * higher bits mean the namespace and lower bits mean
94 * the type id within the namespace.
95 */
d108dac0
JG
96 u16 obj_type;
97 u8 access;
fac9658c 98 } obj;
d108dac0
JG
99
100 struct {
101 u8 num_elems;
102 } enum_def;
103 } u;
104
70cd20ae 105 /* This weird split lets us remove some padding */
d108dac0 106 union {
494c5580 107 struct {
494c5580
MB
108 /*
109 * The enum attribute can select one of the attributes
110 * contained in the ids array. Currently only PTR_IN
111 * attributes are supported in the ids array.
112 */
d108dac0 113 const struct uverbs_attr_spec *ids;
494c5580 114 } enum_def;
70cd20ae
GL
115
116 struct {
117 /*
118 * higher bits mean the namespace and lower bits mean
119 * the type id within the namespace.
120 */
121 u16 obj_type;
122 u16 min_len;
123 u16 max_len;
124 u8 access;
125 } objs_arr;
d108dac0 126 } u2;
f43dbebf
MB
127};
128
9ed3e5f4
JG
129/*
130 * Information about the API is loaded into a radix tree. For IOCTL we start
131 * with a tuple of:
132 * object_id, attr_id, method_id
133 *
134 * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
135 * on the current kernel support this is compressed into 16 bit key for the
136 * radix tree. Since this compression is entirely internal to the kernel the
137 * below limits can be revised if the kernel gains additional data.
138 *
139 * With 64 leafs per node this is a 3 level radix tree.
140 *
141 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
142 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
6884c6c4
JG
143 *
144 * This also encodes the tables for the write() and write() extended commands
145 * using the coding
146 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
147 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
148 * ie the WRITE path is treated as a special method type in the ioctl
149 * framework.
9ed3e5f4
JG
150 */
151enum uapi_radix_data {
152 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
153
154 UVERBS_API_ATTR_KEY_BITS = 6,
155 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
156 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
6884c6c4 157 UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
9ed3e5f4
JG
158
159 UVERBS_API_METHOD_KEY_BITS = 5,
160 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
6884c6c4
JG
161 UVERBS_API_METHOD_KEY_NUM_CORE = 22,
162 UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
163 UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
164 UVERBS_API_METHOD_KEY_NUM_DRIVER =
165 (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
166 UVERBS_API_METHOD_KEY_NUM_CORE,
9ed3e5f4
JG
167 UVERBS_API_METHOD_KEY_MASK = GENMASK(
168 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
169 UVERBS_API_METHOD_KEY_SHIFT),
170
171 UVERBS_API_OBJ_KEY_BITS = 5,
172 UVERBS_API_OBJ_KEY_SHIFT =
173 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
174 UVERBS_API_OBJ_KEY_NUM_CORE = 24,
175 UVERBS_API_OBJ_KEY_NUM_DRIVER =
176 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
177 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
178
179 /* This id guaranteed to not exist in the radix tree */
180 UVERBS_API_KEY_ERR = 0xFFFFFFFF,
181};
182
183static inline __attribute_const__ u32 uapi_key_obj(u32 id)
184{
185 if (id & UVERBS_API_NS_FLAG) {
186 id &= ~UVERBS_API_NS_FLAG;
187 if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
188 return UVERBS_API_KEY_ERR;
189 id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
190 } else {
191 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
192 return UVERBS_API_KEY_ERR;
193 }
194
195 return id << UVERBS_API_OBJ_KEY_SHIFT;
196}
197
198static inline __attribute_const__ bool uapi_key_is_object(u32 key)
199{
200 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
201}
202
203static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
204{
205 if (id & UVERBS_API_NS_FLAG) {
206 id &= ~UVERBS_API_NS_FLAG;
207 if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
208 return UVERBS_API_KEY_ERR;
209 id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
210 } else {
211 id++;
212 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
213 return UVERBS_API_KEY_ERR;
214 }
215
216 return id << UVERBS_API_METHOD_KEY_SHIFT;
217}
218
6884c6c4
JG
219static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
220{
221 if (id >= UVERBS_API_WRITE_KEY_NUM)
222 return UVERBS_API_KEY_ERR;
223 return UVERBS_API_METHOD_IS_WRITE | id;
224}
225
226static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
227{
228 if (id >= UVERBS_API_WRITE_KEY_NUM)
229 return UVERBS_API_KEY_ERR;
230 return UVERBS_API_METHOD_IS_WRITE_EX | id;
231}
232
233static inline __attribute_const__ u32
234uapi_key_attr_to_ioctl_method(u32 attr_key)
9ed3e5f4
JG
235{
236 return attr_key &
237 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
238}
239
240static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
241{
6884c6c4
JG
242 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
243
244 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
9ed3e5f4
JG
245 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
246}
247
6884c6c4
JG
248static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
249{
250 return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
251}
252
253static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
254{
255 return (key & UVERBS_API_METHOD_KEY_MASK) ==
256 UVERBS_API_METHOD_IS_WRITE_EX;
257}
258
9ed3e5f4
JG
259static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
260{
261 /* 0 is the method slot itself */
262 return ioctl_method_key + 1;
263}
264
265static inline __attribute_const__ u32 uapi_key_attr(u32 id)
266{
267 /*
268 * The attr is designed to fit in the typical single radix tree node
269 * of 64 entries. Since allmost all methods have driver attributes we
270 * organize things so that the driver and core attributes interleave to
271 * reduce the length of the attributes array in typical cases.
272 */
273 if (id & UVERBS_API_NS_FLAG) {
274 id &= ~UVERBS_API_NS_FLAG;
275 id++;
276 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
277 return UVERBS_API_KEY_ERR;
278 id = (id << 1) | 0;
279 } else {
280 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
281 return UVERBS_API_KEY_ERR;
282 id = (id << 1) | 1;
283 }
284
285 return id;
286}
287
6884c6c4 288/* Only true for ioctl methods */
9ed3e5f4
JG
289static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
290{
6884c6c4
JG
291 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
292
293 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
9ed3e5f4
JG
294 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
295}
296
297/*
298 * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
299 * basically it undoes the reservation of 0 in the ID numbering. attr_key
300 * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
301 * uapi_key_attr().
302 */
303static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
304{
305 return attr_key - 1;
306}
307
70cd20ae
GL
308static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
309{
310 return attr_bkey + 1;
311}
312
5009010f
MB
313/*
314 * =======================================
315 * Verbs definitions
316 * =======================================
317 */
318
09e3ebf8
MB
319struct uverbs_attr_def {
320 u16 id;
321 struct uverbs_attr_spec attr;
322};
323
324struct uverbs_method_def {
325 u16 id;
326 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
327 u32 flags;
328 size_t num_attrs;
329 const struct uverbs_attr_def * const (*attrs)[];
15a1b4be 330 int (*handler)(struct uverbs_attr_bundle *attrs);
09e3ebf8
MB
331};
332
5009010f 333struct uverbs_object_def {
09e3ebf8 334 u16 id;
5009010f 335 const struct uverbs_obj_type *type_attrs;
09e3ebf8
MB
336 size_t num_methods;
337 const struct uverbs_method_def * const (*methods)[];
338};
339
0cbf432d
JG
340enum uapi_definition_kind {
341 UAPI_DEF_END = 0,
6884c6c4
JG
342 UAPI_DEF_OBJECT_START,
343 UAPI_DEF_WRITE,
0cbf432d
JG
344 UAPI_DEF_CHAIN_OBJ_TREE,
345 UAPI_DEF_CHAIN,
6829c1c2
JG
346 UAPI_DEF_IS_SUPPORTED_FUNC,
347 UAPI_DEF_IS_SUPPORTED_DEV_FN,
348};
349
350enum uapi_definition_scope {
351 UAPI_SCOPE_OBJECT = 1,
a140692a 352 UAPI_SCOPE_METHOD = 2,
5009010f
MB
353};
354
0cbf432d
JG
355struct uapi_definition {
356 u8 kind;
6829c1c2 357 u8 scope;
0cbf432d
JG
358 union {
359 struct {
360 u16 object_id;
361 } object_start;
6884c6c4 362 struct {
6884c6c4 363 u16 command_num;
669dac1e
JG
364 u8 is_ex:1;
365 u8 has_udata:1;
366 u8 has_resp:1;
367 u8 req_size;
368 u8 resp_size;
6884c6c4 369 } write;
0cbf432d
JG
370 };
371
372 union {
6829c1c2 373 bool (*func_is_supported)(struct ib_device *device);
7106a976
JG
374 int (*func_write)(struct uverbs_attr_bundle *attrs,
375 const char __user *buf, int in_len,
376 int out_len);
8313c10f 377 int (*func_write_ex)(struct uverbs_attr_bundle *attrs,
ef87df2c 378 struct ib_udata *ucore);
0cbf432d
JG
379 const struct uapi_definition *chain;
380 const struct uverbs_object_def *chain_obj_tree;
6829c1c2 381 size_t needs_fn_offset;
0cbf432d
JG
382 };
383};
384
6884c6c4
JG
385/* Define things connected to object_id */
386#define DECLARE_UVERBS_OBJECT(_object_id, ...) \
387 { \
388 .kind = UAPI_DEF_OBJECT_START, \
389 .object_start = { .object_id = _object_id }, \
390 }, \
391 ##__VA_ARGS__
392
393/* Use in a var_args of DECLARE_UVERBS_OBJECT */
669dac1e 394#define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
6884c6c4
JG
395 { \
396 .kind = UAPI_DEF_WRITE, \
397 .scope = UAPI_SCOPE_OBJECT, \
398 .write = { .is_ex = 0, .command_num = _command_num }, \
399 .func_write = _func, \
669dac1e 400 _cmd_desc, \
6884c6c4
JG
401 }, \
402 ##__VA_ARGS__
403
404/* Use in a var_args of DECLARE_UVERBS_OBJECT */
669dac1e 405#define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
6884c6c4
JG
406 { \
407 .kind = UAPI_DEF_WRITE, \
408 .scope = UAPI_SCOPE_OBJECT, \
409 .write = { .is_ex = 1, .command_num = _command_num }, \
410 .func_write_ex = _func, \
669dac1e 411 _cmd_desc, \
6884c6c4
JG
412 }, \
413 ##__VA_ARGS__
414
6829c1c2
JG
415/*
416 * Object is only supported if the function pointer named ibdev_fn in struct
417 * ib_device is not NULL.
418 */
419#define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
420 { \
421 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
422 .scope = UAPI_SCOPE_OBJECT, \
423 .needs_fn_offset = \
424 offsetof(struct ib_device, ibdev_fn) + \
425 BUILD_BUG_ON_ZERO( \
426 sizeof(((struct ib_device *)0)->ibdev_fn) != \
427 sizeof(void *)), \
428 }
429
a140692a
JG
430/*
431 * Method is only supported if the function pointer named ibdev_fn in struct
432 * ib_device is not NULL.
433 */
434#define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
435 { \
436 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
437 .scope = UAPI_SCOPE_METHOD, \
438 .needs_fn_offset = \
439 offsetof(struct ib_device, ibdev_fn) + \
440 BUILD_BUG_ON_ZERO( \
441 sizeof(((struct ib_device *)0)->ibdev_fn) != \
442 sizeof(void *)), \
443 }
444
6829c1c2
JG
445/* Call a function to determine if the entire object is supported or not */
446#define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
447 { \
448 .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
449 .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
450 }
451
0cbf432d
JG
452/* Include another struct uapi_definition in this one */
453#define UAPI_DEF_CHAIN(_def_var) \
454 { \
455 .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
456 }
457
458/* Temporary until the tree base description is replaced */
459#define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr) \
460 { \
461 .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
462 .object_start = { .object_id = _object_enum }, \
463 .chain_obj_tree = _object_ptr, \
464 }
465#define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
466 UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum)), \
467 ##__VA_ARGS__
468
d108dac0
JG
469/*
470 * =======================================
471 * Attribute Specifications
472 * =======================================
473 */
c66db311 474
c66db311 475#define UVERBS_ATTR_SIZE(_min_len, _len) \
d108dac0 476 .u.ptr.min_len = _min_len, .u.ptr.len = _len
422e3d37 477
fd44e385
YH
478#define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
479
422e3d37
JG
480/*
481 * Specifies a uapi structure that cannot be extended. The user must always
482 * supply the whole structure and nothing more. The structure must be declared
483 * in a header under include/uapi/rdma.
484 */
485#define UVERBS_ATTR_TYPE(_type) \
486 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
487/*
488 * Specifies a uapi structure where the user must provide at least up to
489 * member 'last'. Anything after last and up until the end of the structure
490 * can be non-zero, anything longer than the end of the structure must be
491 * zero. The structure must be declared in a header under include/uapi/rdma.
492 */
493#define UVERBS_ATTR_STRUCT(_type, _last) \
494 .zero_trailing = 1, \
495 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
496 sizeof(_type))
540cd692
JG
497/*
498 * Specifies at least min_len bytes must be passed in, but the amount can be
499 * larger, up to the protocol maximum size. No check for zeroing is done.
500 */
501#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
c66db311 502
d108dac0 503/* Must be used in the '...' of any UVERBS_ATTR */
83bb4442
JG
504#define UA_ALLOC_AND_COPY .alloc_and_copy = 1
505#define UA_MANDATORY .mandatory = 1
83bb4442 506#define UA_OPTIONAL .mandatory = 0
d108dac0 507
70cd20ae
GL
508/*
509 * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
510 * READ\WRITE accesses are supported.
511 */
512#define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
513 ...) \
514 (&(const struct uverbs_attr_def){ \
515 .id = (_attr_id) + \
516 BUILD_BUG_ON_ZERO((_min_len) == 0 || \
517 (_max_len) > \
518 PAGE_SIZE / sizeof(void *) || \
519 (_min_len) > (_max_len) || \
520 (_access) == UVERBS_ACCESS_NEW || \
521 (_access) == UVERBS_ACCESS_DESTROY), \
522 .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
523 .u2.objs_arr.obj_type = _idr_type, \
524 .u2.objs_arr.access = _access, \
525 .u2.objs_arr.min_len = _min_len, \
526 .u2.objs_arr.max_len = _max_len, \
527 __VA_ARGS__ } })
528
d108dac0 529#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
9a119cd5 530 (&(const struct uverbs_attr_def){ \
d108dac0
JG
531 .id = _attr_id, \
532 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
533 .u.obj.obj_type = _idr_type, \
534 .u.obj.access = _access, \
535 __VA_ARGS__ } })
536
537#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
9a119cd5 538 (&(const struct uverbs_attr_def){ \
d108dac0
JG
539 .id = (_attr_id) + \
540 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
541 (_access) != UVERBS_ACCESS_READ), \
542 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
543 .u.obj.obj_type = _fd_type, \
544 .u.obj.access = _access, \
545 __VA_ARGS__ } })
546
547#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
9a119cd5 548 (&(const struct uverbs_attr_def){ \
d108dac0
JG
549 .id = _attr_id, \
550 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
551 _type, \
552 __VA_ARGS__ } })
553
554#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
9a119cd5 555 (&(const struct uverbs_attr_def){ \
d108dac0
JG
556 .id = _attr_id, \
557 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
558 _type, \
559 __VA_ARGS__ } })
560
561/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
562#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
9a119cd5 563 (&(const struct uverbs_attr_def){ \
d108dac0
JG
564 .id = _attr_id, \
565 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
566 .u2.enum_def.ids = _enum_arr, \
567 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
568 __VA_ARGS__ }, \
569 })
35410306 570
0953fffe
MB
571/* An input value that is a member in the enum _enum_type. */
572#define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
573 UVERBS_ATTR_PTR_IN( \
574 _attr_id, \
575 UVERBS_ATTR_SIZE( \
576 sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
577 sizeof(u64)), \
578 __VA_ARGS__)
579
bccd0622
JG
580/*
581 * An input value that is a bitwise combination of values of _enum_type.
582 * This permits the flag value to be passed as either a u32 or u64, it must
583 * be retrieved via uverbs_get_flag().
584 */
585#define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
586 UVERBS_ATTR_PTR_IN( \
587 _attr_id, \
588 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
589 !sizeof(_enum_type *)), \
590 sizeof(u64)), \
591 __VA_ARGS__)
592
6c61d2a5
JG
593/*
594 * This spec is used in order to pass information to the hardware driver in a
595 * legacy way. Every verb that could get driver specific data should get this
596 * spec.
597 */
598#define UVERBS_ATTR_UHW() \
9a119cd5 599 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
540cd692
JG
600 UVERBS_ATTR_MIN_SIZE(0), \
601 UA_OPTIONAL), \
9a119cd5 602 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
540cd692
JG
603 UVERBS_ATTR_MIN_SIZE(0), \
604 UA_OPTIONAL)
6c61d2a5 605
fac9658c
MB
606/* =================================================
607 * Parsing infrastructure
608 * =================================================
609 */
610
3a863577 611
fac9658c 612struct uverbs_ptr_attr {
8762d149
MB
613 /*
614 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
615 * used.
616 */
617 union {
618 void *ptr;
619 u64 data;
620 };
fac9658c 621 u16 len;
6a1f444f 622 u16 uattr_idx;
494c5580 623 u8 enum_id;
fac9658c
MB
624};
625
f43dbebf
MB
626struct uverbs_obj_attr {
627 struct ib_uobject *uobject;
3a863577 628 const struct uverbs_api_attr *attr_elm;
f43dbebf
MB
629};
630
70cd20ae
GL
631struct uverbs_objs_arr_attr {
632 struct ib_uobject **uobjects;
633 u16 len;
634};
635
f43dbebf 636struct uverbs_attr {
fac9658c
MB
637 union {
638 struct uverbs_ptr_attr ptr_attr;
639 struct uverbs_obj_attr obj_attr;
70cd20ae 640 struct uverbs_objs_arr_attr objs_arr_attr;
fac9658c 641 };
f43dbebf
MB
642};
643
f43dbebf 644struct uverbs_attr_bundle {
ef87df2c 645 struct ib_udata driver_udata;
4b3dd2bb 646 struct ib_uverbs_file *ufile;
3a863577
JG
647 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
648 struct uverbs_attr attrs[];
f43dbebf
MB
649};
650
35410306
MB
651static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
652 unsigned int idx)
653{
3a863577
JG
654 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
655 attrs_bundle->attr_present);
35410306
MB
656}
657
41b2a71f
MB
658#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
659
d70724f1
MB
660static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
661 u16 idx)
662{
d70724f1
MB
663 if (!uverbs_attr_is_valid(attrs_bundle, idx))
664 return ERR_PTR(-ENOENT);
665
3a863577 666 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
d70724f1
MB
667}
668
494c5580
MB
669static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
670 u16 idx)
671{
672 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
673
674 if (IS_ERR(attr))
675 return PTR_ERR(attr);
676
677 return attr->ptr_attr.enum_id;
678}
679
be934cca
AL
680static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
681 u16 idx)
682{
f4602cbb 683 const struct uverbs_attr *attr;
be934cca 684
f4602cbb
JG
685 attr = uverbs_attr_get(attrs_bundle, idx);
686 if (IS_ERR(attr))
687 return ERR_CAST(attr);
be934cca 688
f4602cbb 689 return attr->obj_attr.uobject->object;
be934cca
AL
690}
691
3efa3812
MB
692static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
693 u16 idx)
694{
695 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
696
697 if (IS_ERR(attr))
698 return ERR_CAST(attr);
699
700 return attr->obj_attr.uobject;
701}
702
8762d149
MB
703static inline int
704uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
705{
706 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
707
708 if (IS_ERR(attr))
709 return PTR_ERR(attr);
710
711 return attr->ptr_attr.len;
712}
713
70cd20ae
GL
714/**
715 * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
716 * UVERBS_ATTR_TYPE_IDRS_ARRAY.
717 * @arr: Returned pointer to array of pointers for uobjects or NULL if
718 * the attribute isn't provided.
719 *
720 * Return: The array length or 0 if no attribute was provided.
721 */
722static inline int uverbs_attr_get_uobjs_arr(
723 const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
724 struct ib_uobject ***arr)
725{
726 const struct uverbs_attr *attr =
727 uverbs_attr_get(attrs_bundle, attr_idx);
728
729 if (IS_ERR(attr)) {
730 *arr = NULL;
731 return 0;
732 }
733
734 *arr = attr->objs_arr_attr.uobjects;
735
736 return attr->objs_arr_attr.len;
737}
738
89d9e8d3
MB
739static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
740{
741 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
742}
743
8762d149
MB
744static inline void *uverbs_attr_get_alloced_ptr(
745 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
746{
747 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
748
749 if (IS_ERR(attr))
750 return (void *)attr;
751
752 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
753 attr->ptr_attr.ptr;
754}
755
89d9e8d3 756static inline int _uverbs_copy_from(void *to,
d70724f1 757 const struct uverbs_attr_bundle *attrs_bundle,
89d9e8d3
MB
758 size_t idx,
759 size_t size)
d70724f1
MB
760{
761 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
762
763 if (IS_ERR(attr))
764 return PTR_ERR(attr);
765
89d9e8d3
MB
766 /*
767 * Validation ensures attr->ptr_attr.len >= size. If the caller is
c66db311
MB
768 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
769 * uverbs_copy_from_or_zero.
89d9e8d3
MB
770 */
771 if (unlikely(size < attr->ptr_attr.len))
772 return -EINVAL;
773
774 if (uverbs_attr_ptr_is_inline(attr))
d70724f1 775 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
2f36028c
JG
776 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
777 attr->ptr_attr.len))
d70724f1
MB
778 return -EFAULT;
779
780 return 0;
781}
782
c66db311
MB
783static inline int _uverbs_copy_from_or_zero(void *to,
784 const struct uverbs_attr_bundle *attrs_bundle,
785 size_t idx,
786 size_t size)
787{
788 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
789 size_t min_size;
790
791 if (IS_ERR(attr))
792 return PTR_ERR(attr);
793
794 min_size = min_t(size_t, size, attr->ptr_attr.len);
795
796 if (uverbs_attr_ptr_is_inline(attr))
797 memcpy(to, &attr->ptr_attr.data, min_size);
798 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
799 min_size))
800 return -EFAULT;
801
802 if (size > min_size)
803 memset(to + min_size, 0, size - min_size);
804
805 return 0;
806}
807
d70724f1 808#define uverbs_copy_from(to, attrs_bundle, idx) \
89d9e8d3 809 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
d70724f1 810
c66db311
MB
811#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
812 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
813
8313c10f
JG
814static inline struct ib_ucontext *
815ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
816{
817 return ib_uverbs_get_ucontext_file(attrs->ufile);
818}
819
bccd0622
JG
820#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
821int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
822 size_t idx, u64 allowed_bits);
823int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
824 size_t idx, u64 allowed_bits);
6a1f444f
JG
825int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
826 const void *from, size_t size);
461bb2ee
JG
827__malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
828 gfp_t flags);
829
830static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
831 size_t size)
832{
833 return _uverbs_alloc(bundle, size, GFP_KERNEL);
834}
835
836static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
837 size_t size)
838{
839 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
840}
0953fffe
MB
841int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
842 size_t idx, s64 lower_bound, u64 upper_bound,
843 s64 *def_val);
bccd0622
JG
844#else
845static inline int
846uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
847 size_t idx, u64 allowed_bits)
848{
849 return -EINVAL;
850}
851static inline int
852uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
853 size_t idx, u64 allowed_bits)
854{
855 return -EINVAL;
856}
6a1f444f
JG
857static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
858 size_t idx, const void *from, size_t size)
859{
860 return -EINVAL;
861}
461bb2ee
JG
862static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
863 size_t size)
864{
865 return ERR_PTR(-EINVAL);
866}
867static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
868 size_t size)
869{
870 return ERR_PTR(-EINVAL);
871}
0953fffe
MB
872static inline int
873_uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
874 size_t idx, s64 lower_bound, u64 upper_bound,
875 s64 *def_val)
876{
877 return -EINVAL;
878}
bccd0622
JG
879#endif
880
0953fffe
MB
881#define uverbs_get_const(_to, _attrs_bundle, _idx) \
882 ({ \
883 s64 _val; \
884 int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
885 type_min(typeof(*_to)), \
886 type_max(typeof(*_to)), NULL); \
887 (*_to) = _val; \
888 _ret; \
889 })
890
891#define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
892 ({ \
893 s64 _val; \
894 s64 _def_val = _default; \
895 int _ret = \
896 _uverbs_get_const(&_val, _attrs_bundle, _idx, \
897 type_min(typeof(*_to)), \
898 type_max(typeof(*_to)), &_def_val); \
899 (*_to) = _val; \
900 _ret; \
901 })
118620d3 902#endif