1 #ifndef QUEMU_VIO_VIRTIO_H
2 #define QUEMU_VIO_VIRTIO_H
11 #include <linux/virtio_blk.h>
13 #define VIRTQUEUE_MAX_SIZE 1024
14 #define BLOCK_SERIAL_STRLEN 20
16 #define VIRTIO_BLK_S_OK 0
17 #define VIRTIO_BLK_S_IOERR 1
18 #define VIRTIO_BLK_S_UNSUPP 2
20 #define VIRTQUEUE_MAX_SIZE 1024
21 #define VIRTIO_PCI_VRING_ALIGN 4096
24 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
25 /* We have found a driver for the device. */
26 #define VIRTIO_CONFIG_S_DRIVER 2
27 /* Driver has used its parts of the config, and is happy */
28 #define VIRTIO_CONFIG_S_DRIVER_OK 4
29 /* We've given up on this device. */
30 #define VIRTIO_CONFIG_S_FAILED 0x80
32 /* Some virtio feature bits (currently bits 28 through 31) are reserved for the
33 * transport being used (eg. virtio_ring), the rest are per-device feature bits. */
34 #define VIRTIO_TRANSPORT_F_START 28
35 #define VIRTIO_TRANSPORT_F_END 32
37 /* We notify when the ring is completely used, even if the guest is suppressing
39 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
40 /* We support indirect buffer descriptors */
41 #define VIRTIO_RING_F_INDIRECT_DESC 28
42 /* A guest should never accept this. It implies negotiation is broken. */
43 #define VIRTIO_F_BAD_FEATURE 30
45 /* from Linux's linux/virtio_ring.h */
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 /* This means the buffer contains a list of buffer descriptors. */
52 #define VRING_DESC_F_INDIRECT 4
54 /* This means don't notify other side when buffer added. */
55 #define VRING_USED_F_NO_NOTIFY 1
56 /* This means don't interrupt guest when buffer consumed. */
57 #define VRING_AVAIL_F_NO_INTERRUPT 1
60 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
63 #define container_of(ptr, type, member) ({ \
64 const typeof(((type *) 0)->member) *__mptr = (ptr); \
65 (type *) ((char *) __mptr - offsetof(type, member));})
69 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
72 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
75 /* This is the last element of the write scatter-gather list */
76 struct virtio_blk_inhdr
81 /* SCSI pass-through header */
83 struct virtio_scsi_inhdr
93 void (*notify
)(void * opaque
, uint16_t vector
);
94 void (*save_config
)(void * opaque
, QEMUFile
*f
);
95 void (*save_queue
)(void * opaque
, int n
, QEMUFile
*f
);
96 int (*load_config
)(void * opaque
, QEMUFile
*f
);
97 int (*load_queue
)(void * opaque
, int n
, QEMUFile
*f
);
98 unsigned (*get_features
)(void * opaque
);
106 typedef struct VRingDesc
116 typedef struct VRingAvail
123 typedef struct VRingUsedElem
129 typedef struct VRingUsed
133 VRingUsedElem ring
[0];
139 target_phys_addr_t desc
;
140 target_phys_addr_t avail
;
141 target_phys_addr_t used
;
144 typedef struct VirtQueue
147 target_phys_addr_t pa
;
148 uint16_t last_avail_idx
;
151 // void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
154 typedef struct VirtQueueElement
157 unsigned int out_num
;
159 target_phys_addr_t in_addr
[VIRTQUEUE_MAX_SIZE
];
160 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
161 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
173 uint16_t config_vector
;
175 uint32_t (*get_features
)(VirtIODevice
*vdev
);
176 uint32_t (*bad_features
)(VirtIODevice
*vdev
);
177 void (*set_features
)(VirtIODevice
*vdev
, uint32_t val
);
178 void (*get_config
)(VirtIODevice
*vdev
, uint8_t *config
);
179 void (*set_config
)(VirtIODevice
*vdev
, const uint8_t *config
);
180 void (*reset
)(VirtIODevice
*vdev
);
182 const VirtIOBindings
*binding
;
183 void *binding_opaque
;
187 typedef struct VirtIOBlock
190 BlockDriverState
*bs
;
193 char serial_str
[BLOCK_SERIAL_STRLEN
+ 1];
198 typedef struct VirtIOBlockReq
201 VirtQueueElement elem
;
202 struct virtio_blk_inhdr
*in
;
203 struct virtio_blk_outhdr
*out
;
204 struct virtio_scsi_inhdr
*scsi
;
206 struct VirtIOBlockReq
*next
;
209 typedef struct MultiReqBuffer
{
210 BlockRequest blkreq
[32];
212 BlockDriverState
*old_bs
;
215 void *cpu_physical_memory_map(target_phys_addr_t addr
,
216 target_phys_addr_t
*plen
,
218 void cpu_physical_memory_unmap(void *buffer
, target_phys_addr_t len
,
219 int is_write
, target_phys_addr_t access_len
);
222 static inline __u64
ldq_phys(target_phys_addr_t pa
) {
224 memcpy(&ret
,(void *)pa
,8);
228 static inline __u32
ldl_phys(target_phys_addr_t pa
) {
230 memcpy(&ret
,(void *)pa
,4);
234 static inline __u16
lduw_phys(target_phys_addr_t pa
) {
236 memcpy(&ret
,(void *)pa
,2);
240 static inline void stl_phys(target_phys_addr_t pa
, uint32_t val
) {
241 memcpy((void *)pa
,&val
,4);
244 static inline void stw_phys(target_phys_addr_t pa
, uint16_t val
) {
245 memcpy((void *)pa
,&val
,2);
248 void virtqueue_init(VirtQueue
*vq
);
249 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,unsigned int len
);
250 int virtqueue_pop(VirtQueue
*vq
, VirtQueueElement
*elem
);
251 void virtio_notify(VirtQueue
*vq
);