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