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 */
41caa1ee43SChangpeng Liu #define VIRTIO_BLK_F_DISCARD	13	/* DISCARD is supported */
42caa1ee43SChangpeng Liu #define VIRTIO_BLK_F_WRITE_ZEROES	14	/* WRITE ZEROES is supported */
439fbe302bSMichael S. Tsirkin 
449fbe302bSMichael S. Tsirkin /* Legacy feature bits */
459fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
469fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_BARRIER	0	/* Does host support barriers? */
479fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_SCSI	7	/* Supports scsi command passthru */
48b89485a5SPaolo Bonzini #define VIRTIO_BLK_F_FLUSH	9	/* Flush command supported */
499fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_CONFIG_WCE	11	/* Writeback mode available in config */
50b89485a5SPaolo Bonzini /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */
51b89485a5SPaolo Bonzini #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH
529fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
539fbe302bSMichael S. Tsirkin 
549fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
559fbe302bSMichael S. Tsirkin 
569fbe302bSMichael S. Tsirkin struct virtio_blk_config {
579fbe302bSMichael S. Tsirkin 	/* The capacity (in 512-byte sectors). */
58*e6546342SJason Wang 	__virtio64 capacity;
599fbe302bSMichael S. Tsirkin 	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
60*e6546342SJason Wang 	__virtio32 size_max;
619fbe302bSMichael S. Tsirkin 	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
62*e6546342SJason Wang 	__virtio32 seg_max;
634eb867e9SMichael S. Tsirkin 	/* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
649fbe302bSMichael S. Tsirkin 	struct virtio_blk_geometry {
65*e6546342SJason Wang 		__virtio16 cylinders;
669fbe302bSMichael S. Tsirkin 		uint8_t heads;
679fbe302bSMichael S. Tsirkin 		uint8_t sectors;
689fbe302bSMichael S. Tsirkin 	} geometry;
699fbe302bSMichael S. Tsirkin 
709fbe302bSMichael S. Tsirkin 	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
71*e6546342SJason Wang 	__virtio32 blk_size;
729fbe302bSMichael S. Tsirkin 
739fbe302bSMichael S. Tsirkin 	/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
749fbe302bSMichael S. Tsirkin 	/* exponent for physical block per logical block. */
759fbe302bSMichael S. Tsirkin 	uint8_t physical_block_exp;
769fbe302bSMichael S. Tsirkin 	/* alignment offset in logical blocks. */
779fbe302bSMichael S. Tsirkin 	uint8_t alignment_offset;
789fbe302bSMichael S. Tsirkin 	/* minimum I/O size without performance penalty in logical blocks. */
79*e6546342SJason Wang 	__virtio16 min_io_size;
809fbe302bSMichael S. Tsirkin 	/* optimal sustained I/O size in logical blocks. */
81*e6546342SJason Wang 	__virtio32 opt_io_size;
829fbe302bSMichael S. Tsirkin 
839fbe302bSMichael S. Tsirkin 	/* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
849fbe302bSMichael S. Tsirkin 	uint8_t wce;
859fbe302bSMichael S. Tsirkin 	uint8_t unused;
869fbe302bSMichael S. Tsirkin 
879fbe302bSMichael S. Tsirkin 	/* number of vqs, only available when VIRTIO_BLK_F_MQ is set */
88*e6546342SJason Wang 	__virtio16 num_queues;
89caa1ee43SChangpeng Liu 
90caa1ee43SChangpeng Liu 	/* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */
91caa1ee43SChangpeng Liu 	/*
92caa1ee43SChangpeng Liu 	 * The maximum discard sectors (in 512-byte sectors) for
93caa1ee43SChangpeng Liu 	 * one segment.
94caa1ee43SChangpeng Liu 	 */
95*e6546342SJason Wang 	__virtio32 max_discard_sectors;
96caa1ee43SChangpeng Liu 	/*
97caa1ee43SChangpeng Liu 	 * The maximum number of discard segments in a
98caa1ee43SChangpeng Liu 	 * discard command.
99caa1ee43SChangpeng Liu 	 */
100*e6546342SJason Wang 	__virtio32 max_discard_seg;
101caa1ee43SChangpeng Liu 	/* Discard commands must be aligned to this number of sectors. */
102*e6546342SJason Wang 	__virtio32 discard_sector_alignment;
103caa1ee43SChangpeng Liu 
104caa1ee43SChangpeng Liu 	/* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */
105caa1ee43SChangpeng Liu 	/*
106caa1ee43SChangpeng Liu 	 * The maximum number of write zeroes sectors (in 512-byte sectors) in
107caa1ee43SChangpeng Liu 	 * one segment.
108caa1ee43SChangpeng Liu 	 */
109*e6546342SJason Wang 	__virtio32 max_write_zeroes_sectors;
110caa1ee43SChangpeng Liu 	/*
111caa1ee43SChangpeng Liu 	 * The maximum number of segments in a write zeroes
112caa1ee43SChangpeng Liu 	 * command.
113caa1ee43SChangpeng Liu 	 */
114*e6546342SJason Wang 	__virtio32 max_write_zeroes_seg;
115caa1ee43SChangpeng Liu 	/*
116caa1ee43SChangpeng Liu 	 * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the
117caa1ee43SChangpeng Liu 	 * deallocation of one or more of the sectors.
118caa1ee43SChangpeng Liu 	 */
119caa1ee43SChangpeng Liu 	uint8_t write_zeroes_may_unmap;
120caa1ee43SChangpeng Liu 
121caa1ee43SChangpeng Liu 	uint8_t unused1[3];
1229fbe302bSMichael S. Tsirkin } QEMU_PACKED;
1239fbe302bSMichael S. Tsirkin 
1249fbe302bSMichael S. Tsirkin /*
1259fbe302bSMichael S. Tsirkin  * Command types
1269fbe302bSMichael S. Tsirkin  *
1279fbe302bSMichael S. Tsirkin  * Usage is a bit tricky as some bits are used as flags and some are not.
1289fbe302bSMichael S. Tsirkin  *
1299fbe302bSMichael S. Tsirkin  * Rules:
1309fbe302bSMichael S. Tsirkin  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
1319fbe302bSMichael S. Tsirkin  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
1329fbe302bSMichael S. Tsirkin  *   and may not be combined with any of the other flags.
1339fbe302bSMichael S. Tsirkin  */
1349fbe302bSMichael S. Tsirkin 
1359fbe302bSMichael S. Tsirkin /* These two define direction. */
1369fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_IN		0
1379fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_OUT	1
1389fbe302bSMichael S. Tsirkin 
1399fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1409fbe302bSMichael S. Tsirkin /* This bit says it's a scsi command, not an actual read or write. */
1419fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_SCSI_CMD	2
1429fbe302bSMichael S. Tsirkin #endif /* VIRTIO_BLK_NO_LEGACY */
1439fbe302bSMichael S. Tsirkin 
1449fbe302bSMichael S. Tsirkin /* Cache flush command */
1459fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_FLUSH	4
1469fbe302bSMichael S. Tsirkin 
1479fbe302bSMichael S. Tsirkin /* Get device ID command */
1489fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_GET_ID    8
1499fbe302bSMichael S. Tsirkin 
150da054c64SPaolo Bonzini /* Discard command */
151da054c64SPaolo Bonzini #define VIRTIO_BLK_T_DISCARD	11
152da054c64SPaolo Bonzini 
153da054c64SPaolo Bonzini /* Write zeroes command */
154da054c64SPaolo Bonzini #define VIRTIO_BLK_T_WRITE_ZEROES	13
155da054c64SPaolo Bonzini 
1569fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1579fbe302bSMichael S. Tsirkin /* Barrier before this op. */
1589fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_BARRIER	0x80000000
1599fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
1609fbe302bSMichael S. Tsirkin 
1614eb867e9SMichael S. Tsirkin /*
1624eb867e9SMichael S. Tsirkin  * This comes first in the read scatter-gather list.
1634eb867e9SMichael S. Tsirkin  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
1644eb867e9SMichael S. Tsirkin  * this is the first element of the read scatter-gather list.
1654eb867e9SMichael S. Tsirkin  */
1669fbe302bSMichael S. Tsirkin struct virtio_blk_outhdr {
1679fbe302bSMichael S. Tsirkin 	/* VIRTIO_BLK_T* */
1689fbe302bSMichael S. Tsirkin 	__virtio32 type;
1699fbe302bSMichael S. Tsirkin 	/* io priority. */
1709fbe302bSMichael S. Tsirkin 	__virtio32 ioprio;
1719fbe302bSMichael S. Tsirkin 	/* Sector (ie. 512 byte offset) */
1729fbe302bSMichael S. Tsirkin 	__virtio64 sector;
1739fbe302bSMichael S. Tsirkin };
1749fbe302bSMichael S. Tsirkin 
175caa1ee43SChangpeng Liu /* Unmap this range (only valid for write zeroes command) */
176caa1ee43SChangpeng Liu #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP	0x00000001
177caa1ee43SChangpeng Liu 
178caa1ee43SChangpeng Liu /* Discard/write zeroes range for each request. */
179caa1ee43SChangpeng Liu struct virtio_blk_discard_write_zeroes {
180caa1ee43SChangpeng Liu 	/* discard/write zeroes start sector */
181caa1ee43SChangpeng Liu 	uint64_t sector;
182caa1ee43SChangpeng Liu 	/* number of discard/write zeroes sectors */
183caa1ee43SChangpeng Liu 	uint32_t num_sectors;
184caa1ee43SChangpeng Liu 	/* flags for this range */
185caa1ee43SChangpeng Liu 	uint32_t flags;
186caa1ee43SChangpeng Liu };
187caa1ee43SChangpeng Liu 
1889fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1899fbe302bSMichael S. Tsirkin struct virtio_scsi_inhdr {
1909fbe302bSMichael S. Tsirkin 	__virtio32 errors;
1919fbe302bSMichael S. Tsirkin 	__virtio32 data_len;
1929fbe302bSMichael S. Tsirkin 	__virtio32 sense_len;
1939fbe302bSMichael S. Tsirkin 	__virtio32 residual;
1949fbe302bSMichael S. Tsirkin };
1959fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
1969fbe302bSMichael S. Tsirkin 
1979fbe302bSMichael S. Tsirkin /* And this is the final byte of the write scatter-gather list. */
1989fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_OK		0
1999fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_IOERR	1
2009fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_UNSUPP	2
2019fbe302bSMichael S. Tsirkin #endif /* _LINUX_VIRTIO_BLK_H */
202