vmbus_chan.c revision 302874
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 302874 2016-07-15 06:08:48Z 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/include/hyperv_busdma.h> 49#include <dev/hyperv/vmbus/hv_vmbus_priv.h> 50#include <dev/hyperv/vmbus/hyperv_var.h> 51#include <dev/hyperv/vmbus/vmbus_reg.h> 52#include <dev/hyperv/vmbus/vmbus_var.h> 53 54static void vmbus_chan_send_event(hv_vmbus_channel* channel); 55static void vmbus_chan_update_evtflagcnt(struct vmbus_softc *, 56 const struct hv_vmbus_channel *); 57 58static void vmbus_chan_task(void *, int); 59static void vmbus_chan_task_nobatch(void *, int); 60static void vmbus_chan_detach_task(void *, int); 61 62static void vmbus_chan_msgproc_choffer(struct vmbus_softc *, 63 const struct vmbus_message *); 64static void vmbus_chan_msgproc_chrescind(struct vmbus_softc *, 65 const struct vmbus_message *); 66 67/* 68 * Vmbus channel message processing. 69 */ 70static const vmbus_chanmsg_proc_t 71vmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = { 72 VMBUS_CHANMSG_PROC(CHOFFER, vmbus_chan_msgproc_choffer), 73 VMBUS_CHANMSG_PROC(CHRESCIND, vmbus_chan_msgproc_chrescind), 74 75 VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP), 76 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP), 77 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP) 78}; 79 80/** 81 * @brief Trigger an event notification on the specified channel 82 */ 83static void 84vmbus_chan_send_event(hv_vmbus_channel *channel) 85{ 86 struct vmbus_softc *sc = channel->vmbus_sc; 87 uint32_t chanid = channel->ch_id; 88 89 atomic_set_long(&sc->vmbus_tx_evtflags[chanid >> VMBUS_EVTFLAG_SHIFT], 90 1UL << (chanid & VMBUS_EVTFLAG_MASK)); 91 92 if (channel->ch_flags & VMBUS_CHAN_FLAG_HASMNF) { 93 atomic_set_int( 94 &sc->vmbus_mnf2->mnf_trigs[channel->ch_montrig_idx].mt_pending, 95 channel->ch_montrig_mask); 96 } else { 97 hypercall_signal_event(channel->ch_monprm_dma.hv_paddr); 98 } 99} 100 101static int 102vmbus_channel_sysctl_monalloc(SYSCTL_HANDLER_ARGS) 103{ 104 struct hv_vmbus_channel *chan = arg1; 105 int alloc = 0; 106 107 if (chan->ch_flags & VMBUS_CHAN_FLAG_HASMNF) 108 alloc = 1; 109 return sysctl_handle_int(oidp, &alloc, 0, req); 110} 111 112static void 113vmbus_channel_sysctl_create(hv_vmbus_channel* channel) 114{ 115 device_t dev; 116 struct sysctl_oid *devch_sysctl; 117 struct sysctl_oid *devch_id_sysctl, *devch_sub_sysctl; 118 struct sysctl_oid *devch_id_in_sysctl, *devch_id_out_sysctl; 119 struct sysctl_ctx_list *ctx; 120 uint32_t ch_id; 121 uint16_t sub_ch_id; 122 char name[16]; 123 124 hv_vmbus_channel* primary_ch = channel->ch_prichan; 125 126 if (primary_ch == NULL) { 127 dev = channel->ch_dev; 128 ch_id = channel->ch_id; 129 } else { 130 dev = primary_ch->ch_dev; 131 ch_id = primary_ch->ch_id; 132 sub_ch_id = channel->ch_subidx; 133 } 134 ctx = &channel->ch_sysctl_ctx; 135 sysctl_ctx_init(ctx); 136 /* This creates dev.DEVNAME.DEVUNIT.channel tree */ 137 devch_sysctl = SYSCTL_ADD_NODE(ctx, 138 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 139 OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 140 /* This creates dev.DEVNAME.DEVUNIT.channel.CHANID tree */ 141 snprintf(name, sizeof(name), "%d", ch_id); 142 devch_id_sysctl = SYSCTL_ADD_NODE(ctx, 143 SYSCTL_CHILDREN(devch_sysctl), 144 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 145 146 if (primary_ch != NULL) { 147 devch_sub_sysctl = SYSCTL_ADD_NODE(ctx, 148 SYSCTL_CHILDREN(devch_id_sysctl), 149 OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 150 snprintf(name, sizeof(name), "%d", sub_ch_id); 151 devch_id_sysctl = SYSCTL_ADD_NODE(ctx, 152 SYSCTL_CHILDREN(devch_sub_sysctl), 153 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 154 155 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), 156 OID_AUTO, "chanid", CTLFLAG_RD, 157 &channel->ch_id, 0, "channel id"); 158 } 159 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO, 160 "cpu", CTLFLAG_RD, &channel->ch_cpuid, 0, "owner CPU id"); 161 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(devch_id_sysctl), OID_AUTO, 162 "monitor_allocated", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 163 channel, 0, vmbus_channel_sysctl_monalloc, "I", 164 "is monitor allocated to this channel"); 165 166 devch_id_in_sysctl = SYSCTL_ADD_NODE(ctx, 167 SYSCTL_CHILDREN(devch_id_sysctl), 168 OID_AUTO, 169 "in", 170 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 171 devch_id_out_sysctl = SYSCTL_ADD_NODE(ctx, 172 SYSCTL_CHILDREN(devch_id_sysctl), 173 OID_AUTO, 174 "out", 175 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 176 hv_ring_buffer_stat(ctx, 177 SYSCTL_CHILDREN(devch_id_in_sysctl), 178 &(channel->inbound), 179 "inbound ring buffer stats"); 180 hv_ring_buffer_stat(ctx, 181 SYSCTL_CHILDREN(devch_id_out_sysctl), 182 &(channel->outbound), 183 "outbound ring buffer stats"); 184} 185 186/** 187 * @brief Open the specified channel 188 */ 189int 190hv_vmbus_channel_open( 191 hv_vmbus_channel* new_channel, 192 uint32_t send_ring_buffer_size, 193 uint32_t recv_ring_buffer_size, 194 void* user_data, 195 uint32_t user_data_len, 196 vmbus_chan_callback_t cb, 197 void *cbarg) 198{ 199 struct vmbus_softc *sc = new_channel->vmbus_sc; 200 const struct vmbus_chanmsg_chopen_resp *resp; 201 const struct vmbus_message *msg; 202 struct vmbus_chanmsg_chopen *req; 203 struct vmbus_msghc *mh; 204 uint32_t status; 205 int ret = 0; 206 uint8_t *br; 207 208 if (user_data_len > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) { 209 device_printf(sc->vmbus_dev, 210 "invalid udata len %u for chan%u\n", 211 user_data_len, new_channel->ch_id); 212 return EINVAL; 213 } 214 KASSERT((send_ring_buffer_size & PAGE_MASK) == 0, 215 ("send bufring size is not multiple page")); 216 KASSERT((recv_ring_buffer_size & PAGE_MASK) == 0, 217 ("recv bufring size is not multiple page")); 218 219 if (atomic_testandset_int(&new_channel->ch_stflags, 220 VMBUS_CHAN_ST_OPENED_SHIFT)) 221 panic("double-open chan%u", new_channel->ch_id); 222 223 new_channel->ch_cb = cb; 224 new_channel->ch_cbarg = cbarg; 225 226 vmbus_chan_update_evtflagcnt(sc, new_channel); 227 228 new_channel->ch_tq = VMBUS_PCPU_GET(new_channel->vmbus_sc, event_tq, 229 new_channel->ch_cpuid); 230 if (new_channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) { 231 TASK_INIT(&new_channel->ch_task, 0, vmbus_chan_task, 232 new_channel); 233 } else { 234 TASK_INIT(&new_channel->ch_task, 0, vmbus_chan_task_nobatch, 235 new_channel); 236 } 237 238 /* 239 * Allocate the TX+RX bufrings. 240 * XXX should use ch_dev dtag 241 */ 242 br = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev), 243 PAGE_SIZE, 0, send_ring_buffer_size + recv_ring_buffer_size, 244 &new_channel->ch_bufring_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 245 if (br == NULL) { 246 device_printf(sc->vmbus_dev, "bufring allocation failed\n"); 247 ret = ENOMEM; 248 goto failed; 249 } 250 new_channel->ch_bufring = br; 251 252 /* TX bufring comes first */ 253 hv_vmbus_ring_buffer_init(&new_channel->outbound, 254 br, send_ring_buffer_size); 255 /* RX bufring immediately follows TX bufring */ 256 hv_vmbus_ring_buffer_init(&new_channel->inbound, 257 br + send_ring_buffer_size, recv_ring_buffer_size); 258 259 /* Create sysctl tree for this channel */ 260 vmbus_channel_sysctl_create(new_channel); 261 262 /* 263 * Connect the bufrings, both RX and TX, to this channel. 264 */ 265 ret = vmbus_chan_gpadl_connect(new_channel, 266 new_channel->ch_bufring_dma.hv_paddr, 267 send_ring_buffer_size + recv_ring_buffer_size, 268 &new_channel->ch_bufring_gpadl); 269 if (ret != 0) { 270 device_printf(sc->vmbus_dev, 271 "failed to connect bufring GPADL to chan%u\n", 272 new_channel->ch_id); 273 goto failed; 274 } 275 276 /* 277 * Open channel w/ the bufring GPADL on the target CPU. 278 */ 279 mh = vmbus_msghc_get(sc, sizeof(*req)); 280 if (mh == NULL) { 281 device_printf(sc->vmbus_dev, 282 "can not get msg hypercall for chopen(chan%u)\n", 283 new_channel->ch_id); 284 ret = ENXIO; 285 goto failed; 286 } 287 288 req = vmbus_msghc_dataptr(mh); 289 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN; 290 req->chm_chanid = new_channel->ch_id; 291 req->chm_openid = new_channel->ch_id; 292 req->chm_gpadl = new_channel->ch_bufring_gpadl; 293 req->chm_vcpuid = new_channel->ch_vcpuid; 294 req->chm_rxbr_pgofs = send_ring_buffer_size >> PAGE_SHIFT; 295 if (user_data_len) 296 memcpy(req->chm_udata, user_data, user_data_len); 297 298 ret = vmbus_msghc_exec(sc, mh); 299 if (ret != 0) { 300 device_printf(sc->vmbus_dev, 301 "chopen(chan%u) msg hypercall exec failed: %d\n", 302 new_channel->ch_id, ret); 303 vmbus_msghc_put(sc, mh); 304 goto failed; 305 } 306 307 msg = vmbus_msghc_wait_result(sc, mh); 308 resp = (const struct vmbus_chanmsg_chopen_resp *)msg->msg_data; 309 status = resp->chm_status; 310 311 vmbus_msghc_put(sc, mh); 312 313 if (status == 0) { 314 if (bootverbose) { 315 device_printf(sc->vmbus_dev, "chan%u opened\n", 316 new_channel->ch_id); 317 } 318 return 0; 319 } 320 321 device_printf(sc->vmbus_dev, "failed to open chan%u\n", 322 new_channel->ch_id); 323 ret = ENXIO; 324 325failed: 326 if (new_channel->ch_bufring_gpadl) { 327 hv_vmbus_channel_teardown_gpdal(new_channel, 328 new_channel->ch_bufring_gpadl); 329 new_channel->ch_bufring_gpadl = 0; 330 } 331 if (new_channel->ch_bufring != NULL) { 332 hyperv_dmamem_free(&new_channel->ch_bufring_dma, 333 new_channel->ch_bufring); 334 new_channel->ch_bufring = NULL; 335 } 336 atomic_clear_int(&new_channel->ch_stflags, VMBUS_CHAN_ST_OPENED); 337 return ret; 338} 339 340/** 341 * @brief Establish a GPADL for the specified buffer 342 */ 343int 344hv_vmbus_channel_establish_gpadl(struct hv_vmbus_channel *channel, 345 void *contig_buffer, uint32_t size, uint32_t *gpadl) 346{ 347 return vmbus_chan_gpadl_connect(channel, 348 hv_get_phys_addr(contig_buffer), size, gpadl); 349} 350 351int 352vmbus_chan_gpadl_connect(struct hv_vmbus_channel *chan, bus_addr_t paddr, 353 int size, uint32_t *gpadl0) 354{ 355 struct vmbus_softc *sc = chan->vmbus_sc; 356 struct vmbus_msghc *mh; 357 struct vmbus_chanmsg_gpadl_conn *req; 358 const struct vmbus_message *msg; 359 size_t reqsz; 360 uint32_t gpadl, status; 361 int page_count, range_len, i, cnt, error; 362 uint64_t page_id; 363 364 /* 365 * Preliminary checks. 366 */ 367 368 KASSERT((size & PAGE_MASK) == 0, 369 ("invalid GPA size %d, not multiple page size", size)); 370 page_count = size >> PAGE_SHIFT; 371 372 KASSERT((paddr & PAGE_MASK) == 0, 373 ("GPA is not page aligned %jx", (uintmax_t)paddr)); 374 page_id = paddr >> PAGE_SHIFT; 375 376 range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]); 377 /* 378 * We don't support multiple GPA ranges. 379 */ 380 if (range_len > UINT16_MAX) { 381 device_printf(sc->vmbus_dev, "GPA too large, %d pages\n", 382 page_count); 383 return EOPNOTSUPP; 384 } 385 386 /* 387 * Allocate GPADL id. 388 */ 389 gpadl = vmbus_gpadl_alloc(sc); 390 *gpadl0 = gpadl; 391 392 /* 393 * Connect this GPADL to the target channel. 394 * 395 * NOTE: 396 * Since each message can only hold small set of page 397 * addresses, several messages may be required to 398 * complete the connection. 399 */ 400 if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX) 401 cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX; 402 else 403 cnt = page_count; 404 page_count -= cnt; 405 406 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn, 407 chm_range.gpa_page[cnt]); 408 mh = vmbus_msghc_get(sc, reqsz); 409 if (mh == NULL) { 410 device_printf(sc->vmbus_dev, 411 "can not get msg hypercall for gpadl->chan%u\n", 412 chan->ch_id); 413 return EIO; 414 } 415 416 req = vmbus_msghc_dataptr(mh); 417 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN; 418 req->chm_chanid = chan->ch_id; 419 req->chm_gpadl = gpadl; 420 req->chm_range_len = range_len; 421 req->chm_range_cnt = 1; 422 req->chm_range.gpa_len = size; 423 req->chm_range.gpa_ofs = 0; 424 for (i = 0; i < cnt; ++i) 425 req->chm_range.gpa_page[i] = page_id++; 426 427 error = vmbus_msghc_exec(sc, mh); 428 if (error) { 429 device_printf(sc->vmbus_dev, 430 "gpadl->chan%u msg hypercall exec failed: %d\n", 431 chan->ch_id, error); 432 vmbus_msghc_put(sc, mh); 433 return error; 434 } 435 436 while (page_count > 0) { 437 struct vmbus_chanmsg_gpadl_subconn *subreq; 438 439 if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX) 440 cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX; 441 else 442 cnt = page_count; 443 page_count -= cnt; 444 445 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn, 446 chm_gpa_page[cnt]); 447 vmbus_msghc_reset(mh, reqsz); 448 449 subreq = vmbus_msghc_dataptr(mh); 450 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN; 451 subreq->chm_gpadl = gpadl; 452 for (i = 0; i < cnt; ++i) 453 subreq->chm_gpa_page[i] = page_id++; 454 455 vmbus_msghc_exec_noresult(mh); 456 } 457 KASSERT(page_count == 0, ("invalid page count %d", page_count)); 458 459 msg = vmbus_msghc_wait_result(sc, mh); 460 status = ((const struct vmbus_chanmsg_gpadl_connresp *) 461 msg->msg_data)->chm_status; 462 463 vmbus_msghc_put(sc, mh); 464 465 if (status != 0) { 466 device_printf(sc->vmbus_dev, "gpadl->chan%u failed: " 467 "status %u\n", chan->ch_id, status); 468 return EIO; 469 } else { 470 if (bootverbose) { 471 device_printf(sc->vmbus_dev, "gpadl->chan%u " 472 "succeeded\n", chan->ch_id); 473 } 474 } 475 return 0; 476} 477 478/* 479 * Disconnect the GPA from the target channel 480 */ 481int 482hv_vmbus_channel_teardown_gpdal(struct hv_vmbus_channel *chan, uint32_t gpadl) 483{ 484 struct vmbus_softc *sc = chan->vmbus_sc; 485 struct vmbus_msghc *mh; 486 struct vmbus_chanmsg_gpadl_disconn *req; 487 int error; 488 489 mh = vmbus_msghc_get(sc, sizeof(*req)); 490 if (mh == NULL) { 491 device_printf(sc->vmbus_dev, 492 "can not get msg hypercall for gpa x->chan%u\n", 493 chan->ch_id); 494 return EBUSY; 495 } 496 497 req = vmbus_msghc_dataptr(mh); 498 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN; 499 req->chm_chanid = chan->ch_id; 500 req->chm_gpadl = gpadl; 501 502 error = vmbus_msghc_exec(sc, mh); 503 if (error) { 504 device_printf(sc->vmbus_dev, 505 "gpa x->chan%u msg hypercall exec failed: %d\n", 506 chan->ch_id, error); 507 vmbus_msghc_put(sc, mh); 508 return error; 509 } 510 511 vmbus_msghc_wait_result(sc, mh); 512 /* Discard result; no useful information */ 513 vmbus_msghc_put(sc, mh); 514 515 return 0; 516} 517 518static void 519hv_vmbus_channel_close_internal(hv_vmbus_channel *channel) 520{ 521 struct vmbus_softc *sc = channel->vmbus_sc; 522 struct vmbus_msghc *mh; 523 struct vmbus_chanmsg_chclose *req; 524 struct taskqueue *tq = channel->ch_tq; 525 int error; 526 527 /* TODO: stringent check */ 528 atomic_clear_int(&channel->ch_stflags, VMBUS_CHAN_ST_OPENED); 529 530 sysctl_ctx_free(&channel->ch_sysctl_ctx); 531 532 /* 533 * Set ch_tq to NULL to avoid more requests be scheduled 534 */ 535 channel->ch_tq = NULL; 536 taskqueue_drain(tq, &channel->ch_task); 537 channel->ch_cb = NULL; 538 539 /** 540 * Send a closing message 541 */ 542 543 mh = vmbus_msghc_get(sc, sizeof(*req)); 544 if (mh == NULL) { 545 device_printf(sc->vmbus_dev, 546 "can not get msg hypercall for chclose(chan%u)\n", 547 channel->ch_id); 548 return; 549 } 550 551 req = vmbus_msghc_dataptr(mh); 552 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE; 553 req->chm_chanid = channel->ch_id; 554 555 error = vmbus_msghc_exec_noresult(mh); 556 vmbus_msghc_put(sc, mh); 557 558 if (error) { 559 device_printf(sc->vmbus_dev, 560 "chclose(chan%u) msg hypercall exec failed: %d\n", 561 channel->ch_id, error); 562 return; 563 } else if (bootverbose) { 564 device_printf(sc->vmbus_dev, "close chan%u\n", 565 channel->ch_id); 566 } 567 568 /* Tear down the gpadl for the channel's ring buffer */ 569 if (channel->ch_bufring_gpadl) { 570 hv_vmbus_channel_teardown_gpdal(channel, 571 channel->ch_bufring_gpadl); 572 channel->ch_bufring_gpadl = 0; 573 } 574 575 /* TODO: Send a msg to release the childRelId */ 576 577 /* cleanup the ring buffers for this channel */ 578 hv_ring_buffer_cleanup(&channel->outbound); 579 hv_ring_buffer_cleanup(&channel->inbound); 580 581 if (channel->ch_bufring != NULL) { 582 hyperv_dmamem_free(&channel->ch_bufring_dma, 583 channel->ch_bufring); 584 channel->ch_bufring = NULL; 585 } 586} 587 588/* 589 * Caller should make sure that all sub-channels have 590 * been added to 'chan' and all to-be-closed channels 591 * are not being opened. 592 */ 593void 594hv_vmbus_channel_close(struct hv_vmbus_channel *chan) 595{ 596 int subchan_cnt; 597 598 if (!VMBUS_CHAN_ISPRIMARY(chan)) { 599 /* 600 * Sub-channel is closed when its primary channel 601 * is closed; done. 602 */ 603 return; 604 } 605 606 /* 607 * Close all sub-channels, if any. 608 */ 609 subchan_cnt = chan->ch_subchan_cnt; 610 if (subchan_cnt > 0) { 611 struct hv_vmbus_channel **subchan; 612 int i; 613 614 subchan = vmbus_get_subchan(chan, subchan_cnt); 615 for (i = 0; i < subchan_cnt; ++i) 616 hv_vmbus_channel_close_internal(subchan[i]); 617 vmbus_rel_subchan(subchan, subchan_cnt); 618 } 619 620 /* Then close the primary channel. */ 621 hv_vmbus_channel_close_internal(chan); 622} 623 624/** 625 * @brief Send the specified buffer on the given channel 626 */ 627int 628hv_vmbus_channel_send_packet( 629 hv_vmbus_channel* channel, 630 void* buffer, 631 uint32_t buffer_len, 632 uint64_t request_id, 633 hv_vmbus_packet_type type, 634 uint32_t flags) 635{ 636 int ret = 0; 637 hv_vm_packet_descriptor desc; 638 uint32_t packet_len; 639 uint64_t aligned_data; 640 uint32_t packet_len_aligned; 641 boolean_t need_sig; 642 struct iovec iov[3]; 643 644 packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len; 645 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 646 aligned_data = 0; 647 648 /* Setup the descriptor */ 649 desc.type = type; /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND; */ 650 desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */ 651 /* in 8-bytes granularity */ 652 desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3; 653 desc.length8 = (uint16_t) (packet_len_aligned >> 3); 654 desc.transaction_id = request_id; 655 656 iov[0].iov_base = &desc; 657 iov[0].iov_len = sizeof(hv_vm_packet_descriptor); 658 659 iov[1].iov_base = buffer; 660 iov[1].iov_len = buffer_len; 661 662 iov[2].iov_base = &aligned_data; 663 iov[2].iov_len = packet_len_aligned - packet_len; 664 665 ret = hv_ring_buffer_write(&channel->outbound, iov, 3, &need_sig); 666 667 /* TODO: We should determine if this is optional */ 668 if (ret == 0 && need_sig) 669 vmbus_chan_send_event(channel); 670 671 return (ret); 672} 673 674/** 675 * @brief Send a range of single-page buffer packets using 676 * a GPADL Direct packet type 677 */ 678int 679hv_vmbus_channel_send_packet_pagebuffer( 680 hv_vmbus_channel* channel, 681 hv_vmbus_page_buffer page_buffers[], 682 uint32_t page_count, 683 void* buffer, 684 uint32_t buffer_len, 685 uint64_t request_id) 686{ 687 688 int ret = 0; 689 boolean_t need_sig; 690 uint32_t packet_len; 691 uint32_t page_buflen; 692 uint32_t packetLen_aligned; 693 struct iovec iov[4]; 694 hv_vmbus_channel_packet_page_buffer desc; 695 uint32_t descSize; 696 uint64_t alignedData = 0; 697 698 if (page_count > HV_MAX_PAGE_BUFFER_COUNT) 699 return (EINVAL); 700 701 /* 702 * Adjust the size down since hv_vmbus_channel_packet_page_buffer 703 * is the largest size we support 704 */ 705 descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range); 706 page_buflen = sizeof(hv_vmbus_page_buffer) * page_count; 707 packet_len = descSize + page_buflen + buffer_len; 708 packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 709 710 /* Setup the descriptor */ 711 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 712 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 713 /* in 8-bytes granularity */ 714 desc.data_offset8 = (descSize + page_buflen) >> 3; 715 desc.length8 = (uint16_t) (packetLen_aligned >> 3); 716 desc.transaction_id = request_id; 717 desc.range_count = page_count; 718 719 iov[0].iov_base = &desc; 720 iov[0].iov_len = descSize; 721 722 iov[1].iov_base = page_buffers; 723 iov[1].iov_len = page_buflen; 724 725 iov[2].iov_base = buffer; 726 iov[2].iov_len = buffer_len; 727 728 iov[3].iov_base = &alignedData; 729 iov[3].iov_len = packetLen_aligned - packet_len; 730 731 ret = hv_ring_buffer_write(&channel->outbound, iov, 4, &need_sig); 732 733 /* TODO: We should determine if this is optional */ 734 if (ret == 0 && need_sig) 735 vmbus_chan_send_event(channel); 736 737 return (ret); 738} 739 740/** 741 * @brief Send a multi-page buffer packet using a GPADL Direct packet type 742 */ 743int 744hv_vmbus_channel_send_packet_multipagebuffer( 745 hv_vmbus_channel* channel, 746 hv_vmbus_multipage_buffer* multi_page_buffer, 747 void* buffer, 748 uint32_t buffer_len, 749 uint64_t request_id) 750{ 751 752 int ret = 0; 753 uint32_t desc_size; 754 boolean_t need_sig; 755 uint32_t packet_len; 756 uint32_t packet_len_aligned; 757 uint32_t pfn_count; 758 uint64_t aligned_data = 0; 759 struct iovec iov[3]; 760 hv_vmbus_channel_packet_multipage_buffer desc; 761 762 pfn_count = 763 HV_NUM_PAGES_SPANNED( 764 multi_page_buffer->offset, 765 multi_page_buffer->length); 766 767 if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT)) 768 return (EINVAL); 769 /* 770 * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer 771 * is the largest size we support 772 */ 773 desc_size = 774 sizeof(hv_vmbus_channel_packet_multipage_buffer) - 775 ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) * 776 sizeof(uint64_t)); 777 packet_len = desc_size + buffer_len; 778 packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t)); 779 780 /* 781 * Setup the descriptor 782 */ 783 desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT; 784 desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 785 desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */ 786 desc.length8 = (uint16_t) (packet_len_aligned >> 3); 787 desc.transaction_id = request_id; 788 desc.range_count = 1; 789 790 desc.range.length = multi_page_buffer->length; 791 desc.range.offset = multi_page_buffer->offset; 792 793 memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array, 794 pfn_count * sizeof(uint64_t)); 795 796 iov[0].iov_base = &desc; 797 iov[0].iov_len = desc_size; 798 799 iov[1].iov_base = buffer; 800 iov[1].iov_len = buffer_len; 801 802 iov[2].iov_base = &aligned_data; 803 iov[2].iov_len = packet_len_aligned - packet_len; 804 805 ret = hv_ring_buffer_write(&channel->outbound, iov, 3, &need_sig); 806 807 /* TODO: We should determine if this is optional */ 808 if (ret == 0 && need_sig) 809 vmbus_chan_send_event(channel); 810 811 return (ret); 812} 813 814/** 815 * @brief Retrieve the user packet on the specified channel 816 */ 817int 818hv_vmbus_channel_recv_packet( 819 hv_vmbus_channel* channel, 820 void* Buffer, 821 uint32_t buffer_len, 822 uint32_t* buffer_actual_len, 823 uint64_t* request_id) 824{ 825 int ret; 826 uint32_t user_len; 827 uint32_t packet_len; 828 hv_vm_packet_descriptor desc; 829 830 *buffer_actual_len = 0; 831 *request_id = 0; 832 833 ret = hv_ring_buffer_peek(&channel->inbound, &desc, 834 sizeof(hv_vm_packet_descriptor)); 835 if (ret != 0) 836 return (0); 837 838 packet_len = desc.length8 << 3; 839 user_len = packet_len - (desc.data_offset8 << 3); 840 841 *buffer_actual_len = user_len; 842 843 if (user_len > buffer_len) 844 return (EINVAL); 845 846 *request_id = desc.transaction_id; 847 848 /* Copy over the packet to the user buffer */ 849 ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len, 850 (desc.data_offset8 << 3)); 851 852 return (0); 853} 854 855/** 856 * @brief Retrieve the raw packet on the specified channel 857 */ 858int 859hv_vmbus_channel_recv_packet_raw( 860 hv_vmbus_channel* channel, 861 void* buffer, 862 uint32_t buffer_len, 863 uint32_t* buffer_actual_len, 864 uint64_t* request_id) 865{ 866 int ret; 867 uint32_t packetLen; 868 hv_vm_packet_descriptor desc; 869 870 *buffer_actual_len = 0; 871 *request_id = 0; 872 873 ret = hv_ring_buffer_peek( 874 &channel->inbound, &desc, 875 sizeof(hv_vm_packet_descriptor)); 876 877 if (ret != 0) 878 return (0); 879 880 packetLen = desc.length8 << 3; 881 *buffer_actual_len = packetLen; 882 883 if (packetLen > buffer_len) 884 return (ENOBUFS); 885 886 *request_id = desc.transaction_id; 887 888 /* Copy over the entire packet to the user buffer */ 889 ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0); 890 891 return (0); 892} 893 894static void 895vmbus_chan_task(void *xchan, int pending __unused) 896{ 897 struct hv_vmbus_channel *chan = xchan; 898 vmbus_chan_callback_t cb = chan->ch_cb; 899 void *cbarg = chan->ch_cbarg; 900 901 /* 902 * Optimize host to guest signaling by ensuring: 903 * 1. While reading the channel, we disable interrupts from 904 * host. 905 * 2. Ensure that we process all posted messages from the host 906 * before returning from this callback. 907 * 3. Once we return, enable signaling from the host. Once this 908 * state is set we check to see if additional packets are 909 * available to read. In this case we repeat the process. 910 * 911 * NOTE: Interrupt has been disabled in the ISR. 912 */ 913 for (;;) { 914 uint32_t left; 915 916 cb(cbarg); 917 918 left = hv_ring_buffer_read_end(&chan->inbound); 919 if (left == 0) { 920 /* No more data in RX bufring; done */ 921 break; 922 } 923 hv_ring_buffer_read_begin(&chan->inbound); 924 } 925} 926 927static void 928vmbus_chan_task_nobatch(void *xchan, int pending __unused) 929{ 930 struct hv_vmbus_channel *chan = xchan; 931 932 chan->ch_cb(chan->ch_cbarg); 933} 934 935static __inline void 936vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags, 937 int flag_cnt) 938{ 939 int f; 940 941 for (f = 0; f < flag_cnt; ++f) { 942 uint32_t chid_base; 943 u_long flags; 944 int chid_ofs; 945 946 if (event_flags[f] == 0) 947 continue; 948 949 flags = atomic_swap_long(&event_flags[f], 0); 950 chid_base = f << VMBUS_EVTFLAG_SHIFT; 951 952 while ((chid_ofs = ffsl(flags)) != 0) { 953 struct hv_vmbus_channel *channel; 954 955 --chid_ofs; /* NOTE: ffsl is 1-based */ 956 flags &= ~(1UL << chid_ofs); 957 958 channel = sc->vmbus_chmap[chid_base + chid_ofs]; 959 960 /* if channel is closed or closing */ 961 if (channel == NULL || channel->ch_tq == NULL) 962 continue; 963 964 if (channel->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) 965 hv_ring_buffer_read_begin(&channel->inbound); 966 taskqueue_enqueue(channel->ch_tq, &channel->ch_task); 967 } 968 } 969} 970 971void 972vmbus_event_proc(struct vmbus_softc *sc, int cpu) 973{ 974 struct vmbus_evtflags *eventf; 975 976 /* 977 * On Host with Win8 or above, the event page can be checked directly 978 * to get the id of the channel that has the pending interrupt. 979 */ 980 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 981 vmbus_event_flags_proc(sc, eventf->evt_flags, 982 VMBUS_PCPU_GET(sc, event_flags_cnt, cpu)); 983} 984 985void 986vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu) 987{ 988 struct vmbus_evtflags *eventf; 989 990 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 991 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) { 992 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags, 993 VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT); 994 } 995} 996 997static void 998vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc, 999 const struct hv_vmbus_channel *chan) 1000{ 1001 volatile int *flag_cnt_ptr; 1002 int flag_cnt; 1003 1004 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1; 1005 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid); 1006 1007 for (;;) { 1008 int old_flag_cnt; 1009 1010 old_flag_cnt = *flag_cnt_ptr; 1011 if (old_flag_cnt >= flag_cnt) 1012 break; 1013 if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) { 1014 if (bootverbose) { 1015 device_printf(sc->vmbus_dev, 1016 "channel%u update cpu%d flag_cnt to %d\n", 1017 chan->ch_id, chan->ch_cpuid, flag_cnt); 1018 } 1019 break; 1020 } 1021 } 1022} 1023 1024static struct hv_vmbus_channel * 1025vmbus_chan_alloc(struct vmbus_softc *sc) 1026{ 1027 struct hv_vmbus_channel *chan; 1028 1029 chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO); 1030 1031 chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev), 1032 HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param), 1033 &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 1034 if (chan->ch_monprm == NULL) { 1035 device_printf(sc->vmbus_dev, "monprm alloc failed\n"); 1036 free(chan, M_DEVBUF); 1037 return NULL; 1038 } 1039 1040 chan->vmbus_sc = sc; 1041 mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF); 1042 TAILQ_INIT(&chan->ch_subchans); 1043 TASK_INIT(&chan->ch_detach_task, 0, vmbus_chan_detach_task, chan); 1044 1045 return chan; 1046} 1047 1048static void 1049vmbus_chan_free(struct hv_vmbus_channel *chan) 1050{ 1051 /* TODO: assert sub-channel list is empty */ 1052 /* TODO: asset no longer on the primary channel's sub-channel list */ 1053 /* TODO: asset no longer on the vmbus channel list */ 1054 hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm); 1055 mtx_destroy(&chan->ch_subchan_lock); 1056 free(chan, M_DEVBUF); 1057} 1058 1059static int 1060vmbus_chan_add(struct hv_vmbus_channel *newchan) 1061{ 1062 struct vmbus_softc *sc = newchan->vmbus_sc; 1063 struct hv_vmbus_channel *prichan; 1064 1065 if (newchan->ch_id == 0) { 1066 /* 1067 * XXX 1068 * Chan0 will neither be processed nor should be offered; 1069 * skip it. 1070 */ 1071 device_printf(sc->vmbus_dev, "got chan0 offer, discard\n"); 1072 return EINVAL; 1073 } else if (newchan->ch_id >= VMBUS_CHAN_MAX) { 1074 device_printf(sc->vmbus_dev, "invalid chan%u offer\n", 1075 newchan->ch_id); 1076 return EINVAL; 1077 } 1078 sc->vmbus_chmap[newchan->ch_id] = newchan; 1079 1080 if (bootverbose) { 1081 device_printf(sc->vmbus_dev, "chan%u subidx%u offer\n", 1082 newchan->ch_id, newchan->ch_subidx); 1083 } 1084 1085 mtx_lock(&sc->vmbus_prichan_lock); 1086 TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) { 1087 /* 1088 * Sub-channel will have the same type GUID and instance 1089 * GUID as its primary channel. 1090 */ 1091 if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type, 1092 sizeof(struct hyperv_guid)) == 0 && 1093 memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst, 1094 sizeof(struct hyperv_guid)) == 0) 1095 break; 1096 } 1097 if (VMBUS_CHAN_ISPRIMARY(newchan)) { 1098 if (prichan == NULL) { 1099 /* Install the new primary channel */ 1100 TAILQ_INSERT_TAIL(&sc->vmbus_prichans, newchan, 1101 ch_prilink); 1102 mtx_unlock(&sc->vmbus_prichan_lock); 1103 return 0; 1104 } else { 1105 mtx_unlock(&sc->vmbus_prichan_lock); 1106 device_printf(sc->vmbus_dev, "duplicated primary " 1107 "chan%u\n", newchan->ch_id); 1108 return EINVAL; 1109 } 1110 } else { /* Sub-channel */ 1111 if (prichan == NULL) { 1112 mtx_unlock(&sc->vmbus_prichan_lock); 1113 device_printf(sc->vmbus_dev, "no primary chan for " 1114 "chan%u\n", newchan->ch_id); 1115 return EINVAL; 1116 } 1117 /* 1118 * Found the primary channel for this sub-channel and 1119 * move on. 1120 * 1121 * XXX refcnt prichan 1122 */ 1123 } 1124 mtx_unlock(&sc->vmbus_prichan_lock); 1125 1126 /* 1127 * This is a sub-channel; link it with the primary channel. 1128 */ 1129 KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan), 1130 ("new channel is not sub-channel")); 1131 KASSERT(prichan != NULL, ("no primary channel")); 1132 1133 newchan->ch_prichan = prichan; 1134 newchan->ch_dev = prichan->ch_dev; 1135 1136 mtx_lock(&prichan->ch_subchan_lock); 1137 TAILQ_INSERT_TAIL(&prichan->ch_subchans, newchan, ch_sublink); 1138 /* 1139 * Bump up sub-channel count and notify anyone that is 1140 * interested in this sub-channel, after this sub-channel 1141 * is setup. 1142 */ 1143 prichan->ch_subchan_cnt++; 1144 mtx_unlock(&prichan->ch_subchan_lock); 1145 wakeup(prichan); 1146 1147 return 0; 1148} 1149 1150void 1151vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu) 1152{ 1153 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu)); 1154 1155 if (chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WS2008 || 1156 chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WIN7) { 1157 /* Only cpu0 is supported */ 1158 cpu = 0; 1159 } 1160 1161 chan->ch_cpuid = cpu; 1162 chan->ch_vcpuid = VMBUS_PCPU_GET(chan->vmbus_sc, vcpuid, cpu); 1163 1164 if (bootverbose) { 1165 printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n", 1166 chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid); 1167 } 1168} 1169 1170void 1171vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan) 1172{ 1173 static uint32_t vmbus_chan_nextcpu; 1174 int cpu; 1175 1176 cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus; 1177 vmbus_channel_cpu_set(chan, cpu); 1178} 1179 1180static void 1181vmbus_chan_cpu_default(struct hv_vmbus_channel *chan) 1182{ 1183 /* 1184 * By default, pin the channel to cpu0. Devices having 1185 * special channel-cpu mapping requirement should call 1186 * vmbus_channel_cpu_{set,rr}(). 1187 */ 1188 vmbus_channel_cpu_set(chan, 0); 1189} 1190 1191static void 1192vmbus_chan_msgproc_choffer(struct vmbus_softc *sc, 1193 const struct vmbus_message *msg) 1194{ 1195 const struct vmbus_chanmsg_choffer *offer; 1196 struct hv_vmbus_channel *chan; 1197 int error; 1198 1199 offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data; 1200 1201 chan = vmbus_chan_alloc(sc); 1202 if (chan == NULL) { 1203 device_printf(sc->vmbus_dev, "allocate chan%u failed\n", 1204 offer->chm_chanid); 1205 return; 1206 } 1207 1208 chan->ch_id = offer->chm_chanid; 1209 chan->ch_subidx = offer->chm_subidx; 1210 chan->ch_guid_type = offer->chm_chtype; 1211 chan->ch_guid_inst = offer->chm_chinst; 1212 1213 /* Batch reading is on by default */ 1214 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD; 1215 1216 chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT; 1217 if (sc->vmbus_version != VMBUS_VERSION_WS2008) 1218 chan->ch_monprm->mp_connid = offer->chm_connid; 1219 1220 if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) { 1221 /* 1222 * Setup MNF stuffs. 1223 */ 1224 chan->ch_flags |= VMBUS_CHAN_FLAG_HASMNF; 1225 chan->ch_montrig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN; 1226 if (chan->ch_montrig_idx >= VMBUS_MONTRIGS_MAX) 1227 panic("invalid monitor trigger %u", offer->chm_montrig); 1228 chan->ch_montrig_mask = 1229 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN); 1230 } 1231 1232 /* Select default cpu for this channel. */ 1233 vmbus_chan_cpu_default(chan); 1234 1235 error = vmbus_chan_add(chan); 1236 if (error) { 1237 device_printf(sc->vmbus_dev, "add chan%u failed: %d\n", 1238 chan->ch_id, error); 1239 vmbus_chan_free(chan); 1240 return; 1241 } 1242 1243 if (VMBUS_CHAN_ISPRIMARY(chan)) { 1244 /* 1245 * Add device for this primary channel. 1246 * 1247 * NOTE: 1248 * Error is ignored here; don't have much to do if error 1249 * really happens. 1250 */ 1251 vmbus_add_child(chan); 1252 } 1253} 1254 1255/* 1256 * XXX pretty broken; need rework. 1257 */ 1258static void 1259vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc, 1260 const struct vmbus_message *msg) 1261{ 1262 const struct vmbus_chanmsg_chrescind *note; 1263 struct hv_vmbus_channel *chan; 1264 1265 note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data; 1266 if (note->chm_chanid > VMBUS_CHAN_MAX) { 1267 device_printf(sc->vmbus_dev, "invalid rescinded chan%u\n", 1268 note->chm_chanid); 1269 return; 1270 } 1271 1272 if (bootverbose) { 1273 device_printf(sc->vmbus_dev, "chan%u rescinded\n", 1274 note->chm_chanid); 1275 } 1276 1277 chan = sc->vmbus_chmap[note->chm_chanid]; 1278 if (chan == NULL) 1279 return; 1280 sc->vmbus_chmap[note->chm_chanid] = NULL; 1281 1282 taskqueue_enqueue(taskqueue_thread, &chan->ch_detach_task); 1283} 1284 1285static void 1286vmbus_chan_detach_task(void *xchan, int pending __unused) 1287{ 1288 struct hv_vmbus_channel *chan = xchan; 1289 1290 if (VMBUS_CHAN_ISPRIMARY(chan)) { 1291 /* Only primary channel owns the device */ 1292 vmbus_delete_child(chan); 1293 /* NOTE: DO NOT free primary channel for now */ 1294 } else { 1295 struct vmbus_softc *sc = chan->vmbus_sc; 1296 struct hv_vmbus_channel *pri_chan = chan->ch_prichan; 1297 struct vmbus_chanmsg_chfree *req; 1298 struct vmbus_msghc *mh; 1299 int error; 1300 1301 mh = vmbus_msghc_get(sc, sizeof(*req)); 1302 if (mh == NULL) { 1303 device_printf(sc->vmbus_dev, 1304 "can not get msg hypercall for chfree(chan%u)\n", 1305 chan->ch_id); 1306 goto remove; 1307 } 1308 1309 req = vmbus_msghc_dataptr(mh); 1310 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE; 1311 req->chm_chanid = chan->ch_id; 1312 1313 error = vmbus_msghc_exec_noresult(mh); 1314 vmbus_msghc_put(sc, mh); 1315 1316 if (error) { 1317 device_printf(sc->vmbus_dev, 1318 "chfree(chan%u) failed: %d", 1319 chan->ch_id, error); 1320 /* NOTE: Move on! */ 1321 } else { 1322 if (bootverbose) { 1323 device_printf(sc->vmbus_dev, "chan%u freed\n", 1324 chan->ch_id); 1325 } 1326 } 1327remove: 1328 mtx_lock(&pri_chan->ch_subchan_lock); 1329 TAILQ_REMOVE(&pri_chan->ch_subchans, chan, ch_sublink); 1330 KASSERT(pri_chan->ch_subchan_cnt > 0, 1331 ("invalid subchan_cnt %d", pri_chan->ch_subchan_cnt)); 1332 pri_chan->ch_subchan_cnt--; 1333 mtx_unlock(&pri_chan->ch_subchan_lock); 1334 wakeup(pri_chan); 1335 1336 vmbus_chan_free(chan); 1337 } 1338} 1339 1340/* 1341 * Detach all devices and destroy the corresponding primary channels. 1342 */ 1343void 1344vmbus_chan_destroy_all(struct vmbus_softc *sc) 1345{ 1346 struct hv_vmbus_channel *chan; 1347 1348 mtx_lock(&sc->vmbus_prichan_lock); 1349 while ((chan = TAILQ_FIRST(&sc->vmbus_prichans)) != NULL) { 1350 KASSERT(VMBUS_CHAN_ISPRIMARY(chan), ("not primary channel")); 1351 TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink); 1352 mtx_unlock(&sc->vmbus_prichan_lock); 1353 1354 vmbus_delete_child(chan); 1355 vmbus_chan_free(chan); 1356 1357 mtx_lock(&sc->vmbus_prichan_lock); 1358 } 1359 bzero(sc->vmbus_chmap, 1360 sizeof(struct hv_vmbus_channel *) * VMBUS_CHAN_MAX); 1361 mtx_unlock(&sc->vmbus_prichan_lock); 1362} 1363 1364/** 1365 * @brief Select the best outgoing channel 1366 * 1367 * The channel whose vcpu binding is closest to the currect vcpu will 1368 * be selected. 1369 * If no multi-channel, always select primary channel 1370 * 1371 * @param primary - primary channel 1372 */ 1373struct hv_vmbus_channel * 1374vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary) 1375{ 1376 hv_vmbus_channel *new_channel = NULL; 1377 hv_vmbus_channel *outgoing_channel = primary; 1378 int old_cpu_distance = 0; 1379 int new_cpu_distance = 0; 1380 int cur_vcpu = 0; 1381 int smp_pro_id = PCPU_GET(cpuid); 1382 1383 if (TAILQ_EMPTY(&primary->ch_subchans)) { 1384 return outgoing_channel; 1385 } 1386 1387 if (smp_pro_id >= MAXCPU) { 1388 return outgoing_channel; 1389 } 1390 1391 cur_vcpu = VMBUS_PCPU_GET(primary->vmbus_sc, vcpuid, smp_pro_id); 1392 1393 /* XXX need lock */ 1394 TAILQ_FOREACH(new_channel, &primary->ch_subchans, ch_sublink) { 1395 if ((new_channel->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0) { 1396 continue; 1397 } 1398 1399 if (new_channel->ch_vcpuid == cur_vcpu){ 1400 return new_channel; 1401 } 1402 1403 old_cpu_distance = ((outgoing_channel->ch_vcpuid > cur_vcpu) ? 1404 (outgoing_channel->ch_vcpuid - cur_vcpu) : 1405 (cur_vcpu - outgoing_channel->ch_vcpuid)); 1406 1407 new_cpu_distance = ((new_channel->ch_vcpuid > cur_vcpu) ? 1408 (new_channel->ch_vcpuid - cur_vcpu) : 1409 (cur_vcpu - new_channel->ch_vcpuid)); 1410 1411 if (old_cpu_distance < new_cpu_distance) { 1412 continue; 1413 } 1414 1415 outgoing_channel = new_channel; 1416 } 1417 1418 return(outgoing_channel); 1419} 1420 1421struct hv_vmbus_channel ** 1422vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt) 1423{ 1424 struct hv_vmbus_channel **ret, *chan; 1425 int i; 1426 1427 ret = malloc(subchan_cnt * sizeof(struct hv_vmbus_channel *), M_TEMP, 1428 M_WAITOK); 1429 1430 mtx_lock(&pri_chan->ch_subchan_lock); 1431 1432 while (pri_chan->ch_subchan_cnt < subchan_cnt) 1433 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0); 1434 1435 i = 0; 1436 TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) { 1437 /* TODO: refcnt chan */ 1438 ret[i] = chan; 1439 1440 ++i; 1441 if (i == subchan_cnt) 1442 break; 1443 } 1444 KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d", 1445 pri_chan->ch_subchan_cnt, subchan_cnt)); 1446 1447 mtx_unlock(&pri_chan->ch_subchan_lock); 1448 1449 return ret; 1450} 1451 1452void 1453vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt __unused) 1454{ 1455 1456 free(subchan, M_TEMP); 1457} 1458 1459void 1460vmbus_drain_subchan(struct hv_vmbus_channel *pri_chan) 1461{ 1462 mtx_lock(&pri_chan->ch_subchan_lock); 1463 while (pri_chan->ch_subchan_cnt > 0) 1464 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0); 1465 mtx_unlock(&pri_chan->ch_subchan_lock); 1466} 1467 1468void 1469vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg) 1470{ 1471 vmbus_chanmsg_proc_t msg_proc; 1472 uint32_t msg_type; 1473 1474 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type; 1475 KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX, 1476 ("invalid message type %u", msg_type)); 1477 1478 msg_proc = vmbus_chan_msgprocs[msg_type]; 1479 if (msg_proc != NULL) 1480 msg_proc(sc, msg); 1481} 1482