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