xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 540cd69209682a351ab76b83b85ea856b8192720)
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 	/*
7083bb4442SJason Gunthorpe 	 * Support extending attributes by length, validate all
7183bb4442SJason Gunthorpe 	 * unknown size == zero
7283bb4442SJason Gunthorpe 	 */
7383bb4442SJason Gunthorpe 	u8 min_sz_or_zero:1;
7483bb4442SJason Gunthorpe 	/*
7583bb4442SJason Gunthorpe 	 * Valid only for PTR_IN. Allocate and copy the data inside
7683bb4442SJason Gunthorpe 	 * the parser
7783bb4442SJason Gunthorpe 	 */
7883bb4442SJason Gunthorpe 	u8 alloc_and_copy:1;
7983bb4442SJason Gunthorpe 	u8 mandatory:1;
80d108dac0SJason Gunthorpe 
81fac9658cSMatan Barak 	union {
82f43dbebfSMatan Barak 		struct {
83c66db311SMatan Barak 			/* Current known size to kernel */
841f07e08fSMatan Barak 			u16 len;
85c66db311SMatan Barak 			/* User isn't allowed to provide something < min_len */
86c66db311SMatan Barak 			u16 min_len;
871f07e08fSMatan Barak 		} ptr;
88d108dac0SJason Gunthorpe 
891f07e08fSMatan Barak 		struct {
90f43dbebfSMatan Barak 			/*
91f43dbebfSMatan Barak 			 * higher bits mean the namespace and lower bits mean
92f43dbebfSMatan Barak 			 * the type id within the namespace.
93f43dbebfSMatan Barak 			 */
94f43dbebfSMatan Barak 			u16 obj_type;
95f43dbebfSMatan Barak 			u8 access;
96f43dbebfSMatan Barak 		} obj;
97d108dac0SJason Gunthorpe 
98494c5580SMatan Barak 		struct {
99494c5580SMatan Barak 			u8 num_elems;
100d108dac0SJason Gunthorpe 		} enum_def;
101d108dac0SJason Gunthorpe 	} u;
102d108dac0SJason Gunthorpe 
103d108dac0SJason Gunthorpe 	/* This weird split of the enum lets us remove some padding */
104d108dac0SJason Gunthorpe 	union {
105d108dac0SJason Gunthorpe 		struct {
106494c5580SMatan Barak 			/*
107494c5580SMatan Barak 			 * The enum attribute can select one of the attributes
108494c5580SMatan Barak 			 * contained in the ids array. Currently only PTR_IN
109494c5580SMatan Barak 			 * attributes are supported in the ids array.
110494c5580SMatan Barak 			 */
111494c5580SMatan Barak 			const struct uverbs_attr_spec *ids;
112494c5580SMatan Barak 		} enum_def;
113d108dac0SJason Gunthorpe 	} u2;
114fac9658cSMatan Barak };
115f43dbebfSMatan Barak 
116f43dbebfSMatan Barak struct uverbs_attr_spec_hash {
117f43dbebfSMatan Barak 	size_t				num_attrs;
118fac9658cSMatan Barak 	unsigned long			*mandatory_attrs_bitmask;
119f43dbebfSMatan Barak 	struct uverbs_attr_spec		attrs[0];
120f43dbebfSMatan Barak };
121f43dbebfSMatan Barak 
122fac9658cSMatan Barak struct uverbs_attr_bundle;
123fac9658cSMatan Barak struct ib_uverbs_file;
124fac9658cSMatan Barak 
125fac9658cSMatan Barak enum {
126fac9658cSMatan Barak 	/*
127fac9658cSMatan Barak 	 * Action marked with this flag creates a context (or root for all
128fac9658cSMatan Barak 	 * objects).
129fac9658cSMatan Barak 	 */
130fac9658cSMatan Barak 	UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
131fac9658cSMatan Barak };
132fac9658cSMatan Barak 
133fac9658cSMatan Barak struct uverbs_method_spec {
134fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
135fac9658cSMatan Barak 	u32						flags;
136fac9658cSMatan Barak 	size_t						num_buckets;
137fac9658cSMatan Barak 	size_t						num_child_attrs;
138fac9658cSMatan Barak 	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
139fac9658cSMatan Barak 		       struct uverbs_attr_bundle *ctx);
140fac9658cSMatan Barak 	struct uverbs_attr_spec_hash		*attr_buckets[0];
141fac9658cSMatan Barak };
142fac9658cSMatan Barak 
143fac9658cSMatan Barak struct uverbs_method_spec_hash {
144fac9658cSMatan Barak 	size_t					num_methods;
145fac9658cSMatan Barak 	struct uverbs_method_spec		*methods[0];
146fac9658cSMatan Barak };
147fac9658cSMatan Barak 
148fac9658cSMatan Barak struct uverbs_object_spec {
149fac9658cSMatan Barak 	const struct uverbs_obj_type		*type_attrs;
150fac9658cSMatan Barak 	size_t					num_buckets;
151fac9658cSMatan Barak 	struct uverbs_method_spec_hash		*method_buckets[0];
152fac9658cSMatan Barak };
153fac9658cSMatan Barak 
154fac9658cSMatan Barak struct uverbs_object_spec_hash {
155fac9658cSMatan Barak 	size_t					num_objects;
156fac9658cSMatan Barak 	struct uverbs_object_spec		*objects[0];
157fac9658cSMatan Barak };
158fac9658cSMatan Barak 
159fac9658cSMatan Barak struct uverbs_root_spec {
160fac9658cSMatan Barak 	size_t					num_buckets;
161fac9658cSMatan Barak 	struct uverbs_object_spec_hash		*object_buckets[0];
162fac9658cSMatan Barak };
163fac9658cSMatan Barak 
1645009010fSMatan Barak /*
1655009010fSMatan Barak  * =======================================
1665009010fSMatan Barak  *	Verbs definitions
1675009010fSMatan Barak  * =======================================
1685009010fSMatan Barak  */
1695009010fSMatan Barak 
17009e3ebf8SMatan Barak struct uverbs_attr_def {
17109e3ebf8SMatan Barak 	u16                           id;
17209e3ebf8SMatan Barak 	struct uverbs_attr_spec       attr;
17309e3ebf8SMatan Barak };
17409e3ebf8SMatan Barak 
17509e3ebf8SMatan Barak struct uverbs_method_def {
17609e3ebf8SMatan Barak 	u16                                  id;
17709e3ebf8SMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
17809e3ebf8SMatan Barak 	u32				     flags;
17909e3ebf8SMatan Barak 	size_t				     num_attrs;
18009e3ebf8SMatan Barak 	const struct uverbs_attr_def * const (*attrs)[];
18109e3ebf8SMatan Barak 	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
18209e3ebf8SMatan Barak 		       struct uverbs_attr_bundle *ctx);
18309e3ebf8SMatan Barak };
18409e3ebf8SMatan Barak 
1855009010fSMatan Barak struct uverbs_object_def {
18609e3ebf8SMatan Barak 	u16					 id;
1875009010fSMatan Barak 	const struct uverbs_obj_type	        *type_attrs;
18809e3ebf8SMatan Barak 	size_t				         num_methods;
18909e3ebf8SMatan Barak 	const struct uverbs_method_def * const (*methods)[];
19009e3ebf8SMatan Barak };
19109e3ebf8SMatan Barak 
19209e3ebf8SMatan Barak struct uverbs_object_tree_def {
19309e3ebf8SMatan Barak 	size_t					 num_objects;
19409e3ebf8SMatan Barak 	const struct uverbs_object_def * const (*objects)[];
1955009010fSMatan Barak };
1965009010fSMatan Barak 
197d108dac0SJason Gunthorpe /*
198d108dac0SJason Gunthorpe  * =======================================
199d108dac0SJason Gunthorpe  *	Attribute Specifications
200d108dac0SJason Gunthorpe  * =======================================
201d108dac0SJason Gunthorpe  */
202c66db311SMatan Barak 
203d108dac0SJason Gunthorpe /* Use in the _type parameter for attribute specifications */
204c66db311SMatan Barak #define UVERBS_ATTR_TYPE(_type)					\
205d108dac0SJason Gunthorpe 	.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
206c66db311SMatan Barak #define UVERBS_ATTR_STRUCT(_type, _last)			\
207d108dac0SJason Gunthorpe 	.u.ptr.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .u.ptr.len = sizeof(_type)
208c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len)			\
209d108dac0SJason Gunthorpe 	.u.ptr.min_len = _min_len, .u.ptr.len = _len
210*540cd692SJason Gunthorpe /*
211*540cd692SJason Gunthorpe  * Specifies at least min_len bytes must be passed in, but the amount can be
212*540cd692SJason Gunthorpe  * larger, up to the protocol maximum size. No check for zeroing is done.
213*540cd692SJason Gunthorpe  */
214*540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
215c66db311SMatan Barak 
216d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */
21783bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
21883bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1
21983bb4442SJason Gunthorpe #define UA_MIN_SZ_OR_ZERO .min_sz_or_zero = 1
22083bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0
22135410306SMatan Barak 
222d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...)                     \
2239a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
224d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
225d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_IDR,                        \
226d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _idr_type,                         \
227d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
228d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
229d108dac0SJason Gunthorpe 
230d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...)                       \
2319a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
232d108dac0SJason Gunthorpe 		.id = (_attr_id) +                                             \
233d108dac0SJason Gunthorpe 		      BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW &&      \
23435410306SMatan Barak 					(_access) != UVERBS_ACCESS_READ),      \
235d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_FD,                         \
236d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _fd_type,                          \
237d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
238d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
23935410306SMatan Barak 
240d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...)                               \
2419a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
242d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
243d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN,                     \
244d108dac0SJason Gunthorpe 			  _type,                                               \
245d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
246d108dac0SJason Gunthorpe 
247d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...)                              \
2489a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
249d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
250d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT,                    \
251d108dac0SJason Gunthorpe 			  _type,                                               \
252d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
253d108dac0SJason Gunthorpe 
254d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
255d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...)                          \
2569a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
257d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
258d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN,                    \
259d108dac0SJason Gunthorpe 			  .u2.enum_def.ids = _enum_arr,                        \
260d108dac0SJason Gunthorpe 			  .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr),       \
261d108dac0SJason Gunthorpe 			  __VA_ARGS__ },                                       \
262d108dac0SJason Gunthorpe 	})
263d108dac0SJason Gunthorpe 
264d108dac0SJason Gunthorpe /*
2656c61d2a5SJason Gunthorpe  * This spec is used in order to pass information to the hardware driver in a
2666c61d2a5SJason Gunthorpe  * legacy way. Every verb that could get driver specific data should get this
2676c61d2a5SJason Gunthorpe  * spec.
2686c61d2a5SJason Gunthorpe  */
2696c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW()                                                      \
2709a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN,                                 \
271*540cd692SJason Gunthorpe 			   UVERBS_ATTR_MIN_SIZE(0),			       \
272*540cd692SJason Gunthorpe 			   UA_OPTIONAL),				       \
2739a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT,                               \
274*540cd692SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(0),			       \
275*540cd692SJason Gunthorpe 			    UA_OPTIONAL)
2766c61d2a5SJason Gunthorpe 
2776c61d2a5SJason Gunthorpe /*
278d108dac0SJason Gunthorpe  * =======================================
279d108dac0SJason Gunthorpe  *	Declaration helpers
280d108dac0SJason Gunthorpe  * =======================================
281d108dac0SJason Gunthorpe  */
2826c61d2a5SJason Gunthorpe 
28309e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...)                                 \
2846c61d2a5SJason Gunthorpe 	static const struct uverbs_object_def *const _name##_ptr[] = {         \
2856c61d2a5SJason Gunthorpe 		__VA_ARGS__,                                                   \
2866c61d2a5SJason Gunthorpe 	};                                                                     \
2876c61d2a5SJason Gunthorpe 	static const struct uverbs_object_tree_def _name = {                   \
2886c61d2a5SJason Gunthorpe 		.num_objects = ARRAY_SIZE(_name##_ptr),                        \
2896c61d2a5SJason Gunthorpe 		.objects = &_name##_ptr,                                       \
2906c61d2a5SJason Gunthorpe 	}
29109e3ebf8SMatan Barak 
292fac9658cSMatan Barak /* =================================================
293fac9658cSMatan Barak  *              Parsing infrastructure
294fac9658cSMatan Barak  * =================================================
295fac9658cSMatan Barak  */
296fac9658cSMatan Barak 
297fac9658cSMatan Barak struct uverbs_ptr_attr {
2988762d149SMatan Barak 	/*
2998762d149SMatan Barak 	 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
3008762d149SMatan Barak 	 * used.
3018762d149SMatan Barak 	 */
3028762d149SMatan Barak 	union {
3038762d149SMatan Barak 		void *ptr;
304fac9658cSMatan Barak 		u64 data;
3058762d149SMatan Barak 	};
306fac9658cSMatan Barak 	u16		len;
307fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
308fac9658cSMatan Barak 	u16		flags;
309494c5580SMatan Barak 	u8		enum_id;
310fac9658cSMatan Barak };
311fac9658cSMatan Barak 
312f43dbebfSMatan Barak struct uverbs_obj_attr {
313f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
314f43dbebfSMatan Barak };
315f43dbebfSMatan Barak 
316f43dbebfSMatan Barak struct uverbs_attr {
317fac9658cSMatan Barak 	/*
318fac9658cSMatan Barak 	 * pointer to the user-space given attribute, in order to write the
319fac9658cSMatan Barak 	 * new uobject's id or update flags.
320fac9658cSMatan Barak 	 */
321fac9658cSMatan Barak 	struct ib_uverbs_attr __user	*uattr;
322fac9658cSMatan Barak 	union {
323fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
324f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
325f43dbebfSMatan Barak 	};
326fac9658cSMatan Barak };
327f43dbebfSMatan Barak 
328f43dbebfSMatan Barak struct uverbs_attr_bundle_hash {
329f43dbebfSMatan Barak 	/* if bit i is set, it means attrs[i] contains valid information */
330f43dbebfSMatan Barak 	unsigned long *valid_bitmap;
331f43dbebfSMatan Barak 	size_t num_attrs;
332f43dbebfSMatan Barak 	/*
333f43dbebfSMatan Barak 	 * arrays of attributes, each element corresponds to the specification
334f43dbebfSMatan Barak 	 * of the attribute in the same index.
335f43dbebfSMatan Barak 	 */
336f43dbebfSMatan Barak 	struct uverbs_attr *attrs;
337f43dbebfSMatan Barak };
338f43dbebfSMatan Barak 
339f43dbebfSMatan Barak struct uverbs_attr_bundle {
340f43dbebfSMatan Barak 	size_t				num_buckets;
341f43dbebfSMatan Barak 	struct uverbs_attr_bundle_hash  hash[];
342f43dbebfSMatan Barak };
343f43dbebfSMatan Barak 
344f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
345f43dbebfSMatan Barak 						unsigned int idx)
346f43dbebfSMatan Barak {
347f43dbebfSMatan Barak 	return test_bit(idx, attrs_hash->valid_bitmap);
348f43dbebfSMatan Barak }
349f43dbebfSMatan Barak 
35035410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
35135410306SMatan Barak 					unsigned int idx)
35235410306SMatan Barak {
35335410306SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
35435410306SMatan Barak 
35535410306SMatan Barak 	if (attrs_bundle->num_buckets <= idx_bucket)
35635410306SMatan Barak 		return false;
35735410306SMatan Barak 
35835410306SMatan Barak 	return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
35935410306SMatan Barak 					    idx & ~UVERBS_ID_NS_MASK);
36035410306SMatan Barak }
36135410306SMatan Barak 
36241b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
36341b2a71fSMatan Barak 
364d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
365d70724f1SMatan Barak 							u16 idx)
366d70724f1SMatan Barak {
367d70724f1SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
368d70724f1SMatan Barak 
369d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
370d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
371d70724f1SMatan Barak 
372d70724f1SMatan Barak 	return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
373d70724f1SMatan Barak }
374d70724f1SMatan Barak 
375494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
376494c5580SMatan Barak 					  u16 idx)
377494c5580SMatan Barak {
378494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
379494c5580SMatan Barak 
380494c5580SMatan Barak 	if (IS_ERR(attr))
381494c5580SMatan Barak 		return PTR_ERR(attr);
382494c5580SMatan Barak 
383494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
384494c5580SMatan Barak }
385494c5580SMatan Barak 
386be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
387be934ccaSAriel Levkovich 					u16 idx)
388be934ccaSAriel Levkovich {
389f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
390be934ccaSAriel Levkovich 
391f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
392f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
393f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
394be934ccaSAriel Levkovich 
395f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
396be934ccaSAriel Levkovich }
397be934ccaSAriel Levkovich 
3983efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
3993efa3812SMatan Barak 							 u16 idx)
4003efa3812SMatan Barak {
4013efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
4023efa3812SMatan Barak 
4033efa3812SMatan Barak 	if (IS_ERR(attr))
4043efa3812SMatan Barak 		return ERR_CAST(attr);
4053efa3812SMatan Barak 
4063efa3812SMatan Barak 	return attr->obj_attr.uobject;
4073efa3812SMatan Barak }
4083efa3812SMatan Barak 
4098762d149SMatan Barak static inline int
4108762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
4118762d149SMatan Barak {
4128762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
4138762d149SMatan Barak 
4148762d149SMatan Barak 	if (IS_ERR(attr))
4158762d149SMatan Barak 		return PTR_ERR(attr);
4168762d149SMatan Barak 
4178762d149SMatan Barak 	return attr->ptr_attr.len;
4188762d149SMatan Barak }
4198762d149SMatan Barak 
420d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
42189d9e8d3SMatan Barak 				 size_t idx, const void *from, size_t size)
422d70724f1SMatan Barak {
423d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
424d70724f1SMatan Barak 	u16 flags;
42589d9e8d3SMatan Barak 	size_t min_size;
426d70724f1SMatan Barak 
427d70724f1SMatan Barak 	if (IS_ERR(attr))
428d70724f1SMatan Barak 		return PTR_ERR(attr);
429d70724f1SMatan Barak 
43089d9e8d3SMatan Barak 	min_size = min_t(size_t, attr->ptr_attr.len, size);
4312f36028cSJason Gunthorpe 	if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
43289d9e8d3SMatan Barak 		return -EFAULT;
43389d9e8d3SMatan Barak 
434d70724f1SMatan Barak 	flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
43589d9e8d3SMatan Barak 	if (put_user(flags, &attr->uattr->flags))
43689d9e8d3SMatan Barak 		return -EFAULT;
43789d9e8d3SMatan Barak 
43889d9e8d3SMatan Barak 	return 0;
439d70724f1SMatan Barak }
440d70724f1SMatan Barak 
44189d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
44289d9e8d3SMatan Barak {
44389d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
44489d9e8d3SMatan Barak }
44589d9e8d3SMatan Barak 
4468762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
4478762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
4488762d149SMatan Barak {
4498762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
4508762d149SMatan Barak 
4518762d149SMatan Barak 	if (IS_ERR(attr))
4528762d149SMatan Barak 		return (void *)attr;
4538762d149SMatan Barak 
4548762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
4558762d149SMatan Barak 						 attr->ptr_attr.ptr;
4568762d149SMatan Barak }
4578762d149SMatan Barak 
45889d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
459d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
46089d9e8d3SMatan Barak 				    size_t idx,
46189d9e8d3SMatan Barak 				    size_t size)
462d70724f1SMatan Barak {
463d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
464d70724f1SMatan Barak 
465d70724f1SMatan Barak 	if (IS_ERR(attr))
466d70724f1SMatan Barak 		return PTR_ERR(attr);
467d70724f1SMatan Barak 
46889d9e8d3SMatan Barak 	/*
46989d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
470c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
471c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
47289d9e8d3SMatan Barak 	 */
47389d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
47489d9e8d3SMatan Barak 		return -EINVAL;
47589d9e8d3SMatan Barak 
47689d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
477d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
4782f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
4792f36028cSJason Gunthorpe 				attr->ptr_attr.len))
480d70724f1SMatan Barak 		return -EFAULT;
481d70724f1SMatan Barak 
482d70724f1SMatan Barak 	return 0;
483d70724f1SMatan Barak }
484d70724f1SMatan Barak 
485c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
486c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
487c66db311SMatan Barak 					    size_t idx,
488c66db311SMatan Barak 					    size_t size)
489c66db311SMatan Barak {
490c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
491c66db311SMatan Barak 	size_t min_size;
492c66db311SMatan Barak 
493c66db311SMatan Barak 	if (IS_ERR(attr))
494c66db311SMatan Barak 		return PTR_ERR(attr);
495c66db311SMatan Barak 
496c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
497c66db311SMatan Barak 
498c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
499c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
500c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
501c66db311SMatan Barak 				min_size))
502c66db311SMatan Barak 		return -EFAULT;
503c66db311SMatan Barak 
504c66db311SMatan Barak 	if (size > min_size)
505c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
506c66db311SMatan Barak 
507c66db311SMatan Barak 	return 0;
508c66db311SMatan Barak }
509c66db311SMatan Barak 
510d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
51189d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
512d70724f1SMatan Barak 
513c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
514c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
515c66db311SMatan Barak 
516118620d3SMatan Barak /* =================================================
517118620d3SMatan Barak  *	 Definitions -> Specs infrastructure
518118620d3SMatan Barak  * =================================================
519118620d3SMatan Barak  */
520a0aa309cSMatan Barak 
521118620d3SMatan Barak /*
522118620d3SMatan Barak  * uverbs_alloc_spec_tree - Merges different common and driver specific feature
523118620d3SMatan Barak  *	into one parsing tree that every uverbs command will be parsed upon.
524118620d3SMatan Barak  *
525118620d3SMatan Barak  * @num_trees: Number of trees in the array @trees.
526118620d3SMatan Barak  * @trees: Array of pointers to tree root definitions to merge. Each such tree
527118620d3SMatan Barak  *	   possibly contains objects, methods and attributes definitions.
528118620d3SMatan Barak  *
529118620d3SMatan Barak  * Returns:
530118620d3SMatan Barak  *	uverbs_root_spec *: The root of the merged parsing tree.
531118620d3SMatan Barak  *	On error, we return an error code. Error is checked via IS_ERR.
532118620d3SMatan Barak  *
533118620d3SMatan Barak  * The following merges could take place:
534118620d3SMatan Barak  * a. Two trees representing the same method with different handler
535118620d3SMatan Barak  *	-> We take the handler of the tree that its handler != NULL
536118620d3SMatan Barak  *	   and its index in the trees array is greater. The incentive for that
537118620d3SMatan Barak  *	   is that developers are expected to first merge common trees and then
538118620d3SMatan Barak  *	   merge trees that gives specialized the behaviour.
539118620d3SMatan Barak  * b. Two trees representing the same object with different
540118620d3SMatan Barak  *    type_attrs (struct uverbs_obj_type):
541118620d3SMatan Barak  *	-> We take the type_attrs of the tree that its type_attr != NULL
542118620d3SMatan Barak  *	   and its index in the trees array is greater. This could be used
543118620d3SMatan Barak  *	   in order to override the free function, allocation size, etc.
544118620d3SMatan Barak  * c. Two trees representing the same method attribute (same id but possibly
545118620d3SMatan Barak  *    different attributes):
546118620d3SMatan Barak  *	-> ERROR (-ENOENT), we believe that's not the programmer's intent.
547118620d3SMatan Barak  *
548118620d3SMatan Barak  * An object without any methods is considered invalid and will abort the
549118620d3SMatan Barak  * function with -ENOENT error.
550118620d3SMatan Barak  */
55152427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
552118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
553118620d3SMatan Barak 						const struct uverbs_object_tree_def **trees);
554118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root);
55552427112SMatan Barak #else
55652427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
55752427112SMatan Barak 							      const struct uverbs_object_tree_def **trees)
55852427112SMatan Barak {
55952427112SMatan Barak 	return NULL;
56052427112SMatan Barak }
56152427112SMatan Barak 
56252427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
56352427112SMatan Barak {
56452427112SMatan Barak }
56552427112SMatan Barak #endif
566118620d3SMatan Barak 
567118620d3SMatan Barak #endif
568