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