xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 70cd20aed00f719f3536154df02596106e431e45)
1a0aa309cSMatan Barak /*
2a0aa309cSMatan Barak  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
3a0aa309cSMatan Barak  *
4a0aa309cSMatan Barak  * This software is available to you under a choice of one of two
5a0aa309cSMatan Barak  * licenses.  You may choose to be licensed under the terms of the GNU
6a0aa309cSMatan Barak  * General Public License (GPL) Version 2, available from the file
7a0aa309cSMatan Barak  * COPYING in the main directory of this source tree, or the
8a0aa309cSMatan Barak  * OpenIB.org BSD license below:
9a0aa309cSMatan Barak  *
10a0aa309cSMatan Barak  *     Redistribution and use in source and binary forms, with or
11a0aa309cSMatan Barak  *     without modification, are permitted provided that the following
12a0aa309cSMatan Barak  *     conditions are met:
13a0aa309cSMatan Barak  *
14a0aa309cSMatan Barak  *      - Redistributions of source code must retain the above
15a0aa309cSMatan Barak  *        copyright notice, this list of conditions and the following
16a0aa309cSMatan Barak  *        disclaimer.
17a0aa309cSMatan Barak  *
18a0aa309cSMatan Barak  *      - Redistributions in binary form must reproduce the above
19a0aa309cSMatan Barak  *        copyright notice, this list of conditions and the following
20a0aa309cSMatan Barak  *        disclaimer in the documentation and/or other materials
21a0aa309cSMatan Barak  *        provided with the distribution.
22a0aa309cSMatan Barak  *
23a0aa309cSMatan Barak  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24a0aa309cSMatan Barak  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25a0aa309cSMatan Barak  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26a0aa309cSMatan Barak  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27a0aa309cSMatan Barak  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28a0aa309cSMatan Barak  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29a0aa309cSMatan Barak  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30a0aa309cSMatan Barak  * SOFTWARE.
31a0aa309cSMatan Barak  */
32a0aa309cSMatan Barak 
33a0aa309cSMatan Barak #ifndef _UVERBS_IOCTL_
34a0aa309cSMatan Barak #define _UVERBS_IOCTL_
35a0aa309cSMatan Barak 
36a0aa309cSMatan Barak #include <rdma/uverbs_types.h>
3735410306SMatan Barak #include <linux/uaccess.h>
3835410306SMatan Barak #include <rdma/rdma_user_ioctl.h>
39d70724f1SMatan Barak #include <rdma/ib_user_ioctl_verbs.h>
401f7ff9d5SMatan Barak #include <rdma/ib_user_ioctl_cmds.h>
41a0aa309cSMatan Barak 
42a0aa309cSMatan Barak /*
43a0aa309cSMatan Barak  * =======================================
44a0aa309cSMatan Barak  *	Verbs action specifications
45a0aa309cSMatan Barak  * =======================================
46a0aa309cSMatan Barak  */
47a0aa309cSMatan Barak 
48f43dbebfSMatan Barak enum uverbs_attr_type {
49f43dbebfSMatan Barak 	UVERBS_ATTR_TYPE_NA,
50fac9658cSMatan Barak 	UVERBS_ATTR_TYPE_PTR_IN,
51fac9658cSMatan Barak 	UVERBS_ATTR_TYPE_PTR_OUT,
52f43dbebfSMatan Barak 	UVERBS_ATTR_TYPE_IDR,
53f43dbebfSMatan Barak 	UVERBS_ATTR_TYPE_FD,
54494c5580SMatan Barak 	UVERBS_ATTR_TYPE_ENUM_IN,
55*70cd20aeSGuy Levi 	UVERBS_ATTR_TYPE_IDRS_ARRAY,
56f43dbebfSMatan Barak };
57f43dbebfSMatan Barak 
58a0aa309cSMatan Barak enum uverbs_obj_access {
59a0aa309cSMatan Barak 	UVERBS_ACCESS_READ,
60a0aa309cSMatan Barak 	UVERBS_ACCESS_WRITE,
61a0aa309cSMatan Barak 	UVERBS_ACCESS_NEW,
62a0aa309cSMatan Barak 	UVERBS_ACCESS_DESTROY
63a0aa309cSMatan Barak };
64a0aa309cSMatan Barak 
651f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */
6683bb4442SJason Gunthorpe /* good size 16 */
67f43dbebfSMatan Barak struct uverbs_attr_spec {
68d108dac0SJason Gunthorpe 	u8 type;
6983bb4442SJason Gunthorpe 
7083bb4442SJason Gunthorpe 	/*
71422e3d37SJason Gunthorpe 	 * Support extending attributes by length. Allow the user to provide
72422e3d37SJason Gunthorpe 	 * more bytes than ptr.len, but check that everything after is zero'd
73422e3d37SJason Gunthorpe 	 * by the user.
7483bb4442SJason Gunthorpe 	 */
75422e3d37SJason Gunthorpe 	u8 zero_trailing:1;
7683bb4442SJason Gunthorpe 	/*
7783bb4442SJason Gunthorpe 	 * Valid only for PTR_IN. Allocate and copy the data inside
7883bb4442SJason Gunthorpe 	 * the parser
7983bb4442SJason Gunthorpe 	 */
8083bb4442SJason Gunthorpe 	u8 alloc_and_copy:1;
8183bb4442SJason Gunthorpe 	u8 mandatory:1;
82d108dac0SJason Gunthorpe 
83fac9658cSMatan Barak 	union {
84f43dbebfSMatan Barak 		struct {
85c66db311SMatan Barak 			/* Current known size to kernel */
861f07e08fSMatan Barak 			u16 len;
87c66db311SMatan Barak 			/* User isn't allowed to provide something < min_len */
88c66db311SMatan Barak 			u16 min_len;
891f07e08fSMatan Barak 		} ptr;
90d108dac0SJason Gunthorpe 
911f07e08fSMatan Barak 		struct {
92f43dbebfSMatan Barak 			/*
93f43dbebfSMatan Barak 			 * higher bits mean the namespace and lower bits mean
94f43dbebfSMatan Barak 			 * the type id within the namespace.
95f43dbebfSMatan Barak 			 */
96f43dbebfSMatan Barak 			u16 obj_type;
97f43dbebfSMatan Barak 			u8 access;
98f43dbebfSMatan Barak 		} obj;
99d108dac0SJason Gunthorpe 
100494c5580SMatan Barak 		struct {
101494c5580SMatan Barak 			u8 num_elems;
102d108dac0SJason Gunthorpe 		} enum_def;
103d108dac0SJason Gunthorpe 	} u;
104d108dac0SJason Gunthorpe 
105*70cd20aeSGuy Levi 	/* This weird split lets us remove some padding */
106d108dac0SJason Gunthorpe 	union {
107d108dac0SJason Gunthorpe 		struct {
108494c5580SMatan Barak 			/*
109494c5580SMatan Barak 			 * The enum attribute can select one of the attributes
110494c5580SMatan Barak 			 * contained in the ids array. Currently only PTR_IN
111494c5580SMatan Barak 			 * attributes are supported in the ids array.
112494c5580SMatan Barak 			 */
113494c5580SMatan Barak 			const struct uverbs_attr_spec *ids;
114494c5580SMatan Barak 		} enum_def;
115*70cd20aeSGuy Levi 
116*70cd20aeSGuy Levi 		struct {
117*70cd20aeSGuy Levi 			/*
118*70cd20aeSGuy Levi 			 * higher bits mean the namespace and lower bits mean
119*70cd20aeSGuy Levi 			 * the type id within the namespace.
120*70cd20aeSGuy Levi 			 */
121*70cd20aeSGuy Levi 			u16				obj_type;
122*70cd20aeSGuy Levi 			u16				min_len;
123*70cd20aeSGuy Levi 			u16				max_len;
124*70cd20aeSGuy Levi 			u8				access;
125*70cd20aeSGuy Levi 		} objs_arr;
126d108dac0SJason Gunthorpe 	} u2;
127fac9658cSMatan Barak };
128f43dbebfSMatan Barak 
1295009010fSMatan Barak /*
1309ed3e5f4SJason Gunthorpe  * Information about the API is loaded into a radix tree. For IOCTL we start
1319ed3e5f4SJason Gunthorpe  * with a tuple of:
1329ed3e5f4SJason Gunthorpe  *  object_id, attr_id, method_id
1339ed3e5f4SJason Gunthorpe  *
1349ed3e5f4SJason Gunthorpe  * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
1359ed3e5f4SJason Gunthorpe  * on the current kernel support this is compressed into 16 bit key for the
1369ed3e5f4SJason Gunthorpe  * radix tree. Since this compression is entirely internal to the kernel the
1379ed3e5f4SJason Gunthorpe  * below limits can be revised if the kernel gains additional data.
1389ed3e5f4SJason Gunthorpe  *
1399ed3e5f4SJason Gunthorpe  * With 64 leafs per node this is a 3 level radix tree.
1409ed3e5f4SJason Gunthorpe  *
1419ed3e5f4SJason Gunthorpe  * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
1429ed3e5f4SJason Gunthorpe  * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
1439ed3e5f4SJason Gunthorpe  */
1449ed3e5f4SJason Gunthorpe enum uapi_radix_data {
1459ed3e5f4SJason Gunthorpe 	UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
1469ed3e5f4SJason Gunthorpe 
1479ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_BITS = 6,
1489ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
1499ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
1509ed3e5f4SJason Gunthorpe 
1519ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_BITS = 5,
1529ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
1539ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_CORE = 24,
1549ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) -
1559ed3e5f4SJason Gunthorpe 					   UVERBS_API_METHOD_KEY_NUM_CORE,
1569ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_MASK = GENMASK(
1579ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
1589ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_SHIFT),
1599ed3e5f4SJason Gunthorpe 
1609ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_BITS = 5,
1619ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_SHIFT =
1629ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
1639ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_CORE = 24,
1649ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_DRIVER =
1659ed3e5f4SJason Gunthorpe 		(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
1669ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
1679ed3e5f4SJason Gunthorpe 
1689ed3e5f4SJason Gunthorpe 	/* This id guaranteed to not exist in the radix tree */
1699ed3e5f4SJason Gunthorpe 	UVERBS_API_KEY_ERR = 0xFFFFFFFF,
1709ed3e5f4SJason Gunthorpe };
1719ed3e5f4SJason Gunthorpe 
1729ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id)
1739ed3e5f4SJason Gunthorpe {
1749ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
1759ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
1769ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
1779ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
1789ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
1799ed3e5f4SJason Gunthorpe 	} else {
1809ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
1819ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
1829ed3e5f4SJason Gunthorpe 	}
1839ed3e5f4SJason Gunthorpe 
1849ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_OBJ_KEY_SHIFT;
1859ed3e5f4SJason Gunthorpe }
1869ed3e5f4SJason Gunthorpe 
1879ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key)
1889ed3e5f4SJason Gunthorpe {
1899ed3e5f4SJason Gunthorpe 	return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
1909ed3e5f4SJason Gunthorpe }
1919ed3e5f4SJason Gunthorpe 
1929ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
1939ed3e5f4SJason Gunthorpe {
1949ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
1959ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
1969ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
1979ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
1989ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
1999ed3e5f4SJason Gunthorpe 	} else {
2009ed3e5f4SJason Gunthorpe 		id++;
2019ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
2029ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2039ed3e5f4SJason Gunthorpe 	}
2049ed3e5f4SJason Gunthorpe 
2059ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_METHOD_KEY_SHIFT;
2069ed3e5f4SJason Gunthorpe }
2079ed3e5f4SJason Gunthorpe 
2089ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
2099ed3e5f4SJason Gunthorpe {
2109ed3e5f4SJason Gunthorpe 	return attr_key &
2119ed3e5f4SJason Gunthorpe 	       (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
2129ed3e5f4SJason Gunthorpe }
2139ed3e5f4SJason Gunthorpe 
2149ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
2159ed3e5f4SJason Gunthorpe {
2169ed3e5f4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
2179ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) == 0;
2189ed3e5f4SJason Gunthorpe }
2199ed3e5f4SJason Gunthorpe 
2209ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
2219ed3e5f4SJason Gunthorpe {
2229ed3e5f4SJason Gunthorpe 	/* 0 is the method slot itself */
2239ed3e5f4SJason Gunthorpe 	return ioctl_method_key + 1;
2249ed3e5f4SJason Gunthorpe }
2259ed3e5f4SJason Gunthorpe 
2269ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id)
2279ed3e5f4SJason Gunthorpe {
2289ed3e5f4SJason Gunthorpe 	/*
2299ed3e5f4SJason Gunthorpe 	 * The attr is designed to fit in the typical single radix tree node
2309ed3e5f4SJason Gunthorpe 	 * of 64 entries. Since allmost all methods have driver attributes we
2319ed3e5f4SJason Gunthorpe 	 * organize things so that the driver and core attributes interleave to
2329ed3e5f4SJason Gunthorpe 	 * reduce the length of the attributes array in typical cases.
2339ed3e5f4SJason Gunthorpe 	 */
2349ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
2359ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
2369ed3e5f4SJason Gunthorpe 		id++;
2379ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2389ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2399ed3e5f4SJason Gunthorpe 		id = (id << 1) | 0;
2409ed3e5f4SJason Gunthorpe 	} else {
2419ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2429ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2439ed3e5f4SJason Gunthorpe 		id = (id << 1) | 1;
2449ed3e5f4SJason Gunthorpe 	}
2459ed3e5f4SJason Gunthorpe 
2469ed3e5f4SJason Gunthorpe 	return id;
2479ed3e5f4SJason Gunthorpe }
2489ed3e5f4SJason Gunthorpe 
2499ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
2509ed3e5f4SJason Gunthorpe {
2519ed3e5f4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
2529ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) != 0;
2539ed3e5f4SJason Gunthorpe }
2549ed3e5f4SJason Gunthorpe 
2559ed3e5f4SJason Gunthorpe /*
2569ed3e5f4SJason Gunthorpe  * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
2579ed3e5f4SJason Gunthorpe  * basically it undoes the reservation of 0 in the ID numbering. attr_key
2589ed3e5f4SJason Gunthorpe  * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
2599ed3e5f4SJason Gunthorpe  * uapi_key_attr().
2609ed3e5f4SJason Gunthorpe  */
2619ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
2629ed3e5f4SJason Gunthorpe {
2639ed3e5f4SJason Gunthorpe 	return attr_key - 1;
2649ed3e5f4SJason Gunthorpe }
2659ed3e5f4SJason Gunthorpe 
266*70cd20aeSGuy Levi static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
267*70cd20aeSGuy Levi {
268*70cd20aeSGuy Levi 	return attr_bkey + 1;
269*70cd20aeSGuy Levi }
270*70cd20aeSGuy Levi 
2719ed3e5f4SJason Gunthorpe /*
2725009010fSMatan Barak  * =======================================
2735009010fSMatan Barak  *	Verbs definitions
2745009010fSMatan Barak  * =======================================
2755009010fSMatan Barak  */
2765009010fSMatan Barak 
27709e3ebf8SMatan Barak struct uverbs_attr_def {
27809e3ebf8SMatan Barak 	u16                           id;
27909e3ebf8SMatan Barak 	struct uverbs_attr_spec       attr;
28009e3ebf8SMatan Barak };
28109e3ebf8SMatan Barak 
28209e3ebf8SMatan Barak struct uverbs_method_def {
28309e3ebf8SMatan Barak 	u16                                  id;
28409e3ebf8SMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
28509e3ebf8SMatan Barak 	u32				     flags;
28609e3ebf8SMatan Barak 	size_t				     num_attrs;
28709e3ebf8SMatan Barak 	const struct uverbs_attr_def * const (*attrs)[];
288e83f0ecdSJason Gunthorpe 	int (*handler)(struct ib_uverbs_file *ufile,
28909e3ebf8SMatan Barak 		       struct uverbs_attr_bundle *ctx);
29009e3ebf8SMatan Barak };
29109e3ebf8SMatan Barak 
2925009010fSMatan Barak struct uverbs_object_def {
29309e3ebf8SMatan Barak 	u16					 id;
2945009010fSMatan Barak 	const struct uverbs_obj_type	        *type_attrs;
29509e3ebf8SMatan Barak 	size_t				         num_methods;
29609e3ebf8SMatan Barak 	const struct uverbs_method_def * const (*methods)[];
29709e3ebf8SMatan Barak };
29809e3ebf8SMatan Barak 
29909e3ebf8SMatan Barak struct uverbs_object_tree_def {
30009e3ebf8SMatan Barak 	size_t					 num_objects;
30109e3ebf8SMatan Barak 	const struct uverbs_object_def * const (*objects)[];
3025009010fSMatan Barak };
3035009010fSMatan Barak 
304d108dac0SJason Gunthorpe /*
305d108dac0SJason Gunthorpe  * =======================================
306d108dac0SJason Gunthorpe  *	Attribute Specifications
307d108dac0SJason Gunthorpe  * =======================================
308d108dac0SJason Gunthorpe  */
309c66db311SMatan Barak 
310c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len)			\
311d108dac0SJason Gunthorpe 	.u.ptr.min_len = _min_len, .u.ptr.len = _len
312422e3d37SJason Gunthorpe 
313fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
314fd44e385SYishai Hadas 
315422e3d37SJason Gunthorpe /*
316422e3d37SJason Gunthorpe  * Specifies a uapi structure that cannot be extended. The user must always
317422e3d37SJason Gunthorpe  * supply the whole structure and nothing more. The structure must be declared
318422e3d37SJason Gunthorpe  * in a header under include/uapi/rdma.
319422e3d37SJason Gunthorpe  */
320422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type)					\
321422e3d37SJason Gunthorpe 	.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
322422e3d37SJason Gunthorpe /*
323422e3d37SJason Gunthorpe  * Specifies a uapi structure where the user must provide at least up to
324422e3d37SJason Gunthorpe  * member 'last'.  Anything after last and up until the end of the structure
325422e3d37SJason Gunthorpe  * can be non-zero, anything longer than the end of the structure must be
326422e3d37SJason Gunthorpe  * zero. The structure must be declared in a header under include/uapi/rdma.
327422e3d37SJason Gunthorpe  */
328422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last)                                       \
329422e3d37SJason Gunthorpe 	.zero_trailing = 1,                                                    \
330422e3d37SJason Gunthorpe 	UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)),              \
331422e3d37SJason Gunthorpe 			 sizeof(_type))
332540cd692SJason Gunthorpe /*
333540cd692SJason Gunthorpe  * Specifies at least min_len bytes must be passed in, but the amount can be
334540cd692SJason Gunthorpe  * larger, up to the protocol maximum size. No check for zeroing is done.
335540cd692SJason Gunthorpe  */
336540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
337c66db311SMatan Barak 
338d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */
33983bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
34083bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1
34183bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0
34235410306SMatan Barak 
343*70cd20aeSGuy Levi /*
344*70cd20aeSGuy Levi  * min_len must be bigger than 0 and _max_len must be smaller than 4095.  Only
345*70cd20aeSGuy Levi  * READ\WRITE accesses are supported.
346*70cd20aeSGuy Levi  */
347*70cd20aeSGuy Levi #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
348*70cd20aeSGuy Levi 			     ...)                                              \
349*70cd20aeSGuy Levi 	(&(const struct uverbs_attr_def){                                      \
350*70cd20aeSGuy Levi 		.id = (_attr_id) +                                             \
351*70cd20aeSGuy Levi 		      BUILD_BUG_ON_ZERO((_min_len) == 0 ||                     \
352*70cd20aeSGuy Levi 					(_max_len) >                           \
353*70cd20aeSGuy Levi 						PAGE_SIZE / sizeof(void *) ||  \
354*70cd20aeSGuy Levi 					(_min_len) > (_max_len) ||             \
355*70cd20aeSGuy Levi 					(_access) == UVERBS_ACCESS_NEW ||      \
356*70cd20aeSGuy Levi 					(_access) == UVERBS_ACCESS_DESTROY),   \
357*70cd20aeSGuy Levi 		.attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY,                 \
358*70cd20aeSGuy Levi 			  .u2.objs_arr.obj_type = _idr_type,                   \
359*70cd20aeSGuy Levi 			  .u2.objs_arr.access = _access,                       \
360*70cd20aeSGuy Levi 			  .u2.objs_arr.min_len = _min_len,                     \
361*70cd20aeSGuy Levi 			  .u2.objs_arr.max_len = _max_len,                     \
362*70cd20aeSGuy Levi 			  __VA_ARGS__ } })
363*70cd20aeSGuy Levi 
364d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...)                     \
3659a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
366d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
367d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_IDR,                        \
368d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _idr_type,                         \
369d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
370d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
371d108dac0SJason Gunthorpe 
372d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...)                       \
3739a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
374d108dac0SJason Gunthorpe 		.id = (_attr_id) +                                             \
375d108dac0SJason Gunthorpe 		      BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW &&      \
37635410306SMatan Barak 					(_access) != UVERBS_ACCESS_READ),      \
377d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_FD,                         \
378d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _fd_type,                          \
379d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
380d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
38135410306SMatan Barak 
382d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...)                               \
3839a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
384d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
385d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN,                     \
386d108dac0SJason Gunthorpe 			  _type,                                               \
387d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
388d108dac0SJason Gunthorpe 
389d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...)                              \
3909a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
391d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
392d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT,                    \
393d108dac0SJason Gunthorpe 			  _type,                                               \
394d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
395d108dac0SJason Gunthorpe 
396d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
397d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...)                          \
3989a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
399d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
400d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN,                    \
401d108dac0SJason Gunthorpe 			  .u2.enum_def.ids = _enum_arr,                        \
402d108dac0SJason Gunthorpe 			  .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr),       \
403d108dac0SJason Gunthorpe 			  __VA_ARGS__ },                                       \
404d108dac0SJason Gunthorpe 	})
405d108dac0SJason Gunthorpe 
4060953fffeSMark Bloch /* An input value that is a member in the enum _enum_type. */
4070953fffeSMark Bloch #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...)                        \
4080953fffeSMark Bloch 	UVERBS_ATTR_PTR_IN(                                                    \
4090953fffeSMark Bloch 		_attr_id,                                                      \
4100953fffeSMark Bloch 		UVERBS_ATTR_SIZE(                                              \
4110953fffeSMark Bloch 			sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)),  \
4120953fffeSMark Bloch 			sizeof(u64)),                                          \
4130953fffeSMark Bloch 		__VA_ARGS__)
4140953fffeSMark Bloch 
415d108dac0SJason Gunthorpe /*
416bccd0622SJason Gunthorpe  * An input value that is a bitwise combination of values of _enum_type.
417bccd0622SJason Gunthorpe  * This permits the flag value to be passed as either a u32 or u64, it must
418bccd0622SJason Gunthorpe  * be retrieved via uverbs_get_flag().
419bccd0622SJason Gunthorpe  */
420bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...)                        \
421bccd0622SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(                                                    \
422bccd0622SJason Gunthorpe 		_attr_id,                                                      \
423bccd0622SJason Gunthorpe 		UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO(              \
424bccd0622SJason Gunthorpe 						       !sizeof(_enum_type *)), \
425bccd0622SJason Gunthorpe 				 sizeof(u64)),                                 \
426bccd0622SJason Gunthorpe 		__VA_ARGS__)
427bccd0622SJason Gunthorpe 
428bccd0622SJason Gunthorpe /*
4296c61d2a5SJason Gunthorpe  * This spec is used in order to pass information to the hardware driver in a
4306c61d2a5SJason Gunthorpe  * legacy way. Every verb that could get driver specific data should get this
4316c61d2a5SJason Gunthorpe  * spec.
4326c61d2a5SJason Gunthorpe  */
4336c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW()                                                      \
4349a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN,                                 \
435540cd692SJason Gunthorpe 			   UVERBS_ATTR_MIN_SIZE(0),			       \
436540cd692SJason Gunthorpe 			   UA_OPTIONAL),				       \
4379a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT,                               \
438540cd692SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(0),			       \
439540cd692SJason Gunthorpe 			    UA_OPTIONAL)
4406c61d2a5SJason Gunthorpe 
4416c61d2a5SJason Gunthorpe /*
442d108dac0SJason Gunthorpe  * =======================================
443d108dac0SJason Gunthorpe  *	Declaration helpers
444d108dac0SJason Gunthorpe  * =======================================
445d108dac0SJason Gunthorpe  */
4466c61d2a5SJason Gunthorpe 
44709e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...)                                 \
4486c61d2a5SJason Gunthorpe 	static const struct uverbs_object_def *const _name##_ptr[] = {         \
4496c61d2a5SJason Gunthorpe 		__VA_ARGS__,                                                   \
4506c61d2a5SJason Gunthorpe 	};                                                                     \
4516c61d2a5SJason Gunthorpe 	static const struct uverbs_object_tree_def _name = {                   \
4526c61d2a5SJason Gunthorpe 		.num_objects = ARRAY_SIZE(_name##_ptr),                        \
4536c61d2a5SJason Gunthorpe 		.objects = &_name##_ptr,                                       \
4546c61d2a5SJason Gunthorpe 	}
45509e3ebf8SMatan Barak 
456fac9658cSMatan Barak /* =================================================
457fac9658cSMatan Barak  *              Parsing infrastructure
458fac9658cSMatan Barak  * =================================================
459fac9658cSMatan Barak  */
460fac9658cSMatan Barak 
4613a863577SJason Gunthorpe 
462fac9658cSMatan Barak struct uverbs_ptr_attr {
4638762d149SMatan Barak 	/*
4648762d149SMatan Barak 	 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
4658762d149SMatan Barak 	 * used.
4668762d149SMatan Barak 	 */
4678762d149SMatan Barak 	union {
4688762d149SMatan Barak 		void *ptr;
469fac9658cSMatan Barak 		u64 data;
4708762d149SMatan Barak 	};
471fac9658cSMatan Barak 	u16		len;
4726a1f444fSJason Gunthorpe 	u16		uattr_idx;
473494c5580SMatan Barak 	u8		enum_id;
474fac9658cSMatan Barak };
475fac9658cSMatan Barak 
476f43dbebfSMatan Barak struct uverbs_obj_attr {
477f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
4783a863577SJason Gunthorpe 	const struct uverbs_api_attr	*attr_elm;
479f43dbebfSMatan Barak };
480f43dbebfSMatan Barak 
481*70cd20aeSGuy Levi struct uverbs_objs_arr_attr {
482*70cd20aeSGuy Levi 	struct ib_uobject **uobjects;
483*70cd20aeSGuy Levi 	u16 len;
484*70cd20aeSGuy Levi };
485*70cd20aeSGuy Levi 
486f43dbebfSMatan Barak struct uverbs_attr {
487fac9658cSMatan Barak 	union {
488fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
489f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
490*70cd20aeSGuy Levi 		struct uverbs_objs_arr_attr objs_arr_attr;
491f43dbebfSMatan Barak 	};
492fac9658cSMatan Barak };
493f43dbebfSMatan Barak 
494f43dbebfSMatan Barak struct uverbs_attr_bundle {
4954b3dd2bbSJason Gunthorpe 	struct ib_uverbs_file *ufile;
4963a863577SJason Gunthorpe 	DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
4973a863577SJason Gunthorpe 	struct uverbs_attr attrs[];
498f43dbebfSMatan Barak };
499f43dbebfSMatan Barak 
50035410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
50135410306SMatan Barak 					unsigned int idx)
50235410306SMatan Barak {
5033a863577SJason Gunthorpe 	return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
5043a863577SJason Gunthorpe 			attrs_bundle->attr_present);
50535410306SMatan Barak }
50635410306SMatan Barak 
50741b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
50841b2a71fSMatan Barak 
509d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
510d70724f1SMatan Barak 							u16 idx)
511d70724f1SMatan Barak {
512d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
513d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
514d70724f1SMatan Barak 
5153a863577SJason Gunthorpe 	return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
516d70724f1SMatan Barak }
517d70724f1SMatan Barak 
518494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
519494c5580SMatan Barak 					  u16 idx)
520494c5580SMatan Barak {
521494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
522494c5580SMatan Barak 
523494c5580SMatan Barak 	if (IS_ERR(attr))
524494c5580SMatan Barak 		return PTR_ERR(attr);
525494c5580SMatan Barak 
526494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
527494c5580SMatan Barak }
528494c5580SMatan Barak 
529be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
530be934ccaSAriel Levkovich 					u16 idx)
531be934ccaSAriel Levkovich {
532f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
533be934ccaSAriel Levkovich 
534f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
535f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
536f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
537be934ccaSAriel Levkovich 
538f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
539be934ccaSAriel Levkovich }
540be934ccaSAriel Levkovich 
5413efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
5423efa3812SMatan Barak 							 u16 idx)
5433efa3812SMatan Barak {
5443efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5453efa3812SMatan Barak 
5463efa3812SMatan Barak 	if (IS_ERR(attr))
5473efa3812SMatan Barak 		return ERR_CAST(attr);
5483efa3812SMatan Barak 
5493efa3812SMatan Barak 	return attr->obj_attr.uobject;
5503efa3812SMatan Barak }
5513efa3812SMatan Barak 
5528762d149SMatan Barak static inline int
5538762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
5548762d149SMatan Barak {
5558762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5568762d149SMatan Barak 
5578762d149SMatan Barak 	if (IS_ERR(attr))
5588762d149SMatan Barak 		return PTR_ERR(attr);
5598762d149SMatan Barak 
5608762d149SMatan Barak 	return attr->ptr_attr.len;
5618762d149SMatan Barak }
5628762d149SMatan Barak 
563*70cd20aeSGuy Levi /**
564*70cd20aeSGuy Levi  * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
565*70cd20aeSGuy Levi  * UVERBS_ATTR_TYPE_IDRS_ARRAY.
566*70cd20aeSGuy Levi  * @arr: Returned pointer to array of pointers for uobjects or NULL if
567*70cd20aeSGuy Levi  *       the attribute isn't provided.
568*70cd20aeSGuy Levi  *
569*70cd20aeSGuy Levi  * Return: The array length or 0 if no attribute was provided.
570*70cd20aeSGuy Levi  */
571*70cd20aeSGuy Levi static inline int uverbs_attr_get_uobjs_arr(
572*70cd20aeSGuy Levi 	const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
573*70cd20aeSGuy Levi 	struct ib_uobject ***arr)
574*70cd20aeSGuy Levi {
575*70cd20aeSGuy Levi 	const struct uverbs_attr *attr =
576*70cd20aeSGuy Levi 			uverbs_attr_get(attrs_bundle, attr_idx);
577*70cd20aeSGuy Levi 
578*70cd20aeSGuy Levi 	if (IS_ERR(attr)) {
579*70cd20aeSGuy Levi 		*arr = NULL;
580*70cd20aeSGuy Levi 		return 0;
581*70cd20aeSGuy Levi 	}
582*70cd20aeSGuy Levi 
583*70cd20aeSGuy Levi 	*arr = attr->objs_arr_attr.uobjects;
584*70cd20aeSGuy Levi 
585*70cd20aeSGuy Levi 	return attr->objs_arr_attr.len;
586*70cd20aeSGuy Levi }
587*70cd20aeSGuy Levi 
58889d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
58989d9e8d3SMatan Barak {
59089d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
59189d9e8d3SMatan Barak }
59289d9e8d3SMatan Barak 
5938762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
5948762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
5958762d149SMatan Barak {
5968762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5978762d149SMatan Barak 
5988762d149SMatan Barak 	if (IS_ERR(attr))
5998762d149SMatan Barak 		return (void *)attr;
6008762d149SMatan Barak 
6018762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
6028762d149SMatan Barak 						 attr->ptr_attr.ptr;
6038762d149SMatan Barak }
6048762d149SMatan Barak 
60589d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
606d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
60789d9e8d3SMatan Barak 				    size_t idx,
60889d9e8d3SMatan Barak 				    size_t size)
609d70724f1SMatan Barak {
610d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
611d70724f1SMatan Barak 
612d70724f1SMatan Barak 	if (IS_ERR(attr))
613d70724f1SMatan Barak 		return PTR_ERR(attr);
614d70724f1SMatan Barak 
61589d9e8d3SMatan Barak 	/*
61689d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
617c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
618c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
61989d9e8d3SMatan Barak 	 */
62089d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
62189d9e8d3SMatan Barak 		return -EINVAL;
62289d9e8d3SMatan Barak 
62389d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
624d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
6252f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
6262f36028cSJason Gunthorpe 				attr->ptr_attr.len))
627d70724f1SMatan Barak 		return -EFAULT;
628d70724f1SMatan Barak 
629d70724f1SMatan Barak 	return 0;
630d70724f1SMatan Barak }
631d70724f1SMatan Barak 
632c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
633c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
634c66db311SMatan Barak 					    size_t idx,
635c66db311SMatan Barak 					    size_t size)
636c66db311SMatan Barak {
637c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
638c66db311SMatan Barak 	size_t min_size;
639c66db311SMatan Barak 
640c66db311SMatan Barak 	if (IS_ERR(attr))
641c66db311SMatan Barak 		return PTR_ERR(attr);
642c66db311SMatan Barak 
643c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
644c66db311SMatan Barak 
645c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
646c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
647c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
648c66db311SMatan Barak 				min_size))
649c66db311SMatan Barak 		return -EFAULT;
650c66db311SMatan Barak 
651c66db311SMatan Barak 	if (size > min_size)
652c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
653c66db311SMatan Barak 
654c66db311SMatan Barak 	return 0;
655c66db311SMatan Barak }
656c66db311SMatan Barak 
657d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
65889d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
659d70724f1SMatan Barak 
660c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
661c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
662c66db311SMatan Barak 
663bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
664bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
665bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
666bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
667bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
6686a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
6696a1f444fSJason Gunthorpe 		   const void *from, size_t size);
670461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
671461bb2eeSJason Gunthorpe 			     gfp_t flags);
672461bb2eeSJason Gunthorpe 
673461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
674461bb2eeSJason Gunthorpe 					  size_t size)
675461bb2eeSJason Gunthorpe {
676461bb2eeSJason Gunthorpe 	return _uverbs_alloc(bundle, size, GFP_KERNEL);
677461bb2eeSJason Gunthorpe }
678461bb2eeSJason Gunthorpe 
679461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
680461bb2eeSJason Gunthorpe 					   size_t size)
681461bb2eeSJason Gunthorpe {
682461bb2eeSJason Gunthorpe 	return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
683461bb2eeSJason Gunthorpe }
6840953fffeSMark Bloch int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
6850953fffeSMark Bloch 		      size_t idx, s64 lower_bound, u64 upper_bound,
6860953fffeSMark Bloch 		      s64 *def_val);
687bccd0622SJason Gunthorpe #else
688bccd0622SJason Gunthorpe static inline int
689bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
690bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
691bccd0622SJason Gunthorpe {
692bccd0622SJason Gunthorpe 	return -EINVAL;
693bccd0622SJason Gunthorpe }
694bccd0622SJason Gunthorpe static inline int
695bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
696bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
697bccd0622SJason Gunthorpe {
698bccd0622SJason Gunthorpe 	return -EINVAL;
699bccd0622SJason Gunthorpe }
7006a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
7016a1f444fSJason Gunthorpe 				 size_t idx, const void *from, size_t size)
7026a1f444fSJason Gunthorpe {
7036a1f444fSJason Gunthorpe 	return -EINVAL;
7046a1f444fSJason Gunthorpe }
705461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
706461bb2eeSJason Gunthorpe 					  size_t size)
707461bb2eeSJason Gunthorpe {
708461bb2eeSJason Gunthorpe 	return ERR_PTR(-EINVAL);
709461bb2eeSJason Gunthorpe }
710461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
711461bb2eeSJason Gunthorpe 					   size_t size)
712461bb2eeSJason Gunthorpe {
713461bb2eeSJason Gunthorpe 	return ERR_PTR(-EINVAL);
714461bb2eeSJason Gunthorpe }
7150953fffeSMark Bloch static inline int
7160953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
7170953fffeSMark Bloch 		  size_t idx, s64 lower_bound, u64 upper_bound,
7180953fffeSMark Bloch 		  s64 *def_val)
7190953fffeSMark Bloch {
7200953fffeSMark Bloch 	return -EINVAL;
7210953fffeSMark Bloch }
722bccd0622SJason Gunthorpe #endif
723bccd0622SJason Gunthorpe 
7240953fffeSMark Bloch #define uverbs_get_const(_to, _attrs_bundle, _idx)                             \
7250953fffeSMark Bloch 	({                                                                     \
7260953fffeSMark Bloch 		s64 _val;                                                      \
7270953fffeSMark Bloch 		int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx,       \
7280953fffeSMark Bloch 					     type_min(typeof(*_to)),           \
7290953fffeSMark Bloch 					     type_max(typeof(*_to)), NULL);    \
7300953fffeSMark Bloch 		(*_to) = _val;                                                 \
7310953fffeSMark Bloch 		_ret;                                                          \
7320953fffeSMark Bloch 	})
7330953fffeSMark Bloch 
7340953fffeSMark Bloch #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default)           \
7350953fffeSMark Bloch 	({                                                                     \
7360953fffeSMark Bloch 		s64 _val;                                                      \
7370953fffeSMark Bloch 		s64 _def_val = _default;                                       \
7380953fffeSMark Bloch 		int _ret =                                                     \
7390953fffeSMark Bloch 			_uverbs_get_const(&_val, _attrs_bundle, _idx,          \
7400953fffeSMark Bloch 					  type_min(typeof(*_to)),              \
7410953fffeSMark Bloch 					  type_max(typeof(*_to)), &_def_val);  \
7420953fffeSMark Bloch 		(*_to) = _val;                                                 \
7430953fffeSMark Bloch 		_ret;                                                          \
7440953fffeSMark Bloch 	})
745118620d3SMatan Barak #endif
746