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