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