| 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 | #define UBLK_U_CMD_DEL_DEV_ASYNC \ |
| 53 | _IOR('u', 0x14, struct ublksrv_ctrl_cmd) |
| 54 | #define UBLK_U_CMD_UPDATE_SIZE \ |
| 55 | _IOWR('u', 0x15, struct ublksrv_ctrl_cmd) |
| 56 | #define UBLK_U_CMD_QUIESCE_DEV \ |
| 57 | _IOWR('u', 0x16, struct ublksrv_ctrl_cmd) |
| 58 | |
| 59 | /* |
| 60 | * 64bits are enough now, and it should be easy to extend in case of |
| 61 | * running out of feature flags |
| 62 | */ |
| 63 | #define UBLK_FEATURES_LEN 8 |
| 64 | |
| 65 | /* |
| 66 | * IO commands, issued by ublk server, and handled by ublk driver. |
| 67 | * |
| 68 | * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request |
| 69 | * from ublk driver, should be issued only when starting device. After |
| 70 | * the associated cqe is returned, request's tag can be retrieved via |
| 71 | * cqe->userdata. |
| 72 | * |
| 73 | * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled |
| 74 | * this IO request, request's handling result is committed to ublk |
| 75 | * driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be |
| 76 | * handled before completing io request. |
| 77 | * |
| 78 | * NEED_GET_DATA: only used for write requests to set io addr and copy data |
| 79 | * When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA |
| 80 | * command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA. |
| 81 | * |
| 82 | * It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag |
| 83 | * while starting a ublk device. |
| 84 | */ |
| 85 | |
| 86 | /* |
| 87 | * Legacy IO command definition, don't use in new application, and don't |
| 88 | * add new such definition any more |
| 89 | */ |
| 90 | #define UBLK_IO_FETCH_REQ 0x20 |
| 91 | #define UBLK_IO_COMMIT_AND_FETCH_REQ 0x21 |
| 92 | #define UBLK_IO_NEED_GET_DATA 0x22 |
| 93 | |
| 94 | /* Any new IO command should encode by __IOWR() */ |
| 95 | #define UBLK_U_IO_FETCH_REQ \ |
| 96 | _IOWR('u', UBLK_IO_FETCH_REQ, struct ublksrv_io_cmd) |
| 97 | #define UBLK_U_IO_COMMIT_AND_FETCH_REQ \ |
| 98 | _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd) |
| 99 | #define UBLK_U_IO_NEED_GET_DATA \ |
| 100 | _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd) |
| 101 | #define UBLK_U_IO_REGISTER_IO_BUF \ |
| 102 | _IOWR('u', 0x23, struct ublksrv_io_cmd) |
| 103 | #define UBLK_U_IO_UNREGISTER_IO_BUF \ |
| 104 | _IOWR('u', 0x24, struct ublksrv_io_cmd) |
| 105 | |
| 106 | /* only ABORT means that no re-fetch */ |
| 107 | #define UBLK_IO_RES_OK 0 |
| 108 | #define UBLK_IO_RES_NEED_GET_DATA 1 |
| 109 | #define UBLK_IO_RES_ABORT (-ENODEV) |
| 110 | |
| 111 | #define UBLKSRV_CMD_BUF_OFFSET 0 |
| 112 | #define UBLKSRV_IO_BUF_OFFSET 0x80000000 |
| 113 | |
| 114 | /* tag bit is 16bit, so far limit at most 4096 IOs for each queue */ |
| 115 | #define UBLK_MAX_QUEUE_DEPTH 4096 |
| 116 | |
| 117 | /* single IO buffer max size is 32MB */ |
| 118 | #define UBLK_IO_BUF_OFF 0 |
| 119 | #define UBLK_IO_BUF_BITS 25 |
| 120 | #define UBLK_IO_BUF_BITS_MASK ((1ULL << UBLK_IO_BUF_BITS) - 1) |
| 121 | |
| 122 | /* so at most 64K IOs for each queue */ |
| 123 | #define UBLK_TAG_OFF UBLK_IO_BUF_BITS |
| 124 | #define UBLK_TAG_BITS 16 |
| 125 | #define UBLK_TAG_BITS_MASK ((1ULL << UBLK_TAG_BITS) - 1) |
| 126 | |
| 127 | /* max 4096 queues */ |
| 128 | #define UBLK_QID_OFF (UBLK_TAG_OFF + UBLK_TAG_BITS) |
| 129 | #define UBLK_QID_BITS 12 |
| 130 | #define UBLK_QID_BITS_MASK ((1ULL << UBLK_QID_BITS) - 1) |
| 131 | |
| 132 | #define UBLK_MAX_NR_QUEUES (1U << UBLK_QID_BITS) |
| 133 | |
| 134 | #define UBLKSRV_IO_BUF_TOTAL_BITS (UBLK_QID_OFF + UBLK_QID_BITS) |
| 135 | #define UBLKSRV_IO_BUF_TOTAL_SIZE (1ULL << UBLKSRV_IO_BUF_TOTAL_BITS) |
| 136 | |
| 137 | /* |
| 138 | * ublk server can register data buffers for incoming I/O requests with a sparse |
| 139 | * io_uring buffer table. The request buffer can then be used as the data buffer |
| 140 | * for io_uring operations via the fixed buffer index. |
| 141 | * Note that the ublk server can never directly access the request data memory. |
| 142 | * |
| 143 | * To use this feature, the ublk server must first register a sparse buffer |
| 144 | * table on an io_uring instance. |
| 145 | * When an incoming ublk request is received, the ublk server submits a |
| 146 | * UBLK_U_IO_REGISTER_IO_BUF command to that io_uring instance. The |
| 147 | * ublksrv_io_cmd's q_id and tag specify the request whose buffer to register |
| 148 | * and addr is the index in the io_uring's buffer table to install the buffer. |
| 149 | * SQEs can now be submitted to the io_uring to read/write the request's buffer |
| 150 | * by enabling fixed buffers (e.g. using IORING_OP_{READ,WRITE}_FIXED or |
| 151 | * IORING_URING_CMD_FIXED) and passing the registered buffer index in buf_index. |
| 152 | * Once the last io_uring operation using the request's buffer has completed, |
| 153 | * the ublk server submits a UBLK_U_IO_UNREGISTER_IO_BUF command with q_id, tag, |
| 154 | * and addr again specifying the request buffer to unregister. |
| 155 | * The ublk request is completed when its buffer is unregistered from all |
| 156 | * io_uring instances and the ublk server issues UBLK_U_IO_COMMIT_AND_FETCH_REQ. |
| 157 | * |
| 158 | * Not available for UBLK_F_UNPRIVILEGED_DEV, as a ublk server can leak |
| 159 | * uninitialized kernel memory by not reading into the full request buffer. |
| 160 | */ |
| 161 | #define UBLK_F_SUPPORT_ZERO_COPY (1ULL << 0) |
| 162 | |
| 163 | /* |
| 164 | * Force to complete io cmd via io_uring_cmd_complete_in_task so that |
| 165 | * performance comparison is done easily with using task_work_add |
| 166 | */ |
| 167 | #define UBLK_F_URING_CMD_COMP_IN_TASK (1ULL << 1) |
| 168 | |
| 169 | /* |
| 170 | * User should issue io cmd again for write requests to |
| 171 | * set io buffer address and copy data from bio vectors |
| 172 | * to the userspace io buffer. |
| 173 | * |
| 174 | * In this mode, task_work is not used. |
| 175 | */ |
| 176 | #define UBLK_F_NEED_GET_DATA (1UL << 2) |
| 177 | |
| 178 | /* |
| 179 | * - Block devices are recoverable if ublk server exits and restarts |
| 180 | * - Outstanding I/O when ublk server exits is met with errors |
| 181 | * - I/O issued while there is no ublk server queues |
| 182 | */ |
| 183 | #define UBLK_F_USER_RECOVERY (1UL << 3) |
| 184 | |
| 185 | /* |
| 186 | * - Block devices are recoverable if ublk server exits and restarts |
| 187 | * - Outstanding I/O when ublk server exits is reissued |
| 188 | * - I/O issued while there is no ublk server queues |
| 189 | */ |
| 190 | #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4) |
| 191 | |
| 192 | /* |
| 193 | * Unprivileged user can create /dev/ublkcN and /dev/ublkbN. |
| 194 | * |
| 195 | * /dev/ublk-control needs to be available for unprivileged user, and it |
| 196 | * can be done via udev rule to make all control commands available to |
| 197 | * unprivileged user. Except for the command of UBLK_CMD_ADD_DEV, all |
| 198 | * other commands are only allowed for the owner of the specified device. |
| 199 | * |
| 200 | * When userspace sends UBLK_CMD_ADD_DEV, the device pair's owner_uid and |
| 201 | * owner_gid are stored to ublksrv_ctrl_dev_info by kernel, so far only |
| 202 | * the current user's uid/gid is stored, that said owner of the created |
| 203 | * device is always the current user. |
| 204 | * |
| 205 | * We still need udev rule to apply OWNER/GROUP with the stored owner_uid |
| 206 | * and owner_gid. |
| 207 | * |
| 208 | * Then ublk server can be run as unprivileged user, and /dev/ublkbN can |
| 209 | * be accessed and managed by its owner represented by owner_uid/owner_gid. |
| 210 | */ |
| 211 | #define UBLK_F_UNPRIVILEGED_DEV (1UL << 5) |
| 212 | |
| 213 | /* use ioctl encoding for uring command */ |
| 214 | #define UBLK_F_CMD_IOCTL_ENCODE (1UL << 6) |
| 215 | |
| 216 | /* |
| 217 | * Copy between request and user buffer by pread()/pwrite() |
| 218 | * |
| 219 | * Not available for UBLK_F_UNPRIVILEGED_DEV, otherwise userspace may |
| 220 | * deceive us by not filling request buffer, then kernel uninitialized |
| 221 | * data may be leaked. |
| 222 | */ |
| 223 | #define UBLK_F_USER_COPY (1UL << 7) |
| 224 | |
| 225 | /* |
| 226 | * User space sets this flag when setting up the device to request zoned storage support. Kernel may |
| 227 | * deny the request by returning an error. |
| 228 | */ |
| 229 | #define UBLK_F_ZONED (1ULL << 8) |
| 230 | |
| 231 | /* |
| 232 | * - Block devices are recoverable if ublk server exits and restarts |
| 233 | * - Outstanding I/O when ublk server exits is met with errors |
| 234 | * - I/O issued while there is no ublk server is met with errors |
| 235 | */ |
| 236 | #define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9) |
| 237 | |
| 238 | /* |
| 239 | * Resizing a block device is possible with UBLK_U_CMD_UPDATE_SIZE |
| 240 | * New size is passed in cmd->data[0] and is in units of sectors |
| 241 | */ |
| 242 | #define UBLK_F_UPDATE_SIZE (1ULL << 10) |
| 243 | |
| 244 | /* |
| 245 | * request buffer is registered automatically to uring_cmd's io_uring |
| 246 | * context before delivering this io command to ublk server, meantime |
| 247 | * it is un-registered automatically when completing this io command. |
| 248 | * |
| 249 | * For using this feature: |
| 250 | * |
| 251 | * - ublk server has to create sparse buffer table on the same `io_ring_ctx` |
| 252 | * for issuing `UBLK_IO_FETCH_REQ` and `UBLK_IO_COMMIT_AND_FETCH_REQ`. |
| 253 | * If uring_cmd isn't issued on same `io_ring_ctx`, it is ublk server's |
| 254 | * responsibility to unregister the buffer by issuing `IO_UNREGISTER_IO_BUF` |
| 255 | * manually, otherwise this ublk request won't complete. |
| 256 | * |
| 257 | * - ublk server passes auto buf register data via uring_cmd's sqe->addr, |
| 258 | * `struct ublk_auto_buf_reg` is populated from sqe->addr, please see |
| 259 | * the definition of ublk_sqe_addr_to_auto_buf_reg() |
| 260 | * |
| 261 | * - pass buffer index from `ublk_auto_buf_reg.index` |
| 262 | * |
| 263 | * - all reserved fields in `ublk_auto_buf_reg` need to be zeroed |
| 264 | * |
| 265 | * - pass flags from `ublk_auto_buf_reg.flags` if needed |
| 266 | * |
| 267 | * This way avoids extra cost from two uring_cmd, but also simplifies backend |
| 268 | * implementation, such as, the dependency on IO_REGISTER_IO_BUF and |
| 269 | * IO_UNREGISTER_IO_BUF becomes not necessary. |
| 270 | * |
| 271 | * If wrong data or flags are provided, both IO_FETCH_REQ and |
| 272 | * IO_COMMIT_AND_FETCH_REQ are failed, for the latter, the ublk IO request |
| 273 | * won't be completed until new IO_COMMIT_AND_FETCH_REQ command is issued |
| 274 | * successfully |
| 275 | */ |
| 276 | #define UBLK_F_AUTO_BUF_REG (1ULL << 11) |
| 277 | |
| 278 | /* |
| 279 | * Control command `UBLK_U_CMD_QUIESCE_DEV` is added for quiescing device, |
| 280 | * which state can be transitioned to `UBLK_S_DEV_QUIESCED` or |
| 281 | * `UBLK_S_DEV_FAIL_IO` finally, and it needs ublk server cooperation for |
| 282 | * handling `UBLK_IO_RES_ABORT` correctly. |
| 283 | * |
| 284 | * Typical use case is for supporting to upgrade ublk server application, |
| 285 | * meantime keep ublk block device persistent during the period. |
| 286 | * |
| 287 | * This feature is only available when UBLK_F_USER_RECOVERY is enabled. |
| 288 | * |
| 289 | * Note, this command returns -EBUSY in case that all IO commands are being |
| 290 | * handled by ublk server and not completed in specified time period which |
| 291 | * is passed from the control command parameter. |
| 292 | */ |
| 293 | #define UBLK_F_QUIESCE (1ULL << 12) |
| 294 | |
| 295 | /* |
| 296 | * If this feature is set, ublk_drv supports each (qid,tag) pair having |
| 297 | * its own independent daemon task that is responsible for handling it. |
| 298 | * If it is not set, daemons are per-queue instead, so for two pairs |
| 299 | * (qid1,tag1) and (qid2,tag2), if qid1 == qid2, then the same task must |
| 300 | * be responsible for handling (qid1,tag1) and (qid2,tag2). |
| 301 | */ |
| 302 | #define UBLK_F_PER_IO_DAEMON (1ULL << 13) |
| 303 | |
| 304 | /* |
| 305 | * If this feature is set, UBLK_U_IO_REGISTER_IO_BUF/UBLK_U_IO_UNREGISTER_IO_BUF |
| 306 | * can be issued for an I/O on any task. q_id and tag are also ignored in |
| 307 | * UBLK_U_IO_UNREGISTER_IO_BUF's ublksrv_io_cmd. |
| 308 | * If it is unset, zero-copy buffers can only be registered and unregistered by |
| 309 | * the I/O's daemon task. The q_id and tag of the registered buffer are required |
| 310 | * in UBLK_U_IO_UNREGISTER_IO_BUF's ublksrv_io_cmd. |
| 311 | */ |
| 312 | #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14) |
| 313 | |
| 314 | /* device state */ |
| 315 | #define UBLK_S_DEV_DEAD 0 |
| 316 | #define UBLK_S_DEV_LIVE 1 |
| 317 | #define UBLK_S_DEV_QUIESCED 2 |
| 318 | #define UBLK_S_DEV_FAIL_IO 3 |
| 319 | |
| 320 | /* shipped via sqe->cmd of io_uring command */ |
| 321 | struct ublksrv_ctrl_cmd { |
| 322 | /* sent to which device, must be valid */ |
| 323 | __u32 dev_id; |
| 324 | |
| 325 | /* sent to which queue, must be -1 if the cmd isn't for queue */ |
| 326 | __u16 queue_id; |
| 327 | /* |
| 328 | * cmd specific buffer, can be IN or OUT. |
| 329 | */ |
| 330 | __u16 len; |
| 331 | __u64 addr; |
| 332 | |
| 333 | /* inline data */ |
| 334 | __u64 data[1]; |
| 335 | |
| 336 | /* |
| 337 | * Used for UBLK_F_UNPRIVILEGED_DEV and UBLK_CMD_GET_DEV_INFO2 |
| 338 | * only, include null char |
| 339 | */ |
| 340 | __u16 dev_path_len; |
| 341 | __u16 pad; |
| 342 | __u32 reserved; |
| 343 | }; |
| 344 | |
| 345 | struct ublksrv_ctrl_dev_info { |
| 346 | __u16 nr_hw_queues; |
| 347 | __u16 queue_depth; |
| 348 | __u16 state; |
| 349 | __u16 pad0; |
| 350 | |
| 351 | __u32 max_io_buf_bytes; |
| 352 | __u32 dev_id; |
| 353 | |
| 354 | __s32 ublksrv_pid; |
| 355 | __u32 pad1; |
| 356 | |
| 357 | __u64 flags; |
| 358 | |
| 359 | /* For ublksrv internal use, invisible to ublk driver */ |
| 360 | __u64 ublksrv_flags; |
| 361 | |
| 362 | __u32 owner_uid; /* store by kernel */ |
| 363 | __u32 owner_gid; /* store by kernel */ |
| 364 | __u64 reserved1; |
| 365 | __u64 reserved2; |
| 366 | }; |
| 367 | |
| 368 | #define UBLK_IO_OP_READ 0 |
| 369 | #define UBLK_IO_OP_WRITE 1 |
| 370 | #define UBLK_IO_OP_FLUSH 2 |
| 371 | #define UBLK_IO_OP_DISCARD 3 |
| 372 | #define UBLK_IO_OP_WRITE_SAME 4 |
| 373 | #define UBLK_IO_OP_WRITE_ZEROES 5 |
| 374 | #define UBLK_IO_OP_ZONE_OPEN 10 |
| 375 | #define UBLK_IO_OP_ZONE_CLOSE 11 |
| 376 | #define UBLK_IO_OP_ZONE_FINISH 12 |
| 377 | #define UBLK_IO_OP_ZONE_APPEND 13 |
| 378 | #define UBLK_IO_OP_ZONE_RESET_ALL 14 |
| 379 | #define UBLK_IO_OP_ZONE_RESET 15 |
| 380 | /* |
| 381 | * Construct a zone report. The report request is carried in `struct |
| 382 | * ublksrv_io_desc`. The `start_sector` field must be the first sector of a zone |
| 383 | * and shall indicate the first zone of the report. The `nr_zones` shall |
| 384 | * indicate how many zones should be reported at most. The report shall be |
| 385 | * delivered as a `struct blk_zone` array. To report fewer zones than requested, |
| 386 | * zero the last entry of the returned array. |
| 387 | * |
| 388 | * Related definitions(blk_zone, blk_zone_cond, blk_zone_type, ...) in |
| 389 | * include/uapi/linux/blkzoned.h are part of ublk UAPI. |
| 390 | */ |
| 391 | #define UBLK_IO_OP_REPORT_ZONES 18 |
| 392 | |
| 393 | #define UBLK_IO_F_FAILFAST_DEV (1U << 8) |
| 394 | #define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9) |
| 395 | #define UBLK_IO_F_FAILFAST_DRIVER (1U << 10) |
| 396 | #define UBLK_IO_F_META (1U << 11) |
| 397 | #define UBLK_IO_F_FUA (1U << 13) |
| 398 | #define UBLK_IO_F_NOUNMAP (1U << 15) |
| 399 | #define UBLK_IO_F_SWAP (1U << 16) |
| 400 | /* |
| 401 | * For UBLK_F_AUTO_BUF_REG & UBLK_AUTO_BUF_REG_FALLBACK only. |
| 402 | * |
| 403 | * This flag is set if auto buffer register is failed & ublk server passes |
| 404 | * UBLK_AUTO_BUF_REG_FALLBACK, and ublk server need to register buffer |
| 405 | * manually for handling the delivered IO command if this flag is observed |
| 406 | * |
| 407 | * ublk server has to check this flag if UBLK_AUTO_BUF_REG_FALLBACK is |
| 408 | * passed in. |
| 409 | */ |
| 410 | #define UBLK_IO_F_NEED_REG_BUF (1U << 17) |
| 411 | |
| 412 | /* |
| 413 | * io cmd is described by this structure, and stored in share memory, indexed |
| 414 | * by request tag. |
| 415 | * |
| 416 | * The data is stored by ublk driver, and read by ublksrv after one fetch command |
| 417 | * returns. |
| 418 | */ |
| 419 | struct ublksrv_io_desc { |
| 420 | /* op: bit 0-7, flags: bit 8-31 */ |
| 421 | __u32 op_flags; |
| 422 | |
| 423 | union { |
| 424 | __u32 nr_sectors; |
| 425 | __u32 nr_zones; /* for UBLK_IO_OP_REPORT_ZONES */ |
| 426 | }; |
| 427 | |
| 428 | /* start sector for this io */ |
| 429 | __u64 start_sector; |
| 430 | |
| 431 | /* buffer address in ublksrv daemon vm space, from ublk driver */ |
| 432 | __u64 addr; |
| 433 | }; |
| 434 | |
| 435 | static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod) |
| 436 | { |
| 437 | return iod->op_flags & 0xff; |
| 438 | } |
| 439 | |
| 440 | static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod) |
| 441 | { |
| 442 | return iod->op_flags >> 8; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * If this flag is set, fallback by completing the uring_cmd and setting |
| 447 | * `UBLK_IO_F_NEED_REG_BUF` in case of auto-buf-register failure; |
| 448 | * otherwise the client ublk request is failed silently |
| 449 | * |
| 450 | * If ublk server passes this flag, it has to check if UBLK_IO_F_NEED_REG_BUF |
| 451 | * is set in `ublksrv_io_desc.op_flags`. If UBLK_IO_F_NEED_REG_BUF is set, |
| 452 | * ublk server needs to register io buffer manually for handling IO command. |
| 453 | */ |
| 454 | #define UBLK_AUTO_BUF_REG_FALLBACK (1 << 0) |
| 455 | #define UBLK_AUTO_BUF_REG_F_MASK UBLK_AUTO_BUF_REG_FALLBACK |
| 456 | |
| 457 | struct ublk_auto_buf_reg { |
| 458 | /* index for registering the delivered request buffer */ |
| 459 | __u16 index; |
| 460 | __u8 flags; |
| 461 | __u8 reserved0; |
| 462 | |
| 463 | /* |
| 464 | * io_ring FD can be passed via the reserve field in future for |
| 465 | * supporting to register io buffer to external io_uring |
| 466 | */ |
| 467 | __u32 reserved1; |
| 468 | }; |
| 469 | |
| 470 | /* |
| 471 | * For UBLK_F_AUTO_BUF_REG, auto buffer register data is carried via |
| 472 | * uring_cmd's sqe->addr: |
| 473 | * |
| 474 | * - bit0 ~ bit15: buffer index |
| 475 | * - bit16 ~ bit23: flags |
| 476 | * - bit24 ~ bit31: reserved0 |
| 477 | * - bit32 ~ bit63: reserved1 |
| 478 | */ |
| 479 | static inline struct ublk_auto_buf_reg ublk_sqe_addr_to_auto_buf_reg( |
| 480 | __u64 sqe_addr) |
| 481 | { |
| 482 | struct ublk_auto_buf_reg reg = { |
| 483 | .index = (__u16)sqe_addr, |
| 484 | .flags = (__u8)(sqe_addr >> 16), |
| 485 | .reserved0 = (__u8)(sqe_addr >> 24), |
| 486 | .reserved1 = (__u32)(sqe_addr >> 32), |
| 487 | }; |
| 488 | |
| 489 | return reg; |
| 490 | } |
| 491 | |
| 492 | static inline __u64 |
| 493 | ublk_auto_buf_reg_to_sqe_addr(const struct ublk_auto_buf_reg *buf) |
| 494 | { |
| 495 | __u64 addr = buf->index | (__u64)buf->flags << 16 | (__u64)buf->reserved0 << 24 | |
| 496 | (__u64)buf->reserved1 << 32; |
| 497 | |
| 498 | return addr; |
| 499 | } |
| 500 | |
| 501 | /* issued to ublk driver via /dev/ublkcN */ |
| 502 | struct ublksrv_io_cmd { |
| 503 | __u16 q_id; |
| 504 | |
| 505 | /* for fetch/commit which result */ |
| 506 | __u16 tag; |
| 507 | |
| 508 | /* io result, it is valid for COMMIT* command only */ |
| 509 | __s32 result; |
| 510 | |
| 511 | union { |
| 512 | /* |
| 513 | * userspace buffer address in ublksrv daemon process, valid for |
| 514 | * FETCH* command only |
| 515 | * |
| 516 | * `addr` should not be used when UBLK_F_USER_COPY is enabled, |
| 517 | * because userspace handles data copy by pread()/pwrite() over |
| 518 | * /dev/ublkcN. But in case of UBLK_F_ZONED, this union is |
| 519 | * re-used to pass back the allocated LBA for |
| 520 | * UBLK_IO_OP_ZONE_APPEND which actually depends on |
| 521 | * UBLK_F_USER_COPY |
| 522 | */ |
| 523 | __u64 addr; |
| 524 | __u64 zone_append_lba; |
| 525 | }; |
| 526 | }; |
| 527 | |
| 528 | struct ublk_param_basic { |
| 529 | #define UBLK_ATTR_READ_ONLY (1 << 0) |
| 530 | #define UBLK_ATTR_ROTATIONAL (1 << 1) |
| 531 | #define UBLK_ATTR_VOLATILE_CACHE (1 << 2) |
| 532 | #define UBLK_ATTR_FUA (1 << 3) |
| 533 | __u32 attrs; |
| 534 | __u8 logical_bs_shift; |
| 535 | __u8 physical_bs_shift; |
| 536 | __u8 io_opt_shift; |
| 537 | __u8 io_min_shift; |
| 538 | |
| 539 | __u32 max_sectors; |
| 540 | __u32 chunk_sectors; |
| 541 | |
| 542 | __u64 dev_sectors; |
| 543 | __u64 virt_boundary_mask; |
| 544 | }; |
| 545 | |
| 546 | struct ublk_param_discard { |
| 547 | __u32 discard_alignment; |
| 548 | |
| 549 | __u32 discard_granularity; |
| 550 | __u32 max_discard_sectors; |
| 551 | |
| 552 | __u32 max_write_zeroes_sectors; |
| 553 | __u16 max_discard_segments; |
| 554 | __u16 reserved0; |
| 555 | }; |
| 556 | |
| 557 | /* |
| 558 | * read-only, can't set via UBLK_CMD_SET_PARAMS, disk_devt is available |
| 559 | * after device is started |
| 560 | */ |
| 561 | struct ublk_param_devt { |
| 562 | __u32 char_major; |
| 563 | __u32 char_minor; |
| 564 | __u32 disk_major; |
| 565 | __u32 disk_minor; |
| 566 | }; |
| 567 | |
| 568 | struct ublk_param_zoned { |
| 569 | __u32 max_open_zones; |
| 570 | __u32 max_active_zones; |
| 571 | __u32 max_zone_append_sectors; |
| 572 | __u8 reserved[20]; |
| 573 | }; |
| 574 | |
| 575 | struct ublk_param_dma_align { |
| 576 | __u32 alignment; |
| 577 | __u8 pad[4]; |
| 578 | }; |
| 579 | |
| 580 | #define UBLK_MIN_SEGMENT_SIZE 4096 |
| 581 | /* |
| 582 | * If any one of the three segment parameter is set as 0, the behavior is |
| 583 | * undefined. |
| 584 | */ |
| 585 | struct ublk_param_segment { |
| 586 | /* |
| 587 | * seg_boundary_mask + 1 needs to be power_of_2(), and the sum has |
| 588 | * to be >= UBLK_MIN_SEGMENT_SIZE(4096) |
| 589 | */ |
| 590 | __u64 seg_boundary_mask; |
| 591 | |
| 592 | /* |
| 593 | * max_segment_size could be override by virt_boundary_mask, so be |
| 594 | * careful when setting both. |
| 595 | * |
| 596 | * max_segment_size has to be >= UBLK_MIN_SEGMENT_SIZE(4096) |
| 597 | */ |
| 598 | __u32 max_segment_size; |
| 599 | __u16 max_segments; |
| 600 | __u8 pad[2]; |
| 601 | }; |
| 602 | |
| 603 | struct ublk_params { |
| 604 | /* |
| 605 | * Total length of parameters, userspace has to set 'len' for both |
| 606 | * SET_PARAMS and GET_PARAMS command, and driver may update len |
| 607 | * if two sides use different version of 'ublk_params', same with |
| 608 | * 'types' fields. |
| 609 | */ |
| 610 | __u32 len; |
| 611 | #define UBLK_PARAM_TYPE_BASIC (1 << 0) |
| 612 | #define UBLK_PARAM_TYPE_DISCARD (1 << 1) |
| 613 | #define UBLK_PARAM_TYPE_DEVT (1 << 2) |
| 614 | #define UBLK_PARAM_TYPE_ZONED (1 << 3) |
| 615 | #define UBLK_PARAM_TYPE_DMA_ALIGN (1 << 4) |
| 616 | #define UBLK_PARAM_TYPE_SEGMENT (1 << 5) |
| 617 | __u32 types; /* types of parameter included */ |
| 618 | |
| 619 | struct ublk_param_basic basic; |
| 620 | struct ublk_param_discard discard; |
| 621 | struct ublk_param_devt devt; |
| 622 | struct ublk_param_zoned zoned; |
| 623 | struct ublk_param_dma_align dma; |
| 624 | struct ublk_param_segment seg; |
| 625 | }; |
| 626 | |
| 627 | #endif |
| 628 | |