1a0aa309cSMatan Barak /* 2a0aa309cSMatan Barak * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. 3a0aa309cSMatan Barak * 4a0aa309cSMatan Barak * This software is available to you under a choice of one of two 5a0aa309cSMatan Barak * licenses. You may choose to be licensed under the terms of the GNU 6a0aa309cSMatan Barak * General Public License (GPL) Version 2, available from the file 7a0aa309cSMatan Barak * COPYING in the main directory of this source tree, or the 8a0aa309cSMatan Barak * OpenIB.org BSD license below: 9a0aa309cSMatan Barak * 10a0aa309cSMatan Barak * Redistribution and use in source and binary forms, with or 11a0aa309cSMatan Barak * without modification, are permitted provided that the following 12a0aa309cSMatan Barak * conditions are met: 13a0aa309cSMatan Barak * 14a0aa309cSMatan Barak * - Redistributions of source code must retain the above 15a0aa309cSMatan Barak * copyright notice, this list of conditions and the following 16a0aa309cSMatan Barak * disclaimer. 17a0aa309cSMatan Barak * 18a0aa309cSMatan Barak * - Redistributions in binary form must reproduce the above 19a0aa309cSMatan Barak * copyright notice, this list of conditions and the following 20a0aa309cSMatan Barak * disclaimer in the documentation and/or other materials 21a0aa309cSMatan Barak * provided with the distribution. 22a0aa309cSMatan Barak * 23a0aa309cSMatan Barak * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24a0aa309cSMatan Barak * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25a0aa309cSMatan Barak * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26a0aa309cSMatan Barak * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27a0aa309cSMatan Barak * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28a0aa309cSMatan Barak * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29a0aa309cSMatan Barak * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30a0aa309cSMatan Barak * SOFTWARE. 31a0aa309cSMatan Barak */ 32a0aa309cSMatan Barak 33a0aa309cSMatan Barak #ifndef _UVERBS_IOCTL_ 34a0aa309cSMatan Barak #define _UVERBS_IOCTL_ 35a0aa309cSMatan Barak 36a0aa309cSMatan Barak #include <rdma/uverbs_types.h> 3735410306SMatan Barak #include <linux/uaccess.h> 3835410306SMatan Barak #include <rdma/rdma_user_ioctl.h> 39d70724f1SMatan Barak #include <rdma/ib_user_ioctl_verbs.h> 401f7ff9d5SMatan Barak #include <rdma/ib_user_ioctl_cmds.h> 41a0aa309cSMatan Barak 42a0aa309cSMatan Barak /* 43a0aa309cSMatan Barak * ======================================= 44a0aa309cSMatan Barak * Verbs action specifications 45a0aa309cSMatan Barak * ======================================= 46a0aa309cSMatan Barak */ 47a0aa309cSMatan Barak 48f43dbebfSMatan Barak enum uverbs_attr_type { 49f43dbebfSMatan Barak UVERBS_ATTR_TYPE_NA, 50fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_IN, 51fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_OUT, 52f43dbebfSMatan Barak UVERBS_ATTR_TYPE_IDR, 53f43dbebfSMatan Barak UVERBS_ATTR_TYPE_FD, 54494c5580SMatan Barak UVERBS_ATTR_TYPE_ENUM_IN, 5570cd20aeSGuy Levi UVERBS_ATTR_TYPE_IDRS_ARRAY, 56f43dbebfSMatan Barak }; 57f43dbebfSMatan Barak 58a0aa309cSMatan Barak enum uverbs_obj_access { 59a0aa309cSMatan Barak UVERBS_ACCESS_READ, 60a0aa309cSMatan Barak UVERBS_ACCESS_WRITE, 61a0aa309cSMatan Barak UVERBS_ACCESS_NEW, 62a0aa309cSMatan Barak UVERBS_ACCESS_DESTROY 63a0aa309cSMatan Barak }; 64a0aa309cSMatan Barak 651f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */ 6683bb4442SJason Gunthorpe /* good size 16 */ 67f43dbebfSMatan Barak struct uverbs_attr_spec { 68d108dac0SJason Gunthorpe u8 type; 6983bb4442SJason Gunthorpe 7083bb4442SJason Gunthorpe /* 71422e3d37SJason Gunthorpe * Support extending attributes by length. Allow the user to provide 72422e3d37SJason Gunthorpe * more bytes than ptr.len, but check that everything after is zero'd 73422e3d37SJason Gunthorpe * by the user. 7483bb4442SJason Gunthorpe */ 75422e3d37SJason Gunthorpe u8 zero_trailing:1; 7683bb4442SJason Gunthorpe /* 7783bb4442SJason Gunthorpe * Valid only for PTR_IN. Allocate and copy the data inside 7883bb4442SJason Gunthorpe * the parser 7983bb4442SJason Gunthorpe */ 8083bb4442SJason Gunthorpe u8 alloc_and_copy:1; 8183bb4442SJason Gunthorpe u8 mandatory:1; 82d108dac0SJason Gunthorpe 83fac9658cSMatan Barak union { 84f43dbebfSMatan Barak struct { 85c66db311SMatan Barak /* Current known size to kernel */ 861f07e08fSMatan Barak u16 len; 87c66db311SMatan Barak /* User isn't allowed to provide something < min_len */ 88c66db311SMatan Barak u16 min_len; 891f07e08fSMatan Barak } ptr; 90d108dac0SJason Gunthorpe 911f07e08fSMatan Barak struct { 92f43dbebfSMatan Barak /* 93f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean 94f43dbebfSMatan Barak * the type id within the namespace. 95f43dbebfSMatan Barak */ 96f43dbebfSMatan Barak u16 obj_type; 97f43dbebfSMatan Barak u8 access; 98f43dbebfSMatan Barak } obj; 99d108dac0SJason Gunthorpe 100494c5580SMatan Barak struct { 101494c5580SMatan Barak u8 num_elems; 102d108dac0SJason Gunthorpe } enum_def; 103d108dac0SJason Gunthorpe } u; 104d108dac0SJason Gunthorpe 10570cd20aeSGuy Levi /* This weird split lets us remove some padding */ 106d108dac0SJason Gunthorpe union { 107d108dac0SJason Gunthorpe struct { 108494c5580SMatan Barak /* 109494c5580SMatan Barak * The enum attribute can select one of the attributes 110494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN 111494c5580SMatan Barak * attributes are supported in the ids array. 112494c5580SMatan Barak */ 113494c5580SMatan Barak const struct uverbs_attr_spec *ids; 114494c5580SMatan Barak } enum_def; 11570cd20aeSGuy Levi 11670cd20aeSGuy Levi struct { 11770cd20aeSGuy Levi /* 11870cd20aeSGuy Levi * higher bits mean the namespace and lower bits mean 11970cd20aeSGuy Levi * the type id within the namespace. 12070cd20aeSGuy Levi */ 12170cd20aeSGuy Levi u16 obj_type; 12270cd20aeSGuy Levi u16 min_len; 12370cd20aeSGuy Levi u16 max_len; 12470cd20aeSGuy Levi u8 access; 12570cd20aeSGuy Levi } objs_arr; 126d108dac0SJason Gunthorpe } u2; 127fac9658cSMatan Barak }; 128f43dbebfSMatan Barak 1295009010fSMatan Barak /* 1309ed3e5f4SJason Gunthorpe * Information about the API is loaded into a radix tree. For IOCTL we start 1319ed3e5f4SJason Gunthorpe * with a tuple of: 1329ed3e5f4SJason Gunthorpe * object_id, attr_id, method_id 1339ed3e5f4SJason Gunthorpe * 1349ed3e5f4SJason Gunthorpe * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based 1359ed3e5f4SJason Gunthorpe * on the current kernel support this is compressed into 16 bit key for the 1369ed3e5f4SJason Gunthorpe * radix tree. Since this compression is entirely internal to the kernel the 1379ed3e5f4SJason Gunthorpe * below limits can be revised if the kernel gains additional data. 1389ed3e5f4SJason Gunthorpe * 1399ed3e5f4SJason Gunthorpe * With 64 leafs per node this is a 3 level radix tree. 1409ed3e5f4SJason Gunthorpe * 1419ed3e5f4SJason Gunthorpe * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns 1429ed3e5f4SJason Gunthorpe * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. 1436884c6c4SJason Gunthorpe * 1446884c6c4SJason Gunthorpe * This also encodes the tables for the write() and write() extended commands 1456884c6c4SJason Gunthorpe * using the coding 1466884c6c4SJason Gunthorpe * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command # 1476884c6c4SJason Gunthorpe * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex # 1486884c6c4SJason Gunthorpe * ie the WRITE path is treated as a special method type in the ioctl 1496884c6c4SJason Gunthorpe * framework. 1509ed3e5f4SJason Gunthorpe */ 1519ed3e5f4SJason Gunthorpe enum uapi_radix_data { 1529ed3e5f4SJason Gunthorpe UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, 1539ed3e5f4SJason Gunthorpe 1549ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_BITS = 6, 1559ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), 1569ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, 1576884c6c4SJason Gunthorpe UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS, 1589ed3e5f4SJason Gunthorpe 1599ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS = 5, 1609ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, 1616884c6c4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE = 22, 1626884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT, 1636884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT, 1646884c6c4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_DRIVER = 1656884c6c4SJason Gunthorpe (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) - 1669ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE, 1679ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_MASK = GENMASK( 1689ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, 1699ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT), 1709ed3e5f4SJason Gunthorpe 1719ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_BITS = 5, 1729ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_SHIFT = 1739ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, 1749ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_NUM_CORE = 24, 1759ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_NUM_DRIVER = 1769ed3e5f4SJason Gunthorpe (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, 1779ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), 1789ed3e5f4SJason Gunthorpe 1799ed3e5f4SJason Gunthorpe /* This id guaranteed to not exist in the radix tree */ 1809ed3e5f4SJason Gunthorpe UVERBS_API_KEY_ERR = 0xFFFFFFFF, 1819ed3e5f4SJason Gunthorpe }; 1829ed3e5f4SJason Gunthorpe 1839ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id) 1849ed3e5f4SJason Gunthorpe { 1859ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 1869ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 1879ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) 1889ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1899ed3e5f4SJason Gunthorpe id = id + UVERBS_API_OBJ_KEY_NUM_CORE; 1909ed3e5f4SJason Gunthorpe } else { 1919ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) 1929ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 1939ed3e5f4SJason Gunthorpe } 1949ed3e5f4SJason Gunthorpe 1959ed3e5f4SJason Gunthorpe return id << UVERBS_API_OBJ_KEY_SHIFT; 1969ed3e5f4SJason Gunthorpe } 1979ed3e5f4SJason Gunthorpe 1989ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key) 1999ed3e5f4SJason Gunthorpe { 2009ed3e5f4SJason Gunthorpe return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; 2019ed3e5f4SJason Gunthorpe } 2029ed3e5f4SJason Gunthorpe 2039ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) 2049ed3e5f4SJason Gunthorpe { 2059ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 2069ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 2079ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) 2089ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2099ed3e5f4SJason Gunthorpe id = id + UVERBS_API_METHOD_KEY_NUM_CORE; 2109ed3e5f4SJason Gunthorpe } else { 2119ed3e5f4SJason Gunthorpe id++; 2129ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) 2139ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2149ed3e5f4SJason Gunthorpe } 2159ed3e5f4SJason Gunthorpe 2169ed3e5f4SJason Gunthorpe return id << UVERBS_API_METHOD_KEY_SHIFT; 2179ed3e5f4SJason Gunthorpe } 2189ed3e5f4SJason Gunthorpe 2196884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_method(u32 id) 2206884c6c4SJason Gunthorpe { 2216884c6c4SJason Gunthorpe if (id >= UVERBS_API_WRITE_KEY_NUM) 2226884c6c4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2236884c6c4SJason Gunthorpe return UVERBS_API_METHOD_IS_WRITE | id; 2246884c6c4SJason Gunthorpe } 2256884c6c4SJason Gunthorpe 2266884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id) 2276884c6c4SJason Gunthorpe { 2286884c6c4SJason Gunthorpe if (id >= UVERBS_API_WRITE_KEY_NUM) 2296884c6c4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2306884c6c4SJason Gunthorpe return UVERBS_API_METHOD_IS_WRITE_EX | id; 2316884c6c4SJason Gunthorpe } 2326884c6c4SJason Gunthorpe 2336884c6c4SJason Gunthorpe static inline __attribute_const__ u32 2346884c6c4SJason Gunthorpe uapi_key_attr_to_ioctl_method(u32 attr_key) 2359ed3e5f4SJason Gunthorpe { 2369ed3e5f4SJason Gunthorpe return attr_key & 2379ed3e5f4SJason Gunthorpe (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); 2389ed3e5f4SJason Gunthorpe } 2399ed3e5f4SJason Gunthorpe 2409ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) 2419ed3e5f4SJason Gunthorpe { 2426884c6c4SJason Gunthorpe unsigned int method = key & UVERBS_API_METHOD_KEY_MASK; 2436884c6c4SJason Gunthorpe 2446884c6c4SJason Gunthorpe return method != 0 && method < UVERBS_API_METHOD_IS_WRITE && 2459ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) == 0; 2469ed3e5f4SJason Gunthorpe } 2479ed3e5f4SJason Gunthorpe 2486884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_method(u32 key) 2496884c6c4SJason Gunthorpe { 2506884c6c4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE; 2516884c6c4SJason Gunthorpe } 2526884c6c4SJason Gunthorpe 2536884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key) 2546884c6c4SJason Gunthorpe { 2556884c6c4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) == 2566884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE_EX; 2576884c6c4SJason Gunthorpe } 2586884c6c4SJason Gunthorpe 2599ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) 2609ed3e5f4SJason Gunthorpe { 2619ed3e5f4SJason Gunthorpe /* 0 is the method slot itself */ 2629ed3e5f4SJason Gunthorpe return ioctl_method_key + 1; 2639ed3e5f4SJason Gunthorpe } 2649ed3e5f4SJason Gunthorpe 2659ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id) 2669ed3e5f4SJason Gunthorpe { 2679ed3e5f4SJason Gunthorpe /* 2689ed3e5f4SJason Gunthorpe * The attr is designed to fit in the typical single radix tree node 2699ed3e5f4SJason Gunthorpe * of 64 entries. Since allmost all methods have driver attributes we 2709ed3e5f4SJason Gunthorpe * organize things so that the driver and core attributes interleave to 2719ed3e5f4SJason Gunthorpe * reduce the length of the attributes array in typical cases. 2729ed3e5f4SJason Gunthorpe */ 2739ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) { 2749ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG; 2759ed3e5f4SJason Gunthorpe id++; 2769ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 2779ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2789ed3e5f4SJason Gunthorpe id = (id << 1) | 0; 2799ed3e5f4SJason Gunthorpe } else { 2809ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 2819ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR; 2829ed3e5f4SJason Gunthorpe id = (id << 1) | 1; 2839ed3e5f4SJason Gunthorpe } 2849ed3e5f4SJason Gunthorpe 2859ed3e5f4SJason Gunthorpe return id; 2869ed3e5f4SJason Gunthorpe } 2879ed3e5f4SJason Gunthorpe 2886884c6c4SJason Gunthorpe /* Only true for ioctl methods */ 2899ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key) 2909ed3e5f4SJason Gunthorpe { 2916884c6c4SJason Gunthorpe unsigned int method = key & UVERBS_API_METHOD_KEY_MASK; 2926884c6c4SJason Gunthorpe 2936884c6c4SJason Gunthorpe return method != 0 && method < UVERBS_API_METHOD_IS_WRITE && 2949ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) != 0; 2959ed3e5f4SJason Gunthorpe } 2969ed3e5f4SJason Gunthorpe 2979ed3e5f4SJason Gunthorpe /* 2989ed3e5f4SJason Gunthorpe * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), 2999ed3e5f4SJason Gunthorpe * basically it undoes the reservation of 0 in the ID numbering. attr_key 3009ed3e5f4SJason Gunthorpe * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of 3019ed3e5f4SJason Gunthorpe * uapi_key_attr(). 3029ed3e5f4SJason Gunthorpe */ 3039ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) 3049ed3e5f4SJason Gunthorpe { 3059ed3e5f4SJason Gunthorpe return attr_key - 1; 3069ed3e5f4SJason Gunthorpe } 3079ed3e5f4SJason Gunthorpe 30870cd20aeSGuy Levi static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey) 30970cd20aeSGuy Levi { 31070cd20aeSGuy Levi return attr_bkey + 1; 31170cd20aeSGuy Levi } 31270cd20aeSGuy Levi 3139ed3e5f4SJason Gunthorpe /* 3145009010fSMatan Barak * ======================================= 3155009010fSMatan Barak * Verbs definitions 3165009010fSMatan Barak * ======================================= 3175009010fSMatan Barak */ 3185009010fSMatan Barak 31909e3ebf8SMatan Barak struct uverbs_attr_def { 32009e3ebf8SMatan Barak u16 id; 32109e3ebf8SMatan Barak struct uverbs_attr_spec attr; 32209e3ebf8SMatan Barak }; 32309e3ebf8SMatan Barak 32409e3ebf8SMatan Barak struct uverbs_method_def { 32509e3ebf8SMatan Barak u16 id; 32609e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 32709e3ebf8SMatan Barak u32 flags; 32809e3ebf8SMatan Barak size_t num_attrs; 32909e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[]; 330e83f0ecdSJason Gunthorpe int (*handler)(struct ib_uverbs_file *ufile, 33109e3ebf8SMatan Barak struct uverbs_attr_bundle *ctx); 33209e3ebf8SMatan Barak }; 33309e3ebf8SMatan Barak 3345009010fSMatan Barak struct uverbs_object_def { 33509e3ebf8SMatan Barak u16 id; 3365009010fSMatan Barak const struct uverbs_obj_type *type_attrs; 33709e3ebf8SMatan Barak size_t num_methods; 33809e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[]; 33909e3ebf8SMatan Barak }; 34009e3ebf8SMatan Barak 3410cbf432dSJason Gunthorpe enum uapi_definition_kind { 3420cbf432dSJason Gunthorpe UAPI_DEF_END = 0, 3436884c6c4SJason Gunthorpe UAPI_DEF_OBJECT_START, 3446884c6c4SJason Gunthorpe UAPI_DEF_WRITE, 3450cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE, 3460cbf432dSJason Gunthorpe UAPI_DEF_CHAIN, 3476829c1c2SJason Gunthorpe UAPI_DEF_IS_SUPPORTED_FUNC, 3486829c1c2SJason Gunthorpe UAPI_DEF_IS_SUPPORTED_DEV_FN, 3496829c1c2SJason Gunthorpe }; 3506829c1c2SJason Gunthorpe 3516829c1c2SJason Gunthorpe enum uapi_definition_scope { 3526829c1c2SJason Gunthorpe UAPI_SCOPE_OBJECT = 1, 353*a140692aSJason Gunthorpe UAPI_SCOPE_METHOD = 2, 3545009010fSMatan Barak }; 3555009010fSMatan Barak 3560cbf432dSJason Gunthorpe struct uapi_definition { 3570cbf432dSJason Gunthorpe u8 kind; 3586829c1c2SJason Gunthorpe u8 scope; 3590cbf432dSJason Gunthorpe union { 3600cbf432dSJason Gunthorpe struct { 3610cbf432dSJason Gunthorpe u16 object_id; 3620cbf432dSJason Gunthorpe } object_start; 3636884c6c4SJason Gunthorpe struct { 3646884c6c4SJason Gunthorpe u8 is_ex; 3656884c6c4SJason Gunthorpe u16 command_num; 3666884c6c4SJason Gunthorpe } write; 3670cbf432dSJason Gunthorpe }; 3680cbf432dSJason Gunthorpe 3690cbf432dSJason Gunthorpe union { 3706829c1c2SJason Gunthorpe bool (*func_is_supported)(struct ib_device *device); 3716884c6c4SJason Gunthorpe ssize_t (*func_write)(struct ib_uverbs_file *file, 3726884c6c4SJason Gunthorpe const char __user *buf, int in_len, 3736884c6c4SJason Gunthorpe int out_len); 3746884c6c4SJason Gunthorpe int (*func_write_ex)(struct ib_uverbs_file *file, 3756884c6c4SJason Gunthorpe struct ib_udata *ucore, 3766884c6c4SJason Gunthorpe struct ib_udata *uhw); 3770cbf432dSJason Gunthorpe const struct uapi_definition *chain; 3780cbf432dSJason Gunthorpe const struct uverbs_object_def *chain_obj_tree; 3796829c1c2SJason Gunthorpe size_t needs_fn_offset; 3800cbf432dSJason Gunthorpe }; 3810cbf432dSJason Gunthorpe }; 3820cbf432dSJason Gunthorpe 3836884c6c4SJason Gunthorpe /* Define things connected to object_id */ 3846884c6c4SJason Gunthorpe #define DECLARE_UVERBS_OBJECT(_object_id, ...) \ 3856884c6c4SJason Gunthorpe { \ 3866884c6c4SJason Gunthorpe .kind = UAPI_DEF_OBJECT_START, \ 3876884c6c4SJason Gunthorpe .object_start = { .object_id = _object_id }, \ 3886884c6c4SJason Gunthorpe }, \ 3896884c6c4SJason Gunthorpe ##__VA_ARGS__ 3906884c6c4SJason Gunthorpe 3916884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */ 3926884c6c4SJason Gunthorpe #define DECLARE_UVERBS_WRITE(_command_num, _func, ...) \ 3936884c6c4SJason Gunthorpe { \ 3946884c6c4SJason Gunthorpe .kind = UAPI_DEF_WRITE, \ 3956884c6c4SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \ 3966884c6c4SJason Gunthorpe .write = { .is_ex = 0, .command_num = _command_num }, \ 3976884c6c4SJason Gunthorpe .func_write = _func, \ 3986884c6c4SJason Gunthorpe }, \ 3996884c6c4SJason Gunthorpe ##__VA_ARGS__ 4006884c6c4SJason Gunthorpe 4016884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */ 4026884c6c4SJason Gunthorpe #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, ...) \ 4036884c6c4SJason Gunthorpe { \ 4046884c6c4SJason Gunthorpe .kind = UAPI_DEF_WRITE, \ 4056884c6c4SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \ 4066884c6c4SJason Gunthorpe .write = { .is_ex = 1, .command_num = _command_num }, \ 4076884c6c4SJason Gunthorpe .func_write_ex = _func, \ 4086884c6c4SJason Gunthorpe }, \ 4096884c6c4SJason Gunthorpe ##__VA_ARGS__ 4106884c6c4SJason Gunthorpe 4116829c1c2SJason Gunthorpe /* 4126829c1c2SJason Gunthorpe * Object is only supported if the function pointer named ibdev_fn in struct 4136829c1c2SJason Gunthorpe * ib_device is not NULL. 4146829c1c2SJason Gunthorpe */ 4156829c1c2SJason Gunthorpe #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \ 4166829c1c2SJason Gunthorpe { \ 4176829c1c2SJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \ 4186829c1c2SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \ 4196829c1c2SJason Gunthorpe .needs_fn_offset = \ 4206829c1c2SJason Gunthorpe offsetof(struct ib_device, ibdev_fn) + \ 4216829c1c2SJason Gunthorpe BUILD_BUG_ON_ZERO( \ 4226829c1c2SJason Gunthorpe sizeof(((struct ib_device *)0)->ibdev_fn) != \ 4236829c1c2SJason Gunthorpe sizeof(void *)), \ 4246829c1c2SJason Gunthorpe } 4256829c1c2SJason Gunthorpe 426*a140692aSJason Gunthorpe /* 427*a140692aSJason Gunthorpe * Method is only supported if the function pointer named ibdev_fn in struct 428*a140692aSJason Gunthorpe * ib_device is not NULL. 429*a140692aSJason Gunthorpe */ 430*a140692aSJason Gunthorpe #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \ 431*a140692aSJason Gunthorpe { \ 432*a140692aSJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \ 433*a140692aSJason Gunthorpe .scope = UAPI_SCOPE_METHOD, \ 434*a140692aSJason Gunthorpe .needs_fn_offset = \ 435*a140692aSJason Gunthorpe offsetof(struct ib_device, ibdev_fn) + \ 436*a140692aSJason Gunthorpe BUILD_BUG_ON_ZERO( \ 437*a140692aSJason Gunthorpe sizeof(((struct ib_device *)0)->ibdev_fn) != \ 438*a140692aSJason Gunthorpe sizeof(void *)), \ 439*a140692aSJason Gunthorpe } 440*a140692aSJason Gunthorpe 4416829c1c2SJason Gunthorpe /* Call a function to determine if the entire object is supported or not */ 4426829c1c2SJason Gunthorpe #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \ 4436829c1c2SJason Gunthorpe { \ 4446829c1c2SJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \ 4456829c1c2SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \ 4466829c1c2SJason Gunthorpe } 4476829c1c2SJason Gunthorpe 4480cbf432dSJason Gunthorpe /* Include another struct uapi_definition in this one */ 4490cbf432dSJason Gunthorpe #define UAPI_DEF_CHAIN(_def_var) \ 4500cbf432dSJason Gunthorpe { \ 4510cbf432dSJason Gunthorpe .kind = UAPI_DEF_CHAIN, .chain = _def_var, \ 4520cbf432dSJason Gunthorpe } 4530cbf432dSJason Gunthorpe 4540cbf432dSJason Gunthorpe /* Temporary until the tree base description is replaced */ 4550cbf432dSJason Gunthorpe #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr) \ 4560cbf432dSJason Gunthorpe { \ 4570cbf432dSJason Gunthorpe .kind = UAPI_DEF_CHAIN_OBJ_TREE, \ 4580cbf432dSJason Gunthorpe .object_start = { .object_id = _object_enum }, \ 4590cbf432dSJason Gunthorpe .chain_obj_tree = _object_ptr, \ 4600cbf432dSJason Gunthorpe } 4610cbf432dSJason Gunthorpe #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \ 4620cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum)), \ 4630cbf432dSJason Gunthorpe ##__VA_ARGS__ 4640cbf432dSJason Gunthorpe 465d108dac0SJason Gunthorpe /* 466d108dac0SJason Gunthorpe * ======================================= 467d108dac0SJason Gunthorpe * Attribute Specifications 468d108dac0SJason Gunthorpe * ======================================= 469d108dac0SJason Gunthorpe */ 470c66db311SMatan Barak 471c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \ 472d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len 473422e3d37SJason Gunthorpe 474fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) 475fd44e385SYishai Hadas 476422e3d37SJason Gunthorpe /* 477422e3d37SJason Gunthorpe * Specifies a uapi structure that cannot be extended. The user must always 478422e3d37SJason Gunthorpe * supply the whole structure and nothing more. The structure must be declared 479422e3d37SJason Gunthorpe * in a header under include/uapi/rdma. 480422e3d37SJason Gunthorpe */ 481422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type) \ 482422e3d37SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 483422e3d37SJason Gunthorpe /* 484422e3d37SJason Gunthorpe * Specifies a uapi structure where the user must provide at least up to 485422e3d37SJason Gunthorpe * member 'last'. Anything after last and up until the end of the structure 486422e3d37SJason Gunthorpe * can be non-zero, anything longer than the end of the structure must be 487422e3d37SJason Gunthorpe * zero. The structure must be declared in a header under include/uapi/rdma. 488422e3d37SJason Gunthorpe */ 489422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last) \ 490422e3d37SJason Gunthorpe .zero_trailing = 1, \ 491422e3d37SJason Gunthorpe UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ 492422e3d37SJason Gunthorpe sizeof(_type)) 493540cd692SJason Gunthorpe /* 494540cd692SJason Gunthorpe * Specifies at least min_len bytes must be passed in, but the amount can be 495540cd692SJason Gunthorpe * larger, up to the protocol maximum size. No check for zeroing is done. 496540cd692SJason Gunthorpe */ 497540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 498c66db311SMatan Barak 499d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */ 50083bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 50183bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1 50283bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0 50335410306SMatan Barak 50470cd20aeSGuy Levi /* 50570cd20aeSGuy Levi * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only 50670cd20aeSGuy Levi * READ\WRITE accesses are supported. 50770cd20aeSGuy Levi */ 50870cd20aeSGuy Levi #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \ 50970cd20aeSGuy Levi ...) \ 51070cd20aeSGuy Levi (&(const struct uverbs_attr_def){ \ 51170cd20aeSGuy Levi .id = (_attr_id) + \ 51270cd20aeSGuy Levi BUILD_BUG_ON_ZERO((_min_len) == 0 || \ 51370cd20aeSGuy Levi (_max_len) > \ 51470cd20aeSGuy Levi PAGE_SIZE / sizeof(void *) || \ 51570cd20aeSGuy Levi (_min_len) > (_max_len) || \ 51670cd20aeSGuy Levi (_access) == UVERBS_ACCESS_NEW || \ 51770cd20aeSGuy Levi (_access) == UVERBS_ACCESS_DESTROY), \ 51870cd20aeSGuy Levi .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \ 51970cd20aeSGuy Levi .u2.objs_arr.obj_type = _idr_type, \ 52070cd20aeSGuy Levi .u2.objs_arr.access = _access, \ 52170cd20aeSGuy Levi .u2.objs_arr.min_len = _min_len, \ 52270cd20aeSGuy Levi .u2.objs_arr.max_len = _max_len, \ 52370cd20aeSGuy Levi __VA_ARGS__ } }) 52470cd20aeSGuy Levi 525d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 5269a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 527d108dac0SJason Gunthorpe .id = _attr_id, \ 528d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 529d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \ 530d108dac0SJason Gunthorpe .u.obj.access = _access, \ 531d108dac0SJason Gunthorpe __VA_ARGS__ } }) 532d108dac0SJason Gunthorpe 533d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 5349a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 535d108dac0SJason Gunthorpe .id = (_attr_id) + \ 536d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 53735410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \ 538d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 539d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \ 540d108dac0SJason Gunthorpe .u.obj.access = _access, \ 541d108dac0SJason Gunthorpe __VA_ARGS__ } }) 54235410306SMatan Barak 543d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 5449a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 545d108dac0SJason Gunthorpe .id = _attr_id, \ 546d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 547d108dac0SJason Gunthorpe _type, \ 548d108dac0SJason Gunthorpe __VA_ARGS__ } }) 549d108dac0SJason Gunthorpe 550d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 5519a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 552d108dac0SJason Gunthorpe .id = _attr_id, \ 553d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 554d108dac0SJason Gunthorpe _type, \ 555d108dac0SJason Gunthorpe __VA_ARGS__ } }) 556d108dac0SJason Gunthorpe 557d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 558d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 5599a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \ 560d108dac0SJason Gunthorpe .id = _attr_id, \ 561d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 562d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \ 563d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 564d108dac0SJason Gunthorpe __VA_ARGS__ }, \ 565d108dac0SJason Gunthorpe }) 566d108dac0SJason Gunthorpe 5670953fffeSMark Bloch /* An input value that is a member in the enum _enum_type. */ 5680953fffeSMark Bloch #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \ 5690953fffeSMark Bloch UVERBS_ATTR_PTR_IN( \ 5700953fffeSMark Bloch _attr_id, \ 5710953fffeSMark Bloch UVERBS_ATTR_SIZE( \ 5720953fffeSMark Bloch sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \ 5730953fffeSMark Bloch sizeof(u64)), \ 5740953fffeSMark Bloch __VA_ARGS__) 5750953fffeSMark Bloch 576d108dac0SJason Gunthorpe /* 577bccd0622SJason Gunthorpe * An input value that is a bitwise combination of values of _enum_type. 578bccd0622SJason Gunthorpe * This permits the flag value to be passed as either a u32 or u64, it must 579bccd0622SJason Gunthorpe * be retrieved via uverbs_get_flag(). 580bccd0622SJason Gunthorpe */ 581bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ 582bccd0622SJason Gunthorpe UVERBS_ATTR_PTR_IN( \ 583bccd0622SJason Gunthorpe _attr_id, \ 584bccd0622SJason Gunthorpe UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ 585bccd0622SJason Gunthorpe !sizeof(_enum_type *)), \ 586bccd0622SJason Gunthorpe sizeof(u64)), \ 587bccd0622SJason Gunthorpe __VA_ARGS__) 588bccd0622SJason Gunthorpe 589bccd0622SJason Gunthorpe /* 5906c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a 5916c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this 5926c61d2a5SJason Gunthorpe * spec. 5936c61d2a5SJason Gunthorpe */ 5946c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \ 5959a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 596540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 597540cd692SJason Gunthorpe UA_OPTIONAL), \ 5989a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 599540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \ 600540cd692SJason Gunthorpe UA_OPTIONAL) 6016c61d2a5SJason Gunthorpe 602fac9658cSMatan Barak /* ================================================= 603fac9658cSMatan Barak * Parsing infrastructure 604fac9658cSMatan Barak * ================================================= 605fac9658cSMatan Barak */ 606fac9658cSMatan Barak 6073a863577SJason Gunthorpe 608fac9658cSMatan Barak struct uverbs_ptr_attr { 6098762d149SMatan Barak /* 6108762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 6118762d149SMatan Barak * used. 6128762d149SMatan Barak */ 6138762d149SMatan Barak union { 6148762d149SMatan Barak void *ptr; 615fac9658cSMatan Barak u64 data; 6168762d149SMatan Barak }; 617fac9658cSMatan Barak u16 len; 6186a1f444fSJason Gunthorpe u16 uattr_idx; 619494c5580SMatan Barak u8 enum_id; 620fac9658cSMatan Barak }; 621fac9658cSMatan Barak 622f43dbebfSMatan Barak struct uverbs_obj_attr { 623f43dbebfSMatan Barak struct ib_uobject *uobject; 6243a863577SJason Gunthorpe const struct uverbs_api_attr *attr_elm; 625f43dbebfSMatan Barak }; 626f43dbebfSMatan Barak 62770cd20aeSGuy Levi struct uverbs_objs_arr_attr { 62870cd20aeSGuy Levi struct ib_uobject **uobjects; 62970cd20aeSGuy Levi u16 len; 63070cd20aeSGuy Levi }; 63170cd20aeSGuy Levi 632f43dbebfSMatan Barak struct uverbs_attr { 633fac9658cSMatan Barak union { 634fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr; 635f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr; 63670cd20aeSGuy Levi struct uverbs_objs_arr_attr objs_arr_attr; 637f43dbebfSMatan Barak }; 638fac9658cSMatan Barak }; 639f43dbebfSMatan Barak 640f43dbebfSMatan Barak struct uverbs_attr_bundle { 6414b3dd2bbSJason Gunthorpe struct ib_uverbs_file *ufile; 6423a863577SJason Gunthorpe DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN); 6433a863577SJason Gunthorpe struct uverbs_attr attrs[]; 644f43dbebfSMatan Barak }; 645f43dbebfSMatan Barak 64635410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 64735410306SMatan Barak unsigned int idx) 64835410306SMatan Barak { 6493a863577SJason Gunthorpe return test_bit(uapi_bkey_attr(uapi_key_attr(idx)), 6503a863577SJason Gunthorpe attrs_bundle->attr_present); 65135410306SMatan Barak } 65235410306SMatan Barak 65341b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 65441b2a71fSMatan Barak 655d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 656d70724f1SMatan Barak u16 idx) 657d70724f1SMatan Barak { 658d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx)) 659d70724f1SMatan Barak return ERR_PTR(-ENOENT); 660d70724f1SMatan Barak 6613a863577SJason Gunthorpe return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))]; 662d70724f1SMatan Barak } 663d70724f1SMatan Barak 664494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 665494c5580SMatan Barak u16 idx) 666494c5580SMatan Barak { 667494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 668494c5580SMatan Barak 669494c5580SMatan Barak if (IS_ERR(attr)) 670494c5580SMatan Barak return PTR_ERR(attr); 671494c5580SMatan Barak 672494c5580SMatan Barak return attr->ptr_attr.enum_id; 673494c5580SMatan Barak } 674494c5580SMatan Barak 675be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 676be934ccaSAriel Levkovich u16 idx) 677be934ccaSAriel Levkovich { 678f4602cbbSJason Gunthorpe const struct uverbs_attr *attr; 679be934ccaSAriel Levkovich 680f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx); 681f4602cbbSJason Gunthorpe if (IS_ERR(attr)) 682f4602cbbSJason Gunthorpe return ERR_CAST(attr); 683be934ccaSAriel Levkovich 684f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object; 685be934ccaSAriel Levkovich } 686be934ccaSAriel Levkovich 6873efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 6883efa3812SMatan Barak u16 idx) 6893efa3812SMatan Barak { 6903efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 6913efa3812SMatan Barak 6923efa3812SMatan Barak if (IS_ERR(attr)) 6933efa3812SMatan Barak return ERR_CAST(attr); 6943efa3812SMatan Barak 6953efa3812SMatan Barak return attr->obj_attr.uobject; 6963efa3812SMatan Barak } 6973efa3812SMatan Barak 6988762d149SMatan Barak static inline int 6998762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 7008762d149SMatan Barak { 7018762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 7028762d149SMatan Barak 7038762d149SMatan Barak if (IS_ERR(attr)) 7048762d149SMatan Barak return PTR_ERR(attr); 7058762d149SMatan Barak 7068762d149SMatan Barak return attr->ptr_attr.len; 7078762d149SMatan Barak } 7088762d149SMatan Barak 70970cd20aeSGuy Levi /** 71070cd20aeSGuy Levi * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for 71170cd20aeSGuy Levi * UVERBS_ATTR_TYPE_IDRS_ARRAY. 71270cd20aeSGuy Levi * @arr: Returned pointer to array of pointers for uobjects or NULL if 71370cd20aeSGuy Levi * the attribute isn't provided. 71470cd20aeSGuy Levi * 71570cd20aeSGuy Levi * Return: The array length or 0 if no attribute was provided. 71670cd20aeSGuy Levi */ 71770cd20aeSGuy Levi static inline int uverbs_attr_get_uobjs_arr( 71870cd20aeSGuy Levi const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx, 71970cd20aeSGuy Levi struct ib_uobject ***arr) 72070cd20aeSGuy Levi { 72170cd20aeSGuy Levi const struct uverbs_attr *attr = 72270cd20aeSGuy Levi uverbs_attr_get(attrs_bundle, attr_idx); 72370cd20aeSGuy Levi 72470cd20aeSGuy Levi if (IS_ERR(attr)) { 72570cd20aeSGuy Levi *arr = NULL; 72670cd20aeSGuy Levi return 0; 72770cd20aeSGuy Levi } 72870cd20aeSGuy Levi 72970cd20aeSGuy Levi *arr = attr->objs_arr_attr.uobjects; 73070cd20aeSGuy Levi 73170cd20aeSGuy Levi return attr->objs_arr_attr.len; 73270cd20aeSGuy Levi } 73370cd20aeSGuy Levi 73489d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 73589d9e8d3SMatan Barak { 73689d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 73789d9e8d3SMatan Barak } 73889d9e8d3SMatan Barak 7398762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr( 7408762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 7418762d149SMatan Barak { 7428762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 7438762d149SMatan Barak 7448762d149SMatan Barak if (IS_ERR(attr)) 7458762d149SMatan Barak return (void *)attr; 7468762d149SMatan Barak 7478762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 7488762d149SMatan Barak attr->ptr_attr.ptr; 7498762d149SMatan Barak } 7508762d149SMatan Barak 75189d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to, 752d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 75389d9e8d3SMatan Barak size_t idx, 75489d9e8d3SMatan Barak size_t size) 755d70724f1SMatan Barak { 756d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 757d70724f1SMatan Barak 758d70724f1SMatan Barak if (IS_ERR(attr)) 759d70724f1SMatan Barak return PTR_ERR(attr); 760d70724f1SMatan Barak 76189d9e8d3SMatan Barak /* 76289d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is 763c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 764c66db311SMatan Barak * uverbs_copy_from_or_zero. 76589d9e8d3SMatan Barak */ 76689d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len)) 76789d9e8d3SMatan Barak return -EINVAL; 76889d9e8d3SMatan Barak 76989d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 770d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 7712f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 7722f36028cSJason Gunthorpe attr->ptr_attr.len)) 773d70724f1SMatan Barak return -EFAULT; 774d70724f1SMatan Barak 775d70724f1SMatan Barak return 0; 776d70724f1SMatan Barak } 777d70724f1SMatan Barak 778c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to, 779c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, 780c66db311SMatan Barak size_t idx, 781c66db311SMatan Barak size_t size) 782c66db311SMatan Barak { 783c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 784c66db311SMatan Barak size_t min_size; 785c66db311SMatan Barak 786c66db311SMatan Barak if (IS_ERR(attr)) 787c66db311SMatan Barak return PTR_ERR(attr); 788c66db311SMatan Barak 789c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len); 790c66db311SMatan Barak 791c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr)) 792c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size); 793c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 794c66db311SMatan Barak min_size)) 795c66db311SMatan Barak return -EFAULT; 796c66db311SMatan Barak 797c66db311SMatan Barak if (size > min_size) 798c66db311SMatan Barak memset(to + min_size, 0, size - min_size); 799c66db311SMatan Barak 800c66db311SMatan Barak return 0; 801c66db311SMatan Barak } 802c66db311SMatan Barak 803d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \ 80489d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 805d70724f1SMatan Barak 806c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 807c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 808c66db311SMatan Barak 809bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 810bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 811bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 812bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 813bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits); 8146a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, 8156a1f444fSJason Gunthorpe const void *from, size_t size); 816461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, 817461bb2eeSJason Gunthorpe gfp_t flags); 818461bb2eeSJason Gunthorpe 819461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 820461bb2eeSJason Gunthorpe size_t size) 821461bb2eeSJason Gunthorpe { 822461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL); 823461bb2eeSJason Gunthorpe } 824461bb2eeSJason Gunthorpe 825461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 826461bb2eeSJason Gunthorpe size_t size) 827461bb2eeSJason Gunthorpe { 828461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); 829461bb2eeSJason Gunthorpe } 8300953fffeSMark Bloch int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 8310953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 8320953fffeSMark Bloch s64 *def_val); 833bccd0622SJason Gunthorpe #else 834bccd0622SJason Gunthorpe static inline int 835bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 836bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 837bccd0622SJason Gunthorpe { 838bccd0622SJason Gunthorpe return -EINVAL; 839bccd0622SJason Gunthorpe } 840bccd0622SJason Gunthorpe static inline int 841bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 842bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits) 843bccd0622SJason Gunthorpe { 844bccd0622SJason Gunthorpe return -EINVAL; 845bccd0622SJason Gunthorpe } 8466a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 8476a1f444fSJason Gunthorpe size_t idx, const void *from, size_t size) 8486a1f444fSJason Gunthorpe { 8496a1f444fSJason Gunthorpe return -EINVAL; 8506a1f444fSJason Gunthorpe } 851461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 852461bb2eeSJason Gunthorpe size_t size) 853461bb2eeSJason Gunthorpe { 854461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 855461bb2eeSJason Gunthorpe } 856461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 857461bb2eeSJason Gunthorpe size_t size) 858461bb2eeSJason Gunthorpe { 859461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL); 860461bb2eeSJason Gunthorpe } 8610953fffeSMark Bloch static inline int 8620953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 8630953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound, 8640953fffeSMark Bloch s64 *def_val) 8650953fffeSMark Bloch { 8660953fffeSMark Bloch return -EINVAL; 8670953fffeSMark Bloch } 868bccd0622SJason Gunthorpe #endif 869bccd0622SJason Gunthorpe 8700953fffeSMark Bloch #define uverbs_get_const(_to, _attrs_bundle, _idx) \ 8710953fffeSMark Bloch ({ \ 8720953fffeSMark Bloch s64 _val; \ 8730953fffeSMark Bloch int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \ 8740953fffeSMark Bloch type_min(typeof(*_to)), \ 8750953fffeSMark Bloch type_max(typeof(*_to)), NULL); \ 8760953fffeSMark Bloch (*_to) = _val; \ 8770953fffeSMark Bloch _ret; \ 8780953fffeSMark Bloch }) 8790953fffeSMark Bloch 8800953fffeSMark Bloch #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \ 8810953fffeSMark Bloch ({ \ 8820953fffeSMark Bloch s64 _val; \ 8830953fffeSMark Bloch s64 _def_val = _default; \ 8840953fffeSMark Bloch int _ret = \ 8850953fffeSMark Bloch _uverbs_get_const(&_val, _attrs_bundle, _idx, \ 8860953fffeSMark Bloch type_min(typeof(*_to)), \ 8870953fffeSMark Bloch type_max(typeof(*_to)), &_def_val); \ 8880953fffeSMark Bloch (*_to) = _val; \ 8890953fffeSMark Bloch _ret; \ 8900953fffeSMark Bloch }) 891118620d3SMatan Barak #endif 892