4 * Copyright (c) 2017-2018 Virtuozzo International GmbH.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu/error-report.h"
12 #include "qemu/main-loop.h"
13 #include "qapi/error.h"
14 #include "migration/vmstate.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/hyperv/hyperv.h"
17 #include "hw/hyperv/vmbus.h"
18 #include "hw/hyperv/vmbus-bridge.h"
19 #include "hw/sysbus.h"
33 /* associated channel id (rudimentary?) */
36 /* number of pages in the GPADL as declared in GPADL_HEADER message */
39 * Due to limited message size, GPADL may not fit fully in a single
40 * GPADL_HEADER message, and is further popluated using GPADL_BODY
41 * messages. @seen_gfns is the number of pages seen so far; once it
42 * reaches @num_gfns, the GPADL is ready to use.
45 /* array of GFNs (of size @num_gfns once allocated) */
50 QTAILQ_ENTRY(VMBusGpadl
) link
;
56 * Wrap sequential read from / write to GPADL.
58 typedef struct GpadlIter
{
62 /* offset into GPADL where the next i/o will be performed */
65 * Cached mapping of the currently accessed page, up to page boundary.
66 * Updated lazily on i/o.
67 * Note: MemoryRegionCache can not be used here because pages in the GPADL
68 * are non-contiguous and may belong to different memory regions.
71 /* offset after last i/o (i.e. not affected by seek) */
74 * Indicator that the iterator is active and may have a cached mapping.
75 * Allows to enforce bracketing of all i/o (which may create cached
76 * mappings) and thus exclude mapping leaks.
82 * Ring buffer. There are two of them, sitting in the same GPADL, for each
84 * Each ring buffer consists of a set of pages, with the first page containing
85 * the ring buffer header, and the remaining pages being for data packets.
87 typedef struct VMBusRingBufCommon
{
89 /* GPA of the ring buffer header */
91 /* start and length of the ring buffer data area within GPADL */
98 typedef struct VMBusSendRingBuf
{
99 VMBusRingBufCommon common
;
100 /* current write index, to be committed at the end of send */
102 /* write index at the start of send */
103 uint32_t last_wr_idx
;
104 /* space to be requested from the guest */
106 /* space reserved for planned sends */
108 /* last seen read index */
109 uint32_t last_seen_rd_idx
;
112 typedef struct VMBusRecvRingBuf
{
113 VMBusRingBufCommon common
;
114 /* current read index, to be committed at the end of receive */
116 /* read index at the start of receive */
117 uint32_t last_rd_idx
;
118 /* last seen write index */
119 uint32_t last_seen_wr_idx
;
135 struct VMBusChannel
{
141 * subchannel index within the device; subchannel #0 is "primary" and
144 uint16_t subchan_idx
;
146 /* VP_INDEX of the vCPU to notify with (synthetic) interrupts */
148 /* GPADL id to use for the ring buffers */
149 uint32_t ringbuf_gpadl
;
150 /* start (in pages) of the send ring buffer within @ringbuf_gpadl */
151 uint32_t ringbuf_send_offset
;
157 /* main device worker; copied from the device class */
158 VMBusChannelNotifyCb notify_cb
;
160 * guest->host notifications, either sent directly or dispatched via
161 * interrupt page (older VMBus)
163 EventNotifier notifier
;
167 * SINT route to signal with host->guest notifications; may be shared with
168 * the main VMBus SINT route
170 HvSintRoute
*notify_route
;
173 VMBusSendRingBuf send_ringbuf
;
174 VMBusRecvRingBuf recv_ringbuf
;
176 QTAILQ_ENTRY(VMBusChannel
) link
;
180 * Hyper-V spec mandates that every message port has 16 buffers, which means
181 * that the guest can post up to this many messages without blocking.
182 * Therefore a queue for incoming messages has to be provided.
183 * For outgoing (i.e. host->guest) messages there's no queue; the VMBus just
184 * doesn't transition to a new state until the message is known to have been
185 * successfully delivered to the respective SynIC message slot.
187 #define HV_MSG_QUEUE_LEN 16
189 /* Hyper-V devices never use channel #0. Must be something special. */
190 #define VMBUS_FIRST_CHANID 1
191 /* Each channel occupies one bit within a single event page sint slot. */
192 #define VMBUS_CHANID_COUNT (HV_EVENT_FLAGS_COUNT - VMBUS_FIRST_CHANID)
193 /* Leave a few connection numbers for other purposes. */
194 #define VMBUS_CHAN_CONNECTION_OFFSET 16
197 * Since the success or failure of sending a message is reported
198 * asynchronously, the VMBus state machine has effectively two entry points:
199 * vmbus_run and vmbus_msg_cb (the latter is called when the host->guest
200 * message delivery status becomes known). Both are run as oneshot BHs on the
201 * main aio context, ensuring serialization.
208 VMBUS_TEARDOWN_GPADL
,
218 /* protection against recursive aio_poll (see vmbus_run) */
220 /* whether there's a message being delivered to the guest */
221 bool msg_in_progress
;
223 /* VP_INDEX of the vCPU to send messages and interrupts to */
225 HvSintRoute
*sint_route
;
227 * interrupt page for older protocol versions; newer ones use SynIC event
232 DECLARE_BITMAP(chanid_bitmap
, VMBUS_CHANID_COUNT
);
234 /* incoming message queue */
235 struct hyperv_post_message_input rx_queue
[HV_MSG_QUEUE_LEN
];
236 uint8_t rx_queue_head
;
237 uint8_t rx_queue_size
;
238 QemuMutex rx_queue_lock
;
240 QTAILQ_HEAD(, VMBusGpadl
) gpadl_list
;
241 QTAILQ_HEAD(, VMBusChannel
) channel_list
;
244 * guest->host notifications for older VMBus, to be dispatched via
247 EventNotifier notifier
;
250 static bool gpadl_full(VMBusGpadl
*gpadl
)
252 return gpadl
->seen_gfns
== gpadl
->num_gfns
;
255 static VMBusGpadl
*create_gpadl(VMBus
*vmbus
, uint32_t id
,
256 uint32_t child_relid
, uint32_t num_gfns
)
258 VMBusGpadl
*gpadl
= g_new0(VMBusGpadl
, 1);
261 gpadl
->child_relid
= child_relid
;
262 gpadl
->num_gfns
= num_gfns
;
263 gpadl
->gfns
= g_new(uint64_t, num_gfns
);
264 QTAILQ_INSERT_HEAD(&vmbus
->gpadl_list
, gpadl
, link
);
265 gpadl
->vmbus
= vmbus
;
270 static void free_gpadl(VMBusGpadl
*gpadl
)
272 QTAILQ_REMOVE(&gpadl
->vmbus
->gpadl_list
, gpadl
, link
);
277 static VMBusGpadl
*find_gpadl(VMBus
*vmbus
, uint32_t gpadl_id
)
280 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
281 if (gpadl
->id
== gpadl_id
) {
288 VMBusGpadl
*vmbus_get_gpadl(VMBusChannel
*chan
, uint32_t gpadl_id
)
290 VMBusGpadl
*gpadl
= find_gpadl(chan
->vmbus
, gpadl_id
);
291 if (!gpadl
|| !gpadl_full(gpadl
)) {
298 void vmbus_put_gpadl(VMBusGpadl
*gpadl
)
303 if (--gpadl
->refcount
) {
309 uint32_t vmbus_gpadl_len(VMBusGpadl
*gpadl
)
311 return gpadl
->num_gfns
* TARGET_PAGE_SIZE
;
314 static void gpadl_iter_init(GpadlIter
*iter
, VMBusGpadl
*gpadl
,
315 AddressSpace
*as
, DMADirection dir
)
320 iter
->active
= false;
323 static inline void gpadl_iter_cache_unmap(GpadlIter
*iter
)
325 uint32_t map_start_in_page
= (uintptr_t)iter
->map
& ~TARGET_PAGE_MASK
;
326 uint32_t io_end_in_page
= ((iter
->last_off
- 1) & ~TARGET_PAGE_MASK
) + 1;
328 /* mapping is only done to do non-zero amount of i/o */
329 assert(iter
->last_off
> 0);
330 assert(map_start_in_page
< io_end_in_page
);
332 dma_memory_unmap(iter
->as
, iter
->map
, TARGET_PAGE_SIZE
- map_start_in_page
,
333 iter
->dir
, io_end_in_page
- map_start_in_page
);
337 * Copy exactly @len bytes between the GPADL pointed to by @iter and @buf.
338 * The direction of the copy is determined by @iter->dir.
339 * The caller must ensure the operation overflows neither @buf nor the GPADL
340 * (there's an assert for the latter).
341 * Reuse the currently mapped page in the GPADL if possible.
343 static ssize_t
gpadl_iter_io(GpadlIter
*iter
, void *buf
, uint32_t len
)
347 assert(iter
->active
);
350 uint32_t off_in_page
= iter
->off
& ~TARGET_PAGE_MASK
;
351 uint32_t pgleft
= TARGET_PAGE_SIZE
- off_in_page
;
352 uint32_t cplen
= MIN(pgleft
, len
);
355 /* try to reuse the cached mapping */
357 uint32_t map_start_in_page
=
358 (uintptr_t)iter
->map
& ~TARGET_PAGE_MASK
;
359 uint32_t off_base
= iter
->off
& ~TARGET_PAGE_MASK
;
360 uint32_t mapped_base
= (iter
->last_off
- 1) & ~TARGET_PAGE_MASK
;
361 if (off_base
!= mapped_base
|| off_in_page
< map_start_in_page
) {
362 gpadl_iter_cache_unmap(iter
);
369 dma_addr_t mlen
= pgleft
;
370 uint32_t idx
= iter
->off
>> TARGET_PAGE_BITS
;
371 assert(idx
< iter
->gpadl
->num_gfns
);
373 maddr
= (iter
->gpadl
->gfns
[idx
] << TARGET_PAGE_BITS
) | off_in_page
;
375 iter
->map
= dma_memory_map(iter
->as
, maddr
, &mlen
, iter
->dir
);
376 if (mlen
!= pgleft
) {
377 dma_memory_unmap(iter
->as
, iter
->map
, mlen
, iter
->dir
, 0);
383 p
= (void *)(((uintptr_t)iter
->map
& TARGET_PAGE_MASK
) | off_in_page
);
384 if (iter
->dir
== DMA_DIRECTION_FROM_DEVICE
) {
385 memcpy(p
, buf
, cplen
);
387 memcpy(buf
, p
, cplen
);
393 iter
->last_off
= iter
->off
;
400 * Position the iterator @iter at new offset @new_off.
401 * If this results in the cached mapping being unusable with the new offset,
404 static inline void gpadl_iter_seek(GpadlIter
*iter
, uint32_t new_off
)
406 assert(iter
->active
);
411 * Start a series of i/o on the GPADL.
412 * After this i/o and seek operations on @iter become legal.
414 static inline void gpadl_iter_start_io(GpadlIter
*iter
)
416 assert(!iter
->active
);
417 /* mapping is cached lazily on i/o */
423 * End the eariler started series of i/o on the GPADL and release the cached
426 static inline void gpadl_iter_end_io(GpadlIter
*iter
)
428 assert(iter
->active
);
431 gpadl_iter_cache_unmap(iter
);
434 iter
->active
= false;
437 static void vmbus_resched(VMBus
*vmbus
);
438 static void vmbus_msg_cb(void *data
, int status
);
440 ssize_t
vmbus_iov_to_gpadl(VMBusChannel
*chan
, VMBusGpadl
*gpadl
, uint32_t off
,
441 const struct iovec
*iov
, size_t iov_cnt
)
447 gpadl_iter_init(&iter
, gpadl
, chan
->dev
->dma_as
,
448 DMA_DIRECTION_FROM_DEVICE
);
449 gpadl_iter_start_io(&iter
);
450 gpadl_iter_seek(&iter
, off
);
451 for (i
= 0; i
< iov_cnt
; i
++) {
452 ret
= gpadl_iter_io(&iter
, iov
[i
].iov_base
, iov
[i
].iov_len
);
458 gpadl_iter_end_io(&iter
);
462 int vmbus_map_sgl(VMBusChanReq
*req
, DMADirection dir
, struct iovec
*iov
,
463 unsigned iov_cnt
, size_t len
, size_t off
)
465 int ret_cnt
= 0, ret
;
467 QEMUSGList
*sgl
= &req
->sgl
;
468 ScatterGatherEntry
*sg
= sgl
->sg
;
470 for (i
= 0; i
< sgl
->nsg
; i
++) {
471 if (sg
[i
].len
> off
) {
476 for (; len
&& i
< sgl
->nsg
; i
++) {
477 dma_addr_t mlen
= MIN(sg
[i
].len
- off
, len
);
478 dma_addr_t addr
= sg
[i
].base
+ off
;
482 for (; mlen
; ret_cnt
++) {
486 if (ret_cnt
== iov_cnt
) {
491 iov
[ret_cnt
].iov_base
= dma_memory_map(sgl
->as
, a
, &l
, dir
);
496 iov
[ret_cnt
].iov_len
= l
;
504 vmbus_unmap_sgl(req
, dir
, iov
, ret_cnt
, 0);
508 void vmbus_unmap_sgl(VMBusChanReq
*req
, DMADirection dir
, struct iovec
*iov
,
509 unsigned iov_cnt
, size_t accessed
)
511 QEMUSGList
*sgl
= &req
->sgl
;
514 for (i
= 0; i
< iov_cnt
; i
++) {
515 size_t acsd
= MIN(accessed
, iov
[i
].iov_len
);
516 dma_memory_unmap(sgl
->as
, iov
[i
].iov_base
, iov
[i
].iov_len
, dir
, acsd
);
521 static const VMStateDescription vmstate_gpadl
= {
522 .name
= "vmbus/gpadl",
524 .minimum_version_id
= 0,
525 .fields
= (VMStateField
[]) {
526 VMSTATE_UINT32(id
, VMBusGpadl
),
527 VMSTATE_UINT32(child_relid
, VMBusGpadl
),
528 VMSTATE_UINT32(num_gfns
, VMBusGpadl
),
529 VMSTATE_UINT32(seen_gfns
, VMBusGpadl
),
530 VMSTATE_VARRAY_UINT32_ALLOC(gfns
, VMBusGpadl
, num_gfns
, 0,
531 vmstate_info_uint64
, uint64_t),
532 VMSTATE_UINT8(state
, VMBusGpadl
),
533 VMSTATE_END_OF_LIST()
538 * Wrap the index into a ring buffer of @len bytes.
539 * @idx is assumed not to exceed twice the size of the ringbuffer, so only
540 * single wraparound is considered.
542 static inline uint32_t rb_idx_wrap(uint32_t idx
, uint32_t len
)
551 * Circular difference between two indices into a ring buffer of @len bytes.
552 * @allow_catchup - whether @idx1 may catch up @idx2; e.g. read index may catch
553 * up write index but not vice versa.
555 static inline uint32_t rb_idx_delta(uint32_t idx1
, uint32_t idx2
, uint32_t len
,
558 return rb_idx_wrap(idx2
+ len
- idx1
- !allow_catchup
, len
);
561 static vmbus_ring_buffer
*ringbuf_map_hdr(VMBusRingBufCommon
*ringbuf
)
563 vmbus_ring_buffer
*rb
;
564 dma_addr_t mlen
= sizeof(*rb
);
566 rb
= dma_memory_map(ringbuf
->as
, ringbuf
->rb_addr
, &mlen
,
567 DMA_DIRECTION_FROM_DEVICE
);
568 if (mlen
!= sizeof(*rb
)) {
569 dma_memory_unmap(ringbuf
->as
, rb
, mlen
,
570 DMA_DIRECTION_FROM_DEVICE
, 0);
576 static void ringbuf_unmap_hdr(VMBusRingBufCommon
*ringbuf
,
577 vmbus_ring_buffer
*rb
, bool dirty
)
581 dma_memory_unmap(ringbuf
->as
, rb
, sizeof(*rb
), DMA_DIRECTION_FROM_DEVICE
,
582 dirty
? sizeof(*rb
) : 0);
585 static void ringbuf_init_common(VMBusRingBufCommon
*ringbuf
, VMBusGpadl
*gpadl
,
586 AddressSpace
*as
, DMADirection dir
,
587 uint32_t begin
, uint32_t end
)
590 ringbuf
->rb_addr
= gpadl
->gfns
[begin
] << TARGET_PAGE_BITS
;
591 ringbuf
->base
= (begin
+ 1) << TARGET_PAGE_BITS
;
592 ringbuf
->len
= (end
- begin
- 1) << TARGET_PAGE_BITS
;
593 gpadl_iter_init(&ringbuf
->iter
, gpadl
, as
, dir
);
596 static int ringbufs_init(VMBusChannel
*chan
)
598 vmbus_ring_buffer
*rb
;
599 VMBusSendRingBuf
*send_ringbuf
= &chan
->send_ringbuf
;
600 VMBusRecvRingBuf
*recv_ringbuf
= &chan
->recv_ringbuf
;
602 if (chan
->ringbuf_send_offset
<= 1 ||
603 chan
->gpadl
->num_gfns
<= chan
->ringbuf_send_offset
+ 1) {
607 ringbuf_init_common(&recv_ringbuf
->common
, chan
->gpadl
, chan
->dev
->dma_as
,
608 DMA_DIRECTION_TO_DEVICE
, 0, chan
->ringbuf_send_offset
);
609 ringbuf_init_common(&send_ringbuf
->common
, chan
->gpadl
, chan
->dev
->dma_as
,
610 DMA_DIRECTION_FROM_DEVICE
, chan
->ringbuf_send_offset
,
611 chan
->gpadl
->num_gfns
);
612 send_ringbuf
->wanted
= 0;
613 send_ringbuf
->reserved
= 0;
615 rb
= ringbuf_map_hdr(&recv_ringbuf
->common
);
619 recv_ringbuf
->rd_idx
= recv_ringbuf
->last_rd_idx
= rb
->read_index
;
620 ringbuf_unmap_hdr(&recv_ringbuf
->common
, rb
, false);
622 rb
= ringbuf_map_hdr(&send_ringbuf
->common
);
626 send_ringbuf
->wr_idx
= send_ringbuf
->last_wr_idx
= rb
->write_index
;
627 send_ringbuf
->last_seen_rd_idx
= rb
->read_index
;
628 rb
->feature_bits
|= VMBUS_RING_BUFFER_FEAT_PENDING_SZ
;
629 ringbuf_unmap_hdr(&send_ringbuf
->common
, rb
, true);
631 if (recv_ringbuf
->rd_idx
>= recv_ringbuf
->common
.len
||
632 send_ringbuf
->wr_idx
>= send_ringbuf
->common
.len
) {
640 * Perform io between the GPADL-backed ringbuffer @ringbuf and @buf, wrapping
642 * @len is assumed not to exceed the size of the ringbuffer, so only single
643 * wraparound is considered.
645 static ssize_t
ringbuf_io(VMBusRingBufCommon
*ringbuf
, void *buf
, uint32_t len
)
647 ssize_t ret1
= 0, ret2
= 0;
648 uint32_t remain
= ringbuf
->len
+ ringbuf
->base
- ringbuf
->iter
.off
;
651 ret1
= gpadl_iter_io(&ringbuf
->iter
, buf
, remain
);
655 gpadl_iter_seek(&ringbuf
->iter
, ringbuf
->base
);
659 ret2
= gpadl_iter_io(&ringbuf
->iter
, buf
, len
);
667 * Position the circular iterator within @ringbuf to offset @new_off, wrapping
669 * @new_off is assumed not to exceed twice the size of the ringbuffer, so only
670 * single wraparound is considered.
672 static inline void ringbuf_seek(VMBusRingBufCommon
*ringbuf
, uint32_t new_off
)
674 gpadl_iter_seek(&ringbuf
->iter
,
675 ringbuf
->base
+ rb_idx_wrap(new_off
, ringbuf
->len
));
678 static inline uint32_t ringbuf_tell(VMBusRingBufCommon
*ringbuf
)
680 return ringbuf
->iter
.off
- ringbuf
->base
;
683 static inline void ringbuf_start_io(VMBusRingBufCommon
*ringbuf
)
685 gpadl_iter_start_io(&ringbuf
->iter
);
688 static inline void ringbuf_end_io(VMBusRingBufCommon
*ringbuf
)
690 gpadl_iter_end_io(&ringbuf
->iter
);
693 VMBusDevice
*vmbus_channel_device(VMBusChannel
*chan
)
698 VMBusChannel
*vmbus_device_channel(VMBusDevice
*dev
, uint32_t chan_idx
)
700 if (chan_idx
>= dev
->num_channels
) {
703 return &dev
->channels
[chan_idx
];
706 uint32_t vmbus_channel_idx(VMBusChannel
*chan
)
708 return chan
- chan
->dev
->channels
;
711 void vmbus_channel_notify_host(VMBusChannel
*chan
)
713 event_notifier_set(&chan
->notifier
);
716 bool vmbus_channel_is_open(VMBusChannel
*chan
)
718 return chan
->is_open
;
722 * Notify the guest side about the data to work on in the channel ring buffer.
723 * The notification is done by signaling a dedicated per-channel SynIC event
724 * flag (more recent guests) or setting a bit in the interrupt page and firing
725 * the VMBus SINT (older guests).
727 static int vmbus_channel_notify_guest(VMBusChannel
*chan
)
730 unsigned long *int_map
, mask
;
732 hwaddr addr
= chan
->vmbus
->int_page_gpa
;
733 hwaddr len
= TARGET_PAGE_SIZE
/ 2, dirty
= 0;
735 trace_vmbus_channel_notify_guest(chan
->id
);
738 return hyperv_set_event_flag(chan
->notify_route
, chan
->id
);
741 int_map
= cpu_physical_memory_map(addr
, &len
, 1);
742 if (len
!= TARGET_PAGE_SIZE
/ 2) {
747 idx
= BIT_WORD(chan
->id
);
748 mask
= BIT_MASK(chan
->id
);
749 if ((atomic_fetch_or(&int_map
[idx
], mask
) & mask
) != mask
) {
750 res
= hyperv_sint_route_set_sint(chan
->notify_route
);
755 cpu_physical_memory_unmap(int_map
, len
, 1, dirty
);
759 #define VMBUS_PKT_TRAILER sizeof(uint64_t)
761 static uint32_t vmbus_pkt_hdr_set_offsets(vmbus_packet_hdr
*hdr
,
762 uint32_t desclen
, uint32_t msglen
)
764 hdr
->offset_qwords
= sizeof(*hdr
) / sizeof(uint64_t) +
765 DIV_ROUND_UP(desclen
, sizeof(uint64_t));
766 hdr
->len_qwords
= hdr
->offset_qwords
+
767 DIV_ROUND_UP(msglen
, sizeof(uint64_t));
768 return hdr
->len_qwords
* sizeof(uint64_t) + VMBUS_PKT_TRAILER
;
772 * Simplified ring buffer operation with paired barriers annotations in the
773 * producer and consumer loops:
775 * producer * consumer
776 * ~~~~~~~~ * ~~~~~~~~
777 * write pending_send_sz * read write_index
778 * smp_mb [A] * smp_mb [C]
779 * read read_index * read packet
780 * smp_mb [B] * read/write out-of-band data
781 * read/write out-of-band data * smp_mb [B]
782 * write packet * write read_index
783 * smp_mb [C] * smp_mb [A]
784 * write write_index * read pending_send_sz
785 * smp_wmb [D] * smp_rmb [D]
786 * write pending_send_sz * read write_index
790 static inline uint32_t ringbuf_send_avail(VMBusSendRingBuf
*ringbuf
)
792 /* don't trust guest data */
793 if (ringbuf
->last_seen_rd_idx
>= ringbuf
->common
.len
) {
796 return rb_idx_delta(ringbuf
->wr_idx
, ringbuf
->last_seen_rd_idx
,
797 ringbuf
->common
.len
, false);
800 static ssize_t
ringbuf_send_update_idx(VMBusChannel
*chan
)
802 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
803 vmbus_ring_buffer
*rb
;
806 written
= rb_idx_delta(ringbuf
->last_wr_idx
, ringbuf
->wr_idx
,
807 ringbuf
->common
.len
, true);
812 rb
= ringbuf_map_hdr(&ringbuf
->common
);
817 ringbuf
->reserved
-= written
;
819 /* prevent reorder with the data operation and packet write */
820 smp_mb(); /* barrier pair [C] */
821 rb
->write_index
= ringbuf
->wr_idx
;
824 * If the producer earlier indicated that it wants to be notified when the
825 * consumer frees certain amount of space in the ring buffer, that amount
826 * is reduced by the size of the completed write.
828 if (ringbuf
->wanted
) {
829 /* otherwise reservation would fail */
830 assert(ringbuf
->wanted
< written
);
831 ringbuf
->wanted
-= written
;
832 /* prevent reorder with write_index write */
833 smp_wmb(); /* barrier pair [D] */
834 rb
->pending_send_sz
= ringbuf
->wanted
;
837 /* prevent reorder with write_index or pending_send_sz write */
838 smp_mb(); /* barrier pair [A] */
839 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
842 * The consumer may have missed the reduction of pending_send_sz and skip
843 * notification, so re-check the blocking condition, and, if it's no longer
844 * true, ensure processing another iteration by simulating consumer's
847 if (ringbuf_send_avail(ringbuf
) >= ringbuf
->wanted
) {
848 vmbus_channel_notify_host(chan
);
851 /* skip notification by consumer's request */
852 if (rb
->interrupt_mask
) {
857 * The consumer hasn't caught up with the producer's previous state so it's
859 * (last_seen_rd_idx comes from the guest but it's safe to use w/o
860 * validation here as it only affects notification.)
862 if (rb_idx_delta(ringbuf
->last_seen_rd_idx
, ringbuf
->wr_idx
,
863 ringbuf
->common
.len
, true) > written
) {
867 vmbus_channel_notify_guest(chan
);
869 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, true);
870 ringbuf
->last_wr_idx
= ringbuf
->wr_idx
;
874 int vmbus_channel_reserve(VMBusChannel
*chan
,
875 uint32_t desclen
, uint32_t msglen
)
877 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
878 vmbus_ring_buffer
*rb
= NULL
;
879 vmbus_packet_hdr hdr
;
880 uint32_t needed
= ringbuf
->reserved
+
881 vmbus_pkt_hdr_set_offsets(&hdr
, desclen
, msglen
);
883 /* avoid touching the guest memory if possible */
884 if (likely(needed
<= ringbuf_send_avail(ringbuf
))) {
888 rb
= ringbuf_map_hdr(&ringbuf
->common
);
893 /* fetch read index from guest memory and try again */
894 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
896 if (likely(needed
<= ringbuf_send_avail(ringbuf
))) {
900 rb
->pending_send_sz
= needed
;
903 * The consumer may have made progress and freed up some space before
904 * seeing updated pending_send_sz, so re-read read_index (preventing
905 * reorder with the pending_send_sz write) and try again.
907 smp_mb(); /* barrier pair [A] */
908 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
910 if (needed
> ringbuf_send_avail(ringbuf
)) {
915 ringbuf
->reserved
= needed
;
918 /* clear pending_send_sz if it was set */
919 if (ringbuf
->wanted
) {
921 rb
= ringbuf_map_hdr(&ringbuf
->common
);
923 /* failure to clear pending_send_sz is non-fatal */
928 rb
->pending_send_sz
= 0;
931 /* prevent reorder of the following data operation with read_index read */
932 smp_mb(); /* barrier pair [B] */
936 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, ringbuf
->wanted
== needed
);
938 ringbuf
->wanted
= needed
;
939 return needed
? -ENOSPC
: 0;
942 ssize_t
vmbus_channel_send(VMBusChannel
*chan
, uint16_t pkt_type
,
943 void *desc
, uint32_t desclen
,
944 void *msg
, uint32_t msglen
,
945 bool need_comp
, uint64_t transaction_id
)
948 vmbus_packet_hdr hdr
;
950 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
952 if (!vmbus_channel_is_open(chan
)) {
956 totlen
= vmbus_pkt_hdr_set_offsets(&hdr
, desclen
, msglen
);
958 hdr
.flags
= need_comp
? VMBUS_PACKET_FLAG_REQUEST_COMPLETION
: 0;
959 hdr
.transaction_id
= transaction_id
;
961 assert(totlen
<= ringbuf
->reserved
);
963 ringbuf_start_io(&ringbuf
->common
);
964 ringbuf_seek(&ringbuf
->common
, ringbuf
->wr_idx
);
965 ret
= ringbuf_io(&ringbuf
->common
, &hdr
, sizeof(hdr
));
971 ret
= ringbuf_io(&ringbuf
->common
, desc
, desclen
);
975 ringbuf_seek(&ringbuf
->common
,
976 ringbuf
->wr_idx
+ hdr
.offset_qwords
* sizeof(uint64_t));
978 ret
= ringbuf_io(&ringbuf
->common
, msg
, msglen
);
982 ringbuf_seek(&ringbuf
->common
, ringbuf
->wr_idx
+ totlen
);
983 ringbuf
->wr_idx
= ringbuf_tell(&ringbuf
->common
);
986 ringbuf_end_io(&ringbuf
->common
);
990 return ringbuf_send_update_idx(chan
);
993 ssize_t
vmbus_channel_send_completion(VMBusChanReq
*req
,
994 void *msg
, uint32_t msglen
)
996 assert(req
->need_comp
);
997 return vmbus_channel_send(req
->chan
, VMBUS_PACKET_COMP
, NULL
, 0,
998 msg
, msglen
, false, req
->transaction_id
);
1001 static int sgl_from_gpa_ranges(QEMUSGList
*sgl
, VMBusDevice
*dev
,
1002 VMBusRingBufCommon
*ringbuf
, uint32_t len
)
1005 vmbus_pkt_gpa_direct hdr
;
1010 if (len
< sizeof(hdr
)) {
1013 ret
= ringbuf_io(ringbuf
, &hdr
, sizeof(hdr
));
1019 num
= (len
- hdr
.rangecount
* sizeof(vmbus_gpa_range
)) / sizeof(uint64_t);
1023 qemu_sglist_init(sgl
, DEVICE(dev
), num
, ringbuf
->as
);
1025 for (; hdr
.rangecount
; hdr
.rangecount
--) {
1026 vmbus_gpa_range range
;
1028 if (len
< sizeof(range
)) {
1031 ret
= ringbuf_io(ringbuf
, &range
, sizeof(range
));
1035 len
-= sizeof(range
);
1037 if (range
.byte_offset
& TARGET_PAGE_MASK
) {
1041 for (; range
.byte_count
; range
.byte_offset
= 0) {
1043 uint32_t plen
= MIN(range
.byte_count
,
1044 TARGET_PAGE_SIZE
- range
.byte_offset
);
1046 if (len
< sizeof(uint64_t)) {
1049 ret
= ringbuf_io(ringbuf
, &paddr
, sizeof(paddr
));
1053 len
-= sizeof(uint64_t);
1054 paddr
<<= TARGET_PAGE_BITS
;
1055 paddr
|= range
.byte_offset
;
1056 range
.byte_count
-= plen
;
1058 if (curaddr
+ curlen
== paddr
) {
1059 /* consecutive fragments - join */
1063 qemu_sglist_add(sgl
, curaddr
, curlen
);
1073 qemu_sglist_add(sgl
, curaddr
, curlen
);
1080 qemu_sglist_destroy(sgl
);
1084 static VMBusChanReq
*vmbus_alloc_req(VMBusChannel
*chan
,
1085 uint32_t size
, uint16_t pkt_type
,
1086 uint32_t msglen
, uint64_t transaction_id
,
1090 uint32_t msgoff
= QEMU_ALIGN_UP(size
, __alignof__(*req
->msg
));
1091 uint32_t totlen
= msgoff
+ msglen
;
1093 req
= g_malloc0(totlen
);
1095 req
->pkt_type
= pkt_type
;
1096 req
->msg
= (void *)req
+ msgoff
;
1097 req
->msglen
= msglen
;
1098 req
->transaction_id
= transaction_id
;
1099 req
->need_comp
= need_comp
;
1103 int vmbus_channel_recv_start(VMBusChannel
*chan
)
1105 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1106 vmbus_ring_buffer
*rb
;
1108 rb
= ringbuf_map_hdr(&ringbuf
->common
);
1112 ringbuf
->last_seen_wr_idx
= rb
->write_index
;
1113 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, false);
1115 if (ringbuf
->last_seen_wr_idx
>= ringbuf
->common
.len
) {
1119 /* prevent reorder of the following data operation with write_index read */
1120 smp_mb(); /* barrier pair [C] */
1124 void *vmbus_channel_recv_peek(VMBusChannel
*chan
, uint32_t size
)
1126 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1127 vmbus_packet_hdr hdr
= {};
1130 uint32_t totlen
, pktlen
, msglen
, msgoff
, desclen
;
1132 assert(size
>= sizeof(*req
));
1134 /* safe as last_seen_wr_idx is validated in vmbus_channel_recv_start */
1135 avail
= rb_idx_delta(ringbuf
->rd_idx
, ringbuf
->last_seen_wr_idx
,
1136 ringbuf
->common
.len
, true);
1137 if (avail
< sizeof(hdr
)) {
1141 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
);
1142 if (ringbuf_io(&ringbuf
->common
, &hdr
, sizeof(hdr
)) < 0) {
1146 pktlen
= hdr
.len_qwords
* sizeof(uint64_t);
1147 totlen
= pktlen
+ VMBUS_PKT_TRAILER
;
1148 if (totlen
> avail
) {
1152 msgoff
= hdr
.offset_qwords
* sizeof(uint64_t);
1153 if (msgoff
> pktlen
|| msgoff
< sizeof(hdr
)) {
1154 error_report("%s: malformed packet: %u %u", __func__
, msgoff
, pktlen
);
1158 msglen
= pktlen
- msgoff
;
1160 req
= vmbus_alloc_req(chan
, size
, hdr
.type
, msglen
, hdr
.transaction_id
,
1161 hdr
.flags
& VMBUS_PACKET_FLAG_REQUEST_COMPLETION
);
1164 case VMBUS_PACKET_DATA_USING_GPA_DIRECT
:
1165 desclen
= msgoff
- sizeof(hdr
);
1166 if (sgl_from_gpa_ranges(&req
->sgl
, chan
->dev
, &ringbuf
->common
,
1168 error_report("%s: failed to convert GPA ranges to SGL", __func__
);
1172 case VMBUS_PACKET_DATA_INBAND
:
1173 case VMBUS_PACKET_COMP
:
1176 error_report("%s: unexpected msg type: %x", __func__
, hdr
.type
);
1180 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
+ msgoff
);
1181 if (ringbuf_io(&ringbuf
->common
, req
->msg
, msglen
) < 0) {
1184 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
+ totlen
);
1188 vmbus_free_req(req
);
1192 void vmbus_channel_recv_pop(VMBusChannel
*chan
)
1194 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1195 ringbuf
->rd_idx
= ringbuf_tell(&ringbuf
->common
);
1198 ssize_t
vmbus_channel_recv_done(VMBusChannel
*chan
)
1200 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1201 vmbus_ring_buffer
*rb
;
1204 read
= rb_idx_delta(ringbuf
->last_rd_idx
, ringbuf
->rd_idx
,
1205 ringbuf
->common
.len
, true);
1210 rb
= ringbuf_map_hdr(&ringbuf
->common
);
1215 /* prevent reorder with the data operation and packet read */
1216 smp_mb(); /* barrier pair [B] */
1217 rb
->read_index
= ringbuf
->rd_idx
;
1219 /* prevent reorder of the following pending_send_sz read */
1220 smp_mb(); /* barrier pair [A] */
1222 if (rb
->interrupt_mask
) {
1226 if (rb
->feature_bits
& VMBUS_RING_BUFFER_FEAT_PENDING_SZ
) {
1227 uint32_t wr_idx
, wr_avail
;
1228 uint32_t wanted
= rb
->pending_send_sz
;
1234 /* prevent reorder with pending_send_sz read */
1235 smp_rmb(); /* barrier pair [D] */
1236 wr_idx
= rb
->write_index
;
1238 wr_avail
= rb_idx_delta(wr_idx
, ringbuf
->rd_idx
, ringbuf
->common
.len
,
1241 /* the producer wasn't blocked on the consumer state */
1242 if (wr_avail
>= read
+ wanted
) {
1245 /* there's not enough space for the producer to make progress */
1246 if (wr_avail
< wanted
) {
1251 vmbus_channel_notify_guest(chan
);
1253 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, true);
1254 ringbuf
->last_rd_idx
= ringbuf
->rd_idx
;
1258 void vmbus_free_req(void *req
)
1260 VMBusChanReq
*r
= req
;
1267 qemu_sglist_destroy(&r
->sgl
);
1272 static const VMStateDescription vmstate_sgent
= {
1273 .name
= "vmbus/sgentry",
1275 .minimum_version_id
= 0,
1276 .fields
= (VMStateField
[]) {
1277 VMSTATE_UINT64(base
, ScatterGatherEntry
),
1278 VMSTATE_UINT64(len
, ScatterGatherEntry
),
1279 VMSTATE_END_OF_LIST()
1283 typedef struct VMBusChanReqSave
{
1288 uint64_t transaction_id
;
1291 ScatterGatherEntry
*sgl
;
1294 static const VMStateDescription vmstate_vmbus_chan_req
= {
1295 .name
= "vmbus/vmbus_chan_req",
1297 .minimum_version_id
= 0,
1298 .fields
= (VMStateField
[]) {
1299 VMSTATE_UINT16(chan_idx
, VMBusChanReqSave
),
1300 VMSTATE_UINT16(pkt_type
, VMBusChanReqSave
),
1301 VMSTATE_UINT32(msglen
, VMBusChanReqSave
),
1302 VMSTATE_VBUFFER_ALLOC_UINT32(msg
, VMBusChanReqSave
, 0, NULL
, msglen
),
1303 VMSTATE_UINT64(transaction_id
, VMBusChanReqSave
),
1304 VMSTATE_BOOL(need_comp
, VMBusChanReqSave
),
1305 VMSTATE_UINT32(num
, VMBusChanReqSave
),
1306 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(sgl
, VMBusChanReqSave
, num
,
1307 vmstate_sgent
, ScatterGatherEntry
),
1308 VMSTATE_END_OF_LIST()
1312 void vmbus_save_req(QEMUFile
*f
, VMBusChanReq
*req
)
1314 VMBusChanReqSave req_save
;
1316 req_save
.chan_idx
= req
->chan
->subchan_idx
;
1317 req_save
.pkt_type
= req
->pkt_type
;
1318 req_save
.msglen
= req
->msglen
;
1319 req_save
.msg
= req
->msg
;
1320 req_save
.transaction_id
= req
->transaction_id
;
1321 req_save
.need_comp
= req
->need_comp
;
1322 req_save
.num
= req
->sgl
.nsg
;
1323 req_save
.sgl
= g_memdup(req
->sgl
.sg
,
1324 req_save
.num
* sizeof(ScatterGatherEntry
));
1326 vmstate_save_state(f
, &vmstate_vmbus_chan_req
, &req_save
, NULL
);
1328 g_free(req_save
.sgl
);
1331 void *vmbus_load_req(QEMUFile
*f
, VMBusDevice
*dev
, uint32_t size
)
1333 VMBusChanReqSave req_save
;
1334 VMBusChanReq
*req
= NULL
;
1335 VMBusChannel
*chan
= NULL
;
1338 vmstate_load_state(f
, &vmstate_vmbus_chan_req
, &req_save
, 0);
1340 if (req_save
.chan_idx
>= dev
->num_channels
) {
1341 error_report("%s: %u(chan_idx) > %u(num_channels)", __func__
,
1342 req_save
.chan_idx
, dev
->num_channels
);
1345 chan
= &dev
->channels
[req_save
.chan_idx
];
1347 if (vmbus_channel_reserve(chan
, 0, req_save
.msglen
)) {
1351 req
= vmbus_alloc_req(chan
, size
, req_save
.pkt_type
, req_save
.msglen
,
1352 req_save
.transaction_id
, req_save
.need_comp
);
1353 if (req_save
.msglen
) {
1354 memcpy(req
->msg
, req_save
.msg
, req_save
.msglen
);
1357 for (i
= 0; i
< req_save
.num
; i
++) {
1358 qemu_sglist_add(&req
->sgl
, req_save
.sgl
[i
].base
, req_save
.sgl
[i
].len
);
1362 if (req_save
.msglen
) {
1363 g_free(req_save
.msg
);
1366 g_free(req_save
.sgl
);
1371 static void channel_event_cb(EventNotifier
*e
)
1373 VMBusChannel
*chan
= container_of(e
, VMBusChannel
, notifier
);
1374 if (event_notifier_test_and_clear(e
)) {
1376 * All receives are supposed to happen within the device worker, so
1377 * bracket it with ringbuf_start/end_io on the receive ringbuffer, and
1378 * potentially reuse the cached mapping throughout the worker.
1379 * Can't do this for sends as they may happen outside the device
1382 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1383 ringbuf_start_io(&ringbuf
->common
);
1384 chan
->notify_cb(chan
);
1385 ringbuf_end_io(&ringbuf
->common
);
1390 static int alloc_chan_id(VMBus
*vmbus
)
1394 ret
= find_next_zero_bit(vmbus
->chanid_bitmap
, VMBUS_CHANID_COUNT
, 0);
1395 if (ret
== VMBUS_CHANID_COUNT
) {
1398 return ret
+ VMBUS_FIRST_CHANID
;
1401 static int register_chan_id(VMBusChannel
*chan
)
1403 return test_and_set_bit(chan
->id
- VMBUS_FIRST_CHANID
,
1404 chan
->vmbus
->chanid_bitmap
) ? -EEXIST
: 0;
1407 static void unregister_chan_id(VMBusChannel
*chan
)
1409 clear_bit(chan
->id
- VMBUS_FIRST_CHANID
, chan
->vmbus
->chanid_bitmap
);
1412 static uint32_t chan_connection_id(VMBusChannel
*chan
)
1414 return VMBUS_CHAN_CONNECTION_OFFSET
+ chan
->id
;
1417 static void init_channel(VMBus
*vmbus
, VMBusDevice
*dev
, VMBusDeviceClass
*vdc
,
1418 VMBusChannel
*chan
, uint16_t idx
, Error
**errp
)
1423 chan
->notify_cb
= vdc
->chan_notify_cb
;
1424 chan
->subchan_idx
= idx
;
1425 chan
->vmbus
= vmbus
;
1427 res
= alloc_chan_id(vmbus
);
1429 error_setg(errp
, "no spare channel id");
1433 register_chan_id(chan
);
1436 * The guest drivers depend on the device subchannels (idx #1+) to be
1437 * offered after the primary channel (idx #0) of that device. To ensure
1438 * that, record the channels on the channel list in the order they appear
1439 * within the device.
1441 QTAILQ_INSERT_TAIL(&vmbus
->channel_list
, chan
, link
);
1444 static void deinit_channel(VMBusChannel
*chan
)
1446 assert(chan
->state
== VMCHAN_INIT
);
1447 QTAILQ_REMOVE(&chan
->vmbus
->channel_list
, chan
, link
);
1448 unregister_chan_id(chan
);
1451 static void create_channels(VMBus
*vmbus
, VMBusDevice
*dev
, Error
**errp
)
1454 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(dev
);
1457 dev
->num_channels
= vdc
->num_channels
? vdc
->num_channels(dev
) : 1;
1458 if (dev
->num_channels
< 1) {
1459 error_setg(errp
, "invalid #channels: %u", dev
->num_channels
);
1463 dev
->channels
= g_new0(VMBusChannel
, dev
->num_channels
);
1464 for (i
= 0; i
< dev
->num_channels
; i
++) {
1465 init_channel(vmbus
, dev
, vdc
, &dev
->channels
[i
], i
, &err
);
1475 deinit_channel(&dev
->channels
[i
]);
1477 error_propagate(errp
, err
);
1480 static void free_channels(VMBusDevice
*dev
)
1483 for (i
= 0; i
< dev
->num_channels
; i
++) {
1484 deinit_channel(&dev
->channels
[i
]);
1486 g_free(dev
->channels
);
1489 static HvSintRoute
*make_sint_route(VMBus
*vmbus
, uint32_t vp_index
)
1493 if (vp_index
== vmbus
->target_vp
) {
1494 hyperv_sint_route_ref(vmbus
->sint_route
);
1495 return vmbus
->sint_route
;
1498 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1499 if (chan
->target_vp
== vp_index
&& vmbus_channel_is_open(chan
)) {
1500 hyperv_sint_route_ref(chan
->notify_route
);
1501 return chan
->notify_route
;
1505 return hyperv_sint_route_new(vp_index
, VMBUS_SINT
, NULL
, NULL
);
1508 static void open_channel(VMBusChannel
*chan
)
1510 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1512 chan
->gpadl
= vmbus_get_gpadl(chan
, chan
->ringbuf_gpadl
);
1517 if (ringbufs_init(chan
)) {
1521 if (event_notifier_init(&chan
->notifier
, 0)) {
1525 event_notifier_set_handler(&chan
->notifier
, channel_event_cb
);
1527 if (hyperv_set_event_flag_handler(chan_connection_id(chan
),
1529 goto cleanup_notifier
;
1532 chan
->notify_route
= make_sint_route(chan
->vmbus
, chan
->target_vp
);
1533 if (!chan
->notify_route
) {
1534 goto clear_event_flag_handler
;
1537 if (vdc
->open_channel
&& vdc
->open_channel(chan
)) {
1538 goto unref_sint_route
;
1541 chan
->is_open
= true;
1545 hyperv_sint_route_unref(chan
->notify_route
);
1546 clear_event_flag_handler
:
1547 hyperv_set_event_flag_handler(chan_connection_id(chan
), NULL
);
1549 event_notifier_set_handler(&chan
->notifier
, NULL
);
1550 event_notifier_cleanup(&chan
->notifier
);
1552 vmbus_put_gpadl(chan
->gpadl
);
1555 static void close_channel(VMBusChannel
*chan
)
1557 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1559 if (!chan
->is_open
) {
1563 if (vdc
->close_channel
) {
1564 vdc
->close_channel(chan
);
1567 hyperv_sint_route_unref(chan
->notify_route
);
1568 hyperv_set_event_flag_handler(chan_connection_id(chan
), NULL
);
1569 event_notifier_set_handler(&chan
->notifier
, NULL
);
1570 event_notifier_cleanup(&chan
->notifier
);
1571 vmbus_put_gpadl(chan
->gpadl
);
1572 chan
->is_open
= false;
1575 static int channel_post_load(void *opaque
, int version_id
)
1577 VMBusChannel
*chan
= opaque
;
1579 return register_chan_id(chan
);
1582 static const VMStateDescription vmstate_channel
= {
1583 .name
= "vmbus/channel",
1585 .minimum_version_id
= 0,
1586 .post_load
= channel_post_load
,
1587 .fields
= (VMStateField
[]) {
1588 VMSTATE_UINT32(id
, VMBusChannel
),
1589 VMSTATE_UINT16(subchan_idx
, VMBusChannel
),
1590 VMSTATE_UINT32(open_id
, VMBusChannel
),
1591 VMSTATE_UINT32(target_vp
, VMBusChannel
),
1592 VMSTATE_UINT32(ringbuf_gpadl
, VMBusChannel
),
1593 VMSTATE_UINT32(ringbuf_send_offset
, VMBusChannel
),
1594 VMSTATE_UINT8(offer_state
, VMBusChannel
),
1595 VMSTATE_UINT8(state
, VMBusChannel
),
1596 VMSTATE_END_OF_LIST()
1600 static VMBusChannel
*find_channel(VMBus
*vmbus
, uint32_t id
)
1603 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1604 if (chan
->id
== id
) {
1611 static int enqueue_incoming_message(VMBus
*vmbus
,
1612 const struct hyperv_post_message_input
*msg
)
1615 uint8_t idx
, prev_size
;
1617 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
1619 if (vmbus
->rx_queue_size
== HV_MSG_QUEUE_LEN
) {
1624 prev_size
= vmbus
->rx_queue_size
;
1625 idx
= (vmbus
->rx_queue_head
+ vmbus
->rx_queue_size
) % HV_MSG_QUEUE_LEN
;
1626 memcpy(&vmbus
->rx_queue
[idx
], msg
, sizeof(*msg
));
1627 vmbus
->rx_queue_size
++;
1629 /* only need to resched if the queue was empty before */
1631 vmbus_resched(vmbus
);
1634 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
1638 static uint16_t vmbus_recv_message(const struct hyperv_post_message_input
*msg
,
1641 VMBus
*vmbus
= data
;
1642 struct vmbus_message_header
*vmbus_msg
;
1644 if (msg
->message_type
!= HV_MESSAGE_VMBUS
) {
1645 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1648 if (msg
->payload_size
< sizeof(struct vmbus_message_header
)) {
1649 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1652 vmbus_msg
= (struct vmbus_message_header
*)msg
->payload
;
1654 trace_vmbus_recv_message(vmbus_msg
->message_type
, msg
->payload_size
);
1656 if (vmbus_msg
->message_type
== VMBUS_MSG_INVALID
||
1657 vmbus_msg
->message_type
>= VMBUS_MSG_COUNT
) {
1658 error_report("vmbus: unknown message type %#x",
1659 vmbus_msg
->message_type
);
1660 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1663 if (enqueue_incoming_message(vmbus
, msg
)) {
1664 return HV_STATUS_INSUFFICIENT_BUFFERS
;
1666 return HV_STATUS_SUCCESS
;
1669 static bool vmbus_initialized(VMBus
*vmbus
)
1671 return vmbus
->version
> 0 && vmbus
->version
<= VMBUS_VERSION_CURRENT
;
1674 static void vmbus_reset_all(VMBus
*vmbus
)
1676 qbus_reset_all(BUS(vmbus
));
1679 static void post_msg(VMBus
*vmbus
, void *msgdata
, uint32_t msglen
)
1682 struct hyperv_message msg
= {
1683 .header
.message_type
= HV_MESSAGE_VMBUS
,
1686 assert(!vmbus
->msg_in_progress
);
1687 assert(msglen
<= sizeof(msg
.payload
));
1688 assert(msglen
>= sizeof(struct vmbus_message_header
));
1690 vmbus
->msg_in_progress
= true;
1692 trace_vmbus_post_msg(((struct vmbus_message_header
*)msgdata
)->message_type
,
1695 memcpy(msg
.payload
, msgdata
, msglen
);
1696 msg
.header
.payload_size
= ROUND_UP(msglen
, VMBUS_MESSAGE_SIZE_ALIGN
);
1698 ret
= hyperv_post_msg(vmbus
->sint_route
, &msg
);
1699 if (ret
== 0 || ret
== -EAGAIN
) {
1703 error_report("message delivery fatal failure: %d; aborting vmbus", ret
);
1704 vmbus_reset_all(vmbus
);
1707 static int vmbus_init(VMBus
*vmbus
)
1709 if (vmbus
->target_vp
!= (uint32_t)-1) {
1710 vmbus
->sint_route
= hyperv_sint_route_new(vmbus
->target_vp
, VMBUS_SINT
,
1711 vmbus_msg_cb
, vmbus
);
1712 if (!vmbus
->sint_route
) {
1713 error_report("failed to set up SINT route");
1720 static void vmbus_deinit(VMBus
*vmbus
)
1722 VMBusGpadl
*gpadl
, *tmp_gpadl
;
1725 QTAILQ_FOREACH_SAFE(gpadl
, &vmbus
->gpadl_list
, link
, tmp_gpadl
) {
1726 if (gpadl
->state
== VMGPADL_TORNDOWN
) {
1729 vmbus_put_gpadl(gpadl
);
1732 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1733 chan
->offer_state
= VMOFFER_INIT
;
1736 hyperv_sint_route_unref(vmbus
->sint_route
);
1737 vmbus
->sint_route
= NULL
;
1738 vmbus
->int_page_gpa
= 0;
1739 vmbus
->target_vp
= (uint32_t)-1;
1741 vmbus
->state
= VMBUS_LISTEN
;
1742 vmbus
->msg_in_progress
= false;
1745 static void handle_initiate_contact(VMBus
*vmbus
,
1746 vmbus_message_initiate_contact
*msg
,
1749 if (msglen
< sizeof(*msg
)) {
1753 trace_vmbus_initiate_contact(msg
->version_requested
>> 16,
1754 msg
->version_requested
& 0xffff,
1755 msg
->target_vcpu
, msg
->monitor_page1
,
1756 msg
->monitor_page2
, msg
->interrupt_page
);
1759 * Reset vmbus on INITIATE_CONTACT regardless of its previous state.
1760 * Useful, in particular, with vmbus-aware BIOS which can't shut vmbus down
1761 * before handing over to OS loader.
1763 vmbus_reset_all(vmbus
);
1765 vmbus
->target_vp
= msg
->target_vcpu
;
1766 vmbus
->version
= msg
->version_requested
;
1767 if (vmbus
->version
< VMBUS_VERSION_WIN8
) {
1768 /* linux passes interrupt page even when it doesn't need it */
1769 vmbus
->int_page_gpa
= msg
->interrupt_page
;
1771 vmbus
->state
= VMBUS_HANDSHAKE
;
1773 if (vmbus_init(vmbus
)) {
1774 error_report("failed to init vmbus; aborting");
1775 vmbus_deinit(vmbus
);
1780 static void send_handshake(VMBus
*vmbus
)
1782 struct vmbus_message_version_response msg
= {
1783 .header
.message_type
= VMBUS_MSG_VERSION_RESPONSE
,
1784 .version_supported
= vmbus_initialized(vmbus
),
1787 post_msg(vmbus
, &msg
, sizeof(msg
));
1790 static void handle_request_offers(VMBus
*vmbus
, void *msgdata
, uint32_t msglen
)
1794 if (!vmbus_initialized(vmbus
)) {
1798 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1799 if (chan
->offer_state
== VMOFFER_INIT
) {
1800 chan
->offer_state
= VMOFFER_SENDING
;
1805 vmbus
->state
= VMBUS_OFFER
;
1808 static void send_offer(VMBus
*vmbus
)
1811 struct vmbus_message_header alloffers_msg
= {
1812 .message_type
= VMBUS_MSG_ALLOFFERS_DELIVERED
,
1815 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1816 if (chan
->offer_state
== VMOFFER_SENDING
) {
1817 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1818 /* Hyper-V wants LE GUIDs */
1819 QemuUUID classid
= qemu_uuid_bswap(vdc
->classid
);
1820 QemuUUID instanceid
= qemu_uuid_bswap(chan
->dev
->instanceid
);
1821 struct vmbus_message_offer_channel msg
= {
1822 .header
.message_type
= VMBUS_MSG_OFFERCHANNEL
,
1823 .child_relid
= chan
->id
,
1824 .connection_id
= chan_connection_id(chan
),
1825 .channel_flags
= vdc
->channel_flags
,
1826 .mmio_size_mb
= vdc
->mmio_size_mb
,
1827 .sub_channel_index
= vmbus_channel_idx(chan
),
1828 .interrupt_flags
= VMBUS_OFFER_INTERRUPT_DEDICATED
,
1831 memcpy(msg
.type_uuid
, &classid
, sizeof(classid
));
1832 memcpy(msg
.instance_uuid
, &instanceid
, sizeof(instanceid
));
1834 trace_vmbus_send_offer(chan
->id
, chan
->dev
);
1836 post_msg(vmbus
, &msg
, sizeof(msg
));
1841 /* no more offers, send terminator message */
1842 trace_vmbus_terminate_offers();
1843 post_msg(vmbus
, &alloffers_msg
, sizeof(alloffers_msg
));
1846 static bool complete_offer(VMBus
*vmbus
)
1850 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1851 if (chan
->offer_state
== VMOFFER_SENDING
) {
1852 chan
->offer_state
= VMOFFER_SENT
;
1857 * no transitioning channels found so this is completing the terminator
1858 * message, and vmbus can move to the next state
1863 /* try to mark another channel for offering */
1864 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1865 if (chan
->offer_state
== VMOFFER_INIT
) {
1866 chan
->offer_state
= VMOFFER_SENDING
;
1871 * if an offer has been sent there are more offers or the terminator yet to
1872 * send, so no state transition for vmbus
1878 static void handle_gpadl_header(VMBus
*vmbus
, vmbus_message_gpadl_header
*msg
,
1882 uint32_t num_gfns
, i
;
1884 /* must include at least one gpa range */
1885 if (msglen
< sizeof(*msg
) + sizeof(msg
->range
[0]) ||
1886 !vmbus_initialized(vmbus
)) {
1890 num_gfns
= (msg
->range_buflen
- msg
->rangecount
* sizeof(msg
->range
[0])) /
1891 sizeof(msg
->range
[0].pfn_array
[0]);
1893 trace_vmbus_gpadl_header(msg
->gpadl_id
, num_gfns
);
1896 * In theory the GPADL_HEADER message can define a GPADL with multiple GPA
1897 * ranges each with arbitrary size and alignment. However in practice only
1898 * single-range page-aligned GPADLs have been observed so just ignore
1899 * anything else and simplify things greatly.
1901 if (msg
->rangecount
!= 1 || msg
->range
[0].byte_offset
||
1902 (msg
->range
[0].byte_count
!= (num_gfns
<< TARGET_PAGE_BITS
))) {
1906 /* ignore requests to create already existing GPADLs */
1907 if (find_gpadl(vmbus
, msg
->gpadl_id
)) {
1911 gpadl
= create_gpadl(vmbus
, msg
->gpadl_id
, msg
->child_relid
, num_gfns
);
1913 for (i
= 0; i
< num_gfns
&&
1914 (void *)&msg
->range
[0].pfn_array
[i
+ 1] <= (void *)msg
+ msglen
;
1916 gpadl
->gfns
[gpadl
->seen_gfns
++] = msg
->range
[0].pfn_array
[i
];
1919 if (gpadl_full(gpadl
)) {
1920 vmbus
->state
= VMBUS_CREATE_GPADL
;
1924 static void handle_gpadl_body(VMBus
*vmbus
, vmbus_message_gpadl_body
*msg
,
1928 uint32_t num_gfns_left
, i
;
1930 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
1934 trace_vmbus_gpadl_body(msg
->gpadl_id
);
1936 gpadl
= find_gpadl(vmbus
, msg
->gpadl_id
);
1941 num_gfns_left
= gpadl
->num_gfns
- gpadl
->seen_gfns
;
1942 assert(num_gfns_left
);
1944 for (i
= 0; i
< num_gfns_left
&&
1945 (void *)&msg
->pfn_array
[i
+ 1] <= (void *)msg
+ msglen
; i
++) {
1946 gpadl
->gfns
[gpadl
->seen_gfns
++] = msg
->pfn_array
[i
];
1949 if (gpadl_full(gpadl
)) {
1950 vmbus
->state
= VMBUS_CREATE_GPADL
;
1954 static void send_create_gpadl(VMBus
*vmbus
)
1958 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1959 if (gpadl_full(gpadl
) && gpadl
->state
== VMGPADL_INIT
) {
1960 struct vmbus_message_gpadl_created msg
= {
1961 .header
.message_type
= VMBUS_MSG_GPADL_CREATED
,
1962 .gpadl_id
= gpadl
->id
,
1963 .child_relid
= gpadl
->child_relid
,
1966 trace_vmbus_gpadl_created(gpadl
->id
);
1967 post_msg(vmbus
, &msg
, sizeof(msg
));
1975 static bool complete_create_gpadl(VMBus
*vmbus
)
1979 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1980 if (gpadl_full(gpadl
) && gpadl
->state
== VMGPADL_INIT
) {
1981 gpadl
->state
= VMGPADL_ALIVE
;
1991 static void handle_gpadl_teardown(VMBus
*vmbus
,
1992 vmbus_message_gpadl_teardown
*msg
,
1997 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2001 trace_vmbus_gpadl_teardown(msg
->gpadl_id
);
2003 gpadl
= find_gpadl(vmbus
, msg
->gpadl_id
);
2004 if (!gpadl
|| gpadl
->state
== VMGPADL_TORNDOWN
) {
2008 gpadl
->state
= VMGPADL_TEARINGDOWN
;
2009 vmbus
->state
= VMBUS_TEARDOWN_GPADL
;
2012 static void send_teardown_gpadl(VMBus
*vmbus
)
2016 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2017 if (gpadl
->state
== VMGPADL_TEARINGDOWN
) {
2018 struct vmbus_message_gpadl_torndown msg
= {
2019 .header
.message_type
= VMBUS_MSG_GPADL_TORNDOWN
,
2020 .gpadl_id
= gpadl
->id
,
2023 trace_vmbus_gpadl_torndown(gpadl
->id
);
2024 post_msg(vmbus
, &msg
, sizeof(msg
));
2032 static bool complete_teardown_gpadl(VMBus
*vmbus
)
2036 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2037 if (gpadl
->state
== VMGPADL_TEARINGDOWN
) {
2038 gpadl
->state
= VMGPADL_TORNDOWN
;
2039 vmbus_put_gpadl(gpadl
);
2048 static void handle_open_channel(VMBus
*vmbus
, vmbus_message_open_channel
*msg
,
2053 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2057 trace_vmbus_open_channel(msg
->child_relid
, msg
->ring_buffer_gpadl_id
,
2059 chan
= find_channel(vmbus
, msg
->child_relid
);
2060 if (!chan
|| chan
->state
!= VMCHAN_INIT
) {
2064 chan
->ringbuf_gpadl
= msg
->ring_buffer_gpadl_id
;
2065 chan
->ringbuf_send_offset
= msg
->ring_buffer_offset
;
2066 chan
->target_vp
= msg
->target_vp
;
2067 chan
->open_id
= msg
->open_id
;
2071 chan
->state
= VMCHAN_OPENING
;
2072 vmbus
->state
= VMBUS_OPEN_CHANNEL
;
2075 static void send_open_channel(VMBus
*vmbus
)
2079 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2080 if (chan
->state
== VMCHAN_OPENING
) {
2081 struct vmbus_message_open_result msg
= {
2082 .header
.message_type
= VMBUS_MSG_OPENCHANNEL_RESULT
,
2083 .child_relid
= chan
->id
,
2084 .open_id
= chan
->open_id
,
2085 .status
= !vmbus_channel_is_open(chan
),
2088 trace_vmbus_channel_open(chan
->id
, msg
.status
);
2089 post_msg(vmbus
, &msg
, sizeof(msg
));
2097 static bool complete_open_channel(VMBus
*vmbus
)
2101 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2102 if (chan
->state
== VMCHAN_OPENING
) {
2103 if (vmbus_channel_is_open(chan
)) {
2104 chan
->state
= VMCHAN_OPEN
;
2106 * simulate guest notification of ringbuffer space made
2107 * available, for the channel protocols where the host
2108 * initiates the communication
2110 vmbus_channel_notify_host(chan
);
2112 chan
->state
= VMCHAN_INIT
;
2122 static void vdev_reset_on_close(VMBusDevice
*vdev
)
2126 for (i
= 0; i
< vdev
->num_channels
; i
++) {
2127 if (vmbus_channel_is_open(&vdev
->channels
[i
])) {
2132 /* all channels closed -- reset device */
2133 qdev_reset_all(DEVICE(vdev
));
2136 static void handle_close_channel(VMBus
*vmbus
, vmbus_message_close_channel
*msg
,
2141 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2145 trace_vmbus_close_channel(msg
->child_relid
);
2147 chan
= find_channel(vmbus
, msg
->child_relid
);
2152 close_channel(chan
);
2153 chan
->state
= VMCHAN_INIT
;
2155 vdev_reset_on_close(chan
->dev
);
2158 static void handle_unload(VMBus
*vmbus
, void *msg
, uint32_t msglen
)
2160 vmbus
->state
= VMBUS_UNLOAD
;
2163 static void send_unload(VMBus
*vmbus
)
2165 vmbus_message_header msg
= {
2166 .message_type
= VMBUS_MSG_UNLOAD_RESPONSE
,
2169 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
2170 vmbus
->rx_queue_size
= 0;
2171 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
2173 post_msg(vmbus
, &msg
, sizeof(msg
));
2177 static bool complete_unload(VMBus
*vmbus
)
2179 vmbus_reset_all(vmbus
);
2183 static void process_message(VMBus
*vmbus
)
2185 struct hyperv_post_message_input
*hv_msg
;
2186 struct vmbus_message_header
*msg
;
2190 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
2192 if (!vmbus
->rx_queue_size
) {
2196 hv_msg
= &vmbus
->rx_queue
[vmbus
->rx_queue_head
];
2197 msglen
= hv_msg
->payload_size
;
2198 if (msglen
< sizeof(*msg
)) {
2201 msgdata
= hv_msg
->payload
;
2202 msg
= (struct vmbus_message_header
*)msgdata
;
2204 trace_vmbus_process_incoming_message(msg
->message_type
);
2206 switch (msg
->message_type
) {
2207 case VMBUS_MSG_INITIATE_CONTACT
:
2208 handle_initiate_contact(vmbus
, msgdata
, msglen
);
2210 case VMBUS_MSG_REQUESTOFFERS
:
2211 handle_request_offers(vmbus
, msgdata
, msglen
);
2213 case VMBUS_MSG_GPADL_HEADER
:
2214 handle_gpadl_header(vmbus
, msgdata
, msglen
);
2216 case VMBUS_MSG_GPADL_BODY
:
2217 handle_gpadl_body(vmbus
, msgdata
, msglen
);
2219 case VMBUS_MSG_GPADL_TEARDOWN
:
2220 handle_gpadl_teardown(vmbus
, msgdata
, msglen
);
2222 case VMBUS_MSG_OPENCHANNEL
:
2223 handle_open_channel(vmbus
, msgdata
, msglen
);
2225 case VMBUS_MSG_CLOSECHANNEL
:
2226 handle_close_channel(vmbus
, msgdata
, msglen
);
2228 case VMBUS_MSG_UNLOAD
:
2229 handle_unload(vmbus
, msgdata
, msglen
);
2232 error_report("unknown message type %#x", msg
->message_type
);
2237 vmbus
->rx_queue_size
--;
2238 vmbus
->rx_queue_head
++;
2239 vmbus
->rx_queue_head
%= HV_MSG_QUEUE_LEN
;
2241 vmbus_resched(vmbus
);
2243 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
2246 static const struct {
2247 void (*run
)(VMBus
*vmbus
);
2248 bool (*complete
)(VMBus
*vmbus
);
2249 } state_runner
[] = {
2250 [VMBUS_LISTEN
] = {process_message
, NULL
},
2251 [VMBUS_HANDSHAKE
] = {send_handshake
, NULL
},
2252 [VMBUS_OFFER
] = {send_offer
, complete_offer
},
2253 [VMBUS_CREATE_GPADL
] = {send_create_gpadl
, complete_create_gpadl
},
2254 [VMBUS_TEARDOWN_GPADL
] = {send_teardown_gpadl
, complete_teardown_gpadl
},
2255 [VMBUS_OPEN_CHANNEL
] = {send_open_channel
, complete_open_channel
},
2256 [VMBUS_UNLOAD
] = {send_unload
, complete_unload
},
2259 static void vmbus_do_run(VMBus
*vmbus
)
2261 if (vmbus
->msg_in_progress
) {
2265 assert(vmbus
->state
< VMBUS_STATE_MAX
);
2266 assert(state_runner
[vmbus
->state
].run
);
2267 state_runner
[vmbus
->state
].run(vmbus
);
2270 static void vmbus_run(void *opaque
)
2272 VMBus
*vmbus
= opaque
;
2274 /* make sure no recursion happens (e.g. due to recursive aio_poll()) */
2275 if (vmbus
->in_progress
) {
2279 vmbus
->in_progress
= true;
2281 * FIXME: if vmbus_resched() is called from within vmbus_do_run(), it
2282 * should go *after* the code that can result in aio_poll; otherwise
2283 * reschedules can be missed. No idea how to enforce that.
2285 vmbus_do_run(vmbus
);
2286 vmbus
->in_progress
= false;
2289 static void vmbus_msg_cb(void *data
, int status
)
2291 VMBus
*vmbus
= data
;
2292 bool (*complete
)(VMBus
*vmbus
);
2294 assert(vmbus
->msg_in_progress
);
2296 trace_vmbus_msg_cb(status
);
2298 if (status
== -EAGAIN
) {
2302 error_report("message delivery fatal failure: %d; aborting vmbus",
2304 vmbus_reset_all(vmbus
);
2308 assert(vmbus
->state
< VMBUS_STATE_MAX
);
2309 complete
= state_runner
[vmbus
->state
].complete
;
2310 if (!complete
|| complete(vmbus
)) {
2311 vmbus
->state
= VMBUS_LISTEN
;
2314 vmbus
->msg_in_progress
= false;
2315 vmbus_resched(vmbus
);
2318 static void vmbus_resched(VMBus
*vmbus
)
2320 aio_bh_schedule_oneshot(qemu_get_aio_context(), vmbus_run
, vmbus
);
2323 static void vmbus_signal_event(EventNotifier
*e
)
2326 VMBus
*vmbus
= container_of(e
, VMBus
, notifier
);
2327 unsigned long *int_map
;
2329 bool is_dirty
= false;
2331 if (!event_notifier_test_and_clear(e
)) {
2335 trace_vmbus_signal_event();
2337 if (!vmbus
->int_page_gpa
) {
2341 addr
= vmbus
->int_page_gpa
+ TARGET_PAGE_SIZE
/ 2;
2342 len
= TARGET_PAGE_SIZE
/ 2;
2343 int_map
= cpu_physical_memory_map(addr
, &len
, 1);
2344 if (len
!= TARGET_PAGE_SIZE
/ 2) {
2348 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2349 if (bitmap_test_and_clear_atomic(int_map
, chan
->id
, 1)) {
2350 if (!vmbus_channel_is_open(chan
)) {
2353 vmbus_channel_notify_host(chan
);
2359 cpu_physical_memory_unmap(int_map
, len
, 1, is_dirty
);
2362 static void vmbus_dev_realize(DeviceState
*dev
, Error
**errp
)
2364 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2365 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2366 VMBus
*vmbus
= VMBUS(qdev_get_parent_bus(dev
));
2369 char idstr
[UUID_FMT_LEN
+ 1];
2371 assert(!qemu_uuid_is_null(&vdev
->instanceid
));
2373 /* Check for instance id collision for this class id */
2374 QTAILQ_FOREACH(child
, &BUS(vmbus
)->children
, sibling
) {
2375 VMBusDevice
*child_dev
= VMBUS_DEVICE(child
->child
);
2377 if (child_dev
== vdev
) {
2381 if (qemu_uuid_is_equal(&child_dev
->instanceid
, &vdev
->instanceid
)) {
2382 qemu_uuid_unparse(&vdev
->instanceid
, idstr
);
2383 error_setg(&err
, "duplicate vmbus device instance id %s", idstr
);
2388 vdev
->dma_as
= &address_space_memory
;
2390 create_channels(vmbus
, vdev
, &err
);
2395 if (vdc
->vmdev_realize
) {
2396 vdc
->vmdev_realize(vdev
, &err
);
2398 goto err_vdc_realize
;
2404 free_channels(vdev
);
2406 error_propagate(errp
, err
);
2409 static void vmbus_dev_reset(DeviceState
*dev
)
2412 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2413 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2415 if (vdev
->channels
) {
2416 for (i
= 0; i
< vdev
->num_channels
; i
++) {
2417 VMBusChannel
*chan
= &vdev
->channels
[i
];
2418 close_channel(chan
);
2419 chan
->state
= VMCHAN_INIT
;
2423 if (vdc
->vmdev_reset
) {
2424 vdc
->vmdev_reset(vdev
);
2428 static void vmbus_dev_unrealize(DeviceState
*dev
)
2430 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2431 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2433 if (vdc
->vmdev_unrealize
) {
2434 vdc
->vmdev_unrealize(vdev
);
2436 free_channels(vdev
);
2439 static void vmbus_dev_class_init(ObjectClass
*klass
, void *data
)
2441 DeviceClass
*kdev
= DEVICE_CLASS(klass
);
2442 kdev
->bus_type
= TYPE_VMBUS
;
2443 kdev
->realize
= vmbus_dev_realize
;
2444 kdev
->unrealize
= vmbus_dev_unrealize
;
2445 kdev
->reset
= vmbus_dev_reset
;
2448 static Property vmbus_dev_instanceid
=
2449 DEFINE_PROP_UUID("instanceid", VMBusDevice
, instanceid
);
2451 static void vmbus_dev_instance_init(Object
*obj
)
2453 VMBusDevice
*vdev
= VMBUS_DEVICE(obj
);
2454 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2456 if (!qemu_uuid_is_null(&vdc
->instanceid
)) {
2457 /* Class wants to only have a single instance with a fixed UUID */
2458 vdev
->instanceid
= vdc
->instanceid
;
2460 qdev_property_add_static(DEVICE(vdev
), &vmbus_dev_instanceid
);
2464 const VMStateDescription vmstate_vmbus_dev
= {
2465 .name
= TYPE_VMBUS_DEVICE
,
2467 .minimum_version_id
= 0,
2468 .fields
= (VMStateField
[]) {
2469 VMSTATE_UINT8_ARRAY(instanceid
.data
, VMBusDevice
, 16),
2470 VMSTATE_UINT16(num_channels
, VMBusDevice
),
2471 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(channels
, VMBusDevice
,
2472 num_channels
, vmstate_channel
,
2474 VMSTATE_END_OF_LIST()
2478 /* vmbus generic device base */
2479 static const TypeInfo vmbus_dev_type_info
= {
2480 .name
= TYPE_VMBUS_DEVICE
,
2481 .parent
= TYPE_DEVICE
,
2483 .instance_size
= sizeof(VMBusDevice
),
2484 .class_size
= sizeof(VMBusDeviceClass
),
2485 .class_init
= vmbus_dev_class_init
,
2486 .instance_init
= vmbus_dev_instance_init
,
2489 static void vmbus_realize(BusState
*bus
, Error
**errp
)
2492 Error
*local_err
= NULL
;
2493 VMBus
*vmbus
= VMBUS(bus
);
2495 qemu_mutex_init(&vmbus
->rx_queue_lock
);
2497 QTAILQ_INIT(&vmbus
->gpadl_list
);
2498 QTAILQ_INIT(&vmbus
->channel_list
);
2500 ret
= hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
,
2501 vmbus_recv_message
, vmbus
);
2503 error_setg(&local_err
, "hyperv set message handler failed: %d", ret
);
2507 ret
= event_notifier_init(&vmbus
->notifier
, 0);
2509 error_setg(&local_err
, "event notifier failed to init with %d", ret
);
2510 goto remove_msg_handler
;
2513 event_notifier_set_handler(&vmbus
->notifier
, vmbus_signal_event
);
2514 ret
= hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID
,
2517 error_setg(&local_err
, "hyperv set event handler failed with %d", ret
);
2518 goto clear_event_notifier
;
2523 clear_event_notifier
:
2524 event_notifier_cleanup(&vmbus
->notifier
);
2526 hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
, NULL
, NULL
);
2528 qemu_mutex_destroy(&vmbus
->rx_queue_lock
);
2529 error_propagate(errp
, local_err
);
2532 static void vmbus_unrealize(BusState
*bus
)
2534 VMBus
*vmbus
= VMBUS(bus
);
2536 hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
, NULL
, NULL
);
2537 hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID
, NULL
);
2538 event_notifier_cleanup(&vmbus
->notifier
);
2540 qemu_mutex_destroy(&vmbus
->rx_queue_lock
);
2543 static void vmbus_reset(BusState
*bus
)
2545 vmbus_deinit(VMBUS(bus
));
2548 static char *vmbus_get_dev_path(DeviceState
*dev
)
2550 BusState
*bus
= qdev_get_parent_bus(dev
);
2551 return qdev_get_dev_path(bus
->parent
);
2554 static char *vmbus_get_fw_dev_path(DeviceState
*dev
)
2556 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2557 char uuid
[UUID_FMT_LEN
+ 1];
2559 qemu_uuid_unparse(&vdev
->instanceid
, uuid
);
2560 return g_strdup_printf("%s@%s", qdev_fw_name(dev
), uuid
);
2563 static void vmbus_class_init(ObjectClass
*klass
, void *data
)
2565 BusClass
*k
= BUS_CLASS(klass
);
2567 k
->get_dev_path
= vmbus_get_dev_path
;
2568 k
->get_fw_dev_path
= vmbus_get_fw_dev_path
;
2569 k
->realize
= vmbus_realize
;
2570 k
->unrealize
= vmbus_unrealize
;
2571 k
->reset
= vmbus_reset
;
2574 static int vmbus_pre_load(void *opaque
)
2577 VMBus
*vmbus
= VMBUS(opaque
);
2580 * channel IDs allocated by the source will come in the migration stream
2581 * for each channel, so clean up the ones allocated at realize
2583 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2584 unregister_chan_id(chan
);
2589 static int vmbus_post_load(void *opaque
, int version_id
)
2592 VMBus
*vmbus
= VMBUS(opaque
);
2596 ret
= vmbus_init(vmbus
);
2601 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2602 gpadl
->vmbus
= vmbus
;
2603 gpadl
->refcount
= 1;
2607 * reopening channels depends on initialized vmbus so it's done here
2608 * instead of channel_post_load()
2610 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2612 if (chan
->state
== VMCHAN_OPENING
|| chan
->state
== VMCHAN_OPEN
) {
2616 if (chan
->state
!= VMCHAN_OPEN
) {
2620 if (!vmbus_channel_is_open(chan
)) {
2621 /* reopen failed, abort loading */
2625 /* resume processing on the guest side if it missed the notification */
2626 hyperv_sint_route_set_sint(chan
->notify_route
);
2627 /* ditto on the host side */
2628 vmbus_channel_notify_host(chan
);
2631 vmbus_resched(vmbus
);
2635 static const VMStateDescription vmstate_post_message_input
= {
2636 .name
= "vmbus/hyperv_post_message_input",
2638 .minimum_version_id
= 0,
2639 .fields
= (VMStateField
[]) {
2641 * skip connection_id and message_type as they are validated before
2642 * queueing and ignored on dequeueing
2644 VMSTATE_UINT32(payload_size
, struct hyperv_post_message_input
),
2645 VMSTATE_UINT8_ARRAY(payload
, struct hyperv_post_message_input
,
2646 HV_MESSAGE_PAYLOAD_SIZE
),
2647 VMSTATE_END_OF_LIST()
2651 static bool vmbus_rx_queue_needed(void *opaque
)
2653 VMBus
*vmbus
= VMBUS(opaque
);
2654 return vmbus
->rx_queue_size
;
2657 static const VMStateDescription vmstate_rx_queue
= {
2658 .name
= "vmbus/rx_queue",
2660 .minimum_version_id
= 0,
2661 .needed
= vmbus_rx_queue_needed
,
2662 .fields
= (VMStateField
[]) {
2663 VMSTATE_UINT8(rx_queue_head
, VMBus
),
2664 VMSTATE_UINT8(rx_queue_size
, VMBus
),
2665 VMSTATE_STRUCT_ARRAY(rx_queue
, VMBus
,
2666 HV_MSG_QUEUE_LEN
, 0,
2667 vmstate_post_message_input
,
2668 struct hyperv_post_message_input
),
2669 VMSTATE_END_OF_LIST()
2673 static const VMStateDescription vmstate_vmbus
= {
2676 .minimum_version_id
= 0,
2677 .pre_load
= vmbus_pre_load
,
2678 .post_load
= vmbus_post_load
,
2679 .fields
= (VMStateField
[]) {
2680 VMSTATE_UINT8(state
, VMBus
),
2681 VMSTATE_UINT32(version
, VMBus
),
2682 VMSTATE_UINT32(target_vp
, VMBus
),
2683 VMSTATE_UINT64(int_page_gpa
, VMBus
),
2684 VMSTATE_QTAILQ_V(gpadl_list
, VMBus
, 0,
2685 vmstate_gpadl
, VMBusGpadl
, link
),
2686 VMSTATE_END_OF_LIST()
2688 .subsections
= (const VMStateDescription
* []) {
2694 static const TypeInfo vmbus_type_info
= {
2697 .instance_size
= sizeof(VMBus
),
2698 .class_init
= vmbus_class_init
,
2701 static void vmbus_bridge_realize(DeviceState
*dev
, Error
**errp
)
2703 VMBusBridge
*bridge
= VMBUS_BRIDGE(dev
);
2706 * here there's at least one vmbus bridge that is being realized, so
2707 * vmbus_bridge_find can only return NULL if it's not unique
2709 if (!vmbus_bridge_find()) {
2710 error_setg(errp
, "there can be at most one %s in the system",
2715 if (!hyperv_is_synic_enabled()) {
2716 error_report("VMBus requires usable Hyper-V SynIC and VP_INDEX");
2720 bridge
->bus
= VMBUS(qbus_create(TYPE_VMBUS
, dev
, "vmbus"));
2723 static char *vmbus_bridge_ofw_unit_address(const SysBusDevice
*dev
)
2725 /* there can be only one VMBus */
2726 return g_strdup("0");
2729 static const VMStateDescription vmstate_vmbus_bridge
= {
2730 .name
= TYPE_VMBUS_BRIDGE
,
2732 .minimum_version_id
= 0,
2733 .fields
= (VMStateField
[]) {
2734 VMSTATE_STRUCT_POINTER(bus
, VMBusBridge
, vmstate_vmbus
, VMBus
),
2735 VMSTATE_END_OF_LIST()
2739 static Property vmbus_bridge_props
[] = {
2740 DEFINE_PROP_UINT8("irq", VMBusBridge
, irq
, 7),
2741 DEFINE_PROP_END_OF_LIST()
2744 static void vmbus_bridge_class_init(ObjectClass
*klass
, void *data
)
2746 DeviceClass
*k
= DEVICE_CLASS(klass
);
2747 SysBusDeviceClass
*sk
= SYS_BUS_DEVICE_CLASS(klass
);
2749 k
->realize
= vmbus_bridge_realize
;
2750 k
->fw_name
= "vmbus";
2751 sk
->explicit_ofw_unit_address
= vmbus_bridge_ofw_unit_address
;
2752 set_bit(DEVICE_CATEGORY_BRIDGE
, k
->categories
);
2753 k
->vmsd
= &vmstate_vmbus_bridge
;
2754 device_class_set_props(k
, vmbus_bridge_props
);
2755 /* override SysBusDevice's default */
2756 k
->user_creatable
= true;
2759 static const TypeInfo vmbus_bridge_type_info
= {
2760 .name
= TYPE_VMBUS_BRIDGE
,
2761 .parent
= TYPE_SYS_BUS_DEVICE
,
2762 .instance_size
= sizeof(VMBusBridge
),
2763 .class_init
= vmbus_bridge_class_init
,
2766 static void vmbus_register_types(void)
2768 type_register_static(&vmbus_bridge_type_info
);
2769 type_register_static(&vmbus_dev_type_info
);
2770 type_register_static(&vmbus_type_info
);
2773 type_init(vmbus_register_types
)