1*b633e08cSHans Petter Selasky /*
2*b633e08cSHans Petter Selasky * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
3*b633e08cSHans Petter Selasky *
4*b633e08cSHans Petter Selasky * This software is available to you under a choice of one of two
5*b633e08cSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
6*b633e08cSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
7*b633e08cSHans Petter Selasky * COPYING in the main directory of this source tree, or the
8*b633e08cSHans Petter Selasky * OpenIB.org BSD license below:
9*b633e08cSHans Petter Selasky *
10*b633e08cSHans Petter Selasky * Redistribution and use in source and binary forms, with or
11*b633e08cSHans Petter Selasky * without modification, are permitted provided that the following
12*b633e08cSHans Petter Selasky * conditions are met:
13*b633e08cSHans Petter Selasky *
14*b633e08cSHans Petter Selasky * - Redistributions of source code must retain the above
15*b633e08cSHans Petter Selasky * copyright notice, this list of conditions and the following
16*b633e08cSHans Petter Selasky * disclaimer.
17*b633e08cSHans Petter Selasky *
18*b633e08cSHans Petter Selasky * - Redistributions in binary form must reproduce the above
19*b633e08cSHans Petter Selasky * copyright notice, this list of conditions and the following
20*b633e08cSHans Petter Selasky * disclaimer in the documentation and/or other materials
21*b633e08cSHans Petter Selasky * provided with the distribution.
22*b633e08cSHans Petter Selasky *
23*b633e08cSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*b633e08cSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*b633e08cSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*b633e08cSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*b633e08cSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*b633e08cSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*b633e08cSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*b633e08cSHans Petter Selasky * SOFTWARE.
31*b633e08cSHans Petter Selasky */
32*b633e08cSHans Petter Selasky
33*b633e08cSHans Petter Selasky #ifndef _UVERBS_IOCTL_
34*b633e08cSHans Petter Selasky #define _UVERBS_IOCTL_
35*b633e08cSHans Petter Selasky
36*b633e08cSHans Petter Selasky #include <rdma/uverbs_types.h>
37*b633e08cSHans Petter Selasky #include <linux/uaccess.h>
38*b633e08cSHans Petter Selasky #include <rdma/rdma_user_ioctl.h>
39*b633e08cSHans Petter Selasky #include <rdma/ib_user_ioctl_verbs.h>
40*b633e08cSHans Petter Selasky #include <rdma/ib_user_ioctl_cmds.h>
41*b633e08cSHans Petter Selasky
42*b633e08cSHans Petter Selasky /*
43*b633e08cSHans Petter Selasky * =======================================
44*b633e08cSHans Petter Selasky * Verbs action specifications
45*b633e08cSHans Petter Selasky * =======================================
46*b633e08cSHans Petter Selasky */
47*b633e08cSHans Petter Selasky
48*b633e08cSHans Petter Selasky enum uverbs_attr_type {
49*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_NA,
50*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_PTR_IN,
51*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_PTR_OUT,
52*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_IDR,
53*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_FD,
54*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_ENUM_IN,
55*b633e08cSHans Petter Selasky UVERBS_ATTR_TYPE_IDRS_ARRAY,
56*b633e08cSHans Petter Selasky };
57*b633e08cSHans Petter Selasky
58*b633e08cSHans Petter Selasky enum uverbs_obj_access {
59*b633e08cSHans Petter Selasky UVERBS_ACCESS_READ,
60*b633e08cSHans Petter Selasky UVERBS_ACCESS_WRITE,
61*b633e08cSHans Petter Selasky UVERBS_ACCESS_NEW,
62*b633e08cSHans Petter Selasky UVERBS_ACCESS_DESTROY
63*b633e08cSHans Petter Selasky };
64*b633e08cSHans Petter Selasky
65*b633e08cSHans Petter Selasky /* Specification of a single attribute inside the ioctl message */
66*b633e08cSHans Petter Selasky /* good size 16 */
67*b633e08cSHans Petter Selasky struct uverbs_attr_spec {
68*b633e08cSHans Petter Selasky u8 type;
69*b633e08cSHans Petter Selasky
70*b633e08cSHans Petter Selasky /*
71*b633e08cSHans Petter Selasky * Support extending attributes by length. Allow the user to provide
72*b633e08cSHans Petter Selasky * more bytes than ptr.len, but check that everything after is zero'd
73*b633e08cSHans Petter Selasky * by the user.
74*b633e08cSHans Petter Selasky */
75*b633e08cSHans Petter Selasky u8 zero_trailing:1;
76*b633e08cSHans Petter Selasky /*
77*b633e08cSHans Petter Selasky * Valid only for PTR_IN. Allocate and copy the data inside
78*b633e08cSHans Petter Selasky * the parser
79*b633e08cSHans Petter Selasky */
80*b633e08cSHans Petter Selasky u8 alloc_and_copy:1;
81*b633e08cSHans Petter Selasky u8 mandatory:1;
82*b633e08cSHans Petter Selasky /* True if this is from UVERBS_ATTR_UHW */
83*b633e08cSHans Petter Selasky u8 is_udata:1;
84*b633e08cSHans Petter Selasky
85*b633e08cSHans Petter Selasky union {
86*b633e08cSHans Petter Selasky struct {
87*b633e08cSHans Petter Selasky /* Current known size to kernel */
88*b633e08cSHans Petter Selasky u16 len;
89*b633e08cSHans Petter Selasky /* User isn't allowed to provide something < min_len */
90*b633e08cSHans Petter Selasky u16 min_len;
91*b633e08cSHans Petter Selasky } ptr;
92*b633e08cSHans Petter Selasky
93*b633e08cSHans Petter Selasky struct {
94*b633e08cSHans Petter Selasky /*
95*b633e08cSHans Petter Selasky * higher bits mean the namespace and lower bits mean
96*b633e08cSHans Petter Selasky * the type id within the namespace.
97*b633e08cSHans Petter Selasky */
98*b633e08cSHans Petter Selasky u16 obj_type;
99*b633e08cSHans Petter Selasky u8 access;
100*b633e08cSHans Petter Selasky } obj;
101*b633e08cSHans Petter Selasky
102*b633e08cSHans Petter Selasky struct {
103*b633e08cSHans Petter Selasky u8 num_elems;
104*b633e08cSHans Petter Selasky } enum_def;
105*b633e08cSHans Petter Selasky } u;
106*b633e08cSHans Petter Selasky
107*b633e08cSHans Petter Selasky /* This weird split lets us remove some padding */
108*b633e08cSHans Petter Selasky union {
109*b633e08cSHans Petter Selasky struct {
110*b633e08cSHans Petter Selasky /*
111*b633e08cSHans Petter Selasky * The enum attribute can select one of the attributes
112*b633e08cSHans Petter Selasky * contained in the ids array. Currently only PTR_IN
113*b633e08cSHans Petter Selasky * attributes are supported in the ids array.
114*b633e08cSHans Petter Selasky */
115*b633e08cSHans Petter Selasky const struct uverbs_attr_spec *ids;
116*b633e08cSHans Petter Selasky } enum_def;
117*b633e08cSHans Petter Selasky
118*b633e08cSHans Petter Selasky struct {
119*b633e08cSHans Petter Selasky /*
120*b633e08cSHans Petter Selasky * higher bits mean the namespace and lower bits mean
121*b633e08cSHans Petter Selasky * the type id within the namespace.
122*b633e08cSHans Petter Selasky */
123*b633e08cSHans Petter Selasky u16 obj_type;
124*b633e08cSHans Petter Selasky u16 min_len;
125*b633e08cSHans Petter Selasky u16 max_len;
126*b633e08cSHans Petter Selasky u8 access;
127*b633e08cSHans Petter Selasky } objs_arr;
128*b633e08cSHans Petter Selasky } u2;
129*b633e08cSHans Petter Selasky };
130*b633e08cSHans Petter Selasky
131*b633e08cSHans Petter Selasky /*
132*b633e08cSHans Petter Selasky * Information about the API is loaded into a radix tree. For IOCTL we start
133*b633e08cSHans Petter Selasky * with a tuple of:
134*b633e08cSHans Petter Selasky * object_id, attr_id, method_id
135*b633e08cSHans Petter Selasky *
136*b633e08cSHans Petter Selasky * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
137*b633e08cSHans Petter Selasky * on the current kernel support this is compressed into 16 bit key for the
138*b633e08cSHans Petter Selasky * radix tree. Since this compression is entirely internal to the kernel the
139*b633e08cSHans Petter Selasky * below limits can be revised if the kernel gains additional data.
140*b633e08cSHans Petter Selasky *
141*b633e08cSHans Petter Selasky * With 64 leafs per node this is a 3 level radix tree.
142*b633e08cSHans Petter Selasky *
143*b633e08cSHans Petter Selasky * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
144*b633e08cSHans Petter Selasky * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
145*b633e08cSHans Petter Selasky *
146*b633e08cSHans Petter Selasky * This also encodes the tables for the write() and write() extended commands
147*b633e08cSHans Petter Selasky * using the coding
148*b633e08cSHans Petter Selasky * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
149*b633e08cSHans Petter Selasky * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
150*b633e08cSHans Petter Selasky * ie the WRITE path is treated as a special method type in the ioctl
151*b633e08cSHans Petter Selasky * framework.
152*b633e08cSHans Petter Selasky */
153*b633e08cSHans Petter Selasky enum uapi_radix_data {
154*b633e08cSHans Petter Selasky UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
155*b633e08cSHans Petter Selasky
156*b633e08cSHans Petter Selasky UVERBS_API_ATTR_KEY_BITS = 6,
157*b633e08cSHans Petter Selasky UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
158*b633e08cSHans Petter Selasky UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
159*b633e08cSHans Petter Selasky UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
160*b633e08cSHans Petter Selasky
161*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_BITS = 5,
162*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
163*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_NUM_CORE = 22,
164*b633e08cSHans Petter Selasky UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
165*b633e08cSHans Petter Selasky UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
166*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_NUM_DRIVER =
167*b633e08cSHans Petter Selasky (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
168*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_NUM_CORE,
169*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_MASK = GENMASK(
170*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
171*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_SHIFT),
172*b633e08cSHans Petter Selasky
173*b633e08cSHans Petter Selasky UVERBS_API_OBJ_KEY_BITS = 5,
174*b633e08cSHans Petter Selasky UVERBS_API_OBJ_KEY_SHIFT =
175*b633e08cSHans Petter Selasky UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
176*b633e08cSHans Petter Selasky UVERBS_API_OBJ_KEY_NUM_CORE = 20,
177*b633e08cSHans Petter Selasky UVERBS_API_OBJ_KEY_NUM_DRIVER =
178*b633e08cSHans Petter Selasky (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
179*b633e08cSHans Petter Selasky UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
180*b633e08cSHans Petter Selasky
181*b633e08cSHans Petter Selasky /* This id guaranteed to not exist in the radix tree */
182*b633e08cSHans Petter Selasky UVERBS_API_KEY_ERR = 0xFFFFFFFF,
183*b633e08cSHans Petter Selasky };
184*b633e08cSHans Petter Selasky
uapi_key_obj(u32 id)185*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_key_obj(u32 id)
186*b633e08cSHans Petter Selasky {
187*b633e08cSHans Petter Selasky if (id & UVERBS_API_NS_FLAG) {
188*b633e08cSHans Petter Selasky id &= ~UVERBS_API_NS_FLAG;
189*b633e08cSHans Petter Selasky if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
190*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
191*b633e08cSHans Petter Selasky id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
192*b633e08cSHans Petter Selasky } else {
193*b633e08cSHans Petter Selasky if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
194*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
195*b633e08cSHans Petter Selasky }
196*b633e08cSHans Petter Selasky
197*b633e08cSHans Petter Selasky return id << UVERBS_API_OBJ_KEY_SHIFT;
198*b633e08cSHans Petter Selasky }
199*b633e08cSHans Petter Selasky
uapi_key_is_object(u32 key)200*b633e08cSHans Petter Selasky static inline __attribute_const__ bool uapi_key_is_object(u32 key)
201*b633e08cSHans Petter Selasky {
202*b633e08cSHans Petter Selasky return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
203*b633e08cSHans Petter Selasky }
204*b633e08cSHans Petter Selasky
uapi_key_ioctl_method(u32 id)205*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
206*b633e08cSHans Petter Selasky {
207*b633e08cSHans Petter Selasky if (id & UVERBS_API_NS_FLAG) {
208*b633e08cSHans Petter Selasky id &= ~UVERBS_API_NS_FLAG;
209*b633e08cSHans Petter Selasky if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
210*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
211*b633e08cSHans Petter Selasky id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
212*b633e08cSHans Petter Selasky } else {
213*b633e08cSHans Petter Selasky id++;
214*b633e08cSHans Petter Selasky if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
215*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
216*b633e08cSHans Petter Selasky }
217*b633e08cSHans Petter Selasky
218*b633e08cSHans Petter Selasky return id << UVERBS_API_METHOD_KEY_SHIFT;
219*b633e08cSHans Petter Selasky }
220*b633e08cSHans Petter Selasky
uapi_key_write_method(u32 id)221*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
222*b633e08cSHans Petter Selasky {
223*b633e08cSHans Petter Selasky if (id >= UVERBS_API_WRITE_KEY_NUM)
224*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
225*b633e08cSHans Petter Selasky return UVERBS_API_METHOD_IS_WRITE | id;
226*b633e08cSHans Petter Selasky }
227*b633e08cSHans Petter Selasky
uapi_key_write_ex_method(u32 id)228*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
229*b633e08cSHans Petter Selasky {
230*b633e08cSHans Petter Selasky if (id >= UVERBS_API_WRITE_KEY_NUM)
231*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
232*b633e08cSHans Petter Selasky return UVERBS_API_METHOD_IS_WRITE_EX | id;
233*b633e08cSHans Petter Selasky }
234*b633e08cSHans Petter Selasky
235*b633e08cSHans Petter Selasky static inline __attribute_const__ u32
uapi_key_attr_to_ioctl_method(u32 attr_key)236*b633e08cSHans Petter Selasky uapi_key_attr_to_ioctl_method(u32 attr_key)
237*b633e08cSHans Petter Selasky {
238*b633e08cSHans Petter Selasky return attr_key &
239*b633e08cSHans Petter Selasky (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
240*b633e08cSHans Petter Selasky }
241*b633e08cSHans Petter Selasky
uapi_key_is_ioctl_method(u32 key)242*b633e08cSHans Petter Selasky static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
243*b633e08cSHans Petter Selasky {
244*b633e08cSHans Petter Selasky unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
245*b633e08cSHans Petter Selasky
246*b633e08cSHans Petter Selasky return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
247*b633e08cSHans Petter Selasky (key & UVERBS_API_ATTR_KEY_MASK) == 0;
248*b633e08cSHans Petter Selasky }
249*b633e08cSHans Petter Selasky
uapi_key_is_write_method(u32 key)250*b633e08cSHans Petter Selasky static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
251*b633e08cSHans Petter Selasky {
252*b633e08cSHans Petter Selasky return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
253*b633e08cSHans Petter Selasky }
254*b633e08cSHans Petter Selasky
uapi_key_is_write_ex_method(u32 key)255*b633e08cSHans Petter Selasky static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
256*b633e08cSHans Petter Selasky {
257*b633e08cSHans Petter Selasky return (key & UVERBS_API_METHOD_KEY_MASK) ==
258*b633e08cSHans Petter Selasky UVERBS_API_METHOD_IS_WRITE_EX;
259*b633e08cSHans Petter Selasky }
260*b633e08cSHans Petter Selasky
uapi_key_attrs_start(u32 ioctl_method_key)261*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
262*b633e08cSHans Petter Selasky {
263*b633e08cSHans Petter Selasky /* 0 is the method slot itself */
264*b633e08cSHans Petter Selasky return ioctl_method_key + 1;
265*b633e08cSHans Petter Selasky }
266*b633e08cSHans Petter Selasky
uapi_key_attr(u32 id)267*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_key_attr(u32 id)
268*b633e08cSHans Petter Selasky {
269*b633e08cSHans Petter Selasky /*
270*b633e08cSHans Petter Selasky * The attr is designed to fit in the typical single radix tree node
271*b633e08cSHans Petter Selasky * of 64 entries. Since allmost all methods have driver attributes we
272*b633e08cSHans Petter Selasky * organize things so that the driver and core attributes interleave to
273*b633e08cSHans Petter Selasky * reduce the length of the attributes array in typical cases.
274*b633e08cSHans Petter Selasky */
275*b633e08cSHans Petter Selasky if (id & UVERBS_API_NS_FLAG) {
276*b633e08cSHans Petter Selasky id &= ~UVERBS_API_NS_FLAG;
277*b633e08cSHans Petter Selasky id++;
278*b633e08cSHans Petter Selasky if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
279*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
280*b633e08cSHans Petter Selasky id = (id << 1) | 0;
281*b633e08cSHans Petter Selasky } else {
282*b633e08cSHans Petter Selasky if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
283*b633e08cSHans Petter Selasky return UVERBS_API_KEY_ERR;
284*b633e08cSHans Petter Selasky id = (id << 1) | 1;
285*b633e08cSHans Petter Selasky }
286*b633e08cSHans Petter Selasky
287*b633e08cSHans Petter Selasky return id;
288*b633e08cSHans Petter Selasky }
289*b633e08cSHans Petter Selasky
290*b633e08cSHans Petter Selasky /* Only true for ioctl methods */
uapi_key_is_attr(u32 key)291*b633e08cSHans Petter Selasky static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
292*b633e08cSHans Petter Selasky {
293*b633e08cSHans Petter Selasky unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
294*b633e08cSHans Petter Selasky
295*b633e08cSHans Petter Selasky return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
296*b633e08cSHans Petter Selasky (key & UVERBS_API_ATTR_KEY_MASK) != 0;
297*b633e08cSHans Petter Selasky }
298*b633e08cSHans Petter Selasky
299*b633e08cSHans Petter Selasky /*
300*b633e08cSHans Petter Selasky * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
301*b633e08cSHans Petter Selasky * basically it undoes the reservation of 0 in the ID numbering. attr_key
302*b633e08cSHans Petter Selasky * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
303*b633e08cSHans Petter Selasky * uapi_key_attr().
304*b633e08cSHans Petter Selasky */
uapi_bkey_attr(u32 attr_key)305*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
306*b633e08cSHans Petter Selasky {
307*b633e08cSHans Petter Selasky return attr_key - 1;
308*b633e08cSHans Petter Selasky }
309*b633e08cSHans Petter Selasky
uapi_bkey_to_key_attr(u32 attr_bkey)310*b633e08cSHans Petter Selasky static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
311*b633e08cSHans Petter Selasky {
312*b633e08cSHans Petter Selasky return attr_bkey + 1;
313*b633e08cSHans Petter Selasky }
314*b633e08cSHans Petter Selasky
315*b633e08cSHans Petter Selasky /*
316*b633e08cSHans Petter Selasky * =======================================
317*b633e08cSHans Petter Selasky * Verbs definitions
318*b633e08cSHans Petter Selasky * =======================================
319*b633e08cSHans Petter Selasky */
320*b633e08cSHans Petter Selasky
321*b633e08cSHans Petter Selasky struct uverbs_attr_def {
322*b633e08cSHans Petter Selasky u16 id;
323*b633e08cSHans Petter Selasky struct uverbs_attr_spec attr;
324*b633e08cSHans Petter Selasky };
325*b633e08cSHans Petter Selasky
326*b633e08cSHans Petter Selasky struct uverbs_method_def {
327*b633e08cSHans Petter Selasky u16 id;
328*b633e08cSHans Petter Selasky /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
329*b633e08cSHans Petter Selasky u32 flags;
330*b633e08cSHans Petter Selasky size_t num_attrs;
331*b633e08cSHans Petter Selasky const struct uverbs_attr_def * const (*attrs)[];
332*b633e08cSHans Petter Selasky int (*handler)(struct uverbs_attr_bundle *attrs);
333*b633e08cSHans Petter Selasky };
334*b633e08cSHans Petter Selasky
335*b633e08cSHans Petter Selasky struct uverbs_object_def {
336*b633e08cSHans Petter Selasky u16 id;
337*b633e08cSHans Petter Selasky const struct uverbs_obj_type *type_attrs;
338*b633e08cSHans Petter Selasky size_t num_methods;
339*b633e08cSHans Petter Selasky const struct uverbs_method_def * const (*methods)[];
340*b633e08cSHans Petter Selasky };
341*b633e08cSHans Petter Selasky
342*b633e08cSHans Petter Selasky enum uapi_definition_kind {
343*b633e08cSHans Petter Selasky UAPI_DEF_END = 0,
344*b633e08cSHans Petter Selasky UAPI_DEF_OBJECT_START,
345*b633e08cSHans Petter Selasky UAPI_DEF_WRITE,
346*b633e08cSHans Petter Selasky UAPI_DEF_CHAIN_OBJ_TREE,
347*b633e08cSHans Petter Selasky UAPI_DEF_CHAIN,
348*b633e08cSHans Petter Selasky UAPI_DEF_IS_SUPPORTED_FUNC,
349*b633e08cSHans Petter Selasky UAPI_DEF_IS_SUPPORTED_DEV_FN,
350*b633e08cSHans Petter Selasky };
351*b633e08cSHans Petter Selasky
352*b633e08cSHans Petter Selasky enum uapi_definition_scope {
353*b633e08cSHans Petter Selasky UAPI_SCOPE_OBJECT = 1,
354*b633e08cSHans Petter Selasky UAPI_SCOPE_METHOD = 2,
355*b633e08cSHans Petter Selasky };
356*b633e08cSHans Petter Selasky
357*b633e08cSHans Petter Selasky struct uapi_definition {
358*b633e08cSHans Petter Selasky u8 kind;
359*b633e08cSHans Petter Selasky u8 scope;
360*b633e08cSHans Petter Selasky union {
361*b633e08cSHans Petter Selasky struct {
362*b633e08cSHans Petter Selasky u16 object_id;
363*b633e08cSHans Petter Selasky } object_start;
364*b633e08cSHans Petter Selasky struct {
365*b633e08cSHans Petter Selasky u16 command_num;
366*b633e08cSHans Petter Selasky u8 is_ex:1;
367*b633e08cSHans Petter Selasky u8 has_udata:1;
368*b633e08cSHans Petter Selasky u8 has_resp:1;
369*b633e08cSHans Petter Selasky u8 req_size;
370*b633e08cSHans Petter Selasky u8 resp_size;
371*b633e08cSHans Petter Selasky } write;
372*b633e08cSHans Petter Selasky };
373*b633e08cSHans Petter Selasky
374*b633e08cSHans Petter Selasky union {
375*b633e08cSHans Petter Selasky bool (*func_is_supported)(struct ib_device *device);
376*b633e08cSHans Petter Selasky int (*func_write)(struct uverbs_attr_bundle *attrs);
377*b633e08cSHans Petter Selasky const struct uapi_definition *chain;
378*b633e08cSHans Petter Selasky const struct uverbs_object_def *chain_obj_tree;
379*b633e08cSHans Petter Selasky size_t needs_fn_offset;
380*b633e08cSHans Petter Selasky };
381*b633e08cSHans Petter Selasky };
382*b633e08cSHans Petter Selasky
383*b633e08cSHans Petter Selasky /* Define things connected to object_id */
384*b633e08cSHans Petter Selasky #define DECLARE_UVERBS_OBJECT(_object_id, ...) \
385*b633e08cSHans Petter Selasky { \
386*b633e08cSHans Petter Selasky .kind = UAPI_DEF_OBJECT_START, \
387*b633e08cSHans Petter Selasky .object_start = { .object_id = _object_id }, \
388*b633e08cSHans Petter Selasky }, \
389*b633e08cSHans Petter Selasky ##__VA_ARGS__
390*b633e08cSHans Petter Selasky
391*b633e08cSHans Petter Selasky /* Use in a var_args of DECLARE_UVERBS_OBJECT */
392*b633e08cSHans Petter Selasky #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
393*b633e08cSHans Petter Selasky { \
394*b633e08cSHans Petter Selasky .kind = UAPI_DEF_WRITE, \
395*b633e08cSHans Petter Selasky .scope = UAPI_SCOPE_OBJECT, \
396*b633e08cSHans Petter Selasky .write = { .is_ex = 0, .command_num = _command_num }, \
397*b633e08cSHans Petter Selasky .func_write = _func, \
398*b633e08cSHans Petter Selasky _cmd_desc, \
399*b633e08cSHans Petter Selasky }, \
400*b633e08cSHans Petter Selasky ##__VA_ARGS__
401*b633e08cSHans Petter Selasky
402*b633e08cSHans Petter Selasky /* Use in a var_args of DECLARE_UVERBS_OBJECT */
403*b633e08cSHans Petter Selasky #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
404*b633e08cSHans Petter Selasky { \
405*b633e08cSHans Petter Selasky .kind = UAPI_DEF_WRITE, \
406*b633e08cSHans Petter Selasky .scope = UAPI_SCOPE_OBJECT, \
407*b633e08cSHans Petter Selasky .write = { .is_ex = 1, .command_num = _command_num }, \
408*b633e08cSHans Petter Selasky .func_write = _func, \
409*b633e08cSHans Petter Selasky _cmd_desc, \
410*b633e08cSHans Petter Selasky }, \
411*b633e08cSHans Petter Selasky ##__VA_ARGS__
412*b633e08cSHans Petter Selasky
413*b633e08cSHans Petter Selasky /*
414*b633e08cSHans Petter Selasky * Object is only supported if the function pointer named ibdev_fn in struct
415*b633e08cSHans Petter Selasky * ib_device is not NULL.
416*b633e08cSHans Petter Selasky */
417*b633e08cSHans Petter Selasky #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
418*b633e08cSHans Petter Selasky { \
419*b633e08cSHans Petter Selasky .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
420*b633e08cSHans Petter Selasky .scope = UAPI_SCOPE_OBJECT, \
421*b633e08cSHans Petter Selasky .needs_fn_offset = \
422*b633e08cSHans Petter Selasky offsetof(struct ib_device, ibdev_fn) + \
423*b633e08cSHans Petter Selasky BUILD_BUG_ON_ZERO( \
424*b633e08cSHans Petter Selasky sizeof(((struct ib_device *)0)->ibdev_fn) != \
425*b633e08cSHans Petter Selasky sizeof(void *)), \
426*b633e08cSHans Petter Selasky }
427*b633e08cSHans Petter Selasky
428*b633e08cSHans Petter Selasky /*
429*b633e08cSHans Petter Selasky * Method is only supported if the function pointer named ibdev_fn in struct
430*b633e08cSHans Petter Selasky * ib_device is not NULL.
431*b633e08cSHans Petter Selasky */
432*b633e08cSHans Petter Selasky #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
433*b633e08cSHans Petter Selasky { \
434*b633e08cSHans Petter Selasky .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
435*b633e08cSHans Petter Selasky .scope = UAPI_SCOPE_METHOD, \
436*b633e08cSHans Petter Selasky .needs_fn_offset = \
437*b633e08cSHans Petter Selasky offsetof(struct ib_device, ibdev_fn) + \
438*b633e08cSHans Petter Selasky BUILD_BUG_ON_ZERO( \
439*b633e08cSHans Petter Selasky sizeof(((struct ib_device *)0)->ibdev_fn) != \
440*b633e08cSHans Petter Selasky sizeof(void *)), \
441*b633e08cSHans Petter Selasky }
442*b633e08cSHans Petter Selasky
443*b633e08cSHans Petter Selasky /* Call a function to determine if the entire object is supported or not */
444*b633e08cSHans Petter Selasky #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
445*b633e08cSHans Petter Selasky { \
446*b633e08cSHans Petter Selasky .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
447*b633e08cSHans Petter Selasky .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
448*b633e08cSHans Petter Selasky }
449*b633e08cSHans Petter Selasky
450*b633e08cSHans Petter Selasky /* Include another struct uapi_definition in this one */
451*b633e08cSHans Petter Selasky #define UAPI_DEF_CHAIN(_def_var) \
452*b633e08cSHans Petter Selasky { \
453*b633e08cSHans Petter Selasky .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
454*b633e08cSHans Petter Selasky }
455*b633e08cSHans Petter Selasky
456*b633e08cSHans Petter Selasky /* Temporary until the tree base description is replaced */
457*b633e08cSHans Petter Selasky #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \
458*b633e08cSHans Petter Selasky { \
459*b633e08cSHans Petter Selasky .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
460*b633e08cSHans Petter Selasky .object_start = { .object_id = _object_enum }, \
461*b633e08cSHans Petter Selasky .chain_obj_tree = _object_ptr, \
462*b633e08cSHans Petter Selasky }, \
463*b633e08cSHans Petter Selasky ##__VA_ARGS__
464*b633e08cSHans Petter Selasky #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
465*b633e08cSHans Petter Selasky UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum), \
466*b633e08cSHans Petter Selasky ##__VA_ARGS__)
467*b633e08cSHans Petter Selasky
468*b633e08cSHans Petter Selasky /*
469*b633e08cSHans Petter Selasky * =======================================
470*b633e08cSHans Petter Selasky * Attribute Specifications
471*b633e08cSHans Petter Selasky * =======================================
472*b633e08cSHans Petter Selasky */
473*b633e08cSHans Petter Selasky
474*b633e08cSHans Petter Selasky #define UVERBS_ATTR_SIZE(_min_len, _len) \
475*b633e08cSHans Petter Selasky .u.ptr.min_len = _min_len, .u.ptr.len = _len
476*b633e08cSHans Petter Selasky
477*b633e08cSHans Petter Selasky #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
478*b633e08cSHans Petter Selasky
479*b633e08cSHans Petter Selasky /*
480*b633e08cSHans Petter Selasky * Specifies a uapi structure that cannot be extended. The user must always
481*b633e08cSHans Petter Selasky * supply the whole structure and nothing more. The structure must be declared
482*b633e08cSHans Petter Selasky * in a header under include/uapi/rdma.
483*b633e08cSHans Petter Selasky */
484*b633e08cSHans Petter Selasky #define UVERBS_ATTR_TYPE(_type) \
485*b633e08cSHans Petter Selasky .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
486*b633e08cSHans Petter Selasky /*
487*b633e08cSHans Petter Selasky * Specifies a uapi structure where the user must provide at least up to
488*b633e08cSHans Petter Selasky * member 'last'. Anything after last and up until the end of the structure
489*b633e08cSHans Petter Selasky * can be non-zero, anything longer than the end of the structure must be
490*b633e08cSHans Petter Selasky * zero. The structure must be declared in a header under include/uapi/rdma.
491*b633e08cSHans Petter Selasky */
492*b633e08cSHans Petter Selasky #define UVERBS_ATTR_STRUCT(_type, _last) \
493*b633e08cSHans Petter Selasky .zero_trailing = 1, \
494*b633e08cSHans Petter Selasky UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
495*b633e08cSHans Petter Selasky sizeof(_type))
496*b633e08cSHans Petter Selasky /*
497*b633e08cSHans Petter Selasky * Specifies at least min_len bytes must be passed in, but the amount can be
498*b633e08cSHans Petter Selasky * larger, up to the protocol maximum size. No check for zeroing is done.
499*b633e08cSHans Petter Selasky */
500*b633e08cSHans Petter Selasky #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
501*b633e08cSHans Petter Selasky
502*b633e08cSHans Petter Selasky /* Must be used in the '...' of any UVERBS_ATTR */
503*b633e08cSHans Petter Selasky #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
504*b633e08cSHans Petter Selasky #define UA_MANDATORY .mandatory = 1
505*b633e08cSHans Petter Selasky #define UA_OPTIONAL .mandatory = 0
506*b633e08cSHans Petter Selasky
507*b633e08cSHans Petter Selasky /*
508*b633e08cSHans Petter Selasky * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
509*b633e08cSHans Petter Selasky * READ\WRITE accesses are supported.
510*b633e08cSHans Petter Selasky */
511*b633e08cSHans Petter Selasky #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
512*b633e08cSHans Petter Selasky ...) \
513*b633e08cSHans Petter Selasky (&(const struct uverbs_attr_def){ \
514*b633e08cSHans Petter Selasky .id = (_attr_id) + \
515*b633e08cSHans Petter Selasky BUILD_BUG_ON_ZERO((_min_len) == 0 || \
516*b633e08cSHans Petter Selasky (_max_len) > \
517*b633e08cSHans Petter Selasky PAGE_SIZE / sizeof(void *) || \
518*b633e08cSHans Petter Selasky (_min_len) > (_max_len) || \
519*b633e08cSHans Petter Selasky (_access) == UVERBS_ACCESS_NEW || \
520*b633e08cSHans Petter Selasky (_access) == UVERBS_ACCESS_DESTROY), \
521*b633e08cSHans Petter Selasky .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
522*b633e08cSHans Petter Selasky .u2.objs_arr.obj_type = _idr_type, \
523*b633e08cSHans Petter Selasky .u2.objs_arr.access = _access, \
524*b633e08cSHans Petter Selasky .u2.objs_arr.min_len = _min_len, \
525*b633e08cSHans Petter Selasky .u2.objs_arr.max_len = _max_len, \
526*b633e08cSHans Petter Selasky __VA_ARGS__ } })
527*b633e08cSHans Petter Selasky
528*b633e08cSHans Petter Selasky /*
529*b633e08cSHans Petter Selasky * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
530*b633e08cSHans Petter Selasky * the user must validate the type of the uobject instead.
531*b633e08cSHans Petter Selasky */
532*b633e08cSHans Petter Selasky #define UVERBS_IDR_ANY_OBJECT 0xFFFF
533*b633e08cSHans Petter Selasky
534*b633e08cSHans Petter Selasky #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
535*b633e08cSHans Petter Selasky (&(const struct uverbs_attr_def){ \
536*b633e08cSHans Petter Selasky .id = _attr_id, \
537*b633e08cSHans Petter Selasky .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
538*b633e08cSHans Petter Selasky .u.obj.obj_type = _idr_type, \
539*b633e08cSHans Petter Selasky .u.obj.access = _access, \
540*b633e08cSHans Petter Selasky __VA_ARGS__ } })
541*b633e08cSHans Petter Selasky
542*b633e08cSHans Petter Selasky #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
543*b633e08cSHans Petter Selasky (&(const struct uverbs_attr_def){ \
544*b633e08cSHans Petter Selasky .id = (_attr_id) + \
545*b633e08cSHans Petter Selasky BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
546*b633e08cSHans Petter Selasky (_access) != UVERBS_ACCESS_READ), \
547*b633e08cSHans Petter Selasky .attr = { .type = UVERBS_ATTR_TYPE_FD, \
548*b633e08cSHans Petter Selasky .u.obj.obj_type = _fd_type, \
549*b633e08cSHans Petter Selasky .u.obj.access = _access, \
550*b633e08cSHans Petter Selasky __VA_ARGS__ } })
551*b633e08cSHans Petter Selasky
552*b633e08cSHans Petter Selasky #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
553*b633e08cSHans Petter Selasky (&(const struct uverbs_attr_def){ \
554*b633e08cSHans Petter Selasky .id = _attr_id, \
555*b633e08cSHans Petter Selasky .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
556*b633e08cSHans Petter Selasky _type, \
557*b633e08cSHans Petter Selasky __VA_ARGS__ } })
558*b633e08cSHans Petter Selasky
559*b633e08cSHans Petter Selasky #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
560*b633e08cSHans Petter Selasky (&(const struct uverbs_attr_def){ \
561*b633e08cSHans Petter Selasky .id = _attr_id, \
562*b633e08cSHans Petter Selasky .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
563*b633e08cSHans Petter Selasky _type, \
564*b633e08cSHans Petter Selasky __VA_ARGS__ } })
565*b633e08cSHans Petter Selasky
566*b633e08cSHans Petter Selasky /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
567*b633e08cSHans Petter Selasky #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
568*b633e08cSHans Petter Selasky (&(const struct uverbs_attr_def){ \
569*b633e08cSHans Petter Selasky .id = _attr_id, \
570*b633e08cSHans Petter Selasky .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
571*b633e08cSHans Petter Selasky .u2.enum_def.ids = _enum_arr, \
572*b633e08cSHans Petter Selasky .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
573*b633e08cSHans Petter Selasky __VA_ARGS__ }, \
574*b633e08cSHans Petter Selasky })
575*b633e08cSHans Petter Selasky
576*b633e08cSHans Petter Selasky /* An input value that is a member in the enum _enum_type. */
577*b633e08cSHans Petter Selasky #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
578*b633e08cSHans Petter Selasky UVERBS_ATTR_PTR_IN( \
579*b633e08cSHans Petter Selasky _attr_id, \
580*b633e08cSHans Petter Selasky UVERBS_ATTR_SIZE( \
581*b633e08cSHans Petter Selasky sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
582*b633e08cSHans Petter Selasky sizeof(u64)), \
583*b633e08cSHans Petter Selasky __VA_ARGS__)
584*b633e08cSHans Petter Selasky
585*b633e08cSHans Petter Selasky /*
586*b633e08cSHans Petter Selasky * An input value that is a bitwise combination of values of _enum_type.
587*b633e08cSHans Petter Selasky * This permits the flag value to be passed as either a u32 or u64, it must
588*b633e08cSHans Petter Selasky * be retrieved via uverbs_get_flag().
589*b633e08cSHans Petter Selasky */
590*b633e08cSHans Petter Selasky #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
591*b633e08cSHans Petter Selasky UVERBS_ATTR_PTR_IN( \
592*b633e08cSHans Petter Selasky _attr_id, \
593*b633e08cSHans Petter Selasky UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
594*b633e08cSHans Petter Selasky !sizeof(_enum_type *)), \
595*b633e08cSHans Petter Selasky sizeof(u64)), \
596*b633e08cSHans Petter Selasky __VA_ARGS__)
597*b633e08cSHans Petter Selasky
598*b633e08cSHans Petter Selasky /*
599*b633e08cSHans Petter Selasky * This spec is used in order to pass information to the hardware driver in a
600*b633e08cSHans Petter Selasky * legacy way. Every verb that could get driver specific data should get this
601*b633e08cSHans Petter Selasky * spec.
602*b633e08cSHans Petter Selasky */
603*b633e08cSHans Petter Selasky #define UVERBS_ATTR_UHW() \
604*b633e08cSHans Petter Selasky UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
605*b633e08cSHans Petter Selasky UVERBS_ATTR_MIN_SIZE(0), \
606*b633e08cSHans Petter Selasky UA_OPTIONAL, \
607*b633e08cSHans Petter Selasky .is_udata = 1), \
608*b633e08cSHans Petter Selasky UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
609*b633e08cSHans Petter Selasky UVERBS_ATTR_MIN_SIZE(0), \
610*b633e08cSHans Petter Selasky UA_OPTIONAL, \
611*b633e08cSHans Petter Selasky .is_udata = 1)
612*b633e08cSHans Petter Selasky
613*b633e08cSHans Petter Selasky /* =================================================
614*b633e08cSHans Petter Selasky * Parsing infrastructure
615*b633e08cSHans Petter Selasky * =================================================
616*b633e08cSHans Petter Selasky */
617*b633e08cSHans Petter Selasky
618*b633e08cSHans Petter Selasky
619*b633e08cSHans Petter Selasky struct uverbs_ptr_attr {
620*b633e08cSHans Petter Selasky /*
621*b633e08cSHans Petter Selasky * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
622*b633e08cSHans Petter Selasky * used.
623*b633e08cSHans Petter Selasky */
624*b633e08cSHans Petter Selasky union {
625*b633e08cSHans Petter Selasky void *ptr;
626*b633e08cSHans Petter Selasky u64 data;
627*b633e08cSHans Petter Selasky };
628*b633e08cSHans Petter Selasky u16 len;
629*b633e08cSHans Petter Selasky u16 uattr_idx;
630*b633e08cSHans Petter Selasky u8 enum_id;
631*b633e08cSHans Petter Selasky };
632*b633e08cSHans Petter Selasky
633*b633e08cSHans Petter Selasky struct uverbs_obj_attr {
634*b633e08cSHans Petter Selasky struct ib_uobject *uobject;
635*b633e08cSHans Petter Selasky const struct uverbs_api_attr *attr_elm;
636*b633e08cSHans Petter Selasky };
637*b633e08cSHans Petter Selasky
638*b633e08cSHans Petter Selasky struct uverbs_objs_arr_attr {
639*b633e08cSHans Petter Selasky struct ib_uobject **uobjects;
640*b633e08cSHans Petter Selasky u16 len;
641*b633e08cSHans Petter Selasky };
642*b633e08cSHans Petter Selasky
643*b633e08cSHans Petter Selasky struct uverbs_attr {
644*b633e08cSHans Petter Selasky union {
645*b633e08cSHans Petter Selasky struct uverbs_ptr_attr ptr_attr;
646*b633e08cSHans Petter Selasky struct uverbs_obj_attr obj_attr;
647*b633e08cSHans Petter Selasky struct uverbs_objs_arr_attr objs_arr_attr;
648*b633e08cSHans Petter Selasky };
649*b633e08cSHans Petter Selasky };
650*b633e08cSHans Petter Selasky
651*b633e08cSHans Petter Selasky struct uverbs_attr_bundle {
652*b633e08cSHans Petter Selasky struct ib_udata driver_udata;
653*b633e08cSHans Petter Selasky struct ib_udata ucore;
654*b633e08cSHans Petter Selasky struct ib_uverbs_file *ufile;
655*b633e08cSHans Petter Selasky struct ib_ucontext *context;
656*b633e08cSHans Petter Selasky DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
657*b633e08cSHans Petter Selasky struct uverbs_attr attrs[0];
658*b633e08cSHans Petter Selasky };
659*b633e08cSHans Petter Selasky
uverbs_attr_is_valid(const struct uverbs_attr_bundle * attrs_bundle,unsigned int idx)660*b633e08cSHans Petter Selasky static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
661*b633e08cSHans Petter Selasky unsigned int idx)
662*b633e08cSHans Petter Selasky {
663*b633e08cSHans Petter Selasky return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
664*b633e08cSHans Petter Selasky attrs_bundle->attr_present);
665*b633e08cSHans Petter Selasky }
666*b633e08cSHans Petter Selasky
667*b633e08cSHans Petter Selasky /**
668*b633e08cSHans Petter Selasky * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
669*b633e08cSHans Petter Selasky * ib_udata which is embedded in uverbs_attr_bundle.
670*b633e08cSHans Petter Selasky *
671*b633e08cSHans Petter Selasky * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
672*b633e08cSHans Petter Selasky * in a NULL ucontext pointer, as a safety precaution. Callers should be using
673*b633e08cSHans Petter Selasky * 'udata' to determine if the driver call is in user or kernel mode, not
674*b633e08cSHans Petter Selasky * 'ucontext'.
675*b633e08cSHans Petter Selasky *
676*b633e08cSHans Petter Selasky */
677*b633e08cSHans Petter Selasky #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
678*b633e08cSHans Petter Selasky (udata ? container_of(container_of(udata, struct uverbs_attr_bundle, \
679*b633e08cSHans Petter Selasky driver_udata) \
680*b633e08cSHans Petter Selasky ->context, \
681*b633e08cSHans Petter Selasky drv_dev_struct, member) : \
682*b633e08cSHans Petter Selasky (drv_dev_struct *)NULL)
683*b633e08cSHans Petter Selasky
684*b633e08cSHans Petter Selasky #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
685*b633e08cSHans Petter Selasky
uverbs_attr_get(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)686*b633e08cSHans Petter Selasky static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
687*b633e08cSHans Petter Selasky u16 idx)
688*b633e08cSHans Petter Selasky {
689*b633e08cSHans Petter Selasky if (!uverbs_attr_is_valid(attrs_bundle, idx))
690*b633e08cSHans Petter Selasky return ERR_PTR(-ENOENT);
691*b633e08cSHans Petter Selasky
692*b633e08cSHans Petter Selasky return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
693*b633e08cSHans Petter Selasky }
694*b633e08cSHans Petter Selasky
uverbs_attr_get_enum_id(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)695*b633e08cSHans Petter Selasky static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
696*b633e08cSHans Petter Selasky u16 idx)
697*b633e08cSHans Petter Selasky {
698*b633e08cSHans Petter Selasky const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
699*b633e08cSHans Petter Selasky
700*b633e08cSHans Petter Selasky if (IS_ERR(attr))
701*b633e08cSHans Petter Selasky return PTR_ERR(attr);
702*b633e08cSHans Petter Selasky
703*b633e08cSHans Petter Selasky return attr->ptr_attr.enum_id;
704*b633e08cSHans Petter Selasky }
705*b633e08cSHans Petter Selasky
uverbs_attr_get_obj(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)706*b633e08cSHans Petter Selasky static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
707*b633e08cSHans Petter Selasky u16 idx)
708*b633e08cSHans Petter Selasky {
709*b633e08cSHans Petter Selasky const struct uverbs_attr *attr;
710*b633e08cSHans Petter Selasky
711*b633e08cSHans Petter Selasky attr = uverbs_attr_get(attrs_bundle, idx);
712*b633e08cSHans Petter Selasky if (IS_ERR(attr))
713*b633e08cSHans Petter Selasky return ERR_CAST(attr);
714*b633e08cSHans Petter Selasky
715*b633e08cSHans Petter Selasky return attr->obj_attr.uobject->object;
716*b633e08cSHans Petter Selasky }
717*b633e08cSHans Petter Selasky
uverbs_attr_get_uobject(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)718*b633e08cSHans Petter Selasky static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
719*b633e08cSHans Petter Selasky u16 idx)
720*b633e08cSHans Petter Selasky {
721*b633e08cSHans Petter Selasky const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
722*b633e08cSHans Petter Selasky
723*b633e08cSHans Petter Selasky if (IS_ERR(attr))
724*b633e08cSHans Petter Selasky return ERR_CAST(attr);
725*b633e08cSHans Petter Selasky
726*b633e08cSHans Petter Selasky return attr->obj_attr.uobject;
727*b633e08cSHans Petter Selasky }
728*b633e08cSHans Petter Selasky
729*b633e08cSHans Petter Selasky static inline int
uverbs_attr_get_len(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)730*b633e08cSHans Petter Selasky uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
731*b633e08cSHans Petter Selasky {
732*b633e08cSHans Petter Selasky const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
733*b633e08cSHans Petter Selasky
734*b633e08cSHans Petter Selasky if (IS_ERR(attr))
735*b633e08cSHans Petter Selasky return PTR_ERR(attr);
736*b633e08cSHans Petter Selasky
737*b633e08cSHans Petter Selasky return attr->ptr_attr.len;
738*b633e08cSHans Petter Selasky }
739*b633e08cSHans Petter Selasky
740*b633e08cSHans Petter Selasky /*
741*b633e08cSHans Petter Selasky * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
742*b633e08cSHans Petter Selasky * attribute.
743*b633e08cSHans Petter Selasky * @attrs: The attribute bundle
744*b633e08cSHans Petter Selasky * @idx: The ID of the attribute
745*b633e08cSHans Petter Selasky * @elem_size: The size of the element in the array
746*b633e08cSHans Petter Selasky */
747*b633e08cSHans Petter Selasky static inline int
uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle * attrs,u16 idx,size_t elem_size)748*b633e08cSHans Petter Selasky uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
749*b633e08cSHans Petter Selasky size_t elem_size)
750*b633e08cSHans Petter Selasky {
751*b633e08cSHans Petter Selasky int size = uverbs_attr_get_len(attrs, idx);
752*b633e08cSHans Petter Selasky
753*b633e08cSHans Petter Selasky if (size < 0)
754*b633e08cSHans Petter Selasky return size;
755*b633e08cSHans Petter Selasky
756*b633e08cSHans Petter Selasky if (size % elem_size)
757*b633e08cSHans Petter Selasky return -EINVAL;
758*b633e08cSHans Petter Selasky
759*b633e08cSHans Petter Selasky return size / elem_size;
760*b633e08cSHans Petter Selasky }
761*b633e08cSHans Petter Selasky
762*b633e08cSHans Petter Selasky /**
763*b633e08cSHans Petter Selasky * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
764*b633e08cSHans Petter Selasky * UVERBS_ATTR_TYPE_IDRS_ARRAY.
765*b633e08cSHans Petter Selasky * @arr: Returned pointer to array of pointers for uobjects or NULL if
766*b633e08cSHans Petter Selasky * the attribute isn't provided.
767*b633e08cSHans Petter Selasky *
768*b633e08cSHans Petter Selasky * Return: The array length or 0 if no attribute was provided.
769*b633e08cSHans Petter Selasky */
uverbs_attr_get_uobjs_arr(const struct uverbs_attr_bundle * attrs_bundle,u16 attr_idx,struct ib_uobject *** arr)770*b633e08cSHans Petter Selasky static inline int uverbs_attr_get_uobjs_arr(
771*b633e08cSHans Petter Selasky const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
772*b633e08cSHans Petter Selasky struct ib_uobject ***arr)
773*b633e08cSHans Petter Selasky {
774*b633e08cSHans Petter Selasky const struct uverbs_attr *attr =
775*b633e08cSHans Petter Selasky uverbs_attr_get(attrs_bundle, attr_idx);
776*b633e08cSHans Petter Selasky
777*b633e08cSHans Petter Selasky if (IS_ERR(attr)) {
778*b633e08cSHans Petter Selasky *arr = NULL;
779*b633e08cSHans Petter Selasky return 0;
780*b633e08cSHans Petter Selasky }
781*b633e08cSHans Petter Selasky
782*b633e08cSHans Petter Selasky *arr = attr->objs_arr_attr.uobjects;
783*b633e08cSHans Petter Selasky
784*b633e08cSHans Petter Selasky return attr->objs_arr_attr.len;
785*b633e08cSHans Petter Selasky }
786*b633e08cSHans Petter Selasky
uverbs_attr_ptr_is_inline(const struct uverbs_attr * attr)787*b633e08cSHans Petter Selasky static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
788*b633e08cSHans Petter Selasky {
789*b633e08cSHans Petter Selasky return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
790*b633e08cSHans Petter Selasky }
791*b633e08cSHans Petter Selasky
uverbs_attr_get_alloced_ptr(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)792*b633e08cSHans Petter Selasky static inline void *uverbs_attr_get_alloced_ptr(
793*b633e08cSHans Petter Selasky const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
794*b633e08cSHans Petter Selasky {
795*b633e08cSHans Petter Selasky struct uverbs_attr *attr = __DECONST(struct uverbs_attr *, uverbs_attr_get(attrs_bundle, idx));
796*b633e08cSHans Petter Selasky
797*b633e08cSHans Petter Selasky if (IS_ERR(attr))
798*b633e08cSHans Petter Selasky return (void *)attr;
799*b633e08cSHans Petter Selasky
800*b633e08cSHans Petter Selasky return uverbs_attr_ptr_is_inline(attr) ?
801*b633e08cSHans Petter Selasky (void *)&attr->ptr_attr.data : attr->ptr_attr.ptr;
802*b633e08cSHans Petter Selasky }
803*b633e08cSHans Petter Selasky
_uverbs_copy_from(void * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,size_t size)804*b633e08cSHans Petter Selasky static inline int _uverbs_copy_from(void *to,
805*b633e08cSHans Petter Selasky const struct uverbs_attr_bundle *attrs_bundle,
806*b633e08cSHans Petter Selasky size_t idx,
807*b633e08cSHans Petter Selasky size_t size)
808*b633e08cSHans Petter Selasky {
809*b633e08cSHans Petter Selasky const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
810*b633e08cSHans Petter Selasky
811*b633e08cSHans Petter Selasky if (IS_ERR(attr))
812*b633e08cSHans Petter Selasky return PTR_ERR(attr);
813*b633e08cSHans Petter Selasky
814*b633e08cSHans Petter Selasky /*
815*b633e08cSHans Petter Selasky * Validation ensures attr->ptr_attr.len >= size. If the caller is
816*b633e08cSHans Petter Selasky * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
817*b633e08cSHans Petter Selasky * uverbs_copy_from_or_zero.
818*b633e08cSHans Petter Selasky */
819*b633e08cSHans Petter Selasky if (unlikely(size < attr->ptr_attr.len))
820*b633e08cSHans Petter Selasky return -EINVAL;
821*b633e08cSHans Petter Selasky
822*b633e08cSHans Petter Selasky if (uverbs_attr_ptr_is_inline(attr))
823*b633e08cSHans Petter Selasky memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
824*b633e08cSHans Petter Selasky else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
825*b633e08cSHans Petter Selasky attr->ptr_attr.len))
826*b633e08cSHans Petter Selasky return -EFAULT;
827*b633e08cSHans Petter Selasky
828*b633e08cSHans Petter Selasky return 0;
829*b633e08cSHans Petter Selasky }
830*b633e08cSHans Petter Selasky
_uverbs_copy_from_or_zero(void * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,size_t size)831*b633e08cSHans Petter Selasky static inline int _uverbs_copy_from_or_zero(void *to,
832*b633e08cSHans Petter Selasky const struct uverbs_attr_bundle *attrs_bundle,
833*b633e08cSHans Petter Selasky size_t idx,
834*b633e08cSHans Petter Selasky size_t size)
835*b633e08cSHans Petter Selasky {
836*b633e08cSHans Petter Selasky const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
837*b633e08cSHans Petter Selasky size_t min_size;
838*b633e08cSHans Petter Selasky
839*b633e08cSHans Petter Selasky if (IS_ERR(attr))
840*b633e08cSHans Petter Selasky return PTR_ERR(attr);
841*b633e08cSHans Petter Selasky
842*b633e08cSHans Petter Selasky min_size = min_t(size_t, size, attr->ptr_attr.len);
843*b633e08cSHans Petter Selasky
844*b633e08cSHans Petter Selasky if (uverbs_attr_ptr_is_inline(attr))
845*b633e08cSHans Petter Selasky memcpy(to, &attr->ptr_attr.data, min_size);
846*b633e08cSHans Petter Selasky else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
847*b633e08cSHans Petter Selasky min_size))
848*b633e08cSHans Petter Selasky return -EFAULT;
849*b633e08cSHans Petter Selasky
850*b633e08cSHans Petter Selasky if (size > min_size)
851*b633e08cSHans Petter Selasky memset((char *)to + min_size, 0, size - min_size);
852*b633e08cSHans Petter Selasky
853*b633e08cSHans Petter Selasky return 0;
854*b633e08cSHans Petter Selasky }
855*b633e08cSHans Petter Selasky
856*b633e08cSHans Petter Selasky #define uverbs_copy_from(to, attrs_bundle, idx) \
857*b633e08cSHans Petter Selasky _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
858*b633e08cSHans Petter Selasky
859*b633e08cSHans Petter Selasky #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
860*b633e08cSHans Petter Selasky _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
861*b633e08cSHans Petter Selasky
862*b633e08cSHans Petter Selasky static inline struct ib_ucontext *
ib_uverbs_get_ucontext(const struct uverbs_attr_bundle * attrs)863*b633e08cSHans Petter Selasky ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
864*b633e08cSHans Petter Selasky {
865*b633e08cSHans Petter Selasky return ib_uverbs_get_ucontext_file(attrs->ufile);
866*b633e08cSHans Petter Selasky }
867*b633e08cSHans Petter Selasky
868*b633e08cSHans Petter Selasky #if 1 /* IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) */
869*b633e08cSHans Petter Selasky int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
870*b633e08cSHans Petter Selasky size_t idx, u64 allowed_bits);
871*b633e08cSHans Petter Selasky int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
872*b633e08cSHans Petter Selasky size_t idx, u64 allowed_bits);
873*b633e08cSHans Petter Selasky int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
874*b633e08cSHans Petter Selasky const void *from, size_t size);
875*b633e08cSHans Petter Selasky __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
876*b633e08cSHans Petter Selasky gfp_t flags);
877*b633e08cSHans Petter Selasky
uverbs_alloc(struct uverbs_attr_bundle * bundle,size_t size)878*b633e08cSHans Petter Selasky static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
879*b633e08cSHans Petter Selasky size_t size)
880*b633e08cSHans Petter Selasky {
881*b633e08cSHans Petter Selasky return _uverbs_alloc(bundle, size, GFP_KERNEL);
882*b633e08cSHans Petter Selasky }
883*b633e08cSHans Petter Selasky
uverbs_zalloc(struct uverbs_attr_bundle * bundle,size_t size)884*b633e08cSHans Petter Selasky static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
885*b633e08cSHans Petter Selasky size_t size)
886*b633e08cSHans Petter Selasky {
887*b633e08cSHans Petter Selasky return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
888*b633e08cSHans Petter Selasky }
889*b633e08cSHans Petter Selasky int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
890*b633e08cSHans Petter Selasky size_t idx, s64 lower_bound, u64 upper_bound,
891*b633e08cSHans Petter Selasky s64 *def_val);
892*b633e08cSHans Petter Selasky int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
893*b633e08cSHans Petter Selasky size_t idx, const void *from, size_t size);
894*b633e08cSHans Petter Selasky #else
895*b633e08cSHans Petter Selasky static inline int
uverbs_get_flags64(u64 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,u64 allowed_bits)896*b633e08cSHans Petter Selasky uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
897*b633e08cSHans Petter Selasky size_t idx, u64 allowed_bits)
898*b633e08cSHans Petter Selasky {
899*b633e08cSHans Petter Selasky return -EINVAL;
900*b633e08cSHans Petter Selasky }
901*b633e08cSHans Petter Selasky static inline int
uverbs_get_flags32(u32 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,u64 allowed_bits)902*b633e08cSHans Petter Selasky uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
903*b633e08cSHans Petter Selasky size_t idx, u64 allowed_bits)
904*b633e08cSHans Petter Selasky {
905*b633e08cSHans Petter Selasky return -EINVAL;
906*b633e08cSHans Petter Selasky }
uverbs_copy_to(const struct uverbs_attr_bundle * attrs_bundle,size_t idx,const void * from,size_t size)907*b633e08cSHans Petter Selasky static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
908*b633e08cSHans Petter Selasky size_t idx, const void *from, size_t size)
909*b633e08cSHans Petter Selasky {
910*b633e08cSHans Petter Selasky return -EINVAL;
911*b633e08cSHans Petter Selasky }
uverbs_alloc(struct uverbs_attr_bundle * bundle,size_t size)912*b633e08cSHans Petter Selasky static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
913*b633e08cSHans Petter Selasky size_t size)
914*b633e08cSHans Petter Selasky {
915*b633e08cSHans Petter Selasky return ERR_PTR(-EINVAL);
916*b633e08cSHans Petter Selasky }
uverbs_zalloc(struct uverbs_attr_bundle * bundle,size_t size)917*b633e08cSHans Petter Selasky static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
918*b633e08cSHans Petter Selasky size_t size)
919*b633e08cSHans Petter Selasky {
920*b633e08cSHans Petter Selasky return ERR_PTR(-EINVAL);
921*b633e08cSHans Petter Selasky }
922*b633e08cSHans Petter Selasky static inline int
_uverbs_get_const(s64 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,s64 lower_bound,u64 upper_bound,s64 * def_val)923*b633e08cSHans Petter Selasky _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
924*b633e08cSHans Petter Selasky size_t idx, s64 lower_bound, u64 upper_bound,
925*b633e08cSHans Petter Selasky s64 *def_val)
926*b633e08cSHans Petter Selasky {
927*b633e08cSHans Petter Selasky return -EINVAL;
928*b633e08cSHans Petter Selasky }
929*b633e08cSHans Petter Selasky static inline int
uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle * bundle,size_t idx,const void * from,size_t size)930*b633e08cSHans Petter Selasky uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
931*b633e08cSHans Petter Selasky size_t idx, const void *from, size_t size)
932*b633e08cSHans Petter Selasky {
933*b633e08cSHans Petter Selasky return -EINVAL;
934*b633e08cSHans Petter Selasky }
935*b633e08cSHans Petter Selasky #endif
936*b633e08cSHans Petter Selasky
937*b633e08cSHans Petter Selasky #define uverbs_get_const(_to, _attrs_bundle, _idx) \
938*b633e08cSHans Petter Selasky ({ \
939*b633e08cSHans Petter Selasky s64 _val; \
940*b633e08cSHans Petter Selasky int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
941*b633e08cSHans Petter Selasky type_min(typeof(*_to)), \
942*b633e08cSHans Petter Selasky type_max(typeof(*_to)), NULL); \
943*b633e08cSHans Petter Selasky (*_to) = _val; \
944*b633e08cSHans Petter Selasky _ret; \
945*b633e08cSHans Petter Selasky })
946*b633e08cSHans Petter Selasky
947*b633e08cSHans Petter Selasky #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
948*b633e08cSHans Petter Selasky ({ \
949*b633e08cSHans Petter Selasky s64 _val; \
950*b633e08cSHans Petter Selasky s64 _def_val = _default; \
951*b633e08cSHans Petter Selasky int _ret = \
952*b633e08cSHans Petter Selasky _uverbs_get_const(&_val, _attrs_bundle, _idx, \
953*b633e08cSHans Petter Selasky type_min(typeof(*_to)), \
954*b633e08cSHans Petter Selasky type_max(typeof(*_to)), &_def_val); \
955*b633e08cSHans Petter Selasky (*_to) = _val; \
956*b633e08cSHans Petter Selasky _ret; \
957*b633e08cSHans Petter Selasky })
958*b633e08cSHans Petter Selasky #endif
959