xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 4b3dd2bbf0818ccb23e7f2831f2ca4a86789cd1f)
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 /*
1589ed3e5f4SJason Gunthorpe  * Information about the API is loaded into a radix tree. For IOCTL we start
1599ed3e5f4SJason Gunthorpe  * with a tuple of:
1609ed3e5f4SJason Gunthorpe  *  object_id, attr_id, method_id
1619ed3e5f4SJason Gunthorpe  *
1629ed3e5f4SJason Gunthorpe  * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
1639ed3e5f4SJason Gunthorpe  * on the current kernel support this is compressed into 16 bit key for the
1649ed3e5f4SJason Gunthorpe  * radix tree. Since this compression is entirely internal to the kernel the
1659ed3e5f4SJason Gunthorpe  * below limits can be revised if the kernel gains additional data.
1669ed3e5f4SJason Gunthorpe  *
1679ed3e5f4SJason Gunthorpe  * With 64 leafs per node this is a 3 level radix tree.
1689ed3e5f4SJason Gunthorpe  *
1699ed3e5f4SJason Gunthorpe  * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
1709ed3e5f4SJason Gunthorpe  * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
1719ed3e5f4SJason Gunthorpe  */
1729ed3e5f4SJason Gunthorpe enum uapi_radix_data {
1739ed3e5f4SJason Gunthorpe 	UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
1749ed3e5f4SJason Gunthorpe 
1759ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_BITS = 6,
1769ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
1779ed3e5f4SJason Gunthorpe 	UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
1789ed3e5f4SJason Gunthorpe 
1799ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_BITS = 5,
1809ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
1819ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_CORE = 24,
1829ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) -
1839ed3e5f4SJason Gunthorpe 					   UVERBS_API_METHOD_KEY_NUM_CORE,
1849ed3e5f4SJason Gunthorpe 	UVERBS_API_METHOD_KEY_MASK = GENMASK(
1859ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
1869ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_SHIFT),
1879ed3e5f4SJason Gunthorpe 
1889ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_BITS = 5,
1899ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_SHIFT =
1909ed3e5f4SJason Gunthorpe 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
1919ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_CORE = 24,
1929ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_NUM_DRIVER =
1939ed3e5f4SJason Gunthorpe 		(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
1949ed3e5f4SJason Gunthorpe 	UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
1959ed3e5f4SJason Gunthorpe 
1969ed3e5f4SJason Gunthorpe 	/* This id guaranteed to not exist in the radix tree */
1979ed3e5f4SJason Gunthorpe 	UVERBS_API_KEY_ERR = 0xFFFFFFFF,
1989ed3e5f4SJason Gunthorpe };
1999ed3e5f4SJason Gunthorpe 
2009ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id)
2019ed3e5f4SJason Gunthorpe {
2029ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
2039ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
2049ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
2059ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2069ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
2079ed3e5f4SJason Gunthorpe 	} else {
2089ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
2099ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2109ed3e5f4SJason Gunthorpe 	}
2119ed3e5f4SJason Gunthorpe 
2129ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_OBJ_KEY_SHIFT;
2139ed3e5f4SJason Gunthorpe }
2149ed3e5f4SJason Gunthorpe 
2159ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key)
2169ed3e5f4SJason Gunthorpe {
2179ed3e5f4SJason Gunthorpe 	return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
2189ed3e5f4SJason Gunthorpe }
2199ed3e5f4SJason Gunthorpe 
2209ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
2219ed3e5f4SJason Gunthorpe {
2229ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
2239ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
2249ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
2259ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2269ed3e5f4SJason Gunthorpe 		id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
2279ed3e5f4SJason Gunthorpe 	} else {
2289ed3e5f4SJason Gunthorpe 		id++;
2299ed3e5f4SJason Gunthorpe 		if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
2309ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2319ed3e5f4SJason Gunthorpe 	}
2329ed3e5f4SJason Gunthorpe 
2339ed3e5f4SJason Gunthorpe 	return id << UVERBS_API_METHOD_KEY_SHIFT;
2349ed3e5f4SJason Gunthorpe }
2359ed3e5f4SJason Gunthorpe 
2369ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
2379ed3e5f4SJason Gunthorpe {
2389ed3e5f4SJason Gunthorpe 	return attr_key &
2399ed3e5f4SJason Gunthorpe 	       (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
2409ed3e5f4SJason Gunthorpe }
2419ed3e5f4SJason Gunthorpe 
2429ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
2439ed3e5f4SJason Gunthorpe {
2449ed3e5f4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
2459ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) == 0;
2469ed3e5f4SJason Gunthorpe }
2479ed3e5f4SJason Gunthorpe 
2489ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
2499ed3e5f4SJason Gunthorpe {
2509ed3e5f4SJason Gunthorpe 	/* 0 is the method slot itself */
2519ed3e5f4SJason Gunthorpe 	return ioctl_method_key + 1;
2529ed3e5f4SJason Gunthorpe }
2539ed3e5f4SJason Gunthorpe 
2549ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id)
2559ed3e5f4SJason Gunthorpe {
2569ed3e5f4SJason Gunthorpe 	/*
2579ed3e5f4SJason Gunthorpe 	 * The attr is designed to fit in the typical single radix tree node
2589ed3e5f4SJason Gunthorpe 	 * of 64 entries. Since allmost all methods have driver attributes we
2599ed3e5f4SJason Gunthorpe 	 * organize things so that the driver and core attributes interleave to
2609ed3e5f4SJason Gunthorpe 	 * reduce the length of the attributes array in typical cases.
2619ed3e5f4SJason Gunthorpe 	 */
2629ed3e5f4SJason Gunthorpe 	if (id & UVERBS_API_NS_FLAG) {
2639ed3e5f4SJason Gunthorpe 		id &= ~UVERBS_API_NS_FLAG;
2649ed3e5f4SJason Gunthorpe 		id++;
2659ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2669ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2679ed3e5f4SJason Gunthorpe 		id = (id << 1) | 0;
2689ed3e5f4SJason Gunthorpe 	} else {
2699ed3e5f4SJason Gunthorpe 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2709ed3e5f4SJason Gunthorpe 			return UVERBS_API_KEY_ERR;
2719ed3e5f4SJason Gunthorpe 		id = (id << 1) | 1;
2729ed3e5f4SJason Gunthorpe 	}
2739ed3e5f4SJason Gunthorpe 
2749ed3e5f4SJason Gunthorpe 	return id;
2759ed3e5f4SJason Gunthorpe }
2769ed3e5f4SJason Gunthorpe 
2779ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
2789ed3e5f4SJason Gunthorpe {
2799ed3e5f4SJason Gunthorpe 	return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
2809ed3e5f4SJason Gunthorpe 	       (key & UVERBS_API_ATTR_KEY_MASK) != 0;
2819ed3e5f4SJason Gunthorpe }
2829ed3e5f4SJason Gunthorpe 
2839ed3e5f4SJason Gunthorpe /*
2849ed3e5f4SJason Gunthorpe  * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
2859ed3e5f4SJason Gunthorpe  * basically it undoes the reservation of 0 in the ID numbering. attr_key
2869ed3e5f4SJason Gunthorpe  * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
2879ed3e5f4SJason Gunthorpe  * uapi_key_attr().
2889ed3e5f4SJason Gunthorpe  */
2899ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
2909ed3e5f4SJason Gunthorpe {
2919ed3e5f4SJason Gunthorpe 	return attr_key - 1;
2929ed3e5f4SJason Gunthorpe }
2939ed3e5f4SJason Gunthorpe 
2949ed3e5f4SJason 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 {
497*4b3dd2bbSJason Gunthorpe 	struct ib_uverbs_file *ufile;
498f43dbebfSMatan Barak 	size_t				num_buckets;
499f43dbebfSMatan Barak 	struct uverbs_attr_bundle_hash  hash[];
500f43dbebfSMatan Barak };
501f43dbebfSMatan Barak 
502f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
503f43dbebfSMatan Barak 						unsigned int idx)
504f43dbebfSMatan Barak {
505f43dbebfSMatan Barak 	return test_bit(idx, attrs_hash->valid_bitmap);
506f43dbebfSMatan Barak }
507f43dbebfSMatan Barak 
50835410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
50935410306SMatan Barak 					unsigned int idx)
51035410306SMatan Barak {
51135410306SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
51235410306SMatan Barak 
51335410306SMatan Barak 	if (attrs_bundle->num_buckets <= idx_bucket)
51435410306SMatan Barak 		return false;
51535410306SMatan Barak 
51635410306SMatan Barak 	return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
51735410306SMatan Barak 					    idx & ~UVERBS_ID_NS_MASK);
51835410306SMatan Barak }
51935410306SMatan Barak 
52041b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
52141b2a71fSMatan Barak 
522d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
523d70724f1SMatan Barak 							u16 idx)
524d70724f1SMatan Barak {
525d70724f1SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
526d70724f1SMatan Barak 
527d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
528d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
529d70724f1SMatan Barak 
530d70724f1SMatan Barak 	return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
531d70724f1SMatan Barak }
532d70724f1SMatan Barak 
533494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
534494c5580SMatan Barak 					  u16 idx)
535494c5580SMatan Barak {
536494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
537494c5580SMatan Barak 
538494c5580SMatan Barak 	if (IS_ERR(attr))
539494c5580SMatan Barak 		return PTR_ERR(attr);
540494c5580SMatan Barak 
541494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
542494c5580SMatan Barak }
543494c5580SMatan Barak 
544be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
545be934ccaSAriel Levkovich 					u16 idx)
546be934ccaSAriel Levkovich {
547f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
548be934ccaSAriel Levkovich 
549f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
550f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
551f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
552be934ccaSAriel Levkovich 
553f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
554be934ccaSAriel Levkovich }
555be934ccaSAriel Levkovich 
5563efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
5573efa3812SMatan Barak 							 u16 idx)
5583efa3812SMatan Barak {
5593efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5603efa3812SMatan Barak 
5613efa3812SMatan Barak 	if (IS_ERR(attr))
5623efa3812SMatan Barak 		return ERR_CAST(attr);
5633efa3812SMatan Barak 
5643efa3812SMatan Barak 	return attr->obj_attr.uobject;
5653efa3812SMatan Barak }
5663efa3812SMatan Barak 
5678762d149SMatan Barak static inline int
5688762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
5698762d149SMatan Barak {
5708762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5718762d149SMatan Barak 
5728762d149SMatan Barak 	if (IS_ERR(attr))
5738762d149SMatan Barak 		return PTR_ERR(attr);
5748762d149SMatan Barak 
5758762d149SMatan Barak 	return attr->ptr_attr.len;
5768762d149SMatan Barak }
5778762d149SMatan Barak 
578d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
57989d9e8d3SMatan Barak 				 size_t idx, const void *from, size_t size)
580d70724f1SMatan Barak {
581d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
582d70724f1SMatan Barak 	u16 flags;
58389d9e8d3SMatan Barak 	size_t min_size;
584d70724f1SMatan Barak 
585d70724f1SMatan Barak 	if (IS_ERR(attr))
586d70724f1SMatan Barak 		return PTR_ERR(attr);
587d70724f1SMatan Barak 
58889d9e8d3SMatan Barak 	min_size = min_t(size_t, attr->ptr_attr.len, size);
5892f36028cSJason Gunthorpe 	if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
59089d9e8d3SMatan Barak 		return -EFAULT;
59189d9e8d3SMatan Barak 
592d70724f1SMatan Barak 	flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
59389d9e8d3SMatan Barak 	if (put_user(flags, &attr->uattr->flags))
59489d9e8d3SMatan Barak 		return -EFAULT;
59589d9e8d3SMatan Barak 
59689d9e8d3SMatan Barak 	return 0;
597d70724f1SMatan Barak }
598d70724f1SMatan Barak 
59989d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
60089d9e8d3SMatan Barak {
60189d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
60289d9e8d3SMatan Barak }
60389d9e8d3SMatan Barak 
6048762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
6058762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
6068762d149SMatan Barak {
6078762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
6088762d149SMatan Barak 
6098762d149SMatan Barak 	if (IS_ERR(attr))
6108762d149SMatan Barak 		return (void *)attr;
6118762d149SMatan Barak 
6128762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
6138762d149SMatan Barak 						 attr->ptr_attr.ptr;
6148762d149SMatan Barak }
6158762d149SMatan Barak 
61689d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
617d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
61889d9e8d3SMatan Barak 				    size_t idx,
61989d9e8d3SMatan Barak 				    size_t size)
620d70724f1SMatan Barak {
621d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
622d70724f1SMatan Barak 
623d70724f1SMatan Barak 	if (IS_ERR(attr))
624d70724f1SMatan Barak 		return PTR_ERR(attr);
625d70724f1SMatan Barak 
62689d9e8d3SMatan Barak 	/*
62789d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
628c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
629c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
63089d9e8d3SMatan Barak 	 */
63189d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
63289d9e8d3SMatan Barak 		return -EINVAL;
63389d9e8d3SMatan Barak 
63489d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
635d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
6362f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
6372f36028cSJason Gunthorpe 				attr->ptr_attr.len))
638d70724f1SMatan Barak 		return -EFAULT;
639d70724f1SMatan Barak 
640d70724f1SMatan Barak 	return 0;
641d70724f1SMatan Barak }
642d70724f1SMatan Barak 
643c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
644c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
645c66db311SMatan Barak 					    size_t idx,
646c66db311SMatan Barak 					    size_t size)
647c66db311SMatan Barak {
648c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
649c66db311SMatan Barak 	size_t min_size;
650c66db311SMatan Barak 
651c66db311SMatan Barak 	if (IS_ERR(attr))
652c66db311SMatan Barak 		return PTR_ERR(attr);
653c66db311SMatan Barak 
654c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
655c66db311SMatan Barak 
656c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
657c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
658c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
659c66db311SMatan Barak 				min_size))
660c66db311SMatan Barak 		return -EFAULT;
661c66db311SMatan Barak 
662c66db311SMatan Barak 	if (size > min_size)
663c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
664c66db311SMatan Barak 
665c66db311SMatan Barak 	return 0;
666c66db311SMatan Barak }
667c66db311SMatan Barak 
668d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
66989d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
670d70724f1SMatan Barak 
671c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
672c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
673c66db311SMatan Barak 
674bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
675bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
676bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
677bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
678bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
679bccd0622SJason Gunthorpe #else
680bccd0622SJason Gunthorpe static inline int
681bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
682bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
683bccd0622SJason Gunthorpe {
684bccd0622SJason Gunthorpe 	return -EINVAL;
685bccd0622SJason Gunthorpe }
686bccd0622SJason Gunthorpe static inline int
687bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
688bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
689bccd0622SJason Gunthorpe {
690bccd0622SJason Gunthorpe 	return -EINVAL;
691bccd0622SJason Gunthorpe }
692bccd0622SJason Gunthorpe #endif
693bccd0622SJason Gunthorpe 
694118620d3SMatan Barak /* =================================================
695118620d3SMatan Barak  *	 Definitions -> Specs infrastructure
696118620d3SMatan Barak  * =================================================
697118620d3SMatan Barak  */
698a0aa309cSMatan Barak 
699118620d3SMatan Barak /*
700118620d3SMatan Barak  * uverbs_alloc_spec_tree - Merges different common and driver specific feature
701118620d3SMatan Barak  *	into one parsing tree that every uverbs command will be parsed upon.
702118620d3SMatan Barak  *
703118620d3SMatan Barak  * @num_trees: Number of trees in the array @trees.
704118620d3SMatan Barak  * @trees: Array of pointers to tree root definitions to merge. Each such tree
705118620d3SMatan Barak  *	   possibly contains objects, methods and attributes definitions.
706118620d3SMatan Barak  *
707118620d3SMatan Barak  * Returns:
708118620d3SMatan Barak  *	uverbs_root_spec *: The root of the merged parsing tree.
709118620d3SMatan Barak  *	On error, we return an error code. Error is checked via IS_ERR.
710118620d3SMatan Barak  *
711118620d3SMatan Barak  * The following merges could take place:
712118620d3SMatan Barak  * a. Two trees representing the same method with different handler
713118620d3SMatan Barak  *	-> We take the handler of the tree that its handler != NULL
714118620d3SMatan Barak  *	   and its index in the trees array is greater. The incentive for that
715118620d3SMatan Barak  *	   is that developers are expected to first merge common trees and then
716118620d3SMatan Barak  *	   merge trees that gives specialized the behaviour.
717118620d3SMatan Barak  * b. Two trees representing the same object with different
718118620d3SMatan Barak  *    type_attrs (struct uverbs_obj_type):
719118620d3SMatan Barak  *	-> We take the type_attrs of the tree that its type_attr != NULL
720118620d3SMatan Barak  *	   and its index in the trees array is greater. This could be used
721118620d3SMatan Barak  *	   in order to override the free function, allocation size, etc.
722118620d3SMatan Barak  * c. Two trees representing the same method attribute (same id but possibly
723118620d3SMatan Barak  *    different attributes):
724118620d3SMatan Barak  *	-> ERROR (-ENOENT), we believe that's not the programmer's intent.
725118620d3SMatan Barak  *
726118620d3SMatan Barak  * An object without any methods is considered invalid and will abort the
727118620d3SMatan Barak  * function with -ENOENT error.
728118620d3SMatan Barak  */
72952427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
730118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
731118620d3SMatan Barak 						const struct uverbs_object_tree_def **trees);
732118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root);
73352427112SMatan Barak #else
73452427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
73552427112SMatan Barak 							      const struct uverbs_object_tree_def **trees)
73652427112SMatan Barak {
73752427112SMatan Barak 	return NULL;
73852427112SMatan Barak }
73952427112SMatan Barak 
74052427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
74152427112SMatan Barak {
74252427112SMatan Barak }
74352427112SMatan Barak #endif
744118620d3SMatan Barak 
745118620d3SMatan Barak #endif
746