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 */ 65*83bb4442SJason Gunthorpe /* good size 16 */ 66f43dbebfSMatan Barak struct uverbs_attr_spec { 67d108dac0SJason Gunthorpe u8 type; 68*83bb4442SJason Gunthorpe 69*83bb4442SJason Gunthorpe /* 70*83bb4442SJason Gunthorpe * Support extending attributes by length, validate all 71*83bb4442SJason Gunthorpe * unknown size == zero 72*83bb4442SJason Gunthorpe */ 73*83bb4442SJason Gunthorpe u8 min_sz_or_zero:1; 74*83bb4442SJason Gunthorpe /* 75*83bb4442SJason Gunthorpe * Valid only for PTR_IN. Allocate and copy the data inside 76*83bb4442SJason Gunthorpe * the parser 77*83bb4442SJason Gunthorpe */ 78*83bb4442SJason Gunthorpe u8 alloc_and_copy:1; 79*83bb4442SJason Gunthorpe u8 mandatory:1; 80d108dac0SJason Gunthorpe 81fac9658cSMatan Barak union { 82f43dbebfSMatan Barak struct { 83c66db311SMatan Barak /* Current known size to kernel */ 841f07e08fSMatan Barak u16 len; 85c66db311SMatan Barak /* User isn't allowed to provide something < min_len */ 86c66db311SMatan Barak u16 min_len; 871f07e08fSMatan Barak } ptr; 88d108dac0SJason Gunthorpe 891f07e08fSMatan Barak struct { 90f43dbebfSMatan Barak /* 91f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean 92f43dbebfSMatan Barak * the type id within the namespace. 93f43dbebfSMatan Barak */ 94f43dbebfSMatan Barak u16 obj_type; 95f43dbebfSMatan Barak u8 access; 96f43dbebfSMatan Barak } obj; 97d108dac0SJason Gunthorpe 98494c5580SMatan Barak struct { 99494c5580SMatan Barak u8 num_elems; 100d108dac0SJason Gunthorpe } enum_def; 101d108dac0SJason Gunthorpe } u; 102d108dac0SJason Gunthorpe 103d108dac0SJason Gunthorpe /* This weird split of the enum lets us remove some padding */ 104d108dac0SJason Gunthorpe union { 105d108dac0SJason Gunthorpe struct { 106494c5580SMatan Barak /* 107494c5580SMatan Barak * The enum attribute can select one of the attributes 108494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN 109494c5580SMatan Barak * attributes are supported in the ids array. 110494c5580SMatan Barak */ 111494c5580SMatan Barak const struct uverbs_attr_spec *ids; 112494c5580SMatan Barak } enum_def; 113d108dac0SJason Gunthorpe } u2; 114fac9658cSMatan Barak }; 115f43dbebfSMatan Barak 116f43dbebfSMatan Barak struct uverbs_attr_spec_hash { 117f43dbebfSMatan Barak size_t num_attrs; 118fac9658cSMatan Barak unsigned long *mandatory_attrs_bitmask; 119f43dbebfSMatan Barak struct uverbs_attr_spec attrs[0]; 120f43dbebfSMatan Barak }; 121f43dbebfSMatan Barak 122fac9658cSMatan Barak struct uverbs_attr_bundle; 123fac9658cSMatan Barak struct ib_uverbs_file; 124fac9658cSMatan Barak 125fac9658cSMatan Barak enum { 126fac9658cSMatan Barak /* 127fac9658cSMatan Barak * Action marked with this flag creates a context (or root for all 128fac9658cSMatan Barak * objects). 129fac9658cSMatan Barak */ 130fac9658cSMatan Barak UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0, 131fac9658cSMatan Barak }; 132fac9658cSMatan Barak 133fac9658cSMatan Barak struct uverbs_method_spec { 134fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 135fac9658cSMatan Barak u32 flags; 136fac9658cSMatan Barak size_t num_buckets; 137fac9658cSMatan Barak size_t num_child_attrs; 138fac9658cSMatan Barak int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, 139fac9658cSMatan Barak struct uverbs_attr_bundle *ctx); 140fac9658cSMatan Barak struct uverbs_attr_spec_hash *attr_buckets[0]; 141fac9658cSMatan Barak }; 142fac9658cSMatan Barak 143fac9658cSMatan Barak struct uverbs_method_spec_hash { 144fac9658cSMatan Barak size_t num_methods; 145fac9658cSMatan Barak struct uverbs_method_spec *methods[0]; 146fac9658cSMatan Barak }; 147fac9658cSMatan Barak 148fac9658cSMatan Barak struct uverbs_object_spec { 149fac9658cSMatan Barak const struct uverbs_obj_type *type_attrs; 150fac9658cSMatan Barak size_t num_buckets; 151fac9658cSMatan Barak struct uverbs_method_spec_hash *method_buckets[0]; 152fac9658cSMatan Barak }; 153fac9658cSMatan Barak 154fac9658cSMatan Barak struct uverbs_object_spec_hash { 155fac9658cSMatan Barak size_t num_objects; 156fac9658cSMatan Barak struct uverbs_object_spec *objects[0]; 157fac9658cSMatan Barak }; 158fac9658cSMatan Barak 159fac9658cSMatan Barak struct uverbs_root_spec { 160fac9658cSMatan Barak size_t num_buckets; 161fac9658cSMatan Barak struct uverbs_object_spec_hash *object_buckets[0]; 162fac9658cSMatan Barak }; 163fac9658cSMatan Barak 1645009010fSMatan Barak /* 1655009010fSMatan Barak * ======================================= 1665009010fSMatan Barak * Verbs definitions 1675009010fSMatan Barak * ======================================= 1685009010fSMatan Barak */ 1695009010fSMatan Barak 17009e3ebf8SMatan Barak struct uverbs_attr_def { 17109e3ebf8SMatan Barak u16 id; 17209e3ebf8SMatan Barak struct uverbs_attr_spec attr; 17309e3ebf8SMatan Barak }; 17409e3ebf8SMatan Barak 17509e3ebf8SMatan Barak struct uverbs_method_def { 17609e3ebf8SMatan Barak u16 id; 17709e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 17809e3ebf8SMatan Barak u32 flags; 17909e3ebf8SMatan Barak size_t num_attrs; 18009e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[]; 18109e3ebf8SMatan Barak int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, 18209e3ebf8SMatan Barak struct uverbs_attr_bundle *ctx); 18309e3ebf8SMatan Barak }; 18409e3ebf8SMatan Barak 1855009010fSMatan Barak struct uverbs_object_def { 18609e3ebf8SMatan Barak u16 id; 1875009010fSMatan Barak const struct uverbs_obj_type *type_attrs; 18809e3ebf8SMatan Barak size_t num_methods; 18909e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[]; 19009e3ebf8SMatan Barak }; 19109e3ebf8SMatan Barak 19209e3ebf8SMatan Barak struct uverbs_object_tree_def { 19309e3ebf8SMatan Barak size_t num_objects; 19409e3ebf8SMatan Barak const struct uverbs_object_def * const (*objects)[]; 1955009010fSMatan Barak }; 1965009010fSMatan Barak 197d108dac0SJason Gunthorpe /* 198d108dac0SJason Gunthorpe * ======================================= 199d108dac0SJason Gunthorpe * Attribute Specifications 200d108dac0SJason Gunthorpe * ======================================= 201d108dac0SJason Gunthorpe */ 202c66db311SMatan Barak 203d108dac0SJason Gunthorpe /* Use in the _type parameter for attribute specifications */ 204c66db311SMatan Barak #define UVERBS_ATTR_TYPE(_type) \ 205d108dac0SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 206c66db311SMatan Barak #define UVERBS_ATTR_STRUCT(_type, _last) \ 207d108dac0SJason Gunthorpe .u.ptr.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .u.ptr.len = sizeof(_type) 208c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \ 209d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len 2102d9c1bd7SMatan Barak #define UVERBS_ATTR_MIN_SIZE(_min_len) \ 2112d9c1bd7SMatan Barak UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 212c66db311SMatan Barak 213d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 214*83bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 215*83bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1 216*83bb4442SJason Gunthorpe #define UA_MIN_SZ_OR_ZERO .min_sz_or_zero = 1 217*83bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0 21835410306SMatan Barak 219d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 2209a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 221d108dac0SJason Gunthorpe .id = _attr_id, \ 222d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 223d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 224d108dac0SJason Gunthorpe .u.obj.access = _access, \ 225d108dac0SJason Gunthorpe __VA_ARGS__ } }) 226d108dac0SJason Gunthorpe 227d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 2289a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 229d108dac0SJason Gunthorpe .id = (_attr_id) + \ 230d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 23135410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 232d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 233d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 234d108dac0SJason Gunthorpe .u.obj.access = _access, \ 235d108dac0SJason Gunthorpe __VA_ARGS__ } }) 23635410306SMatan Barak 237d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 2389a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 239d108dac0SJason Gunthorpe .id = _attr_id, \ 240d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 241d108dac0SJason Gunthorpe _type, \ 242d108dac0SJason Gunthorpe __VA_ARGS__ } }) 243d108dac0SJason Gunthorpe 244d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 2459a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 246d108dac0SJason Gunthorpe .id = _attr_id, \ 247d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 248d108dac0SJason Gunthorpe _type, \ 249d108dac0SJason Gunthorpe __VA_ARGS__ } }) 250d108dac0SJason Gunthorpe 251d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 252d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 2539a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 254d108dac0SJason Gunthorpe .id = _attr_id, \ 255d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 256d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 257d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 258d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 259d108dac0SJason Gunthorpe }) 260d108dac0SJason Gunthorpe 261d108dac0SJason Gunthorpe /* 2626c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 2636c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 2646c61d2a5SJason Gunthorpe * spec. 2656c61d2a5SJason Gunthorpe */ 2666c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 2679a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 2686c61d2a5SJason Gunthorpe UVERBS_ATTR_SIZE(0, USHRT_MAX), \ 269*83bb4442SJason Gunthorpe UA_OPTIONAL, \ 270*83bb4442SJason Gunthorpe UA_MIN_SZ_OR_ZERO), \ 2719a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 2726c61d2a5SJason Gunthorpe UVERBS_ATTR_SIZE(0, USHRT_MAX), \ 273*83bb4442SJason Gunthorpe UA_OPTIONAL, \ 274*83bb4442SJason Gunthorpe UA_MIN_SZ_OR_ZERO), \ 2756c61d2a5SJason Gunthorpe 2766c61d2a5SJason Gunthorpe /* 277d108dac0SJason Gunthorpe * ======================================= 278d108dac0SJason Gunthorpe * Declaration helpers 279d108dac0SJason Gunthorpe * ======================================= 280d108dac0SJason Gunthorpe */ 2816c61d2a5SJason Gunthorpe 28209e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ 2836c61d2a5SJason Gunthorpe static const struct uverbs_object_def *const _name##_ptr[] = { \ 2846c61d2a5SJason Gunthorpe __VA_ARGS__, \ 2856c61d2a5SJason Gunthorpe }; \ 2866c61d2a5SJason Gunthorpe static const struct uverbs_object_tree_def _name = { \ 2876c61d2a5SJason Gunthorpe .num_objects = ARRAY_SIZE(_name##_ptr), \ 2886c61d2a5SJason Gunthorpe .objects = &_name##_ptr, \ 2896c61d2a5SJason Gunthorpe } 29009e3ebf8SMatan Barak 291fac9658cSMatan Barak /* ================================================= 292fac9658cSMatan Barak * Parsing infrastructure 293fac9658cSMatan Barak * ================================================= 294fac9658cSMatan Barak */ 295fac9658cSMatan Barak 296fac9658cSMatan Barak struct uverbs_ptr_attr { 2978762d149SMatan Barak /* 2988762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 2998762d149SMatan Barak * used. 3008762d149SMatan Barak */ 3018762d149SMatan Barak union { 3028762d149SMatan Barak void *ptr; 303fac9658cSMatan Barak u64 data; 3048762d149SMatan Barak }; 305fac9658cSMatan Barak u16 len; 306fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ 307fac9658cSMatan Barak u16 flags; 308494c5580SMatan Barak u8 enum_id; 309fac9658cSMatan Barak }; 310fac9658cSMatan Barak 311f43dbebfSMatan Barak struct uverbs_obj_attr { 312f43dbebfSMatan Barak struct ib_uobject *uobject; 313f43dbebfSMatan Barak }; 314f43dbebfSMatan Barak 315f43dbebfSMatan Barak struct uverbs_attr { 316fac9658cSMatan Barak /* 317fac9658cSMatan Barak * pointer to the user-space given attribute, in order to write the 318fac9658cSMatan Barak * new uobject's id or update flags. 319fac9658cSMatan Barak */ 320fac9658cSMatan Barak struct ib_uverbs_attr __user *uattr; 321fac9658cSMatan Barak union { 322fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 323f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 324f43dbebfSMatan Barak }; 325fac9658cSMatan Barak }; 326f43dbebfSMatan Barak 327f43dbebfSMatan Barak struct uverbs_attr_bundle_hash { 328f43dbebfSMatan Barak /* if bit i is set, it means attrs[i] contains valid information */ 329f43dbebfSMatan Barak unsigned long *valid_bitmap; 330f43dbebfSMatan Barak size_t num_attrs; 331f43dbebfSMatan Barak /* 332f43dbebfSMatan Barak * arrays of attributes, each element corresponds to the specification 333f43dbebfSMatan Barak * of the attribute in the same index. 334f43dbebfSMatan Barak */ 335f43dbebfSMatan Barak struct uverbs_attr *attrs; 336f43dbebfSMatan Barak }; 337f43dbebfSMatan Barak 338f43dbebfSMatan Barak struct uverbs_attr_bundle { 339f43dbebfSMatan Barak size_t num_buckets; 340f43dbebfSMatan Barak struct uverbs_attr_bundle_hash hash[]; 341f43dbebfSMatan Barak }; 342f43dbebfSMatan Barak 343f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, 344f43dbebfSMatan Barak unsigned int idx) 345f43dbebfSMatan Barak { 346f43dbebfSMatan Barak return test_bit(idx, attrs_hash->valid_bitmap); 347f43dbebfSMatan Barak } 348f43dbebfSMatan Barak 34935410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 35035410306SMatan Barak unsigned int idx) 35135410306SMatan Barak { 35235410306SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 35335410306SMatan Barak 35435410306SMatan Barak if (attrs_bundle->num_buckets <= idx_bucket) 35535410306SMatan Barak return false; 35635410306SMatan Barak 35735410306SMatan Barak return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], 35835410306SMatan Barak idx & ~UVERBS_ID_NS_MASK); 35935410306SMatan Barak } 36035410306SMatan Barak 36141b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 36241b2a71fSMatan Barak 363d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 364d70724f1SMatan Barak u16 idx) 365d70724f1SMatan Barak { 366d70724f1SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 367d70724f1SMatan Barak 368d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 369d70724f1SMatan Barak return ERR_PTR(-ENOENT); 370d70724f1SMatan Barak 371d70724f1SMatan Barak return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; 372d70724f1SMatan Barak } 373d70724f1SMatan Barak 374494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 375494c5580SMatan Barak u16 idx) 376494c5580SMatan Barak { 377494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 378494c5580SMatan Barak 379494c5580SMatan Barak if (IS_ERR(attr)) 380494c5580SMatan Barak return PTR_ERR(attr); 381494c5580SMatan Barak 382494c5580SMatan Barak return attr->ptr_attr.enum_id; 383494c5580SMatan Barak } 384494c5580SMatan Barak 385be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 386be934ccaSAriel Levkovich u16 idx) 387be934ccaSAriel Levkovich { 388f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 389be934ccaSAriel Levkovich 390f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 391f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 392f4602cbbSJason Gunthorpe return ERR_CAST(attr); 393be934ccaSAriel Levkovich 394f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 395be934ccaSAriel Levkovich } 396be934ccaSAriel Levkovich 3973efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 3983efa3812SMatan Barak u16 idx) 3993efa3812SMatan Barak { 4003efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4013efa3812SMatan Barak 4023efa3812SMatan Barak if (IS_ERR(attr)) 4033efa3812SMatan Barak return ERR_CAST(attr); 4043efa3812SMatan Barak 4053efa3812SMatan Barak return attr->obj_attr.uobject; 4063efa3812SMatan Barak } 4073efa3812SMatan Barak 4088762d149SMatan Barak static inline int 4098762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4108762d149SMatan Barak { 4118762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4128762d149SMatan Barak 4138762d149SMatan Barak if (IS_ERR(attr)) 4148762d149SMatan Barak return PTR_ERR(attr); 4158762d149SMatan Barak 4168762d149SMatan Barak return attr->ptr_attr.len; 4178762d149SMatan Barak } 4188762d149SMatan Barak 419d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 42089d9e8d3SMatan Barak size_t idx, const void *from, size_t size) 421d70724f1SMatan Barak { 422d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 423d70724f1SMatan Barak u16 flags; 42489d9e8d3SMatan Barak size_t min_size; 425d70724f1SMatan Barak 426d70724f1SMatan Barak if (IS_ERR(attr)) 427d70724f1SMatan Barak return PTR_ERR(attr); 428d70724f1SMatan Barak 42989d9e8d3SMatan Barak min_size = min_t(size_t, attr->ptr_attr.len, size); 4302f36028cSJason Gunthorpe if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) 43189d9e8d3SMatan Barak return -EFAULT; 43289d9e8d3SMatan Barak 433d70724f1SMatan Barak flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; 43489d9e8d3SMatan Barak if (put_user(flags, &attr->uattr->flags)) 43589d9e8d3SMatan Barak return -EFAULT; 43689d9e8d3SMatan Barak 43789d9e8d3SMatan Barak return 0; 438d70724f1SMatan Barak } 439d70724f1SMatan Barak 44089d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 44189d9e8d3SMatan Barak { 44289d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 44389d9e8d3SMatan Barak } 44489d9e8d3SMatan Barak 4458762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 4468762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4478762d149SMatan Barak { 4488762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4498762d149SMatan Barak 4508762d149SMatan Barak if (IS_ERR(attr)) 4518762d149SMatan Barak return (void *)attr; 4528762d149SMatan Barak 4538762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 4548762d149SMatan Barak attr->ptr_attr.ptr; 4558762d149SMatan Barak } 4568762d149SMatan Barak 45789d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 458d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 45989d9e8d3SMatan Barak size_t idx, 46089d9e8d3SMatan Barak size_t size) 461d70724f1SMatan Barak { 462d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 463d70724f1SMatan Barak 464d70724f1SMatan Barak if (IS_ERR(attr)) 465d70724f1SMatan Barak return PTR_ERR(attr); 466d70724f1SMatan Barak 46789d9e8d3SMatan Barak /* 46889d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 469c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 470c66db311SMatan Barak * uverbs_copy_from_or_zero. 47189d9e8d3SMatan Barak */ 47289d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 47389d9e8d3SMatan Barak return -EINVAL; 47489d9e8d3SMatan Barak 47589d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 476d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 4772f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 4782f36028cSJason Gunthorpe attr->ptr_attr.len)) 479d70724f1SMatan Barak return -EFAULT; 480d70724f1SMatan Barak 481d70724f1SMatan Barak return 0; 482d70724f1SMatan Barak } 483d70724f1SMatan Barak 484c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 485c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 486c66db311SMatan Barak size_t idx, 487c66db311SMatan Barak size_t size) 488c66db311SMatan Barak { 489c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 490c66db311SMatan Barak size_t min_size; 491c66db311SMatan Barak 492c66db311SMatan Barak if (IS_ERR(attr)) 493c66db311SMatan Barak return PTR_ERR(attr); 494c66db311SMatan Barak 495c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 496c66db311SMatan Barak 497c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 498c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 499c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 500c66db311SMatan Barak min_size)) 501c66db311SMatan Barak return -EFAULT; 502c66db311SMatan Barak 503c66db311SMatan Barak if (size > min_size) 504c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 505c66db311SMatan Barak 506c66db311SMatan Barak return 0; 507c66db311SMatan Barak } 508c66db311SMatan Barak 509d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 51089d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 511d70724f1SMatan Barak 512c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 513c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 514c66db311SMatan Barak 515118620d3SMatan Barak /* ================================================= 516118620d3SMatan Barak * Definitions -> Specs infrastructure 517118620d3SMatan Barak * ================================================= 518118620d3SMatan Barak */ 519a0aa309cSMatan Barak 520118620d3SMatan Barak /* 521118620d3SMatan Barak * uverbs_alloc_spec_tree - Merges different common and driver specific feature 522118620d3SMatan Barak * into one parsing tree that every uverbs command will be parsed upon. 523118620d3SMatan Barak * 524118620d3SMatan Barak * @num_trees: Number of trees in the array @trees. 525118620d3SMatan Barak * @trees: Array of pointers to tree root definitions to merge. Each such tree 526118620d3SMatan Barak * possibly contains objects, methods and attributes definitions. 527118620d3SMatan Barak * 528118620d3SMatan Barak * Returns: 529118620d3SMatan Barak * uverbs_root_spec *: The root of the merged parsing tree. 530118620d3SMatan Barak * On error, we return an error code. Error is checked via IS_ERR. 531118620d3SMatan Barak * 532118620d3SMatan Barak * The following merges could take place: 533118620d3SMatan Barak * a. Two trees representing the same method with different handler 534118620d3SMatan Barak * -> We take the handler of the tree that its handler != NULL 535118620d3SMatan Barak * and its index in the trees array is greater. The incentive for that 536118620d3SMatan Barak * is that developers are expected to first merge common trees and then 537118620d3SMatan Barak * merge trees that gives specialized the behaviour. 538118620d3SMatan Barak * b. Two trees representing the same object with different 539118620d3SMatan Barak * type_attrs (struct uverbs_obj_type): 540118620d3SMatan Barak * -> We take the type_attrs of the tree that its type_attr != NULL 541118620d3SMatan Barak * and its index in the trees array is greater. This could be used 542118620d3SMatan Barak * in order to override the free function, allocation size, etc. 543118620d3SMatan Barak * c. Two trees representing the same method attribute (same id but possibly 544118620d3SMatan Barak * different attributes): 545118620d3SMatan Barak * -> ERROR (-ENOENT), we believe that's not the programmer's intent. 546118620d3SMatan Barak * 547118620d3SMatan Barak * An object without any methods is considered invalid and will abort the 548118620d3SMatan Barak * function with -ENOENT error. 549118620d3SMatan Barak */ 55052427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 551118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 552118620d3SMatan Barak const struct uverbs_object_tree_def **trees); 553118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root); 55452427112SMatan Barak #else 55552427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 55652427112SMatan Barak const struct uverbs_object_tree_def **trees) 55752427112SMatan Barak { 55852427112SMatan Barak return NULL; 55952427112SMatan Barak } 56052427112SMatan Barak 56152427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) 56252427112SMatan Barak { 56352427112SMatan Barak } 56452427112SMatan Barak #endif 565118620d3SMatan Barak 566118620d3SMatan Barak #endif 567