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 <linux/rtnetlink.h>
33 #include <linux/prefetch.h>
35 #include <asm/sync_bitops.h>
37 #include "hyperv_net.h"
40 * Switch the data path from the synthetic interface to the VF
43 void netvsc_switch_datapath(struct net_device
*ndev
, bool vf
)
45 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
46 struct hv_device
*dev
= net_device_ctx
->device_ctx
;
47 struct netvsc_device
*nv_dev
= rtnl_dereference(net_device_ctx
->nvdev
);
48 struct nvsp_message
*init_pkt
= &nv_dev
->channel_init_pkt
;
50 memset(init_pkt
, 0, sizeof(struct nvsp_message
));
51 init_pkt
->hdr
.msg_type
= NVSP_MSG4_TYPE_SWITCH_DATA_PATH
;
53 init_pkt
->msg
.v4_msg
.active_dp
.active_datapath
=
56 init_pkt
->msg
.v4_msg
.active_dp
.active_datapath
=
57 NVSP_DATAPATH_SYNTHETIC
;
59 vmbus_sendpacket(dev
->channel
, init_pkt
,
60 sizeof(struct nvsp_message
),
61 (unsigned long)init_pkt
,
62 VM_PKT_DATA_INBAND
, 0);
65 /* Worker to setup sub channels on initial setup
66 * Initial hotplug event occurs in softirq context
67 * and can't wait for channels.
69 static void netvsc_subchan_work(struct work_struct
*w
)
71 struct netvsc_device
*nvdev
=
72 container_of(w
, struct netvsc_device
, subchan_work
);
73 struct rndis_device
*rdev
;
76 /* Avoid deadlock with device removal already under RTNL */
77 if (!rtnl_trylock()) {
82 rdev
= nvdev
->extension
;
84 ret
= rndis_set_subchannel(rdev
->ndev
, nvdev
);
86 netif_device_attach(rdev
->ndev
);
88 /* fallback to only primary channel */
89 for (i
= 1; i
< nvdev
->num_chn
; i
++)
90 netif_napi_del(&nvdev
->chan_table
[i
].napi
);
100 static struct netvsc_device
*alloc_net_device(void)
102 struct netvsc_device
*net_device
;
104 net_device
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
108 init_waitqueue_head(&net_device
->wait_drain
);
109 net_device
->destroy
= false;
110 net_device
->tx_disable
= false;
111 atomic_set(&net_device
->open_cnt
, 0);
112 net_device
->max_pkt
= RNDIS_MAX_PKT_DEFAULT
;
113 net_device
->pkt_align
= RNDIS_PKT_ALIGN_DEFAULT
;
115 init_completion(&net_device
->channel_init_wait
);
116 init_waitqueue_head(&net_device
->subchan_open
);
117 INIT_WORK(&net_device
->subchan_work
, netvsc_subchan_work
);
122 static void free_netvsc_device(struct rcu_head
*head
)
124 struct netvsc_device
*nvdev
125 = container_of(head
, struct netvsc_device
, rcu
);
128 kfree(nvdev
->extension
);
129 vfree(nvdev
->recv_buf
);
130 vfree(nvdev
->send_buf
);
131 kfree(nvdev
->send_section_map
);
133 for (i
= 0; i
< VRSS_CHANNEL_MAX
; i
++)
134 vfree(nvdev
->chan_table
[i
].mrc
.slots
);
139 static void free_netvsc_device_rcu(struct netvsc_device
*nvdev
)
141 call_rcu(&nvdev
->rcu
, free_netvsc_device
);
144 static void netvsc_revoke_recv_buf(struct hv_device
*device
,
145 struct netvsc_device
*net_device
)
147 struct net_device
*ndev
= hv_get_drvdata(device
);
148 struct nvsp_message
*revoke_packet
;
152 * If we got a section count, it means we received a
153 * SendReceiveBufferComplete msg (ie sent
154 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
155 * to send a revoke msg here
157 if (net_device
->recv_section_cnt
) {
158 /* Send the revoke receive buffer */
159 revoke_packet
= &net_device
->revoke_packet
;
160 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
162 revoke_packet
->hdr
.msg_type
=
163 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
164 revoke_packet
->msg
.v1_msg
.
165 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
167 ret
= vmbus_sendpacket(device
->channel
,
169 sizeof(struct nvsp_message
),
170 (unsigned long)revoke_packet
,
171 VM_PKT_DATA_INBAND
, 0);
172 /* If the failure is because the channel is rescinded;
173 * ignore the failure since we cannot send on a rescinded
174 * channel. This would allow us to properly cleanup
175 * even when the channel is rescinded.
177 if (device
->channel
->rescind
)
180 * If we failed here, we might as well return and
181 * have a leak rather than continue and a bugchk
184 netdev_err(ndev
, "unable to send "
185 "revoke receive buffer to netvsp\n");
188 net_device
->recv_section_cnt
= 0;
192 static void netvsc_revoke_send_buf(struct hv_device
*device
,
193 struct netvsc_device
*net_device
)
195 struct net_device
*ndev
= hv_get_drvdata(device
);
196 struct nvsp_message
*revoke_packet
;
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_cnt
) {
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);
221 /* If the failure is because the channel is rescinded;
222 * ignore the failure since we cannot send on a rescinded
223 * channel. This would allow us to properly cleanup
224 * even when the channel is rescinded.
226 if (device
->channel
->rescind
)
229 /* If we failed here, we might as well return and
230 * have a leak rather than continue and a bugchk
233 netdev_err(ndev
, "unable to send "
234 "revoke send buffer to netvsp\n");
237 net_device
->send_section_cnt
= 0;
241 static void netvsc_teardown_recv_gpadl(struct hv_device
*device
,
242 struct netvsc_device
*net_device
)
244 struct net_device
*ndev
= hv_get_drvdata(device
);
247 if (net_device
->recv_buf_gpadl_handle
) {
248 ret
= vmbus_teardown_gpadl(device
->channel
,
249 net_device
->recv_buf_gpadl_handle
);
251 /* If we failed here, we might as well return and have a leak
252 * rather than continue and a bugchk
256 "unable to teardown receive buffer's gpadl\n");
259 net_device
->recv_buf_gpadl_handle
= 0;
263 static void netvsc_teardown_send_gpadl(struct hv_device
*device
,
264 struct netvsc_device
*net_device
)
266 struct net_device
*ndev
= hv_get_drvdata(device
);
269 if (net_device
->send_buf_gpadl_handle
) {
270 ret
= vmbus_teardown_gpadl(device
->channel
,
271 net_device
->send_buf_gpadl_handle
);
273 /* If we failed here, we might as well return and have a leak
274 * rather than continue and a bugchk
278 "unable to teardown send buffer's gpadl\n");
281 net_device
->send_buf_gpadl_handle
= 0;
285 int netvsc_alloc_recv_comp_ring(struct netvsc_device
*net_device
, u32 q_idx
)
287 struct netvsc_channel
*nvchan
= &net_device
->chan_table
[q_idx
];
288 int node
= cpu_to_node(nvchan
->channel
->target_cpu
);
291 size
= net_device
->recv_completion_cnt
* sizeof(struct recv_comp_data
);
292 nvchan
->mrc
.slots
= vzalloc_node(size
, node
);
293 if (!nvchan
->mrc
.slots
)
294 nvchan
->mrc
.slots
= vzalloc(size
);
296 return nvchan
->mrc
.slots
? 0 : -ENOMEM
;
299 static int netvsc_init_buf(struct hv_device
*device
,
300 struct netvsc_device
*net_device
,
301 const struct netvsc_device_info
*device_info
)
303 struct nvsp_1_message_send_receive_buffer_complete
*resp
;
304 struct net_device
*ndev
= hv_get_drvdata(device
);
305 struct nvsp_message
*init_packet
;
306 unsigned int buf_size
;
310 /* Get receive buffer area. */
311 buf_size
= device_info
->recv_sections
* device_info
->recv_section_size
;
312 buf_size
= roundup(buf_size
, PAGE_SIZE
);
314 /* Legacy hosts only allow smaller receive buffer */
315 if (net_device
->nvsp_version
<= NVSP_PROTOCOL_VERSION_2
)
316 buf_size
= min_t(unsigned int, buf_size
,
317 NETVSC_RECEIVE_BUFFER_SIZE_LEGACY
);
319 net_device
->recv_buf
= vzalloc(buf_size
);
320 if (!net_device
->recv_buf
) {
322 "unable to allocate receive buffer of size %u\n",
329 * Establish the gpadl handle for this buffer on this
330 * channel. Note: This call uses the vmbus connection rather
331 * than the channel to establish the gpadl handle.
333 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
335 &net_device
->recv_buf_gpadl_handle
);
338 "unable to establish receive buffer's gpadl\n");
342 /* Notify the NetVsp of the gpadl handle */
343 init_packet
= &net_device
->channel_init_pkt
;
344 memset(init_packet
, 0, sizeof(struct nvsp_message
));
345 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
346 init_packet
->msg
.v1_msg
.send_recv_buf
.
347 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
348 init_packet
->msg
.v1_msg
.
349 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
351 /* Send the gpadl notification request */
352 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
353 sizeof(struct nvsp_message
),
354 (unsigned long)init_packet
,
356 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
359 "unable to send receive buffer's gpadl to netvsp\n");
363 wait_for_completion(&net_device
->channel_init_wait
);
365 /* Check the response */
366 resp
= &init_packet
->msg
.v1_msg
.send_recv_buf_complete
;
367 if (resp
->status
!= NVSP_STAT_SUCCESS
) {
369 "Unable to complete receive buffer initialization with NetVsp - status %d\n",
375 /* Parse the response */
376 netdev_dbg(ndev
, "Receive sections: %u sub_allocs: size %u count: %u\n",
377 resp
->num_sections
, resp
->sections
[0].sub_alloc_size
,
378 resp
->sections
[0].num_sub_allocs
);
380 /* There should only be one section for the entire receive buffer */
381 if (resp
->num_sections
!= 1 || resp
->sections
[0].offset
!= 0) {
386 net_device
->recv_section_size
= resp
->sections
[0].sub_alloc_size
;
387 net_device
->recv_section_cnt
= resp
->sections
[0].num_sub_allocs
;
389 /* Setup receive completion ring */
390 net_device
->recv_completion_cnt
391 = round_up(net_device
->recv_section_cnt
+ 1,
392 PAGE_SIZE
/ sizeof(u64
));
393 ret
= netvsc_alloc_recv_comp_ring(net_device
, 0);
397 /* Now setup the send buffer. */
398 buf_size
= device_info
->send_sections
* device_info
->send_section_size
;
399 buf_size
= round_up(buf_size
, PAGE_SIZE
);
401 net_device
->send_buf
= vzalloc(buf_size
);
402 if (!net_device
->send_buf
) {
403 netdev_err(ndev
, "unable to allocate send buffer of size %u\n",
409 /* Establish the gpadl handle for this buffer on this
410 * channel. Note: This call uses the vmbus connection rather
411 * than the channel to establish the gpadl handle.
413 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->send_buf
,
415 &net_device
->send_buf_gpadl_handle
);
418 "unable to establish send buffer's gpadl\n");
422 /* Notify the NetVsp of the gpadl handle */
423 init_packet
= &net_device
->channel_init_pkt
;
424 memset(init_packet
, 0, sizeof(struct nvsp_message
));
425 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_SEND_BUF
;
426 init_packet
->msg
.v1_msg
.send_send_buf
.gpadl_handle
=
427 net_device
->send_buf_gpadl_handle
;
428 init_packet
->msg
.v1_msg
.send_send_buf
.id
= NETVSC_SEND_BUFFER_ID
;
430 /* Send the gpadl notification request */
431 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
432 sizeof(struct nvsp_message
),
433 (unsigned long)init_packet
,
435 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
438 "unable to send send buffer's gpadl to netvsp\n");
442 wait_for_completion(&net_device
->channel_init_wait
);
444 /* Check the response */
445 if (init_packet
->msg
.v1_msg
.
446 send_send_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
447 netdev_err(ndev
, "Unable to complete send buffer "
448 "initialization with NetVsp - status %d\n",
449 init_packet
->msg
.v1_msg
.
450 send_send_buf_complete
.status
);
455 /* Parse the response */
456 net_device
->send_section_size
= init_packet
->msg
.
457 v1_msg
.send_send_buf_complete
.section_size
;
459 /* Section count is simply the size divided by the section size. */
460 net_device
->send_section_cnt
= buf_size
/ net_device
->send_section_size
;
462 netdev_dbg(ndev
, "Send section size: %d, Section count:%d\n",
463 net_device
->send_section_size
, net_device
->send_section_cnt
);
465 /* Setup state for managing the send buffer. */
466 map_words
= DIV_ROUND_UP(net_device
->send_section_cnt
, BITS_PER_LONG
);
468 net_device
->send_section_map
= kcalloc(map_words
, sizeof(ulong
), GFP_KERNEL
);
469 if (net_device
->send_section_map
== NULL
) {
477 netvsc_revoke_recv_buf(device
, net_device
);
478 netvsc_revoke_send_buf(device
, net_device
);
479 netvsc_teardown_recv_gpadl(device
, net_device
);
480 netvsc_teardown_send_gpadl(device
, net_device
);
486 /* Negotiate NVSP protocol version */
487 static int negotiate_nvsp_ver(struct hv_device
*device
,
488 struct netvsc_device
*net_device
,
489 struct nvsp_message
*init_packet
,
492 struct net_device
*ndev
= hv_get_drvdata(device
);
495 memset(init_packet
, 0, sizeof(struct nvsp_message
));
496 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
497 init_packet
->msg
.init_msg
.init
.min_protocol_ver
= nvsp_ver
;
498 init_packet
->msg
.init_msg
.init
.max_protocol_ver
= nvsp_ver
;
500 /* Send the init request */
501 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
502 sizeof(struct nvsp_message
),
503 (unsigned long)init_packet
,
505 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
510 wait_for_completion(&net_device
->channel_init_wait
);
512 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
516 if (nvsp_ver
== NVSP_PROTOCOL_VERSION_1
)
519 /* NVSPv2 or later: Send NDIS config */
520 memset(init_packet
, 0, sizeof(struct nvsp_message
));
521 init_packet
->hdr
.msg_type
= NVSP_MSG2_TYPE_SEND_NDIS_CONFIG
;
522 init_packet
->msg
.v2_msg
.send_ndis_config
.mtu
= ndev
->mtu
+ ETH_HLEN
;
523 init_packet
->msg
.v2_msg
.send_ndis_config
.capability
.ieee8021q
= 1;
525 if (nvsp_ver
>= NVSP_PROTOCOL_VERSION_5
) {
526 init_packet
->msg
.v2_msg
.send_ndis_config
.capability
.sriov
= 1;
528 /* Teaming bit is needed to receive link speed updates */
529 init_packet
->msg
.v2_msg
.send_ndis_config
.capability
.teaming
= 1;
532 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
533 sizeof(struct nvsp_message
),
534 (unsigned long)init_packet
,
535 VM_PKT_DATA_INBAND
, 0);
540 static int netvsc_connect_vsp(struct hv_device
*device
,
541 struct netvsc_device
*net_device
,
542 const struct netvsc_device_info
*device_info
)
544 const u32 ver_list
[] = {
545 NVSP_PROTOCOL_VERSION_1
, NVSP_PROTOCOL_VERSION_2
,
546 NVSP_PROTOCOL_VERSION_4
, NVSP_PROTOCOL_VERSION_5
548 struct nvsp_message
*init_packet
;
549 int ndis_version
, i
, ret
;
551 init_packet
= &net_device
->channel_init_pkt
;
553 /* Negotiate the latest NVSP protocol supported */
554 for (i
= ARRAY_SIZE(ver_list
) - 1; i
>= 0; i
--)
555 if (negotiate_nvsp_ver(device
, net_device
, init_packet
,
557 net_device
->nvsp_version
= ver_list
[i
];
566 pr_debug("Negotiated NVSP version:%x\n", net_device
->nvsp_version
);
568 /* Send the ndis version */
569 memset(init_packet
, 0, sizeof(struct nvsp_message
));
571 if (net_device
->nvsp_version
<= NVSP_PROTOCOL_VERSION_4
)
572 ndis_version
= 0x00060001;
574 ndis_version
= 0x0006001e;
576 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
577 init_packet
->msg
.v1_msg
.
578 send_ndis_ver
.ndis_major_ver
=
579 (ndis_version
& 0xFFFF0000) >> 16;
580 init_packet
->msg
.v1_msg
.
581 send_ndis_ver
.ndis_minor_ver
=
582 ndis_version
& 0xFFFF;
584 /* Send the init request */
585 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
586 sizeof(struct nvsp_message
),
587 (unsigned long)init_packet
,
588 VM_PKT_DATA_INBAND
, 0);
593 ret
= netvsc_init_buf(device
, net_device
, device_info
);
600 * netvsc_device_remove - Callback when the root bus device is removed
602 void netvsc_device_remove(struct hv_device
*device
)
604 struct net_device
*ndev
= hv_get_drvdata(device
);
605 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
606 struct netvsc_device
*net_device
607 = rtnl_dereference(net_device_ctx
->nvdev
);
611 * Revoke receive buffer. If host is pre-Win2016 then tear down
612 * receive buffer GPADL. Do the same for send buffer.
614 netvsc_revoke_recv_buf(device
, net_device
);
615 if (vmbus_proto_version
< VERSION_WIN10
)
616 netvsc_teardown_recv_gpadl(device
, net_device
);
618 netvsc_revoke_send_buf(device
, net_device
);
619 if (vmbus_proto_version
< VERSION_WIN10
)
620 netvsc_teardown_send_gpadl(device
, net_device
);
622 RCU_INIT_POINTER(net_device_ctx
->nvdev
, NULL
);
624 /* And disassociate NAPI context from device */
625 for (i
= 0; i
< net_device
->num_chn
; i
++)
626 netif_napi_del(&net_device
->chan_table
[i
].napi
);
629 * At this point, no one should be accessing net_device
632 netdev_dbg(ndev
, "net device safe to remove\n");
634 /* Now, we can close the channel safely */
635 vmbus_close(device
->channel
);
638 * If host is Win2016 or higher then we do the GPADL tear down
639 * here after VMBus is closed.
641 if (vmbus_proto_version
>= VERSION_WIN10
) {
642 netvsc_teardown_recv_gpadl(device
, net_device
);
643 netvsc_teardown_send_gpadl(device
, net_device
);
646 /* Release all resources */
647 free_netvsc_device_rcu(net_device
);
650 #define RING_AVAIL_PERCENT_HIWATER 20
651 #define RING_AVAIL_PERCENT_LOWATER 10
654 * Get the percentage of available bytes to write in the ring.
655 * The return value is in range from 0 to 100.
657 static inline u32
hv_ringbuf_avail_percent(
658 struct hv_ring_buffer_info
*ring_info
)
660 u32 avail_read
, avail_write
;
662 hv_get_ringbuffer_availbytes(ring_info
, &avail_read
, &avail_write
);
664 return avail_write
* 100 / ring_info
->ring_datasize
;
667 static inline void netvsc_free_send_slot(struct netvsc_device
*net_device
,
670 sync_change_bit(index
, net_device
->send_section_map
);
673 static void netvsc_send_tx_complete(struct netvsc_device
*net_device
,
674 struct vmbus_channel
*incoming_channel
,
675 struct hv_device
*device
,
676 const struct vmpacket_descriptor
*desc
,
679 struct sk_buff
*skb
= (struct sk_buff
*)(unsigned long)desc
->trans_id
;
680 struct net_device
*ndev
= hv_get_drvdata(device
);
681 struct vmbus_channel
*channel
= device
->channel
;
685 /* Notify the layer above us */
687 const struct hv_netvsc_packet
*packet
688 = (struct hv_netvsc_packet
*)skb
->cb
;
689 u32 send_index
= packet
->send_buf_index
;
690 struct netvsc_stats
*tx_stats
;
692 if (send_index
!= NETVSC_INVALID_INDEX
)
693 netvsc_free_send_slot(net_device
, send_index
);
694 q_idx
= packet
->q_idx
;
695 channel
= incoming_channel
;
697 tx_stats
= &net_device
->chan_table
[q_idx
].tx_stats
;
699 u64_stats_update_begin(&tx_stats
->syncp
);
700 tx_stats
->packets
+= packet
->total_packets
;
701 tx_stats
->bytes
+= packet
->total_bytes
;
702 u64_stats_update_end(&tx_stats
->syncp
);
704 napi_consume_skb(skb
, budget
);
708 atomic_dec_return(&net_device
->chan_table
[q_idx
].queue_sends
);
710 if (unlikely(net_device
->destroy
)) {
711 if (queue_sends
== 0)
712 wake_up(&net_device
->wait_drain
);
714 struct netdev_queue
*txq
= netdev_get_tx_queue(ndev
, q_idx
);
716 if (netif_tx_queue_stopped(txq
) && !net_device
->tx_disable
&&
717 (hv_ringbuf_avail_percent(&channel
->outbound
) > RING_AVAIL_PERCENT_HIWATER
||
719 netif_tx_wake_queue(txq
);
724 static void netvsc_send_completion(struct netvsc_device
*net_device
,
725 struct vmbus_channel
*incoming_channel
,
726 struct hv_device
*device
,
727 const struct vmpacket_descriptor
*desc
,
730 struct nvsp_message
*nvsp_packet
= hv_pkt_data(desc
);
731 struct net_device
*ndev
= hv_get_drvdata(device
);
733 switch (nvsp_packet
->hdr
.msg_type
) {
734 case NVSP_MSG_TYPE_INIT_COMPLETE
:
735 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
:
736 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
:
737 case NVSP_MSG5_TYPE_SUBCHANNEL
:
738 /* Copy the response back */
739 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
740 sizeof(struct nvsp_message
));
741 complete(&net_device
->channel_init_wait
);
744 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
:
745 netvsc_send_tx_complete(net_device
, incoming_channel
,
746 device
, desc
, budget
);
751 "Unknown send completion type %d received!!\n",
752 nvsp_packet
->hdr
.msg_type
);
756 static u32
netvsc_get_next_send_section(struct netvsc_device
*net_device
)
758 unsigned long *map_addr
= net_device
->send_section_map
;
761 for_each_clear_bit(i
, map_addr
, net_device
->send_section_cnt
) {
762 if (sync_test_and_set_bit(i
, map_addr
) == 0)
766 return NETVSC_INVALID_INDEX
;
769 static void netvsc_copy_to_send_buf(struct netvsc_device
*net_device
,
770 unsigned int section_index
,
772 struct hv_netvsc_packet
*packet
,
773 struct rndis_message
*rndis_msg
,
774 struct hv_page_buffer
*pb
,
777 char *start
= net_device
->send_buf
;
778 char *dest
= start
+ (section_index
* net_device
->send_section_size
)
783 u32 remain
= packet
->total_data_buflen
% net_device
->pkt_align
;
784 u32 page_count
= packet
->cp_partial
? packet
->rmsg_pgcnt
:
785 packet
->page_buf_cnt
;
788 remain
= packet
->total_data_buflen
& (net_device
->pkt_align
- 1);
789 if (xmit_more
&& remain
) {
790 padding
= net_device
->pkt_align
- remain
;
791 rndis_msg
->msg_len
+= padding
;
792 packet
->total_data_buflen
+= padding
;
795 for (i
= 0; i
< page_count
; i
++) {
796 char *src
= phys_to_virt(pb
[i
].pfn
<< PAGE_SHIFT
);
797 u32 offset
= pb
[i
].offset
;
800 memcpy(dest
, (src
+ offset
), len
);
806 memset(dest
, 0, padding
);
811 static inline int netvsc_send_pkt(
812 struct hv_device
*device
,
813 struct hv_netvsc_packet
*packet
,
814 struct netvsc_device
*net_device
,
815 struct hv_page_buffer
*pb
,
818 struct nvsp_message nvmsg
;
819 struct nvsp_1_message_send_rndis_packet
* const rpkt
=
820 &nvmsg
.msg
.v1_msg
.send_rndis_pkt
;
821 struct netvsc_channel
* const nvchan
=
822 &net_device
->chan_table
[packet
->q_idx
];
823 struct vmbus_channel
*out_channel
= nvchan
->channel
;
824 struct net_device
*ndev
= hv_get_drvdata(device
);
825 struct netdev_queue
*txq
= netdev_get_tx_queue(ndev
, packet
->q_idx
);
828 u32 ring_avail
= hv_ringbuf_avail_percent(&out_channel
->outbound
);
830 nvmsg
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
832 rpkt
->channel_type
= 0; /* 0 is RMC_DATA */
834 rpkt
->channel_type
= 1; /* 1 is RMC_CONTROL */
836 rpkt
->send_buf_section_index
= packet
->send_buf_index
;
837 if (packet
->send_buf_index
== NETVSC_INVALID_INDEX
)
838 rpkt
->send_buf_section_size
= 0;
840 rpkt
->send_buf_section_size
= packet
->total_data_buflen
;
844 if (out_channel
->rescind
)
847 if (packet
->page_buf_cnt
) {
848 if (packet
->cp_partial
)
849 pb
+= packet
->rmsg_pgcnt
;
851 ret
= vmbus_sendpacket_pagebuffer(out_channel
,
852 pb
, packet
->page_buf_cnt
,
853 &nvmsg
, sizeof(nvmsg
),
856 ret
= vmbus_sendpacket(out_channel
,
857 &nvmsg
, sizeof(nvmsg
),
858 req_id
, VM_PKT_DATA_INBAND
,
859 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
863 atomic_inc_return(&nvchan
->queue_sends
);
865 if (ring_avail
< RING_AVAIL_PERCENT_LOWATER
)
866 netif_tx_stop_queue(txq
);
867 } else if (ret
== -EAGAIN
) {
868 netif_tx_stop_queue(txq
);
869 if (atomic_read(&nvchan
->queue_sends
) < 1 &&
870 !net_device
->tx_disable
) {
871 netif_tx_wake_queue(txq
);
876 "Unable to send packet pages %u len %u, ret %d\n",
877 packet
->page_buf_cnt
, packet
->total_data_buflen
,
884 /* Move packet out of multi send data (msd), and clear msd */
885 static inline void move_pkt_msd(struct hv_netvsc_packet
**msd_send
,
886 struct sk_buff
**msd_skb
,
887 struct multi_send_data
*msdp
)
889 *msd_skb
= msdp
->skb
;
890 *msd_send
= msdp
->pkt
;
896 /* RCU already held by caller */
897 int netvsc_send(struct net_device
*ndev
,
898 struct hv_netvsc_packet
*packet
,
899 struct rndis_message
*rndis_msg
,
900 struct hv_page_buffer
*pb
,
903 struct net_device_context
*ndev_ctx
= netdev_priv(ndev
);
904 struct netvsc_device
*net_device
905 = rcu_dereference_bh(ndev_ctx
->nvdev
);
906 struct hv_device
*device
= ndev_ctx
->device_ctx
;
908 struct netvsc_channel
*nvchan
;
909 u32 pktlen
= packet
->total_data_buflen
, msd_len
= 0;
910 unsigned int section_index
= NETVSC_INVALID_INDEX
;
911 struct multi_send_data
*msdp
;
912 struct hv_netvsc_packet
*msd_send
= NULL
, *cur_send
= NULL
;
913 struct sk_buff
*msd_skb
= NULL
;
914 bool try_batch
, xmit_more
;
916 /* If device is rescinded, return error and packet will get dropped. */
917 if (unlikely(!net_device
|| net_device
->destroy
))
920 nvchan
= &net_device
->chan_table
[packet
->q_idx
];
921 packet
->send_buf_index
= NETVSC_INVALID_INDEX
;
922 packet
->cp_partial
= false;
924 /* Send control message directly without accessing msd (Multi-Send
925 * Data) field which may be changed during data packet processing.
928 return netvsc_send_pkt(device
, packet
, net_device
, pb
, skb
);
930 /* batch packets in send buffer if possible */
933 msd_len
= msdp
->pkt
->total_data_buflen
;
935 try_batch
= msd_len
> 0 && msdp
->count
< net_device
->max_pkt
;
936 if (try_batch
&& msd_len
+ pktlen
+ net_device
->pkt_align
<
937 net_device
->send_section_size
) {
938 section_index
= msdp
->pkt
->send_buf_index
;
940 } else if (try_batch
&& msd_len
+ packet
->rmsg_size
<
941 net_device
->send_section_size
) {
942 section_index
= msdp
->pkt
->send_buf_index
;
943 packet
->cp_partial
= true;
945 } else if (pktlen
+ net_device
->pkt_align
<
946 net_device
->send_section_size
) {
947 section_index
= netvsc_get_next_send_section(net_device
);
948 if (unlikely(section_index
== NETVSC_INVALID_INDEX
)) {
949 ++ndev_ctx
->eth_stats
.tx_send_full
;
951 move_pkt_msd(&msd_send
, &msd_skb
, msdp
);
956 /* Keep aggregating only if stack says more data is coming
957 * and not doing mixed modes send and not flow blocked
959 xmit_more
= skb
->xmit_more
&&
960 !packet
->cp_partial
&&
961 !netif_xmit_stopped(netdev_get_tx_queue(ndev
, packet
->q_idx
));
963 if (section_index
!= NETVSC_INVALID_INDEX
) {
964 netvsc_copy_to_send_buf(net_device
,
965 section_index
, msd_len
,
966 packet
, rndis_msg
, pb
, xmit_more
);
968 packet
->send_buf_index
= section_index
;
970 if (packet
->cp_partial
) {
971 packet
->page_buf_cnt
-= packet
->rmsg_pgcnt
;
972 packet
->total_data_buflen
= msd_len
+ packet
->rmsg_size
;
974 packet
->page_buf_cnt
= 0;
975 packet
->total_data_buflen
+= msd_len
;
979 packet
->total_packets
+= msdp
->pkt
->total_packets
;
980 packet
->total_bytes
+= msdp
->pkt
->total_bytes
;
984 dev_consume_skb_any(msdp
->skb
);
997 move_pkt_msd(&msd_send
, &msd_skb
, msdp
);
1002 int m_ret
= netvsc_send_pkt(device
, msd_send
, net_device
,
1006 netvsc_free_send_slot(net_device
,
1007 msd_send
->send_buf_index
);
1008 dev_kfree_skb_any(msd_skb
);
1013 ret
= netvsc_send_pkt(device
, cur_send
, net_device
, pb
, skb
);
1015 if (ret
!= 0 && section_index
!= NETVSC_INVALID_INDEX
)
1016 netvsc_free_send_slot(net_device
, section_index
);
1021 /* Send pending recv completions */
1022 static int send_recv_completions(struct net_device
*ndev
,
1023 struct netvsc_device
*nvdev
,
1024 struct netvsc_channel
*nvchan
)
1026 struct multi_recv_comp
*mrc
= &nvchan
->mrc
;
1027 struct recv_comp_msg
{
1028 struct nvsp_message_header hdr
;
1031 struct recv_comp_msg msg
= {
1032 .hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
,
1036 while (mrc
->first
!= mrc
->next
) {
1037 const struct recv_comp_data
*rcd
1038 = mrc
->slots
+ mrc
->first
;
1040 msg
.status
= rcd
->status
;
1041 ret
= vmbus_sendpacket(nvchan
->channel
, &msg
, sizeof(msg
),
1042 rcd
->tid
, VM_PKT_COMP
, 0);
1043 if (unlikely(ret
)) {
1044 struct net_device_context
*ndev_ctx
= netdev_priv(ndev
);
1046 ++ndev_ctx
->eth_stats
.rx_comp_busy
;
1050 if (++mrc
->first
== nvdev
->recv_completion_cnt
)
1054 /* receive completion ring has been emptied */
1055 if (unlikely(nvdev
->destroy
))
1056 wake_up(&nvdev
->wait_drain
);
1061 /* Count how many receive completions are outstanding */
1062 static void recv_comp_slot_avail(const struct netvsc_device
*nvdev
,
1063 const struct multi_recv_comp
*mrc
,
1064 u32
*filled
, u32
*avail
)
1066 u32 count
= nvdev
->recv_completion_cnt
;
1068 if (mrc
->next
>= mrc
->first
)
1069 *filled
= mrc
->next
- mrc
->first
;
1071 *filled
= (count
- mrc
->first
) + mrc
->next
;
1073 *avail
= count
- *filled
- 1;
1076 /* Add receive complete to ring to send to host. */
1077 static void enq_receive_complete(struct net_device
*ndev
,
1078 struct netvsc_device
*nvdev
, u16 q_idx
,
1079 u64 tid
, u32 status
)
1081 struct netvsc_channel
*nvchan
= &nvdev
->chan_table
[q_idx
];
1082 struct multi_recv_comp
*mrc
= &nvchan
->mrc
;
1083 struct recv_comp_data
*rcd
;
1086 recv_comp_slot_avail(nvdev
, mrc
, &filled
, &avail
);
1088 if (unlikely(filled
> NAPI_POLL_WEIGHT
)) {
1089 send_recv_completions(ndev
, nvdev
, nvchan
);
1090 recv_comp_slot_avail(nvdev
, mrc
, &filled
, &avail
);
1093 if (unlikely(!avail
)) {
1094 netdev_err(ndev
, "Recv_comp full buf q:%hd, tid:%llx\n",
1099 rcd
= mrc
->slots
+ mrc
->next
;
1101 rcd
->status
= status
;
1103 if (++mrc
->next
== nvdev
->recv_completion_cnt
)
1107 static int netvsc_receive(struct net_device
*ndev
,
1108 struct netvsc_device
*net_device
,
1109 struct net_device_context
*net_device_ctx
,
1110 struct hv_device
*device
,
1111 struct vmbus_channel
*channel
,
1112 const struct vmpacket_descriptor
*desc
,
1113 struct nvsp_message
*nvsp
)
1115 const struct vmtransfer_page_packet_header
*vmxferpage_packet
1116 = container_of(desc
, const struct vmtransfer_page_packet_header
, d
);
1117 u16 q_idx
= channel
->offermsg
.offer
.sub_channel_index
;
1118 char *recv_buf
= net_device
->recv_buf
;
1119 u32 status
= NVSP_STAT_SUCCESS
;
1123 /* Make sure this is a valid nvsp packet */
1124 if (unlikely(nvsp
->hdr
.msg_type
!= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
)) {
1125 netif_err(net_device_ctx
, rx_err
, ndev
,
1126 "Unknown nvsp packet type received %u\n",
1127 nvsp
->hdr
.msg_type
);
1131 if (unlikely(vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
)) {
1132 netif_err(net_device_ctx
, rx_err
, ndev
,
1133 "Invalid xfer page set id - expecting %x got %x\n",
1134 NETVSC_RECEIVE_BUFFER_ID
,
1135 vmxferpage_packet
->xfer_pageset_id
);
1139 count
= vmxferpage_packet
->range_cnt
;
1141 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1142 for (i
= 0; i
< count
; i
++) {
1143 void *data
= recv_buf
1144 + vmxferpage_packet
->ranges
[i
].byte_offset
;
1145 u32 buflen
= vmxferpage_packet
->ranges
[i
].byte_count
;
1147 /* Pass it to the upper layer */
1148 status
= rndis_filter_receive(ndev
, net_device
, device
,
1149 channel
, data
, buflen
);
1152 enq_receive_complete(ndev
, net_device
, q_idx
,
1153 vmxferpage_packet
->d
.trans_id
, status
);
1158 static void netvsc_send_table(struct hv_device
*hdev
,
1159 struct nvsp_message
*nvmsg
)
1161 struct net_device
*ndev
= hv_get_drvdata(hdev
);
1162 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
1166 count
= nvmsg
->msg
.v5_msg
.send_table
.count
;
1167 if (count
!= VRSS_SEND_TAB_SIZE
) {
1168 netdev_err(ndev
, "Received wrong send-table size:%u\n", count
);
1172 tab
= (u32
*)((unsigned long)&nvmsg
->msg
.v5_msg
.send_table
+
1173 nvmsg
->msg
.v5_msg
.send_table
.offset
);
1175 for (i
= 0; i
< count
; i
++)
1176 net_device_ctx
->tx_table
[i
] = tab
[i
];
1179 static void netvsc_send_vf(struct net_device_context
*net_device_ctx
,
1180 struct nvsp_message
*nvmsg
)
1182 net_device_ctx
->vf_alloc
= nvmsg
->msg
.v4_msg
.vf_assoc
.allocated
;
1183 net_device_ctx
->vf_serial
= nvmsg
->msg
.v4_msg
.vf_assoc
.serial
;
1186 static inline void netvsc_receive_inband(struct hv_device
*hdev
,
1187 struct net_device_context
*net_device_ctx
,
1188 struct nvsp_message
*nvmsg
)
1190 switch (nvmsg
->hdr
.msg_type
) {
1191 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE
:
1192 netvsc_send_table(hdev
, nvmsg
);
1195 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION
:
1196 netvsc_send_vf(net_device_ctx
, nvmsg
);
1201 static int netvsc_process_raw_pkt(struct hv_device
*device
,
1202 struct vmbus_channel
*channel
,
1203 struct netvsc_device
*net_device
,
1204 struct net_device
*ndev
,
1205 const struct vmpacket_descriptor
*desc
,
1208 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
1209 struct nvsp_message
*nvmsg
= hv_pkt_data(desc
);
1211 switch (desc
->type
) {
1213 netvsc_send_completion(net_device
, channel
, device
,
1217 case VM_PKT_DATA_USING_XFER_PAGES
:
1218 return netvsc_receive(ndev
, net_device
, net_device_ctx
,
1219 device
, channel
, desc
, nvmsg
);
1222 case VM_PKT_DATA_INBAND
:
1223 netvsc_receive_inband(device
, net_device_ctx
, nvmsg
);
1227 netdev_err(ndev
, "unhandled packet type %d, tid %llx\n",
1228 desc
->type
, desc
->trans_id
);
1235 static struct hv_device
*netvsc_channel_to_device(struct vmbus_channel
*channel
)
1237 struct vmbus_channel
*primary
= channel
->primary_channel
;
1239 return primary
? primary
->device_obj
: channel
->device_obj
;
1242 /* Network processing softirq
1243 * Process data in incoming ring buffer from host
1244 * Stops when ring is empty or budget is met or exceeded.
1246 int netvsc_poll(struct napi_struct
*napi
, int budget
)
1248 struct netvsc_channel
*nvchan
1249 = container_of(napi
, struct netvsc_channel
, napi
);
1250 struct netvsc_device
*net_device
= nvchan
->net_device
;
1251 struct vmbus_channel
*channel
= nvchan
->channel
;
1252 struct hv_device
*device
= netvsc_channel_to_device(channel
);
1253 struct net_device
*ndev
= hv_get_drvdata(device
);
1257 /* If starting a new interval */
1259 nvchan
->desc
= hv_pkt_iter_first(channel
);
1261 while (nvchan
->desc
&& work_done
< budget
) {
1262 work_done
+= netvsc_process_raw_pkt(device
, channel
, net_device
,
1263 ndev
, nvchan
->desc
, budget
);
1264 nvchan
->desc
= hv_pkt_iter_next(channel
, nvchan
->desc
);
1267 /* Send any pending receive completions */
1268 ret
= send_recv_completions(ndev
, net_device
, nvchan
);
1270 /* If it did not exhaust NAPI budget this time
1271 * and not doing busy poll
1272 * then re-enable host interrupts
1273 * and reschedule if ring is not empty
1274 * or sending receive completion failed.
1276 if (work_done
< budget
&&
1277 napi_complete_done(napi
, work_done
) &&
1278 (ret
|| hv_end_read(&channel
->inbound
)) &&
1279 napi_schedule_prep(napi
)) {
1280 hv_begin_read(&channel
->inbound
);
1281 __napi_schedule(napi
);
1284 /* Driver may overshoot since multiple packets per descriptor */
1285 return min(work_done
, budget
);
1288 /* Call back when data is available in host ring buffer.
1289 * Processing is deferred until network softirq (NAPI)
1291 void netvsc_channel_cb(void *context
)
1293 struct netvsc_channel
*nvchan
= context
;
1294 struct vmbus_channel
*channel
= nvchan
->channel
;
1295 struct hv_ring_buffer_info
*rbi
= &channel
->inbound
;
1297 /* preload first vmpacket descriptor */
1298 prefetch(hv_get_ring_buffer(rbi
) + rbi
->priv_read_index
);
1300 if (napi_schedule_prep(&nvchan
->napi
)) {
1301 /* disable interupts from host */
1304 __napi_schedule_irqoff(&nvchan
->napi
);
1309 * netvsc_device_add - Callback when the device belonging to this
1312 struct netvsc_device
*netvsc_device_add(struct hv_device
*device
,
1313 const struct netvsc_device_info
*device_info
)
1316 int ring_size
= device_info
->ring_size
;
1317 struct netvsc_device
*net_device
;
1318 struct net_device
*ndev
= hv_get_drvdata(device
);
1319 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
1321 net_device
= alloc_net_device();
1323 return ERR_PTR(-ENOMEM
);
1325 for (i
= 0; i
< VRSS_SEND_TAB_SIZE
; i
++)
1326 net_device_ctx
->tx_table
[i
] = 0;
1328 net_device
->ring_size
= ring_size
;
1330 /* Because the device uses NAPI, all the interrupt batching and
1331 * control is done via Net softirq, not the channel handling
1333 set_channel_read_mode(device
->channel
, HV_CALL_ISR
);
1335 /* If we're reopening the device we may have multiple queues, fill the
1336 * chn_table with the default channel to use it before subchannels are
1338 * Initialize the channel state before we open;
1339 * we can be interrupted as soon as we open the channel.
1342 for (i
= 0; i
< VRSS_CHANNEL_MAX
; i
++) {
1343 struct netvsc_channel
*nvchan
= &net_device
->chan_table
[i
];
1345 nvchan
->channel
= device
->channel
;
1346 nvchan
->net_device
= net_device
;
1347 u64_stats_init(&nvchan
->tx_stats
.syncp
);
1348 u64_stats_init(&nvchan
->rx_stats
.syncp
);
1351 /* Enable NAPI handler before init callbacks */
1352 netif_napi_add(ndev
, &net_device
->chan_table
[0].napi
,
1353 netvsc_poll
, NAPI_POLL_WEIGHT
);
1355 /* Open the channel */
1356 ret
= vmbus_open(device
->channel
, ring_size
* PAGE_SIZE
,
1357 ring_size
* PAGE_SIZE
, NULL
, 0,
1359 net_device
->chan_table
);
1362 netdev_err(ndev
, "unable to open channel: %d\n", ret
);
1366 /* Channel is opened */
1367 netdev_dbg(ndev
, "hv_netvsc channel opened successfully\n");
1369 napi_enable(&net_device
->chan_table
[0].napi
);
1371 /* Connect with the NetVsp */
1372 ret
= netvsc_connect_vsp(device
, net_device
, device_info
);
1375 "unable to connect to NetVSP - %d\n", ret
);
1379 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1382 rcu_assign_pointer(net_device_ctx
->nvdev
, net_device
);
1387 RCU_INIT_POINTER(net_device_ctx
->nvdev
, NULL
);
1388 napi_disable(&net_device
->chan_table
[0].napi
);
1390 /* Now, we can close the channel safely */
1391 vmbus_close(device
->channel
);
1394 netif_napi_del(&net_device
->chan_table
[0].napi
);
1395 free_netvsc_device(&net_device
->rcu
);
1397 return ERR_PTR(ret
);