Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #ifndef _UVERBS_IOCTL_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define _UVERBS_IOCTL_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <rdma/uverbs_types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <rdma/rdma_user_ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <rdma/ib_user_ioctl_verbs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <rdma/ib_user_ioctl_cmds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *	Verbs action specifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) enum uverbs_attr_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	UVERBS_ATTR_TYPE_NA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	UVERBS_ATTR_TYPE_PTR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	UVERBS_ATTR_TYPE_PTR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	UVERBS_ATTR_TYPE_IDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	UVERBS_ATTR_TYPE_FD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	UVERBS_ATTR_TYPE_ENUM_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	UVERBS_ATTR_TYPE_IDRS_ARRAY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) enum uverbs_obj_access {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	UVERBS_ACCESS_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	UVERBS_ACCESS_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	UVERBS_ACCESS_NEW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	UVERBS_ACCESS_DESTROY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /* Specification of a single attribute inside the ioctl message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* good size 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct uverbs_attr_spec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 * Support extending attributes by length. Allow the user to provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	 * more bytes than ptr.len, but check that everything after is zero'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 * by the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	u8 zero_trailing:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	 * Valid only for PTR_IN. Allocate and copy the data inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * the parser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u8 alloc_and_copy:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u8 mandatory:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	/* True if this is from UVERBS_ATTR_UHW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	u8 is_udata:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			/* Current known size to kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			u16 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			/* User isn't allowed to provide something < min_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			u16 min_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		} ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			 * higher bits mean the namespace and lower bits mean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			 * the type id within the namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			u16 obj_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			u8 access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		} obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			u8 num_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		} enum_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* This weird split lets us remove some padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			 * The enum attribute can select one of the attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			 * contained in the ids array. Currently only PTR_IN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			 * attributes are supported in the ids array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			const struct uverbs_attr_spec *ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		} enum_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			 * higher bits mean the namespace and lower bits mean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			 * the type id within the namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			u16				obj_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			u16				min_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			u16				max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			u8				access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		} objs_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	} u2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * Information about the API is loaded into a radix tree. For IOCTL we start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * with a tuple of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  *  object_id, attr_id, method_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * on the current kernel support this is compressed into 16 bit key for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * radix tree. Since this compression is entirely internal to the kernel the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * below limits can be revised if the kernel gains additional data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * With 64 leafs per node this is a 3 level radix tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * This also encodes the tables for the write() and write() extended commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * using the coding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  *   OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  *   OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * ie the WRITE path is treated as a special method type in the ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) enum uapi_radix_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	UVERBS_API_ATTR_KEY_BITS = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	UVERBS_API_METHOD_KEY_BITS = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	UVERBS_API_METHOD_KEY_NUM_CORE = 22,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	UVERBS_API_METHOD_KEY_NUM_DRIVER =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		(UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		UVERBS_API_METHOD_KEY_NUM_CORE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	UVERBS_API_METHOD_KEY_MASK = GENMASK(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		UVERBS_API_METHOD_KEY_SHIFT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	UVERBS_API_OBJ_KEY_BITS = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	UVERBS_API_OBJ_KEY_SHIFT =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	UVERBS_API_OBJ_KEY_NUM_CORE = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	UVERBS_API_OBJ_KEY_NUM_DRIVER =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	/* This id guaranteed to not exist in the radix tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	UVERBS_API_KEY_ERR = 0xFFFFFFFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static inline __attribute_const__ u32 uapi_key_obj(u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (id & UVERBS_API_NS_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		id &= ~UVERBS_API_NS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return id << UVERBS_API_OBJ_KEY_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline __attribute_const__ bool uapi_key_is_object(u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (id & UVERBS_API_NS_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		id &= ~UVERBS_API_NS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return id << UVERBS_API_METHOD_KEY_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (id >= UVERBS_API_WRITE_KEY_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return UVERBS_API_METHOD_IS_WRITE | id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (id >= UVERBS_API_WRITE_KEY_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return UVERBS_API_METHOD_IS_WRITE_EX | id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static inline __attribute_const__ u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) uapi_key_attr_to_ioctl_method(u32 attr_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return attr_key &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	       (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	       (key & UVERBS_API_ATTR_KEY_MASK) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return (key & UVERBS_API_METHOD_KEY_MASK) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	       UVERBS_API_METHOD_IS_WRITE_EX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	/* 0 is the method slot itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return ioctl_method_key + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static inline __attribute_const__ u32 uapi_key_attr(u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * The attr is designed to fit in the typical single radix tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 * of 64 entries. Since allmost all methods have driver attributes we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 * organize things so that the driver and core attributes interleave to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 * reduce the length of the attributes array in typical cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (id & UVERBS_API_NS_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		id &= ~UVERBS_API_NS_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		id = (id << 1) | 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			return UVERBS_API_KEY_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		id = (id << 1) | 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* Only true for ioctl methods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	       (key & UVERBS_API_ATTR_KEY_MASK) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * basically it undoes the reservation of 0 in the ID numbering. attr_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * uapi_key_attr().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return attr_key - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return attr_bkey + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  *	Verbs definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct uverbs_attr_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	u16                           id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct uverbs_attr_spec       attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct uverbs_method_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	u16                                  id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	u32				     flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	size_t				     num_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	const struct uverbs_attr_def * const (*attrs)[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int (*handler)(struct uverbs_attr_bundle *attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct uverbs_object_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	u16					 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	const struct uverbs_obj_type	        *type_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	size_t				         num_methods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	const struct uverbs_method_def * const (*methods)[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) enum uapi_definition_kind {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	UAPI_DEF_END = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	UAPI_DEF_OBJECT_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	UAPI_DEF_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	UAPI_DEF_CHAIN_OBJ_TREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	UAPI_DEF_CHAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	UAPI_DEF_IS_SUPPORTED_FUNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	UAPI_DEF_IS_SUPPORTED_DEV_FN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) enum uapi_definition_scope {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	UAPI_SCOPE_OBJECT = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	UAPI_SCOPE_METHOD = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct uapi_definition {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	u8 kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	u8 scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			u16 object_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		} object_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			u16 command_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			u8 is_ex:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			u8 has_udata:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			u8 has_resp:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			u8 req_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			u8 resp_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		} write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		bool (*func_is_supported)(struct ib_device *device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		int (*func_write)(struct uverbs_attr_bundle *attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		const struct uapi_definition *chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		const struct uverbs_object_def *chain_obj_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		size_t needs_fn_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* Define things connected to object_id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) #define DECLARE_UVERBS_OBJECT(_object_id, ...)                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		.kind = UAPI_DEF_OBJECT_START,                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		.object_start = { .object_id = _object_id },                   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	},                                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		##__VA_ARGS__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /* Use in a var_args of DECLARE_UVERBS_OBJECT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...)              \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		.kind = UAPI_DEF_WRITE,                                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		.scope = UAPI_SCOPE_OBJECT,                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		.write = { .is_ex = 0, .command_num = _command_num },          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		.func_write = _func,                                           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		_cmd_desc,                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	},                                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		##__VA_ARGS__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* Use in a var_args of DECLARE_UVERBS_OBJECT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...)           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		.kind = UAPI_DEF_WRITE,                                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		.scope = UAPI_SCOPE_OBJECT,                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		.write = { .is_ex = 1, .command_num = _command_num },          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		.func_write = _func,                                           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		_cmd_desc,                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	},                                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		##__VA_ARGS__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * Object is only supported if the function pointer named ibdev_fn in struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * ib_device is not NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn)                                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		.kind = UAPI_DEF_IS_SUPPORTED_DEV_FN,                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		.scope = UAPI_SCOPE_OBJECT,                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		.needs_fn_offset =                                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			offsetof(struct ib_device_ops, ibdev_fn) +             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops,   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 						       ibdev_fn) !=            \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 					  sizeof(void *)),                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * Method is only supported if the function pointer named ibdev_fn in struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * ib_device is not NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn)                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		.kind = UAPI_DEF_IS_SUPPORTED_DEV_FN,                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		.scope = UAPI_SCOPE_METHOD,                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		.needs_fn_offset =                                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			offsetof(struct ib_device_ops, ibdev_fn) +             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops,   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 						       ibdev_fn) !=            \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 					  sizeof(void *)),                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* Call a function to determine if the entire object is supported or not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) #define UAPI_DEF_IS_OBJ_SUPPORTED(_func)                                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		.kind = UAPI_DEF_IS_SUPPORTED_FUNC,                            \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		.scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func,        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /* Include another struct uapi_definition in this one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) #define UAPI_DEF_CHAIN(_def_var)                                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		.kind = UAPI_DEF_CHAIN, .chain = _def_var,                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* Temporary until the tree base description is replaced */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...)                \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	{                                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		.kind = UAPI_DEF_CHAIN_OBJ_TREE,                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		.object_start = { .object_id = _object_enum },                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		.chain_obj_tree = _object_ptr,                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	},								       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		##__VA_ARGS__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...)                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum),    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 				##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  *	Attribute Specifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  * =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) #define UVERBS_ATTR_SIZE(_min_len, _len)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	.u.ptr.min_len = _min_len, .u.ptr.len = _len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * Specifies a uapi structure that cannot be extended. The user must always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  * supply the whole structure and nothing more. The structure must be declared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  * in a header under include/uapi/rdma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) #define UVERBS_ATTR_TYPE(_type)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * Specifies a uapi structure where the user must provide at least up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  * member 'last'.  Anything after last and up until the end of the structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  * can be non-zero, anything longer than the end of the structure must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)  * zero. The structure must be declared in a header under include/uapi/rdma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #define UVERBS_ATTR_STRUCT(_type, _last)                                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	.zero_trailing = 1,                                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  * Specifies at least min_len bytes must be passed in, but the amount can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)  * larger, up to the protocol maximum size. No check for zeroing is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /* Must be used in the '...' of any UVERBS_ATTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #define UA_MANDATORY .mandatory = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) #define UA_OPTIONAL .mandatory = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)  * min_len must be bigger than 0 and _max_len must be smaller than 4095.  Only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)  * READ\WRITE accesses are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			     ...)                                              \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	(&(const struct uverbs_attr_def){                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		.id = (_attr_id) +                                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		      BUILD_BUG_ON_ZERO((_min_len) == 0 ||                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 					(_max_len) >                           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 						PAGE_SIZE / sizeof(void *) ||  \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 					(_min_len) > (_max_len) ||             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 					(_access) == UVERBS_ACCESS_NEW ||      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 					(_access) == UVERBS_ACCESS_DESTROY),   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		.attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY,                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			  .u2.objs_arr.obj_type = _idr_type,                   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			  .u2.objs_arr.access = _access,                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			  .u2.objs_arr.min_len = _min_len,                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			  .u2.objs_arr.max_len = _max_len,                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			  __VA_ARGS__ } })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)  * the user must validate the type of the uobject instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) #define UVERBS_IDR_ANY_OBJECT 0xFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...)                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	(&(const struct uverbs_attr_def){                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		.id = _attr_id,                                                \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		.attr = { .type = UVERBS_ATTR_TYPE_IDR,                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			  .u.obj.obj_type = _idr_type,                         \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			  .u.obj.access = _access,                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			  __VA_ARGS__ } })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...)                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	(&(const struct uverbs_attr_def){                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		.id = (_attr_id) +                                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		      BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW &&      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 					(_access) != UVERBS_ACCESS_READ),      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		.attr = { .type = UVERBS_ATTR_TYPE_FD,                         \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			  .u.obj.obj_type = _fd_type,                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			  .u.obj.access = _access,                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			  __VA_ARGS__ } })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...)                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	(&(const struct uverbs_attr_def){                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		.id = _attr_id,                                                \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN,                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			  _type,                                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			  __VA_ARGS__ } })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...)                              \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	(&(const struct uverbs_attr_def){                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		.id = _attr_id,                                                \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT,                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			  _type,                                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			  __VA_ARGS__ } })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...)                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	(&(const struct uverbs_attr_def){                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		.id = _attr_id,                                                \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN,                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			  .u2.enum_def.ids = _enum_arr,                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			  .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr),       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			  __VA_ARGS__ },                                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /* An input value that is a member in the enum _enum_type. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...)                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	UVERBS_ATTR_PTR_IN(                                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		_attr_id,                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		UVERBS_ATTR_SIZE(                                              \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)),  \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			sizeof(u64)),                                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)  * An input value that is a bitwise combination of values of _enum_type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  * This permits the flag value to be passed as either a u32 or u64, it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * be retrieved via uverbs_get_flag().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...)                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	UVERBS_ATTR_PTR_IN(                                                    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		_attr_id,                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO(              \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 						       !sizeof(_enum_type *)), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 				 sizeof(u64)),                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)  * This spec is used in order to pass information to the hardware driver in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)  * legacy way. Every verb that could get driver specific data should get this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)  * spec.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) #define UVERBS_ATTR_UHW()                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN,                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			   UVERBS_ATTR_MIN_SIZE(0),			       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			   UA_OPTIONAL,                                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			   .is_udata = 1),				       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT,                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 			    UVERBS_ATTR_MIN_SIZE(0),			       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			    UA_OPTIONAL,                                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			    .is_udata = 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /* =================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  *              Parsing infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  * =================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct uverbs_ptr_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	 * used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		u64 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	u16		len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	u16		uattr_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	u8		enum_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct uverbs_obj_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	struct ib_uobject		*uobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	const struct uverbs_api_attr	*attr_elm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct uverbs_objs_arr_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	struct ib_uobject **uobjects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	u16 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct uverbs_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		struct uverbs_ptr_attr	ptr_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		struct uverbs_obj_attr	obj_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		struct uverbs_objs_arr_attr objs_arr_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct uverbs_attr_bundle {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	struct ib_udata driver_udata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	struct ib_udata ucore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	struct ib_uverbs_file *ufile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	struct ib_ucontext *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	struct ib_uobject *uobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct uverbs_attr attrs[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 					unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			attrs_bundle->attr_present);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)  * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)  *                             ib_udata which is embedded in uverbs_attr_bundle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)  * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)  * in a NULL ucontext pointer, as a safety precaution. Callers should be using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)  * 'udata' to determine if the driver call is in user or kernel mode, not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)  * 'ucontext'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) #define rdma_udata_to_drv_context(udata, drv_dev_struct, member)               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	(udata ? container_of(container_of(udata, struct uverbs_attr_bundle,   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 					   driver_udata)                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 				      ->context,                               \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			      drv_dev_struct, member) :                        \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		 (drv_dev_struct *)NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) #define IS_UVERBS_COPY_ERR(_ret)		((_ret) && (_ret) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 							u16 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	if (!uverbs_attr_is_valid(attrs_bundle, idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 					  u16 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		return PTR_ERR(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	return attr->ptr_attr.enum_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 					u16 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	const struct uverbs_attr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		return ERR_CAST(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	return attr->obj_attr.uobject->object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 							 u16 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		return ERR_CAST(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	return attr->obj_attr.uobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		return PTR_ERR(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	return attr->ptr_attr.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 				 u16 idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)  * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)  * attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)  * @attrs: The attribute bundle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)  * @idx: The ID of the attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)  * @elem_size: The size of the element in the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 			       size_t elem_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	int size = uverbs_attr_get_len(attrs, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	if (size % elem_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	return size / elem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)  * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)  * UVERBS_ATTR_TYPE_IDRS_ARRAY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)  * @arr: Returned pointer to array of pointers for uobjects or NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)  *       the attribute isn't provided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)  * Return: The array length or 0 if no attribute was provided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static inline int uverbs_attr_get_uobjs_arr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	struct ib_uobject ***arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	const struct uverbs_attr *attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 			uverbs_attr_get(attrs_bundle, attr_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	if (IS_ERR(attr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		*arr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	*arr = attr->objs_arr_attr.uobjects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	return attr->objs_arr_attr.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) static inline void *uverbs_attr_get_alloced_ptr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 		return (void *)attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 						 attr->ptr_attr.ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) static inline int _uverbs_copy_from(void *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 				    const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 				    size_t idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 				    size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 		return PTR_ERR(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	 * Validation ensures attr->ptr_attr.len >= size. If the caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	 * uverbs_copy_from_or_zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	if (unlikely(size < attr->ptr_attr.len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	if (uverbs_attr_ptr_is_inline(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 		memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 				attr->ptr_attr.len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static inline int _uverbs_copy_from_or_zero(void *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 					    const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 					    size_t idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 					    size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	size_t min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	if (IS_ERR(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 		return PTR_ERR(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	min_size = min_t(size_t, size, attr->ptr_attr.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	if (uverbs_attr_ptr_is_inline(attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 		memcpy(to, &attr->ptr_attr.data, min_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 				min_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	if (size > min_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 		memset(to + min_size, 0, size - min_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) #define uverbs_copy_from(to, attrs_bundle, idx)				      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) #define uverbs_copy_from_or_zero(to, attrs_bundle, idx)			      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) static inline struct ib_ucontext *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	return ib_uverbs_get_ucontext_file(attrs->ufile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 		       size_t idx, u64 allowed_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		       size_t idx, u64 allowed_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		   const void *from, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 			     gfp_t flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 					  size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	return _uverbs_alloc(bundle, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 					   size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 					    size_t n, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	size_t bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	if (unlikely(check_mul_overflow(n, size, &bytes)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 		return ERR_PTR(-EOVERFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	return uverbs_zalloc(bundle, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 		      size_t idx, s64 lower_bound, u64 upper_bound,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		      s64 *def_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 				  size_t idx, const void *from, size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 		   size_t idx, u64 allowed_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 		   size_t idx, u64 allowed_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 				 size_t idx, const void *from, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 					  size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 					   size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 		  size_t idx, s64 lower_bound, u64 upper_bound,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 		  s64 *def_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 			      size_t idx, const void *from, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) #define uverbs_get_const(_to, _attrs_bundle, _idx)                             \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	({                                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 		s64 _val;                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 		int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx,       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 					     type_min(typeof(*_to)),           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 					     type_max(typeof(*_to)), NULL);    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 		(*_to) = _val;                                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 		_ret;                                                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default)           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	({                                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 		s64 _val;                                                      \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 		s64 _def_val = _default;                                       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 		int _ret =                                                     \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 			_uverbs_get_const(&_val, _attrs_bundle, _idx,          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 					  type_min(typeof(*_to)),              \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 					  type_max(typeof(*_to)), &_def_val);  \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 		(*_to) = _val;                                                 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 		_ret;                                                          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) #endif