vmbus_chan.c revision 256276
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 256276 2013-10-10 16:25:53Z dim $"); 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); 54 55/** 56 * @brief Trigger an event notification on the specified channel 57 */ 58static void 59vmbus_channel_set_event(hv_vmbus_channel *channel) 60{ 61 hv_vmbus_monitor_page *monitor_page; 62 63 if (channel->offer_msg.monitor_allocated) { 64 /* Each uint32_t represents 32 channels */ 65 synch_set_bit((channel->offer_msg.child_rel_id & 31), 66 ((uint32_t *)hv_vmbus_g_connection.send_interrupt_page 67 + ((channel->offer_msg.child_rel_id >> 5)))); 68 69 monitor_page = (hv_vmbus_monitor_page *) 70 hv_vmbus_g_connection.monitor_pages; 71 72 monitor_page++; /* Get the child to parent monitor page */ 73 74 synch_set_bit(channel->monitor_bit, 75 (uint32_t *)&monitor_page-> 76 trigger_group[channel->monitor_group].u.pending); 77 } else { 78 hv_vmbus_set_event(channel->offer_msg.child_rel_id); 79 } 80 81} 82 83/** 84 * @brief Open the specified channel 85 */ 86int 87hv_vmbus_channel_open( 88 hv_vmbus_channel* new_channel, 89 uint32_t send_ring_buffer_size, 90 uint32_t recv_ring_buffer_size, 91 void* user_data, 92 uint32_t user_data_len, 93 hv_vmbus_pfn_channel_callback pfn_on_channel_callback, 94 void* context) 95{ 96 97 int ret = 0; 98 void *in, *out; 99 hv_vmbus_channel_open_channel* open_msg; 100 hv_vmbus_channel_msg_info* open_info; 101 102 new_channel->on_channel_callback = pfn_on_channel_callback; 103 new_channel->channel_callback_context = context; 104 105 /* Allocate the ring buffer */ 106 out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size), 107 M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0); 108 KASSERT(out != NULL, 109 ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!")); 110 if (out == NULL) 111 return (ENOMEM); 112 113 in = ((uint8_t *) out + send_ring_buffer_size); 114 115 new_channel->ring_buffer_pages = out; 116 new_channel->ring_buffer_page_count = (send_ring_buffer_size 117 + recv_ring_buffer_size) >> PAGE_SHIFT; 118 119 hv_vmbus_ring_buffer_init( 120 &new_channel->outbound, 121 out, 122 send_ring_buffer_size); 123 124 hv_vmbus_ring_buffer_init( 125 &new_channel->inbound, 126 in, 127 recv_ring_buffer_size); 128 129 /** 130 * Establish the gpadl for the ring buffer 131 */ 132 new_channel->ring_buffer_gpadl_handle = 0; 133 134 ret = hv_vmbus_channel_establish_gpadl(new_channel, 135 new_channel->outbound.ring_buffer, 136 send_ring_buffer_size + recv_ring_buffer_size, 137 &new_channel->ring_buffer_gpadl_handle); 138 139 /** 140 * Create and init the channel open message 141 */ 142 open_info = (hv_vmbus_channel_msg_info*) malloc( 143 sizeof(hv_vmbus_channel_msg_info) + 144 sizeof(hv_vmbus_channel_open_channel), 145 M_DEVBUF, 146 M_NOWAIT); 147 KASSERT(open_info != NULL, 148 ("Error VMBUS: malloc failed to allocate Open Channel message!")); 149 150 if (open_info == NULL) 151 return (ENOMEM); 152 153 sema_init(&open_info->wait_sema, 0, "Open Info Sema"); 154 155 open_msg = (hv_vmbus_channel_open_channel*) open_info->msg; 156 open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL; 157 open_msg->open_id = new_channel->offer_msg.child_rel_id; 158 open_msg->child_rel_id = new_channel->offer_msg.child_rel_id; 159 open_msg->ring_buffer_gpadl_handle = 160 new_channel->ring_buffer_gpadl_handle; 161 open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size 162 >> PAGE_SHIFT; 163 open_msg->server_context_area_gpadl_handle = 0; 164 165 if (user_data_len) 166 memcpy(open_msg->user_data, user_data, user_data_len); 167 168 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 169 TAILQ_INSERT_TAIL( 170 &hv_vmbus_g_connection.channel_msg_anchor, 171 open_info, 172 msg_list_entry); 173 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 174 175 ret = hv_vmbus_post_message( 176 open_msg, sizeof(hv_vmbus_channel_open_channel)); 177 178 if (ret != 0) 179 goto cleanup; 180 181 ret = sema_timedwait(&open_info->wait_sema, 500); /* KYS 5 seconds */ 182 183 if (ret) 184 goto cleanup; 185 186 if (open_info->response.open_result.status == 0) { 187 if(bootverbose) 188 printf("VMBUS: channel <%p> open success.\n", new_channel); 189 } else { 190 if(bootverbose) 191 printf("Error VMBUS: channel <%p> open failed - %d!\n", 192 new_channel, open_info->response.open_result.status); 193 } 194 195 cleanup: 196 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 197 TAILQ_REMOVE( 198 &hv_vmbus_g_connection.channel_msg_anchor, 199 open_info, 200 msg_list_entry); 201 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 202 sema_destroy(&open_info->wait_sema); 203 free(open_info, M_DEVBUF); 204 205 return (ret); 206} 207 208/** 209 * @brief Create a gpadl for the specified buffer 210 */ 211static int 212vmbus_channel_create_gpadl_header( 213 void* contig_buffer, 214 uint32_t size, /* page-size multiple */ 215 hv_vmbus_channel_msg_info** msg_info, 216 uint32_t* message_count) 217{ 218 int i; 219 int page_count; 220 unsigned long long pfn; 221 uint32_t msg_size; 222 hv_vmbus_channel_gpadl_header* gpa_header; 223 hv_vmbus_channel_gpadl_body* gpadl_body; 224 hv_vmbus_channel_msg_info* msg_header; 225 hv_vmbus_channel_msg_info* msg_body; 226 227 int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize; 228 229 page_count = size >> PAGE_SHIFT; 230 pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT; 231 232 /*do we need a gpadl body msg */ 233 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE 234 - sizeof(hv_vmbus_channel_gpadl_header) 235 - sizeof(hv_gpa_range); 236 pfnCount = pfnSize / sizeof(uint64_t); 237 238 if (page_count > pfnCount) { /* if(we need a gpadl body) */ 239 /* fill in the header */ 240 msg_size = sizeof(hv_vmbus_channel_msg_info) 241 + sizeof(hv_vmbus_channel_gpadl_header) 242 + sizeof(hv_gpa_range) 243 + pfnCount * sizeof(uint64_t); 244 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO); 245 KASSERT( 246 msg_header != NULL, 247 ("Error VMBUS: malloc failed to allocate Gpadl Message!")); 248 if (msg_header == NULL) 249 return (ENOMEM); 250 251 TAILQ_INIT(&msg_header->sub_msg_list_anchor); 252 msg_header->message_size = msg_size; 253 254 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg; 255 gpa_header->range_count = 1; 256 gpa_header->range_buf_len = sizeof(hv_gpa_range) 257 + page_count * sizeof(uint64_t); 258 gpa_header->range[0].byte_offset = 0; 259 gpa_header->range[0].byte_count = size; 260 for (i = 0; i < pfnCount; i++) { 261 gpa_header->range[0].pfn_array[i] = pfn + i; 262 } 263 *msg_info = msg_header; 264 *message_count = 1; 265 266 pfnSum = pfnCount; 267 pfnLeft = page_count - pfnCount; 268 269 /* 270 * figure out how many pfns we can fit 271 */ 272 pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE 273 - sizeof(hv_vmbus_channel_gpadl_body); 274 pfnCount = pfnSize / sizeof(uint64_t); 275 276 /* 277 * fill in the body 278 */ 279 while (pfnLeft) { 280 if (pfnLeft > pfnCount) { 281 pfnCurr = pfnCount; 282 } else { 283 pfnCurr = pfnLeft; 284 } 285 286 msg_size = sizeof(hv_vmbus_channel_msg_info) + 287 sizeof(hv_vmbus_channel_gpadl_body) + 288 pfnCurr * sizeof(uint64_t); 289 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO); 290 KASSERT( 291 msg_body != NULL, 292 ("Error VMBUS: malloc failed to allocate Gpadl msg_body!")); 293 if (msg_body == NULL) 294 return (ENOMEM); 295 296 msg_body->message_size = msg_size; 297 (*message_count)++; 298 gpadl_body = 299 (hv_vmbus_channel_gpadl_body*) msg_body->msg; 300 /* 301 * gpadl_body->gpadl = kbuffer; 302 */ 303 for (i = 0; i < pfnCurr; i++) { 304 gpadl_body->pfn[i] = pfn + pfnSum + i; 305 } 306 307 TAILQ_INSERT_TAIL( 308 &msg_header->sub_msg_list_anchor, 309 msg_body, 310 msg_list_entry); 311 pfnSum += pfnCurr; 312 pfnLeft -= pfnCurr; 313 } 314 } else { /* else everything fits in a header */ 315 316 msg_size = sizeof(hv_vmbus_channel_msg_info) + 317 sizeof(hv_vmbus_channel_gpadl_header) + 318 sizeof(hv_gpa_range) + 319 page_count * sizeof(uint64_t); 320 msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO); 321 KASSERT( 322 msg_header != NULL, 323 ("Error VMBUS: malloc failed to allocate Gpadl Message!")); 324 if (msg_header == NULL) 325 return (ENOMEM); 326 327 msg_header->message_size = msg_size; 328 329 gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg; 330 gpa_header->range_count = 1; 331 gpa_header->range_buf_len = sizeof(hv_gpa_range) + 332 page_count * sizeof(uint64_t); 333 gpa_header->range[0].byte_offset = 0; 334 gpa_header->range[0].byte_count = size; 335 for (i = 0; i < page_count; i++) { 336 gpa_header->range[0].pfn_array[i] = pfn + i; 337 } 338 339 *msg_info = msg_header; 340 *message_count = 1; 341 } 342 343 return (0); 344} 345 346/** 347 * @brief Establish a GPADL for the specified buffer 348 */ 349int 350hv_vmbus_channel_establish_gpadl( 351 hv_vmbus_channel* channel, 352 void* contig_buffer, 353 uint32_t size, /* page-size multiple */ 354 uint32_t* gpadl_handle) 355 356{ 357 int ret = 0; 358 hv_vmbus_channel_gpadl_header* gpadl_msg; 359 hv_vmbus_channel_gpadl_body* gpadl_body; 360 hv_vmbus_channel_msg_info* msg_info; 361 hv_vmbus_channel_msg_info* sub_msg_info; 362 uint32_t msg_count; 363 hv_vmbus_channel_msg_info* curr; 364 uint32_t next_gpadl_handle; 365 366 next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle; 367 atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1); 368 369 ret = vmbus_channel_create_gpadl_header( 370 contig_buffer, size, &msg_info, &msg_count); 371 372 if(ret != 0) { /* if(allocation failed) return immediately */ 373 /* reverse atomic_add_int above */ 374 atomic_subtract_int((int*) 375 &hv_vmbus_g_connection.next_gpadl_handle, 1); 376 return ret; 377 } 378 379 sema_init(&msg_info->wait_sema, 0, "Open Info Sema"); 380 gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg; 381 gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER; 382 gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id; 383 gpadl_msg->gpadl = next_gpadl_handle; 384 385 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 386 TAILQ_INSERT_TAIL( 387 &hv_vmbus_g_connection.channel_msg_anchor, 388 msg_info, 389 msg_list_entry); 390 391 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 392 393 ret = hv_vmbus_post_message( 394 gpadl_msg, 395 msg_info->message_size - 396 (uint32_t) sizeof(hv_vmbus_channel_msg_info)); 397 398 if (ret != 0) 399 goto cleanup; 400 401 if (msg_count > 1) { 402 TAILQ_FOREACH(curr, 403 &msg_info->sub_msg_list_anchor, msg_list_entry) { 404 sub_msg_info = curr; 405 gpadl_body = 406 (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg; 407 408 gpadl_body->header.message_type = 409 HV_CHANNEL_MESSAGE_GPADL_BODY; 410 gpadl_body->gpadl = next_gpadl_handle; 411 412 ret = hv_vmbus_post_message( 413 gpadl_body, 414 sub_msg_info->message_size 415 - (uint32_t) sizeof(hv_vmbus_channel_msg_info)); 416 /* if (the post message failed) give up and clean up */ 417 if(ret != 0) 418 goto cleanup; 419 } 420 } 421 422 ret = sema_timedwait(&msg_info->wait_sema, 500); /* KYS 5 seconds*/ 423 if (ret != 0) 424 goto cleanup; 425 426 *gpadl_handle = gpadl_msg->gpadl; 427 428cleanup: 429 430 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 431 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor, 432 msg_info, msg_list_entry); 433 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 434 435 sema_destroy(&msg_info->wait_sema); 436 free(msg_info, M_DEVBUF); 437 438 return (ret); 439} 440 441/** 442 * @brief Teardown the specified GPADL handle 443 */ 444int 445hv_vmbus_channel_teardown_gpdal( 446 hv_vmbus_channel* channel, 447 uint32_t gpadl_handle) 448{ 449 int ret = 0; 450 hv_vmbus_channel_gpadl_teardown* msg; 451 hv_vmbus_channel_msg_info* info; 452 453 info = (hv_vmbus_channel_msg_info *) 454 malloc( sizeof(hv_vmbus_channel_msg_info) + 455 sizeof(hv_vmbus_channel_gpadl_teardown), 456 M_DEVBUF, M_NOWAIT); 457 KASSERT(info != NULL, 458 ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!")); 459 if (info == NULL) { 460 ret = ENOMEM; 461 goto cleanup; 462 } 463 464 sema_init(&info->wait_sema, 0, "Open Info Sema"); 465 466 msg = (hv_vmbus_channel_gpadl_teardown*) info->msg; 467 468 msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN; 469 msg->child_rel_id = channel->offer_msg.child_rel_id; 470 msg->gpadl = gpadl_handle; 471 472 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 473 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor, 474 info, msg_list_entry); 475 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 476 477 ret = hv_vmbus_post_message(msg, 478 sizeof(hv_vmbus_channel_gpadl_teardown)); 479 if (ret != 0) 480 goto cleanup; 481 482 ret = sema_timedwait(&info->wait_sema, 500); /* KYS 5 seconds */ 483 484cleanup: 485 /* 486 * Received a torndown response 487 */ 488 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock); 489 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor, 490 info, msg_list_entry); 491 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock); 492 sema_destroy(&info->wait_sema); 493 free(info, M_DEVBUF); 494 495 return (ret); 496} 497 498/** 499 * @brief Close the specified channel 500 */ 501void 502hv_vmbus_channel_close(hv_vmbus_channel *channel) 503{ 504 int ret = 0; 505 hv_vmbus_channel_close_channel* msg; 506 hv_vmbus_channel_msg_info* info; 507 508 mtx_lock(&channel->inbound_lock); 509 channel->on_channel_callback = NULL; 510 mtx_unlock(&channel->inbound_lock); 511 512 /** 513 * Send a closing message 514 */ 515 info = (hv_vmbus_channel_msg_info *) 516 malloc( sizeof(hv_vmbus_channel_msg_info) + 517 sizeof(hv_vmbus_channel_close_channel), 518 M_DEVBUF, M_NOWAIT); 519 KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!")); 520 if(info == NULL) 521 return; 522 523 msg = (hv_vmbus_channel_close_channel*) info->msg; 524 msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL; 525 msg->child_rel_id = channel->offer_msg.child_rel_id; 526 527 ret = hv_vmbus_post_message( 528 msg, sizeof(hv_vmbus_channel_close_channel)); 529 530 /* Tear down the gpadl for the channel's ring buffer */ 531 if (channel->ring_buffer_gpadl_handle) { 532 hv_vmbus_channel_teardown_gpdal(channel, 533 channel->ring_buffer_gpadl_handle); 534 } 535 536 /* TODO: Send a msg to release the childRelId */ 537 538 /* cleanup the ring buffers for this channel */ 539 hv_ring_buffer_cleanup(&channel->outbound); 540 hv_ring_buffer_cleanup(&channel->inbound); 541 542 contigfree( 543 channel->ring_buffer_pages, 544 channel->ring_buffer_page_count, 545 M_DEVBUF); 546 547 free(info, M_DEVBUF); 548 549 /* 550 * If we are closing the channel during an error path in 551 * opening the channel, don't free the channel 552 * since the caller will free the channel 553 */ 554 if (channel->state == HV_CHANNEL_OPEN_STATE) { 555 mtx_lock_spin(&hv_vmbus_g_connection.channel_lock); 556 TAILQ_REMOVE( 557 &hv_vmbus_g_connection.channel_anchor, 558 channel, 559 list_entry); 560 mtx_unlock_spin(&hv_vmbus_g_connection.channel_lock); 561 562 hv_vmbus_free_vmbus_channel(channel); 563 } 564 565} 566 567/** 568 * @brief Send the specified buffer on the given channel 569 */ 570int 571hv_vmbus_channel_send_packet( 572 hv_vmbus_channel* channel, 573 void* buffer, 574 uint32_t buffer_len, 575 uint64_t request_id, 576 hv_vmbus_packet_type type, 577 uint32_t flags) 578{ 579 int ret = 0; 580 hv_vm_packet_descriptor desc; 581 uint32_t packet_len; 582 uint64_t aligned_data; 583 uint32_t packet_len_aligned; 584 hv_vmbus_sg_buffer_list buffer_list[3]; 585 586 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len; 587 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 588 aligned_data = 0; 589 590 /* Setup the descriptor */ 591 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */ 592 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */ 593 /* in 8-bytes granularity */ 594 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3; 595 desc.length8 = (uint16_t) (packet_len_aligned >> 3); 596 desc.transaction_id = request_id; 597 598 buffer_list[0].data = &desc; 599 buffer_list[0].length = sizeof(hv_vm_packet_descriptor); 600 601 buffer_list[1].data = buffer; 602 buffer_list[1].length = buffer_len; 603 604 buffer_list[2].data = &aligned_data; 605 buffer_list[2].length = packet_len_aligned - packet_len; 606 607 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3); 608 609 /* TODO: We should determine if this is optional */ 610 if (ret == 0 611 && !hv_vmbus_get_ring_buffer_interrupt_mask( 612 &channel->outbound)) { 613 vmbus_channel_set_event(channel); 614 } 615 616 return (ret); 617} 618 619/** 620 * @brief Send a range of single-page buffer packets using 621 * a GPADL Direct packet type 622 */ 623int 624hv_vmbus_channel_send_packet_pagebuffer( 625 hv_vmbus_channel* channel, 626 hv_vmbus_page_buffer page_buffers[], 627 uint32_t page_count, 628 void* buffer, 629 uint32_t buffer_len, 630 uint64_t request_id) 631{ 632 633 int ret = 0; 634 int i = 0; 635 uint32_t packet_len; 636 uint32_t packetLen_aligned; 637 hv_vmbus_sg_buffer_list buffer_list[3]; 638 hv_vmbus_channel_packet_page_buffer desc; 639 uint32_t descSize; 640 uint64_t alignedData = 0; 641 642 if (page_count > HV_MAX_PAGE_BUFFER_COUNT) 643 return (EINVAL); 644 645 /* 646 * Adjust the size down since hv_vmbus_channel_packet_page_buffer 647 * is the largest size we support 648 */ 649 descSize = sizeof(hv_vmbus_channel_packet_page_buffer) - 650 ((HV_MAX_PAGE_BUFFER_COUNT - page_count) * 651 sizeof(hv_vmbus_page_buffer)); 652 packet_len = descSize + buffer_len; 653 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 654 655 /* Setup the descriptor */ 656 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 657 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 658 desc.data_offset8 = descSize >> 3; /* in 8-bytes granularity */ 659 desc.length8 = (uint16_t) (packetLen_aligned >> 3); 660 desc.transaction_id = request_id; 661 desc.range_count = page_count; 662 663 for (i = 0; i < page_count; i++) { 664 desc.range[i].length = page_buffers[i].length; 665 desc.range[i].offset = page_buffers[i].offset; 666 desc.range[i].pfn = page_buffers[i].pfn; 667 } 668 669 buffer_list[0].data = &desc; 670 buffer_list[0].length = descSize; 671 672 buffer_list[1].data = buffer; 673 buffer_list[1].length = buffer_len; 674 675 buffer_list[2].data = &alignedData; 676 buffer_list[2].length = packetLen_aligned - packet_len; 677 678 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3); 679 680 /* TODO: We should determine if this is optional */ 681 if (ret == 0 && 682 !hv_vmbus_get_ring_buffer_interrupt_mask(&channel->outbound)) { 683 vmbus_channel_set_event(channel); 684 } 685 686 return (ret); 687} 688 689/** 690 * @brief Send a multi-page buffer packet using a GPADL Direct packet type 691 */ 692int 693hv_vmbus_channel_send_packet_multipagebuffer( 694 hv_vmbus_channel* channel, 695 hv_vmbus_multipage_buffer* multi_page_buffer, 696 void* buffer, 697 uint32_t buffer_len, 698 uint64_t request_id) 699{ 700 701 int ret = 0; 702 uint32_t desc_size; 703 uint32_t packet_len; 704 uint32_t packet_len_aligned; 705 uint32_t pfn_count; 706 uint64_t aligned_data = 0; 707 hv_vmbus_sg_buffer_list buffer_list[3]; 708 hv_vmbus_channel_packet_multipage_buffer desc; 709 710 pfn_count = 711 HV_NUM_PAGES_SPANNED( 712 multi_page_buffer->offset, 713 multi_page_buffer->length); 714 715 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT)) 716 return (EINVAL); 717 /* 718 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer 719 * is the largest size we support 720 */ 721 desc_size = 722 sizeof(hv_vmbus_channel_packet_multipage_buffer) - 723 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) * 724 sizeof(uint64_t)); 725 packet_len = desc_size + buffer_len; 726 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 727 728 /* 729 * Setup the descriptor 730 */ 731 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 732 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 733 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */ 734 desc.length8 = (uint16_t) (packet_len_aligned >> 3); 735 desc.transaction_id = request_id; 736 desc.range_count = 1; 737 738 desc.range.length = multi_page_buffer->length; 739 desc.range.offset = multi_page_buffer->offset; 740 741 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array, 742 pfn_count * sizeof(uint64_t)); 743 744 buffer_list[0].data = &desc; 745 buffer_list[0].length = desc_size; 746 747 buffer_list[1].data = buffer; 748 buffer_list[1].length = buffer_len; 749 750 buffer_list[2].data = &aligned_data; 751 buffer_list[2].length = packet_len_aligned - packet_len; 752 753 ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3); 754 755 /* TODO: We should determine if this is optional */ 756 if (ret == 0 && 757 !hv_vmbus_get_ring_buffer_interrupt_mask(&channel->outbound)) { 758 vmbus_channel_set_event(channel); 759 } 760 761 return (ret); 762} 763 764/** 765 * @brief Retrieve the user packet on the specified channel 766 */ 767int 768hv_vmbus_channel_recv_packet( 769 hv_vmbus_channel* channel, 770 void* Buffer, 771 uint32_t buffer_len, 772 uint32_t* buffer_actual_len, 773 uint64_t* request_id) 774{ 775 int ret; 776 uint32_t user_len; 777 uint32_t packet_len; 778 hv_vm_packet_descriptor desc; 779 780 *buffer_actual_len = 0; 781 *request_id = 0; 782 783 ret = hv_ring_buffer_peek(&channel->inbound, &desc, 784 sizeof(hv_vm_packet_descriptor)); 785 if (ret != 0) 786 return (0); 787 788 packet_len = desc.length8 << 3; 789 user_len = packet_len - (desc.data_offset8 << 3); 790 791 *buffer_actual_len = user_len; 792 793 if (user_len > buffer_len) 794 return (EINVAL); 795 796 *request_id = desc.transaction_id; 797 798 /* Copy over the packet to the user buffer */ 799 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len, 800 (desc.data_offset8 << 3)); 801 802 return (0); 803} 804 805/** 806 * @brief Retrieve the raw packet on the specified channel 807 */ 808int 809hv_vmbus_channel_recv_packet_raw( 810 hv_vmbus_channel* channel, 811 void* buffer, 812 uint32_t buffer_len, 813 uint32_t* buffer_actual_len, 814 uint64_t* request_id) 815{ 816 int ret; 817 uint32_t packetLen; 818 uint32_t userLen; 819 hv_vm_packet_descriptor desc; 820 821 *buffer_actual_len = 0; 822 *request_id = 0; 823 824 ret = hv_ring_buffer_peek( 825 &channel->inbound, &desc, 826 sizeof(hv_vm_packet_descriptor)); 827 828 if (ret != 0) 829 return (0); 830 831 packetLen = desc.length8 << 3; 832 userLen = packetLen - (desc.data_offset8 << 3); 833 834 *buffer_actual_len = packetLen; 835 836 if (packetLen > buffer_len) 837 return (ENOBUFS); 838 839 *request_id = desc.transaction_id; 840 841 /* Copy over the entire packet to the user buffer */ 842 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0); 843 844 return (0); 845} 846