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 { 76*d108dac0SJason Gunthorpe u8 type; 77*d108dac0SJason Gunthorpe u8 flags; 78*d108dac0SJason 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; 86*d108dac0SJason 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; 95*d108dac0SJason Gunthorpe 96494c5580SMatan Barak struct { 97494c5580SMatan Barak u8 num_elems; 98*d108dac0SJason Gunthorpe } enum_def; 99*d108dac0SJason Gunthorpe } u; 100*d108dac0SJason Gunthorpe 101*d108dac0SJason Gunthorpe /* This weird split of the enum lets us remove some padding */ 102*d108dac0SJason Gunthorpe union { 103*d108dac0SJason 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; 111*d108dac0SJason 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 195*d108dac0SJason Gunthorpe /* 196*d108dac0SJason Gunthorpe * ======================================= 197*d108dac0SJason Gunthorpe * Attribute Specifications 198*d108dac0SJason Gunthorpe * ======================================= 199*d108dac0SJason Gunthorpe */ 200c66db311SMatan Barak 201*d108dac0SJason Gunthorpe /* Use in the _type parameter for attribute specifications */ 202c66db311SMatan Barak #define UVERBS_ATTR_TYPE(_type) \ 203*d108dac0SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 204c66db311SMatan Barak #define UVERBS_ATTR_STRUCT(_type, _last) \ 205*d108dac0SJason 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) \ 207*d108dac0SJason 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 211*d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 212*d108dac0SJason Gunthorpe #define UA_FLAGS(_flags) .flags = _flags 21335410306SMatan Barak 214*d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 215*d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 216*d108dac0SJason Gunthorpe .id = _attr_id, \ 217*d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 218*d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 219*d108dac0SJason Gunthorpe .u.obj.access = _access, \ 220*d108dac0SJason Gunthorpe __VA_ARGS__ } }) 221*d108dac0SJason Gunthorpe 222*d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 223*d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 224*d108dac0SJason Gunthorpe .id = (_attr_id) + \ 225*d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 22635410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 227*d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 228*d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 229*d108dac0SJason Gunthorpe .u.obj.access = _access, \ 230*d108dac0SJason Gunthorpe __VA_ARGS__ } }) 23135410306SMatan Barak 232*d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 233*d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 234*d108dac0SJason Gunthorpe .id = _attr_id, \ 235*d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 236*d108dac0SJason Gunthorpe _type, \ 237*d108dac0SJason Gunthorpe __VA_ARGS__ } }) 238*d108dac0SJason Gunthorpe 239*d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 240*d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 241*d108dac0SJason Gunthorpe .id = _attr_id, \ 242*d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 243*d108dac0SJason Gunthorpe _type, \ 244*d108dac0SJason Gunthorpe __VA_ARGS__ } }) 245*d108dac0SJason Gunthorpe 246*d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 247*d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 248*d108dac0SJason Gunthorpe ((const struct uverbs_attr_def){ \ 249*d108dac0SJason Gunthorpe .id = _attr_id, \ 250*d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 251*d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 252*d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 253*d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 254*d108dac0SJason Gunthorpe }) 255*d108dac0SJason Gunthorpe 256*d108dac0SJason Gunthorpe /* 257*d108dac0SJason Gunthorpe * ======================================= 258*d108dac0SJason Gunthorpe * Declaration helpers 259*d108dac0SJason Gunthorpe * ======================================= 260*d108dac0SJason Gunthorpe */ 26135410306SMatan Barak #define _UVERBS_METHOD_ATTRS_SZ(...) \ 26235410306SMatan Barak (sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\ 26335410306SMatan Barak sizeof(const struct uverbs_attr_def *)) 26435410306SMatan Barak #define _UVERBS_METHOD(_id, _handler, _flags, ...) \ 26535410306SMatan Barak ((const struct uverbs_method_def) { \ 26635410306SMatan Barak .id = _id, \ 26735410306SMatan Barak .flags = _flags, \ 26835410306SMatan Barak .handler = _handler, \ 26935410306SMatan Barak .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \ 27035410306SMatan Barak .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} }) 27135410306SMatan Barak #define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \ 27235410306SMatan Barak const struct uverbs_method_def _name = \ 27335410306SMatan Barak _UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__) 27435410306SMatan Barak #define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \ 27535410306SMatan Barak const struct uverbs_method_def _name = \ 27635410306SMatan Barak _UVERBS_METHOD(_id, _handler, \ 27735410306SMatan Barak UVERBS_ACTION_FLAG_CREATE_ROOT, \ 27835410306SMatan Barak ##__VA_ARGS__) 27935410306SMatan Barak #define _UVERBS_OBJECT_METHODS_SZ(...) \ 28035410306SMatan Barak (sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \ 28135410306SMatan Barak sizeof(const struct uverbs_method_def *)) 2825009010fSMatan Barak #define _UVERBS_OBJECT(_id, _type_attrs, ...) \ 2835009010fSMatan Barak ((const struct uverbs_object_def) { \ 28409e3ebf8SMatan Barak .id = _id, \ 28535410306SMatan Barak .type_attrs = _type_attrs, \ 28635410306SMatan Barak .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \ 28735410306SMatan Barak .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} }) 2885009010fSMatan Barak #define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \ 2895009010fSMatan Barak const struct uverbs_object_def _name = \ 2905009010fSMatan Barak _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__) 29109e3ebf8SMatan Barak #define _UVERBS_TREE_OBJECTS_SZ(...) \ 29209e3ebf8SMatan Barak (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \ 29309e3ebf8SMatan Barak sizeof(const struct uverbs_object_def *)) 29409e3ebf8SMatan Barak #define _UVERBS_OBJECT_TREE(...) \ 29509e3ebf8SMatan Barak ((const struct uverbs_object_tree_def) { \ 29609e3ebf8SMatan Barak .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \ 29709e3ebf8SMatan Barak .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} }) 29809e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ 29909e3ebf8SMatan Barak const struct uverbs_object_tree_def _name = \ 30009e3ebf8SMatan Barak _UVERBS_OBJECT_TREE(__VA_ARGS__) 30109e3ebf8SMatan Barak 302fac9658cSMatan Barak /* ================================================= 303fac9658cSMatan Barak * Parsing infrastructure 304fac9658cSMatan Barak * ================================================= 305fac9658cSMatan Barak */ 306fac9658cSMatan Barak 307fac9658cSMatan Barak struct uverbs_ptr_attr { 3088762d149SMatan Barak /* 3098762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 3108762d149SMatan Barak * used. 3118762d149SMatan Barak */ 3128762d149SMatan Barak union { 3138762d149SMatan Barak void *ptr; 314fac9658cSMatan Barak u64 data; 3158762d149SMatan Barak }; 316fac9658cSMatan Barak u16 len; 317fac9658cSMatan Barak /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ 318fac9658cSMatan Barak u16 flags; 319494c5580SMatan Barak u8 enum_id; 320fac9658cSMatan Barak }; 321fac9658cSMatan Barak 322f43dbebfSMatan Barak struct uverbs_obj_attr { 323f43dbebfSMatan Barak struct ib_uobject *uobject; 324f43dbebfSMatan Barak }; 325f43dbebfSMatan Barak 326f43dbebfSMatan Barak struct uverbs_attr { 327fac9658cSMatan Barak /* 328fac9658cSMatan Barak * pointer to the user-space given attribute, in order to write the 329fac9658cSMatan Barak * new uobject's id or update flags. 330fac9658cSMatan Barak */ 331fac9658cSMatan Barak struct ib_uverbs_attr __user *uattr; 332fac9658cSMatan Barak union { 333fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 334f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 335f43dbebfSMatan Barak }; 336fac9658cSMatan Barak }; 337f43dbebfSMatan Barak 338f43dbebfSMatan Barak struct uverbs_attr_bundle_hash { 339f43dbebfSMatan Barak /* if bit i is set, it means attrs[i] contains valid information */ 340f43dbebfSMatan Barak unsigned long *valid_bitmap; 341f43dbebfSMatan Barak size_t num_attrs; 342f43dbebfSMatan Barak /* 343f43dbebfSMatan Barak * arrays of attributes, each element corresponds to the specification 344f43dbebfSMatan Barak * of the attribute in the same index. 345f43dbebfSMatan Barak */ 346f43dbebfSMatan Barak struct uverbs_attr *attrs; 347f43dbebfSMatan Barak }; 348f43dbebfSMatan Barak 349f43dbebfSMatan Barak struct uverbs_attr_bundle { 350f43dbebfSMatan Barak size_t num_buckets; 351f43dbebfSMatan Barak struct uverbs_attr_bundle_hash hash[]; 352f43dbebfSMatan Barak }; 353f43dbebfSMatan Barak 354f43dbebfSMatan Barak static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, 355f43dbebfSMatan Barak unsigned int idx) 356f43dbebfSMatan Barak { 357f43dbebfSMatan Barak return test_bit(idx, attrs_hash->valid_bitmap); 358f43dbebfSMatan Barak } 359f43dbebfSMatan Barak 36035410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 36135410306SMatan Barak unsigned int idx) 36235410306SMatan Barak { 36335410306SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 36435410306SMatan Barak 36535410306SMatan Barak if (attrs_bundle->num_buckets <= idx_bucket) 36635410306SMatan Barak return false; 36735410306SMatan Barak 36835410306SMatan Barak return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], 36935410306SMatan Barak idx & ~UVERBS_ID_NS_MASK); 37035410306SMatan Barak } 37135410306SMatan Barak 37241b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 37341b2a71fSMatan Barak 374d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 375d70724f1SMatan Barak u16 idx) 376d70724f1SMatan Barak { 377d70724f1SMatan Barak u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; 378d70724f1SMatan Barak 379d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 380d70724f1SMatan Barak return ERR_PTR(-ENOENT); 381d70724f1SMatan Barak 382d70724f1SMatan Barak return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; 383d70724f1SMatan Barak } 384d70724f1SMatan Barak 385494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 386494c5580SMatan Barak u16 idx) 387494c5580SMatan Barak { 388494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 389494c5580SMatan Barak 390494c5580SMatan Barak if (IS_ERR(attr)) 391494c5580SMatan Barak return PTR_ERR(attr); 392494c5580SMatan Barak 393494c5580SMatan Barak return attr->ptr_attr.enum_id; 394494c5580SMatan Barak } 395494c5580SMatan Barak 396be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 397be934ccaSAriel Levkovich u16 idx) 398be934ccaSAriel Levkovich { 399f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 400be934ccaSAriel Levkovich 401f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 402f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 403f4602cbbSJason Gunthorpe return ERR_CAST(attr); 404be934ccaSAriel Levkovich 405f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 406be934ccaSAriel Levkovich } 407be934ccaSAriel Levkovich 4083efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 4093efa3812SMatan Barak u16 idx) 4103efa3812SMatan Barak { 4113efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4123efa3812SMatan Barak 4133efa3812SMatan Barak if (IS_ERR(attr)) 4143efa3812SMatan Barak return ERR_CAST(attr); 4153efa3812SMatan Barak 4163efa3812SMatan Barak return attr->obj_attr.uobject; 4173efa3812SMatan Barak } 4183efa3812SMatan Barak 4198762d149SMatan Barak static inline int 4208762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4218762d149SMatan Barak { 4228762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4238762d149SMatan Barak 4248762d149SMatan Barak if (IS_ERR(attr)) 4258762d149SMatan Barak return PTR_ERR(attr); 4268762d149SMatan Barak 4278762d149SMatan Barak return attr->ptr_attr.len; 4288762d149SMatan Barak } 4298762d149SMatan Barak 430d70724f1SMatan Barak static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 43189d9e8d3SMatan Barak size_t idx, const void *from, size_t size) 432d70724f1SMatan Barak { 433d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 434d70724f1SMatan Barak u16 flags; 43589d9e8d3SMatan Barak size_t min_size; 436d70724f1SMatan Barak 437d70724f1SMatan Barak if (IS_ERR(attr)) 438d70724f1SMatan Barak return PTR_ERR(attr); 439d70724f1SMatan Barak 44089d9e8d3SMatan Barak min_size = min_t(size_t, attr->ptr_attr.len, size); 4412f36028cSJason Gunthorpe if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) 44289d9e8d3SMatan Barak return -EFAULT; 44389d9e8d3SMatan Barak 444d70724f1SMatan Barak flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; 44589d9e8d3SMatan Barak if (put_user(flags, &attr->uattr->flags)) 44689d9e8d3SMatan Barak return -EFAULT; 44789d9e8d3SMatan Barak 44889d9e8d3SMatan Barak return 0; 449d70724f1SMatan Barak } 450d70724f1SMatan Barak 45189d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 45289d9e8d3SMatan Barak { 45389d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 45489d9e8d3SMatan Barak } 45589d9e8d3SMatan Barak 4568762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 4578762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 4588762d149SMatan Barak { 4598762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 4608762d149SMatan Barak 4618762d149SMatan Barak if (IS_ERR(attr)) 4628762d149SMatan Barak return (void *)attr; 4638762d149SMatan Barak 4648762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 4658762d149SMatan Barak attr->ptr_attr.ptr; 4668762d149SMatan Barak } 4678762d149SMatan Barak 46889d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 469d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 47089d9e8d3SMatan Barak size_t idx, 47189d9e8d3SMatan Barak size_t size) 472d70724f1SMatan Barak { 473d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 474d70724f1SMatan Barak 475d70724f1SMatan Barak if (IS_ERR(attr)) 476d70724f1SMatan Barak return PTR_ERR(attr); 477d70724f1SMatan Barak 47889d9e8d3SMatan Barak /* 47989d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 480c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 481c66db311SMatan Barak * uverbs_copy_from_or_zero. 48289d9e8d3SMatan Barak */ 48389d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 48489d9e8d3SMatan Barak return -EINVAL; 48589d9e8d3SMatan Barak 48689d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 487d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 4882f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 4892f36028cSJason Gunthorpe attr->ptr_attr.len)) 490d70724f1SMatan Barak return -EFAULT; 491d70724f1SMatan Barak 492d70724f1SMatan Barak return 0; 493d70724f1SMatan Barak } 494d70724f1SMatan Barak 495c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 496c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 497c66db311SMatan Barak size_t idx, 498c66db311SMatan Barak size_t size) 499c66db311SMatan Barak { 500c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 501c66db311SMatan Barak size_t min_size; 502c66db311SMatan Barak 503c66db311SMatan Barak if (IS_ERR(attr)) 504c66db311SMatan Barak return PTR_ERR(attr); 505c66db311SMatan Barak 506c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 507c66db311SMatan Barak 508c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 509c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 510c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 511c66db311SMatan Barak min_size)) 512c66db311SMatan Barak return -EFAULT; 513c66db311SMatan Barak 514c66db311SMatan Barak if (size > min_size) 515c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 516c66db311SMatan Barak 517c66db311SMatan Barak return 0; 518c66db311SMatan Barak } 519c66db311SMatan Barak 520d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 52189d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 522d70724f1SMatan Barak 523c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 524c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 525c66db311SMatan Barak 526118620d3SMatan Barak /* ================================================= 527118620d3SMatan Barak * Definitions -> Specs infrastructure 528118620d3SMatan Barak * ================================================= 529118620d3SMatan Barak */ 530a0aa309cSMatan Barak 531118620d3SMatan Barak /* 532118620d3SMatan Barak * uverbs_alloc_spec_tree - Merges different common and driver specific feature 533118620d3SMatan Barak * into one parsing tree that every uverbs command will be parsed upon. 534118620d3SMatan Barak * 535118620d3SMatan Barak * @num_trees: Number of trees in the array @trees. 536118620d3SMatan Barak * @trees: Array of pointers to tree root definitions to merge. Each such tree 537118620d3SMatan Barak * possibly contains objects, methods and attributes definitions. 538118620d3SMatan Barak * 539118620d3SMatan Barak * Returns: 540118620d3SMatan Barak * uverbs_root_spec *: The root of the merged parsing tree. 541118620d3SMatan Barak * On error, we return an error code. Error is checked via IS_ERR. 542118620d3SMatan Barak * 543118620d3SMatan Barak * The following merges could take place: 544118620d3SMatan Barak * a. Two trees representing the same method with different handler 545118620d3SMatan Barak * -> We take the handler of the tree that its handler != NULL 546118620d3SMatan Barak * and its index in the trees array is greater. The incentive for that 547118620d3SMatan Barak * is that developers are expected to first merge common trees and then 548118620d3SMatan Barak * merge trees that gives specialized the behaviour. 549118620d3SMatan Barak * b. Two trees representing the same object with different 550118620d3SMatan Barak * type_attrs (struct uverbs_obj_type): 551118620d3SMatan Barak * -> We take the type_attrs of the tree that its type_attr != NULL 552118620d3SMatan Barak * and its index in the trees array is greater. This could be used 553118620d3SMatan Barak * in order to override the free function, allocation size, etc. 554118620d3SMatan Barak * c. Two trees representing the same method attribute (same id but possibly 555118620d3SMatan Barak * different attributes): 556118620d3SMatan Barak * -> ERROR (-ENOENT), we believe that's not the programmer's intent. 557118620d3SMatan Barak * 558118620d3SMatan Barak * An object without any methods is considered invalid and will abort the 559118620d3SMatan Barak * function with -ENOENT error. 560118620d3SMatan Barak */ 56152427112SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 562118620d3SMatan Barak struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 563118620d3SMatan Barak const struct uverbs_object_tree_def **trees); 564118620d3SMatan Barak void uverbs_free_spec_tree(struct uverbs_root_spec *root); 56552427112SMatan Barak #else 56652427112SMatan Barak static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, 56752427112SMatan Barak const struct uverbs_object_tree_def **trees) 56852427112SMatan Barak { 56952427112SMatan Barak return NULL; 57052427112SMatan Barak } 57152427112SMatan Barak 57252427112SMatan Barak static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) 57352427112SMatan Barak { 57452427112SMatan Barak } 57552427112SMatan Barak #endif 576118620d3SMatan Barak 577118620d3SMatan Barak #endif 578