1*6bf9d8f6SLeon 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 */ 650730623f4SShamir Rabinovitch #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \ 651730623f4SShamir Rabinovitch (udata ? container_of(container_of(udata, struct uverbs_attr_bundle, \ 652730623f4SShamir Rabinovitch driver_udata) \ 653730623f4SShamir Rabinovitch ->context, \ 654730623f4SShamir Rabinovitch drv_dev_struct, member) : \ 655730623f4SShamir Rabinovitch (drv_dev_struct *)NULL) 656730623f4SShamir Rabinovitch 65741b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 65841b2a71fSMatan Barak 659d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 660d70724f1SMatan Barak u16 idx) 661d70724f1SMatan Barak { 662d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 663d70724f1SMatan Barak return ERR_PTR(-ENOENT); 664d70724f1SMatan Barak 6653a863577SJason Gunthorpe return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))]; 666d70724f1SMatan Barak } 667d70724f1SMatan Barak 668494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 669494c5580SMatan Barak u16 idx) 670494c5580SMatan Barak { 671494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 672494c5580SMatan Barak 673494c5580SMatan Barak if (IS_ERR(attr)) 674494c5580SMatan Barak return PTR_ERR(attr); 675494c5580SMatan Barak 676494c5580SMatan Barak return attr->ptr_attr.enum_id; 677494c5580SMatan Barak } 678494c5580SMatan Barak 679be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 680be934ccaSAriel Levkovich u16 idx) 681be934ccaSAriel Levkovich { 682f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 683be934ccaSAriel Levkovich 684f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 685f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 686f4602cbbSJason Gunthorpe return ERR_CAST(attr); 687be934ccaSAriel Levkovich 688f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 689be934ccaSAriel Levkovich } 690be934ccaSAriel Levkovich 6913efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 6923efa3812SMatan Barak u16 idx) 6933efa3812SMatan Barak { 6943efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 6953efa3812SMatan Barak 6963efa3812SMatan Barak if (IS_ERR(attr)) 6973efa3812SMatan Barak return ERR_CAST(attr); 6983efa3812SMatan Barak 6993efa3812SMatan Barak return attr->obj_attr.uobject; 7003efa3812SMatan Barak } 7013efa3812SMatan Barak 7028762d149SMatan Barak static inline int 7038762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 7048762d149SMatan Barak { 7058762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 7068762d149SMatan Barak 7078762d149SMatan Barak if (IS_ERR(attr)) 7088762d149SMatan Barak return PTR_ERR(attr); 7098762d149SMatan Barak 7108762d149SMatan Barak return attr->ptr_attr.len; 7118762d149SMatan Barak } 7128762d149SMatan Barak 7130ac8903cSJason Gunthorpe void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle, 7140ac8903cSJason Gunthorpe u16 idx); 7150ac8903cSJason Gunthorpe 716cbfdd442SMoni Shoua /* 717cbfdd442SMoni Shoua * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr 718cbfdd442SMoni Shoua * attribute. 719cbfdd442SMoni Shoua * @attrs: The attribute bundle 720cbfdd442SMoni Shoua * @idx: The ID of the attribute 721cbfdd442SMoni Shoua * @elem_size: The size of the element in the array 722cbfdd442SMoni Shoua */ 723cbfdd442SMoni Shoua static inline int 724cbfdd442SMoni Shoua uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx, 725cbfdd442SMoni Shoua size_t elem_size) 726cbfdd442SMoni Shoua { 727cbfdd442SMoni Shoua int size = uverbs_attr_get_len(attrs, idx); 728cbfdd442SMoni Shoua 729cbfdd442SMoni Shoua if (size < 0) 730cbfdd442SMoni Shoua return size; 731cbfdd442SMoni Shoua 732cbfdd442SMoni Shoua if (size % elem_size) 733cbfdd442SMoni Shoua return -EINVAL; 734cbfdd442SMoni Shoua 735cbfdd442SMoni Shoua return size / elem_size; 736cbfdd442SMoni Shoua } 737cbfdd442SMoni Shoua 73870cd20aeSGuy Levi /** 73970cd20aeSGuy Levi * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for 74070cd20aeSGuy Levi * UVERBS_ATTR_TYPE_IDRS_ARRAY. 74170cd20aeSGuy Levi * @arr: Returned pointer to array of pointers for uobjects or NULL if 74270cd20aeSGuy Levi * the attribute isn't provided. 74370cd20aeSGuy Levi * 74470cd20aeSGuy Levi * Return: The array length or 0 if no attribute was provided. 74570cd20aeSGuy Levi */ 74670cd20aeSGuy Levi static inline int uverbs_attr_get_uobjs_arr( 74770cd20aeSGuy Levi const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx, 74870cd20aeSGuy Levi struct ib_uobject ***arr) 74970cd20aeSGuy Levi { 75070cd20aeSGuy Levi const struct uverbs_attr *attr = 75170cd20aeSGuy Levi uverbs_attr_get(attrs_bundle, attr_idx); 75270cd20aeSGuy Levi 75370cd20aeSGuy Levi if (IS_ERR(attr)) { 75470cd20aeSGuy Levi *arr = NULL; 75570cd20aeSGuy Levi return 0; 75670cd20aeSGuy Levi } 75770cd20aeSGuy Levi 75870cd20aeSGuy Levi *arr = attr->objs_arr_attr.uobjects; 75970cd20aeSGuy Levi 76070cd20aeSGuy Levi return attr->objs_arr_attr.len; 76170cd20aeSGuy Levi } 76270cd20aeSGuy Levi 76389d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 76489d9e8d3SMatan Barak { 76589d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 76689d9e8d3SMatan Barak } 76789d9e8d3SMatan Barak 7688762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 7698762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 7708762d149SMatan Barak { 7718762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 7728762d149SMatan Barak 7738762d149SMatan Barak if (IS_ERR(attr)) 7748762d149SMatan Barak return (void *)attr; 7758762d149SMatan Barak 7768762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 7778762d149SMatan Barak attr->ptr_attr.ptr; 7788762d149SMatan Barak } 7798762d149SMatan Barak 78089d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 781d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 78289d9e8d3SMatan Barak size_t idx, 78389d9e8d3SMatan Barak size_t size) 784d70724f1SMatan Barak { 785d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 786d70724f1SMatan Barak 787d70724f1SMatan Barak if (IS_ERR(attr)) 788d70724f1SMatan Barak return PTR_ERR(attr); 789d70724f1SMatan Barak 79089d9e8d3SMatan Barak /* 79189d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 792c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 793c66db311SMatan Barak * uverbs_copy_from_or_zero. 79489d9e8d3SMatan Barak */ 79589d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 79689d9e8d3SMatan Barak return -EINVAL; 79789d9e8d3SMatan Barak 79889d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 799d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 8002f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 8012f36028cSJason Gunthorpe attr->ptr_attr.len)) 802d70724f1SMatan Barak return -EFAULT; 803d70724f1SMatan Barak 804d70724f1SMatan Barak return 0; 805d70724f1SMatan Barak } 806d70724f1SMatan Barak 807c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 808c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 809c66db311SMatan Barak size_t idx, 810c66db311SMatan Barak size_t size) 811c66db311SMatan Barak { 812c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 813c66db311SMatan Barak size_t min_size; 814c66db311SMatan Barak 815c66db311SMatan Barak if (IS_ERR(attr)) 816c66db311SMatan Barak return PTR_ERR(attr); 817c66db311SMatan Barak 818c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 819c66db311SMatan Barak 820c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 821c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 822c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 823c66db311SMatan Barak min_size)) 824c66db311SMatan Barak return -EFAULT; 825c66db311SMatan Barak 826c66db311SMatan Barak if (size > min_size) 827c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 828c66db311SMatan Barak 829c66db311SMatan Barak return 0; 830c66db311SMatan Barak } 831c66db311SMatan Barak 832d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 83389d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 834d70724f1SMatan Barak 835c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 836c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 837c66db311SMatan Barak 8388313c10fSJason Gunthorpe static inline struct ib_ucontext * 8398313c10fSJason Gunthorpe ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs) 8408313c10fSJason Gunthorpe { 8418313c10fSJason Gunthorpe return ib_uverbs_get_ucontext_file(attrs->ufile); 8428313c10fSJason Gunthorpe } 8438313c10fSJason Gunthorpe 844bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 845bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 846bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 847bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 848bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 8496a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, 8506a1f444fSJason Gunthorpe const void *from, size_t size); 851461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, 852461bb2eeSJason Gunthorpe gfp_t flags); 853461bb2eeSJason Gunthorpe 854461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 855461bb2eeSJason Gunthorpe size_t size) 856461bb2eeSJason Gunthorpe { 857461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL); 858461bb2eeSJason Gunthorpe } 859461bb2eeSJason Gunthorpe 860461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 861461bb2eeSJason Gunthorpe size_t size) 862461bb2eeSJason Gunthorpe { 863461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); 864461bb2eeSJason Gunthorpe } 8650953fffeSMark Bloch int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 8660953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 8670953fffeSMark Bloch s64 *def_val); 8682e8039c6SMichael Guralnik int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 8692e8039c6SMichael Guralnik size_t idx, const void *from, size_t size); 870bccd0622SJason Gunthorpe #else 871bccd0622SJason Gunthorpe static inline int 872bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 873bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 874bccd0622SJason Gunthorpe { 875bccd0622SJason Gunthorpe return -EINVAL; 876bccd0622SJason Gunthorpe } 877bccd0622SJason Gunthorpe static inline int 878bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 879bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 880bccd0622SJason Gunthorpe { 881bccd0622SJason Gunthorpe return -EINVAL; 882bccd0622SJason Gunthorpe } 8836a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 8846a1f444fSJason Gunthorpe size_t idx, const void *from, size_t size) 8856a1f444fSJason Gunthorpe { 8866a1f444fSJason Gunthorpe return -EINVAL; 8876a1f444fSJason Gunthorpe } 888461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 889461bb2eeSJason Gunthorpe size_t size) 890461bb2eeSJason Gunthorpe { 891461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 892461bb2eeSJason Gunthorpe } 893461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 894461bb2eeSJason Gunthorpe size_t size) 895461bb2eeSJason Gunthorpe { 896461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 897461bb2eeSJason Gunthorpe } 8980953fffeSMark Bloch static inline int 8990953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 9000953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 9010953fffeSMark Bloch s64 *def_val) 9020953fffeSMark Bloch { 9030953fffeSMark Bloch return -EINVAL; 9040953fffeSMark Bloch } 9052e8039c6SMichael Guralnik static inline int 9062e8039c6SMichael Guralnik uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 9072e8039c6SMichael Guralnik size_t idx, const void *from, size_t size) 9082e8039c6SMichael Guralnik { 9092e8039c6SMichael Guralnik return -EINVAL; 9102e8039c6SMichael Guralnik } 911bccd0622SJason Gunthorpe #endif 912bccd0622SJason Gunthorpe 9130953fffeSMark Bloch #define uverbs_get_const(_to, _attrs_bundle, _idx) \ 9140953fffeSMark Bloch ({ \ 9150953fffeSMark Bloch s64 _val; \ 9160953fffeSMark Bloch int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \ 9170953fffeSMark Bloch type_min(typeof(*_to)), \ 9180953fffeSMark Bloch type_max(typeof(*_to)), NULL); \ 9190953fffeSMark Bloch (*_to) = _val; \ 9200953fffeSMark Bloch _ret; \ 9210953fffeSMark Bloch }) 9220953fffeSMark Bloch 9230953fffeSMark Bloch #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \ 9240953fffeSMark Bloch ({ \ 9250953fffeSMark Bloch s64 _val; \ 9260953fffeSMark Bloch s64 _def_val = _default; \ 9270953fffeSMark Bloch int _ret = \ 9280953fffeSMark Bloch _uverbs_get_const(&_val, _attrs_bundle, _idx, \ 9290953fffeSMark Bloch type_min(typeof(*_to)), \ 9300953fffeSMark Bloch type_max(typeof(*_to)), &_def_val); \ 9310953fffeSMark Bloch (*_to) = _val; \ 9320953fffeSMark Bloch _ret; \ 9330953fffeSMark Bloch }) 934118620d3SMatan Barak #endif 935