xref: /openbmc/linux/drivers/net/hyperv/netvsc_drv.c (revision 6396bb221514d2876fd6dc0aa2a1f240d99b37bb)
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
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.
7  *
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
11  * more details.
12  *
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/>.
15  *
16  * Authors:
17  *   Haiyang Zhang <haiyangz@microsoft.com>
18  *   Hank Janssen  <hjanssen@microsoft.com>
19  */
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 
22 #include <linux/init.h>
23 #include <linux/atomic.h>
24 #include <linux/module.h>
25 #include <linux/highmem.h>
26 #include <linux/device.h>
27 #include <linux/io.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/inetdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/if_vlan.h>
34 #include <linux/in.h>
35 #include <linux/slab.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/netpoll.h>
38 
39 #include <net/arp.h>
40 #include <net/route.h>
41 #include <net/sock.h>
42 #include <net/pkt_sched.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <net/failover.h>
46 
47 #include "hyperv_net.h"
48 
49 #define RING_SIZE_MIN	64
50 #define RETRY_US_LO	5000
51 #define RETRY_US_HI	10000
52 #define RETRY_MAX	2000	/* >10 sec */
53 
54 #define LINKCHANGE_INT (2 * HZ)
55 #define VF_TAKEOVER_INT (HZ / 10)
56 
57 static unsigned int ring_size __ro_after_init = 128;
58 module_param(ring_size, uint, 0444);
59 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
60 unsigned int netvsc_ring_bytes __ro_after_init;
61 
62 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
63 				NETIF_MSG_LINK | NETIF_MSG_IFUP |
64 				NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
65 				NETIF_MSG_TX_ERR;
66 
67 static int debug = -1;
68 module_param(debug, int, 0444);
69 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
70 
71 static void netvsc_change_rx_flags(struct net_device *net, int change)
72 {
73 	struct net_device_context *ndev_ctx = netdev_priv(net);
74 	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
75 	int inc;
76 
77 	if (!vf_netdev)
78 		return;
79 
80 	if (change & IFF_PROMISC) {
81 		inc = (net->flags & IFF_PROMISC) ? 1 : -1;
82 		dev_set_promiscuity(vf_netdev, inc);
83 	}
84 
85 	if (change & IFF_ALLMULTI) {
86 		inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
87 		dev_set_allmulti(vf_netdev, inc);
88 	}
89 }
90 
91 static void netvsc_set_rx_mode(struct net_device *net)
92 {
93 	struct net_device_context *ndev_ctx = netdev_priv(net);
94 	struct net_device *vf_netdev;
95 	struct netvsc_device *nvdev;
96 
97 	rcu_read_lock();
98 	vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
99 	if (vf_netdev) {
100 		dev_uc_sync(vf_netdev, net);
101 		dev_mc_sync(vf_netdev, net);
102 	}
103 
104 	nvdev = rcu_dereference(ndev_ctx->nvdev);
105 	if (nvdev)
106 		rndis_filter_update(nvdev);
107 	rcu_read_unlock();
108 }
109 
110 static int netvsc_open(struct net_device *net)
111 {
112 	struct net_device_context *ndev_ctx = netdev_priv(net);
113 	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
114 	struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
115 	struct rndis_device *rdev;
116 	int ret = 0;
117 
118 	netif_carrier_off(net);
119 
120 	/* Open up the device */
121 	ret = rndis_filter_open(nvdev);
122 	if (ret != 0) {
123 		netdev_err(net, "unable to open device (ret %d).\n", ret);
124 		return ret;
125 	}
126 
127 	rdev = nvdev->extension;
128 	if (!rdev->link_state) {
129 		netif_carrier_on(net);
130 		netif_tx_wake_all_queues(net);
131 	}
132 
133 	if (vf_netdev) {
134 		/* Setting synthetic device up transparently sets
135 		 * slave as up. If open fails, then slave will be
136 		 * still be offline (and not used).
137 		 */
138 		ret = dev_open(vf_netdev);
139 		if (ret)
140 			netdev_warn(net,
141 				    "unable to open slave: %s: %d\n",
142 				    vf_netdev->name, ret);
143 	}
144 	return 0;
145 }
146 
147 static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
148 {
149 	unsigned int retry = 0;
150 	int i;
151 
152 	/* Ensure pending bytes in ring are read */
153 	for (;;) {
154 		u32 aread = 0;
155 
156 		for (i = 0; i < nvdev->num_chn; i++) {
157 			struct vmbus_channel *chn
158 				= nvdev->chan_table[i].channel;
159 
160 			if (!chn)
161 				continue;
162 
163 			/* make sure receive not running now */
164 			napi_synchronize(&nvdev->chan_table[i].napi);
165 
166 			aread = hv_get_bytes_to_read(&chn->inbound);
167 			if (aread)
168 				break;
169 
170 			aread = hv_get_bytes_to_read(&chn->outbound);
171 			if (aread)
172 				break;
173 		}
174 
175 		if (aread == 0)
176 			return 0;
177 
178 		if (++retry > RETRY_MAX)
179 			return -ETIMEDOUT;
180 
181 		usleep_range(RETRY_US_LO, RETRY_US_HI);
182 	}
183 }
184 
185 static int netvsc_close(struct net_device *net)
186 {
187 	struct net_device_context *net_device_ctx = netdev_priv(net);
188 	struct net_device *vf_netdev
189 		= rtnl_dereference(net_device_ctx->vf_netdev);
190 	struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
191 	int ret;
192 
193 	netif_tx_disable(net);
194 
195 	/* No need to close rndis filter if it is removed already */
196 	if (!nvdev)
197 		return 0;
198 
199 	ret = rndis_filter_close(nvdev);
200 	if (ret != 0) {
201 		netdev_err(net, "unable to close device (ret %d).\n", ret);
202 		return ret;
203 	}
204 
205 	ret = netvsc_wait_until_empty(nvdev);
206 	if (ret)
207 		netdev_err(net, "Ring buffer not empty after closing rndis\n");
208 
209 	if (vf_netdev)
210 		dev_close(vf_netdev);
211 
212 	return ret;
213 }
214 
215 static inline void *init_ppi_data(struct rndis_message *msg,
216 				  u32 ppi_size, u32 pkt_type)
217 {
218 	struct rndis_packet *rndis_pkt = &msg->msg.pkt;
219 	struct rndis_per_packet_info *ppi;
220 
221 	rndis_pkt->data_offset += ppi_size;
222 	ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
223 		+ rndis_pkt->per_pkt_info_len;
224 
225 	ppi->size = ppi_size;
226 	ppi->type = pkt_type;
227 	ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
228 
229 	rndis_pkt->per_pkt_info_len += ppi_size;
230 
231 	return ppi + 1;
232 }
233 
234 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
235  * packets. We can use ethtool to change UDP hash level when necessary.
236  */
237 static inline u32 netvsc_get_hash(
238 	struct sk_buff *skb,
239 	const struct net_device_context *ndc)
240 {
241 	struct flow_keys flow;
242 	u32 hash, pkt_proto = 0;
243 	static u32 hashrnd __read_mostly;
244 
245 	net_get_random_once(&hashrnd, sizeof(hashrnd));
246 
247 	if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
248 		return 0;
249 
250 	switch (flow.basic.ip_proto) {
251 	case IPPROTO_TCP:
252 		if (flow.basic.n_proto == htons(ETH_P_IP))
253 			pkt_proto = HV_TCP4_L4HASH;
254 		else if (flow.basic.n_proto == htons(ETH_P_IPV6))
255 			pkt_proto = HV_TCP6_L4HASH;
256 
257 		break;
258 
259 	case IPPROTO_UDP:
260 		if (flow.basic.n_proto == htons(ETH_P_IP))
261 			pkt_proto = HV_UDP4_L4HASH;
262 		else if (flow.basic.n_proto == htons(ETH_P_IPV6))
263 			pkt_proto = HV_UDP6_L4HASH;
264 
265 		break;
266 	}
267 
268 	if (pkt_proto & ndc->l4_hash) {
269 		return skb_get_hash(skb);
270 	} else {
271 		if (flow.basic.n_proto == htons(ETH_P_IP))
272 			hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
273 		else if (flow.basic.n_proto == htons(ETH_P_IPV6))
274 			hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
275 		else
276 			hash = 0;
277 
278 		skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
279 	}
280 
281 	return hash;
282 }
283 
284 static inline int netvsc_get_tx_queue(struct net_device *ndev,
285 				      struct sk_buff *skb, int old_idx)
286 {
287 	const struct net_device_context *ndc = netdev_priv(ndev);
288 	struct sock *sk = skb->sk;
289 	int q_idx;
290 
291 	q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
292 			      (VRSS_SEND_TAB_SIZE - 1)];
293 
294 	/* If queue index changed record the new value */
295 	if (q_idx != old_idx &&
296 	    sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
297 		sk_tx_queue_set(sk, q_idx);
298 
299 	return q_idx;
300 }
301 
302 /*
303  * Select queue for transmit.
304  *
305  * If a valid queue has already been assigned, then use that.
306  * Otherwise compute tx queue based on hash and the send table.
307  *
308  * This is basically similar to default (__netdev_pick_tx) with the added step
309  * of using the host send_table when no other queue has been assigned.
310  *
311  * TODO support XPS - but get_xps_queue not exported
312  */
313 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
314 {
315 	int q_idx = sk_tx_queue_get(skb->sk);
316 
317 	if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
318 		/* If forwarding a packet, we use the recorded queue when
319 		 * available for better cache locality.
320 		 */
321 		if (skb_rx_queue_recorded(skb))
322 			q_idx = skb_get_rx_queue(skb);
323 		else
324 			q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
325 	}
326 
327 	return q_idx;
328 }
329 
330 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
331 			       void *accel_priv,
332 			       select_queue_fallback_t fallback)
333 {
334 	struct net_device_context *ndc = netdev_priv(ndev);
335 	struct net_device *vf_netdev;
336 	u16 txq;
337 
338 	rcu_read_lock();
339 	vf_netdev = rcu_dereference(ndc->vf_netdev);
340 	if (vf_netdev) {
341 		const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
342 
343 		if (vf_ops->ndo_select_queue)
344 			txq = vf_ops->ndo_select_queue(vf_netdev, skb,
345 						       accel_priv, fallback);
346 		else
347 			txq = fallback(vf_netdev, skb);
348 
349 		/* Record the queue selected by VF so that it can be
350 		 * used for common case where VF has more queues than
351 		 * the synthetic device.
352 		 */
353 		qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
354 	} else {
355 		txq = netvsc_pick_tx(ndev, skb);
356 	}
357 	rcu_read_unlock();
358 
359 	while (unlikely(txq >= ndev->real_num_tx_queues))
360 		txq -= ndev->real_num_tx_queues;
361 
362 	return txq;
363 }
364 
365 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
366 		       struct hv_page_buffer *pb)
367 {
368 	int j = 0;
369 
370 	/* Deal with compund pages by ignoring unused part
371 	 * of the page.
372 	 */
373 	page += (offset >> PAGE_SHIFT);
374 	offset &= ~PAGE_MASK;
375 
376 	while (len > 0) {
377 		unsigned long bytes;
378 
379 		bytes = PAGE_SIZE - offset;
380 		if (bytes > len)
381 			bytes = len;
382 		pb[j].pfn = page_to_pfn(page);
383 		pb[j].offset = offset;
384 		pb[j].len = bytes;
385 
386 		offset += bytes;
387 		len -= bytes;
388 
389 		if (offset == PAGE_SIZE && len) {
390 			page++;
391 			offset = 0;
392 			j++;
393 		}
394 	}
395 
396 	return j + 1;
397 }
398 
399 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
400 			   struct hv_netvsc_packet *packet,
401 			   struct hv_page_buffer *pb)
402 {
403 	u32 slots_used = 0;
404 	char *data = skb->data;
405 	int frags = skb_shinfo(skb)->nr_frags;
406 	int i;
407 
408 	/* The packet is laid out thus:
409 	 * 1. hdr: RNDIS header and PPI
410 	 * 2. skb linear data
411 	 * 3. skb fragment data
412 	 */
413 	slots_used += fill_pg_buf(virt_to_page(hdr),
414 				  offset_in_page(hdr),
415 				  len, &pb[slots_used]);
416 
417 	packet->rmsg_size = len;
418 	packet->rmsg_pgcnt = slots_used;
419 
420 	slots_used += fill_pg_buf(virt_to_page(data),
421 				offset_in_page(data),
422 				skb_headlen(skb), &pb[slots_used]);
423 
424 	for (i = 0; i < frags; i++) {
425 		skb_frag_t *frag = skb_shinfo(skb)->frags + i;
426 
427 		slots_used += fill_pg_buf(skb_frag_page(frag),
428 					frag->page_offset,
429 					skb_frag_size(frag), &pb[slots_used]);
430 	}
431 	return slots_used;
432 }
433 
434 static int count_skb_frag_slots(struct sk_buff *skb)
435 {
436 	int i, frags = skb_shinfo(skb)->nr_frags;
437 	int pages = 0;
438 
439 	for (i = 0; i < frags; i++) {
440 		skb_frag_t *frag = skb_shinfo(skb)->frags + i;
441 		unsigned long size = skb_frag_size(frag);
442 		unsigned long offset = frag->page_offset;
443 
444 		/* Skip unused frames from start of page */
445 		offset &= ~PAGE_MASK;
446 		pages += PFN_UP(offset + size);
447 	}
448 	return pages;
449 }
450 
451 static int netvsc_get_slots(struct sk_buff *skb)
452 {
453 	char *data = skb->data;
454 	unsigned int offset = offset_in_page(data);
455 	unsigned int len = skb_headlen(skb);
456 	int slots;
457 	int frag_slots;
458 
459 	slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
460 	frag_slots = count_skb_frag_slots(skb);
461 	return slots + frag_slots;
462 }
463 
464 static u32 net_checksum_info(struct sk_buff *skb)
465 {
466 	if (skb->protocol == htons(ETH_P_IP)) {
467 		struct iphdr *ip = ip_hdr(skb);
468 
469 		if (ip->protocol == IPPROTO_TCP)
470 			return TRANSPORT_INFO_IPV4_TCP;
471 		else if (ip->protocol == IPPROTO_UDP)
472 			return TRANSPORT_INFO_IPV4_UDP;
473 	} else {
474 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
475 
476 		if (ip6->nexthdr == IPPROTO_TCP)
477 			return TRANSPORT_INFO_IPV6_TCP;
478 		else if (ip6->nexthdr == IPPROTO_UDP)
479 			return TRANSPORT_INFO_IPV6_UDP;
480 	}
481 
482 	return TRANSPORT_INFO_NOT_IP;
483 }
484 
485 /* Send skb on the slave VF device. */
486 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
487 			  struct sk_buff *skb)
488 {
489 	struct net_device_context *ndev_ctx = netdev_priv(net);
490 	unsigned int len = skb->len;
491 	int rc;
492 
493 	skb->dev = vf_netdev;
494 	skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
495 
496 	rc = dev_queue_xmit(skb);
497 	if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
498 		struct netvsc_vf_pcpu_stats *pcpu_stats
499 			= this_cpu_ptr(ndev_ctx->vf_stats);
500 
501 		u64_stats_update_begin(&pcpu_stats->syncp);
502 		pcpu_stats->tx_packets++;
503 		pcpu_stats->tx_bytes += len;
504 		u64_stats_update_end(&pcpu_stats->syncp);
505 	} else {
506 		this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
507 	}
508 
509 	return rc;
510 }
511 
512 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
513 {
514 	struct net_device_context *net_device_ctx = netdev_priv(net);
515 	struct hv_netvsc_packet *packet = NULL;
516 	int ret;
517 	unsigned int num_data_pgs;
518 	struct rndis_message *rndis_msg;
519 	struct net_device *vf_netdev;
520 	u32 rndis_msg_size;
521 	u32 hash;
522 	struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
523 
524 	/* if VF is present and up then redirect packets
525 	 * already called with rcu_read_lock_bh
526 	 */
527 	vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
528 	if (vf_netdev && netif_running(vf_netdev) &&
529 	    !netpoll_tx_running(net))
530 		return netvsc_vf_xmit(net, vf_netdev, skb);
531 
532 	/* We will atmost need two pages to describe the rndis
533 	 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
534 	 * of pages in a single packet. If skb is scattered around
535 	 * more pages we try linearizing it.
536 	 */
537 
538 	num_data_pgs = netvsc_get_slots(skb) + 2;
539 
540 	if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
541 		++net_device_ctx->eth_stats.tx_scattered;
542 
543 		if (skb_linearize(skb))
544 			goto no_memory;
545 
546 		num_data_pgs = netvsc_get_slots(skb) + 2;
547 		if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
548 			++net_device_ctx->eth_stats.tx_too_big;
549 			goto drop;
550 		}
551 	}
552 
553 	/*
554 	 * Place the rndis header in the skb head room and
555 	 * the skb->cb will be used for hv_netvsc_packet
556 	 * structure.
557 	 */
558 	ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
559 	if (ret)
560 		goto no_memory;
561 
562 	/* Use the skb control buffer for building up the packet */
563 	BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
564 			FIELD_SIZEOF(struct sk_buff, cb));
565 	packet = (struct hv_netvsc_packet *)skb->cb;
566 
567 	packet->q_idx = skb_get_queue_mapping(skb);
568 
569 	packet->total_data_buflen = skb->len;
570 	packet->total_bytes = skb->len;
571 	packet->total_packets = 1;
572 
573 	rndis_msg = (struct rndis_message *)skb->head;
574 
575 	/* Add the rndis header */
576 	rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
577 	rndis_msg->msg_len = packet->total_data_buflen;
578 
579 	rndis_msg->msg.pkt = (struct rndis_packet) {
580 		.data_offset = sizeof(struct rndis_packet),
581 		.data_len = packet->total_data_buflen,
582 		.per_pkt_info_offset = sizeof(struct rndis_packet),
583 	};
584 
585 	rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
586 
587 	hash = skb_get_hash_raw(skb);
588 	if (hash != 0 && net->real_num_tx_queues > 1) {
589 		u32 *hash_info;
590 
591 		rndis_msg_size += NDIS_HASH_PPI_SIZE;
592 		hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
593 					  NBL_HASH_VALUE);
594 		*hash_info = hash;
595 	}
596 
597 	if (skb_vlan_tag_present(skb)) {
598 		struct ndis_pkt_8021q_info *vlan;
599 
600 		rndis_msg_size += NDIS_VLAN_PPI_SIZE;
601 		vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
602 				     IEEE_8021Q_INFO);
603 
604 		vlan->value = 0;
605 		vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
606 		vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
607 				VLAN_PRIO_SHIFT;
608 	}
609 
610 	if (skb_is_gso(skb)) {
611 		struct ndis_tcp_lso_info *lso_info;
612 
613 		rndis_msg_size += NDIS_LSO_PPI_SIZE;
614 		lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
615 					 TCP_LARGESEND_PKTINFO);
616 
617 		lso_info->value = 0;
618 		lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
619 		if (skb->protocol == htons(ETH_P_IP)) {
620 			lso_info->lso_v2_transmit.ip_version =
621 				NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
622 			ip_hdr(skb)->tot_len = 0;
623 			ip_hdr(skb)->check = 0;
624 			tcp_hdr(skb)->check =
625 				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
626 						   ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
627 		} else {
628 			lso_info->lso_v2_transmit.ip_version =
629 				NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
630 			ipv6_hdr(skb)->payload_len = 0;
631 			tcp_hdr(skb)->check =
632 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
633 						 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
634 		}
635 		lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
636 		lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
637 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
638 		if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
639 			struct ndis_tcp_ip_checksum_info *csum_info;
640 
641 			rndis_msg_size += NDIS_CSUM_PPI_SIZE;
642 			csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
643 						  TCPIP_CHKSUM_PKTINFO);
644 
645 			csum_info->value = 0;
646 			csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
647 
648 			if (skb->protocol == htons(ETH_P_IP)) {
649 				csum_info->transmit.is_ipv4 = 1;
650 
651 				if (ip_hdr(skb)->protocol == IPPROTO_TCP)
652 					csum_info->transmit.tcp_checksum = 1;
653 				else
654 					csum_info->transmit.udp_checksum = 1;
655 			} else {
656 				csum_info->transmit.is_ipv6 = 1;
657 
658 				if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
659 					csum_info->transmit.tcp_checksum = 1;
660 				else
661 					csum_info->transmit.udp_checksum = 1;
662 			}
663 		} else {
664 			/* Can't do offload of this type of checksum */
665 			if (skb_checksum_help(skb))
666 				goto drop;
667 		}
668 	}
669 
670 	/* Start filling in the page buffers with the rndis hdr */
671 	rndis_msg->msg_len += rndis_msg_size;
672 	packet->total_data_buflen = rndis_msg->msg_len;
673 	packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
674 					       skb, packet, pb);
675 
676 	/* timestamp packet in software */
677 	skb_tx_timestamp(skb);
678 
679 	ret = netvsc_send(net, packet, rndis_msg, pb, skb);
680 	if (likely(ret == 0))
681 		return NETDEV_TX_OK;
682 
683 	if (ret == -EAGAIN) {
684 		++net_device_ctx->eth_stats.tx_busy;
685 		return NETDEV_TX_BUSY;
686 	}
687 
688 	if (ret == -ENOSPC)
689 		++net_device_ctx->eth_stats.tx_no_space;
690 
691 drop:
692 	dev_kfree_skb_any(skb);
693 	net->stats.tx_dropped++;
694 
695 	return NETDEV_TX_OK;
696 
697 no_memory:
698 	++net_device_ctx->eth_stats.tx_no_memory;
699 	goto drop;
700 }
701 
702 /*
703  * netvsc_linkstatus_callback - Link up/down notification
704  */
705 void netvsc_linkstatus_callback(struct net_device *net,
706 				struct rndis_message *resp)
707 {
708 	struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
709 	struct net_device_context *ndev_ctx = netdev_priv(net);
710 	struct netvsc_reconfig *event;
711 	unsigned long flags;
712 
713 	/* Update the physical link speed when changing to another vSwitch */
714 	if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
715 		u32 speed;
716 
717 		speed = *(u32 *)((void *)indicate
718 				 + indicate->status_buf_offset) / 10000;
719 		ndev_ctx->speed = speed;
720 		return;
721 	}
722 
723 	/* Handle these link change statuses below */
724 	if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
725 	    indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
726 	    indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
727 		return;
728 
729 	if (net->reg_state != NETREG_REGISTERED)
730 		return;
731 
732 	event = kzalloc(sizeof(*event), GFP_ATOMIC);
733 	if (!event)
734 		return;
735 	event->event = indicate->status;
736 
737 	spin_lock_irqsave(&ndev_ctx->lock, flags);
738 	list_add_tail(&event->list, &ndev_ctx->reconfig_events);
739 	spin_unlock_irqrestore(&ndev_ctx->lock, flags);
740 
741 	schedule_delayed_work(&ndev_ctx->dwork, 0);
742 }
743 
744 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
745 					     struct napi_struct *napi,
746 					     const struct ndis_tcp_ip_checksum_info *csum_info,
747 					     const struct ndis_pkt_8021q_info *vlan,
748 					     void *data, u32 buflen)
749 {
750 	struct sk_buff *skb;
751 
752 	skb = napi_alloc_skb(napi, buflen);
753 	if (!skb)
754 		return skb;
755 
756 	/*
757 	 * Copy to skb. This copy is needed here since the memory pointed by
758 	 * hv_netvsc_packet cannot be deallocated
759 	 */
760 	skb_put_data(skb, data, buflen);
761 
762 	skb->protocol = eth_type_trans(skb, net);
763 
764 	/* skb is already created with CHECKSUM_NONE */
765 	skb_checksum_none_assert(skb);
766 
767 	/*
768 	 * In Linux, the IP checksum is always checked.
769 	 * Do L4 checksum offload if enabled and present.
770 	 */
771 	if (csum_info && (net->features & NETIF_F_RXCSUM)) {
772 		if (csum_info->receive.tcp_checksum_succeeded ||
773 		    csum_info->receive.udp_checksum_succeeded)
774 			skb->ip_summed = CHECKSUM_UNNECESSARY;
775 	}
776 
777 	if (vlan) {
778 		u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
779 
780 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
781 				       vlan_tci);
782 	}
783 
784 	return skb;
785 }
786 
787 /*
788  * netvsc_recv_callback -  Callback when we receive a packet from the
789  * "wire" on the specified device.
790  */
791 int netvsc_recv_callback(struct net_device *net,
792 			 struct netvsc_device *net_device,
793 			 struct vmbus_channel *channel,
794 			 void  *data, u32 len,
795 			 const struct ndis_tcp_ip_checksum_info *csum_info,
796 			 const struct ndis_pkt_8021q_info *vlan)
797 {
798 	struct net_device_context *net_device_ctx = netdev_priv(net);
799 	u16 q_idx = channel->offermsg.offer.sub_channel_index;
800 	struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
801 	struct sk_buff *skb;
802 	struct netvsc_stats *rx_stats;
803 
804 	if (net->reg_state != NETREG_REGISTERED)
805 		return NVSP_STAT_FAIL;
806 
807 	/* Allocate a skb - TODO direct I/O to pages? */
808 	skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
809 				    csum_info, vlan, data, len);
810 	if (unlikely(!skb)) {
811 		++net_device_ctx->eth_stats.rx_no_memory;
812 		rcu_read_unlock();
813 		return NVSP_STAT_FAIL;
814 	}
815 
816 	skb_record_rx_queue(skb, q_idx);
817 
818 	/*
819 	 * Even if injecting the packet, record the statistics
820 	 * on the synthetic device because modifying the VF device
821 	 * statistics will not work correctly.
822 	 */
823 	rx_stats = &nvchan->rx_stats;
824 	u64_stats_update_begin(&rx_stats->syncp);
825 	rx_stats->packets++;
826 	rx_stats->bytes += len;
827 
828 	if (skb->pkt_type == PACKET_BROADCAST)
829 		++rx_stats->broadcast;
830 	else if (skb->pkt_type == PACKET_MULTICAST)
831 		++rx_stats->multicast;
832 	u64_stats_update_end(&rx_stats->syncp);
833 
834 	napi_gro_receive(&nvchan->napi, skb);
835 	return NVSP_STAT_SUCCESS;
836 }
837 
838 static void netvsc_get_drvinfo(struct net_device *net,
839 			       struct ethtool_drvinfo *info)
840 {
841 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
842 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
843 }
844 
845 static void netvsc_get_channels(struct net_device *net,
846 				struct ethtool_channels *channel)
847 {
848 	struct net_device_context *net_device_ctx = netdev_priv(net);
849 	struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
850 
851 	if (nvdev) {
852 		channel->max_combined	= nvdev->max_chn;
853 		channel->combined_count = nvdev->num_chn;
854 	}
855 }
856 
857 static int netvsc_detach(struct net_device *ndev,
858 			 struct netvsc_device *nvdev)
859 {
860 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
861 	struct hv_device *hdev = ndev_ctx->device_ctx;
862 	int ret;
863 
864 	/* Don't try continuing to try and setup sub channels */
865 	if (cancel_work_sync(&nvdev->subchan_work))
866 		nvdev->num_chn = 1;
867 
868 	/* If device was up (receiving) then shutdown */
869 	if (netif_running(ndev)) {
870 		netif_tx_disable(ndev);
871 
872 		ret = rndis_filter_close(nvdev);
873 		if (ret) {
874 			netdev_err(ndev,
875 				   "unable to close device (ret %d).\n", ret);
876 			return ret;
877 		}
878 
879 		ret = netvsc_wait_until_empty(nvdev);
880 		if (ret) {
881 			netdev_err(ndev,
882 				   "Ring buffer not empty after closing rndis\n");
883 			return ret;
884 		}
885 	}
886 
887 	netif_device_detach(ndev);
888 
889 	rndis_filter_device_remove(hdev, nvdev);
890 
891 	return 0;
892 }
893 
894 static int netvsc_attach(struct net_device *ndev,
895 			 struct netvsc_device_info *dev_info)
896 {
897 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
898 	struct hv_device *hdev = ndev_ctx->device_ctx;
899 	struct netvsc_device *nvdev;
900 	struct rndis_device *rdev;
901 	int ret;
902 
903 	nvdev = rndis_filter_device_add(hdev, dev_info);
904 	if (IS_ERR(nvdev))
905 		return PTR_ERR(nvdev);
906 
907 	/* Note: enable and attach happen when sub-channels setup */
908 
909 	netif_carrier_off(ndev);
910 
911 	if (netif_running(ndev)) {
912 		ret = rndis_filter_open(nvdev);
913 		if (ret)
914 			return ret;
915 
916 		rdev = nvdev->extension;
917 		if (!rdev->link_state)
918 			netif_carrier_on(ndev);
919 	}
920 
921 	return 0;
922 }
923 
924 static int netvsc_set_channels(struct net_device *net,
925 			       struct ethtool_channels *channels)
926 {
927 	struct net_device_context *net_device_ctx = netdev_priv(net);
928 	struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
929 	unsigned int orig, count = channels->combined_count;
930 	struct netvsc_device_info device_info;
931 	int ret;
932 
933 	/* We do not support separate count for rx, tx, or other */
934 	if (count == 0 ||
935 	    channels->rx_count || channels->tx_count || channels->other_count)
936 		return -EINVAL;
937 
938 	if (!nvdev || nvdev->destroy)
939 		return -ENODEV;
940 
941 	if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
942 		return -EINVAL;
943 
944 	if (count > nvdev->max_chn)
945 		return -EINVAL;
946 
947 	orig = nvdev->num_chn;
948 
949 	memset(&device_info, 0, sizeof(device_info));
950 	device_info.num_chn = count;
951 	device_info.send_sections = nvdev->send_section_cnt;
952 	device_info.send_section_size = nvdev->send_section_size;
953 	device_info.recv_sections = nvdev->recv_section_cnt;
954 	device_info.recv_section_size = nvdev->recv_section_size;
955 
956 	ret = netvsc_detach(net, nvdev);
957 	if (ret)
958 		return ret;
959 
960 	ret = netvsc_attach(net, &device_info);
961 	if (ret) {
962 		device_info.num_chn = orig;
963 		if (netvsc_attach(net, &device_info))
964 			netdev_err(net, "restoring channel setting failed\n");
965 	}
966 
967 	return ret;
968 }
969 
970 static bool
971 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
972 {
973 	struct ethtool_link_ksettings diff1 = *cmd;
974 	struct ethtool_link_ksettings diff2 = {};
975 
976 	diff1.base.speed = 0;
977 	diff1.base.duplex = 0;
978 	/* advertising and cmd are usually set */
979 	ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
980 	diff1.base.cmd = 0;
981 	/* We set port to PORT_OTHER */
982 	diff2.base.port = PORT_OTHER;
983 
984 	return !memcmp(&diff1, &diff2, sizeof(diff1));
985 }
986 
987 static void netvsc_init_settings(struct net_device *dev)
988 {
989 	struct net_device_context *ndc = netdev_priv(dev);
990 
991 	ndc->l4_hash = HV_DEFAULT_L4HASH;
992 
993 	ndc->speed = SPEED_UNKNOWN;
994 	ndc->duplex = DUPLEX_FULL;
995 }
996 
997 static int netvsc_get_link_ksettings(struct net_device *dev,
998 				     struct ethtool_link_ksettings *cmd)
999 {
1000 	struct net_device_context *ndc = netdev_priv(dev);
1001 
1002 	cmd->base.speed = ndc->speed;
1003 	cmd->base.duplex = ndc->duplex;
1004 	cmd->base.port = PORT_OTHER;
1005 
1006 	return 0;
1007 }
1008 
1009 static int netvsc_set_link_ksettings(struct net_device *dev,
1010 				     const struct ethtool_link_ksettings *cmd)
1011 {
1012 	struct net_device_context *ndc = netdev_priv(dev);
1013 	u32 speed;
1014 
1015 	speed = cmd->base.speed;
1016 	if (!ethtool_validate_speed(speed) ||
1017 	    !ethtool_validate_duplex(cmd->base.duplex) ||
1018 	    !netvsc_validate_ethtool_ss_cmd(cmd))
1019 		return -EINVAL;
1020 
1021 	ndc->speed = speed;
1022 	ndc->duplex = cmd->base.duplex;
1023 
1024 	return 0;
1025 }
1026 
1027 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
1028 {
1029 	struct net_device_context *ndevctx = netdev_priv(ndev);
1030 	struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1031 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1032 	int orig_mtu = ndev->mtu;
1033 	struct netvsc_device_info device_info;
1034 	int ret = 0;
1035 
1036 	if (!nvdev || nvdev->destroy)
1037 		return -ENODEV;
1038 
1039 	/* Change MTU of underlying VF netdev first. */
1040 	if (vf_netdev) {
1041 		ret = dev_set_mtu(vf_netdev, mtu);
1042 		if (ret)
1043 			return ret;
1044 	}
1045 
1046 	memset(&device_info, 0, sizeof(device_info));
1047 	device_info.num_chn = nvdev->num_chn;
1048 	device_info.send_sections = nvdev->send_section_cnt;
1049 	device_info.send_section_size = nvdev->send_section_size;
1050 	device_info.recv_sections = nvdev->recv_section_cnt;
1051 	device_info.recv_section_size = nvdev->recv_section_size;
1052 
1053 	ret = netvsc_detach(ndev, nvdev);
1054 	if (ret)
1055 		goto rollback_vf;
1056 
1057 	ndev->mtu = mtu;
1058 
1059 	ret = netvsc_attach(ndev, &device_info);
1060 	if (ret)
1061 		goto rollback;
1062 
1063 	return 0;
1064 
1065 rollback:
1066 	/* Attempt rollback to original MTU */
1067 	ndev->mtu = orig_mtu;
1068 
1069 	if (netvsc_attach(ndev, &device_info))
1070 		netdev_err(ndev, "restoring mtu failed\n");
1071 rollback_vf:
1072 	if (vf_netdev)
1073 		dev_set_mtu(vf_netdev, orig_mtu);
1074 
1075 	return ret;
1076 }
1077 
1078 static void netvsc_get_vf_stats(struct net_device *net,
1079 				struct netvsc_vf_pcpu_stats *tot)
1080 {
1081 	struct net_device_context *ndev_ctx = netdev_priv(net);
1082 	int i;
1083 
1084 	memset(tot, 0, sizeof(*tot));
1085 
1086 	for_each_possible_cpu(i) {
1087 		const struct netvsc_vf_pcpu_stats *stats
1088 			= per_cpu_ptr(ndev_ctx->vf_stats, i);
1089 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1090 		unsigned int start;
1091 
1092 		do {
1093 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1094 			rx_packets = stats->rx_packets;
1095 			tx_packets = stats->tx_packets;
1096 			rx_bytes = stats->rx_bytes;
1097 			tx_bytes = stats->tx_bytes;
1098 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1099 
1100 		tot->rx_packets += rx_packets;
1101 		tot->tx_packets += tx_packets;
1102 		tot->rx_bytes   += rx_bytes;
1103 		tot->tx_bytes   += tx_bytes;
1104 		tot->tx_dropped += stats->tx_dropped;
1105 	}
1106 }
1107 
1108 static void netvsc_get_stats64(struct net_device *net,
1109 			       struct rtnl_link_stats64 *t)
1110 {
1111 	struct net_device_context *ndev_ctx = netdev_priv(net);
1112 	struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1113 	struct netvsc_vf_pcpu_stats vf_tot;
1114 	int i;
1115 
1116 	if (!nvdev)
1117 		return;
1118 
1119 	netdev_stats_to_stats64(t, &net->stats);
1120 
1121 	netvsc_get_vf_stats(net, &vf_tot);
1122 	t->rx_packets += vf_tot.rx_packets;
1123 	t->tx_packets += vf_tot.tx_packets;
1124 	t->rx_bytes   += vf_tot.rx_bytes;
1125 	t->tx_bytes   += vf_tot.tx_bytes;
1126 	t->tx_dropped += vf_tot.tx_dropped;
1127 
1128 	for (i = 0; i < nvdev->num_chn; i++) {
1129 		const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1130 		const struct netvsc_stats *stats;
1131 		u64 packets, bytes, multicast;
1132 		unsigned int start;
1133 
1134 		stats = &nvchan->tx_stats;
1135 		do {
1136 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1137 			packets = stats->packets;
1138 			bytes = stats->bytes;
1139 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1140 
1141 		t->tx_bytes	+= bytes;
1142 		t->tx_packets	+= packets;
1143 
1144 		stats = &nvchan->rx_stats;
1145 		do {
1146 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1147 			packets = stats->packets;
1148 			bytes = stats->bytes;
1149 			multicast = stats->multicast + stats->broadcast;
1150 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1151 
1152 		t->rx_bytes	+= bytes;
1153 		t->rx_packets	+= packets;
1154 		t->multicast	+= multicast;
1155 	}
1156 }
1157 
1158 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1159 {
1160 	struct net_device_context *ndc = netdev_priv(ndev);
1161 	struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1162 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1163 	struct sockaddr *addr = p;
1164 	int err;
1165 
1166 	err = eth_prepare_mac_addr_change(ndev, p);
1167 	if (err)
1168 		return err;
1169 
1170 	if (!nvdev)
1171 		return -ENODEV;
1172 
1173 	if (vf_netdev) {
1174 		err = dev_set_mac_address(vf_netdev, addr);
1175 		if (err)
1176 			return err;
1177 	}
1178 
1179 	err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1180 	if (!err) {
1181 		eth_commit_mac_addr_change(ndev, p);
1182 	} else if (vf_netdev) {
1183 		/* rollback change on VF */
1184 		memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1185 		dev_set_mac_address(vf_netdev, addr);
1186 	}
1187 
1188 	return err;
1189 }
1190 
1191 static const struct {
1192 	char name[ETH_GSTRING_LEN];
1193 	u16 offset;
1194 } netvsc_stats[] = {
1195 	{ "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1196 	{ "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1197 	{ "tx_no_space",  offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1198 	{ "tx_too_big",	  offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1199 	{ "tx_busy",	  offsetof(struct netvsc_ethtool_stats, tx_busy) },
1200 	{ "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1201 	{ "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1202 	{ "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1203 	{ "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1204 	{ "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1205 }, vf_stats[] = {
1206 	{ "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1207 	{ "vf_rx_bytes",   offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1208 	{ "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1209 	{ "vf_tx_bytes",   offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1210 	{ "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1211 };
1212 
1213 #define NETVSC_GLOBAL_STATS_LEN	ARRAY_SIZE(netvsc_stats)
1214 #define NETVSC_VF_STATS_LEN	ARRAY_SIZE(vf_stats)
1215 
1216 /* 4 statistics per queue (rx/tx packets/bytes) */
1217 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1218 
1219 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1220 {
1221 	struct net_device_context *ndc = netdev_priv(dev);
1222 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1223 
1224 	if (!nvdev)
1225 		return -ENODEV;
1226 
1227 	switch (string_set) {
1228 	case ETH_SS_STATS:
1229 		return NETVSC_GLOBAL_STATS_LEN
1230 			+ NETVSC_VF_STATS_LEN
1231 			+ NETVSC_QUEUE_STATS_LEN(nvdev);
1232 	default:
1233 		return -EINVAL;
1234 	}
1235 }
1236 
1237 static void netvsc_get_ethtool_stats(struct net_device *dev,
1238 				     struct ethtool_stats *stats, u64 *data)
1239 {
1240 	struct net_device_context *ndc = netdev_priv(dev);
1241 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1242 	const void *nds = &ndc->eth_stats;
1243 	const struct netvsc_stats *qstats;
1244 	struct netvsc_vf_pcpu_stats sum;
1245 	unsigned int start;
1246 	u64 packets, bytes;
1247 	int i, j;
1248 
1249 	if (!nvdev)
1250 		return;
1251 
1252 	for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1253 		data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1254 
1255 	netvsc_get_vf_stats(dev, &sum);
1256 	for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1257 		data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1258 
1259 	for (j = 0; j < nvdev->num_chn; j++) {
1260 		qstats = &nvdev->chan_table[j].tx_stats;
1261 
1262 		do {
1263 			start = u64_stats_fetch_begin_irq(&qstats->syncp);
1264 			packets = qstats->packets;
1265 			bytes = qstats->bytes;
1266 		} while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1267 		data[i++] = packets;
1268 		data[i++] = bytes;
1269 
1270 		qstats = &nvdev->chan_table[j].rx_stats;
1271 		do {
1272 			start = u64_stats_fetch_begin_irq(&qstats->syncp);
1273 			packets = qstats->packets;
1274 			bytes = qstats->bytes;
1275 		} while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1276 		data[i++] = packets;
1277 		data[i++] = bytes;
1278 	}
1279 }
1280 
1281 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1282 {
1283 	struct net_device_context *ndc = netdev_priv(dev);
1284 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1285 	u8 *p = data;
1286 	int i;
1287 
1288 	if (!nvdev)
1289 		return;
1290 
1291 	switch (stringset) {
1292 	case ETH_SS_STATS:
1293 		for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1294 			memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1295 			p += ETH_GSTRING_LEN;
1296 		}
1297 
1298 		for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1299 			memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1300 			p += ETH_GSTRING_LEN;
1301 		}
1302 
1303 		for (i = 0; i < nvdev->num_chn; i++) {
1304 			sprintf(p, "tx_queue_%u_packets", i);
1305 			p += ETH_GSTRING_LEN;
1306 			sprintf(p, "tx_queue_%u_bytes", i);
1307 			p += ETH_GSTRING_LEN;
1308 			sprintf(p, "rx_queue_%u_packets", i);
1309 			p += ETH_GSTRING_LEN;
1310 			sprintf(p, "rx_queue_%u_bytes", i);
1311 			p += ETH_GSTRING_LEN;
1312 		}
1313 
1314 		break;
1315 	}
1316 }
1317 
1318 static int
1319 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1320 			 struct ethtool_rxnfc *info)
1321 {
1322 	const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1323 
1324 	info->data = RXH_IP_SRC | RXH_IP_DST;
1325 
1326 	switch (info->flow_type) {
1327 	case TCP_V4_FLOW:
1328 		if (ndc->l4_hash & HV_TCP4_L4HASH)
1329 			info->data |= l4_flag;
1330 
1331 		break;
1332 
1333 	case TCP_V6_FLOW:
1334 		if (ndc->l4_hash & HV_TCP6_L4HASH)
1335 			info->data |= l4_flag;
1336 
1337 		break;
1338 
1339 	case UDP_V4_FLOW:
1340 		if (ndc->l4_hash & HV_UDP4_L4HASH)
1341 			info->data |= l4_flag;
1342 
1343 		break;
1344 
1345 	case UDP_V6_FLOW:
1346 		if (ndc->l4_hash & HV_UDP6_L4HASH)
1347 			info->data |= l4_flag;
1348 
1349 		break;
1350 
1351 	case IPV4_FLOW:
1352 	case IPV6_FLOW:
1353 		break;
1354 	default:
1355 		info->data = 0;
1356 		break;
1357 	}
1358 
1359 	return 0;
1360 }
1361 
1362 static int
1363 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1364 		 u32 *rules)
1365 {
1366 	struct net_device_context *ndc = netdev_priv(dev);
1367 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1368 
1369 	if (!nvdev)
1370 		return -ENODEV;
1371 
1372 	switch (info->cmd) {
1373 	case ETHTOOL_GRXRINGS:
1374 		info->data = nvdev->num_chn;
1375 		return 0;
1376 
1377 	case ETHTOOL_GRXFH:
1378 		return netvsc_get_rss_hash_opts(ndc, info);
1379 	}
1380 	return -EOPNOTSUPP;
1381 }
1382 
1383 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1384 				    struct ethtool_rxnfc *info)
1385 {
1386 	if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1387 			   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1388 		switch (info->flow_type) {
1389 		case TCP_V4_FLOW:
1390 			ndc->l4_hash |= HV_TCP4_L4HASH;
1391 			break;
1392 
1393 		case TCP_V6_FLOW:
1394 			ndc->l4_hash |= HV_TCP6_L4HASH;
1395 			break;
1396 
1397 		case UDP_V4_FLOW:
1398 			ndc->l4_hash |= HV_UDP4_L4HASH;
1399 			break;
1400 
1401 		case UDP_V6_FLOW:
1402 			ndc->l4_hash |= HV_UDP6_L4HASH;
1403 			break;
1404 
1405 		default:
1406 			return -EOPNOTSUPP;
1407 		}
1408 
1409 		return 0;
1410 	}
1411 
1412 	if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1413 		switch (info->flow_type) {
1414 		case TCP_V4_FLOW:
1415 			ndc->l4_hash &= ~HV_TCP4_L4HASH;
1416 			break;
1417 
1418 		case TCP_V6_FLOW:
1419 			ndc->l4_hash &= ~HV_TCP6_L4HASH;
1420 			break;
1421 
1422 		case UDP_V4_FLOW:
1423 			ndc->l4_hash &= ~HV_UDP4_L4HASH;
1424 			break;
1425 
1426 		case UDP_V6_FLOW:
1427 			ndc->l4_hash &= ~HV_UDP6_L4HASH;
1428 			break;
1429 
1430 		default:
1431 			return -EOPNOTSUPP;
1432 		}
1433 
1434 		return 0;
1435 	}
1436 
1437 	return -EOPNOTSUPP;
1438 }
1439 
1440 static int
1441 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1442 {
1443 	struct net_device_context *ndc = netdev_priv(ndev);
1444 
1445 	if (info->cmd == ETHTOOL_SRXFH)
1446 		return netvsc_set_rss_hash_opts(ndc, info);
1447 
1448 	return -EOPNOTSUPP;
1449 }
1450 
1451 #ifdef CONFIG_NET_POLL_CONTROLLER
1452 static void netvsc_poll_controller(struct net_device *dev)
1453 {
1454 	struct net_device_context *ndc = netdev_priv(dev);
1455 	struct netvsc_device *ndev;
1456 	int i;
1457 
1458 	rcu_read_lock();
1459 	ndev = rcu_dereference(ndc->nvdev);
1460 	if (ndev) {
1461 		for (i = 0; i < ndev->num_chn; i++) {
1462 			struct netvsc_channel *nvchan = &ndev->chan_table[i];
1463 
1464 			napi_schedule(&nvchan->napi);
1465 		}
1466 	}
1467 	rcu_read_unlock();
1468 }
1469 #endif
1470 
1471 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1472 {
1473 	return NETVSC_HASH_KEYLEN;
1474 }
1475 
1476 static u32 netvsc_rss_indir_size(struct net_device *dev)
1477 {
1478 	return ITAB_NUM;
1479 }
1480 
1481 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1482 			   u8 *hfunc)
1483 {
1484 	struct net_device_context *ndc = netdev_priv(dev);
1485 	struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1486 	struct rndis_device *rndis_dev;
1487 	int i;
1488 
1489 	if (!ndev)
1490 		return -ENODEV;
1491 
1492 	if (hfunc)
1493 		*hfunc = ETH_RSS_HASH_TOP;	/* Toeplitz */
1494 
1495 	rndis_dev = ndev->extension;
1496 	if (indir) {
1497 		for (i = 0; i < ITAB_NUM; i++)
1498 			indir[i] = rndis_dev->rx_table[i];
1499 	}
1500 
1501 	if (key)
1502 		memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1503 
1504 	return 0;
1505 }
1506 
1507 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1508 			   const u8 *key, const u8 hfunc)
1509 {
1510 	struct net_device_context *ndc = netdev_priv(dev);
1511 	struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1512 	struct rndis_device *rndis_dev;
1513 	int i;
1514 
1515 	if (!ndev)
1516 		return -ENODEV;
1517 
1518 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1519 		return -EOPNOTSUPP;
1520 
1521 	rndis_dev = ndev->extension;
1522 	if (indir) {
1523 		for (i = 0; i < ITAB_NUM; i++)
1524 			if (indir[i] >= ndev->num_chn)
1525 				return -EINVAL;
1526 
1527 		for (i = 0; i < ITAB_NUM; i++)
1528 			rndis_dev->rx_table[i] = indir[i];
1529 	}
1530 
1531 	if (!key) {
1532 		if (!indir)
1533 			return 0;
1534 
1535 		key = rndis_dev->rss_key;
1536 	}
1537 
1538 	return rndis_filter_set_rss_param(rndis_dev, key);
1539 }
1540 
1541 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1542  * It does have pre-allocated receive area which is divided into sections.
1543  */
1544 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1545 				   struct ethtool_ringparam *ring)
1546 {
1547 	u32 max_buf_size;
1548 
1549 	ring->rx_pending = nvdev->recv_section_cnt;
1550 	ring->tx_pending = nvdev->send_section_cnt;
1551 
1552 	if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1553 		max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1554 	else
1555 		max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1556 
1557 	ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1558 	ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1559 		/ nvdev->send_section_size;
1560 }
1561 
1562 static void netvsc_get_ringparam(struct net_device *ndev,
1563 				 struct ethtool_ringparam *ring)
1564 {
1565 	struct net_device_context *ndevctx = netdev_priv(ndev);
1566 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1567 
1568 	if (!nvdev)
1569 		return;
1570 
1571 	__netvsc_get_ringparam(nvdev, ring);
1572 }
1573 
1574 static int netvsc_set_ringparam(struct net_device *ndev,
1575 				struct ethtool_ringparam *ring)
1576 {
1577 	struct net_device_context *ndevctx = netdev_priv(ndev);
1578 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1579 	struct netvsc_device_info device_info;
1580 	struct ethtool_ringparam orig;
1581 	u32 new_tx, new_rx;
1582 	int ret = 0;
1583 
1584 	if (!nvdev || nvdev->destroy)
1585 		return -ENODEV;
1586 
1587 	memset(&orig, 0, sizeof(orig));
1588 	__netvsc_get_ringparam(nvdev, &orig);
1589 
1590 	new_tx = clamp_t(u32, ring->tx_pending,
1591 			 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1592 	new_rx = clamp_t(u32, ring->rx_pending,
1593 			 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1594 
1595 	if (new_tx == orig.tx_pending &&
1596 	    new_rx == orig.rx_pending)
1597 		return 0;	 /* no change */
1598 
1599 	memset(&device_info, 0, sizeof(device_info));
1600 	device_info.num_chn = nvdev->num_chn;
1601 	device_info.send_sections = new_tx;
1602 	device_info.send_section_size = nvdev->send_section_size;
1603 	device_info.recv_sections = new_rx;
1604 	device_info.recv_section_size = nvdev->recv_section_size;
1605 
1606 	ret = netvsc_detach(ndev, nvdev);
1607 	if (ret)
1608 		return ret;
1609 
1610 	ret = netvsc_attach(ndev, &device_info);
1611 	if (ret) {
1612 		device_info.send_sections = orig.tx_pending;
1613 		device_info.recv_sections = orig.rx_pending;
1614 
1615 		if (netvsc_attach(ndev, &device_info))
1616 			netdev_err(ndev, "restoring ringparam failed");
1617 	}
1618 
1619 	return ret;
1620 }
1621 
1622 static u32 netvsc_get_msglevel(struct net_device *ndev)
1623 {
1624 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1625 
1626 	return ndev_ctx->msg_enable;
1627 }
1628 
1629 static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
1630 {
1631 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1632 
1633 	ndev_ctx->msg_enable = val;
1634 }
1635 
1636 static const struct ethtool_ops ethtool_ops = {
1637 	.get_drvinfo	= netvsc_get_drvinfo,
1638 	.get_msglevel	= netvsc_get_msglevel,
1639 	.set_msglevel	= netvsc_set_msglevel,
1640 	.get_link	= ethtool_op_get_link,
1641 	.get_ethtool_stats = netvsc_get_ethtool_stats,
1642 	.get_sset_count = netvsc_get_sset_count,
1643 	.get_strings	= netvsc_get_strings,
1644 	.get_channels   = netvsc_get_channels,
1645 	.set_channels   = netvsc_set_channels,
1646 	.get_ts_info	= ethtool_op_get_ts_info,
1647 	.get_rxnfc	= netvsc_get_rxnfc,
1648 	.set_rxnfc	= netvsc_set_rxnfc,
1649 	.get_rxfh_key_size = netvsc_get_rxfh_key_size,
1650 	.get_rxfh_indir_size = netvsc_rss_indir_size,
1651 	.get_rxfh	= netvsc_get_rxfh,
1652 	.set_rxfh	= netvsc_set_rxfh,
1653 	.get_link_ksettings = netvsc_get_link_ksettings,
1654 	.set_link_ksettings = netvsc_set_link_ksettings,
1655 	.get_ringparam	= netvsc_get_ringparam,
1656 	.set_ringparam	= netvsc_set_ringparam,
1657 };
1658 
1659 static const struct net_device_ops device_ops = {
1660 	.ndo_open =			netvsc_open,
1661 	.ndo_stop =			netvsc_close,
1662 	.ndo_start_xmit =		netvsc_start_xmit,
1663 	.ndo_change_rx_flags =		netvsc_change_rx_flags,
1664 	.ndo_set_rx_mode =		netvsc_set_rx_mode,
1665 	.ndo_change_mtu =		netvsc_change_mtu,
1666 	.ndo_validate_addr =		eth_validate_addr,
1667 	.ndo_set_mac_address =		netvsc_set_mac_addr,
1668 	.ndo_select_queue =		netvsc_select_queue,
1669 	.ndo_get_stats64 =		netvsc_get_stats64,
1670 #ifdef CONFIG_NET_POLL_CONTROLLER
1671 	.ndo_poll_controller =		netvsc_poll_controller,
1672 #endif
1673 };
1674 
1675 /*
1676  * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1677  * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1678  * present send GARP packet to network peers with netif_notify_peers().
1679  */
1680 static void netvsc_link_change(struct work_struct *w)
1681 {
1682 	struct net_device_context *ndev_ctx =
1683 		container_of(w, struct net_device_context, dwork.work);
1684 	struct hv_device *device_obj = ndev_ctx->device_ctx;
1685 	struct net_device *net = hv_get_drvdata(device_obj);
1686 	struct netvsc_device *net_device;
1687 	struct rndis_device *rdev;
1688 	struct netvsc_reconfig *event = NULL;
1689 	bool notify = false, reschedule = false;
1690 	unsigned long flags, next_reconfig, delay;
1691 
1692 	/* if changes are happening, comeback later */
1693 	if (!rtnl_trylock()) {
1694 		schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1695 		return;
1696 	}
1697 
1698 	net_device = rtnl_dereference(ndev_ctx->nvdev);
1699 	if (!net_device)
1700 		goto out_unlock;
1701 
1702 	rdev = net_device->extension;
1703 
1704 	next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1705 	if (time_is_after_jiffies(next_reconfig)) {
1706 		/* link_watch only sends one notification with current state
1707 		 * per second, avoid doing reconfig more frequently. Handle
1708 		 * wrap around.
1709 		 */
1710 		delay = next_reconfig - jiffies;
1711 		delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1712 		schedule_delayed_work(&ndev_ctx->dwork, delay);
1713 		goto out_unlock;
1714 	}
1715 	ndev_ctx->last_reconfig = jiffies;
1716 
1717 	spin_lock_irqsave(&ndev_ctx->lock, flags);
1718 	if (!list_empty(&ndev_ctx->reconfig_events)) {
1719 		event = list_first_entry(&ndev_ctx->reconfig_events,
1720 					 struct netvsc_reconfig, list);
1721 		list_del(&event->list);
1722 		reschedule = !list_empty(&ndev_ctx->reconfig_events);
1723 	}
1724 	spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1725 
1726 	if (!event)
1727 		goto out_unlock;
1728 
1729 	switch (event->event) {
1730 		/* Only the following events are possible due to the check in
1731 		 * netvsc_linkstatus_callback()
1732 		 */
1733 	case RNDIS_STATUS_MEDIA_CONNECT:
1734 		if (rdev->link_state) {
1735 			rdev->link_state = false;
1736 			netif_carrier_on(net);
1737 			netif_tx_wake_all_queues(net);
1738 		} else {
1739 			notify = true;
1740 		}
1741 		kfree(event);
1742 		break;
1743 	case RNDIS_STATUS_MEDIA_DISCONNECT:
1744 		if (!rdev->link_state) {
1745 			rdev->link_state = true;
1746 			netif_carrier_off(net);
1747 			netif_tx_stop_all_queues(net);
1748 		}
1749 		kfree(event);
1750 		break;
1751 	case RNDIS_STATUS_NETWORK_CHANGE:
1752 		/* Only makes sense if carrier is present */
1753 		if (!rdev->link_state) {
1754 			rdev->link_state = true;
1755 			netif_carrier_off(net);
1756 			netif_tx_stop_all_queues(net);
1757 			event->event = RNDIS_STATUS_MEDIA_CONNECT;
1758 			spin_lock_irqsave(&ndev_ctx->lock, flags);
1759 			list_add(&event->list, &ndev_ctx->reconfig_events);
1760 			spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1761 			reschedule = true;
1762 		}
1763 		break;
1764 	}
1765 
1766 	rtnl_unlock();
1767 
1768 	if (notify)
1769 		netdev_notify_peers(net);
1770 
1771 	/* link_watch only sends one notification with current state per
1772 	 * second, handle next reconfig event in 2 seconds.
1773 	 */
1774 	if (reschedule)
1775 		schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1776 
1777 	return;
1778 
1779 out_unlock:
1780 	rtnl_unlock();
1781 }
1782 
1783 /* Called when VF is injecting data into network stack.
1784  * Change the associated network device from VF to netvsc.
1785  * note: already called with rcu_read_lock
1786  */
1787 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1788 {
1789 	struct sk_buff *skb = *pskb;
1790 	struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1791 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1792 	struct netvsc_vf_pcpu_stats *pcpu_stats
1793 		 = this_cpu_ptr(ndev_ctx->vf_stats);
1794 
1795 	skb->dev = ndev;
1796 
1797 	u64_stats_update_begin(&pcpu_stats->syncp);
1798 	pcpu_stats->rx_packets++;
1799 	pcpu_stats->rx_bytes += skb->len;
1800 	u64_stats_update_end(&pcpu_stats->syncp);
1801 
1802 	return RX_HANDLER_ANOTHER;
1803 }
1804 
1805 static void __netvsc_vf_setup(struct net_device *ndev,
1806 			      struct net_device *vf_netdev)
1807 {
1808 	int ret;
1809 
1810 	/* Align MTU of VF with master */
1811 	ret = dev_set_mtu(vf_netdev, ndev->mtu);
1812 	if (ret)
1813 		netdev_warn(vf_netdev,
1814 			    "unable to change mtu to %u\n", ndev->mtu);
1815 
1816 	/* set multicast etc flags on VF */
1817 	dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE);
1818 
1819 	/* sync address list from ndev to VF */
1820 	netif_addr_lock_bh(ndev);
1821 	dev_uc_sync(vf_netdev, ndev);
1822 	dev_mc_sync(vf_netdev, ndev);
1823 	netif_addr_unlock_bh(ndev);
1824 
1825 	if (netif_running(ndev)) {
1826 		ret = dev_open(vf_netdev);
1827 		if (ret)
1828 			netdev_warn(vf_netdev,
1829 				    "unable to open: %d\n", ret);
1830 	}
1831 }
1832 
1833 /* Setup VF as slave of the synthetic device.
1834  * Runs in workqueue to avoid recursion in netlink callbacks.
1835  */
1836 static void netvsc_vf_setup(struct work_struct *w)
1837 {
1838 	struct net_device_context *ndev_ctx
1839 		= container_of(w, struct net_device_context, vf_takeover.work);
1840 	struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1841 	struct net_device *vf_netdev;
1842 
1843 	if (!rtnl_trylock()) {
1844 		schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
1845 		return;
1846 	}
1847 
1848 	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1849 	if (vf_netdev)
1850 		__netvsc_vf_setup(ndev, vf_netdev);
1851 
1852 	rtnl_unlock();
1853 }
1854 
1855 static int netvsc_pre_register_vf(struct net_device *vf_netdev,
1856 				  struct net_device *ndev)
1857 {
1858 	struct net_device_context *net_device_ctx;
1859 	struct netvsc_device *netvsc_dev;
1860 
1861 	net_device_ctx = netdev_priv(ndev);
1862 	netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1863 	if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1864 		return -ENODEV;
1865 
1866 	return 0;
1867 }
1868 
1869 static int netvsc_register_vf(struct net_device *vf_netdev,
1870 			      struct net_device *ndev)
1871 {
1872 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1873 
1874 	/* set slave flag before open to prevent IPv6 addrconf */
1875 	vf_netdev->flags |= IFF_SLAVE;
1876 
1877 	schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1878 
1879 	call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1880 
1881 	netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1882 
1883 	dev_hold(vf_netdev);
1884 	rcu_assign_pointer(ndev_ctx->vf_netdev, vf_netdev);
1885 
1886 	return 0;
1887 }
1888 
1889 /* VF up/down change detected, schedule to change data path */
1890 static int netvsc_vf_changed(struct net_device *vf_netdev,
1891 			     struct net_device *ndev)
1892 {
1893 	struct net_device_context *net_device_ctx;
1894 	struct netvsc_device *netvsc_dev;
1895 	bool vf_is_up = netif_running(vf_netdev);
1896 
1897 	net_device_ctx = netdev_priv(ndev);
1898 	netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1899 	if (!netvsc_dev)
1900 		return -ENODEV;
1901 
1902 	netvsc_switch_datapath(ndev, vf_is_up);
1903 	netdev_info(ndev, "Data path switched %s VF: %s\n",
1904 		    vf_is_up ? "to" : "from", vf_netdev->name);
1905 
1906 	return 0;
1907 }
1908 
1909 static int netvsc_pre_unregister_vf(struct net_device *vf_netdev,
1910 				    struct net_device *ndev)
1911 {
1912 	struct net_device_context *net_device_ctx;
1913 
1914 	net_device_ctx = netdev_priv(ndev);
1915 	cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
1916 
1917 	return 0;
1918 }
1919 
1920 static int netvsc_unregister_vf(struct net_device *vf_netdev,
1921 				struct net_device *ndev)
1922 {
1923 	struct net_device_context *net_device_ctx;
1924 
1925 	net_device_ctx = netdev_priv(ndev);
1926 
1927 	netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1928 
1929 	RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1930 	dev_put(vf_netdev);
1931 
1932 	return 0;
1933 }
1934 
1935 static struct failover_ops netvsc_failover_ops = {
1936 	.slave_pre_register	= netvsc_pre_register_vf,
1937 	.slave_register		= netvsc_register_vf,
1938 	.slave_pre_unregister	= netvsc_pre_unregister_vf,
1939 	.slave_unregister	= netvsc_unregister_vf,
1940 	.slave_link_change	= netvsc_vf_changed,
1941 	.slave_handle_frame	= netvsc_vf_handle_frame,
1942 };
1943 
1944 static int netvsc_probe(struct hv_device *dev,
1945 			const struct hv_vmbus_device_id *dev_id)
1946 {
1947 	struct net_device *net = NULL;
1948 	struct net_device_context *net_device_ctx;
1949 	struct netvsc_device_info device_info;
1950 	struct netvsc_device *nvdev;
1951 	int ret = -ENOMEM;
1952 
1953 	net = alloc_etherdev_mq(sizeof(struct net_device_context),
1954 				VRSS_CHANNEL_MAX);
1955 	if (!net)
1956 		goto no_net;
1957 
1958 	netif_carrier_off(net);
1959 
1960 	netvsc_init_settings(net);
1961 
1962 	net_device_ctx = netdev_priv(net);
1963 	net_device_ctx->device_ctx = dev;
1964 	net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1965 	if (netif_msg_probe(net_device_ctx))
1966 		netdev_dbg(net, "netvsc msg_enable: %d\n",
1967 			   net_device_ctx->msg_enable);
1968 
1969 	hv_set_drvdata(dev, net);
1970 
1971 	INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1972 
1973 	spin_lock_init(&net_device_ctx->lock);
1974 	INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1975 	INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
1976 
1977 	net_device_ctx->vf_stats
1978 		= netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1979 	if (!net_device_ctx->vf_stats)
1980 		goto no_stats;
1981 
1982 	net->netdev_ops = &device_ops;
1983 	net->ethtool_ops = &ethtool_ops;
1984 	SET_NETDEV_DEV(net, &dev->device);
1985 
1986 	/* We always need headroom for rndis header */
1987 	net->needed_headroom = RNDIS_AND_PPI_SIZE;
1988 
1989 	/* Initialize the number of queues to be 1, we may change it if more
1990 	 * channels are offered later.
1991 	 */
1992 	netif_set_real_num_tx_queues(net, 1);
1993 	netif_set_real_num_rx_queues(net, 1);
1994 
1995 	/* Notify the netvsc driver of the new device */
1996 	memset(&device_info, 0, sizeof(device_info));
1997 	device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1998 	device_info.send_sections = NETVSC_DEFAULT_TX;
1999 	device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
2000 	device_info.recv_sections = NETVSC_DEFAULT_RX;
2001 	device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
2002 
2003 	nvdev = rndis_filter_device_add(dev, &device_info);
2004 	if (IS_ERR(nvdev)) {
2005 		ret = PTR_ERR(nvdev);
2006 		netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2007 		goto rndis_failed;
2008 	}
2009 
2010 	memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
2011 
2012 	/* hw_features computed in rndis_netdev_set_hwcaps() */
2013 	net->features = net->hw_features |
2014 		NETIF_F_HIGHDMA | NETIF_F_SG |
2015 		NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2016 	net->vlan_features = net->features;
2017 
2018 	netdev_lockdep_set_classes(net);
2019 
2020 	/* MTU range: 68 - 1500 or 65521 */
2021 	net->min_mtu = NETVSC_MTU_MIN;
2022 	if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2023 		net->max_mtu = NETVSC_MTU - ETH_HLEN;
2024 	else
2025 		net->max_mtu = ETH_DATA_LEN;
2026 
2027 	ret = register_netdev(net);
2028 	if (ret != 0) {
2029 		pr_err("Unable to register netdev.\n");
2030 		goto register_failed;
2031 	}
2032 
2033 	net_device_ctx->failover = failover_register(net, &netvsc_failover_ops);
2034 	if (IS_ERR(net_device_ctx->failover)) {
2035 		ret = PTR_ERR(net_device_ctx->failover);
2036 		goto err_failover;
2037 	}
2038 
2039 	return ret;
2040 
2041 err_failover:
2042 	unregister_netdev(net);
2043 register_failed:
2044 	rndis_filter_device_remove(dev, nvdev);
2045 rndis_failed:
2046 	free_percpu(net_device_ctx->vf_stats);
2047 no_stats:
2048 	hv_set_drvdata(dev, NULL);
2049 	free_netdev(net);
2050 no_net:
2051 	return ret;
2052 }
2053 
2054 static int netvsc_remove(struct hv_device *dev)
2055 {
2056 	struct net_device_context *ndev_ctx;
2057 	struct net_device *vf_netdev, *net;
2058 	struct netvsc_device *nvdev;
2059 
2060 	net = hv_get_drvdata(dev);
2061 	if (net == NULL) {
2062 		dev_err(&dev->device, "No net device to remove\n");
2063 		return 0;
2064 	}
2065 
2066 	ndev_ctx = netdev_priv(net);
2067 
2068 	cancel_delayed_work_sync(&ndev_ctx->dwork);
2069 
2070 	rcu_read_lock();
2071 	nvdev = rcu_dereference(ndev_ctx->nvdev);
2072 
2073 	if  (nvdev)
2074 		cancel_work_sync(&nvdev->subchan_work);
2075 
2076 	/*
2077 	 * Call to the vsc driver to let it know that the device is being
2078 	 * removed. Also blocks mtu and channel changes.
2079 	 */
2080 	rtnl_lock();
2081 	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2082 	if (vf_netdev)
2083 		failover_slave_unregister(vf_netdev);
2084 
2085 	if (nvdev)
2086 		rndis_filter_device_remove(dev, nvdev);
2087 
2088 	unregister_netdevice(net);
2089 
2090 	failover_unregister(ndev_ctx->failover);
2091 
2092 	rtnl_unlock();
2093 	rcu_read_unlock();
2094 
2095 	hv_set_drvdata(dev, NULL);
2096 
2097 	free_percpu(ndev_ctx->vf_stats);
2098 	free_netdev(net);
2099 	return 0;
2100 }
2101 
2102 static const struct hv_vmbus_device_id id_table[] = {
2103 	/* Network guid */
2104 	{ HV_NIC_GUID, },
2105 	{ },
2106 };
2107 
2108 MODULE_DEVICE_TABLE(vmbus, id_table);
2109 
2110 /* The one and only one */
2111 static struct  hv_driver netvsc_drv = {
2112 	.name = KBUILD_MODNAME,
2113 	.id_table = id_table,
2114 	.probe = netvsc_probe,
2115 	.remove = netvsc_remove,
2116 };
2117 
2118 static void __exit netvsc_drv_exit(void)
2119 {
2120 	vmbus_driver_unregister(&netvsc_drv);
2121 }
2122 
2123 static int __init netvsc_drv_init(void)
2124 {
2125 	int ret;
2126 
2127 	if (ring_size < RING_SIZE_MIN) {
2128 		ring_size = RING_SIZE_MIN;
2129 		pr_info("Increased ring_size to %u (min allowed)\n",
2130 			ring_size);
2131 	}
2132 	netvsc_ring_bytes = ring_size * PAGE_SIZE;
2133 
2134 	ret = vmbus_driver_register(&netvsc_drv);
2135 	if (ret)
2136 		return ret;
2137 
2138 	return 0;
2139 }
2140 
2141 MODULE_LICENSE("GPL");
2142 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2143 
2144 module_init(netvsc_drv_init);
2145 module_exit(netvsc_drv_exit);
2146