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