xref: /openbmc/qemu/include/standard-headers/linux/virtio_blk.h (revision 93e0932b7be2498024cd6ba8446a0fa2cb1769bc)
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 */
43*93e0932bSPeter Xu #define VIRTIO_BLK_F_SECURE_ERASE	16 /* Secure Erase is supported */
449fbe302bSMichael S. Tsirkin 
459fbe302bSMichael S. Tsirkin /* Legacy feature bits */
469fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
479fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_BARRIER	0	/* Does host support barriers? */
489fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_SCSI	7	/* Supports scsi command passthru */
49b89485a5SPaolo Bonzini #define VIRTIO_BLK_F_FLUSH	9	/* Flush command supported */
509fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_F_CONFIG_WCE	11	/* Writeback mode available in config */
51b89485a5SPaolo Bonzini /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */
52b89485a5SPaolo Bonzini #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH
539fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
549fbe302bSMichael S. Tsirkin 
559fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
569fbe302bSMichael S. Tsirkin 
579fbe302bSMichael S. Tsirkin struct virtio_blk_config {
589fbe302bSMichael S. Tsirkin 	/* The capacity (in 512-byte sectors). */
59e6546342SJason Wang 	__virtio64 capacity;
609fbe302bSMichael S. Tsirkin 	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
61e6546342SJason Wang 	__virtio32 size_max;
629fbe302bSMichael S. Tsirkin 	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
63e6546342SJason Wang 	__virtio32 seg_max;
644eb867e9SMichael S. Tsirkin 	/* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
659fbe302bSMichael S. Tsirkin 	struct virtio_blk_geometry {
66e6546342SJason Wang 		__virtio16 cylinders;
679fbe302bSMichael S. Tsirkin 		uint8_t heads;
689fbe302bSMichael S. Tsirkin 		uint8_t sectors;
699fbe302bSMichael S. Tsirkin 	} geometry;
709fbe302bSMichael S. Tsirkin 
719fbe302bSMichael S. Tsirkin 	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
72e6546342SJason Wang 	__virtio32 blk_size;
739fbe302bSMichael S. Tsirkin 
749fbe302bSMichael S. Tsirkin 	/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
759fbe302bSMichael S. Tsirkin 	/* exponent for physical block per logical block. */
769fbe302bSMichael S. Tsirkin 	uint8_t physical_block_exp;
779fbe302bSMichael S. Tsirkin 	/* alignment offset in logical blocks. */
789fbe302bSMichael S. Tsirkin 	uint8_t alignment_offset;
799fbe302bSMichael S. Tsirkin 	/* minimum I/O size without performance penalty in logical blocks. */
80e6546342SJason Wang 	__virtio16 min_io_size;
819fbe302bSMichael S. Tsirkin 	/* optimal sustained I/O size in logical blocks. */
82e6546342SJason Wang 	__virtio32 opt_io_size;
839fbe302bSMichael S. Tsirkin 
849fbe302bSMichael S. Tsirkin 	/* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
859fbe302bSMichael S. Tsirkin 	uint8_t wce;
869fbe302bSMichael S. Tsirkin 	uint8_t unused;
879fbe302bSMichael S. Tsirkin 
889fbe302bSMichael S. Tsirkin 	/* number of vqs, only available when VIRTIO_BLK_F_MQ is set */
89e6546342SJason Wang 	__virtio16 num_queues;
90caa1ee43SChangpeng Liu 
91caa1ee43SChangpeng Liu 	/* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */
92caa1ee43SChangpeng Liu 	/*
93caa1ee43SChangpeng Liu 	 * The maximum discard sectors (in 512-byte sectors) for
94caa1ee43SChangpeng Liu 	 * one segment.
95caa1ee43SChangpeng Liu 	 */
96e6546342SJason Wang 	__virtio32 max_discard_sectors;
97caa1ee43SChangpeng Liu 	/*
98caa1ee43SChangpeng Liu 	 * The maximum number of discard segments in a
99caa1ee43SChangpeng Liu 	 * discard command.
100caa1ee43SChangpeng Liu 	 */
101e6546342SJason Wang 	__virtio32 max_discard_seg;
102caa1ee43SChangpeng Liu 	/* Discard commands must be aligned to this number of sectors. */
103e6546342SJason Wang 	__virtio32 discard_sector_alignment;
104caa1ee43SChangpeng Liu 
105caa1ee43SChangpeng Liu 	/* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */
106caa1ee43SChangpeng Liu 	/*
107caa1ee43SChangpeng Liu 	 * The maximum number of write zeroes sectors (in 512-byte sectors) in
108caa1ee43SChangpeng Liu 	 * one segment.
109caa1ee43SChangpeng Liu 	 */
110e6546342SJason Wang 	__virtio32 max_write_zeroes_sectors;
111caa1ee43SChangpeng Liu 	/*
112caa1ee43SChangpeng Liu 	 * The maximum number of segments in a write zeroes
113caa1ee43SChangpeng Liu 	 * command.
114caa1ee43SChangpeng Liu 	 */
115e6546342SJason Wang 	__virtio32 max_write_zeroes_seg;
116caa1ee43SChangpeng Liu 	/*
117caa1ee43SChangpeng Liu 	 * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the
118caa1ee43SChangpeng Liu 	 * deallocation of one or more of the sectors.
119caa1ee43SChangpeng Liu 	 */
120caa1ee43SChangpeng Liu 	uint8_t write_zeroes_may_unmap;
121caa1ee43SChangpeng Liu 
122caa1ee43SChangpeng Liu 	uint8_t unused1[3];
123*93e0932bSPeter Xu 
124*93e0932bSPeter Xu 	/* the next 3 entries are guarded by VIRTIO_BLK_F_SECURE_ERASE */
125*93e0932bSPeter Xu 	/*
126*93e0932bSPeter Xu 	 * The maximum secure erase sectors (in 512-byte sectors) for
127*93e0932bSPeter Xu 	 * one segment.
128*93e0932bSPeter Xu 	 */
129*93e0932bSPeter Xu 	__virtio32 max_secure_erase_sectors;
130*93e0932bSPeter Xu 	/*
131*93e0932bSPeter Xu 	 * The maximum number of secure erase segments in a
132*93e0932bSPeter Xu 	 * secure erase command.
133*93e0932bSPeter Xu 	 */
134*93e0932bSPeter Xu 	__virtio32 max_secure_erase_seg;
135*93e0932bSPeter Xu 	/* Secure erase commands must be aligned to this number of sectors. */
136*93e0932bSPeter Xu 	__virtio32 secure_erase_sector_alignment;
137*93e0932bSPeter Xu 
1389fbe302bSMichael S. Tsirkin } QEMU_PACKED;
1399fbe302bSMichael S. Tsirkin 
1409fbe302bSMichael S. Tsirkin /*
1419fbe302bSMichael S. Tsirkin  * Command types
1429fbe302bSMichael S. Tsirkin  *
1439fbe302bSMichael S. Tsirkin  * Usage is a bit tricky as some bits are used as flags and some are not.
1449fbe302bSMichael S. Tsirkin  *
1459fbe302bSMichael S. Tsirkin  * Rules:
1469fbe302bSMichael S. Tsirkin  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
1479fbe302bSMichael S. Tsirkin  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
1489fbe302bSMichael S. Tsirkin  *   and may not be combined with any of the other flags.
1499fbe302bSMichael S. Tsirkin  */
1509fbe302bSMichael S. Tsirkin 
1519fbe302bSMichael S. Tsirkin /* These two define direction. */
1529fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_IN		0
1539fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_OUT	1
1549fbe302bSMichael S. Tsirkin 
1559fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1569fbe302bSMichael S. Tsirkin /* This bit says it's a scsi command, not an actual read or write. */
1579fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_SCSI_CMD	2
1589fbe302bSMichael S. Tsirkin #endif /* VIRTIO_BLK_NO_LEGACY */
1599fbe302bSMichael S. Tsirkin 
1609fbe302bSMichael S. Tsirkin /* Cache flush command */
1619fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_FLUSH	4
1629fbe302bSMichael S. Tsirkin 
1639fbe302bSMichael S. Tsirkin /* Get device ID command */
1649fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_GET_ID    8
1659fbe302bSMichael S. Tsirkin 
166da054c64SPaolo Bonzini /* Discard command */
167da054c64SPaolo Bonzini #define VIRTIO_BLK_T_DISCARD	11
168da054c64SPaolo Bonzini 
169da054c64SPaolo Bonzini /* Write zeroes command */
170da054c64SPaolo Bonzini #define VIRTIO_BLK_T_WRITE_ZEROES	13
171da054c64SPaolo Bonzini 
172*93e0932bSPeter Xu /* Secure erase command */
173*93e0932bSPeter Xu #define VIRTIO_BLK_T_SECURE_ERASE	14
174*93e0932bSPeter Xu 
1759fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
1769fbe302bSMichael S. Tsirkin /* Barrier before this op. */
1779fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_T_BARRIER	0x80000000
1789fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
1799fbe302bSMichael S. Tsirkin 
1804eb867e9SMichael S. Tsirkin /*
1814eb867e9SMichael S. Tsirkin  * This comes first in the read scatter-gather list.
1824eb867e9SMichael S. Tsirkin  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
1834eb867e9SMichael S. Tsirkin  * this is the first element of the read scatter-gather list.
1844eb867e9SMichael S. Tsirkin  */
1859fbe302bSMichael S. Tsirkin struct virtio_blk_outhdr {
1869fbe302bSMichael S. Tsirkin 	/* VIRTIO_BLK_T* */
1879fbe302bSMichael S. Tsirkin 	__virtio32 type;
1889fbe302bSMichael S. Tsirkin 	/* io priority. */
1899fbe302bSMichael S. Tsirkin 	__virtio32 ioprio;
1909fbe302bSMichael S. Tsirkin 	/* Sector (ie. 512 byte offset) */
1919fbe302bSMichael S. Tsirkin 	__virtio64 sector;
1929fbe302bSMichael S. Tsirkin };
1939fbe302bSMichael S. Tsirkin 
194caa1ee43SChangpeng Liu /* Unmap this range (only valid for write zeroes command) */
195caa1ee43SChangpeng Liu #define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP	0x00000001
196caa1ee43SChangpeng Liu 
197caa1ee43SChangpeng Liu /* Discard/write zeroes range for each request. */
198caa1ee43SChangpeng Liu struct virtio_blk_discard_write_zeroes {
199caa1ee43SChangpeng Liu 	/* discard/write zeroes start sector */
200caa1ee43SChangpeng Liu 	uint64_t sector;
201caa1ee43SChangpeng Liu 	/* number of discard/write zeroes sectors */
202caa1ee43SChangpeng Liu 	uint32_t num_sectors;
203caa1ee43SChangpeng Liu 	/* flags for this range */
204caa1ee43SChangpeng Liu 	uint32_t flags;
205caa1ee43SChangpeng Liu };
206caa1ee43SChangpeng Liu 
2079fbe302bSMichael S. Tsirkin #ifndef VIRTIO_BLK_NO_LEGACY
2089fbe302bSMichael S. Tsirkin struct virtio_scsi_inhdr {
2099fbe302bSMichael S. Tsirkin 	__virtio32 errors;
2109fbe302bSMichael S. Tsirkin 	__virtio32 data_len;
2119fbe302bSMichael S. Tsirkin 	__virtio32 sense_len;
2129fbe302bSMichael S. Tsirkin 	__virtio32 residual;
2139fbe302bSMichael S. Tsirkin };
2149fbe302bSMichael S. Tsirkin #endif /* !VIRTIO_BLK_NO_LEGACY */
2159fbe302bSMichael S. Tsirkin 
2169fbe302bSMichael S. Tsirkin /* And this is the final byte of the write scatter-gather list. */
2179fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_OK		0
2189fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_IOERR	1
2199fbe302bSMichael S. Tsirkin #define VIRTIO_BLK_S_UNSUPP	2
2209fbe302bSMichael S. Tsirkin #endif /* _LINUX_VIRTIO_BLK_H */
221