netvsc: fix incorrect receive checksum offloading
[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
446         /*
447          * Setup the sendside checksum offload only if this is not a
448          * GSO packet.
449          */
450         if ((net_trans_info & (INFO_TCP | INFO_UDP)) && skb_is_gso(skb)) {
451                 struct ndis_tcp_lso_info *lso_info;
452
453                 rndis_msg_size += NDIS_LSO_PPI_SIZE;
454                 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
455                                     TCP_LARGESEND_PKTINFO);
456
457                 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
458                                                         ppi->ppi_offset);
459
460                 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
461                 if (net_trans_info & (INFO_IPV4 << 16)) {
462                         lso_info->lso_v2_transmit.ip_version =
463                                 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
464                         ip_hdr(skb)->tot_len = 0;
465                         ip_hdr(skb)->check = 0;
466                         tcp_hdr(skb)->check =
467                                 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
468                                                    ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
469                 } else {
470                         lso_info->lso_v2_transmit.ip_version =
471                                 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
472                         ipv6_hdr(skb)->payload_len = 0;
473                         tcp_hdr(skb)->check =
474                                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
475                                                  &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
476                 }
477                 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
478                 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
479         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
480                 if (net_trans_info & INFO_TCP) {
481                         rndis_msg_size += NDIS_CSUM_PPI_SIZE;
482                         ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
483                                             TCPIP_CHKSUM_PKTINFO);
484
485                         csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
486                                                                          ppi->ppi_offset);
487
488                         if (net_trans_info & (INFO_IPV4 << 16))
489                                 csum_info->transmit.is_ipv4 = 1;
490                         else
491                                 csum_info->transmit.is_ipv6 = 1;
492
493                         csum_info->transmit.tcp_checksum = 1;
494                         csum_info->transmit.tcp_header_offset = hdr_offset;
495                 } else {
496                         /* UDP checksum (and other) offload is not supported. */
497                         if (skb_checksum_help(skb))
498                                 goto drop;
499                 }
500         }
501
502         /* Start filling in the page buffers with the rndis hdr */
503         rndis_msg->msg_len += rndis_msg_size;
504         packet->total_data_buflen = rndis_msg->msg_len;
505         packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
506                                                skb, packet, &pb);
507
508         /* timestamp packet in software */
509         skb_tx_timestamp(skb);
510         ret = netvsc_send(net_device_ctx->device_ctx, packet,
511                           rndis_msg, &pb, skb);
512         if (likely(ret == 0)) {
513                 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
514
515                 u64_stats_update_begin(&tx_stats->syncp);
516                 tx_stats->packets++;
517                 tx_stats->bytes += skb_length;
518                 u64_stats_update_end(&tx_stats->syncp);
519                 return NETDEV_TX_OK;
520         }
521
522         if (ret == -EAGAIN) {
523                 ++net_device_ctx->eth_stats.tx_busy;
524                 return NETDEV_TX_BUSY;
525         }
526
527         if (ret == -ENOSPC)
528                 ++net_device_ctx->eth_stats.tx_no_space;
529
530 drop:
531         dev_kfree_skb_any(skb);
532         net->stats.tx_dropped++;
533
534         return NETDEV_TX_OK;
535
536 no_memory:
537         ++net_device_ctx->eth_stats.tx_no_memory;
538         goto drop;
539 }
540
541 /*
542  * netvsc_linkstatus_callback - Link up/down notification
543  */
544 void netvsc_linkstatus_callback(struct hv_device *device_obj,
545                                 struct rndis_message *resp)
546 {
547         struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
548         struct net_device *net;
549         struct net_device_context *ndev_ctx;
550         struct netvsc_reconfig *event;
551         unsigned long flags;
552
553         net = hv_get_drvdata(device_obj);
554
555         if (!net)
556                 return;
557
558         ndev_ctx = netdev_priv(net);
559
560         /* Update the physical link speed when changing to another vSwitch */
561         if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
562                 u32 speed;
563
564                 speed = *(u32 *)((void *)indicate + indicate->
565                                  status_buf_offset) / 10000;
566                 ndev_ctx->speed = speed;
567                 return;
568         }
569
570         /* Handle these link change statuses below */
571         if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
572             indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
573             indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
574                 return;
575
576         if (net->reg_state != NETREG_REGISTERED)
577                 return;
578
579         event = kzalloc(sizeof(*event), GFP_ATOMIC);
580         if (!event)
581                 return;
582         event->event = indicate->status;
583
584         spin_lock_irqsave(&ndev_ctx->lock, flags);
585         list_add_tail(&event->list, &ndev_ctx->reconfig_events);
586         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
587
588         schedule_delayed_work(&ndev_ctx->dwork, 0);
589 }
590
591 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
592                                 struct hv_netvsc_packet *packet,
593                                 struct ndis_tcp_ip_checksum_info *csum_info,
594                                 void *data, u16 vlan_tci)
595 {
596         struct sk_buff *skb;
597
598         skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
599         if (!skb)
600                 return skb;
601
602         /*
603          * Copy to skb. This copy is needed here since the memory pointed by
604          * hv_netvsc_packet cannot be deallocated
605          */
606         memcpy(skb_put(skb, packet->total_data_buflen), data,
607                packet->total_data_buflen);
608
609         skb->protocol = eth_type_trans(skb, net);
610
611         /* skb is already created with CHECKSUM_NONE */
612         skb_checksum_none_assert(skb);
613
614         /*
615          * In Linux, the IP checksum is always checked.
616          * Do L4 checksum offload if enabled and present.
617          */
618         if (csum_info && (net->features & NETIF_F_RXCSUM)) {
619                 if (csum_info->receive.tcp_checksum_succeeded ||
620                     csum_info->receive.udp_checksum_succeeded)
621                         skb->ip_summed = CHECKSUM_UNNECESSARY;
622         }
623
624         if (vlan_tci & VLAN_TAG_PRESENT)
625                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
626                                        vlan_tci);
627
628         return skb;
629 }
630
631 /*
632  * netvsc_recv_callback -  Callback when we receive a packet from the
633  * "wire" on the specified device.
634  */
635 int netvsc_recv_callback(struct hv_device *device_obj,
636                                 struct hv_netvsc_packet *packet,
637                                 void **data,
638                                 struct ndis_tcp_ip_checksum_info *csum_info,
639                                 struct vmbus_channel *channel,
640                                 u16 vlan_tci)
641 {
642         struct net_device *net = hv_get_drvdata(device_obj);
643         struct net_device_context *net_device_ctx = netdev_priv(net);
644         struct net_device *vf_netdev;
645         struct sk_buff *skb;
646         struct netvsc_stats *rx_stats;
647
648         if (net->reg_state != NETREG_REGISTERED)
649                 return NVSP_STAT_FAIL;
650
651         /*
652          * If necessary, inject this packet into the VF interface.
653          * On Hyper-V, multicast and brodcast packets are only delivered
654          * to the synthetic interface (after subjecting these to
655          * policy filters on the host). Deliver these via the VF
656          * interface in the guest.
657          */
658         vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
659         if (vf_netdev && (vf_netdev->flags & IFF_UP))
660                 net = vf_netdev;
661
662         /* Allocate a skb - TODO direct I/O to pages? */
663         skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
664         if (unlikely(!skb)) {
665                 ++net->stats.rx_dropped;
666                 return NVSP_STAT_FAIL;
667         }
668
669         if (net != vf_netdev)
670                 skb_record_rx_queue(skb,
671                                     channel->offermsg.offer.sub_channel_index);
672
673         /*
674          * Even if injecting the packet, record the statistics
675          * on the synthetic device because modifying the VF device
676          * statistics will not work correctly.
677          */
678         rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
679         u64_stats_update_begin(&rx_stats->syncp);
680         rx_stats->packets++;
681         rx_stats->bytes += packet->total_data_buflen;
682
683         if (skb->pkt_type == PACKET_BROADCAST)
684                 ++rx_stats->broadcast;
685         else if (skb->pkt_type == PACKET_MULTICAST)
686                 ++rx_stats->multicast;
687         u64_stats_update_end(&rx_stats->syncp);
688
689         /*
690          * Pass the skb back up. Network stack will deallocate the skb when it
691          * is done.
692          * TODO - use NAPI?
693          */
694         netif_rx(skb);
695
696         return 0;
697 }
698
699 static void netvsc_get_drvinfo(struct net_device *net,
700                                struct ethtool_drvinfo *info)
701 {
702         struct net_device_context *net_device_ctx = netdev_priv(net);
703         struct hv_device *dev = net_device_ctx->device_ctx;
704
705         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
706         strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
707         strlcpy(info->bus_info, vmbus_dev_name(dev), sizeof(info->bus_info));
708 }
709
710 static void netvsc_get_channels(struct net_device *net,
711                                 struct ethtool_channels *channel)
712 {
713         struct net_device_context *net_device_ctx = netdev_priv(net);
714         struct netvsc_device *nvdev = net_device_ctx->nvdev;
715
716         if (nvdev) {
717                 channel->max_combined   = nvdev->max_chn;
718                 channel->combined_count = nvdev->num_chn;
719         }
720 }
721
722 static int netvsc_set_channels(struct net_device *net,
723                                struct ethtool_channels *channels)
724 {
725         struct net_device_context *net_device_ctx = netdev_priv(net);
726         struct hv_device *dev = net_device_ctx->device_ctx;
727         struct netvsc_device *nvdev = net_device_ctx->nvdev;
728         struct netvsc_device_info device_info;
729         u32 num_chn;
730         u32 max_chn;
731         int ret = 0;
732         bool recovering = false;
733
734         if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
735                 return -ENODEV;
736
737         num_chn = nvdev->num_chn;
738         max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
739
740         if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
741                 pr_info("vRSS unsupported before NVSP Version 5\n");
742                 return -EINVAL;
743         }
744
745         /* We do not support rx, tx, or other */
746         if (!channels ||
747             channels->rx_count ||
748             channels->tx_count ||
749             channels->other_count ||
750             (channels->combined_count < 1))
751                 return -EINVAL;
752
753         if (channels->combined_count > max_chn) {
754                 pr_info("combined channels too high, using %d\n", max_chn);
755                 channels->combined_count = max_chn;
756         }
757
758         ret = netvsc_close(net);
759         if (ret)
760                 goto out;
761
762  do_set:
763         net_device_ctx->start_remove = true;
764         rndis_filter_device_remove(dev);
765
766         nvdev->num_chn = channels->combined_count;
767
768         memset(&device_info, 0, sizeof(device_info));
769         device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
770         device_info.ring_size = ring_size;
771         device_info.max_num_vrss_chns = max_num_vrss_chns;
772
773         ret = rndis_filter_device_add(dev, &device_info);
774         if (ret) {
775                 if (recovering) {
776                         netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
777                         return ret;
778                 }
779                 goto recover;
780         }
781
782         nvdev = net_device_ctx->nvdev;
783
784         ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
785         if (ret) {
786                 if (recovering) {
787                         netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
788                         return ret;
789                 }
790                 goto recover;
791         }
792
793         ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
794         if (ret) {
795                 if (recovering) {
796                         netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
797                         return ret;
798                 }
799                 goto recover;
800         }
801
802  out:
803         netvsc_open(net);
804         net_device_ctx->start_remove = false;
805         /* We may have missed link change notifications */
806         schedule_delayed_work(&net_device_ctx->dwork, 0);
807
808         return ret;
809
810  recover:
811         /* If the above failed, we attempt to recover through the same
812          * process but with the original number of channels.
813          */
814         netdev_err(net, "could not set channels, recovering\n");
815         recovering = true;
816         channels->combined_count = num_chn;
817         goto do_set;
818 }
819
820 static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
821 {
822         struct ethtool_cmd diff1 = *cmd;
823         struct ethtool_cmd diff2 = {};
824
825         ethtool_cmd_speed_set(&diff1, 0);
826         diff1.duplex = 0;
827         /* advertising and cmd are usually set */
828         diff1.advertising = 0;
829         diff1.cmd = 0;
830         /* We set port to PORT_OTHER */
831         diff2.port = PORT_OTHER;
832
833         return !memcmp(&diff1, &diff2, sizeof(diff1));
834 }
835
836 static void netvsc_init_settings(struct net_device *dev)
837 {
838         struct net_device_context *ndc = netdev_priv(dev);
839
840         ndc->speed = SPEED_UNKNOWN;
841         ndc->duplex = DUPLEX_UNKNOWN;
842 }
843
844 static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
845 {
846         struct net_device_context *ndc = netdev_priv(dev);
847
848         ethtool_cmd_speed_set(cmd, ndc->speed);
849         cmd->duplex = ndc->duplex;
850         cmd->port = PORT_OTHER;
851
852         return 0;
853 }
854
855 static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
856 {
857         struct net_device_context *ndc = netdev_priv(dev);
858         u32 speed;
859
860         speed = ethtool_cmd_speed(cmd);
861         if (!ethtool_validate_speed(speed) ||
862             !ethtool_validate_duplex(cmd->duplex) ||
863             !netvsc_validate_ethtool_ss_cmd(cmd))
864                 return -EINVAL;
865
866         ndc->speed = speed;
867         ndc->duplex = cmd->duplex;
868
869         return 0;
870 }
871
872 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
873 {
874         struct net_device_context *ndevctx = netdev_priv(ndev);
875         struct netvsc_device *nvdev = ndevctx->nvdev;
876         struct hv_device *hdev = ndevctx->device_ctx;
877         struct netvsc_device_info device_info;
878         int limit = ETH_DATA_LEN;
879         u32 num_chn;
880         int ret = 0;
881
882         if (ndevctx->start_remove || !nvdev || nvdev->destroy)
883                 return -ENODEV;
884
885         if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
886                 limit = NETVSC_MTU - ETH_HLEN;
887
888         if (mtu < NETVSC_MTU_MIN || mtu > limit)
889                 return -EINVAL;
890
891         ret = netvsc_close(ndev);
892         if (ret)
893                 goto out;
894
895         num_chn = nvdev->num_chn;
896
897         ndevctx->start_remove = true;
898         rndis_filter_device_remove(hdev);
899
900         ndev->mtu = mtu;
901
902         memset(&device_info, 0, sizeof(device_info));
903         device_info.ring_size = ring_size;
904         device_info.num_chn = num_chn;
905         device_info.max_num_vrss_chns = max_num_vrss_chns;
906         rndis_filter_device_add(hdev, &device_info);
907
908 out:
909         netvsc_open(ndev);
910         ndevctx->start_remove = false;
911
912         /* We may have missed link change notifications */
913         schedule_delayed_work(&ndevctx->dwork, 0);
914
915         return ret;
916 }
917
918 static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
919                                                     struct rtnl_link_stats64 *t)
920 {
921         struct net_device_context *ndev_ctx = netdev_priv(net);
922         int cpu;
923
924         for_each_possible_cpu(cpu) {
925                 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
926                                                             cpu);
927                 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
928                                                             cpu);
929                 u64 tx_packets, tx_bytes, rx_packets, rx_bytes, rx_multicast;
930                 unsigned int start;
931
932                 do {
933                         start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
934                         tx_packets = tx_stats->packets;
935                         tx_bytes = tx_stats->bytes;
936                 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
937
938                 do {
939                         start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
940                         rx_packets = rx_stats->packets;
941                         rx_bytes = rx_stats->bytes;
942                         rx_multicast = rx_stats->multicast + rx_stats->broadcast;
943                 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
944
945                 t->tx_bytes     += tx_bytes;
946                 t->tx_packets   += tx_packets;
947                 t->rx_bytes     += rx_bytes;
948                 t->rx_packets   += rx_packets;
949                 t->multicast    += rx_multicast;
950         }
951
952         t->tx_dropped   = net->stats.tx_dropped;
953         t->tx_errors    = net->stats.tx_dropped;
954
955         t->rx_dropped   = net->stats.rx_dropped;
956         t->rx_errors    = net->stats.rx_errors;
957
958         return t;
959 }
960
961 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
962 {
963         struct sockaddr *addr = p;
964         char save_adr[ETH_ALEN];
965         unsigned char save_aatype;
966         int err;
967
968         memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
969         save_aatype = ndev->addr_assign_type;
970
971         err = eth_mac_addr(ndev, p);
972         if (err != 0)
973                 return err;
974
975         err = rndis_filter_set_device_mac(ndev, addr->sa_data);
976         if (err != 0) {
977                 /* roll back to saved MAC */
978                 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
979                 ndev->addr_assign_type = save_aatype;
980         }
981
982         return err;
983 }
984
985 static const struct {
986         char name[ETH_GSTRING_LEN];
987         u16 offset;
988 } netvsc_stats[] = {
989         { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
990         { "tx_no_memory",  offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
991         { "tx_no_space",  offsetof(struct netvsc_ethtool_stats, tx_no_space) },
992         { "tx_too_big",   offsetof(struct netvsc_ethtool_stats, tx_too_big) },
993         { "tx_busy",      offsetof(struct netvsc_ethtool_stats, tx_busy) },
994 };
995
996 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
997 {
998         switch (string_set) {
999         case ETH_SS_STATS:
1000                 return ARRAY_SIZE(netvsc_stats);
1001         default:
1002                 return -EINVAL;
1003         }
1004 }
1005
1006 static void netvsc_get_ethtool_stats(struct net_device *dev,
1007                                      struct ethtool_stats *stats, u64 *data)
1008 {
1009         struct net_device_context *ndc = netdev_priv(dev);
1010         const void *nds = &ndc->eth_stats;
1011         int i;
1012
1013         for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1014                 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1015 }
1016
1017 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1018 {
1019         int i;
1020
1021         switch (stringset) {
1022         case ETH_SS_STATS:
1023                 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1024                         memcpy(data + i * ETH_GSTRING_LEN,
1025                                netvsc_stats[i].name, ETH_GSTRING_LEN);
1026                 break;
1027         }
1028 }
1029
1030 #ifdef CONFIG_NET_POLL_CONTROLLER
1031 static void netvsc_poll_controller(struct net_device *net)
1032 {
1033         /* As netvsc_start_xmit() works synchronous we don't have to
1034          * trigger anything here.
1035          */
1036 }
1037 #endif
1038
1039 static const struct ethtool_ops ethtool_ops = {
1040         .get_drvinfo    = netvsc_get_drvinfo,
1041         .get_link       = ethtool_op_get_link,
1042         .get_ethtool_stats = netvsc_get_ethtool_stats,
1043         .get_sset_count = netvsc_get_sset_count,
1044         .get_strings    = netvsc_get_strings,
1045         .get_channels   = netvsc_get_channels,
1046         .set_channels   = netvsc_set_channels,
1047         .get_ts_info    = ethtool_op_get_ts_info,
1048         .get_settings   = netvsc_get_settings,
1049         .set_settings   = netvsc_set_settings,
1050 };
1051
1052 static const struct net_device_ops device_ops = {
1053         .ndo_open =                     netvsc_open,
1054         .ndo_stop =                     netvsc_close,
1055         .ndo_start_xmit =               netvsc_start_xmit,
1056         .ndo_set_rx_mode =              netvsc_set_multicast_list,
1057         .ndo_change_mtu =               netvsc_change_mtu,
1058         .ndo_validate_addr =            eth_validate_addr,
1059         .ndo_set_mac_address =          netvsc_set_mac_addr,
1060         .ndo_select_queue =             netvsc_select_queue,
1061         .ndo_get_stats64 =              netvsc_get_stats64,
1062 #ifdef CONFIG_NET_POLL_CONTROLLER
1063         .ndo_poll_controller =          netvsc_poll_controller,
1064 #endif
1065 };
1066
1067 /*
1068  * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1069  * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1070  * present send GARP packet to network peers with netif_notify_peers().
1071  */
1072 static void netvsc_link_change(struct work_struct *w)
1073 {
1074         struct net_device_context *ndev_ctx =
1075                 container_of(w, struct net_device_context, dwork.work);
1076         struct hv_device *device_obj = ndev_ctx->device_ctx;
1077         struct net_device *net = hv_get_drvdata(device_obj);
1078         struct netvsc_device *net_device;
1079         struct rndis_device *rdev;
1080         struct netvsc_reconfig *event = NULL;
1081         bool notify = false, reschedule = false;
1082         unsigned long flags, next_reconfig, delay;
1083
1084         rtnl_lock();
1085         if (ndev_ctx->start_remove)
1086                 goto out_unlock;
1087
1088         net_device = ndev_ctx->nvdev;
1089         rdev = net_device->extension;
1090
1091         next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1092         if (time_is_after_jiffies(next_reconfig)) {
1093                 /* link_watch only sends one notification with current state
1094                  * per second, avoid doing reconfig more frequently. Handle
1095                  * wrap around.
1096                  */
1097                 delay = next_reconfig - jiffies;
1098                 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1099                 schedule_delayed_work(&ndev_ctx->dwork, delay);
1100                 goto out_unlock;
1101         }
1102         ndev_ctx->last_reconfig = jiffies;
1103
1104         spin_lock_irqsave(&ndev_ctx->lock, flags);
1105         if (!list_empty(&ndev_ctx->reconfig_events)) {
1106                 event = list_first_entry(&ndev_ctx->reconfig_events,
1107                                          struct netvsc_reconfig, list);
1108                 list_del(&event->list);
1109                 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1110         }
1111         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1112
1113         if (!event)
1114                 goto out_unlock;
1115
1116         switch (event->event) {
1117                 /* Only the following events are possible due to the check in
1118                  * netvsc_linkstatus_callback()
1119                  */
1120         case RNDIS_STATUS_MEDIA_CONNECT:
1121                 if (rdev->link_state) {
1122                         rdev->link_state = false;
1123                         netif_carrier_on(net);
1124                         netif_tx_wake_all_queues(net);
1125                 } else {
1126                         notify = true;
1127                 }
1128                 kfree(event);
1129                 break;
1130         case RNDIS_STATUS_MEDIA_DISCONNECT:
1131                 if (!rdev->link_state) {
1132                         rdev->link_state = true;
1133                         netif_carrier_off(net);
1134                         netif_tx_stop_all_queues(net);
1135                 }
1136                 kfree(event);
1137                 break;
1138         case RNDIS_STATUS_NETWORK_CHANGE:
1139                 /* Only makes sense if carrier is present */
1140                 if (!rdev->link_state) {
1141                         rdev->link_state = true;
1142                         netif_carrier_off(net);
1143                         netif_tx_stop_all_queues(net);
1144                         event->event = RNDIS_STATUS_MEDIA_CONNECT;
1145                         spin_lock_irqsave(&ndev_ctx->lock, flags);
1146                         list_add(&event->list, &ndev_ctx->reconfig_events);
1147                         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1148                         reschedule = true;
1149                 }
1150                 break;
1151         }
1152
1153         rtnl_unlock();
1154
1155         if (notify)
1156                 netdev_notify_peers(net);
1157
1158         /* link_watch only sends one notification with current state per
1159          * second, handle next reconfig event in 2 seconds.
1160          */
1161         if (reschedule)
1162                 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1163
1164         return;
1165
1166 out_unlock:
1167         rtnl_unlock();
1168 }
1169
1170 static void netvsc_free_netdev(struct net_device *netdev)
1171 {
1172         struct net_device_context *net_device_ctx = netdev_priv(netdev);
1173
1174         free_percpu(net_device_ctx->tx_stats);
1175         free_percpu(net_device_ctx->rx_stats);
1176         free_netdev(netdev);
1177 }
1178
1179 static struct net_device *get_netvsc_bymac(const u8 *mac)
1180 {
1181         struct net_device *dev;
1182
1183         ASSERT_RTNL();
1184
1185         for_each_netdev(&init_net, dev) {
1186                 if (dev->netdev_ops != &device_ops)
1187                         continue;       /* not a netvsc device */
1188
1189                 if (ether_addr_equal(mac, dev->perm_addr))
1190                         return dev;
1191         }
1192
1193         return NULL;
1194 }
1195
1196 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1197 {
1198         struct net_device *dev;
1199
1200         ASSERT_RTNL();
1201
1202         for_each_netdev(&init_net, dev) {
1203                 struct net_device_context *net_device_ctx;
1204
1205                 if (dev->netdev_ops != &device_ops)
1206                         continue;       /* not a netvsc device */
1207
1208                 net_device_ctx = netdev_priv(dev);
1209                 if (net_device_ctx->nvdev == NULL)
1210                         continue;       /* device is removed */
1211
1212                 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1213                         return dev;     /* a match */
1214         }
1215
1216         return NULL;
1217 }
1218
1219 static int netvsc_register_vf(struct net_device *vf_netdev)
1220 {
1221         struct net_device *ndev;
1222         struct net_device_context *net_device_ctx;
1223         struct netvsc_device *netvsc_dev;
1224
1225         if (vf_netdev->addr_len != ETH_ALEN)
1226                 return NOTIFY_DONE;
1227
1228         /*
1229          * We will use the MAC address to locate the synthetic interface to
1230          * associate with the VF interface. If we don't find a matching
1231          * synthetic interface, move on.
1232          */
1233         ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1234         if (!ndev)
1235                 return NOTIFY_DONE;
1236
1237         net_device_ctx = netdev_priv(ndev);
1238         netvsc_dev = net_device_ctx->nvdev;
1239         if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1240                 return NOTIFY_DONE;
1241
1242         netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1243         /*
1244          * Take a reference on the module.
1245          */
1246         try_module_get(THIS_MODULE);
1247
1248         dev_hold(vf_netdev);
1249         rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1250         return NOTIFY_OK;
1251 }
1252
1253 static int netvsc_vf_up(struct net_device *vf_netdev)
1254 {
1255         struct net_device *ndev;
1256         struct netvsc_device *netvsc_dev;
1257         struct net_device_context *net_device_ctx;
1258
1259         ndev = get_netvsc_byref(vf_netdev);
1260         if (!ndev)
1261                 return NOTIFY_DONE;
1262
1263         net_device_ctx = netdev_priv(ndev);
1264         netvsc_dev = net_device_ctx->nvdev;
1265
1266         netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
1267
1268         /*
1269          * Open the device before switching data path.
1270          */
1271         rndis_filter_open(netvsc_dev);
1272
1273         /*
1274          * notify the host to switch the data path.
1275          */
1276         netvsc_switch_datapath(ndev, true);
1277         netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1278
1279         netif_carrier_off(ndev);
1280
1281         /* Now notify peers through VF device. */
1282         call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
1283
1284         return NOTIFY_OK;
1285 }
1286
1287 static int netvsc_vf_down(struct net_device *vf_netdev)
1288 {
1289         struct net_device *ndev;
1290         struct netvsc_device *netvsc_dev;
1291         struct net_device_context *net_device_ctx;
1292
1293         ndev = get_netvsc_byref(vf_netdev);
1294         if (!ndev)
1295                 return NOTIFY_DONE;
1296
1297         net_device_ctx = netdev_priv(ndev);
1298         netvsc_dev = net_device_ctx->nvdev;
1299
1300         netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
1301         netvsc_switch_datapath(ndev, false);
1302         netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1303         rndis_filter_close(netvsc_dev);
1304         netif_carrier_on(ndev);
1305
1306         /* Now notify peers through netvsc device. */
1307         call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
1308
1309         return NOTIFY_OK;
1310 }
1311
1312 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1313 {
1314         struct net_device *ndev;
1315         struct netvsc_device *netvsc_dev;
1316         struct net_device_context *net_device_ctx;
1317
1318         ndev = get_netvsc_byref(vf_netdev);
1319         if (!ndev)
1320                 return NOTIFY_DONE;
1321
1322         net_device_ctx = netdev_priv(ndev);
1323         netvsc_dev = net_device_ctx->nvdev;
1324
1325         netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1326
1327         RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1328         dev_put(vf_netdev);
1329         module_put(THIS_MODULE);
1330         return NOTIFY_OK;
1331 }
1332
1333 static int netvsc_probe(struct hv_device *dev,
1334                         const struct hv_vmbus_device_id *dev_id)
1335 {
1336         struct net_device *net = NULL;
1337         struct net_device_context *net_device_ctx;
1338         struct netvsc_device_info device_info;
1339         struct netvsc_device *nvdev;
1340         int ret;
1341
1342         net = alloc_etherdev_mq(sizeof(struct net_device_context),
1343                                 num_online_cpus());
1344         if (!net)
1345                 return -ENOMEM;
1346
1347         netif_carrier_off(net);
1348
1349         netvsc_init_settings(net);
1350
1351         net_device_ctx = netdev_priv(net);
1352         net_device_ctx->device_ctx = dev;
1353         net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1354         if (netif_msg_probe(net_device_ctx))
1355                 netdev_dbg(net, "netvsc msg_enable: %d\n",
1356                            net_device_ctx->msg_enable);
1357
1358         net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1359         if (!net_device_ctx->tx_stats) {
1360                 free_netdev(net);
1361                 return -ENOMEM;
1362         }
1363         net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1364         if (!net_device_ctx->rx_stats) {
1365                 free_percpu(net_device_ctx->tx_stats);
1366                 free_netdev(net);
1367                 return -ENOMEM;
1368         }
1369
1370         hv_set_drvdata(dev, net);
1371
1372         net_device_ctx->start_remove = false;
1373
1374         INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1375         INIT_WORK(&net_device_ctx->work, do_set_multicast);
1376
1377         spin_lock_init(&net_device_ctx->lock);
1378         INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1379
1380         net->netdev_ops = &device_ops;
1381
1382         net->hw_features = NETVSC_HW_FEATURES;
1383         net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
1384
1385         net->ethtool_ops = &ethtool_ops;
1386         SET_NETDEV_DEV(net, &dev->device);
1387
1388         /* We always need headroom for rndis header */
1389         net->needed_headroom = RNDIS_AND_PPI_SIZE;
1390
1391         /* Notify the netvsc driver of the new device */
1392         memset(&device_info, 0, sizeof(device_info));
1393         device_info.ring_size = ring_size;
1394         device_info.max_num_vrss_chns = max_num_vrss_chns;
1395         ret = rndis_filter_device_add(dev, &device_info);
1396         if (ret != 0) {
1397                 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1398                 netvsc_free_netdev(net);
1399                 hv_set_drvdata(dev, NULL);
1400                 return ret;
1401         }
1402         memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1403
1404         nvdev = net_device_ctx->nvdev;
1405         netif_set_real_num_tx_queues(net, nvdev->num_chn);
1406         netif_set_real_num_rx_queues(net, nvdev->num_chn);
1407
1408         ret = register_netdev(net);
1409         if (ret != 0) {
1410                 pr_err("Unable to register netdev.\n");
1411                 rndis_filter_device_remove(dev);
1412                 netvsc_free_netdev(net);
1413         }
1414
1415         return ret;
1416 }
1417
1418 static int netvsc_remove(struct hv_device *dev)
1419 {
1420         struct net_device *net;
1421         struct net_device_context *ndev_ctx;
1422         struct netvsc_device *net_device;
1423
1424         net = hv_get_drvdata(dev);
1425
1426         if (net == NULL) {
1427                 dev_err(&dev->device, "No net device to remove\n");
1428                 return 0;
1429         }
1430
1431         ndev_ctx = netdev_priv(net);
1432         net_device = ndev_ctx->nvdev;
1433
1434         /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1435          * removing the device.
1436          */
1437         rtnl_lock();
1438         ndev_ctx->start_remove = true;
1439         rtnl_unlock();
1440
1441         cancel_delayed_work_sync(&ndev_ctx->dwork);
1442         cancel_work_sync(&ndev_ctx->work);
1443
1444         /* Stop outbound asap */
1445         netif_tx_disable(net);
1446
1447         unregister_netdev(net);
1448
1449         /*
1450          * Call to the vsc driver to let it know that the device is being
1451          * removed
1452          */
1453         rndis_filter_device_remove(dev);
1454
1455         hv_set_drvdata(dev, NULL);
1456
1457         netvsc_free_netdev(net);
1458         return 0;
1459 }
1460
1461 static const struct hv_vmbus_device_id id_table[] = {
1462         /* Network guid */
1463         { HV_NIC_GUID, },
1464         { },
1465 };
1466
1467 MODULE_DEVICE_TABLE(vmbus, id_table);
1468
1469 /* The one and only one */
1470 static struct  hv_driver netvsc_drv = {
1471         .name = KBUILD_MODNAME,
1472         .id_table = id_table,
1473         .probe = netvsc_probe,
1474         .remove = netvsc_remove,
1475 };
1476
1477 /*
1478  * On Hyper-V, every VF interface is matched with a corresponding
1479  * synthetic interface. The synthetic interface is presented first
1480  * to the guest. When the corresponding VF instance is registered,
1481  * we will take care of switching the data path.
1482  */
1483 static int netvsc_netdev_event(struct notifier_block *this,
1484                                unsigned long event, void *ptr)
1485 {
1486         struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1487
1488         /* Skip our own events */
1489         if (event_dev->netdev_ops == &device_ops)
1490                 return NOTIFY_DONE;
1491
1492         /* Avoid non-Ethernet type devices */
1493         if (event_dev->type != ARPHRD_ETHER)
1494                 return NOTIFY_DONE;
1495
1496         /* Avoid Vlan dev with same MAC registering as VF */
1497         if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1498                 return NOTIFY_DONE;
1499
1500         /* Avoid Bonding master dev with same MAC registering as VF */
1501         if ((event_dev->priv_flags & IFF_BONDING) &&
1502             (event_dev->flags & IFF_MASTER))
1503                 return NOTIFY_DONE;
1504
1505         switch (event) {
1506         case NETDEV_REGISTER:
1507                 return netvsc_register_vf(event_dev);
1508         case NETDEV_UNREGISTER:
1509                 return netvsc_unregister_vf(event_dev);
1510         case NETDEV_UP:
1511                 return netvsc_vf_up(event_dev);
1512         case NETDEV_DOWN:
1513                 return netvsc_vf_down(event_dev);
1514         default:
1515                 return NOTIFY_DONE;
1516         }
1517 }
1518
1519 static struct notifier_block netvsc_netdev_notifier = {
1520         .notifier_call = netvsc_netdev_event,
1521 };
1522
1523 static void __exit netvsc_drv_exit(void)
1524 {
1525         unregister_netdevice_notifier(&netvsc_netdev_notifier);
1526         vmbus_driver_unregister(&netvsc_drv);
1527 }
1528
1529 static int __init netvsc_drv_init(void)
1530 {
1531         int ret;
1532
1533         if (ring_size < RING_SIZE_MIN) {
1534                 ring_size = RING_SIZE_MIN;
1535                 pr_info("Increased ring_size to %d (min allowed)\n",
1536                         ring_size);
1537         }
1538         ret = vmbus_driver_register(&netvsc_drv);
1539
1540         if (ret)
1541                 return ret;
1542
1543         register_netdevice_notifier(&netvsc_netdev_notifier);
1544         return 0;
1545 }
1546
1547 MODULE_LICENSE("GPL");
1548 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
1549
1550 module_init(netvsc_drv_init);
1551 module_exit(netvsc_drv_exit);