xref: /qemu/pc-bios/s390-ccw/virtio.h (revision b88d7fa5)
1 /*
2  * Virtio driver bits
3  *
4  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or (at
7  * your option) any later version. See the COPYING file in the top-level
8  * directory.
9  */
10 
11 #ifndef VIRTIO_H
12 #define VIRTIO_H
13 
14 #include "s390-ccw.h"
15 
16 /* Status byte for guest to report progress, and synchronize features. */
17 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
18 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
19 /* We have found a driver for the device. */
20 #define VIRTIO_CONFIG_S_DRIVER          2
21 /* Driver has used its parts of the config, and is happy */
22 #define VIRTIO_CONFIG_S_DRIVER_OK       4
23 /* We've given up on this device. */
24 #define VIRTIO_CONFIG_S_FAILED          0x80
25 
26 enum VirtioDevType {
27     VIRTIO_ID_NET = 1,
28     VIRTIO_ID_BLOCK = 2,
29     VIRTIO_ID_CONSOLE = 3,
30     VIRTIO_ID_BALLOON = 5,
31 };
32 typedef enum VirtioDevType VirtioDevType;
33 
34 struct VirtioDevHeader {
35     VirtioDevType type:8;
36     uint8_t num_vq;
37     uint8_t feature_len;
38     uint8_t config_len;
39     uint8_t status;
40     uint8_t vqconfig[];
41 } __attribute__((packed));
42 typedef struct VirtioDevHeader VirtioDevHeader;
43 
44 struct VirtioVqConfig {
45     uint64_t token;
46     uint64_t address;
47     uint16_t num;
48     uint8_t pad[6];
49 } __attribute__((packed));
50 typedef struct VirtioVqConfig VirtioVqConfig;
51 
52 struct VqInfo {
53     uint64_t queue;
54     uint32_t align;
55     uint16_t index;
56     uint16_t num;
57 } __attribute__((packed));
58 typedef struct VqInfo VqInfo;
59 
60 struct VqConfig {
61     uint16_t index;
62     uint16_t num;
63 } __attribute__((packed));
64 typedef struct VqConfig VqConfig;
65 
66 struct VirtioDev {
67     VirtioDevHeader *header;
68     VirtioVqConfig *vqconfig;
69     char *host_features;
70     char *guest_features;
71     char *config;
72 };
73 typedef struct VirtioDev VirtioDev;
74 
75 #define KVM_S390_VIRTIO_RING_ALIGN  4096
76 
77 #define VRING_USED_F_NO_NOTIFY  1
78 
79 /* This marks a buffer as continuing via the next field. */
80 #define VRING_DESC_F_NEXT       1
81 /* This marks a buffer as write-only (otherwise read-only). */
82 #define VRING_DESC_F_WRITE      2
83 /* This means the buffer contains a list of buffer descriptors. */
84 #define VRING_DESC_F_INDIRECT   4
85 
86 /* Internal flag to mark follow-up segments as such */
87 #define VRING_HIDDEN_IS_CHAIN   256
88 
89 /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
90 struct VRingDesc {
91     /* Address (guest-physical). */
92     uint64_t addr;
93     /* Length. */
94     uint32_t len;
95     /* The flags as indicated above. */
96     uint16_t flags;
97     /* We chain unused descriptors via this, too */
98     uint16_t next;
99 } __attribute__((packed));
100 typedef struct VRingDesc VRingDesc;
101 
102 struct VRingAvail {
103     uint16_t flags;
104     uint16_t idx;
105     uint16_t ring[];
106 } __attribute__((packed));
107 typedef struct VRingAvail VRingAvail;
108 
109 /* uint32_t is used here for ids for padding reasons. */
110 struct VRingUsedElem {
111     /* Index of start of used descriptor chain. */
112     uint32_t id;
113     /* Total length of the descriptor chain which was used (written to) */
114     uint32_t len;
115 } __attribute__((packed));
116 typedef struct VRingUsedElem VRingUsedElem;
117 
118 struct VRingUsed {
119     uint16_t flags;
120     uint16_t idx;
121     VRingUsedElem ring[];
122 } __attribute__((packed));
123 typedef struct VRingUsed VRingUsed;
124 
125 struct VRing {
126     unsigned int num;
127     int next_idx;
128     int used_idx;
129     VRingDesc *desc;
130     VRingAvail *avail;
131     VRingUsed *used;
132     SubChannelId schid;
133 };
134 typedef struct VRing VRing;
135 
136 
137 /***********************************************
138  *               Virtio block                  *
139  ***********************************************/
140 
141 /*
142  * Command types
143  *
144  * Usage is a bit tricky as some bits are used as flags and some are not.
145  *
146  * Rules:
147  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
148  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
149  *   and may not be combined with any of the other flags.
150  */
151 
152 /* These two define direction. */
153 #define VIRTIO_BLK_T_IN         0
154 #define VIRTIO_BLK_T_OUT        1
155 
156 /* This bit says it's a scsi command, not an actual read or write. */
157 #define VIRTIO_BLK_T_SCSI_CMD   2
158 
159 /* Cache flush command */
160 #define VIRTIO_BLK_T_FLUSH      4
161 
162 /* Barrier before this op. */
163 #define VIRTIO_BLK_T_BARRIER    0x80000000
164 
165 /* This is the first element of the read scatter-gather list. */
166 struct VirtioBlkOuthdr {
167         /* VIRTIO_BLK_T* */
168         uint32_t type;
169         /* io priority. */
170         uint32_t ioprio;
171         /* Sector (ie. 512 byte offset) */
172         uint64_t sector;
173 };
174 typedef struct VirtioBlkOuthdr VirtioBlkOuthdr;
175 
176 struct VirtioBlkConfig {
177     uint64_t capacity; /* in 512-byte sectors */
178     uint32_t size_max; /* max segment size (if VIRTIO_BLK_F_SIZE_MAX) */
179     uint32_t seg_max;  /* max number of segments (if VIRTIO_BLK_F_SEG_MAX) */
180 
181     struct VirtioBlkGeometry {
182         uint16_t cylinders;
183         uint8_t heads;
184         uint8_t sectors;
185     } geometry; /* (if VIRTIO_BLK_F_GEOMETRY) */
186 
187     uint32_t blk_size; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
188 
189     /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
190     uint8_t physical_block_exp; /* exponent for physical blk per logical blk */
191     uint8_t alignment_offset;   /* alignment offset in logical blocks */
192     uint16_t min_io_size;       /* min I/O size without performance penalty
193                               in logical blocks */
194     uint32_t opt_io_size;       /* optimal sustained I/O size in logical blks */
195 
196     uint8_t wce; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
197 } __attribute__((packed));
198 typedef struct VirtioBlkConfig VirtioBlkConfig;
199 
200 bool virtio_guessed_disk_nature(void);
201 void virtio_assume_scsi(void);
202 void virtio_assume_eckd(void);
203 void virtio_assume_iso9660(void);
204 
205 extern bool virtio_disk_is_scsi(void);
206 extern bool virtio_disk_is_eckd(void);
207 extern bool virtio_ipl_disk_is_valid(void);
208 extern int virtio_get_block_size(void);
209 extern uint8_t virtio_get_heads(void);
210 extern uint8_t virtio_get_sectors(void);
211 extern uint64_t virtio_get_blocks(void);
212 extern int virtio_read_many(ulong sector, void *load_addr, int sec_num);
213 
214 #define VIRTIO_SECTOR_SIZE 512
215 
216 static inline ulong virtio_sector_adjust(ulong sector)
217 {
218     return sector * (virtio_get_block_size() / VIRTIO_SECTOR_SIZE);
219 }
220 
221 #endif /* VIRTIO_H */
222