1607ca46eSDavid Howells #ifndef _LINUX_VIRTIO_BLK_H 2607ca46eSDavid Howells #define _LINUX_VIRTIO_BLK_H 3607ca46eSDavid Howells /* This header is BSD licensed so anyone can use the definitions to implement 4607ca46eSDavid Howells * compatible drivers/servers. 5607ca46eSDavid Howells * 6607ca46eSDavid Howells * Redistribution and use in source and binary forms, with or without 7607ca46eSDavid Howells * modification, are permitted provided that the following conditions 8607ca46eSDavid Howells * are met: 9607ca46eSDavid Howells * 1. Redistributions of source code must retain the above copyright 10607ca46eSDavid Howells * notice, this list of conditions and the following disclaimer. 11607ca46eSDavid Howells * 2. Redistributions in binary form must reproduce the above copyright 12607ca46eSDavid Howells * notice, this list of conditions and the following disclaimer in the 13607ca46eSDavid Howells * documentation and/or other materials provided with the distribution. 14607ca46eSDavid Howells * 3. Neither the name of IBM nor the names of its contributors 15607ca46eSDavid Howells * may be used to endorse or promote products derived from this software 16607ca46eSDavid Howells * without specific prior written permission. 17607ca46eSDavid Howells * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 18607ca46eSDavid Howells * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19607ca46eSDavid Howells * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20607ca46eSDavid Howells * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 21607ca46eSDavid Howells * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22607ca46eSDavid Howells * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23607ca46eSDavid Howells * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24607ca46eSDavid Howells * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25607ca46eSDavid Howells * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26607ca46eSDavid Howells * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27607ca46eSDavid Howells * SUCH DAMAGE. */ 28607ca46eSDavid Howells #include <linux/types.h> 29607ca46eSDavid Howells #include <linux/virtio_ids.h> 30607ca46eSDavid Howells #include <linux/virtio_config.h> 3119c1c5a6SMichael S. Tsirkin #include <linux/virtio_types.h> 32607ca46eSDavid Howells 33607ca46eSDavid Howells /* Feature bits */ 34607ca46eSDavid Howells #define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */ 35607ca46eSDavid Howells #define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */ 36607ca46eSDavid Howells #define VIRTIO_BLK_F_GEOMETRY 4 /* Legacy geometry available */ 37607ca46eSDavid Howells #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */ 38607ca46eSDavid Howells #define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available*/ 39607ca46eSDavid Howells #define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */ 40cb553215SMing Lei #define VIRTIO_BLK_F_MQ 12 /* support more than one vq */ 411f23816bSChangpeng Liu #define VIRTIO_BLK_F_DISCARD 13 /* DISCARD is supported */ 421f23816bSChangpeng Liu #define VIRTIO_BLK_F_WRITE_ZEROES 14 /* WRITE ZEROES is supported */ 43e60d6407SAlvaro Karsz #define VIRTIO_BLK_F_SECURE_ERASE 16 /* Secure Erase is supported */ 4495bfec41SDmitry Fomichev #define VIRTIO_BLK_F_ZONED 17 /* Zoned block device */ 45607ca46eSDavid Howells 46527100a4SRusty Russell /* Legacy feature bits */ 47527100a4SRusty Russell #ifndef VIRTIO_BLK_NO_LEGACY 48527100a4SRusty Russell #define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */ 49527100a4SRusty Russell #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */ 50592002f5SMichael S. Tsirkin #define VIRTIO_BLK_F_FLUSH 9 /* Flush command supported */ 51527100a4SRusty Russell #define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */ 52607ca46eSDavid Howells #ifndef __KERNEL__ 53592002f5SMichael S. Tsirkin /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */ 54592002f5SMichael S. Tsirkin #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH 55607ca46eSDavid Howells #endif 56527100a4SRusty Russell #endif /* !VIRTIO_BLK_NO_LEGACY */ 57607ca46eSDavid Howells 58607ca46eSDavid Howells #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */ 59607ca46eSDavid Howells 60607ca46eSDavid Howells struct virtio_blk_config { 61607ca46eSDavid Howells /* The capacity (in 512-byte sectors). */ 6240e04c48SMichael S. Tsirkin __virtio64 capacity; 63607ca46eSDavid Howells /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */ 6440e04c48SMichael S. Tsirkin __virtio32 size_max; 65607ca46eSDavid Howells /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */ 6640e04c48SMichael S. Tsirkin __virtio32 seg_max; 677e41a9deSMichael S. Tsirkin /* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */ 68607ca46eSDavid Howells struct virtio_blk_geometry { 6940e04c48SMichael S. Tsirkin __virtio16 cylinders; 70607ca46eSDavid Howells __u8 heads; 71607ca46eSDavid Howells __u8 sectors; 72607ca46eSDavid Howells } geometry; 73607ca46eSDavid Howells 74607ca46eSDavid Howells /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */ 7540e04c48SMichael S. Tsirkin __virtio32 blk_size; 76607ca46eSDavid Howells 77607ca46eSDavid Howells /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */ 78607ca46eSDavid Howells /* exponent for physical block per logical block. */ 79607ca46eSDavid Howells __u8 physical_block_exp; 80607ca46eSDavid Howells /* alignment offset in logical blocks. */ 81607ca46eSDavid Howells __u8 alignment_offset; 82607ca46eSDavid Howells /* minimum I/O size without performance penalty in logical blocks. */ 8340e04c48SMichael S. Tsirkin __virtio16 min_io_size; 84607ca46eSDavid Howells /* optimal sustained I/O size in logical blocks. */ 8540e04c48SMichael S. Tsirkin __virtio32 opt_io_size; 86607ca46eSDavid Howells 87607ca46eSDavid Howells /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */ 88607ca46eSDavid Howells __u8 wce; 89cb553215SMing Lei __u8 unused; 90cb553215SMing Lei 91cb553215SMing Lei /* number of vqs, only available when VIRTIO_BLK_F_MQ is set */ 9240e04c48SMichael S. Tsirkin __virtio16 num_queues; 931f23816bSChangpeng Liu 941f23816bSChangpeng Liu /* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */ 951f23816bSChangpeng Liu /* 961f23816bSChangpeng Liu * The maximum discard sectors (in 512-byte sectors) for 971f23816bSChangpeng Liu * one segment. 981f23816bSChangpeng Liu */ 9940e04c48SMichael S. Tsirkin __virtio32 max_discard_sectors; 1001f23816bSChangpeng Liu /* 1011f23816bSChangpeng Liu * The maximum number of discard segments in a 1021f23816bSChangpeng Liu * discard command. 1031f23816bSChangpeng Liu */ 10440e04c48SMichael S. Tsirkin __virtio32 max_discard_seg; 1051f23816bSChangpeng Liu /* Discard commands must be aligned to this number of sectors. */ 10640e04c48SMichael S. Tsirkin __virtio32 discard_sector_alignment; 1071f23816bSChangpeng Liu 1081f23816bSChangpeng Liu /* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */ 1091f23816bSChangpeng Liu /* 1101f23816bSChangpeng Liu * The maximum number of write zeroes sectors (in 512-byte sectors) in 1111f23816bSChangpeng Liu * one segment. 1121f23816bSChangpeng Liu */ 11340e04c48SMichael S. Tsirkin __virtio32 max_write_zeroes_sectors; 1141f23816bSChangpeng Liu /* 1151f23816bSChangpeng Liu * The maximum number of segments in a write zeroes 1161f23816bSChangpeng Liu * command. 1171f23816bSChangpeng Liu */ 11840e04c48SMichael S. Tsirkin __virtio32 max_write_zeroes_seg; 1191f23816bSChangpeng Liu /* 1201f23816bSChangpeng Liu * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the 1211f23816bSChangpeng Liu * deallocation of one or more of the sectors. 1221f23816bSChangpeng Liu */ 1231f23816bSChangpeng Liu __u8 write_zeroes_may_unmap; 1241f23816bSChangpeng Liu 1251f23816bSChangpeng Liu __u8 unused1[3]; 126e60d6407SAlvaro Karsz 127e60d6407SAlvaro Karsz /* the next 3 entries are guarded by VIRTIO_BLK_F_SECURE_ERASE */ 128e60d6407SAlvaro Karsz /* 129e60d6407SAlvaro Karsz * The maximum secure erase sectors (in 512-byte sectors) for 130e60d6407SAlvaro Karsz * one segment. 131e60d6407SAlvaro Karsz */ 132e60d6407SAlvaro Karsz __virtio32 max_secure_erase_sectors; 133e60d6407SAlvaro Karsz /* 134e60d6407SAlvaro Karsz * The maximum number of secure erase segments in a 135e60d6407SAlvaro Karsz * secure erase command. 136e60d6407SAlvaro Karsz */ 137e60d6407SAlvaro Karsz __virtio32 max_secure_erase_seg; 138e60d6407SAlvaro Karsz /* Secure erase commands must be aligned to this number of sectors. */ 139e60d6407SAlvaro Karsz __virtio32 secure_erase_sector_alignment; 140e60d6407SAlvaro Karsz 14195bfec41SDmitry Fomichev /* Zoned block device characteristics (if VIRTIO_BLK_F_ZONED) */ 14295bfec41SDmitry Fomichev struct virtio_blk_zoned_characteristics { 143*f1ba4e67SDmitry Fomichev __virtio32 zone_sectors; 144*f1ba4e67SDmitry Fomichev __virtio32 max_open_zones; 145*f1ba4e67SDmitry Fomichev __virtio32 max_active_zones; 146*f1ba4e67SDmitry Fomichev __virtio32 max_append_sectors; 147*f1ba4e67SDmitry Fomichev __virtio32 write_granularity; 14895bfec41SDmitry Fomichev __u8 model; 14995bfec41SDmitry Fomichev __u8 unused2[3]; 15095bfec41SDmitry Fomichev } zoned; 151607ca46eSDavid Howells } __attribute__((packed)); 152607ca46eSDavid Howells 153607ca46eSDavid Howells /* 154607ca46eSDavid Howells * Command types 155607ca46eSDavid Howells * 156607ca46eSDavid Howells * Usage is a bit tricky as some bits are used as flags and some are not. 157607ca46eSDavid Howells * 158607ca46eSDavid Howells * Rules: 159607ca46eSDavid Howells * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or 160607ca46eSDavid Howells * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own 161607ca46eSDavid Howells * and may not be combined with any of the other flags. 162607ca46eSDavid Howells */ 163607ca46eSDavid Howells 164607ca46eSDavid Howells /* These two define direction. */ 165607ca46eSDavid Howells #define VIRTIO_BLK_T_IN 0 166607ca46eSDavid Howells #define VIRTIO_BLK_T_OUT 1 167607ca46eSDavid Howells 168527100a4SRusty Russell #ifndef VIRTIO_BLK_NO_LEGACY 169607ca46eSDavid Howells /* This bit says it's a scsi command, not an actual read or write. */ 170607ca46eSDavid Howells #define VIRTIO_BLK_T_SCSI_CMD 2 171527100a4SRusty Russell #endif /* VIRTIO_BLK_NO_LEGACY */ 172607ca46eSDavid Howells 173607ca46eSDavid Howells /* Cache flush command */ 174607ca46eSDavid Howells #define VIRTIO_BLK_T_FLUSH 4 175607ca46eSDavid Howells 176607ca46eSDavid Howells /* Get device ID command */ 177607ca46eSDavid Howells #define VIRTIO_BLK_T_GET_ID 8 178607ca46eSDavid Howells 1791f23816bSChangpeng Liu /* Discard command */ 1801f23816bSChangpeng Liu #define VIRTIO_BLK_T_DISCARD 11 1811f23816bSChangpeng Liu 1821f23816bSChangpeng Liu /* Write zeroes command */ 1831f23816bSChangpeng Liu #define VIRTIO_BLK_T_WRITE_ZEROES 13 1841f23816bSChangpeng Liu 185e60d6407SAlvaro Karsz /* Secure erase command */ 186e60d6407SAlvaro Karsz #define VIRTIO_BLK_T_SECURE_ERASE 14 187e60d6407SAlvaro Karsz 18895bfec41SDmitry Fomichev /* Zone append command */ 18995bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_APPEND 15 19095bfec41SDmitry Fomichev 19195bfec41SDmitry Fomichev /* Report zones command */ 19295bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_REPORT 16 19395bfec41SDmitry Fomichev 19495bfec41SDmitry Fomichev /* Open zone command */ 19595bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_OPEN 18 19695bfec41SDmitry Fomichev 19795bfec41SDmitry Fomichev /* Close zone command */ 19895bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_CLOSE 20 19995bfec41SDmitry Fomichev 20095bfec41SDmitry Fomichev /* Finish zone command */ 20195bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_FINISH 22 20295bfec41SDmitry Fomichev 20395bfec41SDmitry Fomichev /* Reset zone command */ 20495bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_RESET 24 20595bfec41SDmitry Fomichev 20695bfec41SDmitry Fomichev /* Reset All zones command */ 20795bfec41SDmitry Fomichev #define VIRTIO_BLK_T_ZONE_RESET_ALL 26 20895bfec41SDmitry Fomichev 209527100a4SRusty Russell #ifndef VIRTIO_BLK_NO_LEGACY 210607ca46eSDavid Howells /* Barrier before this op. */ 211607ca46eSDavid Howells #define VIRTIO_BLK_T_BARRIER 0x80000000 212527100a4SRusty Russell #endif /* !VIRTIO_BLK_NO_LEGACY */ 213607ca46eSDavid Howells 2140fa2a564SMichael S. Tsirkin /* 2150fa2a564SMichael S. Tsirkin * This comes first in the read scatter-gather list. 2160fa2a564SMichael S. Tsirkin * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, 2170fa2a564SMichael S. Tsirkin * this is the first element of the read scatter-gather list. 2180fa2a564SMichael S. Tsirkin */ 219607ca46eSDavid Howells struct virtio_blk_outhdr { 220607ca46eSDavid Howells /* VIRTIO_BLK_T* */ 22119c1c5a6SMichael S. Tsirkin __virtio32 type; 222607ca46eSDavid Howells /* io priority. */ 22319c1c5a6SMichael S. Tsirkin __virtio32 ioprio; 224607ca46eSDavid Howells /* Sector (ie. 512 byte offset) */ 22519c1c5a6SMichael S. Tsirkin __virtio64 sector; 226607ca46eSDavid Howells }; 227607ca46eSDavid Howells 22895bfec41SDmitry Fomichev /* 22995bfec41SDmitry Fomichev * Supported zoned device models. 23095bfec41SDmitry Fomichev */ 23195bfec41SDmitry Fomichev 23295bfec41SDmitry Fomichev /* Regular block device */ 23395bfec41SDmitry Fomichev #define VIRTIO_BLK_Z_NONE 0 23495bfec41SDmitry Fomichev /* Host-managed zoned device */ 23595bfec41SDmitry Fomichev #define VIRTIO_BLK_Z_HM 1 23695bfec41SDmitry Fomichev /* Host-aware zoned device */ 23795bfec41SDmitry Fomichev #define VIRTIO_BLK_Z_HA 2 23895bfec41SDmitry Fomichev 23995bfec41SDmitry Fomichev /* 24095bfec41SDmitry Fomichev * Zone descriptor. A part of VIRTIO_BLK_T_ZONE_REPORT command reply. 24195bfec41SDmitry Fomichev */ 24295bfec41SDmitry Fomichev struct virtio_blk_zone_descriptor { 24395bfec41SDmitry Fomichev /* Zone capacity */ 244*f1ba4e67SDmitry Fomichev __virtio64 z_cap; 24595bfec41SDmitry Fomichev /* The starting sector of the zone */ 246*f1ba4e67SDmitry Fomichev __virtio64 z_start; 24795bfec41SDmitry Fomichev /* Zone write pointer position in sectors */ 248*f1ba4e67SDmitry Fomichev __virtio64 z_wp; 24995bfec41SDmitry Fomichev /* Zone type */ 25095bfec41SDmitry Fomichev __u8 z_type; 25195bfec41SDmitry Fomichev /* Zone state */ 25295bfec41SDmitry Fomichev __u8 z_state; 25395bfec41SDmitry Fomichev __u8 reserved[38]; 25495bfec41SDmitry Fomichev }; 25595bfec41SDmitry Fomichev 25695bfec41SDmitry Fomichev struct virtio_blk_zone_report { 257*f1ba4e67SDmitry Fomichev __virtio64 nr_zones; 25895bfec41SDmitry Fomichev __u8 reserved[56]; 25995bfec41SDmitry Fomichev struct virtio_blk_zone_descriptor zones[]; 26095bfec41SDmitry Fomichev }; 26195bfec41SDmitry Fomichev 26295bfec41SDmitry Fomichev /* 26395bfec41SDmitry Fomichev * Supported zone types. 26495bfec41SDmitry Fomichev */ 26595bfec41SDmitry Fomichev 26695bfec41SDmitry Fomichev /* Conventional zone */ 26795bfec41SDmitry Fomichev #define VIRTIO_BLK_ZT_CONV 1 26895bfec41SDmitry Fomichev /* Sequential Write Required zone */ 26995bfec41SDmitry Fomichev #define VIRTIO_BLK_ZT_SWR 2 27095bfec41SDmitry Fomichev /* Sequential Write Preferred zone */ 27195bfec41SDmitry Fomichev #define VIRTIO_BLK_ZT_SWP 3 27295bfec41SDmitry Fomichev 27395bfec41SDmitry Fomichev /* 27495bfec41SDmitry Fomichev * Zone states that are available for zones of all types. 27595bfec41SDmitry Fomichev */ 27695bfec41SDmitry Fomichev 27795bfec41SDmitry Fomichev /* Not a write pointer (conventional zones only) */ 27895bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_NOT_WP 0 27995bfec41SDmitry Fomichev /* Empty */ 28095bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_EMPTY 1 28195bfec41SDmitry Fomichev /* Implicitly Open */ 28295bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_IOPEN 2 28395bfec41SDmitry Fomichev /* Explicitly Open */ 28495bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_EOPEN 3 28595bfec41SDmitry Fomichev /* Closed */ 28695bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_CLOSED 4 28795bfec41SDmitry Fomichev /* Read-Only */ 28895bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_RDONLY 13 28995bfec41SDmitry Fomichev /* Full */ 29095bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_FULL 14 29195bfec41SDmitry Fomichev /* Offline */ 29295bfec41SDmitry Fomichev #define VIRTIO_BLK_ZS_OFFLINE 15 29395bfec41SDmitry Fomichev 2941f23816bSChangpeng Liu /* Unmap this range (only valid for write zeroes command) */ 2951f23816bSChangpeng Liu #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001 2961f23816bSChangpeng Liu 2971f23816bSChangpeng Liu /* Discard/write zeroes range for each request. */ 2981f23816bSChangpeng Liu struct virtio_blk_discard_write_zeroes { 2991f23816bSChangpeng Liu /* discard/write zeroes start sector */ 3001f23816bSChangpeng Liu __le64 sector; 3011f23816bSChangpeng Liu /* number of discard/write zeroes sectors */ 3021f23816bSChangpeng Liu __le32 num_sectors; 3031f23816bSChangpeng Liu /* flags for this range */ 3041f23816bSChangpeng Liu __le32 flags; 3051f23816bSChangpeng Liu }; 3061f23816bSChangpeng Liu 307527100a4SRusty Russell #ifndef VIRTIO_BLK_NO_LEGACY 308607ca46eSDavid Howells struct virtio_scsi_inhdr { 30919c1c5a6SMichael S. Tsirkin __virtio32 errors; 31019c1c5a6SMichael S. Tsirkin __virtio32 data_len; 31119c1c5a6SMichael S. Tsirkin __virtio32 sense_len; 31219c1c5a6SMichael S. Tsirkin __virtio32 residual; 313607ca46eSDavid Howells }; 314527100a4SRusty Russell #endif /* !VIRTIO_BLK_NO_LEGACY */ 315607ca46eSDavid Howells 316607ca46eSDavid Howells /* And this is the final byte of the write scatter-gather list. */ 317607ca46eSDavid Howells #define VIRTIO_BLK_S_OK 0 318607ca46eSDavid Howells #define VIRTIO_BLK_S_IOERR 1 319607ca46eSDavid Howells #define VIRTIO_BLK_S_UNSUPP 2 32095bfec41SDmitry Fomichev 32195bfec41SDmitry Fomichev /* Error codes that are specific to zoned block devices */ 32295bfec41SDmitry Fomichev #define VIRTIO_BLK_S_ZONE_INVALID_CMD 3 32395bfec41SDmitry Fomichev #define VIRTIO_BLK_S_ZONE_UNALIGNED_WP 4 32495bfec41SDmitry Fomichev #define VIRTIO_BLK_S_ZONE_OPEN_RESOURCE 5 32595bfec41SDmitry Fomichev #define VIRTIO_BLK_S_ZONE_ACTIVE_RESOURCE 6 32695bfec41SDmitry Fomichev 327607ca46eSDavid Howells #endif /* _LINUX_VIRTIO_BLK_H */ 328