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 | * Legacy command definition, don't use in new application, and don't |
13 | * add new such definition any more |
14 | */ |
15 | #define UBLK_CMD_GET_QUEUE_AFFINITY 0x01 |
16 | #define UBLK_CMD_GET_DEV_INFO 0x02 |
17 | #define UBLK_CMD_ADD_DEV 0x04 |
18 | #define UBLK_CMD_DEL_DEV 0x05 |
19 | #define UBLK_CMD_START_DEV 0x06 |
20 | #define UBLK_CMD_STOP_DEV 0x07 |
21 | #define UBLK_CMD_SET_PARAMS 0x08 |
22 | #define UBLK_CMD_GET_PARAMS 0x09 |
23 | #define UBLK_CMD_START_USER_RECOVERY 0x10 |
24 | #define UBLK_CMD_END_USER_RECOVERY 0x11 |
25 | #define UBLK_CMD_GET_DEV_INFO2 0x12 |
26 | |
27 | /* Any new ctrl command should encode by __IO*() */ |
28 | #define UBLK_U_CMD_GET_QUEUE_AFFINITY \ |
29 | _IOR('u', UBLK_CMD_GET_QUEUE_AFFINITY, struct ublksrv_ctrl_cmd) |
30 | #define UBLK_U_CMD_GET_DEV_INFO \ |
31 | _IOR('u', UBLK_CMD_GET_DEV_INFO, struct ublksrv_ctrl_cmd) |
32 | #define UBLK_U_CMD_ADD_DEV \ |
33 | _IOWR('u', UBLK_CMD_ADD_DEV, struct ublksrv_ctrl_cmd) |
34 | #define UBLK_U_CMD_DEL_DEV \ |
35 | _IOWR('u', UBLK_CMD_DEL_DEV, struct ublksrv_ctrl_cmd) |
36 | #define UBLK_U_CMD_START_DEV \ |
37 | _IOWR('u', UBLK_CMD_START_DEV, struct ublksrv_ctrl_cmd) |
38 | #define UBLK_U_CMD_STOP_DEV \ |
39 | _IOWR('u', UBLK_CMD_STOP_DEV, struct ublksrv_ctrl_cmd) |
40 | #define UBLK_U_CMD_SET_PARAMS \ |
41 | _IOWR('u', UBLK_CMD_SET_PARAMS, struct ublksrv_ctrl_cmd) |
42 | #define UBLK_U_CMD_GET_PARAMS \ |
43 | _IOR('u', UBLK_CMD_GET_PARAMS, struct ublksrv_ctrl_cmd) |
44 | #define UBLK_U_CMD_START_USER_RECOVERY \ |
45 | _IOWR('u', UBLK_CMD_START_USER_RECOVERY, struct ublksrv_ctrl_cmd) |
46 | #define UBLK_U_CMD_END_USER_RECOVERY \ |
47 | _IOWR('u', UBLK_CMD_END_USER_RECOVERY, struct ublksrv_ctrl_cmd) |
48 | #define UBLK_U_CMD_GET_DEV_INFO2 \ |
49 | _IOR('u', UBLK_CMD_GET_DEV_INFO2, struct ublksrv_ctrl_cmd) |
50 | #define UBLK_U_CMD_GET_FEATURES \ |
51 | _IOR('u', 0x13, struct ublksrv_ctrl_cmd) |
52 | |
53 | /* |
54 | * 64bits are enough now, and it should be easy to extend in case of |
55 | * running out of feature flags |
56 | */ |
57 | #define UBLK_FEATURES_LEN 8 |
58 | |
59 | /* |
60 | * IO commands, issued by ublk server, and handled by ublk driver. |
61 | * |
62 | * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request |
63 | * from ublk driver, should be issued only when starting device. After |
64 | * the associated cqe is returned, request's tag can be retrieved via |
65 | * cqe->userdata. |
66 | * |
67 | * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled |
68 | * this IO request, request's handling result is committed to ublk |
69 | * driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be |
70 | * handled before completing io request. |
71 | * |
72 | * NEED_GET_DATA: only used for write requests to set io addr and copy data |
73 | * When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA |
74 | * command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA. |
75 | * |
76 | * It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag |
77 | * while starting a ublk device. |
78 | */ |
79 | |
80 | /* |
81 | * Legacy IO command definition, don't use in new application, and don't |
82 | * add new such definition any more |
83 | */ |
84 | #define UBLK_IO_FETCH_REQ 0x20 |
85 | #define UBLK_IO_COMMIT_AND_FETCH_REQ 0x21 |
86 | #define UBLK_IO_NEED_GET_DATA 0x22 |
87 | |
88 | /* Any new IO command should encode by __IOWR() */ |
89 | #define UBLK_U_IO_FETCH_REQ \ |
90 | _IOWR('u', UBLK_IO_FETCH_REQ, struct ublksrv_io_cmd) |
91 | #define UBLK_U_IO_COMMIT_AND_FETCH_REQ \ |
92 | _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd) |
93 | #define UBLK_U_IO_NEED_GET_DATA \ |
94 | _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd) |
95 | |
96 | /* only ABORT means that no re-fetch */ |
97 | #define UBLK_IO_RES_OK 0 |
98 | #define UBLK_IO_RES_NEED_GET_DATA 1 |
99 | #define UBLK_IO_RES_ABORT (-ENODEV) |
100 | |
101 | #define UBLKSRV_CMD_BUF_OFFSET 0 |
102 | #define UBLKSRV_IO_BUF_OFFSET 0x80000000 |
103 | |
104 | /* tag bit is 16bit, so far limit at most 4096 IOs for each queue */ |
105 | #define UBLK_MAX_QUEUE_DEPTH 4096 |
106 | |
107 | /* single IO buffer max size is 32MB */ |
108 | #define UBLK_IO_BUF_OFF 0 |
109 | #define UBLK_IO_BUF_BITS 25 |
110 | #define UBLK_IO_BUF_BITS_MASK ((1ULL << UBLK_IO_BUF_BITS) - 1) |
111 | |
112 | /* so at most 64K IOs for each queue */ |
113 | #define UBLK_TAG_OFF UBLK_IO_BUF_BITS |
114 | #define UBLK_TAG_BITS 16 |
115 | #define UBLK_TAG_BITS_MASK ((1ULL << UBLK_TAG_BITS) - 1) |
116 | |
117 | /* max 4096 queues */ |
118 | #define UBLK_QID_OFF (UBLK_TAG_OFF + UBLK_TAG_BITS) |
119 | #define UBLK_QID_BITS 12 |
120 | #define UBLK_QID_BITS_MASK ((1ULL << UBLK_QID_BITS) - 1) |
121 | |
122 | #define UBLK_MAX_NR_QUEUES (1U << UBLK_QID_BITS) |
123 | |
124 | #define UBLKSRV_IO_BUF_TOTAL_BITS (UBLK_QID_OFF + UBLK_QID_BITS) |
125 | #define UBLKSRV_IO_BUF_TOTAL_SIZE (1ULL << UBLKSRV_IO_BUF_TOTAL_BITS) |
126 | |
127 | /* |
128 | * zero copy requires 4k block size, and can remap ublk driver's io |
129 | * request into ublksrv's vm space |
130 | */ |
131 | #define UBLK_F_SUPPORT_ZERO_COPY (1ULL << 0) |
132 | |
133 | /* |
134 | * Force to complete io cmd via io_uring_cmd_complete_in_task so that |
135 | * performance comparison is done easily with using task_work_add |
136 | */ |
137 | #define UBLK_F_URING_CMD_COMP_IN_TASK (1ULL << 1) |
138 | |
139 | /* |
140 | * User should issue io cmd again for write requests to |
141 | * set io buffer address and copy data from bio vectors |
142 | * to the userspace io buffer. |
143 | * |
144 | * In this mode, task_work is not used. |
145 | */ |
146 | #define UBLK_F_NEED_GET_DATA (1UL << 2) |
147 | |
148 | #define UBLK_F_USER_RECOVERY (1UL << 3) |
149 | |
150 | #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4) |
151 | |
152 | /* |
153 | * Unprivileged user can create /dev/ublkcN and /dev/ublkbN. |
154 | * |
155 | * /dev/ublk-control needs to be available for unprivileged user, and it |
156 | * can be done via udev rule to make all control commands available to |
157 | * unprivileged user. Except for the command of UBLK_CMD_ADD_DEV, all |
158 | * other commands are only allowed for the owner of the specified device. |
159 | * |
160 | * When userspace sends UBLK_CMD_ADD_DEV, the device pair's owner_uid and |
161 | * owner_gid are stored to ublksrv_ctrl_dev_info by kernel, so far only |
162 | * the current user's uid/gid is stored, that said owner of the created |
163 | * device is always the current user. |
164 | * |
165 | * We still need udev rule to apply OWNER/GROUP with the stored owner_uid |
166 | * and owner_gid. |
167 | * |
168 | * Then ublk server can be run as unprivileged user, and /dev/ublkbN can |
169 | * be accessed and managed by its owner represented by owner_uid/owner_gid. |
170 | */ |
171 | #define UBLK_F_UNPRIVILEGED_DEV (1UL << 5) |
172 | |
173 | /* use ioctl encoding for uring command */ |
174 | #define UBLK_F_CMD_IOCTL_ENCODE (1UL << 6) |
175 | |
176 | /* Copy between request and user buffer by pread()/pwrite() */ |
177 | #define UBLK_F_USER_COPY (1UL << 7) |
178 | |
179 | /* |
180 | * User space sets this flag when setting up the device to request zoned storage support. Kernel may |
181 | * deny the request by returning an error. |
182 | */ |
183 | #define UBLK_F_ZONED (1ULL << 8) |
184 | |
185 | /* device state */ |
186 | #define UBLK_S_DEV_DEAD 0 |
187 | #define UBLK_S_DEV_LIVE 1 |
188 | #define UBLK_S_DEV_QUIESCED 2 |
189 | |
190 | /* shipped via sqe->cmd of io_uring command */ |
191 | struct ublksrv_ctrl_cmd { |
192 | /* sent to which device, must be valid */ |
193 | __u32 dev_id; |
194 | |
195 | /* sent to which queue, must be -1 if the cmd isn't for queue */ |
196 | __u16 queue_id; |
197 | /* |
198 | * cmd specific buffer, can be IN or OUT. |
199 | */ |
200 | __u16 len; |
201 | __u64 addr; |
202 | |
203 | /* inline data */ |
204 | __u64 data[1]; |
205 | |
206 | /* |
207 | * Used for UBLK_F_UNPRIVILEGED_DEV and UBLK_CMD_GET_DEV_INFO2 |
208 | * only, include null char |
209 | */ |
210 | __u16 dev_path_len; |
211 | __u16 pad; |
212 | __u32 reserved; |
213 | }; |
214 | |
215 | struct ublksrv_ctrl_dev_info { |
216 | __u16 nr_hw_queues; |
217 | __u16 queue_depth; |
218 | __u16 state; |
219 | __u16 pad0; |
220 | |
221 | __u32 max_io_buf_bytes; |
222 | __u32 dev_id; |
223 | |
224 | __s32 ublksrv_pid; |
225 | __u32 pad1; |
226 | |
227 | __u64 flags; |
228 | |
229 | /* For ublksrv internal use, invisible to ublk driver */ |
230 | __u64 ublksrv_flags; |
231 | |
232 | __u32 owner_uid; /* store by kernel */ |
233 | __u32 owner_gid; /* store by kernel */ |
234 | __u64 reserved1; |
235 | __u64 reserved2; |
236 | }; |
237 | |
238 | #define UBLK_IO_OP_READ 0 |
239 | #define UBLK_IO_OP_WRITE 1 |
240 | #define UBLK_IO_OP_FLUSH 2 |
241 | #define UBLK_IO_OP_DISCARD 3 |
242 | #define UBLK_IO_OP_WRITE_SAME 4 |
243 | #define UBLK_IO_OP_WRITE_ZEROES 5 |
244 | #define UBLK_IO_OP_ZONE_OPEN 10 |
245 | #define UBLK_IO_OP_ZONE_CLOSE 11 |
246 | #define UBLK_IO_OP_ZONE_FINISH 12 |
247 | #define UBLK_IO_OP_ZONE_APPEND 13 |
248 | #define UBLK_IO_OP_ZONE_RESET_ALL 14 |
249 | #define UBLK_IO_OP_ZONE_RESET 15 |
250 | /* |
251 | * Construct a zone report. The report request is carried in `struct |
252 | * ublksrv_io_desc`. The `start_sector` field must be the first sector of a zone |
253 | * and shall indicate the first zone of the report. The `nr_zones` shall |
254 | * indicate how many zones should be reported at most. The report shall be |
255 | * delivered as a `struct blk_zone` array. To report fewer zones than requested, |
256 | * zero the last entry of the returned array. |
257 | * |
258 | * Related definitions(blk_zone, blk_zone_cond, blk_zone_type, ...) in |
259 | * include/uapi/linux/blkzoned.h are part of ublk UAPI. |
260 | */ |
261 | #define UBLK_IO_OP_REPORT_ZONES 18 |
262 | |
263 | #define UBLK_IO_F_FAILFAST_DEV (1U << 8) |
264 | #define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9) |
265 | #define UBLK_IO_F_FAILFAST_DRIVER (1U << 10) |
266 | #define UBLK_IO_F_META (1U << 11) |
267 | #define UBLK_IO_F_FUA (1U << 13) |
268 | #define UBLK_IO_F_NOUNMAP (1U << 15) |
269 | #define UBLK_IO_F_SWAP (1U << 16) |
270 | |
271 | /* |
272 | * io cmd is described by this structure, and stored in share memory, indexed |
273 | * by request tag. |
274 | * |
275 | * The data is stored by ublk driver, and read by ublksrv after one fetch command |
276 | * returns. |
277 | */ |
278 | struct ublksrv_io_desc { |
279 | /* op: bit 0-7, flags: bit 8-31 */ |
280 | __u32 op_flags; |
281 | |
282 | union { |
283 | __u32 nr_sectors; |
284 | __u32 nr_zones; /* for UBLK_IO_OP_REPORT_ZONES */ |
285 | }; |
286 | |
287 | /* start sector for this io */ |
288 | __u64 start_sector; |
289 | |
290 | /* buffer address in ublksrv daemon vm space, from ublk driver */ |
291 | __u64 addr; |
292 | }; |
293 | |
294 | static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod) |
295 | { |
296 | return iod->op_flags & 0xff; |
297 | } |
298 | |
299 | static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod) |
300 | { |
301 | return iod->op_flags >> 8; |
302 | } |
303 | |
304 | /* issued to ublk driver via /dev/ublkcN */ |
305 | struct ublksrv_io_cmd { |
306 | __u16 q_id; |
307 | |
308 | /* for fetch/commit which result */ |
309 | __u16 tag; |
310 | |
311 | /* io result, it is valid for COMMIT* command only */ |
312 | __s32 result; |
313 | |
314 | union { |
315 | /* |
316 | * userspace buffer address in ublksrv daemon process, valid for |
317 | * FETCH* command only |
318 | * |
319 | * `addr` should not be used when UBLK_F_USER_COPY is enabled, |
320 | * because userspace handles data copy by pread()/pwrite() over |
321 | * /dev/ublkcN. But in case of UBLK_F_ZONED, this union is |
322 | * re-used to pass back the allocated LBA for |
323 | * UBLK_IO_OP_ZONE_APPEND which actually depends on |
324 | * UBLK_F_USER_COPY |
325 | */ |
326 | __u64 addr; |
327 | __u64 zone_append_lba; |
328 | }; |
329 | }; |
330 | |
331 | struct ublk_param_basic { |
332 | #define UBLK_ATTR_READ_ONLY (1 << 0) |
333 | #define UBLK_ATTR_ROTATIONAL (1 << 1) |
334 | #define UBLK_ATTR_VOLATILE_CACHE (1 << 2) |
335 | #define UBLK_ATTR_FUA (1 << 3) |
336 | __u32 attrs; |
337 | __u8 logical_bs_shift; |
338 | __u8 physical_bs_shift; |
339 | __u8 io_opt_shift; |
340 | __u8 io_min_shift; |
341 | |
342 | __u32 max_sectors; |
343 | __u32 chunk_sectors; |
344 | |
345 | __u64 dev_sectors; |
346 | __u64 virt_boundary_mask; |
347 | }; |
348 | |
349 | struct ublk_param_discard { |
350 | __u32 discard_alignment; |
351 | |
352 | __u32 discard_granularity; |
353 | __u32 max_discard_sectors; |
354 | |
355 | __u32 max_write_zeroes_sectors; |
356 | __u16 max_discard_segments; |
357 | __u16 reserved0; |
358 | }; |
359 | |
360 | /* |
361 | * read-only, can't set via UBLK_CMD_SET_PARAMS, disk_devt is available |
362 | * after device is started |
363 | */ |
364 | struct ublk_param_devt { |
365 | __u32 char_major; |
366 | __u32 char_minor; |
367 | __u32 disk_major; |
368 | __u32 disk_minor; |
369 | }; |
370 | |
371 | struct ublk_param_zoned { |
372 | __u32 max_open_zones; |
373 | __u32 max_active_zones; |
374 | __u32 max_zone_append_sectors; |
375 | __u8 reserved[20]; |
376 | }; |
377 | |
378 | struct ublk_params { |
379 | /* |
380 | * Total length of parameters, userspace has to set 'len' for both |
381 | * SET_PARAMS and GET_PARAMS command, and driver may update len |
382 | * if two sides use different version of 'ublk_params', same with |
383 | * 'types' fields. |
384 | */ |
385 | __u32 len; |
386 | #define UBLK_PARAM_TYPE_BASIC (1 << 0) |
387 | #define UBLK_PARAM_TYPE_DISCARD (1 << 1) |
388 | #define UBLK_PARAM_TYPE_DEVT (1 << 2) |
389 | #define UBLK_PARAM_TYPE_ZONED (1 << 3) |
390 | __u32 types; /* types of parameter included */ |
391 | |
392 | struct ublk_param_basic basic; |
393 | struct ublk_param_discard discard; |
394 | struct ublk_param_devt devt; |
395 | struct ublk_param_zoned zoned; |
396 | }; |
397 | |
398 | #endif |
399 | |