]> Git Repo - qemu.git/blame - hw/virtio/virtio.c
virtio-scsi: fix disabled mode
[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 <[email protected]>
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
PB
16#include "qemu-common.h"
17#include "cpu.h"
64979a4d 18#include "trace.h"
fdfba1a2 19#include "exec/address-spaces.h"
1de7afc9 20#include "qemu/error-report.h"
0d09e41a 21#include "hw/virtio/virtio.h"
1de7afc9 22#include "qemu/atomic.h"
0d09e41a 23#include "hw/virtio/virtio-bus.h"
6b321a3d 24#include "migration/migration.h"
cee3ca00 25#include "hw/virtio/virtio-access.h"
967f97fa 26
6ce69d1c
PM
27/*
28 * The alignment to use between consumer and producer parts of vring.
29 * x86 pagesize again. This is the default, used by transports like PCI
30 * which don't provide a means for the guest to tell the host the alignment.
31 */
f46f15bc
AL
32#define VIRTIO_PCI_VRING_ALIGN 4096
33
967f97fa
AL
34typedef struct VRingDesc
35{
36 uint64_t addr;
37 uint32_t len;
38 uint16_t flags;
39 uint16_t next;
40} VRingDesc;
41
42typedef struct VRingAvail
43{
44 uint16_t flags;
45 uint16_t idx;
46 uint16_t ring[0];
47} VRingAvail;
48
49typedef struct VRingUsedElem
50{
51 uint32_t id;
52 uint32_t len;
53} VRingUsedElem;
54
55typedef struct VRingUsed
56{
57 uint16_t flags;
58 uint16_t idx;
59 VRingUsedElem ring[0];
60} VRingUsed;
61
62typedef struct VRing
63{
64 unsigned int num;
46c5d082 65 unsigned int num_default;
6ce69d1c 66 unsigned int align;
a8170e5e
AK
67 hwaddr desc;
68 hwaddr avail;
69 hwaddr used;
967f97fa
AL
70} VRing;
71
72struct VirtQueue
73{
74 VRing vring;
be1fea9b
VM
75
76 /* Next head to pop */
967f97fa 77 uint16_t last_avail_idx;
b796fcd1 78
be1fea9b
VM
79 /* Last avail_idx read from VQ. */
80 uint16_t shadow_avail_idx;
81
b796fcd1
VM
82 uint16_t used_idx;
83
bcbabae8
MT
84 /* Last used index value we have signalled on */
85 uint16_t signalled_used;
86
87 /* Last used index value we have signalled on */
88 bool signalled_used_valid;
89
90 /* Notification enabled? */
91 bool notification;
92
e78a2b42
JW
93 uint16_t queue_index;
94
967f97fa 95 int inuse;
bcbabae8 96
7055e687 97 uint16_t vector;
967f97fa 98 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
1cbdabe2
MT
99 VirtIODevice *vdev;
100 EventNotifier guest_notifier;
101 EventNotifier host_notifier;
e0d686bf 102 QLIST_ENTRY(VirtQueue) node;
967f97fa
AL
103};
104
967f97fa 105/* virt queue functions */
ab223c95 106void virtio_queue_update_rings(VirtIODevice *vdev, int n)
967f97fa 107{
ab223c95 108 VRing *vring = &vdev->vq[n].vring;
53c25cea 109
ab223c95
CH
110 if (!vring->desc) {
111 /* not yet setup -> nothing to do */
112 return;
113 }
114 vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
115 vring->used = vring_align(vring->avail +
116 offsetof(VRingAvail, ring[vring->num]),
117 vring->align);
967f97fa
AL
118}
119
aa570d6f
PB
120static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
121 hwaddr desc_pa, int i)
967f97fa 122{
aa570d6f
PB
123 address_space_read(&address_space_memory, desc_pa + i * sizeof(VRingDesc),
124 MEMTXATTRS_UNSPECIFIED, (void *)desc, sizeof(VRingDesc));
125 virtio_tswap64s(vdev, &desc->addr);
126 virtio_tswap32s(vdev, &desc->len);
127 virtio_tswap16s(vdev, &desc->flags);
128 virtio_tswap16s(vdev, &desc->next);
967f97fa
AL
129}
130
131static inline uint16_t vring_avail_flags(VirtQueue *vq)
132{
a8170e5e 133 hwaddr pa;
967f97fa 134 pa = vq->vring.avail + offsetof(VRingAvail, flags);
cee3ca00 135 return virtio_lduw_phys(vq->vdev, pa);
967f97fa
AL
136}
137
138static inline uint16_t vring_avail_idx(VirtQueue *vq)
139{
a8170e5e 140 hwaddr pa;
967f97fa 141 pa = vq->vring.avail + offsetof(VRingAvail, idx);
be1fea9b
VM
142 vq->shadow_avail_idx = virtio_lduw_phys(vq->vdev, pa);
143 return vq->shadow_avail_idx;
967f97fa
AL
144}
145
146static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
147{
a8170e5e 148 hwaddr pa;
967f97fa 149 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
cee3ca00 150 return virtio_lduw_phys(vq->vdev, pa);
967f97fa
AL
151}
152
e9600c6c 153static inline uint16_t vring_get_used_event(VirtQueue *vq)
bcbabae8
MT
154{
155 return vring_avail_ring(vq, vq->vring.num);
156}
157
1cdd2ee5
VM
158static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
159 int i)
967f97fa 160{
a8170e5e 161 hwaddr pa;
1cdd2ee5
VM
162 virtio_tswap32s(vq->vdev, &uelem->id);
163 virtio_tswap32s(vq->vdev, &uelem->len);
164 pa = vq->vring.used + offsetof(VRingUsed, ring[i]);
165 address_space_write(&address_space_memory, pa, MEMTXATTRS_UNSPECIFIED,
166 (void *)uelem, sizeof(VRingUsedElem));
967f97fa
AL
167}
168
169static uint16_t vring_used_idx(VirtQueue *vq)
170{
a8170e5e 171 hwaddr pa;
967f97fa 172 pa = vq->vring.used + offsetof(VRingUsed, idx);
cee3ca00 173 return virtio_lduw_phys(vq->vdev, pa);
967f97fa
AL
174}
175
bcbabae8 176static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
967f97fa 177{
a8170e5e 178 hwaddr pa;
967f97fa 179 pa = vq->vring.used + offsetof(VRingUsed, idx);
cee3ca00 180 virtio_stw_phys(vq->vdev, pa, val);
b796fcd1 181 vq->used_idx = val;
967f97fa
AL
182}
183
184static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
185{
cee3ca00 186 VirtIODevice *vdev = vq->vdev;
a8170e5e 187 hwaddr pa;
967f97fa 188 pa = vq->vring.used + offsetof(VRingUsed, flags);
cee3ca00 189 virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask);
967f97fa
AL
190}
191
192static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
193{
cee3ca00 194 VirtIODevice *vdev = vq->vdev;
a8170e5e 195 hwaddr pa;
967f97fa 196 pa = vq->vring.used + offsetof(VRingUsed, flags);
cee3ca00 197 virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask);
967f97fa
AL
198}
199
e9600c6c 200static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
bcbabae8 201{
a8170e5e 202 hwaddr pa;
bcbabae8
MT
203 if (!vq->notification) {
204 return;
205 }
206 pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
cee3ca00 207 virtio_stw_phys(vq->vdev, pa, val);
bcbabae8
MT
208}
209
967f97fa
AL
210void virtio_queue_set_notification(VirtQueue *vq, int enable)
211{
bcbabae8 212 vq->notification = enable;
95129d6f 213 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 214 vring_set_avail_event(vq, vring_avail_idx(vq));
bcbabae8 215 } else if (enable) {
967f97fa 216 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 217 } else {
967f97fa 218 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 219 }
92045d80
MT
220 if (enable) {
221 /* Expose avail event/used flags before caller checks the avail idx. */
222 smp_mb();
223 }
967f97fa
AL
224}
225
226int virtio_queue_ready(VirtQueue *vq)
227{
228 return vq->vring.avail != 0;
229}
230
be1fea9b
VM
231/* Fetch avail_idx from VQ memory only when we really need to know if
232 * guest has added some buffers. */
967f97fa
AL
233int virtio_queue_empty(VirtQueue *vq)
234{
be1fea9b
VM
235 if (vq->shadow_avail_idx != vq->last_avail_idx) {
236 return 0;
237 }
238
967f97fa
AL
239 return vring_avail_idx(vq) == vq->last_avail_idx;
240}
241
ce317461
JW
242static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
243 unsigned int len)
967f97fa
AL
244{
245 unsigned int offset;
246 int i;
247
967f97fa
AL
248 offset = 0;
249 for (i = 0; i < elem->in_num; i++) {
250 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
251
26b258e1
AL
252 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
253 elem->in_sg[i].iov_len,
254 1, size);
967f97fa 255
0cea71a2 256 offset += size;
967f97fa
AL
257 }
258
26b258e1
AL
259 for (i = 0; i < elem->out_num; i++)
260 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
261 elem->out_sg[i].iov_len,
262 0, elem->out_sg[i].iov_len);
ce317461
JW
263}
264
29b9f5ef
JW
265void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
266 unsigned int len)
267{
268 vq->last_avail_idx--;
269 virtqueue_unmap_sg(vq, elem, len);
270}
271
ce317461
JW
272void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
273 unsigned int len, unsigned int idx)
274{
1cdd2ee5
VM
275 VRingUsedElem uelem;
276
ce317461
JW
277 trace_virtqueue_fill(vq, elem, len, idx);
278
279 virtqueue_unmap_sg(vq, elem, len);
26b258e1 280
b796fcd1 281 idx = (idx + vq->used_idx) % vq->vring.num;
967f97fa 282
1cdd2ee5
VM
283 uelem.id = elem->index;
284 uelem.len = len;
285 vring_used_write(vq, &uelem, idx);
967f97fa
AL
286}
287
288void virtqueue_flush(VirtQueue *vq, unsigned int count)
289{
bcbabae8 290 uint16_t old, new;
967f97fa 291 /* Make sure buffer is written before we update index. */
b90d2f35 292 smp_wmb();
64979a4d 293 trace_virtqueue_flush(vq, count);
b796fcd1 294 old = vq->used_idx;
bcbabae8
MT
295 new = old + count;
296 vring_used_idx_set(vq, new);
967f97fa 297 vq->inuse -= count;
bcbabae8
MT
298 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
299 vq->signalled_used_valid = false;
967f97fa
AL
300}
301
302void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
303 unsigned int len)
304{
305 virtqueue_fill(vq, elem, len, 0);
306 virtqueue_flush(vq, 1);
307}
308
309static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
310{
311 uint16_t num_heads = vring_avail_idx(vq) - idx;
312
313 /* Check it isn't doing very strange things with descriptor numbers. */
bb6834cf 314 if (num_heads > vq->vring.num) {
ce67ed65 315 error_report("Guest moved used index from %u to %u",
be1fea9b 316 idx, vq->shadow_avail_idx);
bb6834cf
AL
317 exit(1);
318 }
a821ce59
MT
319 /* On success, callers read a descriptor at vq->last_avail_idx.
320 * Make sure descriptor read does not bypass avail index read. */
321 if (num_heads) {
322 smp_rmb();
323 }
967f97fa
AL
324
325 return num_heads;
326}
327
328static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
329{
330 unsigned int head;
331
332 /* Grab the next descriptor number they're advertising, and increment
333 * the index we've seen. */
334 head = vring_avail_ring(vq, idx % vq->vring.num);
335
336 /* If their number is silly, that's a fatal mistake. */
bb6834cf 337 if (head >= vq->vring.num) {
ce67ed65 338 error_report("Guest says index %u is available", head);
bb6834cf
AL
339 exit(1);
340 }
967f97fa
AL
341
342 return head;
343}
344
aa570d6f
PB
345static unsigned virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
346 hwaddr desc_pa, unsigned int max)
967f97fa
AL
347{
348 unsigned int next;
349
350 /* If this descriptor says it doesn't chain, we're done. */
aa570d6f 351 if (!(desc->flags & VRING_DESC_F_NEXT)) {
5774cf98 352 return max;
cee3ca00 353 }
967f97fa
AL
354
355 /* Check they're not leading us off end of descriptors. */
aa570d6f 356 next = desc->next;
967f97fa 357 /* Make sure compiler knows to grab that: we don't want it changing! */
b90d2f35 358 smp_wmb();
967f97fa 359
5774cf98 360 if (next >= max) {
ce67ed65 361 error_report("Desc next is %u", next);
bb6834cf
AL
362 exit(1);
363 }
967f97fa 364
aa570d6f 365 vring_desc_read(vdev, desc, desc_pa, next);
967f97fa
AL
366 return next;
367}
368
0d8d7690 369void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
e1f7b481
MT
370 unsigned int *out_bytes,
371 unsigned max_in_bytes, unsigned max_out_bytes)
967f97fa 372{
efeea6d0 373 unsigned int idx;
385ce95d 374 unsigned int total_bufs, in_total, out_total;
967f97fa
AL
375
376 idx = vq->last_avail_idx;
377
efeea6d0 378 total_bufs = in_total = out_total = 0;
967f97fa 379 while (virtqueue_num_heads(vq, idx)) {
cee3ca00 380 VirtIODevice *vdev = vq->vdev;
efeea6d0 381 unsigned int max, num_bufs, indirect = 0;
aa570d6f 382 VRingDesc desc;
a8170e5e 383 hwaddr desc_pa;
967f97fa
AL
384 int i;
385
efeea6d0
MM
386 max = vq->vring.num;
387 num_bufs = total_bufs;
967f97fa 388 i = virtqueue_get_head(vq, idx++);
efeea6d0 389 desc_pa = vq->vring.desc;
aa570d6f 390 vring_desc_read(vdev, &desc, desc_pa, i);
efeea6d0 391
aa570d6f
PB
392 if (desc.flags & VRING_DESC_F_INDIRECT) {
393 if (desc.len % sizeof(VRingDesc)) {
ce67ed65 394 error_report("Invalid size for indirect buffer table");
efeea6d0
MM
395 exit(1);
396 }
397
398 /* If we've got too many, that implies a descriptor loop. */
399 if (num_bufs >= max) {
ce67ed65 400 error_report("Looped descriptor");
efeea6d0
MM
401 exit(1);
402 }
403
404 /* loop over the indirect descriptor table */
405 indirect = 1;
aa570d6f
PB
406 max = desc.len / sizeof(VRingDesc);
407 desc_pa = desc.addr;
1ae2757c 408 num_bufs = i = 0;
aa570d6f 409 vring_desc_read(vdev, &desc, desc_pa, i);
efeea6d0
MM
410 }
411
967f97fa
AL
412 do {
413 /* If we've got too many, that implies a descriptor loop. */
5774cf98 414 if (++num_bufs > max) {
ce67ed65 415 error_report("Looped descriptor");
bb6834cf
AL
416 exit(1);
417 }
967f97fa 418
aa570d6f
PB
419 if (desc.flags & VRING_DESC_F_WRITE) {
420 in_total += desc.len;
967f97fa 421 } else {
aa570d6f 422 out_total += desc.len;
967f97fa 423 }
e1f7b481
MT
424 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
425 goto done;
426 }
aa570d6f 427 } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) != max);
efeea6d0
MM
428
429 if (!indirect)
430 total_bufs = num_bufs;
431 else
432 total_bufs++;
967f97fa 433 }
e1f7b481 434done:
0d8d7690
AS
435 if (in_bytes) {
436 *in_bytes = in_total;
437 }
438 if (out_bytes) {
439 *out_bytes = out_total;
440 }
441}
967f97fa 442
0d8d7690
AS
443int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
444 unsigned int out_bytes)
445{
446 unsigned int in_total, out_total;
447
e1f7b481
MT
448 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
449 return in_bytes <= in_total && out_bytes <= out_total;
967f97fa
AL
450}
451
3b3b0628
PB
452static void virtqueue_map_desc(unsigned int *p_num_sg, hwaddr *addr, struct iovec *iov,
453 unsigned int max_num_sg, bool is_write,
454 hwaddr pa, size_t sz)
455{
456 unsigned num_sg = *p_num_sg;
457 assert(num_sg <= max_num_sg);
458
459 while (sz) {
460 hwaddr len = sz;
461
462 if (num_sg == max_num_sg) {
463 error_report("virtio: too many write descriptors in indirect table");
464 exit(1);
465 }
466
467 iov[num_sg].iov_base = cpu_physical_memory_map(pa, &len, is_write);
468 iov[num_sg].iov_len = len;
469 addr[num_sg] = pa;
470
471 sz -= len;
472 pa += len;
473 num_sg++;
474 }
475 *p_num_sg = num_sg;
476}
477
8059feee
MT
478static void virtqueue_map_iovec(struct iovec *sg, hwaddr *addr,
479 unsigned int *num_sg, unsigned int max_size,
480 int is_write)
42fb2e07
KW
481{
482 unsigned int i;
a8170e5e 483 hwaddr len;
42fb2e07 484
8059feee
MT
485 /* Note: this function MUST validate input, some callers
486 * are passing in num_sg values received over the network.
487 */
488 /* TODO: teach all callers that this can fail, and return failure instead
489 * of asserting here.
490 * When we do, we might be able to re-enable NDEBUG below.
491 */
492#ifdef NDEBUG
493#error building with NDEBUG is not supported
494#endif
495 assert(*num_sg <= max_size);
496
497 for (i = 0; i < *num_sg; i++) {
42fb2e07
KW
498 len = sg[i].iov_len;
499 sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
8059feee 500 if (!sg[i].iov_base) {
1a285899 501 error_report("virtio: error trying to map MMIO memory");
42fb2e07
KW
502 exit(1);
503 }
3b3b0628
PB
504 if (len != sg[i].iov_len) {
505 error_report("virtio: unexpected memory split");
8059feee
MT
506 exit(1);
507 }
42fb2e07
KW
508 }
509}
510
8059feee
MT
511void virtqueue_map(VirtQueueElement *elem)
512{
513 virtqueue_map_iovec(elem->in_sg, elem->in_addr, &elem->in_num,
3724650d 514 VIRTQUEUE_MAX_SIZE, 1);
8059feee 515 virtqueue_map_iovec(elem->out_sg, elem->out_addr, &elem->out_num,
3724650d
PB
516 VIRTQUEUE_MAX_SIZE, 0);
517}
518
519void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
520{
521 VirtQueueElement *elem;
522 size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
523 size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
524 size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
525 size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
526 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
527 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
528
529 assert(sz >= sizeof(VirtQueueElement));
530 elem = g_malloc(out_sg_end);
531 elem->out_num = out_num;
532 elem->in_num = in_num;
533 elem->in_addr = (void *)elem + in_addr_ofs;
534 elem->out_addr = (void *)elem + out_addr_ofs;
535 elem->in_sg = (void *)elem + in_sg_ofs;
536 elem->out_sg = (void *)elem + out_sg_ofs;
537 return elem;
8059feee
MT
538}
539
51b19ebe 540void *virtqueue_pop(VirtQueue *vq, size_t sz)
967f97fa 541{
5774cf98 542 unsigned int i, head, max;
a8170e5e 543 hwaddr desc_pa = vq->vring.desc;
cee3ca00 544 VirtIODevice *vdev = vq->vdev;
51b19ebe 545 VirtQueueElement *elem;
3b3b0628
PB
546 unsigned out_num, in_num;
547 hwaddr addr[VIRTQUEUE_MAX_SIZE];
548 struct iovec iov[VIRTQUEUE_MAX_SIZE];
aa570d6f 549 VRingDesc desc;
967f97fa 550
be1fea9b 551 if (virtio_queue_empty(vq)) {
51b19ebe
PB
552 return NULL;
553 }
be1fea9b
VM
554 /* Needed after virtio_queue_empty(), see comment in
555 * virtqueue_num_heads(). */
556 smp_rmb();
967f97fa
AL
557
558 /* When we start there are none of either input nor output. */
3b3b0628 559 out_num = in_num = 0;
967f97fa 560
5774cf98
MM
561 max = vq->vring.num;
562
967f97fa 563 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
95129d6f 564 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 565 vring_set_avail_event(vq, vq->last_avail_idx);
bcbabae8 566 }
efeea6d0 567
aa570d6f
PB
568 vring_desc_read(vdev, &desc, desc_pa, i);
569 if (desc.flags & VRING_DESC_F_INDIRECT) {
570 if (desc.len % sizeof(VRingDesc)) {
ce67ed65 571 error_report("Invalid size for indirect buffer table");
efeea6d0
MM
572 exit(1);
573 }
574
575 /* loop over the indirect descriptor table */
aa570d6f
PB
576 max = desc.len / sizeof(VRingDesc);
577 desc_pa = desc.addr;
efeea6d0 578 i = 0;
aa570d6f 579 vring_desc_read(vdev, &desc, desc_pa, i);
efeea6d0
MM
580 }
581
42fb2e07 582 /* Collect all the descriptors */
967f97fa 583 do {
aa570d6f 584 if (desc.flags & VRING_DESC_F_WRITE) {
3b3b0628 585 virtqueue_map_desc(&in_num, addr + out_num, iov + out_num,
aa570d6f 586 VIRTQUEUE_MAX_SIZE - out_num, true, desc.addr, desc.len);
42fb2e07 587 } else {
3b3b0628
PB
588 if (in_num) {
589 error_report("Incorrect order for descriptors");
c8eac1cf
MT
590 exit(1);
591 }
3b3b0628 592 virtqueue_map_desc(&out_num, addr, iov,
aa570d6f 593 VIRTQUEUE_MAX_SIZE, false, desc.addr, desc.len);
42fb2e07 594 }
967f97fa 595
967f97fa 596 /* If we've got too many, that implies a descriptor loop. */
3b3b0628 597 if ((in_num + out_num) > max) {
ce67ed65 598 error_report("Looped descriptor");
bb6834cf
AL
599 exit(1);
600 }
aa570d6f 601 } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) != max);
967f97fa 602
3b3b0628
PB
603 /* Now copy what we have collected and mapped */
604 elem = virtqueue_alloc_element(sz, out_num, in_num);
967f97fa 605 elem->index = head;
3b3b0628
PB
606 for (i = 0; i < out_num; i++) {
607 elem->out_addr[i] = addr[i];
608 elem->out_sg[i] = iov[i];
609 }
610 for (i = 0; i < in_num; i++) {
611 elem->in_addr[i] = addr[out_num + i];
612 elem->in_sg[i] = iov[out_num + i];
613 }
967f97fa
AL
614
615 vq->inuse++;
616
64979a4d 617 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
51b19ebe 618 return elem;
967f97fa
AL
619}
620
3724650d
PB
621/* Reading and writing a structure directly to QEMUFile is *awful*, but
622 * it is what QEMU has always done by mistake. We can change it sooner
623 * or later by bumping the version number of the affected vm states.
624 * In the meanwhile, since the in-memory layout of VirtQueueElement
625 * has changed, we need to marshal to and from the layout that was
626 * used before the change.
627 */
628typedef struct VirtQueueElementOld {
629 unsigned int index;
630 unsigned int out_num;
631 unsigned int in_num;
632 hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
633 hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
634 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
635 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
636} VirtQueueElementOld;
637
ab281c17
PB
638void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz)
639{
3724650d
PB
640 VirtQueueElement *elem;
641 VirtQueueElementOld data;
642 int i;
643
644 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
645
646 elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
647 elem->index = data.index;
648
649 for (i = 0; i < elem->in_num; i++) {
650 elem->in_addr[i] = data.in_addr[i];
651 }
652
653 for (i = 0; i < elem->out_num; i++) {
654 elem->out_addr[i] = data.out_addr[i];
655 }
656
657 for (i = 0; i < elem->in_num; i++) {
658 /* Base is overwritten by virtqueue_map. */
659 elem->in_sg[i].iov_base = 0;
660 elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
661 }
662
663 for (i = 0; i < elem->out_num; i++) {
664 /* Base is overwritten by virtqueue_map. */
665 elem->out_sg[i].iov_base = 0;
666 elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
667 }
668
ab281c17
PB
669 virtqueue_map(elem);
670 return elem;
671}
672
673void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem)
674{
3724650d
PB
675 VirtQueueElementOld data;
676 int i;
677
678 memset(&data, 0, sizeof(data));
679 data.index = elem->index;
680 data.in_num = elem->in_num;
681 data.out_num = elem->out_num;
682
683 for (i = 0; i < elem->in_num; i++) {
684 data.in_addr[i] = elem->in_addr[i];
685 }
686
687 for (i = 0; i < elem->out_num; i++) {
688 data.out_addr[i] = elem->out_addr[i];
689 }
690
691 for (i = 0; i < elem->in_num; i++) {
692 /* Base is overwritten by virtqueue_map when loading. Do not
693 * save it, as it would leak the QEMU address space layout. */
694 data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
695 }
696
697 for (i = 0; i < elem->out_num; i++) {
698 /* Do not save iov_base as above. */
699 data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
700 }
701 qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
ab281c17
PB
702}
703
967f97fa 704/* virtio device */
7055e687
MT
705static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
706{
1c819449
FK
707 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
708 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
709
710 if (k->notify) {
711 k->notify(qbus->parent, vector);
7055e687
MT
712 }
713}
967f97fa 714
53c25cea 715void virtio_update_irq(VirtIODevice *vdev)
967f97fa 716{
7055e687 717 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
967f97fa
AL
718}
719
0b352fd6
CH
720static int virtio_validate_features(VirtIODevice *vdev)
721{
722 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
723
724 if (k->validate_features) {
725 return k->validate_features(vdev);
726 } else {
727 return 0;
728 }
729}
730
731int virtio_set_status(VirtIODevice *vdev, uint8_t val)
4e1837f8 732{
181103cd 733 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
4e1837f8
SH
734 trace_virtio_set_status(vdev, val);
735
95129d6f 736 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
0b352fd6
CH
737 if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
738 val & VIRTIO_CONFIG_S_FEATURES_OK) {
739 int ret = virtio_validate_features(vdev);
740
741 if (ret) {
742 return ret;
743 }
744 }
745 }
181103cd
FK
746 if (k->set_status) {
747 k->set_status(vdev, val);
4e1837f8
SH
748 }
749 vdev->status = val;
0b352fd6 750 return 0;
4e1837f8
SH
751}
752
616a6552
GK
753bool target_words_bigendian(void);
754static enum virtio_device_endian virtio_default_endian(void)
755{
756 if (target_words_bigendian()) {
757 return VIRTIO_DEVICE_ENDIAN_BIG;
758 } else {
759 return VIRTIO_DEVICE_ENDIAN_LITTLE;
760 }
761}
762
763static enum virtio_device_endian virtio_current_cpu_endian(void)
764{
765 CPUClass *cc = CPU_GET_CLASS(current_cpu);
766
767 if (cc->virtio_is_big_endian(current_cpu)) {
768 return VIRTIO_DEVICE_ENDIAN_BIG;
769 } else {
770 return VIRTIO_DEVICE_ENDIAN_LITTLE;
771 }
772}
773
53c25cea 774void virtio_reset(void *opaque)
967f97fa
AL
775{
776 VirtIODevice *vdev = opaque;
181103cd 777 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
778 int i;
779
e0c472d8 780 virtio_set_status(vdev, 0);
616a6552
GK
781 if (current_cpu) {
782 /* Guest initiated reset */
783 vdev->device_endian = virtio_current_cpu_endian();
784 } else {
785 /* System reset */
786 vdev->device_endian = virtio_default_endian();
787 }
e0c472d8 788
181103cd
FK
789 if (k->reset) {
790 k->reset(vdev);
791 }
967f97fa 792
704a76fc 793 vdev->guest_features = 0;
967f97fa
AL
794 vdev->queue_sel = 0;
795 vdev->status = 0;
796 vdev->isr = 0;
7055e687
MT
797 vdev->config_vector = VIRTIO_NO_VECTOR;
798 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa 799
87b3bd1c 800 for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
801 vdev->vq[i].vring.desc = 0;
802 vdev->vq[i].vring.avail = 0;
803 vdev->vq[i].vring.used = 0;
804 vdev->vq[i].last_avail_idx = 0;
be1fea9b 805 vdev->vq[i].shadow_avail_idx = 0;
b796fcd1 806 vdev->vq[i].used_idx = 0;
e0d686bf 807 virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
bcbabae8
MT
808 vdev->vq[i].signalled_used = 0;
809 vdev->vq[i].signalled_used_valid = false;
810 vdev->vq[i].notification = true;
46c5d082 811 vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
967f97fa
AL
812 }
813}
814
53c25cea 815uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
967f97fa 816{
181103cd 817 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
818 uint8_t val;
819
5f5a1318 820 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 821 return (uint32_t)-1;
5f5a1318
JW
822 }
823
824 k->get_config(vdev, vdev->config);
967f97fa 825
06dbfc6f 826 val = ldub_p(vdev->config + addr);
967f97fa
AL
827 return val;
828}
829
53c25cea 830uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
967f97fa 831{
181103cd 832 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
833 uint16_t val;
834
5f5a1318 835 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 836 return (uint32_t)-1;
5f5a1318
JW
837 }
838
839 k->get_config(vdev, vdev->config);
967f97fa 840
06dbfc6f 841 val = lduw_p(vdev->config + addr);
967f97fa
AL
842 return val;
843}
844
53c25cea 845uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
967f97fa 846{
181103cd 847 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
848 uint32_t val;
849
5f5a1318 850 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 851 return (uint32_t)-1;
5f5a1318
JW
852 }
853
854 k->get_config(vdev, vdev->config);
967f97fa 855
06dbfc6f 856 val = ldl_p(vdev->config + addr);
967f97fa
AL
857 return val;
858}
859
53c25cea 860void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 861{
181103cd 862 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
863 uint8_t val = data;
864
5f5a1318 865 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 866 return;
5f5a1318 867 }
967f97fa 868
06dbfc6f 869 stb_p(vdev->config + addr, val);
967f97fa 870
181103cd
FK
871 if (k->set_config) {
872 k->set_config(vdev, vdev->config);
873 }
967f97fa
AL
874}
875
53c25cea 876void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 877{
181103cd 878 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
879 uint16_t val = data;
880
5f5a1318 881 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 882 return;
5f5a1318 883 }
967f97fa 884
06dbfc6f 885 stw_p(vdev->config + addr, val);
967f97fa 886
181103cd
FK
887 if (k->set_config) {
888 k->set_config(vdev, vdev->config);
889 }
967f97fa
AL
890}
891
53c25cea 892void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 893{
181103cd 894 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
895 uint32_t val = data;
896
5f5a1318 897 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 898 return;
5f5a1318 899 }
967f97fa 900
06dbfc6f 901 stl_p(vdev->config + addr, val);
967f97fa 902
181103cd
FK
903 if (k->set_config) {
904 k->set_config(vdev, vdev->config);
905 }
967f97fa
AL
906}
907
adfb743c
MT
908uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
909{
910 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
911 uint8_t val;
912
913 if (addr + sizeof(val) > vdev->config_len) {
914 return (uint32_t)-1;
915 }
916
917 k->get_config(vdev, vdev->config);
918
919 val = ldub_p(vdev->config + addr);
920 return val;
921}
922
923uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
924{
925 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
926 uint16_t val;
927
928 if (addr + sizeof(val) > vdev->config_len) {
929 return (uint32_t)-1;
930 }
931
932 k->get_config(vdev, vdev->config);
933
934 val = lduw_le_p(vdev->config + addr);
935 return val;
936}
937
938uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
939{
940 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
941 uint32_t val;
942
943 if (addr + sizeof(val) > vdev->config_len) {
944 return (uint32_t)-1;
945 }
946
947 k->get_config(vdev, vdev->config);
948
949 val = ldl_le_p(vdev->config + addr);
950 return val;
951}
952
953void virtio_config_modern_writeb(VirtIODevice *vdev,
954 uint32_t addr, uint32_t data)
955{
956 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
957 uint8_t val = data;
958
959 if (addr + sizeof(val) > vdev->config_len) {
960 return;
961 }
962
963 stb_p(vdev->config + addr, val);
964
965 if (k->set_config) {
966 k->set_config(vdev, vdev->config);
967 }
968}
969
970void virtio_config_modern_writew(VirtIODevice *vdev,
971 uint32_t addr, uint32_t data)
972{
973 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
974 uint16_t val = data;
975
976 if (addr + sizeof(val) > vdev->config_len) {
977 return;
978 }
979
980 stw_le_p(vdev->config + addr, val);
981
982 if (k->set_config) {
983 k->set_config(vdev, vdev->config);
984 }
985}
986
987void virtio_config_modern_writel(VirtIODevice *vdev,
988 uint32_t addr, uint32_t data)
989{
990 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
991 uint32_t val = data;
992
993 if (addr + sizeof(val) > vdev->config_len) {
994 return;
995 }
996
997 stl_le_p(vdev->config + addr, val);
998
999 if (k->set_config) {
1000 k->set_config(vdev, vdev->config);
1001 }
1002}
1003
a8170e5e 1004void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
967f97fa 1005{
ab223c95
CH
1006 vdev->vq[n].vring.desc = addr;
1007 virtio_queue_update_rings(vdev, n);
53c25cea
PB
1008}
1009
a8170e5e 1010hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
53c25cea 1011{
ab223c95
CH
1012 return vdev->vq[n].vring.desc;
1013}
1014
1015void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
1016 hwaddr avail, hwaddr used)
1017{
1018 vdev->vq[n].vring.desc = desc;
1019 vdev->vq[n].vring.avail = avail;
1020 vdev->vq[n].vring.used = used;
53c25cea
PB
1021}
1022
e63c0ba1
PM
1023void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
1024{
f6049f44
PM
1025 /* Don't allow guest to flip queue between existent and
1026 * nonexistent states, or to set it to an invalid size.
1027 */
1028 if (!!num != !!vdev->vq[n].vring.num ||
1029 num > VIRTQUEUE_MAX_SIZE ||
1030 num < 0) {
1031 return;
e63c0ba1 1032 }
f6049f44 1033 vdev->vq[n].vring.num = num;
e63c0ba1
PM
1034}
1035
e0d686bf
JW
1036VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
1037{
1038 return QLIST_FIRST(&vdev->vector_queues[vector]);
1039}
1040
1041VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
1042{
1043 return QLIST_NEXT(vq, node);
1044}
1045
53c25cea
PB
1046int virtio_queue_get_num(VirtIODevice *vdev, int n)
1047{
1048 return vdev->vq[n].vring.num;
1049}
967f97fa 1050
8ad176aa
JW
1051int virtio_get_num_queues(VirtIODevice *vdev)
1052{
1053 int i;
1054
87b3bd1c 1055 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
8ad176aa
JW
1056 if (!virtio_queue_get_num(vdev, i)) {
1057 break;
1058 }
1059 }
1060
1061 return i;
1062}
1063
c80decdb
PB
1064int virtio_queue_get_id(VirtQueue *vq)
1065{
1066 VirtIODevice *vdev = vq->vdev;
87b3bd1c 1067 assert(vq >= &vdev->vq[0] && vq < &vdev->vq[VIRTIO_QUEUE_MAX]);
c80decdb
PB
1068 return vq - &vdev->vq[0];
1069}
1070
6ce69d1c
PM
1071void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
1072{
1073 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1074 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1075
ab223c95 1076 /* virtio-1 compliant devices cannot change the alignment */
95129d6f 1077 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
ab223c95
CH
1078 error_report("tried to modify queue alignment for virtio-1 device");
1079 return;
1080 }
6ce69d1c
PM
1081 /* Check that the transport told us it was going to do this
1082 * (so a buggy transport will immediately assert rather than
1083 * silently failing to migrate this state)
1084 */
1085 assert(k->has_variable_vring_alignment);
1086
1087 vdev->vq[n].vring.align = align;
ab223c95 1088 virtio_queue_update_rings(vdev, n);
6ce69d1c
PM
1089}
1090
2b2cbcad 1091static void virtio_queue_notify_vq(VirtQueue *vq)
25db9ebe 1092{
9e0f5b81 1093 if (vq->vring.desc && vq->handle_output) {
25db9ebe 1094 VirtIODevice *vdev = vq->vdev;
9e0f5b81 1095
25db9ebe
SH
1096 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1097 vq->handle_output(vdev, vq);
1098 }
1099}
1100
53c25cea
PB
1101void virtio_queue_notify(VirtIODevice *vdev, int n)
1102{
7157e2e2 1103 virtio_queue_notify_vq(&vdev->vq[n]);
967f97fa
AL
1104}
1105
7055e687
MT
1106uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
1107{
87b3bd1c 1108 return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
7055e687
MT
1109 VIRTIO_NO_VECTOR;
1110}
1111
1112void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
1113{
e0d686bf
JW
1114 VirtQueue *vq = &vdev->vq[n];
1115
87b3bd1c 1116 if (n < VIRTIO_QUEUE_MAX) {
e0d686bf
JW
1117 if (vdev->vector_queues &&
1118 vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
1119 QLIST_REMOVE(vq, node);
1120 }
7055e687 1121 vdev->vq[n].vector = vector;
e0d686bf
JW
1122 if (vdev->vector_queues &&
1123 vector != VIRTIO_NO_VECTOR) {
1124 QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
1125 }
1126 }
7055e687
MT
1127}
1128
967f97fa
AL
1129VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
1130 void (*handle_output)(VirtIODevice *, VirtQueue *))
1131{
1132 int i;
1133
87b3bd1c 1134 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
1135 if (vdev->vq[i].vring.num == 0)
1136 break;
1137 }
1138
87b3bd1c 1139 if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
967f97fa
AL
1140 abort();
1141
1142 vdev->vq[i].vring.num = queue_size;
46c5d082 1143 vdev->vq[i].vring.num_default = queue_size;
6ce69d1c 1144 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
967f97fa
AL
1145 vdev->vq[i].handle_output = handle_output;
1146
1147 return &vdev->vq[i];
1148}
1149
f23fd811
JW
1150void virtio_del_queue(VirtIODevice *vdev, int n)
1151{
87b3bd1c 1152 if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
f23fd811
JW
1153 abort();
1154 }
1155
1156 vdev->vq[n].vring.num = 0;
46c5d082 1157 vdev->vq[n].vring.num_default = 0;
f23fd811
JW
1158}
1159
1cbdabe2
MT
1160void virtio_irq(VirtQueue *vq)
1161{
64979a4d 1162 trace_virtio_irq(vq);
1cbdabe2
MT
1163 vq->vdev->isr |= 0x01;
1164 virtio_notify_vector(vq->vdev, vq->vector);
1165}
1166
adb3feda 1167bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
bcbabae8
MT
1168{
1169 uint16_t old, new;
1170 bool v;
a281ebc1
MT
1171 /* We need to expose used array entries before checking used event. */
1172 smp_mb();
97b83deb 1173 /* Always notify when queue is empty (when feature acknowledge) */
95129d6f 1174 if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
be1fea9b 1175 !vq->inuse && virtio_queue_empty(vq)) {
bcbabae8
MT
1176 return true;
1177 }
1178
95129d6f 1179 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
bcbabae8
MT
1180 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
1181 }
1182
1183 v = vq->signalled_used_valid;
1184 vq->signalled_used_valid = true;
1185 old = vq->signalled_used;
b796fcd1 1186 new = vq->signalled_used = vq->used_idx;
e9600c6c 1187 return !v || vring_need_event(vring_get_used_event(vq), new, old);
bcbabae8
MT
1188}
1189
1190void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
1191{
adb3feda 1192 if (!virtio_should_notify(vdev, vq)) {
967f97fa 1193 return;
bcbabae8 1194 }
967f97fa 1195
64979a4d 1196 trace_virtio_notify(vdev, vq);
967f97fa 1197 vdev->isr |= 0x01;
7055e687 1198 virtio_notify_vector(vdev, vq->vector);
967f97fa
AL
1199}
1200
1201void virtio_notify_config(VirtIODevice *vdev)
1202{
7625162c
AL
1203 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
1204 return;
1205
967f97fa 1206 vdev->isr |= 0x03;
b8f05908 1207 vdev->generation++;
7055e687 1208 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa
AL
1209}
1210
616a6552
GK
1211static bool virtio_device_endian_needed(void *opaque)
1212{
1213 VirtIODevice *vdev = opaque;
1214
1215 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
95129d6f 1216 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3c185597
CH
1217 return vdev->device_endian != virtio_default_endian();
1218 }
1219 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1220 return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
616a6552
GK
1221}
1222
019a3edb
GH
1223static bool virtio_64bit_features_needed(void *opaque)
1224{
1225 VirtIODevice *vdev = opaque;
1226
1227 return (vdev->host_features >> 32) != 0;
1228}
1229
74aae7b2
JW
1230static bool virtio_virtqueue_needed(void *opaque)
1231{
1232 VirtIODevice *vdev = opaque;
1233
1234 return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
1235}
1236
46c5d082
CH
1237static bool virtio_ringsize_needed(void *opaque)
1238{
1239 VirtIODevice *vdev = opaque;
1240 int i;
1241
1242 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1243 if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
1244 return true;
1245 }
1246 }
1247 return false;
1248}
1249
a6df8adf
JW
1250static bool virtio_extra_state_needed(void *opaque)
1251{
1252 VirtIODevice *vdev = opaque;
1253 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1254 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1255
1256 return k->has_extra_state &&
1257 k->has_extra_state(qbus->parent);
1258}
1259
50e5ae4d 1260static const VMStateDescription vmstate_virtqueue = {
74aae7b2 1261 .name = "virtqueue_state",
50e5ae4d
DDAG
1262 .version_id = 1,
1263 .minimum_version_id = 1,
1264 .fields = (VMStateField[]) {
1265 VMSTATE_UINT64(vring.avail, struct VirtQueue),
1266 VMSTATE_UINT64(vring.used, struct VirtQueue),
1267 VMSTATE_END_OF_LIST()
1268 }
74aae7b2
JW
1269};
1270
1271static const VMStateDescription vmstate_virtio_virtqueues = {
1272 .name = "virtio/virtqueues",
1273 .version_id = 1,
1274 .minimum_version_id = 1,
1275 .needed = &virtio_virtqueue_needed,
1276 .fields = (VMStateField[]) {
3e996cc5
DDAG
1277 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1278 VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
74aae7b2
JW
1279 VMSTATE_END_OF_LIST()
1280 }
1281};
1282
50e5ae4d 1283static const VMStateDescription vmstate_ringsize = {
46c5d082 1284 .name = "ringsize_state",
50e5ae4d
DDAG
1285 .version_id = 1,
1286 .minimum_version_id = 1,
1287 .fields = (VMStateField[]) {
1288 VMSTATE_UINT32(vring.num_default, struct VirtQueue),
1289 VMSTATE_END_OF_LIST()
1290 }
46c5d082
CH
1291};
1292
1293static const VMStateDescription vmstate_virtio_ringsize = {
1294 .name = "virtio/ringsize",
1295 .version_id = 1,
1296 .minimum_version_id = 1,
1297 .needed = &virtio_ringsize_needed,
1298 .fields = (VMStateField[]) {
3e996cc5
DDAG
1299 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1300 VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
46c5d082
CH
1301 VMSTATE_END_OF_LIST()
1302 }
1303};
1304
a6df8adf
JW
1305static int get_extra_state(QEMUFile *f, void *pv, size_t size)
1306{
1307 VirtIODevice *vdev = pv;
1308 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1309 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1310
1311 if (!k->load_extra_state) {
1312 return -1;
1313 } else {
1314 return k->load_extra_state(qbus->parent, f);
1315 }
1316}
1317
1318static void put_extra_state(QEMUFile *f, void *pv, size_t size)
1319{
1320 VirtIODevice *vdev = pv;
1321 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1322 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1323
1324 k->save_extra_state(qbus->parent, f);
1325}
1326
1327static const VMStateInfo vmstate_info_extra_state = {
1328 .name = "virtqueue_extra_state",
1329 .get = get_extra_state,
1330 .put = put_extra_state,
1331};
1332
1333static const VMStateDescription vmstate_virtio_extra_state = {
1334 .name = "virtio/extra_state",
1335 .version_id = 1,
1336 .minimum_version_id = 1,
1337 .needed = &virtio_extra_state_needed,
1338 .fields = (VMStateField[]) {
1339 {
1340 .name = "extra_state",
1341 .version_id = 0,
1342 .field_exists = NULL,
1343 .size = 0,
1344 .info = &vmstate_info_extra_state,
1345 .flags = VMS_SINGLE,
1346 .offset = 0,
1347 },
1348 VMSTATE_END_OF_LIST()
1349 }
1350};
1351
616a6552
GK
1352static const VMStateDescription vmstate_virtio_device_endian = {
1353 .name = "virtio/device_endian",
1354 .version_id = 1,
1355 .minimum_version_id = 1,
5cd8cada 1356 .needed = &virtio_device_endian_needed,
616a6552
GK
1357 .fields = (VMStateField[]) {
1358 VMSTATE_UINT8(device_endian, VirtIODevice),
1359 VMSTATE_END_OF_LIST()
1360 }
1361};
1362
019a3edb
GH
1363static const VMStateDescription vmstate_virtio_64bit_features = {
1364 .name = "virtio/64bit_features",
1365 .version_id = 1,
1366 .minimum_version_id = 1,
5cd8cada 1367 .needed = &virtio_64bit_features_needed,
019a3edb
GH
1368 .fields = (VMStateField[]) {
1369 VMSTATE_UINT64(guest_features, VirtIODevice),
1370 VMSTATE_END_OF_LIST()
1371 }
1372};
1373
6b321a3d
GK
1374static const VMStateDescription vmstate_virtio = {
1375 .name = "virtio",
1376 .version_id = 1,
1377 .minimum_version_id = 1,
1378 .minimum_version_id_old = 1,
1379 .fields = (VMStateField[]) {
1380 VMSTATE_END_OF_LIST()
616a6552 1381 },
5cd8cada
JQ
1382 .subsections = (const VMStateDescription*[]) {
1383 &vmstate_virtio_device_endian,
1384 &vmstate_virtio_64bit_features,
74aae7b2 1385 &vmstate_virtio_virtqueues,
46c5d082 1386 &vmstate_virtio_ringsize,
a6df8adf 1387 &vmstate_virtio_extra_state,
5cd8cada 1388 NULL
6b321a3d
GK
1389 }
1390};
1391
967f97fa
AL
1392void virtio_save(VirtIODevice *vdev, QEMUFile *f)
1393{
1c819449
FK
1394 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1395 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 1396 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
019a3edb 1397 uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
967f97fa
AL
1398 int i;
1399
1c819449
FK
1400 if (k->save_config) {
1401 k->save_config(qbus->parent, f);
1402 }
967f97fa 1403
967f97fa
AL
1404 qemu_put_8s(f, &vdev->status);
1405 qemu_put_8s(f, &vdev->isr);
1406 qemu_put_be16s(f, &vdev->queue_sel);
019a3edb 1407 qemu_put_be32s(f, &guest_features_lo);
967f97fa
AL
1408 qemu_put_be32(f, vdev->config_len);
1409 qemu_put_buffer(f, vdev->config, vdev->config_len);
1410
87b3bd1c 1411 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
1412 if (vdev->vq[i].vring.num == 0)
1413 break;
1414 }
1415
1416 qemu_put_be32(f, i);
1417
87b3bd1c 1418 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
1419 if (vdev->vq[i].vring.num == 0)
1420 break;
1421
1422 qemu_put_be32(f, vdev->vq[i].vring.num);
6ce69d1c
PM
1423 if (k->has_variable_vring_alignment) {
1424 qemu_put_be32(f, vdev->vq[i].vring.align);
1425 }
ab223c95
CH
1426 /* XXX virtio-1 devices */
1427 qemu_put_be64(f, vdev->vq[i].vring.desc);
967f97fa 1428 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1c819449
FK
1429 if (k->save_queue) {
1430 k->save_queue(qbus->parent, i, f);
1431 }
967f97fa 1432 }
1b5fc0de
GK
1433
1434 if (vdc->save != NULL) {
1435 vdc->save(vdev, f);
1436 }
6b321a3d
GK
1437
1438 /* Subsections */
8118f095 1439 vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
967f97fa
AL
1440}
1441
6c0196d7 1442static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
ad0c9332 1443{
181103cd 1444 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
6b8f1020 1445 bool bad = (val & ~(vdev->host_features)) != 0;
ad0c9332 1446
6b8f1020 1447 val &= vdev->host_features;
181103cd
FK
1448 if (k->set_features) {
1449 k->set_features(vdev, val);
ad0c9332
PB
1450 }
1451 vdev->guest_features = val;
1452 return bad ? -1 : 0;
1453}
1454
6c0196d7
CH
1455int virtio_set_features(VirtIODevice *vdev, uint64_t val)
1456{
1457 /*
1458 * The driver must not attempt to set features after feature negotiation
1459 * has finished.
1460 */
1461 if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
1462 return -EINVAL;
1463 }
1464 return virtio_set_features_nocheck(vdev, val);
1465}
1466
1b5fc0de 1467int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
967f97fa 1468{
cc459952 1469 int i, ret;
a890a2f9 1470 int32_t config_len;
cc459952 1471 uint32_t num;
6d74ca5a 1472 uint32_t features;
1c819449
FK
1473 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1474 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 1475 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa 1476
616a6552
GK
1477 /*
1478 * We poison the endianness to ensure it does not get used before
1479 * subsections have been loaded.
1480 */
1481 vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
1482
1c819449
FK
1483 if (k->load_config) {
1484 ret = k->load_config(qbus->parent, f);
ff24bd58
MT
1485 if (ret)
1486 return ret;
1487 }
967f97fa 1488
967f97fa
AL
1489 qemu_get_8s(f, &vdev->status);
1490 qemu_get_8s(f, &vdev->isr);
1491 qemu_get_be16s(f, &vdev->queue_sel);
87b3bd1c 1492 if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
4b53c2c7
MR
1493 return -1;
1494 }
6d74ca5a 1495 qemu_get_be32s(f, &features);
ad0c9332 1496
a890a2f9 1497 config_len = qemu_get_be32(f);
2f5732e9
DDAG
1498
1499 /*
1500 * There are cases where the incoming config can be bigger or smaller
1501 * than what we have; so load what we have space for, and skip
1502 * any excess that's in the stream.
1503 */
1504 qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
1505
1506 while (config_len > vdev->config_len) {
1507 qemu_get_byte(f);
1508 config_len--;
a890a2f9 1509 }
967f97fa
AL
1510
1511 num = qemu_get_be32(f);
1512
87b3bd1c 1513 if (num > VIRTIO_QUEUE_MAX) {
8a1be662 1514 error_report("Invalid number of virtqueues: 0x%x", num);
cc459952
MT
1515 return -1;
1516 }
1517
967f97fa
AL
1518 for (i = 0; i < num; i++) {
1519 vdev->vq[i].vring.num = qemu_get_be32(f);
6ce69d1c
PM
1520 if (k->has_variable_vring_alignment) {
1521 vdev->vq[i].vring.align = qemu_get_be32(f);
1522 }
ab223c95 1523 vdev->vq[i].vring.desc = qemu_get_be64(f);
967f97fa 1524 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
bcbabae8
MT
1525 vdev->vq[i].signalled_used_valid = false;
1526 vdev->vq[i].notification = true;
967f97fa 1527
ab223c95
CH
1528 if (vdev->vq[i].vring.desc) {
1529 /* XXX virtio-1 devices */
1530 virtio_queue_update_rings(vdev, i);
1abeb5a6
MT
1531 } else if (vdev->vq[i].last_avail_idx) {
1532 error_report("VQ %d address 0x0 "
6daf194d 1533 "inconsistent with Host index 0x%x",
1abeb5a6
MT
1534 i, vdev->vq[i].last_avail_idx);
1535 return -1;
258dc7c9 1536 }
1c819449
FK
1537 if (k->load_queue) {
1538 ret = k->load_queue(qbus->parent, i, f);
ff24bd58
MT
1539 if (ret)
1540 return ret;
7055e687 1541 }
967f97fa
AL
1542 }
1543
7055e687 1544 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1b5fc0de
GK
1545
1546 if (vdc->load != NULL) {
6b321a3d
GK
1547 ret = vdc->load(vdev, f, version_id);
1548 if (ret) {
1549 return ret;
1550 }
1b5fc0de
GK
1551 }
1552
616a6552
GK
1553 /* Subsections */
1554 ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
1555 if (ret) {
1556 return ret;
1557 }
1558
1559 if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
1560 vdev->device_endian = virtio_default_endian();
1561 }
1562
019a3edb
GH
1563 if (virtio_64bit_features_needed(vdev)) {
1564 /*
1565 * Subsection load filled vdev->guest_features. Run them
1566 * through virtio_set_features to sanity-check them against
1567 * host_features.
1568 */
1569 uint64_t features64 = vdev->guest_features;
6c0196d7 1570 if (virtio_set_features_nocheck(vdev, features64) < 0) {
019a3edb
GH
1571 error_report("Features 0x%" PRIx64 " unsupported. "
1572 "Allowed features: 0x%" PRIx64,
1573 features64, vdev->host_features);
1574 return -1;
1575 }
1576 } else {
6c0196d7 1577 if (virtio_set_features_nocheck(vdev, features) < 0) {
019a3edb
GH
1578 error_report("Features 0x%x unsupported. "
1579 "Allowed features: 0x%" PRIx64,
1580 features, vdev->host_features);
1581 return -1;
1582 }
1583 }
1584
616a6552 1585 for (i = 0; i < num; i++) {
ab223c95 1586 if (vdev->vq[i].vring.desc) {
616a6552
GK
1587 uint16_t nheads;
1588 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
1589 /* Check it isn't doing strange things with descriptor numbers. */
1590 if (nheads > vdev->vq[i].vring.num) {
1591 error_report("VQ %d size 0x%x Guest index 0x%x "
1592 "inconsistent with Host index 0x%x: delta 0x%x",
1593 i, vdev->vq[i].vring.num,
1594 vring_avail_idx(&vdev->vq[i]),
1595 vdev->vq[i].last_avail_idx, nheads);
1596 return -1;
1597 }
b796fcd1 1598 vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
be1fea9b 1599 vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
616a6552
GK
1600 }
1601 }
1602
1603 return 0;
967f97fa
AL
1604}
1605
6a1a8cc7 1606void virtio_cleanup(VirtIODevice *vdev)
b946a153 1607{
85cf2a8d 1608 qemu_del_vm_change_state_handler(vdev->vmstate);
6f79e06b 1609 g_free(vdev->config);
7267c094 1610 g_free(vdev->vq);
e0d686bf 1611 g_free(vdev->vector_queues);
8e05db92
FK
1612}
1613
1dfb4dd9 1614static void virtio_vmstate_change(void *opaque, int running, RunState state)
85cf2a8d
MT
1615{
1616 VirtIODevice *vdev = opaque;
1c819449
FK
1617 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1618 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
85cf2a8d 1619 bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
9e8e8c48 1620 vdev->vm_running = running;
85cf2a8d
MT
1621
1622 if (backend_run) {
1623 virtio_set_status(vdev, vdev->status);
1624 }
1625
1c819449
FK
1626 if (k->vmstate_change) {
1627 k->vmstate_change(qbus->parent, backend_run);
85cf2a8d
MT
1628 }
1629
1630 if (!backend_run) {
1631 virtio_set_status(vdev, vdev->status);
1632 }
1633}
1634
c8075caf
GA
1635void virtio_instance_init_common(Object *proxy_obj, void *data,
1636 size_t vdev_size, const char *vdev_name)
1637{
1638 DeviceState *vdev = data;
1639
1640 object_initialize(vdev, vdev_size, vdev_name);
1641 object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL);
1642 object_unref(OBJECT(vdev));
1643 qdev_alias_all_properties(vdev, proxy_obj);
1644}
1645
8e05db92
FK
1646void virtio_init(VirtIODevice *vdev, const char *name,
1647 uint16_t device_id, size_t config_size)
967f97fa 1648{
e0d686bf
JW
1649 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1650 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
b8193adb 1651 int i;
e0d686bf
JW
1652 int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
1653
1654 if (nvectors) {
1655 vdev->vector_queues =
1656 g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
1657 }
1658
53c25cea 1659 vdev->device_id = device_id;
967f97fa
AL
1660 vdev->status = 0;
1661 vdev->isr = 0;
1662 vdev->queue_sel = 0;
7055e687 1663 vdev->config_vector = VIRTIO_NO_VECTOR;
87b3bd1c 1664 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
1354869c 1665 vdev->vm_running = runstate_is_running();
87b3bd1c 1666 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
b8193adb 1667 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1cbdabe2 1668 vdev->vq[i].vdev = vdev;
e78a2b42 1669 vdev->vq[i].queue_index = i;
1cbdabe2 1670 }
967f97fa 1671
967f97fa
AL
1672 vdev->name = name;
1673 vdev->config_len = config_size;
8e05db92 1674 if (vdev->config_len) {
7267c094 1675 vdev->config = g_malloc0(config_size);
8e05db92 1676 } else {
967f97fa 1677 vdev->config = NULL;
8e05db92
FK
1678 }
1679 vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
1680 vdev);
616a6552 1681 vdev->device_endian = virtio_default_endian();
5669655a 1682 vdev->use_guest_notifier_mask = true;
8e05db92 1683}
967f97fa 1684
a8170e5e 1685hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1686{
1687 return vdev->vq[n].vring.desc;
1688}
1689
a8170e5e 1690hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1691{
1692 return vdev->vq[n].vring.avail;
1693}
1694
a8170e5e 1695hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1696{
1697 return vdev->vq[n].vring.used;
1698}
1699
a8170e5e 1700hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1701{
1702 return vdev->vq[n].vring.desc;
1703}
1704
a8170e5e 1705hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1706{
1707 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
1708}
1709
a8170e5e 1710hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1711{
1712 return offsetof(VRingAvail, ring) +
50764fc8 1713 sizeof(uint16_t) * vdev->vq[n].vring.num;
1cbdabe2
MT
1714}
1715
a8170e5e 1716hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1717{
1718 return offsetof(VRingUsed, ring) +
1719 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
1720}
1721
a8170e5e 1722hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1723{
1724 return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
1725 virtio_queue_get_used_size(vdev, n);
1726}
1727
1728uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
1729{
1730 return vdev->vq[n].last_avail_idx;
1731}
1732
1733void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
1734{
1735 vdev->vq[n].last_avail_idx = idx;
be1fea9b 1736 vdev->vq[n].shadow_avail_idx = idx;
1cbdabe2
MT
1737}
1738
6793dfd1
SH
1739void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
1740{
1741 vdev->vq[n].signalled_used_valid = false;
1742}
1743
1cbdabe2
MT
1744VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
1745{
1746 return vdev->vq + n;
1747}
1748
e78a2b42
JW
1749uint16_t virtio_get_queue_index(VirtQueue *vq)
1750{
1751 return vq->queue_index;
1752}
1753
15b2bd18
PB
1754static void virtio_queue_guest_notifier_read(EventNotifier *n)
1755{
1756 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
1757 if (event_notifier_test_and_clear(n)) {
1758 virtio_irq(vq);
1759 }
1760}
1761
1762void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
1763 bool with_irqfd)
1764{
1765 if (assign && !with_irqfd) {
1766 event_notifier_set_handler(&vq->guest_notifier,
1767 virtio_queue_guest_notifier_read);
1768 } else {
1769 event_notifier_set_handler(&vq->guest_notifier, NULL);
1770 }
1771 if (!assign) {
1772 /* Test and clear notifier before closing it,
1773 * in case poll callback didn't have time to run. */
1774 virtio_queue_guest_notifier_read(&vq->guest_notifier);
1775 }
1776}
1777
1cbdabe2
MT
1778EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
1779{
1780 return &vq->guest_notifier;
1781}
b1f416aa
PB
1782
1783static void virtio_queue_host_notifier_read(EventNotifier *n)
1784{
1785 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
1786 if (event_notifier_test_and_clear(n)) {
1787 virtio_queue_notify_vq(vq);
1788 }
1789}
1790
a1afb606
PB
1791void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
1792 bool assign, bool set_handler)
1793{
1794 if (assign && set_handler) {
1795 aio_set_event_notifier(ctx, &vq->host_notifier, true,
1796 virtio_queue_host_notifier_read);
1797 } else {
1798 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL);
1799 }
1800 if (!assign) {
1801 /* Test and clear notifier before after disabling event,
1802 * in case poll callback didn't have time to run. */
1803 virtio_queue_host_notifier_read(&vq->host_notifier);
1804 }
1805}
1806
26b9b5fe
PB
1807void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
1808 bool set_handler)
b1f416aa 1809{
26b9b5fe 1810 if (assign && set_handler) {
b1f416aa
PB
1811 event_notifier_set_handler(&vq->host_notifier,
1812 virtio_queue_host_notifier_read);
1813 } else {
1814 event_notifier_set_handler(&vq->host_notifier, NULL);
26b9b5fe
PB
1815 }
1816 if (!assign) {
b1f416aa
PB
1817 /* Test and clear notifier before after disabling event,
1818 * in case poll callback didn't have time to run. */
1819 virtio_queue_host_notifier_read(&vq->host_notifier);
1820 }
1821}
1822
1cbdabe2
MT
1823EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
1824{
1825 return &vq->host_notifier;
1826}
8e05db92 1827
1034e9cf
FK
1828void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
1829{
9e288406 1830 g_free(vdev->bus_name);
80e0090a 1831 vdev->bus_name = g_strdup(bus_name);
1034e9cf
FK
1832}
1833
1d244b42
AF
1834static void virtio_device_realize(DeviceState *dev, Error **errp)
1835{
1836 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1837 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1838 Error *err = NULL;
1839
1d244b42
AF
1840 if (vdc->realize != NULL) {
1841 vdc->realize(dev, &err);
1842 if (err != NULL) {
1843 error_propagate(errp, err);
1844 return;
1845 }
8e05db92 1846 }
e8398045
JW
1847
1848 virtio_bus_device_plugged(vdev, &err);
1849 if (err != NULL) {
1850 error_propagate(errp, err);
1851 return;
1852 }
8e05db92
FK
1853}
1854
1d244b42 1855static void virtio_device_unrealize(DeviceState *dev, Error **errp)
1034e9cf 1856{
1d244b42 1857 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
306ec6c3
AF
1858 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1859 Error *err = NULL;
1d244b42 1860
83d07047
PB
1861 virtio_bus_device_unplugged(vdev);
1862
306ec6c3
AF
1863 if (vdc->unrealize != NULL) {
1864 vdc->unrealize(dev, &err);
1865 if (err != NULL) {
1866 error_propagate(errp, err);
1867 return;
1868 }
5e96f5d2 1869 }
1d244b42 1870
9e288406
MA
1871 g_free(vdev->bus_name);
1872 vdev->bus_name = NULL;
1034e9cf
FK
1873}
1874
6b8f1020
CH
1875static Property virtio_properties[] = {
1876 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
1877 DEFINE_PROP_END_OF_LIST(),
1878};
1879
8e05db92
FK
1880static void virtio_device_class_init(ObjectClass *klass, void *data)
1881{
1882 /* Set the default value here. */
1883 DeviceClass *dc = DEVICE_CLASS(klass);
1d244b42
AF
1884
1885 dc->realize = virtio_device_realize;
1886 dc->unrealize = virtio_device_unrealize;
8e05db92 1887 dc->bus_type = TYPE_VIRTIO_BUS;
6b8f1020 1888 dc->props = virtio_properties;
8e05db92
FK
1889}
1890
1891static const TypeInfo virtio_device_info = {
1892 .name = TYPE_VIRTIO_DEVICE,
1893 .parent = TYPE_DEVICE,
1894 .instance_size = sizeof(VirtIODevice),
1895 .class_init = virtio_device_class_init,
1896 .abstract = true,
1897 .class_size = sizeof(VirtioDeviceClass),
1898};
1899
1900static void virtio_register_types(void)
1901{
1902 type_register_static(&virtio_device_info);
1903}
1904
1905type_init(virtio_register_types)
This page took 1.097367 seconds and 4 git commands to generate.