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 <machine/bus.h>
41 #include <vm/vm_param.h>
44 #include "hv_vmbus_priv.h"
46 static int vmbus_channel_create_gpadl_header(
47 /* must be phys and virt contiguous*/
49 /* page-size multiple */
51 hv_vmbus_channel_msg_info** msg_info,
52 uint32_t* message_count);
54 static void vmbus_channel_set_event(hv_vmbus_channel* channel);
55 static void VmbusProcessChannelEvent(void* channel, int pending);
58 * @brief Trigger an event notification on the specified channel
61 vmbus_channel_set_event(hv_vmbus_channel *channel)
63 hv_vmbus_monitor_page *monitor_page;
65 if (channel->offer_msg.monitor_allocated) {
66 /* Each uint32_t represents 32 channels */
67 synch_set_bit((channel->offer_msg.child_rel_id & 31),
68 ((uint32_t *)hv_vmbus_g_connection.send_interrupt_page
69 + ((channel->offer_msg.child_rel_id >> 5))));
71 monitor_page = (hv_vmbus_monitor_page *)
72 hv_vmbus_g_connection.monitor_pages;
74 monitor_page++; /* Get the child to parent monitor page */
76 synch_set_bit(channel->monitor_bit,
77 (uint32_t *)&monitor_page->
78 trigger_group[channel->monitor_group].u.pending);
80 hv_vmbus_set_event(channel);
86 * @brief Open the specified channel
89 hv_vmbus_channel_open(
90 hv_vmbus_channel* new_channel,
91 uint32_t send_ring_buffer_size,
92 uint32_t recv_ring_buffer_size,
94 uint32_t user_data_len,
95 hv_vmbus_pfn_channel_callback pfn_on_channel_callback,
101 hv_vmbus_channel_open_channel* open_msg;
102 hv_vmbus_channel_msg_info* open_info;
104 mtx_lock(&new_channel->sc_lock);
105 if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
106 new_channel->state = HV_CHANNEL_OPENING_STATE;
108 mtx_unlock(&new_channel->sc_lock);
110 printf("VMBUS: Trying to open channel <%p> which in "
111 "%d state.\n", new_channel, new_channel->state);
114 mtx_unlock(&new_channel->sc_lock);
116 new_channel->on_channel_callback = pfn_on_channel_callback;
117 new_channel->channel_callback_context = context;
119 new_channel->rxq = hv_vmbus_g_context.hv_event_queue[new_channel->target_cpu];
120 TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
122 /* Allocate the ring buffer */
123 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
124 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
126 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
130 in = ((uint8_t *) out + send_ring_buffer_size);
132 new_channel->ring_buffer_pages = out;
133 new_channel->ring_buffer_page_count = (send_ring_buffer_size +
134 recv_ring_buffer_size) >> PAGE_SHIFT;
135 new_channel->ring_buffer_size = send_ring_buffer_size +
136 recv_ring_buffer_size;
138 hv_vmbus_ring_buffer_init(
139 &new_channel->outbound,
141 send_ring_buffer_size);
143 hv_vmbus_ring_buffer_init(
144 &new_channel->inbound,
146 recv_ring_buffer_size);
149 * Establish the gpadl for the ring buffer
151 new_channel->ring_buffer_gpadl_handle = 0;
153 ret = hv_vmbus_channel_establish_gpadl(new_channel,
154 new_channel->outbound.ring_buffer,
155 send_ring_buffer_size + recv_ring_buffer_size,
156 &new_channel->ring_buffer_gpadl_handle);
159 * Create and init the channel open message
161 open_info = (hv_vmbus_channel_msg_info*) malloc(
162 sizeof(hv_vmbus_channel_msg_info) +
163 sizeof(hv_vmbus_channel_open_channel),
166 KASSERT(open_info != NULL,
167 ("Error VMBUS: malloc failed to allocate Open Channel message!"));
169 if (open_info == NULL)
172 sema_init(&open_info->wait_sema, 0, "Open Info Sema");
174 open_msg = (hv_vmbus_channel_open_channel*) open_info->msg;
175 open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL;
176 open_msg->open_id = new_channel->offer_msg.child_rel_id;
177 open_msg->child_rel_id = new_channel->offer_msg.child_rel_id;
178 open_msg->ring_buffer_gpadl_handle =
179 new_channel->ring_buffer_gpadl_handle;
180 open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size
182 open_msg->target_vcpu = new_channel->target_vcpu;
185 memcpy(open_msg->user_data, user_data, user_data_len);
187 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
189 &hv_vmbus_g_connection.channel_msg_anchor,
192 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
194 ret = hv_vmbus_post_message(
195 open_msg, sizeof(hv_vmbus_channel_open_channel));
200 ret = sema_timedwait(&open_info->wait_sema, 5 * hz); /* KYS 5 seconds */
204 printf("VMBUS: channel <%p> open timeout.\n", new_channel);
208 if (open_info->response.open_result.status == 0) {
209 new_channel->state = HV_CHANNEL_OPENED_STATE;
211 printf("VMBUS: channel <%p> open success.\n", new_channel);
214 printf("Error VMBUS: channel <%p> open failed - %d!\n",
215 new_channel, open_info->response.open_result.status);
219 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
221 &hv_vmbus_g_connection.channel_msg_anchor,
224 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
225 sema_destroy(&open_info->wait_sema);
226 free(open_info, M_DEVBUF);
232 * @brief Create a gpadl for the specified buffer
235 vmbus_channel_create_gpadl_header(
237 uint32_t size, /* page-size multiple */
238 hv_vmbus_channel_msg_info** msg_info,
239 uint32_t* message_count)
243 unsigned long long pfn;
245 hv_vmbus_channel_gpadl_header* gpa_header;
246 hv_vmbus_channel_gpadl_body* gpadl_body;
247 hv_vmbus_channel_msg_info* msg_header;
248 hv_vmbus_channel_msg_info* msg_body;
250 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
252 page_count = size >> PAGE_SHIFT;
253 pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT;
255 /*do we need a gpadl body msg */
256 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
257 - sizeof(hv_vmbus_channel_gpadl_header)
258 - sizeof(hv_gpa_range);
259 pfnCount = pfnSize / sizeof(uint64_t);
261 if (page_count > pfnCount) { /* if(we need a gpadl body) */
262 /* fill in the header */
263 msg_size = sizeof(hv_vmbus_channel_msg_info)
264 + sizeof(hv_vmbus_channel_gpadl_header)
265 + sizeof(hv_gpa_range)
266 + pfnCount * sizeof(uint64_t);
267 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
270 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
271 if (msg_header == NULL)
274 TAILQ_INIT(&msg_header->sub_msg_list_anchor);
275 msg_header->message_size = msg_size;
277 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
278 gpa_header->range_count = 1;
279 gpa_header->range_buf_len = sizeof(hv_gpa_range)
280 + page_count * sizeof(uint64_t);
281 gpa_header->range[0].byte_offset = 0;
282 gpa_header->range[0].byte_count = size;
283 for (i = 0; i < pfnCount; i++) {
284 gpa_header->range[0].pfn_array[i] = pfn + i;
286 *msg_info = msg_header;
290 pfnLeft = page_count - pfnCount;
293 * figure out how many pfns we can fit
295 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
296 - sizeof(hv_vmbus_channel_gpadl_body);
297 pfnCount = pfnSize / sizeof(uint64_t);
303 if (pfnLeft > pfnCount) {
309 msg_size = sizeof(hv_vmbus_channel_msg_info) +
310 sizeof(hv_vmbus_channel_gpadl_body) +
311 pfnCurr * sizeof(uint64_t);
312 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
315 ("Error VMBUS: malloc failed to allocate Gpadl msg_body!"));
316 if (msg_body == NULL)
319 msg_body->message_size = msg_size;
322 (hv_vmbus_channel_gpadl_body*) msg_body->msg;
324 * gpadl_body->gpadl = kbuffer;
326 for (i = 0; i < pfnCurr; i++) {
327 gpadl_body->pfn[i] = pfn + pfnSum + i;
331 &msg_header->sub_msg_list_anchor,
337 } else { /* else everything fits in a header */
339 msg_size = sizeof(hv_vmbus_channel_msg_info) +
340 sizeof(hv_vmbus_channel_gpadl_header) +
341 sizeof(hv_gpa_range) +
342 page_count * sizeof(uint64_t);
343 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
346 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
347 if (msg_header == NULL)
350 msg_header->message_size = msg_size;
352 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
353 gpa_header->range_count = 1;
354 gpa_header->range_buf_len = sizeof(hv_gpa_range) +
355 page_count * sizeof(uint64_t);
356 gpa_header->range[0].byte_offset = 0;
357 gpa_header->range[0].byte_count = size;
358 for (i = 0; i < page_count; i++) {
359 gpa_header->range[0].pfn_array[i] = pfn + i;
362 *msg_info = msg_header;
370 * @brief Establish a GPADL for the specified buffer
373 hv_vmbus_channel_establish_gpadl(
374 hv_vmbus_channel* channel,
376 uint32_t size, /* page-size multiple */
377 uint32_t* gpadl_handle)
381 hv_vmbus_channel_gpadl_header* gpadl_msg;
382 hv_vmbus_channel_gpadl_body* gpadl_body;
383 hv_vmbus_channel_msg_info* msg_info;
384 hv_vmbus_channel_msg_info* sub_msg_info;
386 hv_vmbus_channel_msg_info* curr;
387 uint32_t next_gpadl_handle;
389 next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle;
390 atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1);
392 ret = vmbus_channel_create_gpadl_header(
393 contig_buffer, size, &msg_info, &msg_count);
395 if(ret != 0) { /* if(allocation failed) return immediately */
396 /* reverse atomic_add_int above */
397 atomic_subtract_int((int*)
398 &hv_vmbus_g_connection.next_gpadl_handle, 1);
402 sema_init(&msg_info->wait_sema, 0, "Open Info Sema");
403 gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg;
404 gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER;
405 gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id;
406 gpadl_msg->gpadl = next_gpadl_handle;
408 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
410 &hv_vmbus_g_connection.channel_msg_anchor,
414 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
416 ret = hv_vmbus_post_message(
418 msg_info->message_size -
419 (uint32_t) sizeof(hv_vmbus_channel_msg_info));
426 &msg_info->sub_msg_list_anchor, msg_list_entry) {
429 (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg;
431 gpadl_body->header.message_type =
432 HV_CHANNEL_MESSAGE_GPADL_BODY;
433 gpadl_body->gpadl = next_gpadl_handle;
435 ret = hv_vmbus_post_message(
437 sub_msg_info->message_size
438 - (uint32_t) sizeof(hv_vmbus_channel_msg_info));
439 /* if (the post message failed) give up and clean up */
445 ret = sema_timedwait(&msg_info->wait_sema, 5 * hz); /* KYS 5 seconds*/
449 *gpadl_handle = gpadl_msg->gpadl;
453 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
454 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
455 msg_info, msg_list_entry);
456 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
458 sema_destroy(&msg_info->wait_sema);
459 free(msg_info, M_DEVBUF);
465 * @brief Teardown the specified GPADL handle
468 hv_vmbus_channel_teardown_gpdal(
469 hv_vmbus_channel* channel,
470 uint32_t gpadl_handle)
473 hv_vmbus_channel_gpadl_teardown* msg;
474 hv_vmbus_channel_msg_info* info;
476 info = (hv_vmbus_channel_msg_info *)
477 malloc( sizeof(hv_vmbus_channel_msg_info) +
478 sizeof(hv_vmbus_channel_gpadl_teardown),
480 KASSERT(info != NULL,
481 ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!"));
487 sema_init(&info->wait_sema, 0, "Open Info Sema");
489 msg = (hv_vmbus_channel_gpadl_teardown*) info->msg;
491 msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN;
492 msg->child_rel_id = channel->offer_msg.child_rel_id;
493 msg->gpadl = gpadl_handle;
495 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
496 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor,
497 info, msg_list_entry);
498 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
500 ret = hv_vmbus_post_message(msg,
501 sizeof(hv_vmbus_channel_gpadl_teardown));
505 ret = sema_timedwait(&info->wait_sema, 5 * hz); /* KYS 5 seconds */
509 * Received a torndown response
511 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
512 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
513 info, msg_list_entry);
514 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
515 sema_destroy(&info->wait_sema);
516 free(info, M_DEVBUF);
522 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
525 struct taskqueue *rxq = channel->rxq;
526 hv_vmbus_channel_close_channel* msg;
527 hv_vmbus_channel_msg_info* info;
529 channel->state = HV_CHANNEL_OPEN_STATE;
530 channel->sc_creation_callback = NULL;
533 * set rxq to NULL to avoid more requests be scheduled
536 taskqueue_drain(rxq, &channel->channel_task);
538 * Grab the lock to prevent race condition when a packet received
539 * and unloading driver is in the process.
541 mtx_lock(&channel->inbound_lock);
542 channel->on_channel_callback = NULL;
543 mtx_unlock(&channel->inbound_lock);
546 * Send a closing message
548 info = (hv_vmbus_channel_msg_info *)
549 malloc( sizeof(hv_vmbus_channel_msg_info) +
550 sizeof(hv_vmbus_channel_close_channel),
552 KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!"));
556 msg = (hv_vmbus_channel_close_channel*) info->msg;
557 msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL;
558 msg->child_rel_id = channel->offer_msg.child_rel_id;
560 ret = hv_vmbus_post_message(
561 msg, sizeof(hv_vmbus_channel_close_channel));
563 /* Tear down the gpadl for the channel's ring buffer */
564 if (channel->ring_buffer_gpadl_handle) {
565 hv_vmbus_channel_teardown_gpdal(channel,
566 channel->ring_buffer_gpadl_handle);
569 /* TODO: Send a msg to release the childRelId */
571 /* cleanup the ring buffers for this channel */
572 hv_ring_buffer_cleanup(&channel->outbound);
573 hv_ring_buffer_cleanup(&channel->inbound);
575 contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
578 free(info, M_DEVBUF);
582 * @brief Close the specified channel
585 hv_vmbus_channel_close(hv_vmbus_channel *channel)
587 hv_vmbus_channel* sub_channel;
589 if (channel->primary_channel != NULL) {
591 * We only close multi-channels when the primary is
598 * Close all multi-channels first.
600 TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
602 if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
604 hv_vmbus_channel_close_internal(sub_channel);
607 * Then close the primary channel.
609 hv_vmbus_channel_close_internal(channel);
613 * @brief Send the specified buffer on the given channel
616 hv_vmbus_channel_send_packet(
617 hv_vmbus_channel* channel,
621 hv_vmbus_packet_type type,
625 hv_vm_packet_descriptor desc;
627 uint64_t aligned_data;
628 uint32_t packet_len_aligned;
630 hv_vmbus_sg_buffer_list buffer_list[3];
632 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
633 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
636 /* Setup the descriptor */
637 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */
638 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
639 /* in 8-bytes granularity */
640 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
641 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
642 desc.transaction_id = request_id;
644 buffer_list[0].data = &desc;
645 buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
647 buffer_list[1].data = buffer;
648 buffer_list[1].length = buffer_len;
650 buffer_list[2].data = &aligned_data;
651 buffer_list[2].length = packet_len_aligned - packet_len;
653 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
656 /* TODO: We should determine if this is optional */
657 if (ret == 0 && need_sig) {
658 vmbus_channel_set_event(channel);
665 * @brief Send a range of single-page buffer packets using
666 * a GPADL Direct packet type
669 hv_vmbus_channel_send_packet_pagebuffer(
670 hv_vmbus_channel* channel,
671 hv_vmbus_page_buffer page_buffers[],
681 uint32_t page_buflen;
682 uint32_t packetLen_aligned;
683 hv_vmbus_sg_buffer_list buffer_list[4];
684 hv_vmbus_channel_packet_page_buffer desc;
686 uint64_t alignedData = 0;
688 if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
692 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
693 * is the largest size we support
695 descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
696 page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
697 packet_len = descSize + page_buflen + buffer_len;
698 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
700 /* Setup the descriptor */
701 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
702 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
703 /* in 8-bytes granularity */
704 desc.data_offset8 = (descSize + page_buflen) >> 3;
705 desc.length8 = (uint16_t) (packetLen_aligned >> 3);
706 desc.transaction_id = request_id;
707 desc.range_count = page_count;
709 buffer_list[0].data = &desc;
710 buffer_list[0].length = descSize;
712 buffer_list[1].data = page_buffers;
713 buffer_list[1].length = page_buflen;
715 buffer_list[2].data = buffer;
716 buffer_list[2].length = buffer_len;
718 buffer_list[3].data = &alignedData;
719 buffer_list[3].length = packetLen_aligned - packet_len;
721 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
724 /* TODO: We should determine if this is optional */
725 if (ret == 0 && need_sig) {
726 vmbus_channel_set_event(channel);
733 * @brief Send a multi-page buffer packet using a GPADL Direct packet type
736 hv_vmbus_channel_send_packet_multipagebuffer(
737 hv_vmbus_channel* channel,
738 hv_vmbus_multipage_buffer* multi_page_buffer,
748 uint32_t packet_len_aligned;
750 uint64_t aligned_data = 0;
751 hv_vmbus_sg_buffer_list buffer_list[3];
752 hv_vmbus_channel_packet_multipage_buffer desc;
755 HV_NUM_PAGES_SPANNED(
756 multi_page_buffer->offset,
757 multi_page_buffer->length);
759 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
762 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
763 * is the largest size we support
766 sizeof(hv_vmbus_channel_packet_multipage_buffer) -
767 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
769 packet_len = desc_size + buffer_len;
770 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
773 * Setup the descriptor
775 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
776 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
777 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
778 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
779 desc.transaction_id = request_id;
780 desc.range_count = 1;
782 desc.range.length = multi_page_buffer->length;
783 desc.range.offset = multi_page_buffer->offset;
785 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
786 pfn_count * sizeof(uint64_t));
788 buffer_list[0].data = &desc;
789 buffer_list[0].length = desc_size;
791 buffer_list[1].data = buffer;
792 buffer_list[1].length = buffer_len;
794 buffer_list[2].data = &aligned_data;
795 buffer_list[2].length = packet_len_aligned - packet_len;
797 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
800 /* TODO: We should determine if this is optional */
801 if (ret == 0 && need_sig) {
802 vmbus_channel_set_event(channel);
809 * @brief Retrieve the user packet on the specified channel
812 hv_vmbus_channel_recv_packet(
813 hv_vmbus_channel* channel,
816 uint32_t* buffer_actual_len,
817 uint64_t* request_id)
822 hv_vm_packet_descriptor desc;
824 *buffer_actual_len = 0;
827 ret = hv_ring_buffer_peek(&channel->inbound, &desc,
828 sizeof(hv_vm_packet_descriptor));
832 packet_len = desc.length8 << 3;
833 user_len = packet_len - (desc.data_offset8 << 3);
835 *buffer_actual_len = user_len;
837 if (user_len > buffer_len)
840 *request_id = desc.transaction_id;
842 /* Copy over the packet to the user buffer */
843 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
844 (desc.data_offset8 << 3));
850 * @brief Retrieve the raw packet on the specified channel
853 hv_vmbus_channel_recv_packet_raw(
854 hv_vmbus_channel* channel,
857 uint32_t* buffer_actual_len,
858 uint64_t* request_id)
863 hv_vm_packet_descriptor desc;
865 *buffer_actual_len = 0;
868 ret = hv_ring_buffer_peek(
869 &channel->inbound, &desc,
870 sizeof(hv_vm_packet_descriptor));
875 packetLen = desc.length8 << 3;
876 userLen = packetLen - (desc.data_offset8 << 3);
878 *buffer_actual_len = packetLen;
880 if (packetLen > buffer_len)
883 *request_id = desc.transaction_id;
885 /* Copy over the entire packet to the user buffer */
886 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
893 * Process a channel event notification
896 VmbusProcessChannelEvent(void* context, int pending)
899 uint32_t bytes_to_read;
900 hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
901 boolean_t is_batched_reading;
904 * Find the channel based on this relid and invokes
905 * the channel callback to process the event
908 if (channel == NULL) {
912 * To deal with the race condition where we might
913 * receive a packet while the relevant driver is
914 * being unloaded, dispatch the callback while
915 * holding the channel lock. The unloading driver
916 * will acquire the same channel lock to set the
917 * callback to NULL. This closes the window.
921 * Disable the lock due to newly added WITNESS check in r277723.
922 * Will seek other way to avoid race condition.
925 // mtx_lock(&channel->inbound_lock);
926 if (channel->on_channel_callback != NULL) {
927 arg = channel->channel_callback_context;
928 is_batched_reading = channel->batched_reading;
930 * Optimize host to guest signaling by ensuring:
931 * 1. While reading the channel, we disable interrupts from
933 * 2. Ensure that we process all posted messages from the host
934 * before returning from this callback.
935 * 3. Once we return, enable signaling from the host. Once this
936 * state is set we check to see if additional packets are
937 * available to read. In this case we repeat the process.
940 if (is_batched_reading)
941 hv_ring_buffer_read_begin(&channel->inbound);
943 channel->on_channel_callback(arg);
945 if (is_batched_reading)
947 hv_ring_buffer_read_end(&channel->inbound);
950 } while (is_batched_reading && (bytes_to_read != 0));
952 // mtx_unlock(&channel->inbound_lock);