blob: 9f3b1e0040468160e0e200ff7cfaaa5811597a9c [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 Gunthorpe07f05f42018-11-25 20:51:20 +020082 /* True if this is from UVERBS_ATTR_UHW */
83 u8 is_udata:1;
Jason Gunthorped108dac2018-07-04 08:50:25 +030084
Matan Barakfac96582017-08-03 16:06:57 +030085 union {
Matan Barakfac96582017-08-03 16:06:57 +030086 struct {
Matan Barakc66db312018-03-19 15:02:36 +020087 /* Current known size to kernel */
Jason Gunthorped108dac2018-07-04 08:50:25 +030088 u16 len;
Matan Barakc66db312018-03-19 15:02:36 +020089 /* User isn't allowed to provide something < min_len */
Jason Gunthorped108dac2018-07-04 08:50:25 +030090 u16 min_len;
Matan Barak1f07e08f2018-03-19 15:02:35 +020091 } ptr;
Jason Gunthorped108dac2018-07-04 08:50:25 +030092
Matan Barak1f07e08f2018-03-19 15:02:35 +020093 struct {
Matan Barakfac96582017-08-03 16:06:57 +030094 /*
95 * higher bits mean the namespace and lower bits mean
96 * the type id within the namespace.
97 */
Jason Gunthorped108dac2018-07-04 08:50:25 +030098 u16 obj_type;
99 u8 access;
Matan Barakfac96582017-08-03 16:06:57 +0300100 } obj;
Jason Gunthorped108dac2018-07-04 08:50:25 +0300101
Matan Barak494c5582018-03-28 09:27:42 +0300102 struct {
Jason Gunthorped108dac2018-07-04 08:50:25 +0300103 u8 num_elems;
104 } enum_def;
105 } u;
106
Guy Levi70cd20a2018-09-06 17:27:01 +0300107 /* This weird split lets us remove some padding */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300108 union {
109 struct {
Matan Barak494c5582018-03-28 09:27:42 +0300110 /*
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 */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300115 const struct uverbs_attr_spec *ids;
Matan Barak494c5582018-03-28 09:27:42 +0300116 } enum_def;
Guy Levi70cd20a2018-09-06 17:27:01 +0300117
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;
Jason Gunthorped108dac2018-07-04 08:50:25 +0300128 } u2;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300129};
130
Matan Barak50090102017-08-03 16:06:58 +0300131/*
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600132 * 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.
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200145 *
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.
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600152 */
153enum 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,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200159 UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600160
161 UVERBS_API_METHOD_KEY_BITS = 5,
162 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200163 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,
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600169 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,
Yishai Hadas342ee592020-03-24 08:01:39 +0200176 UVERBS_API_OBJ_KEY_NUM_CORE = 20,
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600177 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
185static 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
200static inline __attribute_const__ bool uapi_key_is_object(u32 key)
201{
202 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
203}
204
205static 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
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200221static 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
228static 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
235static inline __attribute_const__ u32
236uapi_key_attr_to_ioctl_method(u32 attr_key)
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600237{
238 return attr_key &
239 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
240}
241
242static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
243{
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200244 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
245
246 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600247 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
248}
249
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200250static 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
255static 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
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600261static 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
267static 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
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200290/* Only true for ioctl methods */
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600291static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
292{
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200293 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
294
295 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600296 (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 */
305static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
306{
307 return attr_key - 1;
308}
309
Guy Levi70cd20a2018-09-06 17:27:01 +0300310static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
311{
312 return attr_bkey + 1;
313}
314
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600315/*
Matan Barak50090102017-08-03 16:06:58 +0300316 * =======================================
317 * Verbs definitions
318 * =======================================
319 */
320
Matan Barak09e3ebf2017-08-03 16:06:59 +0300321struct uverbs_attr_def {
322 u16 id;
323 struct uverbs_attr_spec attr;
324};
325
326struct 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)[];
Jason Gunthorpe15a1b4b2018-11-25 20:51:15 +0200332 int (*handler)(struct uverbs_attr_bundle *attrs);
Matan Barak09e3ebf2017-08-03 16:06:59 +0300333};
334
Matan Barak50090102017-08-03 16:06:58 +0300335struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300336 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300337 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300338 size_t num_methods;
339 const struct uverbs_method_def * const (*methods)[];
340};
341
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200342enum uapi_definition_kind {
343 UAPI_DEF_END = 0,
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200344 UAPI_DEF_OBJECT_START,
345 UAPI_DEF_WRITE,
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200346 UAPI_DEF_CHAIN_OBJ_TREE,
347 UAPI_DEF_CHAIN,
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200348 UAPI_DEF_IS_SUPPORTED_FUNC,
349 UAPI_DEF_IS_SUPPORTED_DEV_FN,
350};
351
352enum uapi_definition_scope {
353 UAPI_SCOPE_OBJECT = 1,
Jason Gunthorpea1406922018-11-12 22:59:58 +0200354 UAPI_SCOPE_METHOD = 2,
Matan Barak50090102017-08-03 16:06:58 +0300355};
356
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200357struct uapi_definition {
358 u8 kind;
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200359 u8 scope;
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200360 union {
361 struct {
362 u16 object_id;
363 } object_start;
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200364 struct {
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200365 u16 command_num;
Jason Gunthorpe669dac12018-11-25 20:51:16 +0200366 u8 is_ex:1;
367 u8 has_udata:1;
368 u8 has_resp:1;
369 u8 req_size;
370 u8 resp_size;
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200371 } write;
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200372 };
373
374 union {
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200375 bool (*func_is_supported)(struct ib_device *device);
Jason Gunthorpe974d6b42018-11-25 20:58:45 +0200376 int (*func_write)(struct uverbs_attr_bundle *attrs);
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200377 const struct uapi_definition *chain;
378 const struct uverbs_object_def *chain_obj_tree;
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200379 size_t needs_fn_offset;
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200380 };
381};
382
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200383/* 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 */
Jason Gunthorpe669dac12018-11-25 20:51:16 +0200392#define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200393 { \
394 .kind = UAPI_DEF_WRITE, \
395 .scope = UAPI_SCOPE_OBJECT, \
396 .write = { .is_ex = 0, .command_num = _command_num }, \
397 .func_write = _func, \
Jason Gunthorpe669dac12018-11-25 20:51:16 +0200398 _cmd_desc, \
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200399 }, \
400 ##__VA_ARGS__
401
402/* Use in a var_args of DECLARE_UVERBS_OBJECT */
Jason Gunthorpe669dac12018-11-25 20:51:16 +0200403#define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200404 { \
405 .kind = UAPI_DEF_WRITE, \
406 .scope = UAPI_SCOPE_OBJECT, \
407 .write = { .is_ex = 1, .command_num = _command_num }, \
Jason Gunthorpe974d6b42018-11-25 20:58:45 +0200408 .func_write = _func, \
Jason Gunthorpe669dac12018-11-25 20:51:16 +0200409 _cmd_desc, \
Jason Gunthorpe6884c6c2018-11-12 22:59:55 +0200410 }, \
411 ##__VA_ARGS__
412
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200413/*
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 = \
Kamal Heib3023a1e2018-12-10 21:09:48 +0200422 offsetof(struct ib_device_ops, ibdev_fn) + \
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200423 BUILD_BUG_ON_ZERO( \
Kamal Heib3023a1e2018-12-10 21:09:48 +0200424 sizeof(((struct ib_device_ops *)0)->ibdev_fn) != \
425 sizeof(void *)), \
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200426 }
427
Jason Gunthorpea1406922018-11-12 22:59:58 +0200428/*
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 = \
Kamal Heib3023a1e2018-12-10 21:09:48 +0200437 offsetof(struct ib_device_ops, ibdev_fn) + \
Jason Gunthorpea1406922018-11-12 22:59:58 +0200438 BUILD_BUG_ON_ZERO( \
Kamal Heib3023a1e2018-12-10 21:09:48 +0200439 sizeof(((struct ib_device_ops *)0)->ibdev_fn) != \
440 sizeof(void *)), \
Jason Gunthorpea1406922018-11-12 22:59:58 +0200441 }
442
Jason Gunthorpe6829c1c2018-11-12 22:59:52 +0200443/* 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
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200450/* 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 */
Leon Romanovskya1462352018-12-10 11:19:49 +0200457#define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200458 { \
459 .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
460 .object_start = { .object_id = _object_enum }, \
461 .chain_obj_tree = _object_ptr, \
Leon Romanovskya1462352018-12-10 11:19:49 +0200462 }, \
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200463 ##__VA_ARGS__
Leon Romanovskya1462352018-12-10 11:19:49 +0200464#define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
465 UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum), \
466 ##__VA_ARGS__)
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +0200467
Jason Gunthorped108dac2018-07-04 08:50:25 +0300468/*
469 * =======================================
470 * Attribute Specifications
471 * =======================================
472 */
Matan Barakc66db312018-03-19 15:02:36 +0200473
Matan Barakc66db312018-03-19 15:02:36 +0200474#define UVERBS_ATTR_SIZE(_min_len, _len) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300475 .u.ptr.min_len = _min_len, .u.ptr.len = _len
Jason Gunthorpe422e3d32018-07-04 08:50:31 +0300476
Yishai Hadasfd44e382018-07-23 15:25:07 +0300477#define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
478
Jason Gunthorpe422e3d32018-07-04 08:50:31 +0300479/*
480 * Specifies a uapi structure that cannot be extended. The user must always
481 * supply the whole structure and nothing more. The structure must be declared
482 * in a header under include/uapi/rdma.
483 */
484#define UVERBS_ATTR_TYPE(_type) \
485 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
486/*
487 * Specifies a uapi structure where the user must provide at least up to
488 * member 'last'. Anything after last and up until the end of the structure
489 * can be non-zero, anything longer than the end of the structure must be
490 * zero. The structure must be declared in a header under include/uapi/rdma.
491 */
492#define UVERBS_ATTR_STRUCT(_type, _last) \
493 .zero_trailing = 1, \
494 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
495 sizeof(_type))
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300496/*
497 * Specifies at least min_len bytes must be passed in, but the amount can be
498 * larger, up to the protocol maximum size. No check for zeroing is done.
499 */
500#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
Matan Barakc66db312018-03-19 15:02:36 +0200501
Jason Gunthorped108dac2018-07-04 08:50:25 +0300502/* Must be used in the '...' of any UVERBS_ATTR */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300503#define UA_ALLOC_AND_COPY .alloc_and_copy = 1
504#define UA_MANDATORY .mandatory = 1
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300505#define UA_OPTIONAL .mandatory = 0
Jason Gunthorped108dac2018-07-04 08:50:25 +0300506
Guy Levi70cd20a2018-09-06 17:27:01 +0300507/*
508 * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
509 * READ\WRITE accesses are supported.
510 */
511#define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
512 ...) \
513 (&(const struct uverbs_attr_def){ \
514 .id = (_attr_id) + \
515 BUILD_BUG_ON_ZERO((_min_len) == 0 || \
516 (_max_len) > \
517 PAGE_SIZE / sizeof(void *) || \
518 (_min_len) > (_max_len) || \
519 (_access) == UVERBS_ACCESS_NEW || \
520 (_access) == UVERBS_ACCESS_DESTROY), \
521 .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
522 .u2.objs_arr.obj_type = _idr_type, \
523 .u2.objs_arr.access = _access, \
524 .u2.objs_arr.min_len = _min_len, \
525 .u2.objs_arr.max_len = _max_len, \
526 __VA_ARGS__ } })
527
Yishai Hadas4d7e8cc2018-11-26 08:28:33 +0200528/*
529 * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
530 * the user must validate the type of the uobject instead.
531 */
532#define UVERBS_IDR_ANY_OBJECT 0xFFFF
533
Jason Gunthorped108dac2018-07-04 08:50:25 +0300534#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300535 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300536 .id = _attr_id, \
537 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
538 .u.obj.obj_type = _idr_type, \
539 .u.obj.access = _access, \
540 __VA_ARGS__ } })
541
542#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
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 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
546 (_access) != UVERBS_ACCESS_READ), \
547 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
548 .u.obj.obj_type = _fd_type, \
549 .u.obj.access = _access, \
550 __VA_ARGS__ } })
551
552#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300553 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300554 .id = _attr_id, \
555 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
556 _type, \
557 __VA_ARGS__ } })
558
559#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300560 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300561 .id = _attr_id, \
562 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
563 _type, \
564 __VA_ARGS__ } })
565
566/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
567#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300568 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300569 .id = _attr_id, \
570 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
571 .u2.enum_def.ids = _enum_arr, \
572 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
573 __VA_ARGS__ }, \
574 })
Matan Barak35410302017-08-03 16:07:01 +0300575
Mark Bloch0953fff2018-08-28 14:18:50 +0300576/* An input value that is a member in the enum _enum_type. */
577#define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
578 UVERBS_ATTR_PTR_IN( \
579 _attr_id, \
580 UVERBS_ATTR_SIZE( \
581 sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
582 sizeof(u64)), \
583 __VA_ARGS__)
584
Matan Barak35410302017-08-03 16:07:01 +0300585/*
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600586 * An input value that is a bitwise combination of values of _enum_type.
587 * This permits the flag value to be passed as either a u32 or u64, it must
588 * be retrieved via uverbs_get_flag().
589 */
590#define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
591 UVERBS_ATTR_PTR_IN( \
592 _attr_id, \
593 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
594 !sizeof(_enum_type *)), \
595 sizeof(u64)), \
596 __VA_ARGS__)
597
598/*
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300599 * This spec is used in order to pass information to the hardware driver in a
600 * legacy way. Every verb that could get driver specific data should get this
601 * spec.
602 */
603#define UVERBS_ATTR_UHW() \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300604 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300605 UVERBS_ATTR_MIN_SIZE(0), \
Jason Gunthorpe07f05f42018-11-25 20:51:20 +0200606 UA_OPTIONAL, \
607 .is_udata = 1), \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300608 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300609 UVERBS_ATTR_MIN_SIZE(0), \
Jason Gunthorpe07f05f42018-11-25 20:51:20 +0200610 UA_OPTIONAL, \
611 .is_udata = 1)
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300612
Matan Barakfac96582017-08-03 16:06:57 +0300613/* =================================================
614 * Parsing infrastructure
615 * =================================================
616 */
617
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600618
Matan Barakfac96582017-08-03 16:06:57 +0300619struct uverbs_ptr_attr {
Matan Barak8762d142018-06-17 12:59:52 +0300620 /*
621 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
622 * used.
623 */
624 union {
625 void *ptr;
626 u64 data;
627 };
Matan Barakfac96582017-08-03 16:06:57 +0300628 u16 len;
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600629 u16 uattr_idx;
Matan Barak494c5582018-03-28 09:27:42 +0300630 u8 enum_id;
Matan Barakfac96582017-08-03 16:06:57 +0300631};
632
Matan Barakf43dbeb2017-08-03 16:06:56 +0300633struct uverbs_obj_attr {
634 struct ib_uobject *uobject;
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600635 const struct uverbs_api_attr *attr_elm;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300636};
637
Guy Levi70cd20a2018-09-06 17:27:01 +0300638struct uverbs_objs_arr_attr {
639 struct ib_uobject **uobjects;
640 u16 len;
641};
642
Matan Barakf43dbeb2017-08-03 16:06:56 +0300643struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300644 union {
645 struct uverbs_ptr_attr ptr_attr;
646 struct uverbs_obj_attr obj_attr;
Guy Levi70cd20a2018-09-06 17:27:01 +0300647 struct uverbs_objs_arr_attr objs_arr_attr;
Matan Barakfac96582017-08-03 16:06:57 +0300648 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300649};
650
Matan Barakf43dbeb2017-08-03 16:06:56 +0300651struct uverbs_attr_bundle {
Jason Gunthorpeef87df2c2018-11-25 20:51:18 +0200652 struct ib_udata driver_udata;
Jason Gunthorpec2a939f2018-11-25 20:58:35 +0200653 struct ib_udata ucore;
Jason Gunthorpe4b3dd2b2018-08-09 20:14:38 -0600654 struct ib_uverbs_file *ufile;
Shamir Rabinovitch3d9dfd02019-02-07 18:44:47 +0200655 struct ib_ucontext *context;
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600656 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
657 struct uverbs_attr attrs[];
Matan Barakf43dbeb2017-08-03 16:06:56 +0300658};
659
Matan Barak35410302017-08-03 16:07:01 +0300660static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
661 unsigned int idx)
662{
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600663 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
664 attrs_bundle->attr_present);
Matan Barak35410302017-08-03 16:07:01 +0300665}
666
Shamir Rabinovitch730623f2019-02-07 18:44:48 +0200667/**
668 * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
669 * ib_udata which is embedded in uverbs_attr_bundle.
670 *
671 * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
672 * in a NULL ucontext pointer, as a safety precaution. Callers should be using
673 * 'udata' to determine if the driver call is in user or kernel mode, not
674 * 'ucontext'.
675 *
676 */
677#define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
678 (udata ? container_of(container_of(udata, struct uverbs_attr_bundle, \
679 driver_udata) \
680 ->context, \
681 drv_dev_struct, member) : \
682 (drv_dev_struct *)NULL)
683
Matan Barak41b2a712018-03-19 15:02:38 +0200684#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
685
Matan Barakd70724f2017-08-03 16:07:04 +0300686static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
687 u16 idx)
688{
Matan Barakd70724f2017-08-03 16:07:04 +0300689 if (!uverbs_attr_is_valid(attrs_bundle, idx))
690 return ERR_PTR(-ENOENT);
691
Jason Gunthorpe3a863572018-08-09 20:14:42 -0600692 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
Matan Barakd70724f2017-08-03 16:07:04 +0300693}
694
Matan Barak494c5582018-03-28 09:27:42 +0300695static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
696 u16 idx)
697{
698 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
699
700 if (IS_ERR(attr))
701 return PTR_ERR(attr);
702
703 return attr->ptr_attr.enum_id;
704}
705
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300706static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
707 u16 idx)
708{
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600709 const struct uverbs_attr *attr;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300710
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600711 attr = uverbs_attr_get(attrs_bundle, idx);
712 if (IS_ERR(attr))
713 return ERR_CAST(attr);
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300714
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600715 return attr->obj_attr.uobject->object;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300716}
717
Matan Barak3efa3812018-05-31 16:43:28 +0300718static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
719 u16 idx)
720{
721 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
722
723 if (IS_ERR(attr))
724 return ERR_CAST(attr);
725
726 return attr->obj_attr.uobject;
727}
728
Matan Barak8762d142018-06-17 12:59:52 +0300729static inline int
730uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
731{
732 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
733
734 if (IS_ERR(attr))
735 return PTR_ERR(attr);
736
737 return attr->ptr_attr.len;
738}
739
Moni Shouacbfdd442018-12-11 13:37:51 +0200740/*
741 * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
742 * attribute.
743 * @attrs: The attribute bundle
744 * @idx: The ID of the attribute
745 * @elem_size: The size of the element in the array
746 */
747static inline int
748uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
749 size_t elem_size)
750{
751 int size = uverbs_attr_get_len(attrs, idx);
752
753 if (size < 0)
754 return size;
755
756 if (size % elem_size)
757 return -EINVAL;
758
759 return size / elem_size;
760}
761
Guy Levi70cd20a2018-09-06 17:27:01 +0300762/**
763 * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
764 * UVERBS_ATTR_TYPE_IDRS_ARRAY.
765 * @arr: Returned pointer to array of pointers for uobjects or NULL if
766 * the attribute isn't provided.
767 *
768 * Return: The array length or 0 if no attribute was provided.
769 */
770static inline int uverbs_attr_get_uobjs_arr(
771 const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
772 struct ib_uobject ***arr)
773{
774 const struct uverbs_attr *attr =
775 uverbs_attr_get(attrs_bundle, attr_idx);
776
777 if (IS_ERR(attr)) {
778 *arr = NULL;
779 return 0;
780 }
781
782 *arr = attr->objs_arr_attr.uobjects;
783
784 return attr->objs_arr_attr.len;
785}
786
Matan Barak89d9e8d2018-02-13 12:18:29 +0200787static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
788{
789 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
790}
791
Matan Barak8762d142018-06-17 12:59:52 +0300792static inline void *uverbs_attr_get_alloced_ptr(
793 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
794{
795 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
796
797 if (IS_ERR(attr))
798 return (void *)attr;
799
800 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
801 attr->ptr_attr.ptr;
802}
803
Matan Barak89d9e8d2018-02-13 12:18:29 +0200804static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300805 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200806 size_t idx,
807 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300808{
809 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
810
811 if (IS_ERR(attr))
812 return PTR_ERR(attr);
813
Matan Barak89d9e8d2018-02-13 12:18:29 +0200814 /*
815 * Validation ensures attr->ptr_attr.len >= size. If the caller is
Matan Barakc66db312018-03-19 15:02:36 +0200816 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
817 * uverbs_copy_from_or_zero.
Matan Barak89d9e8d2018-02-13 12:18:29 +0200818 */
819 if (unlikely(size < attr->ptr_attr.len))
820 return -EINVAL;
821
822 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300823 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200824 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
825 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300826 return -EFAULT;
827
828 return 0;
829}
830
Matan Barakc66db312018-03-19 15:02:36 +0200831static inline int _uverbs_copy_from_or_zero(void *to,
832 const struct uverbs_attr_bundle *attrs_bundle,
833 size_t idx,
834 size_t size)
835{
836 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
837 size_t min_size;
838
839 if (IS_ERR(attr))
840 return PTR_ERR(attr);
841
842 min_size = min_t(size_t, size, attr->ptr_attr.len);
843
844 if (uverbs_attr_ptr_is_inline(attr))
845 memcpy(to, &attr->ptr_attr.data, min_size);
846 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
847 min_size))
848 return -EFAULT;
849
850 if (size > min_size)
851 memset(to + min_size, 0, size - min_size);
852
853 return 0;
854}
855
Matan Barakd70724f2017-08-03 16:07:04 +0300856#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200857 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300858
Matan Barakc66db312018-03-19 15:02:36 +0200859#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
860 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
861
Jason Gunthorpe8313c102018-11-25 20:51:13 +0200862static inline struct ib_ucontext *
863ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
864{
865 return ib_uverbs_get_ucontext_file(attrs->ufile);
866}
867
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600868#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
869int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
870 size_t idx, u64 allowed_bits);
871int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
872 size_t idx, u64 allowed_bits);
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600873int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
874 const void *from, size_t size);
Jason Gunthorpe461bb2e2018-08-09 20:14:40 -0600875__malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
876 gfp_t flags);
877
878static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
879 size_t size)
880{
881 return _uverbs_alloc(bundle, size, GFP_KERNEL);
882}
883
884static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
885 size_t size)
886{
887 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
888}
Mark Bloch0953fff2018-08-28 14:18:50 +0300889int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
890 size_t idx, s64 lower_bound, u64 upper_bound,
891 s64 *def_val);
Michael Guralnik2e8039c2018-12-09 11:58:04 +0200892int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
893 size_t idx, const void *from, size_t size);
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600894#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}
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600907static 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}
Jason Gunthorpe461bb2e2018-08-09 20:14:40 -0600912static 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}
Mark Bloch0953fff2018-08-28 14:18:50 +0300922static 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}
Michael Guralnik2e8039c2018-12-09 11:58:04 +0200929static 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}
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600935#endif
936
Mark Bloch0953fff2018-08-28 14:18:50 +0300937#define uverbs_get_const(_to, _attrs_bundle, _idx) \
938 ({ \
939 s64 _val; \
940 int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
941 type_min(typeof(*_to)), \
942 type_max(typeof(*_to)), NULL); \
943 (*_to) = _val; \
944 _ret; \
945 })
946
947#define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
948 ({ \
949 s64 _val; \
950 s64 _def_val = _default; \
951 int _ret = \
952 _uverbs_get_const(&_val, _attrs_bundle, _idx, \
953 type_min(typeof(*_to)), \
954 type_max(typeof(*_to)), &_def_val); \
955 (*_to) = _val; \
956 _ret; \
957 })
Matan Barak118620d2017-08-03 16:07:00 +0300958#endif