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_page_2;
74 synch_set_bit(channel->monitor_bit,
75 (uint32_t *)&monitor_page->
76 trigger_group[channel->monitor_group].u.pending);
78 hv_vmbus_set_event(channel);
84 * @brief Open the specified channel
87 hv_vmbus_channel_open(
88 hv_vmbus_channel* new_channel,
89 uint32_t send_ring_buffer_size,
90 uint32_t recv_ring_buffer_size,
92 uint32_t user_data_len,
93 hv_vmbus_pfn_channel_callback pfn_on_channel_callback,
99 hv_vmbus_channel_open_channel* open_msg;
100 hv_vmbus_channel_msg_info* open_info;
102 mtx_lock(&new_channel->sc_lock);
103 if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
104 new_channel->state = HV_CHANNEL_OPENING_STATE;
106 mtx_unlock(&new_channel->sc_lock);
108 printf("VMBUS: Trying to open channel <%p> which in "
109 "%d state.\n", new_channel, new_channel->state);
112 mtx_unlock(&new_channel->sc_lock);
114 new_channel->on_channel_callback = pfn_on_channel_callback;
115 new_channel->channel_callback_context = context;
117 new_channel->rxq = hv_vmbus_g_context.hv_event_queue[new_channel->target_cpu];
118 TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
120 /* Allocate the ring buffer */
121 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
122 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
124 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
128 in = ((uint8_t *) out + send_ring_buffer_size);
130 new_channel->ring_buffer_pages = out;
131 new_channel->ring_buffer_page_count = (send_ring_buffer_size +
132 recv_ring_buffer_size) >> PAGE_SHIFT;
133 new_channel->ring_buffer_size = send_ring_buffer_size +
134 recv_ring_buffer_size;
136 hv_vmbus_ring_buffer_init(
137 &new_channel->outbound,
139 send_ring_buffer_size);
141 hv_vmbus_ring_buffer_init(
142 &new_channel->inbound,
144 recv_ring_buffer_size);
147 * Establish the gpadl for the ring buffer
149 new_channel->ring_buffer_gpadl_handle = 0;
151 ret = hv_vmbus_channel_establish_gpadl(new_channel,
152 new_channel->outbound.ring_buffer,
153 send_ring_buffer_size + recv_ring_buffer_size,
154 &new_channel->ring_buffer_gpadl_handle);
157 * Create and init the channel open message
159 open_info = (hv_vmbus_channel_msg_info*) malloc(
160 sizeof(hv_vmbus_channel_msg_info) +
161 sizeof(hv_vmbus_channel_open_channel),
164 KASSERT(open_info != NULL,
165 ("Error VMBUS: malloc failed to allocate Open Channel message!"));
167 if (open_info == NULL)
170 sema_init(&open_info->wait_sema, 0, "Open Info Sema");
172 open_msg = (hv_vmbus_channel_open_channel*) open_info->msg;
173 open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL;
174 open_msg->open_id = new_channel->offer_msg.child_rel_id;
175 open_msg->child_rel_id = new_channel->offer_msg.child_rel_id;
176 open_msg->ring_buffer_gpadl_handle =
177 new_channel->ring_buffer_gpadl_handle;
178 open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size
180 open_msg->target_vcpu = new_channel->target_vcpu;
183 memcpy(open_msg->user_data, user_data, user_data_len);
185 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
187 &hv_vmbus_g_connection.channel_msg_anchor,
190 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
192 ret = hv_vmbus_post_message(
193 open_msg, sizeof(hv_vmbus_channel_open_channel));
198 ret = sema_timedwait(&open_info->wait_sema, 5 * hz); /* KYS 5 seconds */
202 printf("VMBUS: channel <%p> open timeout.\n", new_channel);
206 if (open_info->response.open_result.status == 0) {
207 new_channel->state = HV_CHANNEL_OPENED_STATE;
209 printf("VMBUS: channel <%p> open success.\n", new_channel);
212 printf("Error VMBUS: channel <%p> open failed - %d!\n",
213 new_channel, open_info->response.open_result.status);
217 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
219 &hv_vmbus_g_connection.channel_msg_anchor,
222 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
223 sema_destroy(&open_info->wait_sema);
224 free(open_info, M_DEVBUF);
230 * @brief Create a gpadl for the specified buffer
233 vmbus_channel_create_gpadl_header(
235 uint32_t size, /* page-size multiple */
236 hv_vmbus_channel_msg_info** msg_info,
237 uint32_t* message_count)
241 unsigned long long pfn;
243 hv_vmbus_channel_gpadl_header* gpa_header;
244 hv_vmbus_channel_gpadl_body* gpadl_body;
245 hv_vmbus_channel_msg_info* msg_header;
246 hv_vmbus_channel_msg_info* msg_body;
248 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
250 page_count = size >> PAGE_SHIFT;
251 pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT;
253 /*do we need a gpadl body msg */
254 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
255 - sizeof(hv_vmbus_channel_gpadl_header)
256 - sizeof(hv_gpa_range);
257 pfnCount = pfnSize / sizeof(uint64_t);
259 if (page_count > pfnCount) { /* if(we need a gpadl body) */
260 /* fill in the header */
261 msg_size = sizeof(hv_vmbus_channel_msg_info)
262 + sizeof(hv_vmbus_channel_gpadl_header)
263 + sizeof(hv_gpa_range)
264 + pfnCount * sizeof(uint64_t);
265 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
268 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
269 if (msg_header == NULL)
272 TAILQ_INIT(&msg_header->sub_msg_list_anchor);
273 msg_header->message_size = msg_size;
275 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
276 gpa_header->range_count = 1;
277 gpa_header->range_buf_len = sizeof(hv_gpa_range)
278 + page_count * sizeof(uint64_t);
279 gpa_header->range[0].byte_offset = 0;
280 gpa_header->range[0].byte_count = size;
281 for (i = 0; i < pfnCount; i++) {
282 gpa_header->range[0].pfn_array[i] = pfn + i;
284 *msg_info = msg_header;
288 pfnLeft = page_count - pfnCount;
291 * figure out how many pfns we can fit
293 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
294 - sizeof(hv_vmbus_channel_gpadl_body);
295 pfnCount = pfnSize / sizeof(uint64_t);
301 if (pfnLeft > pfnCount) {
307 msg_size = sizeof(hv_vmbus_channel_msg_info) +
308 sizeof(hv_vmbus_channel_gpadl_body) +
309 pfnCurr * sizeof(uint64_t);
310 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
313 ("Error VMBUS: malloc failed to allocate Gpadl msg_body!"));
314 if (msg_body == NULL)
317 msg_body->message_size = msg_size;
320 (hv_vmbus_channel_gpadl_body*) msg_body->msg;
322 * gpadl_body->gpadl = kbuffer;
324 for (i = 0; i < pfnCurr; i++) {
325 gpadl_body->pfn[i] = pfn + pfnSum + i;
329 &msg_header->sub_msg_list_anchor,
335 } else { /* else everything fits in a header */
337 msg_size = sizeof(hv_vmbus_channel_msg_info) +
338 sizeof(hv_vmbus_channel_gpadl_header) +
339 sizeof(hv_gpa_range) +
340 page_count * sizeof(uint64_t);
341 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
344 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
345 if (msg_header == NULL)
348 msg_header->message_size = msg_size;
350 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
351 gpa_header->range_count = 1;
352 gpa_header->range_buf_len = sizeof(hv_gpa_range) +
353 page_count * sizeof(uint64_t);
354 gpa_header->range[0].byte_offset = 0;
355 gpa_header->range[0].byte_count = size;
356 for (i = 0; i < page_count; i++) {
357 gpa_header->range[0].pfn_array[i] = pfn + i;
360 *msg_info = msg_header;
368 * @brief Establish a GPADL for the specified buffer
371 hv_vmbus_channel_establish_gpadl(
372 hv_vmbus_channel* channel,
374 uint32_t size, /* page-size multiple */
375 uint32_t* gpadl_handle)
379 hv_vmbus_channel_gpadl_header* gpadl_msg;
380 hv_vmbus_channel_gpadl_body* gpadl_body;
381 hv_vmbus_channel_msg_info* msg_info;
382 hv_vmbus_channel_msg_info* sub_msg_info;
384 hv_vmbus_channel_msg_info* curr;
385 uint32_t next_gpadl_handle;
387 next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle;
388 atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1);
390 ret = vmbus_channel_create_gpadl_header(
391 contig_buffer, size, &msg_info, &msg_count);
393 if(ret != 0) { /* if(allocation failed) return immediately */
394 /* reverse atomic_add_int above */
395 atomic_subtract_int((int*)
396 &hv_vmbus_g_connection.next_gpadl_handle, 1);
400 sema_init(&msg_info->wait_sema, 0, "Open Info Sema");
401 gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg;
402 gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER;
403 gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id;
404 gpadl_msg->gpadl = next_gpadl_handle;
406 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
408 &hv_vmbus_g_connection.channel_msg_anchor,
412 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
414 ret = hv_vmbus_post_message(
416 msg_info->message_size -
417 (uint32_t) sizeof(hv_vmbus_channel_msg_info));
424 &msg_info->sub_msg_list_anchor, msg_list_entry) {
427 (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg;
429 gpadl_body->header.message_type =
430 HV_CHANNEL_MESSAGE_GPADL_BODY;
431 gpadl_body->gpadl = next_gpadl_handle;
433 ret = hv_vmbus_post_message(
435 sub_msg_info->message_size
436 - (uint32_t) sizeof(hv_vmbus_channel_msg_info));
437 /* if (the post message failed) give up and clean up */
443 ret = sema_timedwait(&msg_info->wait_sema, 5 * hz); /* KYS 5 seconds*/
447 *gpadl_handle = gpadl_msg->gpadl;
451 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
452 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
453 msg_info, msg_list_entry);
454 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
456 sema_destroy(&msg_info->wait_sema);
457 free(msg_info, M_DEVBUF);
463 * @brief Teardown the specified GPADL handle
466 hv_vmbus_channel_teardown_gpdal(
467 hv_vmbus_channel* channel,
468 uint32_t gpadl_handle)
471 hv_vmbus_channel_gpadl_teardown* msg;
472 hv_vmbus_channel_msg_info* info;
474 info = (hv_vmbus_channel_msg_info *)
475 malloc( sizeof(hv_vmbus_channel_msg_info) +
476 sizeof(hv_vmbus_channel_gpadl_teardown),
478 KASSERT(info != NULL,
479 ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!"));
485 sema_init(&info->wait_sema, 0, "Open Info Sema");
487 msg = (hv_vmbus_channel_gpadl_teardown*) info->msg;
489 msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN;
490 msg->child_rel_id = channel->offer_msg.child_rel_id;
491 msg->gpadl = gpadl_handle;
493 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
494 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor,
495 info, msg_list_entry);
496 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
498 ret = hv_vmbus_post_message(msg,
499 sizeof(hv_vmbus_channel_gpadl_teardown));
503 ret = sema_timedwait(&info->wait_sema, 5 * hz); /* KYS 5 seconds */
507 * Received a torndown response
509 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
510 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
511 info, msg_list_entry);
512 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
513 sema_destroy(&info->wait_sema);
514 free(info, M_DEVBUF);
520 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
523 struct taskqueue *rxq = channel->rxq;
524 hv_vmbus_channel_close_channel* msg;
525 hv_vmbus_channel_msg_info* info;
527 channel->state = HV_CHANNEL_OPEN_STATE;
528 channel->sc_creation_callback = NULL;
531 * set rxq to NULL to avoid more requests be scheduled
534 taskqueue_drain(rxq, &channel->channel_task);
536 * Grab the lock to prevent race condition when a packet received
537 * and unloading driver is in the process.
539 mtx_lock(&channel->inbound_lock);
540 channel->on_channel_callback = NULL;
541 mtx_unlock(&channel->inbound_lock);
544 * Send a closing message
546 info = (hv_vmbus_channel_msg_info *)
547 malloc( sizeof(hv_vmbus_channel_msg_info) +
548 sizeof(hv_vmbus_channel_close_channel),
550 KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!"));
554 msg = (hv_vmbus_channel_close_channel*) info->msg;
555 msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL;
556 msg->child_rel_id = channel->offer_msg.child_rel_id;
558 ret = hv_vmbus_post_message(
559 msg, sizeof(hv_vmbus_channel_close_channel));
561 /* Tear down the gpadl for the channel's ring buffer */
562 if (channel->ring_buffer_gpadl_handle) {
563 hv_vmbus_channel_teardown_gpdal(channel,
564 channel->ring_buffer_gpadl_handle);
567 /* TODO: Send a msg to release the childRelId */
569 /* cleanup the ring buffers for this channel */
570 hv_ring_buffer_cleanup(&channel->outbound);
571 hv_ring_buffer_cleanup(&channel->inbound);
573 contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
576 free(info, M_DEVBUF);
580 * @brief Close the specified channel
583 hv_vmbus_channel_close(hv_vmbus_channel *channel)
585 hv_vmbus_channel* sub_channel;
587 if (channel->primary_channel != NULL) {
589 * We only close multi-channels when the primary is
596 * Close all multi-channels first.
598 TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
600 if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
602 hv_vmbus_channel_close_internal(sub_channel);
605 * Then close the primary channel.
607 hv_vmbus_channel_close_internal(channel);
611 * @brief Send the specified buffer on the given channel
614 hv_vmbus_channel_send_packet(
615 hv_vmbus_channel* channel,
619 hv_vmbus_packet_type type,
623 hv_vm_packet_descriptor desc;
625 uint64_t aligned_data;
626 uint32_t packet_len_aligned;
628 hv_vmbus_sg_buffer_list buffer_list[3];
630 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
631 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
634 /* Setup the descriptor */
635 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */
636 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
637 /* in 8-bytes granularity */
638 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
639 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
640 desc.transaction_id = request_id;
642 buffer_list[0].data = &desc;
643 buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
645 buffer_list[1].data = buffer;
646 buffer_list[1].length = buffer_len;
648 buffer_list[2].data = &aligned_data;
649 buffer_list[2].length = packet_len_aligned - packet_len;
651 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
654 /* TODO: We should determine if this is optional */
655 if (ret == 0 && need_sig) {
656 vmbus_channel_set_event(channel);
663 * @brief Send a range of single-page buffer packets using
664 * a GPADL Direct packet type
667 hv_vmbus_channel_send_packet_pagebuffer(
668 hv_vmbus_channel* channel,
669 hv_vmbus_page_buffer page_buffers[],
679 uint32_t page_buflen;
680 uint32_t packetLen_aligned;
681 hv_vmbus_sg_buffer_list buffer_list[4];
682 hv_vmbus_channel_packet_page_buffer desc;
684 uint64_t alignedData = 0;
686 if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
690 * Adjust the size down since hv_vmbus_channel_packet_page_buffer
691 * is the largest size we support
693 descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
694 page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
695 packet_len = descSize + page_buflen + buffer_len;
696 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
698 /* Setup the descriptor */
699 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
700 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
701 /* in 8-bytes granularity */
702 desc.data_offset8 = (descSize + page_buflen) >> 3;
703 desc.length8 = (uint16_t) (packetLen_aligned >> 3);
704 desc.transaction_id = request_id;
705 desc.range_count = page_count;
707 buffer_list[0].data = &desc;
708 buffer_list[0].length = descSize;
710 buffer_list[1].data = page_buffers;
711 buffer_list[1].length = page_buflen;
713 buffer_list[2].data = buffer;
714 buffer_list[2].length = buffer_len;
716 buffer_list[3].data = &alignedData;
717 buffer_list[3].length = packetLen_aligned - packet_len;
719 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
722 /* TODO: We should determine if this is optional */
723 if (ret == 0 && need_sig) {
724 vmbus_channel_set_event(channel);
731 * @brief Send a multi-page buffer packet using a GPADL Direct packet type
734 hv_vmbus_channel_send_packet_multipagebuffer(
735 hv_vmbus_channel* channel,
736 hv_vmbus_multipage_buffer* multi_page_buffer,
746 uint32_t packet_len_aligned;
748 uint64_t aligned_data = 0;
749 hv_vmbus_sg_buffer_list buffer_list[3];
750 hv_vmbus_channel_packet_multipage_buffer desc;
753 HV_NUM_PAGES_SPANNED(
754 multi_page_buffer->offset,
755 multi_page_buffer->length);
757 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
760 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
761 * is the largest size we support
764 sizeof(hv_vmbus_channel_packet_multipage_buffer) -
765 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
767 packet_len = desc_size + buffer_len;
768 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
771 * Setup the descriptor
773 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
774 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
775 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
776 desc.length8 = (uint16_t) (packet_len_aligned >> 3);
777 desc.transaction_id = request_id;
778 desc.range_count = 1;
780 desc.range.length = multi_page_buffer->length;
781 desc.range.offset = multi_page_buffer->offset;
783 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
784 pfn_count * sizeof(uint64_t));
786 buffer_list[0].data = &desc;
787 buffer_list[0].length = desc_size;
789 buffer_list[1].data = buffer;
790 buffer_list[1].length = buffer_len;
792 buffer_list[2].data = &aligned_data;
793 buffer_list[2].length = packet_len_aligned - packet_len;
795 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
798 /* TODO: We should determine if this is optional */
799 if (ret == 0 && need_sig) {
800 vmbus_channel_set_event(channel);
807 * @brief Retrieve the user packet on the specified channel
810 hv_vmbus_channel_recv_packet(
811 hv_vmbus_channel* channel,
814 uint32_t* buffer_actual_len,
815 uint64_t* request_id)
820 hv_vm_packet_descriptor desc;
822 *buffer_actual_len = 0;
825 ret = hv_ring_buffer_peek(&channel->inbound, &desc,
826 sizeof(hv_vm_packet_descriptor));
830 packet_len = desc.length8 << 3;
831 user_len = packet_len - (desc.data_offset8 << 3);
833 *buffer_actual_len = user_len;
835 if (user_len > buffer_len)
838 *request_id = desc.transaction_id;
840 /* Copy over the packet to the user buffer */
841 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
842 (desc.data_offset8 << 3));
848 * @brief Retrieve the raw packet on the specified channel
851 hv_vmbus_channel_recv_packet_raw(
852 hv_vmbus_channel* channel,
855 uint32_t* buffer_actual_len,
856 uint64_t* request_id)
861 hv_vm_packet_descriptor desc;
863 *buffer_actual_len = 0;
866 ret = hv_ring_buffer_peek(
867 &channel->inbound, &desc,
868 sizeof(hv_vm_packet_descriptor));
873 packetLen = desc.length8 << 3;
874 userLen = packetLen - (desc.data_offset8 << 3);
876 *buffer_actual_len = packetLen;
878 if (packetLen > buffer_len)
881 *request_id = desc.transaction_id;
883 /* Copy over the entire packet to the user buffer */
884 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
891 * Process a channel event notification
894 VmbusProcessChannelEvent(void* context, int pending)
897 uint32_t bytes_to_read;
898 hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
899 boolean_t is_batched_reading;
902 * Find the channel based on this relid and invokes
903 * the channel callback to process the event
906 if (channel == NULL) {
910 * To deal with the race condition where we might
911 * receive a packet while the relevant driver is
912 * being unloaded, dispatch the callback while
913 * holding the channel lock. The unloading driver
914 * will acquire the same channel lock to set the
915 * callback to NULL. This closes the window.
919 * Disable the lock due to newly added WITNESS check in r277723.
920 * Will seek other way to avoid race condition.
923 // mtx_lock(&channel->inbound_lock);
924 if (channel->on_channel_callback != NULL) {
925 arg = channel->channel_callback_context;
926 is_batched_reading = channel->batched_reading;
928 * Optimize host to guest signaling by ensuring:
929 * 1. While reading the channel, we disable interrupts from
931 * 2. Ensure that we process all posted messages from the host
932 * before returning from this callback.
933 * 3. Once we return, enable signaling from the host. Once this
934 * state is set we check to see if additional packets are
935 * available to read. In this case we repeat the process.
938 if (is_batched_reading)
939 hv_ring_buffer_read_begin(&channel->inbound);
941 channel->on_channel_callback(arg);
943 if (is_batched_reading)
945 hv_ring_buffer_read_end(&channel->inbound);
948 } while (is_batched_reading && (bytes_to_read != 0));
950 // mtx_unlock(&channel->inbound_lock);