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