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