xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 3541030650c0ddb5d52163082fee427b2a453799)
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>
37*35410306SMatan Barak #include <linux/uaccess.h>
38*35410306SMatan Barak #include <rdma/rdma_user_ioctl.h>
39a0aa309cSMatan Barak 
40a0aa309cSMatan Barak /*
41a0aa309cSMatan Barak  * =======================================
42a0aa309cSMatan Barak  *	Verbs action specifications
43a0aa309cSMatan Barak  * =======================================
44a0aa309cSMatan Barak  */
45a0aa309cSMatan Barak 
46f43dbebfSMatan Barak enum uverbs_attr_type {
47f43dbebfSMatan Barak 	UVERBS_ATTR_TYPE_NA,
48fac9658cSMatan Barak 	UVERBS_ATTR_TYPE_PTR_IN,
49fac9658cSMatan Barak 	UVERBS_ATTR_TYPE_PTR_OUT,
50f43dbebfSMatan Barak 	UVERBS_ATTR_TYPE_IDR,
51f43dbebfSMatan Barak 	UVERBS_ATTR_TYPE_FD,
52f43dbebfSMatan Barak };
53f43dbebfSMatan Barak 
54a0aa309cSMatan Barak enum uverbs_obj_access {
55a0aa309cSMatan Barak 	UVERBS_ACCESS_READ,
56a0aa309cSMatan Barak 	UVERBS_ACCESS_WRITE,
57a0aa309cSMatan Barak 	UVERBS_ACCESS_NEW,
58a0aa309cSMatan Barak 	UVERBS_ACCESS_DESTROY
59a0aa309cSMatan Barak };
60a0aa309cSMatan Barak 
61fac9658cSMatan Barak enum {
62fac9658cSMatan Barak 	UVERBS_ATTR_SPEC_F_MANDATORY	= 1U << 0,
63fac9658cSMatan Barak 	/* Support extending attributes by length */
64fac9658cSMatan Barak 	UVERBS_ATTR_SPEC_F_MIN_SZ	= 1U << 1,
65fac9658cSMatan Barak };
66fac9658cSMatan Barak 
67f43dbebfSMatan Barak struct uverbs_attr_spec {
68f43dbebfSMatan Barak 	enum uverbs_attr_type		type;
69fac9658cSMatan Barak 	union {
70fac9658cSMatan Barak 		u16				len;
71f43dbebfSMatan Barak 		struct {
72f43dbebfSMatan Barak 			/*
73f43dbebfSMatan Barak 			 * higher bits mean the namespace and lower bits mean
74f43dbebfSMatan Barak 			 * the type id within the namespace.
75f43dbebfSMatan Barak 			 */
76f43dbebfSMatan Barak 			u16			obj_type;
77f43dbebfSMatan Barak 			u8			access;
78f43dbebfSMatan Barak 		} obj;
79f43dbebfSMatan Barak 	};
80fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
81fac9658cSMatan Barak 	u8				flags;
82fac9658cSMatan Barak };
83f43dbebfSMatan Barak 
84f43dbebfSMatan Barak struct uverbs_attr_spec_hash {
85f43dbebfSMatan Barak 	size_t				num_attrs;
86fac9658cSMatan Barak 	unsigned long			*mandatory_attrs_bitmask;
87f43dbebfSMatan Barak 	struct uverbs_attr_spec		attrs[0];
88f43dbebfSMatan Barak };
89f43dbebfSMatan Barak 
90fac9658cSMatan Barak struct uverbs_attr_bundle;
91fac9658cSMatan Barak struct ib_uverbs_file;
92fac9658cSMatan Barak 
93fac9658cSMatan Barak enum {
94fac9658cSMatan Barak 	/*
95fac9658cSMatan Barak 	 * Action marked with this flag creates a context (or root for all
96fac9658cSMatan Barak 	 * objects).
97fac9658cSMatan Barak 	 */
98fac9658cSMatan Barak 	UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
99fac9658cSMatan Barak };
100fac9658cSMatan Barak 
101fac9658cSMatan Barak struct uverbs_method_spec {
102fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
103fac9658cSMatan Barak 	u32						flags;
104fac9658cSMatan Barak 	size_t						num_buckets;
105fac9658cSMatan Barak 	size_t						num_child_attrs;
106fac9658cSMatan Barak 	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
107fac9658cSMatan Barak 		       struct uverbs_attr_bundle *ctx);
108fac9658cSMatan Barak 	struct uverbs_attr_spec_hash		*attr_buckets[0];
109fac9658cSMatan Barak };
110fac9658cSMatan Barak 
111fac9658cSMatan Barak struct uverbs_method_spec_hash {
112fac9658cSMatan Barak 	size_t					num_methods;
113fac9658cSMatan Barak 	struct uverbs_method_spec		*methods[0];
114fac9658cSMatan Barak };
115fac9658cSMatan Barak 
116fac9658cSMatan Barak struct uverbs_object_spec {
117fac9658cSMatan Barak 	const struct uverbs_obj_type		*type_attrs;
118fac9658cSMatan Barak 	size_t					num_buckets;
119fac9658cSMatan Barak 	struct uverbs_method_spec_hash		*method_buckets[0];
120fac9658cSMatan Barak };
121fac9658cSMatan Barak 
122fac9658cSMatan Barak struct uverbs_object_spec_hash {
123fac9658cSMatan Barak 	size_t					num_objects;
124fac9658cSMatan Barak 	struct uverbs_object_spec		*objects[0];
125fac9658cSMatan Barak };
126fac9658cSMatan Barak 
127fac9658cSMatan Barak struct uverbs_root_spec {
128fac9658cSMatan Barak 	size_t					num_buckets;
129fac9658cSMatan Barak 	struct uverbs_object_spec_hash		*object_buckets[0];
130fac9658cSMatan Barak };
131fac9658cSMatan Barak 
1325009010fSMatan Barak /*
1335009010fSMatan Barak  * =======================================
1345009010fSMatan Barak  *	Verbs definitions
1355009010fSMatan Barak  * =======================================
1365009010fSMatan Barak  */
1375009010fSMatan Barak 
13809e3ebf8SMatan Barak struct uverbs_attr_def {
13909e3ebf8SMatan Barak 	u16                           id;
14009e3ebf8SMatan Barak 	struct uverbs_attr_spec       attr;
14109e3ebf8SMatan Barak };
14209e3ebf8SMatan Barak 
14309e3ebf8SMatan Barak struct uverbs_method_def {
14409e3ebf8SMatan Barak 	u16                                  id;
14509e3ebf8SMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
14609e3ebf8SMatan Barak 	u32				     flags;
14709e3ebf8SMatan Barak 	size_t				     num_attrs;
14809e3ebf8SMatan Barak 	const struct uverbs_attr_def * const (*attrs)[];
14909e3ebf8SMatan Barak 	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
15009e3ebf8SMatan Barak 		       struct uverbs_attr_bundle *ctx);
15109e3ebf8SMatan Barak };
15209e3ebf8SMatan Barak 
1535009010fSMatan Barak struct uverbs_object_def {
15409e3ebf8SMatan Barak 	u16					 id;
1555009010fSMatan Barak 	const struct uverbs_obj_type	        *type_attrs;
15609e3ebf8SMatan Barak 	size_t				         num_methods;
15709e3ebf8SMatan Barak 	const struct uverbs_method_def * const (*methods)[];
15809e3ebf8SMatan Barak };
15909e3ebf8SMatan Barak 
16009e3ebf8SMatan Barak struct uverbs_object_tree_def {
16109e3ebf8SMatan Barak 	size_t					 num_objects;
16209e3ebf8SMatan Barak 	const struct uverbs_object_def * const (*objects)[];
1635009010fSMatan Barak };
1645009010fSMatan Barak 
165*35410306SMatan Barak #define UA_FLAGS(_flags)  .flags = _flags
166*35410306SMatan Barak #define __UVERBS_ATTR0(_id, _len, _type, ...)                           \
167*35410306SMatan Barak 	((const struct uverbs_attr_def)				  \
168*35410306SMatan Barak 	 {.id = _id, .attr = {.type = _type, {.len = _len}, .flags = 0, } })
169*35410306SMatan Barak #define __UVERBS_ATTR1(_id, _len, _type, _flags)                        \
170*35410306SMatan Barak 	((const struct uverbs_attr_def)				  \
171*35410306SMatan Barak 	 {.id = _id, .attr = {.type = _type, {.len = _len}, _flags, } })
172*35410306SMatan Barak #define __UVERBS_ATTR(_id, _len, _type, _flags, _n, ...)		\
173*35410306SMatan Barak 	__UVERBS_ATTR##_n(_id, _len, _type, _flags)
174*35410306SMatan Barak /*
175*35410306SMatan Barak  * In new compiler, UVERBS_ATTR could be simplified by declaring it as
176*35410306SMatan Barak  * [_id] = {.type = _type, .len = _len, ##__VA_ARGS__}
177*35410306SMatan Barak  * But since we support older compilers too, we need the more complex code.
178*35410306SMatan Barak  */
179*35410306SMatan Barak #define UVERBS_ATTR(_id, _len, _type, ...)				\
180*35410306SMatan Barak 	__UVERBS_ATTR(_id, _len, _type, ##__VA_ARGS__, 1, 0)
181*35410306SMatan Barak #define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...)				\
182*35410306SMatan Barak 	UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_IN, ##__VA_ARGS__)
183*35410306SMatan Barak /* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */
184*35410306SMatan Barak #define UVERBS_ATTR_PTR_IN(_id, _type, ...)				\
185*35410306SMatan Barak 	UVERBS_ATTR_PTR_IN_SZ(_id, sizeof(_type), ##__VA_ARGS__)
186*35410306SMatan Barak #define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...)				\
187*35410306SMatan Barak 	UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_OUT, ##__VA_ARGS__)
188*35410306SMatan Barak #define UVERBS_ATTR_PTR_OUT(_id, _type, ...)				\
189*35410306SMatan Barak 	UVERBS_ATTR_PTR_OUT_SZ(_id, sizeof(_type), ##__VA_ARGS__)
190*35410306SMatan Barak 
191*35410306SMatan Barak /*
192*35410306SMatan Barak  * In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring
193*35410306SMatan Barak  * it as
194*35410306SMatan Barak  * {.id = _id,								\
195*35410306SMatan Barak  *  .attr {.type = __obj_class,						\
196*35410306SMatan Barak  *         .obj = {.obj_type = _idr_type,				\
197*35410306SMatan Barak  *                       .access = _access                              \
198*35410306SMatan Barak  *                }, ##__VA_ARGS__ } }
199*35410306SMatan Barak  * But since we support older compilers too, we need the more complex code.
200*35410306SMatan Barak  */
201*35410306SMatan Barak #define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\
202*35410306SMatan Barak 	((const struct uverbs_attr_def)					\
203*35410306SMatan Barak 	{.id = _id,							\
204*35410306SMatan Barak 	 .attr = {.type = _obj_class,					\
205*35410306SMatan Barak 		  {.obj = {.obj_type = _obj_type, .access = _access } },\
206*35410306SMatan Barak 		  .flags = 0} })
207*35410306SMatan Barak #define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\
208*35410306SMatan Barak 	((const struct uverbs_attr_def)					\
209*35410306SMatan Barak 	{.id = _id,							\
210*35410306SMatan Barak 	.attr = {.type = _obj_class,					\
211*35410306SMatan Barak 		 {.obj = {.obj_type = _obj_type, .access = _access} },	\
212*35410306SMatan Barak 		  _flags} })
213*35410306SMatan Barak #define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \
214*35410306SMatan Barak 			   _n, ...)					\
215*35410306SMatan Barak 	___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags)
216*35410306SMatan Barak #define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...)	\
217*35410306SMatan Barak 	___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access,		\
218*35410306SMatan Barak 			   ##__VA_ARGS__, 1, 0)
219*35410306SMatan Barak #define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...)			 \
220*35410306SMatan Barak 	__UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\
221*35410306SMatan Barak 			  ##__VA_ARGS__)
222*35410306SMatan Barak #define UVERBS_ATTR_FD(_id, _fd_type, _access, ...)			\
223*35410306SMatan Barak 	__UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type,		\
224*35410306SMatan Barak 			  (_access) + BUILD_BUG_ON_ZERO(		\
225*35410306SMatan Barak 				(_access) != UVERBS_ACCESS_NEW &&	\
226*35410306SMatan Barak 				(_access) != UVERBS_ACCESS_READ),	\
227*35410306SMatan Barak 			  ##__VA_ARGS__)
228*35410306SMatan Barak #define DECLARE_UVERBS_ATTR_SPEC(_name, ...)				\
229*35410306SMatan Barak 	const struct uverbs_attr_def _name = __VA_ARGS__
230*35410306SMatan Barak 
231*35410306SMatan Barak #define _UVERBS_METHOD_ATTRS_SZ(...)					\
232*35410306SMatan Barak 	(sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\
233*35410306SMatan Barak 	 sizeof(const struct uverbs_attr_def *))
234*35410306SMatan Barak #define _UVERBS_METHOD(_id, _handler, _flags, ...)			\
235*35410306SMatan Barak 	((const struct uverbs_method_def) {				\
236*35410306SMatan Barak 	 .id = _id,							\
237*35410306SMatan Barak 	 .flags = _flags,						\
238*35410306SMatan Barak 	 .handler = _handler,						\
239*35410306SMatan Barak 	 .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__),		\
240*35410306SMatan Barak 	 .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} })
241*35410306SMatan Barak #define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...)		\
242*35410306SMatan Barak 	const struct uverbs_method_def _name =				\
243*35410306SMatan Barak 		_UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__)
244*35410306SMatan Barak #define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...)	\
245*35410306SMatan Barak 	const struct uverbs_method_def _name =				\
246*35410306SMatan Barak 		_UVERBS_METHOD(_id, _handler,				\
247*35410306SMatan Barak 			       UVERBS_ACTION_FLAG_CREATE_ROOT,		\
248*35410306SMatan Barak 			       ##__VA_ARGS__)
249*35410306SMatan Barak #define _UVERBS_OBJECT_METHODS_SZ(...)					\
250*35410306SMatan Barak 	(sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \
251*35410306SMatan Barak 	 sizeof(const struct uverbs_method_def *))
2525009010fSMatan Barak #define _UVERBS_OBJECT(_id, _type_attrs, ...)				\
2535009010fSMatan Barak 	((const struct uverbs_object_def) {				\
25409e3ebf8SMatan Barak 	 .id = _id,							\
255*35410306SMatan Barak 	 .type_attrs = _type_attrs,					\
256*35410306SMatan Barak 	 .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__),		\
257*35410306SMatan Barak 	 .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} })
2585009010fSMatan Barak #define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...)		\
2595009010fSMatan Barak 	const struct uverbs_object_def _name =				\
2605009010fSMatan Barak 		_UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
26109e3ebf8SMatan Barak #define _UVERBS_TREE_OBJECTS_SZ(...)					\
26209e3ebf8SMatan Barak 	(sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
26309e3ebf8SMatan Barak 	 sizeof(const struct uverbs_object_def *))
26409e3ebf8SMatan Barak #define _UVERBS_OBJECT_TREE(...)					\
26509e3ebf8SMatan Barak 	((const struct uverbs_object_tree_def) {			\
26609e3ebf8SMatan Barak 	 .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__),		\
26709e3ebf8SMatan Barak 	 .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
26809e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...)				\
26909e3ebf8SMatan Barak 	const struct uverbs_object_tree_def _name =			\
27009e3ebf8SMatan Barak 		_UVERBS_OBJECT_TREE(__VA_ARGS__)
27109e3ebf8SMatan Barak 
272fac9658cSMatan Barak /* =================================================
273fac9658cSMatan Barak  *              Parsing infrastructure
274fac9658cSMatan Barak  * =================================================
275fac9658cSMatan Barak  */
276fac9658cSMatan Barak 
277fac9658cSMatan Barak struct uverbs_ptr_attr {
278fac9658cSMatan Barak 	union {
279fac9658cSMatan Barak 		u64		data;
280fac9658cSMatan Barak 		void	__user *ptr;
281fac9658cSMatan Barak 	};
282fac9658cSMatan Barak 	u16		len;
283fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
284fac9658cSMatan Barak 	u16		flags;
285fac9658cSMatan Barak };
286fac9658cSMatan Barak 
287f43dbebfSMatan Barak struct uverbs_obj_attr {
288fac9658cSMatan Barak 	/* pointer to the kernel descriptor -> type, access, etc */
289fac9658cSMatan Barak 	const struct uverbs_obj_type	*type;
290f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
291fac9658cSMatan Barak 	/* fd or id in idr of this object */
292fac9658cSMatan Barak 	int				id;
293f43dbebfSMatan Barak };
294f43dbebfSMatan Barak 
295f43dbebfSMatan Barak struct uverbs_attr {
296fac9658cSMatan Barak 	/*
297fac9658cSMatan Barak 	 * pointer to the user-space given attribute, in order to write the
298fac9658cSMatan Barak 	 * new uobject's id or update flags.
299fac9658cSMatan Barak 	 */
300fac9658cSMatan Barak 	struct ib_uverbs_attr __user	*uattr;
301fac9658cSMatan Barak 	union {
302fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
303f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
304f43dbebfSMatan Barak 	};
305fac9658cSMatan Barak };
306f43dbebfSMatan Barak 
307f43dbebfSMatan Barak struct uverbs_attr_bundle_hash {
308f43dbebfSMatan Barak 	/* if bit i is set, it means attrs[i] contains valid information */
309f43dbebfSMatan Barak 	unsigned long *valid_bitmap;
310f43dbebfSMatan Barak 	size_t num_attrs;
311f43dbebfSMatan Barak 	/*
312f43dbebfSMatan Barak 	 * arrays of attributes, each element corresponds to the specification
313f43dbebfSMatan Barak 	 * of the attribute in the same index.
314f43dbebfSMatan Barak 	 */
315f43dbebfSMatan Barak 	struct uverbs_attr *attrs;
316f43dbebfSMatan Barak };
317f43dbebfSMatan Barak 
318f43dbebfSMatan Barak struct uverbs_attr_bundle {
319f43dbebfSMatan Barak 	size_t				num_buckets;
320f43dbebfSMatan Barak 	struct uverbs_attr_bundle_hash  hash[];
321f43dbebfSMatan Barak };
322f43dbebfSMatan Barak 
323f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
324f43dbebfSMatan Barak 						unsigned int idx)
325f43dbebfSMatan Barak {
326f43dbebfSMatan Barak 	return test_bit(idx, attrs_hash->valid_bitmap);
327f43dbebfSMatan Barak }
328f43dbebfSMatan Barak 
329*35410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
330*35410306SMatan Barak 					unsigned int idx)
331*35410306SMatan Barak {
332*35410306SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
333*35410306SMatan Barak 
334*35410306SMatan Barak 	if (attrs_bundle->num_buckets <= idx_bucket)
335*35410306SMatan Barak 		return false;
336*35410306SMatan Barak 
337*35410306SMatan Barak 	return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
338*35410306SMatan Barak 					    idx & ~UVERBS_ID_NS_MASK);
339*35410306SMatan Barak }
340*35410306SMatan Barak 
341118620d3SMatan Barak /* =================================================
342118620d3SMatan Barak  *	 Definitions -> Specs infrastructure
343118620d3SMatan Barak  * =================================================
344118620d3SMatan Barak  */
345a0aa309cSMatan Barak 
346118620d3SMatan Barak /*
347118620d3SMatan Barak  * uverbs_alloc_spec_tree - Merges different common and driver specific feature
348118620d3SMatan Barak  *	into one parsing tree that every uverbs command will be parsed upon.
349118620d3SMatan Barak  *
350118620d3SMatan Barak  * @num_trees: Number of trees in the array @trees.
351118620d3SMatan Barak  * @trees: Array of pointers to tree root definitions to merge. Each such tree
352118620d3SMatan Barak  *	   possibly contains objects, methods and attributes definitions.
353118620d3SMatan Barak  *
354118620d3SMatan Barak  * Returns:
355118620d3SMatan Barak  *	uverbs_root_spec *: The root of the merged parsing tree.
356118620d3SMatan Barak  *	On error, we return an error code. Error is checked via IS_ERR.
357118620d3SMatan Barak  *
358118620d3SMatan Barak  * The following merges could take place:
359118620d3SMatan Barak  * a. Two trees representing the same method with different handler
360118620d3SMatan Barak  *	-> We take the handler of the tree that its handler != NULL
361118620d3SMatan Barak  *	   and its index in the trees array is greater. The incentive for that
362118620d3SMatan Barak  *	   is that developers are expected to first merge common trees and then
363118620d3SMatan Barak  *	   merge trees that gives specialized the behaviour.
364118620d3SMatan Barak  * b. Two trees representing the same object with different
365118620d3SMatan Barak  *    type_attrs (struct uverbs_obj_type):
366118620d3SMatan Barak  *	-> We take the type_attrs of the tree that its type_attr != NULL
367118620d3SMatan Barak  *	   and its index in the trees array is greater. This could be used
368118620d3SMatan Barak  *	   in order to override the free function, allocation size, etc.
369118620d3SMatan Barak  * c. Two trees representing the same method attribute (same id but possibly
370118620d3SMatan Barak  *    different attributes):
371118620d3SMatan Barak  *	-> ERROR (-ENOENT), we believe that's not the programmer's intent.
372118620d3SMatan Barak  *
373118620d3SMatan Barak  * An object without any methods is considered invalid and will abort the
374118620d3SMatan Barak  * function with -ENOENT error.
375118620d3SMatan Barak  */
376118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
377118620d3SMatan Barak 						const struct uverbs_object_tree_def **trees);
378118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root);
379118620d3SMatan Barak 
380118620d3SMatan Barak #endif
381