xref: /openbmc/linux/drivers/net/hyperv/netvsc_drv.c (revision b03afaa8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * Authors:
6  *   Haiyang Zhang <haiyangz@microsoft.com>
7  *   Hank Janssen  <hjanssen@microsoft.com>
8  */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/init.h>
12 #include <linux/atomic.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/device.h>
16 #include <linux/io.h>
17 #include <linux/delay.h>
18 #include <linux/netdevice.h>
19 #include <linux/inetdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/pci.h>
22 #include <linux/skbuff.h>
23 #include <linux/if_vlan.h>
24 #include <linux/in.h>
25 #include <linux/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/netpoll.h>
28 #include <linux/bpf.h>
29 
30 #include <net/arp.h>
31 #include <net/route.h>
32 #include <net/sock.h>
33 #include <net/pkt_sched.h>
34 #include <net/checksum.h>
35 #include <net/ip6_checksum.h>
36 
37 #include "hyperv_net.h"
38 
39 #define RING_SIZE_MIN	64
40 #define RETRY_US_LO	5000
41 #define RETRY_US_HI	10000
42 #define RETRY_MAX	2000	/* >10 sec */
43 
44 #define LINKCHANGE_INT (2 * HZ)
45 #define VF_TAKEOVER_INT (HZ / 10)
46 
47 static unsigned int ring_size __ro_after_init = 128;
48 module_param(ring_size, uint, 0444);
49 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
50 unsigned int netvsc_ring_bytes __ro_after_init;
51 
52 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
53 				NETIF_MSG_LINK | NETIF_MSG_IFUP |
54 				NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
55 				NETIF_MSG_TX_ERR;
56 
57 static int debug = -1;
58 module_param(debug, int, 0444);
59 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
60 
61 static LIST_HEAD(netvsc_dev_list);
62 
63 static void netvsc_change_rx_flags(struct net_device *net, int change)
64 {
65 	struct net_device_context *ndev_ctx = netdev_priv(net);
66 	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
67 	int inc;
68 
69 	if (!vf_netdev)
70 		return;
71 
72 	if (change & IFF_PROMISC) {
73 		inc = (net->flags & IFF_PROMISC) ? 1 : -1;
74 		dev_set_promiscuity(vf_netdev, inc);
75 	}
76 
77 	if (change & IFF_ALLMULTI) {
78 		inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
79 		dev_set_allmulti(vf_netdev, inc);
80 	}
81 }
82 
83 static void netvsc_set_rx_mode(struct net_device *net)
84 {
85 	struct net_device_context *ndev_ctx = netdev_priv(net);
86 	struct net_device *vf_netdev;
87 	struct netvsc_device *nvdev;
88 
89 	rcu_read_lock();
90 	vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
91 	if (vf_netdev) {
92 		dev_uc_sync(vf_netdev, net);
93 		dev_mc_sync(vf_netdev, net);
94 	}
95 
96 	nvdev = rcu_dereference(ndev_ctx->nvdev);
97 	if (nvdev)
98 		rndis_filter_update(nvdev);
99 	rcu_read_unlock();
100 }
101 
102 static void netvsc_tx_enable(struct netvsc_device *nvscdev,
103 			     struct net_device *ndev)
104 {
105 	nvscdev->tx_disable = false;
106 	virt_wmb(); /* ensure queue wake up mechanism is on */
107 
108 	netif_tx_wake_all_queues(ndev);
109 }
110 
111 static int netvsc_open(struct net_device *net)
112 {
113 	struct net_device_context *ndev_ctx = netdev_priv(net);
114 	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
115 	struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
116 	struct rndis_device *rdev;
117 	int ret = 0;
118 
119 	netif_carrier_off(net);
120 
121 	/* Open up the device */
122 	ret = rndis_filter_open(nvdev);
123 	if (ret != 0) {
124 		netdev_err(net, "unable to open device (ret %d).\n", ret);
125 		return ret;
126 	}
127 
128 	rdev = nvdev->extension;
129 	if (!rdev->link_state) {
130 		netif_carrier_on(net);
131 		netvsc_tx_enable(nvdev, net);
132 	}
133 
134 	if (vf_netdev) {
135 		/* Setting synthetic device up transparently sets
136 		 * slave as up. If open fails, then slave will be
137 		 * still be offline (and not used).
138 		 */
139 		ret = dev_open(vf_netdev, NULL);
140 		if (ret)
141 			netdev_warn(net,
142 				    "unable to open slave: %s: %d\n",
143 				    vf_netdev->name, ret);
144 	}
145 	return 0;
146 }
147 
148 static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
149 {
150 	unsigned int retry = 0;
151 	int i;
152 
153 	/* Ensure pending bytes in ring are read */
154 	for (;;) {
155 		u32 aread = 0;
156 
157 		for (i = 0; i < nvdev->num_chn; i++) {
158 			struct vmbus_channel *chn
159 				= nvdev->chan_table[i].channel;
160 
161 			if (!chn)
162 				continue;
163 
164 			/* make sure receive not running now */
165 			napi_synchronize(&nvdev->chan_table[i].napi);
166 
167 			aread = hv_get_bytes_to_read(&chn->inbound);
168 			if (aread)
169 				break;
170 
171 			aread = hv_get_bytes_to_read(&chn->outbound);
172 			if (aread)
173 				break;
174 		}
175 
176 		if (aread == 0)
177 			return 0;
178 
179 		if (++retry > RETRY_MAX)
180 			return -ETIMEDOUT;
181 
182 		usleep_range(RETRY_US_LO, RETRY_US_HI);
183 	}
184 }
185 
186 static void netvsc_tx_disable(struct netvsc_device *nvscdev,
187 			      struct net_device *ndev)
188 {
189 	if (nvscdev) {
190 		nvscdev->tx_disable = true;
191 		virt_wmb(); /* ensure txq will not wake up after stop */
192 	}
193 
194 	netif_tx_disable(ndev);
195 }
196 
197 static int netvsc_close(struct net_device *net)
198 {
199 	struct net_device_context *net_device_ctx = netdev_priv(net);
200 	struct net_device *vf_netdev
201 		= rtnl_dereference(net_device_ctx->vf_netdev);
202 	struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
203 	int ret;
204 
205 	netvsc_tx_disable(nvdev, net);
206 
207 	/* No need to close rndis filter if it is removed already */
208 	if (!nvdev)
209 		return 0;
210 
211 	ret = rndis_filter_close(nvdev);
212 	if (ret != 0) {
213 		netdev_err(net, "unable to close device (ret %d).\n", ret);
214 		return ret;
215 	}
216 
217 	ret = netvsc_wait_until_empty(nvdev);
218 	if (ret)
219 		netdev_err(net, "Ring buffer not empty after closing rndis\n");
220 
221 	if (vf_netdev)
222 		dev_close(vf_netdev);
223 
224 	return ret;
225 }
226 
227 static inline void *init_ppi_data(struct rndis_message *msg,
228 				  u32 ppi_size, u32 pkt_type)
229 {
230 	struct rndis_packet *rndis_pkt = &msg->msg.pkt;
231 	struct rndis_per_packet_info *ppi;
232 
233 	rndis_pkt->data_offset += ppi_size;
234 	ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
235 		+ rndis_pkt->per_pkt_info_len;
236 
237 	ppi->size = ppi_size;
238 	ppi->type = pkt_type;
239 	ppi->internal = 0;
240 	ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
241 
242 	rndis_pkt->per_pkt_info_len += ppi_size;
243 
244 	return ppi + 1;
245 }
246 
247 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
248  * packets. We can use ethtool to change UDP hash level when necessary.
249  */
250 static inline u32 netvsc_get_hash(
251 	struct sk_buff *skb,
252 	const struct net_device_context *ndc)
253 {
254 	struct flow_keys flow;
255 	u32 hash, pkt_proto = 0;
256 	static u32 hashrnd __read_mostly;
257 
258 	net_get_random_once(&hashrnd, sizeof(hashrnd));
259 
260 	if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
261 		return 0;
262 
263 	switch (flow.basic.ip_proto) {
264 	case IPPROTO_TCP:
265 		if (flow.basic.n_proto == htons(ETH_P_IP))
266 			pkt_proto = HV_TCP4_L4HASH;
267 		else if (flow.basic.n_proto == htons(ETH_P_IPV6))
268 			pkt_proto = HV_TCP6_L4HASH;
269 
270 		break;
271 
272 	case IPPROTO_UDP:
273 		if (flow.basic.n_proto == htons(ETH_P_IP))
274 			pkt_proto = HV_UDP4_L4HASH;
275 		else if (flow.basic.n_proto == htons(ETH_P_IPV6))
276 			pkt_proto = HV_UDP6_L4HASH;
277 
278 		break;
279 	}
280 
281 	if (pkt_proto & ndc->l4_hash) {
282 		return skb_get_hash(skb);
283 	} else {
284 		if (flow.basic.n_proto == htons(ETH_P_IP))
285 			hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
286 		else if (flow.basic.n_proto == htons(ETH_P_IPV6))
287 			hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
288 		else
289 			return 0;
290 
291 		__skb_set_sw_hash(skb, hash, false);
292 	}
293 
294 	return hash;
295 }
296 
297 static inline int netvsc_get_tx_queue(struct net_device *ndev,
298 				      struct sk_buff *skb, int old_idx)
299 {
300 	const struct net_device_context *ndc = netdev_priv(ndev);
301 	struct sock *sk = skb->sk;
302 	int q_idx;
303 
304 	q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
305 			      (VRSS_SEND_TAB_SIZE - 1)];
306 
307 	/* If queue index changed record the new value */
308 	if (q_idx != old_idx &&
309 	    sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
310 		sk_tx_queue_set(sk, q_idx);
311 
312 	return q_idx;
313 }
314 
315 /*
316  * Select queue for transmit.
317  *
318  * If a valid queue has already been assigned, then use that.
319  * Otherwise compute tx queue based on hash and the send table.
320  *
321  * This is basically similar to default (netdev_pick_tx) with the added step
322  * of using the host send_table when no other queue has been assigned.
323  *
324  * TODO support XPS - but get_xps_queue not exported
325  */
326 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
327 {
328 	int q_idx = sk_tx_queue_get(skb->sk);
329 
330 	if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
331 		/* If forwarding a packet, we use the recorded queue when
332 		 * available for better cache locality.
333 		 */
334 		if (skb_rx_queue_recorded(skb))
335 			q_idx = skb_get_rx_queue(skb);
336 		else
337 			q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
338 	}
339 
340 	return q_idx;
341 }
342 
343 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
344 			       struct net_device *sb_dev)
345 {
346 	struct net_device_context *ndc = netdev_priv(ndev);
347 	struct net_device *vf_netdev;
348 	u16 txq;
349 
350 	rcu_read_lock();
351 	vf_netdev = rcu_dereference(ndc->vf_netdev);
352 	if (vf_netdev) {
353 		const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
354 
355 		if (vf_ops->ndo_select_queue)
356 			txq = vf_ops->ndo_select_queue(vf_netdev, skb, sb_dev);
357 		else
358 			txq = netdev_pick_tx(vf_netdev, skb, NULL);
359 
360 		/* Record the queue selected by VF so that it can be
361 		 * used for common case where VF has more queues than
362 		 * the synthetic device.
363 		 */
364 		qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
365 	} else {
366 		txq = netvsc_pick_tx(ndev, skb);
367 	}
368 	rcu_read_unlock();
369 
370 	while (unlikely(txq >= ndev->real_num_tx_queues))
371 		txq -= ndev->real_num_tx_queues;
372 
373 	return txq;
374 }
375 
376 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
377 		       struct hv_page_buffer *pb)
378 {
379 	int j = 0;
380 
381 	/* Deal with compound pages by ignoring unused part
382 	 * of the page.
383 	 */
384 	page += (offset >> PAGE_SHIFT);
385 	offset &= ~PAGE_MASK;
386 
387 	while (len > 0) {
388 		unsigned long bytes;
389 
390 		bytes = PAGE_SIZE - offset;
391 		if (bytes > len)
392 			bytes = len;
393 		pb[j].pfn = page_to_pfn(page);
394 		pb[j].offset = offset;
395 		pb[j].len = bytes;
396 
397 		offset += bytes;
398 		len -= bytes;
399 
400 		if (offset == PAGE_SIZE && len) {
401 			page++;
402 			offset = 0;
403 			j++;
404 		}
405 	}
406 
407 	return j + 1;
408 }
409 
410 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
411 			   struct hv_netvsc_packet *packet,
412 			   struct hv_page_buffer *pb)
413 {
414 	u32 slots_used = 0;
415 	char *data = skb->data;
416 	int frags = skb_shinfo(skb)->nr_frags;
417 	int i;
418 
419 	/* The packet is laid out thus:
420 	 * 1. hdr: RNDIS header and PPI
421 	 * 2. skb linear data
422 	 * 3. skb fragment data
423 	 */
424 	slots_used += fill_pg_buf(virt_to_page(hdr),
425 				  offset_in_page(hdr),
426 				  len, &pb[slots_used]);
427 
428 	packet->rmsg_size = len;
429 	packet->rmsg_pgcnt = slots_used;
430 
431 	slots_used += fill_pg_buf(virt_to_page(data),
432 				offset_in_page(data),
433 				skb_headlen(skb), &pb[slots_used]);
434 
435 	for (i = 0; i < frags; i++) {
436 		skb_frag_t *frag = skb_shinfo(skb)->frags + i;
437 
438 		slots_used += fill_pg_buf(skb_frag_page(frag),
439 					skb_frag_off(frag),
440 					skb_frag_size(frag), &pb[slots_used]);
441 	}
442 	return slots_used;
443 }
444 
445 static int count_skb_frag_slots(struct sk_buff *skb)
446 {
447 	int i, frags = skb_shinfo(skb)->nr_frags;
448 	int pages = 0;
449 
450 	for (i = 0; i < frags; i++) {
451 		skb_frag_t *frag = skb_shinfo(skb)->frags + i;
452 		unsigned long size = skb_frag_size(frag);
453 		unsigned long offset = skb_frag_off(frag);
454 
455 		/* Skip unused frames from start of page */
456 		offset &= ~PAGE_MASK;
457 		pages += PFN_UP(offset + size);
458 	}
459 	return pages;
460 }
461 
462 static int netvsc_get_slots(struct sk_buff *skb)
463 {
464 	char *data = skb->data;
465 	unsigned int offset = offset_in_page(data);
466 	unsigned int len = skb_headlen(skb);
467 	int slots;
468 	int frag_slots;
469 
470 	slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
471 	frag_slots = count_skb_frag_slots(skb);
472 	return slots + frag_slots;
473 }
474 
475 static u32 net_checksum_info(struct sk_buff *skb)
476 {
477 	if (skb->protocol == htons(ETH_P_IP)) {
478 		struct iphdr *ip = ip_hdr(skb);
479 
480 		if (ip->protocol == IPPROTO_TCP)
481 			return TRANSPORT_INFO_IPV4_TCP;
482 		else if (ip->protocol == IPPROTO_UDP)
483 			return TRANSPORT_INFO_IPV4_UDP;
484 	} else {
485 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
486 
487 		if (ip6->nexthdr == IPPROTO_TCP)
488 			return TRANSPORT_INFO_IPV6_TCP;
489 		else if (ip6->nexthdr == IPPROTO_UDP)
490 			return TRANSPORT_INFO_IPV6_UDP;
491 	}
492 
493 	return TRANSPORT_INFO_NOT_IP;
494 }
495 
496 /* Send skb on the slave VF device. */
497 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
498 			  struct sk_buff *skb)
499 {
500 	struct net_device_context *ndev_ctx = netdev_priv(net);
501 	unsigned int len = skb->len;
502 	int rc;
503 
504 	skb->dev = vf_netdev;
505 	skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
506 
507 	rc = dev_queue_xmit(skb);
508 	if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
509 		struct netvsc_vf_pcpu_stats *pcpu_stats
510 			= this_cpu_ptr(ndev_ctx->vf_stats);
511 
512 		u64_stats_update_begin(&pcpu_stats->syncp);
513 		pcpu_stats->tx_packets++;
514 		pcpu_stats->tx_bytes += len;
515 		u64_stats_update_end(&pcpu_stats->syncp);
516 	} else {
517 		this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
518 	}
519 
520 	return rc;
521 }
522 
523 static int netvsc_xmit(struct sk_buff *skb, struct net_device *net, bool xdp_tx)
524 {
525 	struct net_device_context *net_device_ctx = netdev_priv(net);
526 	struct hv_netvsc_packet *packet = NULL;
527 	int ret;
528 	unsigned int num_data_pgs;
529 	struct rndis_message *rndis_msg;
530 	struct net_device *vf_netdev;
531 	u32 rndis_msg_size;
532 	u32 hash;
533 	struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
534 
535 	/* if VF is present and up then redirect packets
536 	 * already called with rcu_read_lock_bh
537 	 */
538 	vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
539 	if (vf_netdev && netif_running(vf_netdev) &&
540 	    !netpoll_tx_running(net))
541 		return netvsc_vf_xmit(net, vf_netdev, skb);
542 
543 	/* We will atmost need two pages to describe the rndis
544 	 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
545 	 * of pages in a single packet. If skb is scattered around
546 	 * more pages we try linearizing it.
547 	 */
548 
549 	num_data_pgs = netvsc_get_slots(skb) + 2;
550 
551 	if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
552 		++net_device_ctx->eth_stats.tx_scattered;
553 
554 		if (skb_linearize(skb))
555 			goto no_memory;
556 
557 		num_data_pgs = netvsc_get_slots(skb) + 2;
558 		if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
559 			++net_device_ctx->eth_stats.tx_too_big;
560 			goto drop;
561 		}
562 	}
563 
564 	/*
565 	 * Place the rndis header in the skb head room and
566 	 * the skb->cb will be used for hv_netvsc_packet
567 	 * structure.
568 	 */
569 	ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
570 	if (ret)
571 		goto no_memory;
572 
573 	/* Use the skb control buffer for building up the packet */
574 	BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
575 			sizeof_field(struct sk_buff, cb));
576 	packet = (struct hv_netvsc_packet *)skb->cb;
577 
578 	packet->q_idx = skb_get_queue_mapping(skb);
579 
580 	packet->total_data_buflen = skb->len;
581 	packet->total_bytes = skb->len;
582 	packet->total_packets = 1;
583 
584 	rndis_msg = (struct rndis_message *)skb->head;
585 
586 	/* Add the rndis header */
587 	rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
588 	rndis_msg->msg_len = packet->total_data_buflen;
589 
590 	rndis_msg->msg.pkt = (struct rndis_packet) {
591 		.data_offset = sizeof(struct rndis_packet),
592 		.data_len = packet->total_data_buflen,
593 		.per_pkt_info_offset = sizeof(struct rndis_packet),
594 	};
595 
596 	rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
597 
598 	hash = skb_get_hash_raw(skb);
599 	if (hash != 0 && net->real_num_tx_queues > 1) {
600 		u32 *hash_info;
601 
602 		rndis_msg_size += NDIS_HASH_PPI_SIZE;
603 		hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
604 					  NBL_HASH_VALUE);
605 		*hash_info = hash;
606 	}
607 
608 	/* When using AF_PACKET we need to drop VLAN header from
609 	 * the frame and update the SKB to allow the HOST OS
610 	 * to transmit the 802.1Q packet
611 	 */
612 	if (skb->protocol == htons(ETH_P_8021Q)) {
613 		u16 vlan_tci;
614 
615 		skb_reset_mac_header(skb);
616 		if (eth_type_vlan(eth_hdr(skb)->h_proto)) {
617 			if (unlikely(__skb_vlan_pop(skb, &vlan_tci) != 0)) {
618 				++net_device_ctx->eth_stats.vlan_error;
619 				goto drop;
620 			}
621 
622 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
623 			/* Update the NDIS header pkt lengths */
624 			packet->total_data_buflen -= VLAN_HLEN;
625 			packet->total_bytes -= VLAN_HLEN;
626 			rndis_msg->msg_len = packet->total_data_buflen;
627 			rndis_msg->msg.pkt.data_len = packet->total_data_buflen;
628 		}
629 	}
630 
631 	if (skb_vlan_tag_present(skb)) {
632 		struct ndis_pkt_8021q_info *vlan;
633 
634 		rndis_msg_size += NDIS_VLAN_PPI_SIZE;
635 		vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
636 				     IEEE_8021Q_INFO);
637 
638 		vlan->value = 0;
639 		vlan->vlanid = skb_vlan_tag_get_id(skb);
640 		vlan->cfi = skb_vlan_tag_get_cfi(skb);
641 		vlan->pri = skb_vlan_tag_get_prio(skb);
642 	}
643 
644 	if (skb_is_gso(skb)) {
645 		struct ndis_tcp_lso_info *lso_info;
646 
647 		rndis_msg_size += NDIS_LSO_PPI_SIZE;
648 		lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
649 					 TCP_LARGESEND_PKTINFO);
650 
651 		lso_info->value = 0;
652 		lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
653 		if (skb->protocol == htons(ETH_P_IP)) {
654 			lso_info->lso_v2_transmit.ip_version =
655 				NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
656 			ip_hdr(skb)->tot_len = 0;
657 			ip_hdr(skb)->check = 0;
658 			tcp_hdr(skb)->check =
659 				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
660 						   ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
661 		} else {
662 			lso_info->lso_v2_transmit.ip_version =
663 				NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
664 			tcp_v6_gso_csum_prep(skb);
665 		}
666 		lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
667 		lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
668 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
669 		if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
670 			struct ndis_tcp_ip_checksum_info *csum_info;
671 
672 			rndis_msg_size += NDIS_CSUM_PPI_SIZE;
673 			csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
674 						  TCPIP_CHKSUM_PKTINFO);
675 
676 			csum_info->value = 0;
677 			csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
678 
679 			if (skb->protocol == htons(ETH_P_IP)) {
680 				csum_info->transmit.is_ipv4 = 1;
681 
682 				if (ip_hdr(skb)->protocol == IPPROTO_TCP)
683 					csum_info->transmit.tcp_checksum = 1;
684 				else
685 					csum_info->transmit.udp_checksum = 1;
686 			} else {
687 				csum_info->transmit.is_ipv6 = 1;
688 
689 				if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
690 					csum_info->transmit.tcp_checksum = 1;
691 				else
692 					csum_info->transmit.udp_checksum = 1;
693 			}
694 		} else {
695 			/* Can't do offload of this type of checksum */
696 			if (skb_checksum_help(skb))
697 				goto drop;
698 		}
699 	}
700 
701 	/* Start filling in the page buffers with the rndis hdr */
702 	rndis_msg->msg_len += rndis_msg_size;
703 	packet->total_data_buflen = rndis_msg->msg_len;
704 	packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
705 					       skb, packet, pb);
706 
707 	/* timestamp packet in software */
708 	skb_tx_timestamp(skb);
709 
710 	ret = netvsc_send(net, packet, rndis_msg, pb, skb, xdp_tx);
711 	if (likely(ret == 0))
712 		return NETDEV_TX_OK;
713 
714 	if (ret == -EAGAIN) {
715 		++net_device_ctx->eth_stats.tx_busy;
716 		return NETDEV_TX_BUSY;
717 	}
718 
719 	if (ret == -ENOSPC)
720 		++net_device_ctx->eth_stats.tx_no_space;
721 
722 drop:
723 	dev_kfree_skb_any(skb);
724 	net->stats.tx_dropped++;
725 
726 	return NETDEV_TX_OK;
727 
728 no_memory:
729 	++net_device_ctx->eth_stats.tx_no_memory;
730 	goto drop;
731 }
732 
733 static netdev_tx_t netvsc_start_xmit(struct sk_buff *skb,
734 				     struct net_device *ndev)
735 {
736 	return netvsc_xmit(skb, ndev, false);
737 }
738 
739 /*
740  * netvsc_linkstatus_callback - Link up/down notification
741  */
742 void netvsc_linkstatus_callback(struct net_device *net,
743 				struct rndis_message *resp)
744 {
745 	struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
746 	struct net_device_context *ndev_ctx = netdev_priv(net);
747 	struct netvsc_reconfig *event;
748 	unsigned long flags;
749 
750 	/* Update the physical link speed when changing to another vSwitch */
751 	if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
752 		u32 speed;
753 
754 		speed = *(u32 *)((void *)indicate
755 				 + indicate->status_buf_offset) / 10000;
756 		ndev_ctx->speed = speed;
757 		return;
758 	}
759 
760 	/* Handle these link change statuses below */
761 	if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
762 	    indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
763 	    indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
764 		return;
765 
766 	if (net->reg_state != NETREG_REGISTERED)
767 		return;
768 
769 	event = kzalloc(sizeof(*event), GFP_ATOMIC);
770 	if (!event)
771 		return;
772 	event->event = indicate->status;
773 
774 	spin_lock_irqsave(&ndev_ctx->lock, flags);
775 	list_add_tail(&event->list, &ndev_ctx->reconfig_events);
776 	spin_unlock_irqrestore(&ndev_ctx->lock, flags);
777 
778 	schedule_delayed_work(&ndev_ctx->dwork, 0);
779 }
780 
781 static void netvsc_xdp_xmit(struct sk_buff *skb, struct net_device *ndev)
782 {
783 	int rc;
784 
785 	skb->queue_mapping = skb_get_rx_queue(skb);
786 	__skb_push(skb, ETH_HLEN);
787 
788 	rc = netvsc_xmit(skb, ndev, true);
789 
790 	if (dev_xmit_complete(rc))
791 		return;
792 
793 	dev_kfree_skb_any(skb);
794 	ndev->stats.tx_dropped++;
795 }
796 
797 static void netvsc_comp_ipcsum(struct sk_buff *skb)
798 {
799 	struct iphdr *iph = (struct iphdr *)skb->data;
800 
801 	iph->check = 0;
802 	iph->check = ip_fast_csum(iph, iph->ihl);
803 }
804 
805 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
806 					     struct netvsc_channel *nvchan,
807 					     struct xdp_buff *xdp)
808 {
809 	struct napi_struct *napi = &nvchan->napi;
810 	const struct ndis_pkt_8021q_info *vlan = nvchan->rsc.vlan;
811 	const struct ndis_tcp_ip_checksum_info *csum_info =
812 						nvchan->rsc.csum_info;
813 	const u32 *hash_info = nvchan->rsc.hash_info;
814 	struct sk_buff *skb;
815 	void *xbuf = xdp->data_hard_start;
816 	int i;
817 
818 	if (xbuf) {
819 		unsigned int hdroom = xdp->data - xdp->data_hard_start;
820 		unsigned int xlen = xdp->data_end - xdp->data;
821 		unsigned int frag_size = xdp->frame_sz;
822 
823 		skb = build_skb(xbuf, frag_size);
824 
825 		if (!skb) {
826 			__free_page(virt_to_page(xbuf));
827 			return NULL;
828 		}
829 
830 		skb_reserve(skb, hdroom);
831 		skb_put(skb, xlen);
832 		skb->dev = napi->dev;
833 	} else {
834 		skb = napi_alloc_skb(napi, nvchan->rsc.pktlen);
835 
836 		if (!skb)
837 			return NULL;
838 
839 		/* Copy to skb. This copy is needed here since the memory
840 		 * pointed by hv_netvsc_packet cannot be deallocated.
841 		 */
842 		for (i = 0; i < nvchan->rsc.cnt; i++)
843 			skb_put_data(skb, nvchan->rsc.data[i],
844 				     nvchan->rsc.len[i]);
845 	}
846 
847 	skb->protocol = eth_type_trans(skb, net);
848 
849 	/* skb is already created with CHECKSUM_NONE */
850 	skb_checksum_none_assert(skb);
851 
852 	/* Incoming packets may have IP header checksum verified by the host.
853 	 * They may not have IP header checksum computed after coalescing.
854 	 * We compute it here if the flags are set, because on Linux, the IP
855 	 * checksum is always checked.
856 	 */
857 	if (csum_info && csum_info->receive.ip_checksum_value_invalid &&
858 	    csum_info->receive.ip_checksum_succeeded &&
859 	    skb->protocol == htons(ETH_P_IP))
860 		netvsc_comp_ipcsum(skb);
861 
862 	/* Do L4 checksum offload if enabled and present. */
863 	if (csum_info && (net->features & NETIF_F_RXCSUM)) {
864 		if (csum_info->receive.tcp_checksum_succeeded ||
865 		    csum_info->receive.udp_checksum_succeeded)
866 			skb->ip_summed = CHECKSUM_UNNECESSARY;
867 	}
868 
869 	if (hash_info && (net->features & NETIF_F_RXHASH))
870 		skb_set_hash(skb, *hash_info, PKT_HASH_TYPE_L4);
871 
872 	if (vlan) {
873 		u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT) |
874 			(vlan->cfi ? VLAN_CFI_MASK : 0);
875 
876 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
877 				       vlan_tci);
878 	}
879 
880 	return skb;
881 }
882 
883 /*
884  * netvsc_recv_callback -  Callback when we receive a packet from the
885  * "wire" on the specified device.
886  */
887 int netvsc_recv_callback(struct net_device *net,
888 			 struct netvsc_device *net_device,
889 			 struct netvsc_channel *nvchan)
890 {
891 	struct net_device_context *net_device_ctx = netdev_priv(net);
892 	struct vmbus_channel *channel = nvchan->channel;
893 	u16 q_idx = channel->offermsg.offer.sub_channel_index;
894 	struct sk_buff *skb;
895 	struct netvsc_stats *rx_stats = &nvchan->rx_stats;
896 	struct xdp_buff xdp;
897 	u32 act;
898 
899 	if (net->reg_state != NETREG_REGISTERED)
900 		return NVSP_STAT_FAIL;
901 
902 	act = netvsc_run_xdp(net, nvchan, &xdp);
903 
904 	if (act != XDP_PASS && act != XDP_TX) {
905 		u64_stats_update_begin(&rx_stats->syncp);
906 		rx_stats->xdp_drop++;
907 		u64_stats_update_end(&rx_stats->syncp);
908 
909 		return NVSP_STAT_SUCCESS; /* consumed by XDP */
910 	}
911 
912 	/* Allocate a skb - TODO direct I/O to pages? */
913 	skb = netvsc_alloc_recv_skb(net, nvchan, &xdp);
914 
915 	if (unlikely(!skb)) {
916 		++net_device_ctx->eth_stats.rx_no_memory;
917 		return NVSP_STAT_FAIL;
918 	}
919 
920 	skb_record_rx_queue(skb, q_idx);
921 
922 	/*
923 	 * Even if injecting the packet, record the statistics
924 	 * on the synthetic device because modifying the VF device
925 	 * statistics will not work correctly.
926 	 */
927 	u64_stats_update_begin(&rx_stats->syncp);
928 	rx_stats->packets++;
929 	rx_stats->bytes += nvchan->rsc.pktlen;
930 
931 	if (skb->pkt_type == PACKET_BROADCAST)
932 		++rx_stats->broadcast;
933 	else if (skb->pkt_type == PACKET_MULTICAST)
934 		++rx_stats->multicast;
935 	u64_stats_update_end(&rx_stats->syncp);
936 
937 	if (act == XDP_TX) {
938 		netvsc_xdp_xmit(skb, net);
939 		return NVSP_STAT_SUCCESS;
940 	}
941 
942 	napi_gro_receive(&nvchan->napi, skb);
943 	return NVSP_STAT_SUCCESS;
944 }
945 
946 static void netvsc_get_drvinfo(struct net_device *net,
947 			       struct ethtool_drvinfo *info)
948 {
949 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
950 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
951 }
952 
953 static void netvsc_get_channels(struct net_device *net,
954 				struct ethtool_channels *channel)
955 {
956 	struct net_device_context *net_device_ctx = netdev_priv(net);
957 	struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
958 
959 	if (nvdev) {
960 		channel->max_combined	= nvdev->max_chn;
961 		channel->combined_count = nvdev->num_chn;
962 	}
963 }
964 
965 /* Alloc struct netvsc_device_info, and initialize it from either existing
966  * struct netvsc_device, or from default values.
967  */
968 static
969 struct netvsc_device_info *netvsc_devinfo_get(struct netvsc_device *nvdev)
970 {
971 	struct netvsc_device_info *dev_info;
972 	struct bpf_prog *prog;
973 
974 	dev_info = kzalloc(sizeof(*dev_info), GFP_ATOMIC);
975 
976 	if (!dev_info)
977 		return NULL;
978 
979 	if (nvdev) {
980 		ASSERT_RTNL();
981 
982 		dev_info->num_chn = nvdev->num_chn;
983 		dev_info->send_sections = nvdev->send_section_cnt;
984 		dev_info->send_section_size = nvdev->send_section_size;
985 		dev_info->recv_sections = nvdev->recv_section_cnt;
986 		dev_info->recv_section_size = nvdev->recv_section_size;
987 
988 		memcpy(dev_info->rss_key, nvdev->extension->rss_key,
989 		       NETVSC_HASH_KEYLEN);
990 
991 		prog = netvsc_xdp_get(nvdev);
992 		if (prog) {
993 			bpf_prog_inc(prog);
994 			dev_info->bprog = prog;
995 		}
996 	} else {
997 		dev_info->num_chn = VRSS_CHANNEL_DEFAULT;
998 		dev_info->send_sections = NETVSC_DEFAULT_TX;
999 		dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
1000 		dev_info->recv_sections = NETVSC_DEFAULT_RX;
1001 		dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
1002 	}
1003 
1004 	return dev_info;
1005 }
1006 
1007 /* Free struct netvsc_device_info */
1008 static void netvsc_devinfo_put(struct netvsc_device_info *dev_info)
1009 {
1010 	if (dev_info->bprog) {
1011 		ASSERT_RTNL();
1012 		bpf_prog_put(dev_info->bprog);
1013 	}
1014 
1015 	kfree(dev_info);
1016 }
1017 
1018 static int netvsc_detach(struct net_device *ndev,
1019 			 struct netvsc_device *nvdev)
1020 {
1021 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1022 	struct hv_device *hdev = ndev_ctx->device_ctx;
1023 	int ret;
1024 
1025 	/* Don't try continuing to try and setup sub channels */
1026 	if (cancel_work_sync(&nvdev->subchan_work))
1027 		nvdev->num_chn = 1;
1028 
1029 	netvsc_xdp_set(ndev, NULL, NULL, nvdev);
1030 
1031 	/* If device was up (receiving) then shutdown */
1032 	if (netif_running(ndev)) {
1033 		netvsc_tx_disable(nvdev, ndev);
1034 
1035 		ret = rndis_filter_close(nvdev);
1036 		if (ret) {
1037 			netdev_err(ndev,
1038 				   "unable to close device (ret %d).\n", ret);
1039 			return ret;
1040 		}
1041 
1042 		ret = netvsc_wait_until_empty(nvdev);
1043 		if (ret) {
1044 			netdev_err(ndev,
1045 				   "Ring buffer not empty after closing rndis\n");
1046 			return ret;
1047 		}
1048 	}
1049 
1050 	netif_device_detach(ndev);
1051 
1052 	rndis_filter_device_remove(hdev, nvdev);
1053 
1054 	return 0;
1055 }
1056 
1057 static int netvsc_attach(struct net_device *ndev,
1058 			 struct netvsc_device_info *dev_info)
1059 {
1060 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1061 	struct hv_device *hdev = ndev_ctx->device_ctx;
1062 	struct netvsc_device *nvdev;
1063 	struct rndis_device *rdev;
1064 	struct bpf_prog *prog;
1065 	int ret = 0;
1066 
1067 	nvdev = rndis_filter_device_add(hdev, dev_info);
1068 	if (IS_ERR(nvdev))
1069 		return PTR_ERR(nvdev);
1070 
1071 	if (nvdev->num_chn > 1) {
1072 		ret = rndis_set_subchannel(ndev, nvdev, dev_info);
1073 
1074 		/* if unavailable, just proceed with one queue */
1075 		if (ret) {
1076 			nvdev->max_chn = 1;
1077 			nvdev->num_chn = 1;
1078 		}
1079 	}
1080 
1081 	prog = dev_info->bprog;
1082 	if (prog) {
1083 		bpf_prog_inc(prog);
1084 		ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
1085 		if (ret) {
1086 			bpf_prog_put(prog);
1087 			goto err1;
1088 		}
1089 	}
1090 
1091 	/* In any case device is now ready */
1092 	nvdev->tx_disable = false;
1093 	netif_device_attach(ndev);
1094 
1095 	/* Note: enable and attach happen when sub-channels setup */
1096 	netif_carrier_off(ndev);
1097 
1098 	if (netif_running(ndev)) {
1099 		ret = rndis_filter_open(nvdev);
1100 		if (ret)
1101 			goto err2;
1102 
1103 		rdev = nvdev->extension;
1104 		if (!rdev->link_state)
1105 			netif_carrier_on(ndev);
1106 	}
1107 
1108 	return 0;
1109 
1110 err2:
1111 	netif_device_detach(ndev);
1112 
1113 err1:
1114 	rndis_filter_device_remove(hdev, nvdev);
1115 
1116 	return ret;
1117 }
1118 
1119 static int netvsc_set_channels(struct net_device *net,
1120 			       struct ethtool_channels *channels)
1121 {
1122 	struct net_device_context *net_device_ctx = netdev_priv(net);
1123 	struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
1124 	unsigned int orig, count = channels->combined_count;
1125 	struct netvsc_device_info *device_info;
1126 	int ret;
1127 
1128 	/* We do not support separate count for rx, tx, or other */
1129 	if (count == 0 ||
1130 	    channels->rx_count || channels->tx_count || channels->other_count)
1131 		return -EINVAL;
1132 
1133 	if (!nvdev || nvdev->destroy)
1134 		return -ENODEV;
1135 
1136 	if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1137 		return -EINVAL;
1138 
1139 	if (count > nvdev->max_chn)
1140 		return -EINVAL;
1141 
1142 	orig = nvdev->num_chn;
1143 
1144 	device_info = netvsc_devinfo_get(nvdev);
1145 
1146 	if (!device_info)
1147 		return -ENOMEM;
1148 
1149 	device_info->num_chn = count;
1150 
1151 	ret = netvsc_detach(net, nvdev);
1152 	if (ret)
1153 		goto out;
1154 
1155 	ret = netvsc_attach(net, device_info);
1156 	if (ret) {
1157 		device_info->num_chn = orig;
1158 		if (netvsc_attach(net, device_info))
1159 			netdev_err(net, "restoring channel setting failed\n");
1160 	}
1161 
1162 out:
1163 	netvsc_devinfo_put(device_info);
1164 	return ret;
1165 }
1166 
1167 static void netvsc_init_settings(struct net_device *dev)
1168 {
1169 	struct net_device_context *ndc = netdev_priv(dev);
1170 
1171 	ndc->l4_hash = HV_DEFAULT_L4HASH;
1172 
1173 	ndc->speed = SPEED_UNKNOWN;
1174 	ndc->duplex = DUPLEX_FULL;
1175 
1176 	dev->features = NETIF_F_LRO;
1177 }
1178 
1179 static int netvsc_get_link_ksettings(struct net_device *dev,
1180 				     struct ethtool_link_ksettings *cmd)
1181 {
1182 	struct net_device_context *ndc = netdev_priv(dev);
1183 	struct net_device *vf_netdev;
1184 
1185 	vf_netdev = rtnl_dereference(ndc->vf_netdev);
1186 
1187 	if (vf_netdev)
1188 		return __ethtool_get_link_ksettings(vf_netdev, cmd);
1189 
1190 	cmd->base.speed = ndc->speed;
1191 	cmd->base.duplex = ndc->duplex;
1192 	cmd->base.port = PORT_OTHER;
1193 
1194 	return 0;
1195 }
1196 
1197 static int netvsc_set_link_ksettings(struct net_device *dev,
1198 				     const struct ethtool_link_ksettings *cmd)
1199 {
1200 	struct net_device_context *ndc = netdev_priv(dev);
1201 	struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1202 
1203 	if (vf_netdev) {
1204 		if (!vf_netdev->ethtool_ops->set_link_ksettings)
1205 			return -EOPNOTSUPP;
1206 
1207 		return vf_netdev->ethtool_ops->set_link_ksettings(vf_netdev,
1208 								  cmd);
1209 	}
1210 
1211 	return ethtool_virtdev_set_link_ksettings(dev, cmd,
1212 						  &ndc->speed, &ndc->duplex);
1213 }
1214 
1215 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
1216 {
1217 	struct net_device_context *ndevctx = netdev_priv(ndev);
1218 	struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1219 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1220 	int orig_mtu = ndev->mtu;
1221 	struct netvsc_device_info *device_info;
1222 	int ret = 0;
1223 
1224 	if (!nvdev || nvdev->destroy)
1225 		return -ENODEV;
1226 
1227 	device_info = netvsc_devinfo_get(nvdev);
1228 
1229 	if (!device_info)
1230 		return -ENOMEM;
1231 
1232 	/* Change MTU of underlying VF netdev first. */
1233 	if (vf_netdev) {
1234 		ret = dev_set_mtu(vf_netdev, mtu);
1235 		if (ret)
1236 			goto out;
1237 	}
1238 
1239 	ret = netvsc_detach(ndev, nvdev);
1240 	if (ret)
1241 		goto rollback_vf;
1242 
1243 	ndev->mtu = mtu;
1244 
1245 	ret = netvsc_attach(ndev, device_info);
1246 	if (!ret)
1247 		goto out;
1248 
1249 	/* Attempt rollback to original MTU */
1250 	ndev->mtu = orig_mtu;
1251 
1252 	if (netvsc_attach(ndev, device_info))
1253 		netdev_err(ndev, "restoring mtu failed\n");
1254 rollback_vf:
1255 	if (vf_netdev)
1256 		dev_set_mtu(vf_netdev, orig_mtu);
1257 
1258 out:
1259 	netvsc_devinfo_put(device_info);
1260 	return ret;
1261 }
1262 
1263 static void netvsc_get_vf_stats(struct net_device *net,
1264 				struct netvsc_vf_pcpu_stats *tot)
1265 {
1266 	struct net_device_context *ndev_ctx = netdev_priv(net);
1267 	int i;
1268 
1269 	memset(tot, 0, sizeof(*tot));
1270 
1271 	for_each_possible_cpu(i) {
1272 		const struct netvsc_vf_pcpu_stats *stats
1273 			= per_cpu_ptr(ndev_ctx->vf_stats, i);
1274 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1275 		unsigned int start;
1276 
1277 		do {
1278 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1279 			rx_packets = stats->rx_packets;
1280 			tx_packets = stats->tx_packets;
1281 			rx_bytes = stats->rx_bytes;
1282 			tx_bytes = stats->tx_bytes;
1283 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1284 
1285 		tot->rx_packets += rx_packets;
1286 		tot->tx_packets += tx_packets;
1287 		tot->rx_bytes   += rx_bytes;
1288 		tot->tx_bytes   += tx_bytes;
1289 		tot->tx_dropped += stats->tx_dropped;
1290 	}
1291 }
1292 
1293 static void netvsc_get_pcpu_stats(struct net_device *net,
1294 				  struct netvsc_ethtool_pcpu_stats *pcpu_tot)
1295 {
1296 	struct net_device_context *ndev_ctx = netdev_priv(net);
1297 	struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1298 	int i;
1299 
1300 	/* fetch percpu stats of vf */
1301 	for_each_possible_cpu(i) {
1302 		const struct netvsc_vf_pcpu_stats *stats =
1303 			per_cpu_ptr(ndev_ctx->vf_stats, i);
1304 		struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
1305 		unsigned int start;
1306 
1307 		do {
1308 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1309 			this_tot->vf_rx_packets = stats->rx_packets;
1310 			this_tot->vf_tx_packets = stats->tx_packets;
1311 			this_tot->vf_rx_bytes = stats->rx_bytes;
1312 			this_tot->vf_tx_bytes = stats->tx_bytes;
1313 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1314 		this_tot->rx_packets = this_tot->vf_rx_packets;
1315 		this_tot->tx_packets = this_tot->vf_tx_packets;
1316 		this_tot->rx_bytes   = this_tot->vf_rx_bytes;
1317 		this_tot->tx_bytes   = this_tot->vf_tx_bytes;
1318 	}
1319 
1320 	/* fetch percpu stats of netvsc */
1321 	for (i = 0; i < nvdev->num_chn; i++) {
1322 		const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1323 		const struct netvsc_stats *stats;
1324 		struct netvsc_ethtool_pcpu_stats *this_tot =
1325 			&pcpu_tot[nvchan->channel->target_cpu];
1326 		u64 packets, bytes;
1327 		unsigned int start;
1328 
1329 		stats = &nvchan->tx_stats;
1330 		do {
1331 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1332 			packets = stats->packets;
1333 			bytes = stats->bytes;
1334 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1335 
1336 		this_tot->tx_bytes	+= bytes;
1337 		this_tot->tx_packets	+= packets;
1338 
1339 		stats = &nvchan->rx_stats;
1340 		do {
1341 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1342 			packets = stats->packets;
1343 			bytes = stats->bytes;
1344 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1345 
1346 		this_tot->rx_bytes	+= bytes;
1347 		this_tot->rx_packets	+= packets;
1348 	}
1349 }
1350 
1351 static void netvsc_get_stats64(struct net_device *net,
1352 			       struct rtnl_link_stats64 *t)
1353 {
1354 	struct net_device_context *ndev_ctx = netdev_priv(net);
1355 	struct netvsc_device *nvdev;
1356 	struct netvsc_vf_pcpu_stats vf_tot;
1357 	int i;
1358 
1359 	rcu_read_lock();
1360 
1361 	nvdev = rcu_dereference(ndev_ctx->nvdev);
1362 	if (!nvdev)
1363 		goto out;
1364 
1365 	netdev_stats_to_stats64(t, &net->stats);
1366 
1367 	netvsc_get_vf_stats(net, &vf_tot);
1368 	t->rx_packets += vf_tot.rx_packets;
1369 	t->tx_packets += vf_tot.tx_packets;
1370 	t->rx_bytes   += vf_tot.rx_bytes;
1371 	t->tx_bytes   += vf_tot.tx_bytes;
1372 	t->tx_dropped += vf_tot.tx_dropped;
1373 
1374 	for (i = 0; i < nvdev->num_chn; i++) {
1375 		const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1376 		const struct netvsc_stats *stats;
1377 		u64 packets, bytes, multicast;
1378 		unsigned int start;
1379 
1380 		stats = &nvchan->tx_stats;
1381 		do {
1382 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1383 			packets = stats->packets;
1384 			bytes = stats->bytes;
1385 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1386 
1387 		t->tx_bytes	+= bytes;
1388 		t->tx_packets	+= packets;
1389 
1390 		stats = &nvchan->rx_stats;
1391 		do {
1392 			start = u64_stats_fetch_begin_irq(&stats->syncp);
1393 			packets = stats->packets;
1394 			bytes = stats->bytes;
1395 			multicast = stats->multicast + stats->broadcast;
1396 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1397 
1398 		t->rx_bytes	+= bytes;
1399 		t->rx_packets	+= packets;
1400 		t->multicast	+= multicast;
1401 	}
1402 out:
1403 	rcu_read_unlock();
1404 }
1405 
1406 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1407 {
1408 	struct net_device_context *ndc = netdev_priv(ndev);
1409 	struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1410 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1411 	struct sockaddr *addr = p;
1412 	int err;
1413 
1414 	err = eth_prepare_mac_addr_change(ndev, p);
1415 	if (err)
1416 		return err;
1417 
1418 	if (!nvdev)
1419 		return -ENODEV;
1420 
1421 	if (vf_netdev) {
1422 		err = dev_set_mac_address(vf_netdev, addr, NULL);
1423 		if (err)
1424 			return err;
1425 	}
1426 
1427 	err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1428 	if (!err) {
1429 		eth_commit_mac_addr_change(ndev, p);
1430 	} else if (vf_netdev) {
1431 		/* rollback change on VF */
1432 		memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1433 		dev_set_mac_address(vf_netdev, addr, NULL);
1434 	}
1435 
1436 	return err;
1437 }
1438 
1439 static const struct {
1440 	char name[ETH_GSTRING_LEN];
1441 	u16 offset;
1442 } netvsc_stats[] = {
1443 	{ "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1444 	{ "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1445 	{ "tx_no_space",  offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1446 	{ "tx_too_big",	  offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1447 	{ "tx_busy",	  offsetof(struct netvsc_ethtool_stats, tx_busy) },
1448 	{ "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1449 	{ "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1450 	{ "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
1451 	{ "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1452 	{ "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1453 	{ "vlan_error", offsetof(struct netvsc_ethtool_stats, vlan_error) },
1454 }, pcpu_stats[] = {
1455 	{ "cpu%u_rx_packets",
1456 		offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
1457 	{ "cpu%u_rx_bytes",
1458 		offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
1459 	{ "cpu%u_tx_packets",
1460 		offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
1461 	{ "cpu%u_tx_bytes",
1462 		offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
1463 	{ "cpu%u_vf_rx_packets",
1464 		offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
1465 	{ "cpu%u_vf_rx_bytes",
1466 		offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
1467 	{ "cpu%u_vf_tx_packets",
1468 		offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
1469 	{ "cpu%u_vf_tx_bytes",
1470 		offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
1471 }, vf_stats[] = {
1472 	{ "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1473 	{ "vf_rx_bytes",   offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1474 	{ "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1475 	{ "vf_tx_bytes",   offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1476 	{ "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1477 };
1478 
1479 #define NETVSC_GLOBAL_STATS_LEN	ARRAY_SIZE(netvsc_stats)
1480 #define NETVSC_VF_STATS_LEN	ARRAY_SIZE(vf_stats)
1481 
1482 /* statistics per queue (rx/tx packets/bytes) */
1483 #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
1484 
1485 /* 5 statistics per queue (rx/tx packets/bytes, rx xdp_drop) */
1486 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 5)
1487 
1488 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1489 {
1490 	struct net_device_context *ndc = netdev_priv(dev);
1491 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1492 
1493 	if (!nvdev)
1494 		return -ENODEV;
1495 
1496 	switch (string_set) {
1497 	case ETH_SS_STATS:
1498 		return NETVSC_GLOBAL_STATS_LEN
1499 			+ NETVSC_VF_STATS_LEN
1500 			+ NETVSC_QUEUE_STATS_LEN(nvdev)
1501 			+ NETVSC_PCPU_STATS_LEN;
1502 	default:
1503 		return -EINVAL;
1504 	}
1505 }
1506 
1507 static void netvsc_get_ethtool_stats(struct net_device *dev,
1508 				     struct ethtool_stats *stats, u64 *data)
1509 {
1510 	struct net_device_context *ndc = netdev_priv(dev);
1511 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1512 	const void *nds = &ndc->eth_stats;
1513 	const struct netvsc_stats *qstats;
1514 	struct netvsc_vf_pcpu_stats sum;
1515 	struct netvsc_ethtool_pcpu_stats *pcpu_sum;
1516 	unsigned int start;
1517 	u64 packets, bytes;
1518 	u64 xdp_drop;
1519 	int i, j, cpu;
1520 
1521 	if (!nvdev)
1522 		return;
1523 
1524 	for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1525 		data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1526 
1527 	netvsc_get_vf_stats(dev, &sum);
1528 	for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1529 		data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1530 
1531 	for (j = 0; j < nvdev->num_chn; j++) {
1532 		qstats = &nvdev->chan_table[j].tx_stats;
1533 
1534 		do {
1535 			start = u64_stats_fetch_begin_irq(&qstats->syncp);
1536 			packets = qstats->packets;
1537 			bytes = qstats->bytes;
1538 		} while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1539 		data[i++] = packets;
1540 		data[i++] = bytes;
1541 
1542 		qstats = &nvdev->chan_table[j].rx_stats;
1543 		do {
1544 			start = u64_stats_fetch_begin_irq(&qstats->syncp);
1545 			packets = qstats->packets;
1546 			bytes = qstats->bytes;
1547 			xdp_drop = qstats->xdp_drop;
1548 		} while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1549 		data[i++] = packets;
1550 		data[i++] = bytes;
1551 		data[i++] = xdp_drop;
1552 	}
1553 
1554 	pcpu_sum = kvmalloc_array(num_possible_cpus(),
1555 				  sizeof(struct netvsc_ethtool_pcpu_stats),
1556 				  GFP_KERNEL);
1557 	netvsc_get_pcpu_stats(dev, pcpu_sum);
1558 	for_each_present_cpu(cpu) {
1559 		struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
1560 
1561 		for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
1562 			data[i++] = *(u64 *)((void *)this_sum
1563 					     + pcpu_stats[j].offset);
1564 	}
1565 	kvfree(pcpu_sum);
1566 }
1567 
1568 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1569 {
1570 	struct net_device_context *ndc = netdev_priv(dev);
1571 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1572 	u8 *p = data;
1573 	int i, cpu;
1574 
1575 	if (!nvdev)
1576 		return;
1577 
1578 	switch (stringset) {
1579 	case ETH_SS_STATS:
1580 		for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1581 			memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1582 			p += ETH_GSTRING_LEN;
1583 		}
1584 
1585 		for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1586 			memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1587 			p += ETH_GSTRING_LEN;
1588 		}
1589 
1590 		for (i = 0; i < nvdev->num_chn; i++) {
1591 			sprintf(p, "tx_queue_%u_packets", i);
1592 			p += ETH_GSTRING_LEN;
1593 			sprintf(p, "tx_queue_%u_bytes", i);
1594 			p += ETH_GSTRING_LEN;
1595 			sprintf(p, "rx_queue_%u_packets", i);
1596 			p += ETH_GSTRING_LEN;
1597 			sprintf(p, "rx_queue_%u_bytes", i);
1598 			p += ETH_GSTRING_LEN;
1599 			sprintf(p, "rx_queue_%u_xdp_drop", i);
1600 			p += ETH_GSTRING_LEN;
1601 		}
1602 
1603 		for_each_present_cpu(cpu) {
1604 			for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++) {
1605 				sprintf(p, pcpu_stats[i].name, cpu);
1606 				p += ETH_GSTRING_LEN;
1607 			}
1608 		}
1609 
1610 		break;
1611 	}
1612 }
1613 
1614 static int
1615 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1616 			 struct ethtool_rxnfc *info)
1617 {
1618 	const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1619 
1620 	info->data = RXH_IP_SRC | RXH_IP_DST;
1621 
1622 	switch (info->flow_type) {
1623 	case TCP_V4_FLOW:
1624 		if (ndc->l4_hash & HV_TCP4_L4HASH)
1625 			info->data |= l4_flag;
1626 
1627 		break;
1628 
1629 	case TCP_V6_FLOW:
1630 		if (ndc->l4_hash & HV_TCP6_L4HASH)
1631 			info->data |= l4_flag;
1632 
1633 		break;
1634 
1635 	case UDP_V4_FLOW:
1636 		if (ndc->l4_hash & HV_UDP4_L4HASH)
1637 			info->data |= l4_flag;
1638 
1639 		break;
1640 
1641 	case UDP_V6_FLOW:
1642 		if (ndc->l4_hash & HV_UDP6_L4HASH)
1643 			info->data |= l4_flag;
1644 
1645 		break;
1646 
1647 	case IPV4_FLOW:
1648 	case IPV6_FLOW:
1649 		break;
1650 	default:
1651 		info->data = 0;
1652 		break;
1653 	}
1654 
1655 	return 0;
1656 }
1657 
1658 static int
1659 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1660 		 u32 *rules)
1661 {
1662 	struct net_device_context *ndc = netdev_priv(dev);
1663 	struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1664 
1665 	if (!nvdev)
1666 		return -ENODEV;
1667 
1668 	switch (info->cmd) {
1669 	case ETHTOOL_GRXRINGS:
1670 		info->data = nvdev->num_chn;
1671 		return 0;
1672 
1673 	case ETHTOOL_GRXFH:
1674 		return netvsc_get_rss_hash_opts(ndc, info);
1675 	}
1676 	return -EOPNOTSUPP;
1677 }
1678 
1679 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1680 				    struct ethtool_rxnfc *info)
1681 {
1682 	if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1683 			   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1684 		switch (info->flow_type) {
1685 		case TCP_V4_FLOW:
1686 			ndc->l4_hash |= HV_TCP4_L4HASH;
1687 			break;
1688 
1689 		case TCP_V6_FLOW:
1690 			ndc->l4_hash |= HV_TCP6_L4HASH;
1691 			break;
1692 
1693 		case UDP_V4_FLOW:
1694 			ndc->l4_hash |= HV_UDP4_L4HASH;
1695 			break;
1696 
1697 		case UDP_V6_FLOW:
1698 			ndc->l4_hash |= HV_UDP6_L4HASH;
1699 			break;
1700 
1701 		default:
1702 			return -EOPNOTSUPP;
1703 		}
1704 
1705 		return 0;
1706 	}
1707 
1708 	if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1709 		switch (info->flow_type) {
1710 		case TCP_V4_FLOW:
1711 			ndc->l4_hash &= ~HV_TCP4_L4HASH;
1712 			break;
1713 
1714 		case TCP_V6_FLOW:
1715 			ndc->l4_hash &= ~HV_TCP6_L4HASH;
1716 			break;
1717 
1718 		case UDP_V4_FLOW:
1719 			ndc->l4_hash &= ~HV_UDP4_L4HASH;
1720 			break;
1721 
1722 		case UDP_V6_FLOW:
1723 			ndc->l4_hash &= ~HV_UDP6_L4HASH;
1724 			break;
1725 
1726 		default:
1727 			return -EOPNOTSUPP;
1728 		}
1729 
1730 		return 0;
1731 	}
1732 
1733 	return -EOPNOTSUPP;
1734 }
1735 
1736 static int
1737 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1738 {
1739 	struct net_device_context *ndc = netdev_priv(ndev);
1740 
1741 	if (info->cmd == ETHTOOL_SRXFH)
1742 		return netvsc_set_rss_hash_opts(ndc, info);
1743 
1744 	return -EOPNOTSUPP;
1745 }
1746 
1747 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1748 {
1749 	return NETVSC_HASH_KEYLEN;
1750 }
1751 
1752 static u32 netvsc_rss_indir_size(struct net_device *dev)
1753 {
1754 	return ITAB_NUM;
1755 }
1756 
1757 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1758 			   u8 *hfunc)
1759 {
1760 	struct net_device_context *ndc = netdev_priv(dev);
1761 	struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1762 	struct rndis_device *rndis_dev;
1763 	int i;
1764 
1765 	if (!ndev)
1766 		return -ENODEV;
1767 
1768 	if (hfunc)
1769 		*hfunc = ETH_RSS_HASH_TOP;	/* Toeplitz */
1770 
1771 	rndis_dev = ndev->extension;
1772 	if (indir) {
1773 		for (i = 0; i < ITAB_NUM; i++)
1774 			indir[i] = ndc->rx_table[i];
1775 	}
1776 
1777 	if (key)
1778 		memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1779 
1780 	return 0;
1781 }
1782 
1783 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1784 			   const u8 *key, const u8 hfunc)
1785 {
1786 	struct net_device_context *ndc = netdev_priv(dev);
1787 	struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1788 	struct rndis_device *rndis_dev;
1789 	int i;
1790 
1791 	if (!ndev)
1792 		return -ENODEV;
1793 
1794 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1795 		return -EOPNOTSUPP;
1796 
1797 	rndis_dev = ndev->extension;
1798 	if (indir) {
1799 		for (i = 0; i < ITAB_NUM; i++)
1800 			if (indir[i] >= ndev->num_chn)
1801 				return -EINVAL;
1802 
1803 		for (i = 0; i < ITAB_NUM; i++)
1804 			ndc->rx_table[i] = indir[i];
1805 	}
1806 
1807 	if (!key) {
1808 		if (!indir)
1809 			return 0;
1810 
1811 		key = rndis_dev->rss_key;
1812 	}
1813 
1814 	return rndis_filter_set_rss_param(rndis_dev, key);
1815 }
1816 
1817 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1818  * It does have pre-allocated receive area which is divided into sections.
1819  */
1820 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1821 				   struct ethtool_ringparam *ring)
1822 {
1823 	u32 max_buf_size;
1824 
1825 	ring->rx_pending = nvdev->recv_section_cnt;
1826 	ring->tx_pending = nvdev->send_section_cnt;
1827 
1828 	if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1829 		max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1830 	else
1831 		max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1832 
1833 	ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1834 	ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1835 		/ nvdev->send_section_size;
1836 }
1837 
1838 static void netvsc_get_ringparam(struct net_device *ndev,
1839 				 struct ethtool_ringparam *ring)
1840 {
1841 	struct net_device_context *ndevctx = netdev_priv(ndev);
1842 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1843 
1844 	if (!nvdev)
1845 		return;
1846 
1847 	__netvsc_get_ringparam(nvdev, ring);
1848 }
1849 
1850 static int netvsc_set_ringparam(struct net_device *ndev,
1851 				struct ethtool_ringparam *ring)
1852 {
1853 	struct net_device_context *ndevctx = netdev_priv(ndev);
1854 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1855 	struct netvsc_device_info *device_info;
1856 	struct ethtool_ringparam orig;
1857 	u32 new_tx, new_rx;
1858 	int ret = 0;
1859 
1860 	if (!nvdev || nvdev->destroy)
1861 		return -ENODEV;
1862 
1863 	memset(&orig, 0, sizeof(orig));
1864 	__netvsc_get_ringparam(nvdev, &orig);
1865 
1866 	new_tx = clamp_t(u32, ring->tx_pending,
1867 			 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1868 	new_rx = clamp_t(u32, ring->rx_pending,
1869 			 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1870 
1871 	if (new_tx == orig.tx_pending &&
1872 	    new_rx == orig.rx_pending)
1873 		return 0;	 /* no change */
1874 
1875 	device_info = netvsc_devinfo_get(nvdev);
1876 
1877 	if (!device_info)
1878 		return -ENOMEM;
1879 
1880 	device_info->send_sections = new_tx;
1881 	device_info->recv_sections = new_rx;
1882 
1883 	ret = netvsc_detach(ndev, nvdev);
1884 	if (ret)
1885 		goto out;
1886 
1887 	ret = netvsc_attach(ndev, device_info);
1888 	if (ret) {
1889 		device_info->send_sections = orig.tx_pending;
1890 		device_info->recv_sections = orig.rx_pending;
1891 
1892 		if (netvsc_attach(ndev, device_info))
1893 			netdev_err(ndev, "restoring ringparam failed");
1894 	}
1895 
1896 out:
1897 	netvsc_devinfo_put(device_info);
1898 	return ret;
1899 }
1900 
1901 static netdev_features_t netvsc_fix_features(struct net_device *ndev,
1902 					     netdev_features_t features)
1903 {
1904 	struct net_device_context *ndevctx = netdev_priv(ndev);
1905 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1906 
1907 	if (!nvdev || nvdev->destroy)
1908 		return features;
1909 
1910 	if ((features & NETIF_F_LRO) && netvsc_xdp_get(nvdev)) {
1911 		features ^= NETIF_F_LRO;
1912 		netdev_info(ndev, "Skip LRO - unsupported with XDP\n");
1913 	}
1914 
1915 	return features;
1916 }
1917 
1918 static int netvsc_set_features(struct net_device *ndev,
1919 			       netdev_features_t features)
1920 {
1921 	netdev_features_t change = features ^ ndev->features;
1922 	struct net_device_context *ndevctx = netdev_priv(ndev);
1923 	struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1924 	struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
1925 	struct ndis_offload_params offloads;
1926 	int ret = 0;
1927 
1928 	if (!nvdev || nvdev->destroy)
1929 		return -ENODEV;
1930 
1931 	if (!(change & NETIF_F_LRO))
1932 		goto syncvf;
1933 
1934 	memset(&offloads, 0, sizeof(struct ndis_offload_params));
1935 
1936 	if (features & NETIF_F_LRO) {
1937 		offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1938 		offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1939 	} else {
1940 		offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1941 		offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1942 	}
1943 
1944 	ret = rndis_filter_set_offload_params(ndev, nvdev, &offloads);
1945 
1946 	if (ret) {
1947 		features ^= NETIF_F_LRO;
1948 		ndev->features = features;
1949 	}
1950 
1951 syncvf:
1952 	if (!vf_netdev)
1953 		return ret;
1954 
1955 	vf_netdev->wanted_features = features;
1956 	netdev_update_features(vf_netdev);
1957 
1958 	return ret;
1959 }
1960 
1961 static int netvsc_get_regs_len(struct net_device *netdev)
1962 {
1963 	return VRSS_SEND_TAB_SIZE * sizeof(u32);
1964 }
1965 
1966 static void netvsc_get_regs(struct net_device *netdev,
1967 			    struct ethtool_regs *regs, void *p)
1968 {
1969 	struct net_device_context *ndc = netdev_priv(netdev);
1970 	u32 *regs_buff = p;
1971 
1972 	/* increase the version, if buffer format is changed. */
1973 	regs->version = 1;
1974 
1975 	memcpy(regs_buff, ndc->tx_table, VRSS_SEND_TAB_SIZE * sizeof(u32));
1976 }
1977 
1978 static u32 netvsc_get_msglevel(struct net_device *ndev)
1979 {
1980 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1981 
1982 	return ndev_ctx->msg_enable;
1983 }
1984 
1985 static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
1986 {
1987 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1988 
1989 	ndev_ctx->msg_enable = val;
1990 }
1991 
1992 static const struct ethtool_ops ethtool_ops = {
1993 	.get_drvinfo	= netvsc_get_drvinfo,
1994 	.get_regs_len	= netvsc_get_regs_len,
1995 	.get_regs	= netvsc_get_regs,
1996 	.get_msglevel	= netvsc_get_msglevel,
1997 	.set_msglevel	= netvsc_set_msglevel,
1998 	.get_link	= ethtool_op_get_link,
1999 	.get_ethtool_stats = netvsc_get_ethtool_stats,
2000 	.get_sset_count = netvsc_get_sset_count,
2001 	.get_strings	= netvsc_get_strings,
2002 	.get_channels   = netvsc_get_channels,
2003 	.set_channels   = netvsc_set_channels,
2004 	.get_ts_info	= ethtool_op_get_ts_info,
2005 	.get_rxnfc	= netvsc_get_rxnfc,
2006 	.set_rxnfc	= netvsc_set_rxnfc,
2007 	.get_rxfh_key_size = netvsc_get_rxfh_key_size,
2008 	.get_rxfh_indir_size = netvsc_rss_indir_size,
2009 	.get_rxfh	= netvsc_get_rxfh,
2010 	.set_rxfh	= netvsc_set_rxfh,
2011 	.get_link_ksettings = netvsc_get_link_ksettings,
2012 	.set_link_ksettings = netvsc_set_link_ksettings,
2013 	.get_ringparam	= netvsc_get_ringparam,
2014 	.set_ringparam	= netvsc_set_ringparam,
2015 };
2016 
2017 static const struct net_device_ops device_ops = {
2018 	.ndo_open =			netvsc_open,
2019 	.ndo_stop =			netvsc_close,
2020 	.ndo_start_xmit =		netvsc_start_xmit,
2021 	.ndo_change_rx_flags =		netvsc_change_rx_flags,
2022 	.ndo_set_rx_mode =		netvsc_set_rx_mode,
2023 	.ndo_fix_features =		netvsc_fix_features,
2024 	.ndo_set_features =		netvsc_set_features,
2025 	.ndo_change_mtu =		netvsc_change_mtu,
2026 	.ndo_validate_addr =		eth_validate_addr,
2027 	.ndo_set_mac_address =		netvsc_set_mac_addr,
2028 	.ndo_select_queue =		netvsc_select_queue,
2029 	.ndo_get_stats64 =		netvsc_get_stats64,
2030 	.ndo_bpf =			netvsc_bpf,
2031 };
2032 
2033 /*
2034  * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
2035  * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
2036  * present send GARP packet to network peers with netif_notify_peers().
2037  */
2038 static void netvsc_link_change(struct work_struct *w)
2039 {
2040 	struct net_device_context *ndev_ctx =
2041 		container_of(w, struct net_device_context, dwork.work);
2042 	struct hv_device *device_obj = ndev_ctx->device_ctx;
2043 	struct net_device *net = hv_get_drvdata(device_obj);
2044 	struct netvsc_device *net_device;
2045 	struct rndis_device *rdev;
2046 	struct netvsc_reconfig *event = NULL;
2047 	bool notify = false, reschedule = false;
2048 	unsigned long flags, next_reconfig, delay;
2049 
2050 	/* if changes are happening, comeback later */
2051 	if (!rtnl_trylock()) {
2052 		schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2053 		return;
2054 	}
2055 
2056 	net_device = rtnl_dereference(ndev_ctx->nvdev);
2057 	if (!net_device)
2058 		goto out_unlock;
2059 
2060 	rdev = net_device->extension;
2061 
2062 	next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
2063 	if (time_is_after_jiffies(next_reconfig)) {
2064 		/* link_watch only sends one notification with current state
2065 		 * per second, avoid doing reconfig more frequently. Handle
2066 		 * wrap around.
2067 		 */
2068 		delay = next_reconfig - jiffies;
2069 		delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
2070 		schedule_delayed_work(&ndev_ctx->dwork, delay);
2071 		goto out_unlock;
2072 	}
2073 	ndev_ctx->last_reconfig = jiffies;
2074 
2075 	spin_lock_irqsave(&ndev_ctx->lock, flags);
2076 	if (!list_empty(&ndev_ctx->reconfig_events)) {
2077 		event = list_first_entry(&ndev_ctx->reconfig_events,
2078 					 struct netvsc_reconfig, list);
2079 		list_del(&event->list);
2080 		reschedule = !list_empty(&ndev_ctx->reconfig_events);
2081 	}
2082 	spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2083 
2084 	if (!event)
2085 		goto out_unlock;
2086 
2087 	switch (event->event) {
2088 		/* Only the following events are possible due to the check in
2089 		 * netvsc_linkstatus_callback()
2090 		 */
2091 	case RNDIS_STATUS_MEDIA_CONNECT:
2092 		if (rdev->link_state) {
2093 			rdev->link_state = false;
2094 			netif_carrier_on(net);
2095 			netvsc_tx_enable(net_device, net);
2096 		} else {
2097 			notify = true;
2098 		}
2099 		kfree(event);
2100 		break;
2101 	case RNDIS_STATUS_MEDIA_DISCONNECT:
2102 		if (!rdev->link_state) {
2103 			rdev->link_state = true;
2104 			netif_carrier_off(net);
2105 			netvsc_tx_disable(net_device, net);
2106 		}
2107 		kfree(event);
2108 		break;
2109 	case RNDIS_STATUS_NETWORK_CHANGE:
2110 		/* Only makes sense if carrier is present */
2111 		if (!rdev->link_state) {
2112 			rdev->link_state = true;
2113 			netif_carrier_off(net);
2114 			netvsc_tx_disable(net_device, net);
2115 			event->event = RNDIS_STATUS_MEDIA_CONNECT;
2116 			spin_lock_irqsave(&ndev_ctx->lock, flags);
2117 			list_add(&event->list, &ndev_ctx->reconfig_events);
2118 			spin_unlock_irqrestore(&ndev_ctx->lock, flags);
2119 			reschedule = true;
2120 		}
2121 		break;
2122 	}
2123 
2124 	rtnl_unlock();
2125 
2126 	if (notify)
2127 		netdev_notify_peers(net);
2128 
2129 	/* link_watch only sends one notification with current state per
2130 	 * second, handle next reconfig event in 2 seconds.
2131 	 */
2132 	if (reschedule)
2133 		schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
2134 
2135 	return;
2136 
2137 out_unlock:
2138 	rtnl_unlock();
2139 }
2140 
2141 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
2142 {
2143 	struct net_device_context *net_device_ctx;
2144 	struct net_device *dev;
2145 
2146 	dev = netdev_master_upper_dev_get(vf_netdev);
2147 	if (!dev || dev->netdev_ops != &device_ops)
2148 		return NULL;	/* not a netvsc device */
2149 
2150 	net_device_ctx = netdev_priv(dev);
2151 	if (!rtnl_dereference(net_device_ctx->nvdev))
2152 		return NULL;	/* device is removed */
2153 
2154 	return dev;
2155 }
2156 
2157 /* Called when VF is injecting data into network stack.
2158  * Change the associated network device from VF to netvsc.
2159  * note: already called with rcu_read_lock
2160  */
2161 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
2162 {
2163 	struct sk_buff *skb = *pskb;
2164 	struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
2165 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
2166 	struct netvsc_vf_pcpu_stats *pcpu_stats
2167 		 = this_cpu_ptr(ndev_ctx->vf_stats);
2168 
2169 	skb = skb_share_check(skb, GFP_ATOMIC);
2170 	if (unlikely(!skb))
2171 		return RX_HANDLER_CONSUMED;
2172 
2173 	*pskb = skb;
2174 
2175 	skb->dev = ndev;
2176 
2177 	u64_stats_update_begin(&pcpu_stats->syncp);
2178 	pcpu_stats->rx_packets++;
2179 	pcpu_stats->rx_bytes += skb->len;
2180 	u64_stats_update_end(&pcpu_stats->syncp);
2181 
2182 	return RX_HANDLER_ANOTHER;
2183 }
2184 
2185 static int netvsc_vf_join(struct net_device *vf_netdev,
2186 			  struct net_device *ndev)
2187 {
2188 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
2189 	int ret;
2190 
2191 	ret = netdev_rx_handler_register(vf_netdev,
2192 					 netvsc_vf_handle_frame, ndev);
2193 	if (ret != 0) {
2194 		netdev_err(vf_netdev,
2195 			   "can not register netvsc VF receive handler (err = %d)\n",
2196 			   ret);
2197 		goto rx_handler_failed;
2198 	}
2199 
2200 	ret = netdev_master_upper_dev_link(vf_netdev, ndev,
2201 					   NULL, NULL, NULL);
2202 	if (ret != 0) {
2203 		netdev_err(vf_netdev,
2204 			   "can not set master device %s (err = %d)\n",
2205 			   ndev->name, ret);
2206 		goto upper_link_failed;
2207 	}
2208 
2209 	/* set slave flag before open to prevent IPv6 addrconf */
2210 	vf_netdev->flags |= IFF_SLAVE;
2211 
2212 	schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
2213 
2214 	call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
2215 
2216 	netdev_info(vf_netdev, "joined to %s\n", ndev->name);
2217 	return 0;
2218 
2219 upper_link_failed:
2220 	netdev_rx_handler_unregister(vf_netdev);
2221 rx_handler_failed:
2222 	return ret;
2223 }
2224 
2225 static void __netvsc_vf_setup(struct net_device *ndev,
2226 			      struct net_device *vf_netdev)
2227 {
2228 	int ret;
2229 
2230 	/* Align MTU of VF with master */
2231 	ret = dev_set_mtu(vf_netdev, ndev->mtu);
2232 	if (ret)
2233 		netdev_warn(vf_netdev,
2234 			    "unable to change mtu to %u\n", ndev->mtu);
2235 
2236 	/* set multicast etc flags on VF */
2237 	dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE, NULL);
2238 
2239 	/* sync address list from ndev to VF */
2240 	netif_addr_lock_bh(ndev);
2241 	dev_uc_sync(vf_netdev, ndev);
2242 	dev_mc_sync(vf_netdev, ndev);
2243 	netif_addr_unlock_bh(ndev);
2244 
2245 	if (netif_running(ndev)) {
2246 		ret = dev_open(vf_netdev, NULL);
2247 		if (ret)
2248 			netdev_warn(vf_netdev,
2249 				    "unable to open: %d\n", ret);
2250 	}
2251 }
2252 
2253 /* Setup VF as slave of the synthetic device.
2254  * Runs in workqueue to avoid recursion in netlink callbacks.
2255  */
2256 static void netvsc_vf_setup(struct work_struct *w)
2257 {
2258 	struct net_device_context *ndev_ctx
2259 		= container_of(w, struct net_device_context, vf_takeover.work);
2260 	struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
2261 	struct net_device *vf_netdev;
2262 
2263 	if (!rtnl_trylock()) {
2264 		schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
2265 		return;
2266 	}
2267 
2268 	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2269 	if (vf_netdev)
2270 		__netvsc_vf_setup(ndev, vf_netdev);
2271 
2272 	rtnl_unlock();
2273 }
2274 
2275 /* Find netvsc by VF serial number.
2276  * The PCI hyperv controller records the serial number as the slot kobj name.
2277  */
2278 static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
2279 {
2280 	struct device *parent = vf_netdev->dev.parent;
2281 	struct net_device_context *ndev_ctx;
2282 	struct pci_dev *pdev;
2283 	u32 serial;
2284 
2285 	if (!parent || !dev_is_pci(parent))
2286 		return NULL; /* not a PCI device */
2287 
2288 	pdev = to_pci_dev(parent);
2289 	if (!pdev->slot) {
2290 		netdev_notice(vf_netdev, "no PCI slot information\n");
2291 		return NULL;
2292 	}
2293 
2294 	if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
2295 		netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
2296 			      pci_slot_name(pdev->slot));
2297 		return NULL;
2298 	}
2299 
2300 	list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
2301 		if (!ndev_ctx->vf_alloc)
2302 			continue;
2303 
2304 		if (ndev_ctx->vf_serial == serial)
2305 			return hv_get_drvdata(ndev_ctx->device_ctx);
2306 	}
2307 
2308 	netdev_notice(vf_netdev,
2309 		      "no netdev found for vf serial:%u\n", serial);
2310 	return NULL;
2311 }
2312 
2313 static int netvsc_register_vf(struct net_device *vf_netdev)
2314 {
2315 	struct net_device_context *net_device_ctx;
2316 	struct netvsc_device *netvsc_dev;
2317 	struct bpf_prog *prog;
2318 	struct net_device *ndev;
2319 	int ret;
2320 
2321 	if (vf_netdev->addr_len != ETH_ALEN)
2322 		return NOTIFY_DONE;
2323 
2324 	ndev = get_netvsc_byslot(vf_netdev);
2325 	if (!ndev)
2326 		return NOTIFY_DONE;
2327 
2328 	net_device_ctx = netdev_priv(ndev);
2329 	netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2330 	if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
2331 		return NOTIFY_DONE;
2332 
2333 	/* if synthetic interface is a different namespace,
2334 	 * then move the VF to that namespace; join will be
2335 	 * done again in that context.
2336 	 */
2337 	if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
2338 		ret = dev_change_net_namespace(vf_netdev,
2339 					       dev_net(ndev), "eth%d");
2340 		if (ret)
2341 			netdev_err(vf_netdev,
2342 				   "could not move to same namespace as %s: %d\n",
2343 				   ndev->name, ret);
2344 		else
2345 			netdev_info(vf_netdev,
2346 				    "VF moved to namespace with: %s\n",
2347 				    ndev->name);
2348 		return NOTIFY_DONE;
2349 	}
2350 
2351 	netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
2352 
2353 	if (netvsc_vf_join(vf_netdev, ndev) != 0)
2354 		return NOTIFY_DONE;
2355 
2356 	dev_hold(vf_netdev);
2357 	rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
2358 
2359 	vf_netdev->wanted_features = ndev->features;
2360 	netdev_update_features(vf_netdev);
2361 
2362 	prog = netvsc_xdp_get(netvsc_dev);
2363 	netvsc_vf_setxdp(vf_netdev, prog);
2364 
2365 	return NOTIFY_OK;
2366 }
2367 
2368 /* VF up/down change detected, schedule to change data path */
2369 static int netvsc_vf_changed(struct net_device *vf_netdev)
2370 {
2371 	struct net_device_context *net_device_ctx;
2372 	struct netvsc_device *netvsc_dev;
2373 	struct net_device *ndev;
2374 	bool vf_is_up = netif_running(vf_netdev);
2375 
2376 	ndev = get_netvsc_byref(vf_netdev);
2377 	if (!ndev)
2378 		return NOTIFY_DONE;
2379 
2380 	net_device_ctx = netdev_priv(ndev);
2381 	netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
2382 	if (!netvsc_dev)
2383 		return NOTIFY_DONE;
2384 
2385 	netvsc_switch_datapath(ndev, vf_is_up);
2386 	netdev_info(ndev, "Data path switched %s VF: %s\n",
2387 		    vf_is_up ? "to" : "from", vf_netdev->name);
2388 
2389 	return NOTIFY_OK;
2390 }
2391 
2392 static int netvsc_unregister_vf(struct net_device *vf_netdev)
2393 {
2394 	struct net_device *ndev;
2395 	struct net_device_context *net_device_ctx;
2396 
2397 	ndev = get_netvsc_byref(vf_netdev);
2398 	if (!ndev)
2399 		return NOTIFY_DONE;
2400 
2401 	net_device_ctx = netdev_priv(ndev);
2402 	cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
2403 
2404 	netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
2405 
2406 	netvsc_vf_setxdp(vf_netdev, NULL);
2407 
2408 	netdev_rx_handler_unregister(vf_netdev);
2409 	netdev_upper_dev_unlink(vf_netdev, ndev);
2410 	RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
2411 	dev_put(vf_netdev);
2412 
2413 	return NOTIFY_OK;
2414 }
2415 
2416 static int netvsc_probe(struct hv_device *dev,
2417 			const struct hv_vmbus_device_id *dev_id)
2418 {
2419 	struct net_device *net = NULL;
2420 	struct net_device_context *net_device_ctx;
2421 	struct netvsc_device_info *device_info = NULL;
2422 	struct netvsc_device *nvdev;
2423 	int ret = -ENOMEM;
2424 
2425 	net = alloc_etherdev_mq(sizeof(struct net_device_context),
2426 				VRSS_CHANNEL_MAX);
2427 	if (!net)
2428 		goto no_net;
2429 
2430 	netif_carrier_off(net);
2431 
2432 	netvsc_init_settings(net);
2433 
2434 	net_device_ctx = netdev_priv(net);
2435 	net_device_ctx->device_ctx = dev;
2436 	net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
2437 	if (netif_msg_probe(net_device_ctx))
2438 		netdev_dbg(net, "netvsc msg_enable: %d\n",
2439 			   net_device_ctx->msg_enable);
2440 
2441 	hv_set_drvdata(dev, net);
2442 
2443 	INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
2444 
2445 	spin_lock_init(&net_device_ctx->lock);
2446 	INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
2447 	INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
2448 
2449 	net_device_ctx->vf_stats
2450 		= netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
2451 	if (!net_device_ctx->vf_stats)
2452 		goto no_stats;
2453 
2454 	net->netdev_ops = &device_ops;
2455 	net->ethtool_ops = &ethtool_ops;
2456 	SET_NETDEV_DEV(net, &dev->device);
2457 
2458 	/* We always need headroom for rndis header */
2459 	net->needed_headroom = RNDIS_AND_PPI_SIZE;
2460 
2461 	/* Initialize the number of queues to be 1, we may change it if more
2462 	 * channels are offered later.
2463 	 */
2464 	netif_set_real_num_tx_queues(net, 1);
2465 	netif_set_real_num_rx_queues(net, 1);
2466 
2467 	/* Notify the netvsc driver of the new device */
2468 	device_info = netvsc_devinfo_get(NULL);
2469 
2470 	if (!device_info) {
2471 		ret = -ENOMEM;
2472 		goto devinfo_failed;
2473 	}
2474 
2475 	nvdev = rndis_filter_device_add(dev, device_info);
2476 	if (IS_ERR(nvdev)) {
2477 		ret = PTR_ERR(nvdev);
2478 		netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
2479 		goto rndis_failed;
2480 	}
2481 
2482 	memcpy(net->dev_addr, device_info->mac_adr, ETH_ALEN);
2483 
2484 	/* We must get rtnl lock before scheduling nvdev->subchan_work,
2485 	 * otherwise netvsc_subchan_work() can get rtnl lock first and wait
2486 	 * all subchannels to show up, but that may not happen because
2487 	 * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
2488 	 * -> ... -> device_add() -> ... -> __device_attach() can't get
2489 	 * the device lock, so all the subchannels can't be processed --
2490 	 * finally netvsc_subchan_work() hangs forever.
2491 	 */
2492 	rtnl_lock();
2493 
2494 	if (nvdev->num_chn > 1)
2495 		schedule_work(&nvdev->subchan_work);
2496 
2497 	/* hw_features computed in rndis_netdev_set_hwcaps() */
2498 	net->features = net->hw_features |
2499 		NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX |
2500 		NETIF_F_HW_VLAN_CTAG_RX;
2501 	net->vlan_features = net->features;
2502 
2503 	netdev_lockdep_set_classes(net);
2504 
2505 	/* MTU range: 68 - 1500 or 65521 */
2506 	net->min_mtu = NETVSC_MTU_MIN;
2507 	if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2508 		net->max_mtu = NETVSC_MTU - ETH_HLEN;
2509 	else
2510 		net->max_mtu = ETH_DATA_LEN;
2511 
2512 	nvdev->tx_disable = false;
2513 
2514 	ret = register_netdevice(net);
2515 	if (ret != 0) {
2516 		pr_err("Unable to register netdev.\n");
2517 		goto register_failed;
2518 	}
2519 
2520 	list_add(&net_device_ctx->list, &netvsc_dev_list);
2521 	rtnl_unlock();
2522 
2523 	netvsc_devinfo_put(device_info);
2524 	return 0;
2525 
2526 register_failed:
2527 	rtnl_unlock();
2528 	rndis_filter_device_remove(dev, nvdev);
2529 rndis_failed:
2530 	netvsc_devinfo_put(device_info);
2531 devinfo_failed:
2532 	free_percpu(net_device_ctx->vf_stats);
2533 no_stats:
2534 	hv_set_drvdata(dev, NULL);
2535 	free_netdev(net);
2536 no_net:
2537 	return ret;
2538 }
2539 
2540 static int netvsc_remove(struct hv_device *dev)
2541 {
2542 	struct net_device_context *ndev_ctx;
2543 	struct net_device *vf_netdev, *net;
2544 	struct netvsc_device *nvdev;
2545 
2546 	net = hv_get_drvdata(dev);
2547 	if (net == NULL) {
2548 		dev_err(&dev->device, "No net device to remove\n");
2549 		return 0;
2550 	}
2551 
2552 	ndev_ctx = netdev_priv(net);
2553 
2554 	cancel_delayed_work_sync(&ndev_ctx->dwork);
2555 
2556 	rtnl_lock();
2557 	nvdev = rtnl_dereference(ndev_ctx->nvdev);
2558 	if (nvdev) {
2559 		cancel_work_sync(&nvdev->subchan_work);
2560 		netvsc_xdp_set(net, NULL, NULL, nvdev);
2561 	}
2562 
2563 	/*
2564 	 * Call to the vsc driver to let it know that the device is being
2565 	 * removed. Also blocks mtu and channel changes.
2566 	 */
2567 	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2568 	if (vf_netdev)
2569 		netvsc_unregister_vf(vf_netdev);
2570 
2571 	if (nvdev)
2572 		rndis_filter_device_remove(dev, nvdev);
2573 
2574 	unregister_netdevice(net);
2575 	list_del(&ndev_ctx->list);
2576 
2577 	rtnl_unlock();
2578 
2579 	hv_set_drvdata(dev, NULL);
2580 
2581 	free_percpu(ndev_ctx->vf_stats);
2582 	free_netdev(net);
2583 	return 0;
2584 }
2585 
2586 static int netvsc_suspend(struct hv_device *dev)
2587 {
2588 	struct net_device_context *ndev_ctx;
2589 	struct net_device *vf_netdev, *net;
2590 	struct netvsc_device *nvdev;
2591 	int ret;
2592 
2593 	net = hv_get_drvdata(dev);
2594 
2595 	ndev_ctx = netdev_priv(net);
2596 	cancel_delayed_work_sync(&ndev_ctx->dwork);
2597 
2598 	rtnl_lock();
2599 
2600 	nvdev = rtnl_dereference(ndev_ctx->nvdev);
2601 	if (nvdev == NULL) {
2602 		ret = -ENODEV;
2603 		goto out;
2604 	}
2605 
2606 	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2607 	if (vf_netdev)
2608 		netvsc_unregister_vf(vf_netdev);
2609 
2610 	/* Save the current config info */
2611 	ndev_ctx->saved_netvsc_dev_info = netvsc_devinfo_get(nvdev);
2612 
2613 	ret = netvsc_detach(net, nvdev);
2614 out:
2615 	rtnl_unlock();
2616 
2617 	return ret;
2618 }
2619 
2620 static int netvsc_resume(struct hv_device *dev)
2621 {
2622 	struct net_device *net = hv_get_drvdata(dev);
2623 	struct net_device_context *net_device_ctx;
2624 	struct netvsc_device_info *device_info;
2625 	int ret;
2626 
2627 	rtnl_lock();
2628 
2629 	net_device_ctx = netdev_priv(net);
2630 	device_info = net_device_ctx->saved_netvsc_dev_info;
2631 
2632 	ret = netvsc_attach(net, device_info);
2633 
2634 	netvsc_devinfo_put(device_info);
2635 	net_device_ctx->saved_netvsc_dev_info = NULL;
2636 
2637 	rtnl_unlock();
2638 
2639 	return ret;
2640 }
2641 static const struct hv_vmbus_device_id id_table[] = {
2642 	/* Network guid */
2643 	{ HV_NIC_GUID, },
2644 	{ },
2645 };
2646 
2647 MODULE_DEVICE_TABLE(vmbus, id_table);
2648 
2649 /* The one and only one */
2650 static struct  hv_driver netvsc_drv = {
2651 	.name = KBUILD_MODNAME,
2652 	.id_table = id_table,
2653 	.probe = netvsc_probe,
2654 	.remove = netvsc_remove,
2655 	.suspend = netvsc_suspend,
2656 	.resume = netvsc_resume,
2657 	.driver = {
2658 		.probe_type = PROBE_FORCE_SYNCHRONOUS,
2659 	},
2660 };
2661 
2662 /*
2663  * On Hyper-V, every VF interface is matched with a corresponding
2664  * synthetic interface. The synthetic interface is presented first
2665  * to the guest. When the corresponding VF instance is registered,
2666  * we will take care of switching the data path.
2667  */
2668 static int netvsc_netdev_event(struct notifier_block *this,
2669 			       unsigned long event, void *ptr)
2670 {
2671 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2672 
2673 	/* Skip our own events */
2674 	if (event_dev->netdev_ops == &device_ops)
2675 		return NOTIFY_DONE;
2676 
2677 	/* Avoid non-Ethernet type devices */
2678 	if (event_dev->type != ARPHRD_ETHER)
2679 		return NOTIFY_DONE;
2680 
2681 	/* Avoid Vlan dev with same MAC registering as VF */
2682 	if (is_vlan_dev(event_dev))
2683 		return NOTIFY_DONE;
2684 
2685 	/* Avoid Bonding master dev with same MAC registering as VF */
2686 	if ((event_dev->priv_flags & IFF_BONDING) &&
2687 	    (event_dev->flags & IFF_MASTER))
2688 		return NOTIFY_DONE;
2689 
2690 	switch (event) {
2691 	case NETDEV_REGISTER:
2692 		return netvsc_register_vf(event_dev);
2693 	case NETDEV_UNREGISTER:
2694 		return netvsc_unregister_vf(event_dev);
2695 	case NETDEV_UP:
2696 	case NETDEV_DOWN:
2697 		return netvsc_vf_changed(event_dev);
2698 	default:
2699 		return NOTIFY_DONE;
2700 	}
2701 }
2702 
2703 static struct notifier_block netvsc_netdev_notifier = {
2704 	.notifier_call = netvsc_netdev_event,
2705 };
2706 
2707 static void __exit netvsc_drv_exit(void)
2708 {
2709 	unregister_netdevice_notifier(&netvsc_netdev_notifier);
2710 	vmbus_driver_unregister(&netvsc_drv);
2711 }
2712 
2713 static int __init netvsc_drv_init(void)
2714 {
2715 	int ret;
2716 
2717 	if (ring_size < RING_SIZE_MIN) {
2718 		ring_size = RING_SIZE_MIN;
2719 		pr_info("Increased ring_size to %u (min allowed)\n",
2720 			ring_size);
2721 	}
2722 	netvsc_ring_bytes = ring_size * PAGE_SIZE;
2723 
2724 	ret = vmbus_driver_register(&netvsc_drv);
2725 	if (ret)
2726 		return ret;
2727 
2728 	register_netdevice_notifier(&netvsc_netdev_notifier);
2729 	return 0;
2730 }
2731 
2732 MODULE_LICENSE("GPL");
2733 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2734 
2735 module_init(netvsc_drv_init);
2736 module_exit(netvsc_drv_exit);
2737