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);
57 * @brief Trigger an event notification on the specified channel
60 vmbus_channel_set_event(hv_vmbus_channel *channel)
62 hv_vmbus_monitor_page *monitor_page;
64 if (channel->offer_msg.monitor_allocated) {
65 /* Each uint32_t represents 32 channels */
66 synch_set_bit((channel->offer_msg.child_rel_id & 31),
67 ((uint32_t *)hv_vmbus_g_connection.send_interrupt_page
68 + ((channel->offer_msg.child_rel_id >> 5))));
70 monitor_page = (hv_vmbus_monitor_page *)
71 hv_vmbus_g_connection.monitor_pages;
73 monitor_page++; /* Get the child to parent monitor page */
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 * @brief Open the specified channel
88 hv_vmbus_channel_open(
89 hv_vmbus_channel* new_channel,
90 uint32_t send_ring_buffer_size,
91 uint32_t recv_ring_buffer_size,
93 uint32_t user_data_len,
94 hv_vmbus_pfn_channel_callback pfn_on_channel_callback,
100 hv_vmbus_channel_open_channel* open_msg;
101 hv_vmbus_channel_msg_info* open_info;
103 mtx_lock(&new_channel->sc_lock);
104 if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
105 new_channel->state = HV_CHANNEL_OPENING_STATE;
107 mtx_unlock(&new_channel->sc_lock);
109 printf("VMBUS: Trying to open channel <%p> which in "
110 "%d state.\n", new_channel, new_channel->state);
113 mtx_unlock(&new_channel->sc_lock);
115 new_channel->on_channel_callback = pfn_on_channel_callback;
116 new_channel->channel_callback_context = context;
118 /* Allocate the ring buffer */
119 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
120 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
122 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
126 in = ((uint8_t *) out + send_ring_buffer_size);
128 new_channel->ring_buffer_pages = out;
129 new_channel->ring_buffer_page_count = (send_ring_buffer_size +
130 recv_ring_buffer_size) >> PAGE_SHIFT;
131 new_channel->ring_buffer_size = send_ring_buffer_size +
132 recv_ring_buffer_size;
134 hv_vmbus_ring_buffer_init(
135 &new_channel->outbound,
137 send_ring_buffer_size);
139 hv_vmbus_ring_buffer_init(
140 &new_channel->inbound,
142 recv_ring_buffer_size);
145 * Establish the gpadl for the ring buffer
147 new_channel->ring_buffer_gpadl_handle = 0;
149 ret = hv_vmbus_channel_establish_gpadl(new_channel,
150 new_channel->outbound.ring_buffer,
151 send_ring_buffer_size + recv_ring_buffer_size,
152 &new_channel->ring_buffer_gpadl_handle);
155 * Create and init the channel open message
157 open_info = (hv_vmbus_channel_msg_info*) malloc(
158 sizeof(hv_vmbus_channel_msg_info) +
159 sizeof(hv_vmbus_channel_open_channel),
162 KASSERT(open_info != NULL,
163 ("Error VMBUS: malloc failed to allocate Open Channel message!"));
165 if (open_info == NULL)
168 sema_init(&open_info->wait_sema, 0, "Open Info Sema");
170 open_msg = (hv_vmbus_channel_open_channel*) open_info->msg;
171 open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL;
172 open_msg->open_id = new_channel->offer_msg.child_rel_id;
173 open_msg->child_rel_id = new_channel->offer_msg.child_rel_id;
174 open_msg->ring_buffer_gpadl_handle =
175 new_channel->ring_buffer_gpadl_handle;
176 open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size
178 open_msg->target_vcpu = new_channel->target_vcpu;
181 memcpy(open_msg->user_data, user_data, user_data_len);
183 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
185 &hv_vmbus_g_connection.channel_msg_anchor,
188 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
190 ret = hv_vmbus_post_message(
191 open_msg, sizeof(hv_vmbus_channel_open_channel));
196 ret = sema_timedwait(&open_info->wait_sema, 5 * hz); /* KYS 5 seconds */
200 printf("VMBUS: channel <%p> open timeout.\n", new_channel);
204 if (open_info->response.open_result.status == 0) {
205 new_channel->state = HV_CHANNEL_OPENED_STATE;
207 printf("VMBUS: channel <%p> open success.\n", new_channel);
210 printf("Error VMBUS: channel <%p> open failed - %d!\n",
211 new_channel, open_info->response.open_result.status);
215 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
217 &hv_vmbus_g_connection.channel_msg_anchor,
220 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
221 sema_destroy(&open_info->wait_sema);
222 free(open_info, M_DEVBUF);
228 * @brief Create a gpadl for the specified buffer
231 vmbus_channel_create_gpadl_header(
233 uint32_t size, /* page-size multiple */
234 hv_vmbus_channel_msg_info** msg_info,
235 uint32_t* message_count)
239 unsigned long long pfn;
241 hv_vmbus_channel_gpadl_header* gpa_header;
242 hv_vmbus_channel_gpadl_body* gpadl_body;
243 hv_vmbus_channel_msg_info* msg_header;
244 hv_vmbus_channel_msg_info* msg_body;
246 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
248 page_count = size >> PAGE_SHIFT;
249 pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT;
251 /*do we need a gpadl body msg */
252 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
253 - sizeof(hv_vmbus_channel_gpadl_header)
254 - sizeof(hv_gpa_range);
255 pfnCount = pfnSize / sizeof(uint64_t);
257 if (page_count > pfnCount) { /* if(we need a gpadl body) */
258 /* fill in the header */
259 msg_size = sizeof(hv_vmbus_channel_msg_info)
260 + sizeof(hv_vmbus_channel_gpadl_header)
261 + sizeof(hv_gpa_range)
262 + pfnCount * sizeof(uint64_t);
263 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
266 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
267 if (msg_header == NULL)
270 TAILQ_INIT(&msg_header->sub_msg_list_anchor);
271 msg_header->message_size = msg_size;
273 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
274 gpa_header->range_count = 1;
275 gpa_header->range_buf_len = sizeof(hv_gpa_range)
276 + page_count * sizeof(uint64_t);
277 gpa_header->range[0].byte_offset = 0;
278 gpa_header->range[0].byte_count = size;
279 for (i = 0; i < pfnCount; i++) {
280 gpa_header->range[0].pfn_array[i] = pfn + i;
282 *msg_info = msg_header;
286 pfnLeft = page_count - pfnCount;
289 * figure out how many pfns we can fit
291 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
292 - sizeof(hv_vmbus_channel_gpadl_body);
293 pfnCount = pfnSize / sizeof(uint64_t);
299 if (pfnLeft > pfnCount) {
305 msg_size = sizeof(hv_vmbus_channel_msg_info) +
306 sizeof(hv_vmbus_channel_gpadl_body) +
307 pfnCurr * sizeof(uint64_t);
308 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
311 ("Error VMBUS: malloc failed to allocate Gpadl msg_body!"));
312 if (msg_body == NULL)
315 msg_body->message_size = msg_size;
318 (hv_vmbus_channel_gpadl_body*) msg_body->msg;
320 * gpadl_body->gpadl = kbuffer;
322 for (i = 0; i < pfnCurr; i++) {
323 gpadl_body->pfn[i] = pfn + pfnSum + i;
327 &msg_header->sub_msg_list_anchor,
333 } else { /* else everything fits in a header */
335 msg_size = sizeof(hv_vmbus_channel_msg_info) +
336 sizeof(hv_vmbus_channel_gpadl_header) +
337 sizeof(hv_gpa_range) +
338 page_count * sizeof(uint64_t);
339 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
342 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
343 if (msg_header == NULL)
346 msg_header->message_size = msg_size;
348 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
349 gpa_header->range_count = 1;
350 gpa_header->range_buf_len = sizeof(hv_gpa_range) +
351 page_count * sizeof(uint64_t);
352 gpa_header->range[0].byte_offset = 0;
353 gpa_header->range[0].byte_count = size;
354 for (i = 0; i < page_count; i++) {
355 gpa_header->range[0].pfn_array[i] = pfn + i;
358 *msg_info = msg_header;
366 * @brief Establish a GPADL for the specified buffer
369 hv_vmbus_channel_establish_gpadl(
370 hv_vmbus_channel* channel,
372 uint32_t size, /* page-size multiple */
373 uint32_t* gpadl_handle)
377 hv_vmbus_channel_gpadl_header* gpadl_msg;
378 hv_vmbus_channel_gpadl_body* gpadl_body;
379 hv_vmbus_channel_msg_info* msg_info;
380 hv_vmbus_channel_msg_info* sub_msg_info;
382 hv_vmbus_channel_msg_info* curr;
383 uint32_t next_gpadl_handle;
385 next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle;
386 atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1);
388 ret = vmbus_channel_create_gpadl_header(
389 contig_buffer, size, &msg_info, &msg_count);
391 if(ret != 0) { /* if(allocation failed) return immediately */
392 /* reverse atomic_add_int above */
393 atomic_subtract_int((int*)
394 &hv_vmbus_g_connection.next_gpadl_handle, 1);
398 sema_init(&msg_info->wait_sema, 0, "Open Info Sema");
399 gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg;
400 gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER;
401 gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id;
402 gpadl_msg->gpadl = next_gpadl_handle;
404 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
406 &hv_vmbus_g_connection.channel_msg_anchor,
410 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
412 ret = hv_vmbus_post_message(
414 msg_info->message_size -
415 (uint32_t) sizeof(hv_vmbus_channel_msg_info));
422 &msg_info->sub_msg_list_anchor, msg_list_entry) {
425 (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg;
427 gpadl_body->header.message_type =
428 HV_CHANNEL_MESSAGE_GPADL_BODY;
429 gpadl_body->gpadl = next_gpadl_handle;
431 ret = hv_vmbus_post_message(
433 sub_msg_info->message_size
434 - (uint32_t) sizeof(hv_vmbus_channel_msg_info));
435 /* if (the post message failed) give up and clean up */
441 ret = sema_timedwait(&msg_info->wait_sema, 5 * hz); /* KYS 5 seconds*/
445 *gpadl_handle = gpadl_msg->gpadl;
449 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
450 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
451 msg_info, msg_list_entry);
452 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
454 sema_destroy(&msg_info->wait_sema);
455 free(msg_info, M_DEVBUF);
461 * @brief Teardown the specified GPADL handle
464 hv_vmbus_channel_teardown_gpdal(
465 hv_vmbus_channel* channel,
466 uint32_t gpadl_handle)
469 hv_vmbus_channel_gpadl_teardown* msg;
470 hv_vmbus_channel_msg_info* info;
472 info = (hv_vmbus_channel_msg_info *)
473 malloc( sizeof(hv_vmbus_channel_msg_info) +
474 sizeof(hv_vmbus_channel_gpadl_teardown),
476 KASSERT(info != NULL,
477 ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!"));
483 sema_init(&info->wait_sema, 0, "Open Info Sema");
485 msg = (hv_vmbus_channel_gpadl_teardown*) info->msg;
487 msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN;
488 msg->child_rel_id = channel->offer_msg.child_rel_id;
489 msg->gpadl = gpadl_handle;
491 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
492 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor,
493 info, msg_list_entry);
494 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
496 ret = hv_vmbus_post_message(msg,
497 sizeof(hv_vmbus_channel_gpadl_teardown));
501 ret = sema_timedwait(&info->wait_sema, 5 * hz); /* KYS 5 seconds */
505 * Received a torndown response
507 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
508 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
509 info, msg_list_entry);
510 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
511 sema_destroy(&info->wait_sema);
512 free(info, M_DEVBUF);
518 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
521 hv_vmbus_channel_close_channel* msg;
522 hv_vmbus_channel_msg_info* info;
524 channel->state = HV_CHANNEL_OPEN_STATE;
525 channel->sc_creation_callback = NULL;
528 * Grab the lock to prevent race condition when a packet received
529 * and unloading driver is in the process.
531 mtx_lock(&channel->inbound_lock);
532 channel->on_channel_callback = NULL;
533 mtx_unlock(&channel->inbound_lock);
536 * Send a closing message
538 info = (hv_vmbus_channel_msg_info *)
539 malloc( sizeof(hv_vmbus_channel_msg_info) +
540 sizeof(hv_vmbus_channel_close_channel),
542 KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!"));
546 msg = (hv_vmbus_channel_close_channel*) info->msg;
547 msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL;
548 msg->child_rel_id = channel->offer_msg.child_rel_id;
550 ret = hv_vmbus_post_message(
551 msg, sizeof(hv_vmbus_channel_close_channel));
553 /* Tear down the gpadl for the channel's ring buffer */
554 if (channel->ring_buffer_gpadl_handle) {
555 hv_vmbus_channel_teardown_gpdal(channel,
556 channel->ring_buffer_gpadl_handle);
559 /* TODO: Send a msg to release the childRelId */
561 /* cleanup the ring buffers for this channel */
562 hv_ring_buffer_cleanup(&channel->outbound);
563 hv_ring_buffer_cleanup(&channel->inbound);
565 contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
568 free(info, M_DEVBUF);
572 * @brief Close the specified channel
575 hv_vmbus_channel_close(hv_vmbus_channel *channel)
577 hv_vmbus_channel* sub_channel;
579 if (channel->primary_channel != NULL) {
581 * We only close multi-channels when the primary is
588 * Close all multi-channels first.
590 TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
592 if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
594 hv_vmbus_channel_close_internal(sub_channel);
597 * Then close the primary channel.
599 hv_vmbus_channel_close_internal(channel);
603 * @brief Send the specified buffer on the given channel
606 hv_vmbus_channel_send_packet(
607 hv_vmbus_channel* channel,
611 hv_vmbus_packet_type type,
615 hv_vm_packet_descriptor desc;
617 uint64_t aligned_data;
618 uint32_t packet_len_aligned;
620 hv_vmbus_sg_buffer_list buffer_list[3];
622 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
623 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
626 /* Setup the descriptor */
627 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */
628 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
629 /* in 8-bytes granularity */
630 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
631 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
632 desc.transaction_id = request_id;
634 buffer_list[0].data = &desc;
635 buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
637 buffer_list[1].data = buffer;
638 buffer_list[1].length = buffer_len;
640 buffer_list[2].data = &aligned_data;
641 buffer_list[2].length = packet_len_aligned - packet_len;
643 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
646 /* TODO: We should determine if this is optional */
647 if (ret == 0 && need_sig) {
648 vmbus_channel_set_event(channel);
655 * @brief Send a range of single-page buffer packets using
656 * a GPADL Direct packet type
659 hv_vmbus_channel_send_packet_pagebuffer(
660 hv_vmbus_channel* channel,
661 hv_vmbus_page_buffer page_buffers[],
672 uint32_t packetLen_aligned;
673 hv_vmbus_sg_buffer_list buffer_list[3];
674 hv_vmbus_channel_packet_page_buffer desc;
676 uint64_t alignedData = 0;
678 if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
682 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
683 * is the largest size we support
685 descSize = sizeof(hv_vmbus_channel_packet_page_buffer) -
686 ((HV_MAX_PAGE_BUFFER_COUNT - page_count) *
687 sizeof(hv_vmbus_page_buffer));
688 packet_len = descSize + buffer_len;
689 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
691 /* Setup the descriptor */
692 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
693 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
694 desc.data_offset8 = descSize >> 3; /* in 8-bytes granularity */
695 desc.length8 = (uint16_t) (packetLen_aligned >> 3);
696 desc.transaction_id = request_id;
697 desc.range_count = page_count;
699 for (i = 0; i < page_count; i++) {
700 desc.range[i].length = page_buffers[i].length;
701 desc.range[i].offset = page_buffers[i].offset;
702 desc.range[i].pfn = page_buffers[i].pfn;
705 buffer_list[0].data = &desc;
706 buffer_list[0].length = descSize;
708 buffer_list[1].data = buffer;
709 buffer_list[1].length = buffer_len;
711 buffer_list[2].data = &alignedData;
712 buffer_list[2].length = packetLen_aligned - packet_len;
714 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
717 /* TODO: We should determine if this is optional */
718 if (ret == 0 && need_sig) {
719 vmbus_channel_set_event(channel);
726 * @brief Send a multi-page buffer packet using a GPADL Direct packet type
729 hv_vmbus_channel_send_packet_multipagebuffer(
730 hv_vmbus_channel* channel,
731 hv_vmbus_multipage_buffer* multi_page_buffer,
741 uint32_t packet_len_aligned;
743 uint64_t aligned_data = 0;
744 hv_vmbus_sg_buffer_list buffer_list[3];
745 hv_vmbus_channel_packet_multipage_buffer desc;
748 HV_NUM_PAGES_SPANNED(
749 multi_page_buffer->offset,
750 multi_page_buffer->length);
752 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
755 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
756 * is the largest size we support
759 sizeof(hv_vmbus_channel_packet_multipage_buffer) -
760 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
762 packet_len = desc_size + buffer_len;
763 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
766 * Setup the descriptor
768 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
769 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
770 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
771 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
772 desc.transaction_id = request_id;
773 desc.range_count = 1;
775 desc.range.length = multi_page_buffer->length;
776 desc.range.offset = multi_page_buffer->offset;
778 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
779 pfn_count * sizeof(uint64_t));
781 buffer_list[0].data = &desc;
782 buffer_list[0].length = desc_size;
784 buffer_list[1].data = buffer;
785 buffer_list[1].length = buffer_len;
787 buffer_list[2].data = &aligned_data;
788 buffer_list[2].length = packet_len_aligned - packet_len;
790 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
793 /* TODO: We should determine if this is optional */
794 if (ret == 0 && need_sig) {
795 vmbus_channel_set_event(channel);
802 * @brief Retrieve the user packet on the specified channel
805 hv_vmbus_channel_recv_packet(
806 hv_vmbus_channel* channel,
809 uint32_t* buffer_actual_len,
810 uint64_t* request_id)
815 hv_vm_packet_descriptor desc;
817 *buffer_actual_len = 0;
820 ret = hv_ring_buffer_peek(&channel->inbound, &desc,
821 sizeof(hv_vm_packet_descriptor));
825 packet_len = desc.length8 << 3;
826 user_len = packet_len - (desc.data_offset8 << 3);
828 *buffer_actual_len = user_len;
830 if (user_len > buffer_len)
833 *request_id = desc.transaction_id;
835 /* Copy over the packet to the user buffer */
836 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
837 (desc.data_offset8 << 3));
843 * @brief Retrieve the raw packet on the specified channel
846 hv_vmbus_channel_recv_packet_raw(
847 hv_vmbus_channel* channel,
850 uint32_t* buffer_actual_len,
851 uint64_t* request_id)
856 hv_vm_packet_descriptor desc;
858 *buffer_actual_len = 0;
861 ret = hv_ring_buffer_peek(
862 &channel->inbound, &desc,
863 sizeof(hv_vm_packet_descriptor));
868 packetLen = desc.length8 << 3;
869 userLen = packetLen - (desc.data_offset8 << 3);
871 *buffer_actual_len = packetLen;
873 if (packetLen > buffer_len)
876 *request_id = desc.transaction_id;
878 /* Copy over the entire packet to the user buffer */
879 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);