xref: /openbmc/linux/include/rdma/uverbs_ioctl.h (revision 461bb2eee4e162617e790c74d9b4ab10056cad7f)
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;
4646a1f444fSJason Gunthorpe 	u16		uattr_idx;
465494c5580SMatan Barak 	u8		enum_id;
466fac9658cSMatan Barak };
467fac9658cSMatan Barak 
468f43dbebfSMatan Barak struct uverbs_obj_attr {
469f43dbebfSMatan Barak 	struct ib_uobject		*uobject;
470f43dbebfSMatan Barak };
471f43dbebfSMatan Barak 
472f43dbebfSMatan Barak struct uverbs_attr {
473fac9658cSMatan Barak 	union {
474fac9658cSMatan Barak 		struct uverbs_ptr_attr	ptr_attr;
475f43dbebfSMatan Barak 		struct uverbs_obj_attr	obj_attr;
476f43dbebfSMatan Barak 	};
477fac9658cSMatan Barak };
478f43dbebfSMatan Barak 
479f43dbebfSMatan Barak struct uverbs_attr_bundle_hash {
480f43dbebfSMatan Barak 	/* if bit i is set, it means attrs[i] contains valid information */
481f43dbebfSMatan Barak 	unsigned long *valid_bitmap;
482f43dbebfSMatan Barak 	size_t num_attrs;
483f43dbebfSMatan Barak 	/*
484f43dbebfSMatan Barak 	 * arrays of attributes, each element corresponds to the specification
485f43dbebfSMatan Barak 	 * of the attribute in the same index.
486f43dbebfSMatan Barak 	 */
487f43dbebfSMatan Barak 	struct uverbs_attr *attrs;
488f43dbebfSMatan Barak };
489f43dbebfSMatan Barak 
490f43dbebfSMatan Barak struct uverbs_attr_bundle {
4914b3dd2bbSJason Gunthorpe 	struct ib_uverbs_file *ufile;
492f43dbebfSMatan Barak 	size_t				num_buckets;
493f43dbebfSMatan Barak 	struct uverbs_attr_bundle_hash  hash[];
494f43dbebfSMatan Barak };
495f43dbebfSMatan Barak 
496f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
497f43dbebfSMatan Barak 						unsigned int idx)
498f43dbebfSMatan Barak {
499f43dbebfSMatan Barak 	return test_bit(idx, attrs_hash->valid_bitmap);
500f43dbebfSMatan Barak }
501f43dbebfSMatan Barak 
50235410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
50335410306SMatan Barak 					unsigned int idx)
50435410306SMatan Barak {
50535410306SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
50635410306SMatan Barak 
50735410306SMatan Barak 	if (attrs_bundle->num_buckets <= idx_bucket)
50835410306SMatan Barak 		return false;
50935410306SMatan Barak 
51035410306SMatan Barak 	return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
51135410306SMatan Barak 					    idx & ~UVERBS_ID_NS_MASK);
51235410306SMatan Barak }
51335410306SMatan Barak 
51441b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
51541b2a71fSMatan Barak 
516d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
517d70724f1SMatan Barak 							u16 idx)
518d70724f1SMatan Barak {
519d70724f1SMatan Barak 	u16 idx_bucket = idx >>	UVERBS_ID_NS_SHIFT;
520d70724f1SMatan Barak 
521d70724f1SMatan Barak 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
522d70724f1SMatan Barak 		return ERR_PTR(-ENOENT);
523d70724f1SMatan Barak 
524d70724f1SMatan Barak 	return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
525d70724f1SMatan Barak }
526d70724f1SMatan Barak 
527494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
528494c5580SMatan Barak 					  u16 idx)
529494c5580SMatan Barak {
530494c5580SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
531494c5580SMatan Barak 
532494c5580SMatan Barak 	if (IS_ERR(attr))
533494c5580SMatan Barak 		return PTR_ERR(attr);
534494c5580SMatan Barak 
535494c5580SMatan Barak 	return attr->ptr_attr.enum_id;
536494c5580SMatan Barak }
537494c5580SMatan Barak 
538be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
539be934ccaSAriel Levkovich 					u16 idx)
540be934ccaSAriel Levkovich {
541f4602cbbSJason Gunthorpe 	const struct uverbs_attr *attr;
542be934ccaSAriel Levkovich 
543f4602cbbSJason Gunthorpe 	attr = uverbs_attr_get(attrs_bundle, idx);
544f4602cbbSJason Gunthorpe 	if (IS_ERR(attr))
545f4602cbbSJason Gunthorpe 		return ERR_CAST(attr);
546be934ccaSAriel Levkovich 
547f4602cbbSJason Gunthorpe 	return attr->obj_attr.uobject->object;
548be934ccaSAriel Levkovich }
549be934ccaSAriel Levkovich 
5503efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
5513efa3812SMatan Barak 							 u16 idx)
5523efa3812SMatan Barak {
5533efa3812SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5543efa3812SMatan Barak 
5553efa3812SMatan Barak 	if (IS_ERR(attr))
5563efa3812SMatan Barak 		return ERR_CAST(attr);
5573efa3812SMatan Barak 
5583efa3812SMatan Barak 	return attr->obj_attr.uobject;
5593efa3812SMatan Barak }
5603efa3812SMatan Barak 
5618762d149SMatan Barak static inline int
5628762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
5638762d149SMatan Barak {
5648762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5658762d149SMatan Barak 
5668762d149SMatan Barak 	if (IS_ERR(attr))
5678762d149SMatan Barak 		return PTR_ERR(attr);
5688762d149SMatan Barak 
5698762d149SMatan Barak 	return attr->ptr_attr.len;
5708762d149SMatan Barak }
5718762d149SMatan Barak 
57289d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
57389d9e8d3SMatan Barak {
57489d9e8d3SMatan Barak 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
57589d9e8d3SMatan Barak }
57689d9e8d3SMatan Barak 
5778762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
5788762d149SMatan Barak 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
5798762d149SMatan Barak {
5808762d149SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
5818762d149SMatan Barak 
5828762d149SMatan Barak 	if (IS_ERR(attr))
5838762d149SMatan Barak 		return (void *)attr;
5848762d149SMatan Barak 
5858762d149SMatan Barak 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
5868762d149SMatan Barak 						 attr->ptr_attr.ptr;
5878762d149SMatan Barak }
5888762d149SMatan Barak 
58989d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
590d70724f1SMatan Barak 				    const struct uverbs_attr_bundle *attrs_bundle,
59189d9e8d3SMatan Barak 				    size_t idx,
59289d9e8d3SMatan Barak 				    size_t size)
593d70724f1SMatan Barak {
594d70724f1SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
595d70724f1SMatan Barak 
596d70724f1SMatan Barak 	if (IS_ERR(attr))
597d70724f1SMatan Barak 		return PTR_ERR(attr);
598d70724f1SMatan Barak 
59989d9e8d3SMatan Barak 	/*
60089d9e8d3SMatan Barak 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
601c66db311SMatan Barak 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
602c66db311SMatan Barak 	 * uverbs_copy_from_or_zero.
60389d9e8d3SMatan Barak 	 */
60489d9e8d3SMatan Barak 	if (unlikely(size < attr->ptr_attr.len))
60589d9e8d3SMatan Barak 		return -EINVAL;
60689d9e8d3SMatan Barak 
60789d9e8d3SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
608d70724f1SMatan Barak 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
6092f36028cSJason Gunthorpe 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
6102f36028cSJason Gunthorpe 				attr->ptr_attr.len))
611d70724f1SMatan Barak 		return -EFAULT;
612d70724f1SMatan Barak 
613d70724f1SMatan Barak 	return 0;
614d70724f1SMatan Barak }
615d70724f1SMatan Barak 
616c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
617c66db311SMatan Barak 					    const struct uverbs_attr_bundle *attrs_bundle,
618c66db311SMatan Barak 					    size_t idx,
619c66db311SMatan Barak 					    size_t size)
620c66db311SMatan Barak {
621c66db311SMatan Barak 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
622c66db311SMatan Barak 	size_t min_size;
623c66db311SMatan Barak 
624c66db311SMatan Barak 	if (IS_ERR(attr))
625c66db311SMatan Barak 		return PTR_ERR(attr);
626c66db311SMatan Barak 
627c66db311SMatan Barak 	min_size = min_t(size_t, size, attr->ptr_attr.len);
628c66db311SMatan Barak 
629c66db311SMatan Barak 	if (uverbs_attr_ptr_is_inline(attr))
630c66db311SMatan Barak 		memcpy(to, &attr->ptr_attr.data, min_size);
631c66db311SMatan Barak 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
632c66db311SMatan Barak 				min_size))
633c66db311SMatan Barak 		return -EFAULT;
634c66db311SMatan Barak 
635c66db311SMatan Barak 	if (size > min_size)
636c66db311SMatan Barak 		memset(to + min_size, 0, size - min_size);
637c66db311SMatan Barak 
638c66db311SMatan Barak 	return 0;
639c66db311SMatan Barak }
640c66db311SMatan Barak 
641d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx)				      \
64289d9e8d3SMatan Barak 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
643d70724f1SMatan Barak 
644c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
645c66db311SMatan Barak 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
646c66db311SMatan Barak 
647bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
648bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
649bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
650bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
651bccd0622SJason Gunthorpe 		       size_t idx, u64 allowed_bits);
6526a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
6536a1f444fSJason Gunthorpe 		   const void *from, size_t size);
654*461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
655*461bb2eeSJason Gunthorpe 			     gfp_t flags);
656*461bb2eeSJason Gunthorpe 
657*461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
658*461bb2eeSJason Gunthorpe 					  size_t size)
659*461bb2eeSJason Gunthorpe {
660*461bb2eeSJason Gunthorpe 	return _uverbs_alloc(bundle, size, GFP_KERNEL);
661*461bb2eeSJason Gunthorpe }
662*461bb2eeSJason Gunthorpe 
663*461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
664*461bb2eeSJason Gunthorpe 					   size_t size)
665*461bb2eeSJason Gunthorpe {
666*461bb2eeSJason Gunthorpe 	return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
667*461bb2eeSJason Gunthorpe }
668bccd0622SJason Gunthorpe #else
669bccd0622SJason Gunthorpe static inline int
670bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
671bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
672bccd0622SJason Gunthorpe {
673bccd0622SJason Gunthorpe 	return -EINVAL;
674bccd0622SJason Gunthorpe }
675bccd0622SJason Gunthorpe static inline int
676bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
677bccd0622SJason Gunthorpe 		   size_t idx, u64 allowed_bits)
678bccd0622SJason Gunthorpe {
679bccd0622SJason Gunthorpe 	return -EINVAL;
680bccd0622SJason Gunthorpe }
6816a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
6826a1f444fSJason Gunthorpe 				 size_t idx, const void *from, size_t size)
6836a1f444fSJason Gunthorpe {
6846a1f444fSJason Gunthorpe 	return -EINVAL;
6856a1f444fSJason Gunthorpe }
686*461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
687*461bb2eeSJason Gunthorpe 					  size_t size)
688*461bb2eeSJason Gunthorpe {
689*461bb2eeSJason Gunthorpe 	return ERR_PTR(-EINVAL);
690*461bb2eeSJason Gunthorpe }
691*461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
692*461bb2eeSJason Gunthorpe 					   size_t size)
693*461bb2eeSJason Gunthorpe {
694*461bb2eeSJason Gunthorpe 	return ERR_PTR(-EINVAL);
695*461bb2eeSJason Gunthorpe }
696bccd0622SJason Gunthorpe #endif
697bccd0622SJason Gunthorpe 
698118620d3SMatan Barak /* =================================================
699118620d3SMatan Barak  *	 Definitions -> Specs infrastructure
700118620d3SMatan Barak  * =================================================
701118620d3SMatan Barak  */
702a0aa309cSMatan Barak 
703118620d3SMatan Barak /*
704118620d3SMatan Barak  * uverbs_alloc_spec_tree - Merges different common and driver specific feature
705118620d3SMatan Barak  *	into one parsing tree that every uverbs command will be parsed upon.
706118620d3SMatan Barak  *
707118620d3SMatan Barak  * @num_trees: Number of trees in the array @trees.
708118620d3SMatan Barak  * @trees: Array of pointers to tree root definitions to merge. Each such tree
709118620d3SMatan Barak  *	   possibly contains objects, methods and attributes definitions.
710118620d3SMatan Barak  *
711118620d3SMatan Barak  * Returns:
712118620d3SMatan Barak  *	uverbs_root_spec *: The root of the merged parsing tree.
713118620d3SMatan Barak  *	On error, we return an error code. Error is checked via IS_ERR.
714118620d3SMatan Barak  *
715118620d3SMatan Barak  * The following merges could take place:
716118620d3SMatan Barak  * a. Two trees representing the same method with different handler
717118620d3SMatan Barak  *	-> We take the handler of the tree that its handler != NULL
718118620d3SMatan Barak  *	   and its index in the trees array is greater. The incentive for that
719118620d3SMatan Barak  *	   is that developers are expected to first merge common trees and then
720118620d3SMatan Barak  *	   merge trees that gives specialized the behaviour.
721118620d3SMatan Barak  * b. Two trees representing the same object with different
722118620d3SMatan Barak  *    type_attrs (struct uverbs_obj_type):
723118620d3SMatan Barak  *	-> We take the type_attrs of the tree that its type_attr != NULL
724118620d3SMatan Barak  *	   and its index in the trees array is greater. This could be used
725118620d3SMatan Barak  *	   in order to override the free function, allocation size, etc.
726118620d3SMatan Barak  * c. Two trees representing the same method attribute (same id but possibly
727118620d3SMatan Barak  *    different attributes):
728118620d3SMatan Barak  *	-> ERROR (-ENOENT), we believe that's not the programmer's intent.
729118620d3SMatan Barak  *
730118620d3SMatan Barak  * An object without any methods is considered invalid and will abort the
731118620d3SMatan Barak  * function with -ENOENT error.
732118620d3SMatan Barak  */
73352427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
734118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
735118620d3SMatan Barak 						const struct uverbs_object_tree_def **trees);
736118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root);
73752427112SMatan Barak #else
73852427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
73952427112SMatan Barak 							      const struct uverbs_object_tree_def **trees)
74052427112SMatan Barak {
74152427112SMatan Barak 	return NULL;
74252427112SMatan Barak }
74352427112SMatan Barak 
74452427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
74552427112SMatan Barak {
74652427112SMatan Barak }
74752427112SMatan Barak #endif
748118620d3SMatan Barak 
749118620d3SMatan Barak #endif
750