1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * This header is BSD licensed so anyone can use the definitions to implement 5 * compatible drivers/servers. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 28 * SUCH DAMAGE. 29 */ 30 31 #ifndef _VIRTIO_BLK_H 32 #define _VIRTIO_BLK_H 33 34 /* Feature bits */ 35 #define VIRTIO_BLK_F_SIZE_MAX 0x0002 /* Indicates maximum segment size */ 36 #define VIRTIO_BLK_F_SEG_MAX 0x0004 /* Indicates maximum # of segments */ 37 #define VIRTIO_BLK_F_GEOMETRY 0x0010 /* Legacy geometry available */ 38 #define VIRTIO_BLK_F_RO 0x0020 /* Disk is read-only */ 39 #define VIRTIO_BLK_F_BLK_SIZE 0x0040 /* Block size of disk is available*/ 40 #define VIRTIO_BLK_F_FLUSH 0x0200 /* Flush command supported */ 41 #define VIRTIO_BLK_F_TOPOLOGY 0x0400 /* Topology information is available */ 42 #define VIRTIO_BLK_F_CONFIG_WCE 0x0800 /* Writeback mode available in config */ 43 #define VIRTIO_BLK_F_MQ 0x1000 /* Support more than one vq */ 44 #define VIRTIO_BLK_F_DISCARD 0x2000 /* DISCARD is supported */ 45 #define VIRTIO_BLK_F_WRITE_ZEROES 0x4000 /* WRITE ZEROES is supported */ 46 47 /* Legacy feature bits */ 48 #define VIRTIO_BLK_F_BARRIER 0x0001 /* Does host support barriers? */ 49 #define VIRTIO_BLK_F_SCSI 0x0080 /* Supports scsi command passthru */ 50 51 /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */ 52 #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH 53 54 #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */ 55 56 struct virtio_blk_config { 57 /* The capacity (in 512-byte sectors). */ 58 uint64_t capacity; 59 /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */ 60 uint32_t size_max; 61 /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */ 62 uint32_t seg_max; 63 /* Geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */ 64 struct virtio_blk_geometry { 65 uint16_t cylinders; 66 uint8_t heads; 67 uint8_t sectors; 68 } geometry; 69 70 /* Block size of device (if VIRTIO_BLK_F_BLK_SIZE) */ 71 uint32_t blk_size; 72 73 /* Topology of the device (if VIRTIO_BLK_F_TOPOLOGY) */ 74 struct virtio_blk_topology { 75 /* Exponent for physical block per logical block. */ 76 uint8_t physical_block_exp; 77 /* Alignment offset in logical blocks. */ 78 uint8_t alignment_offset; 79 /* Minimum I/O size without performance penalty in logical 80 * blocks. */ 81 uint16_t min_io_size; 82 /* Optimal sustained I/O size in logical blocks. */ 83 uint32_t opt_io_size; 84 } topology; 85 86 /* Writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */ 87 uint8_t wce; 88 uint8_t unused; 89 90 /* Number of vqs, only available when VIRTIO_BLK_F_MQ is set */ 91 uint16_t num_queues; 92 93 /* 94 * The next 3 entries are guarded by VIRTIO_BLK_F_DISCARD. 95 * 96 * The maximum discard sectors (in 512-byte sectors) for 97 * one segment. 98 */ 99 uint32_t max_discard_sectors; 100 /* 101 * The maximum number of discard segments in a 102 * discard command. 103 */ 104 uint32_t max_discard_seg; 105 /* Discard commands must be aligned to this number of sectors. */ 106 uint32_t discard_sector_alignment; 107 108 /* 109 * The next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES 110 * 111 * The maximum number of write zeroes sectors (in 512-byte sectors) in 112 * one segment. 113 */ 114 uint32_t max_write_zeroes_sectors; 115 /* 116 * The maximum number of segments in a write zeroes 117 * command. 118 */ 119 uint32_t max_write_zeroes_seg; 120 /* 121 * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the 122 * deallocation of one or more of the sectors. 123 */ 124 uint8_t write_zeroes_may_unmap; 125 126 uint8_t unused1[3]; 127 } __packed; 128 129 /* 130 * Command types 131 * 132 * Usage is a bit tricky as some bits are used as flags and some are not. 133 * 134 * Rules: 135 * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or 136 * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own 137 * and may not be combined with any of the other flags. 138 */ 139 140 /* These two define direction. */ 141 #define VIRTIO_BLK_T_IN 0 142 #define VIRTIO_BLK_T_OUT 1 143 144 /* This bit says it's a scsi command, not an actual read or write. */ 145 #define VIRTIO_BLK_T_SCSI_CMD 2 146 #define VIRTIO_BLK_T_SCSI_CMD_OUT 3 147 148 /* Cache flush command */ 149 #define VIRTIO_BLK_T_FLUSH 4 150 #define VIRTIO_BLK_T_FLUSH_OUT 5 151 152 /* Get device ID command */ 153 #define VIRTIO_BLK_T_GET_ID 8 154 155 /* Discard command */ 156 #define VIRTIO_BLK_T_DISCARD 11 157 158 /* Write zeros command */ 159 #define VIRTIO_BLK_T_WRITE_ZEROES 13 160 161 /* Barrier before this op. */ 162 #define VIRTIO_BLK_T_BARRIER 0x80000000 163 164 /* Unmap this range (only valid for write zeroes command) */ 165 #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001 166 167 /* 168 * This comes first in the read scatter-gather list. 169 * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, 170 * this is the first element of the read scatter-gather list. 171 */ 172 struct virtio_blk_outhdr { 173 /* VIRTIO_BLK_T* */ 174 uint32_t type; 175 /* io priority. */ 176 uint32_t ioprio; 177 /* Sector (ie. 512 byte offset) */ 178 uint64_t sector; 179 }; 180 181 /* Unmap this range (only valid for write zeroes command) */ 182 #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001 183 184 /* Discard/write zeroes range for each request. */ 185 struct virtio_blk_discard_write_zeroes { 186 /* Discard/write zeroes start sector */ 187 uint64_t sector; 188 /* Number of discard/write zeroes sectors */ 189 uint32_t num_sectors; 190 /* Flags for this range */ 191 uint32_t flags; 192 }; 193 194 struct virtio_scsi_inhdr { 195 uint32_t errors; 196 uint32_t data_len; 197 uint32_t sense_len; 198 uint32_t residual; 199 }; 200 201 /* And this is the final byte of the write scatter-gather list. */ 202 #define VIRTIO_BLK_S_OK 0 203 #define VIRTIO_BLK_S_IOERR 1 204 #define VIRTIO_BLK_S_UNSUPP 2 205 206 #endif /* _VIRTIO_BLK_H */ 207