2 * Copyright (c) 2009-2012 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>
40 #include <machine/bus.h>
42 #include <vm/vm_param.h>
45 #include "hv_vmbus_priv.h"
47 static int vmbus_channel_create_gpadl_header(
48 /* must be phys and virt contiguous*/
50 /* page-size multiple */
52 hv_vmbus_channel_msg_info** msg_info,
53 uint32_t* message_count);
55 static void vmbus_channel_set_event(hv_vmbus_channel* channel);
56 static void VmbusProcessChannelEvent(void* channel, int pending);
59 * @brief Trigger an event notification on the specified channel
62 vmbus_channel_set_event(hv_vmbus_channel *channel)
64 hv_vmbus_monitor_page *monitor_page;
66 if (channel->offer_msg.monitor_allocated) {
67 /* Each uint32_t represents 32 channels */
68 synch_set_bit((channel->offer_msg.child_rel_id & 31),
69 ((uint32_t *)hv_vmbus_g_connection.send_interrupt_page
70 + ((channel->offer_msg.child_rel_id >> 5))));
72 monitor_page = (hv_vmbus_monitor_page *)
73 hv_vmbus_g_connection.monitor_page_2;
75 synch_set_bit(channel->monitor_bit,
76 (uint32_t *)&monitor_page->
77 trigger_group[channel->monitor_group].u.pending);
79 hv_vmbus_set_event(channel);
85 vmbus_channel_sysctl_monalloc(SYSCTL_HANDLER_ARGS)
87 struct hv_vmbus_channel *chan = arg1;
90 if (chan->offer_msg.monitor_allocated)
92 return sysctl_handle_int(oidp, &alloc, 0, req);
96 vmbus_channel_sysctl_create(hv_vmbus_channel* channel)
99 struct sysctl_oid *devch_sysctl;
100 struct sysctl_oid *devch_id_sysctl, *devch_sub_sysctl;
101 struct sysctl_oid *devch_id_in_sysctl, *devch_id_out_sysctl;
102 struct sysctl_ctx_list *ctx;
107 hv_vmbus_channel* primary_ch = channel->primary_channel;
109 if (primary_ch == NULL) {
110 dev = channel->device->device;
111 ch_id = channel->offer_msg.child_rel_id;
113 dev = primary_ch->device->device;
114 ch_id = primary_ch->offer_msg.child_rel_id;
115 sub_ch_id = channel->offer_msg.offer.sub_channel_index;
117 ctx = device_get_sysctl_ctx(dev);
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, 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, 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, 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, 0, "");
137 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl),
138 OID_AUTO, "chanid", CTLFLAG_RD,
139 &channel->offer_msg.child_rel_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, channel, 0,
145 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),
153 devch_id_out_sysctl = SYSCTL_ADD_NODE(ctx,
154 SYSCTL_CHILDREN(devch_id_sysctl),
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,
184 hv_vmbus_channel_open_channel* open_msg;
185 hv_vmbus_channel_msg_info* open_info;
187 mtx_lock(&new_channel->sc_lock);
188 if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
189 new_channel->state = HV_CHANNEL_OPENING_STATE;
191 mtx_unlock(&new_channel->sc_lock);
193 printf("VMBUS: Trying to open channel <%p> which in "
194 "%d state.\n", new_channel, new_channel->state);
197 mtx_unlock(&new_channel->sc_lock);
199 new_channel->on_channel_callback = pfn_on_channel_callback;
200 new_channel->channel_callback_context = context;
202 new_channel->rxq = hv_vmbus_g_context.hv_event_queue[new_channel->target_cpu];
203 TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
205 /* Allocate the ring buffer */
206 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
207 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
209 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
213 in = ((uint8_t *) out + send_ring_buffer_size);
215 new_channel->ring_buffer_pages = out;
216 new_channel->ring_buffer_page_count = (send_ring_buffer_size +
217 recv_ring_buffer_size) >> PAGE_SHIFT;
218 new_channel->ring_buffer_size = send_ring_buffer_size +
219 recv_ring_buffer_size;
221 hv_vmbus_ring_buffer_init(
222 &new_channel->outbound,
224 send_ring_buffer_size);
226 hv_vmbus_ring_buffer_init(
227 &new_channel->inbound,
229 recv_ring_buffer_size);
231 /* Create sysctl tree for this channel */
232 vmbus_channel_sysctl_create(new_channel);
235 * Establish the gpadl for the ring buffer
237 new_channel->ring_buffer_gpadl_handle = 0;
239 ret = hv_vmbus_channel_establish_gpadl(new_channel,
240 new_channel->outbound.ring_buffer,
241 send_ring_buffer_size + recv_ring_buffer_size,
242 &new_channel->ring_buffer_gpadl_handle);
245 * Create and init the channel open message
247 open_info = (hv_vmbus_channel_msg_info*) malloc(
248 sizeof(hv_vmbus_channel_msg_info) +
249 sizeof(hv_vmbus_channel_open_channel),
252 KASSERT(open_info != NULL,
253 ("Error VMBUS: malloc failed to allocate Open Channel message!"));
255 if (open_info == NULL)
258 sema_init(&open_info->wait_sema, 0, "Open Info Sema");
260 open_msg = (hv_vmbus_channel_open_channel*) open_info->msg;
261 open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL;
262 open_msg->open_id = new_channel->offer_msg.child_rel_id;
263 open_msg->child_rel_id = new_channel->offer_msg.child_rel_id;
264 open_msg->ring_buffer_gpadl_handle =
265 new_channel->ring_buffer_gpadl_handle;
266 open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size
268 open_msg->target_vcpu = new_channel->target_vcpu;
271 memcpy(open_msg->user_data, user_data, user_data_len);
273 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
275 &hv_vmbus_g_connection.channel_msg_anchor,
278 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
280 ret = hv_vmbus_post_message(
281 open_msg, sizeof(hv_vmbus_channel_open_channel));
286 ret = sema_timedwait(&open_info->wait_sema, 5 * hz); /* KYS 5 seconds */
290 printf("VMBUS: channel <%p> open timeout.\n", new_channel);
294 if (open_info->response.open_result.status == 0) {
295 new_channel->state = HV_CHANNEL_OPENED_STATE;
297 printf("VMBUS: channel <%p> open success.\n", new_channel);
300 printf("Error VMBUS: channel <%p> open failed - %d!\n",
301 new_channel, open_info->response.open_result.status);
305 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
307 &hv_vmbus_g_connection.channel_msg_anchor,
310 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
311 sema_destroy(&open_info->wait_sema);
312 free(open_info, M_DEVBUF);
318 * @brief Create a gpadl for the specified buffer
321 vmbus_channel_create_gpadl_header(
323 uint32_t size, /* page-size multiple */
324 hv_vmbus_channel_msg_info** msg_info,
325 uint32_t* message_count)
329 unsigned long long pfn;
331 hv_vmbus_channel_gpadl_header* gpa_header;
332 hv_vmbus_channel_gpadl_body* gpadl_body;
333 hv_vmbus_channel_msg_info* msg_header;
334 hv_vmbus_channel_msg_info* msg_body;
336 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
338 page_count = size >> PAGE_SHIFT;
339 pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT;
341 /*do we need a gpadl body msg */
342 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
343 - sizeof(hv_vmbus_channel_gpadl_header)
344 - sizeof(hv_gpa_range);
345 pfnCount = pfnSize / sizeof(uint64_t);
347 if (page_count > pfnCount) { /* if(we need a gpadl body) */
348 /* fill in the header */
349 msg_size = sizeof(hv_vmbus_channel_msg_info)
350 + sizeof(hv_vmbus_channel_gpadl_header)
351 + sizeof(hv_gpa_range)
352 + pfnCount * sizeof(uint64_t);
353 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
356 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
357 if (msg_header == NULL)
360 TAILQ_INIT(&msg_header->sub_msg_list_anchor);
361 msg_header->message_size = msg_size;
363 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
364 gpa_header->range_count = 1;
365 gpa_header->range_buf_len = sizeof(hv_gpa_range)
366 + page_count * sizeof(uint64_t);
367 gpa_header->range[0].byte_offset = 0;
368 gpa_header->range[0].byte_count = size;
369 for (i = 0; i < pfnCount; i++) {
370 gpa_header->range[0].pfn_array[i] = pfn + i;
372 *msg_info = msg_header;
376 pfnLeft = page_count - pfnCount;
379 * figure out how many pfns we can fit
381 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
382 - sizeof(hv_vmbus_channel_gpadl_body);
383 pfnCount = pfnSize / sizeof(uint64_t);
389 if (pfnLeft > pfnCount) {
395 msg_size = sizeof(hv_vmbus_channel_msg_info) +
396 sizeof(hv_vmbus_channel_gpadl_body) +
397 pfnCurr * sizeof(uint64_t);
398 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
401 ("Error VMBUS: malloc failed to allocate Gpadl msg_body!"));
402 if (msg_body == NULL)
405 msg_body->message_size = msg_size;
408 (hv_vmbus_channel_gpadl_body*) msg_body->msg;
410 * gpadl_body->gpadl = kbuffer;
412 for (i = 0; i < pfnCurr; i++) {
413 gpadl_body->pfn[i] = pfn + pfnSum + i;
417 &msg_header->sub_msg_list_anchor,
423 } else { /* else everything fits in a header */
425 msg_size = sizeof(hv_vmbus_channel_msg_info) +
426 sizeof(hv_vmbus_channel_gpadl_header) +
427 sizeof(hv_gpa_range) +
428 page_count * sizeof(uint64_t);
429 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
432 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
433 if (msg_header == NULL)
436 msg_header->message_size = msg_size;
438 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
439 gpa_header->range_count = 1;
440 gpa_header->range_buf_len = sizeof(hv_gpa_range) +
441 page_count * sizeof(uint64_t);
442 gpa_header->range[0].byte_offset = 0;
443 gpa_header->range[0].byte_count = size;
444 for (i = 0; i < page_count; i++) {
445 gpa_header->range[0].pfn_array[i] = pfn + i;
448 *msg_info = msg_header;
456 * @brief Establish a GPADL for the specified buffer
459 hv_vmbus_channel_establish_gpadl(
460 hv_vmbus_channel* channel,
462 uint32_t size, /* page-size multiple */
463 uint32_t* gpadl_handle)
467 hv_vmbus_channel_gpadl_header* gpadl_msg;
468 hv_vmbus_channel_gpadl_body* gpadl_body;
469 hv_vmbus_channel_msg_info* msg_info;
470 hv_vmbus_channel_msg_info* sub_msg_info;
472 hv_vmbus_channel_msg_info* curr;
473 uint32_t next_gpadl_handle;
475 next_gpadl_handle = atomic_fetchadd_int(
476 &hv_vmbus_g_connection.next_gpadl_handle, 1);
478 ret = vmbus_channel_create_gpadl_header(
479 contig_buffer, size, &msg_info, &msg_count);
484 * We can _not_ even revert the above incremental,
485 * if multiple GPADL establishments are running
486 * parallelly, decrement the global next_gpadl_handle
487 * is calling for _big_ trouble. A better solution
488 * is to have a 0-based GPADL id bitmap ...
493 sema_init(&msg_info->wait_sema, 0, "Open Info Sema");
494 gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg;
495 gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER;
496 gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id;
497 gpadl_msg->gpadl = next_gpadl_handle;
499 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
501 &hv_vmbus_g_connection.channel_msg_anchor,
505 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
507 ret = hv_vmbus_post_message(
509 msg_info->message_size -
510 (uint32_t) sizeof(hv_vmbus_channel_msg_info));
517 &msg_info->sub_msg_list_anchor, msg_list_entry) {
520 (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg;
522 gpadl_body->header.message_type =
523 HV_CHANNEL_MESSAGE_GPADL_BODY;
524 gpadl_body->gpadl = next_gpadl_handle;
526 ret = hv_vmbus_post_message(
528 sub_msg_info->message_size
529 - (uint32_t) sizeof(hv_vmbus_channel_msg_info));
530 /* if (the post message failed) give up and clean up */
536 ret = sema_timedwait(&msg_info->wait_sema, 5 * hz); /* KYS 5 seconds*/
540 *gpadl_handle = gpadl_msg->gpadl;
544 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
545 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
546 msg_info, msg_list_entry);
547 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
549 sema_destroy(&msg_info->wait_sema);
550 free(msg_info, M_DEVBUF);
556 * @brief Teardown the specified GPADL handle
559 hv_vmbus_channel_teardown_gpdal(
560 hv_vmbus_channel* channel,
561 uint32_t gpadl_handle)
564 hv_vmbus_channel_gpadl_teardown* msg;
565 hv_vmbus_channel_msg_info* info;
567 info = (hv_vmbus_channel_msg_info *)
568 malloc( sizeof(hv_vmbus_channel_msg_info) +
569 sizeof(hv_vmbus_channel_gpadl_teardown),
571 KASSERT(info != NULL,
572 ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!"));
578 sema_init(&info->wait_sema, 0, "Open Info Sema");
580 msg = (hv_vmbus_channel_gpadl_teardown*) info->msg;
582 msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN;
583 msg->child_rel_id = channel->offer_msg.child_rel_id;
584 msg->gpadl = gpadl_handle;
586 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
587 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor,
588 info, msg_list_entry);
589 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
591 ret = hv_vmbus_post_message(msg,
592 sizeof(hv_vmbus_channel_gpadl_teardown));
596 ret = sema_timedwait(&info->wait_sema, 5 * hz); /* KYS 5 seconds */
600 * Received a torndown response
602 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
603 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
604 info, msg_list_entry);
605 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
606 sema_destroy(&info->wait_sema);
607 free(info, M_DEVBUF);
613 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
616 struct taskqueue *rxq = channel->rxq;
617 hv_vmbus_channel_close_channel* msg;
618 hv_vmbus_channel_msg_info* info;
620 channel->state = HV_CHANNEL_OPEN_STATE;
621 channel->sc_creation_callback = NULL;
624 * set rxq to NULL to avoid more requests be scheduled
627 taskqueue_drain(rxq, &channel->channel_task);
628 channel->on_channel_callback = NULL;
631 * Send a closing message
633 info = (hv_vmbus_channel_msg_info *)
634 malloc( sizeof(hv_vmbus_channel_msg_info) +
635 sizeof(hv_vmbus_channel_close_channel),
637 KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!"));
641 msg = (hv_vmbus_channel_close_channel*) info->msg;
642 msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL;
643 msg->child_rel_id = channel->offer_msg.child_rel_id;
645 ret = hv_vmbus_post_message(
646 msg, sizeof(hv_vmbus_channel_close_channel));
648 /* Tear down the gpadl for the channel's ring buffer */
649 if (channel->ring_buffer_gpadl_handle) {
650 hv_vmbus_channel_teardown_gpdal(channel,
651 channel->ring_buffer_gpadl_handle);
654 /* TODO: Send a msg to release the childRelId */
656 /* cleanup the ring buffers for this channel */
657 hv_ring_buffer_cleanup(&channel->outbound);
658 hv_ring_buffer_cleanup(&channel->inbound);
660 contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
663 free(info, M_DEVBUF);
667 * @brief Close the specified channel
670 hv_vmbus_channel_close(hv_vmbus_channel *channel)
672 hv_vmbus_channel* sub_channel;
674 if (channel->primary_channel != NULL) {
676 * We only close multi-channels when the primary is
683 * Close all multi-channels first.
685 TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
687 if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
689 hv_vmbus_channel_close_internal(sub_channel);
692 * Then close the primary channel.
694 hv_vmbus_channel_close_internal(channel);
698 * @brief Send the specified buffer on the given channel
701 hv_vmbus_channel_send_packet(
702 hv_vmbus_channel* channel,
706 hv_vmbus_packet_type type,
710 hv_vm_packet_descriptor desc;
712 uint64_t aligned_data;
713 uint32_t packet_len_aligned;
715 hv_vmbus_sg_buffer_list buffer_list[3];
717 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
718 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
721 /* Setup the descriptor */
722 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */
723 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
724 /* in 8-bytes granularity */
725 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
726 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
727 desc.transaction_id = request_id;
729 buffer_list[0].data = &desc;
730 buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
732 buffer_list[1].data = buffer;
733 buffer_list[1].length = buffer_len;
735 buffer_list[2].data = &aligned_data;
736 buffer_list[2].length = packet_len_aligned - packet_len;
738 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
741 /* TODO: We should determine if this is optional */
742 if (ret == 0 && need_sig) {
743 vmbus_channel_set_event(channel);
750 * @brief Send a range of single-page buffer packets using
751 * a GPADL Direct packet type
754 hv_vmbus_channel_send_packet_pagebuffer(
755 hv_vmbus_channel* channel,
756 hv_vmbus_page_buffer page_buffers[],
766 uint32_t page_buflen;
767 uint32_t packetLen_aligned;
768 hv_vmbus_sg_buffer_list buffer_list[4];
769 hv_vmbus_channel_packet_page_buffer desc;
771 uint64_t alignedData = 0;
773 if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
777 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
778 * is the largest size we support
780 descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
781 page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
782 packet_len = descSize + page_buflen + buffer_len;
783 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
785 /* Setup the descriptor */
786 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
787 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
788 /* in 8-bytes granularity */
789 desc.data_offset8 = (descSize + page_buflen) >> 3;
790 desc.length8 = (uint16_t) (packetLen_aligned >> 3);
791 desc.transaction_id = request_id;
792 desc.range_count = page_count;
794 buffer_list[0].data = &desc;
795 buffer_list[0].length = descSize;
797 buffer_list[1].data = page_buffers;
798 buffer_list[1].length = page_buflen;
800 buffer_list[2].data = buffer;
801 buffer_list[2].length = buffer_len;
803 buffer_list[3].data = &alignedData;
804 buffer_list[3].length = packetLen_aligned - packet_len;
806 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
809 /* TODO: We should determine if this is optional */
810 if (ret == 0 && need_sig) {
811 vmbus_channel_set_event(channel);
818 * @brief Send a multi-page buffer packet using a GPADL Direct packet type
821 hv_vmbus_channel_send_packet_multipagebuffer(
822 hv_vmbus_channel* channel,
823 hv_vmbus_multipage_buffer* multi_page_buffer,
833 uint32_t packet_len_aligned;
835 uint64_t aligned_data = 0;
836 hv_vmbus_sg_buffer_list buffer_list[3];
837 hv_vmbus_channel_packet_multipage_buffer desc;
840 HV_NUM_PAGES_SPANNED(
841 multi_page_buffer->offset,
842 multi_page_buffer->length);
844 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
847 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
848 * is the largest size we support
851 sizeof(hv_vmbus_channel_packet_multipage_buffer) -
852 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
854 packet_len = desc_size + buffer_len;
855 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
858 * Setup the descriptor
860 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
861 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
862 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
863 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
864 desc.transaction_id = request_id;
865 desc.range_count = 1;
867 desc.range.length = multi_page_buffer->length;
868 desc.range.offset = multi_page_buffer->offset;
870 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
871 pfn_count * sizeof(uint64_t));
873 buffer_list[0].data = &desc;
874 buffer_list[0].length = desc_size;
876 buffer_list[1].data = buffer;
877 buffer_list[1].length = buffer_len;
879 buffer_list[2].data = &aligned_data;
880 buffer_list[2].length = packet_len_aligned - packet_len;
882 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
885 /* TODO: We should determine if this is optional */
886 if (ret == 0 && need_sig) {
887 vmbus_channel_set_event(channel);
894 * @brief Retrieve the user packet on the specified channel
897 hv_vmbus_channel_recv_packet(
898 hv_vmbus_channel* channel,
901 uint32_t* buffer_actual_len,
902 uint64_t* request_id)
907 hv_vm_packet_descriptor desc;
909 *buffer_actual_len = 0;
912 ret = hv_ring_buffer_peek(&channel->inbound, &desc,
913 sizeof(hv_vm_packet_descriptor));
917 packet_len = desc.length8 << 3;
918 user_len = packet_len - (desc.data_offset8 << 3);
920 *buffer_actual_len = user_len;
922 if (user_len > buffer_len)
925 *request_id = desc.transaction_id;
927 /* Copy over the packet to the user buffer */
928 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
929 (desc.data_offset8 << 3));
935 * @brief Retrieve the raw packet on the specified channel
938 hv_vmbus_channel_recv_packet_raw(
939 hv_vmbus_channel* channel,
942 uint32_t* buffer_actual_len,
943 uint64_t* request_id)
947 hv_vm_packet_descriptor desc;
949 *buffer_actual_len = 0;
952 ret = hv_ring_buffer_peek(
953 &channel->inbound, &desc,
954 sizeof(hv_vm_packet_descriptor));
959 packetLen = desc.length8 << 3;
960 *buffer_actual_len = packetLen;
962 if (packetLen > buffer_len)
965 *request_id = desc.transaction_id;
967 /* Copy over the entire packet to the user buffer */
968 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
975 * Process a channel event notification
978 VmbusProcessChannelEvent(void* context, int pending)
981 uint32_t bytes_to_read;
982 hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
983 boolean_t is_batched_reading;
986 * Find the channel based on this relid and invokes
987 * the channel callback to process the event
990 if (channel == NULL) {
994 * To deal with the race condition where we might
995 * receive a packet while the relevant driver is
996 * being unloaded, dispatch the callback while
997 * holding the channel lock. The unloading driver
998 * will acquire the same channel lock to set the
999 * callback to NULL. This closes the window.
1002 if (channel->on_channel_callback != NULL) {
1003 arg = channel->channel_callback_context;
1004 is_batched_reading = channel->batched_reading;
1006 * Optimize host to guest signaling by ensuring:
1007 * 1. While reading the channel, we disable interrupts from
1009 * 2. Ensure that we process all posted messages from the host
1010 * before returning from this callback.
1011 * 3. Once we return, enable signaling from the host. Once this
1012 * state is set we check to see if additional packets are
1013 * available to read. In this case we repeat the process.
1016 if (is_batched_reading)
1017 hv_ring_buffer_read_begin(&channel->inbound);
1019 channel->on_channel_callback(arg);
1021 if (is_batched_reading)
1023 hv_ring_buffer_read_end(&channel->inbound);
1026 } while (is_batched_reading && (bytes_to_read != 0));