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 /* 70*422e3d37SJason Gunthorpe * Support extending attributes by length. Allow the user to provide 71*422e3d37SJason Gunthorpe * more bytes than ptr.len, but check that everything after is zero'd 72*422e3d37SJason Gunthorpe * by the user. 7383bb4442SJason Gunthorpe */ 74*422e3d37SJason 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 206*422e3d37SJason Gunthorpe 207*422e3d37SJason Gunthorpe /* 208*422e3d37SJason Gunthorpe * Specifies a uapi structure that cannot be extended. The user must always 209*422e3d37SJason Gunthorpe * supply the whole structure and nothing more. The structure must be declared 210*422e3d37SJason Gunthorpe * in a header under include/uapi/rdma. 211*422e3d37SJason Gunthorpe */ 212*422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type) \ 213*422e3d37SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 214*422e3d37SJason Gunthorpe /* 215*422e3d37SJason Gunthorpe * Specifies a uapi structure where the user must provide at least up to 216*422e3d37SJason Gunthorpe * member 'last'. Anything after last and up until the end of the structure 217*422e3d37SJason Gunthorpe * can be non-zero, anything longer than the end of the structure must be 218*422e3d37SJason Gunthorpe * zero. The structure must be declared in a header under include/uapi/rdma. 219*422e3d37SJason Gunthorpe */ 220*422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last) \ 221*422e3d37SJason Gunthorpe .zero_trailing = 1, \ 222*422e3d37SJason Gunthorpe UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ 223*422e3d37SJason Gunthorpe sizeof(_type)) 224540cd692SJason Gunthorpe /* 225540cd692SJason Gunthorpe * Specifies at least min_len bytes must be passed in, but the amount can be 226540cd692SJason Gunthorpe * larger, up to the protocol maximum size. No check for zeroing is done. 227540cd692SJason Gunthorpe */ 228540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 229c66db311SMatan Barak 230d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 23183bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 23283bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1 23383bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0 23435410306SMatan Barak 235d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 2369a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 237d108dac0SJason Gunthorpe .id = _attr_id, \ 238d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 239d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 240d108dac0SJason Gunthorpe .u.obj.access = _access, \ 241d108dac0SJason Gunthorpe __VA_ARGS__ } }) 242d108dac0SJason Gunthorpe 243d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 2449a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 245d108dac0SJason Gunthorpe .id = (_attr_id) + \ 246d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 24735410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 248d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 249d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 250d108dac0SJason Gunthorpe .u.obj.access = _access, \ 251d108dac0SJason Gunthorpe __VA_ARGS__ } }) 25235410306SMatan Barak 253d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 2549a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 255d108dac0SJason Gunthorpe .id = _attr_id, \ 256d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 257d108dac0SJason Gunthorpe _type, \ 258d108dac0SJason Gunthorpe __VA_ARGS__ } }) 259d108dac0SJason Gunthorpe 260d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 2619a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 262d108dac0SJason Gunthorpe .id = _attr_id, \ 263d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 264d108dac0SJason Gunthorpe _type, \ 265d108dac0SJason Gunthorpe __VA_ARGS__ } }) 266d108dac0SJason Gunthorpe 267d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 268d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 2699a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 270d108dac0SJason Gunthorpe .id = _attr_id, \ 271d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 272d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 273d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 274d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 275d108dac0SJason Gunthorpe }) 276d108dac0SJason Gunthorpe 277d108dac0SJason Gunthorpe /* 2786c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 2796c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 2806c61d2a5SJason Gunthorpe * spec. 2816c61d2a5SJason Gunthorpe */ 2826c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 2839a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 284540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 285540cd692SJason Gunthorpe UA_OPTIONAL), \ 2869a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 287540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 288540cd692SJason Gunthorpe UA_OPTIONAL) 2896c61d2a5SJason Gunthorpe 2906c61d2a5SJason Gunthorpe /* 291d108dac0SJason Gunthorpe * ======================================= 292d108dac0SJason Gunthorpe * Declaration helpers 293d108dac0SJason Gunthorpe * ======================================= 294d108dac0SJason Gunthorpe */ 2956c61d2a5SJason Gunthorpe 29609e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ 2976c61d2a5SJason Gunthorpe static const struct uverbs_object_def *const _name##_ptr[] = { \ 2986c61d2a5SJason Gunthorpe __VA_ARGS__, \ 2996c61d2a5SJason Gunthorpe }; \ 3006c61d2a5SJason Gunthorpe static const struct uverbs_object_tree_def _name = { \ 3016c61d2a5SJason Gunthorpe .num_objects = ARRAY_SIZE(_name##_ptr), \ 3026c61d2a5SJason Gunthorpe .objects = &_name##_ptr, \ 3036c61d2a5SJason Gunthorpe } 30409e3ebf8SMatan Barak 305fac9658cSMatan Barak /* ================================================= 306fac9658cSMatan Barak * Parsing infrastructure 307fac9658cSMatan Barak * ================================================= 308fac9658cSMatan Barak */ 309fac9658cSMatan Barak 310fac9658cSMatan Barak struct uverbs_ptr_attr { 3118762d149SMatan Barak /* 3128762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 3138762d149SMatan Barak * used. 3148762d149SMatan Barak */ 3158762d149SMatan Barak union { 3168762d149SMatan Barak void *ptr; 317fac9658cSMatan Barak u64 data; 3188762d149SMatan Barak }; 319fac9658cSMatan Barak u16 len; 320fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ 321fac9658cSMatan Barak u16 flags; 322494c5580SMatan Barak u8 enum_id; 323fac9658cSMatan Barak }; 324fac9658cSMatan Barak 325f43dbebfSMatan Barak struct uverbs_obj_attr { 326f43dbebfSMatan Barak struct ib_uobject *uobject; 327f43dbebfSMatan Barak }; 328f43dbebfSMatan Barak 329f43dbebfSMatan Barak struct uverbs_attr { 330fac9658cSMatan Barak /* 331fac9658cSMatan Barak * pointer to the user-space given attribute, in order to write the 332fac9658cSMatan Barak * new uobject's id or update flags. 333fac9658cSMatan Barak */ 334fac9658cSMatan Barak struct ib_uverbs_attr __user *uattr; 335fac9658cSMatan Barak union { 336fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 337f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 338f43dbebfSMatan Barak }; 339fac9658cSMatan Barak }; 340f43dbebfSMatan Barak 341f43dbebfSMatan Barak struct uverbs_attr_bundle_hash { 342f43dbebfSMatan Barak /* if bit i is set, it means attrs[i] contains valid information */ 343f43dbebfSMatan Barak unsigned long *valid_bitmap; 344f43dbebfSMatan Barak size_t num_attrs; 345f43dbebfSMatan Barak /* 346f43dbebfSMatan Barak * arrays of attributes, each element corresponds to the specification 347f43dbebfSMatan Barak * of the attribute in the same index. 348f43dbebfSMatan Barak */ 349f43dbebfSMatan Barak struct uverbs_attr *attrs; 350f43dbebfSMatan Barak }; 351f43dbebfSMatan Barak 352f43dbebfSMatan Barak struct uverbs_attr_bundle { 353f43dbebfSMatan Barak size_t num_buckets; 354f43dbebfSMatan Barak struct uverbs_attr_bundle_hash hash[]; 355f43dbebfSMatan Barak }; 356f43dbebfSMatan Barak 357f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, 358f43dbebfSMatan Barak unsigned int idx) 359f43dbebfSMatan Barak { 360f43dbebfSMatan Barak return test_bit(idx, attrs_hash->valid_bitmap); 361f43dbebfSMatan Barak } 362f43dbebfSMatan Barak 36335410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 36435410306SMatan Barak unsigned int idx) 36535410306SMatan Barak { 36635410306SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 36735410306SMatan Barak 36835410306SMatan Barak if (attrs_bundle->num_buckets <= idx_bucket) 36935410306SMatan Barak return false; 37035410306SMatan Barak 37135410306SMatan Barak return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], 37235410306SMatan Barak idx & ~UVERBS_ID_NS_MASK); 37335410306SMatan Barak } 37435410306SMatan Barak 37541b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 37641b2a71fSMatan Barak 377d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 378d70724f1SMatan Barak u16 idx) 379d70724f1SMatan Barak { 380d70724f1SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 381d70724f1SMatan Barak 382d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 383d70724f1SMatan Barak return ERR_PTR(-ENOENT); 384d70724f1SMatan Barak 385d70724f1SMatan Barak return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; 386d70724f1SMatan Barak } 387d70724f1SMatan Barak 388494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 389494c5580SMatan Barak u16 idx) 390494c5580SMatan Barak { 391494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 392494c5580SMatan Barak 393494c5580SMatan Barak if (IS_ERR(attr)) 394494c5580SMatan Barak return PTR_ERR(attr); 395494c5580SMatan Barak 396494c5580SMatan Barak return attr->ptr_attr.enum_id; 397494c5580SMatan Barak } 398494c5580SMatan Barak 399be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 400be934ccaSAriel Levkovich u16 idx) 401be934ccaSAriel Levkovich { 402f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 403be934ccaSAriel Levkovich 404f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 405f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 406f4602cbbSJason Gunthorpe return ERR_CAST(attr); 407be934ccaSAriel Levkovich 408f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 409be934ccaSAriel Levkovich } 410be934ccaSAriel Levkovich 4113efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 4123efa3812SMatan Barak u16 idx) 4133efa3812SMatan Barak { 4143efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4153efa3812SMatan Barak 4163efa3812SMatan Barak if (IS_ERR(attr)) 4173efa3812SMatan Barak return ERR_CAST(attr); 4183efa3812SMatan Barak 4193efa3812SMatan Barak return attr->obj_attr.uobject; 4203efa3812SMatan Barak } 4213efa3812SMatan Barak 4228762d149SMatan Barak static inline int 4238762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4248762d149SMatan Barak { 4258762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4268762d149SMatan Barak 4278762d149SMatan Barak if (IS_ERR(attr)) 4288762d149SMatan Barak return PTR_ERR(attr); 4298762d149SMatan Barak 4308762d149SMatan Barak return attr->ptr_attr.len; 4318762d149SMatan Barak } 4328762d149SMatan Barak 433d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 43489d9e8d3SMatan Barak size_t idx, const void *from, size_t size) 435d70724f1SMatan Barak { 436d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 437d70724f1SMatan Barak u16 flags; 43889d9e8d3SMatan Barak size_t min_size; 439d70724f1SMatan Barak 440d70724f1SMatan Barak if (IS_ERR(attr)) 441d70724f1SMatan Barak return PTR_ERR(attr); 442d70724f1SMatan Barak 44389d9e8d3SMatan Barak min_size = min_t(size_t, attr->ptr_attr.len, size); 4442f36028cSJason Gunthorpe if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) 44589d9e8d3SMatan Barak return -EFAULT; 44689d9e8d3SMatan Barak 447d70724f1SMatan Barak flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; 44889d9e8d3SMatan Barak if (put_user(flags, &attr->uattr->flags)) 44989d9e8d3SMatan Barak return -EFAULT; 45089d9e8d3SMatan Barak 45189d9e8d3SMatan Barak return 0; 452d70724f1SMatan Barak } 453d70724f1SMatan Barak 45489d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 45589d9e8d3SMatan Barak { 45689d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 45789d9e8d3SMatan Barak } 45889d9e8d3SMatan Barak 4598762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 4608762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4618762d149SMatan Barak { 4628762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4638762d149SMatan Barak 4648762d149SMatan Barak if (IS_ERR(attr)) 4658762d149SMatan Barak return (void *)attr; 4668762d149SMatan Barak 4678762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 4688762d149SMatan Barak attr->ptr_attr.ptr; 4698762d149SMatan Barak } 4708762d149SMatan Barak 47189d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 472d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 47389d9e8d3SMatan Barak size_t idx, 47489d9e8d3SMatan Barak size_t size) 475d70724f1SMatan Barak { 476d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 477d70724f1SMatan Barak 478d70724f1SMatan Barak if (IS_ERR(attr)) 479d70724f1SMatan Barak return PTR_ERR(attr); 480d70724f1SMatan Barak 48189d9e8d3SMatan Barak /* 48289d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 483c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 484c66db311SMatan Barak * uverbs_copy_from_or_zero. 48589d9e8d3SMatan Barak */ 48689d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 48789d9e8d3SMatan Barak return -EINVAL; 48889d9e8d3SMatan Barak 48989d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 490d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 4912f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 4922f36028cSJason Gunthorpe attr->ptr_attr.len)) 493d70724f1SMatan Barak return -EFAULT; 494d70724f1SMatan Barak 495d70724f1SMatan Barak return 0; 496d70724f1SMatan Barak } 497d70724f1SMatan Barak 498c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 499c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 500c66db311SMatan Barak size_t idx, 501c66db311SMatan Barak size_t size) 502c66db311SMatan Barak { 503c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 504c66db311SMatan Barak size_t min_size; 505c66db311SMatan Barak 506c66db311SMatan Barak if (IS_ERR(attr)) 507c66db311SMatan Barak return PTR_ERR(attr); 508c66db311SMatan Barak 509c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 510c66db311SMatan Barak 511c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 512c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 513c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 514c66db311SMatan Barak min_size)) 515c66db311SMatan Barak return -EFAULT; 516c66db311SMatan Barak 517c66db311SMatan Barak if (size > min_size) 518c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 519c66db311SMatan Barak 520c66db311SMatan Barak return 0; 521c66db311SMatan Barak } 522c66db311SMatan Barak 523d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 52489d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 525d70724f1SMatan Barak 526c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 527c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 528c66db311SMatan Barak 529118620d3SMatan Barak /* ================================================= 530118620d3SMatan Barak * Definitions -> Specs infrastructure 531118620d3SMatan Barak * ================================================= 532118620d3SMatan Barak */ 533a0aa309cSMatan Barak 534118620d3SMatan Barak /* 535118620d3SMatan Barak * uverbs_alloc_spec_tree - Merges different common and driver specific feature 536118620d3SMatan Barak * into one parsing tree that every uverbs command will be parsed upon. 537118620d3SMatan Barak * 538118620d3SMatan Barak * @num_trees: Number of trees in the array @trees. 539118620d3SMatan Barak * @trees: Array of pointers to tree root definitions to merge. Each such tree 540118620d3SMatan Barak * possibly contains objects, methods and attributes definitions. 541118620d3SMatan Barak * 542118620d3SMatan Barak * Returns: 543118620d3SMatan Barak * uverbs_root_spec *: The root of the merged parsing tree. 544118620d3SMatan Barak * On error, we return an error code. Error is checked via IS_ERR. 545118620d3SMatan Barak * 546118620d3SMatan Barak * The following merges could take place: 547118620d3SMatan Barak * a. Two trees representing the same method with different handler 548118620d3SMatan Barak * -> We take the handler of the tree that its handler != NULL 549118620d3SMatan Barak * and its index in the trees array is greater. The incentive for that 550118620d3SMatan Barak * is that developers are expected to first merge common trees and then 551118620d3SMatan Barak * merge trees that gives specialized the behaviour. 552118620d3SMatan Barak * b. Two trees representing the same object with different 553118620d3SMatan Barak * type_attrs (struct uverbs_obj_type): 554118620d3SMatan Barak * -> We take the type_attrs of the tree that its type_attr != NULL 555118620d3SMatan Barak * and its index in the trees array is greater. This could be used 556118620d3SMatan Barak * in order to override the free function, allocation size, etc. 557118620d3SMatan Barak * c. Two trees representing the same method attribute (same id but possibly 558118620d3SMatan Barak * different attributes): 559118620d3SMatan Barak * -> ERROR (-ENOENT), we believe that's not the programmer's intent. 560118620d3SMatan Barak * 561118620d3SMatan Barak * An object without any methods is considered invalid and will abort the 562118620d3SMatan Barak * function with -ENOENT error. 563118620d3SMatan Barak */ 56452427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 565118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 566118620d3SMatan Barak const struct uverbs_object_tree_def **trees); 567118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root); 56852427112SMatan Barak #else 56952427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 57052427112SMatan Barak const struct uverbs_object_tree_def **trees) 57152427112SMatan Barak { 57252427112SMatan Barak return NULL; 57352427112SMatan Barak } 57452427112SMatan Barak 57552427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) 57652427112SMatan Barak { 57752427112SMatan Barak } 57852427112SMatan Barak #endif 579118620d3SMatan Barak 580118620d3SMatan Barak #endif 581