16bf9d8f6SLeon Romanovsky /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 2a0aa309cSMatan Barak /* 3a0aa309cSMatan Barak * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. 4a0aa309cSMatan Barak */ 5a0aa309cSMatan Barak 6a0aa309cSMatan Barak #ifndef _UVERBS_IOCTL_ 7a0aa309cSMatan Barak #define _UVERBS_IOCTL_ 8a0aa309cSMatan Barak 9a0aa309cSMatan Barak #include <rdma/uverbs_types.h> 1035410306SMatan Barak #include <linux/uaccess.h> 1135410306SMatan Barak #include <rdma/rdma_user_ioctl.h> 12d70724f1SMatan Barak #include <rdma/ib_user_ioctl_verbs.h> 131f7ff9d5SMatan Barak #include <rdma/ib_user_ioctl_cmds.h> 14a0aa309cSMatan Barak 15a0aa309cSMatan Barak /* 16a0aa309cSMatan Barak * ======================================= 17a0aa309cSMatan Barak * Verbs action specifications 18a0aa309cSMatan Barak * ======================================= 19a0aa309cSMatan Barak */ 20a0aa309cSMatan Barak 21f43dbebfSMatan Barak enum uverbs_attr_type { 22f43dbebfSMatan Barak UVERBS_ATTR_TYPE_NA, 23fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_IN, 24fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_OUT, 25f43dbebfSMatan Barak UVERBS_ATTR_TYPE_IDR, 26f43dbebfSMatan Barak UVERBS_ATTR_TYPE_FD, 27494c5580SMatan Barak UVERBS_ATTR_TYPE_ENUM_IN, 2870cd20aeSGuy Levi UVERBS_ATTR_TYPE_IDRS_ARRAY, 29f43dbebfSMatan Barak }; 30f43dbebfSMatan Barak 31a0aa309cSMatan Barak enum uverbs_obj_access { 32a0aa309cSMatan Barak UVERBS_ACCESS_READ, 33a0aa309cSMatan Barak UVERBS_ACCESS_WRITE, 34a0aa309cSMatan Barak UVERBS_ACCESS_NEW, 35a0aa309cSMatan Barak UVERBS_ACCESS_DESTROY 36a0aa309cSMatan Barak }; 37a0aa309cSMatan Barak 381f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */ 3983bb4442SJason Gunthorpe /* good size 16 */ 40f43dbebfSMatan Barak struct uverbs_attr_spec { 41d108dac0SJason Gunthorpe u8 type; 4283bb4442SJason Gunthorpe 4383bb4442SJason Gunthorpe /* 44422e3d37SJason Gunthorpe * Support extending attributes by length. Allow the user to provide 45422e3d37SJason Gunthorpe * more bytes than ptr.len, but check that everything after is zero'd 46422e3d37SJason Gunthorpe * by the user. 4783bb4442SJason Gunthorpe */ 48422e3d37SJason Gunthorpe u8 zero_trailing:1; 4983bb4442SJason Gunthorpe /* 5083bb4442SJason Gunthorpe * Valid only for PTR_IN. Allocate and copy the data inside 5183bb4442SJason Gunthorpe * the parser 5283bb4442SJason Gunthorpe */ 5383bb4442SJason Gunthorpe u8 alloc_and_copy:1; 5483bb4442SJason Gunthorpe u8 mandatory:1; 5507f05f40SJason Gunthorpe /* True if this is from UVERBS_ATTR_UHW */ 5607f05f40SJason Gunthorpe u8 is_udata:1; 57d108dac0SJason Gunthorpe 58fac9658cSMatan Barak union { 59f43dbebfSMatan Barak struct { 60c66db311SMatan Barak /* Current known size to kernel */ 611f07e08fSMatan Barak u16 len; 62c66db311SMatan Barak /* User isn't allowed to provide something < min_len */ 63c66db311SMatan Barak u16 min_len; 641f07e08fSMatan Barak } ptr; 65d108dac0SJason Gunthorpe 661f07e08fSMatan Barak struct { 67f43dbebfSMatan Barak /* 68f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean 69f43dbebfSMatan Barak * the type id within the namespace. 70f43dbebfSMatan Barak */ 71f43dbebfSMatan Barak u16 obj_type; 72f43dbebfSMatan Barak u8 access; 73f43dbebfSMatan Barak } obj; 74d108dac0SJason Gunthorpe 75494c5580SMatan Barak struct { 76494c5580SMatan Barak u8 num_elems; 77d108dac0SJason Gunthorpe } enum_def; 78d108dac0SJason Gunthorpe } u; 79d108dac0SJason Gunthorpe 8070cd20aeSGuy Levi /* This weird split lets us remove some padding */ 81d108dac0SJason Gunthorpe union { 82d108dac0SJason Gunthorpe struct { 83494c5580SMatan Barak /* 84494c5580SMatan Barak * The enum attribute can select one of the attributes 85494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN 86494c5580SMatan Barak * attributes are supported in the ids array. 87494c5580SMatan Barak */ 88494c5580SMatan Barak const struct uverbs_attr_spec *ids; 89494c5580SMatan Barak } enum_def; 9070cd20aeSGuy Levi 9170cd20aeSGuy Levi struct { 9270cd20aeSGuy Levi /* 9370cd20aeSGuy Levi * higher bits mean the namespace and lower bits mean 9470cd20aeSGuy Levi * the type id within the namespace. 9570cd20aeSGuy Levi */ 9670cd20aeSGuy Levi u16 obj_type; 9770cd20aeSGuy Levi u16 min_len; 9870cd20aeSGuy Levi u16 max_len; 9970cd20aeSGuy Levi u8 access; 10070cd20aeSGuy Levi } objs_arr; 101d108dac0SJason Gunthorpe } u2; 102fac9658cSMatan Barak }; 103f43dbebfSMatan Barak 1045009010fSMatan Barak /* 1059ed3e5f4SJason Gunthorpe * Information about the API is loaded into a radix tree. For IOCTL we start 1069ed3e5f4SJason Gunthorpe * with a tuple of: 1079ed3e5f4SJason Gunthorpe * object_id, attr_id, method_id 1089ed3e5f4SJason Gunthorpe * 1099ed3e5f4SJason Gunthorpe * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based 1109ed3e5f4SJason Gunthorpe * on the current kernel support this is compressed into 16 bit key for the 1119ed3e5f4SJason Gunthorpe * radix tree. Since this compression is entirely internal to the kernel the 1129ed3e5f4SJason Gunthorpe * below limits can be revised if the kernel gains additional data. 1139ed3e5f4SJason Gunthorpe * 1149ed3e5f4SJason Gunthorpe * With 64 leafs per node this is a 3 level radix tree. 1159ed3e5f4SJason Gunthorpe * 1169ed3e5f4SJason Gunthorpe * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns 1179ed3e5f4SJason Gunthorpe * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. 1186884c6c4SJason Gunthorpe * 1196884c6c4SJason Gunthorpe * This also encodes the tables for the write() and write() extended commands 1206884c6c4SJason Gunthorpe * using the coding 1216884c6c4SJason Gunthorpe * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command # 1226884c6c4SJason Gunthorpe * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex # 1236884c6c4SJason Gunthorpe * ie the WRITE path is treated as a special method type in the ioctl 1246884c6c4SJason Gunthorpe * framework. 1259ed3e5f4SJason Gunthorpe */ 1269ed3e5f4SJason Gunthorpe enum uapi_radix_data { 1279ed3e5f4SJason Gunthorpe UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, 1289ed3e5f4SJason Gunthorpe 1299ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_BITS = 6, 1309ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), 1319ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, 1326884c6c4SJason Gunthorpe UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS, 1339ed3e5f4SJason Gunthorpe 1349ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS = 5, 1359ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, 1366884c6c4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE = 22, 1376884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT, 1386884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT, 1396884c6c4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_DRIVER = 1406884c6c4SJason Gunthorpe (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) - 1419ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE, 1429ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_MASK = GENMASK( 1439ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, 1449ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT), 1459ed3e5f4SJason Gunthorpe 1469ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_BITS = 5, 1479ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_SHIFT = 1489ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, 149342ee59dSYishai Hadas UVERBS_API_OBJ_KEY_NUM_CORE = 20, 1509ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_NUM_DRIVER = 1519ed3e5f4SJason Gunthorpe (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, 1529ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), 1539ed3e5f4SJason Gunthorpe 1549ed3e5f4SJason Gunthorpe /* This id guaranteed to not exist in the radix tree */ 1559ed3e5f4SJason Gunthorpe UVERBS_API_KEY_ERR = 0xFFFFFFFF, 1569ed3e5f4SJason Gunthorpe }; 1579ed3e5f4SJason Gunthorpe 1589ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id) 1599ed3e5f4SJason Gunthorpe { 1609ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 1619ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 1629ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) 1639ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1649ed3e5f4SJason Gunthorpe id = id + UVERBS_API_OBJ_KEY_NUM_CORE; 1659ed3e5f4SJason Gunthorpe } else { 1669ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) 1679ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1689ed3e5f4SJason Gunthorpe } 1699ed3e5f4SJason Gunthorpe 1709ed3e5f4SJason Gunthorpe return id << UVERBS_API_OBJ_KEY_SHIFT; 1719ed3e5f4SJason Gunthorpe } 1729ed3e5f4SJason Gunthorpe 1739ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key) 1749ed3e5f4SJason Gunthorpe { 1759ed3e5f4SJason Gunthorpe return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; 1769ed3e5f4SJason Gunthorpe } 1779ed3e5f4SJason Gunthorpe 1789ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) 1799ed3e5f4SJason Gunthorpe { 1809ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 1819ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 1829ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) 1839ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1849ed3e5f4SJason Gunthorpe id = id + UVERBS_API_METHOD_KEY_NUM_CORE; 1859ed3e5f4SJason Gunthorpe } else { 1869ed3e5f4SJason Gunthorpe id++; 1879ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) 1889ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1899ed3e5f4SJason Gunthorpe } 1909ed3e5f4SJason Gunthorpe 1919ed3e5f4SJason Gunthorpe return id << UVERBS_API_METHOD_KEY_SHIFT; 1929ed3e5f4SJason Gunthorpe } 1939ed3e5f4SJason Gunthorpe 1946884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_method(u32 id) 1956884c6c4SJason Gunthorpe { 1966884c6c4SJason Gunthorpe if (id >= UVERBS_API_WRITE_KEY_NUM) 1976884c6c4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1986884c6c4SJason Gunthorpe return UVERBS_API_METHOD_IS_WRITE | id; 1996884c6c4SJason Gunthorpe } 2006884c6c4SJason Gunthorpe 2016884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id) 2026884c6c4SJason Gunthorpe { 2036884c6c4SJason Gunthorpe if (id >= UVERBS_API_WRITE_KEY_NUM) 2046884c6c4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2056884c6c4SJason Gunthorpe return UVERBS_API_METHOD_IS_WRITE_EX | id; 2066884c6c4SJason Gunthorpe } 2076884c6c4SJason Gunthorpe 2086884c6c4SJason Gunthorpe static inline __attribute_const__ u32 2096884c6c4SJason Gunthorpe uapi_key_attr_to_ioctl_method(u32 attr_key) 2109ed3e5f4SJason Gunthorpe { 2119ed3e5f4SJason Gunthorpe return attr_key & 2129ed3e5f4SJason Gunthorpe (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); 2139ed3e5f4SJason Gunthorpe } 2149ed3e5f4SJason Gunthorpe 2159ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) 2169ed3e5f4SJason Gunthorpe { 2176884c6c4SJason Gunthorpe unsigned int method = key & UVERBS_API_METHOD_KEY_MASK; 2186884c6c4SJason Gunthorpe 2196884c6c4SJason Gunthorpe return method != 0 && method < UVERBS_API_METHOD_IS_WRITE && 2209ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) == 0; 2219ed3e5f4SJason Gunthorpe } 2229ed3e5f4SJason Gunthorpe 2236884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_method(u32 key) 2246884c6c4SJason Gunthorpe { 2256884c6c4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE; 2266884c6c4SJason Gunthorpe } 2276884c6c4SJason Gunthorpe 2286884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key) 2296884c6c4SJason Gunthorpe { 2306884c6c4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) == 2316884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE_EX; 2326884c6c4SJason Gunthorpe } 2336884c6c4SJason Gunthorpe 2349ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) 2359ed3e5f4SJason Gunthorpe { 2369ed3e5f4SJason Gunthorpe /* 0 is the method slot itself */ 2379ed3e5f4SJason Gunthorpe return ioctl_method_key + 1; 2389ed3e5f4SJason Gunthorpe } 2399ed3e5f4SJason Gunthorpe 2409ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id) 2419ed3e5f4SJason Gunthorpe { 2429ed3e5f4SJason Gunthorpe /* 2439ed3e5f4SJason Gunthorpe * The attr is designed to fit in the typical single radix tree node 2449ed3e5f4SJason Gunthorpe * of 64 entries. Since allmost all methods have driver attributes we 2459ed3e5f4SJason Gunthorpe * organize things so that the driver and core attributes interleave to 2469ed3e5f4SJason Gunthorpe * reduce the length of the attributes array in typical cases. 2479ed3e5f4SJason Gunthorpe */ 2489ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 2499ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 2509ed3e5f4SJason Gunthorpe id++; 2519ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 2529ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2539ed3e5f4SJason Gunthorpe id = (id << 1) | 0; 2549ed3e5f4SJason Gunthorpe } else { 2559ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 2569ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2579ed3e5f4SJason Gunthorpe id = (id << 1) | 1; 2589ed3e5f4SJason Gunthorpe } 2599ed3e5f4SJason Gunthorpe 2609ed3e5f4SJason Gunthorpe return id; 2619ed3e5f4SJason Gunthorpe } 2629ed3e5f4SJason Gunthorpe 2636884c6c4SJason Gunthorpe /* Only true for ioctl methods */ 2649ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key) 2659ed3e5f4SJason Gunthorpe { 2666884c6c4SJason Gunthorpe unsigned int method = key & UVERBS_API_METHOD_KEY_MASK; 2676884c6c4SJason Gunthorpe 2686884c6c4SJason Gunthorpe return method != 0 && method < UVERBS_API_METHOD_IS_WRITE && 2699ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) != 0; 2709ed3e5f4SJason Gunthorpe } 2719ed3e5f4SJason Gunthorpe 2729ed3e5f4SJason Gunthorpe /* 2739ed3e5f4SJason Gunthorpe * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), 2749ed3e5f4SJason Gunthorpe * basically it undoes the reservation of 0 in the ID numbering. attr_key 2759ed3e5f4SJason Gunthorpe * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of 2769ed3e5f4SJason Gunthorpe * uapi_key_attr(). 2779ed3e5f4SJason Gunthorpe */ 2789ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) 2799ed3e5f4SJason Gunthorpe { 2809ed3e5f4SJason Gunthorpe return attr_key - 1; 2819ed3e5f4SJason Gunthorpe } 2829ed3e5f4SJason Gunthorpe 28370cd20aeSGuy Levi static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey) 28470cd20aeSGuy Levi { 28570cd20aeSGuy Levi return attr_bkey + 1; 28670cd20aeSGuy Levi } 28770cd20aeSGuy Levi 2889ed3e5f4SJason Gunthorpe /* 2895009010fSMatan Barak * ======================================= 2905009010fSMatan Barak * Verbs definitions 2915009010fSMatan Barak * ======================================= 2925009010fSMatan Barak */ 2935009010fSMatan Barak 29409e3ebf8SMatan Barak struct uverbs_attr_def { 29509e3ebf8SMatan Barak u16 id; 29609e3ebf8SMatan Barak struct uverbs_attr_spec attr; 29709e3ebf8SMatan Barak }; 29809e3ebf8SMatan Barak 29909e3ebf8SMatan Barak struct uverbs_method_def { 30009e3ebf8SMatan Barak u16 id; 30109e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 30209e3ebf8SMatan Barak u32 flags; 30309e3ebf8SMatan Barak size_t num_attrs; 30409e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[]; 30515a1b4beSJason Gunthorpe int (*handler)(struct uverbs_attr_bundle *attrs); 30609e3ebf8SMatan Barak }; 30709e3ebf8SMatan Barak 3085009010fSMatan Barak struct uverbs_object_def { 30909e3ebf8SMatan Barak u16 id; 3105009010fSMatan Barak const struct uverbs_obj_type *type_attrs; 31109e3ebf8SMatan Barak size_t num_methods; 31209e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[]; 31309e3ebf8SMatan Barak }; 31409e3ebf8SMatan Barak 3150cbf432dSJason Gunthorpe enum uapi_definition_kind { 3160cbf432dSJason Gunthorpe UAPI_DEF_END = 0, 3176884c6c4SJason Gunthorpe UAPI_DEF_OBJECT_START, 3186884c6c4SJason Gunthorpe UAPI_DEF_WRITE, 3190cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE, 3200cbf432dSJason Gunthorpe UAPI_DEF_CHAIN, 3216829c1c2SJason Gunthorpe UAPI_DEF_IS_SUPPORTED_FUNC, 3226829c1c2SJason Gunthorpe UAPI_DEF_IS_SUPPORTED_DEV_FN, 3236829c1c2SJason Gunthorpe }; 3246829c1c2SJason Gunthorpe 3256829c1c2SJason Gunthorpe enum uapi_definition_scope { 3266829c1c2SJason Gunthorpe UAPI_SCOPE_OBJECT = 1, 327a140692aSJason Gunthorpe UAPI_SCOPE_METHOD = 2, 3285009010fSMatan Barak }; 3295009010fSMatan Barak 3300cbf432dSJason Gunthorpe struct uapi_definition { 3310cbf432dSJason Gunthorpe u8 kind; 3326829c1c2SJason Gunthorpe u8 scope; 3330cbf432dSJason Gunthorpe union { 3340cbf432dSJason Gunthorpe struct { 3350cbf432dSJason Gunthorpe u16 object_id; 3360cbf432dSJason Gunthorpe } object_start; 3376884c6c4SJason Gunthorpe struct { 3386884c6c4SJason Gunthorpe u16 command_num; 339669dac1eSJason Gunthorpe u8 is_ex:1; 340669dac1eSJason Gunthorpe u8 has_udata:1; 341669dac1eSJason Gunthorpe u8 has_resp:1; 342669dac1eSJason Gunthorpe u8 req_size; 343669dac1eSJason Gunthorpe u8 resp_size; 3446884c6c4SJason Gunthorpe } write; 3450cbf432dSJason Gunthorpe }; 3460cbf432dSJason Gunthorpe 3470cbf432dSJason Gunthorpe union { 3486829c1c2SJason Gunthorpe bool (*func_is_supported)(struct ib_device *device); 349974d6b4bSJason Gunthorpe int (*func_write)(struct uverbs_attr_bundle *attrs); 3500cbf432dSJason Gunthorpe const struct uapi_definition *chain; 3510cbf432dSJason Gunthorpe const struct uverbs_object_def *chain_obj_tree; 3526829c1c2SJason Gunthorpe size_t needs_fn_offset; 3530cbf432dSJason Gunthorpe }; 3540cbf432dSJason Gunthorpe }; 3550cbf432dSJason Gunthorpe 3566884c6c4SJason Gunthorpe /* Define things connected to object_id */ 3576884c6c4SJason Gunthorpe #define DECLARE_UVERBS_OBJECT(_object_id, ...) \ 3586884c6c4SJason Gunthorpe { \ 3596884c6c4SJason Gunthorpe .kind = UAPI_DEF_OBJECT_START, \ 3606884c6c4SJason Gunthorpe .object_start = { .object_id = _object_id }, \ 3616884c6c4SJason Gunthorpe }, \ 3626884c6c4SJason Gunthorpe ##__VA_ARGS__ 3636884c6c4SJason Gunthorpe 3646884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */ 365669dac1eSJason Gunthorpe #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \ 3666884c6c4SJason Gunthorpe { \ 3676884c6c4SJason Gunthorpe .kind = UAPI_DEF_WRITE, \ 3686884c6c4SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \ 3696884c6c4SJason Gunthorpe .write = { .is_ex = 0, .command_num = _command_num }, \ 3706884c6c4SJason Gunthorpe .func_write = _func, \ 371669dac1eSJason Gunthorpe _cmd_desc, \ 3726884c6c4SJason Gunthorpe }, \ 3736884c6c4SJason Gunthorpe ##__VA_ARGS__ 3746884c6c4SJason Gunthorpe 3756884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */ 376669dac1eSJason Gunthorpe #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \ 3776884c6c4SJason Gunthorpe { \ 3786884c6c4SJason Gunthorpe .kind = UAPI_DEF_WRITE, \ 3796884c6c4SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \ 3806884c6c4SJason Gunthorpe .write = { .is_ex = 1, .command_num = _command_num }, \ 381974d6b4bSJason Gunthorpe .func_write = _func, \ 382669dac1eSJason Gunthorpe _cmd_desc, \ 3836884c6c4SJason Gunthorpe }, \ 3846884c6c4SJason Gunthorpe ##__VA_ARGS__ 3856884c6c4SJason Gunthorpe 3866829c1c2SJason Gunthorpe /* 3876829c1c2SJason Gunthorpe * Object is only supported if the function pointer named ibdev_fn in struct 3886829c1c2SJason Gunthorpe * ib_device is not NULL. 3896829c1c2SJason Gunthorpe */ 3906829c1c2SJason Gunthorpe #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \ 3916829c1c2SJason Gunthorpe { \ 3926829c1c2SJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \ 3936829c1c2SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \ 3946829c1c2SJason Gunthorpe .needs_fn_offset = \ 3953023a1e9SKamal Heib offsetof(struct ib_device_ops, ibdev_fn) + \ 396bebcfe85SGustavo A. R. Silva BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \ 397bebcfe85SGustavo A. R. Silva ibdev_fn) != \ 3986829c1c2SJason Gunthorpe sizeof(void *)), \ 3996829c1c2SJason Gunthorpe } 4006829c1c2SJason Gunthorpe 401a140692aSJason Gunthorpe /* 402a140692aSJason Gunthorpe * Method is only supported if the function pointer named ibdev_fn in struct 403a140692aSJason Gunthorpe * ib_device is not NULL. 404a140692aSJason Gunthorpe */ 405a140692aSJason Gunthorpe #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \ 406a140692aSJason Gunthorpe { \ 407a140692aSJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \ 408a140692aSJason Gunthorpe .scope = UAPI_SCOPE_METHOD, \ 409a140692aSJason Gunthorpe .needs_fn_offset = \ 4103023a1e9SKamal Heib offsetof(struct ib_device_ops, ibdev_fn) + \ 411bebcfe85SGustavo A. R. Silva BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \ 412bebcfe85SGustavo A. R. Silva ibdev_fn) != \ 413a140692aSJason Gunthorpe sizeof(void *)), \ 414a140692aSJason Gunthorpe } 415a140692aSJason Gunthorpe 4166829c1c2SJason Gunthorpe /* Call a function to determine if the entire object is supported or not */ 4176829c1c2SJason Gunthorpe #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \ 4186829c1c2SJason Gunthorpe { \ 4196829c1c2SJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \ 4206829c1c2SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \ 4216829c1c2SJason Gunthorpe } 4226829c1c2SJason Gunthorpe 4230cbf432dSJason Gunthorpe /* Include another struct uapi_definition in this one */ 4240cbf432dSJason Gunthorpe #define UAPI_DEF_CHAIN(_def_var) \ 4250cbf432dSJason Gunthorpe { \ 4260cbf432dSJason Gunthorpe .kind = UAPI_DEF_CHAIN, .chain = _def_var, \ 4270cbf432dSJason Gunthorpe } 4280cbf432dSJason Gunthorpe 4290cbf432dSJason Gunthorpe /* Temporary until the tree base description is replaced */ 430a1462351SLeon Romanovsky #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \ 4310cbf432dSJason Gunthorpe { \ 4320cbf432dSJason Gunthorpe .kind = UAPI_DEF_CHAIN_OBJ_TREE, \ 4330cbf432dSJason Gunthorpe .object_start = { .object_id = _object_enum }, \ 4340cbf432dSJason Gunthorpe .chain_obj_tree = _object_ptr, \ 435a1462351SLeon Romanovsky }, \ 4360cbf432dSJason Gunthorpe ##__VA_ARGS__ 437a1462351SLeon Romanovsky #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \ 438a1462351SLeon Romanovsky UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum), \ 439a1462351SLeon Romanovsky ##__VA_ARGS__) 4400cbf432dSJason Gunthorpe 441d108dac0SJason Gunthorpe /* 442d108dac0SJason Gunthorpe * ======================================= 443d108dac0SJason Gunthorpe * Attribute Specifications 444d108dac0SJason Gunthorpe * ======================================= 445d108dac0SJason Gunthorpe */ 446c66db311SMatan Barak 447c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \ 448d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len 449422e3d37SJason Gunthorpe 450fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) 451fd44e385SYishai Hadas 452422e3d37SJason Gunthorpe /* 453422e3d37SJason Gunthorpe * Specifies a uapi structure that cannot be extended. The user must always 454422e3d37SJason Gunthorpe * supply the whole structure and nothing more. The structure must be declared 455422e3d37SJason Gunthorpe * in a header under include/uapi/rdma. 456422e3d37SJason Gunthorpe */ 457422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type) \ 458422e3d37SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 459422e3d37SJason Gunthorpe /* 460422e3d37SJason Gunthorpe * Specifies a uapi structure where the user must provide at least up to 461422e3d37SJason Gunthorpe * member 'last'. Anything after last and up until the end of the structure 462422e3d37SJason Gunthorpe * can be non-zero, anything longer than the end of the structure must be 463422e3d37SJason Gunthorpe * zero. The structure must be declared in a header under include/uapi/rdma. 464422e3d37SJason Gunthorpe */ 465422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last) \ 466422e3d37SJason Gunthorpe .zero_trailing = 1, \ 467ffd7339aSJason Gunthorpe UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type)) 468540cd692SJason Gunthorpe /* 469540cd692SJason Gunthorpe * Specifies at least min_len bytes must be passed in, but the amount can be 470540cd692SJason Gunthorpe * larger, up to the protocol maximum size. No check for zeroing is done. 471540cd692SJason Gunthorpe */ 472540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 473c66db311SMatan Barak 474d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 47583bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 47683bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1 47783bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0 47835410306SMatan Barak 47970cd20aeSGuy Levi /* 48070cd20aeSGuy Levi * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only 48170cd20aeSGuy Levi * READ\WRITE accesses are supported. 48270cd20aeSGuy Levi */ 48370cd20aeSGuy Levi #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \ 48470cd20aeSGuy Levi ...) \ 48570cd20aeSGuy Levi (&(const struct uverbs_attr_def){ \ 48670cd20aeSGuy Levi .id = (_attr_id) + \ 48770cd20aeSGuy Levi BUILD_BUG_ON_ZERO((_min_len) == 0 || \ 48870cd20aeSGuy Levi (_max_len) > \ 48970cd20aeSGuy Levi PAGE_SIZE / sizeof(void *) || \ 49070cd20aeSGuy Levi (_min_len) > (_max_len) || \ 49170cd20aeSGuy Levi (_access) == UVERBS_ACCESS_NEW || \ 49270cd20aeSGuy Levi (_access) == UVERBS_ACCESS_DESTROY), \ 49370cd20aeSGuy Levi .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \ 49470cd20aeSGuy Levi .u2.objs_arr.obj_type = _idr_type, \ 49570cd20aeSGuy Levi .u2.objs_arr.access = _access, \ 49670cd20aeSGuy Levi .u2.objs_arr.min_len = _min_len, \ 49770cd20aeSGuy Levi .u2.objs_arr.max_len = _max_len, \ 49870cd20aeSGuy Levi __VA_ARGS__ } }) 49970cd20aeSGuy Levi 5004d7e8cc5SYishai Hadas /* 5014d7e8cc5SYishai Hadas * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted, 5024d7e8cc5SYishai Hadas * the user must validate the type of the uobject instead. 5034d7e8cc5SYishai Hadas */ 5044d7e8cc5SYishai Hadas #define UVERBS_IDR_ANY_OBJECT 0xFFFF 5054d7e8cc5SYishai Hadas 506d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 5079a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 508d108dac0SJason Gunthorpe .id = _attr_id, \ 509d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 510d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 511d108dac0SJason Gunthorpe .u.obj.access = _access, \ 512d108dac0SJason Gunthorpe __VA_ARGS__ } }) 513d108dac0SJason Gunthorpe 514d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 5159a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 516d108dac0SJason Gunthorpe .id = (_attr_id) + \ 517d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 51835410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 519d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 520d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 521d108dac0SJason Gunthorpe .u.obj.access = _access, \ 522d108dac0SJason Gunthorpe __VA_ARGS__ } }) 52335410306SMatan Barak 524d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 5259a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 526d108dac0SJason Gunthorpe .id = _attr_id, \ 527d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 528d108dac0SJason Gunthorpe _type, \ 529d108dac0SJason Gunthorpe __VA_ARGS__ } }) 530d108dac0SJason Gunthorpe 531d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 5329a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 533d108dac0SJason Gunthorpe .id = _attr_id, \ 534d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 535d108dac0SJason Gunthorpe _type, \ 536d108dac0SJason Gunthorpe __VA_ARGS__ } }) 537d108dac0SJason Gunthorpe 538d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 539d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 5409a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 541d108dac0SJason Gunthorpe .id = _attr_id, \ 542d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 543d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 544d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 545d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 546d108dac0SJason Gunthorpe }) 547d108dac0SJason Gunthorpe 5480953fffeSMark Bloch /* An input value that is a member in the enum _enum_type. */ 5490953fffeSMark Bloch #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \ 5500953fffeSMark Bloch UVERBS_ATTR_PTR_IN( \ 5510953fffeSMark Bloch _attr_id, \ 5520953fffeSMark Bloch UVERBS_ATTR_SIZE( \ 5530953fffeSMark Bloch sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \ 5540953fffeSMark Bloch sizeof(u64)), \ 5550953fffeSMark Bloch __VA_ARGS__) 5560953fffeSMark Bloch 557d108dac0SJason Gunthorpe /* 558bccd0622SJason Gunthorpe * An input value that is a bitwise combination of values of _enum_type. 559bccd0622SJason Gunthorpe * This permits the flag value to be passed as either a u32 or u64, it must 560bccd0622SJason Gunthorpe * be retrieved via uverbs_get_flag(). 561bccd0622SJason Gunthorpe */ 562bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ 563bccd0622SJason Gunthorpe UVERBS_ATTR_PTR_IN( \ 564bccd0622SJason Gunthorpe _attr_id, \ 565bccd0622SJason Gunthorpe UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ 566bccd0622SJason Gunthorpe !sizeof(_enum_type *)), \ 567bccd0622SJason Gunthorpe sizeof(u64)), \ 568bccd0622SJason Gunthorpe __VA_ARGS__) 569bccd0622SJason Gunthorpe 570bccd0622SJason Gunthorpe /* 5716c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 5726c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 5736c61d2a5SJason Gunthorpe * spec. 5746c61d2a5SJason Gunthorpe */ 5756c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 5769a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 577540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 57807f05f40SJason Gunthorpe UA_OPTIONAL, \ 57907f05f40SJason Gunthorpe .is_udata = 1), \ 5809a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 581540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 58207f05f40SJason Gunthorpe UA_OPTIONAL, \ 58307f05f40SJason Gunthorpe .is_udata = 1) 5846c61d2a5SJason Gunthorpe 585fac9658cSMatan Barak /* ================================================= 586fac9658cSMatan Barak * Parsing infrastructure 587fac9658cSMatan Barak * ================================================= 588fac9658cSMatan Barak */ 589fac9658cSMatan Barak 5903a863577SJason Gunthorpe 591fac9658cSMatan Barak struct uverbs_ptr_attr { 5928762d149SMatan Barak /* 5938762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 5948762d149SMatan Barak * used. 5958762d149SMatan Barak */ 5968762d149SMatan Barak union { 5978762d149SMatan Barak void *ptr; 598fac9658cSMatan Barak u64 data; 5998762d149SMatan Barak }; 600fac9658cSMatan Barak u16 len; 6016a1f444fSJason Gunthorpe u16 uattr_idx; 602494c5580SMatan Barak u8 enum_id; 603fac9658cSMatan Barak }; 604fac9658cSMatan Barak 605f43dbebfSMatan Barak struct uverbs_obj_attr { 606f43dbebfSMatan Barak struct ib_uobject *uobject; 6073a863577SJason Gunthorpe const struct uverbs_api_attr *attr_elm; 608f43dbebfSMatan Barak }; 609f43dbebfSMatan Barak 61070cd20aeSGuy Levi struct uverbs_objs_arr_attr { 61170cd20aeSGuy Levi struct ib_uobject **uobjects; 61270cd20aeSGuy Levi u16 len; 61370cd20aeSGuy Levi }; 61470cd20aeSGuy Levi 615f43dbebfSMatan Barak struct uverbs_attr { 616fac9658cSMatan Barak union { 617fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 618f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 61970cd20aeSGuy Levi struct uverbs_objs_arr_attr objs_arr_attr; 620f43dbebfSMatan Barak }; 621fac9658cSMatan Barak }; 622f43dbebfSMatan Barak 623f43dbebfSMatan Barak struct uverbs_attr_bundle { 624ef87df2cSJason Gunthorpe struct ib_udata driver_udata; 625c2a939fdSJason Gunthorpe struct ib_udata ucore; 6264b3dd2bbSJason Gunthorpe struct ib_uverbs_file *ufile; 6273d9dfd06SShamir Rabinovitch struct ib_ucontext *context; 6280f63ef1dSLeon Romanovsky struct ib_uobject *uobject; 6293a863577SJason Gunthorpe DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN); 6303a863577SJason Gunthorpe struct uverbs_attr attrs[]; 631f43dbebfSMatan Barak }; 632f43dbebfSMatan Barak 63335410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 63435410306SMatan Barak unsigned int idx) 63535410306SMatan Barak { 6363a863577SJason Gunthorpe return test_bit(uapi_bkey_attr(uapi_key_attr(idx)), 6373a863577SJason Gunthorpe attrs_bundle->attr_present); 63835410306SMatan Barak } 63935410306SMatan Barak 640730623f4SShamir Rabinovitch /** 641730623f4SShamir Rabinovitch * rdma_udata_to_drv_context - Helper macro to get the driver's context out of 642730623f4SShamir Rabinovitch * ib_udata which is embedded in uverbs_attr_bundle. 643730623f4SShamir Rabinovitch * 644730623f4SShamir Rabinovitch * If udata is not NULL this cannot fail. Otherwise a NULL udata will result 645730623f4SShamir Rabinovitch * in a NULL ucontext pointer, as a safety precaution. Callers should be using 646730623f4SShamir Rabinovitch * 'udata' to determine if the driver call is in user or kernel mode, not 647730623f4SShamir Rabinovitch * 'ucontext'. 648730623f4SShamir Rabinovitch * 649730623f4SShamir Rabinovitch */ 650bfb972c5SArnd Bergmann static inline struct uverbs_attr_bundle * 651bfb972c5SArnd Bergmann rdma_udata_to_uverbs_attr_bundle(struct ib_udata *udata) 652bfb972c5SArnd Bergmann { 653bfb972c5SArnd Bergmann return container_of(udata, struct uverbs_attr_bundle, driver_udata); 654bfb972c5SArnd Bergmann } 655bfb972c5SArnd Bergmann 656730623f4SShamir Rabinovitch #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \ 657bfb972c5SArnd Bergmann (udata ? container_of(rdma_udata_to_uverbs_attr_bundle(udata)->context, \ 658bfb972c5SArnd Bergmann drv_dev_struct, member) : (drv_dev_struct *)NULL) 659730623f4SShamir Rabinovitch 66041b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 66141b2a71fSMatan Barak 662d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 663d70724f1SMatan Barak u16 idx) 664d70724f1SMatan Barak { 665d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 666d70724f1SMatan Barak return ERR_PTR(-ENOENT); 667d70724f1SMatan Barak 6683a863577SJason Gunthorpe return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))]; 669d70724f1SMatan Barak } 670d70724f1SMatan Barak 671494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 672494c5580SMatan Barak u16 idx) 673494c5580SMatan Barak { 674494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 675494c5580SMatan Barak 676494c5580SMatan Barak if (IS_ERR(attr)) 677494c5580SMatan Barak return PTR_ERR(attr); 678494c5580SMatan Barak 679494c5580SMatan Barak return attr->ptr_attr.enum_id; 680494c5580SMatan Barak } 681494c5580SMatan Barak 682be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 683be934ccaSAriel Levkovich u16 idx) 684be934ccaSAriel Levkovich { 685f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 686be934ccaSAriel Levkovich 687f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 688f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 689f4602cbbSJason Gunthorpe return ERR_CAST(attr); 690be934ccaSAriel Levkovich 691f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 692be934ccaSAriel Levkovich } 693be934ccaSAriel Levkovich 6943efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 6953efa3812SMatan Barak u16 idx) 6963efa3812SMatan Barak { 6973efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 6983efa3812SMatan Barak 6993efa3812SMatan Barak if (IS_ERR(attr)) 7003efa3812SMatan Barak return ERR_CAST(attr); 7013efa3812SMatan Barak 7023efa3812SMatan Barak return attr->obj_attr.uobject; 7033efa3812SMatan Barak } 7043efa3812SMatan Barak 7058762d149SMatan Barak static inline int 7068762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 7078762d149SMatan Barak { 7088762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 7098762d149SMatan Barak 7108762d149SMatan Barak if (IS_ERR(attr)) 7118762d149SMatan Barak return PTR_ERR(attr); 7128762d149SMatan Barak 7138762d149SMatan Barak return attr->ptr_attr.len; 7148762d149SMatan Barak } 7158762d149SMatan Barak 7160ac8903cSJason Gunthorpe void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle, 7170ac8903cSJason Gunthorpe u16 idx); 7180ac8903cSJason Gunthorpe 719cbfdd442SMoni Shoua /* 720cbfdd442SMoni Shoua * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr 721cbfdd442SMoni Shoua * attribute. 722cbfdd442SMoni Shoua * @attrs: The attribute bundle 723cbfdd442SMoni Shoua * @idx: The ID of the attribute 724cbfdd442SMoni Shoua * @elem_size: The size of the element in the array 725cbfdd442SMoni Shoua */ 726cbfdd442SMoni Shoua static inline int 727cbfdd442SMoni Shoua uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx, 728cbfdd442SMoni Shoua size_t elem_size) 729cbfdd442SMoni Shoua { 730cbfdd442SMoni Shoua int size = uverbs_attr_get_len(attrs, idx); 731cbfdd442SMoni Shoua 732cbfdd442SMoni Shoua if (size < 0) 733cbfdd442SMoni Shoua return size; 734cbfdd442SMoni Shoua 735cbfdd442SMoni Shoua if (size % elem_size) 736cbfdd442SMoni Shoua return -EINVAL; 737cbfdd442SMoni Shoua 738cbfdd442SMoni Shoua return size / elem_size; 739cbfdd442SMoni Shoua } 740cbfdd442SMoni Shoua 74170cd20aeSGuy Levi /** 74270cd20aeSGuy Levi * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for 74370cd20aeSGuy Levi * UVERBS_ATTR_TYPE_IDRS_ARRAY. 74470cd20aeSGuy Levi * @arr: Returned pointer to array of pointers for uobjects or NULL if 74570cd20aeSGuy Levi * the attribute isn't provided. 74670cd20aeSGuy Levi * 74770cd20aeSGuy Levi * Return: The array length or 0 if no attribute was provided. 74870cd20aeSGuy Levi */ 74970cd20aeSGuy Levi static inline int uverbs_attr_get_uobjs_arr( 75070cd20aeSGuy Levi const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx, 75170cd20aeSGuy Levi struct ib_uobject ***arr) 75270cd20aeSGuy Levi { 75370cd20aeSGuy Levi const struct uverbs_attr *attr = 75470cd20aeSGuy Levi uverbs_attr_get(attrs_bundle, attr_idx); 75570cd20aeSGuy Levi 75670cd20aeSGuy Levi if (IS_ERR(attr)) { 75770cd20aeSGuy Levi *arr = NULL; 75870cd20aeSGuy Levi return 0; 75970cd20aeSGuy Levi } 76070cd20aeSGuy Levi 76170cd20aeSGuy Levi *arr = attr->objs_arr_attr.uobjects; 76270cd20aeSGuy Levi 76370cd20aeSGuy Levi return attr->objs_arr_attr.len; 76470cd20aeSGuy Levi } 76570cd20aeSGuy Levi 76689d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 76789d9e8d3SMatan Barak { 76889d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 76989d9e8d3SMatan Barak } 77089d9e8d3SMatan Barak 7718762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 7728762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 7738762d149SMatan Barak { 7748762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 7758762d149SMatan Barak 7768762d149SMatan Barak if (IS_ERR(attr)) 7778762d149SMatan Barak return (void *)attr; 7788762d149SMatan Barak 7798762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 7808762d149SMatan Barak attr->ptr_attr.ptr; 7818762d149SMatan Barak } 7828762d149SMatan Barak 78389d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 784d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 78589d9e8d3SMatan Barak size_t idx, 78689d9e8d3SMatan Barak size_t size) 787d70724f1SMatan Barak { 788d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 789d70724f1SMatan Barak 790d70724f1SMatan Barak if (IS_ERR(attr)) 791d70724f1SMatan Barak return PTR_ERR(attr); 792d70724f1SMatan Barak 79389d9e8d3SMatan Barak /* 79489d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 795c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 796c66db311SMatan Barak * uverbs_copy_from_or_zero. 79789d9e8d3SMatan Barak */ 79889d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 79989d9e8d3SMatan Barak return -EINVAL; 80089d9e8d3SMatan Barak 80189d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 802d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 8032f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 8042f36028cSJason Gunthorpe attr->ptr_attr.len)) 805d70724f1SMatan Barak return -EFAULT; 806d70724f1SMatan Barak 807d70724f1SMatan Barak return 0; 808d70724f1SMatan Barak } 809d70724f1SMatan Barak 810c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 811c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 812c66db311SMatan Barak size_t idx, 813c66db311SMatan Barak size_t size) 814c66db311SMatan Barak { 815c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 816c66db311SMatan Barak size_t min_size; 817c66db311SMatan Barak 818c66db311SMatan Barak if (IS_ERR(attr)) 819c66db311SMatan Barak return PTR_ERR(attr); 820c66db311SMatan Barak 821c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 822c66db311SMatan Barak 823c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 824c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 825c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 826c66db311SMatan Barak min_size)) 827c66db311SMatan Barak return -EFAULT; 828c66db311SMatan Barak 829c66db311SMatan Barak if (size > min_size) 830c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 831c66db311SMatan Barak 832c66db311SMatan Barak return 0; 833c66db311SMatan Barak } 834c66db311SMatan Barak 835d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 83689d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 837d70724f1SMatan Barak 838c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 839c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 840c66db311SMatan Barak 8418313c10fSJason Gunthorpe static inline struct ib_ucontext * 8428313c10fSJason Gunthorpe ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs) 8438313c10fSJason Gunthorpe { 8448313c10fSJason Gunthorpe return ib_uverbs_get_ucontext_file(attrs->ufile); 8458313c10fSJason Gunthorpe } 8468313c10fSJason Gunthorpe 847bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 848bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 849bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 850bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 851bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 8526a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, 8536a1f444fSJason Gunthorpe const void *from, size_t size); 854461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, 855461bb2eeSJason Gunthorpe gfp_t flags); 856461bb2eeSJason Gunthorpe 857461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 858461bb2eeSJason Gunthorpe size_t size) 859461bb2eeSJason Gunthorpe { 860461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL); 861461bb2eeSJason Gunthorpe } 862461bb2eeSJason Gunthorpe 863461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 864461bb2eeSJason Gunthorpe size_t size) 865461bb2eeSJason Gunthorpe { 866461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); 867461bb2eeSJason Gunthorpe } 868e0da6899SAvihai Horon 869e0da6899SAvihai Horon static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle, 870e0da6899SAvihai Horon size_t n, size_t size) 871e0da6899SAvihai Horon { 872e0da6899SAvihai Horon size_t bytes; 873e0da6899SAvihai Horon 874e0da6899SAvihai Horon if (unlikely(check_mul_overflow(n, size, &bytes))) 875e0da6899SAvihai Horon return ERR_PTR(-EOVERFLOW); 876e0da6899SAvihai Horon return uverbs_zalloc(bundle, bytes); 877e0da6899SAvihai Horon } 8782904bb37SYishai Hadas 8792904bb37SYishai Hadas int _uverbs_get_const_signed(s64 *to, 8802904bb37SYishai Hadas const struct uverbs_attr_bundle *attrs_bundle, 8810953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 8820953fffeSMark Bloch s64 *def_val); 8832904bb37SYishai Hadas int _uverbs_get_const_unsigned(u64 *to, 8842904bb37SYishai Hadas const struct uverbs_attr_bundle *attrs_bundle, 8852904bb37SYishai Hadas size_t idx, u64 upper_bound, u64 *def_val); 8862e8039c6SMichael Guralnik int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 8872e8039c6SMichael Guralnik size_t idx, const void *from, size_t size); 888bccd0622SJason Gunthorpe #else 889bccd0622SJason Gunthorpe static inline int 890bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 891bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 892bccd0622SJason Gunthorpe { 893bccd0622SJason Gunthorpe return -EINVAL; 894bccd0622SJason Gunthorpe } 895bccd0622SJason Gunthorpe static inline int 896bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 897bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 898bccd0622SJason Gunthorpe { 899bccd0622SJason Gunthorpe return -EINVAL; 900bccd0622SJason Gunthorpe } 9016a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 9026a1f444fSJason Gunthorpe size_t idx, const void *from, size_t size) 9036a1f444fSJason Gunthorpe { 9046a1f444fSJason Gunthorpe return -EINVAL; 9056a1f444fSJason Gunthorpe } 906461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 907461bb2eeSJason Gunthorpe size_t size) 908461bb2eeSJason Gunthorpe { 909461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 910461bb2eeSJason Gunthorpe } 911461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 912461bb2eeSJason Gunthorpe size_t size) 913461bb2eeSJason Gunthorpe { 914461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 915461bb2eeSJason Gunthorpe } 9160953fffeSMark Bloch static inline int 9170953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 9180953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 9190953fffeSMark Bloch s64 *def_val) 9200953fffeSMark Bloch { 9210953fffeSMark Bloch return -EINVAL; 9220953fffeSMark Bloch } 9232e8039c6SMichael Guralnik static inline int 9242e8039c6SMichael Guralnik uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 9252e8039c6SMichael Guralnik size_t idx, const void *from, size_t size) 9262e8039c6SMichael Guralnik { 9272e8039c6SMichael Guralnik return -EINVAL; 9282e8039c6SMichael Guralnik } 929*dbb3e9dbSYueHaibing static inline int 930*dbb3e9dbSYueHaibing _uverbs_get_const_signed(s64 *to, 931*dbb3e9dbSYueHaibing const struct uverbs_attr_bundle *attrs_bundle, 9322904bb37SYishai Hadas size_t idx, s64 lower_bound, u64 upper_bound, 9332904bb37SYishai Hadas s64 *def_val) 9342904bb37SYishai Hadas { 9352904bb37SYishai Hadas return -EINVAL; 9362904bb37SYishai Hadas } 937*dbb3e9dbSYueHaibing static inline int 9382904bb37SYishai Hadas _uverbs_get_const_unsigned(u64 *to, 9392904bb37SYishai Hadas const struct uverbs_attr_bundle *attrs_bundle, 9402904bb37SYishai Hadas size_t idx, u64 upper_bound, u64 *def_val) 9412904bb37SYishai Hadas { 9422904bb37SYishai Hadas return -EINVAL; 9432904bb37SYishai Hadas } 944bccd0622SJason Gunthorpe #endif 945bccd0622SJason Gunthorpe 9462904bb37SYishai Hadas #define uverbs_get_const_signed(_to, _attrs_bundle, _idx) \ 9470953fffeSMark Bloch ({ \ 9480953fffeSMark Bloch s64 _val; \ 9492904bb37SYishai Hadas int _ret = \ 9502904bb37SYishai Hadas _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \ 9512904bb37SYishai Hadas type_min(typeof(*(_to))), \ 9522904bb37SYishai Hadas type_max(typeof(*(_to))), NULL); \ 9532904bb37SYishai Hadas (*(_to)) = _val; \ 9540953fffeSMark Bloch _ret; \ 9550953fffeSMark Bloch }) 9560953fffeSMark Bloch 9572904bb37SYishai Hadas #define uverbs_get_const_unsigned(_to, _attrs_bundle, _idx) \ 9582904bb37SYishai Hadas ({ \ 9592904bb37SYishai Hadas u64 _val; \ 9602904bb37SYishai Hadas int _ret = \ 9612904bb37SYishai Hadas _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \ 9622904bb37SYishai Hadas type_max(typeof(*(_to))), NULL); \ 9632904bb37SYishai Hadas (*(_to)) = _val; \ 9642904bb37SYishai Hadas _ret; \ 9652904bb37SYishai Hadas }) 9662904bb37SYishai Hadas 9672904bb37SYishai Hadas #define uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, _default) \ 9680953fffeSMark Bloch ({ \ 9690953fffeSMark Bloch s64 _val; \ 9700953fffeSMark Bloch s64 _def_val = _default; \ 9710953fffeSMark Bloch int _ret = \ 9722904bb37SYishai Hadas _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \ 9732904bb37SYishai Hadas type_min(typeof(*(_to))), \ 9742904bb37SYishai Hadas type_max(typeof(*(_to))), &_def_val); \ 9752904bb37SYishai Hadas (*(_to)) = _val; \ 9760953fffeSMark Bloch _ret; \ 9770953fffeSMark Bloch }) 9782904bb37SYishai Hadas 9792904bb37SYishai Hadas #define uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, _default) \ 9802904bb37SYishai Hadas ({ \ 9812904bb37SYishai Hadas u64 _val; \ 9822904bb37SYishai Hadas u64 _def_val = _default; \ 9832904bb37SYishai Hadas int _ret = \ 9842904bb37SYishai Hadas _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \ 9852904bb37SYishai Hadas type_max(typeof(*(_to))), &_def_val); \ 9862904bb37SYishai Hadas (*(_to)) = _val; \ 9872904bb37SYishai Hadas _ret; \ 9882904bb37SYishai Hadas }) 9892904bb37SYishai Hadas 9902904bb37SYishai Hadas #define uverbs_get_const(_to, _attrs_bundle, _idx) \ 9912904bb37SYishai Hadas (is_signed_type(typeof(*(_to))) ? \ 9922904bb37SYishai Hadas uverbs_get_const_signed(_to, _attrs_bundle, _idx) : \ 9932904bb37SYishai Hadas uverbs_get_const_unsigned(_to, _attrs_bundle, _idx)) \ 9942904bb37SYishai Hadas 9952904bb37SYishai Hadas #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \ 9962904bb37SYishai Hadas (is_signed_type(typeof(*(_to))) ? \ 9972904bb37SYishai Hadas uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, \ 9982904bb37SYishai Hadas _default) : \ 9992904bb37SYishai Hadas uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, \ 10002904bb37SYishai Hadas _default)) 10012904bb37SYishai Hadas 1002118620d3SMatan Barak #endif 1003