1a0aa309cSMatan Barak /* 2a0aa309cSMatan Barak * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. 3a0aa309cSMatan Barak * 4a0aa309cSMatan Barak * This software is available to you under a choice of one of two 5a0aa309cSMatan Barak * licenses. You may choose to be licensed under the terms of the GNU 6a0aa309cSMatan Barak * General Public License (GPL) Version 2, available from the file 7a0aa309cSMatan Barak * COPYING in the main directory of this source tree, or the 8a0aa309cSMatan Barak * OpenIB.org BSD license below: 9a0aa309cSMatan Barak * 10a0aa309cSMatan Barak * Redistribution and use in source and binary forms, with or 11a0aa309cSMatan Barak * without modification, are permitted provided that the following 12a0aa309cSMatan Barak * conditions are met: 13a0aa309cSMatan Barak * 14a0aa309cSMatan Barak * - Redistributions of source code must retain the above 15a0aa309cSMatan Barak * copyright notice, this list of conditions and the following 16a0aa309cSMatan Barak * disclaimer. 17a0aa309cSMatan Barak * 18a0aa309cSMatan Barak * - Redistributions in binary form must reproduce the above 19a0aa309cSMatan Barak * copyright notice, this list of conditions and the following 20a0aa309cSMatan Barak * disclaimer in the documentation and/or other materials 21a0aa309cSMatan Barak * provided with the distribution. 22a0aa309cSMatan Barak * 23a0aa309cSMatan Barak * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24a0aa309cSMatan Barak * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25a0aa309cSMatan Barak * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26a0aa309cSMatan Barak * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27a0aa309cSMatan Barak * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28a0aa309cSMatan Barak * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29a0aa309cSMatan Barak * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30a0aa309cSMatan Barak * SOFTWARE. 31a0aa309cSMatan Barak */ 32a0aa309cSMatan Barak 33a0aa309cSMatan Barak #ifndef _UVERBS_IOCTL_ 34a0aa309cSMatan Barak #define _UVERBS_IOCTL_ 35a0aa309cSMatan Barak 36a0aa309cSMatan Barak #include <rdma/uverbs_types.h> 3735410306SMatan Barak #include <linux/uaccess.h> 3835410306SMatan Barak #include <rdma/rdma_user_ioctl.h> 39d70724f1SMatan Barak #include <rdma/ib_user_ioctl_verbs.h> 401f7ff9d5SMatan Barak #include <rdma/ib_user_ioctl_cmds.h> 41a0aa309cSMatan Barak 42a0aa309cSMatan Barak /* 43a0aa309cSMatan Barak * ======================================= 44a0aa309cSMatan Barak * Verbs action specifications 45a0aa309cSMatan Barak * ======================================= 46a0aa309cSMatan Barak */ 47a0aa309cSMatan Barak 48f43dbebfSMatan Barak enum uverbs_attr_type { 49f43dbebfSMatan Barak UVERBS_ATTR_TYPE_NA, 50fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_IN, 51fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_OUT, 52f43dbebfSMatan Barak UVERBS_ATTR_TYPE_IDR, 53f43dbebfSMatan Barak UVERBS_ATTR_TYPE_FD, 54494c5580SMatan Barak UVERBS_ATTR_TYPE_ENUM_IN, 55f43dbebfSMatan Barak }; 56f43dbebfSMatan Barak 57a0aa309cSMatan Barak enum uverbs_obj_access { 58a0aa309cSMatan Barak UVERBS_ACCESS_READ, 59a0aa309cSMatan Barak UVERBS_ACCESS_WRITE, 60a0aa309cSMatan Barak UVERBS_ACCESS_NEW, 61a0aa309cSMatan Barak UVERBS_ACCESS_DESTROY 62a0aa309cSMatan Barak }; 63a0aa309cSMatan Barak 641f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */ 6583bb4442SJason Gunthorpe /* good size 16 */ 66f43dbebfSMatan Barak struct uverbs_attr_spec { 67d108dac0SJason Gunthorpe u8 type; 6883bb4442SJason Gunthorpe 6983bb4442SJason Gunthorpe /* 70422e3d37SJason Gunthorpe * Support extending attributes by length. Allow the user to provide 71422e3d37SJason Gunthorpe * more bytes than ptr.len, but check that everything after is zero'd 72422e3d37SJason Gunthorpe * by the user. 7383bb4442SJason Gunthorpe */ 74422e3d37SJason Gunthorpe u8 zero_trailing:1; 7583bb4442SJason Gunthorpe /* 7683bb4442SJason Gunthorpe * Valid only for PTR_IN. Allocate and copy the data inside 7783bb4442SJason Gunthorpe * the parser 7883bb4442SJason Gunthorpe */ 7983bb4442SJason Gunthorpe u8 alloc_and_copy:1; 8083bb4442SJason Gunthorpe u8 mandatory:1; 81d108dac0SJason Gunthorpe 82fac9658cSMatan Barak union { 83f43dbebfSMatan Barak struct { 84c66db311SMatan Barak /* Current known size to kernel */ 851f07e08fSMatan Barak u16 len; 86c66db311SMatan Barak /* User isn't allowed to provide something < min_len */ 87c66db311SMatan Barak u16 min_len; 881f07e08fSMatan Barak } ptr; 89d108dac0SJason Gunthorpe 901f07e08fSMatan Barak struct { 91f43dbebfSMatan Barak /* 92f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean 93f43dbebfSMatan Barak * the type id within the namespace. 94f43dbebfSMatan Barak */ 95f43dbebfSMatan Barak u16 obj_type; 96f43dbebfSMatan Barak u8 access; 97f43dbebfSMatan Barak } obj; 98d108dac0SJason Gunthorpe 99494c5580SMatan Barak struct { 100494c5580SMatan Barak u8 num_elems; 101d108dac0SJason Gunthorpe } enum_def; 102d108dac0SJason Gunthorpe } u; 103d108dac0SJason Gunthorpe 104d108dac0SJason Gunthorpe /* This weird split of the enum lets us remove some padding */ 105d108dac0SJason Gunthorpe union { 106d108dac0SJason Gunthorpe struct { 107494c5580SMatan Barak /* 108494c5580SMatan Barak * The enum attribute can select one of the attributes 109494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN 110494c5580SMatan Barak * attributes are supported in the ids array. 111494c5580SMatan Barak */ 112494c5580SMatan Barak const struct uverbs_attr_spec *ids; 113494c5580SMatan Barak } enum_def; 114d108dac0SJason Gunthorpe } u2; 115fac9658cSMatan Barak }; 116f43dbebfSMatan Barak 117f43dbebfSMatan Barak struct uverbs_attr_spec_hash { 118f43dbebfSMatan Barak size_t num_attrs; 119fac9658cSMatan Barak unsigned long *mandatory_attrs_bitmask; 120f43dbebfSMatan Barak struct uverbs_attr_spec attrs[0]; 121f43dbebfSMatan Barak }; 122f43dbebfSMatan Barak 123fac9658cSMatan Barak struct uverbs_attr_bundle; 124fac9658cSMatan Barak struct ib_uverbs_file; 125fac9658cSMatan Barak 126fac9658cSMatan Barak enum { 127fac9658cSMatan Barak /* 128fac9658cSMatan Barak * Action marked with this flag creates a context (or root for all 129fac9658cSMatan Barak * objects). 130fac9658cSMatan Barak */ 131fac9658cSMatan Barak UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0, 132fac9658cSMatan Barak }; 133fac9658cSMatan Barak 134fac9658cSMatan Barak struct uverbs_method_spec { 135fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 136fac9658cSMatan Barak u32 flags; 137fac9658cSMatan Barak size_t num_buckets; 138fac9658cSMatan Barak size_t num_child_attrs; 139fac9658cSMatan Barak int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, 140fac9658cSMatan Barak struct uverbs_attr_bundle *ctx); 141fac9658cSMatan Barak struct uverbs_attr_spec_hash *attr_buckets[0]; 142fac9658cSMatan Barak }; 143fac9658cSMatan Barak 144fac9658cSMatan Barak struct uverbs_method_spec_hash { 145fac9658cSMatan Barak size_t num_methods; 146fac9658cSMatan Barak struct uverbs_method_spec *methods[0]; 147fac9658cSMatan Barak }; 148fac9658cSMatan Barak 149fac9658cSMatan Barak struct uverbs_object_spec { 150fac9658cSMatan Barak const struct uverbs_obj_type *type_attrs; 151fac9658cSMatan Barak size_t num_buckets; 152fac9658cSMatan Barak struct uverbs_method_spec_hash *method_buckets[0]; 153fac9658cSMatan Barak }; 154fac9658cSMatan Barak 155fac9658cSMatan Barak struct uverbs_object_spec_hash { 156fac9658cSMatan Barak size_t num_objects; 157fac9658cSMatan Barak struct uverbs_object_spec *objects[0]; 158fac9658cSMatan Barak }; 159fac9658cSMatan Barak 160fac9658cSMatan Barak struct uverbs_root_spec { 161fac9658cSMatan Barak size_t num_buckets; 162fac9658cSMatan Barak struct uverbs_object_spec_hash *object_buckets[0]; 163fac9658cSMatan Barak }; 164fac9658cSMatan Barak 1655009010fSMatan Barak /* 1665009010fSMatan Barak * ======================================= 1675009010fSMatan Barak * Verbs definitions 1685009010fSMatan Barak * ======================================= 1695009010fSMatan Barak */ 1705009010fSMatan Barak 17109e3ebf8SMatan Barak struct uverbs_attr_def { 17209e3ebf8SMatan Barak u16 id; 17309e3ebf8SMatan Barak struct uverbs_attr_spec attr; 17409e3ebf8SMatan Barak }; 17509e3ebf8SMatan Barak 17609e3ebf8SMatan Barak struct uverbs_method_def { 17709e3ebf8SMatan Barak u16 id; 17809e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 17909e3ebf8SMatan Barak u32 flags; 18009e3ebf8SMatan Barak size_t num_attrs; 18109e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[]; 18209e3ebf8SMatan Barak int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, 18309e3ebf8SMatan Barak struct uverbs_attr_bundle *ctx); 18409e3ebf8SMatan Barak }; 18509e3ebf8SMatan Barak 1865009010fSMatan Barak struct uverbs_object_def { 18709e3ebf8SMatan Barak u16 id; 1885009010fSMatan Barak const struct uverbs_obj_type *type_attrs; 18909e3ebf8SMatan Barak size_t num_methods; 19009e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[]; 19109e3ebf8SMatan Barak }; 19209e3ebf8SMatan Barak 19309e3ebf8SMatan Barak struct uverbs_object_tree_def { 19409e3ebf8SMatan Barak size_t num_objects; 19509e3ebf8SMatan Barak const struct uverbs_object_def * const (*objects)[]; 1965009010fSMatan Barak }; 1975009010fSMatan Barak 198d108dac0SJason Gunthorpe /* 199d108dac0SJason Gunthorpe * ======================================= 200d108dac0SJason Gunthorpe * Attribute Specifications 201d108dac0SJason Gunthorpe * ======================================= 202d108dac0SJason Gunthorpe */ 203c66db311SMatan Barak 204c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \ 205d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len 206422e3d37SJason Gunthorpe 207*fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) 208*fd44e385SYishai Hadas 209422e3d37SJason Gunthorpe /* 210422e3d37SJason Gunthorpe * Specifies a uapi structure that cannot be extended. The user must always 211422e3d37SJason Gunthorpe * supply the whole structure and nothing more. The structure must be declared 212422e3d37SJason Gunthorpe * in a header under include/uapi/rdma. 213422e3d37SJason Gunthorpe */ 214422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type) \ 215422e3d37SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 216422e3d37SJason Gunthorpe /* 217422e3d37SJason Gunthorpe * Specifies a uapi structure where the user must provide at least up to 218422e3d37SJason Gunthorpe * member 'last'. Anything after last and up until the end of the structure 219422e3d37SJason Gunthorpe * can be non-zero, anything longer than the end of the structure must be 220422e3d37SJason Gunthorpe * zero. The structure must be declared in a header under include/uapi/rdma. 221422e3d37SJason Gunthorpe */ 222422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last) \ 223422e3d37SJason Gunthorpe .zero_trailing = 1, \ 224422e3d37SJason Gunthorpe UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ 225422e3d37SJason Gunthorpe sizeof(_type)) 226540cd692SJason Gunthorpe /* 227540cd692SJason Gunthorpe * Specifies at least min_len bytes must be passed in, but the amount can be 228540cd692SJason Gunthorpe * larger, up to the protocol maximum size. No check for zeroing is done. 229540cd692SJason Gunthorpe */ 230540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 231c66db311SMatan Barak 232d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 23383bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 23483bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1 23583bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0 23635410306SMatan Barak 237d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 2389a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 239d108dac0SJason Gunthorpe .id = _attr_id, \ 240d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 241d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 242d108dac0SJason Gunthorpe .u.obj.access = _access, \ 243d108dac0SJason Gunthorpe __VA_ARGS__ } }) 244d108dac0SJason Gunthorpe 245d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 2469a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 247d108dac0SJason Gunthorpe .id = (_attr_id) + \ 248d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 24935410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 250d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 251d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 252d108dac0SJason Gunthorpe .u.obj.access = _access, \ 253d108dac0SJason Gunthorpe __VA_ARGS__ } }) 25435410306SMatan Barak 255d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 2569a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 257d108dac0SJason Gunthorpe .id = _attr_id, \ 258d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 259d108dac0SJason Gunthorpe _type, \ 260d108dac0SJason Gunthorpe __VA_ARGS__ } }) 261d108dac0SJason Gunthorpe 262d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 2639a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 264d108dac0SJason Gunthorpe .id = _attr_id, \ 265d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 266d108dac0SJason Gunthorpe _type, \ 267d108dac0SJason Gunthorpe __VA_ARGS__ } }) 268d108dac0SJason Gunthorpe 269d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 270d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 2719a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 272d108dac0SJason Gunthorpe .id = _attr_id, \ 273d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 274d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 275d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 276d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 277d108dac0SJason Gunthorpe }) 278d108dac0SJason Gunthorpe 279d108dac0SJason Gunthorpe /* 2806c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 2816c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 2826c61d2a5SJason Gunthorpe * spec. 2836c61d2a5SJason Gunthorpe */ 2846c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 2859a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 286540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 287540cd692SJason Gunthorpe UA_OPTIONAL), \ 2889a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 289540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 290540cd692SJason Gunthorpe UA_OPTIONAL) 2916c61d2a5SJason Gunthorpe 2926c61d2a5SJason Gunthorpe /* 293d108dac0SJason Gunthorpe * ======================================= 294d108dac0SJason Gunthorpe * Declaration helpers 295d108dac0SJason Gunthorpe * ======================================= 296d108dac0SJason Gunthorpe */ 2976c61d2a5SJason Gunthorpe 29809e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ 2996c61d2a5SJason Gunthorpe static const struct uverbs_object_def *const _name##_ptr[] = { \ 3006c61d2a5SJason Gunthorpe __VA_ARGS__, \ 3016c61d2a5SJason Gunthorpe }; \ 3026c61d2a5SJason Gunthorpe static const struct uverbs_object_tree_def _name = { \ 3036c61d2a5SJason Gunthorpe .num_objects = ARRAY_SIZE(_name##_ptr), \ 3046c61d2a5SJason Gunthorpe .objects = &_name##_ptr, \ 3056c61d2a5SJason Gunthorpe } 30609e3ebf8SMatan Barak 307fac9658cSMatan Barak /* ================================================= 308fac9658cSMatan Barak * Parsing infrastructure 309fac9658cSMatan Barak * ================================================= 310fac9658cSMatan Barak */ 311fac9658cSMatan Barak 312fac9658cSMatan Barak struct uverbs_ptr_attr { 3138762d149SMatan Barak /* 3148762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 3158762d149SMatan Barak * used. 3168762d149SMatan Barak */ 3178762d149SMatan Barak union { 3188762d149SMatan Barak void *ptr; 319fac9658cSMatan Barak u64 data; 3208762d149SMatan Barak }; 321fac9658cSMatan Barak u16 len; 322fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ 323fac9658cSMatan Barak u16 flags; 324494c5580SMatan Barak u8 enum_id; 325fac9658cSMatan Barak }; 326fac9658cSMatan Barak 327f43dbebfSMatan Barak struct uverbs_obj_attr { 328f43dbebfSMatan Barak struct ib_uobject *uobject; 329f43dbebfSMatan Barak }; 330f43dbebfSMatan Barak 331f43dbebfSMatan Barak struct uverbs_attr { 332fac9658cSMatan Barak /* 333fac9658cSMatan Barak * pointer to the user-space given attribute, in order to write the 334fac9658cSMatan Barak * new uobject's id or update flags. 335fac9658cSMatan Barak */ 336fac9658cSMatan Barak struct ib_uverbs_attr __user *uattr; 337fac9658cSMatan Barak union { 338fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 339f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 340f43dbebfSMatan Barak }; 341fac9658cSMatan Barak }; 342f43dbebfSMatan Barak 343f43dbebfSMatan Barak struct uverbs_attr_bundle_hash { 344f43dbebfSMatan Barak /* if bit i is set, it means attrs[i] contains valid information */ 345f43dbebfSMatan Barak unsigned long *valid_bitmap; 346f43dbebfSMatan Barak size_t num_attrs; 347f43dbebfSMatan Barak /* 348f43dbebfSMatan Barak * arrays of attributes, each element corresponds to the specification 349f43dbebfSMatan Barak * of the attribute in the same index. 350f43dbebfSMatan Barak */ 351f43dbebfSMatan Barak struct uverbs_attr *attrs; 352f43dbebfSMatan Barak }; 353f43dbebfSMatan Barak 354f43dbebfSMatan Barak struct uverbs_attr_bundle { 355f43dbebfSMatan Barak size_t num_buckets; 356f43dbebfSMatan Barak struct uverbs_attr_bundle_hash hash[]; 357f43dbebfSMatan Barak }; 358f43dbebfSMatan Barak 359f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, 360f43dbebfSMatan Barak unsigned int idx) 361f43dbebfSMatan Barak { 362f43dbebfSMatan Barak return test_bit(idx, attrs_hash->valid_bitmap); 363f43dbebfSMatan Barak } 364f43dbebfSMatan Barak 36535410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 36635410306SMatan Barak unsigned int idx) 36735410306SMatan Barak { 36835410306SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 36935410306SMatan Barak 37035410306SMatan Barak if (attrs_bundle->num_buckets <= idx_bucket) 37135410306SMatan Barak return false; 37235410306SMatan Barak 37335410306SMatan Barak return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], 37435410306SMatan Barak idx & ~UVERBS_ID_NS_MASK); 37535410306SMatan Barak } 37635410306SMatan Barak 37741b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 37841b2a71fSMatan Barak 379d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 380d70724f1SMatan Barak u16 idx) 381d70724f1SMatan Barak { 382d70724f1SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 383d70724f1SMatan Barak 384d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 385d70724f1SMatan Barak return ERR_PTR(-ENOENT); 386d70724f1SMatan Barak 387d70724f1SMatan Barak return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; 388d70724f1SMatan Barak } 389d70724f1SMatan Barak 390494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 391494c5580SMatan Barak u16 idx) 392494c5580SMatan Barak { 393494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 394494c5580SMatan Barak 395494c5580SMatan Barak if (IS_ERR(attr)) 396494c5580SMatan Barak return PTR_ERR(attr); 397494c5580SMatan Barak 398494c5580SMatan Barak return attr->ptr_attr.enum_id; 399494c5580SMatan Barak } 400494c5580SMatan Barak 401be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 402be934ccaSAriel Levkovich u16 idx) 403be934ccaSAriel Levkovich { 404f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 405be934ccaSAriel Levkovich 406f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 407f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 408f4602cbbSJason Gunthorpe return ERR_CAST(attr); 409be934ccaSAriel Levkovich 410f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 411be934ccaSAriel Levkovich } 412be934ccaSAriel Levkovich 4133efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 4143efa3812SMatan Barak u16 idx) 4153efa3812SMatan Barak { 4163efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4173efa3812SMatan Barak 4183efa3812SMatan Barak if (IS_ERR(attr)) 4193efa3812SMatan Barak return ERR_CAST(attr); 4203efa3812SMatan Barak 4213efa3812SMatan Barak return attr->obj_attr.uobject; 4223efa3812SMatan Barak } 4233efa3812SMatan Barak 4248762d149SMatan Barak static inline int 4258762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4268762d149SMatan Barak { 4278762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4288762d149SMatan Barak 4298762d149SMatan Barak if (IS_ERR(attr)) 4308762d149SMatan Barak return PTR_ERR(attr); 4318762d149SMatan Barak 4328762d149SMatan Barak return attr->ptr_attr.len; 4338762d149SMatan Barak } 4348762d149SMatan Barak 435d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 43689d9e8d3SMatan Barak size_t idx, const void *from, size_t size) 437d70724f1SMatan Barak { 438d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 439d70724f1SMatan Barak u16 flags; 44089d9e8d3SMatan Barak size_t min_size; 441d70724f1SMatan Barak 442d70724f1SMatan Barak if (IS_ERR(attr)) 443d70724f1SMatan Barak return PTR_ERR(attr); 444d70724f1SMatan Barak 44589d9e8d3SMatan Barak min_size = min_t(size_t, attr->ptr_attr.len, size); 4462f36028cSJason Gunthorpe if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) 44789d9e8d3SMatan Barak return -EFAULT; 44889d9e8d3SMatan Barak 449d70724f1SMatan Barak flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; 45089d9e8d3SMatan Barak if (put_user(flags, &attr->uattr->flags)) 45189d9e8d3SMatan Barak return -EFAULT; 45289d9e8d3SMatan Barak 45389d9e8d3SMatan Barak return 0; 454d70724f1SMatan Barak } 455d70724f1SMatan Barak 45689d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 45789d9e8d3SMatan Barak { 45889d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 45989d9e8d3SMatan Barak } 46089d9e8d3SMatan Barak 4618762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 4628762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4638762d149SMatan Barak { 4648762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4658762d149SMatan Barak 4668762d149SMatan Barak if (IS_ERR(attr)) 4678762d149SMatan Barak return (void *)attr; 4688762d149SMatan Barak 4698762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 4708762d149SMatan Barak attr->ptr_attr.ptr; 4718762d149SMatan Barak } 4728762d149SMatan Barak 47389d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 474d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 47589d9e8d3SMatan Barak size_t idx, 47689d9e8d3SMatan Barak size_t size) 477d70724f1SMatan Barak { 478d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 479d70724f1SMatan Barak 480d70724f1SMatan Barak if (IS_ERR(attr)) 481d70724f1SMatan Barak return PTR_ERR(attr); 482d70724f1SMatan Barak 48389d9e8d3SMatan Barak /* 48489d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 485c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 486c66db311SMatan Barak * uverbs_copy_from_or_zero. 48789d9e8d3SMatan Barak */ 48889d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 48989d9e8d3SMatan Barak return -EINVAL; 49089d9e8d3SMatan Barak 49189d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 492d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 4932f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 4942f36028cSJason Gunthorpe attr->ptr_attr.len)) 495d70724f1SMatan Barak return -EFAULT; 496d70724f1SMatan Barak 497d70724f1SMatan Barak return 0; 498d70724f1SMatan Barak } 499d70724f1SMatan Barak 500c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 501c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 502c66db311SMatan Barak size_t idx, 503c66db311SMatan Barak size_t size) 504c66db311SMatan Barak { 505c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 506c66db311SMatan Barak size_t min_size; 507c66db311SMatan Barak 508c66db311SMatan Barak if (IS_ERR(attr)) 509c66db311SMatan Barak return PTR_ERR(attr); 510c66db311SMatan Barak 511c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 512c66db311SMatan Barak 513c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 514c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 515c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 516c66db311SMatan Barak min_size)) 517c66db311SMatan Barak return -EFAULT; 518c66db311SMatan Barak 519c66db311SMatan Barak if (size > min_size) 520c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 521c66db311SMatan Barak 522c66db311SMatan Barak return 0; 523c66db311SMatan Barak } 524c66db311SMatan Barak 525d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 52689d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 527d70724f1SMatan Barak 528c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 529c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 530c66db311SMatan Barak 531118620d3SMatan Barak /* ================================================= 532118620d3SMatan Barak * Definitions -> Specs infrastructure 533118620d3SMatan Barak * ================================================= 534118620d3SMatan Barak */ 535a0aa309cSMatan Barak 536118620d3SMatan Barak /* 537118620d3SMatan Barak * uverbs_alloc_spec_tree - Merges different common and driver specific feature 538118620d3SMatan Barak * into one parsing tree that every uverbs command will be parsed upon. 539118620d3SMatan Barak * 540118620d3SMatan Barak * @num_trees: Number of trees in the array @trees. 541118620d3SMatan Barak * @trees: Array of pointers to tree root definitions to merge. Each such tree 542118620d3SMatan Barak * possibly contains objects, methods and attributes definitions. 543118620d3SMatan Barak * 544118620d3SMatan Barak * Returns: 545118620d3SMatan Barak * uverbs_root_spec *: The root of the merged parsing tree. 546118620d3SMatan Barak * On error, we return an error code. Error is checked via IS_ERR. 547118620d3SMatan Barak * 548118620d3SMatan Barak * The following merges could take place: 549118620d3SMatan Barak * a. Two trees representing the same method with different handler 550118620d3SMatan Barak * -> We take the handler of the tree that its handler != NULL 551118620d3SMatan Barak * and its index in the trees array is greater. The incentive for that 552118620d3SMatan Barak * is that developers are expected to first merge common trees and then 553118620d3SMatan Barak * merge trees that gives specialized the behaviour. 554118620d3SMatan Barak * b. Two trees representing the same object with different 555118620d3SMatan Barak * type_attrs (struct uverbs_obj_type): 556118620d3SMatan Barak * -> We take the type_attrs of the tree that its type_attr != NULL 557118620d3SMatan Barak * and its index in the trees array is greater. This could be used 558118620d3SMatan Barak * in order to override the free function, allocation size, etc. 559118620d3SMatan Barak * c. Two trees representing the same method attribute (same id but possibly 560118620d3SMatan Barak * different attributes): 561118620d3SMatan Barak * -> ERROR (-ENOENT), we believe that's not the programmer's intent. 562118620d3SMatan Barak * 563118620d3SMatan Barak * An object without any methods is considered invalid and will abort the 564118620d3SMatan Barak * function with -ENOENT error. 565118620d3SMatan Barak */ 56652427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 567118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 568118620d3SMatan Barak const struct uverbs_object_tree_def **trees); 569118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root); 57052427112SMatan Barak #else 57152427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 57252427112SMatan Barak const struct uverbs_object_tree_def **trees) 57352427112SMatan Barak { 57452427112SMatan Barak return NULL; 57552427112SMatan Barak } 57652427112SMatan Barak 57752427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) 57852427112SMatan Barak { 57952427112SMatan Barak } 58052427112SMatan Barak #endif 581118620d3SMatan Barak 582118620d3SMatan Barak #endif 583