1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2009, Microsoft Corporation. 4 * 5 * Authors: 6 * Haiyang Zhang <haiyangz@microsoft.com> 7 * Hank Janssen <hjanssen@microsoft.com> 8 */ 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/sched.h> 13 #include <linux/wait.h> 14 #include <linux/mm.h> 15 #include <linux/delay.h> 16 #include <linux/io.h> 17 #include <linux/slab.h> 18 #include <linux/netdevice.h> 19 #include <linux/if_ether.h> 20 #include <linux/vmalloc.h> 21 #include <linux/rtnetlink.h> 22 #include <linux/prefetch.h> 23 24 #include <asm/sync_bitops.h> 25 #include <asm/mshyperv.h> 26 27 #include "hyperv_net.h" 28 #include "netvsc_trace.h" 29 30 /* 31 * Switch the data path from the synthetic interface to the VF 32 * interface. 33 */ 34 int netvsc_switch_datapath(struct net_device *ndev, bool vf) 35 { 36 struct net_device_context *net_device_ctx = netdev_priv(ndev); 37 struct hv_device *dev = net_device_ctx->device_ctx; 38 struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev); 39 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt; 40 int ret, retry = 0; 41 42 /* Block sending traffic to VF if it's about to be gone */ 43 if (!vf) 44 net_device_ctx->data_path_is_vf = vf; 45 46 memset(init_pkt, 0, sizeof(struct nvsp_message)); 47 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH; 48 if (vf) 49 init_pkt->msg.v4_msg.active_dp.active_datapath = 50 NVSP_DATAPATH_VF; 51 else 52 init_pkt->msg.v4_msg.active_dp.active_datapath = 53 NVSP_DATAPATH_SYNTHETIC; 54 55 again: 56 trace_nvsp_send(ndev, init_pkt); 57 58 ret = vmbus_sendpacket(dev->channel, init_pkt, 59 sizeof(struct nvsp_message), 60 (unsigned long)init_pkt, VM_PKT_DATA_INBAND, 61 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 62 63 /* If failed to switch to/from VF, let data_path_is_vf stay false, 64 * so we use synthetic path to send data. 65 */ 66 if (ret) { 67 if (ret != -EAGAIN) { 68 netdev_err(ndev, 69 "Unable to send sw datapath msg, err: %d\n", 70 ret); 71 return ret; 72 } 73 74 if (retry++ < RETRY_MAX) { 75 usleep_range(RETRY_US_LO, RETRY_US_HI); 76 goto again; 77 } else { 78 netdev_err( 79 ndev, 80 "Retry failed to send sw datapath msg, err: %d\n", 81 ret); 82 return ret; 83 } 84 } 85 86 wait_for_completion(&nv_dev->channel_init_wait); 87 net_device_ctx->data_path_is_vf = vf; 88 89 return 0; 90 } 91 92 /* Worker to setup sub channels on initial setup 93 * Initial hotplug event occurs in softirq context 94 * and can't wait for channels. 95 */ 96 static void netvsc_subchan_work(struct work_struct *w) 97 { 98 struct netvsc_device *nvdev = 99 container_of(w, struct netvsc_device, subchan_work); 100 struct rndis_device *rdev; 101 int i, ret; 102 103 /* Avoid deadlock with device removal already under RTNL */ 104 if (!rtnl_trylock()) { 105 schedule_work(w); 106 return; 107 } 108 109 rdev = nvdev->extension; 110 if (rdev) { 111 ret = rndis_set_subchannel(rdev->ndev, nvdev, NULL); 112 if (ret == 0) { 113 netif_device_attach(rdev->ndev); 114 } else { 115 /* fallback to only primary channel */ 116 for (i = 1; i < nvdev->num_chn; i++) 117 netif_napi_del(&nvdev->chan_table[i].napi); 118 119 nvdev->max_chn = 1; 120 nvdev->num_chn = 1; 121 } 122 } 123 124 rtnl_unlock(); 125 } 126 127 static struct netvsc_device *alloc_net_device(void) 128 { 129 struct netvsc_device *net_device; 130 131 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL); 132 if (!net_device) 133 return NULL; 134 135 init_waitqueue_head(&net_device->wait_drain); 136 net_device->destroy = false; 137 net_device->tx_disable = true; 138 139 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT; 140 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT; 141 142 init_completion(&net_device->channel_init_wait); 143 init_waitqueue_head(&net_device->subchan_open); 144 INIT_WORK(&net_device->subchan_work, netvsc_subchan_work); 145 146 return net_device; 147 } 148 149 static void free_netvsc_device(struct rcu_head *head) 150 { 151 struct netvsc_device *nvdev 152 = container_of(head, struct netvsc_device, rcu); 153 int i; 154 155 kfree(nvdev->extension); 156 157 if (nvdev->recv_original_buf) { 158 hv_unmap_memory(nvdev->recv_buf); 159 vfree(nvdev->recv_original_buf); 160 } else { 161 vfree(nvdev->recv_buf); 162 } 163 164 if (nvdev->send_original_buf) { 165 hv_unmap_memory(nvdev->send_buf); 166 vfree(nvdev->send_original_buf); 167 } else { 168 vfree(nvdev->send_buf); 169 } 170 171 bitmap_free(nvdev->send_section_map); 172 173 for (i = 0; i < VRSS_CHANNEL_MAX; i++) { 174 xdp_rxq_info_unreg(&nvdev->chan_table[i].xdp_rxq); 175 kfree(nvdev->chan_table[i].recv_buf); 176 vfree(nvdev->chan_table[i].mrc.slots); 177 } 178 179 kfree(nvdev); 180 } 181 182 static void free_netvsc_device_rcu(struct netvsc_device *nvdev) 183 { 184 call_rcu(&nvdev->rcu, free_netvsc_device); 185 } 186 187 static void netvsc_revoke_recv_buf(struct hv_device *device, 188 struct netvsc_device *net_device, 189 struct net_device *ndev) 190 { 191 struct nvsp_message *revoke_packet; 192 int ret; 193 194 /* 195 * If we got a section count, it means we received a 196 * SendReceiveBufferComplete msg (ie sent 197 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need 198 * to send a revoke msg here 199 */ 200 if (net_device->recv_section_cnt) { 201 /* Send the revoke receive buffer */ 202 revoke_packet = &net_device->revoke_packet; 203 memset(revoke_packet, 0, sizeof(struct nvsp_message)); 204 205 revoke_packet->hdr.msg_type = 206 NVSP_MSG1_TYPE_REVOKE_RECV_BUF; 207 revoke_packet->msg.v1_msg. 208 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; 209 210 trace_nvsp_send(ndev, revoke_packet); 211 212 ret = vmbus_sendpacket(device->channel, 213 revoke_packet, 214 sizeof(struct nvsp_message), 215 VMBUS_RQST_ID_NO_RESPONSE, 216 VM_PKT_DATA_INBAND, 0); 217 /* If the failure is because the channel is rescinded; 218 * ignore the failure since we cannot send on a rescinded 219 * channel. This would allow us to properly cleanup 220 * even when the channel is rescinded. 221 */ 222 if (device->channel->rescind) 223 ret = 0; 224 /* 225 * If we failed here, we might as well return and 226 * have a leak rather than continue and a bugchk 227 */ 228 if (ret != 0) { 229 netdev_err(ndev, "unable to send " 230 "revoke receive buffer to netvsp\n"); 231 return; 232 } 233 net_device->recv_section_cnt = 0; 234 } 235 } 236 237 static void netvsc_revoke_send_buf(struct hv_device *device, 238 struct netvsc_device *net_device, 239 struct net_device *ndev) 240 { 241 struct nvsp_message *revoke_packet; 242 int ret; 243 244 /* Deal with the send buffer we may have setup. 245 * If we got a send section size, it means we received a 246 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent 247 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need 248 * to send a revoke msg here 249 */ 250 if (net_device->send_section_cnt) { 251 /* Send the revoke receive buffer */ 252 revoke_packet = &net_device->revoke_packet; 253 memset(revoke_packet, 0, sizeof(struct nvsp_message)); 254 255 revoke_packet->hdr.msg_type = 256 NVSP_MSG1_TYPE_REVOKE_SEND_BUF; 257 revoke_packet->msg.v1_msg.revoke_send_buf.id = 258 NETVSC_SEND_BUFFER_ID; 259 260 trace_nvsp_send(ndev, revoke_packet); 261 262 ret = vmbus_sendpacket(device->channel, 263 revoke_packet, 264 sizeof(struct nvsp_message), 265 VMBUS_RQST_ID_NO_RESPONSE, 266 VM_PKT_DATA_INBAND, 0); 267 268 /* If the failure is because the channel is rescinded; 269 * ignore the failure since we cannot send on a rescinded 270 * channel. This would allow us to properly cleanup 271 * even when the channel is rescinded. 272 */ 273 if (device->channel->rescind) 274 ret = 0; 275 276 /* If we failed here, we might as well return and 277 * have a leak rather than continue and a bugchk 278 */ 279 if (ret != 0) { 280 netdev_err(ndev, "unable to send " 281 "revoke send buffer to netvsp\n"); 282 return; 283 } 284 net_device->send_section_cnt = 0; 285 } 286 } 287 288 static void netvsc_teardown_recv_gpadl(struct hv_device *device, 289 struct netvsc_device *net_device, 290 struct net_device *ndev) 291 { 292 int ret; 293 294 if (net_device->recv_buf_gpadl_handle.gpadl_handle) { 295 ret = vmbus_teardown_gpadl(device->channel, 296 &net_device->recv_buf_gpadl_handle); 297 298 /* If we failed here, we might as well return and have a leak 299 * rather than continue and a bugchk 300 */ 301 if (ret != 0) { 302 netdev_err(ndev, 303 "unable to teardown receive buffer's gpadl\n"); 304 return; 305 } 306 } 307 } 308 309 static void netvsc_teardown_send_gpadl(struct hv_device *device, 310 struct netvsc_device *net_device, 311 struct net_device *ndev) 312 { 313 int ret; 314 315 if (net_device->send_buf_gpadl_handle.gpadl_handle) { 316 ret = vmbus_teardown_gpadl(device->channel, 317 &net_device->send_buf_gpadl_handle); 318 319 /* If we failed here, we might as well return and have a leak 320 * rather than continue and a bugchk 321 */ 322 if (ret != 0) { 323 netdev_err(ndev, 324 "unable to teardown send buffer's gpadl\n"); 325 return; 326 } 327 } 328 } 329 330 int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx) 331 { 332 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx]; 333 int node = cpu_to_node(nvchan->channel->target_cpu); 334 size_t size; 335 336 size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data); 337 nvchan->mrc.slots = vzalloc_node(size, node); 338 if (!nvchan->mrc.slots) 339 nvchan->mrc.slots = vzalloc(size); 340 341 return nvchan->mrc.slots ? 0 : -ENOMEM; 342 } 343 344 static int netvsc_init_buf(struct hv_device *device, 345 struct netvsc_device *net_device, 346 const struct netvsc_device_info *device_info) 347 { 348 struct nvsp_1_message_send_receive_buffer_complete *resp; 349 struct net_device *ndev = hv_get_drvdata(device); 350 struct nvsp_message *init_packet; 351 unsigned int buf_size; 352 int i, ret = 0; 353 void *vaddr; 354 355 /* Get receive buffer area. */ 356 buf_size = device_info->recv_sections * device_info->recv_section_size; 357 buf_size = roundup(buf_size, PAGE_SIZE); 358 359 /* Legacy hosts only allow smaller receive buffer */ 360 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2) 361 buf_size = min_t(unsigned int, buf_size, 362 NETVSC_RECEIVE_BUFFER_SIZE_LEGACY); 363 364 net_device->recv_buf = vzalloc(buf_size); 365 if (!net_device->recv_buf) { 366 netdev_err(ndev, 367 "unable to allocate receive buffer of size %u\n", 368 buf_size); 369 ret = -ENOMEM; 370 goto cleanup; 371 } 372 373 net_device->recv_buf_size = buf_size; 374 375 /* 376 * Establish the gpadl handle for this buffer on this 377 * channel. Note: This call uses the vmbus connection rather 378 * than the channel to establish the gpadl handle. 379 */ 380 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf, 381 buf_size, 382 &net_device->recv_buf_gpadl_handle); 383 if (ret != 0) { 384 netdev_err(ndev, 385 "unable to establish receive buffer's gpadl\n"); 386 goto cleanup; 387 } 388 389 if (hv_isolation_type_snp()) { 390 vaddr = hv_map_memory(net_device->recv_buf, buf_size); 391 if (!vaddr) { 392 ret = -ENOMEM; 393 goto cleanup; 394 } 395 396 net_device->recv_original_buf = net_device->recv_buf; 397 net_device->recv_buf = vaddr; 398 } 399 400 /* Notify the NetVsp of the gpadl handle */ 401 init_packet = &net_device->channel_init_pkt; 402 memset(init_packet, 0, sizeof(struct nvsp_message)); 403 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF; 404 init_packet->msg.v1_msg.send_recv_buf. 405 gpadl_handle = net_device->recv_buf_gpadl_handle.gpadl_handle; 406 init_packet->msg.v1_msg. 407 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; 408 409 trace_nvsp_send(ndev, init_packet); 410 411 /* Send the gpadl notification request */ 412 ret = vmbus_sendpacket(device->channel, init_packet, 413 sizeof(struct nvsp_message), 414 (unsigned long)init_packet, 415 VM_PKT_DATA_INBAND, 416 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 417 if (ret != 0) { 418 netdev_err(ndev, 419 "unable to send receive buffer's gpadl to netvsp\n"); 420 goto cleanup; 421 } 422 423 wait_for_completion(&net_device->channel_init_wait); 424 425 /* Check the response */ 426 resp = &init_packet->msg.v1_msg.send_recv_buf_complete; 427 if (resp->status != NVSP_STAT_SUCCESS) { 428 netdev_err(ndev, 429 "Unable to complete receive buffer initialization with NetVsp - status %d\n", 430 resp->status); 431 ret = -EINVAL; 432 goto cleanup; 433 } 434 435 /* Parse the response */ 436 netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n", 437 resp->num_sections, resp->sections[0].sub_alloc_size, 438 resp->sections[0].num_sub_allocs); 439 440 /* There should only be one section for the entire receive buffer */ 441 if (resp->num_sections != 1 || resp->sections[0].offset != 0) { 442 ret = -EINVAL; 443 goto cleanup; 444 } 445 446 net_device->recv_section_size = resp->sections[0].sub_alloc_size; 447 net_device->recv_section_cnt = resp->sections[0].num_sub_allocs; 448 449 /* Ensure buffer will not overflow */ 450 if (net_device->recv_section_size < NETVSC_MTU_MIN || (u64)net_device->recv_section_size * 451 (u64)net_device->recv_section_cnt > (u64)buf_size) { 452 netdev_err(ndev, "invalid recv_section_size %u\n", 453 net_device->recv_section_size); 454 ret = -EINVAL; 455 goto cleanup; 456 } 457 458 for (i = 0; i < VRSS_CHANNEL_MAX; i++) { 459 struct netvsc_channel *nvchan = &net_device->chan_table[i]; 460 461 nvchan->recv_buf = kzalloc(net_device->recv_section_size, GFP_KERNEL); 462 if (nvchan->recv_buf == NULL) { 463 ret = -ENOMEM; 464 goto cleanup; 465 } 466 } 467 468 /* Setup receive completion ring. 469 * Add 1 to the recv_section_cnt because at least one entry in a 470 * ring buffer has to be empty. 471 */ 472 net_device->recv_completion_cnt = net_device->recv_section_cnt + 1; 473 ret = netvsc_alloc_recv_comp_ring(net_device, 0); 474 if (ret) 475 goto cleanup; 476 477 /* Now setup the send buffer. */ 478 buf_size = device_info->send_sections * device_info->send_section_size; 479 buf_size = round_up(buf_size, PAGE_SIZE); 480 481 net_device->send_buf = vzalloc(buf_size); 482 if (!net_device->send_buf) { 483 netdev_err(ndev, "unable to allocate send buffer of size %u\n", 484 buf_size); 485 ret = -ENOMEM; 486 goto cleanup; 487 } 488 net_device->send_buf_size = buf_size; 489 490 /* Establish the gpadl handle for this buffer on this 491 * channel. Note: This call uses the vmbus connection rather 492 * than the channel to establish the gpadl handle. 493 */ 494 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf, 495 buf_size, 496 &net_device->send_buf_gpadl_handle); 497 if (ret != 0) { 498 netdev_err(ndev, 499 "unable to establish send buffer's gpadl\n"); 500 goto cleanup; 501 } 502 503 if (hv_isolation_type_snp()) { 504 vaddr = hv_map_memory(net_device->send_buf, buf_size); 505 if (!vaddr) { 506 ret = -ENOMEM; 507 goto cleanup; 508 } 509 510 net_device->send_original_buf = net_device->send_buf; 511 net_device->send_buf = vaddr; 512 } 513 514 /* Notify the NetVsp of the gpadl handle */ 515 init_packet = &net_device->channel_init_pkt; 516 memset(init_packet, 0, sizeof(struct nvsp_message)); 517 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF; 518 init_packet->msg.v1_msg.send_send_buf.gpadl_handle = 519 net_device->send_buf_gpadl_handle.gpadl_handle; 520 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID; 521 522 trace_nvsp_send(ndev, init_packet); 523 524 /* Send the gpadl notification request */ 525 ret = vmbus_sendpacket(device->channel, init_packet, 526 sizeof(struct nvsp_message), 527 (unsigned long)init_packet, 528 VM_PKT_DATA_INBAND, 529 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 530 if (ret != 0) { 531 netdev_err(ndev, 532 "unable to send send buffer's gpadl to netvsp\n"); 533 goto cleanup; 534 } 535 536 wait_for_completion(&net_device->channel_init_wait); 537 538 /* Check the response */ 539 if (init_packet->msg.v1_msg. 540 send_send_buf_complete.status != NVSP_STAT_SUCCESS) { 541 netdev_err(ndev, "Unable to complete send buffer " 542 "initialization with NetVsp - status %d\n", 543 init_packet->msg.v1_msg. 544 send_send_buf_complete.status); 545 ret = -EINVAL; 546 goto cleanup; 547 } 548 549 /* Parse the response */ 550 net_device->send_section_size = init_packet->msg. 551 v1_msg.send_send_buf_complete.section_size; 552 if (net_device->send_section_size < NETVSC_MTU_MIN) { 553 netdev_err(ndev, "invalid send_section_size %u\n", 554 net_device->send_section_size); 555 ret = -EINVAL; 556 goto cleanup; 557 } 558 559 /* Section count is simply the size divided by the section size. */ 560 net_device->send_section_cnt = buf_size / net_device->send_section_size; 561 562 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n", 563 net_device->send_section_size, net_device->send_section_cnt); 564 565 /* Setup state for managing the send buffer. */ 566 net_device->send_section_map = bitmap_zalloc(net_device->send_section_cnt, 567 GFP_KERNEL); 568 if (!net_device->send_section_map) { 569 ret = -ENOMEM; 570 goto cleanup; 571 } 572 573 goto exit; 574 575 cleanup: 576 netvsc_revoke_recv_buf(device, net_device, ndev); 577 netvsc_revoke_send_buf(device, net_device, ndev); 578 netvsc_teardown_recv_gpadl(device, net_device, ndev); 579 netvsc_teardown_send_gpadl(device, net_device, ndev); 580 581 exit: 582 return ret; 583 } 584 585 /* Negotiate NVSP protocol version */ 586 static int negotiate_nvsp_ver(struct hv_device *device, 587 struct netvsc_device *net_device, 588 struct nvsp_message *init_packet, 589 u32 nvsp_ver) 590 { 591 struct net_device *ndev = hv_get_drvdata(device); 592 int ret; 593 594 memset(init_packet, 0, sizeof(struct nvsp_message)); 595 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT; 596 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver; 597 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver; 598 trace_nvsp_send(ndev, init_packet); 599 600 /* Send the init request */ 601 ret = vmbus_sendpacket(device->channel, init_packet, 602 sizeof(struct nvsp_message), 603 (unsigned long)init_packet, 604 VM_PKT_DATA_INBAND, 605 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 606 607 if (ret != 0) 608 return ret; 609 610 wait_for_completion(&net_device->channel_init_wait); 611 612 if (init_packet->msg.init_msg.init_complete.status != 613 NVSP_STAT_SUCCESS) 614 return -EINVAL; 615 616 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1) 617 return 0; 618 619 /* NVSPv2 or later: Send NDIS config */ 620 memset(init_packet, 0, sizeof(struct nvsp_message)); 621 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG; 622 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN; 623 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1; 624 625 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) { 626 if (hv_is_isolation_supported()) 627 netdev_info(ndev, "SR-IOV not advertised by guests on the host supporting isolation\n"); 628 else 629 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1; 630 631 /* Teaming bit is needed to receive link speed updates */ 632 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1; 633 } 634 635 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_61) 636 init_packet->msg.v2_msg.send_ndis_config.capability.rsc = 1; 637 638 trace_nvsp_send(ndev, init_packet); 639 640 ret = vmbus_sendpacket(device->channel, init_packet, 641 sizeof(struct nvsp_message), 642 VMBUS_RQST_ID_NO_RESPONSE, 643 VM_PKT_DATA_INBAND, 0); 644 645 return ret; 646 } 647 648 static int netvsc_connect_vsp(struct hv_device *device, 649 struct netvsc_device *net_device, 650 const struct netvsc_device_info *device_info) 651 { 652 struct net_device *ndev = hv_get_drvdata(device); 653 static const u32 ver_list[] = { 654 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2, 655 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5, 656 NVSP_PROTOCOL_VERSION_6, NVSP_PROTOCOL_VERSION_61 657 }; 658 struct nvsp_message *init_packet; 659 int ndis_version, i, ret; 660 661 init_packet = &net_device->channel_init_pkt; 662 663 /* Negotiate the latest NVSP protocol supported */ 664 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--) 665 if (negotiate_nvsp_ver(device, net_device, init_packet, 666 ver_list[i]) == 0) { 667 net_device->nvsp_version = ver_list[i]; 668 break; 669 } 670 671 if (i < 0) { 672 ret = -EPROTO; 673 goto cleanup; 674 } 675 676 if (hv_is_isolation_supported() && net_device->nvsp_version < NVSP_PROTOCOL_VERSION_61) { 677 netdev_err(ndev, "Invalid NVSP version 0x%x (expected >= 0x%x) from the host supporting isolation\n", 678 net_device->nvsp_version, NVSP_PROTOCOL_VERSION_61); 679 ret = -EPROTO; 680 goto cleanup; 681 } 682 683 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version); 684 685 /* Send the ndis version */ 686 memset(init_packet, 0, sizeof(struct nvsp_message)); 687 688 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4) 689 ndis_version = 0x00060001; 690 else 691 ndis_version = 0x0006001e; 692 693 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER; 694 init_packet->msg.v1_msg. 695 send_ndis_ver.ndis_major_ver = 696 (ndis_version & 0xFFFF0000) >> 16; 697 init_packet->msg.v1_msg. 698 send_ndis_ver.ndis_minor_ver = 699 ndis_version & 0xFFFF; 700 701 trace_nvsp_send(ndev, init_packet); 702 703 /* Send the init request */ 704 ret = vmbus_sendpacket(device->channel, init_packet, 705 sizeof(struct nvsp_message), 706 VMBUS_RQST_ID_NO_RESPONSE, 707 VM_PKT_DATA_INBAND, 0); 708 if (ret != 0) 709 goto cleanup; 710 711 712 ret = netvsc_init_buf(device, net_device, device_info); 713 714 cleanup: 715 return ret; 716 } 717 718 /* 719 * netvsc_device_remove - Callback when the root bus device is removed 720 */ 721 void netvsc_device_remove(struct hv_device *device) 722 { 723 struct net_device *ndev = hv_get_drvdata(device); 724 struct net_device_context *net_device_ctx = netdev_priv(ndev); 725 struct netvsc_device *net_device 726 = rtnl_dereference(net_device_ctx->nvdev); 727 int i; 728 729 /* 730 * Revoke receive buffer. If host is pre-Win2016 then tear down 731 * receive buffer GPADL. Do the same for send buffer. 732 */ 733 netvsc_revoke_recv_buf(device, net_device, ndev); 734 if (vmbus_proto_version < VERSION_WIN10) 735 netvsc_teardown_recv_gpadl(device, net_device, ndev); 736 737 netvsc_revoke_send_buf(device, net_device, ndev); 738 if (vmbus_proto_version < VERSION_WIN10) 739 netvsc_teardown_send_gpadl(device, net_device, ndev); 740 741 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL); 742 743 /* Disable NAPI and disassociate its context from the device. */ 744 for (i = 0; i < net_device->num_chn; i++) { 745 /* See also vmbus_reset_channel_cb(). */ 746 napi_disable(&net_device->chan_table[i].napi); 747 netif_napi_del(&net_device->chan_table[i].napi); 748 } 749 750 /* 751 * At this point, no one should be accessing net_device 752 * except in here 753 */ 754 netdev_dbg(ndev, "net device safe to remove\n"); 755 756 /* Now, we can close the channel safely */ 757 vmbus_close(device->channel); 758 759 /* 760 * If host is Win2016 or higher then we do the GPADL tear down 761 * here after VMBus is closed. 762 */ 763 if (vmbus_proto_version >= VERSION_WIN10) { 764 netvsc_teardown_recv_gpadl(device, net_device, ndev); 765 netvsc_teardown_send_gpadl(device, net_device, ndev); 766 } 767 768 /* Release all resources */ 769 free_netvsc_device_rcu(net_device); 770 } 771 772 #define RING_AVAIL_PERCENT_HIWATER 20 773 #define RING_AVAIL_PERCENT_LOWATER 10 774 775 static inline void netvsc_free_send_slot(struct netvsc_device *net_device, 776 u32 index) 777 { 778 sync_change_bit(index, net_device->send_section_map); 779 } 780 781 static void netvsc_send_tx_complete(struct net_device *ndev, 782 struct netvsc_device *net_device, 783 struct vmbus_channel *channel, 784 const struct vmpacket_descriptor *desc, 785 int budget) 786 { 787 struct net_device_context *ndev_ctx = netdev_priv(ndev); 788 struct sk_buff *skb; 789 u16 q_idx = 0; 790 int queue_sends; 791 u64 cmd_rqst; 792 793 cmd_rqst = channel->request_addr_callback(channel, (u64)desc->trans_id); 794 if (cmd_rqst == VMBUS_RQST_ERROR) { 795 netdev_err(ndev, "Incorrect transaction id\n"); 796 return; 797 } 798 799 skb = (struct sk_buff *)(unsigned long)cmd_rqst; 800 801 /* Notify the layer above us */ 802 if (likely(skb)) { 803 struct hv_netvsc_packet *packet 804 = (struct hv_netvsc_packet *)skb->cb; 805 u32 send_index = packet->send_buf_index; 806 struct netvsc_stats *tx_stats; 807 808 if (send_index != NETVSC_INVALID_INDEX) 809 netvsc_free_send_slot(net_device, send_index); 810 q_idx = packet->q_idx; 811 812 tx_stats = &net_device->chan_table[q_idx].tx_stats; 813 814 u64_stats_update_begin(&tx_stats->syncp); 815 tx_stats->packets += packet->total_packets; 816 tx_stats->bytes += packet->total_bytes; 817 u64_stats_update_end(&tx_stats->syncp); 818 819 netvsc_dma_unmap(ndev_ctx->device_ctx, packet); 820 napi_consume_skb(skb, budget); 821 } 822 823 queue_sends = 824 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends); 825 826 if (unlikely(net_device->destroy)) { 827 if (queue_sends == 0) 828 wake_up(&net_device->wait_drain); 829 } else { 830 struct netdev_queue *txq = netdev_get_tx_queue(ndev, q_idx); 831 832 if (netif_tx_queue_stopped(txq) && !net_device->tx_disable && 833 (hv_get_avail_to_write_percent(&channel->outbound) > 834 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1)) { 835 netif_tx_wake_queue(txq); 836 ndev_ctx->eth_stats.wake_queue++; 837 } 838 } 839 } 840 841 static void netvsc_send_completion(struct net_device *ndev, 842 struct netvsc_device *net_device, 843 struct vmbus_channel *incoming_channel, 844 const struct vmpacket_descriptor *desc, 845 int budget) 846 { 847 const struct nvsp_message *nvsp_packet; 848 u32 msglen = hv_pkt_datalen(desc); 849 struct nvsp_message *pkt_rqst; 850 u64 cmd_rqst; 851 852 /* First check if this is a VMBUS completion without data payload */ 853 if (!msglen) { 854 cmd_rqst = incoming_channel->request_addr_callback(incoming_channel, 855 (u64)desc->trans_id); 856 if (cmd_rqst == VMBUS_RQST_ERROR) { 857 netdev_err(ndev, "Invalid transaction id\n"); 858 return; 859 } 860 861 pkt_rqst = (struct nvsp_message *)(uintptr_t)cmd_rqst; 862 switch (pkt_rqst->hdr.msg_type) { 863 case NVSP_MSG4_TYPE_SWITCH_DATA_PATH: 864 complete(&net_device->channel_init_wait); 865 break; 866 867 default: 868 netdev_err(ndev, "Unexpected VMBUS completion!!\n"); 869 } 870 return; 871 } 872 873 /* Ensure packet is big enough to read header fields */ 874 if (msglen < sizeof(struct nvsp_message_header)) { 875 netdev_err(ndev, "nvsp_message length too small: %u\n", msglen); 876 return; 877 } 878 879 nvsp_packet = hv_pkt_data(desc); 880 switch (nvsp_packet->hdr.msg_type) { 881 case NVSP_MSG_TYPE_INIT_COMPLETE: 882 if (msglen < sizeof(struct nvsp_message_header) + 883 sizeof(struct nvsp_message_init_complete)) { 884 netdev_err(ndev, "nvsp_msg length too small: %u\n", 885 msglen); 886 return; 887 } 888 fallthrough; 889 890 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE: 891 if (msglen < sizeof(struct nvsp_message_header) + 892 sizeof(struct nvsp_1_message_send_receive_buffer_complete)) { 893 netdev_err(ndev, "nvsp_msg1 length too small: %u\n", 894 msglen); 895 return; 896 } 897 fallthrough; 898 899 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE: 900 if (msglen < sizeof(struct nvsp_message_header) + 901 sizeof(struct nvsp_1_message_send_send_buffer_complete)) { 902 netdev_err(ndev, "nvsp_msg1 length too small: %u\n", 903 msglen); 904 return; 905 } 906 fallthrough; 907 908 case NVSP_MSG5_TYPE_SUBCHANNEL: 909 if (msglen < sizeof(struct nvsp_message_header) + 910 sizeof(struct nvsp_5_subchannel_complete)) { 911 netdev_err(ndev, "nvsp_msg5 length too small: %u\n", 912 msglen); 913 return; 914 } 915 /* Copy the response back */ 916 memcpy(&net_device->channel_init_pkt, nvsp_packet, 917 sizeof(struct nvsp_message)); 918 complete(&net_device->channel_init_wait); 919 break; 920 921 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE: 922 netvsc_send_tx_complete(ndev, net_device, incoming_channel, 923 desc, budget); 924 break; 925 926 default: 927 netdev_err(ndev, 928 "Unknown send completion type %d received!!\n", 929 nvsp_packet->hdr.msg_type); 930 } 931 } 932 933 static u32 netvsc_get_next_send_section(struct netvsc_device *net_device) 934 { 935 unsigned long *map_addr = net_device->send_section_map; 936 unsigned int i; 937 938 for_each_clear_bit(i, map_addr, net_device->send_section_cnt) { 939 if (sync_test_and_set_bit(i, map_addr) == 0) 940 return i; 941 } 942 943 return NETVSC_INVALID_INDEX; 944 } 945 946 static void netvsc_copy_to_send_buf(struct netvsc_device *net_device, 947 unsigned int section_index, 948 u32 pend_size, 949 struct hv_netvsc_packet *packet, 950 struct rndis_message *rndis_msg, 951 struct hv_page_buffer *pb, 952 bool xmit_more) 953 { 954 char *start = net_device->send_buf; 955 char *dest = start + (section_index * net_device->send_section_size) 956 + pend_size; 957 int i; 958 u32 padding = 0; 959 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt : 960 packet->page_buf_cnt; 961 u32 remain; 962 963 /* Add padding */ 964 remain = packet->total_data_buflen & (net_device->pkt_align - 1); 965 if (xmit_more && remain) { 966 padding = net_device->pkt_align - remain; 967 rndis_msg->msg_len += padding; 968 packet->total_data_buflen += padding; 969 } 970 971 for (i = 0; i < page_count; i++) { 972 char *src = phys_to_virt(pb[i].pfn << HV_HYP_PAGE_SHIFT); 973 u32 offset = pb[i].offset; 974 u32 len = pb[i].len; 975 976 memcpy(dest, (src + offset), len); 977 dest += len; 978 } 979 980 if (padding) 981 memset(dest, 0, padding); 982 } 983 984 void netvsc_dma_unmap(struct hv_device *hv_dev, 985 struct hv_netvsc_packet *packet) 986 { 987 u32 page_count = packet->cp_partial ? 988 packet->page_buf_cnt - packet->rmsg_pgcnt : 989 packet->page_buf_cnt; 990 int i; 991 992 if (!hv_is_isolation_supported()) 993 return; 994 995 if (!packet->dma_range) 996 return; 997 998 for (i = 0; i < page_count; i++) 999 dma_unmap_single(&hv_dev->device, packet->dma_range[i].dma, 1000 packet->dma_range[i].mapping_size, 1001 DMA_TO_DEVICE); 1002 1003 kfree(packet->dma_range); 1004 } 1005 1006 /* netvsc_dma_map - Map swiotlb bounce buffer with data page of 1007 * packet sent by vmbus_sendpacket_pagebuffer() in the Isolation 1008 * VM. 1009 * 1010 * In isolation VM, netvsc send buffer has been marked visible to 1011 * host and so the data copied to send buffer doesn't need to use 1012 * bounce buffer. The data pages handled by vmbus_sendpacket_pagebuffer() 1013 * may not be copied to send buffer and so these pages need to be 1014 * mapped with swiotlb bounce buffer. netvsc_dma_map() is to do 1015 * that. The pfns in the struct hv_page_buffer need to be converted 1016 * to bounce buffer's pfn. The loop here is necessary because the 1017 * entries in the page buffer array are not necessarily full 1018 * pages of data. Each entry in the array has a separate offset and 1019 * len that may be non-zero, even for entries in the middle of the 1020 * array. And the entries are not physically contiguous. So each 1021 * entry must be individually mapped rather than as a contiguous unit. 1022 * So not use dma_map_sg() here. 1023 */ 1024 static int netvsc_dma_map(struct hv_device *hv_dev, 1025 struct hv_netvsc_packet *packet, 1026 struct hv_page_buffer *pb) 1027 { 1028 u32 page_count = packet->cp_partial ? 1029 packet->page_buf_cnt - packet->rmsg_pgcnt : 1030 packet->page_buf_cnt; 1031 dma_addr_t dma; 1032 int i; 1033 1034 if (!hv_is_isolation_supported()) 1035 return 0; 1036 1037 packet->dma_range = kcalloc(page_count, 1038 sizeof(*packet->dma_range), 1039 GFP_KERNEL); 1040 if (!packet->dma_range) 1041 return -ENOMEM; 1042 1043 for (i = 0; i < page_count; i++) { 1044 char *src = phys_to_virt((pb[i].pfn << HV_HYP_PAGE_SHIFT) 1045 + pb[i].offset); 1046 u32 len = pb[i].len; 1047 1048 dma = dma_map_single(&hv_dev->device, src, len, 1049 DMA_TO_DEVICE); 1050 if (dma_mapping_error(&hv_dev->device, dma)) { 1051 kfree(packet->dma_range); 1052 return -ENOMEM; 1053 } 1054 1055 /* pb[].offset and pb[].len are not changed during dma mapping 1056 * and so not reassign. 1057 */ 1058 packet->dma_range[i].dma = dma; 1059 packet->dma_range[i].mapping_size = len; 1060 pb[i].pfn = dma >> HV_HYP_PAGE_SHIFT; 1061 } 1062 1063 return 0; 1064 } 1065 1066 static inline int netvsc_send_pkt( 1067 struct hv_device *device, 1068 struct hv_netvsc_packet *packet, 1069 struct netvsc_device *net_device, 1070 struct hv_page_buffer *pb, 1071 struct sk_buff *skb) 1072 { 1073 struct nvsp_message nvmsg; 1074 struct nvsp_1_message_send_rndis_packet *rpkt = 1075 &nvmsg.msg.v1_msg.send_rndis_pkt; 1076 struct netvsc_channel * const nvchan = 1077 &net_device->chan_table[packet->q_idx]; 1078 struct vmbus_channel *out_channel = nvchan->channel; 1079 struct net_device *ndev = hv_get_drvdata(device); 1080 struct net_device_context *ndev_ctx = netdev_priv(ndev); 1081 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx); 1082 u64 req_id; 1083 int ret; 1084 u32 ring_avail = hv_get_avail_to_write_percent(&out_channel->outbound); 1085 1086 memset(&nvmsg, 0, sizeof(struct nvsp_message)); 1087 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT; 1088 if (skb) 1089 rpkt->channel_type = 0; /* 0 is RMC_DATA */ 1090 else 1091 rpkt->channel_type = 1; /* 1 is RMC_CONTROL */ 1092 1093 rpkt->send_buf_section_index = packet->send_buf_index; 1094 if (packet->send_buf_index == NETVSC_INVALID_INDEX) 1095 rpkt->send_buf_section_size = 0; 1096 else 1097 rpkt->send_buf_section_size = packet->total_data_buflen; 1098 1099 req_id = (ulong)skb; 1100 1101 if (out_channel->rescind) 1102 return -ENODEV; 1103 1104 trace_nvsp_send_pkt(ndev, out_channel, rpkt); 1105 1106 packet->dma_range = NULL; 1107 if (packet->page_buf_cnt) { 1108 if (packet->cp_partial) 1109 pb += packet->rmsg_pgcnt; 1110 1111 ret = netvsc_dma_map(ndev_ctx->device_ctx, packet, pb); 1112 if (ret) { 1113 ret = -EAGAIN; 1114 goto exit; 1115 } 1116 1117 ret = vmbus_sendpacket_pagebuffer(out_channel, 1118 pb, packet->page_buf_cnt, 1119 &nvmsg, sizeof(nvmsg), 1120 req_id); 1121 1122 if (ret) 1123 netvsc_dma_unmap(ndev_ctx->device_ctx, packet); 1124 } else { 1125 ret = vmbus_sendpacket(out_channel, 1126 &nvmsg, sizeof(nvmsg), 1127 req_id, VM_PKT_DATA_INBAND, 1128 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 1129 } 1130 1131 exit: 1132 if (ret == 0) { 1133 atomic_inc_return(&nvchan->queue_sends); 1134 1135 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) { 1136 netif_tx_stop_queue(txq); 1137 ndev_ctx->eth_stats.stop_queue++; 1138 } 1139 } else if (ret == -EAGAIN) { 1140 netif_tx_stop_queue(txq); 1141 ndev_ctx->eth_stats.stop_queue++; 1142 } else { 1143 netdev_err(ndev, 1144 "Unable to send packet pages %u len %u, ret %d\n", 1145 packet->page_buf_cnt, packet->total_data_buflen, 1146 ret); 1147 } 1148 1149 if (netif_tx_queue_stopped(txq) && 1150 atomic_read(&nvchan->queue_sends) < 1 && 1151 !net_device->tx_disable) { 1152 netif_tx_wake_queue(txq); 1153 ndev_ctx->eth_stats.wake_queue++; 1154 if (ret == -EAGAIN) 1155 ret = -ENOSPC; 1156 } 1157 1158 return ret; 1159 } 1160 1161 /* Move packet out of multi send data (msd), and clear msd */ 1162 static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send, 1163 struct sk_buff **msd_skb, 1164 struct multi_send_data *msdp) 1165 { 1166 *msd_skb = msdp->skb; 1167 *msd_send = msdp->pkt; 1168 msdp->skb = NULL; 1169 msdp->pkt = NULL; 1170 msdp->count = 0; 1171 } 1172 1173 /* RCU already held by caller */ 1174 /* Batching/bouncing logic is designed to attempt to optimize 1175 * performance. 1176 * 1177 * For small, non-LSO packets we copy the packet to a send buffer 1178 * which is pre-registered with the Hyper-V side. This enables the 1179 * hypervisor to avoid remapping the aperture to access the packet 1180 * descriptor and data. 1181 * 1182 * If we already started using a buffer and the netdev is transmitting 1183 * a burst of packets, keep on copying into the buffer until it is 1184 * full or we are done collecting a burst. If there is an existing 1185 * buffer with space for the RNDIS descriptor but not the packet, copy 1186 * the RNDIS descriptor to the buffer, keeping the packet in place. 1187 * 1188 * If we do batching and send more than one packet using a single 1189 * NetVSC message, free the SKBs of the packets copied, except for the 1190 * last packet. This is done to streamline the handling of the case 1191 * where the last packet only had the RNDIS descriptor copied to the 1192 * send buffer, with the data pointers included in the NetVSC message. 1193 */ 1194 int netvsc_send(struct net_device *ndev, 1195 struct hv_netvsc_packet *packet, 1196 struct rndis_message *rndis_msg, 1197 struct hv_page_buffer *pb, 1198 struct sk_buff *skb, 1199 bool xdp_tx) 1200 { 1201 struct net_device_context *ndev_ctx = netdev_priv(ndev); 1202 struct netvsc_device *net_device 1203 = rcu_dereference_bh(ndev_ctx->nvdev); 1204 struct hv_device *device = ndev_ctx->device_ctx; 1205 int ret = 0; 1206 struct netvsc_channel *nvchan; 1207 u32 pktlen = packet->total_data_buflen, msd_len = 0; 1208 unsigned int section_index = NETVSC_INVALID_INDEX; 1209 struct multi_send_data *msdp; 1210 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL; 1211 struct sk_buff *msd_skb = NULL; 1212 bool try_batch, xmit_more; 1213 1214 /* If device is rescinded, return error and packet will get dropped. */ 1215 if (unlikely(!net_device || net_device->destroy)) 1216 return -ENODEV; 1217 1218 nvchan = &net_device->chan_table[packet->q_idx]; 1219 packet->send_buf_index = NETVSC_INVALID_INDEX; 1220 packet->cp_partial = false; 1221 1222 /* Send a control message or XDP packet directly without accessing 1223 * msd (Multi-Send Data) field which may be changed during data packet 1224 * processing. 1225 */ 1226 if (!skb || xdp_tx) 1227 return netvsc_send_pkt(device, packet, net_device, pb, skb); 1228 1229 /* batch packets in send buffer if possible */ 1230 msdp = &nvchan->msd; 1231 if (msdp->pkt) 1232 msd_len = msdp->pkt->total_data_buflen; 1233 1234 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt; 1235 if (try_batch && msd_len + pktlen + net_device->pkt_align < 1236 net_device->send_section_size) { 1237 section_index = msdp->pkt->send_buf_index; 1238 1239 } else if (try_batch && msd_len + packet->rmsg_size < 1240 net_device->send_section_size) { 1241 section_index = msdp->pkt->send_buf_index; 1242 packet->cp_partial = true; 1243 1244 } else if (pktlen + net_device->pkt_align < 1245 net_device->send_section_size) { 1246 section_index = netvsc_get_next_send_section(net_device); 1247 if (unlikely(section_index == NETVSC_INVALID_INDEX)) { 1248 ++ndev_ctx->eth_stats.tx_send_full; 1249 } else { 1250 move_pkt_msd(&msd_send, &msd_skb, msdp); 1251 msd_len = 0; 1252 } 1253 } 1254 1255 /* Keep aggregating only if stack says more data is coming 1256 * and not doing mixed modes send and not flow blocked 1257 */ 1258 xmit_more = netdev_xmit_more() && 1259 !packet->cp_partial && 1260 !netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx)); 1261 1262 if (section_index != NETVSC_INVALID_INDEX) { 1263 netvsc_copy_to_send_buf(net_device, 1264 section_index, msd_len, 1265 packet, rndis_msg, pb, xmit_more); 1266 1267 packet->send_buf_index = section_index; 1268 1269 if (packet->cp_partial) { 1270 packet->page_buf_cnt -= packet->rmsg_pgcnt; 1271 packet->total_data_buflen = msd_len + packet->rmsg_size; 1272 } else { 1273 packet->page_buf_cnt = 0; 1274 packet->total_data_buflen += msd_len; 1275 } 1276 1277 if (msdp->pkt) { 1278 packet->total_packets += msdp->pkt->total_packets; 1279 packet->total_bytes += msdp->pkt->total_bytes; 1280 } 1281 1282 if (msdp->skb) 1283 dev_consume_skb_any(msdp->skb); 1284 1285 if (xmit_more) { 1286 msdp->skb = skb; 1287 msdp->pkt = packet; 1288 msdp->count++; 1289 } else { 1290 cur_send = packet; 1291 msdp->skb = NULL; 1292 msdp->pkt = NULL; 1293 msdp->count = 0; 1294 } 1295 } else { 1296 move_pkt_msd(&msd_send, &msd_skb, msdp); 1297 cur_send = packet; 1298 } 1299 1300 if (msd_send) { 1301 int m_ret = netvsc_send_pkt(device, msd_send, net_device, 1302 NULL, msd_skb); 1303 1304 if (m_ret != 0) { 1305 netvsc_free_send_slot(net_device, 1306 msd_send->send_buf_index); 1307 dev_kfree_skb_any(msd_skb); 1308 } 1309 } 1310 1311 if (cur_send) 1312 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb); 1313 1314 if (ret != 0 && section_index != NETVSC_INVALID_INDEX) 1315 netvsc_free_send_slot(net_device, section_index); 1316 1317 return ret; 1318 } 1319 1320 /* Send pending recv completions */ 1321 static int send_recv_completions(struct net_device *ndev, 1322 struct netvsc_device *nvdev, 1323 struct netvsc_channel *nvchan) 1324 { 1325 struct multi_recv_comp *mrc = &nvchan->mrc; 1326 struct recv_comp_msg { 1327 struct nvsp_message_header hdr; 1328 u32 status; 1329 } __packed; 1330 struct recv_comp_msg msg = { 1331 .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE, 1332 }; 1333 int ret; 1334 1335 while (mrc->first != mrc->next) { 1336 const struct recv_comp_data *rcd 1337 = mrc->slots + mrc->first; 1338 1339 msg.status = rcd->status; 1340 ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg), 1341 rcd->tid, VM_PKT_COMP, 0); 1342 if (unlikely(ret)) { 1343 struct net_device_context *ndev_ctx = netdev_priv(ndev); 1344 1345 ++ndev_ctx->eth_stats.rx_comp_busy; 1346 return ret; 1347 } 1348 1349 if (++mrc->first == nvdev->recv_completion_cnt) 1350 mrc->first = 0; 1351 } 1352 1353 /* receive completion ring has been emptied */ 1354 if (unlikely(nvdev->destroy)) 1355 wake_up(&nvdev->wait_drain); 1356 1357 return 0; 1358 } 1359 1360 /* Count how many receive completions are outstanding */ 1361 static void recv_comp_slot_avail(const struct netvsc_device *nvdev, 1362 const struct multi_recv_comp *mrc, 1363 u32 *filled, u32 *avail) 1364 { 1365 u32 count = nvdev->recv_completion_cnt; 1366 1367 if (mrc->next >= mrc->first) 1368 *filled = mrc->next - mrc->first; 1369 else 1370 *filled = (count - mrc->first) + mrc->next; 1371 1372 *avail = count - *filled - 1; 1373 } 1374 1375 /* Add receive complete to ring to send to host. */ 1376 static void enq_receive_complete(struct net_device *ndev, 1377 struct netvsc_device *nvdev, u16 q_idx, 1378 u64 tid, u32 status) 1379 { 1380 struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx]; 1381 struct multi_recv_comp *mrc = &nvchan->mrc; 1382 struct recv_comp_data *rcd; 1383 u32 filled, avail; 1384 1385 recv_comp_slot_avail(nvdev, mrc, &filled, &avail); 1386 1387 if (unlikely(filled > NAPI_POLL_WEIGHT)) { 1388 send_recv_completions(ndev, nvdev, nvchan); 1389 recv_comp_slot_avail(nvdev, mrc, &filled, &avail); 1390 } 1391 1392 if (unlikely(!avail)) { 1393 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n", 1394 q_idx, tid); 1395 return; 1396 } 1397 1398 rcd = mrc->slots + mrc->next; 1399 rcd->tid = tid; 1400 rcd->status = status; 1401 1402 if (++mrc->next == nvdev->recv_completion_cnt) 1403 mrc->next = 0; 1404 } 1405 1406 static int netvsc_receive(struct net_device *ndev, 1407 struct netvsc_device *net_device, 1408 struct netvsc_channel *nvchan, 1409 const struct vmpacket_descriptor *desc) 1410 { 1411 struct net_device_context *net_device_ctx = netdev_priv(ndev); 1412 struct vmbus_channel *channel = nvchan->channel; 1413 const struct vmtransfer_page_packet_header *vmxferpage_packet 1414 = container_of(desc, const struct vmtransfer_page_packet_header, d); 1415 const struct nvsp_message *nvsp = hv_pkt_data(desc); 1416 u32 msglen = hv_pkt_datalen(desc); 1417 u16 q_idx = channel->offermsg.offer.sub_channel_index; 1418 char *recv_buf = net_device->recv_buf; 1419 u32 status = NVSP_STAT_SUCCESS; 1420 int i; 1421 int count = 0; 1422 1423 /* Ensure packet is big enough to read header fields */ 1424 if (msglen < sizeof(struct nvsp_message_header)) { 1425 netif_err(net_device_ctx, rx_err, ndev, 1426 "invalid nvsp header, length too small: %u\n", 1427 msglen); 1428 return 0; 1429 } 1430 1431 /* Make sure this is a valid nvsp packet */ 1432 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) { 1433 netif_err(net_device_ctx, rx_err, ndev, 1434 "Unknown nvsp packet type received %u\n", 1435 nvsp->hdr.msg_type); 1436 return 0; 1437 } 1438 1439 /* Validate xfer page pkt header */ 1440 if ((desc->offset8 << 3) < sizeof(struct vmtransfer_page_packet_header)) { 1441 netif_err(net_device_ctx, rx_err, ndev, 1442 "Invalid xfer page pkt, offset too small: %u\n", 1443 desc->offset8 << 3); 1444 return 0; 1445 } 1446 1447 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) { 1448 netif_err(net_device_ctx, rx_err, ndev, 1449 "Invalid xfer page set id - expecting %x got %x\n", 1450 NETVSC_RECEIVE_BUFFER_ID, 1451 vmxferpage_packet->xfer_pageset_id); 1452 return 0; 1453 } 1454 1455 count = vmxferpage_packet->range_cnt; 1456 1457 /* Check count for a valid value */ 1458 if (NETVSC_XFER_HEADER_SIZE(count) > desc->offset8 << 3) { 1459 netif_err(net_device_ctx, rx_err, ndev, 1460 "Range count is not valid: %d\n", 1461 count); 1462 return 0; 1463 } 1464 1465 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */ 1466 for (i = 0; i < count; i++) { 1467 u32 offset = vmxferpage_packet->ranges[i].byte_offset; 1468 u32 buflen = vmxferpage_packet->ranges[i].byte_count; 1469 void *data; 1470 int ret; 1471 1472 if (unlikely(offset > net_device->recv_buf_size || 1473 buflen > net_device->recv_buf_size - offset)) { 1474 nvchan->rsc.cnt = 0; 1475 status = NVSP_STAT_FAIL; 1476 netif_err(net_device_ctx, rx_err, ndev, 1477 "Packet offset:%u + len:%u too big\n", 1478 offset, buflen); 1479 1480 continue; 1481 } 1482 1483 /* We're going to copy (sections of) the packet into nvchan->recv_buf; 1484 * make sure that nvchan->recv_buf is large enough to hold the packet. 1485 */ 1486 if (unlikely(buflen > net_device->recv_section_size)) { 1487 nvchan->rsc.cnt = 0; 1488 status = NVSP_STAT_FAIL; 1489 netif_err(net_device_ctx, rx_err, ndev, 1490 "Packet too big: buflen=%u recv_section_size=%u\n", 1491 buflen, net_device->recv_section_size); 1492 1493 continue; 1494 } 1495 1496 data = recv_buf + offset; 1497 1498 nvchan->rsc.is_last = (i == count - 1); 1499 1500 trace_rndis_recv(ndev, q_idx, data); 1501 1502 /* Pass it to the upper layer */ 1503 ret = rndis_filter_receive(ndev, net_device, 1504 nvchan, data, buflen); 1505 1506 if (unlikely(ret != NVSP_STAT_SUCCESS)) { 1507 /* Drop incomplete packet */ 1508 nvchan->rsc.cnt = 0; 1509 status = NVSP_STAT_FAIL; 1510 } 1511 } 1512 1513 enq_receive_complete(ndev, net_device, q_idx, 1514 vmxferpage_packet->d.trans_id, status); 1515 1516 return count; 1517 } 1518 1519 static void netvsc_send_table(struct net_device *ndev, 1520 struct netvsc_device *nvscdev, 1521 const struct nvsp_message *nvmsg, 1522 u32 msglen) 1523 { 1524 struct net_device_context *net_device_ctx = netdev_priv(ndev); 1525 u32 count, offset, *tab; 1526 int i; 1527 1528 /* Ensure packet is big enough to read send_table fields */ 1529 if (msglen < sizeof(struct nvsp_message_header) + 1530 sizeof(struct nvsp_5_send_indirect_table)) { 1531 netdev_err(ndev, "nvsp_v5_msg length too small: %u\n", msglen); 1532 return; 1533 } 1534 1535 count = nvmsg->msg.v5_msg.send_table.count; 1536 offset = nvmsg->msg.v5_msg.send_table.offset; 1537 1538 if (count != VRSS_SEND_TAB_SIZE) { 1539 netdev_err(ndev, "Received wrong send-table size:%u\n", count); 1540 return; 1541 } 1542 1543 /* If negotiated version <= NVSP_PROTOCOL_VERSION_6, the offset may be 1544 * wrong due to a host bug. So fix the offset here. 1545 */ 1546 if (nvscdev->nvsp_version <= NVSP_PROTOCOL_VERSION_6 && 1547 msglen >= sizeof(struct nvsp_message_header) + 1548 sizeof(union nvsp_6_message_uber) + count * sizeof(u32)) 1549 offset = sizeof(struct nvsp_message_header) + 1550 sizeof(union nvsp_6_message_uber); 1551 1552 /* Boundary check for all versions */ 1553 if (msglen < count * sizeof(u32) || offset > msglen - count * sizeof(u32)) { 1554 netdev_err(ndev, "Received send-table offset too big:%u\n", 1555 offset); 1556 return; 1557 } 1558 1559 tab = (void *)nvmsg + offset; 1560 1561 for (i = 0; i < count; i++) 1562 net_device_ctx->tx_table[i] = tab[i]; 1563 } 1564 1565 static void netvsc_send_vf(struct net_device *ndev, 1566 const struct nvsp_message *nvmsg, 1567 u32 msglen) 1568 { 1569 struct net_device_context *net_device_ctx = netdev_priv(ndev); 1570 1571 /* Ensure packet is big enough to read its fields */ 1572 if (msglen < sizeof(struct nvsp_message_header) + 1573 sizeof(struct nvsp_4_send_vf_association)) { 1574 netdev_err(ndev, "nvsp_v4_msg length too small: %u\n", msglen); 1575 return; 1576 } 1577 1578 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated; 1579 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial; 1580 netdev_info(ndev, "VF slot %u %s\n", 1581 net_device_ctx->vf_serial, 1582 net_device_ctx->vf_alloc ? "added" : "removed"); 1583 } 1584 1585 static void netvsc_receive_inband(struct net_device *ndev, 1586 struct netvsc_device *nvscdev, 1587 const struct vmpacket_descriptor *desc) 1588 { 1589 const struct nvsp_message *nvmsg = hv_pkt_data(desc); 1590 u32 msglen = hv_pkt_datalen(desc); 1591 1592 /* Ensure packet is big enough to read header fields */ 1593 if (msglen < sizeof(struct nvsp_message_header)) { 1594 netdev_err(ndev, "inband nvsp_message length too small: %u\n", msglen); 1595 return; 1596 } 1597 1598 switch (nvmsg->hdr.msg_type) { 1599 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE: 1600 netvsc_send_table(ndev, nvscdev, nvmsg, msglen); 1601 break; 1602 1603 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION: 1604 if (hv_is_isolation_supported()) 1605 netdev_err(ndev, "Ignore VF_ASSOCIATION msg from the host supporting isolation\n"); 1606 else 1607 netvsc_send_vf(ndev, nvmsg, msglen); 1608 break; 1609 } 1610 } 1611 1612 static int netvsc_process_raw_pkt(struct hv_device *device, 1613 struct netvsc_channel *nvchan, 1614 struct netvsc_device *net_device, 1615 struct net_device *ndev, 1616 const struct vmpacket_descriptor *desc, 1617 int budget) 1618 { 1619 struct vmbus_channel *channel = nvchan->channel; 1620 const struct nvsp_message *nvmsg = hv_pkt_data(desc); 1621 1622 trace_nvsp_recv(ndev, channel, nvmsg); 1623 1624 switch (desc->type) { 1625 case VM_PKT_COMP: 1626 netvsc_send_completion(ndev, net_device, channel, desc, budget); 1627 break; 1628 1629 case VM_PKT_DATA_USING_XFER_PAGES: 1630 return netvsc_receive(ndev, net_device, nvchan, desc); 1631 break; 1632 1633 case VM_PKT_DATA_INBAND: 1634 netvsc_receive_inband(ndev, net_device, desc); 1635 break; 1636 1637 default: 1638 netdev_err(ndev, "unhandled packet type %d, tid %llx\n", 1639 desc->type, desc->trans_id); 1640 break; 1641 } 1642 1643 return 0; 1644 } 1645 1646 static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel) 1647 { 1648 struct vmbus_channel *primary = channel->primary_channel; 1649 1650 return primary ? primary->device_obj : channel->device_obj; 1651 } 1652 1653 /* Network processing softirq 1654 * Process data in incoming ring buffer from host 1655 * Stops when ring is empty or budget is met or exceeded. 1656 */ 1657 int netvsc_poll(struct napi_struct *napi, int budget) 1658 { 1659 struct netvsc_channel *nvchan 1660 = container_of(napi, struct netvsc_channel, napi); 1661 struct netvsc_device *net_device = nvchan->net_device; 1662 struct vmbus_channel *channel = nvchan->channel; 1663 struct hv_device *device = netvsc_channel_to_device(channel); 1664 struct net_device *ndev = hv_get_drvdata(device); 1665 int work_done = 0; 1666 int ret; 1667 1668 /* If starting a new interval */ 1669 if (!nvchan->desc) 1670 nvchan->desc = hv_pkt_iter_first(channel); 1671 1672 while (nvchan->desc && work_done < budget) { 1673 work_done += netvsc_process_raw_pkt(device, nvchan, net_device, 1674 ndev, nvchan->desc, budget); 1675 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc); 1676 } 1677 1678 /* Send any pending receive completions */ 1679 ret = send_recv_completions(ndev, net_device, nvchan); 1680 1681 /* If it did not exhaust NAPI budget this time 1682 * and not doing busy poll 1683 * then re-enable host interrupts 1684 * and reschedule if ring is not empty 1685 * or sending receive completion failed. 1686 */ 1687 if (work_done < budget && 1688 napi_complete_done(napi, work_done) && 1689 (ret || hv_end_read(&channel->inbound)) && 1690 napi_schedule_prep(napi)) { 1691 hv_begin_read(&channel->inbound); 1692 __napi_schedule(napi); 1693 } 1694 1695 /* Driver may overshoot since multiple packets per descriptor */ 1696 return min(work_done, budget); 1697 } 1698 1699 /* Call back when data is available in host ring buffer. 1700 * Processing is deferred until network softirq (NAPI) 1701 */ 1702 void netvsc_channel_cb(void *context) 1703 { 1704 struct netvsc_channel *nvchan = context; 1705 struct vmbus_channel *channel = nvchan->channel; 1706 struct hv_ring_buffer_info *rbi = &channel->inbound; 1707 1708 /* preload first vmpacket descriptor */ 1709 prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index); 1710 1711 if (napi_schedule_prep(&nvchan->napi)) { 1712 /* disable interrupts from host */ 1713 hv_begin_read(rbi); 1714 1715 __napi_schedule_irqoff(&nvchan->napi); 1716 } 1717 } 1718 1719 /* 1720 * netvsc_device_add - Callback when the device belonging to this 1721 * driver is added 1722 */ 1723 struct netvsc_device *netvsc_device_add(struct hv_device *device, 1724 const struct netvsc_device_info *device_info) 1725 { 1726 int i, ret = 0; 1727 struct netvsc_device *net_device; 1728 struct net_device *ndev = hv_get_drvdata(device); 1729 struct net_device_context *net_device_ctx = netdev_priv(ndev); 1730 1731 net_device = alloc_net_device(); 1732 if (!net_device) 1733 return ERR_PTR(-ENOMEM); 1734 1735 for (i = 0; i < VRSS_SEND_TAB_SIZE; i++) 1736 net_device_ctx->tx_table[i] = 0; 1737 1738 /* Because the device uses NAPI, all the interrupt batching and 1739 * control is done via Net softirq, not the channel handling 1740 */ 1741 set_channel_read_mode(device->channel, HV_CALL_ISR); 1742 1743 /* If we're reopening the device we may have multiple queues, fill the 1744 * chn_table with the default channel to use it before subchannels are 1745 * opened. 1746 * Initialize the channel state before we open; 1747 * we can be interrupted as soon as we open the channel. 1748 */ 1749 1750 for (i = 0; i < VRSS_CHANNEL_MAX; i++) { 1751 struct netvsc_channel *nvchan = &net_device->chan_table[i]; 1752 1753 nvchan->channel = device->channel; 1754 nvchan->net_device = net_device; 1755 u64_stats_init(&nvchan->tx_stats.syncp); 1756 u64_stats_init(&nvchan->rx_stats.syncp); 1757 1758 ret = xdp_rxq_info_reg(&nvchan->xdp_rxq, ndev, i, 0); 1759 1760 if (ret) { 1761 netdev_err(ndev, "xdp_rxq_info_reg fail: %d\n", ret); 1762 goto cleanup2; 1763 } 1764 1765 ret = xdp_rxq_info_reg_mem_model(&nvchan->xdp_rxq, 1766 MEM_TYPE_PAGE_SHARED, NULL); 1767 1768 if (ret) { 1769 netdev_err(ndev, "xdp reg_mem_model fail: %d\n", ret); 1770 goto cleanup2; 1771 } 1772 } 1773 1774 /* Enable NAPI handler before init callbacks */ 1775 netif_napi_add(ndev, &net_device->chan_table[0].napi, 1776 netvsc_poll, NAPI_POLL_WEIGHT); 1777 1778 /* Open the channel */ 1779 device->channel->next_request_id_callback = vmbus_next_request_id; 1780 device->channel->request_addr_callback = vmbus_request_addr; 1781 device->channel->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes); 1782 device->channel->max_pkt_size = NETVSC_MAX_PKT_SIZE; 1783 1784 ret = vmbus_open(device->channel, netvsc_ring_bytes, 1785 netvsc_ring_bytes, NULL, 0, 1786 netvsc_channel_cb, net_device->chan_table); 1787 1788 if (ret != 0) { 1789 netdev_err(ndev, "unable to open channel: %d\n", ret); 1790 goto cleanup; 1791 } 1792 1793 /* Channel is opened */ 1794 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n"); 1795 1796 napi_enable(&net_device->chan_table[0].napi); 1797 1798 /* Connect with the NetVsp */ 1799 ret = netvsc_connect_vsp(device, net_device, device_info); 1800 if (ret != 0) { 1801 netdev_err(ndev, 1802 "unable to connect to NetVSP - %d\n", ret); 1803 goto close; 1804 } 1805 1806 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is 1807 * populated. 1808 */ 1809 rcu_assign_pointer(net_device_ctx->nvdev, net_device); 1810 1811 return net_device; 1812 1813 close: 1814 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL); 1815 napi_disable(&net_device->chan_table[0].napi); 1816 1817 /* Now, we can close the channel safely */ 1818 vmbus_close(device->channel); 1819 1820 cleanup: 1821 netif_napi_del(&net_device->chan_table[0].napi); 1822 1823 cleanup2: 1824 free_netvsc_device(&net_device->rcu); 1825 1826 return ERR_PTR(ret); 1827 } 1828