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