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 64fac9658cSMatan Barak enum { 65fac9658cSMatan Barak UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0, 66c66db311SMatan Barak /* Support extending attributes by length, validate all unknown size == zero */ 67c66db311SMatan Barak UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1, 688762d149SMatan Barak /* 698762d149SMatan Barak * Valid only for PTR_IN. Allocate and copy the data inside the parser 708762d149SMatan Barak */ 718762d149SMatan Barak UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY = 1U << 2, 72fac9658cSMatan Barak }; 73fac9658cSMatan Barak 741f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */ 75f43dbebfSMatan Barak struct uverbs_attr_spec { 76d108dac0SJason Gunthorpe u8 type; 77d108dac0SJason Gunthorpe u8 flags; 78d108dac0SJason Gunthorpe 79fac9658cSMatan Barak union { 80f43dbebfSMatan Barak struct { 81c66db311SMatan Barak /* Current known size to kernel */ 821f07e08fSMatan Barak u16 len; 83c66db311SMatan Barak /* User isn't allowed to provide something < min_len */ 84c66db311SMatan Barak u16 min_len; 851f07e08fSMatan Barak } ptr; 86d108dac0SJason Gunthorpe 871f07e08fSMatan Barak struct { 88f43dbebfSMatan Barak /* 89f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean 90f43dbebfSMatan Barak * the type id within the namespace. 91f43dbebfSMatan Barak */ 92f43dbebfSMatan Barak u16 obj_type; 93f43dbebfSMatan Barak u8 access; 94f43dbebfSMatan Barak } obj; 95d108dac0SJason Gunthorpe 96494c5580SMatan Barak struct { 97494c5580SMatan Barak u8 num_elems; 98d108dac0SJason Gunthorpe } enum_def; 99d108dac0SJason Gunthorpe } u; 100d108dac0SJason Gunthorpe 101d108dac0SJason Gunthorpe /* This weird split of the enum lets us remove some padding */ 102d108dac0SJason Gunthorpe union { 103d108dac0SJason Gunthorpe struct { 104494c5580SMatan Barak /* 105494c5580SMatan Barak * The enum attribute can select one of the attributes 106494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN 107494c5580SMatan Barak * attributes are supported in the ids array. 108494c5580SMatan Barak */ 109494c5580SMatan Barak const struct uverbs_attr_spec *ids; 110494c5580SMatan Barak } enum_def; 111d108dac0SJason Gunthorpe } u2; 112fac9658cSMatan Barak }; 113f43dbebfSMatan Barak 114f43dbebfSMatan Barak struct uverbs_attr_spec_hash { 115f43dbebfSMatan Barak size_t num_attrs; 116fac9658cSMatan Barak unsigned long *mandatory_attrs_bitmask; 117f43dbebfSMatan Barak struct uverbs_attr_spec attrs[0]; 118f43dbebfSMatan Barak }; 119f43dbebfSMatan Barak 120fac9658cSMatan Barak struct uverbs_attr_bundle; 121fac9658cSMatan Barak struct ib_uverbs_file; 122fac9658cSMatan Barak 123fac9658cSMatan Barak enum { 124fac9658cSMatan Barak /* 125fac9658cSMatan Barak * Action marked with this flag creates a context (or root for all 126fac9658cSMatan Barak * objects). 127fac9658cSMatan Barak */ 128fac9658cSMatan Barak UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0, 129fac9658cSMatan Barak }; 130fac9658cSMatan Barak 131fac9658cSMatan Barak struct uverbs_method_spec { 132fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 133fac9658cSMatan Barak u32 flags; 134fac9658cSMatan Barak size_t num_buckets; 135fac9658cSMatan Barak size_t num_child_attrs; 136fac9658cSMatan Barak int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, 137fac9658cSMatan Barak struct uverbs_attr_bundle *ctx); 138fac9658cSMatan Barak struct uverbs_attr_spec_hash *attr_buckets[0]; 139fac9658cSMatan Barak }; 140fac9658cSMatan Barak 141fac9658cSMatan Barak struct uverbs_method_spec_hash { 142fac9658cSMatan Barak size_t num_methods; 143fac9658cSMatan Barak struct uverbs_method_spec *methods[0]; 144fac9658cSMatan Barak }; 145fac9658cSMatan Barak 146fac9658cSMatan Barak struct uverbs_object_spec { 147fac9658cSMatan Barak const struct uverbs_obj_type *type_attrs; 148fac9658cSMatan Barak size_t num_buckets; 149fac9658cSMatan Barak struct uverbs_method_spec_hash *method_buckets[0]; 150fac9658cSMatan Barak }; 151fac9658cSMatan Barak 152fac9658cSMatan Barak struct uverbs_object_spec_hash { 153fac9658cSMatan Barak size_t num_objects; 154fac9658cSMatan Barak struct uverbs_object_spec *objects[0]; 155fac9658cSMatan Barak }; 156fac9658cSMatan Barak 157fac9658cSMatan Barak struct uverbs_root_spec { 158fac9658cSMatan Barak size_t num_buckets; 159fac9658cSMatan Barak struct uverbs_object_spec_hash *object_buckets[0]; 160fac9658cSMatan Barak }; 161fac9658cSMatan Barak 1625009010fSMatan Barak /* 1635009010fSMatan Barak * ======================================= 1645009010fSMatan Barak * Verbs definitions 1655009010fSMatan Barak * ======================================= 1665009010fSMatan Barak */ 1675009010fSMatan Barak 16809e3ebf8SMatan Barak struct uverbs_attr_def { 16909e3ebf8SMatan Barak u16 id; 17009e3ebf8SMatan Barak struct uverbs_attr_spec attr; 17109e3ebf8SMatan Barak }; 17209e3ebf8SMatan Barak 17309e3ebf8SMatan Barak struct uverbs_method_def { 17409e3ebf8SMatan Barak u16 id; 17509e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 17609e3ebf8SMatan Barak u32 flags; 17709e3ebf8SMatan Barak size_t num_attrs; 17809e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[]; 17909e3ebf8SMatan Barak int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, 18009e3ebf8SMatan Barak struct uverbs_attr_bundle *ctx); 18109e3ebf8SMatan Barak }; 18209e3ebf8SMatan Barak 1835009010fSMatan Barak struct uverbs_object_def { 18409e3ebf8SMatan Barak u16 id; 1855009010fSMatan Barak const struct uverbs_obj_type *type_attrs; 18609e3ebf8SMatan Barak size_t num_methods; 18709e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[]; 18809e3ebf8SMatan Barak }; 18909e3ebf8SMatan Barak 19009e3ebf8SMatan Barak struct uverbs_object_tree_def { 19109e3ebf8SMatan Barak size_t num_objects; 19209e3ebf8SMatan Barak const struct uverbs_object_def * const (*objects)[]; 1935009010fSMatan Barak }; 1945009010fSMatan Barak 195d108dac0SJason Gunthorpe /* 196d108dac0SJason Gunthorpe * ======================================= 197d108dac0SJason Gunthorpe * Attribute Specifications 198d108dac0SJason Gunthorpe * ======================================= 199d108dac0SJason Gunthorpe */ 200c66db311SMatan Barak 201d108dac0SJason Gunthorpe /* Use in the _type parameter for attribute specifications */ 202c66db311SMatan Barak #define UVERBS_ATTR_TYPE(_type) \ 203d108dac0SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 204c66db311SMatan Barak #define UVERBS_ATTR_STRUCT(_type, _last) \ 205d108dac0SJason Gunthorpe .u.ptr.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .u.ptr.len = sizeof(_type) 206c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \ 207d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len 2082d9c1bd7SMatan Barak #define UVERBS_ATTR_MIN_SIZE(_min_len) \ 2092d9c1bd7SMatan Barak UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 210c66db311SMatan Barak 211d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 212d108dac0SJason Gunthorpe #define UA_FLAGS(_flags) .flags = _flags 21335410306SMatan Barak 214d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 215d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 216d108dac0SJason Gunthorpe .id = _attr_id, \ 217d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 218d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 219d108dac0SJason Gunthorpe .u.obj.access = _access, \ 220d108dac0SJason Gunthorpe __VA_ARGS__ } }) 221d108dac0SJason Gunthorpe 222d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 223d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 224d108dac0SJason Gunthorpe .id = (_attr_id) + \ 225d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 22635410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 227d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 228d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 229d108dac0SJason Gunthorpe .u.obj.access = _access, \ 230d108dac0SJason Gunthorpe __VA_ARGS__ } }) 23135410306SMatan Barak 232d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 233d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 234d108dac0SJason Gunthorpe .id = _attr_id, \ 235d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 236d108dac0SJason Gunthorpe _type, \ 237d108dac0SJason Gunthorpe __VA_ARGS__ } }) 238d108dac0SJason Gunthorpe 239d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 240d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 241d108dac0SJason Gunthorpe .id = _attr_id, \ 242d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 243d108dac0SJason Gunthorpe _type, \ 244d108dac0SJason Gunthorpe __VA_ARGS__ } }) 245d108dac0SJason Gunthorpe 246d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 247d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 248d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 249d108dac0SJason Gunthorpe .id = _attr_id, \ 250d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 251d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 252d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 253d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 254d108dac0SJason Gunthorpe }) 255d108dac0SJason Gunthorpe 256d108dac0SJason Gunthorpe /* 257*6c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 258*6c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 259*6c61d2a5SJason Gunthorpe * spec. 260*6c61d2a5SJason Gunthorpe */ 261*6c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 262*6c61d2a5SJason Gunthorpe &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 263*6c61d2a5SJason Gunthorpe UVERBS_ATTR_SIZE(0, USHRT_MAX), \ 264*6c61d2a5SJason Gunthorpe UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO)), \ 265*6c61d2a5SJason Gunthorpe &UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 266*6c61d2a5SJason Gunthorpe UVERBS_ATTR_SIZE(0, USHRT_MAX), \ 267*6c61d2a5SJason Gunthorpe UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO)) 268*6c61d2a5SJason Gunthorpe 269*6c61d2a5SJason Gunthorpe /* 270d108dac0SJason Gunthorpe * ======================================= 271d108dac0SJason Gunthorpe * Declaration helpers 272d108dac0SJason Gunthorpe * ======================================= 273d108dac0SJason Gunthorpe */ 274*6c61d2a5SJason Gunthorpe 27509e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ 276*6c61d2a5SJason Gunthorpe static const struct uverbs_object_def *const _name##_ptr[] = { \ 277*6c61d2a5SJason Gunthorpe __VA_ARGS__, \ 278*6c61d2a5SJason Gunthorpe }; \ 279*6c61d2a5SJason Gunthorpe static const struct uverbs_object_tree_def _name = { \ 280*6c61d2a5SJason Gunthorpe .num_objects = ARRAY_SIZE(_name##_ptr), \ 281*6c61d2a5SJason Gunthorpe .objects = &_name##_ptr, \ 282*6c61d2a5SJason Gunthorpe } 28309e3ebf8SMatan Barak 284fac9658cSMatan Barak /* ================================================= 285fac9658cSMatan Barak * Parsing infrastructure 286fac9658cSMatan Barak * ================================================= 287fac9658cSMatan Barak */ 288fac9658cSMatan Barak 289fac9658cSMatan Barak struct uverbs_ptr_attr { 2908762d149SMatan Barak /* 2918762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 2928762d149SMatan Barak * used. 2938762d149SMatan Barak */ 2948762d149SMatan Barak union { 2958762d149SMatan Barak void *ptr; 296fac9658cSMatan Barak u64 data; 2978762d149SMatan Barak }; 298fac9658cSMatan Barak u16 len; 299fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ 300fac9658cSMatan Barak u16 flags; 301494c5580SMatan Barak u8 enum_id; 302fac9658cSMatan Barak }; 303fac9658cSMatan Barak 304f43dbebfSMatan Barak struct uverbs_obj_attr { 305f43dbebfSMatan Barak struct ib_uobject *uobject; 306f43dbebfSMatan Barak }; 307f43dbebfSMatan Barak 308f43dbebfSMatan Barak struct uverbs_attr { 309fac9658cSMatan Barak /* 310fac9658cSMatan Barak * pointer to the user-space given attribute, in order to write the 311fac9658cSMatan Barak * new uobject's id or update flags. 312fac9658cSMatan Barak */ 313fac9658cSMatan Barak struct ib_uverbs_attr __user *uattr; 314fac9658cSMatan Barak union { 315fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 316f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 317f43dbebfSMatan Barak }; 318fac9658cSMatan Barak }; 319f43dbebfSMatan Barak 320f43dbebfSMatan Barak struct uverbs_attr_bundle_hash { 321f43dbebfSMatan Barak /* if bit i is set, it means attrs[i] contains valid information */ 322f43dbebfSMatan Barak unsigned long *valid_bitmap; 323f43dbebfSMatan Barak size_t num_attrs; 324f43dbebfSMatan Barak /* 325f43dbebfSMatan Barak * arrays of attributes, each element corresponds to the specification 326f43dbebfSMatan Barak * of the attribute in the same index. 327f43dbebfSMatan Barak */ 328f43dbebfSMatan Barak struct uverbs_attr *attrs; 329f43dbebfSMatan Barak }; 330f43dbebfSMatan Barak 331f43dbebfSMatan Barak struct uverbs_attr_bundle { 332f43dbebfSMatan Barak size_t num_buckets; 333f43dbebfSMatan Barak struct uverbs_attr_bundle_hash hash[]; 334f43dbebfSMatan Barak }; 335f43dbebfSMatan Barak 336f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, 337f43dbebfSMatan Barak unsigned int idx) 338f43dbebfSMatan Barak { 339f43dbebfSMatan Barak return test_bit(idx, attrs_hash->valid_bitmap); 340f43dbebfSMatan Barak } 341f43dbebfSMatan Barak 34235410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 34335410306SMatan Barak unsigned int idx) 34435410306SMatan Barak { 34535410306SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 34635410306SMatan Barak 34735410306SMatan Barak if (attrs_bundle->num_buckets <= idx_bucket) 34835410306SMatan Barak return false; 34935410306SMatan Barak 35035410306SMatan Barak return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], 35135410306SMatan Barak idx & ~UVERBS_ID_NS_MASK); 35235410306SMatan Barak } 35335410306SMatan Barak 35441b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 35541b2a71fSMatan Barak 356d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 357d70724f1SMatan Barak u16 idx) 358d70724f1SMatan Barak { 359d70724f1SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 360d70724f1SMatan Barak 361d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 362d70724f1SMatan Barak return ERR_PTR(-ENOENT); 363d70724f1SMatan Barak 364d70724f1SMatan Barak return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; 365d70724f1SMatan Barak } 366d70724f1SMatan Barak 367494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 368494c5580SMatan Barak u16 idx) 369494c5580SMatan Barak { 370494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 371494c5580SMatan Barak 372494c5580SMatan Barak if (IS_ERR(attr)) 373494c5580SMatan Barak return PTR_ERR(attr); 374494c5580SMatan Barak 375494c5580SMatan Barak return attr->ptr_attr.enum_id; 376494c5580SMatan Barak } 377494c5580SMatan Barak 378be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 379be934ccaSAriel Levkovich u16 idx) 380be934ccaSAriel Levkovich { 381f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 382be934ccaSAriel Levkovich 383f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 384f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 385f4602cbbSJason Gunthorpe return ERR_CAST(attr); 386be934ccaSAriel Levkovich 387f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 388be934ccaSAriel Levkovich } 389be934ccaSAriel Levkovich 3903efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 3913efa3812SMatan Barak u16 idx) 3923efa3812SMatan Barak { 3933efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 3943efa3812SMatan Barak 3953efa3812SMatan Barak if (IS_ERR(attr)) 3963efa3812SMatan Barak return ERR_CAST(attr); 3973efa3812SMatan Barak 3983efa3812SMatan Barak return attr->obj_attr.uobject; 3993efa3812SMatan Barak } 4003efa3812SMatan Barak 4018762d149SMatan Barak static inline int 4028762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4038762d149SMatan Barak { 4048762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4058762d149SMatan Barak 4068762d149SMatan Barak if (IS_ERR(attr)) 4078762d149SMatan Barak return PTR_ERR(attr); 4088762d149SMatan Barak 4098762d149SMatan Barak return attr->ptr_attr.len; 4108762d149SMatan Barak } 4118762d149SMatan Barak 412d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 41389d9e8d3SMatan Barak size_t idx, const void *from, size_t size) 414d70724f1SMatan Barak { 415d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 416d70724f1SMatan Barak u16 flags; 41789d9e8d3SMatan Barak size_t min_size; 418d70724f1SMatan Barak 419d70724f1SMatan Barak if (IS_ERR(attr)) 420d70724f1SMatan Barak return PTR_ERR(attr); 421d70724f1SMatan Barak 42289d9e8d3SMatan Barak min_size = min_t(size_t, attr->ptr_attr.len, size); 4232f36028cSJason Gunthorpe if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) 42489d9e8d3SMatan Barak return -EFAULT; 42589d9e8d3SMatan Barak 426d70724f1SMatan Barak flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; 42789d9e8d3SMatan Barak if (put_user(flags, &attr->uattr->flags)) 42889d9e8d3SMatan Barak return -EFAULT; 42989d9e8d3SMatan Barak 43089d9e8d3SMatan Barak return 0; 431d70724f1SMatan Barak } 432d70724f1SMatan Barak 43389d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 43489d9e8d3SMatan Barak { 43589d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 43689d9e8d3SMatan Barak } 43789d9e8d3SMatan Barak 4388762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 4398762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4408762d149SMatan Barak { 4418762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4428762d149SMatan Barak 4438762d149SMatan Barak if (IS_ERR(attr)) 4448762d149SMatan Barak return (void *)attr; 4458762d149SMatan Barak 4468762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 4478762d149SMatan Barak attr->ptr_attr.ptr; 4488762d149SMatan Barak } 4498762d149SMatan Barak 45089d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 451d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 45289d9e8d3SMatan Barak size_t idx, 45389d9e8d3SMatan Barak size_t size) 454d70724f1SMatan Barak { 455d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 456d70724f1SMatan Barak 457d70724f1SMatan Barak if (IS_ERR(attr)) 458d70724f1SMatan Barak return PTR_ERR(attr); 459d70724f1SMatan Barak 46089d9e8d3SMatan Barak /* 46189d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 462c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 463c66db311SMatan Barak * uverbs_copy_from_or_zero. 46489d9e8d3SMatan Barak */ 46589d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 46689d9e8d3SMatan Barak return -EINVAL; 46789d9e8d3SMatan Barak 46889d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 469d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 4702f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 4712f36028cSJason Gunthorpe attr->ptr_attr.len)) 472d70724f1SMatan Barak return -EFAULT; 473d70724f1SMatan Barak 474d70724f1SMatan Barak return 0; 475d70724f1SMatan Barak } 476d70724f1SMatan Barak 477c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 478c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 479c66db311SMatan Barak size_t idx, 480c66db311SMatan Barak size_t size) 481c66db311SMatan Barak { 482c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 483c66db311SMatan Barak size_t min_size; 484c66db311SMatan Barak 485c66db311SMatan Barak if (IS_ERR(attr)) 486c66db311SMatan Barak return PTR_ERR(attr); 487c66db311SMatan Barak 488c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 489c66db311SMatan Barak 490c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 491c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 492c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 493c66db311SMatan Barak min_size)) 494c66db311SMatan Barak return -EFAULT; 495c66db311SMatan Barak 496c66db311SMatan Barak if (size > min_size) 497c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 498c66db311SMatan Barak 499c66db311SMatan Barak return 0; 500c66db311SMatan Barak } 501c66db311SMatan Barak 502d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 50389d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 504d70724f1SMatan Barak 505c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 506c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 507c66db311SMatan Barak 508118620d3SMatan Barak /* ================================================= 509118620d3SMatan Barak * Definitions -> Specs infrastructure 510118620d3SMatan Barak * ================================================= 511118620d3SMatan Barak */ 512a0aa309cSMatan Barak 513118620d3SMatan Barak /* 514118620d3SMatan Barak * uverbs_alloc_spec_tree - Merges different common and driver specific feature 515118620d3SMatan Barak * into one parsing tree that every uverbs command will be parsed upon. 516118620d3SMatan Barak * 517118620d3SMatan Barak * @num_trees: Number of trees in the array @trees. 518118620d3SMatan Barak * @trees: Array of pointers to tree root definitions to merge. Each such tree 519118620d3SMatan Barak * possibly contains objects, methods and attributes definitions. 520118620d3SMatan Barak * 521118620d3SMatan Barak * Returns: 522118620d3SMatan Barak * uverbs_root_spec *: The root of the merged parsing tree. 523118620d3SMatan Barak * On error, we return an error code. Error is checked via IS_ERR. 524118620d3SMatan Barak * 525118620d3SMatan Barak * The following merges could take place: 526118620d3SMatan Barak * a. Two trees representing the same method with different handler 527118620d3SMatan Barak * -> We take the handler of the tree that its handler != NULL 528118620d3SMatan Barak * and its index in the trees array is greater. The incentive for that 529118620d3SMatan Barak * is that developers are expected to first merge common trees and then 530118620d3SMatan Barak * merge trees that gives specialized the behaviour. 531118620d3SMatan Barak * b. Two trees representing the same object with different 532118620d3SMatan Barak * type_attrs (struct uverbs_obj_type): 533118620d3SMatan Barak * -> We take the type_attrs of the tree that its type_attr != NULL 534118620d3SMatan Barak * and its index in the trees array is greater. This could be used 535118620d3SMatan Barak * in order to override the free function, allocation size, etc. 536118620d3SMatan Barak * c. Two trees representing the same method attribute (same id but possibly 537118620d3SMatan Barak * different attributes): 538118620d3SMatan Barak * -> ERROR (-ENOENT), we believe that's not the programmer's intent. 539118620d3SMatan Barak * 540118620d3SMatan Barak * An object without any methods is considered invalid and will abort the 541118620d3SMatan Barak * function with -ENOENT error. 542118620d3SMatan Barak */ 54352427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 544118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 545118620d3SMatan Barak const struct uverbs_object_tree_def **trees); 546118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root); 54752427112SMatan Barak #else 54852427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 54952427112SMatan Barak const struct uverbs_object_tree_def **trees) 55052427112SMatan Barak { 55152427112SMatan Barak return NULL; 55252427112SMatan Barak } 55352427112SMatan Barak 55452427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) 55552427112SMatan Barak { 55652427112SMatan Barak } 55752427112SMatan Barak #endif 558118620d3SMatan Barak 559118620d3SMatan Barak #endif 560