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