xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 342ee59de98a2ecdf15a46849a2534e7c808eb1f)
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,
5570cd20aeSGuy 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;
8207f05f40SJason Gunthorpe 	/* True if this is from UVERBS_ATTR_UHW */
8307f05f40SJason Gunthorpe 	u8 is_udata:1;
84d108dac0SJason Gunthorpe 
85fac9658cSMatan Barak 	union {
86f43dbebfSMatan Barak 		struct {
87c66db311SMatan Barak 			/* Current known size to kernel */
881f07e08fSMatan Barak 			u16 len;
89c66db311SMatan Barak 			/* User isn't allowed to provide something < min_len */
90c66db311SMatan Barak 			u16 min_len;
911f07e08fSMatan Barak 		} ptr;
92d108dac0SJason Gunthorpe 
931f07e08fSMatan Barak 		struct {
94f43dbebfSMatan Barak 			/*
95f43dbebfSMatan Barak 			 * higher bits mean the namespace and lower bits mean
96f43dbebfSMatan Barak 			 * the type id within the namespace.
97f43dbebfSMatan Barak 			 */
98f43dbebfSMatan Barak 			u16 obj_type;
99f43dbebfSMatan Barak 			u8 access;
100f43dbebfSMatan Barak 		} obj;
101d108dac0SJason Gunthorpe 
102494c5580SMatan Barak 		struct {
103494c5580SMatan Barak 			u8 num_elems;
104d108dac0SJason Gunthorpe 		} enum_def;
105d108dac0SJason Gunthorpe 	} u;
106d108dac0SJason Gunthorpe 
10770cd20aeSGuy Levi 	/* This weird split lets us remove some padding */
108d108dac0SJason Gunthorpe 	union {
109d108dac0SJason Gunthorpe 		struct {
110494c5580SMatan Barak 			/*
111494c5580SMatan Barak 			 * The enum attribute can select one of the attributes
112494c5580SMatan Barak 			 * contained in the ids array. Currently only PTR_IN
113494c5580SMatan Barak 			 * attributes are supported in the ids array.
114494c5580SMatan Barak 			 */
115494c5580SMatan Barak 			const struct uverbs_attr_spec *ids;
116494c5580SMatan Barak 		} enum_def;
11770cd20aeSGuy Levi 
11870cd20aeSGuy Levi 		struct {
11970cd20aeSGuy Levi 			/*
12070cd20aeSGuy Levi 			 * higher bits mean the namespace and lower bits mean
12170cd20aeSGuy Levi 			 * the type id within the namespace.
12270cd20aeSGuy Levi 			 */
12370cd20aeSGuy Levi 			u16				obj_type;
12470cd20aeSGuy Levi 			u16				min_len;
12570cd20aeSGuy Levi 			u16				max_len;
12670cd20aeSGuy Levi 			u8				access;
12770cd20aeSGuy Levi 		} objs_arr;
128d108dac0SJason Gunthorpe 	} u2;
129fac9658cSMatan Barak };
130f43dbebfSMatan Barak 
1315009010fSMatan Barak /*
1329ed3e5f4SJason Gunthorpe  * Information about the API is loaded into a radix tree. For IOCTL we start
1339ed3e5f4SJason Gunthorpe  * with a tuple of:
1349ed3e5f4SJason Gunthorpe  *  object_id, attr_id, method_id
1359ed3e5f4SJason Gunthorpe  *
1369ed3e5f4SJason Gunthorpe  * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
1379ed3e5f4SJason Gunthorpe  * on the current kernel support this is compressed into 16 bit key for the
1389ed3e5f4SJason Gunthorpe  * radix tree. Since this compression is entirely internal to the kernel the
1399ed3e5f4SJason Gunthorpe  * below limits can be revised if the kernel gains additional data.
1409ed3e5f4SJason Gunthorpe  *
1419ed3e5f4SJason Gunthorpe  * With 64 leafs per node this is a 3 level radix tree.
1429ed3e5f4SJason Gunthorpe  *
1439ed3e5f4SJason Gunthorpe  * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
1449ed3e5f4SJason Gunthorpe  * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
1456884c6c4SJason Gunthorpe  *
1466884c6c4SJason Gunthorpe  * This also encodes the tables for the write() and write() extended commands
1476884c6c4SJason Gunthorpe  * using the coding
1486884c6c4SJason Gunthorpe  *   OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
1496884c6c4SJason Gunthorpe  *   OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
1506884c6c4SJason Gunthorpe  * ie the WRITE path is treated as a special method type in the ioctl
1516884c6c4SJason Gunthorpe  * framework.
1529ed3e5f4SJason Gunthorpe  */
1539ed3e5f4SJason Gunthorpe enum uapi_radix_data {
1549ed3e5f4SJason Gunthorpe 	UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
1559ed3e5f4SJason Gunthorpe 
1569ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_BITS = 6,
1579ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
1589ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
1596884c6c4SJason Gunthorpe 	UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
1609ed3e5f4SJason Gunthorpe 
1619ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_BITS = 5,
1629ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
1636884c6c4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_CORE = 22,
1646884c6c4SJason Gunthorpe 	UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
1656884c6c4SJason Gunthorpe 	UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
1666884c6c4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_DRIVER =
1676884c6c4SJason Gunthorpe 		(UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
1689ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_NUM_CORE,
1699ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_MASK = GENMASK(
1709ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
1719ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_SHIFT),
1729ed3e5f4SJason Gunthorpe 
1739ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_BITS = 5,
1749ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_SHIFT =
1759ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
176*342ee59dSYishai Hadas 	UVERBS_API_OBJ_KEY_NUM_CORE = 20,
1779ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_DRIVER =
1789ed3e5f4SJason Gunthorpe 		(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
1799ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
1809ed3e5f4SJason Gunthorpe 
1819ed3e5f4SJason Gunthorpe 	/* This id guaranteed to not exist in the radix tree */
1829ed3e5f4SJason Gunthorpe 	UVERBS_API_KEY_ERR = 0xFFFFFFFF,
1839ed3e5f4SJason Gunthorpe };
1849ed3e5f4SJason Gunthorpe 
1859ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id)
1869ed3e5f4SJason Gunthorpe {
1879ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
1889ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
1899ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
1909ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
1919ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
1929ed3e5f4SJason Gunthorpe 	} else {
1939ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
1949ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
1959ed3e5f4SJason Gunthorpe 	}
1969ed3e5f4SJason Gunthorpe 
1979ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_OBJ_KEY_SHIFT;
1989ed3e5f4SJason Gunthorpe }
1999ed3e5f4SJason Gunthorpe 
2009ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key)
2019ed3e5f4SJason Gunthorpe {
2029ed3e5f4SJason Gunthorpe 	return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
2039ed3e5f4SJason Gunthorpe }
2049ed3e5f4SJason Gunthorpe 
2059ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
2069ed3e5f4SJason Gunthorpe {
2079ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
2089ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
2099ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
2109ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2119ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
2129ed3e5f4SJason Gunthorpe 	} else {
2139ed3e5f4SJason Gunthorpe 		id++;
2149ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
2159ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2169ed3e5f4SJason Gunthorpe 	}
2179ed3e5f4SJason Gunthorpe 
2189ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_METHOD_KEY_SHIFT;
2199ed3e5f4SJason Gunthorpe }
2209ed3e5f4SJason Gunthorpe 
2216884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
2226884c6c4SJason Gunthorpe {
2236884c6c4SJason Gunthorpe 	if (id >= UVERBS_API_WRITE_KEY_NUM)
2246884c6c4SJason Gunthorpe 		return UVERBS_API_KEY_ERR;
2256884c6c4SJason Gunthorpe 	return UVERBS_API_METHOD_IS_WRITE | id;
2266884c6c4SJason Gunthorpe }
2276884c6c4SJason Gunthorpe 
2286884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
2296884c6c4SJason Gunthorpe {
2306884c6c4SJason Gunthorpe 	if (id >= UVERBS_API_WRITE_KEY_NUM)
2316884c6c4SJason Gunthorpe 		return UVERBS_API_KEY_ERR;
2326884c6c4SJason Gunthorpe 	return UVERBS_API_METHOD_IS_WRITE_EX | id;
2336884c6c4SJason Gunthorpe }
2346884c6c4SJason Gunthorpe 
2356884c6c4SJason Gunthorpe static inline __attribute_const__ u32
2366884c6c4SJason Gunthorpe uapi_key_attr_to_ioctl_method(u32 attr_key)
2379ed3e5f4SJason Gunthorpe {
2389ed3e5f4SJason Gunthorpe 	return attr_key &
2399ed3e5f4SJason Gunthorpe 	       (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
2409ed3e5f4SJason Gunthorpe }
2419ed3e5f4SJason Gunthorpe 
2429ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
2439ed3e5f4SJason Gunthorpe {
2446884c6c4SJason Gunthorpe 	unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
2456884c6c4SJason Gunthorpe 
2466884c6c4SJason Gunthorpe 	return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
2479ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) == 0;
2489ed3e5f4SJason Gunthorpe }
2499ed3e5f4SJason Gunthorpe 
2506884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
2516884c6c4SJason Gunthorpe {
2526884c6c4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
2536884c6c4SJason Gunthorpe }
2546884c6c4SJason Gunthorpe 
2556884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
2566884c6c4SJason Gunthorpe {
2576884c6c4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) ==
2586884c6c4SJason Gunthorpe 	       UVERBS_API_METHOD_IS_WRITE_EX;
2596884c6c4SJason Gunthorpe }
2606884c6c4SJason Gunthorpe 
2619ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
2629ed3e5f4SJason Gunthorpe {
2639ed3e5f4SJason Gunthorpe 	/* 0 is the method slot itself */
2649ed3e5f4SJason Gunthorpe 	return ioctl_method_key + 1;
2659ed3e5f4SJason Gunthorpe }
2669ed3e5f4SJason Gunthorpe 
2679ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id)
2689ed3e5f4SJason Gunthorpe {
2699ed3e5f4SJason Gunthorpe 	/*
2709ed3e5f4SJason Gunthorpe 	 * The attr is designed to fit in the typical single radix tree node
2719ed3e5f4SJason Gunthorpe 	 * of 64 entries. Since allmost all methods have driver attributes we
2729ed3e5f4SJason Gunthorpe 	 * organize things so that the driver and core attributes interleave to
2739ed3e5f4SJason Gunthorpe 	 * reduce the length of the attributes array in typical cases.
2749ed3e5f4SJason Gunthorpe 	 */
2759ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
2769ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
2779ed3e5f4SJason Gunthorpe 		id++;
2789ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2799ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2809ed3e5f4SJason Gunthorpe 		id = (id << 1) | 0;
2819ed3e5f4SJason Gunthorpe 	} else {
2829ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2839ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2849ed3e5f4SJason Gunthorpe 		id = (id << 1) | 1;
2859ed3e5f4SJason Gunthorpe 	}
2869ed3e5f4SJason Gunthorpe 
2879ed3e5f4SJason Gunthorpe 	return id;
2889ed3e5f4SJason Gunthorpe }
2899ed3e5f4SJason Gunthorpe 
2906884c6c4SJason Gunthorpe /* Only true for ioctl methods */
2919ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
2929ed3e5f4SJason Gunthorpe {
2936884c6c4SJason Gunthorpe 	unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
2946884c6c4SJason Gunthorpe 
2956884c6c4SJason Gunthorpe 	return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
2969ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) != 0;
2979ed3e5f4SJason Gunthorpe }
2989ed3e5f4SJason Gunthorpe 
2999ed3e5f4SJason Gunthorpe /*
3009ed3e5f4SJason Gunthorpe  * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
3019ed3e5f4SJason Gunthorpe  * basically it undoes the reservation of 0 in the ID numbering. attr_key
3029ed3e5f4SJason Gunthorpe  * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
3039ed3e5f4SJason Gunthorpe  * uapi_key_attr().
3049ed3e5f4SJason Gunthorpe  */
3059ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
3069ed3e5f4SJason Gunthorpe {
3079ed3e5f4SJason Gunthorpe 	return attr_key - 1;
3089ed3e5f4SJason Gunthorpe }
3099ed3e5f4SJason Gunthorpe 
31070cd20aeSGuy Levi static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
31170cd20aeSGuy Levi {
31270cd20aeSGuy Levi 	return attr_bkey + 1;
31370cd20aeSGuy Levi }
31470cd20aeSGuy Levi 
3159ed3e5f4SJason Gunthorpe /*
3165009010fSMatan Barak  * =======================================
3175009010fSMatan Barak  *	Verbs definitions
3185009010fSMatan Barak  * =======================================
3195009010fSMatan Barak  */
3205009010fSMatan Barak 
32109e3ebf8SMatan Barak struct uverbs_attr_def {
32209e3ebf8SMatan Barak 	u16                           id;
32309e3ebf8SMatan Barak 	struct uverbs_attr_spec       attr;
32409e3ebf8SMatan Barak };
32509e3ebf8SMatan Barak 
32609e3ebf8SMatan Barak struct uverbs_method_def {
32709e3ebf8SMatan Barak 	u16                                  id;
32809e3ebf8SMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
32909e3ebf8SMatan Barak 	u32				     flags;
33009e3ebf8SMatan Barak 	size_t				     num_attrs;
33109e3ebf8SMatan Barak 	const struct uverbs_attr_def * const (*attrs)[];
33215a1b4beSJason Gunthorpe 	int (*handler)(struct uverbs_attr_bundle *attrs);
33309e3ebf8SMatan Barak };
33409e3ebf8SMatan Barak 
3355009010fSMatan Barak struct uverbs_object_def {
33609e3ebf8SMatan Barak 	u16					 id;
3375009010fSMatan Barak 	const struct uverbs_obj_type	        *type_attrs;
33809e3ebf8SMatan Barak 	size_t				         num_methods;
33909e3ebf8SMatan Barak 	const struct uverbs_method_def * const (*methods)[];
34009e3ebf8SMatan Barak };
34109e3ebf8SMatan Barak 
3420cbf432dSJason Gunthorpe enum uapi_definition_kind {
3430cbf432dSJason Gunthorpe 	UAPI_DEF_END = 0,
3446884c6c4SJason Gunthorpe 	UAPI_DEF_OBJECT_START,
3456884c6c4SJason Gunthorpe 	UAPI_DEF_WRITE,
3460cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE,
3470cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN,
3486829c1c2SJason Gunthorpe 	UAPI_DEF_IS_SUPPORTED_FUNC,
3496829c1c2SJason Gunthorpe 	UAPI_DEF_IS_SUPPORTED_DEV_FN,
3506829c1c2SJason Gunthorpe };
3516829c1c2SJason Gunthorpe 
3526829c1c2SJason Gunthorpe enum uapi_definition_scope {
3536829c1c2SJason Gunthorpe 	UAPI_SCOPE_OBJECT = 1,
354a140692aSJason Gunthorpe 	UAPI_SCOPE_METHOD = 2,
3555009010fSMatan Barak };
3565009010fSMatan Barak 
3570cbf432dSJason Gunthorpe struct uapi_definition {
3580cbf432dSJason Gunthorpe 	u8 kind;
3596829c1c2SJason Gunthorpe 	u8 scope;
3600cbf432dSJason Gunthorpe 	union {
3610cbf432dSJason Gunthorpe 		struct {
3620cbf432dSJason Gunthorpe 			u16 object_id;
3630cbf432dSJason Gunthorpe 		} object_start;
3646884c6c4SJason Gunthorpe 		struct {
3656884c6c4SJason Gunthorpe 			u16 command_num;
366669dac1eSJason Gunthorpe 			u8 is_ex:1;
367669dac1eSJason Gunthorpe 			u8 has_udata:1;
368669dac1eSJason Gunthorpe 			u8 has_resp:1;
369669dac1eSJason Gunthorpe 			u8 req_size;
370669dac1eSJason Gunthorpe 			u8 resp_size;
3716884c6c4SJason Gunthorpe 		} write;
3720cbf432dSJason Gunthorpe 	};
3730cbf432dSJason Gunthorpe 
3740cbf432dSJason Gunthorpe 	union {
3756829c1c2SJason Gunthorpe 		bool (*func_is_supported)(struct ib_device *device);
376974d6b4bSJason Gunthorpe 		int (*func_write)(struct uverbs_attr_bundle *attrs);
3770cbf432dSJason Gunthorpe 		const struct uapi_definition *chain;
3780cbf432dSJason Gunthorpe 		const struct uverbs_object_def *chain_obj_tree;
3796829c1c2SJason Gunthorpe 		size_t needs_fn_offset;
3800cbf432dSJason Gunthorpe 	};
3810cbf432dSJason Gunthorpe };
3820cbf432dSJason Gunthorpe 
3836884c6c4SJason Gunthorpe /* Define things connected to object_id */
3846884c6c4SJason Gunthorpe #define DECLARE_UVERBS_OBJECT(_object_id, ...)                                 \
3856884c6c4SJason Gunthorpe 	{                                                                      \
3866884c6c4SJason Gunthorpe 		.kind = UAPI_DEF_OBJECT_START,                                 \
3876884c6c4SJason Gunthorpe 		.object_start = { .object_id = _object_id },                   \
3886884c6c4SJason Gunthorpe 	},                                                                     \
3896884c6c4SJason Gunthorpe 		##__VA_ARGS__
3906884c6c4SJason Gunthorpe 
3916884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */
392669dac1eSJason Gunthorpe #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...)              \
3936884c6c4SJason Gunthorpe 	{                                                                      \
3946884c6c4SJason Gunthorpe 		.kind = UAPI_DEF_WRITE,                                        \
3956884c6c4SJason Gunthorpe 		.scope = UAPI_SCOPE_OBJECT,                                    \
3966884c6c4SJason Gunthorpe 		.write = { .is_ex = 0, .command_num = _command_num },          \
3976884c6c4SJason Gunthorpe 		.func_write = _func,                                           \
398669dac1eSJason Gunthorpe 		_cmd_desc,                                                     \
3996884c6c4SJason Gunthorpe 	},                                                                     \
4006884c6c4SJason Gunthorpe 		##__VA_ARGS__
4016884c6c4SJason Gunthorpe 
4026884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */
403669dac1eSJason Gunthorpe #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...)           \
4046884c6c4SJason Gunthorpe 	{                                                                      \
4056884c6c4SJason Gunthorpe 		.kind = UAPI_DEF_WRITE,                                        \
4066884c6c4SJason Gunthorpe 		.scope = UAPI_SCOPE_OBJECT,                                    \
4076884c6c4SJason Gunthorpe 		.write = { .is_ex = 1, .command_num = _command_num },          \
408974d6b4bSJason Gunthorpe 		.func_write = _func,                                           \
409669dac1eSJason Gunthorpe 		_cmd_desc,                                                     \
4106884c6c4SJason Gunthorpe 	},                                                                     \
4116884c6c4SJason Gunthorpe 		##__VA_ARGS__
4126884c6c4SJason Gunthorpe 
4136829c1c2SJason Gunthorpe /*
4146829c1c2SJason Gunthorpe  * Object is only supported if the function pointer named ibdev_fn in struct
4156829c1c2SJason Gunthorpe  * ib_device is not NULL.
4166829c1c2SJason Gunthorpe  */
4176829c1c2SJason Gunthorpe #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn)                                        \
4186829c1c2SJason Gunthorpe 	{                                                                      \
4196829c1c2SJason Gunthorpe 		.kind = UAPI_DEF_IS_SUPPORTED_DEV_FN,                          \
4206829c1c2SJason Gunthorpe 		.scope = UAPI_SCOPE_OBJECT,                                    \
4216829c1c2SJason Gunthorpe 		.needs_fn_offset =                                             \
4223023a1e9SKamal Heib 			offsetof(struct ib_device_ops, ibdev_fn) +             \
4236829c1c2SJason Gunthorpe 			BUILD_BUG_ON_ZERO(                                     \
4243023a1e9SKamal Heib 			    sizeof(((struct ib_device_ops *)0)->ibdev_fn) !=   \
4256829c1c2SJason Gunthorpe 			    sizeof(void *)),				       \
4266829c1c2SJason Gunthorpe 	}
4276829c1c2SJason Gunthorpe 
428a140692aSJason Gunthorpe /*
429a140692aSJason Gunthorpe  * Method is only supported if the function pointer named ibdev_fn in struct
430a140692aSJason Gunthorpe  * ib_device is not NULL.
431a140692aSJason Gunthorpe  */
432a140692aSJason Gunthorpe #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn)                                     \
433a140692aSJason Gunthorpe 	{                                                                      \
434a140692aSJason Gunthorpe 		.kind = UAPI_DEF_IS_SUPPORTED_DEV_FN,                          \
435a140692aSJason Gunthorpe 		.scope = UAPI_SCOPE_METHOD,                                    \
436a140692aSJason Gunthorpe 		.needs_fn_offset =                                             \
4373023a1e9SKamal Heib 			offsetof(struct ib_device_ops, ibdev_fn) +             \
438a140692aSJason Gunthorpe 			BUILD_BUG_ON_ZERO(                                     \
4393023a1e9SKamal Heib 			    sizeof(((struct ib_device_ops *)0)->ibdev_fn) !=   \
440a140692aSJason Gunthorpe 			    sizeof(void *)),                                   \
441a140692aSJason Gunthorpe 	}
442a140692aSJason Gunthorpe 
4436829c1c2SJason Gunthorpe /* Call a function to determine if the entire object is supported or not */
4446829c1c2SJason Gunthorpe #define UAPI_DEF_IS_OBJ_SUPPORTED(_func)                                       \
4456829c1c2SJason Gunthorpe 	{                                                                      \
4466829c1c2SJason Gunthorpe 		.kind = UAPI_DEF_IS_SUPPORTED_FUNC,                            \
4476829c1c2SJason Gunthorpe 		.scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func,        \
4486829c1c2SJason Gunthorpe 	}
4496829c1c2SJason Gunthorpe 
4500cbf432dSJason Gunthorpe /* Include another struct uapi_definition in this one */
4510cbf432dSJason Gunthorpe #define UAPI_DEF_CHAIN(_def_var)                                               \
4520cbf432dSJason Gunthorpe 	{                                                                      \
4530cbf432dSJason Gunthorpe 		.kind = UAPI_DEF_CHAIN, .chain = _def_var,                     \
4540cbf432dSJason Gunthorpe 	}
4550cbf432dSJason Gunthorpe 
4560cbf432dSJason Gunthorpe /* Temporary until the tree base description is replaced */
457a1462351SLeon Romanovsky #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...)                \
4580cbf432dSJason Gunthorpe 	{                                                                      \
4590cbf432dSJason Gunthorpe 		.kind = UAPI_DEF_CHAIN_OBJ_TREE,                               \
4600cbf432dSJason Gunthorpe 		.object_start = { .object_id = _object_enum },                 \
4610cbf432dSJason Gunthorpe 		.chain_obj_tree = _object_ptr,                                 \
462a1462351SLeon Romanovsky 	},								       \
4630cbf432dSJason Gunthorpe 		##__VA_ARGS__
464a1462351SLeon Romanovsky #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...)                       \
465a1462351SLeon Romanovsky 	UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum),    \
466a1462351SLeon Romanovsky 				##__VA_ARGS__)
4670cbf432dSJason Gunthorpe 
468d108dac0SJason Gunthorpe /*
469d108dac0SJason Gunthorpe  * =======================================
470d108dac0SJason Gunthorpe  *	Attribute Specifications
471d108dac0SJason Gunthorpe  * =======================================
472d108dac0SJason Gunthorpe  */
473c66db311SMatan Barak 
474c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len)			\
475d108dac0SJason Gunthorpe 	.u.ptr.min_len = _min_len, .u.ptr.len = _len
476422e3d37SJason Gunthorpe 
477fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
478fd44e385SYishai Hadas 
479422e3d37SJason Gunthorpe /*
480422e3d37SJason Gunthorpe  * Specifies a uapi structure that cannot be extended. The user must always
481422e3d37SJason Gunthorpe  * supply the whole structure and nothing more. The structure must be declared
482422e3d37SJason Gunthorpe  * in a header under include/uapi/rdma.
483422e3d37SJason Gunthorpe  */
484422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type)					\
485422e3d37SJason Gunthorpe 	.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
486422e3d37SJason Gunthorpe /*
487422e3d37SJason Gunthorpe  * Specifies a uapi structure where the user must provide at least up to
488422e3d37SJason Gunthorpe  * member 'last'.  Anything after last and up until the end of the structure
489422e3d37SJason Gunthorpe  * can be non-zero, anything longer than the end of the structure must be
490422e3d37SJason Gunthorpe  * zero. The structure must be declared in a header under include/uapi/rdma.
491422e3d37SJason Gunthorpe  */
492422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last)                                       \
493422e3d37SJason Gunthorpe 	.zero_trailing = 1,                                                    \
494422e3d37SJason Gunthorpe 	UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)),              \
495422e3d37SJason Gunthorpe 			 sizeof(_type))
496540cd692SJason Gunthorpe /*
497540cd692SJason Gunthorpe  * Specifies at least min_len bytes must be passed in, but the amount can be
498540cd692SJason Gunthorpe  * larger, up to the protocol maximum size. No check for zeroing is done.
499540cd692SJason Gunthorpe  */
500540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
501c66db311SMatan Barak 
502d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */
50383bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
50483bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1
50583bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0
50635410306SMatan Barak 
50770cd20aeSGuy Levi /*
50870cd20aeSGuy Levi  * min_len must be bigger than 0 and _max_len must be smaller than 4095.  Only
50970cd20aeSGuy Levi  * READ\WRITE accesses are supported.
51070cd20aeSGuy Levi  */
51170cd20aeSGuy Levi #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
51270cd20aeSGuy Levi 			     ...)                                              \
51370cd20aeSGuy Levi 	(&(const struct uverbs_attr_def){                                      \
51470cd20aeSGuy Levi 		.id = (_attr_id) +                                             \
51570cd20aeSGuy Levi 		      BUILD_BUG_ON_ZERO((_min_len) == 0 ||                     \
51670cd20aeSGuy Levi 					(_max_len) >                           \
51770cd20aeSGuy Levi 						PAGE_SIZE / sizeof(void *) ||  \
51870cd20aeSGuy Levi 					(_min_len) > (_max_len) ||             \
51970cd20aeSGuy Levi 					(_access) == UVERBS_ACCESS_NEW ||      \
52070cd20aeSGuy Levi 					(_access) == UVERBS_ACCESS_DESTROY),   \
52170cd20aeSGuy Levi 		.attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY,                 \
52270cd20aeSGuy Levi 			  .u2.objs_arr.obj_type = _idr_type,                   \
52370cd20aeSGuy Levi 			  .u2.objs_arr.access = _access,                       \
52470cd20aeSGuy Levi 			  .u2.objs_arr.min_len = _min_len,                     \
52570cd20aeSGuy Levi 			  .u2.objs_arr.max_len = _max_len,                     \
52670cd20aeSGuy Levi 			  __VA_ARGS__ } })
52770cd20aeSGuy Levi 
5284d7e8cc5SYishai Hadas /*
5294d7e8cc5SYishai Hadas  * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
5304d7e8cc5SYishai Hadas  * the user must validate the type of the uobject instead.
5314d7e8cc5SYishai Hadas  */
5324d7e8cc5SYishai Hadas #define UVERBS_IDR_ANY_OBJECT 0xFFFF
5334d7e8cc5SYishai Hadas 
534d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...)                     \
5359a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
536d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
537d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_IDR,                        \
538d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _idr_type,                         \
539d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
540d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
541d108dac0SJason Gunthorpe 
542d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...)                       \
5439a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
544d108dac0SJason Gunthorpe 		.id = (_attr_id) +                                             \
545d108dac0SJason Gunthorpe 		      BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW &&      \
54635410306SMatan Barak 					(_access) != UVERBS_ACCESS_READ),      \
547d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_FD,                         \
548d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _fd_type,                          \
549d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
550d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
55135410306SMatan Barak 
552d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...)                               \
5539a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
554d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
555d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN,                     \
556d108dac0SJason Gunthorpe 			  _type,                                               \
557d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
558d108dac0SJason Gunthorpe 
559d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...)                              \
5609a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
561d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
562d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT,                    \
563d108dac0SJason Gunthorpe 			  _type,                                               \
564d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
565d108dac0SJason Gunthorpe 
566d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
567d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...)                          \
5689a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
569d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
570d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN,                    \
571d108dac0SJason Gunthorpe 			  .u2.enum_def.ids = _enum_arr,                        \
572d108dac0SJason Gunthorpe 			  .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr),       \
573d108dac0SJason Gunthorpe 			  __VA_ARGS__ },                                       \
574d108dac0SJason Gunthorpe 	})
575d108dac0SJason Gunthorpe 
5760953fffeSMark Bloch /* An input value that is a member in the enum _enum_type. */
5770953fffeSMark Bloch #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...)                        \
5780953fffeSMark Bloch 	UVERBS_ATTR_PTR_IN(                                                    \
5790953fffeSMark Bloch 		_attr_id,                                                      \
5800953fffeSMark Bloch 		UVERBS_ATTR_SIZE(                                              \
5810953fffeSMark Bloch 			sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)),  \
5820953fffeSMark Bloch 			sizeof(u64)),                                          \
5830953fffeSMark Bloch 		__VA_ARGS__)
5840953fffeSMark Bloch 
585d108dac0SJason Gunthorpe /*
586bccd0622SJason Gunthorpe  * An input value that is a bitwise combination of values of _enum_type.
587bccd0622SJason Gunthorpe  * This permits the flag value to be passed as either a u32 or u64, it must
588bccd0622SJason Gunthorpe  * be retrieved via uverbs_get_flag().
589bccd0622SJason Gunthorpe  */
590bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...)                        \
591bccd0622SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(                                                    \
592bccd0622SJason Gunthorpe 		_attr_id,                                                      \
593bccd0622SJason Gunthorpe 		UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO(              \
594bccd0622SJason Gunthorpe 						       !sizeof(_enum_type *)), \
595bccd0622SJason Gunthorpe 				 sizeof(u64)),                                 \
596bccd0622SJason Gunthorpe 		__VA_ARGS__)
597bccd0622SJason Gunthorpe 
598bccd0622SJason Gunthorpe /*
5996c61d2a5SJason Gunthorpe  * This spec is used in order to pass information to the hardware driver in a
6006c61d2a5SJason Gunthorpe  * legacy way. Every verb that could get driver specific data should get this
6016c61d2a5SJason Gunthorpe  * spec.
6026c61d2a5SJason Gunthorpe  */
6036c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW()                                                      \
6049a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN,                                 \
605540cd692SJason Gunthorpe 			   UVERBS_ATTR_MIN_SIZE(0),			       \
60607f05f40SJason Gunthorpe 			   UA_OPTIONAL,                                        \
60707f05f40SJason Gunthorpe 			   .is_udata = 1),				       \
6089a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT,                               \
609540cd692SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(0),			       \
61007f05f40SJason Gunthorpe 			    UA_OPTIONAL,                                       \
61107f05f40SJason Gunthorpe 			    .is_udata = 1)
6126c61d2a5SJason Gunthorpe 
613fac9658cSMatan Barak /* =================================================
614fac9658cSMatan Barak  *              Parsing infrastructure
615fac9658cSMatan Barak  * =================================================
616fac9658cSMatan Barak  */
617fac9658cSMatan Barak 
6183a863577SJason Gunthorpe 
619fac9658cSMatan Barak struct uverbs_ptr_attr {
6208762d149SMatan Barak 	/*
6218762d149SMatan Barak 	 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
6228762d149SMatan Barak 	 * used.
6238762d149SMatan Barak 	 */
6248762d149SMatan Barak 	union {
6258762d149SMatan Barak 		void *ptr;
626fac9658cSMatan Barak 		u64 data;
6278762d149SMatan Barak 	};
628fac9658cSMatan Barak 	u16		len;
6296a1f444fSJason Gunthorpe 	u16		uattr_idx;
630494c5580SMatan Barak 	u8		enum_id;
631fac9658cSMatan Barak };
632fac9658cSMatan Barak 
633f43dbebfSMatan Barak struct uverbs_obj_attr {
634f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
6353a863577SJason Gunthorpe 	const struct uverbs_api_attr	*attr_elm;
636f43dbebfSMatan Barak };
637f43dbebfSMatan Barak 
63870cd20aeSGuy Levi struct uverbs_objs_arr_attr {
63970cd20aeSGuy Levi 	struct ib_uobject **uobjects;
64070cd20aeSGuy Levi 	u16 len;
64170cd20aeSGuy Levi };
64270cd20aeSGuy Levi 
643f43dbebfSMatan Barak struct uverbs_attr {
644fac9658cSMatan Barak 	union {
645fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
646f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
64770cd20aeSGuy Levi 		struct uverbs_objs_arr_attr objs_arr_attr;
648f43dbebfSMatan Barak 	};
649fac9658cSMatan Barak };
650f43dbebfSMatan Barak 
651f43dbebfSMatan Barak struct uverbs_attr_bundle {
652ef87df2cSJason Gunthorpe 	struct ib_udata driver_udata;
653c2a939fdSJason Gunthorpe 	struct ib_udata ucore;
6544b3dd2bbSJason Gunthorpe 	struct ib_uverbs_file *ufile;
6553d9dfd06SShamir Rabinovitch 	struct ib_ucontext *context;
6563a863577SJason Gunthorpe 	DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
6573a863577SJason Gunthorpe 	struct uverbs_attr attrs[];
658f43dbebfSMatan Barak };
659f43dbebfSMatan Barak 
66035410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
66135410306SMatan Barak 					unsigned int idx)
66235410306SMatan Barak {
6633a863577SJason Gunthorpe 	return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
6643a863577SJason Gunthorpe 			attrs_bundle->attr_present);
66535410306SMatan Barak }
66635410306SMatan Barak 
667730623f4SShamir Rabinovitch /**
668730623f4SShamir Rabinovitch  * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
669730623f4SShamir Rabinovitch  *                             ib_udata which is embedded in uverbs_attr_bundle.
670730623f4SShamir Rabinovitch  *
671730623f4SShamir Rabinovitch  * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
672730623f4SShamir Rabinovitch  * in a NULL ucontext pointer, as a safety precaution. Callers should be using
673730623f4SShamir Rabinovitch  * 'udata' to determine if the driver call is in user or kernel mode, not
674730623f4SShamir Rabinovitch  * 'ucontext'.
675730623f4SShamir Rabinovitch  *
676730623f4SShamir Rabinovitch  */
677730623f4SShamir Rabinovitch #define rdma_udata_to_drv_context(udata, drv_dev_struct, member)               \
678730623f4SShamir Rabinovitch 	(udata ? container_of(container_of(udata, struct uverbs_attr_bundle,   \
679730623f4SShamir Rabinovitch 					   driver_udata)                       \
680730623f4SShamir Rabinovitch 				      ->context,                               \
681730623f4SShamir Rabinovitch 			      drv_dev_struct, member) :                        \
682730623f4SShamir Rabinovitch 		 (drv_dev_struct *)NULL)
683730623f4SShamir Rabinovitch 
68441b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
68541b2a71fSMatan Barak 
686d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
687d70724f1SMatan Barak 							u16 idx)
688d70724f1SMatan Barak {
689d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
690d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
691d70724f1SMatan Barak 
6923a863577SJason Gunthorpe 	return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
693d70724f1SMatan Barak }
694d70724f1SMatan Barak 
695494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
696494c5580SMatan Barak 					  u16 idx)
697494c5580SMatan Barak {
698494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
699494c5580SMatan Barak 
700494c5580SMatan Barak 	if (IS_ERR(attr))
701494c5580SMatan Barak 		return PTR_ERR(attr);
702494c5580SMatan Barak 
703494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
704494c5580SMatan Barak }
705494c5580SMatan Barak 
706be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
707be934ccaSAriel Levkovich 					u16 idx)
708be934ccaSAriel Levkovich {
709f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
710be934ccaSAriel Levkovich 
711f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
712f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
713f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
714be934ccaSAriel Levkovich 
715f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
716be934ccaSAriel Levkovich }
717be934ccaSAriel Levkovich 
7183efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
7193efa3812SMatan Barak 							 u16 idx)
7203efa3812SMatan Barak {
7213efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
7223efa3812SMatan Barak 
7233efa3812SMatan Barak 	if (IS_ERR(attr))
7243efa3812SMatan Barak 		return ERR_CAST(attr);
7253efa3812SMatan Barak 
7263efa3812SMatan Barak 	return attr->obj_attr.uobject;
7273efa3812SMatan Barak }
7283efa3812SMatan Barak 
7298762d149SMatan Barak static inline int
7308762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
7318762d149SMatan Barak {
7328762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
7338762d149SMatan Barak 
7348762d149SMatan Barak 	if (IS_ERR(attr))
7358762d149SMatan Barak 		return PTR_ERR(attr);
7368762d149SMatan Barak 
7378762d149SMatan Barak 	return attr->ptr_attr.len;
7388762d149SMatan Barak }
7398762d149SMatan Barak 
740cbfdd442SMoni Shoua /*
741cbfdd442SMoni Shoua  * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
742cbfdd442SMoni Shoua  * attribute.
743cbfdd442SMoni Shoua  * @attrs: The attribute bundle
744cbfdd442SMoni Shoua  * @idx: The ID of the attribute
745cbfdd442SMoni Shoua  * @elem_size: The size of the element in the array
746cbfdd442SMoni Shoua  */
747cbfdd442SMoni Shoua static inline int
748cbfdd442SMoni Shoua uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
749cbfdd442SMoni Shoua 			       size_t elem_size)
750cbfdd442SMoni Shoua {
751cbfdd442SMoni Shoua 	int size = uverbs_attr_get_len(attrs, idx);
752cbfdd442SMoni Shoua 
753cbfdd442SMoni Shoua 	if (size < 0)
754cbfdd442SMoni Shoua 		return size;
755cbfdd442SMoni Shoua 
756cbfdd442SMoni Shoua 	if (size % elem_size)
757cbfdd442SMoni Shoua 		return -EINVAL;
758cbfdd442SMoni Shoua 
759cbfdd442SMoni Shoua 	return size / elem_size;
760cbfdd442SMoni Shoua }
761cbfdd442SMoni Shoua 
76270cd20aeSGuy Levi /**
76370cd20aeSGuy Levi  * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
76470cd20aeSGuy Levi  * UVERBS_ATTR_TYPE_IDRS_ARRAY.
76570cd20aeSGuy Levi  * @arr: Returned pointer to array of pointers for uobjects or NULL if
76670cd20aeSGuy Levi  *       the attribute isn't provided.
76770cd20aeSGuy Levi  *
76870cd20aeSGuy Levi  * Return: The array length or 0 if no attribute was provided.
76970cd20aeSGuy Levi  */
77070cd20aeSGuy Levi static inline int uverbs_attr_get_uobjs_arr(
77170cd20aeSGuy Levi 	const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
77270cd20aeSGuy Levi 	struct ib_uobject ***arr)
77370cd20aeSGuy Levi {
77470cd20aeSGuy Levi 	const struct uverbs_attr *attr =
77570cd20aeSGuy Levi 			uverbs_attr_get(attrs_bundle, attr_idx);
77670cd20aeSGuy Levi 
77770cd20aeSGuy Levi 	if (IS_ERR(attr)) {
77870cd20aeSGuy Levi 		*arr = NULL;
77970cd20aeSGuy Levi 		return 0;
78070cd20aeSGuy Levi 	}
78170cd20aeSGuy Levi 
78270cd20aeSGuy Levi 	*arr = attr->objs_arr_attr.uobjects;
78370cd20aeSGuy Levi 
78470cd20aeSGuy Levi 	return attr->objs_arr_attr.len;
78570cd20aeSGuy Levi }
78670cd20aeSGuy Levi 
78789d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
78889d9e8d3SMatan Barak {
78989d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
79089d9e8d3SMatan Barak }
79189d9e8d3SMatan Barak 
7928762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
7938762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
7948762d149SMatan Barak {
7958762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
7968762d149SMatan Barak 
7978762d149SMatan Barak 	if (IS_ERR(attr))
7988762d149SMatan Barak 		return (void *)attr;
7998762d149SMatan Barak 
8008762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
8018762d149SMatan Barak 						 attr->ptr_attr.ptr;
8028762d149SMatan Barak }
8038762d149SMatan Barak 
80489d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
805d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
80689d9e8d3SMatan Barak 				    size_t idx,
80789d9e8d3SMatan Barak 				    size_t size)
808d70724f1SMatan Barak {
809d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
810d70724f1SMatan Barak 
811d70724f1SMatan Barak 	if (IS_ERR(attr))
812d70724f1SMatan Barak 		return PTR_ERR(attr);
813d70724f1SMatan Barak 
81489d9e8d3SMatan Barak 	/*
81589d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
816c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
817c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
81889d9e8d3SMatan Barak 	 */
81989d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
82089d9e8d3SMatan Barak 		return -EINVAL;
82189d9e8d3SMatan Barak 
82289d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
823d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
8242f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
8252f36028cSJason Gunthorpe 				attr->ptr_attr.len))
826d70724f1SMatan Barak 		return -EFAULT;
827d70724f1SMatan Barak 
828d70724f1SMatan Barak 	return 0;
829d70724f1SMatan Barak }
830d70724f1SMatan Barak 
831c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
832c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
833c66db311SMatan Barak 					    size_t idx,
834c66db311SMatan Barak 					    size_t size)
835c66db311SMatan Barak {
836c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
837c66db311SMatan Barak 	size_t min_size;
838c66db311SMatan Barak 
839c66db311SMatan Barak 	if (IS_ERR(attr))
840c66db311SMatan Barak 		return PTR_ERR(attr);
841c66db311SMatan Barak 
842c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
843c66db311SMatan Barak 
844c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
845c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
846c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
847c66db311SMatan Barak 				min_size))
848c66db311SMatan Barak 		return -EFAULT;
849c66db311SMatan Barak 
850c66db311SMatan Barak 	if (size > min_size)
851c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
852c66db311SMatan Barak 
853c66db311SMatan Barak 	return 0;
854c66db311SMatan Barak }
855c66db311SMatan Barak 
856d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
85789d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
858d70724f1SMatan Barak 
859c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
860c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
861c66db311SMatan Barak 
8628313c10fSJason Gunthorpe static inline struct ib_ucontext *
8638313c10fSJason Gunthorpe ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
8648313c10fSJason Gunthorpe {
8658313c10fSJason Gunthorpe 	return ib_uverbs_get_ucontext_file(attrs->ufile);
8668313c10fSJason Gunthorpe }
8678313c10fSJason Gunthorpe 
868bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
869bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
870bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
871bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
872bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
8736a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
8746a1f444fSJason Gunthorpe 		   const void *from, size_t size);
875461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
876461bb2eeSJason Gunthorpe 			     gfp_t flags);
877461bb2eeSJason Gunthorpe 
878461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
879461bb2eeSJason Gunthorpe 					  size_t size)
880461bb2eeSJason Gunthorpe {
881461bb2eeSJason Gunthorpe 	return _uverbs_alloc(bundle, size, GFP_KERNEL);
882461bb2eeSJason Gunthorpe }
883461bb2eeSJason Gunthorpe 
884461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
885461bb2eeSJason Gunthorpe 					   size_t size)
886461bb2eeSJason Gunthorpe {
887461bb2eeSJason Gunthorpe 	return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
888461bb2eeSJason Gunthorpe }
8890953fffeSMark Bloch int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
8900953fffeSMark Bloch 		      size_t idx, s64 lower_bound, u64 upper_bound,
8910953fffeSMark Bloch 		      s64 *def_val);
8922e8039c6SMichael Guralnik int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
8932e8039c6SMichael Guralnik 				  size_t idx, const void *from, size_t size);
894bccd0622SJason Gunthorpe #else
895bccd0622SJason Gunthorpe static inline int
896bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
897bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
898bccd0622SJason Gunthorpe {
899bccd0622SJason Gunthorpe 	return -EINVAL;
900bccd0622SJason Gunthorpe }
901bccd0622SJason Gunthorpe static inline int
902bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
903bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
904bccd0622SJason Gunthorpe {
905bccd0622SJason Gunthorpe 	return -EINVAL;
906bccd0622SJason Gunthorpe }
9076a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
9086a1f444fSJason Gunthorpe 				 size_t idx, const void *from, size_t size)
9096a1f444fSJason Gunthorpe {
9106a1f444fSJason Gunthorpe 	return -EINVAL;
9116a1f444fSJason Gunthorpe }
912461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
913461bb2eeSJason Gunthorpe 					  size_t size)
914461bb2eeSJason Gunthorpe {
915461bb2eeSJason Gunthorpe 	return ERR_PTR(-EINVAL);
916461bb2eeSJason Gunthorpe }
917461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
918461bb2eeSJason Gunthorpe 					   size_t size)
919461bb2eeSJason Gunthorpe {
920461bb2eeSJason Gunthorpe 	return ERR_PTR(-EINVAL);
921461bb2eeSJason Gunthorpe }
9220953fffeSMark Bloch static inline int
9230953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
9240953fffeSMark Bloch 		  size_t idx, s64 lower_bound, u64 upper_bound,
9250953fffeSMark Bloch 		  s64 *def_val)
9260953fffeSMark Bloch {
9270953fffeSMark Bloch 	return -EINVAL;
9280953fffeSMark Bloch }
9292e8039c6SMichael Guralnik static inline int
9302e8039c6SMichael Guralnik uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
9312e8039c6SMichael Guralnik 			      size_t idx, const void *from, size_t size)
9322e8039c6SMichael Guralnik {
9332e8039c6SMichael Guralnik 	return -EINVAL;
9342e8039c6SMichael Guralnik }
935bccd0622SJason Gunthorpe #endif
936bccd0622SJason Gunthorpe 
9370953fffeSMark Bloch #define uverbs_get_const(_to, _attrs_bundle, _idx)                             \
9380953fffeSMark Bloch 	({                                                                     \
9390953fffeSMark Bloch 		s64 _val;                                                      \
9400953fffeSMark Bloch 		int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx,       \
9410953fffeSMark Bloch 					     type_min(typeof(*_to)),           \
9420953fffeSMark Bloch 					     type_max(typeof(*_to)), NULL);    \
9430953fffeSMark Bloch 		(*_to) = _val;                                                 \
9440953fffeSMark Bloch 		_ret;                                                          \
9450953fffeSMark Bloch 	})
9460953fffeSMark Bloch 
9470953fffeSMark Bloch #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default)           \
9480953fffeSMark Bloch 	({                                                                     \
9490953fffeSMark Bloch 		s64 _val;                                                      \
9500953fffeSMark Bloch 		s64 _def_val = _default;                                       \
9510953fffeSMark Bloch 		int _ret =                                                     \
9520953fffeSMark Bloch 			_uverbs_get_const(&_val, _attrs_bundle, _idx,          \
9530953fffeSMark Bloch 					  type_min(typeof(*_to)),              \
9540953fffeSMark Bloch 					  type_max(typeof(*_to)), &_def_val);  \
9550953fffeSMark Bloch 		(*_to) = _val;                                                 \
9560953fffeSMark Bloch 		_ret;                                                          \
9570953fffeSMark Bloch 	})
958118620d3SMatan Barak #endif
959