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);
62 static struct netvsc_device *alloc_net_device(void)
64 struct netvsc_device *net_device;
66 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
70 net_device->cb_buffer = kzalloc(NETVSC_PACKET_SIZE, GFP_KERNEL);
71 if (!net_device->cb_buffer) {
76 net_device->mrc[0].buf = vzalloc(NETVSC_RECVSLOT_MAX *
77 sizeof(struct recv_comp_data));
79 init_waitqueue_head(&net_device->wait_drain);
80 net_device->destroy = false;
81 atomic_set(&net_device->open_cnt, 0);
82 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
83 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
84 init_completion(&net_device->channel_init_wait);
89 static void free_netvsc_device(struct netvsc_device *nvdev)
93 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
94 vfree(nvdev->mrc[i].buf);
96 kfree(nvdev->cb_buffer);
100 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
102 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
104 if (net_device && net_device->destroy)
110 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
112 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
117 if (net_device->destroy &&
118 atomic_read(&net_device->num_outstanding_sends) == 0 &&
119 atomic_read(&net_device->num_outstanding_recvs) == 0)
126 static void netvsc_destroy_buf(struct hv_device *device)
128 struct nvsp_message *revoke_packet;
129 struct net_device *ndev = hv_get_drvdata(device);
130 struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
134 * If we got a section count, it means we received a
135 * SendReceiveBufferComplete msg (ie sent
136 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
137 * to send a revoke msg here
139 if (net_device->recv_section_cnt) {
140 /* Send the revoke receive buffer */
141 revoke_packet = &net_device->revoke_packet;
142 memset(revoke_packet, 0, sizeof(struct nvsp_message));
144 revoke_packet->hdr.msg_type =
145 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
146 revoke_packet->msg.v1_msg.
147 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
149 ret = vmbus_sendpacket(device->channel,
151 sizeof(struct nvsp_message),
152 (unsigned long)revoke_packet,
153 VM_PKT_DATA_INBAND, 0);
154 /* If the failure is because the channel is rescinded;
155 * ignore the failure since we cannot send on a rescinded
156 * channel. This would allow us to properly cleanup
157 * even when the channel is rescinded.
159 if (device->channel->rescind)
162 * If we failed here, we might as well return and
163 * have a leak rather than continue and a bugchk
166 netdev_err(ndev, "unable to send "
167 "revoke receive buffer to netvsp\n");
172 /* Teardown the gpadl on the vsp end */
173 if (net_device->recv_buf_gpadl_handle) {
174 ret = vmbus_teardown_gpadl(device->channel,
175 net_device->recv_buf_gpadl_handle);
177 /* If we failed here, we might as well return and have a leak
178 * rather than continue and a bugchk
182 "unable to teardown receive buffer's gpadl\n");
185 net_device->recv_buf_gpadl_handle = 0;
188 if (net_device->recv_buf) {
189 /* Free up the receive buffer */
190 vfree(net_device->recv_buf);
191 net_device->recv_buf = NULL;
194 if (net_device->recv_section) {
195 net_device->recv_section_cnt = 0;
196 kfree(net_device->recv_section);
197 net_device->recv_section = NULL;
200 /* Deal with the send buffer we may have setup.
201 * If we got a send section size, it means we received a
202 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
203 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
204 * to send a revoke msg here
206 if (net_device->send_section_size) {
207 /* Send the revoke receive buffer */
208 revoke_packet = &net_device->revoke_packet;
209 memset(revoke_packet, 0, sizeof(struct nvsp_message));
211 revoke_packet->hdr.msg_type =
212 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
213 revoke_packet->msg.v1_msg.revoke_send_buf.id =
214 NETVSC_SEND_BUFFER_ID;
216 ret = vmbus_sendpacket(device->channel,
218 sizeof(struct nvsp_message),
219 (unsigned long)revoke_packet,
220 VM_PKT_DATA_INBAND, 0);
222 /* If the failure is because the channel is rescinded;
223 * ignore the failure since we cannot send on a rescinded
224 * channel. This would allow us to properly cleanup
225 * even when the channel is rescinded.
227 if (device->channel->rescind)
230 /* If we failed here, we might as well return and
231 * have a leak rather than continue and a bugchk
234 netdev_err(ndev, "unable to send "
235 "revoke send buffer to netvsp\n");
239 /* Teardown the gpadl on the vsp end */
240 if (net_device->send_buf_gpadl_handle) {
241 ret = vmbus_teardown_gpadl(device->channel,
242 net_device->send_buf_gpadl_handle);
244 /* If we failed here, we might as well return and have a leak
245 * rather than continue and a bugchk
249 "unable to teardown send buffer's gpadl\n");
252 net_device->send_buf_gpadl_handle = 0;
254 if (net_device->send_buf) {
255 /* Free up the send buffer */
256 vfree(net_device->send_buf);
257 net_device->send_buf = NULL;
259 kfree(net_device->send_section_map);
262 static int netvsc_init_buf(struct hv_device *device)
265 struct netvsc_device *net_device;
266 struct nvsp_message *init_packet;
267 struct net_device *ndev;
270 net_device = get_outbound_net_device(device);
273 ndev = hv_get_drvdata(device);
275 node = cpu_to_node(device->channel->target_cpu);
276 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
277 if (!net_device->recv_buf)
278 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
280 if (!net_device->recv_buf) {
281 netdev_err(ndev, "unable to allocate receive "
282 "buffer of size %d\n", net_device->recv_buf_size);
288 * Establish the gpadl handle for this buffer on this
289 * channel. Note: This call uses the vmbus connection rather
290 * than the channel to establish the gpadl handle.
292 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
293 net_device->recv_buf_size,
294 &net_device->recv_buf_gpadl_handle);
297 "unable to establish receive buffer's gpadl\n");
301 /* Notify the NetVsp of the gpadl handle */
302 init_packet = &net_device->channel_init_pkt;
304 memset(init_packet, 0, sizeof(struct nvsp_message));
306 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
307 init_packet->msg.v1_msg.send_recv_buf.
308 gpadl_handle = net_device->recv_buf_gpadl_handle;
309 init_packet->msg.v1_msg.
310 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
312 /* Send the gpadl notification request */
313 ret = vmbus_sendpacket(device->channel, init_packet,
314 sizeof(struct nvsp_message),
315 (unsigned long)init_packet,
317 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
320 "unable to send receive buffer's gpadl to netvsp\n");
324 wait_for_completion(&net_device->channel_init_wait);
326 /* Check the response */
327 if (init_packet->msg.v1_msg.
328 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
329 netdev_err(ndev, "Unable to complete receive buffer "
330 "initialization with NetVsp - status %d\n",
331 init_packet->msg.v1_msg.
332 send_recv_buf_complete.status);
337 /* Parse the response */
339 net_device->recv_section_cnt = init_packet->msg.
340 v1_msg.send_recv_buf_complete.num_sections;
342 net_device->recv_section = kmemdup(
343 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
344 net_device->recv_section_cnt *
345 sizeof(struct nvsp_1_receive_buffer_section),
347 if (net_device->recv_section == NULL) {
353 * For 1st release, there should only be 1 section that represents the
354 * entire receive buffer
356 if (net_device->recv_section_cnt != 1 ||
357 net_device->recv_section->offset != 0) {
362 /* Now setup the send buffer.
364 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
365 if (!net_device->send_buf)
366 net_device->send_buf = vzalloc(net_device->send_buf_size);
367 if (!net_device->send_buf) {
368 netdev_err(ndev, "unable to allocate send "
369 "buffer of size %d\n", net_device->send_buf_size);
374 /* Establish the gpadl handle for this buffer on this
375 * channel. Note: This call uses the vmbus connection rather
376 * than the channel to establish the gpadl handle.
378 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
379 net_device->send_buf_size,
380 &net_device->send_buf_gpadl_handle);
383 "unable to establish send buffer's gpadl\n");
387 /* Notify the NetVsp of the gpadl handle */
388 init_packet = &net_device->channel_init_pkt;
389 memset(init_packet, 0, sizeof(struct nvsp_message));
390 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
391 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
392 net_device->send_buf_gpadl_handle;
393 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
395 /* Send the gpadl notification request */
396 ret = vmbus_sendpacket(device->channel, init_packet,
397 sizeof(struct nvsp_message),
398 (unsigned long)init_packet,
400 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
403 "unable to send send buffer's gpadl to netvsp\n");
407 wait_for_completion(&net_device->channel_init_wait);
409 /* Check the response */
410 if (init_packet->msg.v1_msg.
411 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
412 netdev_err(ndev, "Unable to complete send buffer "
413 "initialization with NetVsp - status %d\n",
414 init_packet->msg.v1_msg.
415 send_send_buf_complete.status);
420 /* Parse the response */
421 net_device->send_section_size = init_packet->msg.
422 v1_msg.send_send_buf_complete.section_size;
424 /* Section count is simply the size divided by the section size.
426 net_device->send_section_cnt =
427 net_device->send_buf_size / net_device->send_section_size;
429 dev_info(&device->device, "Send section size: %d, Section count:%d\n",
430 net_device->send_section_size, net_device->send_section_cnt);
432 /* Setup state for managing the send buffer. */
433 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
436 net_device->send_section_map = kcalloc(net_device->map_words,
437 sizeof(ulong), GFP_KERNEL);
438 if (net_device->send_section_map == NULL) {
446 netvsc_destroy_buf(device);
452 /* Negotiate NVSP protocol version */
453 static int negotiate_nvsp_ver(struct hv_device *device,
454 struct netvsc_device *net_device,
455 struct nvsp_message *init_packet,
458 struct net_device *ndev = hv_get_drvdata(device);
461 memset(init_packet, 0, sizeof(struct nvsp_message));
462 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
463 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
464 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
466 /* Send the init request */
467 ret = vmbus_sendpacket(device->channel, init_packet,
468 sizeof(struct nvsp_message),
469 (unsigned long)init_packet,
471 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
476 wait_for_completion(&net_device->channel_init_wait);
478 if (init_packet->msg.init_msg.init_complete.status !=
482 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
485 /* NVSPv2 or later: Send NDIS config */
486 memset(init_packet, 0, sizeof(struct nvsp_message));
487 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
488 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
489 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
491 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
492 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
494 /* Teaming bit is needed to receive link speed updates */
495 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
498 ret = vmbus_sendpacket(device->channel, init_packet,
499 sizeof(struct nvsp_message),
500 (unsigned long)init_packet,
501 VM_PKT_DATA_INBAND, 0);
506 static int netvsc_connect_vsp(struct hv_device *device)
509 struct netvsc_device *net_device;
510 struct nvsp_message *init_packet;
512 const u32 ver_list[] = {
513 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
514 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
517 net_device = get_outbound_net_device(device);
521 init_packet = &net_device->channel_init_pkt;
523 /* Negotiate the latest NVSP protocol supported */
524 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
525 if (negotiate_nvsp_ver(device, net_device, init_packet,
527 net_device->nvsp_version = ver_list[i];
536 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
538 /* Send the ndis version */
539 memset(init_packet, 0, sizeof(struct nvsp_message));
541 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
542 ndis_version = 0x00060001;
544 ndis_version = 0x0006001e;
546 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
547 init_packet->msg.v1_msg.
548 send_ndis_ver.ndis_major_ver =
549 (ndis_version & 0xFFFF0000) >> 16;
550 init_packet->msg.v1_msg.
551 send_ndis_ver.ndis_minor_ver =
552 ndis_version & 0xFFFF;
554 /* Send the init request */
555 ret = vmbus_sendpacket(device->channel, init_packet,
556 sizeof(struct nvsp_message),
557 (unsigned long)init_packet,
558 VM_PKT_DATA_INBAND, 0);
562 /* Post the big receive buffer to NetVSP */
563 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
564 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
566 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
567 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
569 ret = netvsc_init_buf(device);
575 static void netvsc_disconnect_vsp(struct hv_device *device)
577 netvsc_destroy_buf(device);
581 * netvsc_device_remove - Callback when the root bus device is removed
583 void netvsc_device_remove(struct hv_device *device)
585 struct net_device *ndev = hv_get_drvdata(device);
586 struct net_device_context *net_device_ctx = netdev_priv(ndev);
587 struct netvsc_device *net_device = net_device_ctx->nvdev;
589 netvsc_disconnect_vsp(device);
591 net_device_ctx->nvdev = NULL;
594 * At this point, no one should be accessing net_device
597 dev_notice(&device->device, "net device safe to remove\n");
599 /* Now, we can close the channel safely */
600 vmbus_close(device->channel);
602 /* Release all resources */
603 vfree(net_device->sub_cb_buf);
604 free_netvsc_device(net_device);
607 #define RING_AVAIL_PERCENT_HIWATER 20
608 #define RING_AVAIL_PERCENT_LOWATER 10
611 * Get the percentage of available bytes to write in the ring.
612 * The return value is in range from 0 to 100.
614 static inline u32 hv_ringbuf_avail_percent(
615 struct hv_ring_buffer_info *ring_info)
617 u32 avail_read, avail_write;
619 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
621 return avail_write * 100 / ring_info->ring_datasize;
624 static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
627 sync_change_bit(index, net_device->send_section_map);
630 static void netvsc_send_tx_complete(struct netvsc_device *net_device,
631 struct vmbus_channel *incoming_channel,
632 struct hv_device *device,
633 struct vmpacket_descriptor *packet)
635 struct sk_buff *skb = (struct sk_buff *)(unsigned long)packet->trans_id;
636 struct net_device *ndev = hv_get_drvdata(device);
637 struct net_device_context *net_device_ctx = netdev_priv(ndev);
638 struct vmbus_channel *channel = device->channel;
639 int num_outstanding_sends;
643 /* Notify the layer above us */
645 struct hv_netvsc_packet *nvsc_packet
646 = (struct hv_netvsc_packet *)skb->cb;
647 u32 send_index = nvsc_packet->send_buf_index;
649 if (send_index != NETVSC_INVALID_INDEX)
650 netvsc_free_send_slot(net_device, send_index);
651 q_idx = nvsc_packet->q_idx;
652 channel = incoming_channel;
654 dev_consume_skb_any(skb);
657 num_outstanding_sends =
658 atomic_dec_return(&net_device->num_outstanding_sends);
659 queue_sends = atomic_dec_return(&net_device->queue_sends[q_idx]);
661 if (net_device->destroy && num_outstanding_sends == 0)
662 wake_up(&net_device->wait_drain);
664 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
665 !net_device_ctx->start_remove &&
666 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
668 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
671 static void netvsc_send_completion(struct netvsc_device *net_device,
672 struct vmbus_channel *incoming_channel,
673 struct hv_device *device,
674 struct vmpacket_descriptor *packet)
676 struct nvsp_message *nvsp_packet;
677 struct net_device *ndev = hv_get_drvdata(device);
679 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
680 (packet->offset8 << 3));
682 switch (nvsp_packet->hdr.msg_type) {
683 case NVSP_MSG_TYPE_INIT_COMPLETE:
684 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
685 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
686 case NVSP_MSG5_TYPE_SUBCHANNEL:
687 /* Copy the response back */
688 memcpy(&net_device->channel_init_pkt, nvsp_packet,
689 sizeof(struct nvsp_message));
690 complete(&net_device->channel_init_wait);
693 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
694 netvsc_send_tx_complete(net_device, incoming_channel,
700 "Unknown send completion type %d received!!\n",
701 nvsp_packet->hdr.msg_type);
705 static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
708 u32 max_words = net_device->map_words;
709 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
710 u32 section_cnt = net_device->send_section_cnt;
711 int ret_val = NETVSC_INVALID_INDEX;
715 for (i = 0; i < max_words; i++) {
718 index = ffz(map_addr[i]);
719 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
722 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
724 ret_val = (index + (i * BITS_PER_LONG));
730 static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
731 unsigned int section_index,
733 struct hv_netvsc_packet *packet,
734 struct rndis_message *rndis_msg,
735 struct hv_page_buffer **pb,
738 char *start = net_device->send_buf;
739 char *dest = start + (section_index * net_device->send_section_size)
742 bool is_data_pkt = (skb != NULL) ? true : false;
743 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
746 u32 remain = packet->total_data_buflen % net_device->pkt_align;
747 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
748 packet->page_buf_cnt;
751 if (is_data_pkt && xmit_more && remain &&
752 !packet->cp_partial) {
753 padding = net_device->pkt_align - remain;
754 rndis_msg->msg_len += padding;
755 packet->total_data_buflen += padding;
758 for (i = 0; i < page_count; i++) {
759 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
760 u32 offset = (*pb)[i].offset;
761 u32 len = (*pb)[i].len;
763 memcpy(dest, (src + offset), len);
769 memset(dest, 0, padding);
776 static inline int netvsc_send_pkt(
777 struct hv_device *device,
778 struct hv_netvsc_packet *packet,
779 struct netvsc_device *net_device,
780 struct hv_page_buffer **pb,
783 struct nvsp_message nvmsg;
784 u16 q_idx = packet->q_idx;
785 struct vmbus_channel *out_channel = net_device->chn_table[q_idx];
786 struct net_device *ndev = hv_get_drvdata(device);
789 struct hv_page_buffer *pgbuf;
790 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
791 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
793 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
796 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
798 /* 1 is RMC_CONTROL; */
799 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
802 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
803 packet->send_buf_index;
804 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
805 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
807 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
808 packet->total_data_buflen;
812 if (out_channel->rescind)
816 * It is possible that once we successfully place this packet
817 * on the ringbuffer, we may stop the queue. In that case, we want
818 * to notify the host independent of the xmit_more flag. We don't
819 * need to be precise here; in the worst case we may signal the host
822 if (ring_avail < (RING_AVAIL_PERCENT_LOWATER + 1))
825 if (packet->page_buf_cnt) {
826 pgbuf = packet->cp_partial ? (*pb) +
827 packet->rmsg_pgcnt : (*pb);
828 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
830 packet->page_buf_cnt,
832 sizeof(struct nvsp_message),
834 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
837 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
838 sizeof(struct nvsp_message),
841 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
846 atomic_inc(&net_device->num_outstanding_sends);
847 atomic_inc(&net_device->queue_sends[q_idx]);
849 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
850 netif_tx_stop_queue(netdev_get_tx_queue(ndev, q_idx));
852 if (atomic_read(&net_device->
853 queue_sends[q_idx]) < 1)
854 netif_tx_wake_queue(netdev_get_tx_queue(
857 } else if (ret == -EAGAIN) {
858 netif_tx_stop_queue(netdev_get_tx_queue(
860 if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
861 netif_tx_wake_queue(netdev_get_tx_queue(
866 netdev_err(ndev, "Unable to send packet %p ret %d\n",
873 /* Move packet out of multi send data (msd), and clear msd */
874 static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
875 struct sk_buff **msd_skb,
876 struct multi_send_data *msdp)
878 *msd_skb = msdp->skb;
879 *msd_send = msdp->pkt;
885 int netvsc_send(struct hv_device *device,
886 struct hv_netvsc_packet *packet,
887 struct rndis_message *rndis_msg,
888 struct hv_page_buffer **pb,
891 struct netvsc_device *net_device;
893 struct vmbus_channel *out_channel;
894 u16 q_idx = packet->q_idx;
895 u32 pktlen = packet->total_data_buflen, msd_len = 0;
896 unsigned int section_index = NETVSC_INVALID_INDEX;
897 struct multi_send_data *msdp;
898 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
899 struct sk_buff *msd_skb = NULL;
901 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
903 net_device = get_outbound_net_device(device);
907 out_channel = net_device->chn_table[q_idx];
909 packet->send_buf_index = NETVSC_INVALID_INDEX;
910 packet->cp_partial = false;
912 /* Send control message directly without accessing msd (Multi-Send
913 * Data) field which may be changed during data packet processing.
920 msdp = &net_device->msd[q_idx];
922 /* batch packets in send buffer if possible */
924 msd_len = msdp->pkt->total_data_buflen;
926 try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
929 if (try_batch && msd_len + pktlen + net_device->pkt_align <
930 net_device->send_section_size) {
931 section_index = msdp->pkt->send_buf_index;
933 } else if (try_batch && msd_len + packet->rmsg_size <
934 net_device->send_section_size) {
935 section_index = msdp->pkt->send_buf_index;
936 packet->cp_partial = true;
938 } else if ((skb != NULL) && pktlen + net_device->pkt_align <
939 net_device->send_section_size) {
940 section_index = netvsc_get_next_send_section(net_device);
941 if (section_index != NETVSC_INVALID_INDEX) {
942 move_pkt_msd(&msd_send, &msd_skb, msdp);
947 if (section_index != NETVSC_INVALID_INDEX) {
948 netvsc_copy_to_send_buf(net_device,
949 section_index, msd_len,
950 packet, rndis_msg, pb, skb);
952 packet->send_buf_index = section_index;
954 if (packet->cp_partial) {
955 packet->page_buf_cnt -= packet->rmsg_pgcnt;
956 packet->total_data_buflen = msd_len + packet->rmsg_size;
958 packet->page_buf_cnt = 0;
959 packet->total_data_buflen += msd_len;
963 dev_consume_skb_any(msdp->skb);
965 if (xmit_more && !packet->cp_partial) {
976 move_pkt_msd(&msd_send, &msd_skb, msdp);
981 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
985 netvsc_free_send_slot(net_device,
986 msd_send->send_buf_index);
987 dev_kfree_skb_any(msd_skb);
993 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
995 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
996 netvsc_free_send_slot(net_device, section_index);
1001 static int netvsc_send_recv_completion(struct vmbus_channel *channel,
1002 u64 transaction_id, u32 status)
1004 struct nvsp_message recvcompMessage;
1007 recvcompMessage.hdr.msg_type =
1008 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1010 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
1012 /* Send the completion */
1013 ret = vmbus_sendpacket(channel, &recvcompMessage,
1014 sizeof(struct nvsp_message_header) + sizeof(u32),
1015 transaction_id, VM_PKT_COMP, 0);
1020 static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
1021 u32 *filled, u32 *avail)
1023 u32 first = nvdev->mrc[q_idx].first;
1024 u32 next = nvdev->mrc[q_idx].next;
1026 *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
1029 *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
1032 /* Read the first filled slot, no change to index */
1033 static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
1038 if (!nvdev->mrc[q_idx].buf)
1041 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1045 return nvdev->mrc[q_idx].buf + nvdev->mrc[q_idx].first *
1046 sizeof(struct recv_comp_data);
1049 /* Put the first filled slot back to available pool */
1050 static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
1054 nvdev->mrc[q_idx].first = (nvdev->mrc[q_idx].first + 1) %
1055 NETVSC_RECVSLOT_MAX;
1057 num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
1059 if (nvdev->destroy && num_recv == 0)
1060 wake_up(&nvdev->wait_drain);
1063 /* Check and send pending recv completions */
1064 static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
1065 struct vmbus_channel *channel, u16 q_idx)
1067 struct recv_comp_data *rcd;
1071 rcd = read_recv_comp_slot(nvdev, q_idx);
1075 ret = netvsc_send_recv_completion(channel, rcd->tid,
1080 put_recv_comp_slot(nvdev, q_idx);
1084 #define NETVSC_RCD_WATERMARK 80
1086 /* Get next available slot */
1087 static inline struct recv_comp_data *get_recv_comp_slot(
1088 struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
1090 u32 filled, avail, next;
1091 struct recv_comp_data *rcd;
1093 if (!nvdev->recv_section)
1096 if (!nvdev->mrc[q_idx].buf)
1099 if (atomic_read(&nvdev->num_outstanding_recvs) >
1100 nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
1101 netvsc_chk_recv_comp(nvdev, channel, q_idx);
1103 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1107 next = nvdev->mrc[q_idx].next;
1108 rcd = nvdev->mrc[q_idx].buf + next * sizeof(struct recv_comp_data);
1109 nvdev->mrc[q_idx].next = (next + 1) % NETVSC_RECVSLOT_MAX;
1111 atomic_inc(&nvdev->num_outstanding_recvs);
1116 static void netvsc_receive(struct netvsc_device *net_device,
1117 struct vmbus_channel *channel,
1118 struct hv_device *device,
1119 struct vmpacket_descriptor *packet)
1121 struct vmtransfer_page_packet_header *vmxferpage_packet;
1122 struct nvsp_message *nvsp_packet;
1123 struct hv_netvsc_packet nv_pkt;
1124 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
1125 u32 status = NVSP_STAT_SUCCESS;
1128 struct net_device *ndev = hv_get_drvdata(device);
1131 struct recv_comp_data *rcd;
1132 u16 q_idx = channel->offermsg.offer.sub_channel_index;
1135 * All inbound packets other than send completion should be xfer page
1138 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
1139 netdev_err(ndev, "Unknown packet type received - %d\n",
1144 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
1145 (packet->offset8 << 3));
1147 /* Make sure this is a valid nvsp packet */
1148 if (nvsp_packet->hdr.msg_type !=
1149 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1150 netdev_err(ndev, "Unknown nvsp packet type received-"
1151 " %d\n", nvsp_packet->hdr.msg_type);
1155 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1157 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
1158 netdev_err(ndev, "Invalid xfer page set id - "
1159 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
1160 vmxferpage_packet->xfer_pageset_id);
1164 count = vmxferpage_packet->range_cnt;
1166 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1167 for (i = 0; i < count; i++) {
1168 /* Initialize the netvsc packet */
1169 data = (void *)((unsigned long)net_device->
1170 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
1171 netvsc_packet->total_data_buflen =
1172 vmxferpage_packet->ranges[i].byte_count;
1174 /* Pass it to the upper layer */
1175 status = rndis_filter_receive(device, netvsc_packet, &data,
1179 if (!net_device->mrc[q_idx].buf) {
1180 ret = netvsc_send_recv_completion(channel,
1181 vmxferpage_packet->d.trans_id,
1184 netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
1185 q_idx, vmxferpage_packet->d.trans_id, ret);
1189 rcd = get_recv_comp_slot(net_device, channel, q_idx);
1192 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1193 q_idx, vmxferpage_packet->d.trans_id);
1197 rcd->tid = vmxferpage_packet->d.trans_id;
1198 rcd->status = status;
1201 static void netvsc_send_table(struct hv_device *hdev,
1202 struct nvsp_message *nvmsg)
1204 struct netvsc_device *nvscdev;
1205 struct net_device *ndev = hv_get_drvdata(hdev);
1209 nvscdev = get_outbound_net_device(hdev);
1213 count = nvmsg->msg.v5_msg.send_table.count;
1214 if (count != VRSS_SEND_TAB_SIZE) {
1215 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1219 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1220 nvmsg->msg.v5_msg.send_table.offset);
1222 for (i = 0; i < count; i++)
1223 nvscdev->send_table[i] = tab[i];
1226 static void netvsc_send_vf(struct net_device_context *net_device_ctx,
1227 struct nvsp_message *nvmsg)
1229 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1230 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
1233 static inline void netvsc_receive_inband(struct hv_device *hdev,
1234 struct net_device_context *net_device_ctx,
1235 struct nvsp_message *nvmsg)
1237 switch (nvmsg->hdr.msg_type) {
1238 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1239 netvsc_send_table(hdev, nvmsg);
1242 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
1243 netvsc_send_vf(net_device_ctx, nvmsg);
1248 static void netvsc_process_raw_pkt(struct hv_device *device,
1249 struct vmbus_channel *channel,
1250 struct netvsc_device *net_device,
1251 struct net_device *ndev,
1253 struct vmpacket_descriptor *desc)
1255 struct nvsp_message *nvmsg;
1256 struct net_device_context *net_device_ctx = netdev_priv(ndev);
1258 nvmsg = (struct nvsp_message *)((unsigned long)
1259 desc + (desc->offset8 << 3));
1261 switch (desc->type) {
1263 netvsc_send_completion(net_device, channel, device, desc);
1266 case VM_PKT_DATA_USING_XFER_PAGES:
1267 netvsc_receive(net_device, channel, device, desc);
1270 case VM_PKT_DATA_INBAND:
1271 netvsc_receive_inband(device, net_device_ctx, nvmsg);
1275 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
1276 desc->type, request_id);
1281 void netvsc_channel_cb(void *context)
1284 struct vmbus_channel *channel = (struct vmbus_channel *)context;
1285 u16 q_idx = channel->offermsg.offer.sub_channel_index;
1286 struct hv_device *device;
1287 struct netvsc_device *net_device;
1290 struct vmpacket_descriptor *desc;
1291 unsigned char *buffer;
1292 int bufferlen = NETVSC_PACKET_SIZE;
1293 struct net_device *ndev;
1294 bool need_to_commit = false;
1296 if (channel->primary_channel != NULL)
1297 device = channel->primary_channel->device_obj;
1299 device = channel->device_obj;
1301 net_device = get_inbound_net_device(device);
1304 ndev = hv_get_drvdata(device);
1305 buffer = get_per_channel_state(channel);
1307 /* commit_rd_index() -> hv_signal_on_read() needs this. */
1308 init_cached_read_index(channel);
1311 desc = get_next_pkt_raw(channel);
1313 netvsc_process_raw_pkt(device,
1320 put_pkt_raw(channel, desc);
1321 need_to_commit = true;
1324 if (need_to_commit) {
1325 need_to_commit = false;
1326 commit_rd_index(channel);
1329 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
1330 &bytes_recvd, &request_id);
1332 if (bytes_recvd > 0) {
1333 desc = (struct vmpacket_descriptor *)buffer;
1334 netvsc_process_raw_pkt(device,
1342 * We are done for this pass.
1347 } else if (ret == -ENOBUFS) {
1348 if (bufferlen > NETVSC_PACKET_SIZE)
1350 /* Handle large packet */
1351 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1352 if (buffer == NULL) {
1353 /* Try again next time around */
1355 "unable to allocate buffer of size "
1356 "(%d)!!\n", bytes_recvd);
1360 bufferlen = bytes_recvd;
1363 init_cached_read_index(channel);
1367 if (bufferlen > NETVSC_PACKET_SIZE)
1370 netvsc_chk_recv_comp(net_device, channel, q_idx);
1374 * netvsc_device_add - Callback when the device belonging to this
1377 int netvsc_device_add(struct hv_device *device, void *additional_info)
1381 ((struct netvsc_device_info *)additional_info)->ring_size;
1382 struct netvsc_device *net_device;
1383 struct net_device *ndev = hv_get_drvdata(device);
1384 struct net_device_context *net_device_ctx = netdev_priv(ndev);
1386 net_device = alloc_net_device();
1390 net_device->ring_size = ring_size;
1392 set_per_channel_state(device->channel, net_device->cb_buffer);
1394 /* Open the channel */
1395 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1396 ring_size * PAGE_SIZE, NULL, 0,
1397 netvsc_channel_cb, device->channel);
1400 netdev_err(ndev, "unable to open channel: %d\n", ret);
1404 /* Channel is opened */
1405 pr_info("hv_netvsc channel opened successfully\n");
1407 /* If we're reopening the device we may have multiple queues, fill the
1408 * chn_table with the default channel to use it before subchannels are
1411 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
1412 net_device->chn_table[i] = device->channel;
1414 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1419 net_device_ctx->nvdev = net_device;
1421 /* Connect with the NetVsp */
1422 ret = netvsc_connect_vsp(device);
1425 "unable to connect to NetVSP - %d\n", ret);
1432 /* Now, we can close the channel safely */
1433 vmbus_close(device->channel);
1436 free_netvsc_device(net_device);