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$"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/callout.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/mutex.h> 39 #include <sys/smp.h> 40 #include <sys/sysctl.h> 41 #include <sys/systm.h> 42 43 #include <machine/atomic.h> 44 #include <machine/stdarg.h> 45 46 #include <dev/hyperv/include/hyperv_busdma.h> 47 #include <dev/hyperv/include/vmbus_xact.h> 48 #include <dev/hyperv/vmbus/hyperv_var.h> 49 #include <dev/hyperv/vmbus/vmbus_reg.h> 50 #include <dev/hyperv/vmbus/vmbus_var.h> 51 #include <dev/hyperv/vmbus/vmbus_brvar.h> 52 #include <dev/hyperv/vmbus/vmbus_chanvar.h> 53 54 struct vmbus_chan_pollarg { 55 struct vmbus_channel *poll_chan; 56 u_int poll_hz; 57 }; 58 59 static void vmbus_chan_update_evtflagcnt( 60 struct vmbus_softc *, 61 const struct vmbus_channel *); 62 static int vmbus_chan_close_internal( 63 struct vmbus_channel *); 64 static int vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS); 65 static void vmbus_chan_sysctl_create( 66 struct vmbus_channel *); 67 static struct vmbus_channel *vmbus_chan_alloc(struct vmbus_softc *); 68 static void vmbus_chan_free(struct vmbus_channel *); 69 static int vmbus_chan_add(struct vmbus_channel *); 70 static void vmbus_chan_cpu_default(struct vmbus_channel *); 71 static int vmbus_chan_release(struct vmbus_channel *); 72 static void vmbus_chan_set_chmap(struct vmbus_channel *); 73 static void vmbus_chan_clear_chmap(struct vmbus_channel *); 74 static void vmbus_chan_detach(struct vmbus_channel *); 75 static bool vmbus_chan_wait_revoke( 76 const struct vmbus_channel *, bool); 77 static void vmbus_chan_poll_timeout(void *); 78 static bool vmbus_chan_poll_cancel_intq( 79 struct vmbus_channel *); 80 static void vmbus_chan_poll_cancel(struct vmbus_channel *); 81 82 static void vmbus_chan_ins_prilist(struct vmbus_softc *, 83 struct vmbus_channel *); 84 static void vmbus_chan_rem_prilist(struct vmbus_softc *, 85 struct vmbus_channel *); 86 static void vmbus_chan_ins_list(struct vmbus_softc *, 87 struct vmbus_channel *); 88 static void vmbus_chan_rem_list(struct vmbus_softc *, 89 struct vmbus_channel *); 90 static void vmbus_chan_ins_sublist(struct vmbus_channel *, 91 struct vmbus_channel *); 92 static void vmbus_chan_rem_sublist(struct vmbus_channel *, 93 struct vmbus_channel *); 94 95 static void vmbus_chan_task(void *, int); 96 static void vmbus_chan_task_nobatch(void *, int); 97 static void vmbus_chan_poll_task(void *, int); 98 static void vmbus_chan_clrchmap_task(void *, int); 99 static void vmbus_chan_pollcfg_task(void *, int); 100 static void vmbus_chan_polldis_task(void *, int); 101 static void vmbus_chan_poll_cancel_task(void *, int); 102 static void vmbus_prichan_attach_task(void *, int); 103 static void vmbus_subchan_attach_task(void *, int); 104 static void vmbus_prichan_detach_task(void *, int); 105 static void vmbus_subchan_detach_task(void *, int); 106 107 static void vmbus_chan_msgproc_choffer(struct vmbus_softc *, 108 const struct vmbus_message *); 109 static void vmbus_chan_msgproc_chrescind( 110 struct vmbus_softc *, 111 const struct vmbus_message *); 112 113 static int vmbus_chan_printf(const struct vmbus_channel *, 114 const char *, ...) __printflike(2, 3); 115 116 /* 117 * Vmbus channel message processing. 118 */ 119 static const vmbus_chanmsg_proc_t 120 vmbus_chan_msgprocs[VMBUS_CHANMSG_TYPE_MAX] = { 121 VMBUS_CHANMSG_PROC(CHOFFER, vmbus_chan_msgproc_choffer), 122 VMBUS_CHANMSG_PROC(CHRESCIND, vmbus_chan_msgproc_chrescind), 123 124 VMBUS_CHANMSG_PROC_WAKEUP(CHOPEN_RESP), 125 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_CONNRESP), 126 VMBUS_CHANMSG_PROC_WAKEUP(GPADL_DISCONNRESP) 127 }; 128 129 /* 130 * Notify host that there are data pending on our TX bufring. 131 */ 132 static __inline void 133 vmbus_chan_signal_tx(const struct vmbus_channel *chan) 134 { 135 atomic_set_long(chan->ch_evtflag, chan->ch_evtflag_mask); 136 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF) 137 atomic_set_int(chan->ch_montrig, chan->ch_montrig_mask); 138 else 139 hypercall_signal_event(chan->ch_monprm_dma.hv_paddr); 140 } 141 142 static void 143 vmbus_chan_ins_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan) 144 { 145 146 mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED); 147 if (atomic_testandset_int(&chan->ch_stflags, 148 VMBUS_CHAN_ST_ONPRIL_SHIFT)) 149 panic("channel is already on the prilist"); 150 TAILQ_INSERT_TAIL(&sc->vmbus_prichans, chan, ch_prilink); 151 } 152 153 static void 154 vmbus_chan_rem_prilist(struct vmbus_softc *sc, struct vmbus_channel *chan) 155 { 156 157 mtx_assert(&sc->vmbus_prichan_lock, MA_OWNED); 158 if (atomic_testandclear_int(&chan->ch_stflags, 159 VMBUS_CHAN_ST_ONPRIL_SHIFT) == 0) 160 panic("channel is not on the prilist"); 161 TAILQ_REMOVE(&sc->vmbus_prichans, chan, ch_prilink); 162 } 163 164 static void 165 vmbus_chan_ins_sublist(struct vmbus_channel *prichan, 166 struct vmbus_channel *chan) 167 { 168 169 mtx_assert(&prichan->ch_subchan_lock, MA_OWNED); 170 171 if (atomic_testandset_int(&chan->ch_stflags, 172 VMBUS_CHAN_ST_ONSUBL_SHIFT)) 173 panic("channel is already on the sublist"); 174 TAILQ_INSERT_TAIL(&prichan->ch_subchans, chan, ch_sublink); 175 176 /* Bump sub-channel count. */ 177 prichan->ch_subchan_cnt++; 178 } 179 180 static void 181 vmbus_chan_rem_sublist(struct vmbus_channel *prichan, 182 struct vmbus_channel *chan) 183 { 184 185 mtx_assert(&prichan->ch_subchan_lock, MA_OWNED); 186 187 KASSERT(prichan->ch_subchan_cnt > 0, 188 ("invalid subchan_cnt %d", prichan->ch_subchan_cnt)); 189 prichan->ch_subchan_cnt--; 190 191 if (atomic_testandclear_int(&chan->ch_stflags, 192 VMBUS_CHAN_ST_ONSUBL_SHIFT) == 0) 193 panic("channel is not on the sublist"); 194 TAILQ_REMOVE(&prichan->ch_subchans, chan, ch_sublink); 195 } 196 197 static void 198 vmbus_chan_ins_list(struct vmbus_softc *sc, struct vmbus_channel *chan) 199 { 200 201 mtx_assert(&sc->vmbus_chan_lock, MA_OWNED); 202 if (atomic_testandset_int(&chan->ch_stflags, 203 VMBUS_CHAN_ST_ONLIST_SHIFT)) 204 panic("channel is already on the list"); 205 TAILQ_INSERT_TAIL(&sc->vmbus_chans, chan, ch_link); 206 } 207 208 static void 209 vmbus_chan_rem_list(struct vmbus_softc *sc, struct vmbus_channel *chan) 210 { 211 212 mtx_assert(&sc->vmbus_chan_lock, MA_OWNED); 213 if (atomic_testandclear_int(&chan->ch_stflags, 214 VMBUS_CHAN_ST_ONLIST_SHIFT) == 0) 215 panic("channel is not on the list"); 216 TAILQ_REMOVE(&sc->vmbus_chans, chan, ch_link); 217 } 218 219 static int 220 vmbus_chan_sysctl_mnf(SYSCTL_HANDLER_ARGS) 221 { 222 struct vmbus_channel *chan = arg1; 223 int mnf = 0; 224 225 if (chan->ch_txflags & VMBUS_CHAN_TXF_HASMNF) 226 mnf = 1; 227 return sysctl_handle_int(oidp, &mnf, 0, req); 228 } 229 230 static void 231 vmbus_chan_sysctl_create(struct vmbus_channel *chan) 232 { 233 struct sysctl_oid *ch_tree, *chid_tree, *br_tree; 234 struct sysctl_ctx_list *ctx; 235 uint32_t ch_id; 236 char name[16]; 237 238 /* 239 * Add sysctl nodes related to this channel to this 240 * channel's sysctl ctx, so that they can be destroyed 241 * independently upon close of this channel, which can 242 * happen even if the device is not detached. 243 */ 244 ctx = &chan->ch_sysctl_ctx; 245 sysctl_ctx_init(ctx); 246 247 /* 248 * Create dev.NAME.UNIT.channel tree. 249 */ 250 ch_tree = SYSCTL_ADD_NODE(ctx, 251 SYSCTL_CHILDREN(device_get_sysctl_tree(chan->ch_dev)), 252 OID_AUTO, "channel", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 253 if (ch_tree == NULL) 254 return; 255 256 /* 257 * Create dev.NAME.UNIT.channel.CHANID tree. 258 */ 259 if (VMBUS_CHAN_ISPRIMARY(chan)) 260 ch_id = chan->ch_id; 261 else 262 ch_id = chan->ch_prichan->ch_id; 263 snprintf(name, sizeof(name), "%d", ch_id); 264 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree), 265 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 266 if (chid_tree == NULL) 267 return; 268 269 if (!VMBUS_CHAN_ISPRIMARY(chan)) { 270 /* 271 * Create dev.NAME.UNIT.channel.CHANID.sub tree. 272 */ 273 ch_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree), 274 OID_AUTO, "sub", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 275 if (ch_tree == NULL) 276 return; 277 278 /* 279 * Create dev.NAME.UNIT.channel.CHANID.sub.SUBIDX tree. 280 * 281 * NOTE: 282 * chid_tree is changed to this new sysctl tree. 283 */ 284 snprintf(name, sizeof(name), "%d", chan->ch_subidx); 285 chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree), 286 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 287 if (chid_tree == NULL) 288 return; 289 290 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO, 291 "chanid", CTLFLAG_RD, &chan->ch_id, 0, "channel id"); 292 } 293 294 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO, 295 "cpu", CTLFLAG_RD, &chan->ch_cpuid, 0, "owner CPU id"); 296 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO, 297 "mnf", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 298 chan, 0, vmbus_chan_sysctl_mnf, "I", 299 "has monitor notification facilities"); 300 301 br_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO, 302 "br", CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 303 if (br_tree != NULL) { 304 /* 305 * Create sysctl tree for RX bufring. 306 */ 307 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_rxbr.rxbr, "rx"); 308 /* 309 * Create sysctl tree for TX bufring. 310 */ 311 vmbus_br_sysctl_create(ctx, br_tree, &chan->ch_txbr.txbr, "tx"); 312 } 313 } 314 315 int 316 vmbus_chan_open(struct vmbus_channel *chan, int txbr_size, int rxbr_size, 317 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg) 318 { 319 struct vmbus_chan_br cbr; 320 int error; 321 322 /* 323 * Allocate the TX+RX bufrings. 324 */ 325 KASSERT(chan->ch_bufring == NULL, ("bufrings are allocated")); 326 chan->ch_bufring = hyperv_dmamem_alloc(bus_get_dma_tag(chan->ch_dev), 327 PAGE_SIZE, 0, txbr_size + rxbr_size, &chan->ch_bufring_dma, 328 BUS_DMA_WAITOK); 329 if (chan->ch_bufring == NULL) { 330 vmbus_chan_printf(chan, "bufring allocation failed\n"); 331 return (ENOMEM); 332 } 333 334 cbr.cbr = chan->ch_bufring; 335 cbr.cbr_paddr = chan->ch_bufring_dma.hv_paddr; 336 cbr.cbr_txsz = txbr_size; 337 cbr.cbr_rxsz = rxbr_size; 338 339 error = vmbus_chan_open_br(chan, &cbr, udata, udlen, cb, cbarg); 340 if (error) { 341 if (error == EISCONN) { 342 /* 343 * XXX 344 * The bufring GPADL is still connected; abandon 345 * this bufring, instead of having mysterious 346 * crash or trashed data later on. 347 */ 348 vmbus_chan_printf(chan, "chan%u bufring GPADL " 349 "is still connected upon channel open error; " 350 "leak %d bytes memory\n", chan->ch_id, 351 txbr_size + rxbr_size); 352 } else { 353 hyperv_dmamem_free(&chan->ch_bufring_dma, 354 chan->ch_bufring); 355 } 356 chan->ch_bufring = NULL; 357 } 358 return (error); 359 } 360 361 int 362 vmbus_chan_open_br(struct vmbus_channel *chan, const struct vmbus_chan_br *cbr, 363 const void *udata, int udlen, vmbus_chan_callback_t cb, void *cbarg) 364 { 365 struct vmbus_softc *sc = chan->ch_vmbus; 366 const struct vmbus_message *msg; 367 struct vmbus_chanmsg_chopen *req; 368 struct vmbus_msghc *mh; 369 uint32_t status; 370 int error, txbr_size, rxbr_size; 371 task_fn_t *task_fn; 372 uint8_t *br; 373 374 if (udlen > VMBUS_CHANMSG_CHOPEN_UDATA_SIZE) { 375 vmbus_chan_printf(chan, 376 "invalid udata len %d for chan%u\n", udlen, chan->ch_id); 377 return (EINVAL); 378 } 379 380 br = cbr->cbr; 381 txbr_size = cbr->cbr_txsz; 382 rxbr_size = cbr->cbr_rxsz; 383 KASSERT((txbr_size & PAGE_MASK) == 0, 384 ("send bufring size is not multiple page")); 385 KASSERT((rxbr_size & PAGE_MASK) == 0, 386 ("recv bufring size is not multiple page")); 387 KASSERT((cbr->cbr_paddr & PAGE_MASK) == 0, 388 ("bufring is not page aligned")); 389 390 /* 391 * Zero out the TX/RX bufrings, in case that they were used before. 392 */ 393 memset(br, 0, txbr_size + rxbr_size); 394 395 if (atomic_testandset_int(&chan->ch_stflags, 396 VMBUS_CHAN_ST_OPENED_SHIFT)) 397 panic("double-open chan%u", chan->ch_id); 398 399 chan->ch_cb = cb; 400 chan->ch_cbarg = cbarg; 401 402 vmbus_chan_update_evtflagcnt(sc, chan); 403 404 chan->ch_tq = VMBUS_PCPU_GET(chan->ch_vmbus, event_tq, chan->ch_cpuid); 405 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) 406 task_fn = vmbus_chan_task; 407 else 408 task_fn = vmbus_chan_task_nobatch; 409 TASK_INIT(&chan->ch_task, 0, task_fn, chan); 410 411 /* TX bufring comes first */ 412 vmbus_txbr_setup(&chan->ch_txbr, br, txbr_size); 413 /* RX bufring immediately follows TX bufring */ 414 vmbus_rxbr_setup(&chan->ch_rxbr, br + txbr_size, rxbr_size); 415 416 /* Create sysctl tree for this channel */ 417 vmbus_chan_sysctl_create(chan); 418 419 /* 420 * Connect the bufrings, both RX and TX, to this channel. 421 */ 422 error = vmbus_chan_gpadl_connect(chan, cbr->cbr_paddr, 423 txbr_size + rxbr_size, &chan->ch_bufring_gpadl); 424 if (error) { 425 vmbus_chan_printf(chan, 426 "failed to connect bufring GPADL to chan%u\n", chan->ch_id); 427 goto failed; 428 } 429 430 /* 431 * Install this channel, before it is opened, but after everything 432 * else has been setup. 433 */ 434 vmbus_chan_set_chmap(chan); 435 436 /* 437 * Open channel w/ the bufring GPADL on the target CPU. 438 */ 439 mh = vmbus_msghc_get(sc, sizeof(*req)); 440 if (mh == NULL) { 441 vmbus_chan_printf(chan, 442 "can not get msg hypercall for chopen(chan%u)\n", 443 chan->ch_id); 444 error = ENXIO; 445 goto failed; 446 } 447 448 req = vmbus_msghc_dataptr(mh); 449 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHOPEN; 450 req->chm_chanid = chan->ch_id; 451 req->chm_openid = chan->ch_id; 452 req->chm_gpadl = chan->ch_bufring_gpadl; 453 req->chm_vcpuid = chan->ch_vcpuid; 454 req->chm_txbr_pgcnt = txbr_size >> PAGE_SHIFT; 455 if (udlen > 0) 456 memcpy(req->chm_udata, udata, udlen); 457 458 error = vmbus_msghc_exec(sc, mh); 459 if (error) { 460 vmbus_chan_printf(chan, 461 "chopen(chan%u) msg hypercall exec failed: %d\n", 462 chan->ch_id, error); 463 vmbus_msghc_put(sc, mh); 464 goto failed; 465 } 466 467 for (;;) { 468 msg = vmbus_msghc_poll_result(sc, mh); 469 if (msg != NULL) 470 break; 471 if (vmbus_chan_is_revoked(chan)) { 472 int i; 473 474 /* 475 * NOTE: 476 * Hypervisor does _not_ send response CHOPEN to 477 * a revoked channel. 478 */ 479 vmbus_chan_printf(chan, 480 "chan%u is revoked, when it is being opened\n", 481 chan->ch_id); 482 483 /* 484 * XXX 485 * Add extra delay before cancel the hypercall 486 * execution; mainly to close any possible 487 * CHRESCIND and CHOPEN_RESP races on the 488 * hypervisor side. 489 */ 490 #define REVOKE_LINGER 100 491 for (i = 0; i < REVOKE_LINGER; ++i) { 492 msg = vmbus_msghc_poll_result(sc, mh); 493 if (msg != NULL) 494 break; 495 pause("rchopen", 1); 496 } 497 #undef REVOKE_LINGER 498 if (msg == NULL) 499 vmbus_msghc_exec_cancel(sc, mh); 500 break; 501 } 502 pause("chopen", 1); 503 } 504 if (msg != NULL) { 505 status = ((const struct vmbus_chanmsg_chopen_resp *) 506 msg->msg_data)->chm_status; 507 } else { 508 /* XXX any non-0 value is ok here. */ 509 status = 0xff; 510 } 511 512 vmbus_msghc_put(sc, mh); 513 514 if (status == 0) { 515 if (bootverbose) 516 vmbus_chan_printf(chan, "chan%u opened\n", chan->ch_id); 517 return (0); 518 } 519 520 vmbus_chan_printf(chan, "failed to open chan%u\n", chan->ch_id); 521 error = ENXIO; 522 523 failed: 524 sysctl_ctx_free(&chan->ch_sysctl_ctx); 525 vmbus_chan_clear_chmap(chan); 526 if (chan->ch_bufring_gpadl != 0) { 527 int error1; 528 529 error1 = vmbus_chan_gpadl_disconnect(chan, 530 chan->ch_bufring_gpadl); 531 if (error1) { 532 /* 533 * Give caller a hint that the bufring GPADL is still 534 * connected. 535 */ 536 error = EISCONN; 537 } 538 chan->ch_bufring_gpadl = 0; 539 } 540 atomic_clear_int(&chan->ch_stflags, VMBUS_CHAN_ST_OPENED); 541 return (error); 542 } 543 544 int 545 vmbus_chan_gpadl_connect(struct vmbus_channel *chan, bus_addr_t paddr, 546 int size, uint32_t *gpadl0) 547 { 548 struct vmbus_softc *sc = chan->ch_vmbus; 549 struct vmbus_msghc *mh; 550 struct vmbus_chanmsg_gpadl_conn *req; 551 const struct vmbus_message *msg; 552 size_t reqsz; 553 uint32_t gpadl, status; 554 int page_count, range_len, i, cnt, error; 555 uint64_t page_id; 556 557 KASSERT(*gpadl0 == 0, ("GPADL is not zero")); 558 559 /* 560 * Preliminary checks. 561 */ 562 563 KASSERT((size & PAGE_MASK) == 0, 564 ("invalid GPA size %d, not multiple page size", size)); 565 page_count = size >> PAGE_SHIFT; 566 567 KASSERT((paddr & PAGE_MASK) == 0, 568 ("GPA is not page aligned %jx", (uintmax_t)paddr)); 569 page_id = paddr >> PAGE_SHIFT; 570 571 range_len = __offsetof(struct vmbus_gpa_range, gpa_page[page_count]); 572 /* 573 * We don't support multiple GPA ranges. 574 */ 575 if (range_len > UINT16_MAX) { 576 vmbus_chan_printf(chan, "GPA too large, %d pages\n", 577 page_count); 578 return EOPNOTSUPP; 579 } 580 581 /* 582 * Allocate GPADL id. 583 */ 584 gpadl = vmbus_gpadl_alloc(sc); 585 586 /* 587 * Connect this GPADL to the target channel. 588 * 589 * NOTE: 590 * Since each message can only hold small set of page 591 * addresses, several messages may be required to 592 * complete the connection. 593 */ 594 if (page_count > VMBUS_CHANMSG_GPADL_CONN_PGMAX) 595 cnt = VMBUS_CHANMSG_GPADL_CONN_PGMAX; 596 else 597 cnt = page_count; 598 page_count -= cnt; 599 600 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_conn, 601 chm_range.gpa_page[cnt]); 602 mh = vmbus_msghc_get(sc, reqsz); 603 if (mh == NULL) { 604 vmbus_chan_printf(chan, 605 "can not get msg hypercall for gpadl_conn(chan%u)\n", 606 chan->ch_id); 607 return EIO; 608 } 609 610 req = vmbus_msghc_dataptr(mh); 611 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_CONN; 612 req->chm_chanid = chan->ch_id; 613 req->chm_gpadl = gpadl; 614 req->chm_range_len = range_len; 615 req->chm_range_cnt = 1; 616 req->chm_range.gpa_len = size; 617 req->chm_range.gpa_ofs = 0; 618 for (i = 0; i < cnt; ++i) 619 req->chm_range.gpa_page[i] = page_id++; 620 621 error = vmbus_msghc_exec(sc, mh); 622 if (error) { 623 vmbus_chan_printf(chan, 624 "gpadl_conn(chan%u) msg hypercall exec failed: %d\n", 625 chan->ch_id, error); 626 vmbus_msghc_put(sc, mh); 627 return error; 628 } 629 630 while (page_count > 0) { 631 struct vmbus_chanmsg_gpadl_subconn *subreq; 632 633 if (page_count > VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX) 634 cnt = VMBUS_CHANMSG_GPADL_SUBCONN_PGMAX; 635 else 636 cnt = page_count; 637 page_count -= cnt; 638 639 reqsz = __offsetof(struct vmbus_chanmsg_gpadl_subconn, 640 chm_gpa_page[cnt]); 641 vmbus_msghc_reset(mh, reqsz); 642 643 subreq = vmbus_msghc_dataptr(mh); 644 subreq->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_SUBCONN; 645 subreq->chm_gpadl = gpadl; 646 for (i = 0; i < cnt; ++i) 647 subreq->chm_gpa_page[i] = page_id++; 648 649 vmbus_msghc_exec_noresult(mh); 650 } 651 KASSERT(page_count == 0, ("invalid page count %d", page_count)); 652 653 msg = vmbus_msghc_wait_result(sc, mh); 654 status = ((const struct vmbus_chanmsg_gpadl_connresp *) 655 msg->msg_data)->chm_status; 656 657 vmbus_msghc_put(sc, mh); 658 659 if (status != 0) { 660 vmbus_chan_printf(chan, "gpadl_conn(chan%u) failed: %u\n", 661 chan->ch_id, status); 662 return EIO; 663 } 664 665 /* Done; commit the GPADL id. */ 666 *gpadl0 = gpadl; 667 if (bootverbose) { 668 vmbus_chan_printf(chan, "gpadl_conn(chan%u) succeeded\n", 669 chan->ch_id); 670 } 671 return 0; 672 } 673 674 static bool 675 vmbus_chan_wait_revoke(const struct vmbus_channel *chan, bool can_sleep) 676 { 677 #define WAIT_COUNT 200 /* 200ms */ 678 679 int i; 680 681 for (i = 0; i < WAIT_COUNT; ++i) { 682 if (vmbus_chan_is_revoked(chan)) 683 return (true); 684 if (can_sleep) 685 pause("wchrev", 1); 686 else 687 DELAY(1000); 688 } 689 return (false); 690 691 #undef WAIT_COUNT 692 } 693 694 /* 695 * Disconnect the GPA from the target channel 696 */ 697 int 698 vmbus_chan_gpadl_disconnect(struct vmbus_channel *chan, uint32_t gpadl) 699 { 700 struct vmbus_softc *sc = chan->ch_vmbus; 701 struct vmbus_msghc *mh; 702 struct vmbus_chanmsg_gpadl_disconn *req; 703 int error; 704 705 KASSERT(gpadl != 0, ("GPADL is zero")); 706 707 mh = vmbus_msghc_get(sc, sizeof(*req)); 708 if (mh == NULL) { 709 vmbus_chan_printf(chan, 710 "can not get msg hypercall for gpadl_disconn(chan%u)\n", 711 chan->ch_id); 712 return (EBUSY); 713 } 714 715 req = vmbus_msghc_dataptr(mh); 716 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_GPADL_DISCONN; 717 req->chm_chanid = chan->ch_id; 718 req->chm_gpadl = gpadl; 719 720 error = vmbus_msghc_exec(sc, mh); 721 if (error) { 722 vmbus_msghc_put(sc, mh); 723 724 if (vmbus_chan_wait_revoke(chan, true)) { 725 /* 726 * Error is benign; this channel is revoked, 727 * so this GPADL will not be touched anymore. 728 */ 729 vmbus_chan_printf(chan, 730 "gpadl_disconn(revoked chan%u) msg hypercall " 731 "exec failed: %d\n", chan->ch_id, error); 732 return (0); 733 } 734 vmbus_chan_printf(chan, 735 "gpadl_disconn(chan%u) msg hypercall exec failed: %d\n", 736 chan->ch_id, error); 737 return (error); 738 } 739 740 vmbus_msghc_wait_result(sc, mh); 741 /* Discard result; no useful information */ 742 vmbus_msghc_put(sc, mh); 743 744 return (0); 745 } 746 747 static void 748 vmbus_chan_detach(struct vmbus_channel *chan) 749 { 750 int refs; 751 752 KASSERT(chan->ch_refs > 0, ("chan%u: invalid refcnt %d", 753 chan->ch_id, chan->ch_refs)); 754 refs = atomic_fetchadd_int(&chan->ch_refs, -1); 755 #ifdef INVARIANTS 756 if (VMBUS_CHAN_ISPRIMARY(chan)) { 757 KASSERT(refs == 1, ("chan%u: invalid refcnt %d for prichan", 758 chan->ch_id, refs + 1)); 759 } 760 #endif 761 if (refs == 1) { 762 /* 763 * Detach the target channel. 764 */ 765 if (bootverbose) { 766 vmbus_chan_printf(chan, "chan%u detached\n", 767 chan->ch_id); 768 } 769 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task); 770 } 771 } 772 773 static void 774 vmbus_chan_clrchmap_task(void *xchan, int pending __unused) 775 { 776 struct vmbus_channel *chan = xchan; 777 778 critical_enter(); 779 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL; 780 critical_exit(); 781 } 782 783 static void 784 vmbus_chan_clear_chmap(struct vmbus_channel *chan) 785 { 786 struct task chmap_task; 787 788 TASK_INIT(&chmap_task, 0, vmbus_chan_clrchmap_task, chan); 789 vmbus_chan_run_task(chan, &chmap_task); 790 } 791 792 static void 793 vmbus_chan_set_chmap(struct vmbus_channel *chan) 794 { 795 __compiler_membar(); 796 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan; 797 } 798 799 static void 800 vmbus_chan_poll_cancel_task(void *xchan, int pending __unused) 801 { 802 803 vmbus_chan_poll_cancel_intq(xchan); 804 } 805 806 static void 807 vmbus_chan_poll_cancel(struct vmbus_channel *chan) 808 { 809 struct task poll_cancel; 810 811 TASK_INIT(&poll_cancel, 0, vmbus_chan_poll_cancel_task, chan); 812 vmbus_chan_run_task(chan, &poll_cancel); 813 } 814 815 static int 816 vmbus_chan_close_internal(struct vmbus_channel *chan) 817 { 818 struct vmbus_softc *sc = chan->ch_vmbus; 819 struct vmbus_msghc *mh; 820 struct vmbus_chanmsg_chclose *req; 821 uint32_t old_stflags; 822 int error; 823 824 /* 825 * NOTE: 826 * Sub-channels are closed upon their primary channel closing, 827 * so they can be closed even before they are opened. 828 */ 829 for (;;) { 830 old_stflags = chan->ch_stflags; 831 if (atomic_cmpset_int(&chan->ch_stflags, old_stflags, 832 old_stflags & ~VMBUS_CHAN_ST_OPENED)) 833 break; 834 } 835 if ((old_stflags & VMBUS_CHAN_ST_OPENED) == 0) { 836 /* Not opened yet; done */ 837 if (bootverbose) { 838 vmbus_chan_printf(chan, "chan%u not opened\n", 839 chan->ch_id); 840 } 841 return (0); 842 } 843 844 /* 845 * Free this channel's sysctl tree attached to its device's 846 * sysctl tree. 847 */ 848 sysctl_ctx_free(&chan->ch_sysctl_ctx); 849 850 /* 851 * Cancel polling, if it is enabled. 852 */ 853 vmbus_chan_poll_cancel(chan); 854 855 /* 856 * NOTE: 857 * Order is critical. This channel _must_ be uninstalled first, 858 * else the channel task may be enqueued by the IDT after it has 859 * been drained. 860 */ 861 vmbus_chan_clear_chmap(chan); 862 taskqueue_drain(chan->ch_tq, &chan->ch_task); 863 chan->ch_tq = NULL; 864 865 /* 866 * Close this channel. 867 */ 868 mh = vmbus_msghc_get(sc, sizeof(*req)); 869 if (mh == NULL) { 870 vmbus_chan_printf(chan, 871 "can not get msg hypercall for chclose(chan%u)\n", 872 chan->ch_id); 873 error = ENXIO; 874 goto disconnect; 875 } 876 877 req = vmbus_msghc_dataptr(mh); 878 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHCLOSE; 879 req->chm_chanid = chan->ch_id; 880 881 error = vmbus_msghc_exec_noresult(mh); 882 vmbus_msghc_put(sc, mh); 883 884 if (error) { 885 vmbus_chan_printf(chan, 886 "chclose(chan%u) msg hypercall exec failed: %d\n", 887 chan->ch_id, error); 888 goto disconnect; 889 } 890 891 if (bootverbose) 892 vmbus_chan_printf(chan, "chan%u closed\n", chan->ch_id); 893 894 disconnect: 895 /* 896 * Disconnect the TX+RX bufrings from this channel. 897 */ 898 if (chan->ch_bufring_gpadl != 0) { 899 int error1; 900 901 error1 = vmbus_chan_gpadl_disconnect(chan, 902 chan->ch_bufring_gpadl); 903 if (error1) { 904 /* 905 * XXX 906 * The bufring GPADL is still connected; abandon 907 * this bufring, instead of having mysterious 908 * crash or trashed data later on. 909 */ 910 vmbus_chan_printf(chan, "chan%u bufring GPADL " 911 "is still connected after close\n", chan->ch_id); 912 chan->ch_bufring = NULL; 913 /* 914 * Give caller a hint that the bufring GPADL is 915 * still connected. 916 */ 917 error = EISCONN; 918 } 919 chan->ch_bufring_gpadl = 0; 920 } 921 922 /* 923 * Destroy the TX+RX bufrings. 924 */ 925 if (chan->ch_bufring != NULL) { 926 hyperv_dmamem_free(&chan->ch_bufring_dma, chan->ch_bufring); 927 chan->ch_bufring = NULL; 928 } 929 return (error); 930 } 931 932 int 933 vmbus_chan_close_direct(struct vmbus_channel *chan) 934 { 935 int error; 936 937 #ifdef INVARIANTS 938 if (VMBUS_CHAN_ISPRIMARY(chan)) { 939 struct vmbus_channel *subchan; 940 941 /* 942 * All sub-channels _must_ have been closed, or are _not_ 943 * opened at all. 944 */ 945 mtx_lock(&chan->ch_subchan_lock); 946 TAILQ_FOREACH(subchan, &chan->ch_subchans, ch_sublink) { 947 KASSERT( 948 (subchan->ch_stflags & VMBUS_CHAN_ST_OPENED) == 0, 949 ("chan%u: subchan%u is still opened", 950 chan->ch_id, subchan->ch_subidx)); 951 } 952 mtx_unlock(&chan->ch_subchan_lock); 953 } 954 #endif 955 956 error = vmbus_chan_close_internal(chan); 957 if (!VMBUS_CHAN_ISPRIMARY(chan)) { 958 /* 959 * This sub-channel is referenced, when it is linked to 960 * the primary channel; drop that reference now. 961 */ 962 vmbus_chan_detach(chan); 963 } 964 return (error); 965 } 966 967 /* 968 * Caller should make sure that all sub-channels have 969 * been added to 'chan' and all to-be-closed channels 970 * are not being opened. 971 */ 972 void 973 vmbus_chan_close(struct vmbus_channel *chan) 974 { 975 int subchan_cnt; 976 977 if (!VMBUS_CHAN_ISPRIMARY(chan)) { 978 /* 979 * Sub-channel is closed when its primary channel 980 * is closed; done. 981 */ 982 return; 983 } 984 985 /* 986 * Close all sub-channels, if any. 987 */ 988 subchan_cnt = chan->ch_subchan_cnt; 989 if (subchan_cnt > 0) { 990 struct vmbus_channel **subchan; 991 int i; 992 993 subchan = vmbus_subchan_get(chan, subchan_cnt); 994 for (i = 0; i < subchan_cnt; ++i) { 995 vmbus_chan_close_internal(subchan[i]); 996 /* 997 * This sub-channel is referenced, when it is 998 * linked to the primary channel; drop that 999 * reference now. 1000 */ 1001 vmbus_chan_detach(subchan[i]); 1002 } 1003 vmbus_subchan_rel(subchan, subchan_cnt); 1004 } 1005 1006 /* Then close the primary channel. */ 1007 vmbus_chan_close_internal(chan); 1008 } 1009 1010 void 1011 vmbus_chan_intr_drain(struct vmbus_channel *chan) 1012 { 1013 1014 taskqueue_drain(chan->ch_tq, &chan->ch_task); 1015 } 1016 1017 int 1018 vmbus_chan_send(struct vmbus_channel *chan, uint16_t type, uint16_t flags, 1019 void *data, int dlen, uint64_t xactid) 1020 { 1021 struct vmbus_chanpkt pkt; 1022 int pktlen, pad_pktlen, hlen, error; 1023 uint64_t pad = 0; 1024 struct iovec iov[3]; 1025 boolean_t send_evt; 1026 1027 hlen = sizeof(pkt); 1028 pktlen = hlen + dlen; 1029 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen); 1030 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr), 1031 ("invalid packet size %d", pad_pktlen)); 1032 1033 pkt.cp_hdr.cph_type = type; 1034 pkt.cp_hdr.cph_flags = flags; 1035 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen); 1036 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen); 1037 pkt.cp_hdr.cph_xactid = xactid; 1038 1039 iov[0].iov_base = &pkt; 1040 iov[0].iov_len = hlen; 1041 iov[1].iov_base = data; 1042 iov[1].iov_len = dlen; 1043 iov[2].iov_base = &pad; 1044 iov[2].iov_len = pad_pktlen - pktlen; 1045 1046 error = vmbus_txbr_write(&chan->ch_txbr, iov, 3, &send_evt); 1047 if (!error && send_evt) 1048 vmbus_chan_signal_tx(chan); 1049 return error; 1050 } 1051 1052 int 1053 vmbus_chan_send_sglist(struct vmbus_channel *chan, 1054 struct vmbus_gpa sg[], int sglen, void *data, int dlen, uint64_t xactid) 1055 { 1056 struct vmbus_chanpkt_sglist pkt; 1057 int pktlen, pad_pktlen, hlen, error; 1058 struct iovec iov[4]; 1059 boolean_t send_evt; 1060 uint64_t pad = 0; 1061 1062 hlen = __offsetof(struct vmbus_chanpkt_sglist, cp_gpa[sglen]); 1063 pktlen = hlen + dlen; 1064 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen); 1065 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr), 1066 ("invalid packet size %d", pad_pktlen)); 1067 1068 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA; 1069 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC; 1070 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen); 1071 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen); 1072 pkt.cp_hdr.cph_xactid = xactid; 1073 pkt.cp_rsvd = 0; 1074 pkt.cp_gpa_cnt = sglen; 1075 1076 iov[0].iov_base = &pkt; 1077 iov[0].iov_len = sizeof(pkt); 1078 iov[1].iov_base = sg; 1079 iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen; 1080 iov[2].iov_base = data; 1081 iov[2].iov_len = dlen; 1082 iov[3].iov_base = &pad; 1083 iov[3].iov_len = pad_pktlen - pktlen; 1084 1085 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt); 1086 if (!error && send_evt) 1087 vmbus_chan_signal_tx(chan); 1088 return error; 1089 } 1090 1091 int 1092 vmbus_chan_send_prplist(struct vmbus_channel *chan, 1093 struct vmbus_gpa_range *prp, int prp_cnt, void *data, int dlen, 1094 uint64_t xactid) 1095 { 1096 struct vmbus_chanpkt_prplist pkt; 1097 int pktlen, pad_pktlen, hlen, error; 1098 struct iovec iov[4]; 1099 boolean_t send_evt; 1100 uint64_t pad = 0; 1101 1102 hlen = __offsetof(struct vmbus_chanpkt_prplist, 1103 cp_range[0].gpa_page[prp_cnt]); 1104 pktlen = hlen + dlen; 1105 pad_pktlen = VMBUS_CHANPKT_TOTLEN(pktlen); 1106 KASSERT(pad_pktlen <= vmbus_txbr_maxpktsz(&chan->ch_txbr), 1107 ("invalid packet size %d", pad_pktlen)); 1108 1109 pkt.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA; 1110 pkt.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC; 1111 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_hlen, hlen); 1112 VMBUS_CHANPKT_SETLEN(pkt.cp_hdr.cph_tlen, pad_pktlen); 1113 pkt.cp_hdr.cph_xactid = xactid; 1114 pkt.cp_rsvd = 0; 1115 pkt.cp_range_cnt = 1; 1116 1117 iov[0].iov_base = &pkt; 1118 iov[0].iov_len = sizeof(pkt); 1119 iov[1].iov_base = prp; 1120 iov[1].iov_len = __offsetof(struct vmbus_gpa_range, gpa_page[prp_cnt]); 1121 iov[2].iov_base = data; 1122 iov[2].iov_len = dlen; 1123 iov[3].iov_base = &pad; 1124 iov[3].iov_len = pad_pktlen - pktlen; 1125 1126 error = vmbus_txbr_write(&chan->ch_txbr, iov, 4, &send_evt); 1127 if (!error && send_evt) 1128 vmbus_chan_signal_tx(chan); 1129 return error; 1130 } 1131 1132 int 1133 vmbus_chan_recv(struct vmbus_channel *chan, void *data, int *dlen0, 1134 uint64_t *xactid) 1135 { 1136 struct vmbus_chanpkt_hdr pkt; 1137 int error, dlen, hlen; 1138 1139 error = vmbus_rxbr_peek(&chan->ch_rxbr, &pkt, sizeof(pkt)); 1140 if (error) 1141 return (error); 1142 1143 if (__predict_false(pkt.cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) { 1144 vmbus_chan_printf(chan, "invalid hlen %u\n", pkt.cph_hlen); 1145 /* XXX this channel is dead actually. */ 1146 return (EIO); 1147 } 1148 if (__predict_false(pkt.cph_hlen > pkt.cph_tlen)) { 1149 vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n", 1150 pkt.cph_hlen, pkt.cph_tlen); 1151 /* XXX this channel is dead actually. */ 1152 return (EIO); 1153 } 1154 1155 hlen = VMBUS_CHANPKT_GETLEN(pkt.cph_hlen); 1156 dlen = VMBUS_CHANPKT_GETLEN(pkt.cph_tlen) - hlen; 1157 1158 if (*dlen0 < dlen) { 1159 /* Return the size of this packet's data. */ 1160 *dlen0 = dlen; 1161 return (ENOBUFS); 1162 } 1163 1164 *xactid = pkt.cph_xactid; 1165 *dlen0 = dlen; 1166 1167 /* Skip packet header */ 1168 error = vmbus_rxbr_read(&chan->ch_rxbr, data, dlen, hlen); 1169 KASSERT(!error, ("vmbus_rxbr_read failed")); 1170 1171 return (0); 1172 } 1173 1174 int 1175 vmbus_chan_recv_pkt(struct vmbus_channel *chan, 1176 struct vmbus_chanpkt_hdr *pkt, int *pktlen0) 1177 { 1178 int error, pktlen, pkt_hlen; 1179 1180 pkt_hlen = sizeof(*pkt); 1181 error = vmbus_rxbr_peek(&chan->ch_rxbr, pkt, pkt_hlen); 1182 if (error) 1183 return (error); 1184 1185 if (__predict_false(pkt->cph_hlen < VMBUS_CHANPKT_HLEN_MIN)) { 1186 vmbus_chan_printf(chan, "invalid hlen %u\n", pkt->cph_hlen); 1187 /* XXX this channel is dead actually. */ 1188 return (EIO); 1189 } 1190 if (__predict_false(pkt->cph_hlen > pkt->cph_tlen)) { 1191 vmbus_chan_printf(chan, "invalid hlen %u and tlen %u\n", 1192 pkt->cph_hlen, pkt->cph_tlen); 1193 /* XXX this channel is dead actually. */ 1194 return (EIO); 1195 } 1196 1197 pktlen = VMBUS_CHANPKT_GETLEN(pkt->cph_tlen); 1198 if (*pktlen0 < pktlen) { 1199 /* Return the size of this packet. */ 1200 *pktlen0 = pktlen; 1201 return (ENOBUFS); 1202 } 1203 *pktlen0 = pktlen; 1204 1205 /* 1206 * Skip the fixed-size packet header, which has been filled 1207 * by the above vmbus_rxbr_peek(). 1208 */ 1209 error = vmbus_rxbr_read(&chan->ch_rxbr, pkt + 1, 1210 pktlen - pkt_hlen, pkt_hlen); 1211 KASSERT(!error, ("vmbus_rxbr_read failed")); 1212 1213 return (0); 1214 } 1215 1216 static void 1217 vmbus_chan_task(void *xchan, int pending __unused) 1218 { 1219 struct vmbus_channel *chan = xchan; 1220 vmbus_chan_callback_t cb = chan->ch_cb; 1221 void *cbarg = chan->ch_cbarg; 1222 1223 KASSERT(chan->ch_poll_intvl == 0, 1224 ("chan%u: interrupted in polling mode", chan->ch_id)); 1225 1226 /* 1227 * Optimize host to guest signaling by ensuring: 1228 * 1. While reading the channel, we disable interrupts from 1229 * host. 1230 * 2. Ensure that we process all posted messages from the host 1231 * before returning from this callback. 1232 * 3. Once we return, enable signaling from the host. Once this 1233 * state is set we check to see if additional packets are 1234 * available to read. In this case we repeat the process. 1235 * 1236 * NOTE: Interrupt has been disabled in the ISR. 1237 */ 1238 for (;;) { 1239 uint32_t left; 1240 1241 cb(chan, cbarg); 1242 1243 left = vmbus_rxbr_intr_unmask(&chan->ch_rxbr); 1244 if (left == 0) { 1245 /* No more data in RX bufring; done */ 1246 break; 1247 } 1248 vmbus_rxbr_intr_mask(&chan->ch_rxbr); 1249 } 1250 } 1251 1252 static void 1253 vmbus_chan_task_nobatch(void *xchan, int pending __unused) 1254 { 1255 struct vmbus_channel *chan = xchan; 1256 1257 KASSERT(chan->ch_poll_intvl == 0, 1258 ("chan%u: interrupted in polling mode", chan->ch_id)); 1259 chan->ch_cb(chan, chan->ch_cbarg); 1260 } 1261 1262 static void 1263 vmbus_chan_poll_timeout(void *xchan) 1264 { 1265 struct vmbus_channel *chan = xchan; 1266 1267 KASSERT(chan->ch_poll_intvl != 0, 1268 ("chan%u: polling timeout in interrupt mode", chan->ch_id)); 1269 taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task); 1270 } 1271 1272 static void 1273 vmbus_chan_poll_task(void *xchan, int pending __unused) 1274 { 1275 struct vmbus_channel *chan = xchan; 1276 1277 KASSERT(chan->ch_poll_intvl != 0, 1278 ("chan%u: polling in interrupt mode", chan->ch_id)); 1279 callout_reset_sbt_curcpu(&chan->ch_poll_timeo, chan->ch_poll_intvl, 0, 1280 vmbus_chan_poll_timeout, chan, chan->ch_poll_flags); 1281 chan->ch_cb(chan, chan->ch_cbarg); 1282 } 1283 1284 static void 1285 vmbus_chan_pollcfg_task(void *xarg, int pending __unused) 1286 { 1287 const struct vmbus_chan_pollarg *arg = xarg; 1288 struct vmbus_channel *chan = arg->poll_chan; 1289 sbintime_t intvl; 1290 int poll_flags; 1291 1292 /* 1293 * Save polling interval. 1294 */ 1295 intvl = SBT_1S / arg->poll_hz; 1296 if (intvl == 0) 1297 intvl = 1; 1298 if (intvl == chan->ch_poll_intvl) { 1299 /* Nothing changes; done */ 1300 return; 1301 } 1302 chan->ch_poll_intvl = intvl; 1303 1304 /* Adjust callout flags. */ 1305 poll_flags = C_DIRECT_EXEC; 1306 if (arg->poll_hz <= hz) 1307 poll_flags |= C_HARDCLOCK; 1308 chan->ch_poll_flags = poll_flags; 1309 1310 /* 1311 * Disable interrupt from the RX bufring (TX bufring does not 1312 * generate interrupt to VM), and disconnect this channel from 1313 * the channel map to make sure that ISR can not enqueue this 1314 * channel task anymore. 1315 */ 1316 critical_enter(); 1317 vmbus_rxbr_intr_mask(&chan->ch_rxbr); 1318 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = NULL; 1319 critical_exit(); 1320 1321 /* 1322 * NOTE: 1323 * At this point, this channel task will not be enqueued by 1324 * the ISR anymore, time to cancel the pending one. 1325 */ 1326 taskqueue_cancel(chan->ch_tq, &chan->ch_task, NULL); 1327 1328 /* Kick start! */ 1329 taskqueue_enqueue(chan->ch_tq, &chan->ch_poll_task); 1330 } 1331 1332 static bool 1333 vmbus_chan_poll_cancel_intq(struct vmbus_channel *chan) 1334 { 1335 1336 if (chan->ch_poll_intvl == 0) { 1337 /* Not enabled. */ 1338 return (false); 1339 } 1340 1341 /* 1342 * Stop polling callout, so that channel polling task 1343 * will not be enqueued anymore. 1344 */ 1345 callout_drain(&chan->ch_poll_timeo); 1346 1347 /* 1348 * Disable polling by resetting polling interval. 1349 * 1350 * NOTE: 1351 * The polling interval resetting MUST be conducted 1352 * after the callout is drained; mainly to keep the 1353 * proper assertion in place. 1354 */ 1355 chan->ch_poll_intvl = 0; 1356 1357 /* 1358 * NOTE: 1359 * At this point, this channel polling task will not be 1360 * enqueued by the callout anymore, time to cancel the 1361 * pending one. 1362 */ 1363 taskqueue_cancel(chan->ch_tq, &chan->ch_poll_task, NULL); 1364 1365 /* Polling was enabled. */ 1366 return (true); 1367 } 1368 1369 static void 1370 vmbus_chan_polldis_task(void *xchan, int pending __unused) 1371 { 1372 struct vmbus_channel *chan = xchan; 1373 1374 if (!vmbus_chan_poll_cancel_intq(chan)) { 1375 /* Already disabled; done. */ 1376 return; 1377 } 1378 1379 /* 1380 * Plug this channel back to the channel map and unmask 1381 * the RX bufring interrupt. 1382 */ 1383 critical_enter(); 1384 chan->ch_vmbus->vmbus_chmap[chan->ch_id] = chan; 1385 __compiler_membar(); 1386 vmbus_rxbr_intr_unmask(&chan->ch_rxbr); 1387 critical_exit(); 1388 1389 /* 1390 * Kick start the interrupt task, just in case unmasking 1391 * interrupt races ISR. 1392 */ 1393 taskqueue_enqueue(chan->ch_tq, &chan->ch_task); 1394 } 1395 1396 static __inline void 1397 vmbus_event_flags_proc(struct vmbus_softc *sc, volatile u_long *event_flags, 1398 int flag_cnt) 1399 { 1400 int f; 1401 1402 for (f = 0; f < flag_cnt; ++f) { 1403 uint32_t chid_base; 1404 u_long flags; 1405 int chid_ofs; 1406 1407 if (event_flags[f] == 0) 1408 continue; 1409 1410 flags = atomic_swap_long(&event_flags[f], 0); 1411 chid_base = f << VMBUS_EVTFLAG_SHIFT; 1412 1413 while ((chid_ofs = ffsl(flags)) != 0) { 1414 struct vmbus_channel *chan; 1415 1416 --chid_ofs; /* NOTE: ffsl is 1-based */ 1417 flags &= ~(1UL << chid_ofs); 1418 1419 chan = sc->vmbus_chmap[chid_base + chid_ofs]; 1420 if (__predict_false(chan == NULL)) { 1421 /* Channel is closed. */ 1422 continue; 1423 } 1424 __compiler_membar(); 1425 1426 if (chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD) 1427 vmbus_rxbr_intr_mask(&chan->ch_rxbr); 1428 taskqueue_enqueue(chan->ch_tq, &chan->ch_task); 1429 } 1430 } 1431 } 1432 1433 void 1434 vmbus_event_proc(struct vmbus_softc *sc, int cpu) 1435 { 1436 struct vmbus_evtflags *eventf; 1437 1438 /* 1439 * On Host with Win8 or above, the event page can be checked directly 1440 * to get the id of the channel that has the pending interrupt. 1441 */ 1442 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 1443 vmbus_event_flags_proc(sc, eventf->evt_flags, 1444 VMBUS_PCPU_GET(sc, event_flags_cnt, cpu)); 1445 } 1446 1447 void 1448 vmbus_event_proc_compat(struct vmbus_softc *sc, int cpu) 1449 { 1450 struct vmbus_evtflags *eventf; 1451 1452 eventf = VMBUS_PCPU_GET(sc, event_flags, cpu) + VMBUS_SINT_MESSAGE; 1453 if (atomic_testandclear_long(&eventf->evt_flags[0], 0)) { 1454 vmbus_event_flags_proc(sc, sc->vmbus_rx_evtflags, 1455 VMBUS_CHAN_MAX_COMPAT >> VMBUS_EVTFLAG_SHIFT); 1456 } 1457 } 1458 1459 static void 1460 vmbus_chan_update_evtflagcnt(struct vmbus_softc *sc, 1461 const struct vmbus_channel *chan) 1462 { 1463 volatile int *flag_cnt_ptr; 1464 int flag_cnt; 1465 1466 flag_cnt = (chan->ch_id / VMBUS_EVTFLAG_LEN) + 1; 1467 flag_cnt_ptr = VMBUS_PCPU_PTR(sc, event_flags_cnt, chan->ch_cpuid); 1468 1469 for (;;) { 1470 int old_flag_cnt; 1471 1472 old_flag_cnt = *flag_cnt_ptr; 1473 if (old_flag_cnt >= flag_cnt) 1474 break; 1475 if (atomic_cmpset_int(flag_cnt_ptr, old_flag_cnt, flag_cnt)) { 1476 if (bootverbose) { 1477 vmbus_chan_printf(chan, 1478 "chan%u update cpu%d flag_cnt to %d\n", 1479 chan->ch_id, chan->ch_cpuid, flag_cnt); 1480 } 1481 break; 1482 } 1483 } 1484 } 1485 1486 static struct vmbus_channel * 1487 vmbus_chan_alloc(struct vmbus_softc *sc) 1488 { 1489 struct vmbus_channel *chan; 1490 1491 chan = malloc(sizeof(*chan), M_DEVBUF, M_WAITOK | M_ZERO); 1492 1493 chan->ch_monprm = hyperv_dmamem_alloc(bus_get_dma_tag(sc->vmbus_dev), 1494 HYPERCALL_PARAM_ALIGN, 0, sizeof(struct hyperv_mon_param), 1495 &chan->ch_monprm_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 1496 if (chan->ch_monprm == NULL) { 1497 device_printf(sc->vmbus_dev, "monprm alloc failed\n"); 1498 free(chan, M_DEVBUF); 1499 return NULL; 1500 } 1501 1502 chan->ch_refs = 1; 1503 chan->ch_vmbus = sc; 1504 mtx_init(&chan->ch_subchan_lock, "vmbus subchan", NULL, MTX_DEF); 1505 sx_init(&chan->ch_orphan_lock, "vmbus chorphan"); 1506 TAILQ_INIT(&chan->ch_subchans); 1507 vmbus_rxbr_init(&chan->ch_rxbr); 1508 vmbus_txbr_init(&chan->ch_txbr); 1509 1510 TASK_INIT(&chan->ch_poll_task, 0, vmbus_chan_poll_task, chan); 1511 callout_init(&chan->ch_poll_timeo, 1); 1512 1513 return chan; 1514 } 1515 1516 static void 1517 vmbus_chan_free(struct vmbus_channel *chan) 1518 { 1519 1520 KASSERT(TAILQ_EMPTY(&chan->ch_subchans) && chan->ch_subchan_cnt == 0, 1521 ("still owns sub-channels")); 1522 KASSERT((chan->ch_stflags & 1523 (VMBUS_CHAN_ST_OPENED | 1524 VMBUS_CHAN_ST_ONPRIL | 1525 VMBUS_CHAN_ST_ONSUBL | 1526 VMBUS_CHAN_ST_ONLIST)) == 0, ("free busy channel")); 1527 KASSERT(chan->ch_orphan_xact == NULL, 1528 ("still has orphan xact installed")); 1529 KASSERT(chan->ch_refs == 0, ("chan%u: invalid refcnt %d", 1530 chan->ch_id, chan->ch_refs)); 1531 KASSERT(chan->ch_poll_intvl == 0, ("chan%u: polling is activated", 1532 chan->ch_id)); 1533 1534 hyperv_dmamem_free(&chan->ch_monprm_dma, chan->ch_monprm); 1535 mtx_destroy(&chan->ch_subchan_lock); 1536 sx_destroy(&chan->ch_orphan_lock); 1537 vmbus_rxbr_deinit(&chan->ch_rxbr); 1538 vmbus_txbr_deinit(&chan->ch_txbr); 1539 free(chan, M_DEVBUF); 1540 } 1541 1542 static int 1543 vmbus_chan_add(struct vmbus_channel *newchan) 1544 { 1545 struct vmbus_softc *sc = newchan->ch_vmbus; 1546 struct vmbus_channel *prichan; 1547 1548 if (newchan->ch_id == 0) { 1549 /* 1550 * XXX 1551 * Chan0 will neither be processed nor should be offered; 1552 * skip it. 1553 */ 1554 device_printf(sc->vmbus_dev, "got chan0 offer, discard\n"); 1555 return EINVAL; 1556 } else if (newchan->ch_id >= VMBUS_CHAN_MAX) { 1557 device_printf(sc->vmbus_dev, "invalid chan%u offer\n", 1558 newchan->ch_id); 1559 return EINVAL; 1560 } 1561 1562 mtx_lock(&sc->vmbus_prichan_lock); 1563 TAILQ_FOREACH(prichan, &sc->vmbus_prichans, ch_prilink) { 1564 /* 1565 * Sub-channel will have the same type GUID and instance 1566 * GUID as its primary channel. 1567 */ 1568 if (memcmp(&prichan->ch_guid_type, &newchan->ch_guid_type, 1569 sizeof(struct hyperv_guid)) == 0 && 1570 memcmp(&prichan->ch_guid_inst, &newchan->ch_guid_inst, 1571 sizeof(struct hyperv_guid)) == 0) 1572 break; 1573 } 1574 if (VMBUS_CHAN_ISPRIMARY(newchan)) { 1575 if (prichan == NULL) { 1576 /* Install the new primary channel */ 1577 vmbus_chan_ins_prilist(sc, newchan); 1578 mtx_unlock(&sc->vmbus_prichan_lock); 1579 goto done; 1580 } else { 1581 mtx_unlock(&sc->vmbus_prichan_lock); 1582 device_printf(sc->vmbus_dev, 1583 "duplicated primary chan%u\n", newchan->ch_id); 1584 return EINVAL; 1585 } 1586 } else { /* Sub-channel */ 1587 if (prichan == NULL) { 1588 mtx_unlock(&sc->vmbus_prichan_lock); 1589 device_printf(sc->vmbus_dev, 1590 "no primary chan for chan%u\n", newchan->ch_id); 1591 return EINVAL; 1592 } 1593 /* 1594 * Found the primary channel for this sub-channel and 1595 * move on. 1596 * 1597 * XXX refcnt prichan 1598 */ 1599 } 1600 mtx_unlock(&sc->vmbus_prichan_lock); 1601 1602 /* 1603 * This is a sub-channel; link it with the primary channel. 1604 */ 1605 KASSERT(!VMBUS_CHAN_ISPRIMARY(newchan), 1606 ("new channel is not sub-channel")); 1607 KASSERT(prichan != NULL, ("no primary channel")); 1608 1609 /* 1610 * Reference count this sub-channel; it will be dereferenced 1611 * when this sub-channel is closed. 1612 */ 1613 KASSERT(newchan->ch_refs == 1, ("chan%u: invalid refcnt %d", 1614 newchan->ch_id, newchan->ch_refs)); 1615 atomic_add_int(&newchan->ch_refs, 1); 1616 1617 newchan->ch_prichan = prichan; 1618 newchan->ch_dev = prichan->ch_dev; 1619 1620 mtx_lock(&prichan->ch_subchan_lock); 1621 vmbus_chan_ins_sublist(prichan, newchan); 1622 mtx_unlock(&prichan->ch_subchan_lock); 1623 /* 1624 * Notify anyone that is interested in this sub-channel, 1625 * after this sub-channel is setup. 1626 */ 1627 wakeup(prichan); 1628 done: 1629 /* 1630 * Hook this channel up for later revocation. 1631 */ 1632 mtx_lock(&sc->vmbus_chan_lock); 1633 vmbus_chan_ins_list(sc, newchan); 1634 mtx_unlock(&sc->vmbus_chan_lock); 1635 1636 if (bootverbose) { 1637 vmbus_chan_printf(newchan, "chan%u subidx%u offer\n", 1638 newchan->ch_id, newchan->ch_subidx); 1639 } 1640 1641 /* Select default cpu for this channel. */ 1642 vmbus_chan_cpu_default(newchan); 1643 1644 return 0; 1645 } 1646 1647 void 1648 vmbus_chan_cpu_set(struct vmbus_channel *chan, int cpu) 1649 { 1650 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu)); 1651 1652 if (chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WS2008 || 1653 chan->ch_vmbus->vmbus_version == VMBUS_VERSION_WIN7) { 1654 /* Only cpu0 is supported */ 1655 cpu = 0; 1656 } 1657 1658 chan->ch_cpuid = cpu; 1659 chan->ch_vcpuid = VMBUS_PCPU_GET(chan->ch_vmbus, vcpuid, cpu); 1660 1661 if (bootverbose) { 1662 vmbus_chan_printf(chan, 1663 "chan%u assigned to cpu%u [vcpu%u]\n", 1664 chan->ch_id, chan->ch_cpuid, chan->ch_vcpuid); 1665 } 1666 } 1667 1668 void 1669 vmbus_chan_cpu_rr(struct vmbus_channel *chan) 1670 { 1671 static uint32_t vmbus_chan_nextcpu; 1672 int cpu; 1673 1674 cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus; 1675 vmbus_chan_cpu_set(chan, cpu); 1676 } 1677 1678 static void 1679 vmbus_chan_cpu_default(struct vmbus_channel *chan) 1680 { 1681 /* 1682 * By default, pin the channel to cpu0. Devices having 1683 * special channel-cpu mapping requirement should call 1684 * vmbus_chan_cpu_{set,rr}(). 1685 */ 1686 vmbus_chan_cpu_set(chan, 0); 1687 } 1688 1689 static void 1690 vmbus_chan_msgproc_choffer(struct vmbus_softc *sc, 1691 const struct vmbus_message *msg) 1692 { 1693 const struct vmbus_chanmsg_choffer *offer; 1694 struct vmbus_channel *chan; 1695 task_fn_t *detach_fn, *attach_fn; 1696 int error; 1697 1698 offer = (const struct vmbus_chanmsg_choffer *)msg->msg_data; 1699 1700 chan = vmbus_chan_alloc(sc); 1701 if (chan == NULL) { 1702 device_printf(sc->vmbus_dev, "allocate chan%u failed\n", 1703 offer->chm_chanid); 1704 return; 1705 } 1706 1707 chan->ch_id = offer->chm_chanid; 1708 chan->ch_subidx = offer->chm_subidx; 1709 chan->ch_guid_type = offer->chm_chtype; 1710 chan->ch_guid_inst = offer->chm_chinst; 1711 1712 /* Batch reading is on by default */ 1713 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD; 1714 1715 chan->ch_monprm->mp_connid = VMBUS_CONNID_EVENT; 1716 if (sc->vmbus_version != VMBUS_VERSION_WS2008) 1717 chan->ch_monprm->mp_connid = offer->chm_connid; 1718 1719 if (offer->chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) { 1720 int trig_idx; 1721 1722 /* 1723 * Setup MNF stuffs. 1724 */ 1725 chan->ch_txflags |= VMBUS_CHAN_TXF_HASMNF; 1726 1727 trig_idx = offer->chm_montrig / VMBUS_MONTRIG_LEN; 1728 if (trig_idx >= VMBUS_MONTRIGS_MAX) 1729 panic("invalid monitor trigger %u", offer->chm_montrig); 1730 chan->ch_montrig = 1731 &sc->vmbus_mnf2->mnf_trigs[trig_idx].mt_pending; 1732 1733 chan->ch_montrig_mask = 1734 1 << (offer->chm_montrig % VMBUS_MONTRIG_LEN); 1735 } 1736 1737 /* 1738 * Setup event flag. 1739 */ 1740 chan->ch_evtflag = 1741 &sc->vmbus_tx_evtflags[chan->ch_id >> VMBUS_EVTFLAG_SHIFT]; 1742 chan->ch_evtflag_mask = 1UL << (chan->ch_id & VMBUS_EVTFLAG_MASK); 1743 1744 /* 1745 * Setup attach and detach tasks. 1746 */ 1747 if (VMBUS_CHAN_ISPRIMARY(chan)) { 1748 chan->ch_mgmt_tq = sc->vmbus_devtq; 1749 attach_fn = vmbus_prichan_attach_task; 1750 detach_fn = vmbus_prichan_detach_task; 1751 } else { 1752 chan->ch_mgmt_tq = sc->vmbus_subchtq; 1753 attach_fn = vmbus_subchan_attach_task; 1754 detach_fn = vmbus_subchan_detach_task; 1755 } 1756 TASK_INIT(&chan->ch_attach_task, 0, attach_fn, chan); 1757 TASK_INIT(&chan->ch_detach_task, 0, detach_fn, chan); 1758 1759 error = vmbus_chan_add(chan); 1760 if (error) { 1761 device_printf(sc->vmbus_dev, "add chan%u failed: %d\n", 1762 chan->ch_id, error); 1763 atomic_subtract_int(&chan->ch_refs, 1); 1764 vmbus_chan_free(chan); 1765 return; 1766 } 1767 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_attach_task); 1768 } 1769 1770 static void 1771 vmbus_chan_msgproc_chrescind(struct vmbus_softc *sc, 1772 const struct vmbus_message *msg) 1773 { 1774 const struct vmbus_chanmsg_chrescind *note; 1775 struct vmbus_channel *chan; 1776 1777 note = (const struct vmbus_chanmsg_chrescind *)msg->msg_data; 1778 if (note->chm_chanid > VMBUS_CHAN_MAX) { 1779 device_printf(sc->vmbus_dev, "invalid revoked chan%u\n", 1780 note->chm_chanid); 1781 return; 1782 } 1783 1784 /* 1785 * Find and remove the target channel from the channel list. 1786 */ 1787 mtx_lock(&sc->vmbus_chan_lock); 1788 TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) { 1789 if (chan->ch_id == note->chm_chanid) 1790 break; 1791 } 1792 if (chan == NULL) { 1793 mtx_unlock(&sc->vmbus_chan_lock); 1794 device_printf(sc->vmbus_dev, "chan%u is not offered\n", 1795 note->chm_chanid); 1796 return; 1797 } 1798 vmbus_chan_rem_list(sc, chan); 1799 mtx_unlock(&sc->vmbus_chan_lock); 1800 1801 if (VMBUS_CHAN_ISPRIMARY(chan)) { 1802 /* 1803 * The target channel is a primary channel; remove the 1804 * target channel from the primary channel list now, 1805 * instead of later, so that it will not be found by 1806 * other sub-channel offers, which are processed in 1807 * this thread. 1808 */ 1809 mtx_lock(&sc->vmbus_prichan_lock); 1810 vmbus_chan_rem_prilist(sc, chan); 1811 mtx_unlock(&sc->vmbus_prichan_lock); 1812 } 1813 1814 /* 1815 * NOTE: 1816 * The following processing order is critical: 1817 * Set the REVOKED state flag before orphaning the installed xact. 1818 */ 1819 1820 if (atomic_testandset_int(&chan->ch_stflags, 1821 VMBUS_CHAN_ST_REVOKED_SHIFT)) 1822 panic("channel has already been revoked"); 1823 1824 sx_xlock(&chan->ch_orphan_lock); 1825 if (chan->ch_orphan_xact != NULL) 1826 vmbus_xact_ctx_orphan(chan->ch_orphan_xact); 1827 sx_xunlock(&chan->ch_orphan_lock); 1828 1829 if (bootverbose) 1830 vmbus_chan_printf(chan, "chan%u revoked\n", note->chm_chanid); 1831 vmbus_chan_detach(chan); 1832 } 1833 1834 static int 1835 vmbus_chan_release(struct vmbus_channel *chan) 1836 { 1837 struct vmbus_softc *sc = chan->ch_vmbus; 1838 struct vmbus_chanmsg_chfree *req; 1839 struct vmbus_msghc *mh; 1840 int error; 1841 1842 mh = vmbus_msghc_get(sc, sizeof(*req)); 1843 if (mh == NULL) { 1844 vmbus_chan_printf(chan, 1845 "can not get msg hypercall for chfree(chan%u)\n", 1846 chan->ch_id); 1847 return (ENXIO); 1848 } 1849 1850 req = vmbus_msghc_dataptr(mh); 1851 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE; 1852 req->chm_chanid = chan->ch_id; 1853 1854 error = vmbus_msghc_exec_noresult(mh); 1855 vmbus_msghc_put(sc, mh); 1856 1857 if (error) { 1858 vmbus_chan_printf(chan, 1859 "chfree(chan%u) msg hypercall exec failed: %d\n", 1860 chan->ch_id, error); 1861 } else { 1862 if (bootverbose) 1863 vmbus_chan_printf(chan, "chan%u freed\n", chan->ch_id); 1864 } 1865 return (error); 1866 } 1867 1868 static void 1869 vmbus_prichan_detach_task(void *xchan, int pending __unused) 1870 { 1871 struct vmbus_channel *chan = xchan; 1872 1873 KASSERT(VMBUS_CHAN_ISPRIMARY(chan), 1874 ("chan%u is not primary channel", chan->ch_id)); 1875 1876 /* Delete and detach the device associated with this channel. */ 1877 vmbus_delete_child(chan); 1878 1879 /* Release this channel (back to vmbus). */ 1880 vmbus_chan_release(chan); 1881 1882 /* Free this channel's resource. */ 1883 vmbus_chan_free(chan); 1884 } 1885 1886 static void 1887 vmbus_subchan_detach_task(void *xchan, int pending __unused) 1888 { 1889 struct vmbus_channel *chan = xchan; 1890 struct vmbus_channel *pri_chan = chan->ch_prichan; 1891 1892 KASSERT(!VMBUS_CHAN_ISPRIMARY(chan), 1893 ("chan%u is primary channel", chan->ch_id)); 1894 1895 /* Release this channel (back to vmbus). */ 1896 vmbus_chan_release(chan); 1897 1898 /* Unlink from its primary channel's sub-channel list. */ 1899 mtx_lock(&pri_chan->ch_subchan_lock); 1900 vmbus_chan_rem_sublist(pri_chan, chan); 1901 mtx_unlock(&pri_chan->ch_subchan_lock); 1902 /* Notify anyone that is waiting for this sub-channel to vanish. */ 1903 wakeup(pri_chan); 1904 1905 /* Free this channel's resource. */ 1906 vmbus_chan_free(chan); 1907 } 1908 1909 static void 1910 vmbus_prichan_attach_task(void *xchan, int pending __unused) 1911 { 1912 1913 /* 1914 * Add device for this primary channel. 1915 */ 1916 vmbus_add_child(xchan); 1917 } 1918 1919 static void 1920 vmbus_subchan_attach_task(void *xchan __unused, int pending __unused) 1921 { 1922 1923 /* Nothing */ 1924 } 1925 1926 void 1927 vmbus_chan_destroy_all(struct vmbus_softc *sc) 1928 { 1929 1930 /* 1931 * Detach all devices and destroy the corresponding primary 1932 * channels. 1933 */ 1934 for (;;) { 1935 struct vmbus_channel *chan; 1936 1937 mtx_lock(&sc->vmbus_chan_lock); 1938 TAILQ_FOREACH(chan, &sc->vmbus_chans, ch_link) { 1939 if (VMBUS_CHAN_ISPRIMARY(chan)) 1940 break; 1941 } 1942 if (chan == NULL) { 1943 /* No more primary channels; done. */ 1944 mtx_unlock(&sc->vmbus_chan_lock); 1945 break; 1946 } 1947 vmbus_chan_rem_list(sc, chan); 1948 mtx_unlock(&sc->vmbus_chan_lock); 1949 1950 mtx_lock(&sc->vmbus_prichan_lock); 1951 vmbus_chan_rem_prilist(sc, chan); 1952 mtx_unlock(&sc->vmbus_prichan_lock); 1953 1954 taskqueue_enqueue(chan->ch_mgmt_tq, &chan->ch_detach_task); 1955 } 1956 } 1957 1958 struct vmbus_channel ** 1959 vmbus_subchan_get(struct vmbus_channel *pri_chan, int subchan_cnt) 1960 { 1961 struct vmbus_channel **ret, *chan; 1962 int i; 1963 1964 KASSERT(subchan_cnt > 0, ("invalid sub-channel count %d", subchan_cnt)); 1965 1966 ret = malloc(subchan_cnt * sizeof(struct vmbus_channel *), M_TEMP, 1967 M_WAITOK); 1968 1969 mtx_lock(&pri_chan->ch_subchan_lock); 1970 1971 while (pri_chan->ch_subchan_cnt < subchan_cnt) 1972 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "subch", 0); 1973 1974 i = 0; 1975 TAILQ_FOREACH(chan, &pri_chan->ch_subchans, ch_sublink) { 1976 /* TODO: refcnt chan */ 1977 ret[i] = chan; 1978 1979 ++i; 1980 if (i == subchan_cnt) 1981 break; 1982 } 1983 KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d", 1984 pri_chan->ch_subchan_cnt, subchan_cnt)); 1985 1986 mtx_unlock(&pri_chan->ch_subchan_lock); 1987 1988 return ret; 1989 } 1990 1991 void 1992 vmbus_subchan_rel(struct vmbus_channel **subchan, int subchan_cnt __unused) 1993 { 1994 1995 free(subchan, M_TEMP); 1996 } 1997 1998 void 1999 vmbus_subchan_drain(struct vmbus_channel *pri_chan) 2000 { 2001 mtx_lock(&pri_chan->ch_subchan_lock); 2002 while (pri_chan->ch_subchan_cnt > 0) 2003 mtx_sleep(pri_chan, &pri_chan->ch_subchan_lock, 0, "dsubch", 0); 2004 mtx_unlock(&pri_chan->ch_subchan_lock); 2005 } 2006 2007 void 2008 vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg) 2009 { 2010 vmbus_chanmsg_proc_t msg_proc; 2011 uint32_t msg_type; 2012 2013 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type; 2014 KASSERT(msg_type < VMBUS_CHANMSG_TYPE_MAX, 2015 ("invalid message type %u", msg_type)); 2016 2017 msg_proc = vmbus_chan_msgprocs[msg_type]; 2018 if (msg_proc != NULL) 2019 msg_proc(sc, msg); 2020 } 2021 2022 void 2023 vmbus_chan_set_readbatch(struct vmbus_channel *chan, bool on) 2024 { 2025 if (!on) 2026 chan->ch_flags &= ~VMBUS_CHAN_FLAG_BATCHREAD; 2027 else 2028 chan->ch_flags |= VMBUS_CHAN_FLAG_BATCHREAD; 2029 } 2030 2031 uint32_t 2032 vmbus_chan_id(const struct vmbus_channel *chan) 2033 { 2034 return chan->ch_id; 2035 } 2036 2037 uint32_t 2038 vmbus_chan_subidx(const struct vmbus_channel *chan) 2039 { 2040 return chan->ch_subidx; 2041 } 2042 2043 bool 2044 vmbus_chan_is_primary(const struct vmbus_channel *chan) 2045 { 2046 if (VMBUS_CHAN_ISPRIMARY(chan)) 2047 return true; 2048 else 2049 return false; 2050 } 2051 2052 const struct hyperv_guid * 2053 vmbus_chan_guid_inst(const struct vmbus_channel *chan) 2054 { 2055 return &chan->ch_guid_inst; 2056 } 2057 2058 int 2059 vmbus_chan_prplist_nelem(int br_size, int prpcnt_max, int dlen_max) 2060 { 2061 int elem_size; 2062 2063 elem_size = __offsetof(struct vmbus_chanpkt_prplist, 2064 cp_range[0].gpa_page[prpcnt_max]); 2065 elem_size += dlen_max; 2066 elem_size = VMBUS_CHANPKT_TOTLEN(elem_size); 2067 2068 return (vmbus_br_nelem(br_size, elem_size)); 2069 } 2070 2071 bool 2072 vmbus_chan_tx_empty(const struct vmbus_channel *chan) 2073 { 2074 2075 return (vmbus_txbr_empty(&chan->ch_txbr)); 2076 } 2077 2078 bool 2079 vmbus_chan_rx_empty(const struct vmbus_channel *chan) 2080 { 2081 2082 return (vmbus_rxbr_empty(&chan->ch_rxbr)); 2083 } 2084 2085 static int 2086 vmbus_chan_printf(const struct vmbus_channel *chan, const char *fmt, ...) 2087 { 2088 va_list ap; 2089 device_t dev; 2090 int retval; 2091 2092 if (chan->ch_dev == NULL || !device_is_alive(chan->ch_dev)) 2093 dev = chan->ch_vmbus->vmbus_dev; 2094 else 2095 dev = chan->ch_dev; 2096 2097 retval = device_print_prettyname(dev); 2098 va_start(ap, fmt); 2099 retval += vprintf(fmt, ap); 2100 va_end(ap); 2101 2102 return (retval); 2103 } 2104 2105 void 2106 vmbus_chan_run_task(struct vmbus_channel *chan, struct task *task) 2107 { 2108 2109 taskqueue_enqueue(chan->ch_tq, task); 2110 taskqueue_drain(chan->ch_tq, task); 2111 } 2112 2113 struct taskqueue * 2114 vmbus_chan_mgmt_tq(const struct vmbus_channel *chan) 2115 { 2116 2117 return (chan->ch_mgmt_tq); 2118 } 2119 2120 bool 2121 vmbus_chan_is_revoked(const struct vmbus_channel *chan) 2122 { 2123 2124 if (chan->ch_stflags & VMBUS_CHAN_ST_REVOKED) 2125 return (true); 2126 return (false); 2127 } 2128 2129 void 2130 vmbus_chan_set_orphan(struct vmbus_channel *chan, struct vmbus_xact_ctx *xact) 2131 { 2132 2133 sx_xlock(&chan->ch_orphan_lock); 2134 chan->ch_orphan_xact = xact; 2135 sx_xunlock(&chan->ch_orphan_lock); 2136 } 2137 2138 void 2139 vmbus_chan_unset_orphan(struct vmbus_channel *chan) 2140 { 2141 2142 sx_xlock(&chan->ch_orphan_lock); 2143 chan->ch_orphan_xact = NULL; 2144 sx_xunlock(&chan->ch_orphan_lock); 2145 } 2146 2147 const void * 2148 vmbus_chan_xact_wait(const struct vmbus_channel *chan, 2149 struct vmbus_xact *xact, size_t *resp_len, bool can_sleep) 2150 { 2151 const void *ret; 2152 2153 if (can_sleep) 2154 ret = vmbus_xact_wait(xact, resp_len); 2155 else 2156 ret = vmbus_xact_busywait(xact, resp_len); 2157 if (vmbus_chan_is_revoked(chan)) { 2158 /* 2159 * This xact probably is interrupted, and the 2160 * interruption can race the reply reception, 2161 * so we have to make sure that there are nothing 2162 * left on the RX bufring, i.e. this xact will 2163 * not be touched, once this function returns. 2164 * 2165 * Since the hypervisor will not put more data 2166 * onto the RX bufring once the channel is revoked, 2167 * the following loop will be terminated, once all 2168 * data are drained by the driver's channel 2169 * callback. 2170 */ 2171 while (!vmbus_chan_rx_empty(chan)) { 2172 if (can_sleep) 2173 pause("chxact", 1); 2174 else 2175 DELAY(1000); 2176 } 2177 } 2178 return (ret); 2179 } 2180 2181 void 2182 vmbus_chan_poll_enable(struct vmbus_channel *chan, u_int pollhz) 2183 { 2184 struct vmbus_chan_pollarg arg; 2185 struct task poll_cfg; 2186 2187 KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD, 2188 ("enable polling on non-batch chan%u", chan->ch_id)); 2189 KASSERT(pollhz >= VMBUS_CHAN_POLLHZ_MIN && 2190 pollhz <= VMBUS_CHAN_POLLHZ_MAX, ("invalid pollhz %u", pollhz)); 2191 2192 arg.poll_chan = chan; 2193 arg.poll_hz = pollhz; 2194 TASK_INIT(&poll_cfg, 0, vmbus_chan_pollcfg_task, &arg); 2195 vmbus_chan_run_task(chan, &poll_cfg); 2196 } 2197 2198 void 2199 vmbus_chan_poll_disable(struct vmbus_channel *chan) 2200 { 2201 struct task poll_dis; 2202 2203 KASSERT(chan->ch_flags & VMBUS_CHAN_FLAG_BATCHREAD, 2204 ("disable polling on non-batch chan%u", chan->ch_id)); 2205 2206 TASK_INIT(&poll_dis, 0, vmbus_chan_polldis_task, chan); 2207 vmbus_chan_run_task(chan, &poll_dis); 2208 } 2209