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