Merge tag 'nfsd-4.9' of git://linux-nfs.org/~bfields/linux
[cascardo/linux.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 #define RING_SIZE_MIN 64
44 #define LINKCHANGE_INT (2 * HZ)
45 #define NETVSC_HW_FEATURES      (NETIF_F_RXCSUM | \
46                                  NETIF_F_SG | \
47                                  NETIF_F_TSO | \
48                                  NETIF_F_TSO6 | \
49                                  NETIF_F_HW_CSUM)
50 static int ring_size = 128;
51 module_param(ring_size, int, S_IRUGO);
52 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
53
54 static int max_num_vrss_chns = 8;
55
56 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
57                                 NETIF_MSG_LINK | NETIF_MSG_IFUP |
58                                 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
59                                 NETIF_MSG_TX_ERR;
60
61 static int debug = -1;
62 module_param(debug, int, S_IRUGO);
63 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
64
65 static void do_set_multicast(struct work_struct *w)
66 {
67         struct net_device_context *ndevctx =
68                 container_of(w, struct net_device_context, work);
69         struct hv_device *device_obj = ndevctx->device_ctx;
70         struct net_device *ndev = hv_get_drvdata(device_obj);
71         struct netvsc_device *nvdev = ndevctx->nvdev;
72         struct rndis_device *rdev;
73
74         if (!nvdev)
75                 return;
76
77         rdev = nvdev->extension;
78         if (rdev == NULL)
79                 return;
80
81         if (ndev->flags & IFF_PROMISC)
82                 rndis_filter_set_packet_filter(rdev,
83                         NDIS_PACKET_TYPE_PROMISCUOUS);
84         else
85                 rndis_filter_set_packet_filter(rdev,
86                         NDIS_PACKET_TYPE_BROADCAST |
87                         NDIS_PACKET_TYPE_ALL_MULTICAST |
88                         NDIS_PACKET_TYPE_DIRECTED);
89 }
90
91 static void netvsc_set_multicast_list(struct net_device *net)
92 {
93         struct net_device_context *net_device_ctx = netdev_priv(net);
94
95         schedule_work(&net_device_ctx->work);
96 }
97
98 static int netvsc_open(struct net_device *net)
99 {
100         struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
101         struct rndis_device *rdev;
102         int ret = 0;
103
104         netif_carrier_off(net);
105
106         /* Open up the device */
107         ret = rndis_filter_open(nvdev);
108         if (ret != 0) {
109                 netdev_err(net, "unable to open device (ret %d).\n", ret);
110                 return ret;
111         }
112
113         netif_tx_wake_all_queues(net);
114
115         rdev = nvdev->extension;
116         if (!rdev->link_state)
117                 netif_carrier_on(net);
118
119         return ret;
120 }
121
122 static int netvsc_close(struct net_device *net)
123 {
124         struct net_device_context *net_device_ctx = netdev_priv(net);
125         struct netvsc_device *nvdev = net_device_ctx->nvdev;
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(nvdev);
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 netvsc_device *nvsc_dev = net_device_ctx->nvdev;
205         u32 hash;
206         u16 q_idx = 0;
207
208         if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
209                 return 0;
210
211         hash = skb_get_hash(skb);
212         q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
213                 ndev->real_num_tx_queues;
214
215         if (!nvsc_dev->chn_table[q_idx])
216                 q_idx = 0;
217
218         return q_idx;
219 }
220
221 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
222                         struct hv_page_buffer *pb)
223 {
224         int j = 0;
225
226         /* Deal with compund pages by ignoring unused part
227          * of the page.
228          */
229         page += (offset >> PAGE_SHIFT);
230         offset &= ~PAGE_MASK;
231
232         while (len > 0) {
233                 unsigned long bytes;
234
235                 bytes = PAGE_SIZE - offset;
236                 if (bytes > len)
237                         bytes = len;
238                 pb[j].pfn = page_to_pfn(page);
239                 pb[j].offset = offset;
240                 pb[j].len = bytes;
241
242                 offset += bytes;
243                 len -= bytes;
244
245                 if (offset == PAGE_SIZE && len) {
246                         page++;
247                         offset = 0;
248                         j++;
249                 }
250         }
251
252         return j + 1;
253 }
254
255 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
256                            struct hv_netvsc_packet *packet,
257                            struct hv_page_buffer **page_buf)
258 {
259         struct hv_page_buffer *pb = *page_buf;
260         u32 slots_used = 0;
261         char *data = skb->data;
262         int frags = skb_shinfo(skb)->nr_frags;
263         int i;
264
265         /* The packet is laid out thus:
266          * 1. hdr: RNDIS header and PPI
267          * 2. skb linear data
268          * 3. skb fragment data
269          */
270         if (hdr != NULL)
271                 slots_used += fill_pg_buf(virt_to_page(hdr),
272                                         offset_in_page(hdr),
273                                         len, &pb[slots_used]);
274
275         packet->rmsg_size = len;
276         packet->rmsg_pgcnt = slots_used;
277
278         slots_used += fill_pg_buf(virt_to_page(data),
279                                 offset_in_page(data),
280                                 skb_headlen(skb), &pb[slots_used]);
281
282         for (i = 0; i < frags; i++) {
283                 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
284
285                 slots_used += fill_pg_buf(skb_frag_page(frag),
286                                         frag->page_offset,
287                                         skb_frag_size(frag), &pb[slots_used]);
288         }
289         return slots_used;
290 }
291
292 static int count_skb_frag_slots(struct sk_buff *skb)
293 {
294         int i, frags = skb_shinfo(skb)->nr_frags;
295         int pages = 0;
296
297         for (i = 0; i < frags; i++) {
298                 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
299                 unsigned long size = skb_frag_size(frag);
300                 unsigned long offset = frag->page_offset;
301
302                 /* Skip unused frames from start of page */
303                 offset &= ~PAGE_MASK;
304                 pages += PFN_UP(offset + size);
305         }
306         return pages;
307 }
308
309 static int netvsc_get_slots(struct sk_buff *skb)
310 {
311         char *data = skb->data;
312         unsigned int offset = offset_in_page(data);
313         unsigned int len = skb_headlen(skb);
314         int slots;
315         int frag_slots;
316
317         slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
318         frag_slots = count_skb_frag_slots(skb);
319         return slots + frag_slots;
320 }
321
322 static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
323 {
324         u32 ret_val = TRANSPORT_INFO_NOT_IP;
325
326         if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
327                 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
328                 goto not_ip;
329         }
330
331         *trans_off = skb_transport_offset(skb);
332
333         if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
334                 struct iphdr *iphdr = ip_hdr(skb);
335
336                 if (iphdr->protocol == IPPROTO_TCP)
337                         ret_val = TRANSPORT_INFO_IPV4_TCP;
338                 else if (iphdr->protocol == IPPROTO_UDP)
339                         ret_val = TRANSPORT_INFO_IPV4_UDP;
340         } else {
341                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
342                         ret_val = TRANSPORT_INFO_IPV6_TCP;
343                 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
344                         ret_val = TRANSPORT_INFO_IPV6_UDP;
345         }
346
347 not_ip:
348         return ret_val;
349 }
350
351 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
352 {
353         struct net_device_context *net_device_ctx = netdev_priv(net);
354         struct hv_netvsc_packet *packet = NULL;
355         int ret;
356         unsigned int num_data_pgs;
357         struct rndis_message *rndis_msg;
358         struct rndis_packet *rndis_pkt;
359         u32 rndis_msg_size;
360         struct rndis_per_packet_info *ppi;
361         struct ndis_tcp_ip_checksum_info *csum_info;
362         int  hdr_offset;
363         u32 net_trans_info;
364         u32 hash;
365         u32 skb_length;
366         struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
367         struct hv_page_buffer *pb = page_buf;
368
369         /* We will atmost need two pages to describe the rndis
370          * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
371          * of pages in a single packet. If skb is scattered around
372          * more pages we try linearizing it.
373          */
374
375         skb_length = skb->len;
376         num_data_pgs = netvsc_get_slots(skb) + 2;
377
378         if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
379                 ++net_device_ctx->eth_stats.tx_scattered;
380
381                 if (skb_linearize(skb))
382                         goto no_memory;
383
384                 num_data_pgs = netvsc_get_slots(skb) + 2;
385                 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
386                         ++net_device_ctx->eth_stats.tx_too_big;
387                         goto drop;
388                 }
389         }
390
391         /*
392          * Place the rndis header in the skb head room and
393          * the skb->cb will be used for hv_netvsc_packet
394          * structure.
395          */
396         ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
397         if (ret)
398                 goto no_memory;
399
400         /* Use the skb control buffer for building up the packet */
401         BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
402                         FIELD_SIZEOF(struct sk_buff, cb));
403         packet = (struct hv_netvsc_packet *)skb->cb;
404
405         packet->q_idx = skb_get_queue_mapping(skb);
406
407         packet->total_data_buflen = skb->len;
408
409         rndis_msg = (struct rndis_message *)skb->head;
410
411         memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
412
413         /* Add the rndis header */
414         rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
415         rndis_msg->msg_len = packet->total_data_buflen;
416         rndis_pkt = &rndis_msg->msg.pkt;
417         rndis_pkt->data_offset = sizeof(struct rndis_packet);
418         rndis_pkt->data_len = packet->total_data_buflen;
419         rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
420
421         rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
422
423         hash = skb_get_hash_raw(skb);
424         if (hash != 0 && net->real_num_tx_queues > 1) {
425                 rndis_msg_size += NDIS_HASH_PPI_SIZE;
426                 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
427                                     NBL_HASH_VALUE);
428                 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
429         }
430
431         if (skb_vlan_tag_present(skb)) {
432                 struct ndis_pkt_8021q_info *vlan;
433
434                 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
435                 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
436                                         IEEE_8021Q_INFO);
437                 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
438                                                 ppi->ppi_offset);
439                 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
440                 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
441                                 VLAN_PRIO_SHIFT;
442         }
443
444         net_trans_info = get_net_transport_info(skb, &hdr_offset);
445         if (net_trans_info == TRANSPORT_INFO_NOT_IP)
446                 goto do_send;
447
448         /*
449          * Setup the sendside checksum offload only if this is not a
450          * GSO packet.
451          */
452         if (skb_is_gso(skb)) {
453                 struct ndis_tcp_lso_info *lso_info;
454
455                 rndis_msg_size += NDIS_LSO_PPI_SIZE;
456                 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
457                                     TCP_LARGESEND_PKTINFO);
458
459                 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
460                                                         ppi->ppi_offset);
461
462                 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
463                 if (net_trans_info & (INFO_IPV4 << 16)) {
464                         lso_info->lso_v2_transmit.ip_version =
465                                 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
466                         ip_hdr(skb)->tot_len = 0;
467                         ip_hdr(skb)->check = 0;
468                         tcp_hdr(skb)->check =
469                                 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
470                                                    ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
471                 } else {
472                         lso_info->lso_v2_transmit.ip_version =
473                                 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
474                         ipv6_hdr(skb)->payload_len = 0;
475                         tcp_hdr(skb)->check =
476                                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
477                                                  &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
478                 }
479                 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
480                 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
481                 goto do_send;
482         }
483
484         if ((skb->ip_summed == CHECKSUM_NONE) ||
485             (skb->ip_summed == CHECKSUM_UNNECESSARY))
486                 goto do_send;
487
488         rndis_msg_size += NDIS_CSUM_PPI_SIZE;
489         ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
490                             TCPIP_CHKSUM_PKTINFO);
491
492         csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
493                         ppi->ppi_offset);
494
495         if (net_trans_info & (INFO_IPV4 << 16))
496                 csum_info->transmit.is_ipv4 = 1;
497         else
498                 csum_info->transmit.is_ipv6 = 1;
499
500         if (net_trans_info & INFO_TCP) {
501                 csum_info->transmit.tcp_checksum = 1;
502                 csum_info->transmit.tcp_header_offset = hdr_offset;
503         } else if (net_trans_info & INFO_UDP) {
504                 /* UDP checksum offload is not supported on ws2008r2.
505                  * Furthermore, on ws2012 and ws2012r2, there are some
506                  * issues with udp checksum offload from Linux guests.
507                  * (these are host issues).
508                  * For now compute the checksum here.
509                  */
510                 struct udphdr *uh;
511                 u16 udp_len;
512
513                 ret = skb_cow_head(skb, 0);
514                 if (ret)
515                         goto no_memory;
516
517                 uh = udp_hdr(skb);
518                 udp_len = ntohs(uh->len);
519                 uh->check = 0;
520                 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
521                                               ip_hdr(skb)->daddr,
522                                               udp_len, IPPROTO_UDP,
523                                               csum_partial(uh, udp_len, 0));
524                 if (uh->check == 0)
525                         uh->check = CSUM_MANGLED_0;
526
527                 csum_info->transmit.udp_checksum = 0;
528         }
529
530 do_send:
531         /* Start filling in the page buffers with the rndis hdr */
532         rndis_msg->msg_len += rndis_msg_size;
533         packet->total_data_buflen = rndis_msg->msg_len;
534         packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
535                                                skb, packet, &pb);
536
537         /* timestamp packet in software */
538         skb_tx_timestamp(skb);
539         ret = netvsc_send(net_device_ctx->device_ctx, packet,
540                           rndis_msg, &pb, skb);
541         if (likely(ret == 0)) {
542                 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
543
544                 u64_stats_update_begin(&tx_stats->syncp);
545                 tx_stats->packets++;
546                 tx_stats->bytes += skb_length;
547                 u64_stats_update_end(&tx_stats->syncp);
548                 return NETDEV_TX_OK;
549         }
550
551         if (ret == -EAGAIN) {
552                 ++net_device_ctx->eth_stats.tx_busy;
553                 return NETDEV_TX_BUSY;
554         }
555
556         if (ret == -ENOSPC)
557                 ++net_device_ctx->eth_stats.tx_no_space;
558
559 drop:
560         dev_kfree_skb_any(skb);
561         net->stats.tx_dropped++;
562
563         return NETDEV_TX_OK;
564
565 no_memory:
566         ++net_device_ctx->eth_stats.tx_no_memory;
567         goto drop;
568 }
569
570 /*
571  * netvsc_linkstatus_callback - Link up/down notification
572  */
573 void netvsc_linkstatus_callback(struct hv_device *device_obj,
574                                 struct rndis_message *resp)
575 {
576         struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
577         struct net_device *net;
578         struct net_device_context *ndev_ctx;
579         struct netvsc_reconfig *event;
580         unsigned long flags;
581
582         net = hv_get_drvdata(device_obj);
583
584         if (!net)
585                 return;
586
587         ndev_ctx = netdev_priv(net);
588
589         /* Update the physical link speed when changing to another vSwitch */
590         if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
591                 u32 speed;
592
593                 speed = *(u32 *)((void *)indicate + indicate->
594                                  status_buf_offset) / 10000;
595                 ndev_ctx->speed = speed;
596                 return;
597         }
598
599         /* Handle these link change statuses below */
600         if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
601             indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
602             indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
603                 return;
604
605         if (net->reg_state != NETREG_REGISTERED)
606                 return;
607
608         event = kzalloc(sizeof(*event), GFP_ATOMIC);
609         if (!event)
610                 return;
611         event->event = indicate->status;
612
613         spin_lock_irqsave(&ndev_ctx->lock, flags);
614         list_add_tail(&event->list, &ndev_ctx->reconfig_events);
615         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
616
617         schedule_delayed_work(&ndev_ctx->dwork, 0);
618 }
619
620 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
621                                 struct hv_netvsc_packet *packet,
622                                 struct ndis_tcp_ip_checksum_info *csum_info,
623                                 void *data, u16 vlan_tci)
624 {
625         struct sk_buff *skb;
626
627         skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
628         if (!skb)
629                 return skb;
630
631         /*
632          * Copy to skb. This copy is needed here since the memory pointed by
633          * hv_netvsc_packet cannot be deallocated
634          */
635         memcpy(skb_put(skb, packet->total_data_buflen), data,
636                packet->total_data_buflen);
637
638         skb->protocol = eth_type_trans(skb, net);
639         if (csum_info) {
640                 /* We only look at the IP checksum here.
641                  * Should we be dropping the packet if checksum
642                  * failed? How do we deal with other checksums - TCP/UDP?
643                  */
644                 if (csum_info->receive.ip_checksum_succeeded)
645                         skb->ip_summed = CHECKSUM_UNNECESSARY;
646                 else
647                         skb->ip_summed = CHECKSUM_NONE;
648         }
649
650         if (vlan_tci & VLAN_TAG_PRESENT)
651                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
652                                        vlan_tci);
653
654         return skb;
655 }
656
657 /*
658  * netvsc_recv_callback -  Callback when we receive a packet from the
659  * "wire" on the specified device.
660  */
661 int netvsc_recv_callback(struct hv_device *device_obj,
662                                 struct hv_netvsc_packet *packet,
663                                 void **data,
664                                 struct ndis_tcp_ip_checksum_info *csum_info,
665                                 struct vmbus_channel *channel,
666                                 u16 vlan_tci)
667 {
668         struct net_device *net = hv_get_drvdata(device_obj);
669         struct net_device_context *net_device_ctx = netdev_priv(net);
670         struct net_device *vf_netdev;
671         struct sk_buff *skb;
672         struct netvsc_stats *rx_stats;
673
674         if (net->reg_state != NETREG_REGISTERED)
675                 return NVSP_STAT_FAIL;
676
677         /*
678          * If necessary, inject this packet into the VF interface.
679          * On Hyper-V, multicast and brodcast packets are only delivered
680          * to the synthetic interface (after subjecting these to
681          * policy filters on the host). Deliver these via the VF
682          * interface in the guest.
683          */
684         vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
685         if (vf_netdev && (vf_netdev->flags & IFF_UP))
686                 net = vf_netdev;
687
688         /* Allocate a skb - TODO direct I/O to pages? */
689         skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
690         if (unlikely(!skb)) {
691                 ++net->stats.rx_dropped;
692                 return NVSP_STAT_FAIL;
693         }
694
695         if (net != vf_netdev)
696                 skb_record_rx_queue(skb,
697                                     channel->offermsg.offer.sub_channel_index);
698
699         /*
700          * Even if injecting the packet, record the statistics
701          * on the synthetic device because modifying the VF device
702          * statistics will not work correctly.
703          */
704         rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
705         u64_stats_update_begin(&rx_stats->syncp);
706         rx_stats->packets++;
707         rx_stats->bytes += packet->total_data_buflen;
708
709         if (skb->pkt_type == PACKET_BROADCAST)
710                 ++rx_stats->broadcast;
711         else if (skb->pkt_type == PACKET_MULTICAST)
712                 ++rx_stats->multicast;
713         u64_stats_update_end(&rx_stats->syncp);
714
715         /*
716          * Pass the skb back up. Network stack will deallocate the skb when it
717          * is done.
718          * TODO - use NAPI?
719          */
720         netif_rx(skb);
721
722         return 0;
723 }
724
725 static void netvsc_get_drvinfo(struct net_device *net,
726                                struct ethtool_drvinfo *info)
727 {
728         struct net_device_context *net_device_ctx = netdev_priv(net);
729         struct hv_device *dev = net_device_ctx->device_ctx;
730
731         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
732         strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
733         strlcpy(info->bus_info, vmbus_dev_name(dev), sizeof(info->bus_info));
734 }
735
736 static void netvsc_get_channels(struct net_device *net,
737                                 struct ethtool_channels *channel)
738 {
739         struct net_device_context *net_device_ctx = netdev_priv(net);
740         struct netvsc_device *nvdev = net_device_ctx->nvdev;
741
742         if (nvdev) {
743                 channel->max_combined   = nvdev->max_chn;
744                 channel->combined_count = nvdev->num_chn;
745         }
746 }
747
748 static int netvsc_set_channels(struct net_device *net,
749                                struct ethtool_channels *channels)
750 {
751         struct net_device_context *net_device_ctx = netdev_priv(net);
752         struct hv_device *dev = net_device_ctx->device_ctx;
753         struct netvsc_device *nvdev = net_device_ctx->nvdev;
754         struct netvsc_device_info device_info;
755         u32 num_chn;
756         u32 max_chn;
757         int ret = 0;
758         bool recovering = false;
759
760         if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
761                 return -ENODEV;
762
763         num_chn = nvdev->num_chn;
764         max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
765
766         if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
767                 pr_info("vRSS unsupported before NVSP Version 5\n");
768                 return -EINVAL;
769         }
770
771         /* We do not support rx, tx, or other */
772         if (!channels ||
773             channels->rx_count ||
774             channels->tx_count ||
775             channels->other_count ||
776             (channels->combined_count < 1))
777                 return -EINVAL;
778
779         if (channels->combined_count > max_chn) {
780                 pr_info("combined channels too high, using %d\n", max_chn);
781                 channels->combined_count = max_chn;
782         }
783
784         ret = netvsc_close(net);
785         if (ret)
786                 goto out;
787
788  do_set:
789         net_device_ctx->start_remove = true;
790         rndis_filter_device_remove(dev);
791
792         nvdev->num_chn = channels->combined_count;
793
794         memset(&device_info, 0, sizeof(device_info));
795         device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
796         device_info.ring_size = ring_size;
797         device_info.max_num_vrss_chns = max_num_vrss_chns;
798
799         ret = rndis_filter_device_add(dev, &device_info);
800         if (ret) {
801                 if (recovering) {
802                         netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
803                         return ret;
804                 }
805                 goto recover;
806         }
807
808         nvdev = net_device_ctx->nvdev;
809
810         ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
811         if (ret) {
812                 if (recovering) {
813                         netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
814                         return ret;
815                 }
816                 goto recover;
817         }
818
819         ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
820         if (ret) {
821                 if (recovering) {
822                         netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
823                         return ret;
824                 }
825                 goto recover;
826         }
827
828  out:
829         netvsc_open(net);
830         net_device_ctx->start_remove = false;
831         /* We may have missed link change notifications */
832         schedule_delayed_work(&net_device_ctx->dwork, 0);
833
834         return ret;
835
836  recover:
837         /* If the above failed, we attempt to recover through the same
838          * process but with the original number of channels.
839          */
840         netdev_err(net, "could not set channels, recovering\n");
841         recovering = true;
842         channels->combined_count = num_chn;
843         goto do_set;
844 }
845
846 static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
847 {
848         struct ethtool_cmd diff1 = *cmd;
849         struct ethtool_cmd diff2 = {};
850
851         ethtool_cmd_speed_set(&diff1, 0);
852         diff1.duplex = 0;
853         /* advertising and cmd are usually set */
854         diff1.advertising = 0;
855         diff1.cmd = 0;
856         /* We set port to PORT_OTHER */
857         diff2.port = PORT_OTHER;
858
859         return !memcmp(&diff1, &diff2, sizeof(diff1));
860 }
861
862 static void netvsc_init_settings(struct net_device *dev)
863 {
864         struct net_device_context *ndc = netdev_priv(dev);
865
866         ndc->speed = SPEED_UNKNOWN;
867         ndc->duplex = DUPLEX_UNKNOWN;
868 }
869
870 static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
871 {
872         struct net_device_context *ndc = netdev_priv(dev);
873
874         ethtool_cmd_speed_set(cmd, ndc->speed);
875         cmd->duplex = ndc->duplex;
876         cmd->port = PORT_OTHER;
877
878         return 0;
879 }
880
881 static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
882 {
883         struct net_device_context *ndc = netdev_priv(dev);
884         u32 speed;
885
886         speed = ethtool_cmd_speed(cmd);
887         if (!ethtool_validate_speed(speed) ||
888             !ethtool_validate_duplex(cmd->duplex) ||
889             !netvsc_validate_ethtool_ss_cmd(cmd))
890                 return -EINVAL;
891
892         ndc->speed = speed;
893         ndc->duplex = cmd->duplex;
894
895         return 0;
896 }
897
898 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
899 {
900         struct net_device_context *ndevctx = netdev_priv(ndev);
901         struct netvsc_device *nvdev = ndevctx->nvdev;
902         struct hv_device *hdev = ndevctx->device_ctx;
903         struct netvsc_device_info device_info;
904         int limit = ETH_DATA_LEN;
905         u32 num_chn;
906         int ret = 0;
907
908         if (ndevctx->start_remove || !nvdev || nvdev->destroy)
909                 return -ENODEV;
910
911         if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
912                 limit = NETVSC_MTU - ETH_HLEN;
913
914         if (mtu < NETVSC_MTU_MIN || mtu > limit)
915                 return -EINVAL;
916
917         ret = netvsc_close(ndev);
918         if (ret)
919                 goto out;
920
921         num_chn = nvdev->num_chn;
922
923         ndevctx->start_remove = true;
924         rndis_filter_device_remove(hdev);
925
926         ndev->mtu = mtu;
927
928         memset(&device_info, 0, sizeof(device_info));
929         device_info.ring_size = ring_size;
930         device_info.num_chn = num_chn;
931         device_info.max_num_vrss_chns = max_num_vrss_chns;
932         rndis_filter_device_add(hdev, &device_info);
933
934 out:
935         netvsc_open(ndev);
936         ndevctx->start_remove = false;
937
938         /* We may have missed link change notifications */
939         schedule_delayed_work(&ndevctx->dwork, 0);
940
941         return ret;
942 }
943
944 static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
945                                                     struct rtnl_link_stats64 *t)
946 {
947         struct net_device_context *ndev_ctx = netdev_priv(net);
948         int cpu;
949
950         for_each_possible_cpu(cpu) {
951                 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
952                                                             cpu);
953                 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
954                                                             cpu);
955                 u64 tx_packets, tx_bytes, rx_packets, rx_bytes, rx_multicast;
956                 unsigned int start;
957
958                 do {
959                         start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
960                         tx_packets = tx_stats->packets;
961                         tx_bytes = tx_stats->bytes;
962                 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
963
964                 do {
965                         start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
966                         rx_packets = rx_stats->packets;
967                         rx_bytes = rx_stats->bytes;
968                         rx_multicast = rx_stats->multicast + rx_stats->broadcast;
969                 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
970
971                 t->tx_bytes     += tx_bytes;
972                 t->tx_packets   += tx_packets;
973                 t->rx_bytes     += rx_bytes;
974                 t->rx_packets   += rx_packets;
975                 t->multicast    += rx_multicast;
976         }
977
978         t->tx_dropped   = net->stats.tx_dropped;
979         t->tx_errors    = net->stats.tx_dropped;
980
981         t->rx_dropped   = net->stats.rx_dropped;
982         t->rx_errors    = net->stats.rx_errors;
983
984         return t;
985 }
986
987 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
988 {
989         struct sockaddr *addr = p;
990         char save_adr[ETH_ALEN];
991         unsigned char save_aatype;
992         int err;
993
994         memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
995         save_aatype = ndev->addr_assign_type;
996
997         err = eth_mac_addr(ndev, p);
998         if (err != 0)
999                 return err;
1000
1001         err = rndis_filter_set_device_mac(ndev, addr->sa_data);
1002         if (err != 0) {
1003                 /* roll back to saved MAC */
1004                 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
1005                 ndev->addr_assign_type = save_aatype;
1006         }
1007
1008         return err;
1009 }
1010
1011 static const struct {
1012         char name[ETH_GSTRING_LEN];
1013         u16 offset;
1014 } netvsc_stats[] = {
1015         { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1016         { "tx_no_memory",  offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1017         { "tx_no_space",  offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1018         { "tx_too_big",   offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1019         { "tx_busy",      offsetof(struct netvsc_ethtool_stats, tx_busy) },
1020 };
1021
1022 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1023 {
1024         switch (string_set) {
1025         case ETH_SS_STATS:
1026                 return ARRAY_SIZE(netvsc_stats);
1027         default:
1028                 return -EINVAL;
1029         }
1030 }
1031
1032 static void netvsc_get_ethtool_stats(struct net_device *dev,
1033                                      struct ethtool_stats *stats, u64 *data)
1034 {
1035         struct net_device_context *ndc = netdev_priv(dev);
1036         const void *nds = &ndc->eth_stats;
1037         int i;
1038
1039         for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1040                 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1041 }
1042
1043 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1044 {
1045         int i;
1046
1047         switch (stringset) {
1048         case ETH_SS_STATS:
1049                 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1050                         memcpy(data + i * ETH_GSTRING_LEN,
1051                                netvsc_stats[i].name, ETH_GSTRING_LEN);
1052                 break;
1053         }
1054 }
1055
1056 #ifdef CONFIG_NET_POLL_CONTROLLER
1057 static void netvsc_poll_controller(struct net_device *net)
1058 {
1059         /* As netvsc_start_xmit() works synchronous we don't have to
1060          * trigger anything here.
1061          */
1062 }
1063 #endif
1064
1065 static const struct ethtool_ops ethtool_ops = {
1066         .get_drvinfo    = netvsc_get_drvinfo,
1067         .get_link       = ethtool_op_get_link,
1068         .get_ethtool_stats = netvsc_get_ethtool_stats,
1069         .get_sset_count = netvsc_get_sset_count,
1070         .get_strings    = netvsc_get_strings,
1071         .get_channels   = netvsc_get_channels,
1072         .set_channels   = netvsc_set_channels,
1073         .get_ts_info    = ethtool_op_get_ts_info,
1074         .get_settings   = netvsc_get_settings,
1075         .set_settings   = netvsc_set_settings,
1076 };
1077
1078 static const struct net_device_ops device_ops = {
1079         .ndo_open =                     netvsc_open,
1080         .ndo_stop =                     netvsc_close,
1081         .ndo_start_xmit =               netvsc_start_xmit,
1082         .ndo_set_rx_mode =              netvsc_set_multicast_list,
1083         .ndo_change_mtu =               netvsc_change_mtu,
1084         .ndo_validate_addr =            eth_validate_addr,
1085         .ndo_set_mac_address =          netvsc_set_mac_addr,
1086         .ndo_select_queue =             netvsc_select_queue,
1087         .ndo_get_stats64 =              netvsc_get_stats64,
1088 #ifdef CONFIG_NET_POLL_CONTROLLER
1089         .ndo_poll_controller =          netvsc_poll_controller,
1090 #endif
1091 };
1092
1093 /*
1094  * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1095  * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1096  * present send GARP packet to network peers with netif_notify_peers().
1097  */
1098 static void netvsc_link_change(struct work_struct *w)
1099 {
1100         struct net_device_context *ndev_ctx =
1101                 container_of(w, struct net_device_context, dwork.work);
1102         struct hv_device *device_obj = ndev_ctx->device_ctx;
1103         struct net_device *net = hv_get_drvdata(device_obj);
1104         struct netvsc_device *net_device;
1105         struct rndis_device *rdev;
1106         struct netvsc_reconfig *event = NULL;
1107         bool notify = false, reschedule = false;
1108         unsigned long flags, next_reconfig, delay;
1109
1110         rtnl_lock();
1111         if (ndev_ctx->start_remove)
1112                 goto out_unlock;
1113
1114         net_device = ndev_ctx->nvdev;
1115         rdev = net_device->extension;
1116
1117         next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1118         if (time_is_after_jiffies(next_reconfig)) {
1119                 /* link_watch only sends one notification with current state
1120                  * per second, avoid doing reconfig more frequently. Handle
1121                  * wrap around.
1122                  */
1123                 delay = next_reconfig - jiffies;
1124                 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1125                 schedule_delayed_work(&ndev_ctx->dwork, delay);
1126                 goto out_unlock;
1127         }
1128         ndev_ctx->last_reconfig = jiffies;
1129
1130         spin_lock_irqsave(&ndev_ctx->lock, flags);
1131         if (!list_empty(&ndev_ctx->reconfig_events)) {
1132                 event = list_first_entry(&ndev_ctx->reconfig_events,
1133                                          struct netvsc_reconfig, list);
1134                 list_del(&event->list);
1135                 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1136         }
1137         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1138
1139         if (!event)
1140                 goto out_unlock;
1141
1142         switch (event->event) {
1143                 /* Only the following events are possible due to the check in
1144                  * netvsc_linkstatus_callback()
1145                  */
1146         case RNDIS_STATUS_MEDIA_CONNECT:
1147                 if (rdev->link_state) {
1148                         rdev->link_state = false;
1149                         netif_carrier_on(net);
1150                         netif_tx_wake_all_queues(net);
1151                 } else {
1152                         notify = true;
1153                 }
1154                 kfree(event);
1155                 break;
1156         case RNDIS_STATUS_MEDIA_DISCONNECT:
1157                 if (!rdev->link_state) {
1158                         rdev->link_state = true;
1159                         netif_carrier_off(net);
1160                         netif_tx_stop_all_queues(net);
1161                 }
1162                 kfree(event);
1163                 break;
1164         case RNDIS_STATUS_NETWORK_CHANGE:
1165                 /* Only makes sense if carrier is present */
1166                 if (!rdev->link_state) {
1167                         rdev->link_state = true;
1168                         netif_carrier_off(net);
1169                         netif_tx_stop_all_queues(net);
1170                         event->event = RNDIS_STATUS_MEDIA_CONNECT;
1171                         spin_lock_irqsave(&ndev_ctx->lock, flags);
1172                         list_add(&event->list, &ndev_ctx->reconfig_events);
1173                         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1174                         reschedule = true;
1175                 }
1176                 break;
1177         }
1178
1179         rtnl_unlock();
1180
1181         if (notify)
1182                 netdev_notify_peers(net);
1183
1184         /* link_watch only sends one notification with current state per
1185          * second, handle next reconfig event in 2 seconds.
1186          */
1187         if (reschedule)
1188                 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1189
1190         return;
1191
1192 out_unlock:
1193         rtnl_unlock();
1194 }
1195
1196 static void netvsc_free_netdev(struct net_device *netdev)
1197 {
1198         struct net_device_context *net_device_ctx = netdev_priv(netdev);
1199
1200         free_percpu(net_device_ctx->tx_stats);
1201         free_percpu(net_device_ctx->rx_stats);
1202         free_netdev(netdev);
1203 }
1204
1205 static struct net_device *get_netvsc_bymac(const u8 *mac)
1206 {
1207         struct net_device *dev;
1208
1209         ASSERT_RTNL();
1210
1211         for_each_netdev(&init_net, dev) {
1212                 if (dev->netdev_ops != &device_ops)
1213                         continue;       /* not a netvsc device */
1214
1215                 if (ether_addr_equal(mac, dev->perm_addr))
1216                         return dev;
1217         }
1218
1219         return NULL;
1220 }
1221
1222 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1223 {
1224         struct net_device *dev;
1225
1226         ASSERT_RTNL();
1227
1228         for_each_netdev(&init_net, dev) {
1229                 struct net_device_context *net_device_ctx;
1230
1231                 if (dev->netdev_ops != &device_ops)
1232                         continue;       /* not a netvsc device */
1233
1234                 net_device_ctx = netdev_priv(dev);
1235                 if (net_device_ctx->nvdev == NULL)
1236                         continue;       /* device is removed */
1237
1238                 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1239                         return dev;     /* a match */
1240         }
1241
1242         return NULL;
1243 }
1244
1245 static int netvsc_register_vf(struct net_device *vf_netdev)
1246 {
1247         struct net_device *ndev;
1248         struct net_device_context *net_device_ctx;
1249         struct netvsc_device *netvsc_dev;
1250
1251         if (vf_netdev->addr_len != ETH_ALEN)
1252                 return NOTIFY_DONE;
1253
1254         /*
1255          * We will use the MAC address to locate the synthetic interface to
1256          * associate with the VF interface. If we don't find a matching
1257          * synthetic interface, move on.
1258          */
1259         ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1260         if (!ndev)
1261                 return NOTIFY_DONE;
1262
1263         net_device_ctx = netdev_priv(ndev);
1264         netvsc_dev = net_device_ctx->nvdev;
1265         if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1266                 return NOTIFY_DONE;
1267
1268         netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1269         /*
1270          * Take a reference on the module.
1271          */
1272         try_module_get(THIS_MODULE);
1273
1274         dev_hold(vf_netdev);
1275         rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1276         return NOTIFY_OK;
1277 }
1278
1279 static int netvsc_vf_up(struct net_device *vf_netdev)
1280 {
1281         struct net_device *ndev;
1282         struct netvsc_device *netvsc_dev;
1283         struct net_device_context *net_device_ctx;
1284
1285         ndev = get_netvsc_byref(vf_netdev);
1286         if (!ndev)
1287                 return NOTIFY_DONE;
1288
1289         net_device_ctx = netdev_priv(ndev);
1290         netvsc_dev = net_device_ctx->nvdev;
1291
1292         netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
1293
1294         /*
1295          * Open the device before switching data path.
1296          */
1297         rndis_filter_open(netvsc_dev);
1298
1299         /*
1300          * notify the host to switch the data path.
1301          */
1302         netvsc_switch_datapath(ndev, true);
1303         netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1304
1305         netif_carrier_off(ndev);
1306
1307         /* Now notify peers through VF device. */
1308         call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
1309
1310         return NOTIFY_OK;
1311 }
1312
1313 static int netvsc_vf_down(struct net_device *vf_netdev)
1314 {
1315         struct net_device *ndev;
1316         struct netvsc_device *netvsc_dev;
1317         struct net_device_context *net_device_ctx;
1318
1319         ndev = get_netvsc_byref(vf_netdev);
1320         if (!ndev)
1321                 return NOTIFY_DONE;
1322
1323         net_device_ctx = netdev_priv(ndev);
1324         netvsc_dev = net_device_ctx->nvdev;
1325
1326         netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
1327         netvsc_switch_datapath(ndev, false);
1328         netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1329         rndis_filter_close(netvsc_dev);
1330         netif_carrier_on(ndev);
1331
1332         /* Now notify peers through netvsc device. */
1333         call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
1334
1335         return NOTIFY_OK;
1336 }
1337
1338 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1339 {
1340         struct net_device *ndev;
1341         struct netvsc_device *netvsc_dev;
1342         struct net_device_context *net_device_ctx;
1343
1344         ndev = get_netvsc_byref(vf_netdev);
1345         if (!ndev)
1346                 return NOTIFY_DONE;
1347
1348         net_device_ctx = netdev_priv(ndev);
1349         netvsc_dev = net_device_ctx->nvdev;
1350
1351         netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1352
1353         RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1354         dev_put(vf_netdev);
1355         module_put(THIS_MODULE);
1356         return NOTIFY_OK;
1357 }
1358
1359 static int netvsc_probe(struct hv_device *dev,
1360                         const struct hv_vmbus_device_id *dev_id)
1361 {
1362         struct net_device *net = NULL;
1363         struct net_device_context *net_device_ctx;
1364         struct netvsc_device_info device_info;
1365         struct netvsc_device *nvdev;
1366         int ret;
1367
1368         net = alloc_etherdev_mq(sizeof(struct net_device_context),
1369                                 num_online_cpus());
1370         if (!net)
1371                 return -ENOMEM;
1372
1373         netif_carrier_off(net);
1374
1375         netvsc_init_settings(net);
1376
1377         net_device_ctx = netdev_priv(net);
1378         net_device_ctx->device_ctx = dev;
1379         net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1380         if (netif_msg_probe(net_device_ctx))
1381                 netdev_dbg(net, "netvsc msg_enable: %d\n",
1382                            net_device_ctx->msg_enable);
1383
1384         net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1385         if (!net_device_ctx->tx_stats) {
1386                 free_netdev(net);
1387                 return -ENOMEM;
1388         }
1389         net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1390         if (!net_device_ctx->rx_stats) {
1391                 free_percpu(net_device_ctx->tx_stats);
1392                 free_netdev(net);
1393                 return -ENOMEM;
1394         }
1395
1396         hv_set_drvdata(dev, net);
1397
1398         net_device_ctx->start_remove = false;
1399
1400         INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1401         INIT_WORK(&net_device_ctx->work, do_set_multicast);
1402
1403         spin_lock_init(&net_device_ctx->lock);
1404         INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1405
1406         net->netdev_ops = &device_ops;
1407
1408         net->hw_features = NETVSC_HW_FEATURES;
1409         net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
1410
1411         net->ethtool_ops = &ethtool_ops;
1412         SET_NETDEV_DEV(net, &dev->device);
1413
1414         /* We always need headroom for rndis header */
1415         net->needed_headroom = RNDIS_AND_PPI_SIZE;
1416
1417         /* Notify the netvsc driver of the new device */
1418         memset(&device_info, 0, sizeof(device_info));
1419         device_info.ring_size = ring_size;
1420         device_info.max_num_vrss_chns = max_num_vrss_chns;
1421         ret = rndis_filter_device_add(dev, &device_info);
1422         if (ret != 0) {
1423                 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1424                 netvsc_free_netdev(net);
1425                 hv_set_drvdata(dev, NULL);
1426                 return ret;
1427         }
1428         memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1429
1430         nvdev = net_device_ctx->nvdev;
1431         netif_set_real_num_tx_queues(net, nvdev->num_chn);
1432         netif_set_real_num_rx_queues(net, nvdev->num_chn);
1433
1434         ret = register_netdev(net);
1435         if (ret != 0) {
1436                 pr_err("Unable to register netdev.\n");
1437                 rndis_filter_device_remove(dev);
1438                 netvsc_free_netdev(net);
1439         }
1440
1441         return ret;
1442 }
1443
1444 static int netvsc_remove(struct hv_device *dev)
1445 {
1446         struct net_device *net;
1447         struct net_device_context *ndev_ctx;
1448         struct netvsc_device *net_device;
1449
1450         net = hv_get_drvdata(dev);
1451
1452         if (net == NULL) {
1453                 dev_err(&dev->device, "No net device to remove\n");
1454                 return 0;
1455         }
1456
1457         ndev_ctx = netdev_priv(net);
1458         net_device = ndev_ctx->nvdev;
1459
1460         /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1461          * removing the device.
1462          */
1463         rtnl_lock();
1464         ndev_ctx->start_remove = true;
1465         rtnl_unlock();
1466
1467         cancel_delayed_work_sync(&ndev_ctx->dwork);
1468         cancel_work_sync(&ndev_ctx->work);
1469
1470         /* Stop outbound asap */
1471         netif_tx_disable(net);
1472
1473         unregister_netdev(net);
1474
1475         /*
1476          * Call to the vsc driver to let it know that the device is being
1477          * removed
1478          */
1479         rndis_filter_device_remove(dev);
1480
1481         hv_set_drvdata(dev, NULL);
1482
1483         netvsc_free_netdev(net);
1484         return 0;
1485 }
1486
1487 static const struct hv_vmbus_device_id id_table[] = {
1488         /* Network guid */
1489         { HV_NIC_GUID, },
1490         { },
1491 };
1492
1493 MODULE_DEVICE_TABLE(vmbus, id_table);
1494
1495 /* The one and only one */
1496 static struct  hv_driver netvsc_drv = {
1497         .name = KBUILD_MODNAME,
1498         .id_table = id_table,
1499         .probe = netvsc_probe,
1500         .remove = netvsc_remove,
1501 };
1502
1503 /*
1504  * On Hyper-V, every VF interface is matched with a corresponding
1505  * synthetic interface. The synthetic interface is presented first
1506  * to the guest. When the corresponding VF instance is registered,
1507  * we will take care of switching the data path.
1508  */
1509 static int netvsc_netdev_event(struct notifier_block *this,
1510                                unsigned long event, void *ptr)
1511 {
1512         struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1513
1514         /* Skip our own events */
1515         if (event_dev->netdev_ops == &device_ops)
1516                 return NOTIFY_DONE;
1517
1518         /* Avoid non-Ethernet type devices */
1519         if (event_dev->type != ARPHRD_ETHER)
1520                 return NOTIFY_DONE;
1521
1522         /* Avoid Vlan dev with same MAC registering as VF */
1523         if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1524                 return NOTIFY_DONE;
1525
1526         /* Avoid Bonding master dev with same MAC registering as VF */
1527         if ((event_dev->priv_flags & IFF_BONDING) &&
1528             (event_dev->flags & IFF_MASTER))
1529                 return NOTIFY_DONE;
1530
1531         switch (event) {
1532         case NETDEV_REGISTER:
1533                 return netvsc_register_vf(event_dev);
1534         case NETDEV_UNREGISTER:
1535                 return netvsc_unregister_vf(event_dev);
1536         case NETDEV_UP:
1537                 return netvsc_vf_up(event_dev);
1538         case NETDEV_DOWN:
1539                 return netvsc_vf_down(event_dev);
1540         default:
1541                 return NOTIFY_DONE;
1542         }
1543 }
1544
1545 static struct notifier_block netvsc_netdev_notifier = {
1546         .notifier_call = netvsc_netdev_event,
1547 };
1548
1549 static void __exit netvsc_drv_exit(void)
1550 {
1551         unregister_netdevice_notifier(&netvsc_netdev_notifier);
1552         vmbus_driver_unregister(&netvsc_drv);
1553 }
1554
1555 static int __init netvsc_drv_init(void)
1556 {
1557         int ret;
1558
1559         if (ring_size < RING_SIZE_MIN) {
1560                 ring_size = RING_SIZE_MIN;
1561                 pr_info("Increased ring_size to %d (min allowed)\n",
1562                         ring_size);
1563         }
1564         ret = vmbus_driver_register(&netvsc_drv);
1565
1566         if (ret)
1567                 return ret;
1568
1569         register_netdevice_notifier(&netvsc_netdev_notifier);
1570         return 0;
1571 }
1572
1573 MODULE_LICENSE("GPL");
1574 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1575
1576 module_init(netvsc_drv_init);
1577 module_exit(netvsc_drv_exit);