Fix windows build after virtio changes
[qemu] / hw / virtio.h
1 /*
2  * Virtio Support
3  *
4  * Copyright IBM, Corp. 2007
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13
14 #ifndef _QEMU_VIRTIO_H
15 #define _QEMU_VIRTIO_H
16
17 #include "hw.h"
18 #include "pci.h"
19
20 #ifdef _WIN32
21 struct iovec {
22     void *iov_base;
23     size_t iov_len;
24 };
25 #else
26 #include <sys/uio.h>
27 #endif
28
29 /* from Linux's linux/virtio_config.h */
30
31 /* Status byte for guest to report progress, and synchronize features. */
32 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
33 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
34 /* We have found a driver for the device. */
35 #define VIRTIO_CONFIG_S_DRIVER          2
36 /* Driver has used its parts of the config, and is happy */
37 #define VIRTIO_CONFIG_S_DRIVER_OK       4
38 /* We've given up on this device. */
39 #define VIRTIO_CONFIG_S_FAILED          0x80
40
41 /* We notify when the ring is completely used, even if the guest is supressing
42  * callbacks */
43 #define VIRTIO_F_NOTIFY_ON_EMPTY        24
44
45 /* from Linux's linux/virtio_ring.h */
46
47 /* This marks a buffer as continuing via the next field. */
48 #define VRING_DESC_F_NEXT       1
49 /* This marks a buffer as write-only (otherwise read-only). */
50 #define VRING_DESC_F_WRITE      2
51
52 /* This means don't notify other side when buffer added. */
53 #define VRING_USED_F_NO_NOTIFY  1
54 /* This means don't interrupt guest when buffer consumed. */
55 #define VRING_AVAIL_F_NO_INTERRUPT      1
56
57 struct VirtQueue;
58
59 static inline target_phys_addr_t vring_align(target_phys_addr_t addr,
60                                              unsigned long align)
61 {
62     return (addr + align - 1) & ~(align - 1);
63 }
64
65 typedef struct VirtQueue VirtQueue;
66 typedef struct VirtIODevice VirtIODevice;
67
68 #define VIRTQUEUE_MAX_SIZE 1024
69
70 typedef struct VirtQueueElement
71 {
72     unsigned int index;
73     unsigned int out_num;
74     unsigned int in_num;
75     target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE];
76     struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
77     struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
78 } VirtQueueElement;
79
80 #define VIRTIO_PCI_QUEUE_MAX 16
81
82 struct VirtIODevice
83 {
84     PCIDevice pci_dev;
85     const char *name;
86     uint32_t addr;
87     uint8_t status;
88     uint8_t isr;
89     uint16_t queue_sel;
90     uint32_t features;
91     size_t config_len;
92     void *config;
93     uint32_t (*get_features)(VirtIODevice *vdev);
94     void (*set_features)(VirtIODevice *vdev, uint32_t val);
95     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
96     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
97     void (*reset)(VirtIODevice *vdev);
98     VirtQueue *vq;
99 };
100
101 VirtIODevice *virtio_init_pci(PCIBus *bus, const char *name,
102                               uint16_t vendor, uint16_t device,
103                               uint16_t subvendor, uint16_t subdevice,
104                               uint8_t class_code, uint8_t subclass_code,
105                               uint8_t pif, size_t config_size,
106                               size_t struct_size);
107
108 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
109                             void (*handle_output)(VirtIODevice *,
110                                                   VirtQueue *));
111
112 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
113                     unsigned int len);
114 void virtqueue_flush(VirtQueue *vq, unsigned int count);
115 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
116                     unsigned int len, unsigned int idx);
117
118 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
119 int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes);
120
121 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
122
123 void virtio_save(VirtIODevice *vdev, QEMUFile *f);
124
125 void virtio_load(VirtIODevice *vdev, QEMUFile *f);
126
127 void virtio_notify_config(VirtIODevice *vdev);
128
129 void virtio_queue_set_notification(VirtQueue *vq, int enable);
130
131 int virtio_queue_ready(VirtQueue *vq);
132
133 int virtio_queue_empty(VirtQueue *vq);
134
135 #endif