vmbus_chan.c revision 294886
1/*- 2 * Copyright (c) 2009-2012 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 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. 16 * 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. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/dev/hyperv/vmbus/hv_channel.c 294886 2016-01-27 03:53:30Z sephe $"); 31 32#include <sys/param.h> 33#include <sys/malloc.h> 34#include <sys/systm.h> 35#include <sys/mbuf.h> 36#include <sys/lock.h> 37#include <sys/mutex.h> 38#include <machine/bus.h> 39#include <vm/vm.h> 40#include <vm/vm_param.h> 41#include <vm/pmap.h> 42 43#include "hv_vmbus_priv.h" 44 45static int vmbus_channel_create_gpadl_header( 46 /* must be phys and virt contiguous*/ 47 void* contig_buffer, 48 /* page-size multiple */ 49 uint32_t size, 50 hv_vmbus_channel_msg_info** msg_info, 51 uint32_t* message_count); 52 53static void vmbus_channel_set_event(hv_vmbus_channel* channel); 54static void VmbusProcessChannelEvent(void* channel, int pending); 55 56/** 57 * @brief Trigger an event notification on the specified channel 58 */ 59static void 60vmbus_channel_set_event(hv_vmbus_channel *channel) 61{ 62 hv_vmbus_monitor_page *monitor_page; 63 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)))); 69 70 monitor_page = (hv_vmbus_monitor_page *) 71 hv_vmbus_g_connection.monitor_pages; 72 73 monitor_page++; /* Get the child to parent monitor page */ 74 75 synch_set_bit(channel->monitor_bit, 76 (uint32_t *)&monitor_page-> 77 trigger_group[channel->monitor_group].u.pending); 78 } else { 79 hv_vmbus_set_event(channel); 80 } 81 82} 83 84/** 85 * @brief Open the specified channel 86 */ 87int 88hv_vmbus_channel_open( 89 hv_vmbus_channel* new_channel, 90 uint32_t send_ring_buffer_size, 91 uint32_t recv_ring_buffer_size, 92 void* user_data, 93 uint32_t user_data_len, 94 hv_vmbus_pfn_channel_callback pfn_on_channel_callback, 95 void* context) 96{ 97 98 int ret = 0; 99 void *in, *out; 100 hv_vmbus_channel_open_channel* open_msg; 101 hv_vmbus_channel_msg_info* open_info; 102 103 mtx_lock(&new_channel->sc_lock); 104 if (new_channel->state == HV_CHANNEL_OPEN_STATE) { 105 new_channel->state = HV_CHANNEL_OPENING_STATE; 106 } else { 107 mtx_unlock(&new_channel->sc_lock); 108 if(bootverbose) 109 printf("VMBUS: Trying to open channel <%p> which in " 110 "%d state.\n", new_channel, new_channel->state); 111 return (EINVAL); 112 } 113 mtx_unlock(&new_channel->sc_lock); 114 115 new_channel->on_channel_callback = pfn_on_channel_callback; 116 new_channel->channel_callback_context = context; 117 118 new_channel->rxq = hv_vmbus_g_context.hv_event_queue[new_channel->target_cpu]; 119 TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel); 120 121 /* Allocate the ring buffer */ 122 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size), 123 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0); 124 KASSERT(out != NULL, 125 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!")); 126 if (out == NULL) 127 return (ENOMEM); 128 129 in = ((uint8_t *) out + send_ring_buffer_size); 130 131 new_channel->ring_buffer_pages = out; 132 new_channel->ring_buffer_page_count = (send_ring_buffer_size + 133 recv_ring_buffer_size) >> PAGE_SHIFT; 134 new_channel->ring_buffer_size = send_ring_buffer_size + 135 recv_ring_buffer_size; 136 137 hv_vmbus_ring_buffer_init( 138 &new_channel->outbound, 139 out, 140 send_ring_buffer_size); 141 142 hv_vmbus_ring_buffer_init( 143 &new_channel->inbound, 144 in, 145 recv_ring_buffer_size); 146 147 /** 148 * Establish the gpadl for the ring buffer 149 */ 150 new_channel->ring_buffer_gpadl_handle = 0; 151 152 ret = hv_vmbus_channel_establish_gpadl(new_channel, 153 new_channel->outbound.ring_buffer, 154 send_ring_buffer_size + recv_ring_buffer_size, 155 &new_channel->ring_buffer_gpadl_handle); 156 157 /** 158 * Create and init the channel open message 159 */ 160 open_info = (hv_vmbus_channel_msg_info*) malloc( 161 sizeof(hv_vmbus_channel_msg_info) + 162 sizeof(hv_vmbus_channel_open_channel), 163 M_DEVBUF, 164 M_NOWAIT); 165 KASSERT(open_info != NULL, 166 ("Error VMBUS: malloc failed to allocate Open Channel message!")); 167 168 if (open_info == NULL) 169 return (ENOMEM); 170 171 sema_init(&open_info->wait_sema, 0, "Open Info Sema"); 172 173 open_msg = (hv_vmbus_channel_open_channel*) open_info->msg; 174 open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL; 175 open_msg->open_id = new_channel->offer_msg.child_rel_id; 176 open_msg->child_rel_id = new_channel->offer_msg.child_rel_id; 177 open_msg->ring_buffer_gpadl_handle = 178 new_channel->ring_buffer_gpadl_handle; 179 open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size 180 >> PAGE_SHIFT; 181 open_msg->target_vcpu = new_channel->target_vcpu; 182 183 if (user_data_len) 184 memcpy(open_msg->user_data, user_data, user_data_len); 185 186 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 187 TAILQ_INSERT_TAIL( 188 &hv_vmbus_g_connection.channel_msg_anchor, 189 open_info, 190 msg_list_entry); 191 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 192 193 ret = hv_vmbus_post_message( 194 open_msg, sizeof(hv_vmbus_channel_open_channel)); 195 196 if (ret != 0) 197 goto cleanup; 198 199 ret = sema_timedwait(&open_info->wait_sema, 500); /* KYS 5 seconds */ 200 201 if (ret) { 202 if(bootverbose) 203 printf("VMBUS: channel <%p> open timeout.\n", new_channel); 204 goto cleanup; 205 } 206 207 if (open_info->response.open_result.status == 0) { 208 new_channel->state = HV_CHANNEL_OPENED_STATE; 209 if(bootverbose) 210 printf("VMBUS: channel <%p> open success.\n", new_channel); 211 } else { 212 if(bootverbose) 213 printf("Error VMBUS: channel <%p> open failed - %d!\n", 214 new_channel, open_info->response.open_result.status); 215 } 216 217 cleanup: 218 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 219 TAILQ_REMOVE( 220 &hv_vmbus_g_connection.channel_msg_anchor, 221 open_info, 222 msg_list_entry); 223 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 224 sema_destroy(&open_info->wait_sema); 225 free(open_info, M_DEVBUF); 226 227 return (ret); 228} 229 230/** 231 * @brief Create a gpadl for the specified buffer 232 */ 233static int 234vmbus_channel_create_gpadl_header( 235 void* contig_buffer, 236 uint32_t size, /* page-size multiple */ 237 hv_vmbus_channel_msg_info** msg_info, 238 uint32_t* message_count) 239{ 240 int i; 241 int page_count; 242 unsigned long long pfn; 243 uint32_t msg_size; 244 hv_vmbus_channel_gpadl_header* gpa_header; 245 hv_vmbus_channel_gpadl_body* gpadl_body; 246 hv_vmbus_channel_msg_info* msg_header; 247 hv_vmbus_channel_msg_info* msg_body; 248 249 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize; 250 251 page_count = size >> PAGE_SHIFT; 252 pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT; 253 254 /*do we need a gpadl body msg */ 255 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE 256 - sizeof(hv_vmbus_channel_gpadl_header) 257 - sizeof(hv_gpa_range); 258 pfnCount = pfnSize / sizeof(uint64_t); 259 260 if (page_count > pfnCount) { /* if(we need a gpadl body) */ 261 /* fill in the header */ 262 msg_size = sizeof(hv_vmbus_channel_msg_info) 263 + sizeof(hv_vmbus_channel_gpadl_header) 264 + sizeof(hv_gpa_range) 265 + pfnCount * sizeof(uint64_t); 266 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO); 267 KASSERT( 268 msg_header != NULL, 269 ("Error VMBUS: malloc failed to allocate Gpadl Message!")); 270 if (msg_header == NULL) 271 return (ENOMEM); 272 273 TAILQ_INIT(&msg_header->sub_msg_list_anchor); 274 msg_header->message_size = msg_size; 275 276 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg; 277 gpa_header->range_count = 1; 278 gpa_header->range_buf_len = sizeof(hv_gpa_range) 279 + page_count * sizeof(uint64_t); 280 gpa_header->range[0].byte_offset = 0; 281 gpa_header->range[0].byte_count = size; 282 for (i = 0; i < pfnCount; i++) { 283 gpa_header->range[0].pfn_array[i] = pfn + i; 284 } 285 *msg_info = msg_header; 286 *message_count = 1; 287 288 pfnSum = pfnCount; 289 pfnLeft = page_count - pfnCount; 290 291 /* 292 * figure out how many pfns we can fit 293 */ 294 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE 295 - sizeof(hv_vmbus_channel_gpadl_body); 296 pfnCount = pfnSize / sizeof(uint64_t); 297 298 /* 299 * fill in the body 300 */ 301 while (pfnLeft) { 302 if (pfnLeft > pfnCount) { 303 pfnCurr = pfnCount; 304 } else { 305 pfnCurr = pfnLeft; 306 } 307 308 msg_size = sizeof(hv_vmbus_channel_msg_info) + 309 sizeof(hv_vmbus_channel_gpadl_body) + 310 pfnCurr * sizeof(uint64_t); 311 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO); 312 KASSERT( 313 msg_body != NULL, 314 ("Error VMBUS: malloc failed to allocate Gpadl msg_body!")); 315 if (msg_body == NULL) 316 return (ENOMEM); 317 318 msg_body->message_size = msg_size; 319 (*message_count)++; 320 gpadl_body = 321 (hv_vmbus_channel_gpadl_body*) msg_body->msg; 322 /* 323 * gpadl_body->gpadl = kbuffer; 324 */ 325 for (i = 0; i < pfnCurr; i++) { 326 gpadl_body->pfn[i] = pfn + pfnSum + i; 327 } 328 329 TAILQ_INSERT_TAIL( 330 &msg_header->sub_msg_list_anchor, 331 msg_body, 332 msg_list_entry); 333 pfnSum += pfnCurr; 334 pfnLeft -= pfnCurr; 335 } 336 } else { /* else everything fits in a header */ 337 338 msg_size = sizeof(hv_vmbus_channel_msg_info) + 339 sizeof(hv_vmbus_channel_gpadl_header) + 340 sizeof(hv_gpa_range) + 341 page_count * sizeof(uint64_t); 342 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO); 343 KASSERT( 344 msg_header != NULL, 345 ("Error VMBUS: malloc failed to allocate Gpadl Message!")); 346 if (msg_header == NULL) 347 return (ENOMEM); 348 349 msg_header->message_size = msg_size; 350 351 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg; 352 gpa_header->range_count = 1; 353 gpa_header->range_buf_len = sizeof(hv_gpa_range) + 354 page_count * sizeof(uint64_t); 355 gpa_header->range[0].byte_offset = 0; 356 gpa_header->range[0].byte_count = size; 357 for (i = 0; i < page_count; i++) { 358 gpa_header->range[0].pfn_array[i] = pfn + i; 359 } 360 361 *msg_info = msg_header; 362 *message_count = 1; 363 } 364 365 return (0); 366} 367 368/** 369 * @brief Establish a GPADL for the specified buffer 370 */ 371int 372hv_vmbus_channel_establish_gpadl( 373 hv_vmbus_channel* channel, 374 void* contig_buffer, 375 uint32_t size, /* page-size multiple */ 376 uint32_t* gpadl_handle) 377 378{ 379 int ret = 0; 380 hv_vmbus_channel_gpadl_header* gpadl_msg; 381 hv_vmbus_channel_gpadl_body* gpadl_body; 382 hv_vmbus_channel_msg_info* msg_info; 383 hv_vmbus_channel_msg_info* sub_msg_info; 384 uint32_t msg_count; 385 hv_vmbus_channel_msg_info* curr; 386 uint32_t next_gpadl_handle; 387 388 next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle; 389 atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1); 390 391 ret = vmbus_channel_create_gpadl_header( 392 contig_buffer, size, &msg_info, &msg_count); 393 394 if(ret != 0) { /* if(allocation failed) return immediately */ 395 /* reverse atomic_add_int above */ 396 atomic_subtract_int((int*) 397 &hv_vmbus_g_connection.next_gpadl_handle, 1); 398 return ret; 399 } 400 401 sema_init(&msg_info->wait_sema, 0, "Open Info Sema"); 402 gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg; 403 gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER; 404 gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id; 405 gpadl_msg->gpadl = next_gpadl_handle; 406 407 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 408 TAILQ_INSERT_TAIL( 409 &hv_vmbus_g_connection.channel_msg_anchor, 410 msg_info, 411 msg_list_entry); 412 413 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 414 415 ret = hv_vmbus_post_message( 416 gpadl_msg, 417 msg_info->message_size - 418 (uint32_t) sizeof(hv_vmbus_channel_msg_info)); 419 420 if (ret != 0) 421 goto cleanup; 422 423 if (msg_count > 1) { 424 TAILQ_FOREACH(curr, 425 &msg_info->sub_msg_list_anchor, msg_list_entry) { 426 sub_msg_info = curr; 427 gpadl_body = 428 (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg; 429 430 gpadl_body->header.message_type = 431 HV_CHANNEL_MESSAGE_GPADL_BODY; 432 gpadl_body->gpadl = next_gpadl_handle; 433 434 ret = hv_vmbus_post_message( 435 gpadl_body, 436 sub_msg_info->message_size 437 - (uint32_t) sizeof(hv_vmbus_channel_msg_info)); 438 /* if (the post message failed) give up and clean up */ 439 if(ret != 0) 440 goto cleanup; 441 } 442 } 443 444 ret = sema_timedwait(&msg_info->wait_sema, 500); /* KYS 5 seconds*/ 445 if (ret != 0) 446 goto cleanup; 447 448 *gpadl_handle = gpadl_msg->gpadl; 449 450cleanup: 451 452 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 453 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor, 454 msg_info, msg_list_entry); 455 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 456 457 sema_destroy(&msg_info->wait_sema); 458 free(msg_info, M_DEVBUF); 459 460 return (ret); 461} 462 463/** 464 * @brief Teardown the specified GPADL handle 465 */ 466int 467hv_vmbus_channel_teardown_gpdal( 468 hv_vmbus_channel* channel, 469 uint32_t gpadl_handle) 470{ 471 int ret = 0; 472 hv_vmbus_channel_gpadl_teardown* msg; 473 hv_vmbus_channel_msg_info* info; 474 475 info = (hv_vmbus_channel_msg_info *) 476 malloc( sizeof(hv_vmbus_channel_msg_info) + 477 sizeof(hv_vmbus_channel_gpadl_teardown), 478 M_DEVBUF, M_NOWAIT); 479 KASSERT(info != NULL, 480 ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!")); 481 if (info == NULL) { 482 ret = ENOMEM; 483 goto cleanup; 484 } 485 486 sema_init(&info->wait_sema, 0, "Open Info Sema"); 487 488 msg = (hv_vmbus_channel_gpadl_teardown*) info->msg; 489 490 msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN; 491 msg->child_rel_id = channel->offer_msg.child_rel_id; 492 msg->gpadl = gpadl_handle; 493 494 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 495 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor, 496 info, msg_list_entry); 497 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 498 499 ret = hv_vmbus_post_message(msg, 500 sizeof(hv_vmbus_channel_gpadl_teardown)); 501 if (ret != 0) 502 goto cleanup; 503 504 ret = sema_timedwait(&info->wait_sema, 500); /* KYS 5 seconds */ 505 506cleanup: 507 /* 508 * Received a torndown response 509 */ 510 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 511 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor, 512 info, msg_list_entry); 513 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 514 sema_destroy(&info->wait_sema); 515 free(info, M_DEVBUF); 516 517 return (ret); 518} 519 520static void 521hv_vmbus_channel_close_internal(hv_vmbus_channel *channel) 522{ 523 int ret = 0; 524 struct taskqueue *rxq = channel->rxq; 525 hv_vmbus_channel_close_channel* msg; 526 hv_vmbus_channel_msg_info* info; 527 528 channel->state = HV_CHANNEL_OPEN_STATE; 529 channel->sc_creation_callback = NULL; 530 531 /* 532 * set rxq to NULL to avoid more requests be scheduled 533 */ 534 channel->rxq = NULL; 535 taskqueue_drain(rxq, &channel->channel_task); 536 /* 537 * Grab the lock to prevent race condition when a packet received 538 * and unloading driver is in the process. 539 */ 540 mtx_lock(&channel->inbound_lock); 541 channel->on_channel_callback = NULL; 542 mtx_unlock(&channel->inbound_lock); 543 544 /** 545 * Send a closing message 546 */ 547 info = (hv_vmbus_channel_msg_info *) 548 malloc( sizeof(hv_vmbus_channel_msg_info) + 549 sizeof(hv_vmbus_channel_close_channel), 550 M_DEVBUF, M_NOWAIT); 551 KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!")); 552 if(info == NULL) 553 return; 554 555 msg = (hv_vmbus_channel_close_channel*) info->msg; 556 msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL; 557 msg->child_rel_id = channel->offer_msg.child_rel_id; 558 559 ret = hv_vmbus_post_message( 560 msg, sizeof(hv_vmbus_channel_close_channel)); 561 562 /* Tear down the gpadl for the channel's ring buffer */ 563 if (channel->ring_buffer_gpadl_handle) { 564 hv_vmbus_channel_teardown_gpdal(channel, 565 channel->ring_buffer_gpadl_handle); 566 } 567 568 /* TODO: Send a msg to release the childRelId */ 569 570 /* cleanup the ring buffers for this channel */ 571 hv_ring_buffer_cleanup(&channel->outbound); 572 hv_ring_buffer_cleanup(&channel->inbound); 573 574 contigfree(channel->ring_buffer_pages, channel->ring_buffer_size, 575 M_DEVBUF); 576 577 free(info, M_DEVBUF); 578} 579 580/** 581 * @brief Close the specified channel 582 */ 583void 584hv_vmbus_channel_close(hv_vmbus_channel *channel) 585{ 586 hv_vmbus_channel* sub_channel; 587 588 if (channel->primary_channel != NULL) { 589 /* 590 * We only close multi-channels when the primary is 591 * closed. 592 */ 593 return; 594 } 595 596 /* 597 * Close all multi-channels first. 598 */ 599 TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor, 600 sc_list_entry) { 601 if (sub_channel->state != HV_CHANNEL_OPENED_STATE) 602 continue; 603 hv_vmbus_channel_close_internal(sub_channel); 604 } 605 /* 606 * Then close the primary channel. 607 */ 608 hv_vmbus_channel_close_internal(channel); 609} 610 611/** 612 * @brief Send the specified buffer on the given channel 613 */ 614int 615hv_vmbus_channel_send_packet( 616 hv_vmbus_channel* channel, 617 void* buffer, 618 uint32_t buffer_len, 619 uint64_t request_id, 620 hv_vmbus_packet_type type, 621 uint32_t flags) 622{ 623 int ret = 0; 624 hv_vm_packet_descriptor desc; 625 uint32_t packet_len; 626 uint64_t aligned_data; 627 uint32_t packet_len_aligned; 628 boolean_t need_sig; 629 hv_vmbus_sg_buffer_list buffer_list[3]; 630 631 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len; 632 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 633 aligned_data = 0; 634 635 /* Setup the descriptor */ 636 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */ 637 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */ 638 /* in 8-bytes granularity */ 639 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3; 640 desc.length8 = (uint16_t) (packet_len_aligned >> 3); 641 desc.transaction_id = request_id; 642 643 buffer_list[0].data = &desc; 644 buffer_list[0].length = sizeof(hv_vm_packet_descriptor); 645 646 buffer_list[1].data = buffer; 647 buffer_list[1].length = buffer_len; 648 649 buffer_list[2].data = &aligned_data; 650 buffer_list[2].length = packet_len_aligned - packet_len; 651 652 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3, 653 &need_sig); 654 655 /* TODO: We should determine if this is optional */ 656 if (ret == 0 && need_sig) { 657 vmbus_channel_set_event(channel); 658 } 659 660 return (ret); 661} 662 663/** 664 * @brief Send a range of single-page buffer packets using 665 * a GPADL Direct packet type 666 */ 667int 668hv_vmbus_channel_send_packet_pagebuffer( 669 hv_vmbus_channel* channel, 670 hv_vmbus_page_buffer page_buffers[], 671 uint32_t page_count, 672 void* buffer, 673 uint32_t buffer_len, 674 uint64_t request_id) 675{ 676 677 int ret = 0; 678 boolean_t need_sig; 679 uint32_t packet_len; 680 uint32_t page_buflen; 681 uint32_t packetLen_aligned; 682 hv_vmbus_sg_buffer_list buffer_list[4]; 683 hv_vmbus_channel_packet_page_buffer desc; 684 uint32_t descSize; 685 uint64_t alignedData = 0; 686 687 if (page_count > HV_MAX_PAGE_BUFFER_COUNT) 688 return (EINVAL); 689 690 /* 691 * Adjust the size down since hv_vmbus_channel_packet_page_buffer 692 * is the largest size we support 693 */ 694 descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range); 695 page_buflen = sizeof(hv_vmbus_page_buffer) * page_count; 696 packet_len = descSize + page_buflen + buffer_len; 697 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 698 699 /* Setup the descriptor */ 700 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 701 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 702 /* in 8-bytes granularity */ 703 desc.data_offset8 = (descSize + page_buflen) >> 3; 704 desc.length8 = (uint16_t) (packetLen_aligned >> 3); 705 desc.transaction_id = request_id; 706 desc.range_count = page_count; 707 708 buffer_list[0].data = &desc; 709 buffer_list[0].length = descSize; 710 711 buffer_list[1].data = page_buffers; 712 buffer_list[1].length = page_buflen; 713 714 buffer_list[2].data = buffer; 715 buffer_list[2].length = buffer_len; 716 717 buffer_list[3].data = &alignedData; 718 buffer_list[3].length = packetLen_aligned - packet_len; 719 720 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4, 721 &need_sig); 722 723 /* TODO: We should determine if this is optional */ 724 if (ret == 0 && need_sig) { 725 vmbus_channel_set_event(channel); 726 } 727 728 return (ret); 729} 730 731/** 732 * @brief Send a multi-page buffer packet using a GPADL Direct packet type 733 */ 734int 735hv_vmbus_channel_send_packet_multipagebuffer( 736 hv_vmbus_channel* channel, 737 hv_vmbus_multipage_buffer* multi_page_buffer, 738 void* buffer, 739 uint32_t buffer_len, 740 uint64_t request_id) 741{ 742 743 int ret = 0; 744 uint32_t desc_size; 745 boolean_t need_sig; 746 uint32_t packet_len; 747 uint32_t packet_len_aligned; 748 uint32_t pfn_count; 749 uint64_t aligned_data = 0; 750 hv_vmbus_sg_buffer_list buffer_list[3]; 751 hv_vmbus_channel_packet_multipage_buffer desc; 752 753 pfn_count = 754 HV_NUM_PAGES_SPANNED( 755 multi_page_buffer->offset, 756 multi_page_buffer->length); 757 758 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT)) 759 return (EINVAL); 760 /* 761 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer 762 * is the largest size we support 763 */ 764 desc_size = 765 sizeof(hv_vmbus_channel_packet_multipage_buffer) - 766 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) * 767 sizeof(uint64_t)); 768 packet_len = desc_size + buffer_len; 769 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 770 771 /* 772 * Setup the descriptor 773 */ 774 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 775 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 776 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */ 777 desc.length8 = (uint16_t) (packet_len_aligned >> 3); 778 desc.transaction_id = request_id; 779 desc.range_count = 1; 780 781 desc.range.length = multi_page_buffer->length; 782 desc.range.offset = multi_page_buffer->offset; 783 784 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array, 785 pfn_count * sizeof(uint64_t)); 786 787 buffer_list[0].data = &desc; 788 buffer_list[0].length = desc_size; 789 790 buffer_list[1].data = buffer; 791 buffer_list[1].length = buffer_len; 792 793 buffer_list[2].data = &aligned_data; 794 buffer_list[2].length = packet_len_aligned - packet_len; 795 796 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3, 797 &need_sig); 798 799 /* TODO: We should determine if this is optional */ 800 if (ret == 0 && need_sig) { 801 vmbus_channel_set_event(channel); 802 } 803 804 return (ret); 805} 806 807/** 808 * @brief Retrieve the user packet on the specified channel 809 */ 810int 811hv_vmbus_channel_recv_packet( 812 hv_vmbus_channel* channel, 813 void* Buffer, 814 uint32_t buffer_len, 815 uint32_t* buffer_actual_len, 816 uint64_t* request_id) 817{ 818 int ret; 819 uint32_t user_len; 820 uint32_t packet_len; 821 hv_vm_packet_descriptor desc; 822 823 *buffer_actual_len = 0; 824 *request_id = 0; 825 826 ret = hv_ring_buffer_peek(&channel->inbound, &desc, 827 sizeof(hv_vm_packet_descriptor)); 828 if (ret != 0) 829 return (0); 830 831 packet_len = desc.length8 << 3; 832 user_len = packet_len - (desc.data_offset8 << 3); 833 834 *buffer_actual_len = user_len; 835 836 if (user_len > buffer_len) 837 return (EINVAL); 838 839 *request_id = desc.transaction_id; 840 841 /* Copy over the packet to the user buffer */ 842 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len, 843 (desc.data_offset8 << 3)); 844 845 return (0); 846} 847 848/** 849 * @brief Retrieve the raw packet on the specified channel 850 */ 851int 852hv_vmbus_channel_recv_packet_raw( 853 hv_vmbus_channel* channel, 854 void* buffer, 855 uint32_t buffer_len, 856 uint32_t* buffer_actual_len, 857 uint64_t* request_id) 858{ 859 int ret; 860 uint32_t packetLen; 861 uint32_t userLen; 862 hv_vm_packet_descriptor desc; 863 864 *buffer_actual_len = 0; 865 *request_id = 0; 866 867 ret = hv_ring_buffer_peek( 868 &channel->inbound, &desc, 869 sizeof(hv_vm_packet_descriptor)); 870 871 if (ret != 0) 872 return (0); 873 874 packetLen = desc.length8 << 3; 875 userLen = packetLen - (desc.data_offset8 << 3); 876 877 *buffer_actual_len = packetLen; 878 879 if (packetLen > buffer_len) 880 return (ENOBUFS); 881 882 *request_id = desc.transaction_id; 883 884 /* Copy over the entire packet to the user buffer */ 885 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0); 886 887 return (0); 888} 889 890 891/** 892 * Process a channel event notification 893 */ 894static void 895VmbusProcessChannelEvent(void* context, int pending) 896{ 897 void* arg; 898 uint32_t bytes_to_read; 899 hv_vmbus_channel* channel = (hv_vmbus_channel*)context; 900 boolean_t is_batched_reading; 901 902 /** 903 * Find the channel based on this relid and invokes 904 * the channel callback to process the event 905 */ 906 907 if (channel == NULL) { 908 return; 909 } 910 /** 911 * To deal with the race condition where we might 912 * receive a packet while the relevant driver is 913 * being unloaded, dispatch the callback while 914 * holding the channel lock. The unloading driver 915 * will acquire the same channel lock to set the 916 * callback to NULL. This closes the window. 917 */ 918 919 /* 920 * Disable the lock due to newly added WITNESS check in r277723. 921 * Will seek other way to avoid race condition. 922 * -- whu 923 */ 924 // mtx_lock(&channel->inbound_lock); 925 if (channel->on_channel_callback != NULL) { 926 arg = channel->channel_callback_context; 927 is_batched_reading = channel->batched_reading; 928 /* 929 * Optimize host to guest signaling by ensuring: 930 * 1. While reading the channel, we disable interrupts from 931 * host. 932 * 2. Ensure that we process all posted messages from the host 933 * before returning from this callback. 934 * 3. Once we return, enable signaling from the host. Once this 935 * state is set we check to see if additional packets are 936 * available to read. In this case we repeat the process. 937 */ 938 do { 939 if (is_batched_reading) 940 hv_ring_buffer_read_begin(&channel->inbound); 941 942 channel->on_channel_callback(arg); 943 944 if (is_batched_reading) 945 bytes_to_read = 946 hv_ring_buffer_read_end(&channel->inbound); 947 else 948 bytes_to_read = 0; 949 } while (is_batched_reading && (bytes_to_read != 0)); 950 } 951 // mtx_unlock(&channel->inbound_lock); 952} 953