16bf9d8f6SLeon Romanovsky /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2a0aa309cSMatan Barak /*
3a0aa309cSMatan Barak * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
4a0aa309cSMatan Barak */
5a0aa309cSMatan Barak
6a0aa309cSMatan Barak #ifndef _UVERBS_IOCTL_
7a0aa309cSMatan Barak #define _UVERBS_IOCTL_
8a0aa309cSMatan Barak
9a0aa309cSMatan Barak #include <rdma/uverbs_types.h>
1035410306SMatan Barak #include <linux/uaccess.h>
1135410306SMatan Barak #include <rdma/rdma_user_ioctl.h>
12d70724f1SMatan Barak #include <rdma/ib_user_ioctl_verbs.h>
131f7ff9d5SMatan Barak #include <rdma/ib_user_ioctl_cmds.h>
14a0aa309cSMatan Barak
15a0aa309cSMatan Barak /*
16a0aa309cSMatan Barak * =======================================
17a0aa309cSMatan Barak * Verbs action specifications
18a0aa309cSMatan Barak * =======================================
19a0aa309cSMatan Barak */
20a0aa309cSMatan Barak
21f43dbebfSMatan Barak enum uverbs_attr_type {
22f43dbebfSMatan Barak UVERBS_ATTR_TYPE_NA,
23fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_IN,
24fac9658cSMatan Barak UVERBS_ATTR_TYPE_PTR_OUT,
25f43dbebfSMatan Barak UVERBS_ATTR_TYPE_IDR,
26f43dbebfSMatan Barak UVERBS_ATTR_TYPE_FD,
27015bda8aSJason Gunthorpe UVERBS_ATTR_TYPE_RAW_FD,
28494c5580SMatan Barak UVERBS_ATTR_TYPE_ENUM_IN,
2970cd20aeSGuy Levi UVERBS_ATTR_TYPE_IDRS_ARRAY,
30f43dbebfSMatan Barak };
31f43dbebfSMatan Barak
32a0aa309cSMatan Barak enum uverbs_obj_access {
33a0aa309cSMatan Barak UVERBS_ACCESS_READ,
34a0aa309cSMatan Barak UVERBS_ACCESS_WRITE,
35a0aa309cSMatan Barak UVERBS_ACCESS_NEW,
36a0aa309cSMatan Barak UVERBS_ACCESS_DESTROY
37a0aa309cSMatan Barak };
38a0aa309cSMatan Barak
391f07e08fSMatan Barak /* Specification of a single attribute inside the ioctl message */
4083bb4442SJason Gunthorpe /* good size 16 */
41f43dbebfSMatan Barak struct uverbs_attr_spec {
42d108dac0SJason Gunthorpe u8 type;
4383bb4442SJason Gunthorpe
4483bb4442SJason Gunthorpe /*
45422e3d37SJason Gunthorpe * Support extending attributes by length. Allow the user to provide
46422e3d37SJason Gunthorpe * more bytes than ptr.len, but check that everything after is zero'd
47422e3d37SJason Gunthorpe * by the user.
4883bb4442SJason Gunthorpe */
49422e3d37SJason Gunthorpe u8 zero_trailing:1;
5083bb4442SJason Gunthorpe /*
5183bb4442SJason Gunthorpe * Valid only for PTR_IN. Allocate and copy the data inside
5283bb4442SJason Gunthorpe * the parser
5383bb4442SJason Gunthorpe */
5483bb4442SJason Gunthorpe u8 alloc_and_copy:1;
5583bb4442SJason Gunthorpe u8 mandatory:1;
5607f05f40SJason Gunthorpe /* True if this is from UVERBS_ATTR_UHW */
5707f05f40SJason Gunthorpe u8 is_udata:1;
58d108dac0SJason Gunthorpe
59fac9658cSMatan Barak union {
60f43dbebfSMatan Barak struct {
61c66db311SMatan Barak /* Current known size to kernel */
621f07e08fSMatan Barak u16 len;
63c66db311SMatan Barak /* User isn't allowed to provide something < min_len */
64c66db311SMatan Barak u16 min_len;
651f07e08fSMatan Barak } ptr;
66d108dac0SJason Gunthorpe
671f07e08fSMatan Barak struct {
68f43dbebfSMatan Barak /*
69f43dbebfSMatan Barak * higher bits mean the namespace and lower bits mean
70f43dbebfSMatan Barak * the type id within the namespace.
71f43dbebfSMatan Barak */
72f43dbebfSMatan Barak u16 obj_type;
73f43dbebfSMatan Barak u8 access;
74f43dbebfSMatan Barak } obj;
75d108dac0SJason Gunthorpe
76494c5580SMatan Barak struct {
77494c5580SMatan Barak u8 num_elems;
78d108dac0SJason Gunthorpe } enum_def;
79d108dac0SJason Gunthorpe } u;
80d108dac0SJason Gunthorpe
8170cd20aeSGuy Levi /* This weird split lets us remove some padding */
82d108dac0SJason Gunthorpe union {
83d108dac0SJason Gunthorpe struct {
84494c5580SMatan Barak /*
85494c5580SMatan Barak * The enum attribute can select one of the attributes
86494c5580SMatan Barak * contained in the ids array. Currently only PTR_IN
87494c5580SMatan Barak * attributes are supported in the ids array.
88494c5580SMatan Barak */
89494c5580SMatan Barak const struct uverbs_attr_spec *ids;
90494c5580SMatan Barak } enum_def;
9170cd20aeSGuy Levi
9270cd20aeSGuy Levi struct {
9370cd20aeSGuy Levi /*
9470cd20aeSGuy Levi * higher bits mean the namespace and lower bits mean
9570cd20aeSGuy Levi * the type id within the namespace.
9670cd20aeSGuy Levi */
9770cd20aeSGuy Levi u16 obj_type;
9870cd20aeSGuy Levi u16 min_len;
9970cd20aeSGuy Levi u16 max_len;
10070cd20aeSGuy Levi u8 access;
10170cd20aeSGuy Levi } objs_arr;
102d108dac0SJason Gunthorpe } u2;
103fac9658cSMatan Barak };
104f43dbebfSMatan Barak
1055009010fSMatan Barak /*
1069ed3e5f4SJason Gunthorpe * Information about the API is loaded into a radix tree. For IOCTL we start
1079ed3e5f4SJason Gunthorpe * with a tuple of:
1089ed3e5f4SJason Gunthorpe * object_id, attr_id, method_id
1099ed3e5f4SJason Gunthorpe *
1109ed3e5f4SJason Gunthorpe * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
1119ed3e5f4SJason Gunthorpe * on the current kernel support this is compressed into 16 bit key for the
1129ed3e5f4SJason Gunthorpe * radix tree. Since this compression is entirely internal to the kernel the
1139ed3e5f4SJason Gunthorpe * below limits can be revised if the kernel gains additional data.
1149ed3e5f4SJason Gunthorpe *
1159ed3e5f4SJason Gunthorpe * With 64 leafs per node this is a 3 level radix tree.
1169ed3e5f4SJason Gunthorpe *
1179ed3e5f4SJason Gunthorpe * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
1189ed3e5f4SJason Gunthorpe * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
1196884c6c4SJason Gunthorpe *
1206884c6c4SJason Gunthorpe * This also encodes the tables for the write() and write() extended commands
1216884c6c4SJason Gunthorpe * using the coding
1226884c6c4SJason Gunthorpe * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
1236884c6c4SJason Gunthorpe * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
1246884c6c4SJason Gunthorpe * ie the WRITE path is treated as a special method type in the ioctl
1256884c6c4SJason Gunthorpe * framework.
1269ed3e5f4SJason Gunthorpe */
1279ed3e5f4SJason Gunthorpe enum uapi_radix_data {
1289ed3e5f4SJason Gunthorpe UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
1299ed3e5f4SJason Gunthorpe
1309ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_BITS = 6,
1319ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
1329ed3e5f4SJason Gunthorpe UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
1336884c6c4SJason Gunthorpe UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
1349ed3e5f4SJason Gunthorpe
1359ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS = 5,
1369ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
1376884c6c4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE = 22,
1386884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
1396884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
1406884c6c4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_DRIVER =
1416884c6c4SJason Gunthorpe (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
1429ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_NUM_CORE,
1439ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_MASK = GENMASK(
1449ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
1459ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_SHIFT),
1469ed3e5f4SJason Gunthorpe
1479ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_BITS = 5,
1489ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_SHIFT =
1499ed3e5f4SJason Gunthorpe UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
150342ee59dSYishai Hadas UVERBS_API_OBJ_KEY_NUM_CORE = 20,
1519ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_NUM_DRIVER =
1529ed3e5f4SJason Gunthorpe (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
1539ed3e5f4SJason Gunthorpe UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
1549ed3e5f4SJason Gunthorpe
1559ed3e5f4SJason Gunthorpe /* This id guaranteed to not exist in the radix tree */
1569ed3e5f4SJason Gunthorpe UVERBS_API_KEY_ERR = 0xFFFFFFFF,
1579ed3e5f4SJason Gunthorpe };
1589ed3e5f4SJason Gunthorpe
uapi_key_obj(u32 id)1599ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_obj(u32 id)
1609ed3e5f4SJason Gunthorpe {
1619ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) {
1629ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG;
1639ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
1649ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR;
1659ed3e5f4SJason Gunthorpe id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
1669ed3e5f4SJason Gunthorpe } else {
1679ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
1689ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR;
1699ed3e5f4SJason Gunthorpe }
1709ed3e5f4SJason Gunthorpe
1719ed3e5f4SJason Gunthorpe return id << UVERBS_API_OBJ_KEY_SHIFT;
1729ed3e5f4SJason Gunthorpe }
1739ed3e5f4SJason Gunthorpe
uapi_key_is_object(u32 key)1749ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_object(u32 key)
1759ed3e5f4SJason Gunthorpe {
1769ed3e5f4SJason Gunthorpe return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
1779ed3e5f4SJason Gunthorpe }
1789ed3e5f4SJason Gunthorpe
uapi_key_ioctl_method(u32 id)1799ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
1809ed3e5f4SJason Gunthorpe {
1819ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) {
1829ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG;
1839ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
1849ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR;
1859ed3e5f4SJason Gunthorpe id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
1869ed3e5f4SJason Gunthorpe } else {
1879ed3e5f4SJason Gunthorpe id++;
1889ed3e5f4SJason Gunthorpe if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
1899ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR;
1909ed3e5f4SJason Gunthorpe }
1919ed3e5f4SJason Gunthorpe
1929ed3e5f4SJason Gunthorpe return id << UVERBS_API_METHOD_KEY_SHIFT;
1939ed3e5f4SJason Gunthorpe }
1949ed3e5f4SJason Gunthorpe
uapi_key_write_method(u32 id)1956884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
1966884c6c4SJason Gunthorpe {
1976884c6c4SJason Gunthorpe if (id >= UVERBS_API_WRITE_KEY_NUM)
1986884c6c4SJason Gunthorpe return UVERBS_API_KEY_ERR;
1996884c6c4SJason Gunthorpe return UVERBS_API_METHOD_IS_WRITE | id;
2006884c6c4SJason Gunthorpe }
2016884c6c4SJason Gunthorpe
uapi_key_write_ex_method(u32 id)2026884c6c4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
2036884c6c4SJason Gunthorpe {
2046884c6c4SJason Gunthorpe if (id >= UVERBS_API_WRITE_KEY_NUM)
2056884c6c4SJason Gunthorpe return UVERBS_API_KEY_ERR;
2066884c6c4SJason Gunthorpe return UVERBS_API_METHOD_IS_WRITE_EX | id;
2076884c6c4SJason Gunthorpe }
2086884c6c4SJason Gunthorpe
2096884c6c4SJason Gunthorpe static inline __attribute_const__ u32
uapi_key_attr_to_ioctl_method(u32 attr_key)2106884c6c4SJason Gunthorpe uapi_key_attr_to_ioctl_method(u32 attr_key)
2119ed3e5f4SJason Gunthorpe {
2129ed3e5f4SJason Gunthorpe return attr_key &
2139ed3e5f4SJason Gunthorpe (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
2149ed3e5f4SJason Gunthorpe }
2159ed3e5f4SJason Gunthorpe
uapi_key_is_ioctl_method(u32 key)2169ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
2179ed3e5f4SJason Gunthorpe {
2186884c6c4SJason Gunthorpe unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
2196884c6c4SJason Gunthorpe
2206884c6c4SJason Gunthorpe return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
2219ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) == 0;
2229ed3e5f4SJason Gunthorpe }
2239ed3e5f4SJason Gunthorpe
uapi_key_is_write_method(u32 key)2246884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
2256884c6c4SJason Gunthorpe {
2266884c6c4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
2276884c6c4SJason Gunthorpe }
2286884c6c4SJason Gunthorpe
uapi_key_is_write_ex_method(u32 key)2296884c6c4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
2306884c6c4SJason Gunthorpe {
2316884c6c4SJason Gunthorpe return (key & UVERBS_API_METHOD_KEY_MASK) ==
2326884c6c4SJason Gunthorpe UVERBS_API_METHOD_IS_WRITE_EX;
2336884c6c4SJason Gunthorpe }
2346884c6c4SJason Gunthorpe
uapi_key_attrs_start(u32 ioctl_method_key)2359ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
2369ed3e5f4SJason Gunthorpe {
2379ed3e5f4SJason Gunthorpe /* 0 is the method slot itself */
2389ed3e5f4SJason Gunthorpe return ioctl_method_key + 1;
2399ed3e5f4SJason Gunthorpe }
2409ed3e5f4SJason Gunthorpe
uapi_key_attr(u32 id)2419ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_key_attr(u32 id)
2429ed3e5f4SJason Gunthorpe {
2439ed3e5f4SJason Gunthorpe /*
2449ed3e5f4SJason Gunthorpe * The attr is designed to fit in the typical single radix tree node
2459ed3e5f4SJason Gunthorpe * of 64 entries. Since allmost all methods have driver attributes we
2469ed3e5f4SJason Gunthorpe * organize things so that the driver and core attributes interleave to
2479ed3e5f4SJason Gunthorpe * reduce the length of the attributes array in typical cases.
2489ed3e5f4SJason Gunthorpe */
2499ed3e5f4SJason Gunthorpe if (id & UVERBS_API_NS_FLAG) {
2509ed3e5f4SJason Gunthorpe id &= ~UVERBS_API_NS_FLAG;
2519ed3e5f4SJason Gunthorpe id++;
2529ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2539ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR;
2549ed3e5f4SJason Gunthorpe id = (id << 1) | 0;
2559ed3e5f4SJason Gunthorpe } else {
2569ed3e5f4SJason Gunthorpe if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
2579ed3e5f4SJason Gunthorpe return UVERBS_API_KEY_ERR;
2589ed3e5f4SJason Gunthorpe id = (id << 1) | 1;
2599ed3e5f4SJason Gunthorpe }
2609ed3e5f4SJason Gunthorpe
2619ed3e5f4SJason Gunthorpe return id;
2629ed3e5f4SJason Gunthorpe }
2639ed3e5f4SJason Gunthorpe
2646884c6c4SJason Gunthorpe /* Only true for ioctl methods */
uapi_key_is_attr(u32 key)2659ed3e5f4SJason Gunthorpe static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
2669ed3e5f4SJason Gunthorpe {
2676884c6c4SJason Gunthorpe unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
2686884c6c4SJason Gunthorpe
2696884c6c4SJason Gunthorpe return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
2709ed3e5f4SJason Gunthorpe (key & UVERBS_API_ATTR_KEY_MASK) != 0;
2719ed3e5f4SJason Gunthorpe }
2729ed3e5f4SJason Gunthorpe
2739ed3e5f4SJason Gunthorpe /*
2749ed3e5f4SJason Gunthorpe * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
2759ed3e5f4SJason Gunthorpe * basically it undoes the reservation of 0 in the ID numbering. attr_key
2769ed3e5f4SJason Gunthorpe * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
2779ed3e5f4SJason Gunthorpe * uapi_key_attr().
2789ed3e5f4SJason Gunthorpe */
uapi_bkey_attr(u32 attr_key)2799ed3e5f4SJason Gunthorpe static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
2809ed3e5f4SJason Gunthorpe {
2819ed3e5f4SJason Gunthorpe return attr_key - 1;
2829ed3e5f4SJason Gunthorpe }
2839ed3e5f4SJason Gunthorpe
uapi_bkey_to_key_attr(u32 attr_bkey)28470cd20aeSGuy Levi static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
28570cd20aeSGuy Levi {
28670cd20aeSGuy Levi return attr_bkey + 1;
28770cd20aeSGuy Levi }
28870cd20aeSGuy Levi
2899ed3e5f4SJason Gunthorpe /*
2905009010fSMatan Barak * =======================================
2915009010fSMatan Barak * Verbs definitions
2925009010fSMatan Barak * =======================================
2935009010fSMatan Barak */
2945009010fSMatan Barak
29509e3ebf8SMatan Barak struct uverbs_attr_def {
29609e3ebf8SMatan Barak u16 id;
29709e3ebf8SMatan Barak struct uverbs_attr_spec attr;
29809e3ebf8SMatan Barak };
29909e3ebf8SMatan Barak
30009e3ebf8SMatan Barak struct uverbs_method_def {
30109e3ebf8SMatan Barak u16 id;
30209e3ebf8SMatan Barak /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
30309e3ebf8SMatan Barak u32 flags;
30409e3ebf8SMatan Barak size_t num_attrs;
30509e3ebf8SMatan Barak const struct uverbs_attr_def * const (*attrs)[];
30615a1b4beSJason Gunthorpe int (*handler)(struct uverbs_attr_bundle *attrs);
30709e3ebf8SMatan Barak };
30809e3ebf8SMatan Barak
3095009010fSMatan Barak struct uverbs_object_def {
31009e3ebf8SMatan Barak u16 id;
3115009010fSMatan Barak const struct uverbs_obj_type *type_attrs;
31209e3ebf8SMatan Barak size_t num_methods;
31309e3ebf8SMatan Barak const struct uverbs_method_def * const (*methods)[];
31409e3ebf8SMatan Barak };
31509e3ebf8SMatan Barak
3160cbf432dSJason Gunthorpe enum uapi_definition_kind {
3170cbf432dSJason Gunthorpe UAPI_DEF_END = 0,
3186884c6c4SJason Gunthorpe UAPI_DEF_OBJECT_START,
3196884c6c4SJason Gunthorpe UAPI_DEF_WRITE,
3200cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE,
3210cbf432dSJason Gunthorpe UAPI_DEF_CHAIN,
3226829c1c2SJason Gunthorpe UAPI_DEF_IS_SUPPORTED_FUNC,
3236829c1c2SJason Gunthorpe UAPI_DEF_IS_SUPPORTED_DEV_FN,
3246829c1c2SJason Gunthorpe };
3256829c1c2SJason Gunthorpe
3266829c1c2SJason Gunthorpe enum uapi_definition_scope {
3276829c1c2SJason Gunthorpe UAPI_SCOPE_OBJECT = 1,
328a140692aSJason Gunthorpe UAPI_SCOPE_METHOD = 2,
3295009010fSMatan Barak };
3305009010fSMatan Barak
3310cbf432dSJason Gunthorpe struct uapi_definition {
3320cbf432dSJason Gunthorpe u8 kind;
3336829c1c2SJason Gunthorpe u8 scope;
3340cbf432dSJason Gunthorpe union {
3350cbf432dSJason Gunthorpe struct {
3360cbf432dSJason Gunthorpe u16 object_id;
3370cbf432dSJason Gunthorpe } object_start;
3386884c6c4SJason Gunthorpe struct {
3396884c6c4SJason Gunthorpe u16 command_num;
340669dac1eSJason Gunthorpe u8 is_ex:1;
341669dac1eSJason Gunthorpe u8 has_udata:1;
342669dac1eSJason Gunthorpe u8 has_resp:1;
343669dac1eSJason Gunthorpe u8 req_size;
344669dac1eSJason Gunthorpe u8 resp_size;
3456884c6c4SJason Gunthorpe } write;
3460cbf432dSJason Gunthorpe };
3470cbf432dSJason Gunthorpe
3480cbf432dSJason Gunthorpe union {
3496829c1c2SJason Gunthorpe bool (*func_is_supported)(struct ib_device *device);
350974d6b4bSJason Gunthorpe int (*func_write)(struct uverbs_attr_bundle *attrs);
3510cbf432dSJason Gunthorpe const struct uapi_definition *chain;
3520cbf432dSJason Gunthorpe const struct uverbs_object_def *chain_obj_tree;
3536829c1c2SJason Gunthorpe size_t needs_fn_offset;
3540cbf432dSJason Gunthorpe };
3550cbf432dSJason Gunthorpe };
3560cbf432dSJason Gunthorpe
3576884c6c4SJason Gunthorpe /* Define things connected to object_id */
3586884c6c4SJason Gunthorpe #define DECLARE_UVERBS_OBJECT(_object_id, ...) \
3596884c6c4SJason Gunthorpe { \
3606884c6c4SJason Gunthorpe .kind = UAPI_DEF_OBJECT_START, \
3616884c6c4SJason Gunthorpe .object_start = { .object_id = _object_id }, \
3626884c6c4SJason Gunthorpe }, \
3636884c6c4SJason Gunthorpe ##__VA_ARGS__
3646884c6c4SJason Gunthorpe
3656884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */
366669dac1eSJason Gunthorpe #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
3676884c6c4SJason Gunthorpe { \
3686884c6c4SJason Gunthorpe .kind = UAPI_DEF_WRITE, \
3696884c6c4SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \
3706884c6c4SJason Gunthorpe .write = { .is_ex = 0, .command_num = _command_num }, \
3716884c6c4SJason Gunthorpe .func_write = _func, \
372669dac1eSJason Gunthorpe _cmd_desc, \
3736884c6c4SJason Gunthorpe }, \
3746884c6c4SJason Gunthorpe ##__VA_ARGS__
3756884c6c4SJason Gunthorpe
3766884c6c4SJason Gunthorpe /* Use in a var_args of DECLARE_UVERBS_OBJECT */
377669dac1eSJason Gunthorpe #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
3786884c6c4SJason Gunthorpe { \
3796884c6c4SJason Gunthorpe .kind = UAPI_DEF_WRITE, \
3806884c6c4SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \
3816884c6c4SJason Gunthorpe .write = { .is_ex = 1, .command_num = _command_num }, \
382974d6b4bSJason Gunthorpe .func_write = _func, \
383669dac1eSJason Gunthorpe _cmd_desc, \
3846884c6c4SJason Gunthorpe }, \
3856884c6c4SJason Gunthorpe ##__VA_ARGS__
3866884c6c4SJason Gunthorpe
3876829c1c2SJason Gunthorpe /*
3886829c1c2SJason Gunthorpe * Object is only supported if the function pointer named ibdev_fn in struct
3896829c1c2SJason Gunthorpe * ib_device is not NULL.
3906829c1c2SJason Gunthorpe */
3916829c1c2SJason Gunthorpe #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
3926829c1c2SJason Gunthorpe { \
3936829c1c2SJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
3946829c1c2SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, \
3956829c1c2SJason Gunthorpe .needs_fn_offset = \
3963023a1e9SKamal Heib offsetof(struct ib_device_ops, ibdev_fn) + \
397bebcfe85SGustavo A. R. Silva BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
398bebcfe85SGustavo A. R. Silva ibdev_fn) != \
3996829c1c2SJason Gunthorpe sizeof(void *)), \
4006829c1c2SJason Gunthorpe }
4016829c1c2SJason Gunthorpe
402a140692aSJason Gunthorpe /*
403a140692aSJason Gunthorpe * Method is only supported if the function pointer named ibdev_fn in struct
404a140692aSJason Gunthorpe * ib_device is not NULL.
405a140692aSJason Gunthorpe */
406a140692aSJason Gunthorpe #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
407a140692aSJason Gunthorpe { \
408a140692aSJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
409a140692aSJason Gunthorpe .scope = UAPI_SCOPE_METHOD, \
410a140692aSJason Gunthorpe .needs_fn_offset = \
4113023a1e9SKamal Heib offsetof(struct ib_device_ops, ibdev_fn) + \
412bebcfe85SGustavo A. R. Silva BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
413bebcfe85SGustavo A. R. Silva ibdev_fn) != \
414a140692aSJason Gunthorpe sizeof(void *)), \
415a140692aSJason Gunthorpe }
416a140692aSJason Gunthorpe
4176829c1c2SJason Gunthorpe /* Call a function to determine if the entire object is supported or not */
4186829c1c2SJason Gunthorpe #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
4196829c1c2SJason Gunthorpe { \
4206829c1c2SJason Gunthorpe .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
4216829c1c2SJason Gunthorpe .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
4226829c1c2SJason Gunthorpe }
4236829c1c2SJason Gunthorpe
4240cbf432dSJason Gunthorpe /* Include another struct uapi_definition in this one */
4250cbf432dSJason Gunthorpe #define UAPI_DEF_CHAIN(_def_var) \
4260cbf432dSJason Gunthorpe { \
4270cbf432dSJason Gunthorpe .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
4280cbf432dSJason Gunthorpe }
4290cbf432dSJason Gunthorpe
4300cbf432dSJason Gunthorpe /* Temporary until the tree base description is replaced */
431a1462351SLeon Romanovsky #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \
4320cbf432dSJason Gunthorpe { \
4330cbf432dSJason Gunthorpe .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
4340cbf432dSJason Gunthorpe .object_start = { .object_id = _object_enum }, \
4350cbf432dSJason Gunthorpe .chain_obj_tree = _object_ptr, \
436a1462351SLeon Romanovsky }, \
4370cbf432dSJason Gunthorpe ##__VA_ARGS__
438a1462351SLeon Romanovsky #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
439*b39aeb33SArnd Bergmann UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, \
440*b39aeb33SArnd Bergmann PTR_IF(IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS), \
441*b39aeb33SArnd Bergmann &UVERBS_OBJECT(_object_enum)), \
442a1462351SLeon Romanovsky ##__VA_ARGS__)
4430cbf432dSJason Gunthorpe
444d108dac0SJason Gunthorpe /*
445d108dac0SJason Gunthorpe * =======================================
446d108dac0SJason Gunthorpe * Attribute Specifications
447d108dac0SJason Gunthorpe * =======================================
448d108dac0SJason Gunthorpe */
449c66db311SMatan Barak
450c66db311SMatan Barak #define UVERBS_ATTR_SIZE(_min_len, _len) \
451d108dac0SJason Gunthorpe .u.ptr.min_len = _min_len, .u.ptr.len = _len
452422e3d37SJason Gunthorpe
453fd44e385SYishai Hadas #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
454fd44e385SYishai Hadas
455422e3d37SJason Gunthorpe /*
456422e3d37SJason Gunthorpe * Specifies a uapi structure that cannot be extended. The user must always
457422e3d37SJason Gunthorpe * supply the whole structure and nothing more. The structure must be declared
458422e3d37SJason Gunthorpe * in a header under include/uapi/rdma.
459422e3d37SJason Gunthorpe */
460422e3d37SJason Gunthorpe #define UVERBS_ATTR_TYPE(_type) \
461422e3d37SJason Gunthorpe .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
462422e3d37SJason Gunthorpe /*
463422e3d37SJason Gunthorpe * Specifies a uapi structure where the user must provide at least up to
464422e3d37SJason Gunthorpe * member 'last'. Anything after last and up until the end of the structure
465422e3d37SJason Gunthorpe * can be non-zero, anything longer than the end of the structure must be
466422e3d37SJason Gunthorpe * zero. The structure must be declared in a header under include/uapi/rdma.
467422e3d37SJason Gunthorpe */
468422e3d37SJason Gunthorpe #define UVERBS_ATTR_STRUCT(_type, _last) \
469422e3d37SJason Gunthorpe .zero_trailing = 1, \
470ffd7339aSJason Gunthorpe UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type))
471540cd692SJason Gunthorpe /*
472540cd692SJason Gunthorpe * Specifies at least min_len bytes must be passed in, but the amount can be
473540cd692SJason Gunthorpe * larger, up to the protocol maximum size. No check for zeroing is done.
474540cd692SJason Gunthorpe */
475540cd692SJason Gunthorpe #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
476c66db311SMatan Barak
477d108dac0SJason Gunthorpe /* Must be used in the '...' of any UVERBS_ATTR */
47883bb4442SJason Gunthorpe #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
47983bb4442SJason Gunthorpe #define UA_MANDATORY .mandatory = 1
48083bb4442SJason Gunthorpe #define UA_OPTIONAL .mandatory = 0
48135410306SMatan Barak
48270cd20aeSGuy Levi /*
48370cd20aeSGuy Levi * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
48470cd20aeSGuy Levi * READ\WRITE accesses are supported.
48570cd20aeSGuy Levi */
48670cd20aeSGuy Levi #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
48770cd20aeSGuy Levi ...) \
48870cd20aeSGuy Levi (&(const struct uverbs_attr_def){ \
48970cd20aeSGuy Levi .id = (_attr_id) + \
49070cd20aeSGuy Levi BUILD_BUG_ON_ZERO((_min_len) == 0 || \
49170cd20aeSGuy Levi (_max_len) > \
49270cd20aeSGuy Levi PAGE_SIZE / sizeof(void *) || \
49370cd20aeSGuy Levi (_min_len) > (_max_len) || \
49470cd20aeSGuy Levi (_access) == UVERBS_ACCESS_NEW || \
49570cd20aeSGuy Levi (_access) == UVERBS_ACCESS_DESTROY), \
49670cd20aeSGuy Levi .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
49770cd20aeSGuy Levi .u2.objs_arr.obj_type = _idr_type, \
49870cd20aeSGuy Levi .u2.objs_arr.access = _access, \
49970cd20aeSGuy Levi .u2.objs_arr.min_len = _min_len, \
50070cd20aeSGuy Levi .u2.objs_arr.max_len = _max_len, \
50170cd20aeSGuy Levi __VA_ARGS__ } })
50270cd20aeSGuy Levi
5034d7e8cc5SYishai Hadas /*
5044d7e8cc5SYishai Hadas * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
5054d7e8cc5SYishai Hadas * the user must validate the type of the uobject instead.
5064d7e8cc5SYishai Hadas */
5074d7e8cc5SYishai Hadas #define UVERBS_IDR_ANY_OBJECT 0xFFFF
5084d7e8cc5SYishai Hadas
509d108dac0SJason Gunthorpe #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
5109a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \
511d108dac0SJason Gunthorpe .id = _attr_id, \
512d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
513d108dac0SJason Gunthorpe .u.obj.obj_type = _idr_type, \
514d108dac0SJason Gunthorpe .u.obj.access = _access, \
515d108dac0SJason Gunthorpe __VA_ARGS__ } })
516d108dac0SJason Gunthorpe
517d108dac0SJason Gunthorpe #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
5189a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \
519d108dac0SJason Gunthorpe .id = (_attr_id) + \
520d108dac0SJason Gunthorpe BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
52135410306SMatan Barak (_access) != UVERBS_ACCESS_READ), \
522d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_FD, \
523d108dac0SJason Gunthorpe .u.obj.obj_type = _fd_type, \
524d108dac0SJason Gunthorpe .u.obj.access = _access, \
525d108dac0SJason Gunthorpe __VA_ARGS__ } })
52635410306SMatan Barak
527015bda8aSJason Gunthorpe #define UVERBS_ATTR_RAW_FD(_attr_id, ...) \
528015bda8aSJason Gunthorpe (&(const struct uverbs_attr_def){ \
529015bda8aSJason Gunthorpe .id = (_attr_id), \
530015bda8aSJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_RAW_FD, __VA_ARGS__ } })
531015bda8aSJason Gunthorpe
532d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
5339a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \
534d108dac0SJason Gunthorpe .id = _attr_id, \
535d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
536d108dac0SJason Gunthorpe _type, \
537d108dac0SJason Gunthorpe __VA_ARGS__ } })
538d108dac0SJason Gunthorpe
539d108dac0SJason Gunthorpe #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
5409a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \
541d108dac0SJason Gunthorpe .id = _attr_id, \
542d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
543d108dac0SJason Gunthorpe _type, \
544d108dac0SJason Gunthorpe __VA_ARGS__ } })
545d108dac0SJason Gunthorpe
546d108dac0SJason Gunthorpe /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
547d108dac0SJason Gunthorpe #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
5489a119cd5SJason Gunthorpe (&(const struct uverbs_attr_def){ \
549d108dac0SJason Gunthorpe .id = _attr_id, \
550d108dac0SJason Gunthorpe .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
551d108dac0SJason Gunthorpe .u2.enum_def.ids = _enum_arr, \
552d108dac0SJason Gunthorpe .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
553d108dac0SJason Gunthorpe __VA_ARGS__ }, \
554d108dac0SJason Gunthorpe })
555d108dac0SJason Gunthorpe
5560953fffeSMark Bloch /* An input value that is a member in the enum _enum_type. */
5570953fffeSMark Bloch #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
5580953fffeSMark Bloch UVERBS_ATTR_PTR_IN( \
5590953fffeSMark Bloch _attr_id, \
5600953fffeSMark Bloch UVERBS_ATTR_SIZE( \
5610953fffeSMark Bloch sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
5620953fffeSMark Bloch sizeof(u64)), \
5630953fffeSMark Bloch __VA_ARGS__)
5640953fffeSMark Bloch
565d108dac0SJason Gunthorpe /*
566bccd0622SJason Gunthorpe * An input value that is a bitwise combination of values of _enum_type.
567bccd0622SJason Gunthorpe * This permits the flag value to be passed as either a u32 or u64, it must
568bccd0622SJason Gunthorpe * be retrieved via uverbs_get_flag().
569bccd0622SJason Gunthorpe */
570bccd0622SJason Gunthorpe #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
571bccd0622SJason Gunthorpe UVERBS_ATTR_PTR_IN( \
572bccd0622SJason Gunthorpe _attr_id, \
573bccd0622SJason Gunthorpe UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
574bccd0622SJason Gunthorpe !sizeof(_enum_type *)), \
575bccd0622SJason Gunthorpe sizeof(u64)), \
576bccd0622SJason Gunthorpe __VA_ARGS__)
577bccd0622SJason Gunthorpe
578bccd0622SJason Gunthorpe /*
5796c61d2a5SJason Gunthorpe * This spec is used in order to pass information to the hardware driver in a
5806c61d2a5SJason Gunthorpe * legacy way. Every verb that could get driver specific data should get this
5816c61d2a5SJason Gunthorpe * spec.
5826c61d2a5SJason Gunthorpe */
5836c61d2a5SJason Gunthorpe #define UVERBS_ATTR_UHW() \
5849a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
585540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \
58607f05f40SJason Gunthorpe UA_OPTIONAL, \
58707f05f40SJason Gunthorpe .is_udata = 1), \
5889a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
589540cd692SJason Gunthorpe UVERBS_ATTR_MIN_SIZE(0), \
59007f05f40SJason Gunthorpe UA_OPTIONAL, \
59107f05f40SJason Gunthorpe .is_udata = 1)
5926c61d2a5SJason Gunthorpe
593fac9658cSMatan Barak /* =================================================
594fac9658cSMatan Barak * Parsing infrastructure
595fac9658cSMatan Barak * =================================================
596fac9658cSMatan Barak */
597fac9658cSMatan Barak
5983a863577SJason Gunthorpe
599fac9658cSMatan Barak struct uverbs_ptr_attr {
6008762d149SMatan Barak /*
6018762d149SMatan Barak * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
6028762d149SMatan Barak * used.
6038762d149SMatan Barak */
6048762d149SMatan Barak union {
6058762d149SMatan Barak void *ptr;
606fac9658cSMatan Barak u64 data;
6078762d149SMatan Barak };
608fac9658cSMatan Barak u16 len;
6096a1f444fSJason Gunthorpe u16 uattr_idx;
610494c5580SMatan Barak u8 enum_id;
611fac9658cSMatan Barak };
612fac9658cSMatan Barak
613f43dbebfSMatan Barak struct uverbs_obj_attr {
614f43dbebfSMatan Barak struct ib_uobject *uobject;
6153a863577SJason Gunthorpe const struct uverbs_api_attr *attr_elm;
616f43dbebfSMatan Barak };
617f43dbebfSMatan Barak
61870cd20aeSGuy Levi struct uverbs_objs_arr_attr {
61970cd20aeSGuy Levi struct ib_uobject **uobjects;
62070cd20aeSGuy Levi u16 len;
62170cd20aeSGuy Levi };
62270cd20aeSGuy Levi
623f43dbebfSMatan Barak struct uverbs_attr {
624fac9658cSMatan Barak union {
625fac9658cSMatan Barak struct uverbs_ptr_attr ptr_attr;
626f43dbebfSMatan Barak struct uverbs_obj_attr obj_attr;
62770cd20aeSGuy Levi struct uverbs_objs_arr_attr objs_arr_attr;
628f43dbebfSMatan Barak };
629fac9658cSMatan Barak };
630f43dbebfSMatan Barak
631f43dbebfSMatan Barak struct uverbs_attr_bundle {
632ef87df2cSJason Gunthorpe struct ib_udata driver_udata;
633c2a939fdSJason Gunthorpe struct ib_udata ucore;
6344b3dd2bbSJason Gunthorpe struct ib_uverbs_file *ufile;
6353d9dfd06SShamir Rabinovitch struct ib_ucontext *context;
6360f63ef1dSLeon Romanovsky struct ib_uobject *uobject;
6373a863577SJason Gunthorpe DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
6383a863577SJason Gunthorpe struct uverbs_attr attrs[];
639f43dbebfSMatan Barak };
640f43dbebfSMatan Barak
uverbs_attr_is_valid(const struct uverbs_attr_bundle * attrs_bundle,unsigned int idx)64135410306SMatan Barak static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
64235410306SMatan Barak unsigned int idx)
64335410306SMatan Barak {
6443a863577SJason Gunthorpe return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
6453a863577SJason Gunthorpe attrs_bundle->attr_present);
64635410306SMatan Barak }
64735410306SMatan Barak
648730623f4SShamir Rabinovitch /**
649730623f4SShamir Rabinovitch * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
650730623f4SShamir Rabinovitch * ib_udata which is embedded in uverbs_attr_bundle.
651730623f4SShamir Rabinovitch *
652730623f4SShamir Rabinovitch * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
653730623f4SShamir Rabinovitch * in a NULL ucontext pointer, as a safety precaution. Callers should be using
654730623f4SShamir Rabinovitch * 'udata' to determine if the driver call is in user or kernel mode, not
655730623f4SShamir Rabinovitch * 'ucontext'.
656730623f4SShamir Rabinovitch *
657730623f4SShamir Rabinovitch */
658bfb972c5SArnd Bergmann static inline struct uverbs_attr_bundle *
rdma_udata_to_uverbs_attr_bundle(struct ib_udata * udata)659bfb972c5SArnd Bergmann rdma_udata_to_uverbs_attr_bundle(struct ib_udata *udata)
660bfb972c5SArnd Bergmann {
661bfb972c5SArnd Bergmann return container_of(udata, struct uverbs_attr_bundle, driver_udata);
662bfb972c5SArnd Bergmann }
663bfb972c5SArnd Bergmann
664730623f4SShamir Rabinovitch #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
665bfb972c5SArnd Bergmann (udata ? container_of(rdma_udata_to_uverbs_attr_bundle(udata)->context, \
666bfb972c5SArnd Bergmann drv_dev_struct, member) : (drv_dev_struct *)NULL)
667730623f4SShamir Rabinovitch
66841b2a71fSMatan Barak #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
66941b2a71fSMatan Barak
uverbs_attr_get(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)670d70724f1SMatan Barak static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
671d70724f1SMatan Barak u16 idx)
672d70724f1SMatan Barak {
673d70724f1SMatan Barak if (!uverbs_attr_is_valid(attrs_bundle, idx))
674d70724f1SMatan Barak return ERR_PTR(-ENOENT);
675d70724f1SMatan Barak
6763a863577SJason Gunthorpe return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
677d70724f1SMatan Barak }
678d70724f1SMatan Barak
uverbs_attr_get_enum_id(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)679494c5580SMatan Barak static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
680494c5580SMatan Barak u16 idx)
681494c5580SMatan Barak {
682494c5580SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
683494c5580SMatan Barak
684494c5580SMatan Barak if (IS_ERR(attr))
685494c5580SMatan Barak return PTR_ERR(attr);
686494c5580SMatan Barak
687494c5580SMatan Barak return attr->ptr_attr.enum_id;
688494c5580SMatan Barak }
689494c5580SMatan Barak
uverbs_attr_get_obj(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)690be934ccaSAriel Levkovich static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
691be934ccaSAriel Levkovich u16 idx)
692be934ccaSAriel Levkovich {
693f4602cbbSJason Gunthorpe const struct uverbs_attr *attr;
694be934ccaSAriel Levkovich
695f4602cbbSJason Gunthorpe attr = uverbs_attr_get(attrs_bundle, idx);
696f4602cbbSJason Gunthorpe if (IS_ERR(attr))
697f4602cbbSJason Gunthorpe return ERR_CAST(attr);
698be934ccaSAriel Levkovich
699f4602cbbSJason Gunthorpe return attr->obj_attr.uobject->object;
700be934ccaSAriel Levkovich }
701be934ccaSAriel Levkovich
uverbs_attr_get_uobject(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)7023efa3812SMatan Barak static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
7033efa3812SMatan Barak u16 idx)
7043efa3812SMatan Barak {
7053efa3812SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
7063efa3812SMatan Barak
7073efa3812SMatan Barak if (IS_ERR(attr))
7083efa3812SMatan Barak return ERR_CAST(attr);
7093efa3812SMatan Barak
7103efa3812SMatan Barak return attr->obj_attr.uobject;
7113efa3812SMatan Barak }
7123efa3812SMatan Barak
7138762d149SMatan Barak static inline int
uverbs_attr_get_len(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)7148762d149SMatan Barak uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
7158762d149SMatan Barak {
7168762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
7178762d149SMatan Barak
7188762d149SMatan Barak if (IS_ERR(attr))
7198762d149SMatan Barak return PTR_ERR(attr);
7208762d149SMatan Barak
7218762d149SMatan Barak return attr->ptr_attr.len;
7228762d149SMatan Barak }
7238762d149SMatan Barak
7240ac8903cSJason Gunthorpe void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
7250ac8903cSJason Gunthorpe u16 idx);
7260ac8903cSJason Gunthorpe
727cbfdd442SMoni Shoua /*
728cbfdd442SMoni Shoua * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
729cbfdd442SMoni Shoua * attribute.
730cbfdd442SMoni Shoua * @attrs: The attribute bundle
731cbfdd442SMoni Shoua * @idx: The ID of the attribute
732cbfdd442SMoni Shoua * @elem_size: The size of the element in the array
733cbfdd442SMoni Shoua */
734cbfdd442SMoni Shoua static inline int
uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle * attrs,u16 idx,size_t elem_size)735cbfdd442SMoni Shoua uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
736cbfdd442SMoni Shoua size_t elem_size)
737cbfdd442SMoni Shoua {
738cbfdd442SMoni Shoua int size = uverbs_attr_get_len(attrs, idx);
739cbfdd442SMoni Shoua
740cbfdd442SMoni Shoua if (size < 0)
741cbfdd442SMoni Shoua return size;
742cbfdd442SMoni Shoua
743cbfdd442SMoni Shoua if (size % elem_size)
744cbfdd442SMoni Shoua return -EINVAL;
745cbfdd442SMoni Shoua
746cbfdd442SMoni Shoua return size / elem_size;
747cbfdd442SMoni Shoua }
748cbfdd442SMoni Shoua
74970cd20aeSGuy Levi /**
75070cd20aeSGuy Levi * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
75170cd20aeSGuy Levi * UVERBS_ATTR_TYPE_IDRS_ARRAY.
75270cd20aeSGuy Levi * @arr: Returned pointer to array of pointers for uobjects or NULL if
75370cd20aeSGuy Levi * the attribute isn't provided.
75470cd20aeSGuy Levi *
75570cd20aeSGuy Levi * Return: The array length or 0 if no attribute was provided.
75670cd20aeSGuy Levi */
uverbs_attr_get_uobjs_arr(const struct uverbs_attr_bundle * attrs_bundle,u16 attr_idx,struct ib_uobject *** arr)75770cd20aeSGuy Levi static inline int uverbs_attr_get_uobjs_arr(
75870cd20aeSGuy Levi const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
75970cd20aeSGuy Levi struct ib_uobject ***arr)
76070cd20aeSGuy Levi {
76170cd20aeSGuy Levi const struct uverbs_attr *attr =
76270cd20aeSGuy Levi uverbs_attr_get(attrs_bundle, attr_idx);
76370cd20aeSGuy Levi
76470cd20aeSGuy Levi if (IS_ERR(attr)) {
76570cd20aeSGuy Levi *arr = NULL;
76670cd20aeSGuy Levi return 0;
76770cd20aeSGuy Levi }
76870cd20aeSGuy Levi
76970cd20aeSGuy Levi *arr = attr->objs_arr_attr.uobjects;
77070cd20aeSGuy Levi
77170cd20aeSGuy Levi return attr->objs_arr_attr.len;
77270cd20aeSGuy Levi }
77370cd20aeSGuy Levi
uverbs_attr_ptr_is_inline(const struct uverbs_attr * attr)77489d9e8d3SMatan Barak static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
77589d9e8d3SMatan Barak {
77689d9e8d3SMatan Barak return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
77789d9e8d3SMatan Barak }
77889d9e8d3SMatan Barak
uverbs_attr_get_alloced_ptr(const struct uverbs_attr_bundle * attrs_bundle,u16 idx)7798762d149SMatan Barak static inline void *uverbs_attr_get_alloced_ptr(
7808762d149SMatan Barak const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
7818762d149SMatan Barak {
7828762d149SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
7838762d149SMatan Barak
7848762d149SMatan Barak if (IS_ERR(attr))
7858762d149SMatan Barak return (void *)attr;
7868762d149SMatan Barak
7878762d149SMatan Barak return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
7888762d149SMatan Barak attr->ptr_attr.ptr;
7898762d149SMatan Barak }
7908762d149SMatan Barak
_uverbs_copy_from(void * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,size_t size)79189d9e8d3SMatan Barak static inline int _uverbs_copy_from(void *to,
792d70724f1SMatan Barak const struct uverbs_attr_bundle *attrs_bundle,
79389d9e8d3SMatan Barak size_t idx,
79489d9e8d3SMatan Barak size_t size)
795d70724f1SMatan Barak {
796d70724f1SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
797d70724f1SMatan Barak
798d70724f1SMatan Barak if (IS_ERR(attr))
799d70724f1SMatan Barak return PTR_ERR(attr);
800d70724f1SMatan Barak
80189d9e8d3SMatan Barak /*
80289d9e8d3SMatan Barak * Validation ensures attr->ptr_attr.len >= size. If the caller is
803c66db311SMatan Barak * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
804c66db311SMatan Barak * uverbs_copy_from_or_zero.
80589d9e8d3SMatan Barak */
80689d9e8d3SMatan Barak if (unlikely(size < attr->ptr_attr.len))
80789d9e8d3SMatan Barak return -EINVAL;
80889d9e8d3SMatan Barak
80989d9e8d3SMatan Barak if (uverbs_attr_ptr_is_inline(attr))
810d70724f1SMatan Barak memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
8112f36028cSJason Gunthorpe else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
8122f36028cSJason Gunthorpe attr->ptr_attr.len))
813d70724f1SMatan Barak return -EFAULT;
814d70724f1SMatan Barak
815d70724f1SMatan Barak return 0;
816d70724f1SMatan Barak }
817d70724f1SMatan Barak
_uverbs_copy_from_or_zero(void * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,size_t size)818c66db311SMatan Barak static inline int _uverbs_copy_from_or_zero(void *to,
819c66db311SMatan Barak const struct uverbs_attr_bundle *attrs_bundle,
820c66db311SMatan Barak size_t idx,
821c66db311SMatan Barak size_t size)
822c66db311SMatan Barak {
823c66db311SMatan Barak const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
824c66db311SMatan Barak size_t min_size;
825c66db311SMatan Barak
826c66db311SMatan Barak if (IS_ERR(attr))
827c66db311SMatan Barak return PTR_ERR(attr);
828c66db311SMatan Barak
829c66db311SMatan Barak min_size = min_t(size_t, size, attr->ptr_attr.len);
830c66db311SMatan Barak
831c66db311SMatan Barak if (uverbs_attr_ptr_is_inline(attr))
832c66db311SMatan Barak memcpy(to, &attr->ptr_attr.data, min_size);
833c66db311SMatan Barak else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
834c66db311SMatan Barak min_size))
835c66db311SMatan Barak return -EFAULT;
836c66db311SMatan Barak
837c66db311SMatan Barak if (size > min_size)
838c66db311SMatan Barak memset(to + min_size, 0, size - min_size);
839c66db311SMatan Barak
840c66db311SMatan Barak return 0;
841c66db311SMatan Barak }
842c66db311SMatan Barak
843d70724f1SMatan Barak #define uverbs_copy_from(to, attrs_bundle, idx) \
84489d9e8d3SMatan Barak _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
845d70724f1SMatan Barak
846c66db311SMatan Barak #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
847c66db311SMatan Barak _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
848c66db311SMatan Barak
8498313c10fSJason Gunthorpe static inline struct ib_ucontext *
ib_uverbs_get_ucontext(const struct uverbs_attr_bundle * attrs)8508313c10fSJason Gunthorpe ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
8518313c10fSJason Gunthorpe {
8528313c10fSJason Gunthorpe return ib_uverbs_get_ucontext_file(attrs->ufile);
8538313c10fSJason Gunthorpe }
8548313c10fSJason Gunthorpe
855bccd0622SJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
856bccd0622SJason Gunthorpe int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
857bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits);
858bccd0622SJason Gunthorpe int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
859bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits);
8606a1f444fSJason Gunthorpe int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
8616a1f444fSJason Gunthorpe const void *from, size_t size);
862461bb2eeSJason Gunthorpe __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
863461bb2eeSJason Gunthorpe gfp_t flags);
864461bb2eeSJason Gunthorpe
uverbs_alloc(struct uverbs_attr_bundle * bundle,size_t size)865461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
866461bb2eeSJason Gunthorpe size_t size)
867461bb2eeSJason Gunthorpe {
868461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL);
869461bb2eeSJason Gunthorpe }
870461bb2eeSJason Gunthorpe
uverbs_zalloc(struct uverbs_attr_bundle * bundle,size_t size)871461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
872461bb2eeSJason Gunthorpe size_t size)
873461bb2eeSJason Gunthorpe {
874461bb2eeSJason Gunthorpe return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
875461bb2eeSJason Gunthorpe }
876e0da6899SAvihai Horon
uverbs_kcalloc(struct uverbs_attr_bundle * bundle,size_t n,size_t size)877e0da6899SAvihai Horon static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle,
878e0da6899SAvihai Horon size_t n, size_t size)
879e0da6899SAvihai Horon {
880e0da6899SAvihai Horon size_t bytes;
881e0da6899SAvihai Horon
882e0da6899SAvihai Horon if (unlikely(check_mul_overflow(n, size, &bytes)))
883e0da6899SAvihai Horon return ERR_PTR(-EOVERFLOW);
884e0da6899SAvihai Horon return uverbs_zalloc(bundle, bytes);
885e0da6899SAvihai Horon }
8862904bb37SYishai Hadas
8872904bb37SYishai Hadas int _uverbs_get_const_signed(s64 *to,
8882904bb37SYishai Hadas const struct uverbs_attr_bundle *attrs_bundle,
8890953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound,
8900953fffeSMark Bloch s64 *def_val);
8912904bb37SYishai Hadas int _uverbs_get_const_unsigned(u64 *to,
8922904bb37SYishai Hadas const struct uverbs_attr_bundle *attrs_bundle,
8932904bb37SYishai Hadas size_t idx, u64 upper_bound, u64 *def_val);
8942e8039c6SMichael Guralnik int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
8952e8039c6SMichael Guralnik size_t idx, const void *from, size_t size);
896bccd0622SJason Gunthorpe #else
897bccd0622SJason Gunthorpe static inline int
uverbs_get_flags64(u64 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,u64 allowed_bits)898bccd0622SJason Gunthorpe uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
899bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits)
900bccd0622SJason Gunthorpe {
901bccd0622SJason Gunthorpe return -EINVAL;
902bccd0622SJason Gunthorpe }
903bccd0622SJason Gunthorpe static inline int
uverbs_get_flags32(u32 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,u64 allowed_bits)904bccd0622SJason Gunthorpe uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
905bccd0622SJason Gunthorpe size_t idx, u64 allowed_bits)
906bccd0622SJason Gunthorpe {
907bccd0622SJason Gunthorpe return -EINVAL;
908bccd0622SJason Gunthorpe }
uverbs_copy_to(const struct uverbs_attr_bundle * attrs_bundle,size_t idx,const void * from,size_t size)9096a1f444fSJason Gunthorpe static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
9106a1f444fSJason Gunthorpe size_t idx, const void *from, size_t size)
9116a1f444fSJason Gunthorpe {
9126a1f444fSJason Gunthorpe return -EINVAL;
9136a1f444fSJason Gunthorpe }
uverbs_alloc(struct uverbs_attr_bundle * bundle,size_t size)914461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
915461bb2eeSJason Gunthorpe size_t size)
916461bb2eeSJason Gunthorpe {
917461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL);
918461bb2eeSJason Gunthorpe }
uverbs_zalloc(struct uverbs_attr_bundle * bundle,size_t size)919461bb2eeSJason Gunthorpe static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
920461bb2eeSJason Gunthorpe size_t size)
921461bb2eeSJason Gunthorpe {
922461bb2eeSJason Gunthorpe return ERR_PTR(-EINVAL);
923461bb2eeSJason Gunthorpe }
9240953fffeSMark Bloch static inline int
_uverbs_get_const(s64 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,s64 lower_bound,u64 upper_bound,s64 * def_val)9250953fffeSMark Bloch _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
9260953fffeSMark Bloch size_t idx, s64 lower_bound, u64 upper_bound,
9270953fffeSMark Bloch s64 *def_val)
9280953fffeSMark Bloch {
9290953fffeSMark Bloch return -EINVAL;
9300953fffeSMark Bloch }
9312e8039c6SMichael Guralnik static inline int
uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle * bundle,size_t idx,const void * from,size_t size)9322e8039c6SMichael Guralnik uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
9332e8039c6SMichael Guralnik size_t idx, const void *from, size_t size)
9342e8039c6SMichael Guralnik {
9352e8039c6SMichael Guralnik return -EINVAL;
9362e8039c6SMichael Guralnik }
937dbb3e9dbSYueHaibing static inline int
_uverbs_get_const_signed(s64 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,s64 lower_bound,u64 upper_bound,s64 * def_val)938dbb3e9dbSYueHaibing _uverbs_get_const_signed(s64 *to,
939dbb3e9dbSYueHaibing const struct uverbs_attr_bundle *attrs_bundle,
9402904bb37SYishai Hadas size_t idx, s64 lower_bound, u64 upper_bound,
9412904bb37SYishai Hadas s64 *def_val)
9422904bb37SYishai Hadas {
9432904bb37SYishai Hadas return -EINVAL;
9442904bb37SYishai Hadas }
945dbb3e9dbSYueHaibing static inline int
_uverbs_get_const_unsigned(u64 * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx,u64 upper_bound,u64 * def_val)9462904bb37SYishai Hadas _uverbs_get_const_unsigned(u64 *to,
9472904bb37SYishai Hadas const struct uverbs_attr_bundle *attrs_bundle,
9482904bb37SYishai Hadas size_t idx, u64 upper_bound, u64 *def_val)
9492904bb37SYishai Hadas {
9502904bb37SYishai Hadas return -EINVAL;
9512904bb37SYishai Hadas }
952bccd0622SJason Gunthorpe #endif
953bccd0622SJason Gunthorpe
9542904bb37SYishai Hadas #define uverbs_get_const_signed(_to, _attrs_bundle, _idx) \
9550953fffeSMark Bloch ({ \
9560953fffeSMark Bloch s64 _val; \
9572904bb37SYishai Hadas int _ret = \
9582904bb37SYishai Hadas _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \
9592904bb37SYishai Hadas type_min(typeof(*(_to))), \
9602904bb37SYishai Hadas type_max(typeof(*(_to))), NULL); \
9612904bb37SYishai Hadas (*(_to)) = _val; \
9620953fffeSMark Bloch _ret; \
9630953fffeSMark Bloch })
9640953fffeSMark Bloch
9652904bb37SYishai Hadas #define uverbs_get_const_unsigned(_to, _attrs_bundle, _idx) \
9662904bb37SYishai Hadas ({ \
9672904bb37SYishai Hadas u64 _val; \
9682904bb37SYishai Hadas int _ret = \
9692904bb37SYishai Hadas _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \
9702904bb37SYishai Hadas type_max(typeof(*(_to))), NULL); \
9712904bb37SYishai Hadas (*(_to)) = _val; \
9722904bb37SYishai Hadas _ret; \
9732904bb37SYishai Hadas })
9742904bb37SYishai Hadas
9752904bb37SYishai Hadas #define uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, _default) \
9760953fffeSMark Bloch ({ \
9770953fffeSMark Bloch s64 _val; \
9780953fffeSMark Bloch s64 _def_val = _default; \
9790953fffeSMark Bloch int _ret = \
9802904bb37SYishai Hadas _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \
9812904bb37SYishai Hadas type_min(typeof(*(_to))), \
9822904bb37SYishai Hadas type_max(typeof(*(_to))), &_def_val); \
9832904bb37SYishai Hadas (*(_to)) = _val; \
9840953fffeSMark Bloch _ret; \
9850953fffeSMark Bloch })
9862904bb37SYishai Hadas
9872904bb37SYishai Hadas #define uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, _default) \
9882904bb37SYishai Hadas ({ \
9892904bb37SYishai Hadas u64 _val; \
9902904bb37SYishai Hadas u64 _def_val = _default; \
9912904bb37SYishai Hadas int _ret = \
9922904bb37SYishai Hadas _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \
9932904bb37SYishai Hadas type_max(typeof(*(_to))), &_def_val); \
9942904bb37SYishai Hadas (*(_to)) = _val; \
9952904bb37SYishai Hadas _ret; \
9962904bb37SYishai Hadas })
9972904bb37SYishai Hadas
9982904bb37SYishai Hadas #define uverbs_get_const(_to, _attrs_bundle, _idx) \
9992904bb37SYishai Hadas (is_signed_type(typeof(*(_to))) ? \
10002904bb37SYishai Hadas uverbs_get_const_signed(_to, _attrs_bundle, _idx) : \
10012904bb37SYishai Hadas uverbs_get_const_unsigned(_to, _attrs_bundle, _idx)) \
10022904bb37SYishai Hadas
10032904bb37SYishai Hadas #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
10042904bb37SYishai Hadas (is_signed_type(typeof(*(_to))) ? \
10052904bb37SYishai Hadas uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, \
10062904bb37SYishai Hadas _default) : \
10072904bb37SYishai Hadas uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, \
10082904bb37SYishai Hadas _default))
10092904bb37SYishai Hadas
1010015bda8aSJason Gunthorpe static inline int
uverbs_get_raw_fd(int * to,const struct uverbs_attr_bundle * attrs_bundle,size_t idx)1011015bda8aSJason Gunthorpe uverbs_get_raw_fd(int *to, const struct uverbs_attr_bundle *attrs_bundle,
1012015bda8aSJason Gunthorpe size_t idx)
1013015bda8aSJason Gunthorpe {
1014015bda8aSJason Gunthorpe return uverbs_get_const_signed(to, attrs_bundle, idx);
1015015bda8aSJason Gunthorpe }
1016015bda8aSJason Gunthorpe
1017118620d3SMatan Barak #endif
1018