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