1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4  * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5  *
6  * From Linux kernel include/uapi/linux/virtio_blk.h
7  */
8 
9 #ifndef _LINUX_VIRTIO_BLK_H
10 #define _LINUX_VIRTIO_BLK_H
11 
12 /* Feature bits */
13 #define VIRTIO_BLK_F_SIZE_MAX	1	/* Indicates maximum segment size */
14 #define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
15 #define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available */
16 #define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
17 #define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available */
18 #define VIRTIO_BLK_F_TOPOLOGY	10	/* Topology information is available */
19 #define VIRTIO_BLK_F_MQ		12	/* Support more than one vq */
20 
21 /* Legacy feature bits */
22 #ifndef VIRTIO_BLK_NO_LEGACY
23 #define VIRTIO_BLK_F_BARRIER	0	/* Does host support barriers? */
24 #define VIRTIO_BLK_F_SCSI	7	/* Supports scsi command passthru */
25 #define VIRTIO_BLK_F_FLUSH	9	/* Flush command supported */
26 #define VIRTIO_BLK_F_CONFIG_WCE	11	/* Writeback mode available in config */
27 #ifndef __KERNEL__
28 /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH */
29 #define VIRTIO_BLK_F_WCE	VIRTIO_BLK_F_FLUSH
30 #endif
31 #endif /* !VIRTIO_BLK_NO_LEGACY */
32 
33 #define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
34 
35 struct __packed virtio_blk_config {
36 	/* The capacity (in 512-byte sectors) */
37 	__u64 capacity;
38 	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
39 	__u32 size_max;
40 	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
41 	__u32 seg_max;
42 	/* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
43 	struct virtio_blk_geometry {
44 		__u16 cylinders;
45 		__u8 heads;
46 		__u8 sectors;
47 	} geometry;
48 
49 	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
50 	__u32 blk_size;
51 
52 	/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */
53 	/* exponent for physical block per logical block */
54 	__u8 physical_block_exp;
55 	/* alignment offset in logical blocks */
56 	__u8 alignment_offset;
57 	/* minimum I/O size without performance penalty in logical blocks */
58 	__u16 min_io_size;
59 	/* optimal sustained I/O size in logical blocks */
60 	__u32 opt_io_size;
61 
62 	/* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
63 	__u8 wce;
64 	__u8 unused;
65 
66 	/* number of vqs, only available when VIRTIO_BLK_F_MQ is set */
67 	__u16 num_queues;
68 };
69 
70 /*
71  * Command types
72  *
73  * Usage is a bit tricky as some bits are used as flags and some are not.
74  *
75  * Rules:
76  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
77  *   VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own
78  *   and may not be combined with any of the other flags.
79  */
80 
81 /* These two define direction */
82 #define VIRTIO_BLK_T_IN		0
83 #define VIRTIO_BLK_T_OUT	1
84 
85 #ifndef VIRTIO_BLK_NO_LEGACY
86 /* This bit says it's a scsi command, not an actual read or write */
87 #define VIRTIO_BLK_T_SCSI_CMD	2
88 #endif /* VIRTIO_BLK_NO_LEGACY */
89 
90 /* Cache flush command */
91 #define VIRTIO_BLK_T_FLUSH	4
92 
93 /* Get device ID command */
94 #define VIRTIO_BLK_T_GET_ID	8
95 
96 #ifndef VIRTIO_BLK_NO_LEGACY
97 /* Barrier before this op */
98 #define VIRTIO_BLK_T_BARRIER	0x80000000
99 #endif /* !VIRTIO_BLK_NO_LEGACY */
100 
101 /*
102  * This comes first in the read scatter-gather list.
103  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
104  * this is the first element of the read scatter-gather list.
105  */
106 struct virtio_blk_outhdr {
107 	/* VIRTIO_BLK_T* */
108 	__virtio32 type;
109 	/* io priority */
110 	__virtio32 ioprio;
111 	/* Sector (ie. 512 byte offset) */
112 	__virtio64 sector;
113 };
114 
115 #ifndef VIRTIO_BLK_NO_LEGACY
116 struct virtio_scsi_inhdr {
117 	__virtio32 errors;
118 	__virtio32 data_len;
119 	__virtio32 sense_len;
120 	__virtio32 residual;
121 };
122 #endif /* !VIRTIO_BLK_NO_LEGACY */
123 
124 /* And this is the final byte of the write scatter-gather list */
125 #define VIRTIO_BLK_S_OK		0
126 #define VIRTIO_BLK_S_IOERR	1
127 #define VIRTIO_BLK_S_UNSUPP	2
128 
129 #endif /* _LINUX_VIRTIO_BLK_H */
130