blob: e0b1a77b1c2cffb13bc53aa9bbcc233019062eef [file] [log] [blame]
Matan Baraka0aa3092017-08-03 16:06:55 +03001/*
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>
Matan Barak35410302017-08-03 16:07:01 +030037#include <linux/uaccess.h>
38#include <rdma/rdma_user_ioctl.h>
Matan Barakd70724f2017-08-03 16:07:04 +030039#include <rdma/ib_user_ioctl_verbs.h>
Matan Barak1f7ff9d2018-03-19 15:02:33 +020040#include <rdma/ib_user_ioctl_cmds.h>
Matan Baraka0aa3092017-08-03 16:06:55 +030041
42/*
43 * =======================================
44 * Verbs action specifications
45 * =======================================
46 */
47
Matan Barakf43dbeb2017-08-03 16:06:56 +030048enum uverbs_attr_type {
49 UVERBS_ATTR_TYPE_NA,
Matan Barakfac96582017-08-03 16:06:57 +030050 UVERBS_ATTR_TYPE_PTR_IN,
51 UVERBS_ATTR_TYPE_PTR_OUT,
Matan Barakf43dbeb2017-08-03 16:06:56 +030052 UVERBS_ATTR_TYPE_IDR,
53 UVERBS_ATTR_TYPE_FD,
Matan Barak494c5582018-03-28 09:27:42 +030054 UVERBS_ATTR_TYPE_ENUM_IN,
Guy Levi70cd20a2018-09-06 17:27:01 +030055 UVERBS_ATTR_TYPE_IDRS_ARRAY,
Matan Barakf43dbeb2017-08-03 16:06:56 +030056};
57
Matan Baraka0aa3092017-08-03 16:06:55 +030058enum uverbs_obj_access {
59 UVERBS_ACCESS_READ,
60 UVERBS_ACCESS_WRITE,
61 UVERBS_ACCESS_NEW,
62 UVERBS_ACCESS_DESTROY
63};
64
Matan Barak1f07e08f2018-03-19 15:02:35 +020065/* Specification of a single attribute inside the ioctl message */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030066/* good size 16 */
Matan Barakf43dbeb2017-08-03 16:06:56 +030067struct uverbs_attr_spec {
Jason Gunthorped108dac2018-07-04 08:50:25 +030068 u8 type;
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030069
70 /*
Jason Gunthorpe422e3d32018-07-04 08:50:31 +030071 * 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.
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030074 */
Jason Gunthorpe422e3d32018-07-04 08:50:31 +030075 u8 zero_trailing:1;
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030076 /*
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;
Jason Gunthorped108dac2018-07-04 08:50:25 +030082
Matan Barakfac96582017-08-03 16:06:57 +030083 union {
Matan Barakfac96582017-08-03 16:06:57 +030084 struct {
Matan Barakc66db312018-03-19 15:02:36 +020085 /* Current known size to kernel */
Jason Gunthorped108dac2018-07-04 08:50:25 +030086 u16 len;
Matan Barakc66db312018-03-19 15:02:36 +020087 /* User isn't allowed to provide something < min_len */
Jason Gunthorped108dac2018-07-04 08:50:25 +030088 u16 min_len;
Matan Barak1f07e08f2018-03-19 15:02:35 +020089 } ptr;
Jason Gunthorped108dac2018-07-04 08:50:25 +030090
Matan Barak1f07e08f2018-03-19 15:02:35 +020091 struct {
Matan Barakfac96582017-08-03 16:06:57 +030092 /*
93 * higher bits mean the namespace and lower bits mean
94 * the type id within the namespace.
95 */
Jason Gunthorped108dac2018-07-04 08:50:25 +030096 u16 obj_type;
97 u8 access;
Matan Barakfac96582017-08-03 16:06:57 +030098 } obj;
Jason Gunthorped108dac2018-07-04 08:50:25 +030099
Matan Barak494c5582018-03-28 09:27:42 +0300100 struct {
Jason Gunthorped108dac2018-07-04 08:50:25 +0300101 u8 num_elems;
102 } enum_def;
103 } u;
104
Guy Levi70cd20a2018-09-06 17:27:01 +0300105 /* This weird split lets us remove some padding */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300106 union {
107 struct {
Matan Barak494c5582018-03-28 09:27:42 +0300108 /*
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 */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300113 const struct uverbs_attr_spec *ids;
Matan Barak494c5582018-03-28 09:27:42 +0300114 } enum_def;
Guy Levi70cd20a2018-09-06 17:27:01 +0300115
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;
Jason Gunthorped108dac2018-07-04 08:50:25 +0300126 } u2;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300127};
128
Matan Barak50090102017-08-03 16:06:58 +0300129/*
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600130 * 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.
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200143 *
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.
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600150 */
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,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200157 UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600158
159 UVERBS_API_METHOD_KEY_BITS = 5,
160 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200161 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,
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600167 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
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200219static 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)
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600235{
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{
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200242 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
243
244 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600245 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
246}
247
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200248static 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
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600259static 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
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200288/* Only true for ioctl methods */
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600289static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
290{
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200291 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
292
293 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600294 (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
Guy Levi70cd20a2018-09-06 17:27:01 +0300308static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
309{
310 return attr_bkey + 1;
311}
312
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600313/*
Matan Barak50090102017-08-03 16:06:58 +0300314 * =======================================
315 * Verbs definitions
316 * =======================================
317 */
318
Matan Barak09e3ebf2017-08-03 16:06:59 +0300319struct 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)[];
Jason Gunthorpe15a1b4b2018-11-25 20:51:15 +0200330 int (*handler)(struct uverbs_attr_bundle *attrs);
Matan Barak09e3ebf2017-08-03 16:06:59 +0300331};
332
Matan Barak50090102017-08-03 16:06:58 +0300333struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300334 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300335 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300336 size_t num_methods;
337 const struct uverbs_method_def * const (*methods)[];
338};
339
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200340enum uapi_definition_kind {
341 UAPI_DEF_END = 0,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200342 UAPI_DEF_OBJECT_START,
343 UAPI_DEF_WRITE,
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200344 UAPI_DEF_CHAIN_OBJ_TREE,
345 UAPI_DEF_CHAIN,
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200346 UAPI_DEF_IS_SUPPORTED_FUNC,
347 UAPI_DEF_IS_SUPPORTED_DEV_FN,
348};
349
350enum uapi_definition_scope {
351 UAPI_SCOPE_OBJECT = 1,
Jason Gunthorpea1406922018-11-12 22:59:58 +0200352 UAPI_SCOPE_METHOD = 2,
Matan Barak50090102017-08-03 16:06:58 +0300353};
354
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200355struct uapi_definition {
356 u8 kind;
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200357 u8 scope;
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200358 union {
359 struct {
360 u16 object_id;
361 } object_start;
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200362 struct {
363 u8 is_ex;
364 u16 command_num;
365 } write;
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200366 };
367
368 union {
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200369 bool (*func_is_supported)(struct ib_device *device);
Jason Gunthorpe7106a972018-11-25 20:51:14 +0200370 int (*func_write)(struct uverbs_attr_bundle *attrs,
371 const char __user *buf, int in_len,
372 int out_len);
Jason Gunthorpe8313c102018-11-25 20:51:13 +0200373 int (*func_write_ex)(struct uverbs_attr_bundle *attrs,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200374 struct ib_udata *ucore,
375 struct ib_udata *uhw);
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200376 const struct uapi_definition *chain;
377 const struct uverbs_object_def *chain_obj_tree;
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200378 size_t needs_fn_offset;
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200379 };
380};
381
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200382/* Define things connected to object_id */
383#define DECLARE_UVERBS_OBJECT(_object_id, ...) \
384 { \
385 .kind = UAPI_DEF_OBJECT_START, \
386 .object_start = { .object_id = _object_id }, \
387 }, \
388 ##__VA_ARGS__
389
390/* Use in a var_args of DECLARE_UVERBS_OBJECT */
391#define DECLARE_UVERBS_WRITE(_command_num, _func, ...) \
392 { \
393 .kind = UAPI_DEF_WRITE, \
394 .scope = UAPI_SCOPE_OBJECT, \
395 .write = { .is_ex = 0, .command_num = _command_num }, \
396 .func_write = _func, \
397 }, \
398 ##__VA_ARGS__
399
400/* Use in a var_args of DECLARE_UVERBS_OBJECT */
401#define DECLARE_UVERBS_WRITE_EX(_command_num, _func, ...) \
402 { \
403 .kind = UAPI_DEF_WRITE, \
404 .scope = UAPI_SCOPE_OBJECT, \
405 .write = { .is_ex = 1, .command_num = _command_num }, \
406 .func_write_ex = _func, \
407 }, \
408 ##__VA_ARGS__
409
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200410/*
411 * Object is only supported if the function pointer named ibdev_fn in struct
412 * ib_device is not NULL.
413 */
414#define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
415 { \
416 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
417 .scope = UAPI_SCOPE_OBJECT, \
418 .needs_fn_offset = \
419 offsetof(struct ib_device, ibdev_fn) + \
420 BUILD_BUG_ON_ZERO( \
421 sizeof(((struct ib_device *)0)->ibdev_fn) != \
422 sizeof(void *)), \
423 }
424
Jason Gunthorpea1406922018-11-12 22:59:58 +0200425/*
426 * Method is only supported if the function pointer named ibdev_fn in struct
427 * ib_device is not NULL.
428 */
429#define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
430 { \
431 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
432 .scope = UAPI_SCOPE_METHOD, \
433 .needs_fn_offset = \
434 offsetof(struct ib_device, ibdev_fn) + \
435 BUILD_BUG_ON_ZERO( \
436 sizeof(((struct ib_device *)0)->ibdev_fn) != \
437 sizeof(void *)), \
438 }
439
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200440/* Call a function to determine if the entire object is supported or not */
441#define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
442 { \
443 .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
444 .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
445 }
446
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200447/* Include another struct uapi_definition in this one */
448#define UAPI_DEF_CHAIN(_def_var) \
449 { \
450 .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
451 }
452
453/* Temporary until the tree base description is replaced */
454#define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr) \
455 { \
456 .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
457 .object_start = { .object_id = _object_enum }, \
458 .chain_obj_tree = _object_ptr, \
459 }
460#define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
461 UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum)), \
462 ##__VA_ARGS__
463
Jason Gunthorped108dac2018-07-04 08:50:25 +0300464/*
465 * =======================================
466 * Attribute Specifications
467 * =======================================
468 */
Matan Barakc66db312018-03-19 15:02:36 +0200469
Matan Barakc66db312018-03-19 15:02:36 +0200470#define UVERBS_ATTR_SIZE(_min_len, _len) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300471 .u.ptr.min_len = _min_len, .u.ptr.len = _len
Jason Gunthorpe422e3d32018-07-04 08:50:31 +0300472
Yishai Hadasfd44e382018-07-23 15:25:07 +0300473#define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
474
Jason Gunthorpe422e3d32018-07-04 08:50:31 +0300475/*
476 * Specifies a uapi structure that cannot be extended. The user must always
477 * supply the whole structure and nothing more. The structure must be declared
478 * in a header under include/uapi/rdma.
479 */
480#define UVERBS_ATTR_TYPE(_type) \
481 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
482/*
483 * Specifies a uapi structure where the user must provide at least up to
484 * member 'last'. Anything after last and up until the end of the structure
485 * can be non-zero, anything longer than the end of the structure must be
486 * zero. The structure must be declared in a header under include/uapi/rdma.
487 */
488#define UVERBS_ATTR_STRUCT(_type, _last) \
489 .zero_trailing = 1, \
490 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
491 sizeof(_type))
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300492/*
493 * Specifies at least min_len bytes must be passed in, but the amount can be
494 * larger, up to the protocol maximum size. No check for zeroing is done.
495 */
496#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
Matan Barakc66db312018-03-19 15:02:36 +0200497
Jason Gunthorped108dac2018-07-04 08:50:25 +0300498/* Must be used in the '...' of any UVERBS_ATTR */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300499#define UA_ALLOC_AND_COPY .alloc_and_copy = 1
500#define UA_MANDATORY .mandatory = 1
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300501#define UA_OPTIONAL .mandatory = 0
Jason Gunthorped108dac2018-07-04 08:50:25 +0300502
Guy Levi70cd20a2018-09-06 17:27:01 +0300503/*
504 * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
505 * READ\WRITE accesses are supported.
506 */
507#define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
508 ...) \
509 (&(const struct uverbs_attr_def){ \
510 .id = (_attr_id) + \
511 BUILD_BUG_ON_ZERO((_min_len) == 0 || \
512 (_max_len) > \
513 PAGE_SIZE / sizeof(void *) || \
514 (_min_len) > (_max_len) || \
515 (_access) == UVERBS_ACCESS_NEW || \
516 (_access) == UVERBS_ACCESS_DESTROY), \
517 .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
518 .u2.objs_arr.obj_type = _idr_type, \
519 .u2.objs_arr.access = _access, \
520 .u2.objs_arr.min_len = _min_len, \
521 .u2.objs_arr.max_len = _max_len, \
522 __VA_ARGS__ } })
523
Jason Gunthorped108dac2018-07-04 08:50:25 +0300524#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300525 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300526 .id = _attr_id, \
527 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
528 .u.obj.obj_type = _idr_type, \
529 .u.obj.access = _access, \
530 __VA_ARGS__ } })
531
532#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300533 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300534 .id = (_attr_id) + \
535 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
536 (_access) != UVERBS_ACCESS_READ), \
537 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
538 .u.obj.obj_type = _fd_type, \
539 .u.obj.access = _access, \
540 __VA_ARGS__ } })
541
542#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300543 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300544 .id = _attr_id, \
545 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
546 _type, \
547 __VA_ARGS__ } })
548
549#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300550 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300551 .id = _attr_id, \
552 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
553 _type, \
554 __VA_ARGS__ } })
555
556/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
557#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300558 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300559 .id = _attr_id, \
560 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
561 .u2.enum_def.ids = _enum_arr, \
562 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
563 __VA_ARGS__ }, \
564 })
Matan Barak35410302017-08-03 16:07:01 +0300565
Mark Bloch0953fff2018-08-28 14:18:50 +0300566/* An input value that is a member in the enum _enum_type. */
567#define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
568 UVERBS_ATTR_PTR_IN( \
569 _attr_id, \
570 UVERBS_ATTR_SIZE( \
571 sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
572 sizeof(u64)), \
573 __VA_ARGS__)
574
Matan Barak35410302017-08-03 16:07:01 +0300575/*
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600576 * An input value that is a bitwise combination of values of _enum_type.
577 * This permits the flag value to be passed as either a u32 or u64, it must
578 * be retrieved via uverbs_get_flag().
579 */
580#define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
581 UVERBS_ATTR_PTR_IN( \
582 _attr_id, \
583 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
584 !sizeof(_enum_type *)), \
585 sizeof(u64)), \
586 __VA_ARGS__)
587
588/*
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300589 * This spec is used in order to pass information to the hardware driver in a
590 * legacy way. Every verb that could get driver specific data should get this
591 * spec.
592 */
593#define UVERBS_ATTR_UHW() \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300594 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300595 UVERBS_ATTR_MIN_SIZE(0), \
596 UA_OPTIONAL), \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300597 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300598 UVERBS_ATTR_MIN_SIZE(0), \
599 UA_OPTIONAL)
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300600
Matan Barakfac96582017-08-03 16:06:57 +0300601/* =================================================
602 * Parsing infrastructure
603 * =================================================
604 */
605
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600606
Matan Barakfac96582017-08-03 16:06:57 +0300607struct uverbs_ptr_attr {
Matan Barak8762d142018-06-17 12:59:52 +0300608 /*
609 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
610 * used.
611 */
612 union {
613 void *ptr;
614 u64 data;
615 };
Matan Barakfac96582017-08-03 16:06:57 +0300616 u16 len;
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600617 u16 uattr_idx;
Matan Barak494c5582018-03-28 09:27:42 +0300618 u8 enum_id;
Matan Barakfac96582017-08-03 16:06:57 +0300619};
620
Matan Barakf43dbeb2017-08-03 16:06:56 +0300621struct uverbs_obj_attr {
622 struct ib_uobject *uobject;
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600623 const struct uverbs_api_attr *attr_elm;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300624};
625
Guy Levi70cd20a2018-09-06 17:27:01 +0300626struct uverbs_objs_arr_attr {
627 struct ib_uobject **uobjects;
628 u16 len;
629};
630
Matan Barakf43dbeb2017-08-03 16:06:56 +0300631struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300632 union {
633 struct uverbs_ptr_attr ptr_attr;
634 struct uverbs_obj_attr obj_attr;
Guy Levi70cd20a2018-09-06 17:27:01 +0300635 struct uverbs_objs_arr_attr objs_arr_attr;
Matan Barakfac96582017-08-03 16:06:57 +0300636 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300637};
638
Matan Barakf43dbeb2017-08-03 16:06:56 +0300639struct uverbs_attr_bundle {
Jason Gunthorpe4b3dd2b2018-08-09 20:14:38 -0600640 struct ib_uverbs_file *ufile;
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600641 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
642 struct uverbs_attr attrs[];
Matan Barakf43dbeb2017-08-03 16:06:56 +0300643};
644
Matan Barak35410302017-08-03 16:07:01 +0300645static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
646 unsigned int idx)
647{
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600648 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
649 attrs_bundle->attr_present);
Matan Barak35410302017-08-03 16:07:01 +0300650}
651
Matan Barak41b2a712018-03-19 15:02:38 +0200652#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
653
Matan Barakd70724f2017-08-03 16:07:04 +0300654static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
655 u16 idx)
656{
Matan Barakd70724f2017-08-03 16:07:04 +0300657 if (!uverbs_attr_is_valid(attrs_bundle, idx))
658 return ERR_PTR(-ENOENT);
659
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600660 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
Matan Barakd70724f2017-08-03 16:07:04 +0300661}
662
Matan Barak494c5582018-03-28 09:27:42 +0300663static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
664 u16 idx)
665{
666 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
667
668 if (IS_ERR(attr))
669 return PTR_ERR(attr);
670
671 return attr->ptr_attr.enum_id;
672}
673
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300674static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
675 u16 idx)
676{
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600677 const struct uverbs_attr *attr;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300678
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600679 attr = uverbs_attr_get(attrs_bundle, idx);
680 if (IS_ERR(attr))
681 return ERR_CAST(attr);
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300682
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600683 return attr->obj_attr.uobject->object;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300684}
685
Matan Barak3efa3812018-05-31 16:43:28 +0300686static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
687 u16 idx)
688{
689 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
690
691 if (IS_ERR(attr))
692 return ERR_CAST(attr);
693
694 return attr->obj_attr.uobject;
695}
696
Matan Barak8762d142018-06-17 12:59:52 +0300697static inline int
698uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
699{
700 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
701
702 if (IS_ERR(attr))
703 return PTR_ERR(attr);
704
705 return attr->ptr_attr.len;
706}
707
Guy Levi70cd20a2018-09-06 17:27:01 +0300708/**
709 * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
710 * UVERBS_ATTR_TYPE_IDRS_ARRAY.
711 * @arr: Returned pointer to array of pointers for uobjects or NULL if
712 * the attribute isn't provided.
713 *
714 * Return: The array length or 0 if no attribute was provided.
715 */
716static inline int uverbs_attr_get_uobjs_arr(
717 const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
718 struct ib_uobject ***arr)
719{
720 const struct uverbs_attr *attr =
721 uverbs_attr_get(attrs_bundle, attr_idx);
722
723 if (IS_ERR(attr)) {
724 *arr = NULL;
725 return 0;
726 }
727
728 *arr = attr->objs_arr_attr.uobjects;
729
730 return attr->objs_arr_attr.len;
731}
732
Matan Barak89d9e8d2018-02-13 12:18:29 +0200733static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
734{
735 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
736}
737
Matan Barak8762d142018-06-17 12:59:52 +0300738static inline void *uverbs_attr_get_alloced_ptr(
739 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
740{
741 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
742
743 if (IS_ERR(attr))
744 return (void *)attr;
745
746 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
747 attr->ptr_attr.ptr;
748}
749
Matan Barak89d9e8d2018-02-13 12:18:29 +0200750static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300751 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200752 size_t idx,
753 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300754{
755 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
756
757 if (IS_ERR(attr))
758 return PTR_ERR(attr);
759
Matan Barak89d9e8d2018-02-13 12:18:29 +0200760 /*
761 * Validation ensures attr->ptr_attr.len >= size. If the caller is
Matan Barakc66db312018-03-19 15:02:36 +0200762 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
763 * uverbs_copy_from_or_zero.
Matan Barak89d9e8d2018-02-13 12:18:29 +0200764 */
765 if (unlikely(size < attr->ptr_attr.len))
766 return -EINVAL;
767
768 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300769 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200770 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
771 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300772 return -EFAULT;
773
774 return 0;
775}
776
Matan Barakc66db312018-03-19 15:02:36 +0200777static inline int _uverbs_copy_from_or_zero(void *to,
778 const struct uverbs_attr_bundle *attrs_bundle,
779 size_t idx,
780 size_t size)
781{
782 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
783 size_t min_size;
784
785 if (IS_ERR(attr))
786 return PTR_ERR(attr);
787
788 min_size = min_t(size_t, size, attr->ptr_attr.len);
789
790 if (uverbs_attr_ptr_is_inline(attr))
791 memcpy(to, &attr->ptr_attr.data, min_size);
792 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
793 min_size))
794 return -EFAULT;
795
796 if (size > min_size)
797 memset(to + min_size, 0, size - min_size);
798
799 return 0;
800}
801
Matan Barakd70724f2017-08-03 16:07:04 +0300802#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200803 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300804
Matan Barakc66db312018-03-19 15:02:36 +0200805#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
806 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
807
Jason Gunthorpe8313c102018-11-25 20:51:13 +0200808static inline struct ib_ucontext *
809ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
810{
811 return ib_uverbs_get_ucontext_file(attrs->ufile);
812}
813
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600814#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
815int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
816 size_t idx, u64 allowed_bits);
817int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
818 size_t idx, u64 allowed_bits);
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600819int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
820 const void *from, size_t size);
Jason Gunthorpe461bb2e2018-08-09 20:14:40 -0600821__malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
822 gfp_t flags);
823
824static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
825 size_t size)
826{
827 return _uverbs_alloc(bundle, size, GFP_KERNEL);
828}
829
830static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
831 size_t size)
832{
833 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
834}
Mark Bloch0953fff2018-08-28 14:18:50 +0300835int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
836 size_t idx, s64 lower_bound, u64 upper_bound,
837 s64 *def_val);
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600838#else
839static inline int
840uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
841 size_t idx, u64 allowed_bits)
842{
843 return -EINVAL;
844}
845static inline int
846uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
847 size_t idx, u64 allowed_bits)
848{
849 return -EINVAL;
850}
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600851static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
852 size_t idx, const void *from, size_t size)
853{
854 return -EINVAL;
855}
Jason Gunthorpe461bb2e2018-08-09 20:14:40 -0600856static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
857 size_t size)
858{
859 return ERR_PTR(-EINVAL);
860}
861static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
862 size_t size)
863{
864 return ERR_PTR(-EINVAL);
865}
Mark Bloch0953fff2018-08-28 14:18:50 +0300866static inline int
867_uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
868 size_t idx, s64 lower_bound, u64 upper_bound,
869 s64 *def_val)
870{
871 return -EINVAL;
872}
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600873#endif
874
Mark Bloch0953fff2018-08-28 14:18:50 +0300875#define uverbs_get_const(_to, _attrs_bundle, _idx) \
876 ({ \
877 s64 _val; \
878 int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
879 type_min(typeof(*_to)), \
880 type_max(typeof(*_to)), NULL); \
881 (*_to) = _val; \
882 _ret; \
883 })
884
885#define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
886 ({ \
887 s64 _val; \
888 s64 _def_val = _default; \
889 int _ret = \
890 _uverbs_get_const(&_val, _attrs_bundle, _idx, \
891 type_min(typeof(*_to)), \
892 type_max(typeof(*_to)), &_def_val); \
893 (*_to) = _val; \
894 _ret; \
895 })
Matan Barak118620d2017-08-03 16:07:00 +0300896#endif