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"
23 #define TYPE_VMBUS "vmbus"
24 #define VMBUS(obj) OBJECT_CHECK(VMBus, (obj), TYPE_VMBUS)
36 /* associated channel id (rudimentary?) */
39 /* number of pages in the GPADL as declared in GPADL_HEADER message */
42 * Due to limited message size, GPADL may not fit fully in a single
43 * GPADL_HEADER message, and is further popluated using GPADL_BODY
44 * messages. @seen_gfns is the number of pages seen so far; once it
45 * reaches @num_gfns, the GPADL is ready to use.
48 /* array of GFNs (of size @num_gfns once allocated) */
53 QTAILQ_ENTRY(VMBusGpadl
) link
;
59 * Wrap sequential read from / write to GPADL.
61 typedef struct GpadlIter
{
65 /* offset into GPADL where the next i/o will be performed */
68 * Cached mapping of the currently accessed page, up to page boundary.
69 * Updated lazily on i/o.
70 * Note: MemoryRegionCache can not be used here because pages in the GPADL
71 * are non-contiguous and may belong to different memory regions.
74 /* offset after last i/o (i.e. not affected by seek) */
77 * Indicator that the iterator is active and may have a cached mapping.
78 * Allows to enforce bracketing of all i/o (which may create cached
79 * mappings) and thus exclude mapping leaks.
85 * Ring buffer. There are two of them, sitting in the same GPADL, for each
87 * Each ring buffer consists of a set of pages, with the first page containing
88 * the ring buffer header, and the remaining pages being for data packets.
90 typedef struct VMBusRingBufCommon
{
92 /* GPA of the ring buffer header */
94 /* start and length of the ring buffer data area within GPADL */
101 typedef struct VMBusSendRingBuf
{
102 VMBusRingBufCommon common
;
103 /* current write index, to be committed at the end of send */
105 /* write index at the start of send */
106 uint32_t last_wr_idx
;
107 /* space to be requested from the guest */
109 /* space reserved for planned sends */
111 /* last seen read index */
112 uint32_t last_seen_rd_idx
;
115 typedef struct VMBusRecvRingBuf
{
116 VMBusRingBufCommon common
;
117 /* current read index, to be committed at the end of receive */
119 /* read index at the start of receive */
120 uint32_t last_rd_idx
;
121 /* last seen write index */
122 uint32_t last_seen_wr_idx
;
138 struct VMBusChannel
{
144 * subchannel index within the device; subchannel #0 is "primary" and
147 uint16_t subchan_idx
;
149 /* VP_INDEX of the vCPU to notify with (synthetic) interrupts */
151 /* GPADL id to use for the ring buffers */
152 uint32_t ringbuf_gpadl
;
153 /* start (in pages) of the send ring buffer within @ringbuf_gpadl */
154 uint32_t ringbuf_send_offset
;
160 /* main device worker; copied from the device class */
161 VMBusChannelNotifyCb notify_cb
;
163 * guest->host notifications, either sent directly or dispatched via
164 * interrupt page (older VMBus)
166 EventNotifier notifier
;
170 * SINT route to signal with host->guest notifications; may be shared with
171 * the main VMBus SINT route
173 HvSintRoute
*notify_route
;
176 VMBusSendRingBuf send_ringbuf
;
177 VMBusRecvRingBuf recv_ringbuf
;
179 QTAILQ_ENTRY(VMBusChannel
) link
;
183 * Hyper-V spec mandates that every message port has 16 buffers, which means
184 * that the guest can post up to this many messages without blocking.
185 * Therefore a queue for incoming messages has to be provided.
186 * For outgoing (i.e. host->guest) messages there's no queue; the VMBus just
187 * doesn't transition to a new state until the message is known to have been
188 * successfully delivered to the respective SynIC message slot.
190 #define HV_MSG_QUEUE_LEN 16
192 /* Hyper-V devices never use channel #0. Must be something special. */
193 #define VMBUS_FIRST_CHANID 1
194 /* Each channel occupies one bit within a single event page sint slot. */
195 #define VMBUS_CHANID_COUNT (HV_EVENT_FLAGS_COUNT - VMBUS_FIRST_CHANID)
196 /* Leave a few connection numbers for other purposes. */
197 #define VMBUS_CHAN_CONNECTION_OFFSET 16
200 * Since the success or failure of sending a message is reported
201 * asynchronously, the VMBus state machine has effectively two entry points:
202 * vmbus_run and vmbus_msg_cb (the latter is called when the host->guest
203 * message delivery status becomes known). Both are run as oneshot BHs on the
204 * main aio context, ensuring serialization.
211 VMBUS_TEARDOWN_GPADL
,
221 /* protection against recursive aio_poll (see vmbus_run) */
223 /* whether there's a message being delivered to the guest */
224 bool msg_in_progress
;
226 /* VP_INDEX of the vCPU to send messages and interrupts to */
228 HvSintRoute
*sint_route
;
230 * interrupt page for older protocol versions; newer ones use SynIC event
235 DECLARE_BITMAP(chanid_bitmap
, VMBUS_CHANID_COUNT
);
237 /* incoming message queue */
238 struct hyperv_post_message_input rx_queue
[HV_MSG_QUEUE_LEN
];
239 uint8_t rx_queue_head
;
240 uint8_t rx_queue_size
;
241 QemuMutex rx_queue_lock
;
243 QTAILQ_HEAD(, VMBusGpadl
) gpadl_list
;
244 QTAILQ_HEAD(, VMBusChannel
) channel_list
;
247 * guest->host notifications for older VMBus, to be dispatched via
250 EventNotifier notifier
;
253 static bool gpadl_full(VMBusGpadl
*gpadl
)
255 return gpadl
->seen_gfns
== gpadl
->num_gfns
;
258 static VMBusGpadl
*create_gpadl(VMBus
*vmbus
, uint32_t id
,
259 uint32_t child_relid
, uint32_t num_gfns
)
261 VMBusGpadl
*gpadl
= g_new0(VMBusGpadl
, 1);
264 gpadl
->child_relid
= child_relid
;
265 gpadl
->num_gfns
= num_gfns
;
266 gpadl
->gfns
= g_new(uint64_t, num_gfns
);
267 QTAILQ_INSERT_HEAD(&vmbus
->gpadl_list
, gpadl
, link
);
268 gpadl
->vmbus
= vmbus
;
273 static void free_gpadl(VMBusGpadl
*gpadl
)
275 QTAILQ_REMOVE(&gpadl
->vmbus
->gpadl_list
, gpadl
, link
);
280 static VMBusGpadl
*find_gpadl(VMBus
*vmbus
, uint32_t gpadl_id
)
283 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
284 if (gpadl
->id
== gpadl_id
) {
291 VMBusGpadl
*vmbus_get_gpadl(VMBusChannel
*chan
, uint32_t gpadl_id
)
293 VMBusGpadl
*gpadl
= find_gpadl(chan
->vmbus
, gpadl_id
);
294 if (!gpadl
|| !gpadl_full(gpadl
)) {
301 void vmbus_put_gpadl(VMBusGpadl
*gpadl
)
306 if (--gpadl
->refcount
) {
312 uint32_t vmbus_gpadl_len(VMBusGpadl
*gpadl
)
314 return gpadl
->num_gfns
* TARGET_PAGE_SIZE
;
317 static void gpadl_iter_init(GpadlIter
*iter
, VMBusGpadl
*gpadl
,
318 AddressSpace
*as
, DMADirection dir
)
323 iter
->active
= false;
326 static inline void gpadl_iter_cache_unmap(GpadlIter
*iter
)
328 uint32_t map_start_in_page
= (uintptr_t)iter
->map
& ~TARGET_PAGE_MASK
;
329 uint32_t io_end_in_page
= ((iter
->last_off
- 1) & ~TARGET_PAGE_MASK
) + 1;
331 /* mapping is only done to do non-zero amount of i/o */
332 assert(iter
->last_off
> 0);
333 assert(map_start_in_page
< io_end_in_page
);
335 dma_memory_unmap(iter
->as
, iter
->map
, TARGET_PAGE_SIZE
- map_start_in_page
,
336 iter
->dir
, io_end_in_page
- map_start_in_page
);
340 * Copy exactly @len bytes between the GPADL pointed to by @iter and @buf.
341 * The direction of the copy is determined by @iter->dir.
342 * The caller must ensure the operation overflows neither @buf nor the GPADL
343 * (there's an assert for the latter).
344 * Reuse the currently mapped page in the GPADL if possible.
346 static ssize_t
gpadl_iter_io(GpadlIter
*iter
, void *buf
, uint32_t len
)
350 assert(iter
->active
);
353 uint32_t off_in_page
= iter
->off
& ~TARGET_PAGE_MASK
;
354 uint32_t pgleft
= TARGET_PAGE_SIZE
- off_in_page
;
355 uint32_t cplen
= MIN(pgleft
, len
);
358 /* try to reuse the cached mapping */
360 uint32_t map_start_in_page
=
361 (uintptr_t)iter
->map
& ~TARGET_PAGE_MASK
;
362 uint32_t off_base
= iter
->off
& ~TARGET_PAGE_MASK
;
363 uint32_t mapped_base
= (iter
->last_off
- 1) & ~TARGET_PAGE_MASK
;
364 if (off_base
!= mapped_base
|| off_in_page
< map_start_in_page
) {
365 gpadl_iter_cache_unmap(iter
);
372 dma_addr_t mlen
= pgleft
;
373 uint32_t idx
= iter
->off
>> TARGET_PAGE_BITS
;
374 assert(idx
< iter
->gpadl
->num_gfns
);
376 maddr
= (iter
->gpadl
->gfns
[idx
] << TARGET_PAGE_BITS
) | off_in_page
;
378 iter
->map
= dma_memory_map(iter
->as
, maddr
, &mlen
, iter
->dir
);
379 if (mlen
!= pgleft
) {
380 dma_memory_unmap(iter
->as
, iter
->map
, mlen
, iter
->dir
, 0);
386 p
= (void *)(((uintptr_t)iter
->map
& TARGET_PAGE_MASK
) | off_in_page
);
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
);
499 iov
[ret_cnt
].iov_len
= l
;
507 vmbus_unmap_sgl(req
, dir
, iov
, ret_cnt
, 0);
511 void vmbus_unmap_sgl(VMBusChanReq
*req
, DMADirection dir
, struct iovec
*iov
,
512 unsigned iov_cnt
, size_t accessed
)
514 QEMUSGList
*sgl
= &req
->sgl
;
517 for (i
= 0; i
< iov_cnt
; i
++) {
518 size_t acsd
= MIN(accessed
, iov
[i
].iov_len
);
519 dma_memory_unmap(sgl
->as
, iov
[i
].iov_base
, iov
[i
].iov_len
, dir
, acsd
);
524 static const VMStateDescription vmstate_gpadl
= {
525 .name
= "vmbus/gpadl",
527 .minimum_version_id
= 0,
528 .fields
= (VMStateField
[]) {
529 VMSTATE_UINT32(id
, VMBusGpadl
),
530 VMSTATE_UINT32(child_relid
, VMBusGpadl
),
531 VMSTATE_UINT32(num_gfns
, VMBusGpadl
),
532 VMSTATE_UINT32(seen_gfns
, VMBusGpadl
),
533 VMSTATE_VARRAY_UINT32_ALLOC(gfns
, VMBusGpadl
, num_gfns
, 0,
534 vmstate_info_uint64
, uint64_t),
535 VMSTATE_UINT8(state
, VMBusGpadl
),
536 VMSTATE_END_OF_LIST()
541 * Wrap the index into a ring buffer of @len bytes.
542 * @idx is assumed not to exceed twice the size of the ringbuffer, so only
543 * single wraparound is considered.
545 static inline uint32_t rb_idx_wrap(uint32_t idx
, uint32_t len
)
554 * Circular difference between two indices into a ring buffer of @len bytes.
555 * @allow_catchup - whether @idx1 may catch up @idx2; e.g. read index may catch
556 * up write index but not vice versa.
558 static inline uint32_t rb_idx_delta(uint32_t idx1
, uint32_t idx2
, uint32_t len
,
561 return rb_idx_wrap(idx2
+ len
- idx1
- !allow_catchup
, len
);
564 static vmbus_ring_buffer
*ringbuf_map_hdr(VMBusRingBufCommon
*ringbuf
)
566 vmbus_ring_buffer
*rb
;
567 dma_addr_t mlen
= sizeof(*rb
);
569 rb
= dma_memory_map(ringbuf
->as
, ringbuf
->rb_addr
, &mlen
,
570 DMA_DIRECTION_FROM_DEVICE
);
571 if (mlen
!= sizeof(*rb
)) {
572 dma_memory_unmap(ringbuf
->as
, rb
, mlen
,
573 DMA_DIRECTION_FROM_DEVICE
, 0);
579 static void ringbuf_unmap_hdr(VMBusRingBufCommon
*ringbuf
,
580 vmbus_ring_buffer
*rb
, bool dirty
)
584 dma_memory_unmap(ringbuf
->as
, rb
, sizeof(*rb
), DMA_DIRECTION_FROM_DEVICE
,
585 dirty
? sizeof(*rb
) : 0);
588 static void ringbuf_init_common(VMBusRingBufCommon
*ringbuf
, VMBusGpadl
*gpadl
,
589 AddressSpace
*as
, DMADirection dir
,
590 uint32_t begin
, uint32_t end
)
593 ringbuf
->rb_addr
= gpadl
->gfns
[begin
] << TARGET_PAGE_BITS
;
594 ringbuf
->base
= (begin
+ 1) << TARGET_PAGE_BITS
;
595 ringbuf
->len
= (end
- begin
- 1) << TARGET_PAGE_BITS
;
596 gpadl_iter_init(&ringbuf
->iter
, gpadl
, as
, dir
);
599 static int ringbufs_init(VMBusChannel
*chan
)
601 vmbus_ring_buffer
*rb
;
602 VMBusSendRingBuf
*send_ringbuf
= &chan
->send_ringbuf
;
603 VMBusRecvRingBuf
*recv_ringbuf
= &chan
->recv_ringbuf
;
605 if (chan
->ringbuf_send_offset
<= 1 ||
606 chan
->gpadl
->num_gfns
<= chan
->ringbuf_send_offset
+ 1) {
610 ringbuf_init_common(&recv_ringbuf
->common
, chan
->gpadl
, chan
->dev
->dma_as
,
611 DMA_DIRECTION_TO_DEVICE
, 0, chan
->ringbuf_send_offset
);
612 ringbuf_init_common(&send_ringbuf
->common
, chan
->gpadl
, chan
->dev
->dma_as
,
613 DMA_DIRECTION_FROM_DEVICE
, chan
->ringbuf_send_offset
,
614 chan
->gpadl
->num_gfns
);
615 send_ringbuf
->wanted
= 0;
616 send_ringbuf
->reserved
= 0;
618 rb
= ringbuf_map_hdr(&recv_ringbuf
->common
);
622 recv_ringbuf
->rd_idx
= recv_ringbuf
->last_rd_idx
= rb
->read_index
;
623 ringbuf_unmap_hdr(&recv_ringbuf
->common
, rb
, false);
625 rb
= ringbuf_map_hdr(&send_ringbuf
->common
);
629 send_ringbuf
->wr_idx
= send_ringbuf
->last_wr_idx
= rb
->write_index
;
630 send_ringbuf
->last_seen_rd_idx
= rb
->read_index
;
631 rb
->feature_bits
|= VMBUS_RING_BUFFER_FEAT_PENDING_SZ
;
632 ringbuf_unmap_hdr(&send_ringbuf
->common
, rb
, true);
634 if (recv_ringbuf
->rd_idx
>= recv_ringbuf
->common
.len
||
635 send_ringbuf
->wr_idx
>= send_ringbuf
->common
.len
) {
643 * Perform io between the GPADL-backed ringbuffer @ringbuf and @buf, wrapping
645 * @len is assumed not to exceed the size of the ringbuffer, so only single
646 * wraparound is considered.
648 static ssize_t
ringbuf_io(VMBusRingBufCommon
*ringbuf
, void *buf
, uint32_t len
)
650 ssize_t ret1
= 0, ret2
= 0;
651 uint32_t remain
= ringbuf
->len
+ ringbuf
->base
- ringbuf
->iter
.off
;
654 ret1
= gpadl_iter_io(&ringbuf
->iter
, buf
, remain
);
658 gpadl_iter_seek(&ringbuf
->iter
, ringbuf
->base
);
662 ret2
= gpadl_iter_io(&ringbuf
->iter
, buf
, len
);
670 * Position the circular iterator within @ringbuf to offset @new_off, wrapping
672 * @new_off is assumed not to exceed twice the size of the ringbuffer, so only
673 * single wraparound is considered.
675 static inline void ringbuf_seek(VMBusRingBufCommon
*ringbuf
, uint32_t new_off
)
677 gpadl_iter_seek(&ringbuf
->iter
,
678 ringbuf
->base
+ rb_idx_wrap(new_off
, ringbuf
->len
));
681 static inline uint32_t ringbuf_tell(VMBusRingBufCommon
*ringbuf
)
683 return ringbuf
->iter
.off
- ringbuf
->base
;
686 static inline void ringbuf_start_io(VMBusRingBufCommon
*ringbuf
)
688 gpadl_iter_start_io(&ringbuf
->iter
);
691 static inline void ringbuf_end_io(VMBusRingBufCommon
*ringbuf
)
693 gpadl_iter_end_io(&ringbuf
->iter
);
696 VMBusDevice
*vmbus_channel_device(VMBusChannel
*chan
)
701 VMBusChannel
*vmbus_device_channel(VMBusDevice
*dev
, uint32_t chan_idx
)
703 if (chan_idx
>= dev
->num_channels
) {
706 return &dev
->channels
[chan_idx
];
709 uint32_t vmbus_channel_idx(VMBusChannel
*chan
)
711 return chan
- chan
->dev
->channels
;
714 void vmbus_channel_notify_host(VMBusChannel
*chan
)
716 event_notifier_set(&chan
->notifier
);
719 bool vmbus_channel_is_open(VMBusChannel
*chan
)
721 return chan
->is_open
;
725 * Notify the guest side about the data to work on in the channel ring buffer.
726 * The notification is done by signaling a dedicated per-channel SynIC event
727 * flag (more recent guests) or setting a bit in the interrupt page and firing
728 * the VMBus SINT (older guests).
730 static int vmbus_channel_notify_guest(VMBusChannel
*chan
)
733 unsigned long *int_map
, mask
;
735 hwaddr addr
= chan
->vmbus
->int_page_gpa
;
736 hwaddr len
= TARGET_PAGE_SIZE
/ 2, dirty
= 0;
738 trace_vmbus_channel_notify_guest(chan
->id
);
741 return hyperv_set_event_flag(chan
->notify_route
, chan
->id
);
744 int_map
= cpu_physical_memory_map(addr
, &len
, 1);
745 if (len
!= TARGET_PAGE_SIZE
/ 2) {
750 idx
= BIT_WORD(chan
->id
);
751 mask
= BIT_MASK(chan
->id
);
752 if ((atomic_fetch_or(&int_map
[idx
], mask
) & mask
) != mask
) {
753 res
= hyperv_sint_route_set_sint(chan
->notify_route
);
758 cpu_physical_memory_unmap(int_map
, len
, 1, dirty
);
762 #define VMBUS_PKT_TRAILER sizeof(uint64_t)
764 static uint32_t vmbus_pkt_hdr_set_offsets(vmbus_packet_hdr
*hdr
,
765 uint32_t desclen
, uint32_t msglen
)
767 hdr
->offset_qwords
= sizeof(*hdr
) / sizeof(uint64_t) +
768 DIV_ROUND_UP(desclen
, sizeof(uint64_t));
769 hdr
->len_qwords
= hdr
->offset_qwords
+
770 DIV_ROUND_UP(msglen
, sizeof(uint64_t));
771 return hdr
->len_qwords
* sizeof(uint64_t) + VMBUS_PKT_TRAILER
;
775 * Simplified ring buffer operation with paired barriers annotations in the
776 * producer and consumer loops:
778 * producer * consumer
779 * ~~~~~~~~ * ~~~~~~~~
780 * write pending_send_sz * read write_index
781 * smp_mb [A] * smp_mb [C]
782 * read read_index * read packet
783 * smp_mb [B] * read/write out-of-band data
784 * read/write out-of-band data * smp_mb [B]
785 * write packet * write read_index
786 * smp_mb [C] * smp_mb [A]
787 * write write_index * read pending_send_sz
788 * smp_wmb [D] * smp_rmb [D]
789 * write pending_send_sz * read write_index
793 static inline uint32_t ringbuf_send_avail(VMBusSendRingBuf
*ringbuf
)
795 /* don't trust guest data */
796 if (ringbuf
->last_seen_rd_idx
>= ringbuf
->common
.len
) {
799 return rb_idx_delta(ringbuf
->wr_idx
, ringbuf
->last_seen_rd_idx
,
800 ringbuf
->common
.len
, false);
803 static ssize_t
ringbuf_send_update_idx(VMBusChannel
*chan
)
805 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
806 vmbus_ring_buffer
*rb
;
809 written
= rb_idx_delta(ringbuf
->last_wr_idx
, ringbuf
->wr_idx
,
810 ringbuf
->common
.len
, true);
815 rb
= ringbuf_map_hdr(&ringbuf
->common
);
820 ringbuf
->reserved
-= written
;
822 /* prevent reorder with the data operation and packet write */
823 smp_mb(); /* barrier pair [C] */
824 rb
->write_index
= ringbuf
->wr_idx
;
827 * If the producer earlier indicated that it wants to be notified when the
828 * consumer frees certain amount of space in the ring buffer, that amount
829 * is reduced by the size of the completed write.
831 if (ringbuf
->wanted
) {
832 /* otherwise reservation would fail */
833 assert(ringbuf
->wanted
< written
);
834 ringbuf
->wanted
-= written
;
835 /* prevent reorder with write_index write */
836 smp_wmb(); /* barrier pair [D] */
837 rb
->pending_send_sz
= ringbuf
->wanted
;
840 /* prevent reorder with write_index or pending_send_sz write */
841 smp_mb(); /* barrier pair [A] */
842 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
845 * The consumer may have missed the reduction of pending_send_sz and skip
846 * notification, so re-check the blocking condition, and, if it's no longer
847 * true, ensure processing another iteration by simulating consumer's
850 if (ringbuf_send_avail(ringbuf
) >= ringbuf
->wanted
) {
851 vmbus_channel_notify_host(chan
);
854 /* skip notification by consumer's request */
855 if (rb
->interrupt_mask
) {
860 * The consumer hasn't caught up with the producer's previous state so it's
862 * (last_seen_rd_idx comes from the guest but it's safe to use w/o
863 * validation here as it only affects notification.)
865 if (rb_idx_delta(ringbuf
->last_seen_rd_idx
, ringbuf
->wr_idx
,
866 ringbuf
->common
.len
, true) > written
) {
870 vmbus_channel_notify_guest(chan
);
872 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, true);
873 ringbuf
->last_wr_idx
= ringbuf
->wr_idx
;
877 int vmbus_channel_reserve(VMBusChannel
*chan
,
878 uint32_t desclen
, uint32_t msglen
)
880 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
881 vmbus_ring_buffer
*rb
= NULL
;
882 vmbus_packet_hdr hdr
;
883 uint32_t needed
= ringbuf
->reserved
+
884 vmbus_pkt_hdr_set_offsets(&hdr
, desclen
, msglen
);
886 /* avoid touching the guest memory if possible */
887 if (likely(needed
<= ringbuf_send_avail(ringbuf
))) {
891 rb
= ringbuf_map_hdr(&ringbuf
->common
);
896 /* fetch read index from guest memory and try again */
897 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
899 if (likely(needed
<= ringbuf_send_avail(ringbuf
))) {
903 rb
->pending_send_sz
= needed
;
906 * The consumer may have made progress and freed up some space before
907 * seeing updated pending_send_sz, so re-read read_index (preventing
908 * reorder with the pending_send_sz write) and try again.
910 smp_mb(); /* barrier pair [A] */
911 ringbuf
->last_seen_rd_idx
= rb
->read_index
;
913 if (needed
> ringbuf_send_avail(ringbuf
)) {
918 ringbuf
->reserved
= needed
;
921 /* clear pending_send_sz if it was set */
922 if (ringbuf
->wanted
) {
924 rb
= ringbuf_map_hdr(&ringbuf
->common
);
926 /* failure to clear pending_send_sz is non-fatal */
931 rb
->pending_send_sz
= 0;
934 /* prevent reorder of the following data operation with read_index read */
935 smp_mb(); /* barrier pair [B] */
939 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, ringbuf
->wanted
== needed
);
941 ringbuf
->wanted
= needed
;
942 return needed
? -ENOSPC
: 0;
945 ssize_t
vmbus_channel_send(VMBusChannel
*chan
, uint16_t pkt_type
,
946 void *desc
, uint32_t desclen
,
947 void *msg
, uint32_t msglen
,
948 bool need_comp
, uint64_t transaction_id
)
951 vmbus_packet_hdr hdr
;
953 VMBusSendRingBuf
*ringbuf
= &chan
->send_ringbuf
;
955 if (!vmbus_channel_is_open(chan
)) {
959 totlen
= vmbus_pkt_hdr_set_offsets(&hdr
, desclen
, msglen
);
961 hdr
.flags
= need_comp
? VMBUS_PACKET_FLAG_REQUEST_COMPLETION
: 0;
962 hdr
.transaction_id
= transaction_id
;
964 assert(totlen
<= ringbuf
->reserved
);
966 ringbuf_start_io(&ringbuf
->common
);
967 ringbuf_seek(&ringbuf
->common
, ringbuf
->wr_idx
);
968 ret
= ringbuf_io(&ringbuf
->common
, &hdr
, sizeof(hdr
));
974 ret
= ringbuf_io(&ringbuf
->common
, desc
, desclen
);
978 ringbuf_seek(&ringbuf
->common
,
979 ringbuf
->wr_idx
+ hdr
.offset_qwords
* sizeof(uint64_t));
981 ret
= ringbuf_io(&ringbuf
->common
, msg
, msglen
);
985 ringbuf_seek(&ringbuf
->common
, ringbuf
->wr_idx
+ totlen
);
986 ringbuf
->wr_idx
= ringbuf_tell(&ringbuf
->common
);
989 ringbuf_end_io(&ringbuf
->common
);
993 return ringbuf_send_update_idx(chan
);
996 ssize_t
vmbus_channel_send_completion(VMBusChanReq
*req
,
997 void *msg
, uint32_t msglen
)
999 assert(req
->need_comp
);
1000 return vmbus_channel_send(req
->chan
, VMBUS_PACKET_COMP
, NULL
, 0,
1001 msg
, msglen
, false, req
->transaction_id
);
1004 static int sgl_from_gpa_ranges(QEMUSGList
*sgl
, VMBusDevice
*dev
,
1005 VMBusRingBufCommon
*ringbuf
, uint32_t len
)
1008 vmbus_pkt_gpa_direct hdr
;
1013 if (len
< sizeof(hdr
)) {
1016 ret
= ringbuf_io(ringbuf
, &hdr
, sizeof(hdr
));
1022 num
= (len
- hdr
.rangecount
* sizeof(vmbus_gpa_range
)) / sizeof(uint64_t);
1026 qemu_sglist_init(sgl
, DEVICE(dev
), num
, ringbuf
->as
);
1028 for (; hdr
.rangecount
; hdr
.rangecount
--) {
1029 vmbus_gpa_range range
;
1031 if (len
< sizeof(range
)) {
1034 ret
= ringbuf_io(ringbuf
, &range
, sizeof(range
));
1038 len
-= sizeof(range
);
1040 if (range
.byte_offset
& TARGET_PAGE_MASK
) {
1044 for (; range
.byte_count
; range
.byte_offset
= 0) {
1046 uint32_t plen
= MIN(range
.byte_count
,
1047 TARGET_PAGE_SIZE
- range
.byte_offset
);
1049 if (len
< sizeof(uint64_t)) {
1052 ret
= ringbuf_io(ringbuf
, &paddr
, sizeof(paddr
));
1056 len
-= sizeof(uint64_t);
1057 paddr
<<= TARGET_PAGE_BITS
;
1058 paddr
|= range
.byte_offset
;
1059 range
.byte_count
-= plen
;
1061 if (curaddr
+ curlen
== paddr
) {
1062 /* consecutive fragments - join */
1066 qemu_sglist_add(sgl
, curaddr
, curlen
);
1076 qemu_sglist_add(sgl
, curaddr
, curlen
);
1083 qemu_sglist_destroy(sgl
);
1087 static VMBusChanReq
*vmbus_alloc_req(VMBusChannel
*chan
,
1088 uint32_t size
, uint16_t pkt_type
,
1089 uint32_t msglen
, uint64_t transaction_id
,
1093 uint32_t msgoff
= QEMU_ALIGN_UP(size
, __alignof__(*req
->msg
));
1094 uint32_t totlen
= msgoff
+ msglen
;
1096 req
= g_malloc0(totlen
);
1098 req
->pkt_type
= pkt_type
;
1099 req
->msg
= (void *)req
+ msgoff
;
1100 req
->msglen
= msglen
;
1101 req
->transaction_id
= transaction_id
;
1102 req
->need_comp
= need_comp
;
1106 int vmbus_channel_recv_start(VMBusChannel
*chan
)
1108 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1109 vmbus_ring_buffer
*rb
;
1111 rb
= ringbuf_map_hdr(&ringbuf
->common
);
1115 ringbuf
->last_seen_wr_idx
= rb
->write_index
;
1116 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, false);
1118 if (ringbuf
->last_seen_wr_idx
>= ringbuf
->common
.len
) {
1122 /* prevent reorder of the following data operation with write_index read */
1123 smp_mb(); /* barrier pair [C] */
1127 void *vmbus_channel_recv_peek(VMBusChannel
*chan
, uint32_t size
)
1129 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1130 vmbus_packet_hdr hdr
= {};
1133 uint32_t totlen
, pktlen
, msglen
, msgoff
, desclen
;
1135 assert(size
>= sizeof(*req
));
1137 /* safe as last_seen_wr_idx is validated in vmbus_channel_recv_start */
1138 avail
= rb_idx_delta(ringbuf
->rd_idx
, ringbuf
->last_seen_wr_idx
,
1139 ringbuf
->common
.len
, true);
1140 if (avail
< sizeof(hdr
)) {
1144 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
);
1145 if (ringbuf_io(&ringbuf
->common
, &hdr
, sizeof(hdr
)) < 0) {
1149 pktlen
= hdr
.len_qwords
* sizeof(uint64_t);
1150 totlen
= pktlen
+ VMBUS_PKT_TRAILER
;
1151 if (totlen
> avail
) {
1155 msgoff
= hdr
.offset_qwords
* sizeof(uint64_t);
1156 if (msgoff
> pktlen
|| msgoff
< sizeof(hdr
)) {
1157 error_report("%s: malformed packet: %u %u", __func__
, msgoff
, pktlen
);
1161 msglen
= pktlen
- msgoff
;
1163 req
= vmbus_alloc_req(chan
, size
, hdr
.type
, msglen
, hdr
.transaction_id
,
1164 hdr
.flags
& VMBUS_PACKET_FLAG_REQUEST_COMPLETION
);
1167 case VMBUS_PACKET_DATA_USING_GPA_DIRECT
:
1168 desclen
= msgoff
- sizeof(hdr
);
1169 if (sgl_from_gpa_ranges(&req
->sgl
, chan
->dev
, &ringbuf
->common
,
1171 error_report("%s: failed to convert GPA ranges to SGL", __func__
);
1175 case VMBUS_PACKET_DATA_INBAND
:
1176 case VMBUS_PACKET_COMP
:
1179 error_report("%s: unexpected msg type: %x", __func__
, hdr
.type
);
1183 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
+ msgoff
);
1184 if (ringbuf_io(&ringbuf
->common
, req
->msg
, msglen
) < 0) {
1187 ringbuf_seek(&ringbuf
->common
, ringbuf
->rd_idx
+ totlen
);
1191 vmbus_free_req(req
);
1195 void vmbus_channel_recv_pop(VMBusChannel
*chan
)
1197 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1198 ringbuf
->rd_idx
= ringbuf_tell(&ringbuf
->common
);
1201 ssize_t
vmbus_channel_recv_done(VMBusChannel
*chan
)
1203 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1204 vmbus_ring_buffer
*rb
;
1207 read
= rb_idx_delta(ringbuf
->last_rd_idx
, ringbuf
->rd_idx
,
1208 ringbuf
->common
.len
, true);
1213 rb
= ringbuf_map_hdr(&ringbuf
->common
);
1218 /* prevent reorder with the data operation and packet read */
1219 smp_mb(); /* barrier pair [B] */
1220 rb
->read_index
= ringbuf
->rd_idx
;
1222 /* prevent reorder of the following pending_send_sz read */
1223 smp_mb(); /* barrier pair [A] */
1225 if (rb
->interrupt_mask
) {
1229 if (rb
->feature_bits
& VMBUS_RING_BUFFER_FEAT_PENDING_SZ
) {
1230 uint32_t wr_idx
, wr_avail
;
1231 uint32_t wanted
= rb
->pending_send_sz
;
1237 /* prevent reorder with pending_send_sz read */
1238 smp_rmb(); /* barrier pair [D] */
1239 wr_idx
= rb
->write_index
;
1241 wr_avail
= rb_idx_delta(wr_idx
, ringbuf
->rd_idx
, ringbuf
->common
.len
,
1244 /* the producer wasn't blocked on the consumer state */
1245 if (wr_avail
>= read
+ wanted
) {
1248 /* there's not enough space for the producer to make progress */
1249 if (wr_avail
< wanted
) {
1254 vmbus_channel_notify_guest(chan
);
1256 ringbuf_unmap_hdr(&ringbuf
->common
, rb
, true);
1257 ringbuf
->last_rd_idx
= ringbuf
->rd_idx
;
1261 void vmbus_free_req(void *req
)
1263 VMBusChanReq
*r
= req
;
1270 qemu_sglist_destroy(&r
->sgl
);
1275 static const VMStateDescription vmstate_sgent
= {
1276 .name
= "vmbus/sgentry",
1278 .minimum_version_id
= 0,
1279 .fields
= (VMStateField
[]) {
1280 VMSTATE_UINT64(base
, ScatterGatherEntry
),
1281 VMSTATE_UINT64(len
, ScatterGatherEntry
),
1282 VMSTATE_END_OF_LIST()
1286 typedef struct VMBusChanReqSave
{
1291 uint64_t transaction_id
;
1294 ScatterGatherEntry
*sgl
;
1297 static const VMStateDescription vmstate_vmbus_chan_req
= {
1298 .name
= "vmbus/vmbus_chan_req",
1300 .minimum_version_id
= 0,
1301 .fields
= (VMStateField
[]) {
1302 VMSTATE_UINT16(chan_idx
, VMBusChanReqSave
),
1303 VMSTATE_UINT16(pkt_type
, VMBusChanReqSave
),
1304 VMSTATE_UINT32(msglen
, VMBusChanReqSave
),
1305 VMSTATE_VBUFFER_ALLOC_UINT32(msg
, VMBusChanReqSave
, 0, NULL
, msglen
),
1306 VMSTATE_UINT64(transaction_id
, VMBusChanReqSave
),
1307 VMSTATE_BOOL(need_comp
, VMBusChanReqSave
),
1308 VMSTATE_UINT32(num
, VMBusChanReqSave
),
1309 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(sgl
, VMBusChanReqSave
, num
,
1310 vmstate_sgent
, ScatterGatherEntry
),
1311 VMSTATE_END_OF_LIST()
1315 void vmbus_save_req(QEMUFile
*f
, VMBusChanReq
*req
)
1317 VMBusChanReqSave req_save
;
1319 req_save
.chan_idx
= req
->chan
->subchan_idx
;
1320 req_save
.pkt_type
= req
->pkt_type
;
1321 req_save
.msglen
= req
->msglen
;
1322 req_save
.msg
= req
->msg
;
1323 req_save
.transaction_id
= req
->transaction_id
;
1324 req_save
.need_comp
= req
->need_comp
;
1325 req_save
.num
= req
->sgl
.nsg
;
1326 req_save
.sgl
= g_memdup(req
->sgl
.sg
,
1327 req_save
.num
* sizeof(ScatterGatherEntry
));
1329 vmstate_save_state(f
, &vmstate_vmbus_chan_req
, &req_save
, NULL
);
1331 g_free(req_save
.sgl
);
1334 void *vmbus_load_req(QEMUFile
*f
, VMBusDevice
*dev
, uint32_t size
)
1336 VMBusChanReqSave req_save
;
1337 VMBusChanReq
*req
= NULL
;
1338 VMBusChannel
*chan
= NULL
;
1341 vmstate_load_state(f
, &vmstate_vmbus_chan_req
, &req_save
, 0);
1343 if (req_save
.chan_idx
>= dev
->num_channels
) {
1344 error_report("%s: %u(chan_idx) > %u(num_channels)", __func__
,
1345 req_save
.chan_idx
, dev
->num_channels
);
1348 chan
= &dev
->channels
[req_save
.chan_idx
];
1350 if (vmbus_channel_reserve(chan
, 0, req_save
.msglen
)) {
1354 req
= vmbus_alloc_req(chan
, size
, req_save
.pkt_type
, req_save
.msglen
,
1355 req_save
.transaction_id
, req_save
.need_comp
);
1356 if (req_save
.msglen
) {
1357 memcpy(req
->msg
, req_save
.msg
, req_save
.msglen
);
1360 for (i
= 0; i
< req_save
.num
; i
++) {
1361 qemu_sglist_add(&req
->sgl
, req_save
.sgl
[i
].base
, req_save
.sgl
[i
].len
);
1365 if (req_save
.msglen
) {
1366 g_free(req_save
.msg
);
1369 g_free(req_save
.sgl
);
1374 static void channel_event_cb(EventNotifier
*e
)
1376 VMBusChannel
*chan
= container_of(e
, VMBusChannel
, notifier
);
1377 if (event_notifier_test_and_clear(e
)) {
1379 * All receives are supposed to happen within the device worker, so
1380 * bracket it with ringbuf_start/end_io on the receive ringbuffer, and
1381 * potentially reuse the cached mapping throughout the worker.
1382 * Can't do this for sends as they may happen outside the device
1385 VMBusRecvRingBuf
*ringbuf
= &chan
->recv_ringbuf
;
1386 ringbuf_start_io(&ringbuf
->common
);
1387 chan
->notify_cb(chan
);
1388 ringbuf_end_io(&ringbuf
->common
);
1393 static int alloc_chan_id(VMBus
*vmbus
)
1397 ret
= find_next_zero_bit(vmbus
->chanid_bitmap
, VMBUS_CHANID_COUNT
, 0);
1398 if (ret
== VMBUS_CHANID_COUNT
) {
1401 return ret
+ VMBUS_FIRST_CHANID
;
1404 static int register_chan_id(VMBusChannel
*chan
)
1406 return test_and_set_bit(chan
->id
- VMBUS_FIRST_CHANID
,
1407 chan
->vmbus
->chanid_bitmap
) ? -EEXIST
: 0;
1410 static void unregister_chan_id(VMBusChannel
*chan
)
1412 clear_bit(chan
->id
- VMBUS_FIRST_CHANID
, chan
->vmbus
->chanid_bitmap
);
1415 static uint32_t chan_connection_id(VMBusChannel
*chan
)
1417 return VMBUS_CHAN_CONNECTION_OFFSET
+ chan
->id
;
1420 static void init_channel(VMBus
*vmbus
, VMBusDevice
*dev
, VMBusDeviceClass
*vdc
,
1421 VMBusChannel
*chan
, uint16_t idx
, Error
**errp
)
1426 chan
->notify_cb
= vdc
->chan_notify_cb
;
1427 chan
->subchan_idx
= idx
;
1428 chan
->vmbus
= vmbus
;
1430 res
= alloc_chan_id(vmbus
);
1432 error_setg(errp
, "no spare channel id");
1436 register_chan_id(chan
);
1439 * The guest drivers depend on the device subchannels (idx #1+) to be
1440 * offered after the primary channel (idx #0) of that device. To ensure
1441 * that, record the channels on the channel list in the order they appear
1442 * within the device.
1444 QTAILQ_INSERT_TAIL(&vmbus
->channel_list
, chan
, link
);
1447 static void deinit_channel(VMBusChannel
*chan
)
1449 assert(chan
->state
== VMCHAN_INIT
);
1450 QTAILQ_REMOVE(&chan
->vmbus
->channel_list
, chan
, link
);
1451 unregister_chan_id(chan
);
1454 static void create_channels(VMBus
*vmbus
, VMBusDevice
*dev
, Error
**errp
)
1457 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(dev
);
1460 dev
->num_channels
= vdc
->num_channels
? vdc
->num_channels(dev
) : 1;
1461 if (dev
->num_channels
< 1) {
1462 error_setg(errp
, "invalid #channels: %u", dev
->num_channels
);
1466 dev
->channels
= g_new0(VMBusChannel
, dev
->num_channels
);
1467 for (i
= 0; i
< dev
->num_channels
; i
++) {
1468 init_channel(vmbus
, dev
, vdc
, &dev
->channels
[i
], i
, &err
);
1478 deinit_channel(&dev
->channels
[i
]);
1480 error_propagate(errp
, err
);
1483 static void free_channels(VMBusDevice
*dev
)
1486 for (i
= 0; i
< dev
->num_channels
; i
++) {
1487 deinit_channel(&dev
->channels
[i
]);
1489 g_free(dev
->channels
);
1492 static HvSintRoute
*make_sint_route(VMBus
*vmbus
, uint32_t vp_index
)
1496 if (vp_index
== vmbus
->target_vp
) {
1497 hyperv_sint_route_ref(vmbus
->sint_route
);
1498 return vmbus
->sint_route
;
1501 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1502 if (chan
->target_vp
== vp_index
&& vmbus_channel_is_open(chan
)) {
1503 hyperv_sint_route_ref(chan
->notify_route
);
1504 return chan
->notify_route
;
1508 return hyperv_sint_route_new(vp_index
, VMBUS_SINT
, NULL
, NULL
);
1511 static void open_channel(VMBusChannel
*chan
)
1513 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1515 chan
->gpadl
= vmbus_get_gpadl(chan
, chan
->ringbuf_gpadl
);
1520 if (ringbufs_init(chan
)) {
1524 if (event_notifier_init(&chan
->notifier
, 0)) {
1528 event_notifier_set_handler(&chan
->notifier
, channel_event_cb
);
1530 if (hyperv_set_event_flag_handler(chan_connection_id(chan
),
1532 goto cleanup_notifier
;
1535 chan
->notify_route
= make_sint_route(chan
->vmbus
, chan
->target_vp
);
1536 if (!chan
->notify_route
) {
1537 goto clear_event_flag_handler
;
1540 if (vdc
->open_channel
&& vdc
->open_channel(chan
)) {
1541 goto unref_sint_route
;
1544 chan
->is_open
= true;
1548 hyperv_sint_route_unref(chan
->notify_route
);
1549 clear_event_flag_handler
:
1550 hyperv_set_event_flag_handler(chan_connection_id(chan
), NULL
);
1552 event_notifier_set_handler(&chan
->notifier
, NULL
);
1553 event_notifier_cleanup(&chan
->notifier
);
1555 vmbus_put_gpadl(chan
->gpadl
);
1558 static void close_channel(VMBusChannel
*chan
)
1560 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1562 if (!chan
->is_open
) {
1566 if (vdc
->close_channel
) {
1567 vdc
->close_channel(chan
);
1570 hyperv_sint_route_unref(chan
->notify_route
);
1571 hyperv_set_event_flag_handler(chan_connection_id(chan
), NULL
);
1572 event_notifier_set_handler(&chan
->notifier
, NULL
);
1573 event_notifier_cleanup(&chan
->notifier
);
1574 vmbus_put_gpadl(chan
->gpadl
);
1575 chan
->is_open
= false;
1578 static int channel_post_load(void *opaque
, int version_id
)
1580 VMBusChannel
*chan
= opaque
;
1582 return register_chan_id(chan
);
1585 static const VMStateDescription vmstate_channel
= {
1586 .name
= "vmbus/channel",
1588 .minimum_version_id
= 0,
1589 .post_load
= channel_post_load
,
1590 .fields
= (VMStateField
[]) {
1591 VMSTATE_UINT32(id
, VMBusChannel
),
1592 VMSTATE_UINT16(subchan_idx
, VMBusChannel
),
1593 VMSTATE_UINT32(open_id
, VMBusChannel
),
1594 VMSTATE_UINT32(target_vp
, VMBusChannel
),
1595 VMSTATE_UINT32(ringbuf_gpadl
, VMBusChannel
),
1596 VMSTATE_UINT32(ringbuf_send_offset
, VMBusChannel
),
1597 VMSTATE_UINT8(offer_state
, VMBusChannel
),
1598 VMSTATE_UINT8(state
, VMBusChannel
),
1599 VMSTATE_END_OF_LIST()
1603 static VMBusChannel
*find_channel(VMBus
*vmbus
, uint32_t id
)
1606 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1607 if (chan
->id
== id
) {
1614 static int enqueue_incoming_message(VMBus
*vmbus
,
1615 const struct hyperv_post_message_input
*msg
)
1618 uint8_t idx
, prev_size
;
1620 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
1622 if (vmbus
->rx_queue_size
== HV_MSG_QUEUE_LEN
) {
1627 prev_size
= vmbus
->rx_queue_size
;
1628 idx
= (vmbus
->rx_queue_head
+ vmbus
->rx_queue_size
) % HV_MSG_QUEUE_LEN
;
1629 memcpy(&vmbus
->rx_queue
[idx
], msg
, sizeof(*msg
));
1630 vmbus
->rx_queue_size
++;
1632 /* only need to resched if the queue was empty before */
1634 vmbus_resched(vmbus
);
1637 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
1641 static uint16_t vmbus_recv_message(const struct hyperv_post_message_input
*msg
,
1644 VMBus
*vmbus
= data
;
1645 struct vmbus_message_header
*vmbus_msg
;
1647 if (msg
->message_type
!= HV_MESSAGE_VMBUS
) {
1648 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1651 if (msg
->payload_size
< sizeof(struct vmbus_message_header
)) {
1652 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1655 vmbus_msg
= (struct vmbus_message_header
*)msg
->payload
;
1657 trace_vmbus_recv_message(vmbus_msg
->message_type
, msg
->payload_size
);
1659 if (vmbus_msg
->message_type
== VMBUS_MSG_INVALID
||
1660 vmbus_msg
->message_type
>= VMBUS_MSG_COUNT
) {
1661 error_report("vmbus: unknown message type %#x",
1662 vmbus_msg
->message_type
);
1663 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1666 if (enqueue_incoming_message(vmbus
, msg
)) {
1667 return HV_STATUS_INSUFFICIENT_BUFFERS
;
1669 return HV_STATUS_SUCCESS
;
1672 static bool vmbus_initialized(VMBus
*vmbus
)
1674 return vmbus
->version
> 0 && vmbus
->version
<= VMBUS_VERSION_CURRENT
;
1677 static void vmbus_reset_all(VMBus
*vmbus
)
1679 qbus_reset_all(BUS(vmbus
));
1682 static void post_msg(VMBus
*vmbus
, void *msgdata
, uint32_t msglen
)
1685 struct hyperv_message msg
= {
1686 .header
.message_type
= HV_MESSAGE_VMBUS
,
1689 assert(!vmbus
->msg_in_progress
);
1690 assert(msglen
<= sizeof(msg
.payload
));
1691 assert(msglen
>= sizeof(struct vmbus_message_header
));
1693 vmbus
->msg_in_progress
= true;
1695 trace_vmbus_post_msg(((struct vmbus_message_header
*)msgdata
)->message_type
,
1698 memcpy(msg
.payload
, msgdata
, msglen
);
1699 msg
.header
.payload_size
= ROUND_UP(msglen
, VMBUS_MESSAGE_SIZE_ALIGN
);
1701 ret
= hyperv_post_msg(vmbus
->sint_route
, &msg
);
1702 if (ret
== 0 || ret
== -EAGAIN
) {
1706 error_report("message delivery fatal failure: %d; aborting vmbus", ret
);
1707 vmbus_reset_all(vmbus
);
1710 static int vmbus_init(VMBus
*vmbus
)
1712 if (vmbus
->target_vp
!= (uint32_t)-1) {
1713 vmbus
->sint_route
= hyperv_sint_route_new(vmbus
->target_vp
, VMBUS_SINT
,
1714 vmbus_msg_cb
, vmbus
);
1715 if (!vmbus
->sint_route
) {
1716 error_report("failed to set up SINT route");
1723 static void vmbus_deinit(VMBus
*vmbus
)
1725 VMBusGpadl
*gpadl
, *tmp_gpadl
;
1728 QTAILQ_FOREACH_SAFE(gpadl
, &vmbus
->gpadl_list
, link
, tmp_gpadl
) {
1729 if (gpadl
->state
== VMGPADL_TORNDOWN
) {
1732 vmbus_put_gpadl(gpadl
);
1735 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1736 chan
->offer_state
= VMOFFER_INIT
;
1739 hyperv_sint_route_unref(vmbus
->sint_route
);
1740 vmbus
->sint_route
= NULL
;
1741 vmbus
->int_page_gpa
= 0;
1742 vmbus
->target_vp
= (uint32_t)-1;
1744 vmbus
->state
= VMBUS_LISTEN
;
1745 vmbus
->msg_in_progress
= false;
1748 static void handle_initiate_contact(VMBus
*vmbus
,
1749 vmbus_message_initiate_contact
*msg
,
1752 if (msglen
< sizeof(*msg
)) {
1756 trace_vmbus_initiate_contact(msg
->version_requested
>> 16,
1757 msg
->version_requested
& 0xffff,
1758 msg
->target_vcpu
, msg
->monitor_page1
,
1759 msg
->monitor_page2
, msg
->interrupt_page
);
1762 * Reset vmbus on INITIATE_CONTACT regardless of its previous state.
1763 * Useful, in particular, with vmbus-aware BIOS which can't shut vmbus down
1764 * before handing over to OS loader.
1766 vmbus_reset_all(vmbus
);
1768 vmbus
->target_vp
= msg
->target_vcpu
;
1769 vmbus
->version
= msg
->version_requested
;
1770 if (vmbus
->version
< VMBUS_VERSION_WIN8
) {
1771 /* linux passes interrupt page even when it doesn't need it */
1772 vmbus
->int_page_gpa
= msg
->interrupt_page
;
1774 vmbus
->state
= VMBUS_HANDSHAKE
;
1776 if (vmbus_init(vmbus
)) {
1777 error_report("failed to init vmbus; aborting");
1778 vmbus_deinit(vmbus
);
1783 static void send_handshake(VMBus
*vmbus
)
1785 struct vmbus_message_version_response msg
= {
1786 .header
.message_type
= VMBUS_MSG_VERSION_RESPONSE
,
1787 .version_supported
= vmbus_initialized(vmbus
),
1790 post_msg(vmbus
, &msg
, sizeof(msg
));
1793 static void handle_request_offers(VMBus
*vmbus
, void *msgdata
, uint32_t msglen
)
1797 if (!vmbus_initialized(vmbus
)) {
1801 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1802 if (chan
->offer_state
== VMOFFER_INIT
) {
1803 chan
->offer_state
= VMOFFER_SENDING
;
1808 vmbus
->state
= VMBUS_OFFER
;
1811 static void send_offer(VMBus
*vmbus
)
1814 struct vmbus_message_header alloffers_msg
= {
1815 .message_type
= VMBUS_MSG_ALLOFFERS_DELIVERED
,
1818 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1819 if (chan
->offer_state
== VMOFFER_SENDING
) {
1820 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(chan
->dev
);
1821 /* Hyper-V wants LE GUIDs */
1822 QemuUUID classid
= qemu_uuid_bswap(vdc
->classid
);
1823 QemuUUID instanceid
= qemu_uuid_bswap(chan
->dev
->instanceid
);
1824 struct vmbus_message_offer_channel msg
= {
1825 .header
.message_type
= VMBUS_MSG_OFFERCHANNEL
,
1826 .child_relid
= chan
->id
,
1827 .connection_id
= chan_connection_id(chan
),
1828 .channel_flags
= vdc
->channel_flags
,
1829 .mmio_size_mb
= vdc
->mmio_size_mb
,
1830 .sub_channel_index
= vmbus_channel_idx(chan
),
1831 .interrupt_flags
= VMBUS_OFFER_INTERRUPT_DEDICATED
,
1834 memcpy(msg
.type_uuid
, &classid
, sizeof(classid
));
1835 memcpy(msg
.instance_uuid
, &instanceid
, sizeof(instanceid
));
1837 trace_vmbus_send_offer(chan
->id
, chan
->dev
);
1839 post_msg(vmbus
, &msg
, sizeof(msg
));
1844 /* no more offers, send terminator message */
1845 trace_vmbus_terminate_offers();
1846 post_msg(vmbus
, &alloffers_msg
, sizeof(alloffers_msg
));
1849 static bool complete_offer(VMBus
*vmbus
)
1853 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1854 if (chan
->offer_state
== VMOFFER_SENDING
) {
1855 chan
->offer_state
= VMOFFER_SENT
;
1860 * no transitioning channels found so this is completing the terminator
1861 * message, and vmbus can move to the next state
1866 /* try to mark another channel for offering */
1867 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
1868 if (chan
->offer_state
== VMOFFER_INIT
) {
1869 chan
->offer_state
= VMOFFER_SENDING
;
1874 * if an offer has been sent there are more offers or the terminator yet to
1875 * send, so no state transition for vmbus
1881 static void handle_gpadl_header(VMBus
*vmbus
, vmbus_message_gpadl_header
*msg
,
1885 uint32_t num_gfns
, i
;
1887 /* must include at least one gpa range */
1888 if (msglen
< sizeof(*msg
) + sizeof(msg
->range
[0]) ||
1889 !vmbus_initialized(vmbus
)) {
1893 num_gfns
= (msg
->range_buflen
- msg
->rangecount
* sizeof(msg
->range
[0])) /
1894 sizeof(msg
->range
[0].pfn_array
[0]);
1896 trace_vmbus_gpadl_header(msg
->gpadl_id
, num_gfns
);
1899 * In theory the GPADL_HEADER message can define a GPADL with multiple GPA
1900 * ranges each with arbitrary size and alignment. However in practice only
1901 * single-range page-aligned GPADLs have been observed so just ignore
1902 * anything else and simplify things greatly.
1904 if (msg
->rangecount
!= 1 || msg
->range
[0].byte_offset
||
1905 (msg
->range
[0].byte_count
!= (num_gfns
<< TARGET_PAGE_BITS
))) {
1909 /* ignore requests to create already existing GPADLs */
1910 if (find_gpadl(vmbus
, msg
->gpadl_id
)) {
1914 gpadl
= create_gpadl(vmbus
, msg
->gpadl_id
, msg
->child_relid
, num_gfns
);
1916 for (i
= 0; i
< num_gfns
&&
1917 (void *)&msg
->range
[0].pfn_array
[i
+ 1] <= (void *)msg
+ msglen
;
1919 gpadl
->gfns
[gpadl
->seen_gfns
++] = msg
->range
[0].pfn_array
[i
];
1922 if (gpadl_full(gpadl
)) {
1923 vmbus
->state
= VMBUS_CREATE_GPADL
;
1927 static void handle_gpadl_body(VMBus
*vmbus
, vmbus_message_gpadl_body
*msg
,
1931 uint32_t num_gfns_left
, i
;
1933 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
1937 trace_vmbus_gpadl_body(msg
->gpadl_id
);
1939 gpadl
= find_gpadl(vmbus
, msg
->gpadl_id
);
1944 num_gfns_left
= gpadl
->num_gfns
- gpadl
->seen_gfns
;
1945 assert(num_gfns_left
);
1947 for (i
= 0; i
< num_gfns_left
&&
1948 (void *)&msg
->pfn_array
[i
+ 1] <= (void *)msg
+ msglen
; i
++) {
1949 gpadl
->gfns
[gpadl
->seen_gfns
++] = msg
->pfn_array
[i
];
1952 if (gpadl_full(gpadl
)) {
1953 vmbus
->state
= VMBUS_CREATE_GPADL
;
1957 static void send_create_gpadl(VMBus
*vmbus
)
1961 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1962 if (gpadl_full(gpadl
) && gpadl
->state
== VMGPADL_INIT
) {
1963 struct vmbus_message_gpadl_created msg
= {
1964 .header
.message_type
= VMBUS_MSG_GPADL_CREATED
,
1965 .gpadl_id
= gpadl
->id
,
1966 .child_relid
= gpadl
->child_relid
,
1969 trace_vmbus_gpadl_created(gpadl
->id
);
1970 post_msg(vmbus
, &msg
, sizeof(msg
));
1978 static bool complete_create_gpadl(VMBus
*vmbus
)
1982 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
1983 if (gpadl_full(gpadl
) && gpadl
->state
== VMGPADL_INIT
) {
1984 gpadl
->state
= VMGPADL_ALIVE
;
1994 static void handle_gpadl_teardown(VMBus
*vmbus
,
1995 vmbus_message_gpadl_teardown
*msg
,
2000 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2004 trace_vmbus_gpadl_teardown(msg
->gpadl_id
);
2006 gpadl
= find_gpadl(vmbus
, msg
->gpadl_id
);
2007 if (!gpadl
|| gpadl
->state
== VMGPADL_TORNDOWN
) {
2011 gpadl
->state
= VMGPADL_TEARINGDOWN
;
2012 vmbus
->state
= VMBUS_TEARDOWN_GPADL
;
2015 static void send_teardown_gpadl(VMBus
*vmbus
)
2019 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2020 if (gpadl
->state
== VMGPADL_TEARINGDOWN
) {
2021 struct vmbus_message_gpadl_torndown msg
= {
2022 .header
.message_type
= VMBUS_MSG_GPADL_TORNDOWN
,
2023 .gpadl_id
= gpadl
->id
,
2026 trace_vmbus_gpadl_torndown(gpadl
->id
);
2027 post_msg(vmbus
, &msg
, sizeof(msg
));
2035 static bool complete_teardown_gpadl(VMBus
*vmbus
)
2039 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2040 if (gpadl
->state
== VMGPADL_TEARINGDOWN
) {
2041 gpadl
->state
= VMGPADL_TORNDOWN
;
2042 vmbus_put_gpadl(gpadl
);
2051 static void handle_open_channel(VMBus
*vmbus
, vmbus_message_open_channel
*msg
,
2056 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2060 trace_vmbus_open_channel(msg
->child_relid
, msg
->ring_buffer_gpadl_id
,
2062 chan
= find_channel(vmbus
, msg
->child_relid
);
2063 if (!chan
|| chan
->state
!= VMCHAN_INIT
) {
2067 chan
->ringbuf_gpadl
= msg
->ring_buffer_gpadl_id
;
2068 chan
->ringbuf_send_offset
= msg
->ring_buffer_offset
;
2069 chan
->target_vp
= msg
->target_vp
;
2070 chan
->open_id
= msg
->open_id
;
2074 chan
->state
= VMCHAN_OPENING
;
2075 vmbus
->state
= VMBUS_OPEN_CHANNEL
;
2078 static void send_open_channel(VMBus
*vmbus
)
2082 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2083 if (chan
->state
== VMCHAN_OPENING
) {
2084 struct vmbus_message_open_result msg
= {
2085 .header
.message_type
= VMBUS_MSG_OPENCHANNEL_RESULT
,
2086 .child_relid
= chan
->id
,
2087 .open_id
= chan
->open_id
,
2088 .status
= !vmbus_channel_is_open(chan
),
2091 trace_vmbus_channel_open(chan
->id
, msg
.status
);
2092 post_msg(vmbus
, &msg
, sizeof(msg
));
2100 static bool complete_open_channel(VMBus
*vmbus
)
2104 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2105 if (chan
->state
== VMCHAN_OPENING
) {
2106 if (vmbus_channel_is_open(chan
)) {
2107 chan
->state
= VMCHAN_OPEN
;
2109 * simulate guest notification of ringbuffer space made
2110 * available, for the channel protocols where the host
2111 * initiates the communication
2113 vmbus_channel_notify_host(chan
);
2115 chan
->state
= VMCHAN_INIT
;
2125 static void vdev_reset_on_close(VMBusDevice
*vdev
)
2129 for (i
= 0; i
< vdev
->num_channels
; i
++) {
2130 if (vmbus_channel_is_open(&vdev
->channels
[i
])) {
2135 /* all channels closed -- reset device */
2136 qdev_reset_all(DEVICE(vdev
));
2139 static void handle_close_channel(VMBus
*vmbus
, vmbus_message_close_channel
*msg
,
2144 if (msglen
< sizeof(*msg
) || !vmbus_initialized(vmbus
)) {
2148 trace_vmbus_close_channel(msg
->child_relid
);
2150 chan
= find_channel(vmbus
, msg
->child_relid
);
2155 close_channel(chan
);
2156 chan
->state
= VMCHAN_INIT
;
2158 vdev_reset_on_close(chan
->dev
);
2161 static void handle_unload(VMBus
*vmbus
, void *msg
, uint32_t msglen
)
2163 vmbus
->state
= VMBUS_UNLOAD
;
2166 static void send_unload(VMBus
*vmbus
)
2168 vmbus_message_header msg
= {
2169 .message_type
= VMBUS_MSG_UNLOAD_RESPONSE
,
2172 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
2173 vmbus
->rx_queue_size
= 0;
2174 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
2176 post_msg(vmbus
, &msg
, sizeof(msg
));
2180 static bool complete_unload(VMBus
*vmbus
)
2182 vmbus_reset_all(vmbus
);
2186 static void process_message(VMBus
*vmbus
)
2188 struct hyperv_post_message_input
*hv_msg
;
2189 struct vmbus_message_header
*msg
;
2193 qemu_mutex_lock(&vmbus
->rx_queue_lock
);
2195 if (!vmbus
->rx_queue_size
) {
2199 hv_msg
= &vmbus
->rx_queue
[vmbus
->rx_queue_head
];
2200 msglen
= hv_msg
->payload_size
;
2201 if (msglen
< sizeof(*msg
)) {
2204 msgdata
= hv_msg
->payload
;
2205 msg
= (struct vmbus_message_header
*)msgdata
;
2207 trace_vmbus_process_incoming_message(msg
->message_type
);
2209 switch (msg
->message_type
) {
2210 case VMBUS_MSG_INITIATE_CONTACT
:
2211 handle_initiate_contact(vmbus
, msgdata
, msglen
);
2213 case VMBUS_MSG_REQUESTOFFERS
:
2214 handle_request_offers(vmbus
, msgdata
, msglen
);
2216 case VMBUS_MSG_GPADL_HEADER
:
2217 handle_gpadl_header(vmbus
, msgdata
, msglen
);
2219 case VMBUS_MSG_GPADL_BODY
:
2220 handle_gpadl_body(vmbus
, msgdata
, msglen
);
2222 case VMBUS_MSG_GPADL_TEARDOWN
:
2223 handle_gpadl_teardown(vmbus
, msgdata
, msglen
);
2225 case VMBUS_MSG_OPENCHANNEL
:
2226 handle_open_channel(vmbus
, msgdata
, msglen
);
2228 case VMBUS_MSG_CLOSECHANNEL
:
2229 handle_close_channel(vmbus
, msgdata
, msglen
);
2231 case VMBUS_MSG_UNLOAD
:
2232 handle_unload(vmbus
, msgdata
, msglen
);
2235 error_report("unknown message type %#x", msg
->message_type
);
2240 vmbus
->rx_queue_size
--;
2241 vmbus
->rx_queue_head
++;
2242 vmbus
->rx_queue_head
%= HV_MSG_QUEUE_LEN
;
2244 vmbus_resched(vmbus
);
2246 qemu_mutex_unlock(&vmbus
->rx_queue_lock
);
2249 static const struct {
2250 void (*run
)(VMBus
*vmbus
);
2251 bool (*complete
)(VMBus
*vmbus
);
2252 } state_runner
[] = {
2253 [VMBUS_LISTEN
] = {process_message
, NULL
},
2254 [VMBUS_HANDSHAKE
] = {send_handshake
, NULL
},
2255 [VMBUS_OFFER
] = {send_offer
, complete_offer
},
2256 [VMBUS_CREATE_GPADL
] = {send_create_gpadl
, complete_create_gpadl
},
2257 [VMBUS_TEARDOWN_GPADL
] = {send_teardown_gpadl
, complete_teardown_gpadl
},
2258 [VMBUS_OPEN_CHANNEL
] = {send_open_channel
, complete_open_channel
},
2259 [VMBUS_UNLOAD
] = {send_unload
, complete_unload
},
2262 static void vmbus_do_run(VMBus
*vmbus
)
2264 if (vmbus
->msg_in_progress
) {
2268 assert(vmbus
->state
< VMBUS_STATE_MAX
);
2269 assert(state_runner
[vmbus
->state
].run
);
2270 state_runner
[vmbus
->state
].run(vmbus
);
2273 static void vmbus_run(void *opaque
)
2275 VMBus
*vmbus
= opaque
;
2277 /* make sure no recursion happens (e.g. due to recursive aio_poll()) */
2278 if (vmbus
->in_progress
) {
2282 vmbus
->in_progress
= true;
2284 * FIXME: if vmbus_resched() is called from within vmbus_do_run(), it
2285 * should go *after* the code that can result in aio_poll; otherwise
2286 * reschedules can be missed. No idea how to enforce that.
2288 vmbus_do_run(vmbus
);
2289 vmbus
->in_progress
= false;
2292 static void vmbus_msg_cb(void *data
, int status
)
2294 VMBus
*vmbus
= data
;
2295 bool (*complete
)(VMBus
*vmbus
);
2297 assert(vmbus
->msg_in_progress
);
2299 trace_vmbus_msg_cb(status
);
2301 if (status
== -EAGAIN
) {
2305 error_report("message delivery fatal failure: %d; aborting vmbus",
2307 vmbus_reset_all(vmbus
);
2311 assert(vmbus
->state
< VMBUS_STATE_MAX
);
2312 complete
= state_runner
[vmbus
->state
].complete
;
2313 if (!complete
|| complete(vmbus
)) {
2314 vmbus
->state
= VMBUS_LISTEN
;
2317 vmbus
->msg_in_progress
= false;
2318 vmbus_resched(vmbus
);
2321 static void vmbus_resched(VMBus
*vmbus
)
2323 aio_bh_schedule_oneshot(qemu_get_aio_context(), vmbus_run
, vmbus
);
2326 static void vmbus_signal_event(EventNotifier
*e
)
2329 VMBus
*vmbus
= container_of(e
, VMBus
, notifier
);
2330 unsigned long *int_map
;
2332 bool is_dirty
= false;
2334 if (!event_notifier_test_and_clear(e
)) {
2338 trace_vmbus_signal_event();
2340 if (!vmbus
->int_page_gpa
) {
2344 addr
= vmbus
->int_page_gpa
+ TARGET_PAGE_SIZE
/ 2;
2345 len
= TARGET_PAGE_SIZE
/ 2;
2346 int_map
= cpu_physical_memory_map(addr
, &len
, 1);
2347 if (len
!= TARGET_PAGE_SIZE
/ 2) {
2351 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2352 if (bitmap_test_and_clear_atomic(int_map
, chan
->id
, 1)) {
2353 if (!vmbus_channel_is_open(chan
)) {
2356 vmbus_channel_notify_host(chan
);
2362 cpu_physical_memory_unmap(int_map
, len
, 1, is_dirty
);
2365 static void vmbus_dev_realize(DeviceState
*dev
, Error
**errp
)
2367 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2368 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2369 VMBus
*vmbus
= VMBUS(qdev_get_parent_bus(dev
));
2372 char idstr
[UUID_FMT_LEN
+ 1];
2374 assert(!qemu_uuid_is_null(&vdev
->instanceid
));
2376 /* Check for instance id collision for this class id */
2377 QTAILQ_FOREACH(child
, &BUS(vmbus
)->children
, sibling
) {
2378 VMBusDevice
*child_dev
= VMBUS_DEVICE(child
->child
);
2380 if (child_dev
== vdev
) {
2384 if (qemu_uuid_is_equal(&child_dev
->instanceid
, &vdev
->instanceid
)) {
2385 qemu_uuid_unparse(&vdev
->instanceid
, idstr
);
2386 error_setg(&err
, "duplicate vmbus device instance id %s", idstr
);
2391 vdev
->dma_as
= &address_space_memory
;
2393 create_channels(vmbus
, vdev
, &err
);
2398 if (vdc
->vmdev_realize
) {
2399 vdc
->vmdev_realize(vdev
, &err
);
2401 goto err_vdc_realize
;
2407 free_channels(vdev
);
2409 error_propagate(errp
, err
);
2412 static void vmbus_dev_reset(DeviceState
*dev
)
2415 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2416 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2418 if (vdev
->channels
) {
2419 for (i
= 0; i
< vdev
->num_channels
; i
++) {
2420 VMBusChannel
*chan
= &vdev
->channels
[i
];
2421 close_channel(chan
);
2422 chan
->state
= VMCHAN_INIT
;
2426 if (vdc
->vmdev_reset
) {
2427 vdc
->vmdev_reset(vdev
);
2431 static void vmbus_dev_unrealize(DeviceState
*dev
)
2433 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2434 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2436 if (vdc
->vmdev_unrealize
) {
2437 vdc
->vmdev_unrealize(vdev
);
2439 free_channels(vdev
);
2442 static void vmbus_dev_class_init(ObjectClass
*klass
, void *data
)
2444 DeviceClass
*kdev
= DEVICE_CLASS(klass
);
2445 kdev
->bus_type
= TYPE_VMBUS
;
2446 kdev
->realize
= vmbus_dev_realize
;
2447 kdev
->unrealize
= vmbus_dev_unrealize
;
2448 kdev
->reset
= vmbus_dev_reset
;
2451 static Property vmbus_dev_instanceid
=
2452 DEFINE_PROP_UUID("instanceid", VMBusDevice
, instanceid
);
2454 static void vmbus_dev_instance_init(Object
*obj
)
2456 VMBusDevice
*vdev
= VMBUS_DEVICE(obj
);
2457 VMBusDeviceClass
*vdc
= VMBUS_DEVICE_GET_CLASS(vdev
);
2459 if (!qemu_uuid_is_null(&vdc
->instanceid
)) {
2460 /* Class wants to only have a single instance with a fixed UUID */
2461 vdev
->instanceid
= vdc
->instanceid
;
2463 qdev_property_add_static(DEVICE(vdev
), &vmbus_dev_instanceid
);
2467 const VMStateDescription vmstate_vmbus_dev
= {
2468 .name
= TYPE_VMBUS_DEVICE
,
2470 .minimum_version_id
= 0,
2471 .fields
= (VMStateField
[]) {
2472 VMSTATE_UINT8_ARRAY(instanceid
.data
, VMBusDevice
, 16),
2473 VMSTATE_UINT16(num_channels
, VMBusDevice
),
2474 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(channels
, VMBusDevice
,
2475 num_channels
, vmstate_channel
,
2477 VMSTATE_END_OF_LIST()
2481 /* vmbus generic device base */
2482 static const TypeInfo vmbus_dev_type_info
= {
2483 .name
= TYPE_VMBUS_DEVICE
,
2484 .parent
= TYPE_DEVICE
,
2486 .instance_size
= sizeof(VMBusDevice
),
2487 .class_size
= sizeof(VMBusDeviceClass
),
2488 .class_init
= vmbus_dev_class_init
,
2489 .instance_init
= vmbus_dev_instance_init
,
2492 static void vmbus_realize(BusState
*bus
, Error
**errp
)
2495 Error
*local_err
= NULL
;
2496 VMBus
*vmbus
= VMBUS(bus
);
2498 qemu_mutex_init(&vmbus
->rx_queue_lock
);
2500 QTAILQ_INIT(&vmbus
->gpadl_list
);
2501 QTAILQ_INIT(&vmbus
->channel_list
);
2503 ret
= hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
,
2504 vmbus_recv_message
, vmbus
);
2506 error_setg(&local_err
, "hyperv set message handler failed: %d", ret
);
2510 ret
= event_notifier_init(&vmbus
->notifier
, 0);
2512 error_setg(&local_err
, "event notifier failed to init with %d", ret
);
2513 goto remove_msg_handler
;
2516 event_notifier_set_handler(&vmbus
->notifier
, vmbus_signal_event
);
2517 ret
= hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID
,
2520 error_setg(&local_err
, "hyperv set event handler failed with %d", ret
);
2521 goto clear_event_notifier
;
2526 clear_event_notifier
:
2527 event_notifier_cleanup(&vmbus
->notifier
);
2529 hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
, NULL
, NULL
);
2531 qemu_mutex_destroy(&vmbus
->rx_queue_lock
);
2532 error_propagate(errp
, local_err
);
2535 static void vmbus_unrealize(BusState
*bus
)
2537 VMBus
*vmbus
= VMBUS(bus
);
2539 hyperv_set_msg_handler(VMBUS_MESSAGE_CONNECTION_ID
, NULL
, NULL
);
2540 hyperv_set_event_flag_handler(VMBUS_EVENT_CONNECTION_ID
, NULL
);
2541 event_notifier_cleanup(&vmbus
->notifier
);
2543 qemu_mutex_destroy(&vmbus
->rx_queue_lock
);
2546 static void vmbus_reset(BusState
*bus
)
2548 vmbus_deinit(VMBUS(bus
));
2551 static char *vmbus_get_dev_path(DeviceState
*dev
)
2553 BusState
*bus
= qdev_get_parent_bus(dev
);
2554 return qdev_get_dev_path(bus
->parent
);
2557 static char *vmbus_get_fw_dev_path(DeviceState
*dev
)
2559 VMBusDevice
*vdev
= VMBUS_DEVICE(dev
);
2560 char uuid
[UUID_FMT_LEN
+ 1];
2562 qemu_uuid_unparse(&vdev
->instanceid
, uuid
);
2563 return g_strdup_printf("%s@%s", qdev_fw_name(dev
), uuid
);
2566 static void vmbus_class_init(ObjectClass
*klass
, void *data
)
2568 BusClass
*k
= BUS_CLASS(klass
);
2570 k
->get_dev_path
= vmbus_get_dev_path
;
2571 k
->get_fw_dev_path
= vmbus_get_fw_dev_path
;
2572 k
->realize
= vmbus_realize
;
2573 k
->unrealize
= vmbus_unrealize
;
2574 k
->reset
= vmbus_reset
;
2577 static int vmbus_pre_load(void *opaque
)
2580 VMBus
*vmbus
= VMBUS(opaque
);
2583 * channel IDs allocated by the source will come in the migration stream
2584 * for each channel, so clean up the ones allocated at realize
2586 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2587 unregister_chan_id(chan
);
2592 static int vmbus_post_load(void *opaque
, int version_id
)
2595 VMBus
*vmbus
= VMBUS(opaque
);
2599 ret
= vmbus_init(vmbus
);
2604 QTAILQ_FOREACH(gpadl
, &vmbus
->gpadl_list
, link
) {
2605 gpadl
->vmbus
= vmbus
;
2606 gpadl
->refcount
= 1;
2610 * reopening channels depends on initialized vmbus so it's done here
2611 * instead of channel_post_load()
2613 QTAILQ_FOREACH(chan
, &vmbus
->channel_list
, link
) {
2615 if (chan
->state
== VMCHAN_OPENING
|| chan
->state
== VMCHAN_OPEN
) {
2619 if (chan
->state
!= VMCHAN_OPEN
) {
2623 if (!vmbus_channel_is_open(chan
)) {
2624 /* reopen failed, abort loading */
2628 /* resume processing on the guest side if it missed the notification */
2629 hyperv_sint_route_set_sint(chan
->notify_route
);
2630 /* ditto on the host side */
2631 vmbus_channel_notify_host(chan
);
2634 vmbus_resched(vmbus
);
2638 static const VMStateDescription vmstate_post_message_input
= {
2639 .name
= "vmbus/hyperv_post_message_input",
2641 .minimum_version_id
= 0,
2642 .fields
= (VMStateField
[]) {
2644 * skip connection_id and message_type as they are validated before
2645 * queueing and ignored on dequeueing
2647 VMSTATE_UINT32(payload_size
, struct hyperv_post_message_input
),
2648 VMSTATE_UINT8_ARRAY(payload
, struct hyperv_post_message_input
,
2649 HV_MESSAGE_PAYLOAD_SIZE
),
2650 VMSTATE_END_OF_LIST()
2654 static bool vmbus_rx_queue_needed(void *opaque
)
2656 VMBus
*vmbus
= VMBUS(opaque
);
2657 return vmbus
->rx_queue_size
;
2660 static const VMStateDescription vmstate_rx_queue
= {
2661 .name
= "vmbus/rx_queue",
2663 .minimum_version_id
= 0,
2664 .needed
= vmbus_rx_queue_needed
,
2665 .fields
= (VMStateField
[]) {
2666 VMSTATE_UINT8(rx_queue_head
, VMBus
),
2667 VMSTATE_UINT8(rx_queue_size
, VMBus
),
2668 VMSTATE_STRUCT_ARRAY(rx_queue
, VMBus
,
2669 HV_MSG_QUEUE_LEN
, 0,
2670 vmstate_post_message_input
,
2671 struct hyperv_post_message_input
),
2672 VMSTATE_END_OF_LIST()
2676 static const VMStateDescription vmstate_vmbus
= {
2679 .minimum_version_id
= 0,
2680 .pre_load
= vmbus_pre_load
,
2681 .post_load
= vmbus_post_load
,
2682 .fields
= (VMStateField
[]) {
2683 VMSTATE_UINT8(state
, VMBus
),
2684 VMSTATE_UINT32(version
, VMBus
),
2685 VMSTATE_UINT32(target_vp
, VMBus
),
2686 VMSTATE_UINT64(int_page_gpa
, VMBus
),
2687 VMSTATE_QTAILQ_V(gpadl_list
, VMBus
, 0,
2688 vmstate_gpadl
, VMBusGpadl
, link
),
2689 VMSTATE_END_OF_LIST()
2691 .subsections
= (const VMStateDescription
* []) {
2697 static const TypeInfo vmbus_type_info
= {
2700 .instance_size
= sizeof(VMBus
),
2701 .class_init
= vmbus_class_init
,
2704 static void vmbus_bridge_realize(DeviceState
*dev
, Error
**errp
)
2706 VMBusBridge
*bridge
= VMBUS_BRIDGE(dev
);
2709 * here there's at least one vmbus bridge that is being realized, so
2710 * vmbus_bridge_find can only return NULL if it's not unique
2712 if (!vmbus_bridge_find()) {
2713 error_setg(errp
, "there can be at most one %s in the system",
2718 if (!hyperv_is_synic_enabled()) {
2719 error_report("VMBus requires usable Hyper-V SynIC and VP_INDEX");
2723 bridge
->bus
= VMBUS(qbus_create(TYPE_VMBUS
, dev
, "vmbus"));
2726 static char *vmbus_bridge_ofw_unit_address(const SysBusDevice
*dev
)
2728 /* there can be only one VMBus */
2729 return g_strdup("0");
2732 static const VMStateDescription vmstate_vmbus_bridge
= {
2733 .name
= TYPE_VMBUS_BRIDGE
,
2735 .minimum_version_id
= 0,
2736 .fields
= (VMStateField
[]) {
2737 VMSTATE_STRUCT_POINTER(bus
, VMBusBridge
, vmstate_vmbus
, VMBus
),
2738 VMSTATE_END_OF_LIST()
2742 static Property vmbus_bridge_props
[] = {
2743 DEFINE_PROP_UINT8("irq", VMBusBridge
, irq
, 7),
2744 DEFINE_PROP_END_OF_LIST()
2747 static void vmbus_bridge_class_init(ObjectClass
*klass
, void *data
)
2749 DeviceClass
*k
= DEVICE_CLASS(klass
);
2750 SysBusDeviceClass
*sk
= SYS_BUS_DEVICE_CLASS(klass
);
2752 k
->realize
= vmbus_bridge_realize
;
2753 k
->fw_name
= "vmbus";
2754 sk
->explicit_ofw_unit_address
= vmbus_bridge_ofw_unit_address
;
2755 set_bit(DEVICE_CATEGORY_BRIDGE
, k
->categories
);
2756 k
->vmsd
= &vmstate_vmbus_bridge
;
2757 device_class_set_props(k
, vmbus_bridge_props
);
2758 /* override SysBusDevice's default */
2759 k
->user_creatable
= true;
2762 static const TypeInfo vmbus_bridge_type_info
= {
2763 .name
= TYPE_VMBUS_BRIDGE
,
2764 .parent
= TYPE_SYS_BUS_DEVICE
,
2765 .instance_size
= sizeof(VMBusBridge
),
2766 .class_init
= vmbus_bridge_class_init
,
2769 static void vmbus_register_types(void)
2771 type_register_static(&vmbus_bridge_type_info
);
2772 type_register_static(&vmbus_dev_type_info
);
2773 type_register_static(&vmbus_type_info
);
2776 type_init(vmbus_register_types
)