19fbe302bSMichael S. Tsirkin #ifndef _LINUX_VIRTIO_BLK_H
29fbe302bSMichael S. Tsirkin #define _LINUX_VIRTIO_BLK_H
39fbe302bSMichael S. Tsirkin /* This header is BSD licensed so anyone can use the definitions to implement
49fbe302bSMichael S. Tsirkin  * compatible drivers/servers.
59fbe302bSMichael S. Tsirkin  *
69fbe302bSMichael S. Tsirkin  * Redistribution and use in source and binary forms, with or without
79fbe302bSMichael S. Tsirkin  * modification, are permitted provided that the following conditions
89fbe302bSMichael S. Tsirkin  * are met:
99fbe302bSMichael S. Tsirkin  * 1. Redistributions of source code must retain the above copyright
109fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer.
119fbe302bSMichael S. Tsirkin  * 2. Redistributions in binary form must reproduce the above copyright
129fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer in the
139fbe302bSMichael S. Tsirkin  *    documentation and/or other materials provided with the distribution.
149fbe302bSMichael S. Tsirkin  * 3. Neither the name of IBM nor the names of its contributors
159fbe302bSMichael S. Tsirkin  *    may be used to endorse or promote products derived from this software
169fbe302bSMichael S. Tsirkin  *    without specific prior written permission.
179fbe302bSMichael S. Tsirkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
189fbe302bSMichael S. Tsirkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199fbe302bSMichael S. Tsirkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209fbe302bSMichael S. Tsirkin  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
219fbe302bSMichael S. Tsirkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229fbe302bSMichael S. Tsirkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239fbe302bSMichael S. Tsirkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249fbe302bSMichael S. Tsirkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259fbe302bSMichael S. Tsirkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269fbe302bSMichael S. Tsirkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279fbe302bSMichael S. Tsirkin  * SUCH DAMAGE. */
289fbe302bSMichael S. Tsirkin #include "standard-headers/linux/types.h"
299fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_ids.h"
309fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_config.h"
319fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_types.h"
329fbe302bSMichael S. Tsirkin 
339fbe302bSMichael S. Tsirkin /* Feature bits */
349fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_SIZE_MAX	1	/* Indicates maximum segment size */
359fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
369fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available  */
379fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
389fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available*/
399fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_TOPOLOGY	10	/* Topology information is available */
409fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_MQ		12	/* support more than one vq */
419fbe302bSMichael S. Tsirkin 
429fbe302bSMichael S. Tsirkin /* Legacy feature bits */
439fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
449fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_BARRIER	0	/* Does host support barriers? */
459fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_SCSI	7	/* Supports scsi command passthru */
46b89485a5SPaolo Bonzini #define VIRTIO_BLK_F_FLUSH	9	/* Flush command supported */
479fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_CONFIG_WCE	11	/* Writeback mode available in config */
48b89485a5SPaolo Bonzini /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */
49b89485a5SPaolo Bonzini #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH
509fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
519fbe302bSMichael S. Tsirkin 
529fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
539fbe302bSMichael S. Tsirkin 
549fbe302bSMichael S. Tsirkin struct virtio_blk_config {
559fbe302bSMichael S. Tsirkin 	/* The capacity (in 512-byte sectors). */
569fbe302bSMichael S. Tsirkin 	uint64_t capacity;
579fbe302bSMichael S. Tsirkin 	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
589fbe302bSMichael S. Tsirkin 	uint32_t size_max;
599fbe302bSMichael S. Tsirkin 	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
609fbe302bSMichael S. Tsirkin 	uint32_t seg_max;
614eb867e9SMichael S. Tsirkin 	/* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
629fbe302bSMichael S. Tsirkin 	struct virtio_blk_geometry {
639fbe302bSMichael S. Tsirkin 		uint16_t cylinders;
649fbe302bSMichael S. Tsirkin 		uint8_t heads;
659fbe302bSMichael S. Tsirkin 		uint8_t sectors;
669fbe302bSMichael S. Tsirkin 	} geometry;
679fbe302bSMichael S. Tsirkin 
689fbe302bSMichael S. Tsirkin 	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
699fbe302bSMichael S. Tsirkin 	uint32_t blk_size;
709fbe302bSMichael S. Tsirkin 
719fbe302bSMichael S. Tsirkin 	/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
729fbe302bSMichael S. Tsirkin 	/* exponent for physical block per logical block. */
739fbe302bSMichael S. Tsirkin 	uint8_t physical_block_exp;
749fbe302bSMichael S. Tsirkin 	/* alignment offset in logical blocks. */
759fbe302bSMichael S. Tsirkin 	uint8_t alignment_offset;
769fbe302bSMichael S. Tsirkin 	/* minimum I/O size without performance penalty in logical blocks. */
779fbe302bSMichael S. Tsirkin 	uint16_t min_io_size;
789fbe302bSMichael S. Tsirkin 	/* optimal sustained I/O size in logical blocks. */
799fbe302bSMichael S. Tsirkin 	uint32_t opt_io_size;
809fbe302bSMichael S. Tsirkin 
819fbe302bSMichael S. Tsirkin 	/* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
829fbe302bSMichael S. Tsirkin 	uint8_t wce;
839fbe302bSMichael S. Tsirkin 	uint8_t unused;
849fbe302bSMichael S. Tsirkin 
859fbe302bSMichael S. Tsirkin 	/* number of vqs, only available when VIRTIO_BLK_F_MQ is set */
869fbe302bSMichael S. Tsirkin 	uint16_t num_queues;
879fbe302bSMichael S. Tsirkin } QEMU_PACKED;
889fbe302bSMichael S. Tsirkin 
899fbe302bSMichael S. Tsirkin /*
909fbe302bSMichael S. Tsirkin  * Command types
919fbe302bSMichael S. Tsirkin  *
929fbe302bSMichael S. Tsirkin  * Usage is a bit tricky as some bits are used as flags and some are not.
939fbe302bSMichael S. Tsirkin  *
949fbe302bSMichael S. Tsirkin  * Rules:
959fbe302bSMichael S. Tsirkin  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
969fbe302bSMichael S. Tsirkin  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
979fbe302bSMichael S. Tsirkin  *   and may not be combined with any of the other flags.
989fbe302bSMichael S. Tsirkin  */
999fbe302bSMichael S. Tsirkin 
1009fbe302bSMichael S. Tsirkin /* These two define direction. */
1019fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_IN		0
1029fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_OUT	1
1039fbe302bSMichael S. Tsirkin 
1049fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1059fbe302bSMichael S. Tsirkin /* This bit says it's a scsi command, not an actual read or write. */
1069fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_SCSI_CMD	2
1079fbe302bSMichael S. Tsirkin #endif /* VIRTIO_BLK_NO_LEGACY */
1089fbe302bSMichael S. Tsirkin 
1099fbe302bSMichael S. Tsirkin /* Cache flush command */
1109fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_FLUSH	4
1119fbe302bSMichael S. Tsirkin 
1129fbe302bSMichael S. Tsirkin /* Get device ID command */
1139fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_GET_ID    8
1149fbe302bSMichael S. Tsirkin 
115*da054c64SPaolo Bonzini /* Discard command */
116*da054c64SPaolo Bonzini #define VIRTIO_BLK_T_DISCARD	11
117*da054c64SPaolo Bonzini 
118*da054c64SPaolo Bonzini /* Write zeroes command */
119*da054c64SPaolo Bonzini #define VIRTIO_BLK_T_WRITE_ZEROES	13
120*da054c64SPaolo Bonzini 
1219fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1229fbe302bSMichael S. Tsirkin /* Barrier before this op. */
1239fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_BARRIER	0x80000000
1249fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
1259fbe302bSMichael S. Tsirkin 
1264eb867e9SMichael S. Tsirkin /*
1274eb867e9SMichael S. Tsirkin  * This comes first in the read scatter-gather list.
1284eb867e9SMichael S. Tsirkin  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
1294eb867e9SMichael S. Tsirkin  * this is the first element of the read scatter-gather list.
1304eb867e9SMichael S. Tsirkin  */
1319fbe302bSMichael S. Tsirkin struct virtio_blk_outhdr {
1329fbe302bSMichael S. Tsirkin 	/* VIRTIO_BLK_T* */
1339fbe302bSMichael S. Tsirkin 	__virtio32 type;
1349fbe302bSMichael S. Tsirkin 	/* io priority. */
1359fbe302bSMichael S. Tsirkin 	__virtio32 ioprio;
1369fbe302bSMichael S. Tsirkin 	/* Sector (ie. 512 byte offset) */
1379fbe302bSMichael S. Tsirkin 	__virtio64 sector;
1389fbe302bSMichael S. Tsirkin };
1399fbe302bSMichael S. Tsirkin 
1409fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1419fbe302bSMichael S. Tsirkin struct virtio_scsi_inhdr {
1429fbe302bSMichael S. Tsirkin 	__virtio32 errors;
1439fbe302bSMichael S. Tsirkin 	__virtio32 data_len;
1449fbe302bSMichael S. Tsirkin 	__virtio32 sense_len;
1459fbe302bSMichael S. Tsirkin 	__virtio32 residual;
1469fbe302bSMichael S. Tsirkin };
1479fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
1489fbe302bSMichael S. Tsirkin 
1499fbe302bSMichael S. Tsirkin /* And this is the final byte of the write scatter-gather list. */
1509fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_OK		0
1519fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_IOERR	1
1529fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_UNSUPP	2
1539fbe302bSMichael S. Tsirkin #endif /* _LINUX_VIRTIO_BLK_H */
154