xref: /linux-6.15/include/uapi/linux/ublk_cmd.h (revision 0aa73170)
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 #define	UBLK_IO_FETCH_REQ		0x20
35 #define	UBLK_IO_COMMIT_AND_FETCH_REQ	0x21
36 
37 /* only ABORT means that no re-fetch */
38 #define UBLK_IO_RES_OK			0
39 #define UBLK_IO_RES_ABORT		(-ENODEV)
40 
41 #define UBLKSRV_CMD_BUF_OFFSET	0
42 #define UBLKSRV_IO_BUF_OFFSET	0x80000000
43 
44 /* tag bit is 12bit, so at most 4096 IOs for each queue */
45 #define UBLK_MAX_QUEUE_DEPTH	4096
46 
47 /*
48  * zero copy requires 4k block size, and can remap ublk driver's io
49  * request into ublksrv's vm space
50  */
51 #define UBLK_F_SUPPORT_ZERO_COPY	(1ULL << 0)
52 
53 /*
54  * Force to complete io cmd via io_uring_cmd_complete_in_task so that
55  * performance comparison is done easily with using task_work_add
56  */
57 #define UBLK_F_URING_CMD_COMP_IN_TASK	(1ULL << 1)
58 
59 /* device state */
60 #define UBLK_S_DEV_DEAD	0
61 #define UBLK_S_DEV_LIVE	1
62 
63 /* shipped via sqe->cmd of io_uring command */
64 struct ublksrv_ctrl_cmd {
65 	/* sent to which device, must be valid */
66 	__u32	dev_id;
67 
68 	/* sent to which queue, must be -1 if the cmd isn't for queue */
69 	__u16	queue_id;
70 	/*
71 	 * cmd specific buffer, can be IN or OUT.
72 	 */
73 	__u16	len;
74 	__u64	addr;
75 
76 	/* inline data */
77 	__u64	data[2];
78 };
79 
80 struct ublksrv_ctrl_dev_info {
81 	__u16	nr_hw_queues;
82 	__u16	queue_depth;
83 	__u16	block_size;
84 	__u16	state;
85 
86 	__u32	rq_max_blocks;
87 	__u32	dev_id;
88 
89 	__u64   dev_blocks;
90 
91 	__s32	ublksrv_pid;
92 	__s32	reserved0;
93 	__u64	flags;
94 	__u64	flags_reserved;
95 
96 	/* For ublksrv internal use, invisible to ublk driver */
97 	__u64	ublksrv_flags;
98 	__u64	reserved1[9];
99 };
100 
101 #define		UBLK_IO_OP_READ		0
102 #define		UBLK_IO_OP_WRITE		1
103 #define		UBLK_IO_OP_FLUSH		2
104 #define		UBLK_IO_OP_DISCARD	3
105 #define		UBLK_IO_OP_WRITE_SAME	4
106 #define		UBLK_IO_OP_WRITE_ZEROES	5
107 
108 #define		UBLK_IO_F_FAILFAST_DEV		(1U << 8)
109 #define		UBLK_IO_F_FAILFAST_TRANSPORT	(1U << 9)
110 #define		UBLK_IO_F_FAILFAST_DRIVER	(1U << 10)
111 #define		UBLK_IO_F_META			(1U << 11)
112 #define		UBLK_IO_F_FUA			(1U << 13)
113 #define		UBLK_IO_F_NOUNMAP		(1U << 15)
114 #define		UBLK_IO_F_SWAP			(1U << 16)
115 
116 /*
117  * io cmd is described by this structure, and stored in share memory, indexed
118  * by request tag.
119  *
120  * The data is stored by ublk driver, and read by ublksrv after one fetch command
121  * returns.
122  */
123 struct ublksrv_io_desc {
124 	/* op: bit 0-7, flags: bit 8-31 */
125 	__u32		op_flags;
126 
127 	__u32		nr_sectors;
128 
129 	/* start sector for this io */
130 	__u64		start_sector;
131 
132 	/* buffer address in ublksrv daemon vm space, from ublk driver */
133 	__u64		addr;
134 };
135 
136 static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod)
137 {
138 	return iod->op_flags & 0xff;
139 }
140 
141 static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod)
142 {
143 	return iod->op_flags >> 8;
144 }
145 
146 /* issued to ublk driver via /dev/ublkcN */
147 struct ublksrv_io_cmd {
148 	__u16	q_id;
149 
150 	/* for fetch/commit which result */
151 	__u16	tag;
152 
153 	/* io result, it is valid for COMMIT* command only */
154 	__s32	result;
155 
156 	/*
157 	 * userspace buffer address in ublksrv daemon process, valid for
158 	 * FETCH* command only
159 	 */
160 	__u64	addr;
161 };
162 
163 struct ublk_param_basic {
164 #define UBLK_ATTR_READ_ONLY            (1 << 0)
165 #define UBLK_ATTR_ROTATIONAL           (1 << 1)
166 #define UBLK_ATTR_VOLATILE_CACHE       (1 << 2)
167 #define UBLK_ATTR_FUA                  (1 << 3)
168 	__u32	attrs;
169 	__u8	logical_bs_shift;
170 	__u8	physical_bs_shift;
171 	__u8	io_opt_shift;
172 	__u8	io_min_shift;
173 
174 	__u32	max_sectors;
175 	__u32	chunk_sectors;
176 
177 	__u64   dev_sectors;
178 	__u64   virt_boundary_mask;
179 };
180 
181 struct ublk_param_discard {
182 	__u32	discard_alignment;
183 
184 	__u32	discard_granularity;
185 	__u32	max_discard_sectors;
186 
187 	__u32	max_write_zeroes_sectors;
188 	__u16	max_discard_segments;
189 	__u16	reserved0;
190 };
191 
192 struct ublk_params {
193 	/*
194 	 * Total length of parameters, userspace has to set 'len' for both
195 	 * SET_PARAMS and GET_PARAMS command, and driver may update len
196 	 * if two sides use different version of 'ublk_params', same with
197 	 * 'types' fields.
198 	 */
199 	__u32	len;
200 #define UBLK_PARAM_TYPE_BASIC           (1 << 0)
201 #define UBLK_PARAM_TYPE_DISCARD         (1 << 1)
202 	__u32	types;			/* types of parameter included */
203 
204 	struct ublk_param_basic		basic;
205 	struct ublk_param_discard	discard;
206 };
207 
208 #endif
209