xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision bccd06223f21654eb268e153426a77deb117c1e8)
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,
55f43dbebfSMatan Barak };
56f43dbebfSMatan Barak 
57a0aa309cSMatan Barak enum uverbs_obj_access {
58a0aa309cSMatan Barak 	UVERBS_ACCESS_READ,
59a0aa309cSMatan Barak 	UVERBS_ACCESS_WRITE,
60a0aa309cSMatan Barak 	UVERBS_ACCESS_NEW,
61a0aa309cSMatan Barak 	UVERBS_ACCESS_DESTROY
62a0aa309cSMatan Barak };
63a0aa309cSMatan Barak 
641f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */
6583bb4442SJason Gunthorpe /* good size 16 */
66f43dbebfSMatan Barak struct uverbs_attr_spec {
67d108dac0SJason Gunthorpe 	u8 type;
6883bb4442SJason Gunthorpe 
6983bb4442SJason Gunthorpe 	/*
70422e3d37SJason Gunthorpe 	 * Support extending attributes by length. Allow the user to provide
71422e3d37SJason Gunthorpe 	 * more bytes than ptr.len, but check that everything after is zero'd
72422e3d37SJason Gunthorpe 	 * by the user.
7383bb4442SJason Gunthorpe 	 */
74422e3d37SJason Gunthorpe 	u8 zero_trailing:1;
7583bb4442SJason Gunthorpe 	/*
7683bb4442SJason Gunthorpe 	 * Valid only for PTR_IN. Allocate and copy the data inside
7783bb4442SJason Gunthorpe 	 * the parser
7883bb4442SJason Gunthorpe 	 */
7983bb4442SJason Gunthorpe 	u8 alloc_and_copy:1;
8083bb4442SJason Gunthorpe 	u8 mandatory:1;
81d108dac0SJason Gunthorpe 
82fac9658cSMatan Barak 	union {
83f43dbebfSMatan Barak 		struct {
84c66db311SMatan Barak 			/* Current known size to kernel */
851f07e08fSMatan Barak 			u16 len;
86c66db311SMatan Barak 			/* User isn't allowed to provide something < min_len */
87c66db311SMatan Barak 			u16 min_len;
881f07e08fSMatan Barak 		} ptr;
89d108dac0SJason Gunthorpe 
901f07e08fSMatan Barak 		struct {
91f43dbebfSMatan Barak 			/*
92f43dbebfSMatan Barak 			 * higher bits mean the namespace and lower bits mean
93f43dbebfSMatan Barak 			 * the type id within the namespace.
94f43dbebfSMatan Barak 			 */
95f43dbebfSMatan Barak 			u16 obj_type;
96f43dbebfSMatan Barak 			u8 access;
97f43dbebfSMatan Barak 		} obj;
98d108dac0SJason Gunthorpe 
99494c5580SMatan Barak 		struct {
100494c5580SMatan Barak 			u8 num_elems;
101d108dac0SJason Gunthorpe 		} enum_def;
102d108dac0SJason Gunthorpe 	} u;
103d108dac0SJason Gunthorpe 
104d108dac0SJason Gunthorpe 	/* This weird split of the enum lets us remove some padding */
105d108dac0SJason Gunthorpe 	union {
106d108dac0SJason Gunthorpe 		struct {
107494c5580SMatan Barak 			/*
108494c5580SMatan Barak 			 * The enum attribute can select one of the attributes
109494c5580SMatan Barak 			 * contained in the ids array. Currently only PTR_IN
110494c5580SMatan Barak 			 * attributes are supported in the ids array.
111494c5580SMatan Barak 			 */
112494c5580SMatan Barak 			const struct uverbs_attr_spec *ids;
113494c5580SMatan Barak 		} enum_def;
114d108dac0SJason Gunthorpe 	} u2;
115fac9658cSMatan Barak };
116f43dbebfSMatan Barak 
117f43dbebfSMatan Barak struct uverbs_attr_spec_hash {
118f43dbebfSMatan Barak 	size_t				num_attrs;
119fac9658cSMatan Barak 	unsigned long			*mandatory_attrs_bitmask;
120f43dbebfSMatan Barak 	struct uverbs_attr_spec		attrs[0];
121f43dbebfSMatan Barak };
122f43dbebfSMatan Barak 
123fac9658cSMatan Barak struct uverbs_attr_bundle;
124fac9658cSMatan Barak struct ib_uverbs_file;
125fac9658cSMatan Barak 
126fac9658cSMatan Barak struct uverbs_method_spec {
127fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
128fac9658cSMatan Barak 	u32						flags;
129fac9658cSMatan Barak 	size_t						num_buckets;
130fac9658cSMatan Barak 	size_t						num_child_attrs;
131fac9658cSMatan Barak 	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
132fac9658cSMatan Barak 		       struct uverbs_attr_bundle *ctx);
133fac9658cSMatan Barak 	struct uverbs_attr_spec_hash		*attr_buckets[0];
134fac9658cSMatan Barak };
135fac9658cSMatan Barak 
136fac9658cSMatan Barak struct uverbs_method_spec_hash {
137fac9658cSMatan Barak 	size_t					num_methods;
138fac9658cSMatan Barak 	struct uverbs_method_spec		*methods[0];
139fac9658cSMatan Barak };
140fac9658cSMatan Barak 
141fac9658cSMatan Barak struct uverbs_object_spec {
142fac9658cSMatan Barak 	const struct uverbs_obj_type		*type_attrs;
143fac9658cSMatan Barak 	size_t					num_buckets;
144fac9658cSMatan Barak 	struct uverbs_method_spec_hash		*method_buckets[0];
145fac9658cSMatan Barak };
146fac9658cSMatan Barak 
147fac9658cSMatan Barak struct uverbs_object_spec_hash {
148fac9658cSMatan Barak 	size_t					num_objects;
149fac9658cSMatan Barak 	struct uverbs_object_spec		*objects[0];
150fac9658cSMatan Barak };
151fac9658cSMatan Barak 
152fac9658cSMatan Barak struct uverbs_root_spec {
153fac9658cSMatan Barak 	size_t					num_buckets;
154fac9658cSMatan Barak 	struct uverbs_object_spec_hash		*object_buckets[0];
155fac9658cSMatan Barak };
156fac9658cSMatan Barak 
1575009010fSMatan Barak /*
1585009010fSMatan Barak  * =======================================
1595009010fSMatan Barak  *	Verbs definitions
1605009010fSMatan Barak  * =======================================
1615009010fSMatan Barak  */
1625009010fSMatan Barak 
16309e3ebf8SMatan Barak struct uverbs_attr_def {
16409e3ebf8SMatan Barak 	u16                           id;
16509e3ebf8SMatan Barak 	struct uverbs_attr_spec       attr;
16609e3ebf8SMatan Barak };
16709e3ebf8SMatan Barak 
16809e3ebf8SMatan Barak struct uverbs_method_def {
16909e3ebf8SMatan Barak 	u16                                  id;
17009e3ebf8SMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
17109e3ebf8SMatan Barak 	u32				     flags;
17209e3ebf8SMatan Barak 	size_t				     num_attrs;
17309e3ebf8SMatan Barak 	const struct uverbs_attr_def * const (*attrs)[];
17409e3ebf8SMatan Barak 	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
17509e3ebf8SMatan Barak 		       struct uverbs_attr_bundle *ctx);
17609e3ebf8SMatan Barak };
17709e3ebf8SMatan Barak 
1785009010fSMatan Barak struct uverbs_object_def {
17909e3ebf8SMatan Barak 	u16					 id;
1805009010fSMatan Barak 	const struct uverbs_obj_type	        *type_attrs;
18109e3ebf8SMatan Barak 	size_t				         num_methods;
18209e3ebf8SMatan Barak 	const struct uverbs_method_def * const (*methods)[];
18309e3ebf8SMatan Barak };
18409e3ebf8SMatan Barak 
18509e3ebf8SMatan Barak struct uverbs_object_tree_def {
18609e3ebf8SMatan Barak 	size_t					 num_objects;
18709e3ebf8SMatan Barak 	const struct uverbs_object_def * const (*objects)[];
1885009010fSMatan Barak };
1895009010fSMatan Barak 
190d108dac0SJason Gunthorpe /*
191d108dac0SJason Gunthorpe  * =======================================
192d108dac0SJason Gunthorpe  *	Attribute Specifications
193d108dac0SJason Gunthorpe  * =======================================
194d108dac0SJason Gunthorpe  */
195c66db311SMatan Barak 
196c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len)			\
197d108dac0SJason Gunthorpe 	.u.ptr.min_len = _min_len, .u.ptr.len = _len
198422e3d37SJason Gunthorpe 
199fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
200fd44e385SYishai Hadas 
201422e3d37SJason Gunthorpe /*
202422e3d37SJason Gunthorpe  * Specifies a uapi structure that cannot be extended. The user must always
203422e3d37SJason Gunthorpe  * supply the whole structure and nothing more. The structure must be declared
204422e3d37SJason Gunthorpe  * in a header under include/uapi/rdma.
205422e3d37SJason Gunthorpe  */
206422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type)					\
207422e3d37SJason Gunthorpe 	.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
208422e3d37SJason Gunthorpe /*
209422e3d37SJason Gunthorpe  * Specifies a uapi structure where the user must provide at least up to
210422e3d37SJason Gunthorpe  * member 'last'.  Anything after last and up until the end of the structure
211422e3d37SJason Gunthorpe  * can be non-zero, anything longer than the end of the structure must be
212422e3d37SJason Gunthorpe  * zero. The structure must be declared in a header under include/uapi/rdma.
213422e3d37SJason Gunthorpe  */
214422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last)                                       \
215422e3d37SJason Gunthorpe 	.zero_trailing = 1,                                                    \
216422e3d37SJason Gunthorpe 	UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)),              \
217422e3d37SJason Gunthorpe 			 sizeof(_type))
218540cd692SJason Gunthorpe /*
219540cd692SJason Gunthorpe  * Specifies at least min_len bytes must be passed in, but the amount can be
220540cd692SJason Gunthorpe  * larger, up to the protocol maximum size. No check for zeroing is done.
221540cd692SJason Gunthorpe  */
222540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
223c66db311SMatan Barak 
224d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */
22583bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
22683bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1
22783bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0
22835410306SMatan Barak 
229d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...)                     \
2309a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
231d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
232d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_IDR,                        \
233d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _idr_type,                         \
234d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
235d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
236d108dac0SJason Gunthorpe 
237d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...)                       \
2389a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
239d108dac0SJason Gunthorpe 		.id = (_attr_id) +                                             \
240d108dac0SJason Gunthorpe 		      BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW &&      \
24135410306SMatan Barak 					(_access) != UVERBS_ACCESS_READ),      \
242d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_FD,                         \
243d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _fd_type,                          \
244d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
245d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
24635410306SMatan Barak 
247d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...)                               \
2489a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
249d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
250d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN,                     \
251d108dac0SJason Gunthorpe 			  _type,                                               \
252d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
253d108dac0SJason Gunthorpe 
254d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...)                              \
2559a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
256d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
257d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT,                    \
258d108dac0SJason Gunthorpe 			  _type,                                               \
259d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
260d108dac0SJason Gunthorpe 
261d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
262d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...)                          \
2639a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
264d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
265d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN,                    \
266d108dac0SJason Gunthorpe 			  .u2.enum_def.ids = _enum_arr,                        \
267d108dac0SJason Gunthorpe 			  .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr),       \
268d108dac0SJason Gunthorpe 			  __VA_ARGS__ },                                       \
269d108dac0SJason Gunthorpe 	})
270d108dac0SJason Gunthorpe 
271d108dac0SJason Gunthorpe /*
272*bccd0622SJason Gunthorpe  * An input value that is a bitwise combination of values of _enum_type.
273*bccd0622SJason Gunthorpe  * This permits the flag value to be passed as either a u32 or u64, it must
274*bccd0622SJason Gunthorpe  * be retrieved via uverbs_get_flag().
275*bccd0622SJason Gunthorpe  */
276*bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...)                        \
277*bccd0622SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(                                                    \
278*bccd0622SJason Gunthorpe 		_attr_id,                                                      \
279*bccd0622SJason Gunthorpe 		UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO(              \
280*bccd0622SJason Gunthorpe 						       !sizeof(_enum_type *)), \
281*bccd0622SJason Gunthorpe 				 sizeof(u64)),                                 \
282*bccd0622SJason Gunthorpe 		__VA_ARGS__)
283*bccd0622SJason Gunthorpe 
284*bccd0622SJason Gunthorpe /*
2856c61d2a5SJason Gunthorpe  * This spec is used in order to pass information to the hardware driver in a
2866c61d2a5SJason Gunthorpe  * legacy way. Every verb that could get driver specific data should get this
2876c61d2a5SJason Gunthorpe  * spec.
2886c61d2a5SJason Gunthorpe  */
2896c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW()                                                      \
2909a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN,                                 \
291540cd692SJason Gunthorpe 			   UVERBS_ATTR_MIN_SIZE(0),			       \
292540cd692SJason Gunthorpe 			   UA_OPTIONAL),				       \
2939a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT,                               \
294540cd692SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(0),			       \
295540cd692SJason Gunthorpe 			    UA_OPTIONAL)
2966c61d2a5SJason Gunthorpe 
2976c61d2a5SJason Gunthorpe /*
298d108dac0SJason Gunthorpe  * =======================================
299d108dac0SJason Gunthorpe  *	Declaration helpers
300d108dac0SJason Gunthorpe  * =======================================
301d108dac0SJason Gunthorpe  */
3026c61d2a5SJason Gunthorpe 
30309e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...)                                 \
3046c61d2a5SJason Gunthorpe 	static const struct uverbs_object_def *const _name##_ptr[] = {         \
3056c61d2a5SJason Gunthorpe 		__VA_ARGS__,                                                   \
3066c61d2a5SJason Gunthorpe 	};                                                                     \
3076c61d2a5SJason Gunthorpe 	static const struct uverbs_object_tree_def _name = {                   \
3086c61d2a5SJason Gunthorpe 		.num_objects = ARRAY_SIZE(_name##_ptr),                        \
3096c61d2a5SJason Gunthorpe 		.objects = &_name##_ptr,                                       \
3106c61d2a5SJason Gunthorpe 	}
31109e3ebf8SMatan Barak 
312fac9658cSMatan Barak /* =================================================
313fac9658cSMatan Barak  *              Parsing infrastructure
314fac9658cSMatan Barak  * =================================================
315fac9658cSMatan Barak  */
316fac9658cSMatan Barak 
317fac9658cSMatan Barak struct uverbs_ptr_attr {
3188762d149SMatan Barak 	/*
3198762d149SMatan Barak 	 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
3208762d149SMatan Barak 	 * used.
3218762d149SMatan Barak 	 */
3228762d149SMatan Barak 	union {
3238762d149SMatan Barak 		void *ptr;
324fac9658cSMatan Barak 		u64 data;
3258762d149SMatan Barak 	};
326fac9658cSMatan Barak 	u16		len;
327fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
328fac9658cSMatan Barak 	u16		flags;
329494c5580SMatan Barak 	u8		enum_id;
330fac9658cSMatan Barak };
331fac9658cSMatan Barak 
332f43dbebfSMatan Barak struct uverbs_obj_attr {
333f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
334f43dbebfSMatan Barak };
335f43dbebfSMatan Barak 
336f43dbebfSMatan Barak struct uverbs_attr {
337fac9658cSMatan Barak 	/*
338fac9658cSMatan Barak 	 * pointer to the user-space given attribute, in order to write the
339fac9658cSMatan Barak 	 * new uobject's id or update flags.
340fac9658cSMatan Barak 	 */
341fac9658cSMatan Barak 	struct ib_uverbs_attr __user	*uattr;
342fac9658cSMatan Barak 	union {
343fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
344f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
345f43dbebfSMatan Barak 	};
346fac9658cSMatan Barak };
347f43dbebfSMatan Barak 
348f43dbebfSMatan Barak struct uverbs_attr_bundle_hash {
349f43dbebfSMatan Barak 	/* if bit i is set, it means attrs[i] contains valid information */
350f43dbebfSMatan Barak 	unsigned long *valid_bitmap;
351f43dbebfSMatan Barak 	size_t num_attrs;
352f43dbebfSMatan Barak 	/*
353f43dbebfSMatan Barak 	 * arrays of attributes, each element corresponds to the specification
354f43dbebfSMatan Barak 	 * of the attribute in the same index.
355f43dbebfSMatan Barak 	 */
356f43dbebfSMatan Barak 	struct uverbs_attr *attrs;
357f43dbebfSMatan Barak };
358f43dbebfSMatan Barak 
359f43dbebfSMatan Barak struct uverbs_attr_bundle {
360f43dbebfSMatan Barak 	size_t				num_buckets;
361f43dbebfSMatan Barak 	struct uverbs_attr_bundle_hash  hash[];
362f43dbebfSMatan Barak };
363f43dbebfSMatan Barak 
364f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
365f43dbebfSMatan Barak 						unsigned int idx)
366f43dbebfSMatan Barak {
367f43dbebfSMatan Barak 	return test_bit(idx, attrs_hash->valid_bitmap);
368f43dbebfSMatan Barak }
369f43dbebfSMatan Barak 
37035410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
37135410306SMatan Barak 					unsigned int idx)
37235410306SMatan Barak {
37335410306SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
37435410306SMatan Barak 
37535410306SMatan Barak 	if (attrs_bundle->num_buckets <= idx_bucket)
37635410306SMatan Barak 		return false;
37735410306SMatan Barak 
37835410306SMatan Barak 	return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
37935410306SMatan Barak 					    idx & ~UVERBS_ID_NS_MASK);
38035410306SMatan Barak }
38135410306SMatan Barak 
38241b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
38341b2a71fSMatan Barak 
384d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
385d70724f1SMatan Barak 							u16 idx)
386d70724f1SMatan Barak {
387d70724f1SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
388d70724f1SMatan Barak 
389d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
390d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
391d70724f1SMatan Barak 
392d70724f1SMatan Barak 	return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
393d70724f1SMatan Barak }
394d70724f1SMatan Barak 
395494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
396494c5580SMatan Barak 					  u16 idx)
397494c5580SMatan Barak {
398494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
399494c5580SMatan Barak 
400494c5580SMatan Barak 	if (IS_ERR(attr))
401494c5580SMatan Barak 		return PTR_ERR(attr);
402494c5580SMatan Barak 
403494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
404494c5580SMatan Barak }
405494c5580SMatan Barak 
406be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
407be934ccaSAriel Levkovich 					u16 idx)
408be934ccaSAriel Levkovich {
409f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
410be934ccaSAriel Levkovich 
411f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
412f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
413f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
414be934ccaSAriel Levkovich 
415f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
416be934ccaSAriel Levkovich }
417be934ccaSAriel Levkovich 
4183efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
4193efa3812SMatan Barak 							 u16 idx)
4203efa3812SMatan Barak {
4213efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
4223efa3812SMatan Barak 
4233efa3812SMatan Barak 	if (IS_ERR(attr))
4243efa3812SMatan Barak 		return ERR_CAST(attr);
4253efa3812SMatan Barak 
4263efa3812SMatan Barak 	return attr->obj_attr.uobject;
4273efa3812SMatan Barak }
4283efa3812SMatan Barak 
4298762d149SMatan Barak static inline int
4308762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
4318762d149SMatan Barak {
4328762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
4338762d149SMatan Barak 
4348762d149SMatan Barak 	if (IS_ERR(attr))
4358762d149SMatan Barak 		return PTR_ERR(attr);
4368762d149SMatan Barak 
4378762d149SMatan Barak 	return attr->ptr_attr.len;
4388762d149SMatan Barak }
4398762d149SMatan Barak 
440d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
44189d9e8d3SMatan Barak 				 size_t idx, const void *from, size_t size)
442d70724f1SMatan Barak {
443d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
444d70724f1SMatan Barak 	u16 flags;
44589d9e8d3SMatan Barak 	size_t min_size;
446d70724f1SMatan Barak 
447d70724f1SMatan Barak 	if (IS_ERR(attr))
448d70724f1SMatan Barak 		return PTR_ERR(attr);
449d70724f1SMatan Barak 
45089d9e8d3SMatan Barak 	min_size = min_t(size_t, attr->ptr_attr.len, size);
4512f36028cSJason Gunthorpe 	if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
45289d9e8d3SMatan Barak 		return -EFAULT;
45389d9e8d3SMatan Barak 
454d70724f1SMatan Barak 	flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
45589d9e8d3SMatan Barak 	if (put_user(flags, &attr->uattr->flags))
45689d9e8d3SMatan Barak 		return -EFAULT;
45789d9e8d3SMatan Barak 
45889d9e8d3SMatan Barak 	return 0;
459d70724f1SMatan Barak }
460d70724f1SMatan Barak 
46189d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
46289d9e8d3SMatan Barak {
46389d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
46489d9e8d3SMatan Barak }
46589d9e8d3SMatan Barak 
4668762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
4678762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
4688762d149SMatan Barak {
4698762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
4708762d149SMatan Barak 
4718762d149SMatan Barak 	if (IS_ERR(attr))
4728762d149SMatan Barak 		return (void *)attr;
4738762d149SMatan Barak 
4748762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
4758762d149SMatan Barak 						 attr->ptr_attr.ptr;
4768762d149SMatan Barak }
4778762d149SMatan Barak 
47889d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
479d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
48089d9e8d3SMatan Barak 				    size_t idx,
48189d9e8d3SMatan Barak 				    size_t size)
482d70724f1SMatan Barak {
483d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
484d70724f1SMatan Barak 
485d70724f1SMatan Barak 	if (IS_ERR(attr))
486d70724f1SMatan Barak 		return PTR_ERR(attr);
487d70724f1SMatan Barak 
48889d9e8d3SMatan Barak 	/*
48989d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
490c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
491c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
49289d9e8d3SMatan Barak 	 */
49389d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
49489d9e8d3SMatan Barak 		return -EINVAL;
49589d9e8d3SMatan Barak 
49689d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
497d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
4982f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
4992f36028cSJason Gunthorpe 				attr->ptr_attr.len))
500d70724f1SMatan Barak 		return -EFAULT;
501d70724f1SMatan Barak 
502d70724f1SMatan Barak 	return 0;
503d70724f1SMatan Barak }
504d70724f1SMatan Barak 
505c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
506c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
507c66db311SMatan Barak 					    size_t idx,
508c66db311SMatan Barak 					    size_t size)
509c66db311SMatan Barak {
510c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
511c66db311SMatan Barak 	size_t min_size;
512c66db311SMatan Barak 
513c66db311SMatan Barak 	if (IS_ERR(attr))
514c66db311SMatan Barak 		return PTR_ERR(attr);
515c66db311SMatan Barak 
516c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
517c66db311SMatan Barak 
518c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
519c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
520c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
521c66db311SMatan Barak 				min_size))
522c66db311SMatan Barak 		return -EFAULT;
523c66db311SMatan Barak 
524c66db311SMatan Barak 	if (size > min_size)
525c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
526c66db311SMatan Barak 
527c66db311SMatan Barak 	return 0;
528c66db311SMatan Barak }
529c66db311SMatan Barak 
530d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
53189d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
532d70724f1SMatan Barak 
533c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
534c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
535c66db311SMatan Barak 
536*bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
537*bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
538*bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
539*bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
540*bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
541*bccd0622SJason Gunthorpe #else
542*bccd0622SJason Gunthorpe static inline int
543*bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
544*bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
545*bccd0622SJason Gunthorpe {
546*bccd0622SJason Gunthorpe 	return -EINVAL;
547*bccd0622SJason Gunthorpe }
548*bccd0622SJason Gunthorpe static inline int
549*bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
550*bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
551*bccd0622SJason Gunthorpe {
552*bccd0622SJason Gunthorpe 	return -EINVAL;
553*bccd0622SJason Gunthorpe }
554*bccd0622SJason Gunthorpe #endif
555*bccd0622SJason Gunthorpe 
556118620d3SMatan Barak /* =================================================
557118620d3SMatan Barak  *	 Definitions -> Specs infrastructure
558118620d3SMatan Barak  * =================================================
559118620d3SMatan Barak  */
560a0aa309cSMatan Barak 
561118620d3SMatan Barak /*
562118620d3SMatan Barak  * uverbs_alloc_spec_tree - Merges different common and driver specific feature
563118620d3SMatan Barak  *	into one parsing tree that every uverbs command will be parsed upon.
564118620d3SMatan Barak  *
565118620d3SMatan Barak  * @num_trees: Number of trees in the array @trees.
566118620d3SMatan Barak  * @trees: Array of pointers to tree root definitions to merge. Each such tree
567118620d3SMatan Barak  *	   possibly contains objects, methods and attributes definitions.
568118620d3SMatan Barak  *
569118620d3SMatan Barak  * Returns:
570118620d3SMatan Barak  *	uverbs_root_spec *: The root of the merged parsing tree.
571118620d3SMatan Barak  *	On error, we return an error code. Error is checked via IS_ERR.
572118620d3SMatan Barak  *
573118620d3SMatan Barak  * The following merges could take place:
574118620d3SMatan Barak  * a. Two trees representing the same method with different handler
575118620d3SMatan Barak  *	-> We take the handler of the tree that its handler != NULL
576118620d3SMatan Barak  *	   and its index in the trees array is greater. The incentive for that
577118620d3SMatan Barak  *	   is that developers are expected to first merge common trees and then
578118620d3SMatan Barak  *	   merge trees that gives specialized the behaviour.
579118620d3SMatan Barak  * b. Two trees representing the same object with different
580118620d3SMatan Barak  *    type_attrs (struct uverbs_obj_type):
581118620d3SMatan Barak  *	-> We take the type_attrs of the tree that its type_attr != NULL
582118620d3SMatan Barak  *	   and its index in the trees array is greater. This could be used
583118620d3SMatan Barak  *	   in order to override the free function, allocation size, etc.
584118620d3SMatan Barak  * c. Two trees representing the same method attribute (same id but possibly
585118620d3SMatan Barak  *    different attributes):
586118620d3SMatan Barak  *	-> ERROR (-ENOENT), we believe that's not the programmer's intent.
587118620d3SMatan Barak  *
588118620d3SMatan Barak  * An object without any methods is considered invalid and will abort the
589118620d3SMatan Barak  * function with -ENOENT error.
590118620d3SMatan Barak  */
59152427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
592118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
593118620d3SMatan Barak 						const struct uverbs_object_tree_def **trees);
594118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root);
59552427112SMatan Barak #else
59652427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
59752427112SMatan Barak 							      const struct uverbs_object_tree_def **trees)
59852427112SMatan Barak {
59952427112SMatan Barak 	return NULL;
60052427112SMatan Barak }
60152427112SMatan Barak 
60252427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
60352427112SMatan Barak {
60452427112SMatan Barak }
60552427112SMatan Barak #endif
606118620d3SMatan Barak 
607118620d3SMatan Barak #endif
608