blob: 1af2de2714ffacc65dc2112259fc600333a3f836 [file] [log] [blame]
aliguori967f97f2008-12-04 19:38:57 +00001/*
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
Peter Maydell9b8bfe22016-01-26 18:17:07 +000014#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010015#include "qapi/error.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010016#include "qemu-common.h"
17#include "cpu.h"
Stefan Hajnoczi64979a42010-05-24 13:19:21 +010018#include "trace.h"
Edgar E. Iglesiasfdfba1a2013-11-15 14:46:38 +010019#include "exec/address-spaces.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/error-report.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010021#include "hw/virtio/virtio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/atomic.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010023#include "hw/virtio/virtio-bus.h"
Greg Kurz6b321a32014-06-24 19:22:30 +020024#include "migration/migration.h"
Rusty Russellcee3ca02014-06-24 19:40:16 +020025#include "hw/virtio/virtio-access.h"
aliguori967f97f2008-12-04 19:38:57 +000026
Peter Maydell6ce69d12013-07-16 13:25:08 +010027/*
28 * The alignment to use between consumer and producer parts of vring.
29 * x86 pagesize again. This is the default, used by transports like PCI
30 * which don't provide a means for the guest to tell the host the alignment.
31 */
aliguorif46f15b2008-12-04 19:58:45 +000032#define VIRTIO_PCI_VRING_ALIGN 4096
33
aliguori967f97f2008-12-04 19:38:57 +000034typedef struct VRingDesc
35{
36 uint64_t addr;
37 uint32_t len;
38 uint16_t flags;
39 uint16_t next;
40} VRingDesc;
41
42typedef struct VRingAvail
43{
44 uint16_t flags;
45 uint16_t idx;
46 uint16_t ring[0];
47} VRingAvail;
48
49typedef struct VRingUsedElem
50{
51 uint32_t id;
52 uint32_t len;
53} VRingUsedElem;
54
55typedef struct VRingUsed
56{
57 uint16_t flags;
58 uint16_t idx;
59 VRingUsedElem ring[0];
60} VRingUsed;
61
62typedef struct VRing
63{
64 unsigned int num;
Cornelia Huck46c5d082015-09-11 15:16:41 +020065 unsigned int num_default;
Peter Maydell6ce69d12013-07-16 13:25:08 +010066 unsigned int align;
Avi Kivitya8170e52012-10-23 12:30:10 +020067 hwaddr desc;
68 hwaddr avail;
69 hwaddr used;
aliguori967f97f2008-12-04 19:38:57 +000070} VRing;
71
72struct VirtQueue
73{
74 VRing vring;
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +010075
76 /* Next head to pop */
aliguori967f97f2008-12-04 19:38:57 +000077 uint16_t last_avail_idx;
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +010078
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +010079 /* Last avail_idx read from VQ. */
80 uint16_t shadow_avail_idx;
81
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +010082 uint16_t used_idx;
83
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +030084 /* Last used index value we have signalled on */
85 uint16_t signalled_used;
86
87 /* Last used index value we have signalled on */
88 bool signalled_used_valid;
89
90 /* Notification enabled? */
91 bool notification;
92
Jason Wange78a2b42013-01-30 19:12:37 +080093 uint16_t queue_index;
94
aliguori967f97f2008-12-04 19:38:57 +000095 int inuse;
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +030096
Michael S. Tsirkin7055e682009-06-21 19:50:13 +030097 uint16_t vector;
Fam Zhengbf1780b2016-07-13 13:09:43 +080098 VirtIOHandleOutput handle_output;
99 VirtIOHandleOutput handle_aio_output;
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +0200100 VirtIODevice *vdev;
101 EventNotifier guest_notifier;
102 EventNotifier host_notifier;
Jason Wange0d686b2015-04-23 14:21:46 +0800103 QLIST_ENTRY(VirtQueue) node;
aliguori967f97f2008-12-04 19:38:57 +0000104};
105
aliguori967f97f2008-12-04 19:38:57 +0000106/* virt queue functions */
Cornelia Huckab223c92015-06-04 12:34:12 +0200107void virtio_queue_update_rings(VirtIODevice *vdev, int n)
aliguori967f97f2008-12-04 19:38:57 +0000108{
Cornelia Huckab223c92015-06-04 12:34:12 +0200109 VRing *vring = &vdev->vq[n].vring;
Paul Brook53c25ce2009-05-18 14:51:59 +0100110
Cornelia Huckab223c92015-06-04 12:34:12 +0200111 if (!vring->desc) {
112 /* not yet setup -> nothing to do */
113 return;
114 }
115 vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
116 vring->used = vring_align(vring->avail +
117 offsetof(VRingAvail, ring[vring->num]),
118 vring->align);
aliguori967f97f2008-12-04 19:38:57 +0000119}
120
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100121static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
122 hwaddr desc_pa, int i)
aliguori967f97f2008-12-04 19:38:57 +0000123{
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100124 address_space_read(&address_space_memory, desc_pa + i * sizeof(VRingDesc),
125 MEMTXATTRS_UNSPECIFIED, (void *)desc, sizeof(VRingDesc));
126 virtio_tswap64s(vdev, &desc->addr);
127 virtio_tswap32s(vdev, &desc->len);
128 virtio_tswap16s(vdev, &desc->flags);
129 virtio_tswap16s(vdev, &desc->next);
aliguori967f97f2008-12-04 19:38:57 +0000130}
131
132static inline uint16_t vring_avail_flags(VirtQueue *vq)
133{
Avi Kivitya8170e52012-10-23 12:30:10 +0200134 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000135 pa = vq->vring.avail + offsetof(VRingAvail, flags);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200136 return virtio_lduw_phys(vq->vdev, pa);
aliguori967f97f2008-12-04 19:38:57 +0000137}
138
139static inline uint16_t vring_avail_idx(VirtQueue *vq)
140{
Avi Kivitya8170e52012-10-23 12:30:10 +0200141 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000142 pa = vq->vring.avail + offsetof(VRingAvail, idx);
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100143 vq->shadow_avail_idx = virtio_lduw_phys(vq->vdev, pa);
144 return vq->shadow_avail_idx;
aliguori967f97f2008-12-04 19:38:57 +0000145}
146
147static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
148{
Avi Kivitya8170e52012-10-23 12:30:10 +0200149 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000150 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200151 return virtio_lduw_phys(vq->vdev, pa);
aliguori967f97f2008-12-04 19:38:57 +0000152}
153
Michael S. Tsirkine9600c62015-02-16 22:35:46 +0100154static inline uint16_t vring_get_used_event(VirtQueue *vq)
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300155{
156 return vring_avail_ring(vq, vq->vring.num);
157}
158
Vincenzo Maffione1cdd2ee2016-01-31 11:29:06 +0100159static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
160 int i)
aliguori967f97f2008-12-04 19:38:57 +0000161{
Avi Kivitya8170e52012-10-23 12:30:10 +0200162 hwaddr pa;
Vincenzo Maffione1cdd2ee2016-01-31 11:29:06 +0100163 virtio_tswap32s(vq->vdev, &uelem->id);
164 virtio_tswap32s(vq->vdev, &uelem->len);
165 pa = vq->vring.used + offsetof(VRingUsed, ring[i]);
166 address_space_write(&address_space_memory, pa, MEMTXATTRS_UNSPECIFIED,
167 (void *)uelem, sizeof(VRingUsedElem));
aliguori967f97f2008-12-04 19:38:57 +0000168}
169
170static uint16_t vring_used_idx(VirtQueue *vq)
171{
Avi Kivitya8170e52012-10-23 12:30:10 +0200172 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000173 pa = vq->vring.used + offsetof(VRingUsed, idx);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200174 return virtio_lduw_phys(vq->vdev, pa);
aliguori967f97f2008-12-04 19:38:57 +0000175}
176
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300177static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
aliguori967f97f2008-12-04 19:38:57 +0000178{
Avi Kivitya8170e52012-10-23 12:30:10 +0200179 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000180 pa = vq->vring.used + offsetof(VRingUsed, idx);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200181 virtio_stw_phys(vq->vdev, pa, val);
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +0100182 vq->used_idx = val;
aliguori967f97f2008-12-04 19:38:57 +0000183}
184
185static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
186{
Rusty Russellcee3ca02014-06-24 19:40:16 +0200187 VirtIODevice *vdev = vq->vdev;
Avi Kivitya8170e52012-10-23 12:30:10 +0200188 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000189 pa = vq->vring.used + offsetof(VRingUsed, flags);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200190 virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask);
aliguori967f97f2008-12-04 19:38:57 +0000191}
192
193static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
194{
Rusty Russellcee3ca02014-06-24 19:40:16 +0200195 VirtIODevice *vdev = vq->vdev;
Avi Kivitya8170e52012-10-23 12:30:10 +0200196 hwaddr pa;
aliguori967f97f2008-12-04 19:38:57 +0000197 pa = vq->vring.used + offsetof(VRingUsed, flags);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200198 virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask);
aliguori967f97f2008-12-04 19:38:57 +0000199}
200
Michael S. Tsirkine9600c62015-02-16 22:35:46 +0100201static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300202{
Avi Kivitya8170e52012-10-23 12:30:10 +0200203 hwaddr pa;
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300204 if (!vq->notification) {
205 return;
206 }
207 pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
Rusty Russellcee3ca02014-06-24 19:40:16 +0200208 virtio_stw_phys(vq->vdev, pa, val);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300209}
210
aliguori967f97f2008-12-04 19:38:57 +0000211void virtio_queue_set_notification(VirtQueue *vq, int enable)
212{
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300213 vq->notification = enable;
Cornelia Huck95129d62015-08-17 11:48:29 +0200214 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkine9600c62015-02-16 22:35:46 +0100215 vring_set_avail_event(vq, vring_avail_idx(vq));
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300216 } else if (enable) {
aliguori967f97f2008-12-04 19:38:57 +0000217 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300218 } else {
aliguori967f97f2008-12-04 19:38:57 +0000219 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300220 }
Michael S. Tsirkin92045d82012-04-23 14:11:14 +0300221 if (enable) {
222 /* Expose avail event/used flags before caller checks the avail idx. */
223 smp_mb();
224 }
aliguori967f97f2008-12-04 19:38:57 +0000225}
226
227int virtio_queue_ready(VirtQueue *vq)
228{
229 return vq->vring.avail != 0;
230}
231
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100232/* Fetch avail_idx from VQ memory only when we really need to know if
233 * guest has added some buffers. */
aliguori967f97f2008-12-04 19:38:57 +0000234int virtio_queue_empty(VirtQueue *vq)
235{
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100236 if (vq->shadow_avail_idx != vq->last_avail_idx) {
237 return 0;
238 }
239
aliguori967f97f2008-12-04 19:38:57 +0000240 return vring_avail_idx(vq) == vq->last_avail_idx;
241}
242
Jason Wangce317462015-09-25 13:21:28 +0800243static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
244 unsigned int len)
aliguori967f97f2008-12-04 19:38:57 +0000245{
246 unsigned int offset;
247 int i;
248
aliguori967f97f2008-12-04 19:38:57 +0000249 offset = 0;
250 for (i = 0; i < elem->in_num; i++) {
251 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
252
aliguori26b258e2009-03-28 17:46:18 +0000253 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
254 elem->in_sg[i].iov_len,
255 1, size);
aliguori967f97f2008-12-04 19:38:57 +0000256
Michael S. Tsirkin0cea71a2012-09-24 15:09:30 +0200257 offset += size;
aliguori967f97f2008-12-04 19:38:57 +0000258 }
259
aliguori26b258e2009-03-28 17:46:18 +0000260 for (i = 0; i < elem->out_num; i++)
261 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
262 elem->out_sg[i].iov_len,
263 0, elem->out_sg[i].iov_len);
Jason Wangce317462015-09-25 13:21:28 +0800264}
265
Stefan Hajnoczi2640d2a2016-09-19 14:28:03 +0100266/* virtqueue_detach_element:
267 * @vq: The #VirtQueue
268 * @elem: The #VirtQueueElement
269 * @len: number of bytes written
270 *
271 * Detach the element from the virtqueue. This function is suitable for device
272 * reset or other situations where a #VirtQueueElement is simply freed and will
273 * not be pushed or discarded.
274 */
275void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
276 unsigned int len)
277{
278 vq->inuse--;
279 virtqueue_unmap_sg(vq, elem, len);
280}
281
Ladi Prosek27e57ef2016-11-03 09:55:49 +0100282/* virtqueue_unpop:
Stefan Hajnoczi2640d2a2016-09-19 14:28:03 +0100283 * @vq: The #VirtQueue
284 * @elem: The #VirtQueueElement
285 * @len: number of bytes written
286 *
287 * Pretend the most recent element wasn't popped from the virtqueue. The next
288 * call to virtqueue_pop() will refetch the element.
289 */
Ladi Prosek27e57ef2016-11-03 09:55:49 +0100290void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
291 unsigned int len)
Jason Wang29b9f5e2015-09-25 13:21:29 +0800292{
293 vq->last_avail_idx--;
Stefan Hajnoczi2640d2a2016-09-19 14:28:03 +0100294 virtqueue_detach_element(vq, elem, len);
Jason Wang29b9f5e2015-09-25 13:21:29 +0800295}
296
Stefan Hajnoczi297a75e2016-09-07 17:20:48 +0200297/* virtqueue_rewind:
298 * @vq: The #VirtQueue
299 * @num: Number of elements to push back
300 *
301 * Pretend that elements weren't popped from the virtqueue. The next
302 * virtqueue_pop() will refetch the oldest element.
303 *
Ladi Prosek27e57ef2016-11-03 09:55:49 +0100304 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
Stefan Hajnoczi297a75e2016-09-07 17:20:48 +0200305 *
306 * Returns: true on success, false if @num is greater than the number of in use
307 * elements.
308 */
309bool virtqueue_rewind(VirtQueue *vq, unsigned int num)
310{
311 if (num > vq->inuse) {
312 return false;
313 }
314 vq->last_avail_idx -= num;
315 vq->inuse -= num;
316 return true;
317}
318
Jason Wangce317462015-09-25 13:21:28 +0800319void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
320 unsigned int len, unsigned int idx)
321{
Vincenzo Maffione1cdd2ee2016-01-31 11:29:06 +0100322 VRingUsedElem uelem;
323
Jason Wangce317462015-09-25 13:21:28 +0800324 trace_virtqueue_fill(vq, elem, len, idx);
325
326 virtqueue_unmap_sg(vq, elem, len);
aliguori26b258e2009-03-28 17:46:18 +0000327
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +0100328 if (unlikely(vq->vdev->broken)) {
329 return;
330 }
331
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +0100332 idx = (idx + vq->used_idx) % vq->vring.num;
aliguori967f97f2008-12-04 19:38:57 +0000333
Vincenzo Maffione1cdd2ee2016-01-31 11:29:06 +0100334 uelem.id = elem->index;
335 uelem.len = len;
336 vring_used_write(vq, &uelem, idx);
aliguori967f97f2008-12-04 19:38:57 +0000337}
338
339void virtqueue_flush(VirtQueue *vq, unsigned int count)
340{
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300341 uint16_t old, new;
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +0100342
343 if (unlikely(vq->vdev->broken)) {
344 vq->inuse -= count;
345 return;
346 }
347
aliguori967f97f2008-12-04 19:38:57 +0000348 /* Make sure buffer is written before we update index. */
David Gibsonb90d2f32011-09-20 12:05:20 +1000349 smp_wmb();
Stefan Hajnoczi64979a42010-05-24 13:19:21 +0100350 trace_virtqueue_flush(vq, count);
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +0100351 old = vq->used_idx;
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300352 new = old + count;
353 vring_used_idx_set(vq, new);
aliguori967f97f2008-12-04 19:38:57 +0000354 vq->inuse -= count;
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300355 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
356 vq->signalled_used_valid = false;
aliguori967f97f2008-12-04 19:38:57 +0000357}
358
359void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
360 unsigned int len)
361{
362 virtqueue_fill(vq, elem, len, 0);
363 virtqueue_flush(vq, 1);
364}
365
366static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
367{
368 uint16_t num_heads = vring_avail_idx(vq) - idx;
369
370 /* Check it isn't doing very strange things with descriptor numbers. */
aliguoribb6834c2008-12-04 21:28:28 +0000371 if (num_heads > vq->vring.num) {
Stefan Hajnoczi4355c1a2016-09-21 16:52:25 +0100372 virtio_error(vq->vdev, "Guest moved used index from %u to %u",
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100373 idx, vq->shadow_avail_idx);
Stefan Hajnoczi4355c1a2016-09-21 16:52:25 +0100374 return -EINVAL;
aliguoribb6834c2008-12-04 21:28:28 +0000375 }
Michael S. Tsirkina821ce52012-04-23 15:46:22 +0300376 /* On success, callers read a descriptor at vq->last_avail_idx.
377 * Make sure descriptor read does not bypass avail index read. */
378 if (num_heads) {
379 smp_rmb();
380 }
aliguori967f97f2008-12-04 19:38:57 +0000381
382 return num_heads;
383}
384
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100385static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
386 unsigned int *head)
aliguori967f97f2008-12-04 19:38:57 +0000387{
aliguori967f97f2008-12-04 19:38:57 +0000388 /* Grab the next descriptor number they're advertising, and increment
389 * the index we've seen. */
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100390 *head = vring_avail_ring(vq, idx % vq->vring.num);
aliguori967f97f2008-12-04 19:38:57 +0000391
392 /* If their number is silly, that's a fatal mistake. */
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100393 if (*head >= vq->vring.num) {
394 virtio_error(vq->vdev, "Guest says index %u is available", *head);
395 return false;
aliguoribb6834c2008-12-04 21:28:28 +0000396 }
aliguori967f97f2008-12-04 19:38:57 +0000397
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100398 return true;
aliguori967f97f2008-12-04 19:38:57 +0000399}
400
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100401enum {
402 VIRTQUEUE_READ_DESC_ERROR = -1,
403 VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
404 VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
405};
aliguori967f97f2008-12-04 19:38:57 +0000406
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100407static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
408 hwaddr desc_pa, unsigned int max,
409 unsigned int *next)
410{
aliguori967f97f2008-12-04 19:38:57 +0000411 /* If this descriptor says it doesn't chain, we're done. */
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100412 if (!(desc->flags & VRING_DESC_F_NEXT)) {
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100413 return VIRTQUEUE_READ_DESC_DONE;
Rusty Russellcee3ca02014-06-24 19:40:16 +0200414 }
aliguori967f97f2008-12-04 19:38:57 +0000415
416 /* Check they're not leading us off end of descriptors. */
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100417 *next = desc->next;
aliguori967f97f2008-12-04 19:38:57 +0000418 /* Make sure compiler knows to grab that: we don't want it changing! */
David Gibsonb90d2f32011-09-20 12:05:20 +1000419 smp_wmb();
aliguori967f97f2008-12-04 19:38:57 +0000420
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100421 if (*next >= max) {
422 virtio_error(vdev, "Desc next is %u", *next);
423 return VIRTQUEUE_READ_DESC_ERROR;
aliguoribb6834c2008-12-04 21:28:28 +0000424 }
aliguori967f97f2008-12-04 19:38:57 +0000425
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100426 vring_desc_read(vdev, desc, desc_pa, *next);
427 return VIRTQUEUE_READ_DESC_MORE;
aliguori967f97f2008-12-04 19:38:57 +0000428}
429
Amit Shah0d8d7692012-09-25 00:05:15 +0530430void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +0200431 unsigned int *out_bytes,
432 unsigned max_in_bytes, unsigned max_out_bytes)
aliguori967f97f2008-12-04 19:38:57 +0000433{
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100434 unsigned int idx;
Amit Shah385ce952012-09-25 00:05:14 +0530435 unsigned int total_bufs, in_total, out_total;
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100436 int rc;
aliguori967f97f2008-12-04 19:38:57 +0000437
438 idx = vq->last_avail_idx;
439
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100440 total_bufs = in_total = out_total = 0;
Stefan Hajnoczi4355c1a2016-09-21 16:52:25 +0100441 while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
Rusty Russellcee3ca02014-06-24 19:40:16 +0200442 VirtIODevice *vdev = vq->vdev;
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100443 unsigned int max, num_bufs, indirect = 0;
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100444 VRingDesc desc;
Avi Kivitya8170e52012-10-23 12:30:10 +0200445 hwaddr desc_pa;
Stefan Hajnoczib1c7c072016-09-21 16:52:23 +0100446 unsigned int i;
aliguori967f97f2008-12-04 19:38:57 +0000447
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100448 max = vq->vring.num;
449 num_bufs = total_bufs;
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100450
451 if (!virtqueue_get_head(vq, idx++, &i)) {
452 goto err;
453 }
454
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100455 desc_pa = vq->vring.desc;
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100456 vring_desc_read(vdev, &desc, desc_pa, i);
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100457
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100458 if (desc.flags & VRING_DESC_F_INDIRECT) {
459 if (desc.len % sizeof(VRingDesc)) {
Stefan Hajnoczid65abf82016-09-21 16:52:22 +0100460 virtio_error(vdev, "Invalid size for indirect buffer table");
461 goto err;
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100462 }
463
464 /* If we've got too many, that implies a descriptor loop. */
465 if (num_bufs >= max) {
Stefan Hajnoczid65abf82016-09-21 16:52:22 +0100466 virtio_error(vdev, "Looped descriptor");
467 goto err;
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100468 }
469
470 /* loop over the indirect descriptor table */
471 indirect = 1;
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100472 max = desc.len / sizeof(VRingDesc);
473 desc_pa = desc.addr;
yinyin1ae27572013-08-22 14:47:16 +0800474 num_bufs = i = 0;
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100475 vring_desc_read(vdev, &desc, desc_pa, i);
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100476 }
477
aliguori967f97f2008-12-04 19:38:57 +0000478 do {
479 /* If we've got too many, that implies a descriptor loop. */
Mark McLoughlin5774cf92009-06-17 11:37:32 +0100480 if (++num_bufs > max) {
Stefan Hajnoczid65abf82016-09-21 16:52:22 +0100481 virtio_error(vdev, "Looped descriptor");
482 goto err;
aliguoribb6834c2008-12-04 21:28:28 +0000483 }
aliguori967f97f2008-12-04 19:38:57 +0000484
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100485 if (desc.flags & VRING_DESC_F_WRITE) {
486 in_total += desc.len;
aliguori967f97f2008-12-04 19:38:57 +0000487 } else {
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100488 out_total += desc.len;
aliguori967f97f2008-12-04 19:38:57 +0000489 }
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +0200490 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
491 goto done;
492 }
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100493
494 rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
495 } while (rc == VIRTQUEUE_READ_DESC_MORE);
496
497 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
498 goto err;
499 }
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100500
501 if (!indirect)
502 total_bufs = num_bufs;
503 else
504 total_bufs++;
aliguori967f97f2008-12-04 19:38:57 +0000505 }
Stefan Hajnoczi4355c1a2016-09-21 16:52:25 +0100506
507 if (rc < 0) {
508 goto err;
509 }
510
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +0200511done:
Amit Shah0d8d7692012-09-25 00:05:15 +0530512 if (in_bytes) {
513 *in_bytes = in_total;
514 }
515 if (out_bytes) {
516 *out_bytes = out_total;
517 }
Stefan Hajnoczid65abf82016-09-21 16:52:22 +0100518 return;
519
520err:
521 in_total = out_total = 0;
522 goto done;
Amit Shah0d8d7692012-09-25 00:05:15 +0530523}
aliguori967f97f2008-12-04 19:38:57 +0000524
Amit Shah0d8d7692012-09-25 00:05:15 +0530525int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
526 unsigned int out_bytes)
527{
528 unsigned int in_total, out_total;
529
Michael S. Tsirkine1f7b482012-11-30 00:02:56 +0200530 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
531 return in_bytes <= in_total && out_bytes <= out_total;
aliguori967f97f2008-12-04 19:38:57 +0000532}
533
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100534static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
535 hwaddr *addr, struct iovec *iov,
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100536 unsigned int max_num_sg, bool is_write,
537 hwaddr pa, size_t sz)
538{
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100539 bool ok = false;
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100540 unsigned num_sg = *p_num_sg;
541 assert(num_sg <= max_num_sg);
542
Prasad J Pandit1e7aed72016-07-27 21:07:56 +0530543 if (!sz) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100544 virtio_error(vdev, "virtio: zero sized buffers are not allowed");
545 goto out;
Prasad J Pandit1e7aed72016-07-27 21:07:56 +0530546 }
547
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100548 while (sz) {
549 hwaddr len = sz;
550
551 if (num_sg == max_num_sg) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100552 virtio_error(vdev, "virtio: too many write descriptors in "
553 "indirect table");
554 goto out;
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100555 }
556
557 iov[num_sg].iov_base = cpu_physical_memory_map(pa, &len, is_write);
Prasad J Pandit973e7172016-09-19 23:55:45 +0530558 if (!iov[num_sg].iov_base) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100559 virtio_error(vdev, "virtio: bogus descriptor or out of resources");
560 goto out;
Prasad J Pandit973e7172016-09-19 23:55:45 +0530561 }
562
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100563 iov[num_sg].iov_len = len;
564 addr[num_sg] = pa;
565
566 sz -= len;
567 pa += len;
568 num_sg++;
569 }
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100570 ok = true;
571
572out:
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100573 *p_num_sg = num_sg;
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100574 return ok;
575}
576
577/* Only used by error code paths before we have a VirtQueueElement (therefore
578 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
579 * yet.
580 */
581static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
582 struct iovec *iov)
583{
584 unsigned int i;
585
586 for (i = 0; i < out_num + in_num; i++) {
587 int is_write = i >= out_num;
588
589 cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0);
590 iov++;
591 }
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100592}
593
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200594static void virtqueue_map_iovec(struct iovec *sg, hwaddr *addr,
595 unsigned int *num_sg, unsigned int max_size,
596 int is_write)
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200597{
598 unsigned int i;
Avi Kivitya8170e52012-10-23 12:30:10 +0200599 hwaddr len;
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200600
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200601 /* Note: this function MUST validate input, some callers
602 * are passing in num_sg values received over the network.
603 */
604 /* TODO: teach all callers that this can fail, and return failure instead
605 * of asserting here.
606 * When we do, we might be able to re-enable NDEBUG below.
607 */
608#ifdef NDEBUG
609#error building with NDEBUG is not supported
610#endif
611 assert(*num_sg <= max_size);
Michael S. Tsirkin36cf2a32014-04-03 19:51:53 +0300612
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200613 for (i = 0; i < *num_sg; i++) {
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200614 len = sg[i].iov_len;
615 sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200616 if (!sg[i].iov_base) {
Michael Tokarev1a285892014-06-10 19:56:27 +0400617 error_report("virtio: error trying to map MMIO memory");
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200618 exit(1);
619 }
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100620 if (len != sg[i].iov_len) {
621 error_report("virtio: unexpected memory split");
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200622 exit(1);
623 }
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200624 }
625}
626
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200627void virtqueue_map(VirtQueueElement *elem)
628{
629 virtqueue_map_iovec(elem->in_sg, elem->in_addr, &elem->in_num,
Paolo Bonzini37246502016-01-31 11:29:00 +0100630 VIRTQUEUE_MAX_SIZE, 1);
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200631 virtqueue_map_iovec(elem->out_sg, elem->out_addr, &elem->out_num,
Paolo Bonzini37246502016-01-31 11:29:00 +0100632 VIRTQUEUE_MAX_SIZE, 0);
633}
634
Ladi Prosekbf91bd22016-11-03 09:55:50 +0100635static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
Paolo Bonzini37246502016-01-31 11:29:00 +0100636{
637 VirtQueueElement *elem;
638 size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
639 size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
640 size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
641 size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
642 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
643 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
644
645 assert(sz >= sizeof(VirtQueueElement));
646 elem = g_malloc(out_sg_end);
647 elem->out_num = out_num;
648 elem->in_num = in_num;
649 elem->in_addr = (void *)elem + in_addr_ofs;
650 elem->out_addr = (void *)elem + out_addr_ofs;
651 elem->in_sg = (void *)elem + in_sg_ofs;
652 elem->out_sg = (void *)elem + out_sg_ofs;
653 return elem;
Michael S. Tsirkin8059fee2015-10-27 10:01:44 +0200654}
655
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200656void *virtqueue_pop(VirtQueue *vq, size_t sz)
aliguori967f97f2008-12-04 19:38:57 +0000657{
Mark McLoughlin5774cf92009-06-17 11:37:32 +0100658 unsigned int i, head, max;
Avi Kivitya8170e52012-10-23 12:30:10 +0200659 hwaddr desc_pa = vq->vring.desc;
Rusty Russellcee3ca02014-06-24 19:40:16 +0200660 VirtIODevice *vdev = vq->vdev;
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200661 VirtQueueElement *elem;
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100662 unsigned out_num, in_num;
663 hwaddr addr[VIRTQUEUE_MAX_SIZE];
664 struct iovec iov[VIRTQUEUE_MAX_SIZE];
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100665 VRingDesc desc;
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100666 int rc;
aliguori967f97f2008-12-04 19:38:57 +0000667
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +0100668 if (unlikely(vdev->broken)) {
669 return NULL;
670 }
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100671 if (virtio_queue_empty(vq)) {
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200672 return NULL;
673 }
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100674 /* Needed after virtio_queue_empty(), see comment in
675 * virtqueue_num_heads(). */
676 smp_rmb();
aliguori967f97f2008-12-04 19:38:57 +0000677
678 /* When we start there are none of either input nor output. */
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100679 out_num = in_num = 0;
aliguori967f97f2008-12-04 19:38:57 +0000680
Mark McLoughlin5774cf92009-06-17 11:37:32 +0100681 max = vq->vring.num;
682
Stefan Hajnocziafd90962016-07-19 13:07:13 +0100683 if (vq->inuse >= vq->vring.num) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100684 virtio_error(vdev, "Virtqueue size exceeded");
685 return NULL;
Stefan Hajnocziafd90962016-07-19 13:07:13 +0100686 }
687
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100688 if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
689 return NULL;
690 }
691
Cornelia Huck95129d62015-08-17 11:48:29 +0200692 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkine9600c62015-02-16 22:35:46 +0100693 vring_set_avail_event(vq, vq->last_avail_idx);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300694 }
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100695
Stefan Hajnoczifb1131b2016-09-21 16:52:26 +0100696 i = head;
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100697 vring_desc_read(vdev, &desc, desc_pa, i);
698 if (desc.flags & VRING_DESC_F_INDIRECT) {
699 if (desc.len % sizeof(VRingDesc)) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100700 virtio_error(vdev, "Invalid size for indirect buffer table");
701 return NULL;
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100702 }
703
704 /* loop over the indirect descriptor table */
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100705 max = desc.len / sizeof(VRingDesc);
706 desc_pa = desc.addr;
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100707 i = 0;
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100708 vring_desc_read(vdev, &desc, desc_pa, i);
Mark McLoughlinefeea6d2009-06-17 11:38:28 +0100709 }
710
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200711 /* Collect all the descriptors */
aliguori967f97f2008-12-04 19:38:57 +0000712 do {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100713 bool map_ok;
714
Paolo Bonziniaa570d62016-01-31 11:29:03 +0100715 if (desc.flags & VRING_DESC_F_WRITE) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100716 map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
717 iov + out_num,
718 VIRTQUEUE_MAX_SIZE - out_num, true,
719 desc.addr, desc.len);
Kevin Wolf42fb2e02010-08-03 16:54:38 +0200720 } else {
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100721 if (in_num) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100722 virtio_error(vdev, "Incorrect order for descriptors");
723 goto err_undo_map;
Michael S. Tsirkinc8eac1c2011-06-20 13:42:27 +0300724 }
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100725 map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
726 VIRTQUEUE_MAX_SIZE, false,
727 desc.addr, desc.len);
728 }
729 if (!map_ok) {
730 goto err_undo_map;
aliguoribb6834c2008-12-04 21:28:28 +0000731 }
aliguori967f97f2008-12-04 19:38:57 +0000732
733 /* If we've got too many, that implies a descriptor loop. */
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100734 if ((in_num + out_num) > max) {
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100735 virtio_error(vdev, "Looped descriptor");
736 goto err_undo_map;
aliguoribb6834c2008-12-04 21:28:28 +0000737 }
Stefan Hajnoczi412e0e82016-09-21 16:52:24 +0100738
739 rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
740 } while (rc == VIRTQUEUE_READ_DESC_MORE);
741
742 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
743 goto err_undo_map;
744 }
aliguori967f97f2008-12-04 19:38:57 +0000745
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100746 /* Now copy what we have collected and mapped */
747 elem = virtqueue_alloc_element(sz, out_num, in_num);
aliguori967f97f2008-12-04 19:38:57 +0000748 elem->index = head;
Paolo Bonzini3b3b0622016-01-31 11:29:01 +0100749 for (i = 0; i < out_num; i++) {
750 elem->out_addr[i] = addr[i];
751 elem->out_sg[i] = iov[i];
752 }
753 for (i = 0; i < in_num; i++) {
754 elem->in_addr[i] = addr[out_num + i];
755 elem->in_sg[i] = iov[out_num + i];
756 }
aliguori967f97f2008-12-04 19:38:57 +0000757
758 vq->inuse++;
759
Stefan Hajnoczi64979a42010-05-24 13:19:21 +0100760 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200761 return elem;
Stefan Hajnocziec55da12016-09-21 16:52:21 +0100762
763err_undo_map:
764 virtqueue_undo_map_desc(out_num, in_num, iov);
765 return NULL;
aliguori967f97f2008-12-04 19:38:57 +0000766}
767
Paolo Bonzini37246502016-01-31 11:29:00 +0100768/* Reading and writing a structure directly to QEMUFile is *awful*, but
769 * it is what QEMU has always done by mistake. We can change it sooner
770 * or later by bumping the version number of the affected vm states.
771 * In the meanwhile, since the in-memory layout of VirtQueueElement
772 * has changed, we need to marshal to and from the layout that was
773 * used before the change.
774 */
775typedef struct VirtQueueElementOld {
776 unsigned int index;
777 unsigned int out_num;
778 unsigned int in_num;
779 hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
780 hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
781 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
782 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
783} VirtQueueElementOld;
784
Paolo Bonziniab281c12016-01-31 11:28:59 +0100785void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz)
786{
Paolo Bonzini37246502016-01-31 11:29:00 +0100787 VirtQueueElement *elem;
788 VirtQueueElementOld data;
789 int i;
790
791 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
792
793 elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
794 elem->index = data.index;
795
796 for (i = 0; i < elem->in_num; i++) {
797 elem->in_addr[i] = data.in_addr[i];
798 }
799
800 for (i = 0; i < elem->out_num; i++) {
801 elem->out_addr[i] = data.out_addr[i];
802 }
803
804 for (i = 0; i < elem->in_num; i++) {
805 /* Base is overwritten by virtqueue_map. */
806 elem->in_sg[i].iov_base = 0;
807 elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
808 }
809
810 for (i = 0; i < elem->out_num; i++) {
811 /* Base is overwritten by virtqueue_map. */
812 elem->out_sg[i].iov_base = 0;
813 elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
814 }
815
Paolo Bonziniab281c12016-01-31 11:28:59 +0100816 virtqueue_map(elem);
817 return elem;
818}
819
820void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem)
821{
Paolo Bonzini37246502016-01-31 11:29:00 +0100822 VirtQueueElementOld data;
823 int i;
824
825 memset(&data, 0, sizeof(data));
826 data.index = elem->index;
827 data.in_num = elem->in_num;
828 data.out_num = elem->out_num;
829
830 for (i = 0; i < elem->in_num; i++) {
831 data.in_addr[i] = elem->in_addr[i];
832 }
833
834 for (i = 0; i < elem->out_num; i++) {
835 data.out_addr[i] = elem->out_addr[i];
836 }
837
838 for (i = 0; i < elem->in_num; i++) {
839 /* Base is overwritten by virtqueue_map when loading. Do not
840 * save it, as it would leak the QEMU address space layout. */
841 data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
842 }
843
844 for (i = 0; i < elem->out_num; i++) {
845 /* Do not save iov_base as above. */
846 data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
847 }
848 qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
Paolo Bonziniab281c12016-01-31 11:28:59 +0100849}
850
aliguori967f97f2008-12-04 19:38:57 +0000851/* virtio device */
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300852static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
853{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200854 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
855 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
856
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +0100857 if (unlikely(vdev->broken)) {
858 return;
859 }
860
KONRAD Frederic1c819442013-04-24 10:21:21 +0200861 if (k->notify) {
862 k->notify(qbus->parent, vector);
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300863 }
864}
aliguori967f97f2008-12-04 19:38:57 +0000865
Paul Brook53c25ce2009-05-18 14:51:59 +0100866void virtio_update_irq(VirtIODevice *vdev)
aliguori967f97f2008-12-04 19:38:57 +0000867{
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300868 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
aliguori967f97f2008-12-04 19:38:57 +0000869}
870
Cornelia Huck0b352fd2015-06-04 12:34:15 +0200871static int virtio_validate_features(VirtIODevice *vdev)
872{
873 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
874
875 if (k->validate_features) {
876 return k->validate_features(vdev);
877 } else {
878 return 0;
879 }
880}
881
882int virtio_set_status(VirtIODevice *vdev, uint8_t val)
Stefan Hajnoczi4e1837f2011-09-13 13:34:37 +0100883{
KONRAD Frederic181103c2013-04-24 10:21:20 +0200884 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
Stefan Hajnoczi4e1837f2011-09-13 13:34:37 +0100885 trace_virtio_set_status(vdev, val);
886
Cornelia Huck95129d62015-08-17 11:48:29 +0200887 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Cornelia Huck0b352fd2015-06-04 12:34:15 +0200888 if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
889 val & VIRTIO_CONFIG_S_FEATURES_OK) {
890 int ret = virtio_validate_features(vdev);
891
892 if (ret) {
893 return ret;
894 }
895 }
896 }
KONRAD Frederic181103c2013-04-24 10:21:20 +0200897 if (k->set_status) {
898 k->set_status(vdev, val);
Stefan Hajnoczi4e1837f2011-09-13 13:34:37 +0100899 }
900 vdev->status = val;
Cornelia Huck0b352fd2015-06-04 12:34:15 +0200901 return 0;
Stefan Hajnoczi4e1837f2011-09-13 13:34:37 +0100902}
903
Greg Kurz616a6552014-06-24 19:38:54 +0200904bool target_words_bigendian(void);
905static enum virtio_device_endian virtio_default_endian(void)
906{
907 if (target_words_bigendian()) {
908 return VIRTIO_DEVICE_ENDIAN_BIG;
909 } else {
910 return VIRTIO_DEVICE_ENDIAN_LITTLE;
911 }
912}
913
914static enum virtio_device_endian virtio_current_cpu_endian(void)
915{
916 CPUClass *cc = CPU_GET_CLASS(current_cpu);
917
918 if (cc->virtio_is_big_endian(current_cpu)) {
919 return VIRTIO_DEVICE_ENDIAN_BIG;
920 } else {
921 return VIRTIO_DEVICE_ENDIAN_LITTLE;
922 }
923}
924
Paul Brook53c25ce2009-05-18 14:51:59 +0100925void virtio_reset(void *opaque)
aliguori967f97f2008-12-04 19:38:57 +0000926{
927 VirtIODevice *vdev = opaque;
KONRAD Frederic181103c2013-04-24 10:21:20 +0200928 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +0000929 int i;
930
Michael S. Tsirkine0c472d2010-09-27 18:32:52 +0200931 virtio_set_status(vdev, 0);
Greg Kurz616a6552014-06-24 19:38:54 +0200932 if (current_cpu) {
933 /* Guest initiated reset */
934 vdev->device_endian = virtio_current_cpu_endian();
935 } else {
936 /* System reset */
937 vdev->device_endian = virtio_default_endian();
938 }
Michael S. Tsirkine0c472d2010-09-27 18:32:52 +0200939
KONRAD Frederic181103c2013-04-24 10:21:20 +0200940 if (k->reset) {
941 k->reset(vdev);
942 }
aliguori967f97f2008-12-04 19:38:57 +0000943
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +0100944 vdev->broken = false;
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200945 vdev->guest_features = 0;
aliguori967f97f2008-12-04 19:38:57 +0000946 vdev->queue_sel = 0;
947 vdev->status = 0;
Paolo Bonzini0687c372016-11-18 16:07:01 +0100948 atomic_set(&vdev->isr, 0);
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300949 vdev->config_vector = VIRTIO_NO_VECTOR;
950 virtio_notify_vector(vdev, vdev->config_vector);
aliguori967f97f2008-12-04 19:38:57 +0000951
Jason Wang87b3bd12015-05-29 14:15:31 +0800952 for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
aliguori967f97f2008-12-04 19:38:57 +0000953 vdev->vq[i].vring.desc = 0;
954 vdev->vq[i].vring.avail = 0;
955 vdev->vq[i].vring.used = 0;
956 vdev->vq[i].last_avail_idx = 0;
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +0100957 vdev->vq[i].shadow_avail_idx = 0;
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +0100958 vdev->vq[i].used_idx = 0;
Jason Wange0d686b2015-04-23 14:21:46 +0800959 virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +0300960 vdev->vq[i].signalled_used = 0;
961 vdev->vq[i].signalled_used_valid = false;
962 vdev->vq[i].notification = true;
Cornelia Huck46c5d082015-09-11 15:16:41 +0200963 vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
Stefan Hajnoczi4b7f91e2016-09-07 11:51:25 -0400964 vdev->vq[i].inuse = 0;
aliguori967f97f2008-12-04 19:38:57 +0000965 }
966}
967
Paul Brook53c25ce2009-05-18 14:51:59 +0100968uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
aliguori967f97f2008-12-04 19:38:57 +0000969{
KONRAD Frederic181103c2013-04-24 10:21:20 +0200970 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +0000971 uint8_t val;
972
Jason Wang5f5a1312013-05-07 13:42:49 +0800973 if (addr + sizeof(val) > vdev->config_len) {
aliguori967f97f2008-12-04 19:38:57 +0000974 return (uint32_t)-1;
Jason Wang5f5a1312013-05-07 13:42:49 +0800975 }
976
977 k->get_config(vdev, vdev->config);
aliguori967f97f2008-12-04 19:38:57 +0000978
Alexander Graf06dbfc62012-01-10 23:33:10 +0100979 val = ldub_p(vdev->config + addr);
aliguori967f97f2008-12-04 19:38:57 +0000980 return val;
981}
982
Paul Brook53c25ce2009-05-18 14:51:59 +0100983uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
aliguori967f97f2008-12-04 19:38:57 +0000984{
KONRAD Frederic181103c2013-04-24 10:21:20 +0200985 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +0000986 uint16_t val;
987
Jason Wang5f5a1312013-05-07 13:42:49 +0800988 if (addr + sizeof(val) > vdev->config_len) {
aliguori967f97f2008-12-04 19:38:57 +0000989 return (uint32_t)-1;
Jason Wang5f5a1312013-05-07 13:42:49 +0800990 }
991
992 k->get_config(vdev, vdev->config);
aliguori967f97f2008-12-04 19:38:57 +0000993
Alexander Graf06dbfc62012-01-10 23:33:10 +0100994 val = lduw_p(vdev->config + addr);
aliguori967f97f2008-12-04 19:38:57 +0000995 return val;
996}
997
Paul Brook53c25ce2009-05-18 14:51:59 +0100998uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
aliguori967f97f2008-12-04 19:38:57 +0000999{
KONRAD Frederic181103c2013-04-24 10:21:20 +02001000 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +00001001 uint32_t val;
1002
Jason Wang5f5a1312013-05-07 13:42:49 +08001003 if (addr + sizeof(val) > vdev->config_len) {
aliguori967f97f2008-12-04 19:38:57 +00001004 return (uint32_t)-1;
Jason Wang5f5a1312013-05-07 13:42:49 +08001005 }
1006
1007 k->get_config(vdev, vdev->config);
aliguori967f97f2008-12-04 19:38:57 +00001008
Alexander Graf06dbfc62012-01-10 23:33:10 +01001009 val = ldl_p(vdev->config + addr);
aliguori967f97f2008-12-04 19:38:57 +00001010 return val;
1011}
1012
Paul Brook53c25ce2009-05-18 14:51:59 +01001013void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
aliguori967f97f2008-12-04 19:38:57 +00001014{
KONRAD Frederic181103c2013-04-24 10:21:20 +02001015 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +00001016 uint8_t val = data;
1017
Jason Wang5f5a1312013-05-07 13:42:49 +08001018 if (addr + sizeof(val) > vdev->config_len) {
aliguori967f97f2008-12-04 19:38:57 +00001019 return;
Jason Wang5f5a1312013-05-07 13:42:49 +08001020 }
aliguori967f97f2008-12-04 19:38:57 +00001021
Alexander Graf06dbfc62012-01-10 23:33:10 +01001022 stb_p(vdev->config + addr, val);
aliguori967f97f2008-12-04 19:38:57 +00001023
KONRAD Frederic181103c2013-04-24 10:21:20 +02001024 if (k->set_config) {
1025 k->set_config(vdev, vdev->config);
1026 }
aliguori967f97f2008-12-04 19:38:57 +00001027}
1028
Paul Brook53c25ce2009-05-18 14:51:59 +01001029void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
aliguori967f97f2008-12-04 19:38:57 +00001030{
KONRAD Frederic181103c2013-04-24 10:21:20 +02001031 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +00001032 uint16_t val = data;
1033
Jason Wang5f5a1312013-05-07 13:42:49 +08001034 if (addr + sizeof(val) > vdev->config_len) {
aliguori967f97f2008-12-04 19:38:57 +00001035 return;
Jason Wang5f5a1312013-05-07 13:42:49 +08001036 }
aliguori967f97f2008-12-04 19:38:57 +00001037
Alexander Graf06dbfc62012-01-10 23:33:10 +01001038 stw_p(vdev->config + addr, val);
aliguori967f97f2008-12-04 19:38:57 +00001039
KONRAD Frederic181103c2013-04-24 10:21:20 +02001040 if (k->set_config) {
1041 k->set_config(vdev, vdev->config);
1042 }
aliguori967f97f2008-12-04 19:38:57 +00001043}
1044
Paul Brook53c25ce2009-05-18 14:51:59 +01001045void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
aliguori967f97f2008-12-04 19:38:57 +00001046{
KONRAD Frederic181103c2013-04-24 10:21:20 +02001047 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +00001048 uint32_t val = data;
1049
Jason Wang5f5a1312013-05-07 13:42:49 +08001050 if (addr + sizeof(val) > vdev->config_len) {
aliguori967f97f2008-12-04 19:38:57 +00001051 return;
Jason Wang5f5a1312013-05-07 13:42:49 +08001052 }
aliguori967f97f2008-12-04 19:38:57 +00001053
Alexander Graf06dbfc62012-01-10 23:33:10 +01001054 stl_p(vdev->config + addr, val);
aliguori967f97f2008-12-04 19:38:57 +00001055
KONRAD Frederic181103c2013-04-24 10:21:20 +02001056 if (k->set_config) {
1057 k->set_config(vdev, vdev->config);
1058 }
aliguori967f97f2008-12-04 19:38:57 +00001059}
1060
Michael S. Tsirkinadfb7432015-06-04 12:34:24 +02001061uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
1062{
1063 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1064 uint8_t val;
1065
1066 if (addr + sizeof(val) > vdev->config_len) {
1067 return (uint32_t)-1;
1068 }
1069
1070 k->get_config(vdev, vdev->config);
1071
1072 val = ldub_p(vdev->config + addr);
1073 return val;
1074}
1075
1076uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
1077{
1078 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1079 uint16_t val;
1080
1081 if (addr + sizeof(val) > vdev->config_len) {
1082 return (uint32_t)-1;
1083 }
1084
1085 k->get_config(vdev, vdev->config);
1086
1087 val = lduw_le_p(vdev->config + addr);
1088 return val;
1089}
1090
1091uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
1092{
1093 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1094 uint32_t val;
1095
1096 if (addr + sizeof(val) > vdev->config_len) {
1097 return (uint32_t)-1;
1098 }
1099
1100 k->get_config(vdev, vdev->config);
1101
1102 val = ldl_le_p(vdev->config + addr);
1103 return val;
1104}
1105
1106void virtio_config_modern_writeb(VirtIODevice *vdev,
1107 uint32_t addr, uint32_t data)
1108{
1109 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1110 uint8_t val = data;
1111
1112 if (addr + sizeof(val) > vdev->config_len) {
1113 return;
1114 }
1115
1116 stb_p(vdev->config + addr, val);
1117
1118 if (k->set_config) {
1119 k->set_config(vdev, vdev->config);
1120 }
1121}
1122
1123void virtio_config_modern_writew(VirtIODevice *vdev,
1124 uint32_t addr, uint32_t data)
1125{
1126 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1127 uint16_t val = data;
1128
1129 if (addr + sizeof(val) > vdev->config_len) {
1130 return;
1131 }
1132
1133 stw_le_p(vdev->config + addr, val);
1134
1135 if (k->set_config) {
1136 k->set_config(vdev, vdev->config);
1137 }
1138}
1139
1140void virtio_config_modern_writel(VirtIODevice *vdev,
1141 uint32_t addr, uint32_t data)
1142{
1143 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1144 uint32_t val = data;
1145
1146 if (addr + sizeof(val) > vdev->config_len) {
1147 return;
1148 }
1149
1150 stl_le_p(vdev->config + addr, val);
1151
1152 if (k->set_config) {
1153 k->set_config(vdev, vdev->config);
1154 }
1155}
1156
Avi Kivitya8170e52012-10-23 12:30:10 +02001157void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
aliguori967f97f2008-12-04 19:38:57 +00001158{
Cornelia Huckab223c92015-06-04 12:34:12 +02001159 vdev->vq[n].vring.desc = addr;
1160 virtio_queue_update_rings(vdev, n);
Paul Brook53c25ce2009-05-18 14:51:59 +01001161}
aliguori967f97f2008-12-04 19:38:57 +00001162
Avi Kivitya8170e52012-10-23 12:30:10 +02001163hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
Paul Brook53c25ce2009-05-18 14:51:59 +01001164{
Cornelia Huckab223c92015-06-04 12:34:12 +02001165 return vdev->vq[n].vring.desc;
1166}
1167
1168void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
1169 hwaddr avail, hwaddr used)
1170{
1171 vdev->vq[n].vring.desc = desc;
1172 vdev->vq[n].vring.avail = avail;
1173 vdev->vq[n].vring.used = used;
Paul Brook53c25ce2009-05-18 14:51:59 +01001174}
aliguori967f97f2008-12-04 19:38:57 +00001175
Peter Maydelle63c0ba2013-07-16 13:25:07 +01001176void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
1177{
Peter Maydellf6049f42013-07-26 16:41:27 +01001178 /* Don't allow guest to flip queue between existent and
1179 * nonexistent states, or to set it to an invalid size.
1180 */
1181 if (!!num != !!vdev->vq[n].vring.num ||
1182 num > VIRTQUEUE_MAX_SIZE ||
1183 num < 0) {
1184 return;
Peter Maydelle63c0ba2013-07-16 13:25:07 +01001185 }
Peter Maydellf6049f42013-07-26 16:41:27 +01001186 vdev->vq[n].vring.num = num;
Peter Maydelle63c0ba2013-07-16 13:25:07 +01001187}
1188
Jason Wange0d686b2015-04-23 14:21:46 +08001189VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
1190{
1191 return QLIST_FIRST(&vdev->vector_queues[vector]);
1192}
1193
1194VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
1195{
1196 return QLIST_NEXT(vq, node);
1197}
1198
Paul Brook53c25ce2009-05-18 14:51:59 +01001199int virtio_queue_get_num(VirtIODevice *vdev, int n)
1200{
1201 return vdev->vq[n].vring.num;
1202}
1203
Jason Wang8ad176a2015-05-29 14:15:26 +08001204int virtio_get_num_queues(VirtIODevice *vdev)
1205{
1206 int i;
1207
Jason Wang87b3bd12015-05-29 14:15:31 +08001208 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
Jason Wang8ad176a2015-05-29 14:15:26 +08001209 if (!virtio_queue_get_num(vdev, i)) {
1210 break;
1211 }
1212 }
1213
1214 return i;
1215}
1216
Peter Maydell6ce69d12013-07-16 13:25:08 +01001217void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
1218{
1219 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1220 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1221
Cornelia Huckab223c92015-06-04 12:34:12 +02001222 /* virtio-1 compliant devices cannot change the alignment */
Cornelia Huck95129d62015-08-17 11:48:29 +02001223 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Cornelia Huckab223c92015-06-04 12:34:12 +02001224 error_report("tried to modify queue alignment for virtio-1 device");
1225 return;
1226 }
Peter Maydell6ce69d12013-07-16 13:25:08 +01001227 /* Check that the transport told us it was going to do this
1228 * (so a buggy transport will immediately assert rather than
1229 * silently failing to migrate this state)
1230 */
1231 assert(k->has_variable_vring_alignment);
1232
1233 vdev->vq[n].vring.align = align;
Cornelia Huckab223c92015-06-04 12:34:12 +02001234 virtio_queue_update_rings(vdev, n);
Peter Maydell6ce69d12013-07-16 13:25:08 +01001235}
1236
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02001237static void virtio_queue_notify_aio_vq(VirtQueue *vq)
1238{
1239 if (vq->vring.desc && vq->handle_aio_output) {
1240 VirtIODevice *vdev = vq->vdev;
1241
1242 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1243 vq->handle_aio_output(vdev, vq);
1244 }
1245}
1246
Paolo Bonzini2b2cbca2016-04-06 12:16:22 +02001247static void virtio_queue_notify_vq(VirtQueue *vq)
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +00001248{
Jason Wang9e0f5b82015-03-12 17:50:18 +08001249 if (vq->vring.desc && vq->handle_output) {
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +00001250 VirtIODevice *vdev = vq->vdev;
Jason Wang9e0f5b82015-03-12 17:50:18 +08001251
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +01001252 if (unlikely(vdev->broken)) {
1253 return;
1254 }
1255
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +00001256 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1257 vq->handle_output(vdev, vq);
1258 }
1259}
1260
Paul Brook53c25ce2009-05-18 14:51:59 +01001261void virtio_queue_notify(VirtIODevice *vdev, int n)
1262{
Stefan Hajnoczi7157e2e2011-05-08 22:29:07 +01001263 virtio_queue_notify_vq(&vdev->vq[n]);
aliguori967f97f2008-12-04 19:38:57 +00001264}
1265
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001266uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
1267{
Jason Wang87b3bd12015-05-29 14:15:31 +08001268 return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001269 VIRTIO_NO_VECTOR;
1270}
1271
1272void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
1273{
Jason Wange0d686b2015-04-23 14:21:46 +08001274 VirtQueue *vq = &vdev->vq[n];
1275
Jason Wang87b3bd12015-05-29 14:15:31 +08001276 if (n < VIRTIO_QUEUE_MAX) {
Jason Wange0d686b2015-04-23 14:21:46 +08001277 if (vdev->vector_queues &&
1278 vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
1279 QLIST_REMOVE(vq, node);
1280 }
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001281 vdev->vq[n].vector = vector;
Jason Wange0d686b2015-04-23 14:21:46 +08001282 if (vdev->vector_queues &&
1283 vector != VIRTIO_NO_VECTOR) {
1284 QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
1285 }
1286 }
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001287}
1288
Paolo Bonzinif1ac6a52016-10-21 22:48:11 +02001289VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
1290 VirtIOHandleOutput handle_output)
aliguori967f97f2008-12-04 19:38:57 +00001291{
1292 int i;
1293
Jason Wang87b3bd12015-05-29 14:15:31 +08001294 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
aliguori967f97f2008-12-04 19:38:57 +00001295 if (vdev->vq[i].vring.num == 0)
1296 break;
1297 }
1298
Jason Wang87b3bd12015-05-29 14:15:31 +08001299 if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
aliguori967f97f2008-12-04 19:38:57 +00001300 abort();
1301
1302 vdev->vq[i].vring.num = queue_size;
Cornelia Huck46c5d082015-09-11 15:16:41 +02001303 vdev->vq[i].vring.num_default = queue_size;
Peter Maydell6ce69d12013-07-16 13:25:08 +01001304 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
aliguori967f97f2008-12-04 19:38:57 +00001305 vdev->vq[i].handle_output = handle_output;
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02001306 vdev->vq[i].handle_aio_output = NULL;
aliguori967f97f2008-12-04 19:38:57 +00001307
1308 return &vdev->vq[i];
1309}
1310
Jason Wangf23fd812013-01-30 19:12:36 +08001311void virtio_del_queue(VirtIODevice *vdev, int n)
1312{
Jason Wang87b3bd12015-05-29 14:15:31 +08001313 if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
Jason Wangf23fd812013-01-30 19:12:36 +08001314 abort();
1315 }
1316
1317 vdev->vq[n].vring.num = 0;
Cornelia Huck46c5d082015-09-11 15:16:41 +02001318 vdev->vq[n].vring.num_default = 0;
Jason Wangf23fd812013-01-30 19:12:36 +08001319}
1320
Paolo Bonzini0687c372016-11-18 16:07:01 +01001321static void virtio_set_isr(VirtIODevice *vdev, int value)
1322{
1323 uint8_t old = atomic_read(&vdev->isr);
1324
1325 /* Do not write ISR if it does not change, so that its cacheline remains
1326 * shared in the common case where the guest does not read it.
1327 */
1328 if ((old & value) != value) {
1329 atomic_or(&vdev->isr, value);
1330 }
1331}
1332
Paolo Bonziniadb3fed2016-02-14 18:17:07 +01001333bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001334{
1335 uint16_t old, new;
1336 bool v;
Michael S. Tsirkina281ebc2012-04-22 16:45:53 +03001337 /* We need to expose used array entries before checking used event. */
1338 smp_mb();
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001339 /* Always notify when queue is empty (when feature acknowledge) */
Cornelia Huck95129d62015-08-17 11:48:29 +02001340 if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +01001341 !vq->inuse && virtio_queue_empty(vq)) {
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001342 return true;
1343 }
1344
Cornelia Huck95129d62015-08-17 11:48:29 +02001345 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001346 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
1347 }
1348
1349 v = vq->signalled_used_valid;
1350 vq->signalled_used_valid = true;
1351 old = vq->signalled_used;
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +01001352 new = vq->signalled_used = vq->used_idx;
Michael S. Tsirkine9600c62015-02-16 22:35:46 +01001353 return !v || vring_need_event(vring_get_used_event(vq), new, old);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001354}
1355
Paolo Bonzini83d768b2016-11-18 16:07:02 +01001356void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
1357{
1358 if (!virtio_should_notify(vdev, vq)) {
1359 return;
1360 }
1361
1362 trace_virtio_notify_irqfd(vdev, vq);
1363
1364 /*
1365 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
1366 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
1367 * incorrectly polling this bit during crashdump and hibernation
1368 * in MSI mode, causing a hang if this bit is never updated.
1369 * Recent releases of Windows do not really shut down, but rather
1370 * log out and hibernate to make the next startup faster. Hence,
1371 * this manifested as a more serious hang during shutdown with
1372 *
1373 * Next driver release from 2016 fixed this problem, so working around it
1374 * is not a must, but it's easy to do so let's do it here.
1375 *
1376 * Note: it's safe to update ISR from any thread as it was switched
1377 * to an atomic operation.
1378 */
1379 virtio_set_isr(vq->vdev, 0x1);
1380 event_notifier_set(&vq->guest_notifier);
1381}
1382
aliguori967f97f2008-12-04 19:38:57 +00001383void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
1384{
Paolo Bonziniadb3fed2016-02-14 18:17:07 +01001385 if (!virtio_should_notify(vdev, vq)) {
aliguori967f97f2008-12-04 19:38:57 +00001386 return;
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001387 }
aliguori967f97f2008-12-04 19:38:57 +00001388
Stefan Hajnoczi64979a42010-05-24 13:19:21 +01001389 trace_virtio_notify(vdev, vq);
Paolo Bonzini0687c372016-11-18 16:07:01 +01001390 virtio_set_isr(vq->vdev, 0x1);
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001391 virtio_notify_vector(vdev, vq->vector);
aliguori967f97f2008-12-04 19:38:57 +00001392}
1393
1394void virtio_notify_config(VirtIODevice *vdev)
1395{
aliguori76251622009-01-29 17:02:13 +00001396 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
1397 return;
1398
Paolo Bonzini0687c372016-11-18 16:07:01 +01001399 virtio_set_isr(vdev, 0x3);
Michael S. Tsirkinb8f05902015-06-04 12:34:23 +02001400 vdev->generation++;
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001401 virtio_notify_vector(vdev, vdev->config_vector);
aliguori967f97f2008-12-04 19:38:57 +00001402}
1403
Greg Kurz616a6552014-06-24 19:38:54 +02001404static bool virtio_device_endian_needed(void *opaque)
1405{
1406 VirtIODevice *vdev = opaque;
1407
1408 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
Cornelia Huck95129d62015-08-17 11:48:29 +02001409 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
Cornelia Huck3c185592015-06-04 12:34:11 +02001410 return vdev->device_endian != virtio_default_endian();
1411 }
1412 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1413 return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
Greg Kurz616a6552014-06-24 19:38:54 +02001414}
1415
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001416static bool virtio_64bit_features_needed(void *opaque)
1417{
1418 VirtIODevice *vdev = opaque;
1419
1420 return (vdev->host_features >> 32) != 0;
1421}
1422
Jason Wang74aae7b2015-08-05 17:50:07 +08001423static bool virtio_virtqueue_needed(void *opaque)
1424{
1425 VirtIODevice *vdev = opaque;
1426
1427 return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
1428}
1429
Cornelia Huck46c5d082015-09-11 15:16:41 +02001430static bool virtio_ringsize_needed(void *opaque)
1431{
1432 VirtIODevice *vdev = opaque;
1433 int i;
1434
1435 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1436 if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
1437 return true;
1438 }
1439 }
1440 return false;
1441}
1442
Jason Wanga6df8ad2015-11-06 16:02:44 +08001443static bool virtio_extra_state_needed(void *opaque)
1444{
1445 VirtIODevice *vdev = opaque;
1446 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1447 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1448
1449 return k->has_extra_state &&
1450 k->has_extra_state(qbus->parent);
1451}
1452
Stefan Hajnoczi791b1da2016-09-21 16:52:20 +01001453static bool virtio_broken_needed(void *opaque)
1454{
1455 VirtIODevice *vdev = opaque;
1456
1457 return vdev->broken;
1458}
1459
Dr. David Alan Gilbert50e5ae42016-01-06 12:23:39 +00001460static const VMStateDescription vmstate_virtqueue = {
Jason Wang74aae7b2015-08-05 17:50:07 +08001461 .name = "virtqueue_state",
Dr. David Alan Gilbert50e5ae42016-01-06 12:23:39 +00001462 .version_id = 1,
1463 .minimum_version_id = 1,
1464 .fields = (VMStateField[]) {
1465 VMSTATE_UINT64(vring.avail, struct VirtQueue),
1466 VMSTATE_UINT64(vring.used, struct VirtQueue),
1467 VMSTATE_END_OF_LIST()
1468 }
Jason Wang74aae7b2015-08-05 17:50:07 +08001469};
1470
1471static const VMStateDescription vmstate_virtio_virtqueues = {
1472 .name = "virtio/virtqueues",
1473 .version_id = 1,
1474 .minimum_version_id = 1,
1475 .needed = &virtio_virtqueue_needed,
1476 .fields = (VMStateField[]) {
Dr. David Alan Gilbert3e996cc2016-01-29 13:18:56 +00001477 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1478 VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
Jason Wang74aae7b2015-08-05 17:50:07 +08001479 VMSTATE_END_OF_LIST()
1480 }
1481};
1482
Dr. David Alan Gilbert50e5ae42016-01-06 12:23:39 +00001483static const VMStateDescription vmstate_ringsize = {
Cornelia Huck46c5d082015-09-11 15:16:41 +02001484 .name = "ringsize_state",
Dr. David Alan Gilbert50e5ae42016-01-06 12:23:39 +00001485 .version_id = 1,
1486 .minimum_version_id = 1,
1487 .fields = (VMStateField[]) {
1488 VMSTATE_UINT32(vring.num_default, struct VirtQueue),
1489 VMSTATE_END_OF_LIST()
1490 }
Cornelia Huck46c5d082015-09-11 15:16:41 +02001491};
1492
1493static const VMStateDescription vmstate_virtio_ringsize = {
1494 .name = "virtio/ringsize",
1495 .version_id = 1,
1496 .minimum_version_id = 1,
1497 .needed = &virtio_ringsize_needed,
1498 .fields = (VMStateField[]) {
Dr. David Alan Gilbert3e996cc2016-01-29 13:18:56 +00001499 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1500 VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
Cornelia Huck46c5d082015-09-11 15:16:41 +02001501 VMSTATE_END_OF_LIST()
1502 }
1503};
1504
Jason Wanga6df8ad2015-11-06 16:02:44 +08001505static int get_extra_state(QEMUFile *f, void *pv, size_t size)
1506{
1507 VirtIODevice *vdev = pv;
1508 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1509 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1510
1511 if (!k->load_extra_state) {
1512 return -1;
1513 } else {
1514 return k->load_extra_state(qbus->parent, f);
1515 }
1516}
1517
1518static void put_extra_state(QEMUFile *f, void *pv, size_t size)
1519{
1520 VirtIODevice *vdev = pv;
1521 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1522 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1523
1524 k->save_extra_state(qbus->parent, f);
1525}
1526
1527static const VMStateInfo vmstate_info_extra_state = {
1528 .name = "virtqueue_extra_state",
1529 .get = get_extra_state,
1530 .put = put_extra_state,
1531};
1532
1533static const VMStateDescription vmstate_virtio_extra_state = {
1534 .name = "virtio/extra_state",
1535 .version_id = 1,
1536 .minimum_version_id = 1,
1537 .needed = &virtio_extra_state_needed,
1538 .fields = (VMStateField[]) {
1539 {
1540 .name = "extra_state",
1541 .version_id = 0,
1542 .field_exists = NULL,
1543 .size = 0,
1544 .info = &vmstate_info_extra_state,
1545 .flags = VMS_SINGLE,
1546 .offset = 0,
1547 },
1548 VMSTATE_END_OF_LIST()
1549 }
1550};
1551
Greg Kurz616a6552014-06-24 19:38:54 +02001552static const VMStateDescription vmstate_virtio_device_endian = {
1553 .name = "virtio/device_endian",
1554 .version_id = 1,
1555 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +02001556 .needed = &virtio_device_endian_needed,
Greg Kurz616a6552014-06-24 19:38:54 +02001557 .fields = (VMStateField[]) {
1558 VMSTATE_UINT8(device_endian, VirtIODevice),
1559 VMSTATE_END_OF_LIST()
1560 }
1561};
1562
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001563static const VMStateDescription vmstate_virtio_64bit_features = {
1564 .name = "virtio/64bit_features",
1565 .version_id = 1,
1566 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +02001567 .needed = &virtio_64bit_features_needed,
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001568 .fields = (VMStateField[]) {
1569 VMSTATE_UINT64(guest_features, VirtIODevice),
1570 VMSTATE_END_OF_LIST()
1571 }
1572};
1573
Stefan Hajnoczi791b1da2016-09-21 16:52:20 +01001574static const VMStateDescription vmstate_virtio_broken = {
1575 .name = "virtio/broken",
1576 .version_id = 1,
1577 .minimum_version_id = 1,
1578 .needed = &virtio_broken_needed,
1579 .fields = (VMStateField[]) {
1580 VMSTATE_BOOL(broken, VirtIODevice),
1581 VMSTATE_END_OF_LIST()
1582 }
1583};
1584
Greg Kurz6b321a32014-06-24 19:22:30 +02001585static const VMStateDescription vmstate_virtio = {
1586 .name = "virtio",
1587 .version_id = 1,
1588 .minimum_version_id = 1,
1589 .minimum_version_id_old = 1,
1590 .fields = (VMStateField[]) {
1591 VMSTATE_END_OF_LIST()
Greg Kurz616a6552014-06-24 19:38:54 +02001592 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +02001593 .subsections = (const VMStateDescription*[]) {
1594 &vmstate_virtio_device_endian,
1595 &vmstate_virtio_64bit_features,
Jason Wang74aae7b2015-08-05 17:50:07 +08001596 &vmstate_virtio_virtqueues,
Cornelia Huck46c5d082015-09-11 15:16:41 +02001597 &vmstate_virtio_ringsize,
Stefan Hajnoczi791b1da2016-09-21 16:52:20 +01001598 &vmstate_virtio_broken,
Jason Wanga6df8ad2015-11-06 16:02:44 +08001599 &vmstate_virtio_extra_state,
Juan Quintela5cd8cad2014-09-23 14:09:54 +02001600 NULL
Greg Kurz6b321a32014-06-24 19:22:30 +02001601 }
1602};
1603
aliguori967f97f2008-12-04 19:38:57 +00001604void virtio_save(VirtIODevice *vdev, QEMUFile *f)
1605{
KONRAD Frederic1c819442013-04-24 10:21:21 +02001606 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1607 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
Greg Kurz1b5fc0d2014-06-24 19:15:31 +02001608 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001609 uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
aliguori967f97f2008-12-04 19:38:57 +00001610 int i;
1611
KONRAD Frederic1c819442013-04-24 10:21:21 +02001612 if (k->save_config) {
1613 k->save_config(qbus->parent, f);
1614 }
aliguori967f97f2008-12-04 19:38:57 +00001615
aliguori967f97f2008-12-04 19:38:57 +00001616 qemu_put_8s(f, &vdev->status);
1617 qemu_put_8s(f, &vdev->isr);
1618 qemu_put_be16s(f, &vdev->queue_sel);
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001619 qemu_put_be32s(f, &guest_features_lo);
aliguori967f97f2008-12-04 19:38:57 +00001620 qemu_put_be32(f, vdev->config_len);
1621 qemu_put_buffer(f, vdev->config, vdev->config_len);
1622
Jason Wang87b3bd12015-05-29 14:15:31 +08001623 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
aliguori967f97f2008-12-04 19:38:57 +00001624 if (vdev->vq[i].vring.num == 0)
1625 break;
1626 }
1627
1628 qemu_put_be32(f, i);
1629
Jason Wang87b3bd12015-05-29 14:15:31 +08001630 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
aliguori967f97f2008-12-04 19:38:57 +00001631 if (vdev->vq[i].vring.num == 0)
1632 break;
1633
1634 qemu_put_be32(f, vdev->vq[i].vring.num);
Peter Maydell6ce69d12013-07-16 13:25:08 +01001635 if (k->has_variable_vring_alignment) {
1636 qemu_put_be32(f, vdev->vq[i].vring.align);
1637 }
Cornelia Huckab223c92015-06-04 12:34:12 +02001638 /* XXX virtio-1 devices */
1639 qemu_put_be64(f, vdev->vq[i].vring.desc);
aliguori967f97f2008-12-04 19:38:57 +00001640 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
KONRAD Frederic1c819442013-04-24 10:21:21 +02001641 if (k->save_queue) {
1642 k->save_queue(qbus->parent, i, f);
1643 }
aliguori967f97f2008-12-04 19:38:57 +00001644 }
Greg Kurz1b5fc0d2014-06-24 19:15:31 +02001645
1646 if (vdc->save != NULL) {
1647 vdc->save(vdev, f);
1648 }
Greg Kurz6b321a32014-06-24 19:22:30 +02001649
Dr. David Alan Gilbertea43e252016-10-27 18:36:36 +01001650 if (vdc->vmsd) {
1651 vmstate_save_state(f, vdc->vmsd, vdev, NULL);
1652 }
1653
Greg Kurz6b321a32014-06-24 19:22:30 +02001654 /* Subsections */
Alexander Graf8118f092015-01-22 15:01:39 +01001655 vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
aliguori967f97f2008-12-04 19:38:57 +00001656}
1657
Dr. David Alan Gilbert59431242016-07-14 18:22:45 +01001658/* A wrapper for use as a VMState .put function */
Halil Pasic1a665852016-10-06 14:55:39 +02001659static void virtio_device_put(QEMUFile *f, void *opaque, size_t size)
1660{
1661 virtio_save(VIRTIO_DEVICE(opaque), f);
1662}
1663
1664/* A wrapper for use as a VMState .get function */
1665static int virtio_device_get(QEMUFile *f, void *opaque, size_t size)
1666{
1667 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1668 DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
1669
1670 return virtio_load(vdev, f, dc->vmsd->version_id);
1671}
1672
1673const VMStateInfo virtio_vmstate_info = {
1674 .name = "virtio",
1675 .get = virtio_device_get,
1676 .put = virtio_device_put,
1677};
1678
Cornelia Huck6c0196d2015-06-04 12:34:14 +02001679static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
Paolo Bonziniad0c9332011-11-24 13:28:52 +01001680{
KONRAD Frederic181103c2013-04-24 10:21:20 +02001681 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
Cornelia Huck6b8f1022015-05-26 16:34:47 +02001682 bool bad = (val & ~(vdev->host_features)) != 0;
Paolo Bonziniad0c9332011-11-24 13:28:52 +01001683
Cornelia Huck6b8f1022015-05-26 16:34:47 +02001684 val &= vdev->host_features;
KONRAD Frederic181103c2013-04-24 10:21:20 +02001685 if (k->set_features) {
1686 k->set_features(vdev, val);
Paolo Bonziniad0c9332011-11-24 13:28:52 +01001687 }
1688 vdev->guest_features = val;
1689 return bad ? -1 : 0;
1690}
1691
Cornelia Huck6c0196d2015-06-04 12:34:14 +02001692int virtio_set_features(VirtIODevice *vdev, uint64_t val)
1693{
1694 /*
1695 * The driver must not attempt to set features after feature negotiation
1696 * has finished.
1697 */
1698 if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
1699 return -EINVAL;
1700 }
1701 return virtio_set_features_nocheck(vdev, val);
1702}
1703
Greg Kurz1b5fc0d2014-06-24 19:15:31 +02001704int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
aliguori967f97f2008-12-04 19:38:57 +00001705{
Michael S. Tsirkincc459952014-04-03 19:51:14 +03001706 int i, ret;
Michael S. Tsirkina890a2f2014-04-28 16:08:23 +03001707 int32_t config_len;
Michael S. Tsirkincc459952014-04-03 19:51:14 +03001708 uint32_t num;
Michael S. Tsirkin6d74ca52009-12-08 20:07:48 +02001709 uint32_t features;
KONRAD Frederic1c819442013-04-24 10:21:21 +02001710 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1711 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
Greg Kurz1b5fc0d2014-06-24 19:15:31 +02001712 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
aliguori967f97f2008-12-04 19:38:57 +00001713
Greg Kurz616a6552014-06-24 19:38:54 +02001714 /*
1715 * We poison the endianness to ensure it does not get used before
1716 * subsections have been loaded.
1717 */
1718 vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
1719
KONRAD Frederic1c819442013-04-24 10:21:21 +02001720 if (k->load_config) {
1721 ret = k->load_config(qbus->parent, f);
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +03001722 if (ret)
1723 return ret;
1724 }
aliguori967f97f2008-12-04 19:38:57 +00001725
aliguori967f97f2008-12-04 19:38:57 +00001726 qemu_get_8s(f, &vdev->status);
1727 qemu_get_8s(f, &vdev->isr);
1728 qemu_get_be16s(f, &vdev->queue_sel);
Jason Wang87b3bd12015-05-29 14:15:31 +08001729 if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
Michael Roth4b53c2c2014-04-03 19:51:46 +03001730 return -1;
1731 }
Michael S. Tsirkin6d74ca52009-12-08 20:07:48 +02001732 qemu_get_be32s(f, &features);
Paolo Bonziniad0c9332011-11-24 13:28:52 +01001733
Michael S. Tsirkin62cee1a2016-07-04 14:39:10 +03001734 /*
1735 * Temporarily set guest_features low bits - needed by
1736 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
1737 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
1738 *
1739 * Note: devices should always test host features in future - don't create
1740 * new dependencies like this.
1741 */
1742 vdev->guest_features = features;
1743
Michael S. Tsirkina890a2f2014-04-28 16:08:23 +03001744 config_len = qemu_get_be32(f);
Dr. David Alan Gilbert2f5732e2014-06-27 20:02:48 +01001745
1746 /*
1747 * There are cases where the incoming config can be bigger or smaller
1748 * than what we have; so load what we have space for, and skip
1749 * any excess that's in the stream.
1750 */
1751 qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
1752
1753 while (config_len > vdev->config_len) {
1754 qemu_get_byte(f);
1755 config_len--;
Michael S. Tsirkina890a2f2014-04-28 16:08:23 +03001756 }
aliguori967f97f2008-12-04 19:38:57 +00001757
1758 num = qemu_get_be32(f);
1759
Jason Wang87b3bd12015-05-29 14:15:31 +08001760 if (num > VIRTIO_QUEUE_MAX) {
Cornelia Huck8a1be662016-01-08 11:00:00 +01001761 error_report("Invalid number of virtqueues: 0x%x", num);
Michael S. Tsirkincc459952014-04-03 19:51:14 +03001762 return -1;
1763 }
1764
aliguori967f97f2008-12-04 19:38:57 +00001765 for (i = 0; i < num; i++) {
1766 vdev->vq[i].vring.num = qemu_get_be32(f);
Peter Maydell6ce69d12013-07-16 13:25:08 +01001767 if (k->has_variable_vring_alignment) {
1768 vdev->vq[i].vring.align = qemu_get_be32(f);
1769 }
Cornelia Huckab223c92015-06-04 12:34:12 +02001770 vdev->vq[i].vring.desc = qemu_get_be64(f);
aliguori967f97f2008-12-04 19:38:57 +00001771 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
Michael S. Tsirkinbcbabae2011-06-12 16:21:57 +03001772 vdev->vq[i].signalled_used_valid = false;
1773 vdev->vq[i].notification = true;
aliguori967f97f2008-12-04 19:38:57 +00001774
Cornelia Huckab223c92015-06-04 12:34:12 +02001775 if (vdev->vq[i].vring.desc) {
1776 /* XXX virtio-1 devices */
1777 virtio_queue_update_rings(vdev, i);
Michael S. Tsirkin1abeb5a2010-11-23 21:55:39 +02001778 } else if (vdev->vq[i].last_avail_idx) {
1779 error_report("VQ %d address 0x0 "
Markus Armbruster6daf1942011-06-22 14:03:54 +02001780 "inconsistent with Host index 0x%x",
Michael S. Tsirkin1abeb5a2010-11-23 21:55:39 +02001781 i, vdev->vq[i].last_avail_idx);
1782 return -1;
Stefan Hajnoczi8275e2f2016-09-21 16:52:18 +01001783 }
KONRAD Frederic1c819442013-04-24 10:21:21 +02001784 if (k->load_queue) {
1785 ret = k->load_queue(qbus->parent, i, f);
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +03001786 if (ret)
1787 return ret;
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001788 }
aliguori967f97f2008-12-04 19:38:57 +00001789 }
1790
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001791 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
Greg Kurz1b5fc0d2014-06-24 19:15:31 +02001792
1793 if (vdc->load != NULL) {
Greg Kurz6b321a32014-06-24 19:22:30 +02001794 ret = vdc->load(vdev, f, version_id);
1795 if (ret) {
1796 return ret;
1797 }
Greg Kurz1b5fc0d2014-06-24 19:15:31 +02001798 }
1799
Dr. David Alan Gilbertea43e252016-10-27 18:36:36 +01001800 if (vdc->vmsd) {
1801 ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id);
1802 if (ret) {
1803 return ret;
1804 }
1805 }
1806
Greg Kurz616a6552014-06-24 19:38:54 +02001807 /* Subsections */
1808 ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
1809 if (ret) {
1810 return ret;
1811 }
1812
1813 if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
1814 vdev->device_endian = virtio_default_endian();
1815 }
1816
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001817 if (virtio_64bit_features_needed(vdev)) {
1818 /*
1819 * Subsection load filled vdev->guest_features. Run them
1820 * through virtio_set_features to sanity-check them against
1821 * host_features.
1822 */
1823 uint64_t features64 = vdev->guest_features;
Cornelia Huck6c0196d2015-06-04 12:34:14 +02001824 if (virtio_set_features_nocheck(vdev, features64) < 0) {
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001825 error_report("Features 0x%" PRIx64 " unsupported. "
1826 "Allowed features: 0x%" PRIx64,
1827 features64, vdev->host_features);
1828 return -1;
1829 }
1830 } else {
Cornelia Huck6c0196d2015-06-04 12:34:14 +02001831 if (virtio_set_features_nocheck(vdev, features) < 0) {
Gerd Hoffmann019a3ed2015-06-01 10:45:40 +02001832 error_report("Features 0x%x unsupported. "
1833 "Allowed features: 0x%" PRIx64,
1834 features, vdev->host_features);
1835 return -1;
1836 }
1837 }
1838
Greg Kurz616a6552014-06-24 19:38:54 +02001839 for (i = 0; i < num; i++) {
Cornelia Huckab223c92015-06-04 12:34:12 +02001840 if (vdev->vq[i].vring.desc) {
Greg Kurz616a6552014-06-24 19:38:54 +02001841 uint16_t nheads;
1842 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
1843 /* Check it isn't doing strange things with descriptor numbers. */
1844 if (nheads > vdev->vq[i].vring.num) {
1845 error_report("VQ %d size 0x%x Guest index 0x%x "
1846 "inconsistent with Host index 0x%x: delta 0x%x",
1847 i, vdev->vq[i].vring.num,
1848 vring_avail_idx(&vdev->vq[i]),
1849 vdev->vq[i].last_avail_idx, nheads);
1850 return -1;
1851 }
Vincenzo Maffioneb796fcd2016-01-31 11:29:04 +01001852 vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +01001853 vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
Stefan Hajnoczibccdef62016-08-15 13:54:15 +01001854
1855 /*
1856 * Some devices migrate VirtQueueElements that have been popped
1857 * from the avail ring but not yet returned to the used ring.
1858 */
1859 vdev->vq[i].inuse = vdev->vq[i].last_avail_idx -
1860 vdev->vq[i].used_idx;
1861 if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
1862 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
1863 "used_idx 0x%x",
1864 i, vdev->vq[i].vring.num,
1865 vdev->vq[i].last_avail_idx,
1866 vdev->vq[i].used_idx);
1867 return -1;
1868 }
Greg Kurz616a6552014-06-24 19:38:54 +02001869 }
1870 }
1871
1872 return 0;
aliguori967f97f2008-12-04 19:38:57 +00001873}
1874
KONRAD Frederic6a1a8cc2013-04-24 10:21:22 +02001875void virtio_cleanup(VirtIODevice *vdev)
aliguorib946a152009-04-17 17:11:08 +00001876{
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +02001877 qemu_del_vm_change_state_handler(vdev->vmstate);
Luiz Capitulino6f79e062012-02-09 11:29:42 -02001878 g_free(vdev->config);
Anthony Liguori7267c092011-08-20 22:09:37 -05001879 g_free(vdev->vq);
Jason Wange0d686b2015-04-23 14:21:46 +08001880 g_free(vdev->vector_queues);
KONRAD Frederic8e05db92013-01-15 00:08:02 +01001881}
1882
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001883static void virtio_vmstate_change(void *opaque, int running, RunState state)
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +02001884{
1885 VirtIODevice *vdev = opaque;
KONRAD Frederic1c819442013-04-24 10:21:21 +02001886 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1887 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +02001888 bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
Michael S. Tsirkin9e8e8c42014-09-11 18:42:02 +03001889 vdev->vm_running = running;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +02001890
1891 if (backend_run) {
1892 virtio_set_status(vdev, vdev->status);
1893 }
1894
KONRAD Frederic1c819442013-04-24 10:21:21 +02001895 if (k->vmstate_change) {
1896 k->vmstate_change(qbus->parent, backend_run);
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +02001897 }
1898
1899 if (!backend_run) {
1900 virtio_set_status(vdev, vdev->status);
1901 }
1902}
1903
Gongleic8075ca2014-09-30 14:10:38 +08001904void virtio_instance_init_common(Object *proxy_obj, void *data,
1905 size_t vdev_size, const char *vdev_name)
1906{
1907 DeviceState *vdev = data;
1908
1909 object_initialize(vdev, vdev_size, vdev_name);
1910 object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL);
1911 object_unref(OBJECT(vdev));
1912 qdev_alias_all_properties(vdev, proxy_obj);
1913}
1914
KONRAD Frederic8e05db92013-01-15 00:08:02 +01001915void virtio_init(VirtIODevice *vdev, const char *name,
1916 uint16_t device_id, size_t config_size)
aliguori967f97f2008-12-04 19:38:57 +00001917{
Jason Wange0d686b2015-04-23 14:21:46 +08001918 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1919 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
Michael S. Tsirkinb8193ad2009-09-07 21:20:15 +03001920 int i;
Jason Wange0d686b2015-04-23 14:21:46 +08001921 int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
1922
1923 if (nvectors) {
1924 vdev->vector_queues =
1925 g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
1926 }
1927
Paul Brook53c25ce2009-05-18 14:51:59 +01001928 vdev->device_id = device_id;
aliguori967f97f2008-12-04 19:38:57 +00001929 vdev->status = 0;
Paolo Bonzini0687c372016-11-18 16:07:01 +01001930 atomic_set(&vdev->isr, 0);
aliguori967f97f2008-12-04 19:38:57 +00001931 vdev->queue_sel = 0;
Michael S. Tsirkin7055e682009-06-21 19:50:13 +03001932 vdev->config_vector = VIRTIO_NO_VECTOR;
Jason Wang87b3bd12015-05-29 14:15:31 +08001933 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
Luiz Capitulino13548692011-07-29 15:36:43 -03001934 vdev->vm_running = runstate_is_running();
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +01001935 vdev->broken = false;
Jason Wang87b3bd12015-05-29 14:15:31 +08001936 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
Michael S. Tsirkinb8193ad2009-09-07 21:20:15 +03001937 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001938 vdev->vq[i].vdev = vdev;
Jason Wange78a2b42013-01-30 19:12:37 +08001939 vdev->vq[i].queue_index = i;
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001940 }
aliguori967f97f2008-12-04 19:38:57 +00001941
aliguori967f97f2008-12-04 19:38:57 +00001942 vdev->name = name;
1943 vdev->config_len = config_size;
KONRAD Frederic8e05db92013-01-15 00:08:02 +01001944 if (vdev->config_len) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001945 vdev->config = g_malloc0(config_size);
KONRAD Frederic8e05db92013-01-15 00:08:02 +01001946 } else {
aliguori967f97f2008-12-04 19:38:57 +00001947 vdev->config = NULL;
KONRAD Frederic8e05db92013-01-15 00:08:02 +01001948 }
1949 vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
1950 vdev);
Greg Kurz616a6552014-06-24 19:38:54 +02001951 vdev->device_endian = virtio_default_endian();
Victor Kaplansky56696552016-02-18 16:12:23 +02001952 vdev->use_guest_notifier_mask = true;
KONRAD Frederic8e05db92013-01-15 00:08:02 +01001953}
aliguori967f97f2008-12-04 19:38:57 +00001954
Avi Kivitya8170e52012-10-23 12:30:10 +02001955hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001956{
1957 return vdev->vq[n].vring.desc;
1958}
1959
Avi Kivitya8170e52012-10-23 12:30:10 +02001960hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001961{
1962 return vdev->vq[n].vring.avail;
1963}
1964
Avi Kivitya8170e52012-10-23 12:30:10 +02001965hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001966{
1967 return vdev->vq[n].vring.used;
1968}
1969
Avi Kivitya8170e52012-10-23 12:30:10 +02001970hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001971{
1972 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
1973}
1974
Avi Kivitya8170e52012-10-23 12:30:10 +02001975hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001976{
1977 return offsetof(VRingAvail, ring) +
Pierre Morel50764fc2015-09-10 13:37:10 +02001978 sizeof(uint16_t) * vdev->vq[n].vring.num;
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001979}
1980
Avi Kivitya8170e52012-10-23 12:30:10 +02001981hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001982{
1983 return offsetof(VRingUsed, ring) +
1984 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
1985}
1986
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001987uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
1988{
1989 return vdev->vq[n].last_avail_idx;
1990}
1991
1992void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
1993{
1994 vdev->vq[n].last_avail_idx = idx;
Vincenzo Maffionebe1fea92016-01-31 11:29:05 +01001995 vdev->vq[n].shadow_avail_idx = idx;
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02001996}
1997
Stefan Hajnoczi6793dfd2013-08-12 11:08:09 +02001998void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
1999{
2000 vdev->vq[n].signalled_used_valid = false;
2001}
2002
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02002003VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
2004{
2005 return vdev->vq + n;
2006}
2007
Jason Wange78a2b42013-01-30 19:12:37 +08002008uint16_t virtio_get_queue_index(VirtQueue *vq)
2009{
2010 return vq->queue_index;
2011}
2012
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02002013static void virtio_queue_guest_notifier_read(EventNotifier *n)
2014{
2015 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
2016 if (event_notifier_test_and_clear(n)) {
Paolo Bonzini83d768b2016-11-18 16:07:02 +01002017 virtio_notify_vector(vq->vdev, vq->vector);
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02002018 }
2019}
2020
2021void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
2022 bool with_irqfd)
2023{
2024 if (assign && !with_irqfd) {
Fam Zheng54e18d32016-04-22 21:53:53 +08002025 event_notifier_set_handler(&vq->guest_notifier, false,
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02002026 virtio_queue_guest_notifier_read);
2027 } else {
Fam Zheng54e18d32016-04-22 21:53:53 +08002028 event_notifier_set_handler(&vq->guest_notifier, false, NULL);
Paolo Bonzini15b2bd12012-07-05 17:16:30 +02002029 }
2030 if (!assign) {
2031 /* Test and clear notifier before closing it,
2032 * in case poll callback didn't have time to run. */
2033 virtio_queue_guest_notifier_read(&vq->guest_notifier);
2034 }
2035}
2036
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02002037EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
2038{
2039 return &vq->guest_notifier;
2040}
Paolo Bonzinib1f416a2012-07-05 17:16:29 +02002041
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02002042static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
Paolo Bonzinib1f416a2012-07-05 17:16:29 +02002043{
2044 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2045 if (event_notifier_test_and_clear(n)) {
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02002046 virtio_queue_notify_aio_vq(vq);
Paolo Bonzinib1f416a2012-07-05 17:16:29 +02002047 }
2048}
2049
Paolo Bonzinia1afb602016-02-14 18:17:06 +01002050void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
Fam Zhengbf1780b2016-07-13 13:09:43 +08002051 VirtIOHandleOutput handle_output)
Paolo Bonzinia1afb602016-02-14 18:17:06 +01002052{
Paolo Bonzinia378b492016-04-06 12:16:28 +02002053 if (handle_output) {
2054 vq->handle_aio_output = handle_output;
Paolo Bonzinia1afb602016-02-14 18:17:06 +01002055 aio_set_event_notifier(ctx, &vq->host_notifier, true,
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02002056 virtio_queue_host_notifier_aio_read);
Paolo Bonzinia1afb602016-02-14 18:17:06 +01002057 } else {
2058 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL);
Paolo Bonzinia1afb602016-02-14 18:17:06 +01002059 /* Test and clear notifier before after disabling event,
2060 * in case poll callback didn't have time to run. */
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02002061 virtio_queue_host_notifier_aio_read(&vq->host_notifier);
Paolo Bonzinia378b492016-04-06 12:16:28 +02002062 vq->handle_aio_output = NULL;
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02002063 }
2064}
2065
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002066void virtio_queue_host_notifier_read(EventNotifier *n)
Michael S. Tsirkin344dc162016-04-06 12:16:25 +02002067{
2068 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2069 if (event_notifier_test_and_clear(n)) {
2070 virtio_queue_notify_vq(vq);
Paolo Bonzinia1afb602016-02-14 18:17:06 +01002071 }
2072}
2073
Michael S. Tsirkin1cbdabe2010-03-17 13:08:02 +02002074EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
2075{
2076 return &vq->host_notifier;
2077}
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002078
KONRAD Frederic1034e9c2013-04-30 16:08:48 +02002079void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
2080{
Markus Armbruster9e288402014-06-06 18:43:29 +02002081 g_free(vdev->bus_name);
Markus Armbruster80e00902014-06-06 18:43:30 +02002082 vdev->bus_name = g_strdup(bus_name);
KONRAD Frederic1034e9c2013-04-30 16:08:48 +02002083}
2084
Stefan Hajnoczif5ed3662016-09-21 16:52:19 +01002085void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...)
2086{
2087 va_list ap;
2088
2089 va_start(ap, fmt);
2090 error_vreport(fmt, ap);
2091 va_end(ap);
2092
2093 vdev->broken = true;
2094
2095 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2096 virtio_set_status(vdev, vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET);
2097 virtio_notify_config(vdev);
2098 }
2099}
2100
Andreas Färber1d244b42013-07-30 00:50:27 +02002101static void virtio_device_realize(DeviceState *dev, Error **errp)
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002102{
Andreas Färber1d244b42013-07-30 00:50:27 +02002103 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2104 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
2105 Error *err = NULL;
2106
Dr. David Alan Gilbertea43e252016-10-27 18:36:36 +01002107 /* Devices should either use vmsd or the load/save methods */
2108 assert(!vdc->vmsd || !vdc->load);
2109
Andreas Färber1d244b42013-07-30 00:50:27 +02002110 if (vdc->realize != NULL) {
2111 vdc->realize(dev, &err);
2112 if (err != NULL) {
2113 error_propagate(errp, err);
2114 return;
2115 }
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002116 }
Jason Wange8398042015-05-29 14:15:25 +08002117
2118 virtio_bus_device_plugged(vdev, &err);
2119 if (err != NULL) {
2120 error_propagate(errp, err);
2121 return;
2122 }
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002123}
2124
Andreas Färber1d244b42013-07-30 00:50:27 +02002125static void virtio_device_unrealize(DeviceState *dev, Error **errp)
KONRAD Frederic1034e9c2013-04-30 16:08:48 +02002126{
Andreas Färber1d244b42013-07-30 00:50:27 +02002127 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
Andreas Färber306ec6c2013-07-30 03:50:44 +02002128 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
2129 Error *err = NULL;
KONRAD Frederic1034e9c2013-04-30 16:08:48 +02002130
Paolo Bonzini83d07042013-12-20 19:48:51 +01002131 virtio_bus_device_unplugged(vdev);
2132
Andreas Färber306ec6c2013-07-30 03:50:44 +02002133 if (vdc->unrealize != NULL) {
2134 vdc->unrealize(dev, &err);
2135 if (err != NULL) {
2136 error_propagate(errp, err);
2137 return;
2138 }
Paolo Bonzini5e96f5d2013-09-20 13:59:08 +02002139 }
Andreas Färber1d244b42013-07-30 00:50:27 +02002140
Markus Armbruster9e288402014-06-06 18:43:29 +02002141 g_free(vdev->bus_name);
2142 vdev->bus_name = NULL;
KONRAD Frederic1034e9c2013-04-30 16:08:48 +02002143}
2144
Cornelia Huck6b8f1022015-05-26 16:34:47 +02002145static Property virtio_properties[] = {
2146 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
2147 DEFINE_PROP_END_OF_LIST(),
2148};
2149
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002150static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)
2151{
2152 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002153 int n, r, err;
2154
2155 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002156 VirtQueue *vq = &vdev->vq[n];
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002157 if (!virtio_queue_get_num(vdev, n)) {
2158 continue;
2159 }
Paolo Bonzinied08a2a2016-10-21 22:48:14 +02002160 r = virtio_bus_set_host_notifier(qbus, n, true);
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002161 if (r < 0) {
2162 err = r;
2163 goto assign_error;
2164 }
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002165 event_notifier_set_handler(&vq->host_notifier, true,
2166 virtio_queue_host_notifier_read);
Paolo Bonzini6019f3b2016-10-21 22:48:12 +02002167 }
2168
2169 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2170 /* Kick right away to begin processing requests already in vring */
2171 VirtQueue *vq = &vdev->vq[n];
2172 if (!vq->vring.num) {
2173 continue;
2174 }
2175 event_notifier_set(&vq->host_notifier);
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002176 }
2177 return 0;
2178
2179assign_error:
2180 while (--n >= 0) {
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002181 VirtQueue *vq = &vdev->vq[n];
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002182 if (!virtio_queue_get_num(vdev, n)) {
2183 continue;
2184 }
2185
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002186 event_notifier_set_handler(&vq->host_notifier, true, NULL);
Paolo Bonzinied08a2a2016-10-21 22:48:14 +02002187 r = virtio_bus_set_host_notifier(qbus, n, false);
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002188 assert(r >= 0);
2189 }
2190 return err;
2191}
2192
2193int virtio_device_start_ioeventfd(VirtIODevice *vdev)
2194{
2195 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2196 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2197
2198 return virtio_bus_start_ioeventfd(vbus);
2199}
2200
2201static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev)
2202{
2203 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002204 int n, r;
2205
2206 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002207 VirtQueue *vq = &vdev->vq[n];
2208
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002209 if (!virtio_queue_get_num(vdev, n)) {
2210 continue;
2211 }
Paolo Bonzinifa283a42016-10-21 22:48:15 +02002212 event_notifier_set_handler(&vq->host_notifier, true, NULL);
Paolo Bonzinied08a2a2016-10-21 22:48:14 +02002213 r = virtio_bus_set_host_notifier(qbus, n, false);
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002214 assert(r >= 0);
2215 }
2216}
2217
2218void virtio_device_stop_ioeventfd(VirtIODevice *vdev)
2219{
2220 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2221 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2222
2223 virtio_bus_stop_ioeventfd(vbus);
2224}
2225
Paolo Bonzini310837d2016-11-18 16:07:00 +01002226int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
2227{
2228 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2229 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2230
2231 return virtio_bus_grab_ioeventfd(vbus);
2232}
2233
2234void virtio_device_release_ioeventfd(VirtIODevice *vdev)
2235{
2236 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2237 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2238
2239 virtio_bus_release_ioeventfd(vbus);
2240}
2241
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002242static void virtio_device_class_init(ObjectClass *klass, void *data)
2243{
2244 /* Set the default value here. */
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002245 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002246 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färber1d244b42013-07-30 00:50:27 +02002247
2248 dc->realize = virtio_device_realize;
2249 dc->unrealize = virtio_device_unrealize;
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002250 dc->bus_type = TYPE_VIRTIO_BUS;
Cornelia Huck6b8f1022015-05-26 16:34:47 +02002251 dc->props = virtio_properties;
Paolo Bonziniff4c07d2016-10-21 22:48:07 +02002252 vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
2253 vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
Michael S. Tsirkin9b706db2016-11-04 12:04:23 +02002254
2255 vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002256}
2257
Paolo Bonzini8e93cef2016-10-21 22:48:08 +02002258bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
2259{
2260 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2261 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2262
2263 return virtio_bus_ioeventfd_enabled(vbus);
2264}
2265
KONRAD Frederic8e05db92013-01-15 00:08:02 +01002266static const TypeInfo virtio_device_info = {
2267 .name = TYPE_VIRTIO_DEVICE,
2268 .parent = TYPE_DEVICE,
2269 .instance_size = sizeof(VirtIODevice),
2270 .class_init = virtio_device_class_init,
2271 .abstract = true,
2272 .class_size = sizeof(VirtioDeviceClass),
2273};
2274
2275static void virtio_register_types(void)
2276{
2277 type_register_static(&virtio_device_info);
2278}
2279
2280type_init(virtio_register_types)