2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
41 #include <machine/atomic.h>
42 #include <machine/bus.h>
45 #include <vm/vm_param.h>
48 #include <dev/hyperv/vmbus/hv_vmbus_priv.h>
49 #include <dev/hyperv/vmbus/hyperv_var.h>
50 #include <dev/hyperv/vmbus/vmbus_reg.h>
51 #include <dev/hyperv/vmbus/vmbus_var.h>
53 static void vmbus_channel_set_event(hv_vmbus_channel* channel);
54 static void VmbusProcessChannelEvent(void* channel, int pending);
55 static void vmbus_chan_update_evtflagcnt(struct vmbus_softc *,
56 const struct hv_vmbus_channel *);
59 * @brief Trigger an event notification on the specified channel
62 vmbus_channel_set_event(hv_vmbus_channel *channel)
64 struct vmbus_softc *sc = channel->vmbus_sc;
65 uint32_t chanid = channel->ch_id;
67 atomic_set_long(&sc->vmbus_tx_evtflags[chanid >> VMBUS_EVTFLAG_SHIFT],
68 1UL << (chanid & VMBUS_EVTFLAG_MASK));
70 if (channel->ch_flags & VMBUS_CHAN_FLAG_HASMNF) {
71 hv_vmbus_monitor_page *monitor_page;
73 monitor_page = sc->vmbus_mnf2;
74 synch_set_bit(channel->monitor_bit,
75 (uint32_t *)&monitor_page->
76 trigger_group[channel->monitor_group].u.pending);
78 hypercall_signal_event(channel->ch_sigevt_dma.hv_paddr);
84 vmbus_channel_sysctl_monalloc(SYSCTL_HANDLER_ARGS)
86 struct hv_vmbus_channel *chan = arg1;
89 if (chan->ch_flags & VMBUS_CHAN_FLAG_HASMNF)
91 return sysctl_handle_int(oidp, &alloc, 0, req);
95 vmbus_channel_sysctl_create(hv_vmbus_channel* channel)
98 struct sysctl_oid *devch_sysctl;
99 struct sysctl_oid *devch_id_sysctl, *devch_sub_sysctl;
100 struct sysctl_oid *devch_id_in_sysctl, *devch_id_out_sysctl;
101 struct sysctl_ctx_list *ctx;
106 hv_vmbus_channel* primary_ch = channel->primary_channel;
108 if (primary_ch == NULL) {
109 dev = channel->ch_dev;
110 ch_id = channel->ch_id;
112 dev = primary_ch->ch_dev;
113 ch_id = primary_ch->ch_id;
114 sub_ch_id = channel->ch_subidx;
116 ctx = &channel->ch_sysctl_ctx;
117 sysctl_ctx_init(ctx);
118 /* This creates dev.DEVNAME.DEVUNIT.channel tree */
119 devch_sysctl = SYSCTL_ADD_NODE(ctx,
120 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
121 OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
122 /* This creates dev.DEVNAME.DEVUNIT.channel.CHANID tree */
123 snprintf(name, sizeof(name), "%d", ch_id);
124 devch_id_sysctl = SYSCTL_ADD_NODE(ctx,
125 SYSCTL_CHILDREN(devch_sysctl),
126 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
128 if (primary_ch != NULL) {
129 devch_sub_sysctl = SYSCTL_ADD_NODE(ctx,
130 SYSCTL_CHILDREN(devch_id_sysctl),
131 OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
132 snprintf(name, sizeof(name), "%d", sub_ch_id);
133 devch_id_sysctl = SYSCTL_ADD_NODE(ctx,
134 SYSCTL_CHILDREN(devch_sub_sysctl),
135 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
137 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl),
138 OID_AUTO, "chanid", CTLFLAG_RD,
139 &channel->ch_id, 0, "channel id");
141 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO,
142 "cpu", CTLFLAG_RD, &channel->target_cpu, 0, "owner CPU id");
143 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO,
144 "monitor_allocated", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
145 channel, 0, vmbus_channel_sysctl_monalloc, "I",
146 "is monitor allocated to this channel");
148 devch_id_in_sysctl = SYSCTL_ADD_NODE(ctx,
149 SYSCTL_CHILDREN(devch_id_sysctl),
152 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
153 devch_id_out_sysctl = SYSCTL_ADD_NODE(ctx,
154 SYSCTL_CHILDREN(devch_id_sysctl),
157 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
158 hv_ring_buffer_stat(ctx,
159 SYSCTL_CHILDREN(devch_id_in_sysctl),
161 "inbound ring buffer stats");
162 hv_ring_buffer_stat(ctx,
163 SYSCTL_CHILDREN(devch_id_out_sysctl),
164 &(channel->outbound),
165 "outbound ring buffer stats");
169 * @brief Open the specified channel
172 hv_vmbus_channel_open(
173 hv_vmbus_channel* new_channel,
174 uint32_t send_ring_buffer_size,
175 uint32_t recv_ring_buffer_size,
177 uint32_t user_data_len,
178 hv_vmbus_pfn_channel_callback pfn_on_channel_callback,
181 struct vmbus_softc *sc = new_channel->vmbus_sc;
182 const struct vmbus_chanmsg_chopen_resp *resp;
183 const struct vmbus_message *msg;
184 struct vmbus_chanmsg_chopen *req;
185 struct vmbus_msghc *mh;
190 if (user_data_len > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) {
191 device_printf(sc->vmbus_dev,
192 "invalid udata len %u for chan%u\n",
193 user_data_len, new_channel->ch_id);
197 mtx_lock(&new_channel->sc_lock);
198 if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
199 new_channel->state = HV_CHANNEL_OPENING_STATE;
201 mtx_unlock(&new_channel->sc_lock);
203 printf("VMBUS: Trying to open channel <%p> which in "
204 "%d state.\n", new_channel, new_channel->state);
207 mtx_unlock(&new_channel->sc_lock);
209 new_channel->on_channel_callback = pfn_on_channel_callback;
210 new_channel->channel_callback_context = context;
212 vmbus_chan_update_evtflagcnt(sc, new_channel);
214 new_channel->rxq = VMBUS_PCPU_GET(new_channel->vmbus_sc, event_tq,
215 new_channel->target_cpu);
216 TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
218 /* Allocate the ring buffer */
219 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
220 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
222 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
226 in = ((uint8_t *) out + send_ring_buffer_size);
228 new_channel->ring_buffer_pages = out;
229 new_channel->ring_buffer_page_count = (send_ring_buffer_size +
230 recv_ring_buffer_size) >> PAGE_SHIFT;
231 new_channel->ring_buffer_size = send_ring_buffer_size +
232 recv_ring_buffer_size;
234 hv_vmbus_ring_buffer_init(
235 &new_channel->outbound,
237 send_ring_buffer_size);
239 hv_vmbus_ring_buffer_init(
240 &new_channel->inbound,
242 recv_ring_buffer_size);
244 /* Create sysctl tree for this channel */
245 vmbus_channel_sysctl_create(new_channel);
248 * Establish the gpadl for the ring buffer
250 new_channel->ring_buffer_gpadl_handle = 0;
252 ret = hv_vmbus_channel_establish_gpadl(new_channel,
253 new_channel->outbound.ring_buffer,
254 send_ring_buffer_size + recv_ring_buffer_size,
255 &new_channel->ring_buffer_gpadl_handle);
258 * Open channel w/ the bufring GPADL on the target CPU.
260 mh = vmbus_msghc_get(sc, sizeof(*req));
262 device_printf(sc->vmbus_dev,
263 "can not get msg hypercall for chopen(chan%u)\n",
268 req = vmbus_msghc_dataptr(mh);
269 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN;
270 req->chm_chanid = new_channel->ch_id;
271 req->chm_openid = new_channel->ch_id;
272 req->chm_gpadl = new_channel->ring_buffer_gpadl_handle;
273 req->chm_vcpuid = new_channel->target_vcpu;
274 req->chm_rxbr_pgofs = send_ring_buffer_size >> PAGE_SHIFT;
276 memcpy(req->chm_udata, user_data, user_data_len);
278 ret = vmbus_msghc_exec(sc, mh);
280 device_printf(sc->vmbus_dev,
281 "chopen(chan%u) msg hypercall exec failed: %d\n",
282 new_channel->ch_id, ret);
283 vmbus_msghc_put(sc, mh);
287 msg = vmbus_msghc_wait_result(sc, mh);
288 resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data;
289 status = resp->chm_status;
291 vmbus_msghc_put(sc, mh);
294 new_channel->state = HV_CHANNEL_OPENED_STATE;
296 device_printf(sc->vmbus_dev, "chan%u opened\n",
300 device_printf(sc->vmbus_dev, "failed to open chan%u\n",
308 * @brief Establish a GPADL for the specified buffer
311 hv_vmbus_channel_establish_gpadl(struct hv_vmbus_channel *channel,
312 void *contig_buffer, uint32_t size, uint32_t *gpadl0)
314 struct vmbus_softc *sc = channel->vmbus_sc;
315 struct vmbus_msghc *mh;
316 struct vmbus_chanmsg_gpadl_conn *req;
317 const struct vmbus_message *msg;
319 uint32_t gpadl, status;
320 int page_count, range_len, i, cnt, error;
321 uint64_t page_id, paddr;
324 * Preliminary checks.
327 KASSERT((size & PAGE_MASK) == 0,
328 ("invalid GPA size %u, not multiple page size", size));
329 page_count = size >> PAGE_SHIFT;
331 paddr = hv_get_phys_addr(contig_buffer);
332 KASSERT((paddr & PAGE_MASK) == 0,
333 ("GPA is not page aligned %jx", (uintmax_t)paddr));
334 page_id = paddr >> PAGE_SHIFT;
336 range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]);
338 * We don't support multiple GPA ranges.
340 if (range_len > UINT16_MAX) {
341 device_printf(sc->vmbus_dev, "GPA too large, %d pages\n",
349 gpadl = vmbus_gpadl_alloc(sc);
353 * Connect this GPADL to the target channel.
356 * Since each message can only hold small set of page
357 * addresses, several messages may be required to
358 * complete the connection.
360 if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX)
361 cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX;
366 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn,
367 chm_range.gpa_page[cnt]);
368 mh = vmbus_msghc_get(sc, reqsz);
370 device_printf(sc->vmbus_dev,
371 "can not get msg hypercall for gpadl->chan%u\n",
376 req = vmbus_msghc_dataptr(mh);
377 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN;
378 req->chm_chanid = channel->ch_id;
379 req->chm_gpadl = gpadl;
380 req->chm_range_len = range_len;
381 req->chm_range_cnt = 1;
382 req->chm_range.gpa_len = size;
383 req->chm_range.gpa_ofs = 0;
384 for (i = 0; i < cnt; ++i)
385 req->chm_range.gpa_page[i] = page_id++;
387 error = vmbus_msghc_exec(sc, mh);
389 device_printf(sc->vmbus_dev,
390 "gpadl->chan%u msg hypercall exec failed: %d\n",
391 channel->ch_id, error);
392 vmbus_msghc_put(sc, mh);
396 while (page_count > 0) {
397 struct vmbus_chanmsg_gpadl_subconn *subreq;
399 if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX)
400 cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX;
405 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn,
407 vmbus_msghc_reset(mh, reqsz);
409 subreq = vmbus_msghc_dataptr(mh);
410 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN;
411 subreq->chm_gpadl = gpadl;
412 for (i = 0; i < cnt; ++i)
413 subreq->chm_gpa_page[i] = page_id++;
415 vmbus_msghc_exec_noresult(mh);
417 KASSERT(page_count == 0, ("invalid page count %d", page_count));
419 msg = vmbus_msghc_wait_result(sc, mh);
420 status = ((const struct vmbus_chanmsg_gpadl_connresp *)
421 msg->msg_data)->chm_status;
423 vmbus_msghc_put(sc, mh);
426 device_printf(sc->vmbus_dev, "gpadl->chan%u failed: "
427 "status %u\n", channel->ch_id, status);
431 device_printf(sc->vmbus_dev, "gpadl->chan%u "
432 "succeeded\n", channel->ch_id);
439 * Disconnect the GPA from the target channel
442 hv_vmbus_channel_teardown_gpdal(struct hv_vmbus_channel *chan, uint32_t gpadl)
444 struct vmbus_softc *sc = chan->vmbus_sc;
445 struct vmbus_msghc *mh;
446 struct vmbus_chanmsg_gpadl_disconn *req;
449 mh = vmbus_msghc_get(sc, sizeof(*req));
451 device_printf(sc->vmbus_dev,
452 "can not get msg hypercall for gpa x->chan%u\n",
457 req = vmbus_msghc_dataptr(mh);
458 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN;
459 req->chm_chanid = chan->ch_id;
460 req->chm_gpadl = gpadl;
462 error = vmbus_msghc_exec(sc, mh);
464 device_printf(sc->vmbus_dev,
465 "gpa x->chan%u msg hypercall exec failed: %d\n",
467 vmbus_msghc_put(sc, mh);
471 vmbus_msghc_wait_result(sc, mh);
472 /* Discard result; no useful information */
473 vmbus_msghc_put(sc, mh);
479 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
481 struct vmbus_softc *sc = channel->vmbus_sc;
482 struct vmbus_msghc *mh;
483 struct vmbus_chanmsg_chclose *req;
484 struct taskqueue *rxq = channel->rxq;
487 channel->state = HV_CHANNEL_OPEN_STATE;
488 sysctl_ctx_free(&channel->ch_sysctl_ctx);
491 * set rxq to NULL to avoid more requests be scheduled
494 taskqueue_drain(rxq, &channel->channel_task);
495 channel->on_channel_callback = NULL;
498 * Send a closing message
501 mh = vmbus_msghc_get(sc, sizeof(*req));
503 device_printf(sc->vmbus_dev,
504 "can not get msg hypercall for chclose(chan%u)\n",
509 req = vmbus_msghc_dataptr(mh);
510 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE;
511 req->chm_chanid = channel->ch_id;
513 error = vmbus_msghc_exec_noresult(mh);
514 vmbus_msghc_put(sc, mh);
517 device_printf(sc->vmbus_dev,
518 "chclose(chan%u) msg hypercall exec failed: %d\n",
519 channel->ch_id, error);
521 } else if (bootverbose) {
522 device_printf(sc->vmbus_dev, "close chan%u\n",
526 /* Tear down the gpadl for the channel's ring buffer */
527 if (channel->ring_buffer_gpadl_handle) {
528 hv_vmbus_channel_teardown_gpdal(channel,
529 channel->ring_buffer_gpadl_handle);
532 /* TODO: Send a msg to release the childRelId */
534 /* cleanup the ring buffers for this channel */
535 hv_ring_buffer_cleanup(&channel->outbound);
536 hv_ring_buffer_cleanup(&channel->inbound);
538 contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
543 * @brief Close the specified channel
546 hv_vmbus_channel_close(hv_vmbus_channel *channel)
548 hv_vmbus_channel* sub_channel;
550 if (channel->primary_channel != NULL) {
552 * We only close multi-channels when the primary is
559 * Close all multi-channels first.
561 TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
563 if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
565 hv_vmbus_channel_close_internal(sub_channel);
568 * Then close the primary channel.
570 hv_vmbus_channel_close_internal(channel);
574 * @brief Send the specified buffer on the given channel
577 hv_vmbus_channel_send_packet(
578 hv_vmbus_channel* channel,
582 hv_vmbus_packet_type type,
586 hv_vm_packet_descriptor desc;
588 uint64_t aligned_data;
589 uint32_t packet_len_aligned;
591 hv_vmbus_sg_buffer_list buffer_list[3];
593 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
594 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
597 /* Setup the descriptor */
598 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */
599 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
600 /* in 8-bytes granularity */
601 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
602 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
603 desc.transaction_id = request_id;
605 buffer_list[0].data = &desc;
606 buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
608 buffer_list[1].data = buffer;
609 buffer_list[1].length = buffer_len;
611 buffer_list[2].data = &aligned_data;
612 buffer_list[2].length = packet_len_aligned - packet_len;
614 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
617 /* TODO: We should determine if this is optional */
618 if (ret == 0 && need_sig) {
619 vmbus_channel_set_event(channel);
626 * @brief Send a range of single-page buffer packets using
627 * a GPADL Direct packet type
630 hv_vmbus_channel_send_packet_pagebuffer(
631 hv_vmbus_channel* channel,
632 hv_vmbus_page_buffer page_buffers[],
642 uint32_t page_buflen;
643 uint32_t packetLen_aligned;
644 hv_vmbus_sg_buffer_list buffer_list[4];
645 hv_vmbus_channel_packet_page_buffer desc;
647 uint64_t alignedData = 0;
649 if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
653 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
654 * is the largest size we support
656 descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
657 page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
658 packet_len = descSize + page_buflen + buffer_len;
659 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
661 /* Setup the descriptor */
662 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
663 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
664 /* in 8-bytes granularity */
665 desc.data_offset8 = (descSize + page_buflen) >> 3;
666 desc.length8 = (uint16_t) (packetLen_aligned >> 3);
667 desc.transaction_id = request_id;
668 desc.range_count = page_count;
670 buffer_list[0].data = &desc;
671 buffer_list[0].length = descSize;
673 buffer_list[1].data = page_buffers;
674 buffer_list[1].length = page_buflen;
676 buffer_list[2].data = buffer;
677 buffer_list[2].length = buffer_len;
679 buffer_list[3].data = &alignedData;
680 buffer_list[3].length = packetLen_aligned - packet_len;
682 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
685 /* TODO: We should determine if this is optional */
686 if (ret == 0 && need_sig) {
687 vmbus_channel_set_event(channel);
694 * @brief Send a multi-page buffer packet using a GPADL Direct packet type
697 hv_vmbus_channel_send_packet_multipagebuffer(
698 hv_vmbus_channel* channel,
699 hv_vmbus_multipage_buffer* multi_page_buffer,
709 uint32_t packet_len_aligned;
711 uint64_t aligned_data = 0;
712 hv_vmbus_sg_buffer_list buffer_list[3];
713 hv_vmbus_channel_packet_multipage_buffer desc;
716 HV_NUM_PAGES_SPANNED(
717 multi_page_buffer->offset,
718 multi_page_buffer->length);
720 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
723 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
724 * is the largest size we support
727 sizeof(hv_vmbus_channel_packet_multipage_buffer) -
728 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
730 packet_len = desc_size + buffer_len;
731 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
734 * Setup the descriptor
736 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
737 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
738 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
739 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
740 desc.transaction_id = request_id;
741 desc.range_count = 1;
743 desc.range.length = multi_page_buffer->length;
744 desc.range.offset = multi_page_buffer->offset;
746 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
747 pfn_count * sizeof(uint64_t));
749 buffer_list[0].data = &desc;
750 buffer_list[0].length = desc_size;
752 buffer_list[1].data = buffer;
753 buffer_list[1].length = buffer_len;
755 buffer_list[2].data = &aligned_data;
756 buffer_list[2].length = packet_len_aligned - packet_len;
758 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
761 /* TODO: We should determine if this is optional */
762 if (ret == 0 && need_sig) {
763 vmbus_channel_set_event(channel);
770 * @brief Retrieve the user packet on the specified channel
773 hv_vmbus_channel_recv_packet(
774 hv_vmbus_channel* channel,
777 uint32_t* buffer_actual_len,
778 uint64_t* request_id)
783 hv_vm_packet_descriptor desc;
785 *buffer_actual_len = 0;
788 ret = hv_ring_buffer_peek(&channel->inbound, &desc,
789 sizeof(hv_vm_packet_descriptor));
793 packet_len = desc.length8 << 3;
794 user_len = packet_len - (desc.data_offset8 << 3);
796 *buffer_actual_len = user_len;
798 if (user_len > buffer_len)
801 *request_id = desc.transaction_id;
803 /* Copy over the packet to the user buffer */
804 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
805 (desc.data_offset8 << 3));
811 * @brief Retrieve the raw packet on the specified channel
814 hv_vmbus_channel_recv_packet_raw(
815 hv_vmbus_channel* channel,
818 uint32_t* buffer_actual_len,
819 uint64_t* request_id)
823 hv_vm_packet_descriptor desc;
825 *buffer_actual_len = 0;
828 ret = hv_ring_buffer_peek(
829 &channel->inbound, &desc,
830 sizeof(hv_vm_packet_descriptor));
835 packetLen = desc.length8 << 3;
836 *buffer_actual_len = packetLen;
838 if (packetLen > buffer_len)
841 *request_id = desc.transaction_id;
843 /* Copy over the entire packet to the user buffer */
844 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
851 * Process a channel event notification
854 VmbusProcessChannelEvent(void* context, int pending)
857 uint32_t bytes_to_read;
858 hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
859 boolean_t is_batched_reading;
861 if (channel->on_channel_callback != NULL) {
862 arg = channel->channel_callback_context;
863 is_batched_reading = channel->batched_reading;
865 * Optimize host to guest signaling by ensuring:
866 * 1. While reading the channel, we disable interrupts from
868 * 2. Ensure that we process all posted messages from the host
869 * before returning from this callback.
870 * 3. Once we return, enable signaling from the host. Once this
871 * state is set we check to see if additional packets are
872 * available to read. In this case we repeat the process.
875 if (is_batched_reading)
876 hv_ring_buffer_read_begin(&channel->inbound);
878 channel->on_channel_callback(arg);
880 if (is_batched_reading)
882 hv_ring_buffer_read_end(&channel->inbound);
885 } while (is_batched_reading && (bytes_to_read != 0));
890 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags,
895 for (f = 0; f < flag_cnt; ++f) {
896 uint32_t rel_id_base;
900 if (event_flags[f] == 0)
903 flags = atomic_swap_long(&event_flags[f], 0);
904 rel_id_base = f << VMBUS_EVTFLAG_SHIFT;
906 while ((bit = ffsl(flags)) != 0) {
907 struct hv_vmbus_channel *channel;
910 --bit; /* NOTE: ffsl is 1-based */
911 flags &= ~(1UL << bit);
913 rel_id = rel_id_base + bit;
914 channel = sc->vmbus_chmap[rel_id];
916 /* if channel is closed or closing */
917 if (channel == NULL || channel->rxq == NULL)
920 if (channel->batched_reading)
921 hv_ring_buffer_read_begin(&channel->inbound);
922 taskqueue_enqueue(channel->rxq, &channel->channel_task);
928 vmbus_event_proc(struct vmbus_softc *sc, int cpu)
930 struct vmbus_evtflags *eventf;
933 * On Host with Win8 or above, the event page can be checked directly
934 * to get the id of the channel that has the pending interrupt.
936 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
937 vmbus_event_flags_proc(sc, eventf->evt_flags,
938 VMBUS_PCPU_GET(sc, event_flags_cnt, cpu));
942 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu)
944 struct vmbus_evtflags *eventf;
946 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE;
947 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) {
948 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags,
949 VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT);
954 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc,
955 const struct hv_vmbus_channel *chan)
957 volatile int *flag_cnt_ptr;
960 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1;
961 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->target_cpu);
966 old_flag_cnt = *flag_cnt_ptr;
967 if (old_flag_cnt >= flag_cnt)
969 if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) {
971 device_printf(sc->vmbus_dev,
972 "channel%u update cpu%d flag_cnt to %d\n",
974 chan->target_cpu, flag_cnt);