171f28f31SMing Lei /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
271f28f31SMing Lei #ifndef USER_BLK_DRV_CMD_INC_H
371f28f31SMing Lei #define USER_BLK_DRV_CMD_INC_H
471f28f31SMing Lei
571f28f31SMing Lei #include <linux/types.h>
671f28f31SMing Lei
771f28f31SMing Lei /* ublk server command definition */
871f28f31SMing Lei
971f28f31SMing Lei /*
1071f28f31SMing Lei * Admin commands, issued by ublk server, and handled by ublk driver.
112d786e66SMing Lei *
122d786e66SMing Lei * Legacy command definition, don't use in new application, and don't
132d786e66SMing Lei * add new such definition any more
1471f28f31SMing Lei */
1571f28f31SMing Lei #define UBLK_CMD_GET_QUEUE_AFFINITY 0x01
1671f28f31SMing Lei #define UBLK_CMD_GET_DEV_INFO 0x02
1771f28f31SMing Lei #define UBLK_CMD_ADD_DEV 0x04
1871f28f31SMing Lei #define UBLK_CMD_DEL_DEV 0x05
1971f28f31SMing Lei #define UBLK_CMD_START_DEV 0x06
2071f28f31SMing Lei #define UBLK_CMD_STOP_DEV 0x07
210aa73170SMing Lei #define UBLK_CMD_SET_PARAMS 0x08
220aa73170SMing Lei #define UBLK_CMD_GET_PARAMS 0x09
23c732a852SZiyangZhang #define UBLK_CMD_START_USER_RECOVERY 0x10
24c732a852SZiyangZhang #define UBLK_CMD_END_USER_RECOVERY 0x11
254093cb5aSMing Lei #define UBLK_CMD_GET_DEV_INFO2 0x12
264093cb5aSMing Lei
272d786e66SMing Lei /* Any new ctrl command should encode by __IO*() */
282d786e66SMing Lei #define UBLK_U_CMD_GET_QUEUE_AFFINITY \
292d786e66SMing Lei _IOR('u', UBLK_CMD_GET_QUEUE_AFFINITY, struct ublksrv_ctrl_cmd)
302d786e66SMing Lei #define UBLK_U_CMD_GET_DEV_INFO \
312d786e66SMing Lei _IOR('u', UBLK_CMD_GET_DEV_INFO, struct ublksrv_ctrl_cmd)
322d786e66SMing Lei #define UBLK_U_CMD_ADD_DEV \
332d786e66SMing Lei _IOWR('u', UBLK_CMD_ADD_DEV, struct ublksrv_ctrl_cmd)
342d786e66SMing Lei #define UBLK_U_CMD_DEL_DEV \
352d786e66SMing Lei _IOWR('u', UBLK_CMD_DEL_DEV, struct ublksrv_ctrl_cmd)
362d786e66SMing Lei #define UBLK_U_CMD_START_DEV \
372d786e66SMing Lei _IOWR('u', UBLK_CMD_START_DEV, struct ublksrv_ctrl_cmd)
382d786e66SMing Lei #define UBLK_U_CMD_STOP_DEV \
392d786e66SMing Lei _IOWR('u', UBLK_CMD_STOP_DEV, struct ublksrv_ctrl_cmd)
402d786e66SMing Lei #define UBLK_U_CMD_SET_PARAMS \
412d786e66SMing Lei _IOWR('u', UBLK_CMD_SET_PARAMS, struct ublksrv_ctrl_cmd)
422d786e66SMing Lei #define UBLK_U_CMD_GET_PARAMS \
432d786e66SMing Lei _IOR('u', UBLK_CMD_GET_PARAMS, struct ublksrv_ctrl_cmd)
442d786e66SMing Lei #define UBLK_U_CMD_START_USER_RECOVERY \
452d786e66SMing Lei _IOWR('u', UBLK_CMD_START_USER_RECOVERY, struct ublksrv_ctrl_cmd)
462d786e66SMing Lei #define UBLK_U_CMD_END_USER_RECOVERY \
472d786e66SMing Lei _IOWR('u', UBLK_CMD_END_USER_RECOVERY, struct ublksrv_ctrl_cmd)
482d786e66SMing Lei #define UBLK_U_CMD_GET_DEV_INFO2 \
492d786e66SMing Lei _IOR('u', UBLK_CMD_GET_DEV_INFO2, struct ublksrv_ctrl_cmd)
50b5bbc52fSMing Lei #define UBLK_U_CMD_GET_FEATURES \
51b5bbc52fSMing Lei _IOR('u', 0x13, struct ublksrv_ctrl_cmd)
5213fe8e68SMing Lei #define UBLK_U_CMD_DEL_DEV_ASYNC \
5313fe8e68SMing Lei _IOR('u', 0x14, struct ublksrv_ctrl_cmd)
54b5bbc52fSMing Lei
55b5bbc52fSMing Lei /*
56b5bbc52fSMing Lei * 64bits are enough now, and it should be easy to extend in case of
57b5bbc52fSMing Lei * running out of feature flags
58b5bbc52fSMing Lei */
59b5bbc52fSMing Lei #define UBLK_FEATURES_LEN 8
602d786e66SMing Lei
6171f28f31SMing Lei /*
6271f28f31SMing Lei * IO commands, issued by ublk server, and handled by ublk driver.
6371f28f31SMing Lei *
6471f28f31SMing Lei * FETCH_REQ: issued via sqe(URING_CMD) beforehand for fetching IO request
6571f28f31SMing Lei * from ublk driver, should be issued only when starting device. After
6671f28f31SMing Lei * the associated cqe is returned, request's tag can be retrieved via
6771f28f31SMing Lei * cqe->userdata.
6871f28f31SMing Lei *
6971f28f31SMing Lei * COMMIT_AND_FETCH_REQ: issued via sqe(URING_CMD) after ublkserver handled
7071f28f31SMing Lei * this IO request, request's handling result is committed to ublk
7171f28f31SMing Lei * driver, meantime FETCH_REQ is piggyback, and FETCH_REQ has to be
7271f28f31SMing Lei * handled before completing io request.
734e18403dSZiyangZhang *
744e18403dSZiyangZhang * NEED_GET_DATA: only used for write requests to set io addr and copy data
754e18403dSZiyangZhang * When NEED_GET_DATA is set, ublksrv has to issue UBLK_IO_NEED_GET_DATA
764e18403dSZiyangZhang * command after ublk driver returns UBLK_IO_RES_NEED_GET_DATA.
774e18403dSZiyangZhang *
784e18403dSZiyangZhang * It is only used if ublksrv set UBLK_F_NEED_GET_DATA flag
794e18403dSZiyangZhang * while starting a ublk device.
8071f28f31SMing Lei */
812d786e66SMing Lei
822d786e66SMing Lei /*
832d786e66SMing Lei * Legacy IO command definition, don't use in new application, and don't
842d786e66SMing Lei * add new such definition any more
852d786e66SMing Lei */
8671f28f31SMing Lei #define UBLK_IO_FETCH_REQ 0x20
8771f28f31SMing Lei #define UBLK_IO_COMMIT_AND_FETCH_REQ 0x21
884e18403dSZiyangZhang #define UBLK_IO_NEED_GET_DATA 0x22
8971f28f31SMing Lei
902d786e66SMing Lei /* Any new IO command should encode by __IOWR() */
912d786e66SMing Lei #define UBLK_U_IO_FETCH_REQ \
922d786e66SMing Lei _IOWR('u', UBLK_IO_FETCH_REQ, struct ublksrv_io_cmd)
932d786e66SMing Lei #define UBLK_U_IO_COMMIT_AND_FETCH_REQ \
942d786e66SMing Lei _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd)
952d786e66SMing Lei #define UBLK_U_IO_NEED_GET_DATA \
962d786e66SMing Lei _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd)
971f6540e2SKeith Busch #define UBLK_U_IO_REGISTER_IO_BUF \
981f6540e2SKeith Busch _IOWR('u', 0x23, struct ublksrv_io_cmd)
991f6540e2SKeith Busch #define UBLK_U_IO_UNREGISTER_IO_BUF \
1001f6540e2SKeith Busch _IOWR('u', 0x24, struct ublksrv_io_cmd)
1012d786e66SMing Lei
10271f28f31SMing Lei /* only ABORT means that no re-fetch */
10371f28f31SMing Lei #define UBLK_IO_RES_OK 0
1044e18403dSZiyangZhang #define UBLK_IO_RES_NEED_GET_DATA 1
10571f28f31SMing Lei #define UBLK_IO_RES_ABORT (-ENODEV)
10671f28f31SMing Lei
10771f28f31SMing Lei #define UBLKSRV_CMD_BUF_OFFSET 0
10871f28f31SMing Lei #define UBLKSRV_IO_BUF_OFFSET 0x80000000
10971f28f31SMing Lei
11062fe99ceSMing Lei /* tag bit is 16bit, so far limit at most 4096 IOs for each queue */
11171f28f31SMing Lei #define UBLK_MAX_QUEUE_DEPTH 4096
11271f28f31SMing Lei
11362fe99ceSMing Lei /* single IO buffer max size is 32MB */
11462fe99ceSMing Lei #define UBLK_IO_BUF_OFF 0
11562fe99ceSMing Lei #define UBLK_IO_BUF_BITS 25
11662fe99ceSMing Lei #define UBLK_IO_BUF_BITS_MASK ((1ULL << UBLK_IO_BUF_BITS) - 1)
11762fe99ceSMing Lei
11862fe99ceSMing Lei /* so at most 64K IOs for each queue */
11962fe99ceSMing Lei #define UBLK_TAG_OFF UBLK_IO_BUF_BITS
12062fe99ceSMing Lei #define UBLK_TAG_BITS 16
12162fe99ceSMing Lei #define UBLK_TAG_BITS_MASK ((1ULL << UBLK_TAG_BITS) - 1)
12262fe99ceSMing Lei
12362fe99ceSMing Lei /* max 4096 queues */
12462fe99ceSMing Lei #define UBLK_QID_OFF (UBLK_TAG_OFF + UBLK_TAG_BITS)
12562fe99ceSMing Lei #define UBLK_QID_BITS 12
12662fe99ceSMing Lei #define UBLK_QID_BITS_MASK ((1ULL << UBLK_QID_BITS) - 1)
12762fe99ceSMing Lei
12862fe99ceSMing Lei #define UBLK_MAX_NR_QUEUES (1U << UBLK_QID_BITS)
12962fe99ceSMing Lei
13062fe99ceSMing Lei #define UBLKSRV_IO_BUF_TOTAL_BITS (UBLK_QID_OFF + UBLK_QID_BITS)
13162fe99ceSMing Lei #define UBLKSRV_IO_BUF_TOTAL_SIZE (1ULL << UBLKSRV_IO_BUF_TOTAL_BITS)
13262fe99ceSMing Lei
13371f28f31SMing Lei /*
13471f28f31SMing Lei * zero copy requires 4k block size, and can remap ublk driver's io
13571f28f31SMing Lei * request into ublksrv's vm space
13671f28f31SMing Lei */
1376d8c5afcSMing Lei #define UBLK_F_SUPPORT_ZERO_COPY (1ULL << 0)
13871f28f31SMing Lei
1390edb3696SMing Lei /*
1400edb3696SMing Lei * Force to complete io cmd via io_uring_cmd_complete_in_task so that
1410edb3696SMing Lei * performance comparison is done easily with using task_work_add
1420edb3696SMing Lei */
1436d8c5afcSMing Lei #define UBLK_F_URING_CMD_COMP_IN_TASK (1ULL << 1)
1440edb3696SMing Lei
1454e18403dSZiyangZhang /*
1464e18403dSZiyangZhang * User should issue io cmd again for write requests to
1474e18403dSZiyangZhang * set io buffer address and copy data from bio vectors
1484e18403dSZiyangZhang * to the userspace io buffer.
1494e18403dSZiyangZhang *
1504e18403dSZiyangZhang * In this mode, task_work is not used.
1514e18403dSZiyangZhang */
1524e18403dSZiyangZhang #define UBLK_F_NEED_GET_DATA (1UL << 2)
1534e18403dSZiyangZhang
15459eaa01cSUday Shankar /*
15559eaa01cSUday Shankar * - Block devices are recoverable if ublk server exits and restarts
15659eaa01cSUday Shankar * - Outstanding I/O when ublk server exits is met with errors
15759eaa01cSUday Shankar * - I/O issued while there is no ublk server queues
15859eaa01cSUday Shankar */
15977a440e2SZiyangZhang #define UBLK_F_USER_RECOVERY (1UL << 3)
16077a440e2SZiyangZhang
16159eaa01cSUday Shankar /*
16259eaa01cSUday Shankar * - Block devices are recoverable if ublk server exits and restarts
16359eaa01cSUday Shankar * - Outstanding I/O when ublk server exits is reissued
16459eaa01cSUday Shankar * - I/O issued while there is no ublk server queues
16559eaa01cSUday Shankar */
166a0d41dc1SZiyangZhang #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4)
167a0d41dc1SZiyangZhang
1684093cb5aSMing Lei /*
1694093cb5aSMing Lei * Unprivileged user can create /dev/ublkcN and /dev/ublkbN.
1704093cb5aSMing Lei *
1714093cb5aSMing Lei * /dev/ublk-control needs to be available for unprivileged user, and it
1724093cb5aSMing Lei * can be done via udev rule to make all control commands available to
1734093cb5aSMing Lei * unprivileged user. Except for the command of UBLK_CMD_ADD_DEV, all
1744093cb5aSMing Lei * other commands are only allowed for the owner of the specified device.
1754093cb5aSMing Lei *
1764093cb5aSMing Lei * When userspace sends UBLK_CMD_ADD_DEV, the device pair's owner_uid and
1774093cb5aSMing Lei * owner_gid are stored to ublksrv_ctrl_dev_info by kernel, so far only
1784093cb5aSMing Lei * the current user's uid/gid is stored, that said owner of the created
1794093cb5aSMing Lei * device is always the current user.
1804093cb5aSMing Lei *
1814093cb5aSMing Lei * We still need udev rule to apply OWNER/GROUP with the stored owner_uid
1824093cb5aSMing Lei * and owner_gid.
1834093cb5aSMing Lei *
1844093cb5aSMing Lei * Then ublk server can be run as unprivileged user, and /dev/ublkbN can
1854093cb5aSMing Lei * be accessed and managed by its owner represented by owner_uid/owner_gid.
1864093cb5aSMing Lei */
1874093cb5aSMing Lei #define UBLK_F_UNPRIVILEGED_DEV (1UL << 5)
1884093cb5aSMing Lei
1892d786e66SMing Lei /* use ioctl encoding for uring command */
1902d786e66SMing Lei #define UBLK_F_CMD_IOCTL_ENCODE (1UL << 6)
1912d786e66SMing Lei
19242aafd8bSMing Lei /*
19342aafd8bSMing Lei * Copy between request and user buffer by pread()/pwrite()
19442aafd8bSMing Lei *
19542aafd8bSMing Lei * Not available for UBLK_F_UNPRIVILEGED_DEV, otherwise userspace may
19642aafd8bSMing Lei * deceive us by not filling request buffer, then kernel uninitialized
19742aafd8bSMing Lei * data may be leaked.
19842aafd8bSMing Lei */
1991172d5b8SMing Lei #define UBLK_F_USER_COPY (1UL << 7)
2001172d5b8SMing Lei
20129802d7cSAndreas Hindborg /*
20229802d7cSAndreas Hindborg * User space sets this flag when setting up the device to request zoned storage support. Kernel may
20329802d7cSAndreas Hindborg * deny the request by returning an error.
20429802d7cSAndreas Hindborg */
20529802d7cSAndreas Hindborg #define UBLK_F_ZONED (1ULL << 8)
20629802d7cSAndreas Hindborg
20759eaa01cSUday Shankar /*
20859eaa01cSUday Shankar * - Block devices are recoverable if ublk server exits and restarts
20959eaa01cSUday Shankar * - Outstanding I/O when ublk server exits is met with errors
21059eaa01cSUday Shankar * - I/O issued while there is no ublk server is met with errors
21159eaa01cSUday Shankar */
21259eaa01cSUday Shankar #define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9)
21359eaa01cSUday Shankar
21471f28f31SMing Lei /* device state */
21571f28f31SMing Lei #define UBLK_S_DEV_DEAD 0
21671f28f31SMing Lei #define UBLK_S_DEV_LIVE 1
21777a440e2SZiyangZhang #define UBLK_S_DEV_QUIESCED 2
21859eaa01cSUday Shankar #define UBLK_S_DEV_FAIL_IO 3
21971f28f31SMing Lei
22071f28f31SMing Lei /* shipped via sqe->cmd of io_uring command */
22171f28f31SMing Lei struct ublksrv_ctrl_cmd {
22271f28f31SMing Lei /* sent to which device, must be valid */
22371f28f31SMing Lei __u32 dev_id;
22471f28f31SMing Lei
22571f28f31SMing Lei /* sent to which queue, must be -1 if the cmd isn't for queue */
22671f28f31SMing Lei __u16 queue_id;
22771f28f31SMing Lei /*
22871f28f31SMing Lei * cmd specific buffer, can be IN or OUT.
22971f28f31SMing Lei */
23071f28f31SMing Lei __u16 len;
23171f28f31SMing Lei __u64 addr;
23271f28f31SMing Lei
23371f28f31SMing Lei /* inline data */
2344093cb5aSMing Lei __u64 data[1];
2354093cb5aSMing Lei
2364093cb5aSMing Lei /*
2374093cb5aSMing Lei * Used for UBLK_F_UNPRIVILEGED_DEV and UBLK_CMD_GET_DEV_INFO2
2384093cb5aSMing Lei * only, include null char
2394093cb5aSMing Lei */
2404093cb5aSMing Lei __u16 dev_path_len;
2414093cb5aSMing Lei __u16 pad;
2424093cb5aSMing Lei __u32 reserved;
24371f28f31SMing Lei };
24471f28f31SMing Lei
24571f28f31SMing Lei struct ublksrv_ctrl_dev_info {
24671f28f31SMing Lei __u16 nr_hw_queues;
24771f28f31SMing Lei __u16 queue_depth;
24871f28f31SMing Lei __u16 state;
2494bf9cbf3SMing Lei __u16 pad0;
25071f28f31SMing Lei
2514bf9cbf3SMing Lei __u32 max_io_buf_bytes;
25271f28f31SMing Lei __u32 dev_id;
25371f28f31SMing Lei
25471f28f31SMing Lei __s32 ublksrv_pid;
2554bf9cbf3SMing Lei __u32 pad1;
2564bf9cbf3SMing Lei
2576d8c5afcSMing Lei __u64 flags;
25871f28f31SMing Lei
25971f28f31SMing Lei /* For ublksrv internal use, invisible to ublk driver */
26071f28f31SMing Lei __u64 ublksrv_flags;
2614bf9cbf3SMing Lei
2624093cb5aSMing Lei __u32 owner_uid; /* store by kernel */
2634093cb5aSMing Lei __u32 owner_gid; /* store by kernel */
2644bf9cbf3SMing Lei __u64 reserved1;
2654bf9cbf3SMing Lei __u64 reserved2;
26671f28f31SMing Lei };
26771f28f31SMing Lei
26871f28f31SMing Lei #define UBLK_IO_OP_READ 0
26971f28f31SMing Lei #define UBLK_IO_OP_WRITE 1
27071f28f31SMing Lei #define UBLK_IO_OP_FLUSH 2
27171f28f31SMing Lei #define UBLK_IO_OP_DISCARD 3
27271f28f31SMing Lei #define UBLK_IO_OP_WRITE_SAME 4
27371f28f31SMing Lei #define UBLK_IO_OP_WRITE_ZEROES 5
27429802d7cSAndreas Hindborg #define UBLK_IO_OP_ZONE_OPEN 10
27529802d7cSAndreas Hindborg #define UBLK_IO_OP_ZONE_CLOSE 11
27629802d7cSAndreas Hindborg #define UBLK_IO_OP_ZONE_FINISH 12
27729802d7cSAndreas Hindborg #define UBLK_IO_OP_ZONE_APPEND 13
278851e0629SMing Lei #define UBLK_IO_OP_ZONE_RESET_ALL 14
27929802d7cSAndreas Hindborg #define UBLK_IO_OP_ZONE_RESET 15
28029802d7cSAndreas Hindborg /*
28129802d7cSAndreas Hindborg * Construct a zone report. The report request is carried in `struct
28229802d7cSAndreas Hindborg * ublksrv_io_desc`. The `start_sector` field must be the first sector of a zone
28329802d7cSAndreas Hindborg * and shall indicate the first zone of the report. The `nr_zones` shall
28429802d7cSAndreas Hindborg * indicate how many zones should be reported at most. The report shall be
28529802d7cSAndreas Hindborg * delivered as a `struct blk_zone` array. To report fewer zones than requested,
28629802d7cSAndreas Hindborg * zero the last entry of the returned array.
28729802d7cSAndreas Hindborg *
28829802d7cSAndreas Hindborg * Related definitions(blk_zone, blk_zone_cond, blk_zone_type, ...) in
28929802d7cSAndreas Hindborg * include/uapi/linux/blkzoned.h are part of ublk UAPI.
29029802d7cSAndreas Hindborg */
29129802d7cSAndreas Hindborg #define UBLK_IO_OP_REPORT_ZONES 18
29271f28f31SMing Lei
29371f28f31SMing Lei #define UBLK_IO_F_FAILFAST_DEV (1U << 8)
29471f28f31SMing Lei #define UBLK_IO_F_FAILFAST_TRANSPORT (1U << 9)
29571f28f31SMing Lei #define UBLK_IO_F_FAILFAST_DRIVER (1U << 10)
29671f28f31SMing Lei #define UBLK_IO_F_META (1U << 11)
29771f28f31SMing Lei #define UBLK_IO_F_FUA (1U << 13)
29871f28f31SMing Lei #define UBLK_IO_F_NOUNMAP (1U << 15)
29971f28f31SMing Lei #define UBLK_IO_F_SWAP (1U << 16)
30071f28f31SMing Lei
30171f28f31SMing Lei /*
30271f28f31SMing Lei * io cmd is described by this structure, and stored in share memory, indexed
30371f28f31SMing Lei * by request tag.
30471f28f31SMing Lei *
30571f28f31SMing Lei * The data is stored by ublk driver, and read by ublksrv after one fetch command
30671f28f31SMing Lei * returns.
30771f28f31SMing Lei */
30871f28f31SMing Lei struct ublksrv_io_desc {
30971f28f31SMing Lei /* op: bit 0-7, flags: bit 8-31 */
31071f28f31SMing Lei __u32 op_flags;
31171f28f31SMing Lei
31229802d7cSAndreas Hindborg union {
31371f28f31SMing Lei __u32 nr_sectors;
31429802d7cSAndreas Hindborg __u32 nr_zones; /* for UBLK_IO_OP_REPORT_ZONES */
31529802d7cSAndreas Hindborg };
31671f28f31SMing Lei
31771f28f31SMing Lei /* start sector for this io */
31871f28f31SMing Lei __u64 start_sector;
31971f28f31SMing Lei
32071f28f31SMing Lei /* buffer address in ublksrv daemon vm space, from ublk driver */
32171f28f31SMing Lei __u64 addr;
32271f28f31SMing Lei };
32371f28f31SMing Lei
ublksrv_get_op(const struct ublksrv_io_desc * iod)32471f28f31SMing Lei static inline __u8 ublksrv_get_op(const struct ublksrv_io_desc *iod)
32571f28f31SMing Lei {
32671f28f31SMing Lei return iod->op_flags & 0xff;
32771f28f31SMing Lei }
32871f28f31SMing Lei
ublksrv_get_flags(const struct ublksrv_io_desc * iod)32971f28f31SMing Lei static inline __u32 ublksrv_get_flags(const struct ublksrv_io_desc *iod)
33071f28f31SMing Lei {
33171f28f31SMing Lei return iod->op_flags >> 8;
33271f28f31SMing Lei }
33371f28f31SMing Lei
33471f28f31SMing Lei /* issued to ublk driver via /dev/ublkcN */
33571f28f31SMing Lei struct ublksrv_io_cmd {
33671f28f31SMing Lei __u16 q_id;
33771f28f31SMing Lei
33871f28f31SMing Lei /* for fetch/commit which result */
33971f28f31SMing Lei __u16 tag;
34071f28f31SMing Lei
34171f28f31SMing Lei /* io result, it is valid for COMMIT* command only */
34271f28f31SMing Lei __s32 result;
34371f28f31SMing Lei
34429802d7cSAndreas Hindborg union {
34571f28f31SMing Lei /*
34671f28f31SMing Lei * userspace buffer address in ublksrv daemon process, valid for
34771f28f31SMing Lei * FETCH* command only
34829802d7cSAndreas Hindborg *
34929802d7cSAndreas Hindborg * `addr` should not be used when UBLK_F_USER_COPY is enabled,
35029802d7cSAndreas Hindborg * because userspace handles data copy by pread()/pwrite() over
35129802d7cSAndreas Hindborg * /dev/ublkcN. But in case of UBLK_F_ZONED, this union is
35229802d7cSAndreas Hindborg * re-used to pass back the allocated LBA for
35329802d7cSAndreas Hindborg * UBLK_IO_OP_ZONE_APPEND which actually depends on
35429802d7cSAndreas Hindborg * UBLK_F_USER_COPY
35571f28f31SMing Lei */
35671f28f31SMing Lei __u64 addr;
35729802d7cSAndreas Hindborg __u64 zone_append_lba;
35829802d7cSAndreas Hindborg };
35971f28f31SMing Lei };
36071f28f31SMing Lei
3610aa73170SMing Lei struct ublk_param_basic {
3620aa73170SMing Lei #define UBLK_ATTR_READ_ONLY (1 << 0)
3630aa73170SMing Lei #define UBLK_ATTR_ROTATIONAL (1 << 1)
3640aa73170SMing Lei #define UBLK_ATTR_VOLATILE_CACHE (1 << 2)
3650aa73170SMing Lei #define UBLK_ATTR_FUA (1 << 3)
3660aa73170SMing Lei __u32 attrs;
3670aa73170SMing Lei __u8 logical_bs_shift;
3680aa73170SMing Lei __u8 physical_bs_shift;
3690aa73170SMing Lei __u8 io_opt_shift;
3700aa73170SMing Lei __u8 io_min_shift;
3710aa73170SMing Lei
3720aa73170SMing Lei __u32 max_sectors;
3730aa73170SMing Lei __u32 chunk_sectors;
3740aa73170SMing Lei
3750aa73170SMing Lei __u64 dev_sectors;
3760aa73170SMing Lei __u64 virt_boundary_mask;
3770aa73170SMing Lei };
3780aa73170SMing Lei
3790aa73170SMing Lei struct ublk_param_discard {
3800aa73170SMing Lei __u32 discard_alignment;
3810aa73170SMing Lei
3820aa73170SMing Lei __u32 discard_granularity;
3830aa73170SMing Lei __u32 max_discard_sectors;
3840aa73170SMing Lei
3850aa73170SMing Lei __u32 max_write_zeroes_sectors;
3860aa73170SMing Lei __u16 max_discard_segments;
3870aa73170SMing Lei __u16 reserved0;
3880aa73170SMing Lei };
3890aa73170SMing Lei
390abb864d3SMing Lei /*
391abb864d3SMing Lei * read-only, can't set via UBLK_CMD_SET_PARAMS, disk_devt is available
392abb864d3SMing Lei * after device is started
393abb864d3SMing Lei */
394abb864d3SMing Lei struct ublk_param_devt {
395abb864d3SMing Lei __u32 char_major;
396abb864d3SMing Lei __u32 char_minor;
397abb864d3SMing Lei __u32 disk_major;
398abb864d3SMing Lei __u32 disk_minor;
399abb864d3SMing Lei };
400abb864d3SMing Lei
40129802d7cSAndreas Hindborg struct ublk_param_zoned {
40229802d7cSAndreas Hindborg __u32 max_open_zones;
40329802d7cSAndreas Hindborg __u32 max_active_zones;
40429802d7cSAndreas Hindborg __u32 max_zone_append_sectors;
40529802d7cSAndreas Hindborg __u8 reserved[20];
40629802d7cSAndreas Hindborg };
40729802d7cSAndreas Hindborg
408e84025d2SMing Lei struct ublk_param_dma_align {
409e84025d2SMing Lei __u32 alignment;
410e84025d2SMing Lei __u8 pad[4];
411e84025d2SMing Lei };
412e84025d2SMing Lei
413*ebf695f1SMing Lei #define UBLK_MIN_SEGMENT_SIZE 4096
414*ebf695f1SMing Lei /*
415*ebf695f1SMing Lei * If any one of the three segment parameter is set as 0, the behavior is
416*ebf695f1SMing Lei * undefined.
417*ebf695f1SMing Lei */
418*ebf695f1SMing Lei struct ublk_param_segment {
419*ebf695f1SMing Lei /*
420*ebf695f1SMing Lei * seg_boundary_mask + 1 needs to be power_of_2(), and the sum has
421*ebf695f1SMing Lei * to be >= UBLK_MIN_SEGMENT_SIZE(4096)
422*ebf695f1SMing Lei */
423*ebf695f1SMing Lei __u64 seg_boundary_mask;
424*ebf695f1SMing Lei
425*ebf695f1SMing Lei /*
426*ebf695f1SMing Lei * max_segment_size could be override by virt_boundary_mask, so be
427*ebf695f1SMing Lei * careful when setting both.
428*ebf695f1SMing Lei *
429*ebf695f1SMing Lei * max_segment_size has to be >= UBLK_MIN_SEGMENT_SIZE(4096)
430*ebf695f1SMing Lei */
431*ebf695f1SMing Lei __u32 max_segment_size;
432*ebf695f1SMing Lei __u16 max_segments;
433*ebf695f1SMing Lei __u8 pad[2];
434*ebf695f1SMing Lei };
435*ebf695f1SMing Lei
4360aa73170SMing Lei struct ublk_params {
4370aa73170SMing Lei /*
4380aa73170SMing Lei * Total length of parameters, userspace has to set 'len' for both
4390aa73170SMing Lei * SET_PARAMS and GET_PARAMS command, and driver may update len
4400aa73170SMing Lei * if two sides use different version of 'ublk_params', same with
4410aa73170SMing Lei * 'types' fields.
4420aa73170SMing Lei */
4430aa73170SMing Lei __u32 len;
4440aa73170SMing Lei #define UBLK_PARAM_TYPE_BASIC (1 << 0)
4450aa73170SMing Lei #define UBLK_PARAM_TYPE_DISCARD (1 << 1)
446abb864d3SMing Lei #define UBLK_PARAM_TYPE_DEVT (1 << 2)
44729802d7cSAndreas Hindborg #define UBLK_PARAM_TYPE_ZONED (1 << 3)
448e84025d2SMing Lei #define UBLK_PARAM_TYPE_DMA_ALIGN (1 << 4)
449*ebf695f1SMing Lei #define UBLK_PARAM_TYPE_SEGMENT (1 << 5)
4500aa73170SMing Lei __u32 types; /* types of parameter included */
4510aa73170SMing Lei
4520aa73170SMing Lei struct ublk_param_basic basic;
4530aa73170SMing Lei struct ublk_param_discard discard;
454abb864d3SMing Lei struct ublk_param_devt devt;
45529802d7cSAndreas Hindborg struct ublk_param_zoned zoned;
456e84025d2SMing Lei struct ublk_param_dma_align dma;
457*ebf695f1SMing Lei struct ublk_param_segment seg;
4580aa73170SMing Lei };
4590aa73170SMing Lei
46071f28f31SMing Lei #endif
461