]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio.c
bsd-user: pull in target_arch_thread.h update target_arch_elf.h
[mirror_qemu.git] / hw / virtio / virtio.c
CommitLineData
967f97fa
AL
1/*
2 * Virtio Support
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
9b8bfe21 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
4771d756 16#include "cpu.h"
64979a4d 17#include "trace.h"
1de7afc9 18#include "qemu/error-report.h"
d68cdae3 19#include "qemu/log.h"
db725815 20#include "qemu/main-loop.h"
0b8fa32f 21#include "qemu/module.h"
0d09e41a 22#include "hw/virtio/virtio.h"
ca77ee28 23#include "migration/qemu-file-types.h"
1de7afc9 24#include "qemu/atomic.h"
0d09e41a 25#include "hw/virtio/virtio-bus.h"
a27bd6c7 26#include "hw/qdev-properties.h"
cee3ca00 27#include "hw/virtio/virtio-access.h"
8607f5c3 28#include "sysemu/dma.h"
54d31236 29#include "sysemu/runstate.h"
7c78bdd7 30#include "standard-headers/linux/virtio_ids.h"
967f97fa 31
6ce69d1c
PM
32/*
33 * The alignment to use between consumer and producer parts of vring.
34 * x86 pagesize again. This is the default, used by transports like PCI
35 * which don't provide a means for the guest to tell the host the alignment.
36 */
f46f15bc
AL
37#define VIRTIO_PCI_VRING_ALIGN 4096
38
967f97fa
AL
39typedef struct VRingDesc
40{
41 uint64_t addr;
42 uint32_t len;
43 uint16_t flags;
44 uint16_t next;
45} VRingDesc;
46
a40dcec9
WX
47typedef struct VRingPackedDesc {
48 uint64_t addr;
49 uint32_t len;
50 uint16_t id;
51 uint16_t flags;
52} VRingPackedDesc;
53
967f97fa
AL
54typedef struct VRingAvail
55{
56 uint16_t flags;
57 uint16_t idx;
f7795e40 58 uint16_t ring[];
967f97fa
AL
59} VRingAvail;
60
61typedef struct VRingUsedElem
62{
63 uint32_t id;
64 uint32_t len;
65} VRingUsedElem;
66
67typedef struct VRingUsed
68{
69 uint16_t flags;
70 uint16_t idx;
f7795e40 71 VRingUsedElem ring[];
967f97fa
AL
72} VRingUsed;
73
c611c764
PB
74typedef struct VRingMemoryRegionCaches {
75 struct rcu_head rcu;
76 MemoryRegionCache desc;
77 MemoryRegionCache avail;
78 MemoryRegionCache used;
79} VRingMemoryRegionCaches;
80
967f97fa
AL
81typedef struct VRing
82{
83 unsigned int num;
46c5d082 84 unsigned int num_default;
6ce69d1c 85 unsigned int align;
a8170e5e
AK
86 hwaddr desc;
87 hwaddr avail;
88 hwaddr used;
c611c764 89 VRingMemoryRegionCaches *caches;
967f97fa
AL
90} VRing;
91
a40dcec9
WX
92typedef struct VRingPackedDescEvent {
93 uint16_t off_wrap;
94 uint16_t flags;
95} VRingPackedDescEvent ;
96
967f97fa
AL
97struct VirtQueue
98{
99 VRing vring;
86044b24 100 VirtQueueElement *used_elems;
be1fea9b
VM
101
102 /* Next head to pop */
967f97fa 103 uint16_t last_avail_idx;
a40dcec9 104 bool last_avail_wrap_counter;
b796fcd1 105
be1fea9b
VM
106 /* Last avail_idx read from VQ. */
107 uint16_t shadow_avail_idx;
a40dcec9 108 bool shadow_avail_wrap_counter;
be1fea9b 109
b796fcd1 110 uint16_t used_idx;
a40dcec9 111 bool used_wrap_counter;
b796fcd1 112
bcbabae8
MT
113 /* Last used index value we have signalled on */
114 uint16_t signalled_used;
115
116 /* Last used index value we have signalled on */
117 bool signalled_used_valid;
118
332fa82d
SH
119 /* Notification enabled? */
120 bool notification;
bcbabae8 121
e78a2b42
JW
122 uint16_t queue_index;
123
e66bcc40 124 unsigned int inuse;
bcbabae8 125
7055e687 126 uint16_t vector;
bf1780b0 127 VirtIOHandleOutput handle_output;
07931698 128 VirtIOHandleAIOOutput handle_aio_output;
1cbdabe2
MT
129 VirtIODevice *vdev;
130 EventNotifier guest_notifier;
131 EventNotifier host_notifier;
fcccb271 132 bool host_notifier_enabled;
e0d686bf 133 QLIST_ENTRY(VirtQueue) node;
967f97fa
AL
134};
135
b116d6c3 136/* Called within call_rcu(). */
c611c764
PB
137static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
138{
b116d6c3 139 assert(caches != NULL);
c611c764
PB
140 address_space_cache_destroy(&caches->desc);
141 address_space_cache_destroy(&caches->avail);
142 address_space_cache_destroy(&caches->used);
143 g_free(caches);
144}
145
45641dba
PB
146static void virtio_virtqueue_reset_region_cache(struct VirtQueue *vq)
147{
148 VRingMemoryRegionCaches *caches;
149
d73415a3
SH
150 caches = qatomic_read(&vq->vring.caches);
151 qatomic_rcu_set(&vq->vring.caches, NULL);
45641dba
PB
152 if (caches) {
153 call_rcu(caches, virtio_free_region_cache, rcu);
154 }
155}
156
c611c764
PB
157static void virtio_init_region_cache(VirtIODevice *vdev, int n)
158{
159 VirtQueue *vq = &vdev->vq[n];
160 VRingMemoryRegionCaches *old = vq->vring.caches;
45641dba 161 VRingMemoryRegionCaches *new = NULL;
c611c764 162 hwaddr addr, size;
e45da653 163 int64_t len;
86044b24 164 bool packed;
c611c764 165
c611c764
PB
166
167 addr = vq->vring.desc;
168 if (!addr) {
45641dba 169 goto out_no_cache;
c611c764
PB
170 }
171 new = g_new0(VRingMemoryRegionCaches, 1);
172 size = virtio_queue_get_desc_size(vdev, n);
86044b24
JW
173 packed = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ?
174 true : false;
e45da653 175 len = address_space_cache_init(&new->desc, vdev->dma_as,
86044b24 176 addr, size, packed);
e45da653
JW
177 if (len < size) {
178 virtio_error(vdev, "Cannot map desc");
179 goto err_desc;
180 }
c611c764 181
f90cda63 182 size = virtio_queue_get_used_size(vdev, n);
e45da653
JW
183 len = address_space_cache_init(&new->used, vdev->dma_as,
184 vq->vring.used, size, true);
185 if (len < size) {
186 virtio_error(vdev, "Cannot map used");
187 goto err_used;
188 }
c611c764 189
f90cda63 190 size = virtio_queue_get_avail_size(vdev, n);
e45da653
JW
191 len = address_space_cache_init(&new->avail, vdev->dma_as,
192 vq->vring.avail, size, false);
193 if (len < size) {
194 virtio_error(vdev, "Cannot map avail");
195 goto err_avail;
196 }
c611c764 197
d73415a3 198 qatomic_rcu_set(&vq->vring.caches, new);
c611c764
PB
199 if (old) {
200 call_rcu(old, virtio_free_region_cache, rcu);
201 }
e45da653
JW
202 return;
203
204err_avail:
45641dba 205 address_space_cache_destroy(&new->avail);
e45da653 206err_used:
45641dba 207 address_space_cache_destroy(&new->used);
e45da653 208err_desc:
45641dba
PB
209 address_space_cache_destroy(&new->desc);
210out_no_cache:
e45da653 211 g_free(new);
45641dba 212 virtio_virtqueue_reset_region_cache(vq);
c611c764
PB
213}
214
967f97fa 215/* virt queue functions */
ab223c95 216void virtio_queue_update_rings(VirtIODevice *vdev, int n)
967f97fa 217{
ab223c95 218 VRing *vring = &vdev->vq[n].vring;
53c25cea 219
758ead31 220 if (!vring->num || !vring->desc || !vring->align) {
ab223c95
CH
221 /* not yet setup -> nothing to do */
222 return;
223 }
224 vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
225 vring->used = vring_align(vring->avail +
226 offsetof(VRingAvail, ring[vring->num]),
227 vring->align);
c611c764 228 virtio_init_region_cache(vdev, n);
967f97fa
AL
229}
230
97cd965c 231/* Called within rcu_read_lock(). */
86044b24
JW
232static void vring_split_desc_read(VirtIODevice *vdev, VRingDesc *desc,
233 MemoryRegionCache *cache, int i)
967f97fa 234{
5eba0404
PB
235 address_space_read_cached(cache, i * sizeof(VRingDesc),
236 desc, sizeof(VRingDesc));
aa570d6f
PB
237 virtio_tswap64s(vdev, &desc->addr);
238 virtio_tswap32s(vdev, &desc->len);
239 virtio_tswap16s(vdev, &desc->flags);
240 virtio_tswap16s(vdev, &desc->next);
967f97fa
AL
241}
242
683f7665
JW
243static void vring_packed_event_read(VirtIODevice *vdev,
244 MemoryRegionCache *cache,
245 VRingPackedDescEvent *e)
246{
247 hwaddr off_off = offsetof(VRingPackedDescEvent, off_wrap);
248 hwaddr off_flags = offsetof(VRingPackedDescEvent, flags);
249
250 address_space_read_cached(cache, off_flags, &e->flags,
251 sizeof(e->flags));
252 /* Make sure flags is seen before off_wrap */
253 smp_rmb();
254 address_space_read_cached(cache, off_off, &e->off_wrap,
255 sizeof(e->off_wrap));
256 virtio_tswap16s(vdev, &e->off_wrap);
257 virtio_tswap16s(vdev, &e->flags);
258}
259
260static void vring_packed_off_wrap_write(VirtIODevice *vdev,
261 MemoryRegionCache *cache,
262 uint16_t off_wrap)
263{
264 hwaddr off = offsetof(VRingPackedDescEvent, off_wrap);
265
266 virtio_tswap16s(vdev, &off_wrap);
267 address_space_write_cached(cache, off, &off_wrap, sizeof(off_wrap));
268 address_space_cache_invalidate(cache, off, sizeof(off_wrap));
269}
270
271static void vring_packed_flags_write(VirtIODevice *vdev,
272 MemoryRegionCache *cache, uint16_t flags)
273{
274 hwaddr off = offsetof(VRingPackedDescEvent, flags);
275
276 virtio_tswap16s(vdev, &flags);
277 address_space_write_cached(cache, off, &flags, sizeof(flags));
278 address_space_cache_invalidate(cache, off, sizeof(flags));
279}
280
86044b24 281/* Called within rcu_read_lock(). */
e0e2d644
JW
282static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *vq)
283{
d73415a3 284 return qatomic_rcu_read(&vq->vring.caches);
e0e2d644 285}
abdd16f4 286
97cd965c 287/* Called within rcu_read_lock(). */
967f97fa
AL
288static inline uint16_t vring_avail_flags(VirtQueue *vq)
289{
e0e2d644 290 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 291 hwaddr pa = offsetof(VRingAvail, flags);
abdd16f4
SH
292
293 if (!caches) {
294 return 0;
295 }
296
97cd965c 297 return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
967f97fa
AL
298}
299
97cd965c 300/* Called within rcu_read_lock(). */
967f97fa
AL
301static inline uint16_t vring_avail_idx(VirtQueue *vq)
302{
e0e2d644 303 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 304 hwaddr pa = offsetof(VRingAvail, idx);
abdd16f4
SH
305
306 if (!caches) {
307 return 0;
308 }
309
97cd965c 310 vq->shadow_avail_idx = virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
be1fea9b 311 return vq->shadow_avail_idx;
967f97fa
AL
312}
313
97cd965c 314/* Called within rcu_read_lock(). */
967f97fa
AL
315static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
316{
e0e2d644 317 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 318 hwaddr pa = offsetof(VRingAvail, ring[i]);
abdd16f4
SH
319
320 if (!caches) {
321 return 0;
322 }
323
97cd965c 324 return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
967f97fa
AL
325}
326
97cd965c 327/* Called within rcu_read_lock(). */
e9600c6c 328static inline uint16_t vring_get_used_event(VirtQueue *vq)
bcbabae8
MT
329{
330 return vring_avail_ring(vq, vq->vring.num);
331}
332
97cd965c 333/* Called within rcu_read_lock(). */
1cdd2ee5
VM
334static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
335 int i)
967f97fa 336{
e0e2d644 337 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 338 hwaddr pa = offsetof(VRingUsed, ring[i]);
abdd16f4
SH
339
340 if (!caches) {
341 return;
342 }
343
1cdd2ee5
VM
344 virtio_tswap32s(vq->vdev, &uelem->id);
345 virtio_tswap32s(vq->vdev, &uelem->len);
97cd965c
PB
346 address_space_write_cached(&caches->used, pa, uelem, sizeof(VRingUsedElem));
347 address_space_cache_invalidate(&caches->used, pa, sizeof(VRingUsedElem));
967f97fa
AL
348}
349
97cd965c 350/* Called within rcu_read_lock(). */
967f97fa
AL
351static uint16_t vring_used_idx(VirtQueue *vq)
352{
e0e2d644 353 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 354 hwaddr pa = offsetof(VRingUsed, idx);
abdd16f4
SH
355
356 if (!caches) {
357 return 0;
358 }
359
97cd965c 360 return virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
967f97fa
AL
361}
362
97cd965c 363/* Called within rcu_read_lock(). */
bcbabae8 364static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
967f97fa 365{
e0e2d644 366 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 367 hwaddr pa = offsetof(VRingUsed, idx);
abdd16f4
SH
368
369 if (caches) {
370 virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
371 address_space_cache_invalidate(&caches->used, pa, sizeof(val));
372 }
373
b796fcd1 374 vq->used_idx = val;
967f97fa
AL
375}
376
97cd965c 377/* Called within rcu_read_lock(). */
967f97fa
AL
378static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
379{
e0e2d644 380 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
cee3ca00 381 VirtIODevice *vdev = vq->vdev;
97cd965c 382 hwaddr pa = offsetof(VRingUsed, flags);
abdd16f4 383 uint16_t flags;
97cd965c 384
abdd16f4
SH
385 if (!caches) {
386 return;
387 }
388
389 flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
97cd965c
PB
390 virtio_stw_phys_cached(vdev, &caches->used, pa, flags | mask);
391 address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
967f97fa
AL
392}
393
97cd965c 394/* Called within rcu_read_lock(). */
967f97fa
AL
395static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
396{
e0e2d644 397 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
cee3ca00 398 VirtIODevice *vdev = vq->vdev;
97cd965c 399 hwaddr pa = offsetof(VRingUsed, flags);
abdd16f4 400 uint16_t flags;
97cd965c 401
abdd16f4
SH
402 if (!caches) {
403 return;
404 }
405
406 flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
97cd965c
PB
407 virtio_stw_phys_cached(vdev, &caches->used, pa, flags & ~mask);
408 address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
967f97fa
AL
409}
410
97cd965c 411/* Called within rcu_read_lock(). */
e9600c6c 412static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
bcbabae8 413{
97cd965c 414 VRingMemoryRegionCaches *caches;
a8170e5e 415 hwaddr pa;
332fa82d 416 if (!vq->notification) {
bcbabae8
MT
417 return;
418 }
97cd965c 419
e0e2d644 420 caches = vring_get_region_caches(vq);
abdd16f4
SH
421 if (!caches) {
422 return;
423 }
424
97cd965c
PB
425 pa = offsetof(VRingUsed, ring[vq->vring.num]);
426 virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
3cdf8473 427 address_space_cache_invalidate(&caches->used, pa, sizeof(val));
bcbabae8
MT
428}
429
683f7665 430static void virtio_queue_split_set_notification(VirtQueue *vq, int enable)
967f97fa 431{
b5f53d04
DDAG
432 RCU_READ_LOCK_GUARD();
433
95129d6f 434 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 435 vring_set_avail_event(vq, vring_avail_idx(vq));
bcbabae8 436 } else if (enable) {
967f97fa 437 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 438 } else {
967f97fa 439 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 440 }
92045d80
MT
441 if (enable) {
442 /* Expose avail event/used flags before caller checks the avail idx. */
443 smp_mb();
444 }
967f97fa
AL
445}
446
683f7665
JW
447static void virtio_queue_packed_set_notification(VirtQueue *vq, int enable)
448{
449 uint16_t off_wrap;
450 VRingPackedDescEvent e;
451 VRingMemoryRegionCaches *caches;
452
b5f53d04 453 RCU_READ_LOCK_GUARD();
abdd16f4
SH
454 caches = vring_get_region_caches(vq);
455 if (!caches) {
456 return;
457 }
458
683f7665
JW
459 vring_packed_event_read(vq->vdev, &caches->used, &e);
460
461 if (!enable) {
462 e.flags = VRING_PACKED_EVENT_FLAG_DISABLE;
463 } else if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
464 off_wrap = vq->shadow_avail_idx | vq->shadow_avail_wrap_counter << 15;
465 vring_packed_off_wrap_write(vq->vdev, &caches->used, off_wrap);
466 /* Make sure off_wrap is wrote before flags */
467 smp_wmb();
468 e.flags = VRING_PACKED_EVENT_FLAG_DESC;
469 } else {
470 e.flags = VRING_PACKED_EVENT_FLAG_ENABLE;
471 }
472
473 vring_packed_flags_write(vq->vdev, &caches->used, e.flags);
474 if (enable) {
475 /* Expose avail event/used flags before caller checks the avail idx. */
476 smp_mb();
477 }
683f7665
JW
478}
479
d0435bc5
SH
480bool virtio_queue_get_notification(VirtQueue *vq)
481{
482 return vq->notification;
483}
484
683f7665
JW
485void virtio_queue_set_notification(VirtQueue *vq, int enable)
486{
487 vq->notification = enable;
488
489 if (!vq->vring.desc) {
490 return;
491 }
492
493 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
494 virtio_queue_packed_set_notification(vq, enable);
495 } else {
496 virtio_queue_split_set_notification(vq, enable);
497 }
498}
499
967f97fa
AL
500int virtio_queue_ready(VirtQueue *vq)
501{
502 return vq->vring.avail != 0;
503}
504
86044b24
JW
505static void vring_packed_desc_read_flags(VirtIODevice *vdev,
506 uint16_t *flags,
507 MemoryRegionCache *cache,
508 int i)
509{
510 address_space_read_cached(cache,
511 i * sizeof(VRingPackedDesc) +
512 offsetof(VRingPackedDesc, flags),
513 flags, sizeof(*flags));
514 virtio_tswap16s(vdev, flags);
515}
516
517static void vring_packed_desc_read(VirtIODevice *vdev,
518 VRingPackedDesc *desc,
519 MemoryRegionCache *cache,
520 int i, bool strict_order)
521{
522 hwaddr off = i * sizeof(VRingPackedDesc);
523
524 vring_packed_desc_read_flags(vdev, &desc->flags, cache, i);
525
526 if (strict_order) {
527 /* Make sure flags is read before the rest fields. */
528 smp_rmb();
529 }
530
531 address_space_read_cached(cache, off + offsetof(VRingPackedDesc, addr),
532 &desc->addr, sizeof(desc->addr));
533 address_space_read_cached(cache, off + offsetof(VRingPackedDesc, id),
534 &desc->id, sizeof(desc->id));
535 address_space_read_cached(cache, off + offsetof(VRingPackedDesc, len),
536 &desc->len, sizeof(desc->len));
537 virtio_tswap64s(vdev, &desc->addr);
538 virtio_tswap16s(vdev, &desc->id);
539 virtio_tswap32s(vdev, &desc->len);
540}
541
542static void vring_packed_desc_write_data(VirtIODevice *vdev,
543 VRingPackedDesc *desc,
544 MemoryRegionCache *cache,
545 int i)
546{
547 hwaddr off_id = i * sizeof(VRingPackedDesc) +
548 offsetof(VRingPackedDesc, id);
549 hwaddr off_len = i * sizeof(VRingPackedDesc) +
550 offsetof(VRingPackedDesc, len);
551
552 virtio_tswap32s(vdev, &desc->len);
553 virtio_tswap16s(vdev, &desc->id);
554 address_space_write_cached(cache, off_id, &desc->id, sizeof(desc->id));
555 address_space_cache_invalidate(cache, off_id, sizeof(desc->id));
556 address_space_write_cached(cache, off_len, &desc->len, sizeof(desc->len));
557 address_space_cache_invalidate(cache, off_len, sizeof(desc->len));
558}
559
560static void vring_packed_desc_write_flags(VirtIODevice *vdev,
561 VRingPackedDesc *desc,
562 MemoryRegionCache *cache,
563 int i)
564{
565 hwaddr off = i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flags);
566
567 virtio_tswap16s(vdev, &desc->flags);
568 address_space_write_cached(cache, off, &desc->flags, sizeof(desc->flags));
569 address_space_cache_invalidate(cache, off, sizeof(desc->flags));
570}
571
572static void vring_packed_desc_write(VirtIODevice *vdev,
573 VRingPackedDesc *desc,
574 MemoryRegionCache *cache,
575 int i, bool strict_order)
576{
577 vring_packed_desc_write_data(vdev, desc, cache, i);
578 if (strict_order) {
579 /* Make sure data is wrote before flags. */
580 smp_wmb();
581 }
582 vring_packed_desc_write_flags(vdev, desc, cache, i);
583}
584
585static inline bool is_desc_avail(uint16_t flags, bool wrap_counter)
586{
587 bool avail, used;
588
589 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
590 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
591 return (avail != used) && (avail == wrap_counter);
592}
593
be1fea9b 594/* Fetch avail_idx from VQ memory only when we really need to know if
97cd965c
PB
595 * guest has added some buffers.
596 * Called within rcu_read_lock(). */
597static int virtio_queue_empty_rcu(VirtQueue *vq)
967f97fa 598{
9d7bd082 599 if (virtio_device_disabled(vq->vdev)) {
2d1df859
FZ
600 return 1;
601 }
602
168e4af3
JW
603 if (unlikely(!vq->vring.avail)) {
604 return 1;
605 }
606
be1fea9b
VM
607 if (vq->shadow_avail_idx != vq->last_avail_idx) {
608 return 0;
609 }
610
967f97fa
AL
611 return vring_avail_idx(vq) == vq->last_avail_idx;
612}
613
86044b24 614static int virtio_queue_split_empty(VirtQueue *vq)
97cd965c
PB
615{
616 bool empty;
617
9d7bd082 618 if (virtio_device_disabled(vq->vdev)) {
2d1df859
FZ
619 return 1;
620 }
621
168e4af3
JW
622 if (unlikely(!vq->vring.avail)) {
623 return 1;
624 }
625
97cd965c
PB
626 if (vq->shadow_avail_idx != vq->last_avail_idx) {
627 return 0;
628 }
629
b5f53d04 630 RCU_READ_LOCK_GUARD();
97cd965c 631 empty = vring_avail_idx(vq) == vq->last_avail_idx;
97cd965c
PB
632 return empty;
633}
634
7f51bedd 635/* Called within rcu_read_lock(). */
86044b24
JW
636static int virtio_queue_packed_empty_rcu(VirtQueue *vq)
637{
638 struct VRingPackedDesc desc;
639 VRingMemoryRegionCaches *cache;
640
641 if (unlikely(!vq->vring.desc)) {
642 return 1;
643 }
644
645 cache = vring_get_region_caches(vq);
abdd16f4
SH
646 if (!cache) {
647 return 1;
648 }
649
86044b24
JW
650 vring_packed_desc_read_flags(vq->vdev, &desc.flags, &cache->desc,
651 vq->last_avail_idx);
652
653 return !is_desc_avail(desc.flags, vq->last_avail_wrap_counter);
654}
655
656static int virtio_queue_packed_empty(VirtQueue *vq)
657{
b5f53d04
DDAG
658 RCU_READ_LOCK_GUARD();
659 return virtio_queue_packed_empty_rcu(vq);
86044b24
JW
660}
661
662int virtio_queue_empty(VirtQueue *vq)
663{
664 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
665 return virtio_queue_packed_empty(vq);
666 } else {
667 return virtio_queue_split_empty(vq);
668 }
669}
670
ce317461
JW
671static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
672 unsigned int len)
967f97fa 673{
8607f5c3 674 AddressSpace *dma_as = vq->vdev->dma_as;
967f97fa
AL
675 unsigned int offset;
676 int i;
677
967f97fa
AL
678 offset = 0;
679 for (i = 0; i < elem->in_num; i++) {
680 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
681
8607f5c3
JW
682 dma_memory_unmap(dma_as, elem->in_sg[i].iov_base,
683 elem->in_sg[i].iov_len,
684 DMA_DIRECTION_FROM_DEVICE, size);
967f97fa 685
0cea71a2 686 offset += size;
967f97fa
AL
687 }
688
26b258e1 689 for (i = 0; i < elem->out_num; i++)
8607f5c3
JW
690 dma_memory_unmap(dma_as, elem->out_sg[i].iov_base,
691 elem->out_sg[i].iov_len,
692 DMA_DIRECTION_TO_DEVICE,
693 elem->out_sg[i].iov_len);
ce317461
JW
694}
695
2640d2a5
SH
696/* virtqueue_detach_element:
697 * @vq: The #VirtQueue
698 * @elem: The #VirtQueueElement
699 * @len: number of bytes written
700 *
701 * Detach the element from the virtqueue. This function is suitable for device
702 * reset or other situations where a #VirtQueueElement is simply freed and will
703 * not be pushed or discarded.
704 */
705void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
706 unsigned int len)
707{
86044b24 708 vq->inuse -= elem->ndescs;
2640d2a5
SH
709 virtqueue_unmap_sg(vq, elem, len);
710}
711
86044b24
JW
712static void virtqueue_split_rewind(VirtQueue *vq, unsigned int num)
713{
714 vq->last_avail_idx -= num;
715}
716
717static void virtqueue_packed_rewind(VirtQueue *vq, unsigned int num)
718{
719 if (vq->last_avail_idx < num) {
720 vq->last_avail_idx = vq->vring.num + vq->last_avail_idx - num;
721 vq->last_avail_wrap_counter ^= 1;
722 } else {
723 vq->last_avail_idx -= num;
724 }
725}
726
27e57efe 727/* virtqueue_unpop:
2640d2a5
SH
728 * @vq: The #VirtQueue
729 * @elem: The #VirtQueueElement
730 * @len: number of bytes written
731 *
732 * Pretend the most recent element wasn't popped from the virtqueue. The next
733 * call to virtqueue_pop() will refetch the element.
734 */
27e57efe
LP
735void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
736 unsigned int len)
29b9f5ef 737{
86044b24
JW
738
739 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
740 virtqueue_packed_rewind(vq, 1);
741 } else {
742 virtqueue_split_rewind(vq, 1);
743 }
744
2640d2a5 745 virtqueue_detach_element(vq, elem, len);
29b9f5ef
JW
746}
747
297a75e6
SH
748/* virtqueue_rewind:
749 * @vq: The #VirtQueue
750 * @num: Number of elements to push back
751 *
752 * Pretend that elements weren't popped from the virtqueue. The next
753 * virtqueue_pop() will refetch the oldest element.
754 *
27e57efe 755 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
297a75e6
SH
756 *
757 * Returns: true on success, false if @num is greater than the number of in use
758 * elements.
759 */
760bool virtqueue_rewind(VirtQueue *vq, unsigned int num)
761{
762 if (num > vq->inuse) {
763 return false;
764 }
86044b24 765
297a75e6 766 vq->inuse -= num;
86044b24
JW
767 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
768 virtqueue_packed_rewind(vq, num);
769 } else {
770 virtqueue_split_rewind(vq, num);
771 }
297a75e6
SH
772 return true;
773}
774
86044b24 775static void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *elem,
ce317461
JW
776 unsigned int len, unsigned int idx)
777{
1cdd2ee5
VM
778 VRingUsedElem uelem;
779
168e4af3
JW
780 if (unlikely(!vq->vring.used)) {
781 return;
782 }
783
b796fcd1 784 idx = (idx + vq->used_idx) % vq->vring.num;
967f97fa 785
1cdd2ee5
VM
786 uelem.id = elem->index;
787 uelem.len = len;
788 vring_used_write(vq, &uelem, idx);
967f97fa
AL
789}
790
86044b24
JW
791static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *elem,
792 unsigned int len, unsigned int idx)
793{
794 vq->used_elems[idx].index = elem->index;
795 vq->used_elems[idx].len = len;
796 vq->used_elems[idx].ndescs = elem->ndescs;
797}
798
799static void virtqueue_packed_fill_desc(VirtQueue *vq,
800 const VirtQueueElement *elem,
801 unsigned int idx,
802 bool strict_order)
803{
804 uint16_t head;
805 VRingMemoryRegionCaches *caches;
806 VRingPackedDesc desc = {
807 .id = elem->index,
808 .len = elem->len,
809 };
810 bool wrap_counter = vq->used_wrap_counter;
811
812 if (unlikely(!vq->vring.desc)) {
813 return;
814 }
815
816 head = vq->used_idx + idx;
817 if (head >= vq->vring.num) {
818 head -= vq->vring.num;
819 wrap_counter ^= 1;
820 }
821 if (wrap_counter) {
822 desc.flags |= (1 << VRING_PACKED_DESC_F_AVAIL);
823 desc.flags |= (1 << VRING_PACKED_DESC_F_USED);
824 } else {
825 desc.flags &= ~(1 << VRING_PACKED_DESC_F_AVAIL);
826 desc.flags &= ~(1 << VRING_PACKED_DESC_F_USED);
827 }
828
829 caches = vring_get_region_caches(vq);
abdd16f4
SH
830 if (!caches) {
831 return;
832 }
833
86044b24
JW
834 vring_packed_desc_write(vq->vdev, &desc, &caches->desc, head, strict_order);
835}
836
97cd965c 837/* Called within rcu_read_lock(). */
86044b24
JW
838void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
839 unsigned int len, unsigned int idx)
967f97fa 840{
86044b24
JW
841 trace_virtqueue_fill(vq, elem, len, idx);
842
843 virtqueue_unmap_sg(vq, elem, len);
f5ed3663 844
9d7bd082 845 if (virtio_device_disabled(vq->vdev)) {
f5ed3663
SH
846 return;
847 }
848
86044b24
JW
849 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
850 virtqueue_packed_fill(vq, elem, len, idx);
851 } else {
852 virtqueue_split_fill(vq, elem, len, idx);
853 }
854}
855
856/* Called within rcu_read_lock(). */
857static void virtqueue_split_flush(VirtQueue *vq, unsigned int count)
858{
859 uint16_t old, new;
860
168e4af3
JW
861 if (unlikely(!vq->vring.used)) {
862 return;
863 }
864
967f97fa 865 /* Make sure buffer is written before we update index. */
b90d2f35 866 smp_wmb();
64979a4d 867 trace_virtqueue_flush(vq, count);
b796fcd1 868 old = vq->used_idx;
bcbabae8
MT
869 new = old + count;
870 vring_used_idx_set(vq, new);
967f97fa 871 vq->inuse -= count;
bcbabae8
MT
872 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
873 vq->signalled_used_valid = false;
967f97fa
AL
874}
875
86044b24
JW
876static void virtqueue_packed_flush(VirtQueue *vq, unsigned int count)
877{
878 unsigned int i, ndescs = 0;
879
880 if (unlikely(!vq->vring.desc)) {
881 return;
882 }
883
884 for (i = 1; i < count; i++) {
885 virtqueue_packed_fill_desc(vq, &vq->used_elems[i], i, false);
886 ndescs += vq->used_elems[i].ndescs;
887 }
888 virtqueue_packed_fill_desc(vq, &vq->used_elems[0], 0, true);
889 ndescs += vq->used_elems[0].ndescs;
890
891 vq->inuse -= ndescs;
892 vq->used_idx += ndescs;
893 if (vq->used_idx >= vq->vring.num) {
894 vq->used_idx -= vq->vring.num;
895 vq->used_wrap_counter ^= 1;
896 }
897}
898
899void virtqueue_flush(VirtQueue *vq, unsigned int count)
900{
9d7bd082 901 if (virtio_device_disabled(vq->vdev)) {
86044b24
JW
902 vq->inuse -= count;
903 return;
904 }
905
906 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
907 virtqueue_packed_flush(vq, count);
908 } else {
909 virtqueue_split_flush(vq, count);
910 }
911}
912
967f97fa
AL
913void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
914 unsigned int len)
915{
b5f53d04 916 RCU_READ_LOCK_GUARD();
967f97fa
AL
917 virtqueue_fill(vq, elem, len, 0);
918 virtqueue_flush(vq, 1);
919}
920
97cd965c 921/* Called within rcu_read_lock(). */
967f97fa
AL
922static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
923{
924 uint16_t num_heads = vring_avail_idx(vq) - idx;
925
926 /* Check it isn't doing very strange things with descriptor numbers. */
bb6834cf 927 if (num_heads > vq->vring.num) {
4355c1ab 928 virtio_error(vq->vdev, "Guest moved used index from %u to %u",
be1fea9b 929 idx, vq->shadow_avail_idx);
4355c1ab 930 return -EINVAL;
bb6834cf 931 }
a821ce59
MT
932 /* On success, callers read a descriptor at vq->last_avail_idx.
933 * Make sure descriptor read does not bypass avail index read. */
934 if (num_heads) {
935 smp_rmb();
936 }
967f97fa
AL
937
938 return num_heads;
939}
940
97cd965c 941/* Called within rcu_read_lock(). */
fb1131b6
SH
942static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
943 unsigned int *head)
967f97fa 944{
967f97fa
AL
945 /* Grab the next descriptor number they're advertising, and increment
946 * the index we've seen. */
fb1131b6 947 *head = vring_avail_ring(vq, idx % vq->vring.num);
967f97fa
AL
948
949 /* If their number is silly, that's a fatal mistake. */
fb1131b6
SH
950 if (*head >= vq->vring.num) {
951 virtio_error(vq->vdev, "Guest says index %u is available", *head);
952 return false;
bb6834cf 953 }
967f97fa 954
fb1131b6 955 return true;
967f97fa
AL
956}
957
412e0e81
SH
958enum {
959 VIRTQUEUE_READ_DESC_ERROR = -1,
960 VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
961 VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
962};
967f97fa 963
86044b24
JW
964static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
965 MemoryRegionCache *desc_cache,
966 unsigned int max, unsigned int *next)
412e0e81 967{
967f97fa 968 /* If this descriptor says it doesn't chain, we're done. */
aa570d6f 969 if (!(desc->flags & VRING_DESC_F_NEXT)) {
412e0e81 970 return VIRTQUEUE_READ_DESC_DONE;
cee3ca00 971 }
967f97fa
AL
972
973 /* Check they're not leading us off end of descriptors. */
412e0e81 974 *next = desc->next;
967f97fa 975 /* Make sure compiler knows to grab that: we don't want it changing! */
b90d2f35 976 smp_wmb();
967f97fa 977
412e0e81
SH
978 if (*next >= max) {
979 virtio_error(vdev, "Desc next is %u", *next);
980 return VIRTQUEUE_READ_DESC_ERROR;
bb6834cf 981 }
967f97fa 982
86044b24 983 vring_split_desc_read(vdev, desc, desc_cache, *next);
412e0e81 984 return VIRTQUEUE_READ_DESC_MORE;
967f97fa
AL
985}
986
86044b24
JW
987static void virtqueue_split_get_avail_bytes(VirtQueue *vq,
988 unsigned int *in_bytes, unsigned int *out_bytes,
989 unsigned max_in_bytes, unsigned max_out_bytes)
967f97fa 990{
9796d0ac
PB
991 VirtIODevice *vdev = vq->vdev;
992 unsigned int max, idx;
385ce95d 993 unsigned int total_bufs, in_total, out_total;
991976f7 994 VRingMemoryRegionCaches *caches;
5eba0404
PB
995 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
996 int64_t len = 0;
412e0e81 997 int rc;
967f97fa 998
b5f53d04
DDAG
999 RCU_READ_LOCK_GUARD();
1000
967f97fa 1001 idx = vq->last_avail_idx;
efeea6d0 1002 total_bufs = in_total = out_total = 0;
9796d0ac
PB
1003
1004 max = vq->vring.num;
e0e2d644 1005 caches = vring_get_region_caches(vq);
abdd16f4
SH
1006 if (!caches) {
1007 goto err;
1008 }
1009
4355c1ab 1010 while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
991976f7 1011 MemoryRegionCache *desc_cache = &caches->desc;
9796d0ac 1012 unsigned int num_bufs;
aa570d6f 1013 VRingDesc desc;
b1c7c07f 1014 unsigned int i;
967f97fa 1015
efeea6d0 1016 num_bufs = total_bufs;
fb1131b6
SH
1017
1018 if (!virtqueue_get_head(vq, idx++, &i)) {
1019 goto err;
1020 }
1021
86044b24 1022 vring_split_desc_read(vdev, &desc, desc_cache, i);
efeea6d0 1023
aa570d6f 1024 if (desc.flags & VRING_DESC_F_INDIRECT) {
74231929 1025 if (!desc.len || (desc.len % sizeof(VRingDesc))) {
d65abf85
SH
1026 virtio_error(vdev, "Invalid size for indirect buffer table");
1027 goto err;
efeea6d0
MM
1028 }
1029
1030 /* If we've got too many, that implies a descriptor loop. */
1031 if (num_bufs >= max) {
d65abf85
SH
1032 virtio_error(vdev, "Looped descriptor");
1033 goto err;
efeea6d0
MM
1034 }
1035
1036 /* loop over the indirect descriptor table */
5eba0404
PB
1037 len = address_space_cache_init(&indirect_desc_cache,
1038 vdev->dma_as,
1039 desc.addr, desc.len, false);
1040 desc_cache = &indirect_desc_cache;
9796d0ac
PB
1041 if (len < desc.len) {
1042 virtio_error(vdev, "Cannot map indirect buffer");
1043 goto err;
1044 }
1045
aa570d6f 1046 max = desc.len / sizeof(VRingDesc);
1ae2757c 1047 num_bufs = i = 0;
86044b24 1048 vring_split_desc_read(vdev, &desc, desc_cache, i);
efeea6d0
MM
1049 }
1050
967f97fa
AL
1051 do {
1052 /* If we've got too many, that implies a descriptor loop. */
5774cf98 1053 if (++num_bufs > max) {
d65abf85
SH
1054 virtio_error(vdev, "Looped descriptor");
1055 goto err;
bb6834cf 1056 }
967f97fa 1057
aa570d6f
PB
1058 if (desc.flags & VRING_DESC_F_WRITE) {
1059 in_total += desc.len;
967f97fa 1060 } else {
aa570d6f 1061 out_total += desc.len;
967f97fa 1062 }
e1f7b481
MT
1063 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
1064 goto done;
1065 }
412e0e81 1066
86044b24 1067 rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i);
412e0e81
SH
1068 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1069
1070 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
1071 goto err;
1072 }
efeea6d0 1073
5eba0404
PB
1074 if (desc_cache == &indirect_desc_cache) {
1075 address_space_cache_destroy(&indirect_desc_cache);
efeea6d0 1076 total_bufs++;
9796d0ac
PB
1077 } else {
1078 total_bufs = num_bufs;
1079 }
967f97fa 1080 }
4355c1ab
SH
1081
1082 if (rc < 0) {
1083 goto err;
1084 }
1085
e1f7b481 1086done:
5eba0404 1087 address_space_cache_destroy(&indirect_desc_cache);
0d8d7690 1088 if (in_bytes) {
86044b24
JW
1089 *in_bytes = in_total;
1090 }
1091 if (out_bytes) {
1092 *out_bytes = out_total;
1093 }
86044b24
JW
1094 return;
1095
1096err:
1097 in_total = out_total = 0;
1098 goto done;
1099}
1100
1101static int virtqueue_packed_read_next_desc(VirtQueue *vq,
1102 VRingPackedDesc *desc,
1103 MemoryRegionCache
1104 *desc_cache,
1105 unsigned int max,
1106 unsigned int *next,
1107 bool indirect)
1108{
1109 /* If this descriptor says it doesn't chain, we're done. */
1110 if (!indirect && !(desc->flags & VRING_DESC_F_NEXT)) {
1111 return VIRTQUEUE_READ_DESC_DONE;
1112 }
1113
1114 ++*next;
1115 if (*next == max) {
1116 if (indirect) {
1117 return VIRTQUEUE_READ_DESC_DONE;
1118 } else {
1119 (*next) -= vq->vring.num;
1120 }
1121 }
1122
1123 vring_packed_desc_read(vq->vdev, desc, desc_cache, *next, false);
1124 return VIRTQUEUE_READ_DESC_MORE;
1125}
1126
1127static void virtqueue_packed_get_avail_bytes(VirtQueue *vq,
1128 unsigned int *in_bytes,
1129 unsigned int *out_bytes,
1130 unsigned max_in_bytes,
1131 unsigned max_out_bytes)
1132{
1133 VirtIODevice *vdev = vq->vdev;
1134 unsigned int max, idx;
1135 unsigned int total_bufs, in_total, out_total;
1136 MemoryRegionCache *desc_cache;
1137 VRingMemoryRegionCaches *caches;
1138 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
1139 int64_t len = 0;
1140 VRingPackedDesc desc;
1141 bool wrap_counter;
1142
b5f53d04 1143 RCU_READ_LOCK_GUARD();
86044b24
JW
1144 idx = vq->last_avail_idx;
1145 wrap_counter = vq->last_avail_wrap_counter;
1146 total_bufs = in_total = out_total = 0;
1147
1148 max = vq->vring.num;
1149 caches = vring_get_region_caches(vq);
abdd16f4
SH
1150 if (!caches) {
1151 goto err;
1152 }
86044b24
JW
1153
1154 for (;;) {
1155 unsigned int num_bufs = total_bufs;
1156 unsigned int i = idx;
1157 int rc;
1158
1159 desc_cache = &caches->desc;
1160 vring_packed_desc_read(vdev, &desc, desc_cache, idx, true);
1161 if (!is_desc_avail(desc.flags, wrap_counter)) {
1162 break;
1163 }
1164
1165 if (desc.flags & VRING_DESC_F_INDIRECT) {
1166 if (desc.len % sizeof(VRingPackedDesc)) {
1167 virtio_error(vdev, "Invalid size for indirect buffer table");
1168 goto err;
1169 }
1170
1171 /* If we've got too many, that implies a descriptor loop. */
1172 if (num_bufs >= max) {
1173 virtio_error(vdev, "Looped descriptor");
1174 goto err;
1175 }
1176
1177 /* loop over the indirect descriptor table */
1178 len = address_space_cache_init(&indirect_desc_cache,
1179 vdev->dma_as,
1180 desc.addr, desc.len, false);
1181 desc_cache = &indirect_desc_cache;
1182 if (len < desc.len) {
1183 virtio_error(vdev, "Cannot map indirect buffer");
1184 goto err;
1185 }
1186
1187 max = desc.len / sizeof(VRingPackedDesc);
1188 num_bufs = i = 0;
1189 vring_packed_desc_read(vdev, &desc, desc_cache, i, false);
1190 }
1191
1192 do {
1193 /* If we've got too many, that implies a descriptor loop. */
1194 if (++num_bufs > max) {
1195 virtio_error(vdev, "Looped descriptor");
1196 goto err;
1197 }
1198
1199 if (desc.flags & VRING_DESC_F_WRITE) {
1200 in_total += desc.len;
1201 } else {
1202 out_total += desc.len;
1203 }
1204 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
1205 goto done;
1206 }
1207
1208 rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,
1209 &i, desc_cache ==
1210 &indirect_desc_cache);
1211 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1212
1213 if (desc_cache == &indirect_desc_cache) {
1214 address_space_cache_destroy(&indirect_desc_cache);
1215 total_bufs++;
1216 idx++;
1217 } else {
1218 idx += num_bufs - total_bufs;
1219 total_bufs = num_bufs;
1220 }
1221
1222 if (idx >= vq->vring.num) {
1223 idx -= vq->vring.num;
1224 wrap_counter ^= 1;
1225 }
1226 }
1227
1228 /* Record the index and wrap counter for a kick we want */
1229 vq->shadow_avail_idx = idx;
1230 vq->shadow_avail_wrap_counter = wrap_counter;
1231done:
1232 address_space_cache_destroy(&indirect_desc_cache);
1233 if (in_bytes) {
1234 *in_bytes = in_total;
1235 }
1236 if (out_bytes) {
1237 *out_bytes = out_total;
1238 }
86044b24
JW
1239 return;
1240
1241err:
1242 in_total = out_total = 0;
1243 goto done;
1244}
1245
1246void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
1247 unsigned int *out_bytes,
1248 unsigned max_in_bytes, unsigned max_out_bytes)
1249{
1250 uint16_t desc_size;
1251 VRingMemoryRegionCaches *caches;
1252
1253 if (unlikely(!vq->vring.desc)) {
1254 goto err;
1255 }
1256
1257 caches = vring_get_region_caches(vq);
abdd16f4
SH
1258 if (!caches) {
1259 goto err;
1260 }
1261
86044b24
JW
1262 desc_size = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ?
1263 sizeof(VRingPackedDesc) : sizeof(VRingDesc);
1264 if (caches->desc.len < vq->vring.num * desc_size) {
1265 virtio_error(vq->vdev, "Cannot map descriptor ring");
1266 goto err;
1267 }
1268
1269 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
1270 virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes,
1271 max_in_bytes, max_out_bytes);
1272 } else {
1273 virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes,
1274 max_in_bytes, max_out_bytes);
1275 }
1276
1277 return;
1278err:
1279 if (in_bytes) {
1280 *in_bytes = 0;
0d8d7690
AS
1281 }
1282 if (out_bytes) {
86044b24 1283 *out_bytes = 0;
0d8d7690
AS
1284 }
1285}
967f97fa 1286
0d8d7690
AS
1287int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
1288 unsigned int out_bytes)
1289{
1290 unsigned int in_total, out_total;
1291
e1f7b481
MT
1292 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
1293 return in_bytes <= in_total && out_bytes <= out_total;
967f97fa
AL
1294}
1295
ec55da19
SH
1296static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
1297 hwaddr *addr, struct iovec *iov,
3b3b0628
PB
1298 unsigned int max_num_sg, bool is_write,
1299 hwaddr pa, size_t sz)
1300{
ec55da19 1301 bool ok = false;
3b3b0628
PB
1302 unsigned num_sg = *p_num_sg;
1303 assert(num_sg <= max_num_sg);
1304
1e7aed70 1305 if (!sz) {
ec55da19
SH
1306 virtio_error(vdev, "virtio: zero sized buffers are not allowed");
1307 goto out;
1e7aed70
PP
1308 }
1309
3b3b0628
PB
1310 while (sz) {
1311 hwaddr len = sz;
1312
1313 if (num_sg == max_num_sg) {
ec55da19
SH
1314 virtio_error(vdev, "virtio: too many write descriptors in "
1315 "indirect table");
1316 goto out;
3b3b0628
PB
1317 }
1318
8607f5c3
JW
1319 iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len,
1320 is_write ?
1321 DMA_DIRECTION_FROM_DEVICE :
1322 DMA_DIRECTION_TO_DEVICE);
973e7170 1323 if (!iov[num_sg].iov_base) {
ec55da19
SH
1324 virtio_error(vdev, "virtio: bogus descriptor or out of resources");
1325 goto out;
973e7170
PP
1326 }
1327
3b3b0628
PB
1328 iov[num_sg].iov_len = len;
1329 addr[num_sg] = pa;
1330
1331 sz -= len;
1332 pa += len;
1333 num_sg++;
1334 }
ec55da19
SH
1335 ok = true;
1336
1337out:
3b3b0628 1338 *p_num_sg = num_sg;
ec55da19
SH
1339 return ok;
1340}
1341
1342/* Only used by error code paths before we have a VirtQueueElement (therefore
1343 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
1344 * yet.
1345 */
1346static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
1347 struct iovec *iov)
1348{
1349 unsigned int i;
1350
1351 for (i = 0; i < out_num + in_num; i++) {
1352 int is_write = i >= out_num;
1353
1354 cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0);
1355 iov++;
1356 }
3b3b0628
PB
1357}
1358
8607f5c3 1359static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
e4fbf5b2 1360 hwaddr *addr, unsigned int num_sg,
22953364 1361 bool is_write)
42fb2e07
KW
1362{
1363 unsigned int i;
a8170e5e 1364 hwaddr len;
42fb2e07 1365
e4fbf5b2 1366 for (i = 0; i < num_sg; i++) {
42fb2e07 1367 len = sg[i].iov_len;
8607f5c3
JW
1368 sg[i].iov_base = dma_memory_map(vdev->dma_as,
1369 addr[i], &len, is_write ?
1370 DMA_DIRECTION_FROM_DEVICE :
1371 DMA_DIRECTION_TO_DEVICE);
8059feee 1372 if (!sg[i].iov_base) {
1a285899 1373 error_report("virtio: error trying to map MMIO memory");
42fb2e07
KW
1374 exit(1);
1375 }
3b3b0628
PB
1376 if (len != sg[i].iov_len) {
1377 error_report("virtio: unexpected memory split");
8059feee
MT
1378 exit(1);
1379 }
42fb2e07
KW
1380 }
1381}
1382
8607f5c3 1383void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
8059feee 1384{
22953364
PMD
1385 virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, elem->in_num, true);
1386 virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, elem->out_num,
1387 false);
3724650d
PB
1388}
1389
bf91bd27 1390static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
3724650d
PB
1391{
1392 VirtQueueElement *elem;
1393 size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
1394 size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
1395 size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
1396 size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
1397 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
1398 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
1399
1400 assert(sz >= sizeof(VirtQueueElement));
1401 elem = g_malloc(out_sg_end);
b0ac429f 1402 trace_virtqueue_alloc_element(elem, sz, in_num, out_num);
3724650d
PB
1403 elem->out_num = out_num;
1404 elem->in_num = in_num;
1405 elem->in_addr = (void *)elem + in_addr_ofs;
1406 elem->out_addr = (void *)elem + out_addr_ofs;
1407 elem->in_sg = (void *)elem + in_sg_ofs;
1408 elem->out_sg = (void *)elem + out_sg_ofs;
1409 return elem;
8059feee
MT
1410}
1411
86044b24 1412static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)
967f97fa 1413{
5774cf98 1414 unsigned int i, head, max;
991976f7 1415 VRingMemoryRegionCaches *caches;
5eba0404
PB
1416 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
1417 MemoryRegionCache *desc_cache;
1418 int64_t len;
cee3ca00 1419 VirtIODevice *vdev = vq->vdev;
9796d0ac 1420 VirtQueueElement *elem = NULL;
37ef70be 1421 unsigned out_num, in_num, elem_entries;
3b3b0628
PB
1422 hwaddr addr[VIRTQUEUE_MAX_SIZE];
1423 struct iovec iov[VIRTQUEUE_MAX_SIZE];
aa570d6f 1424 VRingDesc desc;
412e0e81 1425 int rc;
967f97fa 1426
b5f53d04 1427 RCU_READ_LOCK_GUARD();
97cd965c
PB
1428 if (virtio_queue_empty_rcu(vq)) {
1429 goto done;
51b19ebe 1430 }
be1fea9b
VM
1431 /* Needed after virtio_queue_empty(), see comment in
1432 * virtqueue_num_heads(). */
1433 smp_rmb();
967f97fa
AL
1434
1435 /* When we start there are none of either input nor output. */
37ef70be 1436 out_num = in_num = elem_entries = 0;
967f97fa 1437
5774cf98
MM
1438 max = vq->vring.num;
1439
afd9096e 1440 if (vq->inuse >= vq->vring.num) {
ec55da19 1441 virtio_error(vdev, "Virtqueue size exceeded");
97cd965c 1442 goto done;
afd9096e
SH
1443 }
1444
fb1131b6 1445 if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
97cd965c 1446 goto done;
fb1131b6
SH
1447 }
1448
95129d6f 1449 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 1450 vring_set_avail_event(vq, vq->last_avail_idx);
bcbabae8 1451 }
efeea6d0 1452
fb1131b6 1453 i = head;
9796d0ac 1454
e0e2d644 1455 caches = vring_get_region_caches(vq);
abdd16f4
SH
1456 if (!caches) {
1457 virtio_error(vdev, "Region caches not initialized");
1458 goto done;
1459 }
1460
991976f7 1461 if (caches->desc.len < max * sizeof(VRingDesc)) {
9796d0ac
PB
1462 virtio_error(vdev, "Cannot map descriptor ring");
1463 goto done;
1464 }
1465
991976f7 1466 desc_cache = &caches->desc;
86044b24 1467 vring_split_desc_read(vdev, &desc, desc_cache, i);
aa570d6f 1468 if (desc.flags & VRING_DESC_F_INDIRECT) {
74231929 1469 if (!desc.len || (desc.len % sizeof(VRingDesc))) {
ec55da19 1470 virtio_error(vdev, "Invalid size for indirect buffer table");
9796d0ac 1471 goto done;
efeea6d0
MM
1472 }
1473
1474 /* loop over the indirect descriptor table */
5eba0404
PB
1475 len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
1476 desc.addr, desc.len, false);
1477 desc_cache = &indirect_desc_cache;
9796d0ac
PB
1478 if (len < desc.len) {
1479 virtio_error(vdev, "Cannot map indirect buffer");
1480 goto done;
1481 }
1482
aa570d6f 1483 max = desc.len / sizeof(VRingDesc);
efeea6d0 1484 i = 0;
86044b24 1485 vring_split_desc_read(vdev, &desc, desc_cache, i);
efeea6d0
MM
1486 }
1487
42fb2e07 1488 /* Collect all the descriptors */
967f97fa 1489 do {
ec55da19
SH
1490 bool map_ok;
1491
aa570d6f 1492 if (desc.flags & VRING_DESC_F_WRITE) {
ec55da19
SH
1493 map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
1494 iov + out_num,
1495 VIRTQUEUE_MAX_SIZE - out_num, true,
1496 desc.addr, desc.len);
42fb2e07 1497 } else {
3b3b0628 1498 if (in_num) {
ec55da19
SH
1499 virtio_error(vdev, "Incorrect order for descriptors");
1500 goto err_undo_map;
c8eac1cf 1501 }
ec55da19
SH
1502 map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
1503 VIRTQUEUE_MAX_SIZE, false,
1504 desc.addr, desc.len);
1505 }
1506 if (!map_ok) {
1507 goto err_undo_map;
42fb2e07 1508 }
967f97fa 1509
967f97fa 1510 /* If we've got too many, that implies a descriptor loop. */
37ef70be 1511 if (++elem_entries > max) {
ec55da19
SH
1512 virtio_error(vdev, "Looped descriptor");
1513 goto err_undo_map;
bb6834cf 1514 }
412e0e81 1515
86044b24 1516 rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i);
412e0e81
SH
1517 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1518
1519 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
1520 goto err_undo_map;
1521 }
967f97fa 1522
3b3b0628
PB
1523 /* Now copy what we have collected and mapped */
1524 elem = virtqueue_alloc_element(sz, out_num, in_num);
967f97fa 1525 elem->index = head;
86044b24 1526 elem->ndescs = 1;
3b3b0628
PB
1527 for (i = 0; i < out_num; i++) {
1528 elem->out_addr[i] = addr[i];
1529 elem->out_sg[i] = iov[i];
1530 }
1531 for (i = 0; i < in_num; i++) {
1532 elem->in_addr[i] = addr[out_num + i];
1533 elem->in_sg[i] = iov[out_num + i];
1534 }
967f97fa
AL
1535
1536 vq->inuse++;
1537
64979a4d 1538 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
9796d0ac 1539done:
5eba0404 1540 address_space_cache_destroy(&indirect_desc_cache);
9796d0ac 1541
51b19ebe 1542 return elem;
ec55da19
SH
1543
1544err_undo_map:
1545 virtqueue_undo_map_desc(out_num, in_num, iov);
9796d0ac 1546 goto done;
967f97fa
AL
1547}
1548
86044b24
JW
1549static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
1550{
1551 unsigned int i, max;
1552 VRingMemoryRegionCaches *caches;
1553 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
1554 MemoryRegionCache *desc_cache;
1555 int64_t len;
1556 VirtIODevice *vdev = vq->vdev;
1557 VirtQueueElement *elem = NULL;
1558 unsigned out_num, in_num, elem_entries;
1559 hwaddr addr[VIRTQUEUE_MAX_SIZE];
1560 struct iovec iov[VIRTQUEUE_MAX_SIZE];
1561 VRingPackedDesc desc;
1562 uint16_t id;
1563 int rc;
1564
b5f53d04 1565 RCU_READ_LOCK_GUARD();
86044b24
JW
1566 if (virtio_queue_packed_empty_rcu(vq)) {
1567 goto done;
1568 }
1569
1570 /* When we start there are none of either input nor output. */
1571 out_num = in_num = elem_entries = 0;
1572
1573 max = vq->vring.num;
1574
1575 if (vq->inuse >= vq->vring.num) {
1576 virtio_error(vdev, "Virtqueue size exceeded");
1577 goto done;
1578 }
1579
1580 i = vq->last_avail_idx;
1581
1582 caches = vring_get_region_caches(vq);
abdd16f4
SH
1583 if (!caches) {
1584 virtio_error(vdev, "Region caches not initialized");
1585 goto done;
1586 }
1587
86044b24
JW
1588 if (caches->desc.len < max * sizeof(VRingDesc)) {
1589 virtio_error(vdev, "Cannot map descriptor ring");
1590 goto done;
1591 }
1592
1593 desc_cache = &caches->desc;
1594 vring_packed_desc_read(vdev, &desc, desc_cache, i, true);
1595 id = desc.id;
1596 if (desc.flags & VRING_DESC_F_INDIRECT) {
1597 if (desc.len % sizeof(VRingPackedDesc)) {
1598 virtio_error(vdev, "Invalid size for indirect buffer table");
1599 goto done;
1600 }
1601
1602 /* loop over the indirect descriptor table */
1603 len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
1604 desc.addr, desc.len, false);
1605 desc_cache = &indirect_desc_cache;
1606 if (len < desc.len) {
1607 virtio_error(vdev, "Cannot map indirect buffer");
1608 goto done;
1609 }
1610
1611 max = desc.len / sizeof(VRingPackedDesc);
1612 i = 0;
1613 vring_packed_desc_read(vdev, &desc, desc_cache, i, false);
1614 }
1615
1616 /* Collect all the descriptors */
1617 do {
1618 bool map_ok;
1619
1620 if (desc.flags & VRING_DESC_F_WRITE) {
1621 map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
1622 iov + out_num,
1623 VIRTQUEUE_MAX_SIZE - out_num, true,
1624 desc.addr, desc.len);
1625 } else {
1626 if (in_num) {
1627 virtio_error(vdev, "Incorrect order for descriptors");
1628 goto err_undo_map;
1629 }
1630 map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
1631 VIRTQUEUE_MAX_SIZE, false,
1632 desc.addr, desc.len);
1633 }
1634 if (!map_ok) {
1635 goto err_undo_map;
1636 }
1637
1638 /* If we've got too many, that implies a descriptor loop. */
1639 if (++elem_entries > max) {
1640 virtio_error(vdev, "Looped descriptor");
1641 goto err_undo_map;
1642 }
1643
1644 rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
1645 desc_cache ==
1646 &indirect_desc_cache);
1647 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1648
1649 /* Now copy what we have collected and mapped */
1650 elem = virtqueue_alloc_element(sz, out_num, in_num);
1651 for (i = 0; i < out_num; i++) {
1652 elem->out_addr[i] = addr[i];
1653 elem->out_sg[i] = iov[i];
1654 }
1655 for (i = 0; i < in_num; i++) {
1656 elem->in_addr[i] = addr[out_num + i];
1657 elem->in_sg[i] = iov[out_num + i];
1658 }
1659
1660 elem->index = id;
1661 elem->ndescs = (desc_cache == &indirect_desc_cache) ? 1 : elem_entries;
1662 vq->last_avail_idx += elem->ndescs;
1663 vq->inuse += elem->ndescs;
1664
1665 if (vq->last_avail_idx >= vq->vring.num) {
1666 vq->last_avail_idx -= vq->vring.num;
1667 vq->last_avail_wrap_counter ^= 1;
1668 }
1669
1670 vq->shadow_avail_idx = vq->last_avail_idx;
1671 vq->shadow_avail_wrap_counter = vq->last_avail_wrap_counter;
1672
1673 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
1674done:
1675 address_space_cache_destroy(&indirect_desc_cache);
86044b24
JW
1676
1677 return elem;
1678
1679err_undo_map:
1680 virtqueue_undo_map_desc(out_num, in_num, iov);
1681 goto done;
1682}
1683
1684void *virtqueue_pop(VirtQueue *vq, size_t sz)
1685{
9d7bd082 1686 if (virtio_device_disabled(vq->vdev)) {
86044b24
JW
1687 return NULL;
1688 }
1689
1690 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
1691 return virtqueue_packed_pop(vq, sz);
1692 } else {
1693 return virtqueue_split_pop(vq, sz);
1694 }
1695}
1696
1697static unsigned int virtqueue_packed_drop_all(VirtQueue *vq)
54e17709 1698{
86044b24
JW
1699 VRingMemoryRegionCaches *caches;
1700 MemoryRegionCache *desc_cache;
54e17709
YB
1701 unsigned int dropped = 0;
1702 VirtQueueElement elem = {};
1703 VirtIODevice *vdev = vq->vdev;
86044b24 1704 VRingPackedDesc desc;
54e17709 1705
86044b24 1706 caches = vring_get_region_caches(vq);
abdd16f4
SH
1707 if (!caches) {
1708 return 0;
1709 }
1710
86044b24
JW
1711 desc_cache = &caches->desc;
1712
1713 virtio_queue_set_notification(vq, 0);
1714
1715 while (vq->inuse < vq->vring.num) {
1716 unsigned int idx = vq->last_avail_idx;
1717 /*
1718 * works similar to virtqueue_pop but does not map buffers
1719 * and does not allocate any memory.
1720 */
1721 vring_packed_desc_read(vdev, &desc, desc_cache,
1722 vq->last_avail_idx , true);
1723 if (!is_desc_avail(desc.flags, vq->last_avail_wrap_counter)) {
1724 break;
1725 }
1726 elem.index = desc.id;
1727 elem.ndescs = 1;
1728 while (virtqueue_packed_read_next_desc(vq, &desc, desc_cache,
1729 vq->vring.num, &idx, false)) {
1730 ++elem.ndescs;
1731 }
1732 /*
1733 * immediately push the element, nothing to unmap
1734 * as both in_num and out_num are set to 0.
1735 */
1736 virtqueue_push(vq, &elem, 0);
1737 dropped++;
1738 vq->last_avail_idx += elem.ndescs;
1739 if (vq->last_avail_idx >= vq->vring.num) {
1740 vq->last_avail_idx -= vq->vring.num;
1741 vq->last_avail_wrap_counter ^= 1;
1742 }
54e17709
YB
1743 }
1744
86044b24
JW
1745 return dropped;
1746}
1747
1748static unsigned int virtqueue_split_drop_all(VirtQueue *vq)
1749{
1750 unsigned int dropped = 0;
1751 VirtQueueElement elem = {};
1752 VirtIODevice *vdev = vq->vdev;
1753 bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
1754
54e17709
YB
1755 while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) {
1756 /* works similar to virtqueue_pop but does not map buffers
1757 * and does not allocate any memory */
1758 smp_rmb();
1759 if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) {
1760 break;
1761 }
1762 vq->inuse++;
1763 vq->last_avail_idx++;
1764 if (fEventIdx) {
1765 vring_set_avail_event(vq, vq->last_avail_idx);
1766 }
1767 /* immediately push the element, nothing to unmap
1768 * as both in_num and out_num are set to 0 */
1769 virtqueue_push(vq, &elem, 0);
1770 dropped++;
1771 }
1772
1773 return dropped;
1774}
1775
86044b24
JW
1776/* virtqueue_drop_all:
1777 * @vq: The #VirtQueue
1778 * Drops all queued buffers and indicates them to the guest
1779 * as if they are done. Useful when buffers can not be
1780 * processed but must be returned to the guest.
1781 */
1782unsigned int virtqueue_drop_all(VirtQueue *vq)
1783{
1784 struct VirtIODevice *vdev = vq->vdev;
1785
9d7bd082 1786 if (virtio_device_disabled(vq->vdev)) {
86044b24
JW
1787 return 0;
1788 }
1789
1790 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
1791 return virtqueue_packed_drop_all(vq);
1792 } else {
1793 return virtqueue_split_drop_all(vq);
1794 }
1795}
1796
3724650d
PB
1797/* Reading and writing a structure directly to QEMUFile is *awful*, but
1798 * it is what QEMU has always done by mistake. We can change it sooner
1799 * or later by bumping the version number of the affected vm states.
1800 * In the meanwhile, since the in-memory layout of VirtQueueElement
1801 * has changed, we need to marshal to and from the layout that was
1802 * used before the change.
1803 */
1804typedef struct VirtQueueElementOld {
1805 unsigned int index;
1806 unsigned int out_num;
1807 unsigned int in_num;
1808 hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
1809 hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
1810 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
1811 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
1812} VirtQueueElementOld;
1813
8607f5c3 1814void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
ab281c17 1815{
3724650d
PB
1816 VirtQueueElement *elem;
1817 VirtQueueElementOld data;
1818 int i;
1819
1820 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
1821
6bdc21c0
MT
1822 /* TODO: teach all callers that this can fail, and return failure instead
1823 * of asserting here.
262a69f4
EB
1824 * This is just one thing (there are probably more) that must be
1825 * fixed before we can allow NDEBUG compilation.
6bdc21c0 1826 */
6bdc21c0
MT
1827 assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
1828 assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
1829
3724650d
PB
1830 elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
1831 elem->index = data.index;
1832
1833 for (i = 0; i < elem->in_num; i++) {
1834 elem->in_addr[i] = data.in_addr[i];
1835 }
1836
1837 for (i = 0; i < elem->out_num; i++) {
1838 elem->out_addr[i] = data.out_addr[i];
1839 }
1840
1841 for (i = 0; i < elem->in_num; i++) {
1842 /* Base is overwritten by virtqueue_map. */
1843 elem->in_sg[i].iov_base = 0;
1844 elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
1845 }
1846
1847 for (i = 0; i < elem->out_num; i++) {
1848 /* Base is overwritten by virtqueue_map. */
1849 elem->out_sg[i].iov_base = 0;
1850 elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
1851 }
1852
86044b24
JW
1853 if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
1854 qemu_get_be32s(f, &elem->ndescs);
1855 }
1856
8607f5c3 1857 virtqueue_map(vdev, elem);
ab281c17
PB
1858 return elem;
1859}
1860
86044b24
JW
1861void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
1862 VirtQueueElement *elem)
ab281c17 1863{
3724650d
PB
1864 VirtQueueElementOld data;
1865 int i;
1866
1867 memset(&data, 0, sizeof(data));
1868 data.index = elem->index;
1869 data.in_num = elem->in_num;
1870 data.out_num = elem->out_num;
1871
1872 for (i = 0; i < elem->in_num; i++) {
1873 data.in_addr[i] = elem->in_addr[i];
1874 }
1875
1876 for (i = 0; i < elem->out_num; i++) {
1877 data.out_addr[i] = elem->out_addr[i];
1878 }
1879
1880 for (i = 0; i < elem->in_num; i++) {
1881 /* Base is overwritten by virtqueue_map when loading. Do not
1882 * save it, as it would leak the QEMU address space layout. */
1883 data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
1884 }
1885
1886 for (i = 0; i < elem->out_num; i++) {
1887 /* Do not save iov_base as above. */
1888 data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
1889 }
86044b24
JW
1890
1891 if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
1892 qemu_put_be32s(f, &elem->ndescs);
1893 }
1894
3724650d 1895 qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
ab281c17
PB
1896}
1897
967f97fa 1898/* virtio device */
7055e687
MT
1899static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
1900{
1c819449
FK
1901 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1902 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1903
9d7bd082 1904 if (virtio_device_disabled(vdev)) {
f5ed3663
SH
1905 return;
1906 }
1907
1c819449
FK
1908 if (k->notify) {
1909 k->notify(qbus->parent, vector);
7055e687
MT
1910 }
1911}
967f97fa 1912
53c25cea 1913void virtio_update_irq(VirtIODevice *vdev)
967f97fa 1914{
7055e687 1915 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
967f97fa
AL
1916}
1917
0b352fd6
CH
1918static int virtio_validate_features(VirtIODevice *vdev)
1919{
1920 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1921
8607f5c3
JW
1922 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) &&
1923 !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
1924 return -EFAULT;
1925 }
1926
0b352fd6
CH
1927 if (k->validate_features) {
1928 return k->validate_features(vdev);
1929 } else {
1930 return 0;
1931 }
1932}
1933
1934int virtio_set_status(VirtIODevice *vdev, uint8_t val)
4e1837f8 1935{
181103cd 1936 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
4e1837f8
SH
1937 trace_virtio_set_status(vdev, val);
1938
95129d6f 1939 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
0b352fd6
CH
1940 if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
1941 val & VIRTIO_CONFIG_S_FEATURES_OK) {
1942 int ret = virtio_validate_features(vdev);
1943
1944 if (ret) {
1945 return ret;
1946 }
1947 }
1948 }
e57f2c31 1949
4c5cf37b
XY
1950 if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) !=
1951 (val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1952 virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK);
1953 }
badaf79c 1954
181103cd
FK
1955 if (k->set_status) {
1956 k->set_status(vdev, val);
4e1837f8
SH
1957 }
1958 vdev->status = val;
badaf79c 1959
0b352fd6 1960 return 0;
4e1837f8
SH
1961}
1962
616a6552
GK
1963static enum virtio_device_endian virtio_default_endian(void)
1964{
1965 if (target_words_bigendian()) {
1966 return VIRTIO_DEVICE_ENDIAN_BIG;
1967 } else {
1968 return VIRTIO_DEVICE_ENDIAN_LITTLE;
1969 }
1970}
1971
1972static enum virtio_device_endian virtio_current_cpu_endian(void)
1973{
cdba7e2f 1974 if (cpu_virtio_is_big_endian(current_cpu)) {
616a6552
GK
1975 return VIRTIO_DEVICE_ENDIAN_BIG;
1976 } else {
1977 return VIRTIO_DEVICE_ENDIAN_LITTLE;
1978 }
1979}
1980
53c25cea 1981void virtio_reset(void *opaque)
967f97fa
AL
1982{
1983 VirtIODevice *vdev = opaque;
181103cd 1984 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
1985 int i;
1986
e0c472d8 1987 virtio_set_status(vdev, 0);
616a6552
GK
1988 if (current_cpu) {
1989 /* Guest initiated reset */
1990 vdev->device_endian = virtio_current_cpu_endian();
1991 } else {
1992 /* System reset */
1993 vdev->device_endian = virtio_default_endian();
1994 }
e0c472d8 1995
181103cd
FK
1996 if (k->reset) {
1997 k->reset(vdev);
1998 }
967f97fa 1999
868a8f44 2000 vdev->start_on_kick = false;
badaf79c 2001 vdev->started = false;
f5ed3663 2002 vdev->broken = false;
704a76fc 2003 vdev->guest_features = 0;
967f97fa
AL
2004 vdev->queue_sel = 0;
2005 vdev->status = 0;
9d7bd082 2006 vdev->disabled = false;
d73415a3 2007 qatomic_set(&vdev->isr, 0);
7055e687
MT
2008 vdev->config_vector = VIRTIO_NO_VECTOR;
2009 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa 2010
87b3bd1c 2011 for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2012 vdev->vq[i].vring.desc = 0;
2013 vdev->vq[i].vring.avail = 0;
2014 vdev->vq[i].vring.used = 0;
2015 vdev->vq[i].last_avail_idx = 0;
be1fea9b 2016 vdev->vq[i].shadow_avail_idx = 0;
b796fcd1 2017 vdev->vq[i].used_idx = 0;
86044b24
JW
2018 vdev->vq[i].last_avail_wrap_counter = true;
2019 vdev->vq[i].shadow_avail_wrap_counter = true;
2020 vdev->vq[i].used_wrap_counter = true;
e0d686bf 2021 virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
bcbabae8
MT
2022 vdev->vq[i].signalled_used = 0;
2023 vdev->vq[i].signalled_used_valid = false;
332fa82d 2024 vdev->vq[i].notification = true;
46c5d082 2025 vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
4b7f91ed 2026 vdev->vq[i].inuse = 0;
e0e2d644 2027 virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
967f97fa
AL
2028 }
2029}
2030
53c25cea 2031uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
967f97fa 2032{
181103cd 2033 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2034 uint8_t val;
2035
5f5a1318 2036 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2037 return (uint32_t)-1;
5f5a1318
JW
2038 }
2039
2040 k->get_config(vdev, vdev->config);
967f97fa 2041
06dbfc6f 2042 val = ldub_p(vdev->config + addr);
967f97fa
AL
2043 return val;
2044}
2045
53c25cea 2046uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
967f97fa 2047{
181103cd 2048 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2049 uint16_t val;
2050
5f5a1318 2051 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2052 return (uint32_t)-1;
5f5a1318
JW
2053 }
2054
2055 k->get_config(vdev, vdev->config);
967f97fa 2056
06dbfc6f 2057 val = lduw_p(vdev->config + addr);
967f97fa
AL
2058 return val;
2059}
2060
53c25cea 2061uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
967f97fa 2062{
181103cd 2063 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2064 uint32_t val;
2065
5f5a1318 2066 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2067 return (uint32_t)-1;
5f5a1318
JW
2068 }
2069
2070 k->get_config(vdev, vdev->config);
967f97fa 2071
06dbfc6f 2072 val = ldl_p(vdev->config + addr);
967f97fa
AL
2073 return val;
2074}
2075
53c25cea 2076void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 2077{
181103cd 2078 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2079 uint8_t val = data;
2080
5f5a1318 2081 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2082 return;
5f5a1318 2083 }
967f97fa 2084
06dbfc6f 2085 stb_p(vdev->config + addr, val);
967f97fa 2086
181103cd
FK
2087 if (k->set_config) {
2088 k->set_config(vdev, vdev->config);
2089 }
967f97fa
AL
2090}
2091
53c25cea 2092void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 2093{
181103cd 2094 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2095 uint16_t val = data;
2096
5f5a1318 2097 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2098 return;
5f5a1318 2099 }
967f97fa 2100
06dbfc6f 2101 stw_p(vdev->config + addr, val);
967f97fa 2102
181103cd
FK
2103 if (k->set_config) {
2104 k->set_config(vdev, vdev->config);
2105 }
967f97fa
AL
2106}
2107
53c25cea 2108void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 2109{
181103cd 2110 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2111 uint32_t val = data;
2112
5f5a1318 2113 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2114 return;
5f5a1318 2115 }
967f97fa 2116
06dbfc6f 2117 stl_p(vdev->config + addr, val);
967f97fa 2118
181103cd
FK
2119 if (k->set_config) {
2120 k->set_config(vdev, vdev->config);
2121 }
967f97fa
AL
2122}
2123
adfb743c
MT
2124uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
2125{
2126 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2127 uint8_t val;
2128
2129 if (addr + sizeof(val) > vdev->config_len) {
2130 return (uint32_t)-1;
2131 }
2132
2133 k->get_config(vdev, vdev->config);
2134
2135 val = ldub_p(vdev->config + addr);
2136 return val;
2137}
2138
2139uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
2140{
2141 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2142 uint16_t val;
2143
2144 if (addr + sizeof(val) > vdev->config_len) {
2145 return (uint32_t)-1;
2146 }
2147
2148 k->get_config(vdev, vdev->config);
2149
2150 val = lduw_le_p(vdev->config + addr);
2151 return val;
2152}
2153
2154uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
2155{
2156 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2157 uint32_t val;
2158
2159 if (addr + sizeof(val) > vdev->config_len) {
2160 return (uint32_t)-1;
2161 }
2162
2163 k->get_config(vdev, vdev->config);
2164
2165 val = ldl_le_p(vdev->config + addr);
2166 return val;
2167}
2168
2169void virtio_config_modern_writeb(VirtIODevice *vdev,
2170 uint32_t addr, uint32_t data)
2171{
2172 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2173 uint8_t val = data;
2174
2175 if (addr + sizeof(val) > vdev->config_len) {
2176 return;
2177 }
2178
2179 stb_p(vdev->config + addr, val);
2180
2181 if (k->set_config) {
2182 k->set_config(vdev, vdev->config);
2183 }
2184}
2185
2186void virtio_config_modern_writew(VirtIODevice *vdev,
2187 uint32_t addr, uint32_t data)
2188{
2189 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2190 uint16_t val = data;
2191
2192 if (addr + sizeof(val) > vdev->config_len) {
2193 return;
2194 }
2195
2196 stw_le_p(vdev->config + addr, val);
2197
2198 if (k->set_config) {
2199 k->set_config(vdev, vdev->config);
2200 }
2201}
2202
2203void virtio_config_modern_writel(VirtIODevice *vdev,
2204 uint32_t addr, uint32_t data)
2205{
2206 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2207 uint32_t val = data;
2208
2209 if (addr + sizeof(val) > vdev->config_len) {
2210 return;
2211 }
2212
2213 stl_le_p(vdev->config + addr, val);
2214
2215 if (k->set_config) {
2216 k->set_config(vdev, vdev->config);
2217 }
2218}
2219
a8170e5e 2220void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
967f97fa 2221{
758ead31
PP
2222 if (!vdev->vq[n].vring.num) {
2223 return;
2224 }
ab223c95
CH
2225 vdev->vq[n].vring.desc = addr;
2226 virtio_queue_update_rings(vdev, n);
53c25cea
PB
2227}
2228
a8170e5e 2229hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
53c25cea 2230{
ab223c95
CH
2231 return vdev->vq[n].vring.desc;
2232}
2233
2234void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
2235 hwaddr avail, hwaddr used)
2236{
758ead31
PP
2237 if (!vdev->vq[n].vring.num) {
2238 return;
2239 }
ab223c95
CH
2240 vdev->vq[n].vring.desc = desc;
2241 vdev->vq[n].vring.avail = avail;
2242 vdev->vq[n].vring.used = used;
c611c764 2243 virtio_init_region_cache(vdev, n);
53c25cea
PB
2244}
2245
e63c0ba1
PM
2246void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
2247{
f6049f44
PM
2248 /* Don't allow guest to flip queue between existent and
2249 * nonexistent states, or to set it to an invalid size.
2250 */
2251 if (!!num != !!vdev->vq[n].vring.num ||
2252 num > VIRTQUEUE_MAX_SIZE ||
2253 num < 0) {
2254 return;
e63c0ba1 2255 }
f6049f44 2256 vdev->vq[n].vring.num = num;
e63c0ba1
PM
2257}
2258
e0d686bf
JW
2259VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
2260{
2261 return QLIST_FIRST(&vdev->vector_queues[vector]);
2262}
2263
2264VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
2265{
2266 return QLIST_NEXT(vq, node);
2267}
2268
53c25cea
PB
2269int virtio_queue_get_num(VirtIODevice *vdev, int n)
2270{
2271 return vdev->vq[n].vring.num;
2272}
967f97fa 2273
8c797e75
MT
2274int virtio_queue_get_max_num(VirtIODevice *vdev, int n)
2275{
2276 return vdev->vq[n].vring.num_default;
2277}
2278
8ad176aa
JW
2279int virtio_get_num_queues(VirtIODevice *vdev)
2280{
2281 int i;
2282
87b3bd1c 2283 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
8ad176aa
JW
2284 if (!virtio_queue_get_num(vdev, i)) {
2285 break;
2286 }
2287 }
2288
2289 return i;
2290}
2291
6ce69d1c
PM
2292void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
2293{
2294 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2295 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2296
ab223c95 2297 /* virtio-1 compliant devices cannot change the alignment */
95129d6f 2298 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
ab223c95
CH
2299 error_report("tried to modify queue alignment for virtio-1 device");
2300 return;
2301 }
6ce69d1c
PM
2302 /* Check that the transport told us it was going to do this
2303 * (so a buggy transport will immediately assert rather than
2304 * silently failing to migrate this state)
2305 */
2306 assert(k->has_variable_vring_alignment);
2307
758ead31
PP
2308 if (align) {
2309 vdev->vq[n].vring.align = align;
2310 virtio_queue_update_rings(vdev, n);
2311 }
6ce69d1c
PM
2312}
2313
07931698 2314static bool virtio_queue_notify_aio_vq(VirtQueue *vq)
344dc16f 2315{
badaf79c
XY
2316 bool ret = false;
2317
344dc16f
MT
2318 if (vq->vring.desc && vq->handle_aio_output) {
2319 VirtIODevice *vdev = vq->vdev;
2320
2321 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
badaf79c
XY
2322 ret = vq->handle_aio_output(vdev, vq);
2323
2324 if (unlikely(vdev->start_on_kick)) {
e57f2c31 2325 virtio_set_started(vdev, true);
badaf79c 2326 }
344dc16f 2327 }
07931698 2328
badaf79c 2329 return ret;
344dc16f
MT
2330}
2331
2b2cbcad 2332static void virtio_queue_notify_vq(VirtQueue *vq)
25db9ebe 2333{
9e0f5b81 2334 if (vq->vring.desc && vq->handle_output) {
25db9ebe 2335 VirtIODevice *vdev = vq->vdev;
9e0f5b81 2336
f5ed3663
SH
2337 if (unlikely(vdev->broken)) {
2338 return;
2339 }
2340
25db9ebe
SH
2341 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
2342 vq->handle_output(vdev, vq);
badaf79c
XY
2343
2344 if (unlikely(vdev->start_on_kick)) {
e57f2c31 2345 virtio_set_started(vdev, true);
badaf79c 2346 }
25db9ebe
SH
2347 }
2348}
2349
53c25cea
PB
2350void virtio_queue_notify(VirtIODevice *vdev, int n)
2351{
e49a6618
PB
2352 VirtQueue *vq = &vdev->vq[n];
2353
2354 if (unlikely(!vq->vring.desc || vdev->broken)) {
2355 return;
2356 }
2357
2358 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
fcccb271 2359 if (vq->host_notifier_enabled) {
e49a6618
PB
2360 event_notifier_set(&vq->host_notifier);
2361 } else if (vq->handle_output) {
2362 vq->handle_output(vdev, vq);
badaf79c 2363
8b04e2c7
XY
2364 if (unlikely(vdev->start_on_kick)) {
2365 virtio_set_started(vdev, true);
2366 }
badaf79c 2367 }
967f97fa
AL
2368}
2369
7055e687
MT
2370uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
2371{
87b3bd1c 2372 return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
7055e687
MT
2373 VIRTIO_NO_VECTOR;
2374}
2375
2376void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
2377{
e0d686bf
JW
2378 VirtQueue *vq = &vdev->vq[n];
2379
87b3bd1c 2380 if (n < VIRTIO_QUEUE_MAX) {
e0d686bf
JW
2381 if (vdev->vector_queues &&
2382 vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
2383 QLIST_REMOVE(vq, node);
2384 }
7055e687 2385 vdev->vq[n].vector = vector;
e0d686bf
JW
2386 if (vdev->vector_queues &&
2387 vector != VIRTIO_NO_VECTOR) {
2388 QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
2389 }
2390 }
7055e687
MT
2391}
2392
f1ac6a55
PB
2393VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
2394 VirtIOHandleOutput handle_output)
967f97fa
AL
2395{
2396 int i;
2397
87b3bd1c 2398 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2399 if (vdev->vq[i].vring.num == 0)
2400 break;
2401 }
2402
87b3bd1c 2403 if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
967f97fa
AL
2404 abort();
2405
2406 vdev->vq[i].vring.num = queue_size;
46c5d082 2407 vdev->vq[i].vring.num_default = queue_size;
6ce69d1c 2408 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
967f97fa 2409 vdev->vq[i].handle_output = handle_output;
344dc16f 2410 vdev->vq[i].handle_aio_output = NULL;
86044b24
JW
2411 vdev->vq[i].used_elems = g_malloc0(sizeof(VirtQueueElement) *
2412 queue_size);
967f97fa
AL
2413
2414 return &vdev->vq[i];
2415}
2416
722f8c51
MT
2417void virtio_delete_queue(VirtQueue *vq)
2418{
2419 vq->vring.num = 0;
2420 vq->vring.num_default = 0;
2421 vq->handle_output = NULL;
2422 vq->handle_aio_output = NULL;
2423 g_free(vq->used_elems);
8cd353ea 2424 vq->used_elems = NULL;
421afd2f 2425 virtio_virtqueue_reset_region_cache(vq);
722f8c51
MT
2426}
2427
f23fd811
JW
2428void virtio_del_queue(VirtIODevice *vdev, int n)
2429{
87b3bd1c 2430 if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
f23fd811
JW
2431 abort();
2432 }
2433
722f8c51 2434 virtio_delete_queue(&vdev->vq[n]);
f23fd811
JW
2435}
2436
0687c37c
PB
2437static void virtio_set_isr(VirtIODevice *vdev, int value)
2438{
d73415a3 2439 uint8_t old = qatomic_read(&vdev->isr);
0687c37c
PB
2440
2441 /* Do not write ISR if it does not change, so that its cacheline remains
2442 * shared in the common case where the guest does not read it.
2443 */
2444 if ((old & value) != value) {
d73415a3 2445 qatomic_or(&vdev->isr, value);
0687c37c
PB
2446 }
2447}
2448
4c6dd9a0 2449/* Called within rcu_read_lock(). */
683f7665 2450static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq)
bcbabae8
MT
2451{
2452 uint16_t old, new;
2453 bool v;
a281ebc1
MT
2454 /* We need to expose used array entries before checking used event. */
2455 smp_mb();
97b83deb 2456 /* Always notify when queue is empty (when feature acknowledge) */
95129d6f 2457 if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
be1fea9b 2458 !vq->inuse && virtio_queue_empty(vq)) {
bcbabae8
MT
2459 return true;
2460 }
2461
95129d6f 2462 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
bcbabae8
MT
2463 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
2464 }
2465
2466 v = vq->signalled_used_valid;
2467 vq->signalled_used_valid = true;
2468 old = vq->signalled_used;
b796fcd1 2469 new = vq->signalled_used = vq->used_idx;
e9600c6c 2470 return !v || vring_need_event(vring_get_used_event(vq), new, old);
bcbabae8
MT
2471}
2472
683f7665
JW
2473static bool vring_packed_need_event(VirtQueue *vq, bool wrap,
2474 uint16_t off_wrap, uint16_t new,
2475 uint16_t old)
2476{
2477 int off = off_wrap & ~(1 << 15);
2478
2479 if (wrap != off_wrap >> 15) {
2480 off -= vq->vring.num;
2481 }
2482
2483 return vring_need_event(off, new, old);
2484}
2485
4c6dd9a0 2486/* Called within rcu_read_lock(). */
683f7665
JW
2487static bool virtio_packed_should_notify(VirtIODevice *vdev, VirtQueue *vq)
2488{
2489 VRingPackedDescEvent e;
2490 uint16_t old, new;
2491 bool v;
2492 VRingMemoryRegionCaches *caches;
2493
2494 caches = vring_get_region_caches(vq);
abdd16f4
SH
2495 if (!caches) {
2496 return false;
2497 }
2498
683f7665
JW
2499 vring_packed_event_read(vdev, &caches->avail, &e);
2500
2501 old = vq->signalled_used;
2502 new = vq->signalled_used = vq->used_idx;
2503 v = vq->signalled_used_valid;
2504 vq->signalled_used_valid = true;
2505
2506 if (e.flags == VRING_PACKED_EVENT_FLAG_DISABLE) {
2507 return false;
2508 } else if (e.flags == VRING_PACKED_EVENT_FLAG_ENABLE) {
2509 return true;
2510 }
2511
2512 return !v || vring_packed_need_event(vq, vq->used_wrap_counter,
2513 e.off_wrap, new, old);
2514}
2515
2516/* Called within rcu_read_lock(). */
2517static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
2518{
2519 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
2520 return virtio_packed_should_notify(vdev, vq);
2521 } else {
2522 return virtio_split_should_notify(vdev, vq);
2523 }
2524}
2525
83d768b5
PB
2526void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
2527{
b5f53d04
DDAG
2528 WITH_RCU_READ_LOCK_GUARD() {
2529 if (!virtio_should_notify(vdev, vq)) {
2530 return;
2531 }
83d768b5
PB
2532 }
2533
2534 trace_virtio_notify_irqfd(vdev, vq);
2535
2536 /*
2537 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
2538 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
2539 * incorrectly polling this bit during crashdump and hibernation
2540 * in MSI mode, causing a hang if this bit is never updated.
2541 * Recent releases of Windows do not really shut down, but rather
2542 * log out and hibernate to make the next startup faster. Hence,
2543 * this manifested as a more serious hang during shutdown with
2544 *
2545 * Next driver release from 2016 fixed this problem, so working around it
2546 * is not a must, but it's easy to do so let's do it here.
2547 *
2548 * Note: it's safe to update ISR from any thread as it was switched
2549 * to an atomic operation.
2550 */
2551 virtio_set_isr(vq->vdev, 0x1);
2552 event_notifier_set(&vq->guest_notifier);
2553}
2554
b4b9862b
MT
2555static void virtio_irq(VirtQueue *vq)
2556{
2557 virtio_set_isr(vq->vdev, 0x1);
2558 virtio_notify_vector(vq->vdev, vq->vector);
2559}
2560
bcbabae8
MT
2561void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
2562{
b5f53d04
DDAG
2563 WITH_RCU_READ_LOCK_GUARD() {
2564 if (!virtio_should_notify(vdev, vq)) {
2565 return;
2566 }
bcbabae8 2567 }
967f97fa 2568
64979a4d 2569 trace_virtio_notify(vdev, vq);
b4b9862b 2570 virtio_irq(vq);
967f97fa
AL
2571}
2572
2573void virtio_notify_config(VirtIODevice *vdev)
2574{
7625162c
AL
2575 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
2576 return;
2577
0687c37c 2578 virtio_set_isr(vdev, 0x3);
b8f05908 2579 vdev->generation++;
7055e687 2580 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa
AL
2581}
2582
616a6552
GK
2583static bool virtio_device_endian_needed(void *opaque)
2584{
2585 VirtIODevice *vdev = opaque;
2586
2587 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
95129d6f 2588 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3c185597
CH
2589 return vdev->device_endian != virtio_default_endian();
2590 }
2591 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
2592 return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
616a6552
GK
2593}
2594
019a3edb
GH
2595static bool virtio_64bit_features_needed(void *opaque)
2596{
2597 VirtIODevice *vdev = opaque;
2598
2599 return (vdev->host_features >> 32) != 0;
2600}
2601
74aae7b2
JW
2602static bool virtio_virtqueue_needed(void *opaque)
2603{
2604 VirtIODevice *vdev = opaque;
2605
2606 return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
2607}
2608
86044b24
JW
2609static bool virtio_packed_virtqueue_needed(void *opaque)
2610{
2611 VirtIODevice *vdev = opaque;
2612
2613 return virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED);
2614}
2615
46c5d082
CH
2616static bool virtio_ringsize_needed(void *opaque)
2617{
2618 VirtIODevice *vdev = opaque;
2619 int i;
2620
2621 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2622 if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
2623 return true;
2624 }
2625 }
2626 return false;
2627}
2628
a6df8adf
JW
2629static bool virtio_extra_state_needed(void *opaque)
2630{
2631 VirtIODevice *vdev = opaque;
2632 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2633 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2634
2635 return k->has_extra_state &&
2636 k->has_extra_state(qbus->parent);
2637}
2638
791b1daf
SH
2639static bool virtio_broken_needed(void *opaque)
2640{
2641 VirtIODevice *vdev = opaque;
2642
2643 return vdev->broken;
2644}
2645
badaf79c
XY
2646static bool virtio_started_needed(void *opaque)
2647{
2648 VirtIODevice *vdev = opaque;
2649
2650 return vdev->started;
2651}
2652
9d7bd082
MR
2653static bool virtio_disabled_needed(void *opaque)
2654{
2655 VirtIODevice *vdev = opaque;
2656
2657 return vdev->disabled;
2658}
2659
50e5ae4d 2660static const VMStateDescription vmstate_virtqueue = {
74aae7b2 2661 .name = "virtqueue_state",
50e5ae4d
DDAG
2662 .version_id = 1,
2663 .minimum_version_id = 1,
2664 .fields = (VMStateField[]) {
2665 VMSTATE_UINT64(vring.avail, struct VirtQueue),
2666 VMSTATE_UINT64(vring.used, struct VirtQueue),
2667 VMSTATE_END_OF_LIST()
2668 }
74aae7b2
JW
2669};
2670
86044b24
JW
2671static const VMStateDescription vmstate_packed_virtqueue = {
2672 .name = "packed_virtqueue_state",
2673 .version_id = 1,
2674 .minimum_version_id = 1,
2675 .fields = (VMStateField[]) {
2676 VMSTATE_UINT16(last_avail_idx, struct VirtQueue),
2677 VMSTATE_BOOL(last_avail_wrap_counter, struct VirtQueue),
2678 VMSTATE_UINT16(used_idx, struct VirtQueue),
2679 VMSTATE_BOOL(used_wrap_counter, struct VirtQueue),
2680 VMSTATE_UINT32(inuse, struct VirtQueue),
2681 VMSTATE_END_OF_LIST()
2682 }
2683};
2684
74aae7b2
JW
2685static const VMStateDescription vmstate_virtio_virtqueues = {
2686 .name = "virtio/virtqueues",
2687 .version_id = 1,
2688 .minimum_version_id = 1,
2689 .needed = &virtio_virtqueue_needed,
2690 .fields = (VMStateField[]) {
3e996cc5
DDAG
2691 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
2692 VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
74aae7b2
JW
2693 VMSTATE_END_OF_LIST()
2694 }
2695};
2696
86044b24
JW
2697static const VMStateDescription vmstate_virtio_packed_virtqueues = {
2698 .name = "virtio/packed_virtqueues",
2699 .version_id = 1,
2700 .minimum_version_id = 1,
2701 .needed = &virtio_packed_virtqueue_needed,
2702 .fields = (VMStateField[]) {
2703 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
2704 VIRTIO_QUEUE_MAX, 0, vmstate_packed_virtqueue, VirtQueue),
2705 VMSTATE_END_OF_LIST()
2706 }
2707};
2708
50e5ae4d 2709static const VMStateDescription vmstate_ringsize = {
46c5d082 2710 .name = "ringsize_state",
50e5ae4d
DDAG
2711 .version_id = 1,
2712 .minimum_version_id = 1,
2713 .fields = (VMStateField[]) {
2714 VMSTATE_UINT32(vring.num_default, struct VirtQueue),
2715 VMSTATE_END_OF_LIST()
2716 }
46c5d082
CH
2717};
2718
2719static const VMStateDescription vmstate_virtio_ringsize = {
2720 .name = "virtio/ringsize",
2721 .version_id = 1,
2722 .minimum_version_id = 1,
2723 .needed = &virtio_ringsize_needed,
2724 .fields = (VMStateField[]) {
3e996cc5
DDAG
2725 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
2726 VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
46c5d082
CH
2727 VMSTATE_END_OF_LIST()
2728 }
2729};
2730
2c21ee76 2731static int get_extra_state(QEMUFile *f, void *pv, size_t size,
03fee66f 2732 const VMStateField *field)
a6df8adf
JW
2733{
2734 VirtIODevice *vdev = pv;
2735 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2736 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2737
2738 if (!k->load_extra_state) {
2739 return -1;
2740 } else {
2741 return k->load_extra_state(qbus->parent, f);
2742 }
2743}
2744
2c21ee76 2745static int put_extra_state(QEMUFile *f, void *pv, size_t size,
3ddba9a9 2746 const VMStateField *field, JSONWriter *vmdesc)
a6df8adf
JW
2747{
2748 VirtIODevice *vdev = pv;
2749 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2750 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2751
2752 k->save_extra_state(qbus->parent, f);
2c21ee76 2753 return 0;
a6df8adf
JW
2754}
2755
2756static const VMStateInfo vmstate_info_extra_state = {
2757 .name = "virtqueue_extra_state",
2758 .get = get_extra_state,
2759 .put = put_extra_state,
2760};
2761
2762static const VMStateDescription vmstate_virtio_extra_state = {
2763 .name = "virtio/extra_state",
2764 .version_id = 1,
2765 .minimum_version_id = 1,
2766 .needed = &virtio_extra_state_needed,
2767 .fields = (VMStateField[]) {
2768 {
2769 .name = "extra_state",
2770 .version_id = 0,
2771 .field_exists = NULL,
2772 .size = 0,
2773 .info = &vmstate_info_extra_state,
2774 .flags = VMS_SINGLE,
2775 .offset = 0,
2776 },
2777 VMSTATE_END_OF_LIST()
2778 }
2779};
2780
616a6552
GK
2781static const VMStateDescription vmstate_virtio_device_endian = {
2782 .name = "virtio/device_endian",
2783 .version_id = 1,
2784 .minimum_version_id = 1,
5cd8cada 2785 .needed = &virtio_device_endian_needed,
616a6552
GK
2786 .fields = (VMStateField[]) {
2787 VMSTATE_UINT8(device_endian, VirtIODevice),
2788 VMSTATE_END_OF_LIST()
2789 }
2790};
2791
019a3edb
GH
2792static const VMStateDescription vmstate_virtio_64bit_features = {
2793 .name = "virtio/64bit_features",
2794 .version_id = 1,
2795 .minimum_version_id = 1,
5cd8cada 2796 .needed = &virtio_64bit_features_needed,
019a3edb
GH
2797 .fields = (VMStateField[]) {
2798 VMSTATE_UINT64(guest_features, VirtIODevice),
2799 VMSTATE_END_OF_LIST()
2800 }
2801};
2802
791b1daf
SH
2803static const VMStateDescription vmstate_virtio_broken = {
2804 .name = "virtio/broken",
2805 .version_id = 1,
2806 .minimum_version_id = 1,
2807 .needed = &virtio_broken_needed,
2808 .fields = (VMStateField[]) {
2809 VMSTATE_BOOL(broken, VirtIODevice),
2810 VMSTATE_END_OF_LIST()
2811 }
2812};
2813
badaf79c
XY
2814static const VMStateDescription vmstate_virtio_started = {
2815 .name = "virtio/started",
2816 .version_id = 1,
2817 .minimum_version_id = 1,
2818 .needed = &virtio_started_needed,
2819 .fields = (VMStateField[]) {
2820 VMSTATE_BOOL(started, VirtIODevice),
2821 VMSTATE_END_OF_LIST()
2822 }
2823};
2824
9d7bd082
MR
2825static const VMStateDescription vmstate_virtio_disabled = {
2826 .name = "virtio/disabled",
2827 .version_id = 1,
2828 .minimum_version_id = 1,
2829 .needed = &virtio_disabled_needed,
2830 .fields = (VMStateField[]) {
2831 VMSTATE_BOOL(disabled, VirtIODevice),
2832 VMSTATE_END_OF_LIST()
2833 }
2834};
2835
6b321a3d
GK
2836static const VMStateDescription vmstate_virtio = {
2837 .name = "virtio",
2838 .version_id = 1,
2839 .minimum_version_id = 1,
2840 .minimum_version_id_old = 1,
2841 .fields = (VMStateField[]) {
2842 VMSTATE_END_OF_LIST()
616a6552 2843 },
5cd8cada
JQ
2844 .subsections = (const VMStateDescription*[]) {
2845 &vmstate_virtio_device_endian,
2846 &vmstate_virtio_64bit_features,
74aae7b2 2847 &vmstate_virtio_virtqueues,
46c5d082 2848 &vmstate_virtio_ringsize,
791b1daf 2849 &vmstate_virtio_broken,
a6df8adf 2850 &vmstate_virtio_extra_state,
badaf79c 2851 &vmstate_virtio_started,
86044b24 2852 &vmstate_virtio_packed_virtqueues,
9d7bd082 2853 &vmstate_virtio_disabled,
5cd8cada 2854 NULL
6b321a3d
GK
2855 }
2856};
2857
2f168d07 2858int virtio_save(VirtIODevice *vdev, QEMUFile *f)
967f97fa 2859{
1c819449
FK
2860 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2861 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 2862 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
019a3edb 2863 uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
967f97fa
AL
2864 int i;
2865
1c819449
FK
2866 if (k->save_config) {
2867 k->save_config(qbus->parent, f);
2868 }
967f97fa 2869
967f97fa
AL
2870 qemu_put_8s(f, &vdev->status);
2871 qemu_put_8s(f, &vdev->isr);
2872 qemu_put_be16s(f, &vdev->queue_sel);
019a3edb 2873 qemu_put_be32s(f, &guest_features_lo);
967f97fa
AL
2874 qemu_put_be32(f, vdev->config_len);
2875 qemu_put_buffer(f, vdev->config, vdev->config_len);
2876
87b3bd1c 2877 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2878 if (vdev->vq[i].vring.num == 0)
2879 break;
2880 }
2881
2882 qemu_put_be32(f, i);
2883
87b3bd1c 2884 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2885 if (vdev->vq[i].vring.num == 0)
2886 break;
2887
2888 qemu_put_be32(f, vdev->vq[i].vring.num);
6ce69d1c
PM
2889 if (k->has_variable_vring_alignment) {
2890 qemu_put_be32(f, vdev->vq[i].vring.align);
2891 }
874adf45
SH
2892 /*
2893 * Save desc now, the rest of the ring addresses are saved in
2894 * subsections for VIRTIO-1 devices.
2895 */
ab223c95 2896 qemu_put_be64(f, vdev->vq[i].vring.desc);
967f97fa 2897 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1c819449
FK
2898 if (k->save_queue) {
2899 k->save_queue(qbus->parent, i, f);
2900 }
967f97fa 2901 }
1b5fc0de
GK
2902
2903 if (vdc->save != NULL) {
2904 vdc->save(vdev, f);
2905 }
6b321a3d 2906
ea43e259 2907 if (vdc->vmsd) {
2f168d07
DDAG
2908 int ret = vmstate_save_state(f, vdc->vmsd, vdev, NULL);
2909 if (ret) {
2910 return ret;
2911 }
ea43e259
DDAG
2912 }
2913
6b321a3d 2914 /* Subsections */
2f168d07 2915 return vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
967f97fa
AL
2916}
2917
1a665855 2918/* A wrapper for use as a VMState .put function */
2c21ee76 2919static int virtio_device_put(QEMUFile *f, void *opaque, size_t size,
3ddba9a9 2920 const VMStateField *field, JSONWriter *vmdesc)
1a665855 2921{
2f168d07 2922 return virtio_save(VIRTIO_DEVICE(opaque), f);
1a665855
HP
2923}
2924
2925/* A wrapper for use as a VMState .get function */
2c21ee76 2926static int virtio_device_get(QEMUFile *f, void *opaque, size_t size,
03fee66f 2927 const VMStateField *field)
1a665855
HP
2928{
2929 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
2930 DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
2931
2932 return virtio_load(vdev, f, dc->vmsd->version_id);
2933}
2934
2935const VMStateInfo virtio_vmstate_info = {
2936 .name = "virtio",
2937 .get = virtio_device_get,
2938 .put = virtio_device_put,
2939};
2940
6c0196d7 2941static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
ad0c9332 2942{
181103cd 2943 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
6b8f1020 2944 bool bad = (val & ~(vdev->host_features)) != 0;
ad0c9332 2945
6b8f1020 2946 val &= vdev->host_features;
181103cd
FK
2947 if (k->set_features) {
2948 k->set_features(vdev, val);
ad0c9332
PB
2949 }
2950 vdev->guest_features = val;
2951 return bad ? -1 : 0;
2952}
2953
6c0196d7
CH
2954int virtio_set_features(VirtIODevice *vdev, uint64_t val)
2955{
db812c40
PB
2956 int ret;
2957 /*
6c0196d7
CH
2958 * The driver must not attempt to set features after feature negotiation
2959 * has finished.
2960 */
2961 if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
2962 return -EINVAL;
2963 }
db812c40 2964 ret = virtio_set_features_nocheck(vdev, val);
2d69eba5
LQ
2965 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
2966 /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
2967 int i;
2968 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2969 if (vdev->vq[i].vring.num != 0) {
2970 virtio_init_region_cache(vdev, i);
db812c40
PB
2971 }
2972 }
2d69eba5
LQ
2973 }
2974 if (!ret) {
868a8f44
XY
2975 if (!virtio_device_started(vdev, vdev->status) &&
2976 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2977 vdev->start_on_kick = true;
2978 }
db812c40
PB
2979 }
2980 return ret;
6c0196d7
CH
2981}
2982
4c21e353 2983size_t virtio_feature_get_config_size(const VirtIOFeature *feature_sizes,
ba550851
SG
2984 uint64_t host_features)
2985{
2986 size_t config_size = 0;
2987 int i;
2988
2989 for (i = 0; feature_sizes[i].flags != 0; i++) {
2990 if (host_features & feature_sizes[i].flags) {
2991 config_size = MAX(feature_sizes[i].end, config_size);
2992 }
2993 }
2994
2995 return config_size;
2996}
2997
1b5fc0de 2998int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
967f97fa 2999{
cc459952 3000 int i, ret;
a890a2f9 3001 int32_t config_len;
cc459952 3002 uint32_t num;
6d74ca5a 3003 uint32_t features;
1c819449
FK
3004 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3005 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 3006 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa 3007
616a6552
GK
3008 /*
3009 * We poison the endianness to ensure it does not get used before
3010 * subsections have been loaded.
3011 */
3012 vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
3013
1c819449
FK
3014 if (k->load_config) {
3015 ret = k->load_config(qbus->parent, f);
ff24bd58
MT
3016 if (ret)
3017 return ret;
3018 }
967f97fa 3019
967f97fa
AL
3020 qemu_get_8s(f, &vdev->status);
3021 qemu_get_8s(f, &vdev->isr);
3022 qemu_get_be16s(f, &vdev->queue_sel);
87b3bd1c 3023 if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
4b53c2c7
MR
3024 return -1;
3025 }
6d74ca5a 3026 qemu_get_be32s(f, &features);
ad0c9332 3027
62cee1a2
MT
3028 /*
3029 * Temporarily set guest_features low bits - needed by
3030 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
3031 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
3032 *
3033 * Note: devices should always test host features in future - don't create
3034 * new dependencies like this.
3035 */
3036 vdev->guest_features = features;
3037
a890a2f9 3038 config_len = qemu_get_be32(f);
2f5732e9
DDAG
3039
3040 /*
3041 * There are cases where the incoming config can be bigger or smaller
3042 * than what we have; so load what we have space for, and skip
3043 * any excess that's in the stream.
3044 */
3045 qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
3046
3047 while (config_len > vdev->config_len) {
3048 qemu_get_byte(f);
3049 config_len--;
a890a2f9 3050 }
967f97fa
AL
3051
3052 num = qemu_get_be32(f);
3053
87b3bd1c 3054 if (num > VIRTIO_QUEUE_MAX) {
8a1be662 3055 error_report("Invalid number of virtqueues: 0x%x", num);
cc459952
MT
3056 return -1;
3057 }
3058
967f97fa
AL
3059 for (i = 0; i < num; i++) {
3060 vdev->vq[i].vring.num = qemu_get_be32(f);
6ce69d1c
PM
3061 if (k->has_variable_vring_alignment) {
3062 vdev->vq[i].vring.align = qemu_get_be32(f);
3063 }
ab223c95 3064 vdev->vq[i].vring.desc = qemu_get_be64(f);
967f97fa 3065 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
bcbabae8 3066 vdev->vq[i].signalled_used_valid = false;
332fa82d 3067 vdev->vq[i].notification = true;
967f97fa 3068
874adf45 3069 if (!vdev->vq[i].vring.desc && vdev->vq[i].last_avail_idx) {
1abeb5a6 3070 error_report("VQ %d address 0x0 "
6daf194d 3071 "inconsistent with Host index 0x%x",
1abeb5a6 3072 i, vdev->vq[i].last_avail_idx);
874adf45 3073 return -1;
8275e2f6 3074 }
1c819449
FK
3075 if (k->load_queue) {
3076 ret = k->load_queue(qbus->parent, i, f);
ff24bd58
MT
3077 if (ret)
3078 return ret;
7055e687 3079 }
967f97fa
AL
3080 }
3081
7055e687 3082 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1b5fc0de
GK
3083
3084 if (vdc->load != NULL) {
6b321a3d
GK
3085 ret = vdc->load(vdev, f, version_id);
3086 if (ret) {
3087 return ret;
3088 }
1b5fc0de
GK
3089 }
3090
ea43e259
DDAG
3091 if (vdc->vmsd) {
3092 ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id);
3093 if (ret) {
3094 return ret;
3095 }
3096 }
3097
616a6552
GK
3098 /* Subsections */
3099 ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
3100 if (ret) {
3101 return ret;
3102 }
3103
3104 if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
3105 vdev->device_endian = virtio_default_endian();
3106 }
3107
019a3edb
GH
3108 if (virtio_64bit_features_needed(vdev)) {
3109 /*
3110 * Subsection load filled vdev->guest_features. Run them
3111 * through virtio_set_features to sanity-check them against
3112 * host_features.
3113 */
3114 uint64_t features64 = vdev->guest_features;
6c0196d7 3115 if (virtio_set_features_nocheck(vdev, features64) < 0) {
019a3edb
GH
3116 error_report("Features 0x%" PRIx64 " unsupported. "
3117 "Allowed features: 0x%" PRIx64,
3118 features64, vdev->host_features);
3119 return -1;
3120 }
3121 } else {
6c0196d7 3122 if (virtio_set_features_nocheck(vdev, features) < 0) {
019a3edb
GH
3123 error_report("Features 0x%x unsupported. "
3124 "Allowed features: 0x%" PRIx64,
3125 features, vdev->host_features);
3126 return -1;
3127 }
3128 }
3129
868a8f44
XY
3130 if (!virtio_device_started(vdev, vdev->status) &&
3131 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3132 vdev->start_on_kick = true;
3133 }
3134
b5f53d04 3135 RCU_READ_LOCK_GUARD();
616a6552 3136 for (i = 0; i < num; i++) {
ab223c95 3137 if (vdev->vq[i].vring.desc) {
616a6552 3138 uint16_t nheads;
874adf45
SH
3139
3140 /*
3141 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
3142 * only the region cache needs to be set up. Legacy devices need
3143 * to calculate used and avail ring addresses based on the desc
3144 * address.
3145 */
3146 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3147 virtio_init_region_cache(vdev, i);
3148 } else {
3149 virtio_queue_update_rings(vdev, i);
3150 }
3151
86044b24
JW
3152 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3153 vdev->vq[i].shadow_avail_idx = vdev->vq[i].last_avail_idx;
3154 vdev->vq[i].shadow_avail_wrap_counter =
3155 vdev->vq[i].last_avail_wrap_counter;
3156 continue;
3157 }
3158
616a6552
GK
3159 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
3160 /* Check it isn't doing strange things with descriptor numbers. */
3161 if (nheads > vdev->vq[i].vring.num) {
4aedda25
JL
3162 virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "
3163 "inconsistent with Host index 0x%x: delta 0x%x",
3164 i, vdev->vq[i].vring.num,
3165 vring_avail_idx(&vdev->vq[i]),
3166 vdev->vq[i].last_avail_idx, nheads);
3167 vdev->vq[i].used_idx = 0;
3168 vdev->vq[i].shadow_avail_idx = 0;
3169 vdev->vq[i].inuse = 0;
3170 continue;
616a6552 3171 }
b796fcd1 3172 vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
be1fea9b 3173 vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
bccdef6b
SH
3174
3175 /*
3176 * Some devices migrate VirtQueueElements that have been popped
3177 * from the avail ring but not yet returned to the used ring.
e66bcc40
HP
3178 * Since max ring size < UINT16_MAX it's safe to use modulo
3179 * UINT16_MAX + 1 subtraction.
bccdef6b 3180 */
e66bcc40
HP
3181 vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
3182 vdev->vq[i].used_idx);
bccdef6b
SH
3183 if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
3184 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
3185 "used_idx 0x%x",
3186 i, vdev->vq[i].vring.num,
3187 vdev->vq[i].last_avail_idx,
3188 vdev->vq[i].used_idx);
3189 return -1;
3190 }
616a6552
GK
3191 }
3192 }
3193
1dd71383
MT
3194 if (vdc->post_load) {
3195 ret = vdc->post_load(vdev);
3196 if (ret) {
3197 return ret;
3198 }
3199 }
3200
616a6552 3201 return 0;
967f97fa
AL
3202}
3203
6a1a8cc7 3204void virtio_cleanup(VirtIODevice *vdev)
b946a153 3205{
85cf2a8d 3206 qemu_del_vm_change_state_handler(vdev->vmstate);
8e05db92
FK
3207}
3208
538f0497 3209static void virtio_vmstate_change(void *opaque, bool running, RunState state)
85cf2a8d
MT
3210{
3211 VirtIODevice *vdev = opaque;
1c819449
FK
3212 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3213 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
e57f2c31 3214 bool backend_run = running && virtio_device_started(vdev, vdev->status);
9e8e8c48 3215 vdev->vm_running = running;
85cf2a8d
MT
3216
3217 if (backend_run) {
3218 virtio_set_status(vdev, vdev->status);
3219 }
3220
1c819449
FK
3221 if (k->vmstate_change) {
3222 k->vmstate_change(qbus->parent, backend_run);
85cf2a8d
MT
3223 }
3224
3225 if (!backend_run) {
3226 virtio_set_status(vdev, vdev->status);
3227 }
3228}
3229
c8075caf
GA
3230void virtio_instance_init_common(Object *proxy_obj, void *data,
3231 size_t vdev_size, const char *vdev_name)
3232{
3233 DeviceState *vdev = data;
3234
9fc7fc4d
MA
3235 object_initialize_child_with_props(proxy_obj, "virtio-backend", vdev,
3236 vdev_size, vdev_name, &error_abort,
3237 NULL);
c8075caf
GA
3238 qdev_alias_all_properties(vdev, proxy_obj);
3239}
3240
8e05db92
FK
3241void virtio_init(VirtIODevice *vdev, const char *name,
3242 uint16_t device_id, size_t config_size)
967f97fa 3243{
e0d686bf
JW
3244 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3245 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
b8193adb 3246 int i;
e0d686bf
JW
3247 int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
3248
3249 if (nvectors) {
3250 vdev->vector_queues =
3251 g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
3252 }
3253
868a8f44 3254 vdev->start_on_kick = false;
badaf79c 3255 vdev->started = false;
53c25cea 3256 vdev->device_id = device_id;
967f97fa 3257 vdev->status = 0;
d73415a3 3258 qatomic_set(&vdev->isr, 0);
967f97fa 3259 vdev->queue_sel = 0;
7055e687 3260 vdev->config_vector = VIRTIO_NO_VECTOR;
87b3bd1c 3261 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
1354869c 3262 vdev->vm_running = runstate_is_running();
f5ed3663 3263 vdev->broken = false;
87b3bd1c 3264 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
b8193adb 3265 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1cbdabe2 3266 vdev->vq[i].vdev = vdev;
e78a2b42 3267 vdev->vq[i].queue_index = i;
fcccb271 3268 vdev->vq[i].host_notifier_enabled = false;
1cbdabe2 3269 }
967f97fa 3270
967f97fa
AL
3271 vdev->name = name;
3272 vdev->config_len = config_size;
8e05db92 3273 if (vdev->config_len) {
7267c094 3274 vdev->config = g_malloc0(config_size);
8e05db92 3275 } else {
967f97fa 3276 vdev->config = NULL;
8e05db92 3277 }
1a8c091c
SH
3278 vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
3279 virtio_vmstate_change, vdev);
616a6552 3280 vdev->device_endian = virtio_default_endian();
5669655a 3281 vdev->use_guest_notifier_mask = true;
8e05db92 3282}
967f97fa 3283
7c78bdd7
CH
3284/*
3285 * Only devices that have already been around prior to defining the virtio
3286 * standard support legacy mode; this includes devices not specified in the
3287 * standard. All newer devices conform to the virtio standard only.
3288 */
3289bool virtio_legacy_allowed(VirtIODevice *vdev)
3290{
3291 switch (vdev->device_id) {
3292 case VIRTIO_ID_NET:
3293 case VIRTIO_ID_BLOCK:
3294 case VIRTIO_ID_CONSOLE:
3295 case VIRTIO_ID_RNG:
3296 case VIRTIO_ID_BALLOON:
3297 case VIRTIO_ID_RPMSG:
3298 case VIRTIO_ID_SCSI:
3299 case VIRTIO_ID_9P:
3300 case VIRTIO_ID_RPROC_SERIAL:
3301 case VIRTIO_ID_CAIF:
3302 return true;
3303 default:
3304 return false;
3305 }
3306}
3307
d55f5182
SG
3308bool virtio_legacy_check_disabled(VirtIODevice *vdev)
3309{
3310 return vdev->disable_legacy_check;
3311}
3312
a8170e5e 3313hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
3314{
3315 return vdev->vq[n].vring.desc;
3316}
3317
0c9753eb
LV
3318bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n)
3319{
3320 return virtio_queue_get_desc_addr(vdev, n) != 0;
3321}
3322
23bfaf77
JW
3323bool virtio_queue_enabled(VirtIODevice *vdev, int n)
3324{
b2a5f62a
JW
3325 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3326 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
3327
3328 if (k->queue_enabled) {
3329 return k->queue_enabled(qbus->parent, n);
3330 }
0c9753eb 3331 return virtio_queue_enabled_legacy(vdev, n);
23bfaf77
JW
3332}
3333
a8170e5e 3334hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
3335{
3336 return vdev->vq[n].vring.avail;
3337}
3338
a8170e5e 3339hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
3340{
3341 return vdev->vq[n].vring.used;
3342}
3343
a8170e5e 3344hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
3345{
3346 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
3347}
3348
a8170e5e 3349hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
1cbdabe2 3350{
f90cda63
WX
3351 int s;
3352
86044b24
JW
3353 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3354 return sizeof(struct VRingPackedDescEvent);
3355 }
3356
f90cda63 3357 s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1cbdabe2 3358 return offsetof(VRingAvail, ring) +
f90cda63 3359 sizeof(uint16_t) * vdev->vq[n].vring.num + s;
1cbdabe2
MT
3360}
3361
a8170e5e 3362hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
1cbdabe2 3363{
f90cda63
WX
3364 int s;
3365
86044b24
JW
3366 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3367 return sizeof(struct VRingPackedDescEvent);
3368 }
3369
f90cda63 3370 s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1cbdabe2 3371 return offsetof(VRingUsed, ring) +
f90cda63 3372 sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s;
1cbdabe2
MT
3373}
3374
86044b24
JW
3375static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice *vdev,
3376 int n)
3377{
3378 unsigned int avail, used;
3379
3380 avail = vdev->vq[n].last_avail_idx;
3381 avail |= ((uint16_t)vdev->vq[n].last_avail_wrap_counter) << 15;
3382
3383 used = vdev->vq[n].used_idx;
3384 used |= ((uint16_t)vdev->vq[n].used_wrap_counter) << 15;
3385
3386 return avail | used << 16;
3387}
3388
3389static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice *vdev,
3390 int n)
1cbdabe2
MT
3391{
3392 return vdev->vq[n].last_avail_idx;
3393}
3394
86044b24 3395unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
1cbdabe2 3396{
86044b24
JW
3397 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3398 return virtio_queue_packed_get_last_avail_idx(vdev, n);
3399 } else {
3400 return virtio_queue_split_get_last_avail_idx(vdev, n);
3401 }
1cbdabe2
MT
3402}
3403
86044b24
JW
3404static void virtio_queue_packed_set_last_avail_idx(VirtIODevice *vdev,
3405 int n, unsigned int idx)
3406{
3407 struct VirtQueue *vq = &vdev->vq[n];
3408
3409 vq->last_avail_idx = vq->shadow_avail_idx = idx & 0x7fff;
3410 vq->last_avail_wrap_counter =
3411 vq->shadow_avail_wrap_counter = !!(idx & 0x8000);
3412 idx >>= 16;
3413 vq->used_idx = idx & 0x7ffff;
3414 vq->used_wrap_counter = !!(idx & 0x8000);
3415}
3416
3417static void virtio_queue_split_set_last_avail_idx(VirtIODevice *vdev,
3418 int n, unsigned int idx)
3419{
3420 vdev->vq[n].last_avail_idx = idx;
3421 vdev->vq[n].shadow_avail_idx = idx;
3422}
3423
3424void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
3425 unsigned int idx)
3426{
3427 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3428 virtio_queue_packed_set_last_avail_idx(vdev, n, idx);
3429 } else {
3430 virtio_queue_split_set_last_avail_idx(vdev, n, idx);
3431 }
3432}
3433
3434static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice *vdev,
3435 int n)
3436{
3437 /* We don't have a reference like avail idx in shared memory */
3438 return;
3439}
3440
3441static void virtio_queue_split_restore_last_avail_idx(VirtIODevice *vdev,
3442 int n)
2d4ba6cc 3443{
b5f53d04 3444 RCU_READ_LOCK_GUARD();
2d4ba6cc
MC
3445 if (vdev->vq[n].vring.desc) {
3446 vdev->vq[n].last_avail_idx = vring_used_idx(&vdev->vq[n]);
3447 vdev->vq[n].shadow_avail_idx = vdev->vq[n].last_avail_idx;
3448 }
2d4ba6cc
MC
3449}
3450
86044b24
JW
3451void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n)
3452{
3453 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3454 virtio_queue_packed_restore_last_avail_idx(vdev, n);
3455 } else {
3456 virtio_queue_split_restore_last_avail_idx(vdev, n);
3457 }
3458}
3459
3460static void virtio_queue_packed_update_used_idx(VirtIODevice *vdev, int n)
3461{
3462 /* used idx was updated through set_last_avail_idx() */
3463 return;
3464}
3465
3466static void virtio_split_packed_update_used_idx(VirtIODevice *vdev, int n)
312d3b35 3467{
b5f53d04 3468 RCU_READ_LOCK_GUARD();
ca0176ad
PB
3469 if (vdev->vq[n].vring.desc) {
3470 vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]);
3471 }
312d3b35
YB
3472}
3473
86044b24
JW
3474void virtio_queue_update_used_idx(VirtIODevice *vdev, int n)
3475{
3476 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3477 return virtio_queue_packed_update_used_idx(vdev, n);
3478 } else {
3479 return virtio_split_packed_update_used_idx(vdev, n);
3480 }
3481}
3482
6793dfd1
SH
3483void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
3484{
3485 vdev->vq[n].signalled_used_valid = false;
3486}
3487
1cbdabe2
MT
3488VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
3489{
3490 return vdev->vq + n;
3491}
3492
e78a2b42
JW
3493uint16_t virtio_get_queue_index(VirtQueue *vq)
3494{
3495 return vq->queue_index;
3496}
3497
15b2bd18
PB
3498static void virtio_queue_guest_notifier_read(EventNotifier *n)
3499{
3500 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
3501 if (event_notifier_test_and_clear(n)) {
b4b9862b 3502 virtio_irq(vq);
15b2bd18
PB
3503 }
3504}
3505
3506void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
3507 bool with_irqfd)
3508{
3509 if (assign && !with_irqfd) {
d6da1e9e 3510 event_notifier_set_handler(&vq->guest_notifier,
15b2bd18
PB
3511 virtio_queue_guest_notifier_read);
3512 } else {
d6da1e9e 3513 event_notifier_set_handler(&vq->guest_notifier, NULL);
15b2bd18
PB
3514 }
3515 if (!assign) {
3516 /* Test and clear notifier before closing it,
3517 * in case poll callback didn't have time to run. */
3518 virtio_queue_guest_notifier_read(&vq->guest_notifier);
3519 }
3520}
3521
1cbdabe2
MT
3522EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
3523{
3524 return &vq->guest_notifier;
3525}
b1f416aa 3526
344dc16f 3527static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
b1f416aa
PB
3528{
3529 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3530 if (event_notifier_test_and_clear(n)) {
344dc16f 3531 virtio_queue_notify_aio_vq(vq);
b1f416aa
PB
3532 }
3533}
3534
a7c8215e
SH
3535static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n)
3536{
3537 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3538
3539 virtio_queue_set_notification(vq, 0);
3540}
3541
0062ea0f
SH
3542static bool virtio_queue_host_notifier_aio_poll(void *opaque)
3543{
3544 EventNotifier *n = opaque;
3545 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3546
dd3dd4ba 3547 if (!vq->vring.desc || virtio_queue_empty(vq)) {
0062ea0f
SH
3548 return false;
3549 }
3550
d0435bc5 3551 return virtio_queue_notify_aio_vq(vq);
0062ea0f
SH
3552}
3553
a7c8215e
SH
3554static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n)
3555{
3556 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3557
3558 /* Caller polls once more after this to catch requests that race with us */
3559 virtio_queue_set_notification(vq, 1);
3560}
3561
a1afb606 3562void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
07931698 3563 VirtIOHandleAIOOutput handle_output)
a1afb606 3564{
a378b49a
PB
3565 if (handle_output) {
3566 vq->handle_aio_output = handle_output;
a1afb606 3567 aio_set_event_notifier(ctx, &vq->host_notifier, true,
0062ea0f
SH
3568 virtio_queue_host_notifier_aio_read,
3569 virtio_queue_host_notifier_aio_poll);
a7c8215e
SH
3570 aio_set_event_notifier_poll(ctx, &vq->host_notifier,
3571 virtio_queue_host_notifier_aio_poll_begin,
3572 virtio_queue_host_notifier_aio_poll_end);
a1afb606 3573 } else {
f6a51c84 3574 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL);
a1afb606
PB
3575 /* Test and clear notifier before after disabling event,
3576 * in case poll callback didn't have time to run. */
344dc16f 3577 virtio_queue_host_notifier_aio_read(&vq->host_notifier);
a378b49a 3578 vq->handle_aio_output = NULL;
344dc16f
MT
3579 }
3580}
3581
fa283a4a 3582void virtio_queue_host_notifier_read(EventNotifier *n)
344dc16f
MT
3583{
3584 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3585 if (event_notifier_test_and_clear(n)) {
3586 virtio_queue_notify_vq(vq);
a1afb606
PB
3587 }
3588}
3589
1cbdabe2
MT
3590EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
3591{
3592 return &vq->host_notifier;
3593}
8e05db92 3594
fcccb271
SH
3595void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
3596{
3597 vq->host_notifier_enabled = enabled;
3598}
3599
6f80e617
TB
3600int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
3601 MemoryRegion *mr, bool assign)
3602{
3603 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3604 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
3605
3606 if (k->set_host_notifier_mr) {
3607 return k->set_host_notifier_mr(qbus->parent, n, mr, assign);
3608 }
3609
3610 return -1;
3611}
3612
1034e9cf
FK
3613void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
3614{
9e288406 3615 g_free(vdev->bus_name);
80e0090a 3616 vdev->bus_name = g_strdup(bus_name);
1034e9cf
FK
3617}
3618
f5ed3663
SH
3619void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...)
3620{
3621 va_list ap;
3622
3623 va_start(ap, fmt);
3624 error_vreport(fmt, ap);
3625 va_end(ap);
3626
f5ed3663 3627 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
8fc47c87 3628 vdev->status = vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET;
f5ed3663
SH
3629 virtio_notify_config(vdev);
3630 }
66453cff
GK
3631
3632 vdev->broken = true;
f5ed3663
SH
3633}
3634
c611c764
PB
3635static void virtio_memory_listener_commit(MemoryListener *listener)
3636{
3637 VirtIODevice *vdev = container_of(listener, VirtIODevice, listener);
3638 int i;
3639
3640 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
3641 if (vdev->vq[i].vring.num == 0) {
3642 break;
3643 }
3644 virtio_init_region_cache(vdev, i);
3645 }
3646}
3647
1d244b42
AF
3648static void virtio_device_realize(DeviceState *dev, Error **errp)
3649{
3650 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3651 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
3652 Error *err = NULL;
3653
ea43e259
DDAG
3654 /* Devices should either use vmsd or the load/save methods */
3655 assert(!vdc->vmsd || !vdc->load);
3656
1d244b42
AF
3657 if (vdc->realize != NULL) {
3658 vdc->realize(dev, &err);
3659 if (err != NULL) {
3660 error_propagate(errp, err);
3661 return;
3662 }
8e05db92 3663 }
e8398045
JW
3664
3665 virtio_bus_device_plugged(vdev, &err);
3666 if (err != NULL) {
3667 error_propagate(errp, err);
b69c3c21 3668 vdc->unrealize(dev);
e8398045
JW
3669 return;
3670 }
c611c764
PB
3671
3672 vdev->listener.commit = virtio_memory_listener_commit;
3673 memory_listener_register(&vdev->listener, vdev->dma_as);
8e05db92
FK
3674}
3675
b69c3c21 3676static void virtio_device_unrealize(DeviceState *dev)
1034e9cf 3677{
1d244b42 3678 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
306ec6c3 3679 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1d244b42 3680
f6ab64c0 3681 memory_listener_unregister(&vdev->listener);
83d07047
PB
3682 virtio_bus_device_unplugged(vdev);
3683
306ec6c3 3684 if (vdc->unrealize != NULL) {
b69c3c21 3685 vdc->unrealize(dev);
5e96f5d2 3686 }
1d244b42 3687
9e288406
MA
3688 g_free(vdev->bus_name);
3689 vdev->bus_name = NULL;
1034e9cf
FK
3690}
3691
c611c764
PB
3692static void virtio_device_free_virtqueues(VirtIODevice *vdev)
3693{
3694 int i;
3695 if (!vdev->vq) {
3696 return;
3697 }
3698
3699 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
c611c764
PB
3700 if (vdev->vq[i].vring.num == 0) {
3701 break;
3702 }
e0e2d644 3703 virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
c611c764
PB
3704 }
3705 g_free(vdev->vq);
3706}
3707
3708static void virtio_device_instance_finalize(Object *obj)
3709{
3710 VirtIODevice *vdev = VIRTIO_DEVICE(obj);
3711
c611c764
PB
3712 virtio_device_free_virtqueues(vdev);
3713
3714 g_free(vdev->config);
3715 g_free(vdev->vector_queues);
3716}
3717
6b8f1020
CH
3718static Property virtio_properties[] = {
3719 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
e57f2c31 3720 DEFINE_PROP_BOOL("use-started", VirtIODevice, use_started, true),
9d7bd082 3721 DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice, use_disabled_flag, true),
d55f5182
SG
3722 DEFINE_PROP_BOOL("x-disable-legacy-check", VirtIODevice,
3723 disable_legacy_check, false),
6b8f1020
CH
3724 DEFINE_PROP_END_OF_LIST(),
3725};
3726
ff4c07df
PB
3727static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)
3728{
3729 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
710fccf8 3730 int i, n, r, err;
ff4c07df 3731
9cf4fd87
GK
3732 /*
3733 * Batch all the host notifiers in a single transaction to avoid
3734 * quadratic time complexity in address_space_update_ioeventfds().
3735 */
710fccf8 3736 memory_region_transaction_begin();
ff4c07df 3737 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
fa283a4a 3738 VirtQueue *vq = &vdev->vq[n];
ff4c07df
PB
3739 if (!virtio_queue_get_num(vdev, n)) {
3740 continue;
3741 }
ed08a2a0 3742 r = virtio_bus_set_host_notifier(qbus, n, true);
ff4c07df
PB
3743 if (r < 0) {
3744 err = r;
3745 goto assign_error;
3746 }
d6da1e9e 3747 event_notifier_set_handler(&vq->host_notifier,
fa283a4a 3748 virtio_queue_host_notifier_read);
6019f3b9
PB
3749 }
3750
3751 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
3752 /* Kick right away to begin processing requests already in vring */
3753 VirtQueue *vq = &vdev->vq[n];
3754 if (!vq->vring.num) {
3755 continue;
3756 }
3757 event_notifier_set(&vq->host_notifier);
ff4c07df 3758 }
710fccf8 3759 memory_region_transaction_commit();
ff4c07df
PB
3760 return 0;
3761
3762assign_error:
710fccf8 3763 i = n; /* save n for a second iteration after transaction is committed. */
ff4c07df 3764 while (--n >= 0) {
fa283a4a 3765 VirtQueue *vq = &vdev->vq[n];
ff4c07df
PB
3766 if (!virtio_queue_get_num(vdev, n)) {
3767 continue;
3768 }
3769
d6da1e9e 3770 event_notifier_set_handler(&vq->host_notifier, NULL);
ed08a2a0 3771 r = virtio_bus_set_host_notifier(qbus, n, false);
ff4c07df 3772 assert(r >= 0);
710fccf8 3773 }
9cf4fd87
GK
3774 /*
3775 * The transaction expects the ioeventfds to be open when it
3776 * commits. Do it now, before the cleanup loop.
3777 */
710fccf8
GH
3778 memory_region_transaction_commit();
3779
3780 while (--i >= 0) {
3781 if (!virtio_queue_get_num(vdev, i)) {
3782 continue;
3783 }
3784 virtio_bus_cleanup_host_notifier(qbus, i);
ff4c07df
PB
3785 }
3786 return err;
3787}
3788
3789int virtio_device_start_ioeventfd(VirtIODevice *vdev)
3790{
3791 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3792 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3793
3794 return virtio_bus_start_ioeventfd(vbus);
3795}
3796
3797static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev)
3798{
3799 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
ff4c07df
PB
3800 int n, r;
3801
9cf4fd87
GK
3802 /*
3803 * Batch all the host notifiers in a single transaction to avoid
3804 * quadratic time complexity in address_space_update_ioeventfds().
3805 */
710fccf8 3806 memory_region_transaction_begin();
ff4c07df 3807 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
fa283a4a
PB
3808 VirtQueue *vq = &vdev->vq[n];
3809
ff4c07df
PB
3810 if (!virtio_queue_get_num(vdev, n)) {
3811 continue;
3812 }
d6da1e9e 3813 event_notifier_set_handler(&vq->host_notifier, NULL);
ed08a2a0 3814 r = virtio_bus_set_host_notifier(qbus, n, false);
ff4c07df 3815 assert(r >= 0);
710fccf8 3816 }
9cf4fd87
GK
3817 /*
3818 * The transaction expects the ioeventfds to be open when it
3819 * commits. Do it now, before the cleanup loop.
3820 */
710fccf8
GH
3821 memory_region_transaction_commit();
3822
3823 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
3824 if (!virtio_queue_get_num(vdev, n)) {
3825 continue;
3826 }
76143618 3827 virtio_bus_cleanup_host_notifier(qbus, n);
ff4c07df
PB
3828 }
3829}
3830
310837de
PB
3831int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
3832{
3833 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3834 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3835
3836 return virtio_bus_grab_ioeventfd(vbus);
3837}
3838
3839void virtio_device_release_ioeventfd(VirtIODevice *vdev)
3840{
3841 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3842 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3843
3844 virtio_bus_release_ioeventfd(vbus);
3845}
3846
8e05db92
FK
3847static void virtio_device_class_init(ObjectClass *klass, void *data)
3848{
3849 /* Set the default value here. */
ff4c07df 3850 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
8e05db92 3851 DeviceClass *dc = DEVICE_CLASS(klass);
1d244b42
AF
3852
3853 dc->realize = virtio_device_realize;
3854 dc->unrealize = virtio_device_unrealize;
8e05db92 3855 dc->bus_type = TYPE_VIRTIO_BUS;
4f67d30b 3856 device_class_set_props(dc, virtio_properties);
ff4c07df
PB
3857 vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
3858 vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
9b706dbb
MT
3859
3860 vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
8e05db92
FK
3861}
3862
8e93cef1
PB
3863bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
3864{
3865 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3866 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3867
3868 return virtio_bus_ioeventfd_enabled(vbus);
3869}
3870
8e05db92
FK
3871static const TypeInfo virtio_device_info = {
3872 .name = TYPE_VIRTIO_DEVICE,
3873 .parent = TYPE_DEVICE,
3874 .instance_size = sizeof(VirtIODevice),
3875 .class_init = virtio_device_class_init,
c611c764 3876 .instance_finalize = virtio_device_instance_finalize,
8e05db92
FK
3877 .abstract = true,
3878 .class_size = sizeof(VirtioDeviceClass),
3879};
3880
3881static void virtio_register_types(void)
3882{
3883 type_register_static(&virtio_device_info);
3884}
3885
3886type_init(virtio_register_types)