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/qdev-properties-system.h"
17 #include "hw/hyperv/hyperv.h"
18 #include "hw/hyperv/vmbus.h"
19 #include "hw/hyperv/vmbus-bridge.h"
20 #include "hw/sysbus.h"
34 /* associated channel id (rudimentary?) */
37 /* number of pages in the GPADL as declared in GPADL_HEADER message */
40 * Due to limited message size, GPADL may not fit fully in a single
41 * GPADL_HEADER message, and is further popluated using GPADL_BODY
42 * messages. @seen_gfns is the number of pages seen so far; once it
43 * reaches @num_gfns, the GPADL is ready to use.
46 /* array of GFNs (of size @num_gfns once allocated) */
51 QTAILQ_ENTRY(VMBusGpadl
) link
;
57 * Wrap sequential read from / write to GPADL.
59 typedef struct GpadlIter
{
63 /* offset into GPADL where the next i/o will be performed */
66 * Cached mapping of the currently accessed page, up to page boundary.
67 * Updated lazily on i/o.
68 * Note: MemoryRegionCache can not be used here because pages in the GPADL
69 * are non-contiguous and may belong to different memory regions.
72 /* offset after last i/o (i.e. not affected by seek) */
75 * Indicator that the iterator is active and may have a cached mapping.
76 * Allows to enforce bracketing of all i/o (which may create cached
77 * mappings) and thus exclude mapping leaks.
83 * Ring buffer. There are two of them, sitting in the same GPADL, for each
85 * Each ring buffer consists of a set of pages, with the first page containing
86 * the ring buffer header, and the remaining pages being for data packets.
88 typedef struct VMBusRingBufCommon
{
90 /* GPA of the ring buffer header */
92 /* start and length of the ring buffer data area within GPADL */
99 typedef struct VMBusSendRingBuf
{
100 VMBusRingBufCommon common
;
101 /* current write index, to be committed at the end of send */
103 /* write index at the start of send */
104 uint32_t last_wr_idx
;
105 /* space to be requested from the guest */
107 /* space reserved for planned sends */
109 /* last seen read index */
110 uint32_t last_seen_rd_idx
;
113 typedef struct VMBusRecvRingBuf
{
114 VMBusRingBufCommon common
;
115 /* current read index, to be committed at the end of receive */
117 /* read index at the start of receive */
118 uint32_t last_rd_idx
;
119 /* last seen write index */
120 uint32_t last_seen_wr_idx
;
136 struct VMBusChannel
{
142 * subchannel index within the device; subchannel #0 is "primary" and
145 uint16_t subchan_idx
;
147 /* VP_INDEX of the vCPU to notify with (synthetic) interrupts */
149 /* GPADL id to use for the ring buffers */
150 uint32_t ringbuf_gpadl
;
151 /* start (in pages) of the send ring buffer within @ringbuf_gpadl */
152 uint32_t ringbuf_send_offset
;
158 /* main device worker; copied from the device class */
159 VMBusChannelNotifyCb notify_cb
;
161 * guest->host notifications, either sent directly or dispatched via
162 * interrupt page (older VMBus)
164 EventNotifier notifier
;
168 * SINT route to signal with host->guest notifications; may be shared with
169 * the main VMBus SINT route
171 HvSintRoute
*notify_route
;
174 VMBusSendRingBuf send_ringbuf
;
175 VMBusRecvRingBuf recv_ringbuf
;
177 QTAILQ_ENTRY(VMBusChannel
) link
;
181 * Hyper-V spec mandates that every message port has 16 buffers, which means
182 * that the guest can post up to this many messages without blocking.
183 * Therefore a queue for incoming messages has to be provided.
184 * For outgoing (i.e. host->guest) messages there's no queue; the VMBus just
185 * doesn't transition to a new state until the message is known to have been
186 * successfully delivered to the respective SynIC message slot.
188 #define HV_MSG_QUEUE_LEN 16
190 /* Hyper-V devices never use channel #0. Must be something special. */
191 #define VMBUS_FIRST_CHANID 1
192 /* Each channel occupies one bit within a single event page sint slot. */
193 #define VMBUS_CHANID_COUNT (HV_EVENT_FLAGS_COUNT - VMBUS_FIRST_CHANID)
194 /* Leave a few connection numbers for other purposes. */
195 #define VMBUS_CHAN_CONNECTION_OFFSET 16
198 * Since the success or failure of sending a message is reported
199 * asynchronously, the VMBus state machine has effectively two entry points:
200 * vmbus_run and vmbus_msg_cb (the latter is called when the host->guest
201 * message delivery status becomes known). Both are run as oneshot BHs on the
202 * main aio context, ensuring serialization.
209 VMBUS_TEARDOWN_GPADL
,
219 /* protection against recursive aio_poll (see vmbus_run) */
221 /* whether there's a message being delivered to the guest */
222 bool msg_in_progress
;
224 /* VP_INDEX of the vCPU to send messages and interrupts to */
226 HvSintRoute
*sint_route
;
228 * interrupt page for older protocol versions; newer ones use SynIC event
233 DECLARE_BITMAP(chanid_bitmap
, VMBUS_CHANID_COUNT
);
235 /* incoming message queue */
236 struct hyperv_post_message_input rx_queue
[HV_MSG_QUEUE_LEN
];
237 uint8_t rx_queue_head
;
238 uint8_t rx_queue_size
;
239 QemuMutex rx_queue_lock
;
241 QTAILQ_HEAD(, VMBusGpadl
) gpadl_list
;
242 QTAILQ_HEAD(, VMBusChannel
) channel_list
;
245 * guest->host notifications for older VMBus, to be dispatched via
248 EventNotifier notifier
;
251 static bool gpadl_full(VMBusGpadl
*gpadl
)
253 return gpadl
->seen_gfns
== gpadl
->num_gfns
;
256 static VMBusGpadl
*create_gpadl(VMBus
*vmbus
, uint32_t id
,
257 uint32_t child_relid
, uint32_t num_gfns
)
259 VMBusGpadl
*gpadl
= g_new0(VMBusGpadl
, 1);
262 gpadl
->child_relid
= child_relid
;
263 gpadl
->num_gfns
= num_gfns
;
264 gpadl
->gfns
= g_new(uint64_t, num_gfns
);
265 QTAILQ_INSERT_HEAD(&vmbus
->gpadl_list
, gpadl
, link
);
266 gpadl
->vmbus
= vmbus
;
271 static void free_gpadl(VMBusGpadl
*gpadl
)
273 QTAILQ_REMOVE(&gpadl
->vmbus
->gpadl_list
, gpadl
, link
);
278 static VMBusGpadl
*find_gpadl(VMBus
*vmbus
, uint32_t gpadl_id
)
281 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
282 if (gpadl
->id
== gpadl_id
) {
289 VMBusGpadl
*vmbus_get_gpadl(VMBusChannel
*chan
, uint32_t gpadl_id
)
291 VMBusGpadl
*gpadl
= find_gpadl(chan
->vmbus
, gpadl_id
);
292 if (!gpadl
|| !gpadl_full(gpadl
)) {
299 void vmbus_put_gpadl(VMBusGpadl
*gpadl
)
304 if (--gpadl
->refcount
) {
310 uint32_t vmbus_gpadl_len(VMBusGpadl
*gpadl
)
312 return gpadl
->num_gfns
* TARGET_PAGE_SIZE
;
315 static void gpadl_iter_init(GpadlIter
*iter
, VMBusGpadl
*gpadl
,
316 AddressSpace
*as
, DMADirection dir
)
321 iter
->active
= false;
324 static inline void gpadl_iter_cache_unmap(GpadlIter
*iter
)
326 uint32_t map_start_in_page
= (uintptr_t)iter
->map
& ~TARGET_PAGE_MASK
;
327 uint32_t io_end_in_page
= ((iter
->last_off
- 1) & ~TARGET_PAGE_MASK
) + 1;
329 /* mapping is only done to do non-zero amount of i/o */
330 assert(iter
->last_off
> 0);
331 assert(map_start_in_page
< io_end_in_page
);
333 dma_memory_unmap(iter
->as
, iter
->map
, TARGET_PAGE_SIZE
- map_start_in_page
,
334 iter
->dir
, io_end_in_page
- map_start_in_page
);
338 * Copy exactly @len bytes between the GPADL pointed to by @iter and @buf.
339 * The direction of the copy is determined by @iter->dir.
340 * The caller must ensure the operation overflows neither @buf nor the GPADL
341 * (there's an assert for the latter).
342 * Reuse the currently mapped page in the GPADL if possible.
344 static ssize_t
gpadl_iter_io(GpadlIter
*iter
, void *buf
, uint32_t len
)
348 assert(iter
->active
);
351 uint32_t off_in_page
= iter
->off
& ~TARGET_PAGE_MASK
;
352 uint32_t pgleft
= TARGET_PAGE_SIZE
- off_in_page
;
353 uint32_t cplen
= MIN(pgleft
, len
);
356 /* try to reuse the cached mapping */
358 uint32_t map_start_in_page
=
359 (uintptr_t)iter
->map
& ~TARGET_PAGE_MASK
;
360 uint32_t off_base
= iter
->off
& ~TARGET_PAGE_MASK
;
361 uint32_t mapped_base
= (iter
->last_off
- 1) & ~TARGET_PAGE_MASK
;
362 if (off_base
!= mapped_base
|| off_in_page
< map_start_in_page
) {
363 gpadl_iter_cache_unmap(iter
);
370 dma_addr_t mlen
= pgleft
;
371 uint32_t idx
= iter
->off
>> TARGET_PAGE_BITS
;
372 assert(idx
< iter
->gpadl
->num_gfns
);
374 maddr
= (iter
->gpadl
->gfns
[idx
] << TARGET_PAGE_BITS
) | off_in_page
;
376 iter
->map
= dma_memory_map(iter
->as
, maddr
, &mlen
, iter
->dir
,
377 MEMTXATTRS_UNSPECIFIED
);
378 if (mlen
!= pgleft
) {
379 dma_memory_unmap(iter
->as
, iter
->map
, mlen
, iter
->dir
, 0);
385 p
= (void *)(uintptr_t)(((uintptr_t)iter
->map
& TARGET_PAGE_MASK
) |
387 if (iter
->dir
== DMA_DIRECTION_FROM_DEVICE
) {
388 memcpy(p
, buf
, cplen
);
390 memcpy(buf
, p
, cplen
);
396 iter
->last_off
= iter
->off
;
403 * Position the iterator @iter at new offset @new_off.
404 * If this results in the cached mapping being unusable with the new offset,
407 static inline void gpadl_iter_seek(GpadlIter
*iter
, uint32_t new_off
)
409 assert(iter
->active
);
414 * Start a series of i/o on the GPADL.
415 * After this i/o and seek operations on @iter become legal.
417 static inline void gpadl_iter_start_io(GpadlIter
*iter
)
419 assert(!iter
->active
);
420 /* mapping is cached lazily on i/o */
426 * End the eariler started series of i/o on the GPADL and release the cached
429 static inline void gpadl_iter_end_io(GpadlIter
*iter
)
431 assert(iter
->active
);
434 gpadl_iter_cache_unmap(iter
);
437 iter
->active
= false;
440 static void vmbus_resched(VMBus
*vmbus
);
441 static void vmbus_msg_cb(void *data
, int status
);
443 ssize_t
vmbus_iov_to_gpadl(VMBusChannel
*chan
, VMBusGpadl
*gpadl
, uint32_t off
,
444 const struct iovec
*iov
, size_t iov_cnt
)
450 gpadl_iter_init(&iter
, gpadl
, chan
->dev
->dma_as
,
451 DMA_DIRECTION_FROM_DEVICE
);
452 gpadl_iter_start_io(&iter
);
453 gpadl_iter_seek(&iter
, off
);
454 for (i
= 0; i
< iov_cnt
; i
++) {
455 ret
= gpadl_iter_io(&iter
, iov
[i
].iov_base
, iov
[i
].iov_len
);
461 gpadl_iter_end_io(&iter
);
465 int vmbus_map_sgl(VMBusChanReq
*req
, DMADirection dir
, struct iovec
*iov
,
466 unsigned iov_cnt
, size_t len
, size_t off
)
468 int ret_cnt
= 0, ret
;
470 QEMUSGList
*sgl
= &req
->sgl
;
471 ScatterGatherEntry
*sg
= sgl
->sg
;
473 for (i
= 0; i
< sgl
->nsg
; i
++) {
474 if (sg
[i
].len
> off
) {
479 for (; len
&& i
< sgl
->nsg
; i
++) {
480 dma_addr_t mlen
= MIN(sg
[i
].len
- off
, len
);
481 dma_addr_t addr
= sg
[i
].base
+ off
;
485 for (; mlen
; ret_cnt
++) {
489 if (ret_cnt
== iov_cnt
) {
494 iov
[ret_cnt
].iov_base
= dma_memory_map(sgl
->as
, a
, &l
, dir
,
495 MEMTXATTRS_UNSPECIFIED
);
500 iov
[ret_cnt
].iov_len
= l
;
508 vmbus_unmap_sgl(req
, dir
, iov
, ret_cnt
, 0);
512 void vmbus_unmap_sgl(VMBusChanReq
*req
, DMADirection dir
, struct iovec
*iov
,
513 unsigned iov_cnt
, size_t accessed
)
515 QEMUSGList
*sgl
= &req
->sgl
;
518 for (i
= 0; i
< iov_cnt
; i
++) {
519 size_t acsd
= MIN(accessed
, iov
[i
].iov_len
);
520 dma_memory_unmap(sgl
->as
, iov
[i
].iov_base
, iov
[i
].iov_len
, dir
, acsd
);
525 static const VMStateDescription vmstate_gpadl
= {
526 .name
= "vmbus/gpadl",
528 .minimum_version_id
= 0,
529 .fields
= (const VMStateField
[]) {
530 VMSTATE_UINT32(id
, VMBusGpadl
),
531 VMSTATE_UINT32(child_relid
, VMBusGpadl
),
532 VMSTATE_UINT32(num_gfns
, VMBusGpadl
),
533 VMSTATE_UINT32(seen_gfns
, VMBusGpadl
),
534 VMSTATE_VARRAY_UINT32_ALLOC(gfns
, VMBusGpadl
, num_gfns
, 0,
535 vmstate_info_uint64
, uint64_t),
536 VMSTATE_UINT8(state
, VMBusGpadl
),
537 VMSTATE_END_OF_LIST()
542 * Wrap the index into a ring buffer of @len bytes.
543 * @idx is assumed not to exceed twice the size of the ringbuffer, so only
544 * single wraparound is considered.
546 static inline uint32_t rb_idx_wrap(uint32_t idx
, uint32_t len
)
555 * Circular difference between two indices into a ring buffer of @len bytes.
556 * @allow_catchup - whether @idx1 may catch up @idx2; e.g. read index may catch
557 * up write index but not vice versa.
559 static inline uint32_t rb_idx_delta(uint32_t idx1
, uint32_t idx2
, uint32_t len
,
562 return rb_idx_wrap(idx2
+ len
- idx1
- !allow_catchup
, len
);
565 static vmbus_ring_buffer
*ringbuf_map_hdr(VMBusRingBufCommon
*ringbuf
)
567 vmbus_ring_buffer
*rb
;
568 dma_addr_t mlen
= sizeof(*rb
);
570 rb
= dma_memory_map(ringbuf
->as
, ringbuf
->rb_addr
, &mlen
,
571 DMA_DIRECTION_FROM_DEVICE
, MEMTXATTRS_UNSPECIFIED
);
572 if (mlen
!= sizeof(*rb
)) {
573 dma_memory_unmap(ringbuf
->as
, rb
, mlen
,
574 DMA_DIRECTION_FROM_DEVICE
, 0);
580 static void ringbuf_unmap_hdr(VMBusRingBufCommon
*ringbuf
,
581 vmbus_ring_buffer
*rb
, bool dirty
)
585 dma_memory_unmap(ringbuf
->as
, rb
, sizeof(*rb
), DMA_DIRECTION_FROM_DEVICE
,
586 dirty
? sizeof(*rb
) : 0);
589 static void ringbuf_init_common(VMBusRingBufCommon
*ringbuf
, VMBusGpadl
*gpadl
,
590 AddressSpace
*as
, DMADirection dir
,
591 uint32_t begin
, uint32_t end
)
594 ringbuf
->rb_addr
= gpadl
->gfns
[begin
] << TARGET_PAGE_BITS
;
595 ringbuf
->base
= (begin
+ 1) << TARGET_PAGE_BITS
;
596 ringbuf
->len
= (end
- begin
- 1) << TARGET_PAGE_BITS
;
597 gpadl_iter_init(&ringbuf
->iter
, gpadl
, as
, dir
);
600 static int ringbufs_init(VMBusChannel
*chan
)
602 vmbus_ring_buffer
*rb
;
603 VMBusSendRingBuf
*send_ringbuf
= &chan
->send_ringbuf
;
604 VMBusRecvRingBuf
*recv_ringbuf
= &chan
->recv_ringbuf
;
606 if (chan
->ringbuf_send_offset
<= 1 ||
607 chan
->gpadl
->num_gfns
<= chan
->ringbuf_send_offset
+ 1) {
611 ringbuf_init_common(&recv_ringbuf
->common
, chan
->gpadl
, chan
->dev
->dma_as
,
612 DMA_DIRECTION_TO_DEVICE
, 0, chan
->ringbuf_send_offset
);
613 ringbuf_init_common(&send_ringbuf
->common
, chan
->gpadl
, chan
->dev
->dma_as
,
614 DMA_DIRECTION_FROM_DEVICE
, chan
->ringbuf_send_offset
,
615 chan
->gpadl
->num_gfns
);
616 send_ringbuf
->wanted
= 0;
617 send_ringbuf
->reserved
= 0;
619 rb
= ringbuf_map_hdr(&recv_ringbuf
->common
);
623 recv_ringbuf
->rd_idx
= recv_ringbuf
->last_rd_idx
= rb
->read_index
;
624 ringbuf_unmap_hdr(&recv_ringbuf
->common
, rb
, false);
626 rb
= ringbuf_map_hdr(&send_ringbuf
->common
);
630 send_ringbuf
->wr_idx
= send_ringbuf
->last_wr_idx
= rb
->write_index
;
631 send_ringbuf
->last_seen_rd_idx
= rb
->read_index
;
632 rb
->feature_bits
|= VMBUS_RING_BUFFER_FEAT_PENDING_SZ
;
633 ringbuf_unmap_hdr(&send_ringbuf
->common
, rb
, true);
635 if (recv_ringbuf
->rd_idx
>= recv_ringbuf
->common
.len
||
636 send_ringbuf
->wr_idx
>= send_ringbuf
->common
.len
) {
644 * Perform io between the GPADL-backed ringbuffer @ringbuf and @buf, wrapping
646 * @len is assumed not to exceed the size of the ringbuffer, so only single
647 * wraparound is considered.
649 static ssize_t
ringbuf_io(VMBusRingBufCommon
*ringbuf
, void *buf
, uint32_t len
)
651 ssize_t ret1
= 0, ret2
= 0;
652 uint32_t remain
= ringbuf
->len
+ ringbuf
->base
- ringbuf
->iter
.off
;
655 ret1
= gpadl_iter_io(&ringbuf
->iter
, buf
, remain
);
659 gpadl_iter_seek(&ringbuf
->iter
, ringbuf
->base
);
663 ret2
= gpadl_iter_io(&ringbuf
->iter
, buf
, len
);
671 * Position the circular iterator within @ringbuf to offset @new_off, wrapping
673 * @new_off is assumed not to exceed twice the size of the ringbuffer, so only
674 * single wraparound is considered.
676 static inline void ringbuf_seek(VMBusRingBufCommon
*ringbuf
, uint32_t new_off
)
678 gpadl_iter_seek(&ringbuf
->iter
,
679 ringbuf
->base
+ rb_idx_wrap(new_off
, ringbuf
->len
));
682 static inline uint32_t ringbuf_tell(VMBusRingBufCommon
*ringbuf
)
684 return ringbuf
->iter
.off
- ringbuf
->base
;
687 static inline void ringbuf_start_io(VMBusRingBufCommon
*ringbuf
)
689 gpadl_iter_start_io(&ringbuf
->iter
);
692 static inline void ringbuf_end_io(VMBusRingBufCommon
*ringbuf
)
694 gpadl_iter_end_io(&ringbuf
->iter
);
697 VMBusDevice
*vmbus_channel_device(VMBusChannel
*chan
)
702 VMBusChannel
*vmbus_device_channel(VMBusDevice
*dev
, uint32_t chan_idx
)
704 if (chan_idx
>= dev
->num_channels
) {
707 return &dev
->channels
[chan_idx
];
710 uint32_t vmbus_channel_idx(VMBusChannel
*chan
)
712 return chan
- chan
->dev
->channels
;
715 void vmbus_channel_notify_host(VMBusChannel
*chan
)
717 event_notifier_set(&chan
->notifier
);
720 bool vmbus_channel_is_open(VMBusChannel
*chan
)
722 return chan
->is_open
;
726 * Notify the guest side about the data to work on in the channel ring buffer.
727 * The notification is done by signaling a dedicated per-channel SynIC event
728 * flag (more recent guests) or setting a bit in the interrupt page and firing
729 * the VMBus SINT (older guests).
731 static int vmbus_channel_notify_guest(VMBusChannel
*chan
)
734 unsigned long *int_map
, mask
;
736 hwaddr addr
= chan
->vmbus
->int_page_gpa
;
737 hwaddr len
= TARGET_PAGE_SIZE
/ 2, dirty
= 0;
739 trace_vmbus_channel_notify_guest(chan
->id
);
742 return hyperv_set_event_flag(chan
->notify_route
, chan
->id
);
745 int_map
= cpu_physical_memory_map(addr
, &len
, 1);
746 if (len
!= TARGET_PAGE_SIZE
/ 2) {
751 idx
= BIT_WORD(chan
->id
);
752 mask
= BIT_MASK(chan
->id
);
753 if ((qatomic_fetch_or(&int_map
[idx
], mask
) & mask
) != mask
) {
754 res
= hyperv_sint_route_set_sint(chan
->notify_route
);
759 cpu_physical_memory_unmap(int_map
, len
, 1, dirty
);
763 #define VMBUS_PKT_TRAILER sizeof(uint64_t)
765 static uint32_t vmbus_pkt_hdr_set_offsets(vmbus_packet_hdr
*hdr
,
766 uint32_t desclen
, uint32_t msglen
)
768 hdr
->offset_qwords
= sizeof(*hdr
) / sizeof(uint64_t) +
769 DIV_ROUND_UP(desclen
, sizeof(uint64_t));
770 hdr
->len_qwords
= hdr
->offset_qwords
+
771 DIV_ROUND_UP(msglen
, sizeof(uint64_t));
772 return hdr
->len_qwords
* sizeof(uint64_t) + VMBUS_PKT_TRAILER
;
776 * Simplified ring buffer operation with paired barriers annotations in the
777 * producer and consumer loops:
779 * producer * consumer
780 * ~~~~~~~~ * ~~~~~~~~
781 * write pending_send_sz * read write_index
782 * smp_mb [A] * smp_mb [C]
783 * read read_index * read packet
784 * smp_mb [B] * read/write out-of-band data
785 * read/write out-of-band data * smp_mb [B]
786 * write packet * write read_index
787 * smp_mb [C] * smp_mb [A]
788 * write write_index * read pending_send_sz
789 * smp_wmb [D] * smp_rmb [D]
790 * write pending_send_sz * read write_index
794 static inline uint32_t ringbuf_send_avail(VMBusSendRingBuf
*ringbuf
)
796 /* don't trust guest data */
797 if (ringbuf
->last_seen_rd_idx
>= ringbuf
->common
.len
) {
800 return rb_idx_delta(ringbuf
->wr_idx
, ringbuf
->last_seen_rd_idx
,
801 ringbuf
->common
.len
, false);
804 static ssize_t
ringbuf_send_update_idx(VMBusChannel
*chan
)
806 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
807 vmbus_ring_buffer
*rb
;
810 written
= rb_idx_delta(ringbuf
->last_wr_idx
, ringbuf
->wr_idx
,
811 ringbuf
->common
.len
, true);
816 rb
= ringbuf_map_hdr(&ringbuf
->common
);
821 ringbuf
->reserved
-= written
;
823 /* prevent reorder with the data operation and packet write */
824 smp_mb(); /* barrier pair [C] */
825 rb
->write_index
= ringbuf
->wr_idx
;
828 * If the producer earlier indicated that it wants to be notified when the
829 * consumer frees certain amount of space in the ring buffer, that amount
830 * is reduced by the size of the completed write.
832 if (ringbuf
->wanted
) {
833 /* otherwise reservation would fail */
834 assert(ringbuf
->wanted
< written
);
835 ringbuf
->wanted
-= written
;
836 /* prevent reorder with write_index write */
837 smp_wmb(); /* barrier pair [D] */
838 rb
->pending_send_sz
= ringbuf
->wanted
;
841 /* prevent reorder with write_index or pending_send_sz write */
842 smp_mb(); /* barrier pair [A] */
843 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
846 * The consumer may have missed the reduction of pending_send_sz and skip
847 * notification, so re-check the blocking condition, and, if it's no longer
848 * true, ensure processing another iteration by simulating consumer's
851 if (ringbuf_send_avail(ringbuf
) >= ringbuf
->wanted
) {
852 vmbus_channel_notify_host(chan
);
855 /* skip notification by consumer's request */
856 if (rb
->interrupt_mask
) {
861 * The consumer hasn't caught up with the producer's previous state so it's
863 * (last_seen_rd_idx comes from the guest but it's safe to use w/o
864 * validation here as it only affects notification.)
866 if (rb_idx_delta(ringbuf
->last_seen_rd_idx
, ringbuf
->wr_idx
,
867 ringbuf
->common
.len
, true) > written
) {
871 vmbus_channel_notify_guest(chan
);
873 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, true);
874 ringbuf
->last_wr_idx
= ringbuf
->wr_idx
;
878 int vmbus_channel_reserve(VMBusChannel
*chan
,
879 uint32_t desclen
, uint32_t msglen
)
881 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
882 vmbus_ring_buffer
*rb
= NULL
;
883 vmbus_packet_hdr hdr
;
884 uint32_t needed
= ringbuf
->reserved
+
885 vmbus_pkt_hdr_set_offsets(&hdr
, desclen
, msglen
);
887 /* avoid touching the guest memory if possible */
888 if (likely(needed
<= ringbuf_send_avail(ringbuf
))) {
892 rb
= ringbuf_map_hdr(&ringbuf
->common
);
897 /* fetch read index from guest memory and try again */
898 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
900 if (likely(needed
<= ringbuf_send_avail(ringbuf
))) {
904 rb
->pending_send_sz
= needed
;
907 * The consumer may have made progress and freed up some space before
908 * seeing updated pending_send_sz, so re-read read_index (preventing
909 * reorder with the pending_send_sz write) and try again.
911 smp_mb(); /* barrier pair [A] */
912 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
914 if (needed
> ringbuf_send_avail(ringbuf
)) {
919 ringbuf
->reserved
= needed
;
922 /* clear pending_send_sz if it was set */
923 if (ringbuf
->wanted
) {
925 rb
= ringbuf_map_hdr(&ringbuf
->common
);
927 /* failure to clear pending_send_sz is non-fatal */
932 rb
->pending_send_sz
= 0;
935 /* prevent reorder of the following data operation with read_index read */
936 smp_mb(); /* barrier pair [B] */
940 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, ringbuf
->wanted
== needed
);
942 ringbuf
->wanted
= needed
;
943 return needed
? -ENOSPC
: 0;
946 ssize_t
vmbus_channel_send(VMBusChannel
*chan
, uint16_t pkt_type
,
947 void *desc
, uint32_t desclen
,
948 void *msg
, uint32_t msglen
,
949 bool need_comp
, uint64_t transaction_id
)
952 vmbus_packet_hdr hdr
;
954 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
956 if (!vmbus_channel_is_open(chan
)) {
960 totlen
= vmbus_pkt_hdr_set_offsets(&hdr
, desclen
, msglen
);
962 hdr
.flags
= need_comp
? VMBUS_PACKET_FLAG_REQUEST_COMPLETION
: 0;
963 hdr
.transaction_id
= transaction_id
;
965 assert(totlen
<= ringbuf
->reserved
);
967 ringbuf_start_io(&ringbuf
->common
);
968 ringbuf_seek(&ringbuf
->common
, ringbuf
->wr_idx
);
969 ret
= ringbuf_io(&ringbuf
->common
, &hdr
, sizeof(hdr
));
975 ret
= ringbuf_io(&ringbuf
->common
, desc
, desclen
);
979 ringbuf_seek(&ringbuf
->common
,
980 ringbuf
->wr_idx
+ hdr
.offset_qwords
* sizeof(uint64_t));
982 ret
= ringbuf_io(&ringbuf
->common
, msg
, msglen
);
986 ringbuf_seek(&ringbuf
->common
, ringbuf
->wr_idx
+ totlen
);
987 ringbuf
->wr_idx
= ringbuf_tell(&ringbuf
->common
);
990 ringbuf_end_io(&ringbuf
->common
);
994 return ringbuf_send_update_idx(chan
);
997 ssize_t
vmbus_channel_send_completion(VMBusChanReq
*req
,
998 void *msg
, uint32_t msglen
)
1000 assert(req
->need_comp
);
1001 return vmbus_channel_send(req
->chan
, VMBUS_PACKET_COMP
, NULL
, 0,
1002 msg
, msglen
, false, req
->transaction_id
);
1005 static int sgl_from_gpa_ranges(QEMUSGList
*sgl
, VMBusDevice
*dev
,
1006 VMBusRingBufCommon
*ringbuf
, uint32_t len
)
1009 vmbus_pkt_gpa_direct hdr
;
1014 if (len
< sizeof(hdr
)) {
1017 ret
= ringbuf_io(ringbuf
, &hdr
, sizeof(hdr
));
1023 num
= (len
- hdr
.rangecount
* sizeof(vmbus_gpa_range
)) / sizeof(uint64_t);
1027 qemu_sglist_init(sgl
, DEVICE(dev
), num
, ringbuf
->as
);
1029 for (; hdr
.rangecount
; hdr
.rangecount
--) {
1030 vmbus_gpa_range range
;
1032 if (len
< sizeof(range
)) {
1035 ret
= ringbuf_io(ringbuf
, &range
, sizeof(range
));
1039 len
-= sizeof(range
);
1041 if (range
.byte_offset
& TARGET_PAGE_MASK
) {
1045 for (; range
.byte_count
; range
.byte_offset
= 0) {
1047 uint32_t plen
= MIN(range
.byte_count
,
1048 TARGET_PAGE_SIZE
- range
.byte_offset
);
1050 if (len
< sizeof(uint64_t)) {
1053 ret
= ringbuf_io(ringbuf
, &paddr
, sizeof(paddr
));
1057 len
-= sizeof(uint64_t);
1058 paddr
<<= TARGET_PAGE_BITS
;
1059 paddr
|= range
.byte_offset
;
1060 range
.byte_count
-= plen
;
1062 if (curaddr
+ curlen
== paddr
) {
1063 /* consecutive fragments - join */
1067 qemu_sglist_add(sgl
, curaddr
, curlen
);
1077 qemu_sglist_add(sgl
, curaddr
, curlen
);
1084 qemu_sglist_destroy(sgl
);
1088 static VMBusChanReq
*vmbus_alloc_req(VMBusChannel
*chan
,
1089 uint32_t size
, uint16_t pkt_type
,
1090 uint32_t msglen
, uint64_t transaction_id
,
1094 uint32_t msgoff
= QEMU_ALIGN_UP(size
, __alignof__(*req
->msg
));
1095 uint32_t totlen
= msgoff
+ msglen
;
1097 req
= g_malloc0(totlen
);
1099 req
->pkt_type
= pkt_type
;
1100 req
->msg
= (void *)req
+ msgoff
;
1101 req
->msglen
= msglen
;
1102 req
->transaction_id
= transaction_id
;
1103 req
->need_comp
= need_comp
;
1107 int vmbus_channel_recv_start(VMBusChannel
*chan
)
1109 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1110 vmbus_ring_buffer
*rb
;
1112 rb
= ringbuf_map_hdr(&ringbuf
->common
);
1116 ringbuf
->last_seen_wr_idx
= rb
->write_index
;
1117 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, false);
1119 if (ringbuf
->last_seen_wr_idx
>= ringbuf
->common
.len
) {
1123 /* prevent reorder of the following data operation with write_index read */
1124 smp_mb(); /* barrier pair [C] */
1128 void *vmbus_channel_recv_peek(VMBusChannel
*chan
, uint32_t size
)
1130 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1131 vmbus_packet_hdr hdr
= {};
1134 uint32_t totlen
, pktlen
, msglen
, msgoff
, desclen
;
1136 assert(size
>= sizeof(*req
));
1138 /* safe as last_seen_wr_idx is validated in vmbus_channel_recv_start */
1139 avail
= rb_idx_delta(ringbuf
->rd_idx
, ringbuf
->last_seen_wr_idx
,
1140 ringbuf
->common
.len
, true);
1141 if (avail
< sizeof(hdr
)) {
1145 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
);
1146 if (ringbuf_io(&ringbuf
->common
, &hdr
, sizeof(hdr
)) < 0) {
1150 pktlen
= hdr
.len_qwords
* sizeof(uint64_t);
1151 totlen
= pktlen
+ VMBUS_PKT_TRAILER
;
1152 if (totlen
> avail
) {
1156 msgoff
= hdr
.offset_qwords
* sizeof(uint64_t);
1157 if (msgoff
> pktlen
|| msgoff
< sizeof(hdr
)) {
1158 error_report("%s: malformed packet: %u %u", __func__
, msgoff
, pktlen
);
1162 msglen
= pktlen
- msgoff
;
1164 req
= vmbus_alloc_req(chan
, size
, hdr
.type
, msglen
, hdr
.transaction_id
,
1165 hdr
.flags
& VMBUS_PACKET_FLAG_REQUEST_COMPLETION
);
1168 case VMBUS_PACKET_DATA_USING_GPA_DIRECT
:
1169 desclen
= msgoff
- sizeof(hdr
);
1170 if (sgl_from_gpa_ranges(&req
->sgl
, chan
->dev
, &ringbuf
->common
,
1172 error_report("%s: failed to convert GPA ranges to SGL", __func__
);
1176 case VMBUS_PACKET_DATA_INBAND
:
1177 case VMBUS_PACKET_COMP
:
1180 error_report("%s: unexpected msg type: %x", __func__
, hdr
.type
);
1184 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
+ msgoff
);
1185 if (ringbuf_io(&ringbuf
->common
, req
->msg
, msglen
) < 0) {
1188 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
+ totlen
);
1192 vmbus_free_req(req
);
1196 void vmbus_channel_recv_pop(VMBusChannel
*chan
)
1198 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1199 ringbuf
->rd_idx
= ringbuf_tell(&ringbuf
->common
);
1202 ssize_t
vmbus_channel_recv_done(VMBusChannel
*chan
)
1204 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1205 vmbus_ring_buffer
*rb
;
1208 read
= rb_idx_delta(ringbuf
->last_rd_idx
, ringbuf
->rd_idx
,
1209 ringbuf
->common
.len
, true);
1214 rb
= ringbuf_map_hdr(&ringbuf
->common
);
1219 /* prevent reorder with the data operation and packet read */
1220 smp_mb(); /* barrier pair [B] */
1221 rb
->read_index
= ringbuf
->rd_idx
;
1223 /* prevent reorder of the following pending_send_sz read */
1224 smp_mb(); /* barrier pair [A] */
1226 if (rb
->interrupt_mask
) {
1230 if (rb
->feature_bits
& VMBUS_RING_BUFFER_FEAT_PENDING_SZ
) {
1231 uint32_t wr_idx
, wr_avail
;
1232 uint32_t wanted
= rb
->pending_send_sz
;
1238 /* prevent reorder with pending_send_sz read */
1239 smp_rmb(); /* barrier pair [D] */
1240 wr_idx
= rb
->write_index
;
1242 wr_avail
= rb_idx_delta(wr_idx
, ringbuf
->rd_idx
, ringbuf
->common
.len
,
1245 /* the producer wasn't blocked on the consumer state */
1246 if (wr_avail
>= read
+ wanted
) {
1249 /* there's not enough space for the producer to make progress */
1250 if (wr_avail
< wanted
) {
1255 vmbus_channel_notify_guest(chan
);
1257 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, true);
1258 ringbuf
->last_rd_idx
= ringbuf
->rd_idx
;
1262 void vmbus_free_req(void *req
)
1264 VMBusChanReq
*r
= req
;
1271 qemu_sglist_destroy(&r
->sgl
);
1276 static void channel_event_cb(EventNotifier
*e
)
1278 VMBusChannel
*chan
= container_of(e
, VMBusChannel
, notifier
);
1279 if (event_notifier_test_and_clear(e
)) {
1281 * All receives are supposed to happen within the device worker, so
1282 * bracket it with ringbuf_start/end_io on the receive ringbuffer, and
1283 * potentially reuse the cached mapping throughout the worker.
1284 * Can't do this for sends as they may happen outside the device
1287 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1288 ringbuf_start_io(&ringbuf
->common
);
1289 chan
->notify_cb(chan
);
1290 ringbuf_end_io(&ringbuf
->common
);
1295 static int alloc_chan_id(VMBus
*vmbus
)
1299 ret
= find_next_zero_bit(vmbus
->chanid_bitmap
, VMBUS_CHANID_COUNT
, 0);
1300 if (ret
== VMBUS_CHANID_COUNT
) {
1303 return ret
+ VMBUS_FIRST_CHANID
;
1306 static int register_chan_id(VMBusChannel
*chan
)
1308 return test_and_set_bit(chan
->id
- VMBUS_FIRST_CHANID
,
1309 chan
->vmbus
->chanid_bitmap
) ? -EEXIST
: 0;
1312 static void unregister_chan_id(VMBusChannel
*chan
)
1314 clear_bit(chan
->id
- VMBUS_FIRST_CHANID
, chan
->vmbus
->chanid_bitmap
);
1317 static uint32_t chan_connection_id(VMBusChannel
*chan
)
1319 return VMBUS_CHAN_CONNECTION_OFFSET
+ chan
->id
;
1322 static void init_channel(VMBus
*vmbus
, VMBusDevice
*dev
, VMBusDeviceClass
*vdc
,
1323 VMBusChannel
*chan
, uint16_t idx
, Error
**errp
)
1328 chan
->notify_cb
= vdc
->chan_notify_cb
;
1329 chan
->subchan_idx
= idx
;
1330 chan
->vmbus
= vmbus
;
1332 res
= alloc_chan_id(vmbus
);
1334 error_setg(errp
, "no spare channel id");
1338 register_chan_id(chan
);
1341 * The guest drivers depend on the device subchannels (idx #1+) to be
1342 * offered after the primary channel (idx #0) of that device. To ensure
1343 * that, record the channels on the channel list in the order they appear
1344 * within the device.
1346 QTAILQ_INSERT_TAIL(&vmbus
->channel_list
, chan
, link
);
1349 static void deinit_channel(VMBusChannel
*chan
)
1351 assert(chan
->state
== VMCHAN_INIT
);
1352 QTAILQ_REMOVE(&chan
->vmbus
->channel_list
, chan
, link
);
1353 unregister_chan_id(chan
);
1356 static void create_channels(VMBus
*vmbus
, VMBusDevice
*dev
, Error
**errp
)
1359 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(dev
);
1362 dev
->num_channels
= vdc
->num_channels
? vdc
->num_channels(dev
) : 1;
1363 if (dev
->num_channels
< 1) {
1364 error_setg(errp
, "invalid #channels: %u", dev
->num_channels
);
1368 dev
->channels
= g_new0(VMBusChannel
, dev
->num_channels
);
1369 for (i
= 0; i
< dev
->num_channels
; i
++) {
1370 init_channel(vmbus
, dev
, vdc
, &dev
->channels
[i
], i
, &err
);
1380 deinit_channel(&dev
->channels
[i
]);
1382 error_propagate(errp
, err
);
1385 static void free_channels(VMBusDevice
*dev
)
1388 for (i
= 0; i
< dev
->num_channels
; i
++) {
1389 deinit_channel(&dev
->channels
[i
]);
1391 g_free(dev
->channels
);
1394 static HvSintRoute
*make_sint_route(VMBus
*vmbus
, uint32_t vp_index
)
1398 if (vp_index
== vmbus
->target_vp
) {
1399 hyperv_sint_route_ref(vmbus
->sint_route
);
1400 return vmbus
->sint_route
;
1403 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1404 if (chan
->target_vp
== vp_index
&& vmbus_channel_is_open(chan
)) {
1405 hyperv_sint_route_ref(chan
->notify_route
);
1406 return chan
->notify_route
;
1410 return hyperv_sint_route_new(vp_index
, VMBUS_SINT
, NULL
, NULL
);
1413 static void open_channel(VMBusChannel
*chan
)
1415 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1417 chan
->gpadl
= vmbus_get_gpadl(chan
, chan
->ringbuf_gpadl
);
1422 if (ringbufs_init(chan
)) {
1426 if (event_notifier_init(&chan
->notifier
, 0)) {
1430 event_notifier_set_handler(&chan
->notifier
, channel_event_cb
);
1432 if (hyperv_set_event_flag_handler(chan_connection_id(chan
),
1434 goto cleanup_notifier
;
1437 chan
->notify_route
= make_sint_route(chan
->vmbus
, chan
->target_vp
);
1438 if (!chan
->notify_route
) {
1439 goto clear_event_flag_handler
;
1442 if (vdc
->open_channel
&& vdc
->open_channel(chan
)) {
1443 goto unref_sint_route
;
1446 chan
->is_open
= true;
1450 hyperv_sint_route_unref(chan
->notify_route
);
1451 clear_event_flag_handler
:
1452 hyperv_set_event_flag_handler(chan_connection_id(chan
), NULL
);
1454 event_notifier_set_handler(&chan
->notifier
, NULL
);
1455 event_notifier_cleanup(&chan
->notifier
);
1457 vmbus_put_gpadl(chan
->gpadl
);
1460 static void close_channel(VMBusChannel
*chan
)
1462 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1464 if (!chan
->is_open
) {
1468 if (vdc
->close_channel
) {
1469 vdc
->close_channel(chan
);
1472 hyperv_sint_route_unref(chan
->notify_route
);
1473 hyperv_set_event_flag_handler(chan_connection_id(chan
), NULL
);
1474 event_notifier_set_handler(&chan
->notifier
, NULL
);
1475 event_notifier_cleanup(&chan
->notifier
);
1476 vmbus_put_gpadl(chan
->gpadl
);
1477 chan
->is_open
= false;
1480 static int channel_post_load(void *opaque
, int version_id
)
1482 VMBusChannel
*chan
= opaque
;
1484 return register_chan_id(chan
);
1487 static const VMStateDescription vmstate_channel
= {
1488 .name
= "vmbus/channel",
1490 .minimum_version_id
= 0,
1491 .post_load
= channel_post_load
,
1492 .fields
= (const VMStateField
[]) {
1493 VMSTATE_UINT32(id
, VMBusChannel
),
1494 VMSTATE_UINT16(subchan_idx
, VMBusChannel
),
1495 VMSTATE_UINT32(open_id
, VMBusChannel
),
1496 VMSTATE_UINT32(target_vp
, VMBusChannel
),
1497 VMSTATE_UINT32(ringbuf_gpadl
, VMBusChannel
),
1498 VMSTATE_UINT32(ringbuf_send_offset
, VMBusChannel
),
1499 VMSTATE_UINT8(offer_state
, VMBusChannel
),
1500 VMSTATE_UINT8(state
, VMBusChannel
),
1501 VMSTATE_END_OF_LIST()
1505 static VMBusChannel
*find_channel(VMBus
*vmbus
, uint32_t id
)
1508 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1509 if (chan
->id
== id
) {
1516 static int enqueue_incoming_message(VMBus
*vmbus
,
1517 const struct hyperv_post_message_input
*msg
)
1520 uint8_t idx
, prev_size
;
1522 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
1524 if (vmbus
->rx_queue_size
== HV_MSG_QUEUE_LEN
) {
1529 prev_size
= vmbus
->rx_queue_size
;
1530 idx
= (vmbus
->rx_queue_head
+ vmbus
->rx_queue_size
) % HV_MSG_QUEUE_LEN
;
1531 memcpy(&vmbus
->rx_queue
[idx
], msg
, sizeof(*msg
));
1532 vmbus
->rx_queue_size
++;
1534 /* only need to resched if the queue was empty before */
1536 vmbus_resched(vmbus
);
1539 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
1543 static uint16_t vmbus_recv_message(const struct hyperv_post_message_input
*msg
,
1546 VMBus
*vmbus
= data
;
1547 struct vmbus_message_header
*vmbus_msg
;
1549 if (msg
->message_type
!= HV_MESSAGE_VMBUS
) {
1550 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1553 if (msg
->payload_size
< sizeof(struct vmbus_message_header
)) {
1554 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1557 vmbus_msg
= (struct vmbus_message_header
*)msg
->payload
;
1559 trace_vmbus_recv_message(vmbus_msg
->message_type
, msg
->payload_size
);
1561 if (vmbus_msg
->message_type
== VMBUS_MSG_INVALID
||
1562 vmbus_msg
->message_type
>= VMBUS_MSG_COUNT
) {
1563 error_report("vmbus: unknown message type %#x",
1564 vmbus_msg
->message_type
);
1565 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1568 if (enqueue_incoming_message(vmbus
, msg
)) {
1569 return HV_STATUS_INSUFFICIENT_BUFFERS
;
1571 return HV_STATUS_SUCCESS
;
1574 static bool vmbus_initialized(VMBus
*vmbus
)
1576 return vmbus
->version
> 0 && vmbus
->version
<= VMBUS_VERSION_CURRENT
;
1579 static void vmbus_reset_all(VMBus
*vmbus
)
1581 bus_cold_reset(BUS(vmbus
));
1584 static void post_msg(VMBus
*vmbus
, void *msgdata
, uint32_t msglen
)
1587 struct hyperv_message msg
= {
1588 .header
.message_type
= HV_MESSAGE_VMBUS
,
1591 assert(!vmbus
->msg_in_progress
);
1592 assert(msglen
<= sizeof(msg
.payload
));
1593 assert(msglen
>= sizeof(struct vmbus_message_header
));
1595 vmbus
->msg_in_progress
= true;
1597 trace_vmbus_post_msg(((struct vmbus_message_header
*)msgdata
)->message_type
,
1600 memcpy(msg
.payload
, msgdata
, msglen
);
1601 msg
.header
.payload_size
= ROUND_UP(msglen
, VMBUS_MESSAGE_SIZE_ALIGN
);
1603 ret
= hyperv_post_msg(vmbus
->sint_route
, &msg
);
1604 if (ret
== 0 || ret
== -EAGAIN
) {
1608 error_report("message delivery fatal failure: %d; aborting vmbus", ret
);
1609 vmbus_reset_all(vmbus
);
1612 static int vmbus_init(VMBus
*vmbus
)
1614 if (vmbus
->target_vp
!= (uint32_t)-1) {
1615 vmbus
->sint_route
= hyperv_sint_route_new(vmbus
->target_vp
, VMBUS_SINT
,
1616 vmbus_msg_cb
, vmbus
);
1617 if (!vmbus
->sint_route
) {
1618 error_report("failed to set up SINT route");
1625 static void vmbus_deinit(VMBus
*vmbus
)
1627 VMBusGpadl
*gpadl
, *tmp_gpadl
;
1630 QTAILQ_FOREACH_SAFE(gpadl
, &vmbus
->gpadl_list
, link
, tmp_gpadl
) {
1631 if (gpadl
->state
== VMGPADL_TORNDOWN
) {
1634 vmbus_put_gpadl(gpadl
);
1637 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1638 chan
->offer_state
= VMOFFER_INIT
;
1641 hyperv_sint_route_unref(vmbus
->sint_route
);
1642 vmbus
->sint_route
= NULL
;
1643 vmbus
->int_page_gpa
= 0;
1644 vmbus
->target_vp
= (uint32_t)-1;
1646 vmbus
->state
= VMBUS_LISTEN
;
1647 vmbus
->msg_in_progress
= false;
1650 static void handle_initiate_contact(VMBus
*vmbus
,
1651 vmbus_message_initiate_contact
*msg
,
1654 if (msglen
< sizeof(*msg
)) {
1658 trace_vmbus_initiate_contact(msg
->version_requested
>> 16,
1659 msg
->version_requested
& 0xffff,
1660 msg
->target_vcpu
, msg
->monitor_page1
,
1661 msg
->monitor_page2
, msg
->interrupt_page
);
1664 * Reset vmbus on INITIATE_CONTACT regardless of its previous state.
1665 * Useful, in particular, with vmbus-aware BIOS which can't shut vmbus down
1666 * before handing over to OS loader.
1668 vmbus_reset_all(vmbus
);
1670 vmbus
->target_vp
= msg
->target_vcpu
;
1671 vmbus
->version
= msg
->version_requested
;
1672 if (vmbus
->version
< VMBUS_VERSION_WIN8
) {
1673 /* linux passes interrupt page even when it doesn't need it */
1674 vmbus
->int_page_gpa
= msg
->interrupt_page
;
1676 vmbus
->state
= VMBUS_HANDSHAKE
;
1678 if (vmbus_init(vmbus
)) {
1679 error_report("failed to init vmbus; aborting");
1680 vmbus_deinit(vmbus
);
1685 static void send_handshake(VMBus
*vmbus
)
1687 struct vmbus_message_version_response msg
= {
1688 .header
.message_type
= VMBUS_MSG_VERSION_RESPONSE
,
1689 .version_supported
= vmbus_initialized(vmbus
),
1692 post_msg(vmbus
, &msg
, sizeof(msg
));
1695 static void handle_request_offers(VMBus
*vmbus
, void *msgdata
, uint32_t msglen
)
1699 if (!vmbus_initialized(vmbus
)) {
1703 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1704 if (chan
->offer_state
== VMOFFER_INIT
) {
1705 chan
->offer_state
= VMOFFER_SENDING
;
1710 vmbus
->state
= VMBUS_OFFER
;
1713 static void send_offer(VMBus
*vmbus
)
1716 struct vmbus_message_header alloffers_msg
= {
1717 .message_type
= VMBUS_MSG_ALLOFFERS_DELIVERED
,
1720 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1721 if (chan
->offer_state
== VMOFFER_SENDING
) {
1722 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1723 /* Hyper-V wants LE GUIDs */
1724 QemuUUID classid
= qemu_uuid_bswap(vdc
->classid
);
1725 QemuUUID instanceid
= qemu_uuid_bswap(chan
->dev
->instanceid
);
1726 struct vmbus_message_offer_channel msg
= {
1727 .header
.message_type
= VMBUS_MSG_OFFERCHANNEL
,
1728 .child_relid
= chan
->id
,
1729 .connection_id
= chan_connection_id(chan
),
1730 .channel_flags
= vdc
->channel_flags
,
1731 .mmio_size_mb
= vdc
->mmio_size_mb
,
1732 .sub_channel_index
= vmbus_channel_idx(chan
),
1733 .interrupt_flags
= VMBUS_OFFER_INTERRUPT_DEDICATED
,
1736 memcpy(msg
.type_uuid
, &classid
, sizeof(classid
));
1737 memcpy(msg
.instance_uuid
, &instanceid
, sizeof(instanceid
));
1739 trace_vmbus_send_offer(chan
->id
, chan
->dev
);
1741 post_msg(vmbus
, &msg
, sizeof(msg
));
1746 /* no more offers, send terminator message */
1747 trace_vmbus_terminate_offers();
1748 post_msg(vmbus
, &alloffers_msg
, sizeof(alloffers_msg
));
1751 static bool complete_offer(VMBus
*vmbus
)
1755 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1756 if (chan
->offer_state
== VMOFFER_SENDING
) {
1757 chan
->offer_state
= VMOFFER_SENT
;
1762 * no transitioning channels found so this is completing the terminator
1763 * message, and vmbus can move to the next state
1768 /* try to mark another channel for offering */
1769 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1770 if (chan
->offer_state
== VMOFFER_INIT
) {
1771 chan
->offer_state
= VMOFFER_SENDING
;
1776 * if an offer has been sent there are more offers or the terminator yet to
1777 * send, so no state transition for vmbus
1783 static void handle_gpadl_header(VMBus
*vmbus
, vmbus_message_gpadl_header
*msg
,
1787 uint32_t num_gfns
, i
;
1789 /* must include at least one gpa range */
1790 if (msglen
< sizeof(*msg
) + sizeof(msg
->range
[0]) ||
1791 !vmbus_initialized(vmbus
)) {
1795 num_gfns
= (msg
->range_buflen
- msg
->rangecount
* sizeof(msg
->range
[0])) /
1796 sizeof(msg
->range
[0].pfn_array
[0]);
1798 trace_vmbus_gpadl_header(msg
->gpadl_id
, num_gfns
);
1801 * In theory the GPADL_HEADER message can define a GPADL with multiple GPA
1802 * ranges each with arbitrary size and alignment. However in practice only
1803 * single-range page-aligned GPADLs have been observed so just ignore
1804 * anything else and simplify things greatly.
1806 if (msg
->rangecount
!= 1 || msg
->range
[0].byte_offset
||
1807 (msg
->range
[0].byte_count
!= (num_gfns
<< TARGET_PAGE_BITS
))) {
1811 /* ignore requests to create already existing GPADLs */
1812 if (find_gpadl(vmbus
, msg
->gpadl_id
)) {
1816 gpadl
= create_gpadl(vmbus
, msg
->gpadl_id
, msg
->child_relid
, num_gfns
);
1818 for (i
= 0; i
< num_gfns
&&
1819 (void *)&msg
->range
[0].pfn_array
[i
+ 1] <= (void *)msg
+ msglen
;
1821 gpadl
->gfns
[gpadl
->seen_gfns
++] = msg
->range
[0].pfn_array
[i
];
1824 if (gpadl_full(gpadl
)) {
1825 vmbus
->state
= VMBUS_CREATE_GPADL
;
1829 static void handle_gpadl_body(VMBus
*vmbus
, vmbus_message_gpadl_body
*msg
,
1833 uint32_t num_gfns_left
, i
;
1835 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
1839 trace_vmbus_gpadl_body(msg
->gpadl_id
);
1841 gpadl
= find_gpadl(vmbus
, msg
->gpadl_id
);
1846 num_gfns_left
= gpadl
->num_gfns
- gpadl
->seen_gfns
;
1847 assert(num_gfns_left
);
1849 for (i
= 0; i
< num_gfns_left
&&
1850 (void *)&msg
->pfn_array
[i
+ 1] <= (void *)msg
+ msglen
; i
++) {
1851 gpadl
->gfns
[gpadl
->seen_gfns
++] = msg
->pfn_array
[i
];
1854 if (gpadl_full(gpadl
)) {
1855 vmbus
->state
= VMBUS_CREATE_GPADL
;
1859 static void send_create_gpadl(VMBus
*vmbus
)
1863 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1864 if (gpadl_full(gpadl
) && gpadl
->state
== VMGPADL_INIT
) {
1865 struct vmbus_message_gpadl_created msg
= {
1866 .header
.message_type
= VMBUS_MSG_GPADL_CREATED
,
1867 .gpadl_id
= gpadl
->id
,
1868 .child_relid
= gpadl
->child_relid
,
1871 trace_vmbus_gpadl_created(gpadl
->id
);
1872 post_msg(vmbus
, &msg
, sizeof(msg
));
1877 g_assert_not_reached();
1880 static bool complete_create_gpadl(VMBus
*vmbus
)
1884 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1885 if (gpadl_full(gpadl
) && gpadl
->state
== VMGPADL_INIT
) {
1886 gpadl
->state
= VMGPADL_ALIVE
;
1892 g_assert_not_reached();
1895 static void handle_gpadl_teardown(VMBus
*vmbus
,
1896 vmbus_message_gpadl_teardown
*msg
,
1901 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
1905 trace_vmbus_gpadl_teardown(msg
->gpadl_id
);
1907 gpadl
= find_gpadl(vmbus
, msg
->gpadl_id
);
1908 if (!gpadl
|| gpadl
->state
== VMGPADL_TORNDOWN
) {
1912 gpadl
->state
= VMGPADL_TEARINGDOWN
;
1913 vmbus
->state
= VMBUS_TEARDOWN_GPADL
;
1916 static void send_teardown_gpadl(VMBus
*vmbus
)
1920 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1921 if (gpadl
->state
== VMGPADL_TEARINGDOWN
) {
1922 struct vmbus_message_gpadl_torndown msg
= {
1923 .header
.message_type
= VMBUS_MSG_GPADL_TORNDOWN
,
1924 .gpadl_id
= gpadl
->id
,
1927 trace_vmbus_gpadl_torndown(gpadl
->id
);
1928 post_msg(vmbus
, &msg
, sizeof(msg
));
1933 g_assert_not_reached();
1936 static bool complete_teardown_gpadl(VMBus
*vmbus
)
1940 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1941 if (gpadl
->state
== VMGPADL_TEARINGDOWN
) {
1942 gpadl
->state
= VMGPADL_TORNDOWN
;
1943 vmbus_put_gpadl(gpadl
);
1948 g_assert_not_reached();
1951 static void handle_open_channel(VMBus
*vmbus
, vmbus_message_open_channel
*msg
,
1956 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
1960 trace_vmbus_open_channel(msg
->child_relid
, msg
->ring_buffer_gpadl_id
,
1962 chan
= find_channel(vmbus
, msg
->child_relid
);
1963 if (!chan
|| chan
->state
!= VMCHAN_INIT
) {
1967 chan
->ringbuf_gpadl
= msg
->ring_buffer_gpadl_id
;
1968 chan
->ringbuf_send_offset
= msg
->ring_buffer_offset
;
1969 chan
->target_vp
= msg
->target_vp
;
1970 chan
->open_id
= msg
->open_id
;
1974 chan
->state
= VMCHAN_OPENING
;
1975 vmbus
->state
= VMBUS_OPEN_CHANNEL
;
1978 static void send_open_channel(VMBus
*vmbus
)
1982 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1983 if (chan
->state
== VMCHAN_OPENING
) {
1984 struct vmbus_message_open_result msg
= {
1985 .header
.message_type
= VMBUS_MSG_OPENCHANNEL_RESULT
,
1986 .child_relid
= chan
->id
,
1987 .open_id
= chan
->open_id
,
1988 .status
= !vmbus_channel_is_open(chan
),
1991 trace_vmbus_channel_open(chan
->id
, msg
.status
);
1992 post_msg(vmbus
, &msg
, sizeof(msg
));
1997 g_assert_not_reached();
2000 static bool complete_open_channel(VMBus
*vmbus
)
2004 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2005 if (chan
->state
== VMCHAN_OPENING
) {
2006 if (vmbus_channel_is_open(chan
)) {
2007 chan
->state
= VMCHAN_OPEN
;
2009 * simulate guest notification of ringbuffer space made
2010 * available, for the channel protocols where the host
2011 * initiates the communication
2013 vmbus_channel_notify_host(chan
);
2015 chan
->state
= VMCHAN_INIT
;
2021 g_assert_not_reached();
2024 static void vdev_reset_on_close(VMBusDevice
*vdev
)
2028 for (i
= 0; i
< vdev
->num_channels
; i
++) {
2029 if (vmbus_channel_is_open(&vdev
->channels
[i
])) {
2034 /* all channels closed -- reset device */
2035 device_cold_reset(DEVICE(vdev
));
2038 static void handle_close_channel(VMBus
*vmbus
, vmbus_message_close_channel
*msg
,
2043 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2047 trace_vmbus_close_channel(msg
->child_relid
);
2049 chan
= find_channel(vmbus
, msg
->child_relid
);
2054 close_channel(chan
);
2055 chan
->state
= VMCHAN_INIT
;
2057 vdev_reset_on_close(chan
->dev
);
2060 static void handle_unload(VMBus
*vmbus
, void *msg
, uint32_t msglen
)
2062 vmbus
->state
= VMBUS_UNLOAD
;
2065 static void send_unload(VMBus
*vmbus
)
2067 vmbus_message_header msg
= {
2068 .message_type
= VMBUS_MSG_UNLOAD_RESPONSE
,
2071 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
2072 vmbus
->rx_queue_size
= 0;
2073 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
2075 post_msg(vmbus
, &msg
, sizeof(msg
));
2079 static bool complete_unload(VMBus
*vmbus
)
2081 vmbus_reset_all(vmbus
);
2085 static void process_message(VMBus
*vmbus
)
2087 struct hyperv_post_message_input
*hv_msg
;
2088 struct vmbus_message_header
*msg
;
2092 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
2094 if (!vmbus
->rx_queue_size
) {
2098 hv_msg
= &vmbus
->rx_queue
[vmbus
->rx_queue_head
];
2099 msglen
= hv_msg
->payload_size
;
2100 if (msglen
< sizeof(*msg
)) {
2103 msgdata
= hv_msg
->payload
;
2106 trace_vmbus_process_incoming_message(msg
->message_type
);
2108 switch (msg
->message_type
) {
2109 case VMBUS_MSG_INITIATE_CONTACT
:
2110 handle_initiate_contact(vmbus
, msgdata
, msglen
);
2112 case VMBUS_MSG_REQUESTOFFERS
:
2113 handle_request_offers(vmbus
, msgdata
, msglen
);
2115 case VMBUS_MSG_GPADL_HEADER
:
2116 handle_gpadl_header(vmbus
, msgdata
, msglen
);
2118 case VMBUS_MSG_GPADL_BODY
:
2119 handle_gpadl_body(vmbus
, msgdata
, msglen
);
2121 case VMBUS_MSG_GPADL_TEARDOWN
:
2122 handle_gpadl_teardown(vmbus
, msgdata
, msglen
);
2124 case VMBUS_MSG_OPENCHANNEL
:
2125 handle_open_channel(vmbus
, msgdata
, msglen
);
2127 case VMBUS_MSG_CLOSECHANNEL
:
2128 handle_close_channel(vmbus
, msgdata
, msglen
);
2130 case VMBUS_MSG_UNLOAD
:
2131 handle_unload(vmbus
, msgdata
, msglen
);
2134 error_report("unknown message type %#x", msg
->message_type
);
2139 vmbus
->rx_queue_size
--;
2140 vmbus
->rx_queue_head
++;
2141 vmbus
->rx_queue_head
%= HV_MSG_QUEUE_LEN
;
2143 vmbus_resched(vmbus
);
2145 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
2148 static const struct {
2149 void (*run
)(VMBus
*vmbus
);
2150 bool (*complete
)(VMBus
*vmbus
);
2151 } state_runner
[] = {
2152 [VMBUS_LISTEN
] = {process_message
, NULL
},
2153 [VMBUS_HANDSHAKE
] = {send_handshake
, NULL
},
2154 [VMBUS_OFFER
] = {send_offer
, complete_offer
},
2155 [VMBUS_CREATE_GPADL
] = {send_create_gpadl
, complete_create_gpadl
},
2156 [VMBUS_TEARDOWN_GPADL
] = {send_teardown_gpadl
, complete_teardown_gpadl
},
2157 [VMBUS_OPEN_CHANNEL
] = {send_open_channel
, complete_open_channel
},
2158 [VMBUS_UNLOAD
] = {send_unload
, complete_unload
},
2161 static void vmbus_do_run(VMBus
*vmbus
)
2163 if (vmbus
->msg_in_progress
) {
2167 assert(vmbus
->state
< VMBUS_STATE_MAX
);
2168 assert(state_runner
[vmbus
->state
].run
);
2169 state_runner
[vmbus
->state
].run(vmbus
);
2172 static void vmbus_run(void *opaque
)
2174 VMBus
*vmbus
= opaque
;
2176 /* make sure no recursion happens (e.g. due to recursive aio_poll()) */
2177 if (vmbus
->in_progress
) {
2181 vmbus
->in_progress
= true;
2183 * FIXME: if vmbus_resched() is called from within vmbus_do_run(), it
2184 * should go *after* the code that can result in aio_poll; otherwise
2185 * reschedules can be missed. No idea how to enforce that.
2187 vmbus_do_run(vmbus
);
2188 vmbus
->in_progress
= false;
2191 static void vmbus_msg_cb(void *data
, int status
)
2193 VMBus
*vmbus
= data
;
2194 bool (*complete
)(VMBus
*vmbus
);
2196 assert(vmbus
->msg_in_progress
);
2198 trace_vmbus_msg_cb(status
);
2200 if (status
== -EAGAIN
) {
2204 error_report("message delivery fatal failure: %d; aborting vmbus",
2206 vmbus_reset_all(vmbus
);
2210 assert(vmbus
->state
< VMBUS_STATE_MAX
);
2211 complete
= state_runner
[vmbus
->state
].complete
;
2212 if (!complete
|| complete(vmbus
)) {
2213 vmbus
->state
= VMBUS_LISTEN
;
2216 vmbus
->msg_in_progress
= false;
2217 vmbus_resched(vmbus
);
2220 static void vmbus_resched(VMBus
*vmbus
)
2222 aio_bh_schedule_oneshot(qemu_get_aio_context(), vmbus_run
, vmbus
);
2225 static void vmbus_signal_event(EventNotifier
*e
)
2228 VMBus
*vmbus
= container_of(e
, VMBus
, notifier
);
2229 unsigned long *int_map
;
2231 bool is_dirty
= false;
2233 if (!event_notifier_test_and_clear(e
)) {
2237 trace_vmbus_signal_event();
2239 if (!vmbus
->int_page_gpa
) {
2243 addr
= vmbus
->int_page_gpa
+ TARGET_PAGE_SIZE
/ 2;
2244 len
= TARGET_PAGE_SIZE
/ 2;
2245 int_map
= cpu_physical_memory_map(addr
, &len
, 1);
2246 if (len
!= TARGET_PAGE_SIZE
/ 2) {
2250 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2251 if (bitmap_test_and_clear_atomic(int_map
, chan
->id
, 1)) {
2252 if (!vmbus_channel_is_open(chan
)) {
2255 vmbus_channel_notify_host(chan
);
2261 cpu_physical_memory_unmap(int_map
, len
, 1, is_dirty
);
2264 static void vmbus_dev_realize(DeviceState
*dev
, Error
**errp
)
2266 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2267 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2268 VMBus
*vmbus
= VMBUS(qdev_get_parent_bus(dev
));
2271 char idstr
[UUID_STR_LEN
];
2273 assert(!qemu_uuid_is_null(&vdev
->instanceid
));
2275 if (!qemu_uuid_is_null(&vdc
->instanceid
)) {
2276 /* Class wants to only have a single instance with a fixed UUID */
2277 if (!qemu_uuid_is_equal(&vdev
->instanceid
, &vdc
->instanceid
)) {
2278 error_setg(&err
, "instance id can't be changed");
2283 /* Check for instance id collision for this class id */
2284 QTAILQ_FOREACH(child
, &BUS(vmbus
)->children
, sibling
) {
2285 VMBusDevice
*child_dev
= VMBUS_DEVICE(child
->child
);
2287 if (child_dev
== vdev
) {
2291 if (qemu_uuid_is_equal(&child_dev
->instanceid
, &vdev
->instanceid
)) {
2292 qemu_uuid_unparse(&vdev
->instanceid
, idstr
);
2293 error_setg(&err
, "duplicate vmbus device instance id %s", idstr
);
2298 vdev
->dma_as
= &address_space_memory
;
2300 create_channels(vmbus
, vdev
, &err
);
2305 if (vdc
->vmdev_realize
) {
2306 vdc
->vmdev_realize(vdev
, &err
);
2308 goto err_vdc_realize
;
2314 free_channels(vdev
);
2316 error_propagate(errp
, err
);
2319 static void vmbus_dev_reset(DeviceState
*dev
)
2322 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2323 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2325 if (vdev
->channels
) {
2326 for (i
= 0; i
< vdev
->num_channels
; i
++) {
2327 VMBusChannel
*chan
= &vdev
->channels
[i
];
2328 close_channel(chan
);
2329 chan
->state
= VMCHAN_INIT
;
2333 if (vdc
->vmdev_reset
) {
2334 vdc
->vmdev_reset(vdev
);
2338 static void vmbus_dev_unrealize(DeviceState
*dev
)
2340 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2341 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2343 if (vdc
->vmdev_unrealize
) {
2344 vdc
->vmdev_unrealize(vdev
);
2346 free_channels(vdev
);
2349 static Property vmbus_dev_props
[] = {
2350 DEFINE_PROP_UUID("instanceid", VMBusDevice
, instanceid
),
2351 DEFINE_PROP_END_OF_LIST()
2355 static void vmbus_dev_class_init(ObjectClass
*klass
, void *data
)
2357 DeviceClass
*kdev
= DEVICE_CLASS(klass
);
2358 device_class_set_props(kdev
, vmbus_dev_props
);
2359 kdev
->bus_type
= TYPE_VMBUS
;
2360 kdev
->realize
= vmbus_dev_realize
;
2361 kdev
->unrealize
= vmbus_dev_unrealize
;
2362 device_class_set_legacy_reset(kdev
, vmbus_dev_reset
);
2365 static void vmbus_dev_instance_init(Object
*obj
)
2367 VMBusDevice
*vdev
= VMBUS_DEVICE(obj
);
2368 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2370 if (!qemu_uuid_is_null(&vdc
->instanceid
)) {
2371 /* Class wants to only have a single instance with a fixed UUID */
2372 vdev
->instanceid
= vdc
->instanceid
;
2376 const VMStateDescription vmstate_vmbus_dev
= {
2377 .name
= TYPE_VMBUS_DEVICE
,
2379 .minimum_version_id
= 0,
2380 .fields
= (const VMStateField
[]) {
2381 VMSTATE_UINT8_ARRAY(instanceid
.data
, VMBusDevice
, 16),
2382 VMSTATE_UINT16(num_channels
, VMBusDevice
),
2383 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(channels
, VMBusDevice
,
2384 num_channels
, vmstate_channel
,
2386 VMSTATE_END_OF_LIST()
2390 /* vmbus generic device base */
2391 static const TypeInfo vmbus_dev_type_info
= {
2392 .name
= TYPE_VMBUS_DEVICE
,
2393 .parent
= TYPE_DEVICE
,
2395 .instance_size
= sizeof(VMBusDevice
),
2396 .class_size
= sizeof(VMBusDeviceClass
),
2397 .class_init
= vmbus_dev_class_init
,
2398 .instance_init
= vmbus_dev_instance_init
,
2401 static void vmbus_realize(BusState
*bus
, Error
**errp
)
2404 VMBus
*vmbus
= VMBUS(bus
);
2406 qemu_mutex_init(&vmbus
->rx_queue_lock
);
2408 QTAILQ_INIT(&vmbus
->gpadl_list
);
2409 QTAILQ_INIT(&vmbus
->channel_list
);
2411 ret
= hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
,
2412 vmbus_recv_message
, vmbus
);
2414 error_setg(errp
, "hyperv set message handler failed: %d", ret
);
2418 ret
= event_notifier_init(&vmbus
->notifier
, 0);
2420 error_setg(errp
, "event notifier failed to init with %d", ret
);
2421 goto remove_msg_handler
;
2424 event_notifier_set_handler(&vmbus
->notifier
, vmbus_signal_event
);
2425 ret
= hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID
,
2428 error_setg(errp
, "hyperv set event handler failed with %d", ret
);
2429 goto clear_event_notifier
;
2434 clear_event_notifier
:
2435 event_notifier_cleanup(&vmbus
->notifier
);
2437 hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
, NULL
, NULL
);
2439 qemu_mutex_destroy(&vmbus
->rx_queue_lock
);
2442 static void vmbus_unrealize(BusState
*bus
)
2444 VMBus
*vmbus
= VMBUS(bus
);
2446 hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
, NULL
, NULL
);
2447 hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID
, NULL
);
2448 event_notifier_cleanup(&vmbus
->notifier
);
2450 qemu_mutex_destroy(&vmbus
->rx_queue_lock
);
2453 static void vmbus_reset_hold(Object
*obj
, ResetType type
)
2455 vmbus_deinit(VMBUS(obj
));
2458 static char *vmbus_get_dev_path(DeviceState
*dev
)
2460 BusState
*bus
= qdev_get_parent_bus(dev
);
2461 return qdev_get_dev_path(bus
->parent
);
2464 static char *vmbus_get_fw_dev_path(DeviceState
*dev
)
2466 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2467 char uuid
[UUID_STR_LEN
];
2469 qemu_uuid_unparse(&vdev
->instanceid
, uuid
);
2470 return g_strdup_printf("%s@%s", qdev_fw_name(dev
), uuid
);
2473 static void vmbus_class_init(ObjectClass
*klass
, void *data
)
2475 BusClass
*k
= BUS_CLASS(klass
);
2476 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
2478 k
->get_dev_path
= vmbus_get_dev_path
;
2479 k
->get_fw_dev_path
= vmbus_get_fw_dev_path
;
2480 k
->realize
= vmbus_realize
;
2481 k
->unrealize
= vmbus_unrealize
;
2482 rc
->phases
.hold
= vmbus_reset_hold
;
2485 static int vmbus_pre_load(void *opaque
)
2488 VMBus
*vmbus
= VMBUS(opaque
);
2491 * channel IDs allocated by the source will come in the migration stream
2492 * for each channel, so clean up the ones allocated at realize
2494 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2495 unregister_chan_id(chan
);
2500 static int vmbus_post_load(void *opaque
, int version_id
)
2503 VMBus
*vmbus
= VMBUS(opaque
);
2507 ret
= vmbus_init(vmbus
);
2512 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2513 gpadl
->vmbus
= vmbus
;
2514 gpadl
->refcount
= 1;
2518 * reopening channels depends on initialized vmbus so it's done here
2519 * instead of channel_post_load()
2521 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2523 if (chan
->state
== VMCHAN_OPENING
|| chan
->state
== VMCHAN_OPEN
) {
2527 if (chan
->state
!= VMCHAN_OPEN
) {
2531 if (!vmbus_channel_is_open(chan
)) {
2532 /* reopen failed, abort loading */
2536 /* resume processing on the guest side if it missed the notification */
2537 hyperv_sint_route_set_sint(chan
->notify_route
);
2538 /* ditto on the host side */
2539 vmbus_channel_notify_host(chan
);
2542 vmbus_resched(vmbus
);
2546 static const VMStateDescription vmstate_post_message_input
= {
2547 .name
= "vmbus/hyperv_post_message_input",
2549 .minimum_version_id
= 0,
2550 .fields
= (const VMStateField
[]) {
2552 * skip connection_id and message_type as they are validated before
2553 * queueing and ignored on dequeueing
2555 VMSTATE_UINT32(payload_size
, struct hyperv_post_message_input
),
2556 VMSTATE_UINT8_ARRAY(payload
, struct hyperv_post_message_input
,
2557 HV_MESSAGE_PAYLOAD_SIZE
),
2558 VMSTATE_END_OF_LIST()
2562 static bool vmbus_rx_queue_needed(void *opaque
)
2564 VMBus
*vmbus
= VMBUS(opaque
);
2565 return vmbus
->rx_queue_size
;
2568 static const VMStateDescription vmstate_rx_queue
= {
2569 .name
= "vmbus/rx_queue",
2571 .minimum_version_id
= 0,
2572 .needed
= vmbus_rx_queue_needed
,
2573 .fields
= (const VMStateField
[]) {
2574 VMSTATE_UINT8(rx_queue_head
, VMBus
),
2575 VMSTATE_UINT8(rx_queue_size
, VMBus
),
2576 VMSTATE_STRUCT_ARRAY(rx_queue
, VMBus
,
2577 HV_MSG_QUEUE_LEN
, 0,
2578 vmstate_post_message_input
,
2579 struct hyperv_post_message_input
),
2580 VMSTATE_END_OF_LIST()
2584 static const VMStateDescription vmstate_vmbus
= {
2587 .minimum_version_id
= 0,
2588 .pre_load
= vmbus_pre_load
,
2589 .post_load
= vmbus_post_load
,
2590 .fields
= (const VMStateField
[]) {
2591 VMSTATE_UINT8(state
, VMBus
),
2592 VMSTATE_UINT32(version
, VMBus
),
2593 VMSTATE_UINT32(target_vp
, VMBus
),
2594 VMSTATE_UINT64(int_page_gpa
, VMBus
),
2595 VMSTATE_QTAILQ_V(gpadl_list
, VMBus
, 0,
2596 vmstate_gpadl
, VMBusGpadl
, link
),
2597 VMSTATE_END_OF_LIST()
2599 .subsections
= (const VMStateDescription
* const []) {
2605 static const TypeInfo vmbus_type_info
= {
2608 .instance_size
= sizeof(VMBus
),
2609 .class_init
= vmbus_class_init
,
2612 static void vmbus_bridge_realize(DeviceState
*dev
, Error
**errp
)
2614 VMBusBridge
*bridge
= VMBUS_BRIDGE(dev
);
2617 * here there's at least one vmbus bridge that is being realized, so
2618 * vmbus_bridge_find can only return NULL if it's not unique
2620 if (!vmbus_bridge_find()) {
2621 error_setg(errp
, "there can be at most one %s in the system",
2626 if (!hyperv_is_synic_enabled()) {
2627 error_report("VMBus requires usable Hyper-V SynIC and VP_INDEX");
2631 if (!hyperv_are_vmbus_recommended_features_enabled()) {
2632 warn_report("VMBus enabled without the recommended set of Hyper-V features: "
2633 "hv-stimer, hv-vapic and hv-runtime. "
2634 "Some Windows versions might not boot or enable the VMBus device");
2637 bridge
->bus
= VMBUS(qbus_new(TYPE_VMBUS
, dev
, "vmbus"));
2640 static char *vmbus_bridge_ofw_unit_address(const SysBusDevice
*dev
)
2642 /* there can be only one VMBus */
2643 return g_strdup("0");
2646 static const VMStateDescription vmstate_vmbus_bridge
= {
2647 .name
= TYPE_VMBUS_BRIDGE
,
2649 .minimum_version_id
= 0,
2650 .fields
= (const VMStateField
[]) {
2651 VMSTATE_STRUCT_POINTER(bus
, VMBusBridge
, vmstate_vmbus
, VMBus
),
2652 VMSTATE_END_OF_LIST()
2656 static Property vmbus_bridge_props
[] = {
2657 DEFINE_PROP_UINT8("irq", VMBusBridge
, irq
, 7),
2658 DEFINE_PROP_END_OF_LIST()
2661 static void vmbus_bridge_class_init(ObjectClass
*klass
, void *data
)
2663 DeviceClass
*k
= DEVICE_CLASS(klass
);
2664 SysBusDeviceClass
*sk
= SYS_BUS_DEVICE_CLASS(klass
);
2666 k
->realize
= vmbus_bridge_realize
;
2667 k
->fw_name
= "vmbus";
2668 sk
->explicit_ofw_unit_address
= vmbus_bridge_ofw_unit_address
;
2669 set_bit(DEVICE_CATEGORY_BRIDGE
, k
->categories
);
2670 k
->vmsd
= &vmstate_vmbus_bridge
;
2671 device_class_set_props(k
, vmbus_bridge_props
);
2672 /* override SysBusDevice's default */
2673 k
->user_creatable
= true;
2676 static const TypeInfo vmbus_bridge_type_info
= {
2677 .name
= TYPE_VMBUS_BRIDGE
,
2678 .parent
= TYPE_SYS_BUS_DEVICE
,
2679 .instance_size
= sizeof(VMBusBridge
),
2680 .class_init
= vmbus_bridge_class_init
,
2683 static void vmbus_register_types(void)
2685 type_register_static(&vmbus_bridge_type_info
);
2686 type_register_static(&vmbus_dev_type_info
);
2687 type_register_static(&vmbus_type_info
);
2690 type_init(vmbus_register_types
)