2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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/>.
17 * Haiyang Zhang <haiyangz@microsoft.com>
18 * Hank Janssen <hjanssen@microsoft.com>
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/wait.h>
26 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/vmalloc.h>
32 #include <asm/sync_bitops.h>
34 #include "hyperv_net.h"
37 * Switch the data path from the synthetic interface to the VF
40 void netvsc_switch_datapath(struct net_device *ndev, bool vf)
42 struct net_device_context *net_device_ctx = netdev_priv(ndev);
43 struct hv_device *dev = net_device_ctx->device_ctx;
44 struct netvsc_device *nv_dev = net_device_ctx->nvdev;
45 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
47 memset(init_pkt, 0, sizeof(struct nvsp_message));
48 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
50 init_pkt->msg.v4_msg.active_dp.active_datapath =
53 init_pkt->msg.v4_msg.active_dp.active_datapath =
54 NVSP_DATAPATH_SYNTHETIC;
56 vmbus_sendpacket(dev->channel, init_pkt,
57 sizeof(struct nvsp_message),
58 (unsigned long)init_pkt,
59 VM_PKT_DATA_INBAND, 0);
63 static struct netvsc_device *alloc_net_device(struct hv_device *device)
65 struct netvsc_device *net_device;
66 struct net_device *ndev = hv_get_drvdata(device);
67 struct net_device_context *net_device_ctx = netdev_priv(ndev);
69 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
73 net_device->cb_buffer = kzalloc(NETVSC_PACKET_SIZE, GFP_KERNEL);
74 if (!net_device->cb_buffer) {
79 init_waitqueue_head(&net_device->wait_drain);
80 net_device->destroy = false;
81 atomic_set(&net_device->open_cnt, 0);
82 atomic_set(&net_device->vf_use_cnt, 0);
83 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
84 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
86 net_device->vf_netdev = NULL;
87 net_device->vf_inject = false;
89 net_device_ctx->nvdev = net_device;
94 static void free_netvsc_device(struct netvsc_device *nvdev)
96 kfree(nvdev->cb_buffer);
100 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
102 struct net_device *ndev = hv_get_drvdata(device);
103 struct net_device_context *net_device_ctx = netdev_priv(ndev);
104 struct netvsc_device *net_device = net_device_ctx->nvdev;
106 if (net_device && net_device->destroy)
112 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
114 struct net_device *ndev = hv_get_drvdata(device);
115 struct net_device_context *net_device_ctx = netdev_priv(ndev);
116 struct netvsc_device *net_device = net_device_ctx->nvdev;
121 if (net_device->destroy &&
122 atomic_read(&net_device->num_outstanding_sends) == 0)
130 static int netvsc_destroy_buf(struct hv_device *device)
132 struct nvsp_message *revoke_packet;
134 struct net_device *ndev = hv_get_drvdata(device);
135 struct net_device_context *net_device_ctx = netdev_priv(ndev);
136 struct netvsc_device *net_device = net_device_ctx->nvdev;
139 * If we got a section count, it means we received a
140 * SendReceiveBufferComplete msg (ie sent
141 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
142 * to send a revoke msg here
144 if (net_device->recv_section_cnt) {
145 /* Send the revoke receive buffer */
146 revoke_packet = &net_device->revoke_packet;
147 memset(revoke_packet, 0, sizeof(struct nvsp_message));
149 revoke_packet->hdr.msg_type =
150 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
151 revoke_packet->msg.v1_msg.
152 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
154 ret = vmbus_sendpacket(device->channel,
156 sizeof(struct nvsp_message),
157 (unsigned long)revoke_packet,
158 VM_PKT_DATA_INBAND, 0);
160 * If we failed here, we might as well return and
161 * have a leak rather than continue and a bugchk
164 netdev_err(ndev, "unable to send "
165 "revoke receive buffer to netvsp\n");
170 /* Teardown the gpadl on the vsp end */
171 if (net_device->recv_buf_gpadl_handle) {
172 ret = vmbus_teardown_gpadl(device->channel,
173 net_device->recv_buf_gpadl_handle);
175 /* If we failed here, we might as well return and have a leak
176 * rather than continue and a bugchk
180 "unable to teardown receive buffer's gpadl\n");
183 net_device->recv_buf_gpadl_handle = 0;
186 if (net_device->recv_buf) {
187 /* Free up the receive buffer */
188 vfree(net_device->recv_buf);
189 net_device->recv_buf = NULL;
192 if (net_device->recv_section) {
193 net_device->recv_section_cnt = 0;
194 kfree(net_device->recv_section);
195 net_device->recv_section = NULL;
198 /* Deal with the send buffer we may have setup.
199 * If we got a send section size, it means we received a
200 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
201 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
202 * to send a revoke msg here
204 if (net_device->send_section_size) {
205 /* Send the revoke receive buffer */
206 revoke_packet = &net_device->revoke_packet;
207 memset(revoke_packet, 0, sizeof(struct nvsp_message));
209 revoke_packet->hdr.msg_type =
210 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
211 revoke_packet->msg.v1_msg.revoke_send_buf.id =
212 NETVSC_SEND_BUFFER_ID;
214 ret = vmbus_sendpacket(device->channel,
216 sizeof(struct nvsp_message),
217 (unsigned long)revoke_packet,
218 VM_PKT_DATA_INBAND, 0);
219 /* If we failed here, we might as well return and
220 * have a leak rather than continue and a bugchk
223 netdev_err(ndev, "unable to send "
224 "revoke send buffer to netvsp\n");
228 /* Teardown the gpadl on the vsp end */
229 if (net_device->send_buf_gpadl_handle) {
230 ret = vmbus_teardown_gpadl(device->channel,
231 net_device->send_buf_gpadl_handle);
233 /* If we failed here, we might as well return and have a leak
234 * rather than continue and a bugchk
238 "unable to teardown send buffer's gpadl\n");
241 net_device->send_buf_gpadl_handle = 0;
243 if (net_device->send_buf) {
244 /* Free up the send buffer */
245 vfree(net_device->send_buf);
246 net_device->send_buf = NULL;
248 kfree(net_device->send_section_map);
253 static int netvsc_init_buf(struct hv_device *device)
257 struct netvsc_device *net_device;
258 struct nvsp_message *init_packet;
259 struct net_device *ndev;
262 net_device = get_outbound_net_device(device);
265 ndev = hv_get_drvdata(device);
267 node = cpu_to_node(device->channel->target_cpu);
268 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
269 if (!net_device->recv_buf)
270 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
272 if (!net_device->recv_buf) {
273 netdev_err(ndev, "unable to allocate receive "
274 "buffer of size %d\n", net_device->recv_buf_size);
280 * Establish the gpadl handle for this buffer on this
281 * channel. Note: This call uses the vmbus connection rather
282 * than the channel to establish the gpadl handle.
284 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
285 net_device->recv_buf_size,
286 &net_device->recv_buf_gpadl_handle);
289 "unable to establish receive buffer's gpadl\n");
294 /* Notify the NetVsp of the gpadl handle */
295 init_packet = &net_device->channel_init_pkt;
297 memset(init_packet, 0, sizeof(struct nvsp_message));
299 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
300 init_packet->msg.v1_msg.send_recv_buf.
301 gpadl_handle = net_device->recv_buf_gpadl_handle;
302 init_packet->msg.v1_msg.
303 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
305 /* Send the gpadl notification request */
306 ret = vmbus_sendpacket(device->channel, init_packet,
307 sizeof(struct nvsp_message),
308 (unsigned long)init_packet,
310 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
313 "unable to send receive buffer's gpadl to netvsp\n");
317 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
321 /* Check the response */
322 if (init_packet->msg.v1_msg.
323 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
324 netdev_err(ndev, "Unable to complete receive buffer "
325 "initialization with NetVsp - status %d\n",
326 init_packet->msg.v1_msg.
327 send_recv_buf_complete.status);
332 /* Parse the response */
334 net_device->recv_section_cnt = init_packet->msg.
335 v1_msg.send_recv_buf_complete.num_sections;
337 net_device->recv_section = kmemdup(
338 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
339 net_device->recv_section_cnt *
340 sizeof(struct nvsp_1_receive_buffer_section),
342 if (net_device->recv_section == NULL) {
348 * For 1st release, there should only be 1 section that represents the
349 * entire receive buffer
351 if (net_device->recv_section_cnt != 1 ||
352 net_device->recv_section->offset != 0) {
357 /* Now setup the send buffer.
359 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
360 if (!net_device->send_buf)
361 net_device->send_buf = vzalloc(net_device->send_buf_size);
362 if (!net_device->send_buf) {
363 netdev_err(ndev, "unable to allocate send "
364 "buffer of size %d\n", net_device->send_buf_size);
369 /* Establish the gpadl handle for this buffer on this
370 * channel. Note: This call uses the vmbus connection rather
371 * than the channel to establish the gpadl handle.
373 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
374 net_device->send_buf_size,
375 &net_device->send_buf_gpadl_handle);
378 "unable to establish send buffer's gpadl\n");
382 /* Notify the NetVsp of the gpadl handle */
383 init_packet = &net_device->channel_init_pkt;
384 memset(init_packet, 0, sizeof(struct nvsp_message));
385 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
386 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
387 net_device->send_buf_gpadl_handle;
388 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
390 /* Send the gpadl notification request */
391 ret = vmbus_sendpacket(device->channel, init_packet,
392 sizeof(struct nvsp_message),
393 (unsigned long)init_packet,
395 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
398 "unable to send send buffer's gpadl to netvsp\n");
402 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
405 /* Check the response */
406 if (init_packet->msg.v1_msg.
407 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
408 netdev_err(ndev, "Unable to complete send buffer "
409 "initialization with NetVsp - status %d\n",
410 init_packet->msg.v1_msg.
411 send_send_buf_complete.status);
416 /* Parse the response */
417 net_device->send_section_size = init_packet->msg.
418 v1_msg.send_send_buf_complete.section_size;
420 /* Section count is simply the size divided by the section size.
422 net_device->send_section_cnt =
423 net_device->send_buf_size/net_device->send_section_size;
425 dev_info(&device->device, "Send section size: %d, Section count:%d\n",
426 net_device->send_section_size, net_device->send_section_cnt);
428 /* Setup state for managing the send buffer. */
429 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
432 net_device->send_section_map =
433 kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL);
434 if (net_device->send_section_map == NULL) {
442 netvsc_destroy_buf(device);
449 /* Negotiate NVSP protocol version */
450 static int negotiate_nvsp_ver(struct hv_device *device,
451 struct netvsc_device *net_device,
452 struct nvsp_message *init_packet,
455 struct net_device *ndev = hv_get_drvdata(device);
459 memset(init_packet, 0, sizeof(struct nvsp_message));
460 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
461 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
462 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
464 /* Send the init request */
465 ret = vmbus_sendpacket(device->channel, init_packet,
466 sizeof(struct nvsp_message),
467 (unsigned long)init_packet,
469 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
474 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
479 if (init_packet->msg.init_msg.init_complete.status !=
483 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
486 /* NVSPv2 or later: Send NDIS config */
487 memset(init_packet, 0, sizeof(struct nvsp_message));
488 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
489 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
490 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
492 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5)
493 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
495 ret = vmbus_sendpacket(device->channel, init_packet,
496 sizeof(struct nvsp_message),
497 (unsigned long)init_packet,
498 VM_PKT_DATA_INBAND, 0);
503 static int netvsc_connect_vsp(struct hv_device *device)
506 struct netvsc_device *net_device;
507 struct nvsp_message *init_packet;
509 u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
510 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
511 int i, num_ver = 4; /* number of different NVSP versions */
513 net_device = get_outbound_net_device(device);
517 init_packet = &net_device->channel_init_pkt;
519 /* Negotiate the latest NVSP protocol supported */
520 for (i = num_ver - 1; i >= 0; i--)
521 if (negotiate_nvsp_ver(device, net_device, init_packet,
523 net_device->nvsp_version = ver_list[i];
532 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
534 /* Send the ndis version */
535 memset(init_packet, 0, sizeof(struct nvsp_message));
537 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
538 ndis_version = 0x00060001;
540 ndis_version = 0x0006001e;
542 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
543 init_packet->msg.v1_msg.
544 send_ndis_ver.ndis_major_ver =
545 (ndis_version & 0xFFFF0000) >> 16;
546 init_packet->msg.v1_msg.
547 send_ndis_ver.ndis_minor_ver =
548 ndis_version & 0xFFFF;
550 /* Send the init request */
551 ret = vmbus_sendpacket(device->channel, init_packet,
552 sizeof(struct nvsp_message),
553 (unsigned long)init_packet,
554 VM_PKT_DATA_INBAND, 0);
558 /* Post the big receive buffer to NetVSP */
559 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
560 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
562 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
563 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
565 ret = netvsc_init_buf(device);
571 static void netvsc_disconnect_vsp(struct hv_device *device)
573 netvsc_destroy_buf(device);
577 * netvsc_device_remove - Callback when the root bus device is removed
579 int netvsc_device_remove(struct hv_device *device)
581 struct net_device *ndev = hv_get_drvdata(device);
582 struct net_device_context *net_device_ctx = netdev_priv(ndev);
583 struct netvsc_device *net_device = net_device_ctx->nvdev;
585 netvsc_disconnect_vsp(device);
587 net_device_ctx->nvdev = NULL;
590 * At this point, no one should be accessing net_device
593 dev_notice(&device->device, "net device safe to remove\n");
595 /* Now, we can close the channel safely */
596 vmbus_close(device->channel);
598 /* Release all resources */
599 vfree(net_device->sub_cb_buf);
600 free_netvsc_device(net_device);
605 #define RING_AVAIL_PERCENT_HIWATER 20
606 #define RING_AVAIL_PERCENT_LOWATER 10
609 * Get the percentage of available bytes to write in the ring.
610 * The return value is in range from 0 to 100.
612 static inline u32 hv_ringbuf_avail_percent(
613 struct hv_ring_buffer_info *ring_info)
615 u32 avail_read, avail_write;
617 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
619 return avail_write * 100 / ring_info->ring_datasize;
622 static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
625 sync_change_bit(index, net_device->send_section_map);
628 static void netvsc_send_completion(struct netvsc_device *net_device,
629 struct vmbus_channel *incoming_channel,
630 struct hv_device *device,
631 struct vmpacket_descriptor *packet)
633 struct nvsp_message *nvsp_packet;
634 struct hv_netvsc_packet *nvsc_packet;
635 struct net_device *ndev = hv_get_drvdata(device);
636 struct net_device_context *net_device_ctx = netdev_priv(ndev);
640 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
641 (packet->offset8 << 3));
643 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
644 (nvsp_packet->hdr.msg_type ==
645 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
646 (nvsp_packet->hdr.msg_type ==
647 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
648 (nvsp_packet->hdr.msg_type ==
649 NVSP_MSG5_TYPE_SUBCHANNEL)) {
650 /* Copy the response back */
651 memcpy(&net_device->channel_init_pkt, nvsp_packet,
652 sizeof(struct nvsp_message));
653 complete(&net_device->channel_init_wait);
654 } else if (nvsp_packet->hdr.msg_type ==
655 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
656 int num_outstanding_sends;
658 struct vmbus_channel *channel = device->channel;
661 /* Get the send context */
662 skb = (struct sk_buff *)(unsigned long)packet->trans_id;
664 /* Notify the layer above us */
666 nvsc_packet = (struct hv_netvsc_packet *) skb->cb;
667 send_index = nvsc_packet->send_buf_index;
668 if (send_index != NETVSC_INVALID_INDEX)
669 netvsc_free_send_slot(net_device, send_index);
670 q_idx = nvsc_packet->q_idx;
671 channel = incoming_channel;
672 dev_kfree_skb_any(skb);
675 num_outstanding_sends =
676 atomic_dec_return(&net_device->num_outstanding_sends);
677 queue_sends = atomic_dec_return(&net_device->
680 if (net_device->destroy && num_outstanding_sends == 0)
681 wake_up(&net_device->wait_drain);
683 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
684 !net_device_ctx->start_remove &&
685 (hv_ringbuf_avail_percent(&channel->outbound) >
686 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
687 netif_tx_wake_queue(netdev_get_tx_queue(
690 netdev_err(ndev, "Unknown send completion packet type- "
691 "%d received!!\n", nvsp_packet->hdr.msg_type);
696 static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
699 u32 max_words = net_device->map_words;
700 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
701 u32 section_cnt = net_device->send_section_cnt;
702 int ret_val = NETVSC_INVALID_INDEX;
706 for (i = 0; i < max_words; i++) {
709 index = ffz(map_addr[i]);
710 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
713 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
715 ret_val = (index + (i * BITS_PER_LONG));
721 static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
722 unsigned int section_index,
724 struct hv_netvsc_packet *packet,
725 struct rndis_message *rndis_msg,
726 struct hv_page_buffer **pb,
729 char *start = net_device->send_buf;
730 char *dest = start + (section_index * net_device->send_section_size)
733 bool is_data_pkt = (skb != NULL) ? true : false;
734 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
737 u32 remain = packet->total_data_buflen % net_device->pkt_align;
738 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
739 packet->page_buf_cnt;
742 if (is_data_pkt && xmit_more && remain &&
743 !packet->cp_partial) {
744 padding = net_device->pkt_align - remain;
745 rndis_msg->msg_len += padding;
746 packet->total_data_buflen += padding;
749 for (i = 0; i < page_count; i++) {
750 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
751 u32 offset = (*pb)[i].offset;
752 u32 len = (*pb)[i].len;
754 memcpy(dest, (src + offset), len);
760 memset(dest, 0, padding);
767 static inline int netvsc_send_pkt(
768 struct hv_device *device,
769 struct hv_netvsc_packet *packet,
770 struct netvsc_device *net_device,
771 struct hv_page_buffer **pb,
774 struct nvsp_message nvmsg;
775 u16 q_idx = packet->q_idx;
776 struct vmbus_channel *out_channel = net_device->chn_table[q_idx];
777 struct net_device *ndev = hv_get_drvdata(device);
780 struct hv_page_buffer *pgbuf;
781 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
782 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
784 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
787 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
789 /* 1 is RMC_CONTROL; */
790 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
793 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
794 packet->send_buf_index;
795 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
796 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
798 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
799 packet->total_data_buflen;
803 if (out_channel->rescind)
807 * It is possible that once we successfully place this packet
808 * on the ringbuffer, we may stop the queue. In that case, we want
809 * to notify the host independent of the xmit_more flag. We don't
810 * need to be precise here; in the worst case we may signal the host
813 if (ring_avail < (RING_AVAIL_PERCENT_LOWATER + 1))
816 if (packet->page_buf_cnt) {
817 pgbuf = packet->cp_partial ? (*pb) +
818 packet->rmsg_pgcnt : (*pb);
819 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
821 packet->page_buf_cnt,
823 sizeof(struct nvsp_message),
825 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
828 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
829 sizeof(struct nvsp_message),
832 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
837 atomic_inc(&net_device->num_outstanding_sends);
838 atomic_inc(&net_device->queue_sends[q_idx]);
840 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
841 netif_tx_stop_queue(netdev_get_tx_queue(ndev, q_idx));
843 if (atomic_read(&net_device->
844 queue_sends[q_idx]) < 1)
845 netif_tx_wake_queue(netdev_get_tx_queue(
848 } else if (ret == -EAGAIN) {
849 netif_tx_stop_queue(netdev_get_tx_queue(
851 if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
852 netif_tx_wake_queue(netdev_get_tx_queue(
857 netdev_err(ndev, "Unable to send packet %p ret %d\n",
864 /* Move packet out of multi send data (msd), and clear msd */
865 static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
866 struct sk_buff **msd_skb,
867 struct multi_send_data *msdp)
869 *msd_skb = msdp->skb;
870 *msd_send = msdp->pkt;
876 int netvsc_send(struct hv_device *device,
877 struct hv_netvsc_packet *packet,
878 struct rndis_message *rndis_msg,
879 struct hv_page_buffer **pb,
882 struct netvsc_device *net_device;
883 int ret = 0, m_ret = 0;
884 struct vmbus_channel *out_channel;
885 u16 q_idx = packet->q_idx;
886 u32 pktlen = packet->total_data_buflen, msd_len = 0;
887 unsigned int section_index = NETVSC_INVALID_INDEX;
888 struct multi_send_data *msdp;
889 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
890 struct sk_buff *msd_skb = NULL;
892 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
894 net_device = get_outbound_net_device(device);
898 out_channel = net_device->chn_table[q_idx];
900 packet->send_buf_index = NETVSC_INVALID_INDEX;
901 packet->cp_partial = false;
903 /* Send control message directly without accessing msd (Multi-Send
904 * Data) field which may be changed during data packet processing.
911 msdp = &net_device->msd[q_idx];
913 /* batch packets in send buffer if possible */
915 msd_len = msdp->pkt->total_data_buflen;
917 try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
920 if (try_batch && msd_len + pktlen + net_device->pkt_align <
921 net_device->send_section_size) {
922 section_index = msdp->pkt->send_buf_index;
924 } else if (try_batch && msd_len + packet->rmsg_size <
925 net_device->send_section_size) {
926 section_index = msdp->pkt->send_buf_index;
927 packet->cp_partial = true;
929 } else if ((skb != NULL) && pktlen + net_device->pkt_align <
930 net_device->send_section_size) {
931 section_index = netvsc_get_next_send_section(net_device);
932 if (section_index != NETVSC_INVALID_INDEX) {
933 move_pkt_msd(&msd_send, &msd_skb, msdp);
938 if (section_index != NETVSC_INVALID_INDEX) {
939 netvsc_copy_to_send_buf(net_device,
940 section_index, msd_len,
941 packet, rndis_msg, pb, skb);
943 packet->send_buf_index = section_index;
945 if (packet->cp_partial) {
946 packet->page_buf_cnt -= packet->rmsg_pgcnt;
947 packet->total_data_buflen = msd_len + packet->rmsg_size;
949 packet->page_buf_cnt = 0;
950 packet->total_data_buflen += msd_len;
954 dev_kfree_skb_any(msdp->skb);
956 if (xmit_more && !packet->cp_partial) {
967 move_pkt_msd(&msd_send, &msd_skb, msdp);
972 m_ret = netvsc_send_pkt(device, msd_send, net_device,
976 netvsc_free_send_slot(net_device,
977 msd_send->send_buf_index);
978 dev_kfree_skb_any(msd_skb);
984 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
986 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
987 netvsc_free_send_slot(net_device, section_index);
992 static void netvsc_send_recv_completion(struct hv_device *device,
993 struct vmbus_channel *channel,
994 struct netvsc_device *net_device,
995 u64 transaction_id, u32 status)
997 struct nvsp_message recvcompMessage;
1000 struct net_device *ndev = hv_get_drvdata(device);
1002 recvcompMessage.hdr.msg_type =
1003 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1005 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
1008 /* Send the completion */
1009 ret = vmbus_sendpacket(channel, &recvcompMessage,
1010 sizeof(struct nvsp_message), transaction_id,
1015 } else if (ret == -EAGAIN) {
1016 /* no more room...wait a bit and attempt to retry 3 times */
1018 netdev_err(ndev, "unable to send receive completion pkt"
1019 " (tid %llx)...retrying %d\n", transaction_id, retries);
1023 goto retry_send_cmplt;
1025 netdev_err(ndev, "unable to send receive "
1026 "completion pkt (tid %llx)...give up retrying\n",
1030 netdev_err(ndev, "unable to send receive "
1031 "completion pkt - %llx\n", transaction_id);
1035 static void netvsc_receive(struct netvsc_device *net_device,
1036 struct vmbus_channel *channel,
1037 struct hv_device *device,
1038 struct vmpacket_descriptor *packet)
1040 struct vmtransfer_page_packet_header *vmxferpage_packet;
1041 struct nvsp_message *nvsp_packet;
1042 struct hv_netvsc_packet nv_pkt;
1043 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
1044 u32 status = NVSP_STAT_SUCCESS;
1047 struct net_device *ndev = hv_get_drvdata(device);
1051 * All inbound packets other than send completion should be xfer page
1054 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
1055 netdev_err(ndev, "Unknown packet type received - %d\n",
1060 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
1061 (packet->offset8 << 3));
1063 /* Make sure this is a valid nvsp packet */
1064 if (nvsp_packet->hdr.msg_type !=
1065 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1066 netdev_err(ndev, "Unknown nvsp packet type received-"
1067 " %d\n", nvsp_packet->hdr.msg_type);
1071 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1073 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
1074 netdev_err(ndev, "Invalid xfer page set id - "
1075 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
1076 vmxferpage_packet->xfer_pageset_id);
1080 count = vmxferpage_packet->range_cnt;
1082 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1083 for (i = 0; i < count; i++) {
1084 /* Initialize the netvsc packet */
1085 data = (void *)((unsigned long)net_device->
1086 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
1087 netvsc_packet->total_data_buflen =
1088 vmxferpage_packet->ranges[i].byte_count;
1090 /* Pass it to the upper layer */
1091 status = rndis_filter_receive(device, netvsc_packet, &data,
1096 netvsc_send_recv_completion(device, channel, net_device,
1097 vmxferpage_packet->d.trans_id, status);
1101 static void netvsc_send_table(struct hv_device *hdev,
1102 struct nvsp_message *nvmsg)
1104 struct netvsc_device *nvscdev;
1105 struct net_device *ndev = hv_get_drvdata(hdev);
1109 nvscdev = get_outbound_net_device(hdev);
1113 count = nvmsg->msg.v5_msg.send_table.count;
1114 if (count != VRSS_SEND_TAB_SIZE) {
1115 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1119 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1120 nvmsg->msg.v5_msg.send_table.offset);
1122 for (i = 0; i < count; i++)
1123 nvscdev->send_table[i] = tab[i];
1126 static void netvsc_send_vf(struct netvsc_device *nvdev,
1127 struct nvsp_message *nvmsg)
1129 nvdev->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1130 nvdev->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
1133 static inline void netvsc_receive_inband(struct hv_device *hdev,
1134 struct netvsc_device *nvdev,
1135 struct nvsp_message *nvmsg)
1137 switch (nvmsg->hdr.msg_type) {
1138 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1139 netvsc_send_table(hdev, nvmsg);
1142 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
1143 netvsc_send_vf(nvdev, nvmsg);
1148 void netvsc_channel_cb(void *context)
1151 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1152 struct hv_device *device;
1153 struct netvsc_device *net_device;
1156 struct vmpacket_descriptor *desc;
1157 unsigned char *buffer;
1158 int bufferlen = NETVSC_PACKET_SIZE;
1159 struct net_device *ndev;
1160 struct nvsp_message *nvmsg;
1162 if (channel->primary_channel != NULL)
1163 device = channel->primary_channel->device_obj;
1165 device = channel->device_obj;
1167 net_device = get_inbound_net_device(device);
1170 ndev = hv_get_drvdata(device);
1171 buffer = get_per_channel_state(channel);
1174 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
1175 &bytes_recvd, &request_id);
1177 if (bytes_recvd > 0) {
1178 desc = (struct vmpacket_descriptor *)buffer;
1179 nvmsg = (struct nvsp_message *)((unsigned long)
1180 desc + (desc->offset8 << 3));
1181 switch (desc->type) {
1183 netvsc_send_completion(net_device,
1188 case VM_PKT_DATA_USING_XFER_PAGES:
1189 netvsc_receive(net_device, channel,
1193 case VM_PKT_DATA_INBAND:
1194 netvsc_receive_inband(device,
1201 "unhandled packet type %d, "
1202 "tid %llx len %d\n",
1203 desc->type, request_id,
1210 * We are done for this pass.
1215 } else if (ret == -ENOBUFS) {
1216 if (bufferlen > NETVSC_PACKET_SIZE)
1218 /* Handle large packet */
1219 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1220 if (buffer == NULL) {
1221 /* Try again next time around */
1223 "unable to allocate buffer of size "
1224 "(%d)!!\n", bytes_recvd);
1228 bufferlen = bytes_recvd;
1232 if (bufferlen > NETVSC_PACKET_SIZE)
1238 * netvsc_device_add - Callback when the device belonging to this
1241 int netvsc_device_add(struct hv_device *device, void *additional_info)
1245 ((struct netvsc_device_info *)additional_info)->ring_size;
1246 struct netvsc_device *net_device;
1247 struct net_device *ndev;
1249 net_device = alloc_net_device(device);
1253 net_device->ring_size = ring_size;
1255 ndev = hv_get_drvdata(device);
1257 /* Initialize the NetVSC channel extension */
1258 init_completion(&net_device->channel_init_wait);
1260 set_per_channel_state(device->channel, net_device->cb_buffer);
1262 /* Open the channel */
1263 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1264 ring_size * PAGE_SIZE, NULL, 0,
1265 netvsc_channel_cb, device->channel);
1268 netdev_err(ndev, "unable to open channel: %d\n", ret);
1272 /* Channel is opened */
1273 pr_info("hv_netvsc channel opened successfully\n");
1275 net_device->chn_table[0] = device->channel;
1277 /* Connect with the NetVsp */
1278 ret = netvsc_connect_vsp(device);
1281 "unable to connect to NetVSP - %d\n", ret);
1288 /* Now, we can close the channel safely */
1289 vmbus_close(device->channel);
1292 free_netvsc_device(net_device);