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 netvsc_device
*nv_dev
, bool vf
)
42 struct nvsp_message
*init_pkt
= &nv_dev
->channel_init_pkt
;
43 struct net_device
*ndev
= nv_dev
->ndev
;
44 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
45 struct hv_device
*dev
= net_device_ctx
->device_ctx
;
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
->ndev
= ndev
;
84 net_device
->max_pkt
= RNDIS_MAX_PKT_DEFAULT
;
85 net_device
->pkt_align
= RNDIS_PKT_ALIGN_DEFAULT
;
87 net_device
->vf_netdev
= NULL
;
88 net_device
->vf_inject
= false;
90 net_device_ctx
->nvdev
= net_device
;
95 static void free_netvsc_device(struct netvsc_device
*nvdev
)
97 kfree(nvdev
->cb_buffer
);
101 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
103 struct net_device
*ndev
= hv_get_drvdata(device
);
104 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
105 struct netvsc_device
*net_device
= net_device_ctx
->nvdev
;
107 if (net_device
&& net_device
->destroy
)
113 static struct netvsc_device
*get_inbound_net_device(struct hv_device
*device
)
115 struct net_device
*ndev
= hv_get_drvdata(device
);
116 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
117 struct netvsc_device
*net_device
= net_device_ctx
->nvdev
;
122 if (net_device
->destroy
&&
123 atomic_read(&net_device
->num_outstanding_sends
) == 0)
131 static int netvsc_destroy_buf(struct hv_device
*device
)
133 struct nvsp_message
*revoke_packet
;
135 struct net_device
*ndev
= hv_get_drvdata(device
);
136 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
137 struct netvsc_device
*net_device
= net_device_ctx
->nvdev
;
140 * If we got a section count, it means we received a
141 * SendReceiveBufferComplete msg (ie sent
142 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
143 * to send a revoke msg here
145 if (net_device
->recv_section_cnt
) {
146 /* Send the revoke receive buffer */
147 revoke_packet
= &net_device
->revoke_packet
;
148 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
150 revoke_packet
->hdr
.msg_type
=
151 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
152 revoke_packet
->msg
.v1_msg
.
153 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
155 ret
= vmbus_sendpacket(device
->channel
,
157 sizeof(struct nvsp_message
),
158 (unsigned long)revoke_packet
,
159 VM_PKT_DATA_INBAND
, 0);
161 * If we failed here, we might as well return and
162 * have a leak rather than continue and a bugchk
165 netdev_err(ndev
, "unable to send "
166 "revoke receive buffer to netvsp\n");
171 /* Teardown the gpadl on the vsp end */
172 if (net_device
->recv_buf_gpadl_handle
) {
173 ret
= vmbus_teardown_gpadl(device
->channel
,
174 net_device
->recv_buf_gpadl_handle
);
176 /* If we failed here, we might as well return and have a leak
177 * rather than continue and a bugchk
181 "unable to teardown receive buffer's gpadl\n");
184 net_device
->recv_buf_gpadl_handle
= 0;
187 if (net_device
->recv_buf
) {
188 /* Free up the receive buffer */
189 vfree(net_device
->recv_buf
);
190 net_device
->recv_buf
= NULL
;
193 if (net_device
->recv_section
) {
194 net_device
->recv_section_cnt
= 0;
195 kfree(net_device
->recv_section
);
196 net_device
->recv_section
= NULL
;
199 /* Deal with the send buffer we may have setup.
200 * If we got a send section size, it means we received a
201 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
202 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
203 * to send a revoke msg here
205 if (net_device
->send_section_size
) {
206 /* Send the revoke receive buffer */
207 revoke_packet
= &net_device
->revoke_packet
;
208 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
210 revoke_packet
->hdr
.msg_type
=
211 NVSP_MSG1_TYPE_REVOKE_SEND_BUF
;
212 revoke_packet
->msg
.v1_msg
.revoke_send_buf
.id
=
213 NETVSC_SEND_BUFFER_ID
;
215 ret
= vmbus_sendpacket(device
->channel
,
217 sizeof(struct nvsp_message
),
218 (unsigned long)revoke_packet
,
219 VM_PKT_DATA_INBAND
, 0);
220 /* If we failed here, we might as well return and
221 * have a leak rather than continue and a bugchk
224 netdev_err(ndev
, "unable to send "
225 "revoke send buffer to netvsp\n");
229 /* Teardown the gpadl on the vsp end */
230 if (net_device
->send_buf_gpadl_handle
) {
231 ret
= vmbus_teardown_gpadl(device
->channel
,
232 net_device
->send_buf_gpadl_handle
);
234 /* If we failed here, we might as well return and have a leak
235 * rather than continue and a bugchk
239 "unable to teardown send buffer's gpadl\n");
242 net_device
->send_buf_gpadl_handle
= 0;
244 if (net_device
->send_buf
) {
245 /* Free up the send buffer */
246 vfree(net_device
->send_buf
);
247 net_device
->send_buf
= NULL
;
249 kfree(net_device
->send_section_map
);
254 static int netvsc_init_buf(struct hv_device
*device
)
258 struct netvsc_device
*net_device
;
259 struct nvsp_message
*init_packet
;
260 struct net_device
*ndev
;
263 net_device
= get_outbound_net_device(device
);
266 ndev
= net_device
->ndev
;
268 node
= cpu_to_node(device
->channel
->target_cpu
);
269 net_device
->recv_buf
= vzalloc_node(net_device
->recv_buf_size
, node
);
270 if (!net_device
->recv_buf
)
271 net_device
->recv_buf
= vzalloc(net_device
->recv_buf_size
);
273 if (!net_device
->recv_buf
) {
274 netdev_err(ndev
, "unable to allocate receive "
275 "buffer of size %d\n", net_device
->recv_buf_size
);
281 * Establish the gpadl handle for this buffer on this
282 * channel. Note: This call uses the vmbus connection rather
283 * than the channel to establish the gpadl handle.
285 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
286 net_device
->recv_buf_size
,
287 &net_device
->recv_buf_gpadl_handle
);
290 "unable to establish receive buffer's gpadl\n");
295 /* Notify the NetVsp of the gpadl handle */
296 init_packet
= &net_device
->channel_init_pkt
;
298 memset(init_packet
, 0, sizeof(struct nvsp_message
));
300 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
301 init_packet
->msg
.v1_msg
.send_recv_buf
.
302 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
303 init_packet
->msg
.v1_msg
.
304 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
306 /* Send the gpadl notification request */
307 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
308 sizeof(struct nvsp_message
),
309 (unsigned long)init_packet
,
311 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
314 "unable to send receive buffer's gpadl to netvsp\n");
318 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
322 /* Check the response */
323 if (init_packet
->msg
.v1_msg
.
324 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
325 netdev_err(ndev
, "Unable to complete receive buffer "
326 "initialization with NetVsp - status %d\n",
327 init_packet
->msg
.v1_msg
.
328 send_recv_buf_complete
.status
);
333 /* Parse the response */
335 net_device
->recv_section_cnt
= init_packet
->msg
.
336 v1_msg
.send_recv_buf_complete
.num_sections
;
338 net_device
->recv_section
= kmemdup(
339 init_packet
->msg
.v1_msg
.send_recv_buf_complete
.sections
,
340 net_device
->recv_section_cnt
*
341 sizeof(struct nvsp_1_receive_buffer_section
),
343 if (net_device
->recv_section
== NULL
) {
349 * For 1st release, there should only be 1 section that represents the
350 * entire receive buffer
352 if (net_device
->recv_section_cnt
!= 1 ||
353 net_device
->recv_section
->offset
!= 0) {
358 /* Now setup the send buffer.
360 net_device
->send_buf
= vzalloc_node(net_device
->send_buf_size
, node
);
361 if (!net_device
->send_buf
)
362 net_device
->send_buf
= vzalloc(net_device
->send_buf_size
);
363 if (!net_device
->send_buf
) {
364 netdev_err(ndev
, "unable to allocate send "
365 "buffer of size %d\n", net_device
->send_buf_size
);
370 /* Establish the gpadl handle for this buffer on this
371 * channel. Note: This call uses the vmbus connection rather
372 * than the channel to establish the gpadl handle.
374 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->send_buf
,
375 net_device
->send_buf_size
,
376 &net_device
->send_buf_gpadl_handle
);
379 "unable to establish send buffer's gpadl\n");
383 /* Notify the NetVsp of the gpadl handle */
384 init_packet
= &net_device
->channel_init_pkt
;
385 memset(init_packet
, 0, sizeof(struct nvsp_message
));
386 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_SEND_BUF
;
387 init_packet
->msg
.v1_msg
.send_send_buf
.gpadl_handle
=
388 net_device
->send_buf_gpadl_handle
;
389 init_packet
->msg
.v1_msg
.send_send_buf
.id
= NETVSC_SEND_BUFFER_ID
;
391 /* Send the gpadl notification request */
392 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
393 sizeof(struct nvsp_message
),
394 (unsigned long)init_packet
,
396 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
399 "unable to send send buffer's gpadl to netvsp\n");
403 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
406 /* Check the response */
407 if (init_packet
->msg
.v1_msg
.
408 send_send_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
409 netdev_err(ndev
, "Unable to complete send buffer "
410 "initialization with NetVsp - status %d\n",
411 init_packet
->msg
.v1_msg
.
412 send_send_buf_complete
.status
);
417 /* Parse the response */
418 net_device
->send_section_size
= init_packet
->msg
.
419 v1_msg
.send_send_buf_complete
.section_size
;
421 /* Section count is simply the size divided by the section size.
423 net_device
->send_section_cnt
=
424 net_device
->send_buf_size
/net_device
->send_section_size
;
426 dev_info(&device
->device
, "Send section size: %d, Section count:%d\n",
427 net_device
->send_section_size
, net_device
->send_section_cnt
);
429 /* Setup state for managing the send buffer. */
430 net_device
->map_words
= DIV_ROUND_UP(net_device
->send_section_cnt
,
433 net_device
->send_section_map
=
434 kzalloc(net_device
->map_words
* sizeof(ulong
), GFP_KERNEL
);
435 if (net_device
->send_section_map
== NULL
) {
443 netvsc_destroy_buf(device
);
450 /* Negotiate NVSP protocol version */
451 static int negotiate_nvsp_ver(struct hv_device
*device
,
452 struct netvsc_device
*net_device
,
453 struct nvsp_message
*init_packet
,
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
= net_device
->ndev
->mtu
+
491 init_packet
->msg
.v2_msg
.send_ndis_config
.capability
.ieee8021q
= 1;
493 if (nvsp_ver
>= NVSP_PROTOCOL_VERSION_5
)
494 init_packet
->msg
.v2_msg
.send_ndis_config
.capability
.sriov
= 1;
496 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
497 sizeof(struct nvsp_message
),
498 (unsigned long)init_packet
,
499 VM_PKT_DATA_INBAND
, 0);
504 static int netvsc_connect_vsp(struct hv_device
*device
)
507 struct netvsc_device
*net_device
;
508 struct nvsp_message
*init_packet
;
510 struct net_device
*ndev
;
511 u32 ver_list
[] = { NVSP_PROTOCOL_VERSION_1
, NVSP_PROTOCOL_VERSION_2
,
512 NVSP_PROTOCOL_VERSION_4
, NVSP_PROTOCOL_VERSION_5
};
513 int i
, num_ver
= 4; /* number of different NVSP versions */
515 net_device
= get_outbound_net_device(device
);
518 ndev
= net_device
->ndev
;
520 init_packet
= &net_device
->channel_init_pkt
;
522 /* Negotiate the latest NVSP protocol supported */
523 for (i
= num_ver
- 1; i
>= 0; i
--)
524 if (negotiate_nvsp_ver(device
, net_device
, init_packet
,
526 net_device
->nvsp_version
= ver_list
[i
];
535 pr_debug("Negotiated NVSP version:%x\n", net_device
->nvsp_version
);
537 /* Send the ndis version */
538 memset(init_packet
, 0, sizeof(struct nvsp_message
));
540 if (net_device
->nvsp_version
<= NVSP_PROTOCOL_VERSION_4
)
541 ndis_version
= 0x00060001;
543 ndis_version
= 0x0006001e;
545 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
546 init_packet
->msg
.v1_msg
.
547 send_ndis_ver
.ndis_major_ver
=
548 (ndis_version
& 0xFFFF0000) >> 16;
549 init_packet
->msg
.v1_msg
.
550 send_ndis_ver
.ndis_minor_ver
=
551 ndis_version
& 0xFFFF;
553 /* Send the init request */
554 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
555 sizeof(struct nvsp_message
),
556 (unsigned long)init_packet
,
557 VM_PKT_DATA_INBAND
, 0);
561 /* Post the big receive buffer to NetVSP */
562 if (net_device
->nvsp_version
<= NVSP_PROTOCOL_VERSION_2
)
563 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE_LEGACY
;
565 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
566 net_device
->send_buf_size
= NETVSC_SEND_BUFFER_SIZE
;
568 ret
= netvsc_init_buf(device
);
574 static void netvsc_disconnect_vsp(struct hv_device
*device
)
576 netvsc_destroy_buf(device
);
580 * netvsc_device_remove - Callback when the root bus device is removed
582 int netvsc_device_remove(struct hv_device
*device
)
584 struct net_device
*ndev
= hv_get_drvdata(device
);
585 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
586 struct netvsc_device
*net_device
= net_device_ctx
->nvdev
;
588 netvsc_disconnect_vsp(device
);
590 net_device_ctx
->nvdev
= NULL
;
593 * At this point, no one should be accessing net_device
596 dev_notice(&device
->device
, "net device safe to remove\n");
598 /* Now, we can close the channel safely */
599 vmbus_close(device
->channel
);
601 /* Release all resources */
602 vfree(net_device
->sub_cb_buf
);
603 free_netvsc_device(net_device
);
608 #define RING_AVAIL_PERCENT_HIWATER 20
609 #define RING_AVAIL_PERCENT_LOWATER 10
612 * Get the percentage of available bytes to write in the ring.
613 * The return value is in range from 0 to 100.
615 static inline u32
hv_ringbuf_avail_percent(
616 struct hv_ring_buffer_info
*ring_info
)
618 u32 avail_read
, avail_write
;
620 hv_get_ringbuffer_availbytes(ring_info
, &avail_read
, &avail_write
);
622 return avail_write
* 100 / ring_info
->ring_datasize
;
625 static inline void netvsc_free_send_slot(struct netvsc_device
*net_device
,
628 sync_change_bit(index
, net_device
->send_section_map
);
631 static void netvsc_send_completion(struct netvsc_device
*net_device
,
632 struct vmbus_channel
*incoming_channel
,
633 struct hv_device
*device
,
634 struct vmpacket_descriptor
*packet
)
636 struct nvsp_message
*nvsp_packet
;
637 struct hv_netvsc_packet
*nvsc_packet
;
638 struct net_device
*ndev
= hv_get_drvdata(device
);
639 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
643 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
644 (packet
->offset8
<< 3));
646 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
647 (nvsp_packet
->hdr
.msg_type
==
648 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
649 (nvsp_packet
->hdr
.msg_type
==
650 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
) ||
651 (nvsp_packet
->hdr
.msg_type
==
652 NVSP_MSG5_TYPE_SUBCHANNEL
)) {
653 /* Copy the response back */
654 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
655 sizeof(struct nvsp_message
));
656 complete(&net_device
->channel_init_wait
);
657 } else if (nvsp_packet
->hdr
.msg_type
==
658 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
659 int num_outstanding_sends
;
661 struct vmbus_channel
*channel
= device
->channel
;
664 /* Get the send context */
665 skb
= (struct sk_buff
*)(unsigned long)packet
->trans_id
;
667 /* Notify the layer above us */
669 nvsc_packet
= (struct hv_netvsc_packet
*) skb
->cb
;
670 send_index
= nvsc_packet
->send_buf_index
;
671 if (send_index
!= NETVSC_INVALID_INDEX
)
672 netvsc_free_send_slot(net_device
, send_index
);
673 q_idx
= nvsc_packet
->q_idx
;
674 channel
= incoming_channel
;
675 dev_kfree_skb_any(skb
);
678 num_outstanding_sends
=
679 atomic_dec_return(&net_device
->num_outstanding_sends
);
680 queue_sends
= atomic_dec_return(&net_device
->
683 if (net_device
->destroy
&& num_outstanding_sends
== 0)
684 wake_up(&net_device
->wait_drain
);
686 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev
, q_idx
)) &&
687 !net_device_ctx
->start_remove
&&
688 (hv_ringbuf_avail_percent(&channel
->outbound
) >
689 RING_AVAIL_PERCENT_HIWATER
|| queue_sends
< 1))
690 netif_tx_wake_queue(netdev_get_tx_queue(
693 netdev_err(ndev
, "Unknown send completion packet type- "
694 "%d received!!\n", nvsp_packet
->hdr
.msg_type
);
699 static u32
netvsc_get_next_send_section(struct netvsc_device
*net_device
)
702 u32 max_words
= net_device
->map_words
;
703 unsigned long *map_addr
= (unsigned long *)net_device
->send_section_map
;
704 u32 section_cnt
= net_device
->send_section_cnt
;
705 int ret_val
= NETVSC_INVALID_INDEX
;
709 for (i
= 0; i
< max_words
; i
++) {
712 index
= ffz(map_addr
[i
]);
713 prev_val
= sync_test_and_set_bit(index
, &map_addr
[i
]);
716 if ((index
+ (i
* BITS_PER_LONG
)) >= section_cnt
)
718 ret_val
= (index
+ (i
* BITS_PER_LONG
));
724 static u32
netvsc_copy_to_send_buf(struct netvsc_device
*net_device
,
725 unsigned int section_index
,
727 struct hv_netvsc_packet
*packet
,
728 struct rndis_message
*rndis_msg
,
729 struct hv_page_buffer
**pb
,
732 char *start
= net_device
->send_buf
;
733 char *dest
= start
+ (section_index
* net_device
->send_section_size
)
736 bool is_data_pkt
= (skb
!= NULL
) ? true : false;
737 bool xmit_more
= (skb
!= NULL
) ? skb
->xmit_more
: false;
740 u32 remain
= packet
->total_data_buflen
% net_device
->pkt_align
;
741 u32 page_count
= packet
->cp_partial
? packet
->rmsg_pgcnt
:
742 packet
->page_buf_cnt
;
745 if (is_data_pkt
&& xmit_more
&& remain
&&
746 !packet
->cp_partial
) {
747 padding
= net_device
->pkt_align
- remain
;
748 rndis_msg
->msg_len
+= padding
;
749 packet
->total_data_buflen
+= padding
;
752 for (i
= 0; i
< page_count
; i
++) {
753 char *src
= phys_to_virt((*pb
)[i
].pfn
<< PAGE_SHIFT
);
754 u32 offset
= (*pb
)[i
].offset
;
755 u32 len
= (*pb
)[i
].len
;
757 memcpy(dest
, (src
+ offset
), len
);
763 memset(dest
, 0, padding
);
770 static inline int netvsc_send_pkt(
771 struct hv_netvsc_packet
*packet
,
772 struct netvsc_device
*net_device
,
773 struct hv_page_buffer
**pb
,
776 struct nvsp_message nvmsg
;
777 u16 q_idx
= packet
->q_idx
;
778 struct vmbus_channel
*out_channel
= net_device
->chn_table
[q_idx
];
779 struct net_device
*ndev
= net_device
->ndev
;
782 struct hv_page_buffer
*pgbuf
;
783 u32 ring_avail
= hv_ringbuf_avail_percent(&out_channel
->outbound
);
784 bool xmit_more
= (skb
!= NULL
) ? skb
->xmit_more
: false;
786 nvmsg
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
789 nvmsg
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
791 /* 1 is RMC_CONTROL; */
792 nvmsg
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
795 nvmsg
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
796 packet
->send_buf_index
;
797 if (packet
->send_buf_index
== NETVSC_INVALID_INDEX
)
798 nvmsg
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
800 nvmsg
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
=
801 packet
->total_data_buflen
;
805 if (out_channel
->rescind
)
809 * It is possible that once we successfully place this packet
810 * on the ringbuffer, we may stop the queue. In that case, we want
811 * to notify the host independent of the xmit_more flag. We don't
812 * need to be precise here; in the worst case we may signal the host
815 if (ring_avail
< (RING_AVAIL_PERCENT_LOWATER
+ 1))
818 if (packet
->page_buf_cnt
) {
819 pgbuf
= packet
->cp_partial
? (*pb
) +
820 packet
->rmsg_pgcnt
: (*pb
);
821 ret
= vmbus_sendpacket_pagebuffer_ctl(out_channel
,
823 packet
->page_buf_cnt
,
825 sizeof(struct nvsp_message
),
827 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
,
830 ret
= vmbus_sendpacket_ctl(out_channel
, &nvmsg
,
831 sizeof(struct nvsp_message
),
834 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
,
839 atomic_inc(&net_device
->num_outstanding_sends
);
840 atomic_inc(&net_device
->queue_sends
[q_idx
]);
842 if (ring_avail
< RING_AVAIL_PERCENT_LOWATER
) {
843 netif_tx_stop_queue(netdev_get_tx_queue(ndev
, q_idx
));
845 if (atomic_read(&net_device
->
846 queue_sends
[q_idx
]) < 1)
847 netif_tx_wake_queue(netdev_get_tx_queue(
850 } else if (ret
== -EAGAIN
) {
851 netif_tx_stop_queue(netdev_get_tx_queue(
853 if (atomic_read(&net_device
->queue_sends
[q_idx
]) < 1) {
854 netif_tx_wake_queue(netdev_get_tx_queue(
859 netdev_err(ndev
, "Unable to send packet %p ret %d\n",
866 /* Move packet out of multi send data (msd), and clear msd */
867 static inline void move_pkt_msd(struct hv_netvsc_packet
**msd_send
,
868 struct sk_buff
**msd_skb
,
869 struct multi_send_data
*msdp
)
871 *msd_skb
= msdp
->skb
;
872 *msd_send
= msdp
->pkt
;
878 int netvsc_send(struct hv_device
*device
,
879 struct hv_netvsc_packet
*packet
,
880 struct rndis_message
*rndis_msg
,
881 struct hv_page_buffer
**pb
,
884 struct netvsc_device
*net_device
;
885 int ret
= 0, m_ret
= 0;
886 struct vmbus_channel
*out_channel
;
887 u16 q_idx
= packet
->q_idx
;
888 u32 pktlen
= packet
->total_data_buflen
, msd_len
= 0;
889 unsigned int section_index
= NETVSC_INVALID_INDEX
;
890 struct multi_send_data
*msdp
;
891 struct hv_netvsc_packet
*msd_send
= NULL
, *cur_send
= NULL
;
892 struct sk_buff
*msd_skb
= NULL
;
894 bool xmit_more
= (skb
!= NULL
) ? skb
->xmit_more
: false;
896 net_device
= get_outbound_net_device(device
);
900 out_channel
= net_device
->chn_table
[q_idx
];
902 packet
->send_buf_index
= NETVSC_INVALID_INDEX
;
903 packet
->cp_partial
= false;
905 /* Send control message directly without accessing msd (Multi-Send
906 * Data) field which may be changed during data packet processing.
913 msdp
= &net_device
->msd
[q_idx
];
915 /* batch packets in send buffer if possible */
917 msd_len
= msdp
->pkt
->total_data_buflen
;
919 try_batch
= (skb
!= NULL
) && msd_len
> 0 && msdp
->count
<
922 if (try_batch
&& msd_len
+ pktlen
+ net_device
->pkt_align
<
923 net_device
->send_section_size
) {
924 section_index
= msdp
->pkt
->send_buf_index
;
926 } else if (try_batch
&& msd_len
+ packet
->rmsg_size
<
927 net_device
->send_section_size
) {
928 section_index
= msdp
->pkt
->send_buf_index
;
929 packet
->cp_partial
= true;
931 } else if ((skb
!= NULL
) && pktlen
+ net_device
->pkt_align
<
932 net_device
->send_section_size
) {
933 section_index
= netvsc_get_next_send_section(net_device
);
934 if (section_index
!= NETVSC_INVALID_INDEX
) {
935 move_pkt_msd(&msd_send
, &msd_skb
, msdp
);
940 if (section_index
!= NETVSC_INVALID_INDEX
) {
941 netvsc_copy_to_send_buf(net_device
,
942 section_index
, msd_len
,
943 packet
, rndis_msg
, pb
, skb
);
945 packet
->send_buf_index
= section_index
;
947 if (packet
->cp_partial
) {
948 packet
->page_buf_cnt
-= packet
->rmsg_pgcnt
;
949 packet
->total_data_buflen
= msd_len
+ packet
->rmsg_size
;
951 packet
->page_buf_cnt
= 0;
952 packet
->total_data_buflen
+= msd_len
;
956 dev_kfree_skb_any(msdp
->skb
);
958 if (xmit_more
&& !packet
->cp_partial
) {
969 move_pkt_msd(&msd_send
, &msd_skb
, msdp
);
974 m_ret
= netvsc_send_pkt(msd_send
, net_device
, NULL
, msd_skb
);
977 netvsc_free_send_slot(net_device
,
978 msd_send
->send_buf_index
);
979 dev_kfree_skb_any(msd_skb
);
985 ret
= netvsc_send_pkt(cur_send
, net_device
, pb
, skb
);
987 if (ret
!= 0 && section_index
!= NETVSC_INVALID_INDEX
)
988 netvsc_free_send_slot(net_device
, section_index
);
993 static void netvsc_send_recv_completion(struct hv_device
*device
,
994 struct vmbus_channel
*channel
,
995 struct netvsc_device
*net_device
,
996 u64 transaction_id
, u32 status
)
998 struct nvsp_message recvcompMessage
;
1001 struct net_device
*ndev
;
1003 ndev
= net_device
->ndev
;
1005 recvcompMessage
.hdr
.msg_type
=
1006 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
1008 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
= status
;
1011 /* Send the completion */
1012 ret
= vmbus_sendpacket(channel
, &recvcompMessage
,
1013 sizeof(struct nvsp_message
), transaction_id
,
1018 } else if (ret
== -EAGAIN
) {
1019 /* no more room...wait a bit and attempt to retry 3 times */
1021 netdev_err(ndev
, "unable to send receive completion pkt"
1022 " (tid %llx)...retrying %d\n", transaction_id
, retries
);
1026 goto retry_send_cmplt
;
1028 netdev_err(ndev
, "unable to send receive "
1029 "completion pkt (tid %llx)...give up retrying\n",
1033 netdev_err(ndev
, "unable to send receive "
1034 "completion pkt - %llx\n", transaction_id
);
1038 static void netvsc_receive(struct netvsc_device
*net_device
,
1039 struct vmbus_channel
*channel
,
1040 struct hv_device
*device
,
1041 struct vmpacket_descriptor
*packet
)
1043 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
1044 struct nvsp_message
*nvsp_packet
;
1045 struct hv_netvsc_packet nv_pkt
;
1046 struct hv_netvsc_packet
*netvsc_packet
= &nv_pkt
;
1047 u32 status
= NVSP_STAT_SUCCESS
;
1050 struct net_device
*ndev
;
1053 ndev
= net_device
->ndev
;
1056 * All inbound packets other than send completion should be xfer page
1059 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
1060 netdev_err(ndev
, "Unknown packet type received - %d\n",
1065 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
1066 (packet
->offset8
<< 3));
1068 /* Make sure this is a valid nvsp packet */
1069 if (nvsp_packet
->hdr
.msg_type
!=
1070 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
1071 netdev_err(ndev
, "Unknown nvsp packet type received-"
1072 " %d\n", nvsp_packet
->hdr
.msg_type
);
1076 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
1078 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
1079 netdev_err(ndev
, "Invalid xfer page set id - "
1080 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID
,
1081 vmxferpage_packet
->xfer_pageset_id
);
1085 count
= vmxferpage_packet
->range_cnt
;
1087 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1088 for (i
= 0; i
< count
; i
++) {
1089 /* Initialize the netvsc packet */
1090 data
= (void *)((unsigned long)net_device
->
1091 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
);
1092 netvsc_packet
->total_data_buflen
=
1093 vmxferpage_packet
->ranges
[i
].byte_count
;
1095 /* Pass it to the upper layer */
1096 status
= rndis_filter_receive(device
, netvsc_packet
, &data
,
1101 netvsc_send_recv_completion(device
, channel
, net_device
,
1102 vmxferpage_packet
->d
.trans_id
, status
);
1106 static void netvsc_send_table(struct hv_device
*hdev
,
1107 struct nvsp_message
*nvmsg
)
1109 struct netvsc_device
*nvscdev
;
1110 struct net_device
*ndev
;
1114 nvscdev
= get_outbound_net_device(hdev
);
1117 ndev
= nvscdev
->ndev
;
1119 count
= nvmsg
->msg
.v5_msg
.send_table
.count
;
1120 if (count
!= VRSS_SEND_TAB_SIZE
) {
1121 netdev_err(ndev
, "Received wrong send-table size:%u\n", count
);
1125 tab
= (u32
*)((unsigned long)&nvmsg
->msg
.v5_msg
.send_table
+
1126 nvmsg
->msg
.v5_msg
.send_table
.offset
);
1128 for (i
= 0; i
< count
; i
++)
1129 nvscdev
->send_table
[i
] = tab
[i
];
1132 static void netvsc_send_vf(struct netvsc_device
*nvdev
,
1133 struct nvsp_message
*nvmsg
)
1135 nvdev
->vf_alloc
= nvmsg
->msg
.v4_msg
.vf_assoc
.allocated
;
1136 nvdev
->vf_serial
= nvmsg
->msg
.v4_msg
.vf_assoc
.serial
;
1139 static inline void netvsc_receive_inband(struct hv_device
*hdev
,
1140 struct netvsc_device
*nvdev
,
1141 struct nvsp_message
*nvmsg
)
1143 switch (nvmsg
->hdr
.msg_type
) {
1144 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE
:
1145 netvsc_send_table(hdev
, nvmsg
);
1148 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION
:
1149 netvsc_send_vf(nvdev
, nvmsg
);
1154 void netvsc_channel_cb(void *context
)
1157 struct vmbus_channel
*channel
= (struct vmbus_channel
*)context
;
1158 struct hv_device
*device
;
1159 struct netvsc_device
*net_device
;
1162 struct vmpacket_descriptor
*desc
;
1163 unsigned char *buffer
;
1164 int bufferlen
= NETVSC_PACKET_SIZE
;
1165 struct net_device
*ndev
;
1166 struct nvsp_message
*nvmsg
;
1168 if (channel
->primary_channel
!= NULL
)
1169 device
= channel
->primary_channel
->device_obj
;
1171 device
= channel
->device_obj
;
1173 net_device
= get_inbound_net_device(device
);
1176 ndev
= net_device
->ndev
;
1177 buffer
= get_per_channel_state(channel
);
1180 ret
= vmbus_recvpacket_raw(channel
, buffer
, bufferlen
,
1181 &bytes_recvd
, &request_id
);
1183 if (bytes_recvd
> 0) {
1184 desc
= (struct vmpacket_descriptor
*)buffer
;
1185 nvmsg
= (struct nvsp_message
*)((unsigned long)
1186 desc
+ (desc
->offset8
<< 3));
1187 switch (desc
->type
) {
1189 netvsc_send_completion(net_device
,
1194 case VM_PKT_DATA_USING_XFER_PAGES
:
1195 netvsc_receive(net_device
, channel
,
1199 case VM_PKT_DATA_INBAND
:
1200 netvsc_receive_inband(device
,
1207 "unhandled packet type %d, "
1208 "tid %llx len %d\n",
1209 desc
->type
, request_id
,
1216 * We are done for this pass.
1221 } else if (ret
== -ENOBUFS
) {
1222 if (bufferlen
> NETVSC_PACKET_SIZE
)
1224 /* Handle large packet */
1225 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1226 if (buffer
== NULL
) {
1227 /* Try again next time around */
1229 "unable to allocate buffer of size "
1230 "(%d)!!\n", bytes_recvd
);
1234 bufferlen
= bytes_recvd
;
1238 if (bufferlen
> NETVSC_PACKET_SIZE
)
1244 * netvsc_device_add - Callback when the device belonging to this
1247 int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
1251 ((struct netvsc_device_info
*)additional_info
)->ring_size
;
1252 struct netvsc_device
*net_device
;
1253 struct net_device
*ndev
;
1255 net_device
= alloc_net_device(device
);
1259 net_device
->ring_size
= ring_size
;
1261 ndev
= hv_get_drvdata(device
);
1263 /* Initialize the NetVSC channel extension */
1264 init_completion(&net_device
->channel_init_wait
);
1266 set_per_channel_state(device
->channel
, net_device
->cb_buffer
);
1268 /* Open the channel */
1269 ret
= vmbus_open(device
->channel
, ring_size
* PAGE_SIZE
,
1270 ring_size
* PAGE_SIZE
, NULL
, 0,
1271 netvsc_channel_cb
, device
->channel
);
1274 netdev_err(ndev
, "unable to open channel: %d\n", ret
);
1278 /* Channel is opened */
1279 pr_info("hv_netvsc channel opened successfully\n");
1281 net_device
->chn_table
[0] = device
->channel
;
1283 /* Connect with the NetVsp */
1284 ret
= netvsc_connect_vsp(device
);
1287 "unable to connect to NetVSP - %d\n", ret
);
1294 /* Now, we can close the channel safely */
1295 vmbus_close(device
->channel
);
1298 free_netvsc_device(net_device
);