1/*- 2 * Copyright (c) 2009-2012,2016 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 * $FreeBSD: releng/11.0/sys/dev/hyperv/netvsc/hv_net_vsc.c 298615 2016-04-26 05:08:55Z sephe $ 29 */ 30 31/** 32 * HyperV vmbus network VSC (virtual services client) module 33 * 34 */ 35 36 37#include <sys/param.h> 38#include <sys/kernel.h> 39#include <sys/socket.h> 40#include <sys/lock.h> 41#include <net/if.h> 42#include <net/if_var.h> 43#include <net/if_arp.h> 44#include <machine/bus.h> 45#include <machine/atomic.h> 46 47#include <dev/hyperv/include/hyperv.h> 48#include "hv_net_vsc.h" 49#include "hv_rndis.h" 50#include "hv_rndis_filter.h" 51 52/* priv1 and priv2 are consumed by the main driver */ 53#define hv_chan_rdbuf hv_chan_priv3 54 55MALLOC_DEFINE(M_NETVSC, "netvsc", "Hyper-V netvsc driver"); 56 57/* 58 * Forward declarations 59 */ 60static void hv_nv_on_channel_callback(void *xchan); 61static int hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device); 62static int hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device); 63static int hv_nv_destroy_send_buffer(netvsc_dev *net_dev); 64static int hv_nv_destroy_rx_buffer(netvsc_dev *net_dev); 65static int hv_nv_connect_to_vsp(struct hv_device *device); 66static void hv_nv_on_send_completion(netvsc_dev *net_dev, 67 struct hv_device *device, struct hv_vmbus_channel *, hv_vm_packet_descriptor *pkt); 68static void hv_nv_on_receive_completion(struct hv_vmbus_channel *chan, 69 uint64_t tid, uint32_t status); 70static void hv_nv_on_receive(netvsc_dev *net_dev, 71 struct hv_device *device, struct hv_vmbus_channel *chan, 72 hv_vm_packet_descriptor *pkt); 73 74/* 75 * 76 */ 77static inline netvsc_dev * 78hv_nv_alloc_net_device(struct hv_device *device) 79{ 80 netvsc_dev *net_dev; 81 hn_softc_t *sc = device_get_softc(device->device); 82 83 net_dev = malloc(sizeof(netvsc_dev), M_NETVSC, M_WAITOK | M_ZERO); 84 85 net_dev->dev = device; 86 net_dev->destroy = FALSE; 87 sc->net_dev = net_dev; 88 89 return (net_dev); 90} 91 92/* 93 * 94 */ 95static inline netvsc_dev * 96hv_nv_get_outbound_net_device(struct hv_device *device) 97{ 98 hn_softc_t *sc = device_get_softc(device->device); 99 netvsc_dev *net_dev = sc->net_dev;; 100 101 if ((net_dev != NULL) && net_dev->destroy) { 102 return (NULL); 103 } 104 105 return (net_dev); 106} 107 108/* 109 * 110 */ 111static inline netvsc_dev * 112hv_nv_get_inbound_net_device(struct hv_device *device) 113{ 114 hn_softc_t *sc = device_get_softc(device->device); 115 netvsc_dev *net_dev = sc->net_dev;; 116 117 if (net_dev == NULL) { 118 return (net_dev); 119 } 120 /* 121 * When the device is being destroyed; we only 122 * permit incoming packets if and only if there 123 * are outstanding sends. 124 */ 125 if (net_dev->destroy) { 126 return (NULL); 127 } 128 129 return (net_dev); 130} 131 132int 133hv_nv_get_next_send_section(netvsc_dev *net_dev) 134{ 135 unsigned long bitsmap_words = net_dev->bitsmap_words; 136 unsigned long *bitsmap = net_dev->send_section_bitsmap; 137 unsigned long idx; 138 int ret = NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX; 139 int i; 140 141 for (i = 0; i < bitsmap_words; i++) { 142 idx = ffsl(~bitsmap[i]); 143 if (0 == idx) 144 continue; 145 146 idx--; 147 KASSERT(i * BITS_PER_LONG + idx < net_dev->send_section_count, 148 ("invalid i %d and idx %lu", i, idx)); 149 150 if (atomic_testandset_long(&bitsmap[i], idx)) 151 continue; 152 153 ret = i * BITS_PER_LONG + idx; 154 break; 155 } 156 157 return (ret); 158} 159 160/* 161 * Net VSC initialize receive buffer with net VSP 162 * 163 * Net VSP: Network virtual services client, also known as the 164 * Hyper-V extensible switch and the synthetic data path. 165 */ 166static int 167hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device) 168{ 169 netvsc_dev *net_dev; 170 nvsp_msg *init_pkt; 171 int ret = 0; 172 173 net_dev = hv_nv_get_outbound_net_device(device); 174 if (!net_dev) { 175 return (ENODEV); 176 } 177 178 net_dev->rx_buf = contigmalloc(net_dev->rx_buf_size, M_NETVSC, 179 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0); 180 181 /* 182 * Establish the GPADL handle for this buffer on this channel. 183 * Note: This call uses the vmbus connection rather than the 184 * channel to establish the gpadl handle. 185 * GPADL: Guest physical address descriptor list. 186 */ 187 ret = hv_vmbus_channel_establish_gpadl( 188 device->channel, net_dev->rx_buf, 189 net_dev->rx_buf_size, &net_dev->rx_buf_gpadl_handle); 190 if (ret != 0) { 191 goto cleanup; 192 } 193 194 /* sema_wait(&ext->channel_init_sema); KYS CHECK */ 195 196 /* Notify the NetVsp of the gpadl handle */ 197 init_pkt = &net_dev->channel_init_packet; 198 199 memset(init_pkt, 0, sizeof(nvsp_msg)); 200 201 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_rx_buf; 202 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle = 203 net_dev->rx_buf_gpadl_handle; 204 init_pkt->msgs.vers_1_msgs.send_rx_buf.id = 205 NETVSC_RECEIVE_BUFFER_ID; 206 207 /* Send the gpadl notification request */ 208 209 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt, 210 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt, 211 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 212 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 213 if (ret != 0) { 214 goto cleanup; 215 } 216 217 sema_wait(&net_dev->channel_init_sema); 218 219 /* Check the response */ 220 if (init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.status 221 != nvsp_status_success) { 222 ret = EINVAL; 223 goto cleanup; 224 } 225 226 net_dev->rx_section_count = 227 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.num_sections; 228 229 net_dev->rx_sections = malloc(net_dev->rx_section_count * 230 sizeof(nvsp_1_rx_buf_section), M_NETVSC, M_WAITOK); 231 memcpy(net_dev->rx_sections, 232 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.sections, 233 net_dev->rx_section_count * sizeof(nvsp_1_rx_buf_section)); 234 235 236 /* 237 * For first release, there should only be 1 section that represents 238 * the entire receive buffer 239 */ 240 if (net_dev->rx_section_count != 1 241 || net_dev->rx_sections->offset != 0) { 242 ret = EINVAL; 243 goto cleanup; 244 } 245 246 goto exit; 247 248cleanup: 249 hv_nv_destroy_rx_buffer(net_dev); 250 251exit: 252 return (ret); 253} 254 255/* 256 * Net VSC initialize send buffer with net VSP 257 */ 258static int 259hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device) 260{ 261 netvsc_dev *net_dev; 262 nvsp_msg *init_pkt; 263 int ret = 0; 264 265 net_dev = hv_nv_get_outbound_net_device(device); 266 if (!net_dev) { 267 return (ENODEV); 268 } 269 270 net_dev->send_buf = contigmalloc(net_dev->send_buf_size, M_NETVSC, 271 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0); 272 if (net_dev->send_buf == NULL) { 273 ret = ENOMEM; 274 goto cleanup; 275 } 276 277 /* 278 * Establish the gpadl handle for this buffer on this channel. 279 * Note: This call uses the vmbus connection rather than the 280 * channel to establish the gpadl handle. 281 */ 282 ret = hv_vmbus_channel_establish_gpadl(device->channel, 283 net_dev->send_buf, net_dev->send_buf_size, 284 &net_dev->send_buf_gpadl_handle); 285 if (ret != 0) { 286 goto cleanup; 287 } 288 289 /* Notify the NetVsp of the gpadl handle */ 290 291 init_pkt = &net_dev->channel_init_packet; 292 293 memset(init_pkt, 0, sizeof(nvsp_msg)); 294 295 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_send_buf; 296 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle = 297 net_dev->send_buf_gpadl_handle; 298 init_pkt->msgs.vers_1_msgs.send_rx_buf.id = 299 NETVSC_SEND_BUFFER_ID; 300 301 /* Send the gpadl notification request */ 302 303 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt, 304 sizeof(nvsp_msg), (uint64_t)init_pkt, 305 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 306 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 307 if (ret != 0) { 308 goto cleanup; 309 } 310 311 sema_wait(&net_dev->channel_init_sema); 312 313 /* Check the response */ 314 if (init_pkt->msgs.vers_1_msgs.send_send_buf_complete.status 315 != nvsp_status_success) { 316 ret = EINVAL; 317 goto cleanup; 318 } 319 320 net_dev->send_section_size = 321 init_pkt->msgs.vers_1_msgs.send_send_buf_complete.section_size; 322 net_dev->send_section_count = 323 net_dev->send_buf_size / net_dev->send_section_size; 324 net_dev->bitsmap_words = howmany(net_dev->send_section_count, 325 BITS_PER_LONG); 326 net_dev->send_section_bitsmap = 327 malloc(net_dev->bitsmap_words * sizeof(long), M_NETVSC, 328 M_WAITOK | M_ZERO); 329 330 goto exit; 331 332cleanup: 333 hv_nv_destroy_send_buffer(net_dev); 334 335exit: 336 return (ret); 337} 338 339/* 340 * Net VSC destroy receive buffer 341 */ 342static int 343hv_nv_destroy_rx_buffer(netvsc_dev *net_dev) 344{ 345 nvsp_msg *revoke_pkt; 346 int ret = 0; 347 348 /* 349 * If we got a section count, it means we received a 350 * send_rx_buf_complete msg 351 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore, 352 * we need to send a revoke msg here 353 */ 354 if (net_dev->rx_section_count) { 355 /* Send the revoke receive buffer */ 356 revoke_pkt = &net_dev->revoke_packet; 357 memset(revoke_pkt, 0, sizeof(nvsp_msg)); 358 359 revoke_pkt->hdr.msg_type = nvsp_msg_1_type_revoke_rx_buf; 360 revoke_pkt->msgs.vers_1_msgs.revoke_rx_buf.id = 361 NETVSC_RECEIVE_BUFFER_ID; 362 363 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel, 364 revoke_pkt, sizeof(nvsp_msg), 365 (uint64_t)(uintptr_t)revoke_pkt, 366 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0); 367 368 /* 369 * If we failed here, we might as well return and have a leak 370 * rather than continue and a bugchk 371 */ 372 if (ret != 0) { 373 return (ret); 374 } 375 } 376 377 /* Tear down the gpadl on the vsp end */ 378 if (net_dev->rx_buf_gpadl_handle) { 379 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel, 380 net_dev->rx_buf_gpadl_handle); 381 /* 382 * If we failed here, we might as well return and have a leak 383 * rather than continue and a bugchk 384 */ 385 if (ret != 0) { 386 return (ret); 387 } 388 net_dev->rx_buf_gpadl_handle = 0; 389 } 390 391 if (net_dev->rx_buf) { 392 /* Free up the receive buffer */ 393 contigfree(net_dev->rx_buf, net_dev->rx_buf_size, M_NETVSC); 394 net_dev->rx_buf = NULL; 395 } 396 397 if (net_dev->rx_sections) { 398 free(net_dev->rx_sections, M_NETVSC); 399 net_dev->rx_sections = NULL; 400 net_dev->rx_section_count = 0; 401 } 402 403 return (ret); 404} 405 406/* 407 * Net VSC destroy send buffer 408 */ 409static int 410hv_nv_destroy_send_buffer(netvsc_dev *net_dev) 411{ 412 nvsp_msg *revoke_pkt; 413 int ret = 0; 414 415 /* 416 * If we got a section count, it means we received a 417 * send_rx_buf_complete msg 418 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore, 419 * we need to send a revoke msg here 420 */ 421 if (net_dev->send_section_size) { 422 /* Send the revoke send buffer */ 423 revoke_pkt = &net_dev->revoke_packet; 424 memset(revoke_pkt, 0, sizeof(nvsp_msg)); 425 426 revoke_pkt->hdr.msg_type = 427 nvsp_msg_1_type_revoke_send_buf; 428 revoke_pkt->msgs.vers_1_msgs.revoke_send_buf.id = 429 NETVSC_SEND_BUFFER_ID; 430 431 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel, 432 revoke_pkt, sizeof(nvsp_msg), 433 (uint64_t)(uintptr_t)revoke_pkt, 434 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0); 435 /* 436 * If we failed here, we might as well return and have a leak 437 * rather than continue and a bugchk 438 */ 439 if (ret != 0) { 440 return (ret); 441 } 442 } 443 444 /* Tear down the gpadl on the vsp end */ 445 if (net_dev->send_buf_gpadl_handle) { 446 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel, 447 net_dev->send_buf_gpadl_handle); 448 449 /* 450 * If we failed here, we might as well return and have a leak 451 * rather than continue and a bugchk 452 */ 453 if (ret != 0) { 454 return (ret); 455 } 456 net_dev->send_buf_gpadl_handle = 0; 457 } 458 459 if (net_dev->send_buf) { 460 /* Free up the receive buffer */ 461 contigfree(net_dev->send_buf, net_dev->send_buf_size, M_NETVSC); 462 net_dev->send_buf = NULL; 463 } 464 465 if (net_dev->send_section_bitsmap) { 466 free(net_dev->send_section_bitsmap, M_NETVSC); 467 } 468 469 return (ret); 470} 471 472 473/* 474 * Attempt to negotiate the caller-specified NVSP version 475 * 476 * For NVSP v2, Server 2008 R2 does not set 477 * init_pkt->msgs.init_msgs.init_compl.negotiated_prot_vers 478 * to the negotiated version, so we cannot rely on that. 479 */ 480static int 481hv_nv_negotiate_nvsp_protocol(struct hv_device *device, netvsc_dev *net_dev, 482 uint32_t nvsp_ver) 483{ 484 nvsp_msg *init_pkt; 485 int ret; 486 487 init_pkt = &net_dev->channel_init_packet; 488 memset(init_pkt, 0, sizeof(nvsp_msg)); 489 init_pkt->hdr.msg_type = nvsp_msg_type_init; 490 491 /* 492 * Specify parameter as the only acceptable protocol version 493 */ 494 init_pkt->msgs.init_msgs.init.p1.protocol_version = nvsp_ver; 495 init_pkt->msgs.init_msgs.init.protocol_version_2 = nvsp_ver; 496 497 /* Send the init request */ 498 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt, 499 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt, 500 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 501 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 502 if (ret != 0) 503 return (-1); 504 505 sema_wait(&net_dev->channel_init_sema); 506 507 if (init_pkt->msgs.init_msgs.init_compl.status != nvsp_status_success) 508 return (EINVAL); 509 510 return (0); 511} 512 513/* 514 * Send NDIS version 2 config packet containing MTU. 515 * 516 * Not valid for NDIS version 1. 517 */ 518static int 519hv_nv_send_ndis_config(struct hv_device *device, uint32_t mtu) 520{ 521 netvsc_dev *net_dev; 522 nvsp_msg *init_pkt; 523 int ret; 524 525 net_dev = hv_nv_get_outbound_net_device(device); 526 if (!net_dev) 527 return (-ENODEV); 528 529 /* 530 * Set up configuration packet, write MTU 531 * Indicate we are capable of handling VLAN tags 532 */ 533 init_pkt = &net_dev->channel_init_packet; 534 memset(init_pkt, 0, sizeof(nvsp_msg)); 535 init_pkt->hdr.msg_type = nvsp_msg_2_type_send_ndis_config; 536 init_pkt->msgs.vers_2_msgs.send_ndis_config.mtu = mtu; 537 init_pkt-> 538 msgs.vers_2_msgs.send_ndis_config.capabilities.u1.u2.ieee8021q 539 = 1; 540 541 /* Send the configuration packet */ 542 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt, 543 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt, 544 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0); 545 if (ret != 0) 546 return (-EINVAL); 547 548 return (0); 549} 550 551/* 552 * Net VSC connect to VSP 553 */ 554static int 555hv_nv_connect_to_vsp(struct hv_device *device) 556{ 557 netvsc_dev *net_dev; 558 nvsp_msg *init_pkt; 559 uint32_t ndis_version; 560 uint32_t protocol_list[] = { NVSP_PROTOCOL_VERSION_1, 561 NVSP_PROTOCOL_VERSION_2, 562 NVSP_PROTOCOL_VERSION_4, 563 NVSP_PROTOCOL_VERSION_5 }; 564 int i; 565 int protocol_number = nitems(protocol_list); 566 int ret = 0; 567 device_t dev = device->device; 568 hn_softc_t *sc = device_get_softc(dev); 569 struct ifnet *ifp = sc->hn_ifp; 570 571 net_dev = hv_nv_get_outbound_net_device(device); 572 if (!net_dev) { 573 return (ENODEV); 574 } 575 576 /* 577 * Negotiate the NVSP version. Try the latest NVSP first. 578 */ 579 for (i = protocol_number - 1; i >= 0; i--) { 580 if (hv_nv_negotiate_nvsp_protocol(device, net_dev, 581 protocol_list[i]) == 0) { 582 net_dev->nvsp_version = protocol_list[i]; 583 if (bootverbose) 584 device_printf(dev, "Netvsc: got version 0x%x\n", 585 net_dev->nvsp_version); 586 break; 587 } 588 } 589 590 if (i < 0) { 591 if (bootverbose) 592 device_printf(dev, "failed to negotiate a valid " 593 "protocol.\n"); 594 return (EPROTO); 595 } 596 597 /* 598 * Set the MTU if supported by this NVSP protocol version 599 * This needs to be right after the NVSP init message per Haiyang 600 */ 601 if (net_dev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) 602 ret = hv_nv_send_ndis_config(device, ifp->if_mtu); 603 604 /* 605 * Send the NDIS version 606 */ 607 init_pkt = &net_dev->channel_init_packet; 608 609 memset(init_pkt, 0, sizeof(nvsp_msg)); 610 611 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_4) { 612 ndis_version = NDIS_VERSION_6_1; 613 } else { 614 ndis_version = NDIS_VERSION_6_30; 615 } 616 617 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_ndis_vers; 618 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_major_vers = 619 (ndis_version & 0xFFFF0000) >> 16; 620 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_minor_vers = 621 ndis_version & 0xFFFF; 622 623 /* Send the init request */ 624 625 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt, 626 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt, 627 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0); 628 if (ret != 0) { 629 goto cleanup; 630 } 631 /* 632 * TODO: BUGBUG - We have to wait for the above msg since the netvsp 633 * uses KMCL which acknowledges packet (completion packet) 634 * since our Vmbus always set the 635 * HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag 636 */ 637 /* sema_wait(&NetVscChannel->channel_init_sema); */ 638 639 /* Post the big receive buffer to NetVSP */ 640 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_2) 641 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY; 642 else 643 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE; 644 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE; 645 646 ret = hv_nv_init_rx_buffer_with_net_vsp(device); 647 if (ret == 0) 648 ret = hv_nv_init_send_buffer_with_net_vsp(device); 649 650cleanup: 651 return (ret); 652} 653 654/* 655 * Net VSC disconnect from VSP 656 */ 657static void 658hv_nv_disconnect_from_vsp(netvsc_dev *net_dev) 659{ 660 hv_nv_destroy_rx_buffer(net_dev); 661 hv_nv_destroy_send_buffer(net_dev); 662} 663 664void 665hv_nv_subchan_attach(struct hv_vmbus_channel *chan) 666{ 667 668 chan->hv_chan_rdbuf = malloc(NETVSC_PACKET_SIZE, M_NETVSC, M_WAITOK); 669 hv_vmbus_channel_open(chan, NETVSC_DEVICE_RING_BUFFER_SIZE, 670 NETVSC_DEVICE_RING_BUFFER_SIZE, NULL, 0, 671 hv_nv_on_channel_callback, chan); 672} 673 674/* 675 * Net VSC on device add 676 * 677 * Callback when the device belonging to this driver is added 678 */ 679netvsc_dev * 680hv_nv_on_device_add(struct hv_device *device, void *additional_info) 681{ 682 struct hv_vmbus_channel *chan = device->channel; 683 netvsc_dev *net_dev; 684 int ret = 0; 685 686 net_dev = hv_nv_alloc_net_device(device); 687 if (net_dev == NULL) 688 return NULL; 689 690 /* Initialize the NetVSC channel extension */ 691 692 sema_init(&net_dev->channel_init_sema, 0, "netdev_sema"); 693 694 chan->hv_chan_rdbuf = malloc(NETVSC_PACKET_SIZE, M_NETVSC, M_WAITOK); 695 696 /* 697 * Open the channel 698 */ 699 ret = hv_vmbus_channel_open(chan, 700 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE, 701 NULL, 0, hv_nv_on_channel_callback, chan); 702 if (ret != 0) { 703 free(chan->hv_chan_rdbuf, M_NETVSC); 704 goto cleanup; 705 } 706 707 /* 708 * Connect with the NetVsp 709 */ 710 ret = hv_nv_connect_to_vsp(device); 711 if (ret != 0) 712 goto close; 713 714 return (net_dev); 715 716close: 717 /* Now, we can close the channel safely */ 718 free(chan->hv_chan_rdbuf, M_NETVSC); 719 hv_vmbus_channel_close(chan); 720 721cleanup: 722 /* 723 * Free the packet buffers on the netvsc device packet queue. 724 * Release other resources. 725 */ 726 sema_destroy(&net_dev->channel_init_sema); 727 free(net_dev, M_NETVSC); 728 729 return (NULL); 730} 731 732/* 733 * Net VSC on device remove 734 */ 735int 736hv_nv_on_device_remove(struct hv_device *device, boolean_t destroy_channel) 737{ 738 hn_softc_t *sc = device_get_softc(device->device); 739 netvsc_dev *net_dev = sc->net_dev;; 740 741 /* Stop outbound traffic ie sends and receives completions */ 742 net_dev->destroy = TRUE; 743 744 hv_nv_disconnect_from_vsp(net_dev); 745 746 /* At this point, no one should be accessing net_dev except in here */ 747 748 /* Now, we can close the channel safely */ 749 750 if (!destroy_channel) { 751 device->channel->state = 752 HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE; 753 } 754 755 free(device->channel->hv_chan_rdbuf, M_NETVSC); 756 hv_vmbus_channel_close(device->channel); 757 758 sema_destroy(&net_dev->channel_init_sema); 759 free(net_dev, M_NETVSC); 760 761 return (0); 762} 763 764/* 765 * Net VSC on send completion 766 */ 767static void 768hv_nv_on_send_completion(netvsc_dev *net_dev, 769 struct hv_device *device, struct hv_vmbus_channel *chan, 770 hv_vm_packet_descriptor *pkt) 771{ 772 nvsp_msg *nvsp_msg_pkt; 773 netvsc_packet *net_vsc_pkt; 774 775 nvsp_msg_pkt = 776 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3)); 777 778 if (nvsp_msg_pkt->hdr.msg_type == nvsp_msg_type_init_complete 779 || nvsp_msg_pkt->hdr.msg_type 780 == nvsp_msg_1_type_send_rx_buf_complete 781 || nvsp_msg_pkt->hdr.msg_type 782 == nvsp_msg_1_type_send_send_buf_complete 783 || nvsp_msg_pkt->hdr.msg_type 784 == nvsp_msg5_type_subchannel) { 785 /* Copy the response back */ 786 memcpy(&net_dev->channel_init_packet, nvsp_msg_pkt, 787 sizeof(nvsp_msg)); 788 sema_post(&net_dev->channel_init_sema); 789 } else if (nvsp_msg_pkt->hdr.msg_type == 790 nvsp_msg_1_type_send_rndis_pkt_complete) { 791 /* Get the send context */ 792 net_vsc_pkt = 793 (netvsc_packet *)(unsigned long)pkt->transaction_id; 794 if (NULL != net_vsc_pkt) { 795 if (net_vsc_pkt->send_buf_section_idx != 796 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) { 797 u_long mask; 798 int idx; 799 800 idx = net_vsc_pkt->send_buf_section_idx / 801 BITS_PER_LONG; 802 KASSERT(idx < net_dev->bitsmap_words, 803 ("invalid section index %u", 804 net_vsc_pkt->send_buf_section_idx)); 805 mask = 1UL << 806 (net_vsc_pkt->send_buf_section_idx % 807 BITS_PER_LONG); 808 809 KASSERT(net_dev->send_section_bitsmap[idx] & 810 mask, 811 ("index bitmap 0x%lx, section index %u, " 812 "bitmap idx %d, bitmask 0x%lx", 813 net_dev->send_section_bitsmap[idx], 814 net_vsc_pkt->send_buf_section_idx, 815 idx, mask)); 816 atomic_clear_long( 817 &net_dev->send_section_bitsmap[idx], mask); 818 } 819 820 /* Notify the layer above us */ 821 net_vsc_pkt->compl.send.on_send_completion(chan, 822 net_vsc_pkt->compl.send.send_completion_context); 823 824 } 825 } 826} 827 828/* 829 * Net VSC on send 830 * Sends a packet on the specified Hyper-V device. 831 * Returns 0 on success, non-zero on failure. 832 */ 833int 834hv_nv_on_send(struct hv_vmbus_channel *chan, netvsc_packet *pkt) 835{ 836 nvsp_msg send_msg; 837 int ret; 838 839 send_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt; 840 if (pkt->is_data_pkt) { 841 /* 0 is RMC_DATA */ 842 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 0; 843 } else { 844 /* 1 is RMC_CONTROL */ 845 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 1; 846 } 847 848 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_idx = 849 pkt->send_buf_section_idx; 850 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_size = 851 pkt->send_buf_section_size; 852 853 if (pkt->page_buf_count) { 854 ret = hv_vmbus_channel_send_packet_pagebuffer(chan, 855 pkt->page_buffers, pkt->page_buf_count, 856 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt); 857 } else { 858 ret = hv_vmbus_channel_send_packet(chan, 859 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt, 860 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 861 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 862 } 863 864 return (ret); 865} 866 867/* 868 * Net VSC on receive 869 * 870 * In the FreeBSD Hyper-V virtual world, this function deals exclusively 871 * with virtual addresses. 872 */ 873static void 874hv_nv_on_receive(netvsc_dev *net_dev, struct hv_device *device, 875 struct hv_vmbus_channel *chan, hv_vm_packet_descriptor *pkt) 876{ 877 hv_vm_transfer_page_packet_header *vm_xfer_page_pkt; 878 nvsp_msg *nvsp_msg_pkt; 879 netvsc_packet vsc_pkt; 880 netvsc_packet *net_vsc_pkt = &vsc_pkt; 881 device_t dev = device->device; 882 int count = 0; 883 int i = 0; 884 int status = nvsp_status_success; 885 886 /* 887 * All inbound packets other than send completion should be 888 * xfer page packet. 889 */ 890 if (pkt->type != HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES) { 891 device_printf(dev, "packet type %d is invalid!\n", pkt->type); 892 return; 893 } 894 895 nvsp_msg_pkt = (nvsp_msg *)((unsigned long)pkt 896 + (pkt->data_offset8 << 3)); 897 898 /* Make sure this is a valid nvsp packet */ 899 if (nvsp_msg_pkt->hdr.msg_type != nvsp_msg_1_type_send_rndis_pkt) { 900 device_printf(dev, "packet hdr type %d is invalid!\n", 901 pkt->type); 902 return; 903 } 904 905 vm_xfer_page_pkt = (hv_vm_transfer_page_packet_header *)pkt; 906 907 if (vm_xfer_page_pkt->transfer_page_set_id != 908 NETVSC_RECEIVE_BUFFER_ID) { 909 device_printf(dev, "transfer_page_set_id %d is invalid!\n", 910 vm_xfer_page_pkt->transfer_page_set_id); 911 return; 912 } 913 914 count = vm_xfer_page_pkt->range_count; 915 net_vsc_pkt->device = device; 916 917 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */ 918 for (i = 0; i < count; i++) { 919 net_vsc_pkt->status = nvsp_status_success; 920 net_vsc_pkt->data = (void *)((unsigned long)net_dev->rx_buf + 921 vm_xfer_page_pkt->ranges[i].byte_offset); 922 net_vsc_pkt->tot_data_buf_len = 923 vm_xfer_page_pkt->ranges[i].byte_count; 924 925 hv_rf_on_receive(net_dev, device, chan, net_vsc_pkt); 926 if (net_vsc_pkt->status != nvsp_status_success) { 927 status = nvsp_status_failure; 928 } 929 } 930 931 /* 932 * Moved completion call back here so that all received 933 * messages (not just data messages) will trigger a response 934 * message back to the host. 935 */ 936 hv_nv_on_receive_completion(chan, vm_xfer_page_pkt->d.transaction_id, 937 status); 938} 939 940/* 941 * Net VSC on receive completion 942 * 943 * Send a receive completion packet to RNDIS device (ie NetVsp) 944 */ 945static void 946hv_nv_on_receive_completion(struct hv_vmbus_channel *chan, uint64_t tid, 947 uint32_t status) 948{ 949 nvsp_msg rx_comp_msg; 950 int retries = 0; 951 int ret = 0; 952 953 rx_comp_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt_complete; 954 955 /* Pass in the status */ 956 rx_comp_msg.msgs.vers_1_msgs.send_rndis_pkt_complete.status = 957 status; 958 959retry_send_cmplt: 960 /* Send the completion */ 961 ret = hv_vmbus_channel_send_packet(chan, &rx_comp_msg, 962 sizeof(nvsp_msg), tid, HV_VMBUS_PACKET_TYPE_COMPLETION, 0); 963 if (ret == 0) { 964 /* success */ 965 /* no-op */ 966 } else if (ret == EAGAIN) { 967 /* no more room... wait a bit and attempt to retry 3 times */ 968 retries++; 969 970 if (retries < 4) { 971 DELAY(100); 972 goto retry_send_cmplt; 973 } 974 } 975} 976 977/* 978 * Net VSC receiving vRSS send table from VSP 979 */ 980static void 981hv_nv_send_table(struct hv_device *device, hv_vm_packet_descriptor *pkt) 982{ 983 netvsc_dev *net_dev; 984 nvsp_msg *nvsp_msg_pkt; 985 int i; 986 uint32_t count, *table; 987 988 net_dev = hv_nv_get_inbound_net_device(device); 989 if (!net_dev) 990 return; 991 992 nvsp_msg_pkt = 993 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3)); 994 995 if (nvsp_msg_pkt->hdr.msg_type != 996 nvsp_msg5_type_send_indirection_table) { 997 printf("Netvsc: !Warning! receive msg type not " 998 "send_indirection_table. type = %d\n", 999 nvsp_msg_pkt->hdr.msg_type); 1000 return; 1001 } 1002 1003 count = nvsp_msg_pkt->msgs.vers_5_msgs.send_table.count; 1004 if (count != VRSS_SEND_TABLE_SIZE) { 1005 printf("Netvsc: Received wrong send table size: %u\n", count); 1006 return; 1007 } 1008 1009 table = (uint32_t *) 1010 ((unsigned long)&nvsp_msg_pkt->msgs.vers_5_msgs.send_table + 1011 nvsp_msg_pkt->msgs.vers_5_msgs.send_table.offset); 1012 1013 for (i = 0; i < count; i++) 1014 net_dev->vrss_send_table[i] = table[i]; 1015} 1016 1017/* 1018 * Net VSC on channel callback 1019 */ 1020static void 1021hv_nv_on_channel_callback(void *xchan) 1022{ 1023 struct hv_vmbus_channel *chan = xchan; 1024 struct hv_device *device = chan->device; 1025 netvsc_dev *net_dev; 1026 device_t dev = device->device; 1027 uint32_t bytes_rxed; 1028 uint64_t request_id; 1029 hv_vm_packet_descriptor *desc; 1030 uint8_t *buffer; 1031 int bufferlen = NETVSC_PACKET_SIZE; 1032 int ret = 0; 1033 1034 net_dev = hv_nv_get_inbound_net_device(device); 1035 if (net_dev == NULL) 1036 return; 1037 1038 buffer = chan->hv_chan_rdbuf; 1039 1040 do { 1041 ret = hv_vmbus_channel_recv_packet_raw(chan, 1042 buffer, bufferlen, &bytes_rxed, &request_id); 1043 if (ret == 0) { 1044 if (bytes_rxed > 0) { 1045 desc = (hv_vm_packet_descriptor *)buffer; 1046 switch (desc->type) { 1047 case HV_VMBUS_PACKET_TYPE_COMPLETION: 1048 hv_nv_on_send_completion(net_dev, device, 1049 chan, desc); 1050 break; 1051 case HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES: 1052 hv_nv_on_receive(net_dev, device, chan, desc); 1053 break; 1054 case HV_VMBUS_PACKET_TYPE_DATA_IN_BAND: 1055 hv_nv_send_table(device, desc); 1056 break; 1057 default: 1058 device_printf(dev, 1059 "hv_cb recv unknow type %d " 1060 " packet\n", desc->type); 1061 break; 1062 } 1063 } else { 1064 break; 1065 } 1066 } else if (ret == ENOBUFS) { 1067 /* Handle large packet */ 1068 if (bufferlen > NETVSC_PACKET_SIZE) { 1069 free(buffer, M_NETVSC); 1070 buffer = NULL; 1071 } 1072 1073 /* alloc new buffer */ 1074 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT); 1075 if (buffer == NULL) { 1076 device_printf(dev, 1077 "hv_cb malloc buffer failed, len=%u\n", 1078 bytes_rxed); 1079 bufferlen = 0; 1080 break; 1081 } 1082 bufferlen = bytes_rxed; 1083 } 1084 } while (1); 1085 1086 if (bufferlen > NETVSC_PACKET_SIZE) 1087 free(buffer, M_NETVSC); 1088 1089 hv_rf_channel_rollup(chan); 1090} 1091