xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 9ed3e5f447723a41de6bcc29633e9f7e6246d2f7)
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;
131e83f0ecdSJason Gunthorpe 	int (*handler)(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 /*
158*9ed3e5f4SJason Gunthorpe  * Information about the API is loaded into a radix tree. For IOCTL we start
159*9ed3e5f4SJason Gunthorpe  * with a tuple of:
160*9ed3e5f4SJason Gunthorpe  *  object_id, attr_id, method_id
161*9ed3e5f4SJason Gunthorpe  *
162*9ed3e5f4SJason Gunthorpe  * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
163*9ed3e5f4SJason Gunthorpe  * on the current kernel support this is compressed into 16 bit key for the
164*9ed3e5f4SJason Gunthorpe  * radix tree. Since this compression is entirely internal to the kernel the
165*9ed3e5f4SJason Gunthorpe  * below limits can be revised if the kernel gains additional data.
166*9ed3e5f4SJason Gunthorpe  *
167*9ed3e5f4SJason Gunthorpe  * With 64 leafs per node this is a 3 level radix tree.
168*9ed3e5f4SJason Gunthorpe  *
169*9ed3e5f4SJason Gunthorpe  * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
170*9ed3e5f4SJason Gunthorpe  * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
171*9ed3e5f4SJason Gunthorpe  */
172*9ed3e5f4SJason Gunthorpe enum uapi_radix_data {
173*9ed3e5f4SJason Gunthorpe 	UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
174*9ed3e5f4SJason Gunthorpe 
175*9ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_BITS = 6,
176*9ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
177*9ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
178*9ed3e5f4SJason Gunthorpe 
179*9ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_BITS = 5,
180*9ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
181*9ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_CORE = 24,
182*9ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) -
183*9ed3e5f4SJason Gunthorpe 					   UVERBS_API_METHOD_KEY_NUM_CORE,
184*9ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_MASK = GENMASK(
185*9ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
186*9ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_SHIFT),
187*9ed3e5f4SJason Gunthorpe 
188*9ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_BITS = 5,
189*9ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_SHIFT =
190*9ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
191*9ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_CORE = 24,
192*9ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_DRIVER =
193*9ed3e5f4SJason Gunthorpe 		(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
194*9ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
195*9ed3e5f4SJason Gunthorpe 
196*9ed3e5f4SJason Gunthorpe 	/* This id guaranteed to not exist in the radix tree */
197*9ed3e5f4SJason Gunthorpe 	UVERBS_API_KEY_ERR = 0xFFFFFFFF,
198*9ed3e5f4SJason Gunthorpe };
199*9ed3e5f4SJason Gunthorpe 
200*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id)
201*9ed3e5f4SJason Gunthorpe {
202*9ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
203*9ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
204*9ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
205*9ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
206*9ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
207*9ed3e5f4SJason Gunthorpe 	} else {
208*9ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
209*9ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
210*9ed3e5f4SJason Gunthorpe 	}
211*9ed3e5f4SJason Gunthorpe 
212*9ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_OBJ_KEY_SHIFT;
213*9ed3e5f4SJason Gunthorpe }
214*9ed3e5f4SJason Gunthorpe 
215*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key)
216*9ed3e5f4SJason Gunthorpe {
217*9ed3e5f4SJason Gunthorpe 	return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
218*9ed3e5f4SJason Gunthorpe }
219*9ed3e5f4SJason Gunthorpe 
220*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
221*9ed3e5f4SJason Gunthorpe {
222*9ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
223*9ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
224*9ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
225*9ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
226*9ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
227*9ed3e5f4SJason Gunthorpe 	} else {
228*9ed3e5f4SJason Gunthorpe 		id++;
229*9ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
230*9ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
231*9ed3e5f4SJason Gunthorpe 	}
232*9ed3e5f4SJason Gunthorpe 
233*9ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_METHOD_KEY_SHIFT;
234*9ed3e5f4SJason Gunthorpe }
235*9ed3e5f4SJason Gunthorpe 
236*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
237*9ed3e5f4SJason Gunthorpe {
238*9ed3e5f4SJason Gunthorpe 	return attr_key &
239*9ed3e5f4SJason Gunthorpe 	       (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
240*9ed3e5f4SJason Gunthorpe }
241*9ed3e5f4SJason Gunthorpe 
242*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
243*9ed3e5f4SJason Gunthorpe {
244*9ed3e5f4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
245*9ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) == 0;
246*9ed3e5f4SJason Gunthorpe }
247*9ed3e5f4SJason Gunthorpe 
248*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
249*9ed3e5f4SJason Gunthorpe {
250*9ed3e5f4SJason Gunthorpe 	/* 0 is the method slot itself */
251*9ed3e5f4SJason Gunthorpe 	return ioctl_method_key + 1;
252*9ed3e5f4SJason Gunthorpe }
253*9ed3e5f4SJason Gunthorpe 
254*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id)
255*9ed3e5f4SJason Gunthorpe {
256*9ed3e5f4SJason Gunthorpe 	/*
257*9ed3e5f4SJason Gunthorpe 	 * The attr is designed to fit in the typical single radix tree node
258*9ed3e5f4SJason Gunthorpe 	 * of 64 entries. Since allmost all methods have driver attributes we
259*9ed3e5f4SJason Gunthorpe 	 * organize things so that the driver and core attributes interleave to
260*9ed3e5f4SJason Gunthorpe 	 * reduce the length of the attributes array in typical cases.
261*9ed3e5f4SJason Gunthorpe 	 */
262*9ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
263*9ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
264*9ed3e5f4SJason Gunthorpe 		id++;
265*9ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
266*9ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
267*9ed3e5f4SJason Gunthorpe 		id = (id << 1) | 0;
268*9ed3e5f4SJason Gunthorpe 	} else {
269*9ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
270*9ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
271*9ed3e5f4SJason Gunthorpe 		id = (id << 1) | 1;
272*9ed3e5f4SJason Gunthorpe 	}
273*9ed3e5f4SJason Gunthorpe 
274*9ed3e5f4SJason Gunthorpe 	return id;
275*9ed3e5f4SJason Gunthorpe }
276*9ed3e5f4SJason Gunthorpe 
277*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
278*9ed3e5f4SJason Gunthorpe {
279*9ed3e5f4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
280*9ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) != 0;
281*9ed3e5f4SJason Gunthorpe }
282*9ed3e5f4SJason Gunthorpe 
283*9ed3e5f4SJason Gunthorpe /*
284*9ed3e5f4SJason Gunthorpe  * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
285*9ed3e5f4SJason Gunthorpe  * basically it undoes the reservation of 0 in the ID numbering. attr_key
286*9ed3e5f4SJason Gunthorpe  * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
287*9ed3e5f4SJason Gunthorpe  * uapi_key_attr().
288*9ed3e5f4SJason Gunthorpe  */
289*9ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
290*9ed3e5f4SJason Gunthorpe {
291*9ed3e5f4SJason Gunthorpe 	return attr_key - 1;
292*9ed3e5f4SJason Gunthorpe }
293*9ed3e5f4SJason Gunthorpe 
294*9ed3e5f4SJason Gunthorpe /*
2955009010fSMatan Barak  * =======================================
2965009010fSMatan Barak  *	Verbs definitions
2975009010fSMatan Barak  * =======================================
2985009010fSMatan Barak  */
2995009010fSMatan Barak 
30009e3ebf8SMatan Barak struct uverbs_attr_def {
30109e3ebf8SMatan Barak 	u16                           id;
30209e3ebf8SMatan Barak 	struct uverbs_attr_spec       attr;
30309e3ebf8SMatan Barak };
30409e3ebf8SMatan Barak 
30509e3ebf8SMatan Barak struct uverbs_method_def {
30609e3ebf8SMatan Barak 	u16                                  id;
30709e3ebf8SMatan Barak 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
30809e3ebf8SMatan Barak 	u32				     flags;
30909e3ebf8SMatan Barak 	size_t				     num_attrs;
31009e3ebf8SMatan Barak 	const struct uverbs_attr_def * const (*attrs)[];
311e83f0ecdSJason Gunthorpe 	int (*handler)(struct ib_uverbs_file *ufile,
31209e3ebf8SMatan Barak 		       struct uverbs_attr_bundle *ctx);
31309e3ebf8SMatan Barak };
31409e3ebf8SMatan Barak 
3155009010fSMatan Barak struct uverbs_object_def {
31609e3ebf8SMatan Barak 	u16					 id;
3175009010fSMatan Barak 	const struct uverbs_obj_type	        *type_attrs;
31809e3ebf8SMatan Barak 	size_t				         num_methods;
31909e3ebf8SMatan Barak 	const struct uverbs_method_def * const (*methods)[];
32009e3ebf8SMatan Barak };
32109e3ebf8SMatan Barak 
32209e3ebf8SMatan Barak struct uverbs_object_tree_def {
32309e3ebf8SMatan Barak 	size_t					 num_objects;
32409e3ebf8SMatan Barak 	const struct uverbs_object_def * const (*objects)[];
3255009010fSMatan Barak };
3265009010fSMatan Barak 
327d108dac0SJason Gunthorpe /*
328d108dac0SJason Gunthorpe  * =======================================
329d108dac0SJason Gunthorpe  *	Attribute Specifications
330d108dac0SJason Gunthorpe  * =======================================
331d108dac0SJason Gunthorpe  */
332c66db311SMatan Barak 
333c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len)			\
334d108dac0SJason Gunthorpe 	.u.ptr.min_len = _min_len, .u.ptr.len = _len
335422e3d37SJason Gunthorpe 
336fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
337fd44e385SYishai Hadas 
338422e3d37SJason Gunthorpe /*
339422e3d37SJason Gunthorpe  * Specifies a uapi structure that cannot be extended. The user must always
340422e3d37SJason Gunthorpe  * supply the whole structure and nothing more. The structure must be declared
341422e3d37SJason Gunthorpe  * in a header under include/uapi/rdma.
342422e3d37SJason Gunthorpe  */
343422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type)					\
344422e3d37SJason Gunthorpe 	.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
345422e3d37SJason Gunthorpe /*
346422e3d37SJason Gunthorpe  * Specifies a uapi structure where the user must provide at least up to
347422e3d37SJason Gunthorpe  * member 'last'.  Anything after last and up until the end of the structure
348422e3d37SJason Gunthorpe  * can be non-zero, anything longer than the end of the structure must be
349422e3d37SJason Gunthorpe  * zero. The structure must be declared in a header under include/uapi/rdma.
350422e3d37SJason Gunthorpe  */
351422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last)                                       \
352422e3d37SJason Gunthorpe 	.zero_trailing = 1,                                                    \
353422e3d37SJason Gunthorpe 	UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)),              \
354422e3d37SJason Gunthorpe 			 sizeof(_type))
355540cd692SJason Gunthorpe /*
356540cd692SJason Gunthorpe  * Specifies at least min_len bytes must be passed in, but the amount can be
357540cd692SJason Gunthorpe  * larger, up to the protocol maximum size. No check for zeroing is done.
358540cd692SJason Gunthorpe  */
359540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
360c66db311SMatan Barak 
361d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */
36283bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
36383bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1
36483bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0
36535410306SMatan Barak 
366d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...)                     \
3679a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
368d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
369d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_IDR,                        \
370d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _idr_type,                         \
371d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
372d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
373d108dac0SJason Gunthorpe 
374d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...)                       \
3759a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
376d108dac0SJason Gunthorpe 		.id = (_attr_id) +                                             \
377d108dac0SJason Gunthorpe 		      BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW &&      \
37835410306SMatan Barak 					(_access) != UVERBS_ACCESS_READ),      \
379d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_FD,                         \
380d108dac0SJason Gunthorpe 			  .u.obj.obj_type = _fd_type,                          \
381d108dac0SJason Gunthorpe 			  .u.obj.access = _access,                             \
382d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
38335410306SMatan Barak 
384d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...)                               \
3859a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
386d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
387d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN,                     \
388d108dac0SJason Gunthorpe 			  _type,                                               \
389d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
390d108dac0SJason Gunthorpe 
391d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...)                              \
3929a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
393d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
394d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT,                    \
395d108dac0SJason Gunthorpe 			  _type,                                               \
396d108dac0SJason Gunthorpe 			  __VA_ARGS__ } })
397d108dac0SJason Gunthorpe 
398d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
399d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...)                          \
4009a119cd5SJason Gunthorpe 	(&(const struct uverbs_attr_def){                                      \
401d108dac0SJason Gunthorpe 		.id = _attr_id,                                                \
402d108dac0SJason Gunthorpe 		.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN,                    \
403d108dac0SJason Gunthorpe 			  .u2.enum_def.ids = _enum_arr,                        \
404d108dac0SJason Gunthorpe 			  .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr),       \
405d108dac0SJason Gunthorpe 			  __VA_ARGS__ },                                       \
406d108dac0SJason Gunthorpe 	})
407d108dac0SJason Gunthorpe 
408d108dac0SJason Gunthorpe /*
409bccd0622SJason Gunthorpe  * An input value that is a bitwise combination of values of _enum_type.
410bccd0622SJason Gunthorpe  * This permits the flag value to be passed as either a u32 or u64, it must
411bccd0622SJason Gunthorpe  * be retrieved via uverbs_get_flag().
412bccd0622SJason Gunthorpe  */
413bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...)                        \
414bccd0622SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(                                                    \
415bccd0622SJason Gunthorpe 		_attr_id,                                                      \
416bccd0622SJason Gunthorpe 		UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO(              \
417bccd0622SJason Gunthorpe 						       !sizeof(_enum_type *)), \
418bccd0622SJason Gunthorpe 				 sizeof(u64)),                                 \
419bccd0622SJason Gunthorpe 		__VA_ARGS__)
420bccd0622SJason Gunthorpe 
421bccd0622SJason Gunthorpe /*
4226c61d2a5SJason Gunthorpe  * This spec is used in order to pass information to the hardware driver in a
4236c61d2a5SJason Gunthorpe  * legacy way. Every verb that could get driver specific data should get this
4246c61d2a5SJason Gunthorpe  * spec.
4256c61d2a5SJason Gunthorpe  */
4266c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW()                                                      \
4279a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN,                                 \
428540cd692SJason Gunthorpe 			   UVERBS_ATTR_MIN_SIZE(0),			       \
429540cd692SJason Gunthorpe 			   UA_OPTIONAL),				       \
4309a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT,                               \
431540cd692SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(0),			       \
432540cd692SJason Gunthorpe 			    UA_OPTIONAL)
4336c61d2a5SJason Gunthorpe 
4346c61d2a5SJason Gunthorpe /*
435d108dac0SJason Gunthorpe  * =======================================
436d108dac0SJason Gunthorpe  *	Declaration helpers
437d108dac0SJason Gunthorpe  * =======================================
438d108dac0SJason Gunthorpe  */
4396c61d2a5SJason Gunthorpe 
44009e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...)                                 \
4416c61d2a5SJason Gunthorpe 	static const struct uverbs_object_def *const _name##_ptr[] = {         \
4426c61d2a5SJason Gunthorpe 		__VA_ARGS__,                                                   \
4436c61d2a5SJason Gunthorpe 	};                                                                     \
4446c61d2a5SJason Gunthorpe 	static const struct uverbs_object_tree_def _name = {                   \
4456c61d2a5SJason Gunthorpe 		.num_objects = ARRAY_SIZE(_name##_ptr),                        \
4466c61d2a5SJason Gunthorpe 		.objects = &_name##_ptr,                                       \
4476c61d2a5SJason Gunthorpe 	}
44809e3ebf8SMatan Barak 
449fac9658cSMatan Barak /* =================================================
450fac9658cSMatan Barak  *              Parsing infrastructure
451fac9658cSMatan Barak  * =================================================
452fac9658cSMatan Barak  */
453fac9658cSMatan Barak 
454fac9658cSMatan Barak struct uverbs_ptr_attr {
4558762d149SMatan Barak 	/*
4568762d149SMatan Barak 	 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
4578762d149SMatan Barak 	 * used.
4588762d149SMatan Barak 	 */
4598762d149SMatan Barak 	union {
4608762d149SMatan Barak 		void *ptr;
461fac9658cSMatan Barak 		u64 data;
4628762d149SMatan Barak 	};
463fac9658cSMatan Barak 	u16		len;
464fac9658cSMatan Barak 	/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
465fac9658cSMatan Barak 	u16		flags;
466494c5580SMatan Barak 	u8		enum_id;
467fac9658cSMatan Barak };
468fac9658cSMatan Barak 
469f43dbebfSMatan Barak struct uverbs_obj_attr {
470f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
471f43dbebfSMatan Barak };
472f43dbebfSMatan Barak 
473f43dbebfSMatan Barak struct uverbs_attr {
474fac9658cSMatan Barak 	/*
475fac9658cSMatan Barak 	 * pointer to the user-space given attribute, in order to write the
476fac9658cSMatan Barak 	 * new uobject's id or update flags.
477fac9658cSMatan Barak 	 */
478fac9658cSMatan Barak 	struct ib_uverbs_attr __user	*uattr;
479fac9658cSMatan Barak 	union {
480fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
481f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
482f43dbebfSMatan Barak 	};
483fac9658cSMatan Barak };
484f43dbebfSMatan Barak 
485f43dbebfSMatan Barak struct uverbs_attr_bundle_hash {
486f43dbebfSMatan Barak 	/* if bit i is set, it means attrs[i] contains valid information */
487f43dbebfSMatan Barak 	unsigned long *valid_bitmap;
488f43dbebfSMatan Barak 	size_t num_attrs;
489f43dbebfSMatan Barak 	/*
490f43dbebfSMatan Barak 	 * arrays of attributes, each element corresponds to the specification
491f43dbebfSMatan Barak 	 * of the attribute in the same index.
492f43dbebfSMatan Barak 	 */
493f43dbebfSMatan Barak 	struct uverbs_attr *attrs;
494f43dbebfSMatan Barak };
495f43dbebfSMatan Barak 
496f43dbebfSMatan Barak struct uverbs_attr_bundle {
497f43dbebfSMatan Barak 	size_t				num_buckets;
498f43dbebfSMatan Barak 	struct uverbs_attr_bundle_hash  hash[];
499f43dbebfSMatan Barak };
500f43dbebfSMatan Barak 
501f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
502f43dbebfSMatan Barak 						unsigned int idx)
503f43dbebfSMatan Barak {
504f43dbebfSMatan Barak 	return test_bit(idx, attrs_hash->valid_bitmap);
505f43dbebfSMatan Barak }
506f43dbebfSMatan Barak 
50735410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
50835410306SMatan Barak 					unsigned int idx)
50935410306SMatan Barak {
51035410306SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
51135410306SMatan Barak 
51235410306SMatan Barak 	if (attrs_bundle->num_buckets <= idx_bucket)
51335410306SMatan Barak 		return false;
51435410306SMatan Barak 
51535410306SMatan Barak 	return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
51635410306SMatan Barak 					    idx & ~UVERBS_ID_NS_MASK);
51735410306SMatan Barak }
51835410306SMatan Barak 
51941b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
52041b2a71fSMatan Barak 
521d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
522d70724f1SMatan Barak 							u16 idx)
523d70724f1SMatan Barak {
524d70724f1SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
525d70724f1SMatan Barak 
526d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
527d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
528d70724f1SMatan Barak 
529d70724f1SMatan Barak 	return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
530d70724f1SMatan Barak }
531d70724f1SMatan Barak 
532494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
533494c5580SMatan Barak 					  u16 idx)
534494c5580SMatan Barak {
535494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
536494c5580SMatan Barak 
537494c5580SMatan Barak 	if (IS_ERR(attr))
538494c5580SMatan Barak 		return PTR_ERR(attr);
539494c5580SMatan Barak 
540494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
541494c5580SMatan Barak }
542494c5580SMatan Barak 
543be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
544be934ccaSAriel Levkovich 					u16 idx)
545be934ccaSAriel Levkovich {
546f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
547be934ccaSAriel Levkovich 
548f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
549f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
550f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
551be934ccaSAriel Levkovich 
552f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
553be934ccaSAriel Levkovich }
554be934ccaSAriel Levkovich 
5553efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
5563efa3812SMatan Barak 							 u16 idx)
5573efa3812SMatan Barak {
5583efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5593efa3812SMatan Barak 
5603efa3812SMatan Barak 	if (IS_ERR(attr))
5613efa3812SMatan Barak 		return ERR_CAST(attr);
5623efa3812SMatan Barak 
5633efa3812SMatan Barak 	return attr->obj_attr.uobject;
5643efa3812SMatan Barak }
5653efa3812SMatan Barak 
5668762d149SMatan Barak static inline int
5678762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
5688762d149SMatan Barak {
5698762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5708762d149SMatan Barak 
5718762d149SMatan Barak 	if (IS_ERR(attr))
5728762d149SMatan Barak 		return PTR_ERR(attr);
5738762d149SMatan Barak 
5748762d149SMatan Barak 	return attr->ptr_attr.len;
5758762d149SMatan Barak }
5768762d149SMatan Barak 
577d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
57889d9e8d3SMatan Barak 				 size_t idx, const void *from, size_t size)
579d70724f1SMatan Barak {
580d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
581d70724f1SMatan Barak 	u16 flags;
58289d9e8d3SMatan Barak 	size_t min_size;
583d70724f1SMatan Barak 
584d70724f1SMatan Barak 	if (IS_ERR(attr))
585d70724f1SMatan Barak 		return PTR_ERR(attr);
586d70724f1SMatan Barak 
58789d9e8d3SMatan Barak 	min_size = min_t(size_t, attr->ptr_attr.len, size);
5882f36028cSJason Gunthorpe 	if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
58989d9e8d3SMatan Barak 		return -EFAULT;
59089d9e8d3SMatan Barak 
591d70724f1SMatan Barak 	flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
59289d9e8d3SMatan Barak 	if (put_user(flags, &attr->uattr->flags))
59389d9e8d3SMatan Barak 		return -EFAULT;
59489d9e8d3SMatan Barak 
59589d9e8d3SMatan Barak 	return 0;
596d70724f1SMatan Barak }
597d70724f1SMatan Barak 
59889d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
59989d9e8d3SMatan Barak {
60089d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
60189d9e8d3SMatan Barak }
60289d9e8d3SMatan Barak 
6038762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
6048762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
6058762d149SMatan Barak {
6068762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
6078762d149SMatan Barak 
6088762d149SMatan Barak 	if (IS_ERR(attr))
6098762d149SMatan Barak 		return (void *)attr;
6108762d149SMatan Barak 
6118762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
6128762d149SMatan Barak 						 attr->ptr_attr.ptr;
6138762d149SMatan Barak }
6148762d149SMatan Barak 
61589d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
616d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
61789d9e8d3SMatan Barak 				    size_t idx,
61889d9e8d3SMatan Barak 				    size_t size)
619d70724f1SMatan Barak {
620d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
621d70724f1SMatan Barak 
622d70724f1SMatan Barak 	if (IS_ERR(attr))
623d70724f1SMatan Barak 		return PTR_ERR(attr);
624d70724f1SMatan Barak 
62589d9e8d3SMatan Barak 	/*
62689d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
627c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
628c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
62989d9e8d3SMatan Barak 	 */
63089d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
63189d9e8d3SMatan Barak 		return -EINVAL;
63289d9e8d3SMatan Barak 
63389d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
634d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
6352f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
6362f36028cSJason Gunthorpe 				attr->ptr_attr.len))
637d70724f1SMatan Barak 		return -EFAULT;
638d70724f1SMatan Barak 
639d70724f1SMatan Barak 	return 0;
640d70724f1SMatan Barak }
641d70724f1SMatan Barak 
642c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
643c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
644c66db311SMatan Barak 					    size_t idx,
645c66db311SMatan Barak 					    size_t size)
646c66db311SMatan Barak {
647c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
648c66db311SMatan Barak 	size_t min_size;
649c66db311SMatan Barak 
650c66db311SMatan Barak 	if (IS_ERR(attr))
651c66db311SMatan Barak 		return PTR_ERR(attr);
652c66db311SMatan Barak 
653c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
654c66db311SMatan Barak 
655c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
656c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
657c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
658c66db311SMatan Barak 				min_size))
659c66db311SMatan Barak 		return -EFAULT;
660c66db311SMatan Barak 
661c66db311SMatan Barak 	if (size > min_size)
662c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
663c66db311SMatan Barak 
664c66db311SMatan Barak 	return 0;
665c66db311SMatan Barak }
666c66db311SMatan Barak 
667d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
66889d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
669d70724f1SMatan Barak 
670c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
671c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
672c66db311SMatan Barak 
673bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
674bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
675bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
676bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
677bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
678bccd0622SJason Gunthorpe #else
679bccd0622SJason Gunthorpe static inline int
680bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
681bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
682bccd0622SJason Gunthorpe {
683bccd0622SJason Gunthorpe 	return -EINVAL;
684bccd0622SJason Gunthorpe }
685bccd0622SJason Gunthorpe static inline int
686bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
687bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
688bccd0622SJason Gunthorpe {
689bccd0622SJason Gunthorpe 	return -EINVAL;
690bccd0622SJason Gunthorpe }
691bccd0622SJason Gunthorpe #endif
692bccd0622SJason Gunthorpe 
693118620d3SMatan Barak /* =================================================
694118620d3SMatan Barak  *	 Definitions -> Specs infrastructure
695118620d3SMatan Barak  * =================================================
696118620d3SMatan Barak  */
697a0aa309cSMatan Barak 
698118620d3SMatan Barak /*
699118620d3SMatan Barak  * uverbs_alloc_spec_tree - Merges different common and driver specific feature
700118620d3SMatan Barak  *	into one parsing tree that every uverbs command will be parsed upon.
701118620d3SMatan Barak  *
702118620d3SMatan Barak  * @num_trees: Number of trees in the array @trees.
703118620d3SMatan Barak  * @trees: Array of pointers to tree root definitions to merge. Each such tree
704118620d3SMatan Barak  *	   possibly contains objects, methods and attributes definitions.
705118620d3SMatan Barak  *
706118620d3SMatan Barak  * Returns:
707118620d3SMatan Barak  *	uverbs_root_spec *: The root of the merged parsing tree.
708118620d3SMatan Barak  *	On error, we return an error code. Error is checked via IS_ERR.
709118620d3SMatan Barak  *
710118620d3SMatan Barak  * The following merges could take place:
711118620d3SMatan Barak  * a. Two trees representing the same method with different handler
712118620d3SMatan Barak  *	-> We take the handler of the tree that its handler != NULL
713118620d3SMatan Barak  *	   and its index in the trees array is greater. The incentive for that
714118620d3SMatan Barak  *	   is that developers are expected to first merge common trees and then
715118620d3SMatan Barak  *	   merge trees that gives specialized the behaviour.
716118620d3SMatan Barak  * b. Two trees representing the same object with different
717118620d3SMatan Barak  *    type_attrs (struct uverbs_obj_type):
718118620d3SMatan Barak  *	-> We take the type_attrs of the tree that its type_attr != NULL
719118620d3SMatan Barak  *	   and its index in the trees array is greater. This could be used
720118620d3SMatan Barak  *	   in order to override the free function, allocation size, etc.
721118620d3SMatan Barak  * c. Two trees representing the same method attribute (same id but possibly
722118620d3SMatan Barak  *    different attributes):
723118620d3SMatan Barak  *	-> ERROR (-ENOENT), we believe that's not the programmer's intent.
724118620d3SMatan Barak  *
725118620d3SMatan Barak  * An object without any methods is considered invalid and will abort the
726118620d3SMatan Barak  * function with -ENOENT error.
727118620d3SMatan Barak  */
72852427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
729118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
730118620d3SMatan Barak 						const struct uverbs_object_tree_def **trees);
731118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root);
73252427112SMatan Barak #else
73352427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
73452427112SMatan Barak 							      const struct uverbs_object_tree_def **trees)
73552427112SMatan Barak {
73652427112SMatan Barak 	return NULL;
73752427112SMatan Barak }
73852427112SMatan Barak 
73952427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
74052427112SMatan Barak {
74152427112SMatan Barak }
74252427112SMatan Barak #endif
743118620d3SMatan Barak 
744118620d3SMatan Barak #endif
745