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_RING_SIZE            (PAGE_SIZE * 8)
151 #define VIRTIO_MAX_VQS              3
152 #define KVM_S390_VIRTIO_RING_ALIGN  4096
153 
154 #define VRING_USED_F_NO_NOTIFY  1
155 
156 /* This marks a buffer as continuing via the next field. */
157 #define VRING_DESC_F_NEXT       1
158 /* This marks a buffer as write-only (otherwise read-only). */
159 #define VRING_DESC_F_WRITE      2
160 /* This means the buffer contains a list of buffer descriptors. */
161 #define VRING_DESC_F_INDIRECT   4
162 
163 /* Internal flag to mark follow-up segments as such */
164 #define VRING_HIDDEN_IS_CHAIN   256
165 
166 /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
167 struct VRingDesc {
168     /* Address (guest-physical). */
169     uint64_t addr;
170     /* Length. */
171     uint32_t len;
172     /* The flags as indicated above. */
173     uint16_t flags;
174     /* We chain unused descriptors via this, too */
175     uint16_t next;
176 } __attribute__((packed));
177 typedef struct VRingDesc VRingDesc;
178 
179 struct VRingAvail {
180     uint16_t flags;
181     uint16_t idx;
182     uint16_t ring[];
183 } __attribute__((packed));
184 typedef struct VRingAvail VRingAvail;
185 
186 /* uint32_t is used here for ids for padding reasons. */
187 struct VRingUsedElem {
188     /* Index of start of used descriptor chain. */
189     uint32_t id;
190     /* Total length of the descriptor chain which was used (written to) */
191     uint32_t len;
192 } __attribute__((packed));
193 typedef struct VRingUsedElem VRingUsedElem;
194 
195 struct VRingUsed {
196     uint16_t flags;
197     uint16_t idx;
198     VRingUsedElem ring[];
199 } __attribute__((packed));
200 typedef struct VRingUsed VRingUsed;
201 
202 struct VRing {
203     unsigned int num;
204     int next_idx;
205     int used_idx;
206     VRingDesc *desc;
207     VRingAvail *avail;
208     VRingUsed *used;
209     long cookie;
210     int id;
211 };
212 typedef struct VRing VRing;
213 
214 
215 /***********************************************
216  *               Virtio block                  *
217  ***********************************************/
218 
219 /*
220  * Command types
221  *
222  * Usage is a bit tricky as some bits are used as flags and some are not.
223  *
224  * Rules:
225  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
226  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
227  *   and may not be combined with any of the other flags.
228  */
229 
230 /* These two define direction. */
231 #define VIRTIO_BLK_T_IN         0
232 #define VIRTIO_BLK_T_OUT        1
233 
234 /* This bit says it's a scsi command, not an actual read or write. */
235 #define VIRTIO_BLK_T_SCSI_CMD   2
236 
237 /* Cache flush command */
238 #define VIRTIO_BLK_T_FLUSH      4
239 
240 /* Barrier before this op. */
241 #define VIRTIO_BLK_T_BARRIER    0x80000000
242 
243 /* This is the first element of the read scatter-gather list. */
244 struct VirtioBlkOuthdr {
245         /* VIRTIO_BLK_T* */
246         uint32_t type;
247         /* io priority. */
248         uint32_t ioprio;
249         /* Sector (ie. 512 byte offset) */
250         uint64_t sector;
251 };
252 typedef struct VirtioBlkOuthdr VirtioBlkOuthdr;
253 
254 struct VirtioBlkConfig {
255     uint64_t capacity; /* in 512-byte sectors */
256     uint32_t size_max; /* max segment size (if VIRTIO_BLK_F_SIZE_MAX) */
257     uint32_t seg_max;  /* max number of segments (if VIRTIO_BLK_F_SEG_MAX) */
258 
259     struct VirtioBlkGeometry {
260         uint16_t cylinders;
261         uint8_t heads;
262         uint8_t sectors;
263     } geometry; /* (if VIRTIO_BLK_F_GEOMETRY) */
264 
265     uint32_t blk_size; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
266 
267     /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
268     uint8_t physical_block_exp; /* exponent for physical blk per logical blk */
269     uint8_t alignment_offset;   /* alignment offset in logical blocks */
270     uint16_t min_io_size;       /* min I/O size without performance penalty
271                               in logical blocks */
272     uint32_t opt_io_size;       /* optimal sustained I/O size in logical blks */
273 
274     uint8_t wce; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
275 } __attribute__((packed));
276 typedef struct VirtioBlkConfig VirtioBlkConfig;
277 
278 enum guessed_disk_nature_type {
279     VIRTIO_GDN_NONE     = 0,
280     VIRTIO_GDN_DASD     = 1,
281     VIRTIO_GDN_CDROM    = 2,
282     VIRTIO_GDN_SCSI     = 3,
283 };
284 typedef enum guessed_disk_nature_type VirtioGDN;
285 
286 #define VIRTIO_SECTOR_SIZE 512
287 #define VIRTIO_ISO_BLOCK_SIZE 2048
288 #define VIRTIO_SCSI_BLOCK_SIZE 512
289 
290 struct VirtioScsiConfig {
291     uint32_t num_queues;
292     uint32_t seg_max;
293     uint32_t max_sectors;
294     uint32_t cmd_per_lun;
295     uint32_t event_info_size;
296     uint32_t sense_size;
297     uint32_t cdb_size;
298     uint16_t max_channel;
299     uint16_t max_target;
300     uint32_t max_lun;
301 } __attribute__((packed));
302 typedef struct VirtioScsiConfig VirtioScsiConfig;
303 
304 struct ScsiDevice {
305     uint16_t channel;   /* Always 0 in QEMU     */
306     uint16_t target;    /* will be scanned over */
307     uint32_t lun;       /* will be reported     */
308 };
309 typedef struct ScsiDevice ScsiDevice;
310 
311 struct VDev {
312     uint64_t common_cfg;
313     uint64_t device_cfg;
314     uint64_t notify_base;
315     uint32_t notify_mult;
316     uint64_t pos;
317     int configured;
318     int nr_vqs;
319     VRing *vrings;
320     int cmd_vr_idx;
321     void *ring_area;
322     long wait_reply_timeout;
323     VirtioGDN guessed_disk_nature;
324     int senseid;
325     union {
326         VirtioBlkConfig blk;
327         VirtioScsiConfig scsi;
328     } config;
329     ScsiDevice *scsi_device;
330     int is_cdrom;
331     int scsi_block_size;
332     int blk_factor;
333     uint64_t scsi_last_block;
334     uint32_t scsi_dev_cyls;
335     uint8_t scsi_dev_heads;
336     int scsi_device_selected;
337     ScsiDevice selected_scsi_device;
338 };
339 typedef struct VDev VDev;
340 
341 extern int virtio_get_block_size(VDev *vdev);
342 extern uint8_t virtio_get_heads(VDev *vdev);
343 extern uint8_t virtio_get_sectors(VDev *vdev);
344 extern uint64_t virtio_get_blocks(VDev *vdev);
345 
virtio_sector_adjust(VDev * vdev,uint64_t sector)346 static inline uint64_t virtio_sector_adjust(VDev *vdev, uint64_t sector)
347 {
348     return sector * (virtio_get_block_size(vdev) / VIRTIO_SECTOR_SIZE);
349 }
350 
351 VirtioGDN virtio_guessed_disk_nature(VDev *vdev);
352 void virtio_assume_scsi(VDev *vdev);
353 void virtio_assume_eckd(VDev *vdev);
354 void virtio_assume_iso9660(VDev *vdev);
355 
356 extern int virtio_disk_is_scsi(VDev *vdev);
357 extern int virtio_disk_is_eckd(VDev *vdev);
358 
359 int virtio_read_many(VDev *vdev, uint64_t sector, void *load_addr, int sec_num);
360 VDev *virtio_get_device(void);
361 VirtioDevType virtio_get_device_type(void);
362 
363 struct VirtioCmd {
364     void *data;
365     int size;
366     int flags;
367 };
368 typedef struct VirtioCmd VirtioCmd;
369 
370 #endif /* VIRTIO_H */
371