netvsc: reduce maximum GSO size
[firefly-linux-kernel-4.4.55.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 <net/arp.h>
37 #include <net/route.h>
38 #include <net/sock.h>
39 #include <net/pkt_sched.h>
40
41 #include "hyperv_net.h"
42
43 /* Restrict GSO size to account for NVGRE */
44 #define NETVSC_GSO_MAX_SIZE     62768
45
46 #define RING_SIZE_MIN 64
47 static int ring_size = 128;
48 module_param(ring_size, int, S_IRUGO);
49 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
50
51 static int max_num_vrss_chns = 8;
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, S_IRUGO);
60 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
61
62 static void do_set_multicast(struct work_struct *w)
63 {
64         struct net_device_context *ndevctx =
65                 container_of(w, struct net_device_context, work);
66         struct netvsc_device *nvdev;
67         struct rndis_device *rdev;
68
69         nvdev = hv_get_drvdata(ndevctx->device_ctx);
70         if (nvdev == NULL || nvdev->ndev == NULL)
71                 return;
72
73         rdev = nvdev->extension;
74         if (rdev == NULL)
75                 return;
76
77         if (nvdev->ndev->flags & IFF_PROMISC)
78                 rndis_filter_set_packet_filter(rdev,
79                         NDIS_PACKET_TYPE_PROMISCUOUS);
80         else
81                 rndis_filter_set_packet_filter(rdev,
82                         NDIS_PACKET_TYPE_BROADCAST |
83                         NDIS_PACKET_TYPE_ALL_MULTICAST |
84                         NDIS_PACKET_TYPE_DIRECTED);
85 }
86
87 static void netvsc_set_multicast_list(struct net_device *net)
88 {
89         struct net_device_context *net_device_ctx = netdev_priv(net);
90
91         schedule_work(&net_device_ctx->work);
92 }
93
94 static int netvsc_open(struct net_device *net)
95 {
96         struct net_device_context *net_device_ctx = netdev_priv(net);
97         struct hv_device *device_obj = net_device_ctx->device_ctx;
98         struct netvsc_device *nvdev;
99         struct rndis_device *rdev;
100         int ret = 0;
101
102         netif_carrier_off(net);
103
104         /* Open up the device */
105         ret = rndis_filter_open(device_obj);
106         if (ret != 0) {
107                 netdev_err(net, "unable to open device (ret %d).\n", ret);
108                 return ret;
109         }
110
111         netif_tx_wake_all_queues(net);
112
113         nvdev = hv_get_drvdata(device_obj);
114         rdev = nvdev->extension;
115         if (!rdev->link_state)
116                 netif_carrier_on(net);
117
118         return ret;
119 }
120
121 static int netvsc_close(struct net_device *net)
122 {
123         struct net_device_context *net_device_ctx = netdev_priv(net);
124         struct hv_device *device_obj = net_device_ctx->device_ctx;
125         struct netvsc_device *nvdev = hv_get_drvdata(device_obj);
126         int ret;
127         u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
128         struct vmbus_channel *chn;
129
130         netif_tx_disable(net);
131
132         /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
133         cancel_work_sync(&net_device_ctx->work);
134         ret = rndis_filter_close(device_obj);
135         if (ret != 0) {
136                 netdev_err(net, "unable to close device (ret %d).\n", ret);
137                 return ret;
138         }
139
140         /* Ensure pending bytes in ring are read */
141         while (true) {
142                 aread = 0;
143                 for (i = 0; i < nvdev->num_chn; i++) {
144                         chn = nvdev->chn_table[i];
145                         if (!chn)
146                                 continue;
147
148                         hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
149                                                      &awrite);
150
151                         if (aread)
152                                 break;
153
154                         hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
155                                                      &awrite);
156
157                         if (aread)
158                                 break;
159                 }
160
161                 retry++;
162                 if (retry > retry_max || aread == 0)
163                         break;
164
165                 msleep(msec);
166
167                 if (msec < 1000)
168                         msec *= 2;
169         }
170
171         if (aread) {
172                 netdev_err(net, "Ring buffer not empty after closing rndis\n");
173                 ret = -ETIMEDOUT;
174         }
175
176         return ret;
177 }
178
179 static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
180                                 int pkt_type)
181 {
182         struct rndis_packet *rndis_pkt;
183         struct rndis_per_packet_info *ppi;
184
185         rndis_pkt = &msg->msg.pkt;
186         rndis_pkt->data_offset += ppi_size;
187
188         ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
189                 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
190
191         ppi->size = ppi_size;
192         ppi->type = pkt_type;
193         ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
194
195         rndis_pkt->per_pkt_info_len += ppi_size;
196
197         return ppi;
198 }
199
200 union sub_key {
201         u64 k;
202         struct {
203                 u8 pad[3];
204                 u8 kb;
205                 u32 ka;
206         };
207 };
208
209 /* Toeplitz hash function
210  * data: network byte order
211  * return: host byte order
212  */
213 static u32 comp_hash(u8 *key, int klen, void *data, int dlen)
214 {
215         union sub_key subk;
216         int k_next = 4;
217         u8 dt;
218         int i, j;
219         u32 ret = 0;
220
221         subk.k = 0;
222         subk.ka = ntohl(*(u32 *)key);
223
224         for (i = 0; i < dlen; i++) {
225                 subk.kb = key[k_next];
226                 k_next = (k_next + 1) % klen;
227                 dt = ((u8 *)data)[i];
228                 for (j = 0; j < 8; j++) {
229                         if (dt & 0x80)
230                                 ret ^= subk.ka;
231                         dt <<= 1;
232                         subk.k <<= 1;
233                 }
234         }
235
236         return ret;
237 }
238
239 static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)
240 {
241         struct flow_keys flow;
242         int data_len;
243
244         if (!skb_flow_dissect_flow_keys(skb, &flow, 0) ||
245             !(flow.basic.n_proto == htons(ETH_P_IP) ||
246               flow.basic.n_proto == htons(ETH_P_IPV6)))
247                 return false;
248
249         if (flow.basic.ip_proto == IPPROTO_TCP)
250                 data_len = 12;
251         else
252                 data_len = 8;
253
254         *hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
255
256         return true;
257 }
258
259 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
260                         void *accel_priv, select_queue_fallback_t fallback)
261 {
262         struct net_device_context *net_device_ctx = netdev_priv(ndev);
263         struct hv_device *hdev =  net_device_ctx->device_ctx;
264         struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
265         u32 hash;
266         u16 q_idx = 0;
267
268         if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
269                 return 0;
270
271         if (netvsc_set_hash(&hash, skb)) {
272                 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
273                         ndev->real_num_tx_queues;
274                 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
275         }
276
277         return q_idx;
278 }
279
280 void netvsc_xmit_completion(void *context)
281 {
282         struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
283         struct sk_buff *skb = (struct sk_buff *)
284                 (unsigned long)packet->send_completion_tid;
285
286         if (skb)
287                 dev_kfree_skb_any(skb);
288 }
289
290 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
291                         struct hv_page_buffer *pb)
292 {
293         int j = 0;
294
295         /* Deal with compund pages by ignoring unused part
296          * of the page.
297          */
298         page += (offset >> PAGE_SHIFT);
299         offset &= ~PAGE_MASK;
300
301         while (len > 0) {
302                 unsigned long bytes;
303
304                 bytes = PAGE_SIZE - offset;
305                 if (bytes > len)
306                         bytes = len;
307                 pb[j].pfn = page_to_pfn(page);
308                 pb[j].offset = offset;
309                 pb[j].len = bytes;
310
311                 offset += bytes;
312                 len -= bytes;
313
314                 if (offset == PAGE_SIZE && len) {
315                         page++;
316                         offset = 0;
317                         j++;
318                 }
319         }
320
321         return j + 1;
322 }
323
324 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
325                            struct hv_netvsc_packet *packet)
326 {
327         struct hv_page_buffer *pb = packet->page_buf;
328         u32 slots_used = 0;
329         char *data = skb->data;
330         int frags = skb_shinfo(skb)->nr_frags;
331         int i;
332
333         /* The packet is laid out thus:
334          * 1. hdr: RNDIS header and PPI
335          * 2. skb linear data
336          * 3. skb fragment data
337          */
338         if (hdr != NULL)
339                 slots_used += fill_pg_buf(virt_to_page(hdr),
340                                         offset_in_page(hdr),
341                                         len, &pb[slots_used]);
342
343         packet->rmsg_size = len;
344         packet->rmsg_pgcnt = slots_used;
345
346         slots_used += fill_pg_buf(virt_to_page(data),
347                                 offset_in_page(data),
348                                 skb_headlen(skb), &pb[slots_used]);
349
350         for (i = 0; i < frags; i++) {
351                 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
352
353                 slots_used += fill_pg_buf(skb_frag_page(frag),
354                                         frag->page_offset,
355                                         skb_frag_size(frag), &pb[slots_used]);
356         }
357         return slots_used;
358 }
359
360 static int count_skb_frag_slots(struct sk_buff *skb)
361 {
362         int i, frags = skb_shinfo(skb)->nr_frags;
363         int pages = 0;
364
365         for (i = 0; i < frags; i++) {
366                 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
367                 unsigned long size = skb_frag_size(frag);
368                 unsigned long offset = frag->page_offset;
369
370                 /* Skip unused frames from start of page */
371                 offset &= ~PAGE_MASK;
372                 pages += PFN_UP(offset + size);
373         }
374         return pages;
375 }
376
377 static int netvsc_get_slots(struct sk_buff *skb)
378 {
379         char *data = skb->data;
380         unsigned int offset = offset_in_page(data);
381         unsigned int len = skb_headlen(skb);
382         int slots;
383         int frag_slots;
384
385         slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
386         frag_slots = count_skb_frag_slots(skb);
387         return slots + frag_slots;
388 }
389
390 static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
391 {
392         u32 ret_val = TRANSPORT_INFO_NOT_IP;
393
394         if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
395                 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
396                 goto not_ip;
397         }
398
399         *trans_off = skb_transport_offset(skb);
400
401         if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
402                 struct iphdr *iphdr = ip_hdr(skb);
403
404                 if (iphdr->protocol == IPPROTO_TCP)
405                         ret_val = TRANSPORT_INFO_IPV4_TCP;
406                 else if (iphdr->protocol == IPPROTO_UDP)
407                         ret_val = TRANSPORT_INFO_IPV4_UDP;
408         } else {
409                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
410                         ret_val = TRANSPORT_INFO_IPV6_TCP;
411                 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
412                         ret_val = TRANSPORT_INFO_IPV6_UDP;
413         }
414
415 not_ip:
416         return ret_val;
417 }
418
419 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
420 {
421         struct net_device_context *net_device_ctx = netdev_priv(net);
422         struct hv_netvsc_packet *packet = NULL;
423         int ret;
424         unsigned int num_data_pgs;
425         struct rndis_message *rndis_msg;
426         struct rndis_packet *rndis_pkt;
427         u32 rndis_msg_size;
428         bool isvlan;
429         bool linear = false;
430         struct rndis_per_packet_info *ppi;
431         struct ndis_tcp_ip_checksum_info *csum_info;
432         struct ndis_tcp_lso_info *lso_info;
433         int  hdr_offset;
434         u32 net_trans_info;
435         u32 hash;
436         u32 skb_length;
437         u32 pkt_sz;
438         struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
439         struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
440
441         /* We will atmost need two pages to describe the rndis
442          * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
443          * of pages in a single packet. If skb is scattered around
444          * more pages we try linearizing it.
445          */
446
447 check_size:
448         skb_length = skb->len;
449         num_data_pgs = netvsc_get_slots(skb) + 2;
450         if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
451                 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
452                                       num_data_pgs, skb->len);
453                 ret = -EFAULT;
454                 goto drop;
455         } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
456                 if (skb_linearize(skb)) {
457                         net_alert_ratelimited("failed to linearize skb\n");
458                         ret = -ENOMEM;
459                         goto drop;
460                 }
461                 linear = true;
462                 goto check_size;
463         }
464
465         pkt_sz = sizeof(struct hv_netvsc_packet) + RNDIS_AND_PPI_SIZE;
466
467         ret = skb_cow_head(skb, pkt_sz);
468         if (ret) {
469                 netdev_err(net, "unable to alloc hv_netvsc_packet\n");
470                 ret = -ENOMEM;
471                 goto drop;
472         }
473         /* Use the headroom for building up the packet */
474         packet = (struct hv_netvsc_packet *)skb->head;
475
476         packet->status = 0;
477         packet->xmit_more = skb->xmit_more;
478
479         packet->vlan_tci = skb->vlan_tci;
480         packet->page_buf = page_buf;
481
482         packet->q_idx = skb_get_queue_mapping(skb);
483
484         packet->is_data_pkt = true;
485         packet->total_data_buflen = skb->len;
486
487         packet->rndis_msg = (struct rndis_message *)((unsigned long)packet +
488                                 sizeof(struct hv_netvsc_packet));
489
490         memset(packet->rndis_msg, 0, RNDIS_AND_PPI_SIZE);
491
492         /* Set the completion routine */
493         packet->send_completion = netvsc_xmit_completion;
494         packet->send_completion_ctx = packet;
495         packet->send_completion_tid = (unsigned long)skb;
496
497         isvlan = packet->vlan_tci & VLAN_TAG_PRESENT;
498
499         /* Add the rndis header */
500         rndis_msg = packet->rndis_msg;
501         rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
502         rndis_msg->msg_len = packet->total_data_buflen;
503         rndis_pkt = &rndis_msg->msg.pkt;
504         rndis_pkt->data_offset = sizeof(struct rndis_packet);
505         rndis_pkt->data_len = packet->total_data_buflen;
506         rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
507
508         rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
509
510         hash = skb_get_hash_raw(skb);
511         if (hash != 0 && net->real_num_tx_queues > 1) {
512                 rndis_msg_size += NDIS_HASH_PPI_SIZE;
513                 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
514                                     NBL_HASH_VALUE);
515                 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
516         }
517
518         if (isvlan) {
519                 struct ndis_pkt_8021q_info *vlan;
520
521                 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
522                 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
523                                         IEEE_8021Q_INFO);
524                 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
525                                                 ppi->ppi_offset);
526                 vlan->vlanid = packet->vlan_tci & VLAN_VID_MASK;
527                 vlan->pri = (packet->vlan_tci & VLAN_PRIO_MASK) >>
528                                 VLAN_PRIO_SHIFT;
529         }
530
531         net_trans_info = get_net_transport_info(skb, &hdr_offset);
532         if (net_trans_info == TRANSPORT_INFO_NOT_IP)
533                 goto do_send;
534
535         /*
536          * Setup the sendside checksum offload only if this is not a
537          * GSO packet.
538          */
539         if (skb_is_gso(skb))
540                 goto do_lso;
541
542         if ((skb->ip_summed == CHECKSUM_NONE) ||
543             (skb->ip_summed == CHECKSUM_UNNECESSARY))
544                 goto do_send;
545
546         rndis_msg_size += NDIS_CSUM_PPI_SIZE;
547         ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
548                             TCPIP_CHKSUM_PKTINFO);
549
550         csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
551                         ppi->ppi_offset);
552
553         if (net_trans_info & (INFO_IPV4 << 16))
554                 csum_info->transmit.is_ipv4 = 1;
555         else
556                 csum_info->transmit.is_ipv6 = 1;
557
558         if (net_trans_info & INFO_TCP) {
559                 csum_info->transmit.tcp_checksum = 1;
560                 csum_info->transmit.tcp_header_offset = hdr_offset;
561         } else if (net_trans_info & INFO_UDP) {
562                 /* UDP checksum offload is not supported on ws2008r2.
563                  * Furthermore, on ws2012 and ws2012r2, there are some
564                  * issues with udp checksum offload from Linux guests.
565                  * (these are host issues).
566                  * For now compute the checksum here.
567                  */
568                 struct udphdr *uh;
569                 u16 udp_len;
570
571                 ret = skb_cow_head(skb, 0);
572                 if (ret)
573                         goto drop;
574
575                 uh = udp_hdr(skb);
576                 udp_len = ntohs(uh->len);
577                 uh->check = 0;
578                 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
579                                               ip_hdr(skb)->daddr,
580                                               udp_len, IPPROTO_UDP,
581                                               csum_partial(uh, udp_len, 0));
582                 if (uh->check == 0)
583                         uh->check = CSUM_MANGLED_0;
584
585                 csum_info->transmit.udp_checksum = 0;
586         }
587         goto do_send;
588
589 do_lso:
590         rndis_msg_size += NDIS_LSO_PPI_SIZE;
591         ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
592                             TCP_LARGESEND_PKTINFO);
593
594         lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
595                         ppi->ppi_offset);
596
597         lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
598         if (net_trans_info & (INFO_IPV4 << 16)) {
599                 lso_info->lso_v2_transmit.ip_version =
600                         NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
601                 ip_hdr(skb)->tot_len = 0;
602                 ip_hdr(skb)->check = 0;
603                 tcp_hdr(skb)->check =
604                 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
605                                    ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
606         } else {
607                 lso_info->lso_v2_transmit.ip_version =
608                         NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
609                 ipv6_hdr(skb)->payload_len = 0;
610                 tcp_hdr(skb)->check =
611                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
612                                 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
613         }
614         lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
615         lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
616
617 do_send:
618         /* Start filling in the page buffers with the rndis hdr */
619         rndis_msg->msg_len += rndis_msg_size;
620         packet->total_data_buflen = rndis_msg->msg_len;
621         packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
622                                                skb, packet);
623
624         ret = netvsc_send(net_device_ctx->device_ctx, packet);
625
626 drop:
627         if (ret == 0) {
628                 u64_stats_update_begin(&tx_stats->syncp);
629                 tx_stats->packets++;
630                 tx_stats->bytes += skb_length;
631                 u64_stats_update_end(&tx_stats->syncp);
632         } else {
633                 if (ret != -EAGAIN) {
634                         dev_kfree_skb_any(skb);
635                         net->stats.tx_dropped++;
636                 }
637         }
638
639         return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
640 }
641
642 /*
643  * netvsc_linkstatus_callback - Link up/down notification
644  */
645 void netvsc_linkstatus_callback(struct hv_device *device_obj,
646                                 struct rndis_message *resp)
647 {
648         struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
649         struct net_device *net;
650         struct net_device_context *ndev_ctx;
651         struct netvsc_device *net_device;
652         struct rndis_device *rdev;
653
654         net_device = hv_get_drvdata(device_obj);
655         rdev = net_device->extension;
656
657         switch (indicate->status) {
658         case RNDIS_STATUS_MEDIA_CONNECT:
659                 rdev->link_state = false;
660                 break;
661         case RNDIS_STATUS_MEDIA_DISCONNECT:
662                 rdev->link_state = true;
663                 break;
664         case RNDIS_STATUS_NETWORK_CHANGE:
665                 rdev->link_change = true;
666                 break;
667         default:
668                 return;
669         }
670
671         net = net_device->ndev;
672
673         if (!net || net->reg_state != NETREG_REGISTERED)
674                 return;
675
676         ndev_ctx = netdev_priv(net);
677         if (!rdev->link_state) {
678                 schedule_delayed_work(&ndev_ctx->dwork, 0);
679                 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
680         } else {
681                 schedule_delayed_work(&ndev_ctx->dwork, 0);
682         }
683 }
684
685 /*
686  * netvsc_recv_callback -  Callback when we receive a packet from the
687  * "wire" on the specified device.
688  */
689 int netvsc_recv_callback(struct hv_device *device_obj,
690                                 struct hv_netvsc_packet *packet,
691                                 struct ndis_tcp_ip_checksum_info *csum_info)
692 {
693         struct net_device *net;
694         struct net_device_context *net_device_ctx;
695         struct sk_buff *skb;
696         struct netvsc_stats *rx_stats;
697
698         net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
699         if (!net || net->reg_state != NETREG_REGISTERED) {
700                 packet->status = NVSP_STAT_FAIL;
701                 return 0;
702         }
703         net_device_ctx = netdev_priv(net);
704         rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
705
706         /* Allocate a skb - TODO direct I/O to pages? */
707         skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
708         if (unlikely(!skb)) {
709                 ++net->stats.rx_dropped;
710                 packet->status = NVSP_STAT_FAIL;
711                 return 0;
712         }
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         memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
719                 packet->total_data_buflen);
720
721         skb->protocol = eth_type_trans(skb, net);
722         if (csum_info) {
723                 /* We only look at the IP checksum here.
724                  * Should we be dropping the packet if checksum
725                  * failed? How do we deal with other checksums - TCP/UDP?
726                  */
727                 if (csum_info->receive.ip_checksum_succeeded)
728                         skb->ip_summed = CHECKSUM_UNNECESSARY;
729                 else
730                         skb->ip_summed = CHECKSUM_NONE;
731         }
732
733         if (packet->vlan_tci & VLAN_TAG_PRESENT)
734                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
735                                        packet->vlan_tci);
736
737         skb_record_rx_queue(skb, packet->channel->
738                             offermsg.offer.sub_channel_index);
739
740         u64_stats_update_begin(&rx_stats->syncp);
741         rx_stats->packets++;
742         rx_stats->bytes += packet->total_data_buflen;
743         u64_stats_update_end(&rx_stats->syncp);
744
745         /*
746          * Pass the skb back up. Network stack will deallocate the skb when it
747          * is done.
748          * TODO - use NAPI?
749          */
750         netif_rx(skb);
751
752         return 0;
753 }
754
755 static void netvsc_get_drvinfo(struct net_device *net,
756                                struct ethtool_drvinfo *info)
757 {
758         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
759         strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
760 }
761
762 static void netvsc_get_channels(struct net_device *net,
763                                 struct ethtool_channels *channel)
764 {
765         struct net_device_context *net_device_ctx = netdev_priv(net);
766         struct hv_device *dev = net_device_ctx->device_ctx;
767         struct netvsc_device *nvdev = hv_get_drvdata(dev);
768
769         if (nvdev) {
770                 channel->max_combined   = nvdev->max_chn;
771                 channel->combined_count = nvdev->num_chn;
772         }
773 }
774
775 static int netvsc_set_channels(struct net_device *net,
776                                struct ethtool_channels *channels)
777 {
778         struct net_device_context *net_device_ctx = netdev_priv(net);
779         struct hv_device *dev = net_device_ctx->device_ctx;
780         struct netvsc_device *nvdev = hv_get_drvdata(dev);
781         struct netvsc_device_info device_info;
782         u32 num_chn;
783         u32 max_chn;
784         int ret = 0;
785         bool recovering = false;
786
787         if (!nvdev || nvdev->destroy)
788                 return -ENODEV;
789
790         num_chn = nvdev->num_chn;
791         max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
792
793         if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
794                 pr_info("vRSS unsupported before NVSP Version 5\n");
795                 return -EINVAL;
796         }
797
798         /* We do not support rx, tx, or other */
799         if (!channels ||
800             channels->rx_count ||
801             channels->tx_count ||
802             channels->other_count ||
803             (channels->combined_count < 1))
804                 return -EINVAL;
805
806         if (channels->combined_count > max_chn) {
807                 pr_info("combined channels too high, using %d\n", max_chn);
808                 channels->combined_count = max_chn;
809         }
810
811         ret = netvsc_close(net);
812         if (ret)
813                 goto out;
814
815  do_set:
816         nvdev->start_remove = true;
817         rndis_filter_device_remove(dev);
818
819         nvdev->num_chn = channels->combined_count;
820
821         net_device_ctx->device_ctx = dev;
822         hv_set_drvdata(dev, net);
823
824         memset(&device_info, 0, sizeof(device_info));
825         device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
826         device_info.ring_size = ring_size;
827         device_info.max_num_vrss_chns = max_num_vrss_chns;
828
829         ret = rndis_filter_device_add(dev, &device_info);
830         if (ret) {
831                 if (recovering) {
832                         netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
833                         return ret;
834                 }
835                 goto recover;
836         }
837
838         nvdev = hv_get_drvdata(dev);
839
840         ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
841         if (ret) {
842                 if (recovering) {
843                         netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
844                         return ret;
845                 }
846                 goto recover;
847         }
848
849         ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
850         if (ret) {
851                 if (recovering) {
852                         netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
853                         return ret;
854                 }
855                 goto recover;
856         }
857         netif_set_gso_max_size(net, NETVSC_GSO_MAX_SIZE);
858
859  out:
860         netvsc_open(net);
861
862         return ret;
863
864  recover:
865         /* If the above failed, we attempt to recover through the same
866          * process but with the original number of channels.
867          */
868         netdev_err(net, "could not set channels, recovering\n");
869         recovering = true;
870         channels->combined_count = num_chn;
871         goto do_set;
872 }
873
874 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
875 {
876         struct net_device_context *ndevctx = netdev_priv(ndev);
877         struct hv_device *hdev =  ndevctx->device_ctx;
878         struct netvsc_device *nvdev = hv_get_drvdata(hdev);
879         struct netvsc_device_info device_info;
880         int limit = ETH_DATA_LEN;
881         int ret = 0;
882
883         if (nvdev == NULL || nvdev->destroy)
884                 return -ENODEV;
885
886         if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
887                 limit = NETVSC_MTU - ETH_HLEN;
888
889         if (mtu < NETVSC_MTU_MIN || mtu > limit)
890                 return -EINVAL;
891
892         ret = netvsc_close(ndev);
893         if (ret)
894                 goto out;
895
896         nvdev->start_remove = true;
897         rndis_filter_device_remove(hdev);
898
899         ndev->mtu = mtu;
900
901         ndevctx->device_ctx = hdev;
902         hv_set_drvdata(hdev, ndev);
903
904         memset(&device_info, 0, sizeof(device_info));
905         device_info.ring_size = ring_size;
906         device_info.num_chn = nvdev->num_chn;
907         device_info.max_num_vrss_chns = max_num_vrss_chns;
908         rndis_filter_device_add(hdev, &device_info);
909
910 out:
911         netvsc_open(ndev);
912
913         return ret;
914 }
915
916 static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
917                                                     struct rtnl_link_stats64 *t)
918 {
919         struct net_device_context *ndev_ctx = netdev_priv(net);
920         int cpu;
921
922         for_each_possible_cpu(cpu) {
923                 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
924                                                             cpu);
925                 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
926                                                             cpu);
927                 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
928                 unsigned int start;
929
930                 do {
931                         start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
932                         tx_packets = tx_stats->packets;
933                         tx_bytes = tx_stats->bytes;
934                 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
935
936                 do {
937                         start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
938                         rx_packets = rx_stats->packets;
939                         rx_bytes = rx_stats->bytes;
940                 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
941
942                 t->tx_bytes     += tx_bytes;
943                 t->tx_packets   += tx_packets;
944                 t->rx_bytes     += rx_bytes;
945                 t->rx_packets   += rx_packets;
946         }
947
948         t->tx_dropped   = net->stats.tx_dropped;
949         t->tx_errors    = net->stats.tx_dropped;
950
951         t->rx_dropped   = net->stats.rx_dropped;
952         t->rx_errors    = net->stats.rx_errors;
953
954         return t;
955 }
956
957 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
958 {
959         struct net_device_context *ndevctx = netdev_priv(ndev);
960         struct hv_device *hdev =  ndevctx->device_ctx;
961         struct sockaddr *addr = p;
962         char save_adr[ETH_ALEN];
963         unsigned char save_aatype;
964         int err;
965
966         memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
967         save_aatype = ndev->addr_assign_type;
968
969         err = eth_mac_addr(ndev, p);
970         if (err != 0)
971                 return err;
972
973         err = rndis_filter_set_device_mac(hdev, addr->sa_data);
974         if (err != 0) {
975                 /* roll back to saved MAC */
976                 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
977                 ndev->addr_assign_type = save_aatype;
978         }
979
980         return err;
981 }
982
983 #ifdef CONFIG_NET_POLL_CONTROLLER
984 static void netvsc_poll_controller(struct net_device *net)
985 {
986         /* As netvsc_start_xmit() works synchronous we don't have to
987          * trigger anything here.
988          */
989 }
990 #endif
991
992 static const struct ethtool_ops ethtool_ops = {
993         .get_drvinfo    = netvsc_get_drvinfo,
994         .get_link       = ethtool_op_get_link,
995         .get_channels   = netvsc_get_channels,
996         .set_channels   = netvsc_set_channels,
997 };
998
999 static const struct net_device_ops device_ops = {
1000         .ndo_open =                     netvsc_open,
1001         .ndo_stop =                     netvsc_close,
1002         .ndo_start_xmit =               netvsc_start_xmit,
1003         .ndo_set_rx_mode =              netvsc_set_multicast_list,
1004         .ndo_change_mtu =               netvsc_change_mtu,
1005         .ndo_validate_addr =            eth_validate_addr,
1006         .ndo_set_mac_address =          netvsc_set_mac_addr,
1007         .ndo_select_queue =             netvsc_select_queue,
1008         .ndo_get_stats64 =              netvsc_get_stats64,
1009 #ifdef CONFIG_NET_POLL_CONTROLLER
1010         .ndo_poll_controller =          netvsc_poll_controller,
1011 #endif
1012 };
1013
1014 /*
1015  * Send GARP packet to network peers after migrations.
1016  * After Quick Migration, the network is not immediately operational in the
1017  * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
1018  * another netif_notify_peers() into a delayed work, otherwise GARP packet
1019  * will not be sent after quick migration, and cause network disconnection.
1020  * Also, we update the carrier status here.
1021  */
1022 static void netvsc_link_change(struct work_struct *w)
1023 {
1024         struct net_device_context *ndev_ctx;
1025         struct net_device *net;
1026         struct netvsc_device *net_device;
1027         struct rndis_device *rdev;
1028         bool notify, refresh = false;
1029         char *argv[] = { "/etc/init.d/network", "restart", NULL };
1030         char *envp[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
1031
1032         rtnl_lock();
1033
1034         ndev_ctx = container_of(w, struct net_device_context, dwork.work);
1035         net_device = hv_get_drvdata(ndev_ctx->device_ctx);
1036         rdev = net_device->extension;
1037         net = net_device->ndev;
1038
1039         if (rdev->link_state) {
1040                 netif_carrier_off(net);
1041                 notify = false;
1042         } else {
1043                 netif_carrier_on(net);
1044                 notify = true;
1045                 if (rdev->link_change) {
1046                         rdev->link_change = false;
1047                         refresh = true;
1048                 }
1049         }
1050
1051         rtnl_unlock();
1052
1053         if (refresh)
1054                 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
1055
1056         if (notify)
1057                 netdev_notify_peers(net);
1058 }
1059
1060 static void netvsc_free_netdev(struct net_device *netdev)
1061 {
1062         struct net_device_context *net_device_ctx = netdev_priv(netdev);
1063
1064         free_percpu(net_device_ctx->tx_stats);
1065         free_percpu(net_device_ctx->rx_stats);
1066         free_netdev(netdev);
1067 }
1068
1069 static int netvsc_probe(struct hv_device *dev,
1070                         const struct hv_vmbus_device_id *dev_id)
1071 {
1072         struct net_device *net = NULL;
1073         struct net_device_context *net_device_ctx;
1074         struct netvsc_device_info device_info;
1075         struct netvsc_device *nvdev;
1076         int ret;
1077         u32 max_needed_headroom;
1078
1079         net = alloc_etherdev_mq(sizeof(struct net_device_context),
1080                                 num_online_cpus());
1081         if (!net)
1082                 return -ENOMEM;
1083
1084         max_needed_headroom = sizeof(struct hv_netvsc_packet) +
1085                               RNDIS_AND_PPI_SIZE;
1086
1087         netif_carrier_off(net);
1088
1089         net_device_ctx = netdev_priv(net);
1090         net_device_ctx->device_ctx = dev;
1091         net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1092         if (netif_msg_probe(net_device_ctx))
1093                 netdev_dbg(net, "netvsc msg_enable: %d\n",
1094                            net_device_ctx->msg_enable);
1095
1096         net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1097         if (!net_device_ctx->tx_stats) {
1098                 free_netdev(net);
1099                 return -ENOMEM;
1100         }
1101         net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1102         if (!net_device_ctx->rx_stats) {
1103                 free_percpu(net_device_ctx->tx_stats);
1104                 free_netdev(net);
1105                 return -ENOMEM;
1106         }
1107
1108         hv_set_drvdata(dev, net);
1109         INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1110         INIT_WORK(&net_device_ctx->work, do_set_multicast);
1111
1112         net->netdev_ops = &device_ops;
1113
1114         net->hw_features = NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_IP_CSUM |
1115                                 NETIF_F_TSO;
1116         net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_RXCSUM |
1117                         NETIF_F_IP_CSUM | NETIF_F_TSO;
1118
1119         net->ethtool_ops = &ethtool_ops;
1120         SET_NETDEV_DEV(net, &dev->device);
1121
1122         /*
1123          * Request additional head room in the skb.
1124          * We will use this space to build the rndis
1125          * heaser and other state we need to maintain.
1126          */
1127         net->needed_headroom = max_needed_headroom;
1128
1129         /* Notify the netvsc driver of the new device */
1130         memset(&device_info, 0, sizeof(device_info));
1131         device_info.ring_size = ring_size;
1132         device_info.max_num_vrss_chns = max_num_vrss_chns;
1133         ret = rndis_filter_device_add(dev, &device_info);
1134         if (ret != 0) {
1135                 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1136                 netvsc_free_netdev(net);
1137                 hv_set_drvdata(dev, NULL);
1138                 return ret;
1139         }
1140         memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1141
1142         nvdev = hv_get_drvdata(dev);
1143         netif_set_real_num_tx_queues(net, nvdev->num_chn);
1144         netif_set_real_num_rx_queues(net, nvdev->num_chn);
1145
1146         ret = register_netdev(net);
1147         if (ret != 0) {
1148                 pr_err("Unable to register netdev.\n");
1149                 rndis_filter_device_remove(dev);
1150                 netvsc_free_netdev(net);
1151         } else {
1152                 schedule_delayed_work(&net_device_ctx->dwork, 0);
1153         }
1154
1155         return ret;
1156 }
1157
1158 static int netvsc_remove(struct hv_device *dev)
1159 {
1160         struct net_device *net;
1161         struct net_device_context *ndev_ctx;
1162         struct netvsc_device *net_device;
1163
1164         net_device = hv_get_drvdata(dev);
1165         net = net_device->ndev;
1166
1167         if (net == NULL) {
1168                 dev_err(&dev->device, "No net device to remove\n");
1169                 return 0;
1170         }
1171
1172         net_device->start_remove = true;
1173
1174         ndev_ctx = netdev_priv(net);
1175         cancel_delayed_work_sync(&ndev_ctx->dwork);
1176         cancel_work_sync(&ndev_ctx->work);
1177
1178         /* Stop outbound asap */
1179         netif_tx_disable(net);
1180
1181         unregister_netdev(net);
1182
1183         /*
1184          * Call to the vsc driver to let it know that the device is being
1185          * removed
1186          */
1187         rndis_filter_device_remove(dev);
1188
1189         netvsc_free_netdev(net);
1190         return 0;
1191 }
1192
1193 static const struct hv_vmbus_device_id id_table[] = {
1194         /* Network guid */
1195         { HV_NIC_GUID, },
1196         { },
1197 };
1198
1199 MODULE_DEVICE_TABLE(vmbus, id_table);
1200
1201 /* The one and only one */
1202 static struct  hv_driver netvsc_drv = {
1203         .name = KBUILD_MODNAME,
1204         .id_table = id_table,
1205         .probe = netvsc_probe,
1206         .remove = netvsc_remove,
1207 };
1208
1209 static void __exit netvsc_drv_exit(void)
1210 {
1211         vmbus_driver_unregister(&netvsc_drv);
1212 }
1213
1214 static int __init netvsc_drv_init(void)
1215 {
1216         if (ring_size < RING_SIZE_MIN) {
1217                 ring_size = RING_SIZE_MIN;
1218                 pr_info("Increased ring_size to %d (min allowed)\n",
1219                         ring_size);
1220         }
1221         return vmbus_driver_register(&netvsc_drv);
1222 }
1223
1224 MODULE_LICENSE("GPL");
1225 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1226
1227 module_init(netvsc_drv_init);
1228 module_exit(netvsc_drv_exit);