1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4  * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5  *
6  * From Linux kernel include/uapi/linux/virtio_blk.h
7  */
8 
9 #ifndef _LINUX_VIRTIO_BLK_H
10 #define _LINUX_VIRTIO_BLK_H
11 
12 /* Feature bits */
13 #define VIRTIO_BLK_F_SIZE_MAX	1	/* Indicates maximum segment size */
14 #define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
15 #define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available */
16 #define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
17 #define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available */
18 #define VIRTIO_BLK_F_TOPOLOGY	10	/* Topology information is available */
19 #define VIRTIO_BLK_F_MQ		12	/* Support more than one vq */
20 #define VIRTIO_BLK_F_DISCARD	13	/* Discard is supported */
21 #define VIRTIO_BLK_F_WRITE_ZEROES	14	/* Write zeroes is supported */
22 
23 /* Legacy feature bits */
24 #ifndef VIRTIO_BLK_NO_LEGACY
25 #define VIRTIO_BLK_F_BARRIER	0	/* Does host support barriers? */
26 #define VIRTIO_BLK_F_SCSI	7	/* Supports scsi command passthru */
27 #define VIRTIO_BLK_F_FLUSH	9	/* Flush command supported */
28 #define VIRTIO_BLK_F_CONFIG_WCE	11	/* Writeback mode available in config */
29 #ifndef __KERNEL__
30 /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH */
31 #define VIRTIO_BLK_F_WCE	VIRTIO_BLK_F_FLUSH
32 #endif
33 #endif /* !VIRTIO_BLK_NO_LEGACY */
34 
35 #define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
36 
37 struct __packed virtio_blk_config {
38 	/* The capacity (in 512-byte sectors) */
39 	__u64 capacity;
40 	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
41 	__u32 size_max;
42 	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
43 	__u32 seg_max;
44 	/* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
45 	struct virtio_blk_geometry {
46 		__u16 cylinders;
47 		__u8 heads;
48 		__u8 sectors;
49 	} geometry;
50 
51 	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
52 	__u32 blk_size;
53 
54 	/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */
55 	/* exponent for physical block per logical block */
56 	__u8 physical_block_exp;
57 	/* alignment offset in logical blocks */
58 	__u8 alignment_offset;
59 	/* minimum I/O size without performance penalty in logical blocks */
60 	__u16 min_io_size;
61 	/* optimal sustained I/O size in logical blocks */
62 	__u32 opt_io_size;
63 
64 	/* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
65 	__u8 wce;
66 	__u8 unused;
67 
68 	/* number of vqs, only available when VIRTIO_BLK_F_MQ is set */
69 	__u16 num_queues;
70 
71 	/* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */
72 	/*
73 	 * The maximum discard sectors (in 512-byte sectors) for
74 	 * one segment.
75 	 */
76 	__u32 max_discard_sectors;
77 	/*
78 	 * The maximum number of discard segments in a
79 	 * discard command.
80 	 */
81 	__u32 max_discard_seg;
82 	/* Discard commands must be aligned to this number of sectors. */
83 	__u32 discard_sector_alignment;
84 
85 	/* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */
86 	/*
87 	 * The maximum number of write zeroes sectors (in 512-byte sectors) in
88 	 * one segment.
89 	 */
90 	__u32 max_write_zeroes_sectors;
91 	/*
92 	 * The maximum number of segments in a write zeroes
93 	 * command.
94 	 */
95 	__u32 max_write_zeroes_seg;
96 	/*
97 	 * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the
98 	 * deallocation of one or more of the sectors.
99 	 */
100 	__u8 write_zeroes_may_unmap;
101 
102 	__u8 unused1[3];
103 };
104 
105 /*
106  * Command types
107  *
108  * Usage is a bit tricky as some bits are used as flags and some are not.
109  *
110  * Rules:
111  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
112  *   VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own
113  *   and may not be combined with any of the other flags.
114  */
115 
116 /* These two define direction */
117 #define VIRTIO_BLK_T_IN		0
118 #define VIRTIO_BLK_T_OUT	1
119 
120 #ifndef VIRTIO_BLK_NO_LEGACY
121 /* This bit says it's a scsi command, not an actual read or write */
122 #define VIRTIO_BLK_T_SCSI_CMD	2
123 #endif /* VIRTIO_BLK_NO_LEGACY */
124 
125 /* Cache flush command */
126 #define VIRTIO_BLK_T_FLUSH	4
127 
128 /* Get device ID command */
129 #define VIRTIO_BLK_T_GET_ID	8
130 
131 /* Write zeroes command */
132 #define VIRTIO_BLK_T_WRITE_ZEROES 13
133 
134 #ifndef VIRTIO_BLK_NO_LEGACY
135 /* Barrier before this op */
136 #define VIRTIO_BLK_T_BARRIER	0x80000000
137 #endif /* !VIRTIO_BLK_NO_LEGACY */
138 
139 /*
140  * This comes first in the read scatter-gather list.
141  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
142  * this is the first element of the read scatter-gather list.
143  */
144 struct virtio_blk_outhdr {
145 	/* VIRTIO_BLK_T* */
146 	__virtio32 type;
147 	/* io priority */
148 	__virtio32 ioprio;
149 	/* Sector (ie. 512 byte offset) */
150 	__virtio64 sector;
151 };
152 
153 struct virtio_blk_discard_write_zeroes {
154 	/* discard/write zeroes start sector */
155 	__virtio64 sector;
156 	/* number of discard/write zeroes sectors */
157 	__virtio32 num_sectors;
158 	/* flags for this range */
159 	__virtio32 flags;
160 };
161 
162 #ifndef VIRTIO_BLK_NO_LEGACY
163 struct virtio_scsi_inhdr {
164 	__virtio32 errors;
165 	__virtio32 data_len;
166 	__virtio32 sense_len;
167 	__virtio32 residual;
168 };
169 #endif /* !VIRTIO_BLK_NO_LEGACY */
170 
171 /* And this is the final byte of the write scatter-gather list */
172 #define VIRTIO_BLK_S_OK		0
173 #define VIRTIO_BLK_S_IOERR	1
174 #define VIRTIO_BLK_S_UNSUPP	2
175 
176 #endif /* _LINUX_VIRTIO_BLK_H */
177