1*4882a593Smuzhiyun #ifndef _LINUX_VIRTIO_BLK_H 2*4882a593Smuzhiyun #define _LINUX_VIRTIO_BLK_H 3*4882a593Smuzhiyun /* This header is BSD licensed so anyone can use the definitions to implement 4*4882a593Smuzhiyun * compatible drivers/servers. 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without 7*4882a593Smuzhiyun * modification, are permitted provided that the following conditions 8*4882a593Smuzhiyun * are met: 9*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright 10*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer. 11*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright 12*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the 13*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution. 14*4882a593Smuzhiyun * 3. Neither the name of IBM nor the names of its contributors 15*4882a593Smuzhiyun * may be used to endorse or promote products derived from this software 16*4882a593Smuzhiyun * without specific prior written permission. 17*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 18*4882a593Smuzhiyun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*4882a593Smuzhiyun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*4882a593Smuzhiyun * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 21*4882a593Smuzhiyun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*4882a593Smuzhiyun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*4882a593Smuzhiyun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*4882a593Smuzhiyun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*4882a593Smuzhiyun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*4882a593Smuzhiyun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*4882a593Smuzhiyun * SUCH DAMAGE. */ 28*4882a593Smuzhiyun #include <linux/types.h> 29*4882a593Smuzhiyun #include <linux/virtio_ids.h> 30*4882a593Smuzhiyun #include <linux/virtio_config.h> 31*4882a593Smuzhiyun #include <linux/virtio_types.h> 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun /* Feature bits */ 34*4882a593Smuzhiyun #define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */ 35*4882a593Smuzhiyun #define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */ 36*4882a593Smuzhiyun #define VIRTIO_BLK_F_GEOMETRY 4 /* Legacy geometry available */ 37*4882a593Smuzhiyun #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */ 38*4882a593Smuzhiyun #define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available*/ 39*4882a593Smuzhiyun #define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */ 40*4882a593Smuzhiyun #define VIRTIO_BLK_F_MQ 12 /* support more than one vq */ 41*4882a593Smuzhiyun #define VIRTIO_BLK_F_DISCARD 13 /* DISCARD is supported */ 42*4882a593Smuzhiyun #define VIRTIO_BLK_F_WRITE_ZEROES 14 /* WRITE ZEROES is supported */ 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun /* Legacy feature bits */ 45*4882a593Smuzhiyun #ifndef VIRTIO_BLK_NO_LEGACY 46*4882a593Smuzhiyun #define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */ 47*4882a593Smuzhiyun #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */ 48*4882a593Smuzhiyun #define VIRTIO_BLK_F_FLUSH 9 /* Flush command supported */ 49*4882a593Smuzhiyun #define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */ 50*4882a593Smuzhiyun #ifndef __KERNEL__ 51*4882a593Smuzhiyun /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */ 52*4882a593Smuzhiyun #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH 53*4882a593Smuzhiyun #endif 54*4882a593Smuzhiyun #endif /* !VIRTIO_BLK_NO_LEGACY */ 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */ 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun struct virtio_blk_config { 59*4882a593Smuzhiyun /* The capacity (in 512-byte sectors). */ 60*4882a593Smuzhiyun __virtio64 capacity; 61*4882a593Smuzhiyun /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */ 62*4882a593Smuzhiyun __virtio32 size_max; 63*4882a593Smuzhiyun /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */ 64*4882a593Smuzhiyun __virtio32 seg_max; 65*4882a593Smuzhiyun /* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */ 66*4882a593Smuzhiyun struct virtio_blk_geometry { 67*4882a593Smuzhiyun __virtio16 cylinders; 68*4882a593Smuzhiyun __u8 heads; 69*4882a593Smuzhiyun __u8 sectors; 70*4882a593Smuzhiyun } geometry; 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */ 73*4882a593Smuzhiyun __virtio32 blk_size; 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */ 76*4882a593Smuzhiyun /* exponent for physical block per logical block. */ 77*4882a593Smuzhiyun __u8 physical_block_exp; 78*4882a593Smuzhiyun /* alignment offset in logical blocks. */ 79*4882a593Smuzhiyun __u8 alignment_offset; 80*4882a593Smuzhiyun /* minimum I/O size without performance penalty in logical blocks. */ 81*4882a593Smuzhiyun __virtio16 min_io_size; 82*4882a593Smuzhiyun /* optimal sustained I/O size in logical blocks. */ 83*4882a593Smuzhiyun __virtio32 opt_io_size; 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */ 86*4882a593Smuzhiyun __u8 wce; 87*4882a593Smuzhiyun __u8 unused; 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /* number of vqs, only available when VIRTIO_BLK_F_MQ is set */ 90*4882a593Smuzhiyun __virtio16 num_queues; 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun /* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */ 93*4882a593Smuzhiyun /* 94*4882a593Smuzhiyun * The maximum discard sectors (in 512-byte sectors) for 95*4882a593Smuzhiyun * one segment. 96*4882a593Smuzhiyun */ 97*4882a593Smuzhiyun __virtio32 max_discard_sectors; 98*4882a593Smuzhiyun /* 99*4882a593Smuzhiyun * The maximum number of discard segments in a 100*4882a593Smuzhiyun * discard command. 101*4882a593Smuzhiyun */ 102*4882a593Smuzhiyun __virtio32 max_discard_seg; 103*4882a593Smuzhiyun /* Discard commands must be aligned to this number of sectors. */ 104*4882a593Smuzhiyun __virtio32 discard_sector_alignment; 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun /* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */ 107*4882a593Smuzhiyun /* 108*4882a593Smuzhiyun * The maximum number of write zeroes sectors (in 512-byte sectors) in 109*4882a593Smuzhiyun * one segment. 110*4882a593Smuzhiyun */ 111*4882a593Smuzhiyun __virtio32 max_write_zeroes_sectors; 112*4882a593Smuzhiyun /* 113*4882a593Smuzhiyun * The maximum number of segments in a write zeroes 114*4882a593Smuzhiyun * command. 115*4882a593Smuzhiyun */ 116*4882a593Smuzhiyun __virtio32 max_write_zeroes_seg; 117*4882a593Smuzhiyun /* 118*4882a593Smuzhiyun * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the 119*4882a593Smuzhiyun * deallocation of one or more of the sectors. 120*4882a593Smuzhiyun */ 121*4882a593Smuzhiyun __u8 write_zeroes_may_unmap; 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun __u8 unused1[3]; 124*4882a593Smuzhiyun } __attribute__((packed)); 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun /* 127*4882a593Smuzhiyun * Command types 128*4882a593Smuzhiyun * 129*4882a593Smuzhiyun * Usage is a bit tricky as some bits are used as flags and some are not. 130*4882a593Smuzhiyun * 131*4882a593Smuzhiyun * Rules: 132*4882a593Smuzhiyun * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or 133*4882a593Smuzhiyun * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own 134*4882a593Smuzhiyun * and may not be combined with any of the other flags. 135*4882a593Smuzhiyun */ 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun /* These two define direction. */ 138*4882a593Smuzhiyun #define VIRTIO_BLK_T_IN 0 139*4882a593Smuzhiyun #define VIRTIO_BLK_T_OUT 1 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun #ifndef VIRTIO_BLK_NO_LEGACY 142*4882a593Smuzhiyun /* This bit says it's a scsi command, not an actual read or write. */ 143*4882a593Smuzhiyun #define VIRTIO_BLK_T_SCSI_CMD 2 144*4882a593Smuzhiyun #endif /* VIRTIO_BLK_NO_LEGACY */ 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun /* Cache flush command */ 147*4882a593Smuzhiyun #define VIRTIO_BLK_T_FLUSH 4 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun /* Get device ID command */ 150*4882a593Smuzhiyun #define VIRTIO_BLK_T_GET_ID 8 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun /* Discard command */ 153*4882a593Smuzhiyun #define VIRTIO_BLK_T_DISCARD 11 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun /* Write zeroes command */ 156*4882a593Smuzhiyun #define VIRTIO_BLK_T_WRITE_ZEROES 13 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun #ifndef VIRTIO_BLK_NO_LEGACY 159*4882a593Smuzhiyun /* Barrier before this op. */ 160*4882a593Smuzhiyun #define VIRTIO_BLK_T_BARRIER 0x80000000 161*4882a593Smuzhiyun #endif /* !VIRTIO_BLK_NO_LEGACY */ 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun /* 164*4882a593Smuzhiyun * This comes first in the read scatter-gather list. 165*4882a593Smuzhiyun * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, 166*4882a593Smuzhiyun * this is the first element of the read scatter-gather list. 167*4882a593Smuzhiyun */ 168*4882a593Smuzhiyun struct virtio_blk_outhdr { 169*4882a593Smuzhiyun /* VIRTIO_BLK_T* */ 170*4882a593Smuzhiyun __virtio32 type; 171*4882a593Smuzhiyun /* io priority. */ 172*4882a593Smuzhiyun __virtio32 ioprio; 173*4882a593Smuzhiyun /* Sector (ie. 512 byte offset) */ 174*4882a593Smuzhiyun __virtio64 sector; 175*4882a593Smuzhiyun }; 176*4882a593Smuzhiyun 177*4882a593Smuzhiyun /* Unmap this range (only valid for write zeroes command) */ 178*4882a593Smuzhiyun #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001 179*4882a593Smuzhiyun 180*4882a593Smuzhiyun /* Discard/write zeroes range for each request. */ 181*4882a593Smuzhiyun struct virtio_blk_discard_write_zeroes { 182*4882a593Smuzhiyun /* discard/write zeroes start sector */ 183*4882a593Smuzhiyun __le64 sector; 184*4882a593Smuzhiyun /* number of discard/write zeroes sectors */ 185*4882a593Smuzhiyun __le32 num_sectors; 186*4882a593Smuzhiyun /* flags for this range */ 187*4882a593Smuzhiyun __le32 flags; 188*4882a593Smuzhiyun }; 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun #ifndef VIRTIO_BLK_NO_LEGACY 191*4882a593Smuzhiyun struct virtio_scsi_inhdr { 192*4882a593Smuzhiyun __virtio32 errors; 193*4882a593Smuzhiyun __virtio32 data_len; 194*4882a593Smuzhiyun __virtio32 sense_len; 195*4882a593Smuzhiyun __virtio32 residual; 196*4882a593Smuzhiyun }; 197*4882a593Smuzhiyun #endif /* !VIRTIO_BLK_NO_LEGACY */ 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun /* And this is the final byte of the write scatter-gather list. */ 200*4882a593Smuzhiyun #define VIRTIO_BLK_S_OK 0 201*4882a593Smuzhiyun #define VIRTIO_BLK_S_IOERR 1 202*4882a593Smuzhiyun #define VIRTIO_BLK_S_UNSUPP 2 203*4882a593Smuzhiyun #endif /* _LINUX_VIRTIO_BLK_H */ 204