xref: /openbmc/linux/include/uapi/linux/ublk_cmd.h (revision 54a611b6)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef USER_BLK_DRV_CMD_INC_H
3 #define USER_BLK_DRV_CMD_INC_H
4 
5 #include <linux/types.h>
6 
7 /* ublk server command definition */
8 
9 /*
10  * Admin commands, issued by ublk server, and handled by ublk driver.
11  */
12 #define	UBLK_CMD_GET_QUEUE_AFFINITY	0x01
13 #define	UBLK_CMD_GET_DEV_INFO	0x02
14 #define	UBLK_CMD_ADD_DEV		0x04
15 #define	UBLK_CMD_DEL_DEV		0x05
16 #define	UBLK_CMD_START_DEV	0x06
17 #define	UBLK_CMD_STOP_DEV	0x07
18 #define	UBLK_CMD_SET_PARAMS	0x08
19 #define	UBLK_CMD_GET_PARAMS	0x09
20 
21 /*
22  * IO commands, issued by ublk server, and handled by ublk driver.
23  *
24  * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request
25  *      from ublk driver, should be issued only when starting device. After
26  *      the associated cqe is returned, request's tag can be retrieved via
27  *      cqe->userdata.
28  *
29  * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled
30  *      this IO request, request's handling result is committed to ublk
31  *      driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be
32  *      handled before completing io request.
33  *
34  * NEED_GET_DATA: only used for write requests to set io addr and copy data
35  *      When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA
36  *      command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA.
37  *
38  *      It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag
39  *      while starting a ublk device.
40  */
41 #define	UBLK_IO_FETCH_REQ		0x20
42 #define	UBLK_IO_COMMIT_AND_FETCH_REQ	0x21
43 #define	UBLK_IO_NEED_GET_DATA	0x22
44 
45 /* only ABORT means that no re-fetch */
46 #define UBLK_IO_RES_OK			0
47 #define UBLK_IO_RES_NEED_GET_DATA	1
48 #define UBLK_IO_RES_ABORT		(-ENODEV)
49 
50 #define UBLKSRV_CMD_BUF_OFFSET	0
51 #define UBLKSRV_IO_BUF_OFFSET	0x80000000
52 
53 /* tag bit is 12bit, so at most 4096 IOs for each queue */
54 #define UBLK_MAX_QUEUE_DEPTH	4096
55 
56 /*
57  * zero copy requires 4k block size, and can remap ublk driver's io
58  * request into ublksrv's vm space
59  */
60 #define UBLK_F_SUPPORT_ZERO_COPY	(1ULL << 0)
61 
62 /*
63  * Force to complete io cmd via io_uring_cmd_complete_in_task so that
64  * performance comparison is done easily with using task_work_add
65  */
66 #define UBLK_F_URING_CMD_COMP_IN_TASK	(1ULL << 1)
67 
68 /*
69  * User should issue io cmd again for write requests to
70  * set io buffer address and copy data from bio vectors
71  * to the userspace io buffer.
72  *
73  * In this mode, task_work is not used.
74  */
75 #define UBLK_F_NEED_GET_DATA (1UL << 2)
76 
77 /* device state */
78 #define UBLK_S_DEV_DEAD	0
79 #define UBLK_S_DEV_LIVE	1
80 
81 /* shipped via sqe->cmd of io_uring command */
82 struct ublksrv_ctrl_cmd {
83 	/* sent to which device, must be valid */
84 	__u32	dev_id;
85 
86 	/* sent to which queue, must be -1 if the cmd isn't for queue */
87 	__u16	queue_id;
88 	/*
89 	 * cmd specific buffer, can be IN or OUT.
90 	 */
91 	__u16	len;
92 	__u64	addr;
93 
94 	/* inline data */
95 	__u64	data[2];
96 };
97 
98 struct ublksrv_ctrl_dev_info {
99 	__u16	nr_hw_queues;
100 	__u16	queue_depth;
101 	__u16	state;
102 	__u16	pad0;
103 
104 	__u32	max_io_buf_bytes;
105 	__u32	dev_id;
106 
107 	__s32	ublksrv_pid;
108 	__u32	pad1;
109 
110 	__u64	flags;
111 
112 	/* For ublksrv internal use, invisible to ublk driver */
113 	__u64	ublksrv_flags;
114 
115 	__u64	reserved0;
116 	__u64	reserved1;
117 	__u64   reserved2;
118 };
119 
120 #define		UBLK_IO_OP_READ		0
121 #define		UBLK_IO_OP_WRITE		1
122 #define		UBLK_IO_OP_FLUSH		2
123 #define		UBLK_IO_OP_DISCARD	3
124 #define		UBLK_IO_OP_WRITE_SAME	4
125 #define		UBLK_IO_OP_WRITE_ZEROES	5
126 
127 #define		UBLK_IO_F_FAILFAST_DEV		(1U << 8)
128 #define		UBLK_IO_F_FAILFAST_TRANSPORT	(1U << 9)
129 #define		UBLK_IO_F_FAILFAST_DRIVER	(1U << 10)
130 #define		UBLK_IO_F_META			(1U << 11)
131 #define		UBLK_IO_F_FUA			(1U << 13)
132 #define		UBLK_IO_F_NOUNMAP		(1U << 15)
133 #define		UBLK_IO_F_SWAP			(1U << 16)
134 
135 /*
136  * io cmd is described by this structure, and stored in share memory, indexed
137  * by request tag.
138  *
139  * The data is stored by ublk driver, and read by ublksrv after one fetch command
140  * returns.
141  */
142 struct ublksrv_io_desc {
143 	/* op: bit 0-7, flags: bit 8-31 */
144 	__u32		op_flags;
145 
146 	__u32		nr_sectors;
147 
148 	/* start sector for this io */
149 	__u64		start_sector;
150 
151 	/* buffer address in ublksrv daemon vm space, from ublk driver */
152 	__u64		addr;
153 };
154 
155 static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod)
156 {
157 	return iod->op_flags & 0xff;
158 }
159 
160 static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod)
161 {
162 	return iod->op_flags >> 8;
163 }
164 
165 /* issued to ublk driver via /dev/ublkcN */
166 struct ublksrv_io_cmd {
167 	__u16	q_id;
168 
169 	/* for fetch/commit which result */
170 	__u16	tag;
171 
172 	/* io result, it is valid for COMMIT* command only */
173 	__s32	result;
174 
175 	/*
176 	 * userspace buffer address in ublksrv daemon process, valid for
177 	 * FETCH* command only
178 	 */
179 	__u64	addr;
180 };
181 
182 struct ublk_param_basic {
183 #define UBLK_ATTR_READ_ONLY            (1 << 0)
184 #define UBLK_ATTR_ROTATIONAL           (1 << 1)
185 #define UBLK_ATTR_VOLATILE_CACHE       (1 << 2)
186 #define UBLK_ATTR_FUA                  (1 << 3)
187 	__u32	attrs;
188 	__u8	logical_bs_shift;
189 	__u8	physical_bs_shift;
190 	__u8	io_opt_shift;
191 	__u8	io_min_shift;
192 
193 	__u32	max_sectors;
194 	__u32	chunk_sectors;
195 
196 	__u64   dev_sectors;
197 	__u64   virt_boundary_mask;
198 };
199 
200 struct ublk_param_discard {
201 	__u32	discard_alignment;
202 
203 	__u32	discard_granularity;
204 	__u32	max_discard_sectors;
205 
206 	__u32	max_write_zeroes_sectors;
207 	__u16	max_discard_segments;
208 	__u16	reserved0;
209 };
210 
211 struct ublk_params {
212 	/*
213 	 * Total length of parameters, userspace has to set 'len' for both
214 	 * SET_PARAMS and GET_PARAMS command, and driver may update len
215 	 * if two sides use different version of 'ublk_params', same with
216 	 * 'types' fields.
217 	 */
218 	__u32	len;
219 #define UBLK_PARAM_TYPE_BASIC           (1 << 0)
220 #define UBLK_PARAM_TYPE_DISCARD         (1 << 1)
221 	__u32	types;			/* types of parameter included */
222 
223 	struct ublk_param_basic		basic;
224 	struct ublk_param_discard	discard;
225 };
226 
227 #endif
228