1 /*- 2 * Copyright (c) 2009-2012,2016-2017 Microsoft Corp. 3 * Copyright (c) 2010-2012 Citrix Inc. 4 * Copyright (c) 2012 NetApp 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 /* 30 * Network Virtualization Service. 31 */ 32 33 #include <sys/cdefs.h> 34 #include "opt_inet6.h" 35 #include "opt_inet.h" 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/limits.h> 40 #include <sys/socket.h> 41 #include <sys/systm.h> 42 #include <sys/taskqueue.h> 43 #include <machine/atomic.h> 44 45 #include <vm/vm.h> 46 #include <vm/vm_extern.h> 47 #include <vm/pmap.h> 48 49 #include <net/ethernet.h> 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/if_media.h> 53 54 #include <netinet/in.h> 55 #include <netinet/tcp_lro.h> 56 57 #include <dev/hyperv/include/hyperv.h> 58 #include <dev/hyperv/include/vmbus.h> 59 #include <dev/hyperv/include/vmbus_xact.h> 60 61 #include <dev/hyperv/netvsc/ndis.h> 62 #include <dev/hyperv/netvsc/if_hnreg.h> 63 #include <dev/hyperv/netvsc/if_hnvar.h> 64 #include <dev/hyperv/netvsc/hn_nvs.h> 65 66 static int hn_nvs_conn_chim(struct hn_softc *); 67 static int hn_nvs_conn_rxbuf(struct hn_softc *); 68 static void hn_nvs_disconn_chim(struct hn_softc *); 69 static void hn_nvs_disconn_rxbuf(struct hn_softc *); 70 static int hn_nvs_conf_ndis(struct hn_softc *, int); 71 static int hn_nvs_init_ndis(struct hn_softc *); 72 static int hn_nvs_doinit(struct hn_softc *, uint32_t); 73 static int hn_nvs_init(struct hn_softc *); 74 static const void *hn_nvs_xact_execute(struct hn_softc *, 75 struct vmbus_xact *, void *, int, 76 size_t *, uint32_t); 77 static void hn_nvs_sent_none(struct hn_nvs_sendctx *, 78 struct hn_softc *, struct vmbus_channel *, 79 const void *, int); 80 81 struct hn_nvs_sendctx hn_nvs_sendctx_none = 82 HN_NVS_SENDCTX_INITIALIZER(hn_nvs_sent_none, NULL); 83 84 static const uint32_t hn_nvs_version[] = { 85 HN_NVS_VERSION_61, 86 HN_NVS_VERSION_6, 87 HN_NVS_VERSION_5, 88 HN_NVS_VERSION_4, 89 HN_NVS_VERSION_2, 90 HN_NVS_VERSION_1 91 }; 92 93 static const void * 94 hn_nvs_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact, 95 void *req, int reqlen, size_t *resplen0, uint32_t type) 96 { 97 struct hn_nvs_sendctx sndc; 98 size_t resplen, min_resplen = *resplen0; 99 const struct hn_nvs_hdr *hdr; 100 int error; 101 102 KASSERT(min_resplen >= sizeof(*hdr), 103 ("invalid minimum response len %zu", min_resplen)); 104 105 /* 106 * Execute the xact setup by the caller. 107 */ 108 hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact); 109 110 vmbus_xact_activate(xact); 111 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC, 112 req, reqlen, &sndc); 113 if (error) { 114 vmbus_xact_deactivate(xact); 115 return (NULL); 116 } 117 hdr = vmbus_chan_xact_wait(sc->hn_prichan, xact, &resplen, 118 HN_CAN_SLEEP(sc)); 119 120 /* 121 * Check this NVS response message. 122 */ 123 if (resplen < min_resplen) { 124 if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen); 125 return (NULL); 126 } 127 if (hdr->nvs_type != type) { 128 if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, " 129 "expect 0x%08x\n", hdr->nvs_type, type); 130 return (NULL); 131 } 132 /* All pass! */ 133 *resplen0 = resplen; 134 return (hdr); 135 } 136 137 static __inline int 138 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen) 139 { 140 141 return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE, 142 req, reqlen, &hn_nvs_sendctx_none)); 143 } 144 145 static int 146 hn_nvs_conn_rxbuf(struct hn_softc *sc) 147 { 148 struct vmbus_xact *xact = NULL; 149 struct hn_nvs_rxbuf_conn *conn; 150 const struct hn_nvs_rxbuf_connresp *resp; 151 size_t resp_len; 152 uint32_t status; 153 int error, rxbuf_size; 154 155 /* 156 * Limit RXBUF size for old NVS. 157 */ 158 if (sc->hn_nvs_ver <= HN_NVS_VERSION_2) 159 rxbuf_size = HN_RXBUF_SIZE_COMPAT; 160 else 161 rxbuf_size = HN_RXBUF_SIZE; 162 163 /* 164 * Connect the RXBUF GPADL to the primary channel. 165 * 166 * NOTE: 167 * Only primary channel has RXBUF connected to it. Sub-channels 168 * just share this RXBUF. 169 */ 170 error = vmbus_chan_gpadl_connect(sc->hn_prichan, 171 pmap_kextract((vm_offset_t)sc->hn_rxbuf), rxbuf_size, 172 &sc->hn_rxbuf_gpadl); 173 if (error) { 174 if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n", 175 error); 176 goto cleanup; 177 } 178 179 /* 180 * Connect RXBUF to NVS. 181 */ 182 183 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn)); 184 if (xact == NULL) { 185 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n"); 186 error = ENXIO; 187 goto cleanup; 188 } 189 conn = vmbus_xact_req_data(xact); 190 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN; 191 conn->nvs_gpadl = sc->hn_rxbuf_gpadl; 192 conn->nvs_sig = HN_NVS_RXBUF_SIG; 193 194 resp_len = sizeof(*resp); 195 resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len, 196 HN_NVS_TYPE_RXBUF_CONNRESP); 197 if (resp == NULL) { 198 if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n"); 199 error = EIO; 200 goto cleanup; 201 } 202 203 status = resp->nvs_status; 204 vmbus_xact_put(xact); 205 xact = NULL; 206 207 if (status != HN_NVS_STATUS_OK) { 208 if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status); 209 error = EIO; 210 goto cleanup; 211 } 212 sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED; 213 214 return (0); 215 216 cleanup: 217 if (xact != NULL) 218 vmbus_xact_put(xact); 219 hn_nvs_disconn_rxbuf(sc); 220 return (error); 221 } 222 223 static int 224 hn_nvs_conn_chim(struct hn_softc *sc) 225 { 226 struct vmbus_xact *xact = NULL; 227 struct hn_nvs_chim_conn *chim; 228 const struct hn_nvs_chim_connresp *resp; 229 size_t resp_len; 230 uint32_t status, sectsz; 231 int error; 232 233 /* 234 * Connect chimney sending buffer GPADL to the primary channel. 235 * 236 * NOTE: 237 * Only primary channel has chimney sending buffer connected to it. 238 * Sub-channels just share this chimney sending buffer. 239 */ 240 error = vmbus_chan_gpadl_connect(sc->hn_prichan, 241 pmap_kextract((vm_offset_t)sc->hn_chim), HN_CHIM_SIZE, 242 &sc->hn_chim_gpadl); 243 if (error) { 244 if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error); 245 goto cleanup; 246 } 247 248 /* 249 * Connect chimney sending buffer to NVS 250 */ 251 252 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim)); 253 if (xact == NULL) { 254 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n"); 255 error = ENXIO; 256 goto cleanup; 257 } 258 chim = vmbus_xact_req_data(xact); 259 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN; 260 chim->nvs_gpadl = sc->hn_chim_gpadl; 261 chim->nvs_sig = HN_NVS_CHIM_SIG; 262 263 resp_len = sizeof(*resp); 264 resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len, 265 HN_NVS_TYPE_CHIM_CONNRESP); 266 if (resp == NULL) { 267 if_printf(sc->hn_ifp, "exec nvs chim conn failed\n"); 268 error = EIO; 269 goto cleanup; 270 } 271 272 status = resp->nvs_status; 273 sectsz = resp->nvs_sectsz; 274 vmbus_xact_put(xact); 275 xact = NULL; 276 277 if (status != HN_NVS_STATUS_OK) { 278 if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status); 279 error = EIO; 280 goto cleanup; 281 } 282 if (sectsz == 0 || sectsz % sizeof(uint32_t) != 0) { 283 /* 284 * Can't use chimney sending buffer; done! 285 */ 286 if (sectsz == 0) { 287 if_printf(sc->hn_ifp, "zero chimney sending buffer " 288 "section size\n"); 289 } else { 290 if_printf(sc->hn_ifp, "misaligned chimney sending " 291 "buffers, section size: %u\n", sectsz); 292 } 293 sc->hn_chim_szmax = 0; 294 sc->hn_chim_cnt = 0; 295 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED; 296 return (0); 297 } 298 299 sc->hn_chim_szmax = sectsz; 300 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax; 301 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) { 302 if_printf(sc->hn_ifp, "chimney sending sections are " 303 "not properly aligned\n"); 304 } 305 if (sc->hn_chim_cnt % LONG_BIT != 0) { 306 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n", 307 sc->hn_chim_cnt % LONG_BIT); 308 } 309 310 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT; 311 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long), 312 M_DEVBUF, M_WAITOK | M_ZERO); 313 314 /* Done! */ 315 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED; 316 if (bootverbose) { 317 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n", 318 sc->hn_chim_szmax, sc->hn_chim_cnt); 319 } 320 return (0); 321 322 cleanup: 323 if (xact != NULL) 324 vmbus_xact_put(xact); 325 hn_nvs_disconn_chim(sc); 326 return (error); 327 } 328 329 static void 330 hn_nvs_disconn_rxbuf(struct hn_softc *sc) 331 { 332 int error; 333 334 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) { 335 struct hn_nvs_rxbuf_disconn disconn; 336 337 /* 338 * Disconnect RXBUF from NVS. 339 */ 340 memset(&disconn, 0, sizeof(disconn)); 341 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN; 342 disconn.nvs_sig = HN_NVS_RXBUF_SIG; 343 344 /* NOTE: No response. */ 345 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn)); 346 if (error) { 347 if_printf(sc->hn_ifp, 348 "send nvs rxbuf disconn failed: %d\n", error); 349 /* 350 * Fine for a revoked channel, since the hypervisor 351 * does not drain TX bufring for a revoked channel. 352 */ 353 if (!vmbus_chan_is_revoked(sc->hn_prichan)) 354 sc->hn_flags |= HN_FLAG_RXBUF_REF; 355 } 356 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED; 357 358 /* 359 * Wait for the hypervisor to receive this NVS request. 360 * 361 * NOTE: 362 * The TX bufring will not be drained by the hypervisor, 363 * if the primary channel is revoked. 364 */ 365 while (!vmbus_chan_tx_empty(sc->hn_prichan) && 366 !vmbus_chan_is_revoked(sc->hn_prichan)) 367 pause("waittx", 1); 368 /* 369 * Linger long enough for NVS to disconnect RXBUF. 370 */ 371 pause("lingtx", (200 * hz) / 1000); 372 } 373 374 if (vmbus_current_version < VMBUS_VERSION_WIN10 && sc->hn_rxbuf_gpadl != 0) { 375 /* 376 * Disconnect RXBUF from primary channel. 377 */ 378 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan, 379 sc->hn_rxbuf_gpadl); 380 if (error) { 381 if_printf(sc->hn_ifp, 382 "rxbuf gpadl disconn failed: %d\n", error); 383 sc->hn_flags |= HN_FLAG_RXBUF_REF; 384 } 385 sc->hn_rxbuf_gpadl = 0; 386 } 387 } 388 389 static void 390 hn_nvs_disconn_chim(struct hn_softc *sc) 391 { 392 int error; 393 394 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) { 395 struct hn_nvs_chim_disconn disconn; 396 397 /* 398 * Disconnect chimney sending buffer from NVS. 399 */ 400 memset(&disconn, 0, sizeof(disconn)); 401 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN; 402 disconn.nvs_sig = HN_NVS_CHIM_SIG; 403 404 /* NOTE: No response. */ 405 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn)); 406 if (error) { 407 if_printf(sc->hn_ifp, 408 "send nvs chim disconn failed: %d\n", error); 409 /* 410 * Fine for a revoked channel, since the hypervisor 411 * does not drain TX bufring for a revoked channel. 412 */ 413 if (!vmbus_chan_is_revoked(sc->hn_prichan)) 414 sc->hn_flags |= HN_FLAG_CHIM_REF; 415 } 416 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED; 417 418 /* 419 * Wait for the hypervisor to receive this NVS request. 420 * 421 * NOTE: 422 * The TX bufring will not be drained by the hypervisor, 423 * if the primary channel is revoked. 424 */ 425 while (!vmbus_chan_tx_empty(sc->hn_prichan) && 426 !vmbus_chan_is_revoked(sc->hn_prichan)) 427 pause("waittx", 1); 428 /* 429 * Linger long enough for NVS to disconnect chimney 430 * sending buffer. 431 */ 432 pause("lingtx", (200 * hz) / 1000); 433 } 434 435 if (vmbus_current_version < VMBUS_VERSION_WIN10 && sc->hn_chim_gpadl != 0) { 436 /* 437 * Disconnect chimney sending buffer from primary channel. 438 */ 439 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan, 440 sc->hn_chim_gpadl); 441 if (error) { 442 if_printf(sc->hn_ifp, 443 "chim gpadl disconn failed: %d\n", error); 444 sc->hn_flags |= HN_FLAG_CHIM_REF; 445 } 446 sc->hn_chim_gpadl = 0; 447 } 448 449 if (sc->hn_chim_bmap != NULL) { 450 free(sc->hn_chim_bmap, M_DEVBUF); 451 sc->hn_chim_bmap = NULL; 452 sc->hn_chim_bmap_cnt = 0; 453 } 454 } 455 456 static int 457 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver) 458 { 459 struct vmbus_xact *xact; 460 struct hn_nvs_init *init; 461 const struct hn_nvs_init_resp *resp; 462 size_t resp_len; 463 uint32_t status; 464 465 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init)); 466 if (xact == NULL) { 467 if_printf(sc->hn_ifp, "no xact for nvs init\n"); 468 return (ENXIO); 469 } 470 init = vmbus_xact_req_data(xact); 471 init->nvs_type = HN_NVS_TYPE_INIT; 472 init->nvs_ver_min = nvs_ver; 473 init->nvs_ver_max = nvs_ver; 474 475 resp_len = sizeof(*resp); 476 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len, 477 HN_NVS_TYPE_INIT_RESP); 478 if (resp == NULL) { 479 if_printf(sc->hn_ifp, "exec init failed\n"); 480 vmbus_xact_put(xact); 481 return (EIO); 482 } 483 484 status = resp->nvs_status; 485 vmbus_xact_put(xact); 486 487 if (status != HN_NVS_STATUS_OK) { 488 if (bootverbose) { 489 /* 490 * Caller may try another NVS version, and will log 491 * error if there are no more NVS versions to try, 492 * so don't bark out loud here. 493 */ 494 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n", 495 nvs_ver); 496 } 497 return (EINVAL); 498 } 499 return (0); 500 } 501 502 /* 503 * Configure MTU and enable VLAN. 504 */ 505 static int 506 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu) 507 { 508 struct hn_nvs_ndis_conf conf; 509 int error; 510 511 memset(&conf, 0, sizeof(conf)); 512 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF; 513 conf.nvs_mtu = mtu + ETHER_HDR_LEN; 514 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN; 515 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5) 516 conf.nvs_caps |= HN_NVS_NDIS_CONF_SRIOV; 517 if (sc->hn_nvs_ver >= HN_NVS_VERSION_61) 518 conf.nvs_caps |= HN_NVS_NDIS_CONF_RSC; 519 520 521 /* NOTE: No response. */ 522 error = hn_nvs_req_send(sc, &conf, sizeof(conf)); 523 if (error) { 524 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error); 525 return (error); 526 } 527 528 if (bootverbose) 529 if_printf(sc->hn_ifp, "nvs ndis conf done\n"); 530 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN; 531 return (0); 532 } 533 534 static int 535 hn_nvs_init_ndis(struct hn_softc *sc) 536 { 537 struct hn_nvs_ndis_init ndis; 538 int error; 539 540 memset(&ndis, 0, sizeof(ndis)); 541 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT; 542 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver); 543 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver); 544 545 /* NOTE: No response. */ 546 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis)); 547 if (error) 548 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error); 549 return (error); 550 } 551 552 static int 553 hn_nvs_init(struct hn_softc *sc) 554 { 555 int i, error; 556 557 if (device_is_attached(sc->hn_dev)) { 558 /* 559 * NVS version and NDIS version MUST NOT be changed. 560 */ 561 if (bootverbose) { 562 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, " 563 "NDIS version %u.%u\n", sc->hn_nvs_ver, 564 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver), 565 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver)); 566 } 567 568 error = hn_nvs_doinit(sc, sc->hn_nvs_ver); 569 if (error) { 570 if_printf(sc->hn_ifp, "reinit NVS version 0x%x " 571 "failed: %d\n", sc->hn_nvs_ver, error); 572 return (error); 573 } 574 goto done; 575 } 576 577 /* 578 * Find the supported NVS version and set NDIS version accordingly. 579 */ 580 for (i = 0; i < nitems(hn_nvs_version); ++i) { 581 error = hn_nvs_doinit(sc, hn_nvs_version[i]); 582 if (!error) { 583 sc->hn_nvs_ver = hn_nvs_version[i]; 584 585 /* Set NDIS version according to NVS version. */ 586 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30; 587 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4) 588 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1; 589 590 if (bootverbose) { 591 if_printf(sc->hn_ifp, "NVS version 0x%x, " 592 "NDIS version %u.%u\n", sc->hn_nvs_ver, 593 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver), 594 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver)); 595 } 596 goto done; 597 } 598 } 599 if_printf(sc->hn_ifp, "no NVS available\n"); 600 return (ENXIO); 601 602 done: 603 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5) 604 sc->hn_caps |= HN_CAP_HASHVAL; 605 return (0); 606 } 607 608 int 609 hn_nvs_attach(struct hn_softc *sc, int mtu) 610 { 611 int error; 612 613 if (hyperv_ver_major >= 10) { 614 /* UDP 4-tuple hash is enforced. */ 615 sc->hn_caps |= HN_CAP_UDPHASH; 616 } 617 618 /* 619 * Initialize NVS. 620 */ 621 error = hn_nvs_init(sc); 622 if (error) 623 return (error); 624 625 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) { 626 /* 627 * Configure NDIS before initializing it. 628 */ 629 error = hn_nvs_conf_ndis(sc, mtu); 630 if (error) 631 return (error); 632 } 633 634 /* 635 * Initialize NDIS. 636 */ 637 error = hn_nvs_init_ndis(sc); 638 if (error) 639 return (error); 640 641 /* 642 * Connect RXBUF. 643 */ 644 error = hn_nvs_conn_rxbuf(sc); 645 if (error) 646 return (error); 647 648 /* 649 * Connect chimney sending buffer. 650 */ 651 error = hn_nvs_conn_chim(sc); 652 if (error) { 653 hn_nvs_disconn_rxbuf(sc); 654 return (error); 655 } 656 return (0); 657 } 658 659 void 660 hn_nvs_detach(struct hn_softc *sc) 661 { 662 663 /* NOTE: there are no requests to stop the NVS. */ 664 hn_nvs_disconn_rxbuf(sc); 665 hn_nvs_disconn_chim(sc); 666 } 667 668 void 669 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc, 670 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused, 671 const void *data, int dlen) 672 { 673 674 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen); 675 } 676 677 static void 678 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused, 679 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused, 680 const void *data __unused, int dlen __unused) 681 { 682 /* EMPTY */ 683 } 684 685 int 686 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0) 687 { 688 struct vmbus_xact *xact; 689 struct hn_nvs_subch_req *req; 690 const struct hn_nvs_subch_resp *resp; 691 int error, nsubch_req; 692 uint32_t nsubch; 693 size_t resp_len; 694 695 nsubch_req = *nsubch0; 696 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req)); 697 698 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req)); 699 if (xact == NULL) { 700 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n"); 701 return (ENXIO); 702 } 703 req = vmbus_xact_req_data(xact); 704 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ; 705 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC; 706 req->nvs_nsubch = nsubch_req; 707 708 resp_len = sizeof(*resp); 709 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len, 710 HN_NVS_TYPE_SUBCH_RESP); 711 if (resp == NULL) { 712 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n"); 713 error = EIO; 714 goto done; 715 } 716 if (resp->nvs_status != HN_NVS_STATUS_OK) { 717 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n", 718 resp->nvs_status); 719 error = EIO; 720 goto done; 721 } 722 723 nsubch = resp->nvs_nsubch; 724 if (nsubch > nsubch_req) { 725 if_printf(sc->hn_ifp, "%u subchans are allocated, " 726 "requested %d\n", nsubch, nsubch_req); 727 nsubch = nsubch_req; 728 } 729 *nsubch0 = nsubch; 730 error = 0; 731 done: 732 vmbus_xact_put(xact); 733 return (error); 734 } 735 736 int 737 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan, 738 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt) 739 { 740 741 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL, 742 sndc, gpa, gpa_cnt); 743 } 744 745 int 746 hn_nvs_set_datapath(struct hn_softc *sc, uint32_t path) 747 { 748 struct hn_nvs_datapath *dp; 749 struct hn_nvs_sendctx sndc; 750 struct vmbus_xact *xact; 751 size_t resplen; 752 int error; 753 754 if (path == HN_NVS_DATAPATH_VF && 755 !(atomic_load_acq_int(&sc->hn_vf_assoc) & HN_VF_ASSOC_ALLOCATED)) 756 return (EAGAIN); 757 758 xact = vmbus_xact_get(sc->hn_xact, sizeof(*dp)); 759 if (xact == NULL) { 760 error = ENXIO; 761 goto failed; 762 } 763 dp = vmbus_xact_req_data(xact); 764 memset(dp, 0, sizeof(*dp)); 765 dp->nvs_type = HN_NVS_TYPE_SET_DATAPATH; 766 dp->nvs_active_path = path; 767 hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact); 768 vmbus_xact_activate(xact); 769 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC, 770 dp, sizeof(*dp), &sndc); 771 if (error) { 772 vmbus_xact_deactivate(xact); 773 } else { 774 /* No NVS response: the empty VMBus completion confirms the switch. */ 775 vmbus_chan_xact_wait(sc->hn_prichan, xact, &resplen, 776 HN_CAN_SLEEP(sc)); 777 if (vmbus_chan_is_revoked(sc->hn_prichan)) 778 error = ENXIO; 779 else if (resplen != 0) 780 error = EIO; 781 } 782 vmbus_xact_put(xact); 783 if (error == 0) 784 return (0); 785 786 failed: 787 if_printf(sc->hn_ifp, "datapath switch to %s failed: %d\n", 788 path == HN_NVS_DATAPATH_VF ? "VF" : "synthetic", error); 789 return (error); 790 } 791