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