Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #ifndef _UVERBS_IOCTL_ |
| 34 | #define _UVERBS_IOCTL_ |
| 35 | |
| 36 | #include <rdma/uverbs_types.h> |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 37 | #include <linux/uaccess.h> |
| 38 | #include <rdma/rdma_user_ioctl.h> |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 39 | #include <rdma/ib_user_ioctl_verbs.h> |
Matan Barak | 1f7ff9d | 2018-03-19 15:02:33 +0200 | [diff] [blame] | 40 | #include <rdma/ib_user_ioctl_cmds.h> |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * ======================================= |
| 44 | * Verbs action specifications |
| 45 | * ======================================= |
| 46 | */ |
| 47 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 48 | enum uverbs_attr_type { |
| 49 | UVERBS_ATTR_TYPE_NA, |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 50 | UVERBS_ATTR_TYPE_PTR_IN, |
| 51 | UVERBS_ATTR_TYPE_PTR_OUT, |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 52 | UVERBS_ATTR_TYPE_IDR, |
| 53 | UVERBS_ATTR_TYPE_FD, |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 54 | UVERBS_ATTR_TYPE_ENUM_IN, |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 55 | }; |
| 56 | |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 57 | enum uverbs_obj_access { |
| 58 | UVERBS_ACCESS_READ, |
| 59 | UVERBS_ACCESS_WRITE, |
| 60 | UVERBS_ACCESS_NEW, |
| 61 | UVERBS_ACCESS_DESTROY |
| 62 | }; |
| 63 | |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 64 | /* Specification of a single attribute inside the ioctl message */ |
Jason Gunthorpe | 83bb444 | 2018-07-04 08:50:29 +0300 | [diff] [blame] | 65 | /* good size 16 */ |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 66 | struct uverbs_attr_spec { |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 67 | u8 type; |
Jason Gunthorpe | 83bb444 | 2018-07-04 08:50:29 +0300 | [diff] [blame] | 68 | |
| 69 | /* |
Jason Gunthorpe | 422e3d3 | 2018-07-04 08:50:31 +0300 | [diff] [blame] | 70 | * Support extending attributes by length. Allow the user to provide |
| 71 | * more bytes than ptr.len, but check that everything after is zero'd |
| 72 | * by the user. |
Jason Gunthorpe | 83bb444 | 2018-07-04 08:50:29 +0300 | [diff] [blame] | 73 | */ |
Jason Gunthorpe | 422e3d3 | 2018-07-04 08:50:31 +0300 | [diff] [blame] | 74 | u8 zero_trailing:1; |
Jason Gunthorpe | 83bb444 | 2018-07-04 08:50:29 +0300 | [diff] [blame] | 75 | /* |
| 76 | * Valid only for PTR_IN. Allocate and copy the data inside |
| 77 | * the parser |
| 78 | */ |
| 79 | u8 alloc_and_copy:1; |
| 80 | u8 mandatory:1; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 81 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 82 | union { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 83 | struct { |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 84 | /* Current known size to kernel */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 85 | u16 len; |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 86 | /* User isn't allowed to provide something < min_len */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 87 | u16 min_len; |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 88 | } ptr; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 89 | |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 90 | struct { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 91 | /* |
| 92 | * higher bits mean the namespace and lower bits mean |
| 93 | * the type id within the namespace. |
| 94 | */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 95 | u16 obj_type; |
| 96 | u8 access; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 97 | } obj; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 98 | |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 99 | struct { |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 100 | u8 num_elems; |
| 101 | } enum_def; |
| 102 | } u; |
| 103 | |
| 104 | /* This weird split of the enum lets us remove some padding */ |
| 105 | union { |
| 106 | struct { |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 107 | /* |
| 108 | * The enum attribute can select one of the attributes |
| 109 | * contained in the ids array. Currently only PTR_IN |
| 110 | * attributes are supported in the ids array. |
| 111 | */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 112 | const struct uverbs_attr_spec *ids; |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 113 | } enum_def; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 114 | } u2; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | struct uverbs_attr_spec_hash { |
| 118 | size_t num_attrs; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 119 | unsigned long *mandatory_attrs_bitmask; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 120 | struct uverbs_attr_spec attrs[0]; |
| 121 | }; |
| 122 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 123 | struct uverbs_attr_bundle; |
| 124 | struct ib_uverbs_file; |
| 125 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 126 | struct uverbs_method_spec { |
| 127 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 128 | u32 flags; |
| 129 | size_t num_buckets; |
| 130 | size_t num_child_attrs; |
Jason Gunthorpe | e83f0ec | 2018-07-25 21:40:18 -0600 | [diff] [blame] | 131 | int (*handler)(struct ib_uverbs_file *ufile, |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 132 | struct uverbs_attr_bundle *ctx); |
| 133 | struct uverbs_attr_spec_hash *attr_buckets[0]; |
| 134 | }; |
| 135 | |
| 136 | struct uverbs_method_spec_hash { |
| 137 | size_t num_methods; |
| 138 | struct uverbs_method_spec *methods[0]; |
| 139 | }; |
| 140 | |
| 141 | struct uverbs_object_spec { |
| 142 | const struct uverbs_obj_type *type_attrs; |
| 143 | size_t num_buckets; |
| 144 | struct uverbs_method_spec_hash *method_buckets[0]; |
| 145 | }; |
| 146 | |
| 147 | struct uverbs_object_spec_hash { |
| 148 | size_t num_objects; |
| 149 | struct uverbs_object_spec *objects[0]; |
| 150 | }; |
| 151 | |
| 152 | struct uverbs_root_spec { |
| 153 | size_t num_buckets; |
| 154 | struct uverbs_object_spec_hash *object_buckets[0]; |
| 155 | }; |
| 156 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 157 | /* |
Jason Gunthorpe | 9ed3e5f | 2018-08-09 20:14:36 -0600 | [diff] [blame] | 158 | * Information about the API is loaded into a radix tree. For IOCTL we start |
| 159 | * with a tuple of: |
| 160 | * object_id, attr_id, method_id |
| 161 | * |
| 162 | * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based |
| 163 | * on the current kernel support this is compressed into 16 bit key for the |
| 164 | * radix tree. Since this compression is entirely internal to the kernel the |
| 165 | * below limits can be revised if the kernel gains additional data. |
| 166 | * |
| 167 | * With 64 leafs per node this is a 3 level radix tree. |
| 168 | * |
| 169 | * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns |
| 170 | * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. |
| 171 | */ |
| 172 | enum uapi_radix_data { |
| 173 | UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, |
| 174 | |
| 175 | UVERBS_API_ATTR_KEY_BITS = 6, |
| 176 | UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), |
| 177 | UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, |
| 178 | |
| 179 | UVERBS_API_METHOD_KEY_BITS = 5, |
| 180 | UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, |
| 181 | UVERBS_API_METHOD_KEY_NUM_CORE = 24, |
| 182 | UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) - |
| 183 | UVERBS_API_METHOD_KEY_NUM_CORE, |
| 184 | UVERBS_API_METHOD_KEY_MASK = GENMASK( |
| 185 | UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, |
| 186 | UVERBS_API_METHOD_KEY_SHIFT), |
| 187 | |
| 188 | UVERBS_API_OBJ_KEY_BITS = 5, |
| 189 | UVERBS_API_OBJ_KEY_SHIFT = |
| 190 | UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, |
| 191 | UVERBS_API_OBJ_KEY_NUM_CORE = 24, |
| 192 | UVERBS_API_OBJ_KEY_NUM_DRIVER = |
| 193 | (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, |
| 194 | UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), |
| 195 | |
| 196 | /* This id guaranteed to not exist in the radix tree */ |
| 197 | UVERBS_API_KEY_ERR = 0xFFFFFFFF, |
| 198 | }; |
| 199 | |
| 200 | static inline __attribute_const__ u32 uapi_key_obj(u32 id) |
| 201 | { |
| 202 | if (id & UVERBS_API_NS_FLAG) { |
| 203 | id &= ~UVERBS_API_NS_FLAG; |
| 204 | if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) |
| 205 | return UVERBS_API_KEY_ERR; |
| 206 | id = id + UVERBS_API_OBJ_KEY_NUM_CORE; |
| 207 | } else { |
| 208 | if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) |
| 209 | return UVERBS_API_KEY_ERR; |
| 210 | } |
| 211 | |
| 212 | return id << UVERBS_API_OBJ_KEY_SHIFT; |
| 213 | } |
| 214 | |
| 215 | static inline __attribute_const__ bool uapi_key_is_object(u32 key) |
| 216 | { |
| 217 | return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; |
| 218 | } |
| 219 | |
| 220 | static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) |
| 221 | { |
| 222 | if (id & UVERBS_API_NS_FLAG) { |
| 223 | id &= ~UVERBS_API_NS_FLAG; |
| 224 | if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) |
| 225 | return UVERBS_API_KEY_ERR; |
| 226 | id = id + UVERBS_API_METHOD_KEY_NUM_CORE; |
| 227 | } else { |
| 228 | id++; |
| 229 | if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) |
| 230 | return UVERBS_API_KEY_ERR; |
| 231 | } |
| 232 | |
| 233 | return id << UVERBS_API_METHOD_KEY_SHIFT; |
| 234 | } |
| 235 | |
| 236 | static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key) |
| 237 | { |
| 238 | return attr_key & |
| 239 | (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); |
| 240 | } |
| 241 | |
| 242 | static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) |
| 243 | { |
| 244 | return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && |
| 245 | (key & UVERBS_API_ATTR_KEY_MASK) == 0; |
| 246 | } |
| 247 | |
| 248 | static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) |
| 249 | { |
| 250 | /* 0 is the method slot itself */ |
| 251 | return ioctl_method_key + 1; |
| 252 | } |
| 253 | |
| 254 | static inline __attribute_const__ u32 uapi_key_attr(u32 id) |
| 255 | { |
| 256 | /* |
| 257 | * The attr is designed to fit in the typical single radix tree node |
| 258 | * of 64 entries. Since allmost all methods have driver attributes we |
| 259 | * organize things so that the driver and core attributes interleave to |
| 260 | * reduce the length of the attributes array in typical cases. |
| 261 | */ |
| 262 | if (id & UVERBS_API_NS_FLAG) { |
| 263 | id &= ~UVERBS_API_NS_FLAG; |
| 264 | id++; |
| 265 | if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) |
| 266 | return UVERBS_API_KEY_ERR; |
| 267 | id = (id << 1) | 0; |
| 268 | } else { |
| 269 | if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) |
| 270 | return UVERBS_API_KEY_ERR; |
| 271 | id = (id << 1) | 1; |
| 272 | } |
| 273 | |
| 274 | return id; |
| 275 | } |
| 276 | |
| 277 | static inline __attribute_const__ bool uapi_key_is_attr(u32 key) |
| 278 | { |
| 279 | return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && |
| 280 | (key & UVERBS_API_ATTR_KEY_MASK) != 0; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), |
| 285 | * basically it undoes the reservation of 0 in the ID numbering. attr_key |
| 286 | * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of |
| 287 | * uapi_key_attr(). |
| 288 | */ |
| 289 | static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) |
| 290 | { |
| 291 | return attr_key - 1; |
| 292 | } |
| 293 | |
| 294 | /* |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 295 | * ======================================= |
| 296 | * Verbs definitions |
| 297 | * ======================================= |
| 298 | */ |
| 299 | |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 300 | struct uverbs_attr_def { |
| 301 | u16 id; |
| 302 | struct uverbs_attr_spec attr; |
| 303 | }; |
| 304 | |
| 305 | struct uverbs_method_def { |
| 306 | u16 id; |
| 307 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 308 | u32 flags; |
| 309 | size_t num_attrs; |
| 310 | const struct uverbs_attr_def * const (*attrs)[]; |
Jason Gunthorpe | e83f0ec | 2018-07-25 21:40:18 -0600 | [diff] [blame] | 311 | int (*handler)(struct ib_uverbs_file *ufile, |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 312 | struct uverbs_attr_bundle *ctx); |
| 313 | }; |
| 314 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 315 | struct uverbs_object_def { |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 316 | u16 id; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 317 | const struct uverbs_obj_type *type_attrs; |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 318 | size_t num_methods; |
| 319 | const struct uverbs_method_def * const (*methods)[]; |
| 320 | }; |
| 321 | |
| 322 | struct uverbs_object_tree_def { |
| 323 | size_t num_objects; |
| 324 | const struct uverbs_object_def * const (*objects)[]; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 325 | }; |
| 326 | |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 327 | /* |
| 328 | * ======================================= |
| 329 | * Attribute Specifications |
| 330 | * ======================================= |
| 331 | */ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 332 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 333 | #define UVERBS_ATTR_SIZE(_min_len, _len) \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 334 | .u.ptr.min_len = _min_len, .u.ptr.len = _len |
Jason Gunthorpe | 422e3d3 | 2018-07-04 08:50:31 +0300 | [diff] [blame] | 335 | |
Yishai Hadas | fd44e38 | 2018-07-23 15:25:07 +0300 | [diff] [blame] | 336 | #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) |
| 337 | |
Jason Gunthorpe | 422e3d3 | 2018-07-04 08:50:31 +0300 | [diff] [blame] | 338 | /* |
| 339 | * Specifies a uapi structure that cannot be extended. The user must always |
| 340 | * supply the whole structure and nothing more. The structure must be declared |
| 341 | * in a header under include/uapi/rdma. |
| 342 | */ |
| 343 | #define UVERBS_ATTR_TYPE(_type) \ |
| 344 | .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) |
| 345 | /* |
| 346 | * Specifies a uapi structure where the user must provide at least up to |
| 347 | * member 'last'. Anything after last and up until the end of the structure |
| 348 | * can be non-zero, anything longer than the end of the structure must be |
| 349 | * zero. The structure must be declared in a header under include/uapi/rdma. |
| 350 | */ |
| 351 | #define UVERBS_ATTR_STRUCT(_type, _last) \ |
| 352 | .zero_trailing = 1, \ |
| 353 | UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ |
| 354 | sizeof(_type)) |
Jason Gunthorpe | 540cd69 | 2018-07-04 08:50:30 +0300 | [diff] [blame] | 355 | /* |
| 356 | * Specifies at least min_len bytes must be passed in, but the amount can be |
| 357 | * larger, up to the protocol maximum size. No check for zeroing is done. |
| 358 | */ |
| 359 | #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 360 | |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 361 | /* Must be used in the '...' of any UVERBS_ATTR */ |
Jason Gunthorpe | 83bb444 | 2018-07-04 08:50:29 +0300 | [diff] [blame] | 362 | #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 |
| 363 | #define UA_MANDATORY .mandatory = 1 |
Jason Gunthorpe | 83bb444 | 2018-07-04 08:50:29 +0300 | [diff] [blame] | 364 | #define UA_OPTIONAL .mandatory = 0 |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 365 | |
| 366 | #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 367 | (&(const struct uverbs_attr_def){ \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 368 | .id = _attr_id, \ |
| 369 | .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ |
| 370 | .u.obj.obj_type = _idr_type, \ |
| 371 | .u.obj.access = _access, \ |
| 372 | __VA_ARGS__ } }) |
| 373 | |
| 374 | #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 375 | (&(const struct uverbs_attr_def){ \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 376 | .id = (_attr_id) + \ |
| 377 | BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ |
| 378 | (_access) != UVERBS_ACCESS_READ), \ |
| 379 | .attr = { .type = UVERBS_ATTR_TYPE_FD, \ |
| 380 | .u.obj.obj_type = _fd_type, \ |
| 381 | .u.obj.access = _access, \ |
| 382 | __VA_ARGS__ } }) |
| 383 | |
| 384 | #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 385 | (&(const struct uverbs_attr_def){ \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 386 | .id = _attr_id, \ |
| 387 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ |
| 388 | _type, \ |
| 389 | __VA_ARGS__ } }) |
| 390 | |
| 391 | #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 392 | (&(const struct uverbs_attr_def){ \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 393 | .id = _attr_id, \ |
| 394 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ |
| 395 | _type, \ |
| 396 | __VA_ARGS__ } }) |
| 397 | |
| 398 | /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ |
| 399 | #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 400 | (&(const struct uverbs_attr_def){ \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 401 | .id = _attr_id, \ |
| 402 | .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ |
| 403 | .u2.enum_def.ids = _enum_arr, \ |
| 404 | .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ |
| 405 | __VA_ARGS__ }, \ |
| 406 | }) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 407 | |
| 408 | /* |
Jason Gunthorpe | bccd062 | 2018-07-26 16:37:14 -0600 | [diff] [blame] | 409 | * An input value that is a bitwise combination of values of _enum_type. |
| 410 | * This permits the flag value to be passed as either a u32 or u64, it must |
| 411 | * be retrieved via uverbs_get_flag(). |
| 412 | */ |
| 413 | #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ |
| 414 | UVERBS_ATTR_PTR_IN( \ |
| 415 | _attr_id, \ |
| 416 | UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ |
| 417 | !sizeof(_enum_type *)), \ |
| 418 | sizeof(u64)), \ |
| 419 | __VA_ARGS__) |
| 420 | |
| 421 | /* |
Jason Gunthorpe | 6c61d2a | 2018-07-04 08:50:27 +0300 | [diff] [blame] | 422 | * This spec is used in order to pass information to the hardware driver in a |
| 423 | * legacy way. Every verb that could get driver specific data should get this |
| 424 | * spec. |
| 425 | */ |
| 426 | #define UVERBS_ATTR_UHW() \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 427 | UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ |
Jason Gunthorpe | 540cd69 | 2018-07-04 08:50:30 +0300 | [diff] [blame] | 428 | UVERBS_ATTR_MIN_SIZE(0), \ |
| 429 | UA_OPTIONAL), \ |
Jason Gunthorpe | 9a119cd | 2018-07-04 08:50:28 +0300 | [diff] [blame] | 430 | UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ |
Jason Gunthorpe | 540cd69 | 2018-07-04 08:50:30 +0300 | [diff] [blame] | 431 | UVERBS_ATTR_MIN_SIZE(0), \ |
| 432 | UA_OPTIONAL) |
Jason Gunthorpe | 6c61d2a | 2018-07-04 08:50:27 +0300 | [diff] [blame] | 433 | |
| 434 | /* |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 435 | * ======================================= |
| 436 | * Declaration helpers |
| 437 | * ======================================= |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 438 | */ |
Jason Gunthorpe | 6c61d2a | 2018-07-04 08:50:27 +0300 | [diff] [blame] | 439 | |
| 440 | #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ |
| 441 | static const struct uverbs_object_def *const _name##_ptr[] = { \ |
| 442 | __VA_ARGS__, \ |
| 443 | }; \ |
| 444 | static const struct uverbs_object_tree_def _name = { \ |
| 445 | .num_objects = ARRAY_SIZE(_name##_ptr), \ |
| 446 | .objects = &_name##_ptr, \ |
| 447 | } |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 448 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 449 | /* ================================================= |
| 450 | * Parsing infrastructure |
| 451 | * ================================================= |
| 452 | */ |
| 453 | |
| 454 | struct uverbs_ptr_attr { |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 455 | /* |
| 456 | * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is |
| 457 | * used. |
| 458 | */ |
| 459 | union { |
| 460 | void *ptr; |
| 461 | u64 data; |
| 462 | }; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 463 | u16 len; |
Jason Gunthorpe | 6a1f444 | 2018-08-09 20:14:39 -0600 | [diff] [blame] | 464 | u16 uattr_idx; |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 465 | u8 enum_id; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 466 | }; |
| 467 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 468 | struct uverbs_obj_attr { |
| 469 | struct ib_uobject *uobject; |
| 470 | }; |
| 471 | |
| 472 | struct uverbs_attr { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 473 | union { |
| 474 | struct uverbs_ptr_attr ptr_attr; |
| 475 | struct uverbs_obj_attr obj_attr; |
| 476 | }; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | struct uverbs_attr_bundle_hash { |
| 480 | /* if bit i is set, it means attrs[i] contains valid information */ |
| 481 | unsigned long *valid_bitmap; |
| 482 | size_t num_attrs; |
| 483 | /* |
| 484 | * arrays of attributes, each element corresponds to the specification |
| 485 | * of the attribute in the same index. |
| 486 | */ |
| 487 | struct uverbs_attr *attrs; |
| 488 | }; |
| 489 | |
| 490 | struct uverbs_attr_bundle { |
Jason Gunthorpe | 4b3dd2b | 2018-08-09 20:14:38 -0600 | [diff] [blame] | 491 | struct ib_uverbs_file *ufile; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 492 | size_t num_buckets; |
| 493 | struct uverbs_attr_bundle_hash hash[]; |
| 494 | }; |
| 495 | |
| 496 | static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, |
| 497 | unsigned int idx) |
| 498 | { |
| 499 | return test_bit(idx, attrs_hash->valid_bitmap); |
| 500 | } |
| 501 | |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 502 | static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, |
| 503 | unsigned int idx) |
| 504 | { |
| 505 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; |
| 506 | |
| 507 | if (attrs_bundle->num_buckets <= idx_bucket) |
| 508 | return false; |
| 509 | |
| 510 | return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], |
| 511 | idx & ~UVERBS_ID_NS_MASK); |
| 512 | } |
| 513 | |
Matan Barak | 41b2a71 | 2018-03-19 15:02:38 +0200 | [diff] [blame] | 514 | #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) |
| 515 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 516 | static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, |
| 517 | u16 idx) |
| 518 | { |
| 519 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; |
| 520 | |
| 521 | if (!uverbs_attr_is_valid(attrs_bundle, idx)) |
| 522 | return ERR_PTR(-ENOENT); |
| 523 | |
| 524 | return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; |
| 525 | } |
| 526 | |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 527 | static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, |
| 528 | u16 idx) |
| 529 | { |
| 530 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 531 | |
| 532 | if (IS_ERR(attr)) |
| 533 | return PTR_ERR(attr); |
| 534 | |
| 535 | return attr->ptr_attr.enum_id; |
| 536 | } |
| 537 | |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 538 | static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, |
| 539 | u16 idx) |
| 540 | { |
Jason Gunthorpe | f4602cb | 2018-05-22 15:56:51 -0600 | [diff] [blame] | 541 | const struct uverbs_attr *attr; |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 542 | |
Jason Gunthorpe | f4602cb | 2018-05-22 15:56:51 -0600 | [diff] [blame] | 543 | attr = uverbs_attr_get(attrs_bundle, idx); |
| 544 | if (IS_ERR(attr)) |
| 545 | return ERR_CAST(attr); |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 546 | |
Jason Gunthorpe | f4602cb | 2018-05-22 15:56:51 -0600 | [diff] [blame] | 547 | return attr->obj_attr.uobject->object; |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 548 | } |
| 549 | |
Matan Barak | 3efa381 | 2018-05-31 16:43:28 +0300 | [diff] [blame] | 550 | static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, |
| 551 | u16 idx) |
| 552 | { |
| 553 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 554 | |
| 555 | if (IS_ERR(attr)) |
| 556 | return ERR_CAST(attr); |
| 557 | |
| 558 | return attr->obj_attr.uobject; |
| 559 | } |
| 560 | |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 561 | static inline int |
| 562 | uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) |
| 563 | { |
| 564 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 565 | |
| 566 | if (IS_ERR(attr)) |
| 567 | return PTR_ERR(attr); |
| 568 | |
| 569 | return attr->ptr_attr.len; |
| 570 | } |
| 571 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 572 | static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) |
| 573 | { |
| 574 | return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); |
| 575 | } |
| 576 | |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 577 | static inline void *uverbs_attr_get_alloced_ptr( |
| 578 | const struct uverbs_attr_bundle *attrs_bundle, u16 idx) |
| 579 | { |
| 580 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 581 | |
| 582 | if (IS_ERR(attr)) |
| 583 | return (void *)attr; |
| 584 | |
| 585 | return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : |
| 586 | attr->ptr_attr.ptr; |
| 587 | } |
| 588 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 589 | static inline int _uverbs_copy_from(void *to, |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 590 | const struct uverbs_attr_bundle *attrs_bundle, |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 591 | size_t idx, |
| 592 | size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 593 | { |
| 594 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 595 | |
| 596 | if (IS_ERR(attr)) |
| 597 | return PTR_ERR(attr); |
| 598 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 599 | /* |
| 600 | * Validation ensures attr->ptr_attr.len >= size. If the caller is |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 601 | * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call |
| 602 | * uverbs_copy_from_or_zero. |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 603 | */ |
| 604 | if (unlikely(size < attr->ptr_attr.len)) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | if (uverbs_attr_ptr_is_inline(attr)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 608 | memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 609 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 610 | attr->ptr_attr.len)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 611 | return -EFAULT; |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 616 | static inline int _uverbs_copy_from_or_zero(void *to, |
| 617 | const struct uverbs_attr_bundle *attrs_bundle, |
| 618 | size_t idx, |
| 619 | size_t size) |
| 620 | { |
| 621 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 622 | size_t min_size; |
| 623 | |
| 624 | if (IS_ERR(attr)) |
| 625 | return PTR_ERR(attr); |
| 626 | |
| 627 | min_size = min_t(size_t, size, attr->ptr_attr.len); |
| 628 | |
| 629 | if (uverbs_attr_ptr_is_inline(attr)) |
| 630 | memcpy(to, &attr->ptr_attr.data, min_size); |
| 631 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 632 | min_size)) |
| 633 | return -EFAULT; |
| 634 | |
| 635 | if (size > min_size) |
| 636 | memset(to + min_size, 0, size - min_size); |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 641 | #define uverbs_copy_from(to, attrs_bundle, idx) \ |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 642 | _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 643 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 644 | #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ |
| 645 | _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) |
| 646 | |
Jason Gunthorpe | bccd062 | 2018-07-26 16:37:14 -0600 | [diff] [blame] | 647 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
| 648 | int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 649 | size_t idx, u64 allowed_bits); |
| 650 | int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 651 | size_t idx, u64 allowed_bits); |
Jason Gunthorpe | 6a1f444 | 2018-08-09 20:14:39 -0600 | [diff] [blame] | 652 | int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, |
| 653 | const void *from, size_t size); |
Jason Gunthorpe | 461bb2e | 2018-08-09 20:14:40 -0600 | [diff] [blame^] | 654 | __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, |
| 655 | gfp_t flags); |
| 656 | |
| 657 | static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, |
| 658 | size_t size) |
| 659 | { |
| 660 | return _uverbs_alloc(bundle, size, GFP_KERNEL); |
| 661 | } |
| 662 | |
| 663 | static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, |
| 664 | size_t size) |
| 665 | { |
| 666 | return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); |
| 667 | } |
Jason Gunthorpe | bccd062 | 2018-07-26 16:37:14 -0600 | [diff] [blame] | 668 | #else |
| 669 | static inline int |
| 670 | uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 671 | size_t idx, u64 allowed_bits) |
| 672 | { |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | static inline int |
| 676 | uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 677 | size_t idx, u64 allowed_bits) |
| 678 | { |
| 679 | return -EINVAL; |
| 680 | } |
Jason Gunthorpe | 6a1f444 | 2018-08-09 20:14:39 -0600 | [diff] [blame] | 681 | static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, |
| 682 | size_t idx, const void *from, size_t size) |
| 683 | { |
| 684 | return -EINVAL; |
| 685 | } |
Jason Gunthorpe | 461bb2e | 2018-08-09 20:14:40 -0600 | [diff] [blame^] | 686 | static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, |
| 687 | size_t size) |
| 688 | { |
| 689 | return ERR_PTR(-EINVAL); |
| 690 | } |
| 691 | static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, |
| 692 | size_t size) |
| 693 | { |
| 694 | return ERR_PTR(-EINVAL); |
| 695 | } |
Jason Gunthorpe | bccd062 | 2018-07-26 16:37:14 -0600 | [diff] [blame] | 696 | #endif |
| 697 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 698 | /* ================================================= |
| 699 | * Definitions -> Specs infrastructure |
| 700 | * ================================================= |
| 701 | */ |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 702 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 703 | /* |
| 704 | * uverbs_alloc_spec_tree - Merges different common and driver specific feature |
| 705 | * into one parsing tree that every uverbs command will be parsed upon. |
| 706 | * |
| 707 | * @num_trees: Number of trees in the array @trees. |
| 708 | * @trees: Array of pointers to tree root definitions to merge. Each such tree |
| 709 | * possibly contains objects, methods and attributes definitions. |
| 710 | * |
| 711 | * Returns: |
| 712 | * uverbs_root_spec *: The root of the merged parsing tree. |
| 713 | * On error, we return an error code. Error is checked via IS_ERR. |
| 714 | * |
| 715 | * The following merges could take place: |
| 716 | * a. Two trees representing the same method with different handler |
| 717 | * -> We take the handler of the tree that its handler != NULL |
| 718 | * and its index in the trees array is greater. The incentive for that |
| 719 | * is that developers are expected to first merge common trees and then |
| 720 | * merge trees that gives specialized the behaviour. |
| 721 | * b. Two trees representing the same object with different |
| 722 | * type_attrs (struct uverbs_obj_type): |
| 723 | * -> We take the type_attrs of the tree that its type_attr != NULL |
| 724 | * and its index in the trees array is greater. This could be used |
| 725 | * in order to override the free function, allocation size, etc. |
| 726 | * c. Two trees representing the same method attribute (same id but possibly |
| 727 | * different attributes): |
| 728 | * -> ERROR (-ENOENT), we believe that's not the programmer's intent. |
| 729 | * |
| 730 | * An object without any methods is considered invalid and will abort the |
| 731 | * function with -ENOENT error. |
| 732 | */ |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 733 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 734 | struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 735 | const struct uverbs_object_tree_def **trees); |
| 736 | void uverbs_free_spec_tree(struct uverbs_root_spec *root); |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 737 | #else |
| 738 | static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 739 | const struct uverbs_object_tree_def **trees) |
| 740 | { |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) |
| 745 | { |
| 746 | } |
| 747 | #endif |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 748 | |
| 749 | #endif |