xref: /freebsd/sys/dev/virtio/block/virtio_blk.h (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * This header is BSD licensed so anyone can use the definitions to implement
5  * compatible drivers/servers.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of IBM nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef _VIRTIO_BLK_H
34 #define _VIRTIO_BLK_H
35 
36 /* Feature bits */
37 #define VIRTIO_BLK_F_BARRIER	0x0001	/* Does host support barriers? */
38 #define VIRTIO_BLK_F_SIZE_MAX	0x0002	/* Indicates maximum segment size */
39 #define VIRTIO_BLK_F_SEG_MAX	0x0004	/* Indicates maximum # of segments */
40 #define VIRTIO_BLK_F_GEOMETRY	0x0010	/* Legacy geometry available  */
41 #define VIRTIO_BLK_F_RO		0x0020	/* Disk is read-only */
42 #define VIRTIO_BLK_F_BLK_SIZE	0x0040	/* Block size of disk is available*/
43 #define VIRTIO_BLK_F_SCSI	0x0080	/* Supports scsi command passthru */
44 #define VIRTIO_BLK_F_WCE	0x0200	/* Writeback mode enabled after reset */
45 #define VIRTIO_BLK_F_TOPOLOGY	0x0400	/* Topology information is available */
46 #define VIRTIO_BLK_F_CONFIG_WCE 0x0800	/* Writeback mode available in config */
47 
48 #define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
49 
50 struct virtio_blk_config {
51 	/* The capacity (in 512-byte sectors). */
52 	uint64_t capacity;
53 	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
54 	uint32_t size_max;
55 	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
56 	uint32_t seg_max;
57 	/* Geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
58 	struct virtio_blk_geometry {
59 		uint16_t cylinders;
60 		uint8_t heads;
61 		uint8_t sectors;
62 	} geometry;
63 
64 	/* Block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
65 	uint32_t blk_size;
66 
67 	/* Topology of the device (if VIRTIO_BLK_F_TOPOLOGY) */
68 	struct virtio_blk_topology {
69 		uint8_t physical_block_exp;
70 		uint8_t alignment_offset;
71 		uint16_t min_io_size;
72 		uint32_t opt_io_size;
73 	} topology;
74 
75 	/* Writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
76 	uint8_t writeback;
77 
78 } __packed;
79 
80 /*
81  * Command types
82  *
83  * Usage is a bit tricky as some bits are used as flags and some are not.
84  *
85  * Rules:
86  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
87  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
88  *   and may not be combined with any of the other flags.
89  */
90 
91 /* These two define direction. */
92 #define VIRTIO_BLK_T_IN		0
93 #define VIRTIO_BLK_T_OUT	1
94 
95 /* This bit says it's a scsi command, not an actual read or write. */
96 #define VIRTIO_BLK_T_SCSI_CMD	2
97 
98 /* Cache flush command */
99 #define VIRTIO_BLK_T_FLUSH	4
100 
101 /* Get device ID command */
102 #define VIRTIO_BLK_T_GET_ID	8
103 
104 /* Barrier before this op. */
105 #define VIRTIO_BLK_T_BARRIER	0x80000000
106 
107 /* ID string length */
108 #define VIRTIO_BLK_ID_BYTES	20
109 
110 /* This is the first element of the read scatter-gather list. */
111 struct virtio_blk_outhdr {
112 	/* VIRTIO_BLK_T* */
113 	uint32_t type;
114 	/* io priority. */
115 	uint32_t ioprio;
116 	/* Sector (ie. 512 byte offset) */
117 	uint64_t sector;
118 };
119 
120 struct virtio_scsi_inhdr {
121 	uint32_t errors;
122 	uint32_t data_len;
123 	uint32_t sense_len;
124 	uint32_t residual;
125 };
126 
127 /* And this is the final byte of the write scatter-gather list. */
128 #define VIRTIO_BLK_S_OK		0
129 #define VIRTIO_BLK_S_IOERR	1
130 #define VIRTIO_BLK_S_UNSUPP	2
131 
132 #endif /* _VIRTIO_BLK_H */
133