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