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 /* A 32-bit r/o bitmask of the features supported by the host */
15 #define VIRTIO_PCI_HOST_FEATURES 0
16
17 /* A 32-bit r/w bitmask of features activated by the guest */
18 #define VIRTIO_PCI_GUEST_FEATURES 4
19
20 /* A 32-bit r/w PFN for the currently selected queue */
21 #define VIRTIO_PCI_QUEUE_PFN 8
22
23 /* A 16-bit r/o queue size for the currently selected queue */
24 #define VIRTIO_PCI_QUEUE_NUM 12
25
26 /* A 16-bit r/w queue selector */
27 #define VIRTIO_PCI_QUEUE_SEL 14
28
29 /* A 16-bit r/w queue notifier */
30 #define VIRTIO_PCI_QUEUE_NOTIFY 16
31
32 /* An 8-bit device status register. */
33 #define VIRTIO_PCI_STATUS 18
34
35 /* An 8-bit r/o interrupt status register. Reading the value will return the
36 * current contents of the ISR and will also clear it. This is effectively
37 * a read-and-acknowledge. */
38 #define VIRTIO_PCI_ISR 19
39
40 /* MSI-X registers: only enabled if MSI-X is enabled. */
41 /* A 16-bit vector for configuration changes. */
42 #define VIRTIO_MSI_CONFIG_VECTOR 20
43 /* A 16-bit vector for selected queue notifications. */
44 #define VIRTIO_MSI_QUEUE_VECTOR 22
45
46 /* How many bits to shift physical queue address written to QUEUE_PFN.
47 * 12 is historical, and due to x86 page size. */
48 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
49
50 /* The alignment to use between consumer and producer parts of vring.
51 * x86 pagesize again. */
52 #define VIRTIO_PCI_VRING_ALIGN 4096
53
54 /* Status byte for guest to report progress, and synchronize features. */
55 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
56 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
57 /* We have found a driver for the device. */
58 #define VIRTIO_CONFIG_S_DRIVER 2
59 /* Driver has used its parts of the config, and is happy */
60 #define VIRTIO_CONFIG_S_DRIVER_OK 4
61 /* Driver has finished configuring features */
62 #define VIRTIO_CONFIG_S_FEATURES_OK 8
63 /* We've given up on this device. */
64 #define VIRTIO_CONFIG_S_FAILED 0x80
65
66 /* v1.0 compliant. */
67 #define VIRTIO_F_VERSION_1 32
68
69 /* Common configuration */
70 #define VIRTIO_PCI_CAP_COMMON_CFG 1
71 /* Notifications */
72 #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
73 /* ISR Status */
74 #define VIRTIO_PCI_CAP_ISR_CFG 3
75 /* Device specific configuration */
76 #define VIRTIO_PCI_CAP_DEVICE_CFG 4
77 /* PCI configuration access */
78 #define VIRTIO_PCI_CAP_PCI_CFG 5
79
80 #define VIRTIO_PCI_COMMON_DFSELECT 0
81 #define VIRTIO_PCI_COMMON_DF 4
82 #define VIRTIO_PCI_COMMON_GFSELECT 8
83 #define VIRTIO_PCI_COMMON_GF 12
84 #define VIRTIO_PCI_COMMON_MSIX 16
85 #define VIRTIO_PCI_COMMON_NUMQ 18
86 #define VIRTIO_PCI_COMMON_STATUS 20
87 #define VIRTIO_PCI_COMMON_CFGGENERATION 21
88 #define VIRTIO_PCI_COMMON_Q_SELECT 22
89 #define VIRTIO_PCI_COMMON_Q_SIZE 24
90 #define VIRTIO_PCI_COMMON_Q_MSIX 26
91 #define VIRTIO_PCI_COMMON_Q_ENABLE 28
92 #define VIRTIO_PCI_COMMON_Q_NOFF 30
93 #define VIRTIO_PCI_COMMON_Q_DESCLO 32
94 #define VIRTIO_PCI_COMMON_Q_DESCHI 36
95 #define VIRTIO_PCI_COMMON_Q_AVAILLO 40
96 #define VIRTIO_PCI_COMMON_Q_AVAILHI 44
97 #define VIRTIO_PCI_COMMON_Q_USEDLO 48
98 #define VIRTIO_PCI_COMMON_Q_USEDHI 52
99
100 enum VirtioDevType {
101 VIRTIO_ID_NET = 1,
102 VIRTIO_ID_BLOCK = 2,
103 VIRTIO_ID_CONSOLE = 3,
104 VIRTIO_ID_BALLOON = 5,
105 VIRTIO_ID_SCSI = 8,
106 };
107 typedef enum VirtioDevType VirtioDevType;
108
109 struct VirtioDevHeader {
110 VirtioDevType type:8;
111 uint8_t num_vq;
112 uint8_t feature_len;
113 uint8_t config_len;
114 uint8_t status;
115 uint8_t vqconfig[];
116 } __attribute__((packed));
117 typedef struct VirtioDevHeader VirtioDevHeader;
118
119 struct VirtioVqConfig {
120 uint64_t token;
121 uint64_t address;
122 uint16_t num;
123 uint8_t pad[6];
124 } __attribute__((packed));
125 typedef struct VirtioVqConfig VirtioVqConfig;
126
127 struct VqInfo {
128 uint32_t queue;
129 uint32_t align;
130 uint16_t index;
131 uint16_t num;
132 } __attribute__((packed));
133 typedef struct VqInfo VqInfo;
134
135 struct VqConfig {
136 uint16_t index;
137 uint16_t num;
138 } __attribute__((packed));
139 typedef struct VqConfig VqConfig;
140
141 struct VirtioDev {
142 VirtioDevHeader *header;
143 VirtioVqConfig *vqconfig;
144 char *host_features;
145 char *guest_features;
146 char *config;
147 };
148 typedef struct VirtioDev VirtioDev;
149
150 #define VIRTIO_MAX_RING_ENTRIES 128
151 #define VIRTIO_RING_SIZE (sizeof(VRingDesc) * VIRTIO_MAX_RING_ENTRIES)
152 #define VIRTIO_MAX_VQS 1
153 #define KVM_S390_VIRTIO_RING_ALIGN 4096
154
155 #define VRING_USED_F_NO_NOTIFY 1
156
157 /* This marks a buffer as continuing via the next field. */
158 #define VRING_DESC_F_NEXT 1
159 /* This marks a buffer as write-only (otherwise read-only). */
160 #define VRING_DESC_F_WRITE 2
161 /* This means the buffer contains a list of buffer descriptors. */
162 #define VRING_DESC_F_INDIRECT 4
163
164 /* Internal flag to mark follow-up segments as such */
165 #define VRING_HIDDEN_IS_CHAIN 256
166
167 /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
168 struct VRingDesc {
169 /* Address (guest-physical). */
170 uint64_t addr;
171 /* Length. */
172 uint32_t len;
173 /* The flags as indicated above. */
174 uint16_t flags;
175 /* We chain unused descriptors via this, too */
176 uint16_t next;
177 } __attribute__((packed));
178 typedef struct VRingDesc VRingDesc;
179
180 struct VRingAvail {
181 uint16_t flags;
182 uint16_t idx;
183 uint16_t ring[];
184 } __attribute__((packed));
185 typedef struct VRingAvail VRingAvail;
186
187 /* uint32_t is used here for ids for padding reasons. */
188 struct VRingUsedElem {
189 /* Index of start of used descriptor chain. */
190 uint32_t id;
191 /* Total length of the descriptor chain which was used (written to) */
192 uint32_t len;
193 } __attribute__((packed));
194 typedef struct VRingUsedElem VRingUsedElem;
195
196 struct VRingUsed {
197 uint16_t flags;
198 uint16_t idx;
199 VRingUsedElem ring[];
200 } __attribute__((packed));
201 typedef struct VRingUsed VRingUsed;
202
203 struct VRing {
204 unsigned int num;
205 int next_idx;
206 int used_idx;
207 VRingDesc *desc;
208 VRingAvail *avail;
209 VRingUsed *used;
210 long cookie;
211 int id;
212 };
213 typedef struct VRing VRing;
214
215
216 /***********************************************
217 * Virtio block *
218 ***********************************************/
219
220 /*
221 * Command types
222 *
223 * Usage is a bit tricky as some bits are used as flags and some are not.
224 *
225 * Rules:
226 * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
227 * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own
228 * and may not be combined with any of the other flags.
229 */
230
231 /* These two define direction. */
232 #define VIRTIO_BLK_T_IN 0
233 #define VIRTIO_BLK_T_OUT 1
234
235 /* This bit says it's a scsi command, not an actual read or write. */
236 #define VIRTIO_BLK_T_SCSI_CMD 2
237
238 /* Cache flush command */
239 #define VIRTIO_BLK_T_FLUSH 4
240
241 /* Barrier before this op. */
242 #define VIRTIO_BLK_T_BARRIER 0x80000000
243
244 /* This is the first element of the read scatter-gather list. */
245 struct VirtioBlkOuthdr {
246 /* VIRTIO_BLK_T* */
247 uint32_t type;
248 /* io priority. */
249 uint32_t ioprio;
250 /* Sector (ie. 512 byte offset) */
251 uint64_t sector;
252 };
253 typedef struct VirtioBlkOuthdr VirtioBlkOuthdr;
254
255 struct VirtioBlkConfig {
256 uint64_t capacity; /* in 512-byte sectors */
257 uint32_t size_max; /* max segment size (if VIRTIO_BLK_F_SIZE_MAX) */
258 uint32_t seg_max; /* max number of segments (if VIRTIO_BLK_F_SEG_MAX) */
259
260 struct VirtioBlkGeometry {
261 uint16_t cylinders;
262 uint8_t heads;
263 uint8_t sectors;
264 } geometry; /* (if VIRTIO_BLK_F_GEOMETRY) */
265
266 uint32_t blk_size; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
267
268 /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */
269 uint8_t physical_block_exp; /* exponent for physical blk per logical blk */
270 uint8_t alignment_offset; /* alignment offset in logical blocks */
271 uint16_t min_io_size; /* min I/O size without performance penalty
272 in logical blocks */
273 uint32_t opt_io_size; /* optimal sustained I/O size in logical blks */
274
275 uint8_t wce; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
276 } __attribute__((packed));
277 typedef struct VirtioBlkConfig VirtioBlkConfig;
278
279 enum guessed_disk_nature_type {
280 VIRTIO_GDN_NONE = 0,
281 VIRTIO_GDN_DASD = 1,
282 VIRTIO_GDN_CDROM = 2,
283 VIRTIO_GDN_SCSI = 3,
284 };
285 typedef enum guessed_disk_nature_type VirtioGDN;
286
287 #define VIRTIO_SECTOR_SIZE 512
288 #define VIRTIO_ISO_BLOCK_SIZE 2048
289 #define VIRTIO_SCSI_BLOCK_SIZE 512
290
291 struct VirtioScsiConfig {
292 uint32_t num_queues;
293 uint32_t seg_max;
294 uint32_t max_sectors;
295 uint32_t cmd_per_lun;
296 uint32_t event_info_size;
297 uint32_t sense_size;
298 uint32_t cdb_size;
299 uint16_t max_channel;
300 uint16_t max_target;
301 uint32_t max_lun;
302 } __attribute__((packed));
303 typedef struct VirtioScsiConfig VirtioScsiConfig;
304
305 struct ScsiDevice {
306 uint16_t channel; /* Always 0 in QEMU */
307 uint16_t target; /* will be scanned over */
308 uint32_t lun; /* will be reported */
309 };
310 typedef struct ScsiDevice ScsiDevice;
311
312 struct VDev {
313 uint64_t common_cfg;
314 uint64_t device_cfg;
315 uint64_t notify_base;
316 uint32_t notify_mult;
317 uint64_t pos;
318 int configured;
319 int nr_vqs;
320 VRing *vrings;
321 int cmd_vr_idx;
322 void *ring_area;
323 long wait_reply_timeout;
324 VirtioGDN guessed_disk_nature;
325 int senseid;
326 union {
327 VirtioBlkConfig blk;
328 VirtioScsiConfig scsi;
329 } config;
330 ScsiDevice *scsi_device;
331 int is_cdrom;
332 int scsi_block_size;
333 int blk_factor;
334 uint64_t scsi_last_block;
335 uint32_t scsi_dev_cyls;
336 uint8_t scsi_dev_heads;
337 int scsi_device_selected;
338 ScsiDevice selected_scsi_device;
339 };
340 typedef struct VDev VDev;
341
342 extern int virtio_get_block_size(VDev *vdev);
343 extern uint8_t virtio_get_heads(VDev *vdev);
344 extern uint8_t virtio_get_sectors(VDev *vdev);
345 extern uint64_t virtio_get_blocks(VDev *vdev);
346
virtio_sector_adjust(VDev * vdev,uint64_t sector)347 static inline uint64_t virtio_sector_adjust(VDev *vdev, uint64_t sector)
348 {
349 return sector * (virtio_get_block_size(vdev) / VIRTIO_SECTOR_SIZE);
350 }
351
352 VirtioGDN virtio_guessed_disk_nature(VDev *vdev);
353 void virtio_assume_scsi(VDev *vdev);
354 void virtio_assume_eckd(VDev *vdev);
355 void virtio_assume_iso9660(VDev *vdev);
356
357 extern int virtio_disk_is_scsi(VDev *vdev);
358 extern int virtio_disk_is_eckd(VDev *vdev);
359
360 int virtio_read_many(VDev *vdev, uint64_t sector, void *load_addr, int sec_num);
361 VDev *virtio_get_device(void);
362 VirtioDevType virtio_get_device_type(void);
363
364 struct VirtioCmd {
365 void *data;
366 int size;
367 int flags;
368 };
369 typedef struct VirtioCmd VirtioCmd;
370
371 #endif /* VIRTIO_H */
372