1 /* 2 * Copyright (c) 2009, Microsoft Corporation. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms and conditions of the GNU General Public License, 6 * version 2, as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program; if not, see <http://www.gnu.org/licenses/>. 15 * 16 * Authors: 17 * Haiyang Zhang <haiyangz@microsoft.com> 18 * Hank Janssen <hjanssen@microsoft.com> 19 */ 20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 21 22 #include <linux/kernel.h> 23 #include <linux/sched.h> 24 #include <linux/wait.h> 25 #include <linux/mm.h> 26 #include <linux/delay.h> 27 #include <linux/io.h> 28 #include <linux/slab.h> 29 #include <linux/netdevice.h> 30 #include <linux/if_ether.h> 31 #include <asm/sync_bitops.h> 32 33 #include "hyperv_net.h" 34 35 36 static struct netvsc_device *alloc_net_device(struct hv_device *device) 37 { 38 struct netvsc_device *net_device; 39 struct net_device *ndev = hv_get_drvdata(device); 40 41 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL); 42 if (!net_device) 43 return NULL; 44 45 init_waitqueue_head(&net_device->wait_drain); 46 net_device->start_remove = false; 47 net_device->destroy = false; 48 net_device->dev = device; 49 net_device->ndev = ndev; 50 51 hv_set_drvdata(device, net_device); 52 return net_device; 53 } 54 55 static struct netvsc_device *get_outbound_net_device(struct hv_device *device) 56 { 57 struct netvsc_device *net_device; 58 59 net_device = hv_get_drvdata(device); 60 if (net_device && net_device->destroy) 61 net_device = NULL; 62 63 return net_device; 64 } 65 66 static struct netvsc_device *get_inbound_net_device(struct hv_device *device) 67 { 68 struct netvsc_device *net_device; 69 70 net_device = hv_get_drvdata(device); 71 72 if (!net_device) 73 goto get_in_err; 74 75 if (net_device->destroy && 76 atomic_read(&net_device->num_outstanding_sends) == 0) 77 net_device = NULL; 78 79 get_in_err: 80 return net_device; 81 } 82 83 84 static int netvsc_destroy_buf(struct netvsc_device *net_device) 85 { 86 struct nvsp_message *revoke_packet; 87 int ret = 0; 88 struct net_device *ndev = net_device->ndev; 89 90 /* 91 * If we got a section count, it means we received a 92 * SendReceiveBufferComplete msg (ie sent 93 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need 94 * to send a revoke msg here 95 */ 96 if (net_device->recv_section_cnt) { 97 /* Send the revoke receive buffer */ 98 revoke_packet = &net_device->revoke_packet; 99 memset(revoke_packet, 0, sizeof(struct nvsp_message)); 100 101 revoke_packet->hdr.msg_type = 102 NVSP_MSG1_TYPE_REVOKE_RECV_BUF; 103 revoke_packet->msg.v1_msg. 104 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; 105 106 ret = vmbus_sendpacket(net_device->dev->channel, 107 revoke_packet, 108 sizeof(struct nvsp_message), 109 (unsigned long)revoke_packet, 110 VM_PKT_DATA_INBAND, 0); 111 /* 112 * If we failed here, we might as well return and 113 * have a leak rather than continue and a bugchk 114 */ 115 if (ret != 0) { 116 netdev_err(ndev, "unable to send " 117 "revoke receive buffer to netvsp\n"); 118 return ret; 119 } 120 } 121 122 /* Teardown the gpadl on the vsp end */ 123 if (net_device->recv_buf_gpadl_handle) { 124 ret = vmbus_teardown_gpadl(net_device->dev->channel, 125 net_device->recv_buf_gpadl_handle); 126 127 /* If we failed here, we might as well return and have a leak 128 * rather than continue and a bugchk 129 */ 130 if (ret != 0) { 131 netdev_err(ndev, 132 "unable to teardown receive buffer's gpadl\n"); 133 return ret; 134 } 135 net_device->recv_buf_gpadl_handle = 0; 136 } 137 138 if (net_device->recv_buf) { 139 /* Free up the receive buffer */ 140 vfree(net_device->recv_buf); 141 net_device->recv_buf = NULL; 142 } 143 144 if (net_device->recv_section) { 145 net_device->recv_section_cnt = 0; 146 kfree(net_device->recv_section); 147 net_device->recv_section = NULL; 148 } 149 150 /* Deal with the send buffer we may have setup. 151 * If we got a send section size, it means we received a 152 * SendsendBufferComplete msg (ie sent 153 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need 154 * to send a revoke msg here 155 */ 156 if (net_device->send_section_size) { 157 /* Send the revoke receive buffer */ 158 revoke_packet = &net_device->revoke_packet; 159 memset(revoke_packet, 0, sizeof(struct nvsp_message)); 160 161 revoke_packet->hdr.msg_type = 162 NVSP_MSG1_TYPE_REVOKE_SEND_BUF; 163 revoke_packet->msg.v1_msg.revoke_recv_buf.id = 0; 164 165 ret = vmbus_sendpacket(net_device->dev->channel, 166 revoke_packet, 167 sizeof(struct nvsp_message), 168 (unsigned long)revoke_packet, 169 VM_PKT_DATA_INBAND, 0); 170 /* If we failed here, we might as well return and 171 * have a leak rather than continue and a bugchk 172 */ 173 if (ret != 0) { 174 netdev_err(ndev, "unable to send " 175 "revoke send buffer to netvsp\n"); 176 return ret; 177 } 178 } 179 /* Teardown the gpadl on the vsp end */ 180 if (net_device->send_buf_gpadl_handle) { 181 ret = vmbus_teardown_gpadl(net_device->dev->channel, 182 net_device->send_buf_gpadl_handle); 183 184 /* If we failed here, we might as well return and have a leak 185 * rather than continue and a bugchk 186 */ 187 if (ret != 0) { 188 netdev_err(ndev, 189 "unable to teardown send buffer's gpadl\n"); 190 return ret; 191 } 192 net_device->send_buf_gpadl_handle = 0; 193 } 194 if (net_device->send_buf) { 195 /* Free up the receive buffer */ 196 free_pages((unsigned long)net_device->send_buf, 197 get_order(net_device->send_buf_size)); 198 net_device->send_buf = NULL; 199 } 200 kfree(net_device->send_section_map); 201 202 return ret; 203 } 204 205 static int netvsc_init_buf(struct hv_device *device) 206 { 207 int ret = 0; 208 int t; 209 struct netvsc_device *net_device; 210 struct nvsp_message *init_packet; 211 struct net_device *ndev; 212 213 net_device = get_outbound_net_device(device); 214 if (!net_device) 215 return -ENODEV; 216 ndev = net_device->ndev; 217 218 net_device->recv_buf = vzalloc(net_device->recv_buf_size); 219 if (!net_device->recv_buf) { 220 netdev_err(ndev, "unable to allocate receive " 221 "buffer of size %d\n", net_device->recv_buf_size); 222 ret = -ENOMEM; 223 goto cleanup; 224 } 225 226 /* 227 * Establish the gpadl handle for this buffer on this 228 * channel. Note: This call uses the vmbus connection rather 229 * than the channel to establish the gpadl handle. 230 */ 231 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf, 232 net_device->recv_buf_size, 233 &net_device->recv_buf_gpadl_handle); 234 if (ret != 0) { 235 netdev_err(ndev, 236 "unable to establish receive buffer's gpadl\n"); 237 goto cleanup; 238 } 239 240 241 /* Notify the NetVsp of the gpadl handle */ 242 init_packet = &net_device->channel_init_pkt; 243 244 memset(init_packet, 0, sizeof(struct nvsp_message)); 245 246 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF; 247 init_packet->msg.v1_msg.send_recv_buf. 248 gpadl_handle = net_device->recv_buf_gpadl_handle; 249 init_packet->msg.v1_msg. 250 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; 251 252 /* Send the gpadl notification request */ 253 ret = vmbus_sendpacket(device->channel, init_packet, 254 sizeof(struct nvsp_message), 255 (unsigned long)init_packet, 256 VM_PKT_DATA_INBAND, 257 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 258 if (ret != 0) { 259 netdev_err(ndev, 260 "unable to send receive buffer's gpadl to netvsp\n"); 261 goto cleanup; 262 } 263 264 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ); 265 BUG_ON(t == 0); 266 267 268 /* Check the response */ 269 if (init_packet->msg.v1_msg. 270 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) { 271 netdev_err(ndev, "Unable to complete receive buffer " 272 "initialization with NetVsp - status %d\n", 273 init_packet->msg.v1_msg. 274 send_recv_buf_complete.status); 275 ret = -EINVAL; 276 goto cleanup; 277 } 278 279 /* Parse the response */ 280 281 net_device->recv_section_cnt = init_packet->msg. 282 v1_msg.send_recv_buf_complete.num_sections; 283 284 net_device->recv_section = kmemdup( 285 init_packet->msg.v1_msg.send_recv_buf_complete.sections, 286 net_device->recv_section_cnt * 287 sizeof(struct nvsp_1_receive_buffer_section), 288 GFP_KERNEL); 289 if (net_device->recv_section == NULL) { 290 ret = -EINVAL; 291 goto cleanup; 292 } 293 294 /* 295 * For 1st release, there should only be 1 section that represents the 296 * entire receive buffer 297 */ 298 if (net_device->recv_section_cnt != 1 || 299 net_device->recv_section->offset != 0) { 300 ret = -EINVAL; 301 goto cleanup; 302 } 303 304 /* Now setup the send buffer. 305 */ 306 net_device->send_buf = 307 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 308 get_order(net_device->send_buf_size)); 309 if (!net_device->send_buf) { 310 netdev_err(ndev, "unable to allocate send " 311 "buffer of size %d\n", net_device->send_buf_size); 312 ret = -ENOMEM; 313 goto cleanup; 314 } 315 316 /* Establish the gpadl handle for this buffer on this 317 * channel. Note: This call uses the vmbus connection rather 318 * than the channel to establish the gpadl handle. 319 */ 320 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf, 321 net_device->send_buf_size, 322 &net_device->send_buf_gpadl_handle); 323 if (ret != 0) { 324 netdev_err(ndev, 325 "unable to establish send buffer's gpadl\n"); 326 goto cleanup; 327 } 328 329 /* Notify the NetVsp of the gpadl handle */ 330 init_packet = &net_device->channel_init_pkt; 331 memset(init_packet, 0, sizeof(struct nvsp_message)); 332 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF; 333 init_packet->msg.v1_msg.send_recv_buf.gpadl_handle = 334 net_device->send_buf_gpadl_handle; 335 init_packet->msg.v1_msg.send_recv_buf.id = 0; 336 337 /* Send the gpadl notification request */ 338 ret = vmbus_sendpacket(device->channel, init_packet, 339 sizeof(struct nvsp_message), 340 (unsigned long)init_packet, 341 VM_PKT_DATA_INBAND, 342 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 343 if (ret != 0) { 344 netdev_err(ndev, 345 "unable to send send buffer's gpadl to netvsp\n"); 346 goto cleanup; 347 } 348 349 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ); 350 BUG_ON(t == 0); 351 352 /* Check the response */ 353 if (init_packet->msg.v1_msg. 354 send_send_buf_complete.status != NVSP_STAT_SUCCESS) { 355 netdev_err(ndev, "Unable to complete send buffer " 356 "initialization with NetVsp - status %d\n", 357 init_packet->msg.v1_msg. 358 send_recv_buf_complete.status); 359 ret = -EINVAL; 360 goto cleanup; 361 } 362 363 /* Parse the response */ 364 net_device->send_section_size = init_packet->msg. 365 v1_msg.send_send_buf_complete.section_size; 366 367 /* Section count is simply the size divided by the section size. 368 */ 369 net_device->send_section_cnt = 370 net_device->send_buf_size/net_device->send_section_size; 371 372 dev_info(&device->device, "Send section size: %d, Section count:%d\n", 373 net_device->send_section_size, net_device->send_section_cnt); 374 375 /* Setup state for managing the send buffer. */ 376 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt, 377 BITS_PER_LONG); 378 379 net_device->send_section_map = 380 kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL); 381 if (net_device->send_section_map == NULL) 382 goto cleanup; 383 384 goto exit; 385 386 cleanup: 387 netvsc_destroy_buf(net_device); 388 389 exit: 390 return ret; 391 } 392 393 394 /* Negotiate NVSP protocol version */ 395 static int negotiate_nvsp_ver(struct hv_device *device, 396 struct netvsc_device *net_device, 397 struct nvsp_message *init_packet, 398 u32 nvsp_ver) 399 { 400 int ret, t; 401 402 memset(init_packet, 0, sizeof(struct nvsp_message)); 403 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT; 404 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver; 405 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver; 406 407 /* Send the init request */ 408 ret = vmbus_sendpacket(device->channel, init_packet, 409 sizeof(struct nvsp_message), 410 (unsigned long)init_packet, 411 VM_PKT_DATA_INBAND, 412 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 413 414 if (ret != 0) 415 return ret; 416 417 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ); 418 419 if (t == 0) 420 return -ETIMEDOUT; 421 422 if (init_packet->msg.init_msg.init_complete.status != 423 NVSP_STAT_SUCCESS) 424 return -EINVAL; 425 426 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1) 427 return 0; 428 429 /* NVSPv2 only: Send NDIS config */ 430 memset(init_packet, 0, sizeof(struct nvsp_message)); 431 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG; 432 init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu; 433 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1; 434 435 ret = vmbus_sendpacket(device->channel, init_packet, 436 sizeof(struct nvsp_message), 437 (unsigned long)init_packet, 438 VM_PKT_DATA_INBAND, 0); 439 440 return ret; 441 } 442 443 static int netvsc_connect_vsp(struct hv_device *device) 444 { 445 int ret; 446 struct netvsc_device *net_device; 447 struct nvsp_message *init_packet; 448 int ndis_version; 449 struct net_device *ndev; 450 u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2, 451 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 }; 452 int i, num_ver = 4; /* number of different NVSP versions */ 453 454 net_device = get_outbound_net_device(device); 455 if (!net_device) 456 return -ENODEV; 457 ndev = net_device->ndev; 458 459 init_packet = &net_device->channel_init_pkt; 460 461 /* Negotiate the latest NVSP protocol supported */ 462 for (i = num_ver - 1; i >= 0; i--) 463 if (negotiate_nvsp_ver(device, net_device, init_packet, 464 ver_list[i]) == 0) { 465 net_device->nvsp_version = ver_list[i]; 466 break; 467 } 468 469 if (i < 0) { 470 ret = -EPROTO; 471 goto cleanup; 472 } 473 474 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version); 475 476 /* Send the ndis version */ 477 memset(init_packet, 0, sizeof(struct nvsp_message)); 478 479 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4) 480 ndis_version = 0x00060001; 481 else 482 ndis_version = 0x0006001e; 483 484 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER; 485 init_packet->msg.v1_msg. 486 send_ndis_ver.ndis_major_ver = 487 (ndis_version & 0xFFFF0000) >> 16; 488 init_packet->msg.v1_msg. 489 send_ndis_ver.ndis_minor_ver = 490 ndis_version & 0xFFFF; 491 492 /* Send the init request */ 493 ret = vmbus_sendpacket(device->channel, init_packet, 494 sizeof(struct nvsp_message), 495 (unsigned long)init_packet, 496 VM_PKT_DATA_INBAND, 0); 497 if (ret != 0) 498 goto cleanup; 499 500 /* Post the big receive buffer to NetVSP */ 501 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2) 502 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY; 503 else 504 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE; 505 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE; 506 507 ret = netvsc_init_buf(device); 508 509 cleanup: 510 return ret; 511 } 512 513 static void netvsc_disconnect_vsp(struct netvsc_device *net_device) 514 { 515 netvsc_destroy_buf(net_device); 516 } 517 518 /* 519 * netvsc_device_remove - Callback when the root bus device is removed 520 */ 521 int netvsc_device_remove(struct hv_device *device) 522 { 523 struct netvsc_device *net_device; 524 unsigned long flags; 525 526 net_device = hv_get_drvdata(device); 527 528 netvsc_disconnect_vsp(net_device); 529 530 /* 531 * Since we have already drained, we don't need to busy wait 532 * as was done in final_release_stor_device() 533 * Note that we cannot set the ext pointer to NULL until 534 * we have drained - to drain the outgoing packets, we need to 535 * allow incoming packets. 536 */ 537 538 spin_lock_irqsave(&device->channel->inbound_lock, flags); 539 hv_set_drvdata(device, NULL); 540 spin_unlock_irqrestore(&device->channel->inbound_lock, flags); 541 542 /* 543 * At this point, no one should be accessing net_device 544 * except in here 545 */ 546 dev_notice(&device->device, "net device safe to remove\n"); 547 548 /* Now, we can close the channel safely */ 549 vmbus_close(device->channel); 550 551 /* Release all resources */ 552 if (net_device->sub_cb_buf) 553 vfree(net_device->sub_cb_buf); 554 555 kfree(net_device); 556 return 0; 557 } 558 559 560 #define RING_AVAIL_PERCENT_HIWATER 20 561 #define RING_AVAIL_PERCENT_LOWATER 10 562 563 /* 564 * Get the percentage of available bytes to write in the ring. 565 * The return value is in range from 0 to 100. 566 */ 567 static inline u32 hv_ringbuf_avail_percent( 568 struct hv_ring_buffer_info *ring_info) 569 { 570 u32 avail_read, avail_write; 571 572 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write); 573 574 return avail_write * 100 / ring_info->ring_datasize; 575 } 576 577 static inline void netvsc_free_send_slot(struct netvsc_device *net_device, 578 u32 index) 579 { 580 sync_change_bit(index, net_device->send_section_map); 581 } 582 583 static void netvsc_send_completion(struct netvsc_device *net_device, 584 struct hv_device *device, 585 struct vmpacket_descriptor *packet) 586 { 587 struct nvsp_message *nvsp_packet; 588 struct hv_netvsc_packet *nvsc_packet; 589 struct net_device *ndev; 590 u32 send_index; 591 592 ndev = net_device->ndev; 593 594 nvsp_packet = (struct nvsp_message *)((unsigned long)packet + 595 (packet->offset8 << 3)); 596 597 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) || 598 (nvsp_packet->hdr.msg_type == 599 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) || 600 (nvsp_packet->hdr.msg_type == 601 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) || 602 (nvsp_packet->hdr.msg_type == 603 NVSP_MSG5_TYPE_SUBCHANNEL)) { 604 /* Copy the response back */ 605 memcpy(&net_device->channel_init_pkt, nvsp_packet, 606 sizeof(struct nvsp_message)); 607 complete(&net_device->channel_init_wait); 608 } else if (nvsp_packet->hdr.msg_type == 609 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) { 610 int num_outstanding_sends; 611 u16 q_idx = 0; 612 struct vmbus_channel *channel = device->channel; 613 int queue_sends; 614 615 /* Get the send context */ 616 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long) 617 packet->trans_id; 618 619 /* Notify the layer above us */ 620 if (nvsc_packet) { 621 send_index = nvsc_packet->send_buf_index; 622 if (send_index != NETVSC_INVALID_INDEX) 623 netvsc_free_send_slot(net_device, send_index); 624 q_idx = nvsc_packet->q_idx; 625 channel = nvsc_packet->channel; 626 nvsc_packet->send_completion(nvsc_packet-> 627 send_completion_ctx); 628 } 629 630 num_outstanding_sends = 631 atomic_dec_return(&net_device->num_outstanding_sends); 632 queue_sends = atomic_dec_return(&net_device-> 633 queue_sends[q_idx]); 634 635 if (net_device->destroy && num_outstanding_sends == 0) 636 wake_up(&net_device->wait_drain); 637 638 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) && 639 !net_device->start_remove && 640 (hv_ringbuf_avail_percent(&channel->outbound) > 641 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1)) 642 netif_tx_wake_queue(netdev_get_tx_queue( 643 ndev, q_idx)); 644 } else { 645 netdev_err(ndev, "Unknown send completion packet type- " 646 "%d received!!\n", nvsp_packet->hdr.msg_type); 647 } 648 649 } 650 651 static u32 netvsc_get_next_send_section(struct netvsc_device *net_device) 652 { 653 unsigned long index; 654 u32 max_words = net_device->map_words; 655 unsigned long *map_addr = (unsigned long *)net_device->send_section_map; 656 u32 section_cnt = net_device->send_section_cnt; 657 int ret_val = NETVSC_INVALID_INDEX; 658 int i; 659 int prev_val; 660 661 for (i = 0; i < max_words; i++) { 662 if (!~(map_addr[i])) 663 continue; 664 index = ffz(map_addr[i]); 665 prev_val = sync_test_and_set_bit(index, &map_addr[i]); 666 if (prev_val) 667 continue; 668 if ((index + (i * BITS_PER_LONG)) >= section_cnt) 669 break; 670 ret_val = (index + (i * BITS_PER_LONG)); 671 break; 672 } 673 return ret_val; 674 } 675 676 u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device, 677 unsigned int section_index, 678 struct hv_netvsc_packet *packet) 679 { 680 char *start = net_device->send_buf; 681 char *dest = (start + (section_index * net_device->send_section_size)); 682 int i; 683 u32 msg_size = 0; 684 685 for (i = 0; i < packet->page_buf_cnt; i++) { 686 char *src = phys_to_virt(packet->page_buf[i].pfn << PAGE_SHIFT); 687 u32 offset = packet->page_buf[i].offset; 688 u32 len = packet->page_buf[i].len; 689 690 memcpy(dest, (src + offset), len); 691 msg_size += len; 692 dest += len; 693 } 694 return msg_size; 695 } 696 697 int netvsc_send(struct hv_device *device, 698 struct hv_netvsc_packet *packet) 699 { 700 struct netvsc_device *net_device; 701 int ret = 0; 702 struct nvsp_message sendMessage; 703 struct net_device *ndev; 704 struct vmbus_channel *out_channel = NULL; 705 u64 req_id; 706 unsigned int section_index = NETVSC_INVALID_INDEX; 707 u32 msg_size = 0; 708 struct sk_buff *skb; 709 710 711 net_device = get_outbound_net_device(device); 712 if (!net_device) 713 return -ENODEV; 714 ndev = net_device->ndev; 715 716 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT; 717 if (packet->is_data_pkt) { 718 /* 0 is RMC_DATA; */ 719 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0; 720 } else { 721 /* 1 is RMC_CONTROL; */ 722 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1; 723 } 724 725 /* Attempt to send via sendbuf */ 726 if (packet->total_data_buflen < net_device->send_section_size) { 727 section_index = netvsc_get_next_send_section(net_device); 728 if (section_index != NETVSC_INVALID_INDEX) { 729 msg_size = netvsc_copy_to_send_buf(net_device, 730 section_index, 731 packet); 732 skb = (struct sk_buff *) 733 (unsigned long)packet->send_completion_tid; 734 if (skb) 735 dev_kfree_skb_any(skb); 736 packet->page_buf_cnt = 0; 737 } 738 } 739 packet->send_buf_index = section_index; 740 741 742 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index = 743 section_index; 744 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = msg_size; 745 746 if (packet->send_completion) 747 req_id = (ulong)packet; 748 else 749 req_id = 0; 750 751 out_channel = net_device->chn_table[packet->q_idx]; 752 if (out_channel == NULL) 753 out_channel = device->channel; 754 packet->channel = out_channel; 755 756 if (packet->page_buf_cnt) { 757 ret = vmbus_sendpacket_pagebuffer(out_channel, 758 packet->page_buf, 759 packet->page_buf_cnt, 760 &sendMessage, 761 sizeof(struct nvsp_message), 762 req_id); 763 } else { 764 ret = vmbus_sendpacket(out_channel, &sendMessage, 765 sizeof(struct nvsp_message), 766 req_id, 767 VM_PKT_DATA_INBAND, 768 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); 769 } 770 771 if (ret == 0) { 772 atomic_inc(&net_device->num_outstanding_sends); 773 atomic_inc(&net_device->queue_sends[packet->q_idx]); 774 775 if (hv_ringbuf_avail_percent(&out_channel->outbound) < 776 RING_AVAIL_PERCENT_LOWATER) { 777 netif_tx_stop_queue(netdev_get_tx_queue( 778 ndev, packet->q_idx)); 779 780 if (atomic_read(&net_device-> 781 queue_sends[packet->q_idx]) < 1) 782 netif_tx_wake_queue(netdev_get_tx_queue( 783 ndev, packet->q_idx)); 784 } 785 } else if (ret == -EAGAIN) { 786 netif_tx_stop_queue(netdev_get_tx_queue( 787 ndev, packet->q_idx)); 788 if (atomic_read(&net_device->queue_sends[packet->q_idx]) < 1) { 789 netif_tx_wake_queue(netdev_get_tx_queue( 790 ndev, packet->q_idx)); 791 ret = -ENOSPC; 792 } 793 } else { 794 netdev_err(ndev, "Unable to send packet %p ret %d\n", 795 packet, ret); 796 } 797 798 return ret; 799 } 800 801 static void netvsc_send_recv_completion(struct hv_device *device, 802 struct vmbus_channel *channel, 803 struct netvsc_device *net_device, 804 u64 transaction_id, u32 status) 805 { 806 struct nvsp_message recvcompMessage; 807 int retries = 0; 808 int ret; 809 struct net_device *ndev; 810 811 ndev = net_device->ndev; 812 813 recvcompMessage.hdr.msg_type = 814 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE; 815 816 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status; 817 818 retry_send_cmplt: 819 /* Send the completion */ 820 ret = vmbus_sendpacket(channel, &recvcompMessage, 821 sizeof(struct nvsp_message), transaction_id, 822 VM_PKT_COMP, 0); 823 if (ret == 0) { 824 /* success */ 825 /* no-op */ 826 } else if (ret == -EAGAIN) { 827 /* no more room...wait a bit and attempt to retry 3 times */ 828 retries++; 829 netdev_err(ndev, "unable to send receive completion pkt" 830 " (tid %llx)...retrying %d\n", transaction_id, retries); 831 832 if (retries < 4) { 833 udelay(100); 834 goto retry_send_cmplt; 835 } else { 836 netdev_err(ndev, "unable to send receive " 837 "completion pkt (tid %llx)...give up retrying\n", 838 transaction_id); 839 } 840 } else { 841 netdev_err(ndev, "unable to send receive " 842 "completion pkt - %llx\n", transaction_id); 843 } 844 } 845 846 static void netvsc_receive(struct netvsc_device *net_device, 847 struct vmbus_channel *channel, 848 struct hv_device *device, 849 struct vmpacket_descriptor *packet) 850 { 851 struct vmtransfer_page_packet_header *vmxferpage_packet; 852 struct nvsp_message *nvsp_packet; 853 struct hv_netvsc_packet nv_pkt; 854 struct hv_netvsc_packet *netvsc_packet = &nv_pkt; 855 u32 status = NVSP_STAT_SUCCESS; 856 int i; 857 int count = 0; 858 struct net_device *ndev; 859 860 ndev = net_device->ndev; 861 862 /* 863 * All inbound packets other than send completion should be xfer page 864 * packet 865 */ 866 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) { 867 netdev_err(ndev, "Unknown packet type received - %d\n", 868 packet->type); 869 return; 870 } 871 872 nvsp_packet = (struct nvsp_message *)((unsigned long)packet + 873 (packet->offset8 << 3)); 874 875 /* Make sure this is a valid nvsp packet */ 876 if (nvsp_packet->hdr.msg_type != 877 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) { 878 netdev_err(ndev, "Unknown nvsp packet type received-" 879 " %d\n", nvsp_packet->hdr.msg_type); 880 return; 881 } 882 883 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet; 884 885 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) { 886 netdev_err(ndev, "Invalid xfer page set id - " 887 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID, 888 vmxferpage_packet->xfer_pageset_id); 889 return; 890 } 891 892 count = vmxferpage_packet->range_cnt; 893 netvsc_packet->device = device; 894 netvsc_packet->channel = channel; 895 896 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */ 897 for (i = 0; i < count; i++) { 898 /* Initialize the netvsc packet */ 899 netvsc_packet->status = NVSP_STAT_SUCCESS; 900 netvsc_packet->data = (void *)((unsigned long)net_device-> 901 recv_buf + vmxferpage_packet->ranges[i].byte_offset); 902 netvsc_packet->total_data_buflen = 903 vmxferpage_packet->ranges[i].byte_count; 904 905 /* Pass it to the upper layer */ 906 rndis_filter_receive(device, netvsc_packet); 907 908 if (netvsc_packet->status != NVSP_STAT_SUCCESS) 909 status = NVSP_STAT_FAIL; 910 } 911 912 netvsc_send_recv_completion(device, channel, net_device, 913 vmxferpage_packet->d.trans_id, status); 914 } 915 916 917 static void netvsc_send_table(struct hv_device *hdev, 918 struct vmpacket_descriptor *vmpkt) 919 { 920 struct netvsc_device *nvscdev; 921 struct net_device *ndev; 922 struct nvsp_message *nvmsg; 923 int i; 924 u32 count, *tab; 925 926 nvscdev = get_outbound_net_device(hdev); 927 if (!nvscdev) 928 return; 929 ndev = nvscdev->ndev; 930 931 nvmsg = (struct nvsp_message *)((unsigned long)vmpkt + 932 (vmpkt->offset8 << 3)); 933 934 if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE) 935 return; 936 937 count = nvmsg->msg.v5_msg.send_table.count; 938 if (count != VRSS_SEND_TAB_SIZE) { 939 netdev_err(ndev, "Received wrong send-table size:%u\n", count); 940 return; 941 } 942 943 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table + 944 nvmsg->msg.v5_msg.send_table.offset); 945 946 for (i = 0; i < count; i++) 947 nvscdev->send_table[i] = tab[i]; 948 } 949 950 void netvsc_channel_cb(void *context) 951 { 952 int ret; 953 struct vmbus_channel *channel = (struct vmbus_channel *)context; 954 struct hv_device *device; 955 struct netvsc_device *net_device; 956 u32 bytes_recvd; 957 u64 request_id; 958 struct vmpacket_descriptor *desc; 959 unsigned char *buffer; 960 int bufferlen = NETVSC_PACKET_SIZE; 961 struct net_device *ndev; 962 963 if (channel->primary_channel != NULL) 964 device = channel->primary_channel->device_obj; 965 else 966 device = channel->device_obj; 967 968 net_device = get_inbound_net_device(device); 969 if (!net_device) 970 return; 971 ndev = net_device->ndev; 972 buffer = get_per_channel_state(channel); 973 974 do { 975 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen, 976 &bytes_recvd, &request_id); 977 if (ret == 0) { 978 if (bytes_recvd > 0) { 979 desc = (struct vmpacket_descriptor *)buffer; 980 switch (desc->type) { 981 case VM_PKT_COMP: 982 netvsc_send_completion(net_device, 983 device, desc); 984 break; 985 986 case VM_PKT_DATA_USING_XFER_PAGES: 987 netvsc_receive(net_device, channel, 988 device, desc); 989 break; 990 991 case VM_PKT_DATA_INBAND: 992 netvsc_send_table(device, desc); 993 break; 994 995 default: 996 netdev_err(ndev, 997 "unhandled packet type %d, " 998 "tid %llx len %d\n", 999 desc->type, request_id, 1000 bytes_recvd); 1001 break; 1002 } 1003 1004 } else { 1005 /* 1006 * We are done for this pass. 1007 */ 1008 break; 1009 } 1010 1011 } else if (ret == -ENOBUFS) { 1012 if (bufferlen > NETVSC_PACKET_SIZE) 1013 kfree(buffer); 1014 /* Handle large packet */ 1015 buffer = kmalloc(bytes_recvd, GFP_ATOMIC); 1016 if (buffer == NULL) { 1017 /* Try again next time around */ 1018 netdev_err(ndev, 1019 "unable to allocate buffer of size " 1020 "(%d)!!\n", bytes_recvd); 1021 break; 1022 } 1023 1024 bufferlen = bytes_recvd; 1025 } 1026 } while (1); 1027 1028 if (bufferlen > NETVSC_PACKET_SIZE) 1029 kfree(buffer); 1030 return; 1031 } 1032 1033 /* 1034 * netvsc_device_add - Callback when the device belonging to this 1035 * driver is added 1036 */ 1037 int netvsc_device_add(struct hv_device *device, void *additional_info) 1038 { 1039 int ret = 0; 1040 int ring_size = 1041 ((struct netvsc_device_info *)additional_info)->ring_size; 1042 struct netvsc_device *net_device; 1043 struct net_device *ndev; 1044 1045 net_device = alloc_net_device(device); 1046 if (!net_device) { 1047 ret = -ENOMEM; 1048 goto cleanup; 1049 } 1050 1051 net_device->ring_size = ring_size; 1052 1053 /* 1054 * Coming into this function, struct net_device * is 1055 * registered as the driver private data. 1056 * In alloc_net_device(), we register struct netvsc_device * 1057 * as the driver private data and stash away struct net_device * 1058 * in struct netvsc_device *. 1059 */ 1060 ndev = net_device->ndev; 1061 1062 /* Initialize the NetVSC channel extension */ 1063 init_completion(&net_device->channel_init_wait); 1064 1065 set_per_channel_state(device->channel, net_device->cb_buffer); 1066 1067 /* Open the channel */ 1068 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE, 1069 ring_size * PAGE_SIZE, NULL, 0, 1070 netvsc_channel_cb, device->channel); 1071 1072 if (ret != 0) { 1073 netdev_err(ndev, "unable to open channel: %d\n", ret); 1074 goto cleanup; 1075 } 1076 1077 /* Channel is opened */ 1078 pr_info("hv_netvsc channel opened successfully\n"); 1079 1080 net_device->chn_table[0] = device->channel; 1081 1082 /* Connect with the NetVsp */ 1083 ret = netvsc_connect_vsp(device); 1084 if (ret != 0) { 1085 netdev_err(ndev, 1086 "unable to connect to NetVSP - %d\n", ret); 1087 goto close; 1088 } 1089 1090 return ret; 1091 1092 close: 1093 /* Now, we can close the channel safely */ 1094 vmbus_close(device->channel); 1095 1096 cleanup: 1097 1098 if (net_device) 1099 kfree(net_device); 1100 1101 return ret; 1102 } 1103