1a0aa309cSMatan Barak /* 2a0aa309cSMatan Barak * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. 3a0aa309cSMatan Barak * 4a0aa309cSMatan Barak * This software is available to you under a choice of one of two 5a0aa309cSMatan Barak * licenses. You may choose to be licensed under the terms of the GNU 6a0aa309cSMatan Barak * General Public License (GPL) Version 2, available from the file 7a0aa309cSMatan Barak * COPYING in the main directory of this source tree, or the 8a0aa309cSMatan Barak * OpenIB.org BSD license below: 9a0aa309cSMatan Barak * 10a0aa309cSMatan Barak * Redistribution and use in source and binary forms, with or 11a0aa309cSMatan Barak * without modification, are permitted provided that the following 12a0aa309cSMatan Barak * conditions are met: 13a0aa309cSMatan Barak * 14a0aa309cSMatan Barak * - Redistributions of source code must retain the above 15a0aa309cSMatan Barak * copyright notice, this list of conditions and the following 16a0aa309cSMatan Barak * disclaimer. 17a0aa309cSMatan Barak * 18a0aa309cSMatan Barak * - Redistributions in binary form must reproduce the above 19a0aa309cSMatan Barak * copyright notice, this list of conditions and the following 20a0aa309cSMatan Barak * disclaimer in the documentation and/or other materials 21a0aa309cSMatan Barak * provided with the distribution. 22a0aa309cSMatan Barak * 23a0aa309cSMatan Barak * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24a0aa309cSMatan Barak * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25a0aa309cSMatan Barak * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26a0aa309cSMatan Barak * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27a0aa309cSMatan Barak * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28a0aa309cSMatan Barak * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29a0aa309cSMatan Barak * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30a0aa309cSMatan Barak * SOFTWARE. 31a0aa309cSMatan Barak */ 32a0aa309cSMatan Barak 33a0aa309cSMatan Barak #ifndef _UVERBS_IOCTL_ 34a0aa309cSMatan Barak #define _UVERBS_IOCTL_ 35a0aa309cSMatan Barak 36a0aa309cSMatan Barak #include <rdma/uverbs_types.h> 3735410306SMatan Barak #include <linux/uaccess.h> 3835410306SMatan Barak #include <rdma/rdma_user_ioctl.h> 39d70724f1SMatan Barak #include <rdma/ib_user_ioctl_verbs.h> 401f7ff9d5SMatan Barak #include <rdma/ib_user_ioctl_cmds.h> 41a0aa309cSMatan Barak 42a0aa309cSMatan Barak /* 43a0aa309cSMatan Barak * ======================================= 44a0aa309cSMatan Barak * Verbs action specifications 45a0aa309cSMatan Barak * ======================================= 46a0aa309cSMatan Barak */ 47a0aa309cSMatan Barak 48f43dbebfSMatan Barak enum uverbs_attr_type { 49f43dbebfSMatan Barak UVERBS_ATTR_TYPE_NA, 50fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_IN, 51fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_OUT, 52f43dbebfSMatan Barak UVERBS_ATTR_TYPE_IDR, 53f43dbebfSMatan Barak UVERBS_ATTR_TYPE_FD, 54494c5580SMatan Barak UVERBS_ATTR_TYPE_ENUM_IN, 55f43dbebfSMatan Barak }; 56f43dbebfSMatan Barak 57a0aa309cSMatan Barak enum uverbs_obj_access { 58a0aa309cSMatan Barak UVERBS_ACCESS_READ, 59a0aa309cSMatan Barak UVERBS_ACCESS_WRITE, 60a0aa309cSMatan Barak UVERBS_ACCESS_NEW, 61a0aa309cSMatan Barak UVERBS_ACCESS_DESTROY 62a0aa309cSMatan Barak }; 63a0aa309cSMatan Barak 641f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */ 6583bb4442SJason Gunthorpe /* good size 16 */ 66f43dbebfSMatan Barak struct uverbs_attr_spec { 67d108dac0SJason Gunthorpe u8 type; 6883bb4442SJason Gunthorpe 6983bb4442SJason Gunthorpe /* 70422e3d37SJason Gunthorpe * Support extending attributes by length. Allow the user to provide 71422e3d37SJason Gunthorpe * more bytes than ptr.len, but check that everything after is zero'd 72422e3d37SJason Gunthorpe * by the user. 7383bb4442SJason Gunthorpe */ 74422e3d37SJason Gunthorpe u8 zero_trailing:1; 7583bb4442SJason Gunthorpe /* 7683bb4442SJason Gunthorpe * Valid only for PTR_IN. Allocate and copy the data inside 7783bb4442SJason Gunthorpe * the parser 7883bb4442SJason Gunthorpe */ 7983bb4442SJason Gunthorpe u8 alloc_and_copy:1; 8083bb4442SJason Gunthorpe u8 mandatory:1; 81d108dac0SJason Gunthorpe 82fac9658cSMatan Barak union { 83f43dbebfSMatan Barak struct { 84c66db311SMatan Barak /* Current known size to kernel */ 851f07e08fSMatan Barak u16 len; 86c66db311SMatan Barak /* User isn't allowed to provide something < min_len */ 87c66db311SMatan Barak u16 min_len; 881f07e08fSMatan Barak } ptr; 89d108dac0SJason Gunthorpe 901f07e08fSMatan Barak struct { 91f43dbebfSMatan Barak /* 92f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean 93f43dbebfSMatan Barak * the type id within the namespace. 94f43dbebfSMatan Barak */ 95f43dbebfSMatan Barak u16 obj_type; 96f43dbebfSMatan Barak u8 access; 97f43dbebfSMatan Barak } obj; 98d108dac0SJason Gunthorpe 99494c5580SMatan Barak struct { 100494c5580SMatan Barak u8 num_elems; 101d108dac0SJason Gunthorpe } enum_def; 102d108dac0SJason Gunthorpe } u; 103d108dac0SJason Gunthorpe 104d108dac0SJason Gunthorpe /* This weird split of the enum lets us remove some padding */ 105d108dac0SJason Gunthorpe union { 106d108dac0SJason Gunthorpe struct { 107494c5580SMatan Barak /* 108494c5580SMatan Barak * The enum attribute can select one of the attributes 109494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN 110494c5580SMatan Barak * attributes are supported in the ids array. 111494c5580SMatan Barak */ 112494c5580SMatan Barak const struct uverbs_attr_spec *ids; 113494c5580SMatan Barak } enum_def; 114d108dac0SJason Gunthorpe } u2; 115fac9658cSMatan Barak }; 116f43dbebfSMatan Barak 1175009010fSMatan Barak /* 1189ed3e5f4SJason Gunthorpe * Information about the API is loaded into a radix tree. For IOCTL we start 1199ed3e5f4SJason Gunthorpe * with a tuple of: 1209ed3e5f4SJason Gunthorpe * object_id, attr_id, method_id 1219ed3e5f4SJason Gunthorpe * 1229ed3e5f4SJason Gunthorpe * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based 1239ed3e5f4SJason Gunthorpe * on the current kernel support this is compressed into 16 bit key for the 1249ed3e5f4SJason Gunthorpe * radix tree. Since this compression is entirely internal to the kernel the 1259ed3e5f4SJason Gunthorpe * below limits can be revised if the kernel gains additional data. 1269ed3e5f4SJason Gunthorpe * 1279ed3e5f4SJason Gunthorpe * With 64 leafs per node this is a 3 level radix tree. 1289ed3e5f4SJason Gunthorpe * 1299ed3e5f4SJason Gunthorpe * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns 1309ed3e5f4SJason Gunthorpe * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. 1319ed3e5f4SJason Gunthorpe */ 1329ed3e5f4SJason Gunthorpe enum uapi_radix_data { 1339ed3e5f4SJason Gunthorpe UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, 1349ed3e5f4SJason Gunthorpe 1359ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_BITS = 6, 1369ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), 1379ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, 1389ed3e5f4SJason Gunthorpe 1399ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS = 5, 1409ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, 1419ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE = 24, 1429ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) - 1439ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE, 1449ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_MASK = GENMASK( 1459ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, 1469ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT), 1479ed3e5f4SJason Gunthorpe 1489ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_BITS = 5, 1499ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_SHIFT = 1509ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, 1519ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_NUM_CORE = 24, 1529ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_NUM_DRIVER = 1539ed3e5f4SJason Gunthorpe (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, 1549ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), 1559ed3e5f4SJason Gunthorpe 1569ed3e5f4SJason Gunthorpe /* This id guaranteed to not exist in the radix tree */ 1579ed3e5f4SJason Gunthorpe UVERBS_API_KEY_ERR = 0xFFFFFFFF, 1589ed3e5f4SJason Gunthorpe }; 1599ed3e5f4SJason Gunthorpe 1609ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id) 1619ed3e5f4SJason Gunthorpe { 1629ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 1639ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 1649ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) 1659ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1669ed3e5f4SJason Gunthorpe id = id + UVERBS_API_OBJ_KEY_NUM_CORE; 1679ed3e5f4SJason Gunthorpe } else { 1689ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) 1699ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1709ed3e5f4SJason Gunthorpe } 1719ed3e5f4SJason Gunthorpe 1729ed3e5f4SJason Gunthorpe return id << UVERBS_API_OBJ_KEY_SHIFT; 1739ed3e5f4SJason Gunthorpe } 1749ed3e5f4SJason Gunthorpe 1759ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key) 1769ed3e5f4SJason Gunthorpe { 1779ed3e5f4SJason Gunthorpe return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; 1789ed3e5f4SJason Gunthorpe } 1799ed3e5f4SJason Gunthorpe 1809ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) 1819ed3e5f4SJason Gunthorpe { 1829ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 1839ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 1849ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) 1859ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1869ed3e5f4SJason Gunthorpe id = id + UVERBS_API_METHOD_KEY_NUM_CORE; 1879ed3e5f4SJason Gunthorpe } else { 1889ed3e5f4SJason Gunthorpe id++; 1899ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) 1909ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1919ed3e5f4SJason Gunthorpe } 1929ed3e5f4SJason Gunthorpe 1939ed3e5f4SJason Gunthorpe return id << UVERBS_API_METHOD_KEY_SHIFT; 1949ed3e5f4SJason Gunthorpe } 1959ed3e5f4SJason Gunthorpe 1969ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key) 1979ed3e5f4SJason Gunthorpe { 1989ed3e5f4SJason Gunthorpe return attr_key & 1999ed3e5f4SJason Gunthorpe (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); 2009ed3e5f4SJason Gunthorpe } 2019ed3e5f4SJason Gunthorpe 2029ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) 2039ed3e5f4SJason Gunthorpe { 2049ed3e5f4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && 2059ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) == 0; 2069ed3e5f4SJason Gunthorpe } 2079ed3e5f4SJason Gunthorpe 2089ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) 2099ed3e5f4SJason Gunthorpe { 2109ed3e5f4SJason Gunthorpe /* 0 is the method slot itself */ 2119ed3e5f4SJason Gunthorpe return ioctl_method_key + 1; 2129ed3e5f4SJason Gunthorpe } 2139ed3e5f4SJason Gunthorpe 2149ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id) 2159ed3e5f4SJason Gunthorpe { 2169ed3e5f4SJason Gunthorpe /* 2179ed3e5f4SJason Gunthorpe * The attr is designed to fit in the typical single radix tree node 2189ed3e5f4SJason Gunthorpe * of 64 entries. Since allmost all methods have driver attributes we 2199ed3e5f4SJason Gunthorpe * organize things so that the driver and core attributes interleave to 2209ed3e5f4SJason Gunthorpe * reduce the length of the attributes array in typical cases. 2219ed3e5f4SJason Gunthorpe */ 2229ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 2239ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 2249ed3e5f4SJason Gunthorpe id++; 2259ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 2269ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2279ed3e5f4SJason Gunthorpe id = (id << 1) | 0; 2289ed3e5f4SJason Gunthorpe } else { 2299ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 2309ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2319ed3e5f4SJason Gunthorpe id = (id << 1) | 1; 2329ed3e5f4SJason Gunthorpe } 2339ed3e5f4SJason Gunthorpe 2349ed3e5f4SJason Gunthorpe return id; 2359ed3e5f4SJason Gunthorpe } 2369ed3e5f4SJason Gunthorpe 2379ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key) 2389ed3e5f4SJason Gunthorpe { 2399ed3e5f4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && 2409ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) != 0; 2419ed3e5f4SJason Gunthorpe } 2429ed3e5f4SJason Gunthorpe 2439ed3e5f4SJason Gunthorpe /* 2449ed3e5f4SJason Gunthorpe * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), 2459ed3e5f4SJason Gunthorpe * basically it undoes the reservation of 0 in the ID numbering. attr_key 2469ed3e5f4SJason Gunthorpe * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of 2479ed3e5f4SJason Gunthorpe * uapi_key_attr(). 2489ed3e5f4SJason Gunthorpe */ 2499ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) 2509ed3e5f4SJason Gunthorpe { 2519ed3e5f4SJason Gunthorpe return attr_key - 1; 2529ed3e5f4SJason Gunthorpe } 2539ed3e5f4SJason Gunthorpe 2549ed3e5f4SJason Gunthorpe /* 2555009010fSMatan Barak * ======================================= 2565009010fSMatan Barak * Verbs definitions 2575009010fSMatan Barak * ======================================= 2585009010fSMatan Barak */ 2595009010fSMatan Barak 26009e3ebf8SMatan Barak struct uverbs_attr_def { 26109e3ebf8SMatan Barak u16 id; 26209e3ebf8SMatan Barak struct uverbs_attr_spec attr; 26309e3ebf8SMatan Barak }; 26409e3ebf8SMatan Barak 26509e3ebf8SMatan Barak struct uverbs_method_def { 26609e3ebf8SMatan Barak u16 id; 26709e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 26809e3ebf8SMatan Barak u32 flags; 26909e3ebf8SMatan Barak size_t num_attrs; 27009e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[]; 271e83f0ecdSJason Gunthorpe int (*handler)(struct ib_uverbs_file *ufile, 27209e3ebf8SMatan Barak struct uverbs_attr_bundle *ctx); 27309e3ebf8SMatan Barak }; 27409e3ebf8SMatan Barak 2755009010fSMatan Barak struct uverbs_object_def { 27609e3ebf8SMatan Barak u16 id; 2775009010fSMatan Barak const struct uverbs_obj_type *type_attrs; 27809e3ebf8SMatan Barak size_t num_methods; 27909e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[]; 28009e3ebf8SMatan Barak }; 28109e3ebf8SMatan Barak 28209e3ebf8SMatan Barak struct uverbs_object_tree_def { 28309e3ebf8SMatan Barak size_t num_objects; 28409e3ebf8SMatan Barak const struct uverbs_object_def * const (*objects)[]; 2855009010fSMatan Barak }; 2865009010fSMatan Barak 287d108dac0SJason Gunthorpe /* 288d108dac0SJason Gunthorpe * ======================================= 289d108dac0SJason Gunthorpe * Attribute Specifications 290d108dac0SJason Gunthorpe * ======================================= 291d108dac0SJason Gunthorpe */ 292c66db311SMatan Barak 293c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \ 294d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len 295422e3d37SJason Gunthorpe 296fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) 297fd44e385SYishai Hadas 298422e3d37SJason Gunthorpe /* 299422e3d37SJason Gunthorpe * Specifies a uapi structure that cannot be extended. The user must always 300422e3d37SJason Gunthorpe * supply the whole structure and nothing more. The structure must be declared 301422e3d37SJason Gunthorpe * in a header under include/uapi/rdma. 302422e3d37SJason Gunthorpe */ 303422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type) \ 304422e3d37SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 305422e3d37SJason Gunthorpe /* 306422e3d37SJason Gunthorpe * Specifies a uapi structure where the user must provide at least up to 307422e3d37SJason Gunthorpe * member 'last'. Anything after last and up until the end of the structure 308422e3d37SJason Gunthorpe * can be non-zero, anything longer than the end of the structure must be 309422e3d37SJason Gunthorpe * zero. The structure must be declared in a header under include/uapi/rdma. 310422e3d37SJason Gunthorpe */ 311422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last) \ 312422e3d37SJason Gunthorpe .zero_trailing = 1, \ 313422e3d37SJason Gunthorpe UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ 314422e3d37SJason Gunthorpe sizeof(_type)) 315540cd692SJason Gunthorpe /* 316540cd692SJason Gunthorpe * Specifies at least min_len bytes must be passed in, but the amount can be 317540cd692SJason Gunthorpe * larger, up to the protocol maximum size. No check for zeroing is done. 318540cd692SJason Gunthorpe */ 319540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 320c66db311SMatan Barak 321d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 32283bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 32383bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1 32483bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0 32535410306SMatan Barak 326d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 3279a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 328d108dac0SJason Gunthorpe .id = _attr_id, \ 329d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 330d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 331d108dac0SJason Gunthorpe .u.obj.access = _access, \ 332d108dac0SJason Gunthorpe __VA_ARGS__ } }) 333d108dac0SJason Gunthorpe 334d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 3359a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 336d108dac0SJason Gunthorpe .id = (_attr_id) + \ 337d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 33835410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 339d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 340d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 341d108dac0SJason Gunthorpe .u.obj.access = _access, \ 342d108dac0SJason Gunthorpe __VA_ARGS__ } }) 34335410306SMatan Barak 344d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 3459a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 346d108dac0SJason Gunthorpe .id = _attr_id, \ 347d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 348d108dac0SJason Gunthorpe _type, \ 349d108dac0SJason Gunthorpe __VA_ARGS__ } }) 350d108dac0SJason Gunthorpe 351d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 3529a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 353d108dac0SJason Gunthorpe .id = _attr_id, \ 354d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 355d108dac0SJason Gunthorpe _type, \ 356d108dac0SJason Gunthorpe __VA_ARGS__ } }) 357d108dac0SJason Gunthorpe 358d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 359d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 3609a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 361d108dac0SJason Gunthorpe .id = _attr_id, \ 362d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 363d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 364d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 365d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 366d108dac0SJason Gunthorpe }) 367d108dac0SJason Gunthorpe 368*0953fffeSMark Bloch /* An input value that is a member in the enum _enum_type. */ 369*0953fffeSMark Bloch #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \ 370*0953fffeSMark Bloch UVERBS_ATTR_PTR_IN( \ 371*0953fffeSMark Bloch _attr_id, \ 372*0953fffeSMark Bloch UVERBS_ATTR_SIZE( \ 373*0953fffeSMark Bloch sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \ 374*0953fffeSMark Bloch sizeof(u64)), \ 375*0953fffeSMark Bloch __VA_ARGS__) 376*0953fffeSMark Bloch 377d108dac0SJason Gunthorpe /* 378bccd0622SJason Gunthorpe * An input value that is a bitwise combination of values of _enum_type. 379bccd0622SJason Gunthorpe * This permits the flag value to be passed as either a u32 or u64, it must 380bccd0622SJason Gunthorpe * be retrieved via uverbs_get_flag(). 381bccd0622SJason Gunthorpe */ 382bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ 383bccd0622SJason Gunthorpe UVERBS_ATTR_PTR_IN( \ 384bccd0622SJason Gunthorpe _attr_id, \ 385bccd0622SJason Gunthorpe UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ 386bccd0622SJason Gunthorpe !sizeof(_enum_type *)), \ 387bccd0622SJason Gunthorpe sizeof(u64)), \ 388bccd0622SJason Gunthorpe __VA_ARGS__) 389bccd0622SJason Gunthorpe 390bccd0622SJason Gunthorpe /* 3916c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 3926c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 3936c61d2a5SJason Gunthorpe * spec. 3946c61d2a5SJason Gunthorpe */ 3956c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 3969a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 397540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 398540cd692SJason Gunthorpe UA_OPTIONAL), \ 3999a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 400540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 401540cd692SJason Gunthorpe UA_OPTIONAL) 4026c61d2a5SJason Gunthorpe 4036c61d2a5SJason Gunthorpe /* 404d108dac0SJason Gunthorpe * ======================================= 405d108dac0SJason Gunthorpe * Declaration helpers 406d108dac0SJason Gunthorpe * ======================================= 407d108dac0SJason Gunthorpe */ 4086c61d2a5SJason Gunthorpe 40909e3ebf8SMatan Barak #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ 4106c61d2a5SJason Gunthorpe static const struct uverbs_object_def *const _name##_ptr[] = { \ 4116c61d2a5SJason Gunthorpe __VA_ARGS__, \ 4126c61d2a5SJason Gunthorpe }; \ 4136c61d2a5SJason Gunthorpe static const struct uverbs_object_tree_def _name = { \ 4146c61d2a5SJason Gunthorpe .num_objects = ARRAY_SIZE(_name##_ptr), \ 4156c61d2a5SJason Gunthorpe .objects = &_name##_ptr, \ 4166c61d2a5SJason Gunthorpe } 41709e3ebf8SMatan Barak 418fac9658cSMatan Barak /* ================================================= 419fac9658cSMatan Barak * Parsing infrastructure 420fac9658cSMatan Barak * ================================================= 421fac9658cSMatan Barak */ 422fac9658cSMatan Barak 4233a863577SJason Gunthorpe 424fac9658cSMatan Barak struct uverbs_ptr_attr { 4258762d149SMatan Barak /* 4268762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 4278762d149SMatan Barak * used. 4288762d149SMatan Barak */ 4298762d149SMatan Barak union { 4308762d149SMatan Barak void *ptr; 431fac9658cSMatan Barak u64 data; 4328762d149SMatan Barak }; 433fac9658cSMatan Barak u16 len; 4346a1f444fSJason Gunthorpe u16 uattr_idx; 435494c5580SMatan Barak u8 enum_id; 436fac9658cSMatan Barak }; 437fac9658cSMatan Barak 438f43dbebfSMatan Barak struct uverbs_obj_attr { 439f43dbebfSMatan Barak struct ib_uobject *uobject; 4403a863577SJason Gunthorpe const struct uverbs_api_attr *attr_elm; 441f43dbebfSMatan Barak }; 442f43dbebfSMatan Barak 443f43dbebfSMatan Barak struct uverbs_attr { 444fac9658cSMatan Barak union { 445fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 446f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 447f43dbebfSMatan Barak }; 448fac9658cSMatan Barak }; 449f43dbebfSMatan Barak 450f43dbebfSMatan Barak struct uverbs_attr_bundle { 4514b3dd2bbSJason Gunthorpe struct ib_uverbs_file *ufile; 4523a863577SJason Gunthorpe DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN); 4533a863577SJason Gunthorpe struct uverbs_attr attrs[]; 454f43dbebfSMatan Barak }; 455f43dbebfSMatan Barak 45635410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 45735410306SMatan Barak unsigned int idx) 45835410306SMatan Barak { 4593a863577SJason Gunthorpe return test_bit(uapi_bkey_attr(uapi_key_attr(idx)), 4603a863577SJason Gunthorpe attrs_bundle->attr_present); 46135410306SMatan Barak } 46235410306SMatan Barak 46341b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 46441b2a71fSMatan Barak 465d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 466d70724f1SMatan Barak u16 idx) 467d70724f1SMatan Barak { 468d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 469d70724f1SMatan Barak return ERR_PTR(-ENOENT); 470d70724f1SMatan Barak 4713a863577SJason Gunthorpe return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))]; 472d70724f1SMatan Barak } 473d70724f1SMatan Barak 474494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 475494c5580SMatan Barak u16 idx) 476494c5580SMatan Barak { 477494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 478494c5580SMatan Barak 479494c5580SMatan Barak if (IS_ERR(attr)) 480494c5580SMatan Barak return PTR_ERR(attr); 481494c5580SMatan Barak 482494c5580SMatan Barak return attr->ptr_attr.enum_id; 483494c5580SMatan Barak } 484494c5580SMatan Barak 485be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 486be934ccaSAriel Levkovich u16 idx) 487be934ccaSAriel Levkovich { 488f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 489be934ccaSAriel Levkovich 490f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 491f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 492f4602cbbSJason Gunthorpe return ERR_CAST(attr); 493be934ccaSAriel Levkovich 494f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 495be934ccaSAriel Levkovich } 496be934ccaSAriel Levkovich 4973efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 4983efa3812SMatan Barak u16 idx) 4993efa3812SMatan Barak { 5003efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 5013efa3812SMatan Barak 5023efa3812SMatan Barak if (IS_ERR(attr)) 5033efa3812SMatan Barak return ERR_CAST(attr); 5043efa3812SMatan Barak 5053efa3812SMatan Barak return attr->obj_attr.uobject; 5063efa3812SMatan Barak } 5073efa3812SMatan Barak 5088762d149SMatan Barak static inline int 5098762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 5108762d149SMatan Barak { 5118762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 5128762d149SMatan Barak 5138762d149SMatan Barak if (IS_ERR(attr)) 5148762d149SMatan Barak return PTR_ERR(attr); 5158762d149SMatan Barak 5168762d149SMatan Barak return attr->ptr_attr.len; 5178762d149SMatan Barak } 5188762d149SMatan Barak 51989d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 52089d9e8d3SMatan Barak { 52189d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 52289d9e8d3SMatan Barak } 52389d9e8d3SMatan Barak 5248762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 5258762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 5268762d149SMatan Barak { 5278762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 5288762d149SMatan Barak 5298762d149SMatan Barak if (IS_ERR(attr)) 5308762d149SMatan Barak return (void *)attr; 5318762d149SMatan Barak 5328762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 5338762d149SMatan Barak attr->ptr_attr.ptr; 5348762d149SMatan Barak } 5358762d149SMatan Barak 53689d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 537d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 53889d9e8d3SMatan Barak size_t idx, 53989d9e8d3SMatan Barak size_t size) 540d70724f1SMatan Barak { 541d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 542d70724f1SMatan Barak 543d70724f1SMatan Barak if (IS_ERR(attr)) 544d70724f1SMatan Barak return PTR_ERR(attr); 545d70724f1SMatan Barak 54689d9e8d3SMatan Barak /* 54789d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 548c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 549c66db311SMatan Barak * uverbs_copy_from_or_zero. 55089d9e8d3SMatan Barak */ 55189d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 55289d9e8d3SMatan Barak return -EINVAL; 55389d9e8d3SMatan Barak 55489d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 555d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 5562f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 5572f36028cSJason Gunthorpe attr->ptr_attr.len)) 558d70724f1SMatan Barak return -EFAULT; 559d70724f1SMatan Barak 560d70724f1SMatan Barak return 0; 561d70724f1SMatan Barak } 562d70724f1SMatan Barak 563c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 564c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 565c66db311SMatan Barak size_t idx, 566c66db311SMatan Barak size_t size) 567c66db311SMatan Barak { 568c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 569c66db311SMatan Barak size_t min_size; 570c66db311SMatan Barak 571c66db311SMatan Barak if (IS_ERR(attr)) 572c66db311SMatan Barak return PTR_ERR(attr); 573c66db311SMatan Barak 574c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 575c66db311SMatan Barak 576c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 577c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 578c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 579c66db311SMatan Barak min_size)) 580c66db311SMatan Barak return -EFAULT; 581c66db311SMatan Barak 582c66db311SMatan Barak if (size > min_size) 583c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 584c66db311SMatan Barak 585c66db311SMatan Barak return 0; 586c66db311SMatan Barak } 587c66db311SMatan Barak 588d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 58989d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 590d70724f1SMatan Barak 591c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 592c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 593c66db311SMatan Barak 594bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 595bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 596bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 597bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 598bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 5996a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, 6006a1f444fSJason Gunthorpe const void *from, size_t size); 601461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, 602461bb2eeSJason Gunthorpe gfp_t flags); 603461bb2eeSJason Gunthorpe 604461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 605461bb2eeSJason Gunthorpe size_t size) 606461bb2eeSJason Gunthorpe { 607461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL); 608461bb2eeSJason Gunthorpe } 609461bb2eeSJason Gunthorpe 610461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 611461bb2eeSJason Gunthorpe size_t size) 612461bb2eeSJason Gunthorpe { 613461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); 614461bb2eeSJason Gunthorpe } 615*0953fffeSMark Bloch int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 616*0953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 617*0953fffeSMark Bloch s64 *def_val); 618bccd0622SJason Gunthorpe #else 619bccd0622SJason Gunthorpe static inline int 620bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 621bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 622bccd0622SJason Gunthorpe { 623bccd0622SJason Gunthorpe return -EINVAL; 624bccd0622SJason Gunthorpe } 625bccd0622SJason Gunthorpe static inline int 626bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 627bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 628bccd0622SJason Gunthorpe { 629bccd0622SJason Gunthorpe return -EINVAL; 630bccd0622SJason Gunthorpe } 6316a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 6326a1f444fSJason Gunthorpe size_t idx, const void *from, size_t size) 6336a1f444fSJason Gunthorpe { 6346a1f444fSJason Gunthorpe return -EINVAL; 6356a1f444fSJason Gunthorpe } 636461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 637461bb2eeSJason Gunthorpe size_t size) 638461bb2eeSJason Gunthorpe { 639461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 640461bb2eeSJason Gunthorpe } 641461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 642461bb2eeSJason Gunthorpe size_t size) 643461bb2eeSJason Gunthorpe { 644461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 645461bb2eeSJason Gunthorpe } 646*0953fffeSMark Bloch static inline int 647*0953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 648*0953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 649*0953fffeSMark Bloch s64 *def_val) 650*0953fffeSMark Bloch { 651*0953fffeSMark Bloch return -EINVAL; 652*0953fffeSMark Bloch } 653bccd0622SJason Gunthorpe #endif 654bccd0622SJason Gunthorpe 655*0953fffeSMark Bloch #define uverbs_get_const(_to, _attrs_bundle, _idx) \ 656*0953fffeSMark Bloch ({ \ 657*0953fffeSMark Bloch s64 _val; \ 658*0953fffeSMark Bloch int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \ 659*0953fffeSMark Bloch type_min(typeof(*_to)), \ 660*0953fffeSMark Bloch type_max(typeof(*_to)), NULL); \ 661*0953fffeSMark Bloch (*_to) = _val; \ 662*0953fffeSMark Bloch _ret; \ 663*0953fffeSMark Bloch }) 664*0953fffeSMark Bloch 665*0953fffeSMark Bloch #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \ 666*0953fffeSMark Bloch ({ \ 667*0953fffeSMark Bloch s64 _val; \ 668*0953fffeSMark Bloch s64 _def_val = _default; \ 669*0953fffeSMark Bloch int _ret = \ 670*0953fffeSMark Bloch _uverbs_get_const(&_val, _attrs_bundle, _idx, \ 671*0953fffeSMark Bloch type_min(typeof(*_to)), \ 672*0953fffeSMark Bloch type_max(typeof(*_to)), &_def_val); \ 673*0953fffeSMark Bloch (*_to) = _val; \ 674*0953fffeSMark Bloch _ret; \ 675*0953fffeSMark Bloch }) 676118620d3SMatan Barak #endif 677