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