2 * SPDX-License-Identifier: BSD-3-Clause
4 * This header is BSD licensed so anyone can use the definitions to implement
5 * compatible drivers/servers.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of IBM nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define VIRTIO_BLK_F_SIZE_MAX 0x0002 /* Indicates maximum segment size */
38 #define VIRTIO_BLK_F_SEG_MAX 0x0004 /* Indicates maximum # of segments */
39 #define VIRTIO_BLK_F_GEOMETRY 0x0010 /* Legacy geometry available */
40 #define VIRTIO_BLK_F_RO 0x0020 /* Disk is read-only */
41 #define VIRTIO_BLK_F_BLK_SIZE 0x0040 /* Block size of disk is available*/
42 #define VIRTIO_BLK_F_FLUSH 0x0200 /* Flush command supported */
43 #define VIRTIO_BLK_F_TOPOLOGY 0x0400 /* Topology information is available */
44 #define VIRTIO_BLK_F_CONFIG_WCE 0x0800 /* Writeback mode available in config */
45 #define VIRTIO_BLK_F_MQ 0x1000 /* Support more than one vq */
46 #define VIRTIO_BLK_F_DISCARD 0x2000 /* DISCARD is supported */
47 #define VIRTIO_BLK_F_WRITE_ZEROES 0x4000 /* WRITE ZEROES is supported */
49 /* Legacy feature bits */
50 #define VIRTIO_BLK_F_BARRIER 0x0001 /* Does host support barriers? */
51 #define VIRTIO_BLK_F_SCSI 0x0080 /* Supports scsi command passthru */
53 /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */
54 #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH
56 #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */
58 struct virtio_blk_config {
59 /* The capacity (in 512-byte sectors). */
61 /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
63 /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
65 /* Geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
66 struct virtio_blk_geometry {
72 /* Block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
75 /* Topology of the device (if VIRTIO_BLK_F_TOPOLOGY) */
76 struct virtio_blk_topology {
77 /* Exponent for physical block per logical block. */
78 uint8_t physical_block_exp;
79 /* Alignment offset in logical blocks. */
80 uint8_t alignment_offset;
81 /* Minimum I/O size without performance penalty in logical
84 /* Optimal sustained I/O size in logical blocks. */
88 /* Writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
92 /* Number of vqs, only available when VIRTIO_BLK_F_MQ is set */
96 * The next 3 entries are guarded by VIRTIO_BLK_F_DISCARD.
98 * The maximum discard sectors (in 512-byte sectors) for
101 uint32_t max_discard_sectors;
103 * The maximum number of discard segments in a
106 uint32_t max_discard_seg;
107 /* Discard commands must be aligned to this number of sectors. */
108 uint32_t discard_sector_alignment;
111 * The next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES
113 * The maximum number of write zeroes sectors (in 512-byte sectors) in
116 uint32_t max_write_zeroes_sectors;
118 * The maximum number of segments in a write zeroes
121 uint32_t max_write_zeroes_seg;
123 * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the
124 * deallocation of one or more of the sectors.
126 uint8_t write_zeroes_may_unmap;
134 * Usage is a bit tricky as some bits are used as flags and some are not.
137 * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
138 * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own
139 * and may not be combined with any of the other flags.
142 /* These two define direction. */
143 #define VIRTIO_BLK_T_IN 0
144 #define VIRTIO_BLK_T_OUT 1
146 /* This bit says it's a scsi command, not an actual read or write. */
147 #define VIRTIO_BLK_T_SCSI_CMD 2
148 #define VIRTIO_BLK_T_SCSI_CMD_OUT 3
150 /* Cache flush command */
151 #define VIRTIO_BLK_T_FLUSH 4
152 #define VIRTIO_BLK_T_FLUSH_OUT 5
154 /* Get device ID command */
155 #define VIRTIO_BLK_T_GET_ID 8
157 /* Discard command */
158 #define VIRTIO_BLK_T_DISCARD 11
160 /* Write zeros command */
161 #define VIRTIO_BLK_T_WRITE_ZEROES 13
163 /* Barrier before this op. */
164 #define VIRTIO_BLK_T_BARRIER 0x80000000
166 /* Unmap this range (only valid for write zeroes command) */
167 #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001
170 * This comes first in the read scatter-gather list.
171 * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
172 * this is the first element of the read scatter-gather list.
174 struct virtio_blk_outhdr {
179 /* Sector (ie. 512 byte offset) */
183 /* Unmap this range (only valid for write zeroes command) */
184 #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001
186 /* Discard/write zeroes range for each request. */
187 struct virtio_blk_discard_write_zeroes {
188 /* Discard/write zeroes start sector */
190 /* Number of discard/write zeroes sectors */
191 uint32_t num_sectors;
192 /* Flags for this range */
196 struct virtio_scsi_inhdr {
203 /* And this is the final byte of the write scatter-gather list. */
204 #define VIRTIO_BLK_S_OK 0
205 #define VIRTIO_BLK_S_IOERR 1
206 #define VIRTIO_BLK_S_UNSUPP 2
208 #endif /* _VIRTIO_BLK_H */