21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
29 #include <linux/slab.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_ether.h>
39 struct net_device *
ndev = hv_get_drvdata(device);
48 net_device->
dev = device;
51 hv_set_drvdata(device, net_device);
59 net_device = hv_get_drvdata(device);
60 if (net_device && net_device->
destroy)
66 static struct netvsc_device *get_inbound_net_device(
struct hv_device *device)
70 net_device = hv_get_drvdata(device);
84 static int netvsc_destroy_recv_buf(
struct netvsc_device *net_device)
88 struct net_device *
ndev = net_device->
ndev;
101 revoke_packet->
hdr.msg_type =
109 (
unsigned long)revoke_packet,
110 VM_PKT_DATA_INBAND, 0);
116 netdev_err(ndev,
"unable to send "
117 "revoke receive buffer to netvsp\n");
132 "unable to teardown receive buffer's gpadl\n");
154 static int netvsc_init_recv_buf(
struct hv_device *device)
160 struct net_device *
ndev;
162 net_device = get_outbound_net_device(device);
165 ndev = net_device->
ndev;
171 netdev_err(ndev,
"unable to allocate receive "
187 "unable to establish receive buffer's gpadl\n");
206 (
unsigned long)init_packet,
208 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
211 "unable to send receive buffer's gpadl to netvsp\n");
222 netdev_err(ndev,
"Unable to complete receive buffer "
223 "initialization with NetVsp - status %d\n",
225 send_recv_buf_complete.status);
233 v1_msg.send_recv_buf_complete.num_sections;
258 netvsc_destroy_recv_buf(net_device);
266 static int negotiate_nvsp_ver(
struct hv_device *device,
281 (
unsigned long)init_packet,
283 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
308 (
unsigned long)init_packet,
309 VM_PKT_DATA_INBAND, 0);
314 static int netvsc_connect_vsp(
struct hv_device *device)
320 struct net_device *
ndev;
322 net_device = get_outbound_net_device(device);
325 ndev = net_device->
ndev;
330 if (negotiate_nvsp_ver(device, net_device, init_packet,
333 }
else if (negotiate_nvsp_ver(device, net_device, init_packet,
346 ndis_version = 0x00050001;
350 send_ndis_ver.ndis_major_ver =
351 (ndis_version & 0xFFFF0000) >> 16;
353 send_ndis_ver.ndis_minor_ver =
354 ndis_version & 0xFFFF;
359 (
unsigned long)init_packet,
360 VM_PKT_DATA_INBAND, 0);
365 ret = netvsc_init_recv_buf(device);
371 static void netvsc_disconnect_vsp(
struct netvsc_device *net_device)
373 netvsc_destroy_recv_buf(net_device);
385 net_device = hv_get_drvdata(device);
387 netvsc_disconnect_vsp(net_device);
398 hv_set_drvdata(device,
NULL);
399 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
405 dev_notice(&device->device,
"net device safe to remove\n");
414 kfree(netvsc_packet);
422 #define RING_AVAIL_PERCENT_HIWATER 20
423 #define RING_AVAIL_PERCENT_LOWATER 10
429 static inline u32 hv_ringbuf_avail_percent(
432 u32 avail_read, avail_write;
434 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
436 return avail_write * 100 / ring_info->ring_datasize;
439 static void netvsc_send_completion(
struct hv_device *device,
440 struct vmpacket_descriptor *
packet)
445 struct net_device *
ndev;
447 net_device = get_inbound_net_device(device);
450 ndev = net_device->
ndev;
452 nvsp_packet = (
struct nvsp_message *)((
unsigned long)packet +
453 (packet->offset8 << 3));
456 (nvsp_packet->
hdr.msg_type ==
458 (nvsp_packet->
hdr.msg_type ==
464 }
else if (nvsp_packet->
hdr.msg_type ==
466 int num_outstanding_sends;
476 num_outstanding_sends =
479 if (net_device->
destroy && num_outstanding_sends == 0)
482 if (netif_queue_stopped(ndev) && !net_device->
start_remove &&
483 (hv_ringbuf_avail_percent(&device->channel->outbound)
485 num_outstanding_sends < 1))
486 netif_wake_queue(ndev);
488 netdev_err(ndev,
"Unknown send completion packet type- "
489 "%d received!!\n", nvsp_packet->
hdr.msg_type);
500 struct net_device *
ndev;
502 net_device = get_outbound_net_device(device);
505 ndev = net_device->
ndev;
527 (
unsigned long)packet);
531 (
unsigned long)packet,
533 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
539 if (hv_ringbuf_avail_percent(&device->channel->outbound) <
541 netif_stop_queue(ndev);
543 num_outstanding_sends) < 1)
544 netif_wake_queue(ndev);
546 }
else if (ret == -
EAGAIN) {
547 netif_stop_queue(ndev);
549 netif_wake_queue(ndev);
553 netdev_err(ndev,
"Unable to send packet %p ret %d\n",
560 static void netvsc_send_recv_completion(
struct hv_device *device,
566 struct net_device *
ndev;
569 ndev = net_device->
ndev;
571 recvcompMessage.hdr.msg_type =
574 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
status;
584 }
else if (ret == -
EAGAIN) {
587 netdev_err(ndev,
"unable to send receive completion pkt"
588 " (tid %llx)...retrying %d\n", transaction_id, retries);
592 goto retry_send_cmplt;
594 netdev_err(ndev,
"unable to send receive "
595 "completion pkt (tid %llx)...give up retrying\n",
599 netdev_err(ndev,
"unable to send receive "
600 "completion pkt - %llx\n", transaction_id);
605 static void netvsc_receive_completion(
void *
context)
608 struct hv_device *device = packet->
device;
610 u64 transaction_id = 0;
611 bool fsend_receive_comp =
false;
613 struct net_device *
ndev;
621 net_device = get_inbound_net_device(device);
624 ndev = net_device->
ndev;
639 fsend_receive_comp =
true;
652 if (fsend_receive_comp)
653 netvsc_send_recv_completion(device, transaction_id, status);
657 static void netvsc_receive(
struct hv_device *device,
658 struct vmpacket_descriptor *packet)
661 struct vmtransfer_page_packet_header *vmxferpage_packet;
669 struct net_device *
ndev;
673 net_device = get_inbound_net_device(device);
676 ndev = net_device->
ndev;
682 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
683 netdev_err(ndev,
"Unknown packet type received - %d\n",
688 nvsp_packet = (
struct nvsp_message *)((
unsigned long)packet +
689 (packet->offset8 << 3));
692 if (nvsp_packet->
hdr.msg_type !=
694 netdev_err(ndev,
"Unknown nvsp packet type received-"
695 " %d\n", nvsp_packet->
hdr.msg_type);
699 vmxferpage_packet = (
struct vmtransfer_page_packet_header *)packet;
702 netdev_err(ndev,
"Invalid xfer page set id - "
704 vmxferpage_packet->xfer_pageset_id);
717 if (++count == vmxferpage_packet->range_cnt + 1)
728 netdev_err(ndev,
"Got only %d netvsc pkt...needed "
729 "%d pkts. Dropping this xfer page packet completely!\n",
730 count, vmxferpage_packet->range_cnt + 1);
734 for (i = count; i != 0; i--) {
735 list_move_tail(listHead.next,
741 netvsc_send_recv_completion(device,
742 vmxferpage_packet->d.trans_id,
749 xferpage_packet = (
struct xferpage_packet *)listHead.next;
754 xferpage_packet->
count = count - 1;
756 if (xferpage_packet->
count != vmxferpage_packet->range_cnt) {
757 netdev_err(ndev,
"Needed %d netvsc pkts to satisfy "
758 "this xfer page...got %d\n",
759 vmxferpage_packet->range_cnt, xferpage_packet->
count);
763 for (i = 0; i < (count - 1); i++) {
771 netvsc_receive_completion;
777 vmxferpage_packet->d.trans_id;
779 netvsc_packet->
data = (
void *)((
unsigned long)net_device->
780 recv_buf + vmxferpage_packet->ranges[
i].byte_offset);
782 vmxferpage_packet->ranges[
i].byte_count;
787 netvsc_receive_completion(netvsc_packet->
793 static void netvsc_channel_cb(
void *context)
796 struct hv_device *device =
context;
800 unsigned char *packet;
801 struct vmpacket_descriptor *
desc;
804 struct net_device *
ndev;
812 net_device = get_inbound_net_device(device);
815 ndev = net_device->
ndev;
819 &bytes_recvd, &request_id);
821 if (bytes_recvd > 0) {
822 desc = (
struct vmpacket_descriptor *)buffer;
823 switch (desc->type) {
825 netvsc_send_completion(device, desc);
828 case VM_PKT_DATA_USING_XFER_PAGES:
829 netvsc_receive(device, desc);
834 "unhandled packet type %d, "
836 desc->
type, request_id,
860 if (buffer ==
NULL) {
863 "unable to allocate buffer of size "
864 "(%d)!!\n", bytes_recvd);
868 bufferlen = bytes_recvd;
889 struct net_device *
ndev;
891 net_device = alloc_net_device(device);
904 ndev = net_device->
ndev;
924 ring_size * PAGE_SIZE,
NULL, 0,
925 netvsc_channel_cb, device);
928 netdev_err(ndev,
"unable to open channel: %d\n", ret);
933 pr_info(
"hv_netvsc channel opened successfully\n");
936 ret = netvsc_connect_vsp(device);
939 "unable to connect to NetVSP - %d\n", ret);