hv_netvsc: Fix the TX/RX buffer default sizes
[platform/kernel/linux-starfive.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 <linux/rtnetlink.h>
37 #include <linux/netpoll.h>
38 #include <linux/reciprocal_div.h>
39
40 #include <net/arp.h>
41 #include <net/route.h>
42 #include <net/sock.h>
43 #include <net/pkt_sched.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46
47 #include "hyperv_net.h"
48
49 #define RING_SIZE_MIN           64
50
51 #define LINKCHANGE_INT (2 * HZ)
52 #define VF_TAKEOVER_INT (HZ / 10)
53
54 static unsigned int ring_size __ro_after_init = 128;
55 module_param(ring_size, uint, S_IRUGO);
56 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
57 unsigned int netvsc_ring_bytes __ro_after_init;
58 struct reciprocal_value netvsc_ring_reciprocal __ro_after_init;
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 netvsc_set_multicast_list(struct net_device *net)
70 {
71         struct net_device_context *net_device_ctx = netdev_priv(net);
72         struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
73
74         rndis_filter_update(nvdev);
75 }
76
77 static int netvsc_open(struct net_device *net)
78 {
79         struct net_device_context *ndev_ctx = netdev_priv(net);
80         struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
81         struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
82         struct rndis_device *rdev;
83         int ret = 0;
84
85         netif_carrier_off(net);
86
87         /* Open up the device */
88         ret = rndis_filter_open(nvdev);
89         if (ret != 0) {
90                 netdev_err(net, "unable to open device (ret %d).\n", ret);
91                 return ret;
92         }
93
94         netif_tx_wake_all_queues(net);
95
96         rdev = nvdev->extension;
97
98         if (!rdev->link_state)
99                 netif_carrier_on(net);
100
101         if (vf_netdev) {
102                 /* Setting synthetic device up transparently sets
103                  * slave as up. If open fails, then slave will be
104                  * still be offline (and not used).
105                  */
106                 ret = dev_open(vf_netdev);
107                 if (ret)
108                         netdev_warn(net,
109                                     "unable to open slave: %s: %d\n",
110                                     vf_netdev->name, ret);
111         }
112         return 0;
113 }
114
115 static int netvsc_close(struct net_device *net)
116 {
117         struct net_device_context *net_device_ctx = netdev_priv(net);
118         struct net_device *vf_netdev
119                 = rtnl_dereference(net_device_ctx->vf_netdev);
120         struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
121         int ret = 0;
122         u32 aread, i, msec = 10, retry = 0, retry_max = 20;
123         struct vmbus_channel *chn;
124
125         netif_tx_disable(net);
126
127         /* No need to close rndis filter if it is removed already */
128         if (!nvdev)
129                 goto out;
130
131         ret = rndis_filter_close(nvdev);
132         if (ret != 0) {
133                 netdev_err(net, "unable to close device (ret %d).\n", ret);
134                 return ret;
135         }
136
137         /* Ensure pending bytes in ring are read */
138         while (true) {
139                 aread = 0;
140                 for (i = 0; i < nvdev->num_chn; i++) {
141                         chn = nvdev->chan_table[i].channel;
142                         if (!chn)
143                                 continue;
144
145                         aread = hv_get_bytes_to_read(&chn->inbound);
146                         if (aread)
147                                 break;
148
149                         aread = hv_get_bytes_to_read(&chn->outbound);
150                         if (aread)
151                                 break;
152                 }
153
154                 retry++;
155                 if (retry > retry_max || aread == 0)
156                         break;
157
158                 msleep(msec);
159
160                 if (msec < 1000)
161                         msec *= 2;
162         }
163
164         if (aread) {
165                 netdev_err(net, "Ring buffer not empty after closing rndis\n");
166                 ret = -ETIMEDOUT;
167         }
168
169 out:
170         if (vf_netdev)
171                 dev_close(vf_netdev);
172
173         return ret;
174 }
175
176 static inline void *init_ppi_data(struct rndis_message *msg,
177                                   u32 ppi_size, u32 pkt_type)
178 {
179         struct rndis_packet *rndis_pkt = &msg->msg.pkt;
180         struct rndis_per_packet_info *ppi;
181
182         rndis_pkt->data_offset += ppi_size;
183         ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
184                 + rndis_pkt->per_pkt_info_len;
185
186         ppi->size = ppi_size;
187         ppi->type = pkt_type;
188         ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
189
190         rndis_pkt->per_pkt_info_len += ppi_size;
191
192         return ppi + 1;
193 }
194
195 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
196  * packets. We can use ethtool to change UDP hash level when necessary.
197  */
198 static inline u32 netvsc_get_hash(
199         struct sk_buff *skb,
200         const struct net_device_context *ndc)
201 {
202         struct flow_keys flow;
203         u32 hash, pkt_proto = 0;
204         static u32 hashrnd __read_mostly;
205
206         net_get_random_once(&hashrnd, sizeof(hashrnd));
207
208         if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
209                 return 0;
210
211         switch (flow.basic.ip_proto) {
212         case IPPROTO_TCP:
213                 if (flow.basic.n_proto == htons(ETH_P_IP))
214                         pkt_proto = HV_TCP4_L4HASH;
215                 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
216                         pkt_proto = HV_TCP6_L4HASH;
217
218                 break;
219
220         case IPPROTO_UDP:
221                 if (flow.basic.n_proto == htons(ETH_P_IP))
222                         pkt_proto = HV_UDP4_L4HASH;
223                 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
224                         pkt_proto = HV_UDP6_L4HASH;
225
226                 break;
227         }
228
229         if (pkt_proto & ndc->l4_hash) {
230                 return skb_get_hash(skb);
231         } else {
232                 if (flow.basic.n_proto == htons(ETH_P_IP))
233                         hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
234                 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
235                         hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
236                 else
237                         hash = 0;
238
239                 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
240         }
241
242         return hash;
243 }
244
245 static inline int netvsc_get_tx_queue(struct net_device *ndev,
246                                       struct sk_buff *skb, int old_idx)
247 {
248         const struct net_device_context *ndc = netdev_priv(ndev);
249         struct sock *sk = skb->sk;
250         int q_idx;
251
252         q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
253                               (VRSS_SEND_TAB_SIZE - 1)];
254
255         /* If queue index changed record the new value */
256         if (q_idx != old_idx &&
257             sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
258                 sk_tx_queue_set(sk, q_idx);
259
260         return q_idx;
261 }
262
263 /*
264  * Select queue for transmit.
265  *
266  * If a valid queue has already been assigned, then use that.
267  * Otherwise compute tx queue based on hash and the send table.
268  *
269  * This is basically similar to default (__netdev_pick_tx) with the added step
270  * of using the host send_table when no other queue has been assigned.
271  *
272  * TODO support XPS - but get_xps_queue not exported
273  */
274 static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
275 {
276         int q_idx = sk_tx_queue_get(skb->sk);
277
278         if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
279                 /* If forwarding a packet, we use the recorded queue when
280                  * available for better cache locality.
281                  */
282                 if (skb_rx_queue_recorded(skb))
283                         q_idx = skb_get_rx_queue(skb);
284                 else
285                         q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
286         }
287
288         return q_idx;
289 }
290
291 static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
292                                void *accel_priv,
293                                select_queue_fallback_t fallback)
294 {
295         struct net_device_context *ndc = netdev_priv(ndev);
296         struct net_device *vf_netdev;
297         u16 txq;
298
299         rcu_read_lock();
300         vf_netdev = rcu_dereference(ndc->vf_netdev);
301         if (vf_netdev) {
302                 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
303                 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
304         } else {
305                 txq = netvsc_pick_tx(ndev, skb);
306         }
307         rcu_read_unlock();
308
309         while (unlikely(txq >= ndev->real_num_tx_queues))
310                 txq -= ndev->real_num_tx_queues;
311
312         return txq;
313 }
314
315 static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
316                        struct hv_page_buffer *pb)
317 {
318         int j = 0;
319
320         /* Deal with compund pages by ignoring unused part
321          * of the page.
322          */
323         page += (offset >> PAGE_SHIFT);
324         offset &= ~PAGE_MASK;
325
326         while (len > 0) {
327                 unsigned long bytes;
328
329                 bytes = PAGE_SIZE - offset;
330                 if (bytes > len)
331                         bytes = len;
332                 pb[j].pfn = page_to_pfn(page);
333                 pb[j].offset = offset;
334                 pb[j].len = bytes;
335
336                 offset += bytes;
337                 len -= bytes;
338
339                 if (offset == PAGE_SIZE && len) {
340                         page++;
341                         offset = 0;
342                         j++;
343                 }
344         }
345
346         return j + 1;
347 }
348
349 static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
350                            struct hv_netvsc_packet *packet,
351                            struct hv_page_buffer *pb)
352 {
353         u32 slots_used = 0;
354         char *data = skb->data;
355         int frags = skb_shinfo(skb)->nr_frags;
356         int i;
357
358         /* The packet is laid out thus:
359          * 1. hdr: RNDIS header and PPI
360          * 2. skb linear data
361          * 3. skb fragment data
362          */
363         slots_used += fill_pg_buf(virt_to_page(hdr),
364                                   offset_in_page(hdr),
365                                   len, &pb[slots_used]);
366
367         packet->rmsg_size = len;
368         packet->rmsg_pgcnt = slots_used;
369
370         slots_used += fill_pg_buf(virt_to_page(data),
371                                 offset_in_page(data),
372                                 skb_headlen(skb), &pb[slots_used]);
373
374         for (i = 0; i < frags; i++) {
375                 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
376
377                 slots_used += fill_pg_buf(skb_frag_page(frag),
378                                         frag->page_offset,
379                                         skb_frag_size(frag), &pb[slots_used]);
380         }
381         return slots_used;
382 }
383
384 static int count_skb_frag_slots(struct sk_buff *skb)
385 {
386         int i, frags = skb_shinfo(skb)->nr_frags;
387         int pages = 0;
388
389         for (i = 0; i < frags; i++) {
390                 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
391                 unsigned long size = skb_frag_size(frag);
392                 unsigned long offset = frag->page_offset;
393
394                 /* Skip unused frames from start of page */
395                 offset &= ~PAGE_MASK;
396                 pages += PFN_UP(offset + size);
397         }
398         return pages;
399 }
400
401 static int netvsc_get_slots(struct sk_buff *skb)
402 {
403         char *data = skb->data;
404         unsigned int offset = offset_in_page(data);
405         unsigned int len = skb_headlen(skb);
406         int slots;
407         int frag_slots;
408
409         slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
410         frag_slots = count_skb_frag_slots(skb);
411         return slots + frag_slots;
412 }
413
414 static u32 net_checksum_info(struct sk_buff *skb)
415 {
416         if (skb->protocol == htons(ETH_P_IP)) {
417                 struct iphdr *ip = ip_hdr(skb);
418
419                 if (ip->protocol == IPPROTO_TCP)
420                         return TRANSPORT_INFO_IPV4_TCP;
421                 else if (ip->protocol == IPPROTO_UDP)
422                         return TRANSPORT_INFO_IPV4_UDP;
423         } else {
424                 struct ipv6hdr *ip6 = ipv6_hdr(skb);
425
426                 if (ip6->nexthdr == IPPROTO_TCP)
427                         return TRANSPORT_INFO_IPV6_TCP;
428                 else if (ip6->nexthdr == IPPROTO_UDP)
429                         return TRANSPORT_INFO_IPV6_UDP;
430         }
431
432         return TRANSPORT_INFO_NOT_IP;
433 }
434
435 /* Send skb on the slave VF device. */
436 static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
437                           struct sk_buff *skb)
438 {
439         struct net_device_context *ndev_ctx = netdev_priv(net);
440         unsigned int len = skb->len;
441         int rc;
442
443         skb->dev = vf_netdev;
444         skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
445
446         rc = dev_queue_xmit(skb);
447         if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
448                 struct netvsc_vf_pcpu_stats *pcpu_stats
449                         = this_cpu_ptr(ndev_ctx->vf_stats);
450
451                 u64_stats_update_begin(&pcpu_stats->syncp);
452                 pcpu_stats->tx_packets++;
453                 pcpu_stats->tx_bytes += len;
454                 u64_stats_update_end(&pcpu_stats->syncp);
455         } else {
456                 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
457         }
458
459         return rc;
460 }
461
462 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
463 {
464         struct net_device_context *net_device_ctx = netdev_priv(net);
465         struct hv_netvsc_packet *packet = NULL;
466         int ret;
467         unsigned int num_data_pgs;
468         struct rndis_message *rndis_msg;
469         struct net_device *vf_netdev;
470         u32 rndis_msg_size;
471         u32 hash;
472         struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
473
474         /* if VF is present and up then redirect packets
475          * already called with rcu_read_lock_bh
476          */
477         vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
478         if (vf_netdev && netif_running(vf_netdev) &&
479             !netpoll_tx_running(net))
480                 return netvsc_vf_xmit(net, vf_netdev, skb);
481
482         /* We will atmost need two pages to describe the rndis
483          * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
484          * of pages in a single packet. If skb is scattered around
485          * more pages we try linearizing it.
486          */
487
488         num_data_pgs = netvsc_get_slots(skb) + 2;
489
490         if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
491                 ++net_device_ctx->eth_stats.tx_scattered;
492
493                 if (skb_linearize(skb))
494                         goto no_memory;
495
496                 num_data_pgs = netvsc_get_slots(skb) + 2;
497                 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
498                         ++net_device_ctx->eth_stats.tx_too_big;
499                         goto drop;
500                 }
501         }
502
503         /*
504          * Place the rndis header in the skb head room and
505          * the skb->cb will be used for hv_netvsc_packet
506          * structure.
507          */
508         ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
509         if (ret)
510                 goto no_memory;
511
512         /* Use the skb control buffer for building up the packet */
513         BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
514                         FIELD_SIZEOF(struct sk_buff, cb));
515         packet = (struct hv_netvsc_packet *)skb->cb;
516
517         packet->q_idx = skb_get_queue_mapping(skb);
518
519         packet->total_data_buflen = skb->len;
520         packet->total_bytes = skb->len;
521         packet->total_packets = 1;
522
523         rndis_msg = (struct rndis_message *)skb->head;
524
525         /* Add the rndis header */
526         rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
527         rndis_msg->msg_len = packet->total_data_buflen;
528
529         rndis_msg->msg.pkt = (struct rndis_packet) {
530                 .data_offset = sizeof(struct rndis_packet),
531                 .data_len = packet->total_data_buflen,
532                 .per_pkt_info_offset = sizeof(struct rndis_packet),
533         };
534
535         rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
536
537         hash = skb_get_hash_raw(skb);
538         if (hash != 0 && net->real_num_tx_queues > 1) {
539                 u32 *hash_info;
540
541                 rndis_msg_size += NDIS_HASH_PPI_SIZE;
542                 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
543                                           NBL_HASH_VALUE);
544                 *hash_info = hash;
545         }
546
547         if (skb_vlan_tag_present(skb)) {
548                 struct ndis_pkt_8021q_info *vlan;
549
550                 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
551                 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
552                                      IEEE_8021Q_INFO);
553
554                 vlan->value = 0;
555                 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
556                 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
557                                 VLAN_PRIO_SHIFT;
558         }
559
560         if (skb_is_gso(skb)) {
561                 struct ndis_tcp_lso_info *lso_info;
562
563                 rndis_msg_size += NDIS_LSO_PPI_SIZE;
564                 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
565                                          TCP_LARGESEND_PKTINFO);
566
567                 lso_info->value = 0;
568                 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
569                 if (skb->protocol == htons(ETH_P_IP)) {
570                         lso_info->lso_v2_transmit.ip_version =
571                                 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
572                         ip_hdr(skb)->tot_len = 0;
573                         ip_hdr(skb)->check = 0;
574                         tcp_hdr(skb)->check =
575                                 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
576                                                    ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
577                 } else {
578                         lso_info->lso_v2_transmit.ip_version =
579                                 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
580                         ipv6_hdr(skb)->payload_len = 0;
581                         tcp_hdr(skb)->check =
582                                 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
583                                                  &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
584                 }
585                 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
586                 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
587         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
588                 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
589                         struct ndis_tcp_ip_checksum_info *csum_info;
590
591                         rndis_msg_size += NDIS_CSUM_PPI_SIZE;
592                         csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
593                                                   TCPIP_CHKSUM_PKTINFO);
594
595                         csum_info->value = 0;
596                         csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
597
598                         if (skb->protocol == htons(ETH_P_IP)) {
599                                 csum_info->transmit.is_ipv4 = 1;
600
601                                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
602                                         csum_info->transmit.tcp_checksum = 1;
603                                 else
604                                         csum_info->transmit.udp_checksum = 1;
605                         } else {
606                                 csum_info->transmit.is_ipv6 = 1;
607
608                                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
609                                         csum_info->transmit.tcp_checksum = 1;
610                                 else
611                                         csum_info->transmit.udp_checksum = 1;
612                         }
613                 } else {
614                         /* Can't do offload of this type of checksum */
615                         if (skb_checksum_help(skb))
616                                 goto drop;
617                 }
618         }
619
620         /* Start filling in the page buffers with the rndis hdr */
621         rndis_msg->msg_len += rndis_msg_size;
622         packet->total_data_buflen = rndis_msg->msg_len;
623         packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
624                                                skb, packet, pb);
625
626         /* timestamp packet in software */
627         skb_tx_timestamp(skb);
628
629         ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
630         if (likely(ret == 0))
631                 return NETDEV_TX_OK;
632
633         if (ret == -EAGAIN) {
634                 ++net_device_ctx->eth_stats.tx_busy;
635                 return NETDEV_TX_BUSY;
636         }
637
638         if (ret == -ENOSPC)
639                 ++net_device_ctx->eth_stats.tx_no_space;
640
641 drop:
642         dev_kfree_skb_any(skb);
643         net->stats.tx_dropped++;
644
645         return NETDEV_TX_OK;
646
647 no_memory:
648         ++net_device_ctx->eth_stats.tx_no_memory;
649         goto drop;
650 }
651
652 /*
653  * netvsc_linkstatus_callback - Link up/down notification
654  */
655 void netvsc_linkstatus_callback(struct hv_device *device_obj,
656                                 struct rndis_message *resp)
657 {
658         struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
659         struct net_device *net;
660         struct net_device_context *ndev_ctx;
661         struct netvsc_reconfig *event;
662         unsigned long flags;
663
664         net = hv_get_drvdata(device_obj);
665
666         if (!net)
667                 return;
668
669         ndev_ctx = netdev_priv(net);
670
671         /* Update the physical link speed when changing to another vSwitch */
672         if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
673                 u32 speed;
674
675                 speed = *(u32 *)((void *)indicate
676                                  + indicate->status_buf_offset) / 10000;
677                 ndev_ctx->speed = speed;
678                 return;
679         }
680
681         /* Handle these link change statuses below */
682         if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
683             indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
684             indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
685                 return;
686
687         if (net->reg_state != NETREG_REGISTERED)
688                 return;
689
690         event = kzalloc(sizeof(*event), GFP_ATOMIC);
691         if (!event)
692                 return;
693         event->event = indicate->status;
694
695         spin_lock_irqsave(&ndev_ctx->lock, flags);
696         list_add_tail(&event->list, &ndev_ctx->reconfig_events);
697         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
698
699         schedule_delayed_work(&ndev_ctx->dwork, 0);
700 }
701
702 static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
703                                              struct napi_struct *napi,
704                                              const struct ndis_tcp_ip_checksum_info *csum_info,
705                                              const struct ndis_pkt_8021q_info *vlan,
706                                              void *data, u32 buflen)
707 {
708         struct sk_buff *skb;
709
710         skb = napi_alloc_skb(napi, buflen);
711         if (!skb)
712                 return skb;
713
714         /*
715          * Copy to skb. This copy is needed here since the memory pointed by
716          * hv_netvsc_packet cannot be deallocated
717          */
718         skb_put_data(skb, data, buflen);
719
720         skb->protocol = eth_type_trans(skb, net);
721
722         /* skb is already created with CHECKSUM_NONE */
723         skb_checksum_none_assert(skb);
724
725         /*
726          * In Linux, the IP checksum is always checked.
727          * Do L4 checksum offload if enabled and present.
728          */
729         if (csum_info && (net->features & NETIF_F_RXCSUM)) {
730                 if (csum_info->receive.tcp_checksum_succeeded ||
731                     csum_info->receive.udp_checksum_succeeded)
732                         skb->ip_summed = CHECKSUM_UNNECESSARY;
733         }
734
735         if (vlan) {
736                 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
737
738                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
739                                        vlan_tci);
740         }
741
742         return skb;
743 }
744
745 /*
746  * netvsc_recv_callback -  Callback when we receive a packet from the
747  * "wire" on the specified device.
748  */
749 int netvsc_recv_callback(struct net_device *net,
750                          struct vmbus_channel *channel,
751                          void  *data, u32 len,
752                          const struct ndis_tcp_ip_checksum_info *csum_info,
753                          const struct ndis_pkt_8021q_info *vlan)
754 {
755         struct net_device_context *net_device_ctx = netdev_priv(net);
756         struct netvsc_device *net_device;
757         u16 q_idx = channel->offermsg.offer.sub_channel_index;
758         struct netvsc_channel *nvchan;
759         struct sk_buff *skb;
760         struct netvsc_stats *rx_stats;
761
762         if (net->reg_state != NETREG_REGISTERED)
763                 return NVSP_STAT_FAIL;
764
765         rcu_read_lock();
766         net_device = rcu_dereference(net_device_ctx->nvdev);
767         if (unlikely(!net_device))
768                 goto drop;
769
770         nvchan = &net_device->chan_table[q_idx];
771
772         /* Allocate a skb - TODO direct I/O to pages? */
773         skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
774                                     csum_info, vlan, data, len);
775         if (unlikely(!skb)) {
776 drop:
777                 ++net->stats.rx_dropped;
778                 rcu_read_unlock();
779                 return NVSP_STAT_FAIL;
780         }
781
782         skb_record_rx_queue(skb, q_idx);
783
784         /*
785          * Even if injecting the packet, record the statistics
786          * on the synthetic device because modifying the VF device
787          * statistics will not work correctly.
788          */
789         rx_stats = &nvchan->rx_stats;
790         u64_stats_update_begin(&rx_stats->syncp);
791         rx_stats->packets++;
792         rx_stats->bytes += len;
793
794         if (skb->pkt_type == PACKET_BROADCAST)
795                 ++rx_stats->broadcast;
796         else if (skb->pkt_type == PACKET_MULTICAST)
797                 ++rx_stats->multicast;
798         u64_stats_update_end(&rx_stats->syncp);
799
800         napi_gro_receive(&nvchan->napi, skb);
801         rcu_read_unlock();
802
803         return 0;
804 }
805
806 static void netvsc_get_drvinfo(struct net_device *net,
807                                struct ethtool_drvinfo *info)
808 {
809         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
810         strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
811 }
812
813 static void netvsc_get_channels(struct net_device *net,
814                                 struct ethtool_channels *channel)
815 {
816         struct net_device_context *net_device_ctx = netdev_priv(net);
817         struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
818
819         if (nvdev) {
820                 channel->max_combined   = nvdev->max_chn;
821                 channel->combined_count = nvdev->num_chn;
822         }
823 }
824
825 static int netvsc_set_channels(struct net_device *net,
826                                struct ethtool_channels *channels)
827 {
828         struct net_device_context *net_device_ctx = netdev_priv(net);
829         struct hv_device *dev = net_device_ctx->device_ctx;
830         struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
831         unsigned int orig, count = channels->combined_count;
832         struct netvsc_device_info device_info;
833         bool was_opened;
834         int ret = 0;
835
836         /* We do not support separate count for rx, tx, or other */
837         if (count == 0 ||
838             channels->rx_count || channels->tx_count || channels->other_count)
839                 return -EINVAL;
840
841         if (!nvdev || nvdev->destroy)
842                 return -ENODEV;
843
844         if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
845                 return -EINVAL;
846
847         if (count > nvdev->max_chn)
848                 return -EINVAL;
849
850         orig = nvdev->num_chn;
851         was_opened = rndis_filter_opened(nvdev);
852         if (was_opened)
853                 rndis_filter_close(nvdev);
854
855         memset(&device_info, 0, sizeof(device_info));
856         device_info.num_chn = count;
857         device_info.send_sections = nvdev->send_section_cnt;
858         device_info.send_section_size = nvdev->send_section_size;
859         device_info.recv_sections = nvdev->recv_section_cnt;
860         device_info.recv_section_size = nvdev->recv_section_size;
861
862         rndis_filter_device_remove(dev, nvdev);
863
864         nvdev = rndis_filter_device_add(dev, &device_info);
865         if (IS_ERR(nvdev)) {
866                 ret = PTR_ERR(nvdev);
867                 device_info.num_chn = orig;
868                 nvdev = rndis_filter_device_add(dev, &device_info);
869
870                 if (IS_ERR(nvdev)) {
871                         netdev_err(net, "restoring channel setting failed: %ld\n",
872                                    PTR_ERR(nvdev));
873                         return ret;
874                 }
875         }
876
877         if (was_opened)
878                 rndis_filter_open(nvdev);
879
880         /* We may have missed link change notifications */
881         net_device_ctx->last_reconfig = 0;
882         schedule_delayed_work(&net_device_ctx->dwork, 0);
883
884         return ret;
885 }
886
887 static bool
888 netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
889 {
890         struct ethtool_link_ksettings diff1 = *cmd;
891         struct ethtool_link_ksettings diff2 = {};
892
893         diff1.base.speed = 0;
894         diff1.base.duplex = 0;
895         /* advertising and cmd are usually set */
896         ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
897         diff1.base.cmd = 0;
898         /* We set port to PORT_OTHER */
899         diff2.base.port = PORT_OTHER;
900
901         return !memcmp(&diff1, &diff2, sizeof(diff1));
902 }
903
904 static void netvsc_init_settings(struct net_device *dev)
905 {
906         struct net_device_context *ndc = netdev_priv(dev);
907
908         ndc->l4_hash = HV_DEFAULT_L4HASH;
909
910         ndc->speed = SPEED_UNKNOWN;
911         ndc->duplex = DUPLEX_FULL;
912 }
913
914 static int netvsc_get_link_ksettings(struct net_device *dev,
915                                      struct ethtool_link_ksettings *cmd)
916 {
917         struct net_device_context *ndc = netdev_priv(dev);
918
919         cmd->base.speed = ndc->speed;
920         cmd->base.duplex = ndc->duplex;
921         cmd->base.port = PORT_OTHER;
922
923         return 0;
924 }
925
926 static int netvsc_set_link_ksettings(struct net_device *dev,
927                                      const struct ethtool_link_ksettings *cmd)
928 {
929         struct net_device_context *ndc = netdev_priv(dev);
930         u32 speed;
931
932         speed = cmd->base.speed;
933         if (!ethtool_validate_speed(speed) ||
934             !ethtool_validate_duplex(cmd->base.duplex) ||
935             !netvsc_validate_ethtool_ss_cmd(cmd))
936                 return -EINVAL;
937
938         ndc->speed = speed;
939         ndc->duplex = cmd->base.duplex;
940
941         return 0;
942 }
943
944 static int netvsc_change_mtu(struct net_device *ndev, int mtu)
945 {
946         struct net_device_context *ndevctx = netdev_priv(ndev);
947         struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
948         struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
949         struct hv_device *hdev = ndevctx->device_ctx;
950         int orig_mtu = ndev->mtu;
951         struct netvsc_device_info device_info;
952         bool was_opened;
953         int ret = 0;
954
955         if (!nvdev || nvdev->destroy)
956                 return -ENODEV;
957
958         /* Change MTU of underlying VF netdev first. */
959         if (vf_netdev) {
960                 ret = dev_set_mtu(vf_netdev, mtu);
961                 if (ret)
962                         return ret;
963         }
964
965         netif_device_detach(ndev);
966         was_opened = rndis_filter_opened(nvdev);
967         if (was_opened)
968                 rndis_filter_close(nvdev);
969
970         memset(&device_info, 0, sizeof(device_info));
971         device_info.num_chn = nvdev->num_chn;
972         device_info.send_sections = nvdev->send_section_cnt;
973         device_info.send_section_size = nvdev->send_section_size;
974         device_info.recv_sections = nvdev->recv_section_cnt;
975         device_info.recv_section_size = nvdev->recv_section_size;
976
977         rndis_filter_device_remove(hdev, nvdev);
978
979         ndev->mtu = mtu;
980
981         nvdev = rndis_filter_device_add(hdev, &device_info);
982         if (IS_ERR(nvdev)) {
983                 ret = PTR_ERR(nvdev);
984
985                 /* Attempt rollback to original MTU */
986                 ndev->mtu = orig_mtu;
987                 nvdev = rndis_filter_device_add(hdev, &device_info);
988
989                 if (vf_netdev)
990                         dev_set_mtu(vf_netdev, orig_mtu);
991
992                 if (IS_ERR(nvdev)) {
993                         netdev_err(ndev, "restoring mtu failed: %ld\n",
994                                    PTR_ERR(nvdev));
995                         return ret;
996                 }
997         }
998
999         if (was_opened)
1000                 rndis_filter_open(nvdev);
1001
1002         netif_device_attach(ndev);
1003
1004         /* We may have missed link change notifications */
1005         schedule_delayed_work(&ndevctx->dwork, 0);
1006
1007         return ret;
1008 }
1009
1010 static void netvsc_get_vf_stats(struct net_device *net,
1011                                 struct netvsc_vf_pcpu_stats *tot)
1012 {
1013         struct net_device_context *ndev_ctx = netdev_priv(net);
1014         int i;
1015
1016         memset(tot, 0, sizeof(*tot));
1017
1018         for_each_possible_cpu(i) {
1019                 const struct netvsc_vf_pcpu_stats *stats
1020                         = per_cpu_ptr(ndev_ctx->vf_stats, i);
1021                 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1022                 unsigned int start;
1023
1024                 do {
1025                         start = u64_stats_fetch_begin_irq(&stats->syncp);
1026                         rx_packets = stats->rx_packets;
1027                         tx_packets = stats->tx_packets;
1028                         rx_bytes = stats->rx_bytes;
1029                         tx_bytes = stats->tx_bytes;
1030                 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1031
1032                 tot->rx_packets += rx_packets;
1033                 tot->tx_packets += tx_packets;
1034                 tot->rx_bytes   += rx_bytes;
1035                 tot->tx_bytes   += tx_bytes;
1036                 tot->tx_dropped += stats->tx_dropped;
1037         }
1038 }
1039
1040 static void netvsc_get_stats64(struct net_device *net,
1041                                struct rtnl_link_stats64 *t)
1042 {
1043         struct net_device_context *ndev_ctx = netdev_priv(net);
1044         struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
1045         struct netvsc_vf_pcpu_stats vf_tot;
1046         int i;
1047
1048         if (!nvdev)
1049                 return;
1050
1051         netdev_stats_to_stats64(t, &net->stats);
1052
1053         netvsc_get_vf_stats(net, &vf_tot);
1054         t->rx_packets += vf_tot.rx_packets;
1055         t->tx_packets += vf_tot.tx_packets;
1056         t->rx_bytes   += vf_tot.rx_bytes;
1057         t->tx_bytes   += vf_tot.tx_bytes;
1058         t->tx_dropped += vf_tot.tx_dropped;
1059
1060         for (i = 0; i < nvdev->num_chn; i++) {
1061                 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1062                 const struct netvsc_stats *stats;
1063                 u64 packets, bytes, multicast;
1064                 unsigned int start;
1065
1066                 stats = &nvchan->tx_stats;
1067                 do {
1068                         start = u64_stats_fetch_begin_irq(&stats->syncp);
1069                         packets = stats->packets;
1070                         bytes = stats->bytes;
1071                 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1072
1073                 t->tx_bytes     += bytes;
1074                 t->tx_packets   += packets;
1075
1076                 stats = &nvchan->rx_stats;
1077                 do {
1078                         start = u64_stats_fetch_begin_irq(&stats->syncp);
1079                         packets = stats->packets;
1080                         bytes = stats->bytes;
1081                         multicast = stats->multicast + stats->broadcast;
1082                 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1083
1084                 t->rx_bytes     += bytes;
1085                 t->rx_packets   += packets;
1086                 t->multicast    += multicast;
1087         }
1088 }
1089
1090 static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1091 {
1092         struct net_device_context *ndc = netdev_priv(ndev);
1093         struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
1094         struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1095         struct sockaddr *addr = p;
1096         int err;
1097
1098         err = eth_prepare_mac_addr_change(ndev, p);
1099         if (err)
1100                 return err;
1101
1102         if (!nvdev)
1103                 return -ENODEV;
1104
1105         if (vf_netdev) {
1106                 err = dev_set_mac_address(vf_netdev, addr);
1107                 if (err)
1108                         return err;
1109         }
1110
1111         err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
1112         if (!err) {
1113                 eth_commit_mac_addr_change(ndev, p);
1114         } else if (vf_netdev) {
1115                 /* rollback change on VF */
1116                 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1117                 dev_set_mac_address(vf_netdev, addr);
1118         }
1119
1120         return err;
1121 }
1122
1123 static const struct {
1124         char name[ETH_GSTRING_LEN];
1125         u16 offset;
1126 } netvsc_stats[] = {
1127         { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1128         { "tx_no_memory",  offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1129         { "tx_no_space",  offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1130         { "tx_too_big",   offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1131         { "tx_busy",      offsetof(struct netvsc_ethtool_stats, tx_busy) },
1132         { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1133         { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
1134         { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1135         { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
1136 }, vf_stats[] = {
1137         { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1138         { "vf_rx_bytes",   offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1139         { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1140         { "vf_tx_bytes",   offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1141         { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
1142 };
1143
1144 #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
1145 #define NETVSC_VF_STATS_LEN     ARRAY_SIZE(vf_stats)
1146
1147 /* 4 statistics per queue (rx/tx packets/bytes) */
1148 #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1149
1150 static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1151 {
1152         struct net_device_context *ndc = netdev_priv(dev);
1153         struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1154
1155         if (!nvdev)
1156                 return -ENODEV;
1157
1158         switch (string_set) {
1159         case ETH_SS_STATS:
1160                 return NETVSC_GLOBAL_STATS_LEN
1161                         + NETVSC_VF_STATS_LEN
1162                         + NETVSC_QUEUE_STATS_LEN(nvdev);
1163         default:
1164                 return -EINVAL;
1165         }
1166 }
1167
1168 static void netvsc_get_ethtool_stats(struct net_device *dev,
1169                                      struct ethtool_stats *stats, u64 *data)
1170 {
1171         struct net_device_context *ndc = netdev_priv(dev);
1172         struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1173         const void *nds = &ndc->eth_stats;
1174         const struct netvsc_stats *qstats;
1175         struct netvsc_vf_pcpu_stats sum;
1176         unsigned int start;
1177         u64 packets, bytes;
1178         int i, j;
1179
1180         if (!nvdev)
1181                 return;
1182
1183         for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
1184                 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1185
1186         netvsc_get_vf_stats(dev, &sum);
1187         for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1188                 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1189
1190         for (j = 0; j < nvdev->num_chn; j++) {
1191                 qstats = &nvdev->chan_table[j].tx_stats;
1192
1193                 do {
1194                         start = u64_stats_fetch_begin_irq(&qstats->syncp);
1195                         packets = qstats->packets;
1196                         bytes = qstats->bytes;
1197                 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1198                 data[i++] = packets;
1199                 data[i++] = bytes;
1200
1201                 qstats = &nvdev->chan_table[j].rx_stats;
1202                 do {
1203                         start = u64_stats_fetch_begin_irq(&qstats->syncp);
1204                         packets = qstats->packets;
1205                         bytes = qstats->bytes;
1206                 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1207                 data[i++] = packets;
1208                 data[i++] = bytes;
1209         }
1210 }
1211
1212 static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1213 {
1214         struct net_device_context *ndc = netdev_priv(dev);
1215         struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1216         u8 *p = data;
1217         int i;
1218
1219         if (!nvdev)
1220                 return;
1221
1222         switch (stringset) {
1223         case ETH_SS_STATS:
1224                 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1225                         memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1226                         p += ETH_GSTRING_LEN;
1227                 }
1228
1229                 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1230                         memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1231                         p += ETH_GSTRING_LEN;
1232                 }
1233
1234                 for (i = 0; i < nvdev->num_chn; i++) {
1235                         sprintf(p, "tx_queue_%u_packets", i);
1236                         p += ETH_GSTRING_LEN;
1237                         sprintf(p, "tx_queue_%u_bytes", i);
1238                         p += ETH_GSTRING_LEN;
1239                         sprintf(p, "rx_queue_%u_packets", i);
1240                         p += ETH_GSTRING_LEN;
1241                         sprintf(p, "rx_queue_%u_bytes", i);
1242                         p += ETH_GSTRING_LEN;
1243                 }
1244
1245                 break;
1246         }
1247 }
1248
1249 static int
1250 netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1251                          struct ethtool_rxnfc *info)
1252 {
1253         const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1254
1255         info->data = RXH_IP_SRC | RXH_IP_DST;
1256
1257         switch (info->flow_type) {
1258         case TCP_V4_FLOW:
1259                 if (ndc->l4_hash & HV_TCP4_L4HASH)
1260                         info->data |= l4_flag;
1261
1262                 break;
1263
1264         case TCP_V6_FLOW:
1265                 if (ndc->l4_hash & HV_TCP6_L4HASH)
1266                         info->data |= l4_flag;
1267
1268                 break;
1269
1270         case UDP_V4_FLOW:
1271                 if (ndc->l4_hash & HV_UDP4_L4HASH)
1272                         info->data |= l4_flag;
1273
1274                 break;
1275
1276         case UDP_V6_FLOW:
1277                 if (ndc->l4_hash & HV_UDP6_L4HASH)
1278                         info->data |= l4_flag;
1279
1280                 break;
1281
1282         case IPV4_FLOW:
1283         case IPV6_FLOW:
1284                 break;
1285         default:
1286                 info->data = 0;
1287                 break;
1288         }
1289
1290         return 0;
1291 }
1292
1293 static int
1294 netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1295                  u32 *rules)
1296 {
1297         struct net_device_context *ndc = netdev_priv(dev);
1298         struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1299
1300         if (!nvdev)
1301                 return -ENODEV;
1302
1303         switch (info->cmd) {
1304         case ETHTOOL_GRXRINGS:
1305                 info->data = nvdev->num_chn;
1306                 return 0;
1307
1308         case ETHTOOL_GRXFH:
1309                 return netvsc_get_rss_hash_opts(ndc, info);
1310         }
1311         return -EOPNOTSUPP;
1312 }
1313
1314 static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1315                                     struct ethtool_rxnfc *info)
1316 {
1317         if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1318                            RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1319                 switch (info->flow_type) {
1320                 case TCP_V4_FLOW:
1321                         ndc->l4_hash |= HV_TCP4_L4HASH;
1322                         break;
1323
1324                 case TCP_V6_FLOW:
1325                         ndc->l4_hash |= HV_TCP6_L4HASH;
1326                         break;
1327
1328                 case UDP_V4_FLOW:
1329                         ndc->l4_hash |= HV_UDP4_L4HASH;
1330                         break;
1331
1332                 case UDP_V6_FLOW:
1333                         ndc->l4_hash |= HV_UDP6_L4HASH;
1334                         break;
1335
1336                 default:
1337                         return -EOPNOTSUPP;
1338                 }
1339
1340                 return 0;
1341         }
1342
1343         if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1344                 switch (info->flow_type) {
1345                 case TCP_V4_FLOW:
1346                         ndc->l4_hash &= ~HV_TCP4_L4HASH;
1347                         break;
1348
1349                 case TCP_V6_FLOW:
1350                         ndc->l4_hash &= ~HV_TCP6_L4HASH;
1351                         break;
1352
1353                 case UDP_V4_FLOW:
1354                         ndc->l4_hash &= ~HV_UDP4_L4HASH;
1355                         break;
1356
1357                 case UDP_V6_FLOW:
1358                         ndc->l4_hash &= ~HV_UDP6_L4HASH;
1359                         break;
1360
1361                 default:
1362                         return -EOPNOTSUPP;
1363                 }
1364
1365                 return 0;
1366         }
1367
1368         return -EOPNOTSUPP;
1369 }
1370
1371 static int
1372 netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1373 {
1374         struct net_device_context *ndc = netdev_priv(ndev);
1375
1376         if (info->cmd == ETHTOOL_SRXFH)
1377                 return netvsc_set_rss_hash_opts(ndc, info);
1378
1379         return -EOPNOTSUPP;
1380 }
1381
1382 #ifdef CONFIG_NET_POLL_CONTROLLER
1383 static void netvsc_poll_controller(struct net_device *dev)
1384 {
1385         struct net_device_context *ndc = netdev_priv(dev);
1386         struct netvsc_device *ndev;
1387         int i;
1388
1389         rcu_read_lock();
1390         ndev = rcu_dereference(ndc->nvdev);
1391         if (ndev) {
1392                 for (i = 0; i < ndev->num_chn; i++) {
1393                         struct netvsc_channel *nvchan = &ndev->chan_table[i];
1394
1395                         napi_schedule(&nvchan->napi);
1396                 }
1397         }
1398         rcu_read_unlock();
1399 }
1400 #endif
1401
1402 static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1403 {
1404         return NETVSC_HASH_KEYLEN;
1405 }
1406
1407 static u32 netvsc_rss_indir_size(struct net_device *dev)
1408 {
1409         return ITAB_NUM;
1410 }
1411
1412 static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1413                            u8 *hfunc)
1414 {
1415         struct net_device_context *ndc = netdev_priv(dev);
1416         struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1417         struct rndis_device *rndis_dev;
1418         int i;
1419
1420         if (!ndev)
1421                 return -ENODEV;
1422
1423         if (hfunc)
1424                 *hfunc = ETH_RSS_HASH_TOP;      /* Toeplitz */
1425
1426         rndis_dev = ndev->extension;
1427         if (indir) {
1428                 for (i = 0; i < ITAB_NUM; i++)
1429                         indir[i] = rndis_dev->rx_table[i];
1430         }
1431
1432         if (key)
1433                 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1434
1435         return 0;
1436 }
1437
1438 static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1439                            const u8 *key, const u8 hfunc)
1440 {
1441         struct net_device_context *ndc = netdev_priv(dev);
1442         struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
1443         struct rndis_device *rndis_dev;
1444         int i;
1445
1446         if (!ndev)
1447                 return -ENODEV;
1448
1449         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1450                 return -EOPNOTSUPP;
1451
1452         rndis_dev = ndev->extension;
1453         if (indir) {
1454                 for (i = 0; i < ITAB_NUM; i++)
1455                         if (indir[i] >= ndev->num_chn)
1456                                 return -EINVAL;
1457
1458                 for (i = 0; i < ITAB_NUM; i++)
1459                         rndis_dev->rx_table[i] = indir[i];
1460         }
1461
1462         if (!key) {
1463                 if (!indir)
1464                         return 0;
1465
1466                 key = rndis_dev->rss_key;
1467         }
1468
1469         return rndis_filter_set_rss_param(rndis_dev, key);
1470 }
1471
1472 /* Hyper-V RNDIS protocol does not have ring in the HW sense.
1473  * It does have pre-allocated receive area which is divided into sections.
1474  */
1475 static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1476                                    struct ethtool_ringparam *ring)
1477 {
1478         u32 max_buf_size;
1479
1480         ring->rx_pending = nvdev->recv_section_cnt;
1481         ring->tx_pending = nvdev->send_section_cnt;
1482
1483         if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1484                 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1485         else
1486                 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1487
1488         ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1489         ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1490                 / nvdev->send_section_size;
1491 }
1492
1493 static void netvsc_get_ringparam(struct net_device *ndev,
1494                                  struct ethtool_ringparam *ring)
1495 {
1496         struct net_device_context *ndevctx = netdev_priv(ndev);
1497         struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1498
1499         if (!nvdev)
1500                 return;
1501
1502         __netvsc_get_ringparam(nvdev, ring);
1503 }
1504
1505 static int netvsc_set_ringparam(struct net_device *ndev,
1506                                 struct ethtool_ringparam *ring)
1507 {
1508         struct net_device_context *ndevctx = netdev_priv(ndev);
1509         struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1510         struct hv_device *hdev = ndevctx->device_ctx;
1511         struct netvsc_device_info device_info;
1512         struct ethtool_ringparam orig;
1513         u32 new_tx, new_rx;
1514         bool was_opened;
1515         int ret = 0;
1516
1517         if (!nvdev || nvdev->destroy)
1518                 return -ENODEV;
1519
1520         memset(&orig, 0, sizeof(orig));
1521         __netvsc_get_ringparam(nvdev, &orig);
1522
1523         new_tx = clamp_t(u32, ring->tx_pending,
1524                          NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1525         new_rx = clamp_t(u32, ring->rx_pending,
1526                          NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1527
1528         if (new_tx == orig.tx_pending &&
1529             new_rx == orig.rx_pending)
1530                 return 0;        /* no change */
1531
1532         memset(&device_info, 0, sizeof(device_info));
1533         device_info.num_chn = nvdev->num_chn;
1534         device_info.send_sections = new_tx;
1535         device_info.send_section_size = nvdev->send_section_size;
1536         device_info.recv_sections = new_rx;
1537         device_info.recv_section_size = nvdev->recv_section_size;
1538
1539         netif_device_detach(ndev);
1540         was_opened = rndis_filter_opened(nvdev);
1541         if (was_opened)
1542                 rndis_filter_close(nvdev);
1543
1544         rndis_filter_device_remove(hdev, nvdev);
1545
1546         nvdev = rndis_filter_device_add(hdev, &device_info);
1547         if (IS_ERR(nvdev)) {
1548                 ret = PTR_ERR(nvdev);
1549
1550                 device_info.send_sections = orig.tx_pending;
1551                 device_info.recv_sections = orig.rx_pending;
1552                 nvdev = rndis_filter_device_add(hdev, &device_info);
1553                 if (IS_ERR(nvdev)) {
1554                         netdev_err(ndev, "restoring ringparam failed: %ld\n",
1555                                    PTR_ERR(nvdev));
1556                         return ret;
1557                 }
1558         }
1559
1560         if (was_opened)
1561                 rndis_filter_open(nvdev);
1562         netif_device_attach(ndev);
1563
1564         /* We may have missed link change notifications */
1565         ndevctx->last_reconfig = 0;
1566         schedule_delayed_work(&ndevctx->dwork, 0);
1567
1568         return ret;
1569 }
1570
1571 static const struct ethtool_ops ethtool_ops = {
1572         .get_drvinfo    = netvsc_get_drvinfo,
1573         .get_link       = ethtool_op_get_link,
1574         .get_ethtool_stats = netvsc_get_ethtool_stats,
1575         .get_sset_count = netvsc_get_sset_count,
1576         .get_strings    = netvsc_get_strings,
1577         .get_channels   = netvsc_get_channels,
1578         .set_channels   = netvsc_set_channels,
1579         .get_ts_info    = ethtool_op_get_ts_info,
1580         .get_rxnfc      = netvsc_get_rxnfc,
1581         .set_rxnfc      = netvsc_set_rxnfc,
1582         .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1583         .get_rxfh_indir_size = netvsc_rss_indir_size,
1584         .get_rxfh       = netvsc_get_rxfh,
1585         .set_rxfh       = netvsc_set_rxfh,
1586         .get_link_ksettings = netvsc_get_link_ksettings,
1587         .set_link_ksettings = netvsc_set_link_ksettings,
1588         .get_ringparam  = netvsc_get_ringparam,
1589         .set_ringparam  = netvsc_set_ringparam,
1590 };
1591
1592 static const struct net_device_ops device_ops = {
1593         .ndo_open =                     netvsc_open,
1594         .ndo_stop =                     netvsc_close,
1595         .ndo_start_xmit =               netvsc_start_xmit,
1596         .ndo_set_rx_mode =              netvsc_set_multicast_list,
1597         .ndo_change_mtu =               netvsc_change_mtu,
1598         .ndo_validate_addr =            eth_validate_addr,
1599         .ndo_set_mac_address =          netvsc_set_mac_addr,
1600         .ndo_select_queue =             netvsc_select_queue,
1601         .ndo_get_stats64 =              netvsc_get_stats64,
1602 #ifdef CONFIG_NET_POLL_CONTROLLER
1603         .ndo_poll_controller =          netvsc_poll_controller,
1604 #endif
1605 };
1606
1607 /*
1608  * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1609  * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1610  * present send GARP packet to network peers with netif_notify_peers().
1611  */
1612 static void netvsc_link_change(struct work_struct *w)
1613 {
1614         struct net_device_context *ndev_ctx =
1615                 container_of(w, struct net_device_context, dwork.work);
1616         struct hv_device *device_obj = ndev_ctx->device_ctx;
1617         struct net_device *net = hv_get_drvdata(device_obj);
1618         struct netvsc_device *net_device;
1619         struct rndis_device *rdev;
1620         struct netvsc_reconfig *event = NULL;
1621         bool notify = false, reschedule = false;
1622         unsigned long flags, next_reconfig, delay;
1623
1624         /* if changes are happening, comeback later */
1625         if (!rtnl_trylock()) {
1626                 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1627                 return;
1628         }
1629
1630         net_device = rtnl_dereference(ndev_ctx->nvdev);
1631         if (!net_device)
1632                 goto out_unlock;
1633
1634         rdev = net_device->extension;
1635
1636         next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1637         if (time_is_after_jiffies(next_reconfig)) {
1638                 /* link_watch only sends one notification with current state
1639                  * per second, avoid doing reconfig more frequently. Handle
1640                  * wrap around.
1641                  */
1642                 delay = next_reconfig - jiffies;
1643                 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1644                 schedule_delayed_work(&ndev_ctx->dwork, delay);
1645                 goto out_unlock;
1646         }
1647         ndev_ctx->last_reconfig = jiffies;
1648
1649         spin_lock_irqsave(&ndev_ctx->lock, flags);
1650         if (!list_empty(&ndev_ctx->reconfig_events)) {
1651                 event = list_first_entry(&ndev_ctx->reconfig_events,
1652                                          struct netvsc_reconfig, list);
1653                 list_del(&event->list);
1654                 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1655         }
1656         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1657
1658         if (!event)
1659                 goto out_unlock;
1660
1661         switch (event->event) {
1662                 /* Only the following events are possible due to the check in
1663                  * netvsc_linkstatus_callback()
1664                  */
1665         case RNDIS_STATUS_MEDIA_CONNECT:
1666                 if (rdev->link_state) {
1667                         rdev->link_state = false;
1668                         netif_carrier_on(net);
1669                         netif_tx_wake_all_queues(net);
1670                 } else {
1671                         notify = true;
1672                 }
1673                 kfree(event);
1674                 break;
1675         case RNDIS_STATUS_MEDIA_DISCONNECT:
1676                 if (!rdev->link_state) {
1677                         rdev->link_state = true;
1678                         netif_carrier_off(net);
1679                         netif_tx_stop_all_queues(net);
1680                 }
1681                 kfree(event);
1682                 break;
1683         case RNDIS_STATUS_NETWORK_CHANGE:
1684                 /* Only makes sense if carrier is present */
1685                 if (!rdev->link_state) {
1686                         rdev->link_state = true;
1687                         netif_carrier_off(net);
1688                         netif_tx_stop_all_queues(net);
1689                         event->event = RNDIS_STATUS_MEDIA_CONNECT;
1690                         spin_lock_irqsave(&ndev_ctx->lock, flags);
1691                         list_add(&event->list, &ndev_ctx->reconfig_events);
1692                         spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1693                         reschedule = true;
1694                 }
1695                 break;
1696         }
1697
1698         rtnl_unlock();
1699
1700         if (notify)
1701                 netdev_notify_peers(net);
1702
1703         /* link_watch only sends one notification with current state per
1704          * second, handle next reconfig event in 2 seconds.
1705          */
1706         if (reschedule)
1707                 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1708
1709         return;
1710
1711 out_unlock:
1712         rtnl_unlock();
1713 }
1714
1715 static struct net_device *get_netvsc_bymac(const u8 *mac)
1716 {
1717         struct net_device *dev;
1718
1719         ASSERT_RTNL();
1720
1721         for_each_netdev(&init_net, dev) {
1722                 if (dev->netdev_ops != &device_ops)
1723                         continue;       /* not a netvsc device */
1724
1725                 if (ether_addr_equal(mac, dev->perm_addr))
1726                         return dev;
1727         }
1728
1729         return NULL;
1730 }
1731
1732 static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
1733 {
1734         struct net_device *dev;
1735
1736         ASSERT_RTNL();
1737
1738         for_each_netdev(&init_net, dev) {
1739                 struct net_device_context *net_device_ctx;
1740
1741                 if (dev->netdev_ops != &device_ops)
1742                         continue;       /* not a netvsc device */
1743
1744                 net_device_ctx = netdev_priv(dev);
1745                 if (!rtnl_dereference(net_device_ctx->nvdev))
1746                         continue;       /* device is removed */
1747
1748                 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
1749                         return dev;     /* a match */
1750         }
1751
1752         return NULL;
1753 }
1754
1755 /* Called when VF is injecting data into network stack.
1756  * Change the associated network device from VF to netvsc.
1757  * note: already called with rcu_read_lock
1758  */
1759 static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1760 {
1761         struct sk_buff *skb = *pskb;
1762         struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1763         struct net_device_context *ndev_ctx = netdev_priv(ndev);
1764         struct netvsc_vf_pcpu_stats *pcpu_stats
1765                  = this_cpu_ptr(ndev_ctx->vf_stats);
1766
1767         skb->dev = ndev;
1768
1769         u64_stats_update_begin(&pcpu_stats->syncp);
1770         pcpu_stats->rx_packets++;
1771         pcpu_stats->rx_bytes += skb->len;
1772         u64_stats_update_end(&pcpu_stats->syncp);
1773
1774         return RX_HANDLER_ANOTHER;
1775 }
1776
1777 static int netvsc_vf_join(struct net_device *vf_netdev,
1778                           struct net_device *ndev)
1779 {
1780         struct net_device_context *ndev_ctx = netdev_priv(ndev);
1781         int ret;
1782
1783         ret = netdev_rx_handler_register(vf_netdev,
1784                                          netvsc_vf_handle_frame, ndev);
1785         if (ret != 0) {
1786                 netdev_err(vf_netdev,
1787                            "can not register netvsc VF receive handler (err = %d)\n",
1788                            ret);
1789                 goto rx_handler_failed;
1790         }
1791
1792         ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
1793         if (ret != 0) {
1794                 netdev_err(vf_netdev,
1795                            "can not set master device %s (err = %d)\n",
1796                            ndev->name, ret);
1797                 goto upper_link_failed;
1798         }
1799
1800         /* set slave flag before open to prevent IPv6 addrconf */
1801         vf_netdev->flags |= IFF_SLAVE;
1802
1803         schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1804
1805         call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
1806
1807         netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1808         return 0;
1809
1810 upper_link_failed:
1811         netdev_rx_handler_unregister(vf_netdev);
1812 rx_handler_failed:
1813         return ret;
1814 }
1815
1816 static void __netvsc_vf_setup(struct net_device *ndev,
1817                               struct net_device *vf_netdev)
1818 {
1819         int ret;
1820
1821         /* Align MTU of VF with master */
1822         ret = dev_set_mtu(vf_netdev, ndev->mtu);
1823         if (ret)
1824                 netdev_warn(vf_netdev,
1825                             "unable to change mtu to %u\n", ndev->mtu);
1826
1827         if (netif_running(ndev)) {
1828                 ret = dev_open(vf_netdev);
1829                 if (ret)
1830                         netdev_warn(vf_netdev,
1831                                     "unable to open: %d\n", ret);
1832         }
1833 }
1834
1835 /* Setup VF as slave of the synthetic device.
1836  * Runs in workqueue to avoid recursion in netlink callbacks.
1837  */
1838 static void netvsc_vf_setup(struct work_struct *w)
1839 {
1840         struct net_device_context *ndev_ctx
1841                 = container_of(w, struct net_device_context, vf_takeover.work);
1842         struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1843         struct net_device *vf_netdev;
1844
1845         if (!rtnl_trylock()) {
1846                 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
1847                 return;
1848         }
1849
1850         vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1851         if (vf_netdev)
1852                 __netvsc_vf_setup(ndev, vf_netdev);
1853
1854         rtnl_unlock();
1855 }
1856
1857 static int netvsc_register_vf(struct net_device *vf_netdev)
1858 {
1859         struct net_device *ndev;
1860         struct net_device_context *net_device_ctx;
1861         struct netvsc_device *netvsc_dev;
1862
1863         if (vf_netdev->addr_len != ETH_ALEN)
1864                 return NOTIFY_DONE;
1865
1866         /*
1867          * We will use the MAC address to locate the synthetic interface to
1868          * associate with the VF interface. If we don't find a matching
1869          * synthetic interface, move on.
1870          */
1871         ndev = get_netvsc_bymac(vf_netdev->perm_addr);
1872         if (!ndev)
1873                 return NOTIFY_DONE;
1874
1875         net_device_ctx = netdev_priv(ndev);
1876         netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1877         if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
1878                 return NOTIFY_DONE;
1879
1880         if (netvsc_vf_join(vf_netdev, ndev) != 0)
1881                 return NOTIFY_DONE;
1882
1883         netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
1884
1885         dev_hold(vf_netdev);
1886         rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
1887         return NOTIFY_OK;
1888 }
1889
1890 /* VF up/down change detected, schedule to change data path */
1891 static int netvsc_vf_changed(struct net_device *vf_netdev)
1892 {
1893         struct net_device_context *net_device_ctx;
1894         struct netvsc_device *netvsc_dev;
1895         struct net_device *ndev;
1896         bool vf_is_up = netif_running(vf_netdev);
1897
1898         ndev = get_netvsc_byref(vf_netdev);
1899         if (!ndev)
1900                 return NOTIFY_DONE;
1901
1902         net_device_ctx = netdev_priv(ndev);
1903         netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1904         if (!netvsc_dev)
1905                 return NOTIFY_DONE;
1906
1907         netvsc_switch_datapath(ndev, vf_is_up);
1908         netdev_info(ndev, "Data path switched %s VF: %s\n",
1909                     vf_is_up ? "to" : "from", vf_netdev->name);
1910
1911         return NOTIFY_OK;
1912 }
1913
1914 static int netvsc_unregister_vf(struct net_device *vf_netdev)
1915 {
1916         struct net_device *ndev;
1917         struct net_device_context *net_device_ctx;
1918
1919         ndev = get_netvsc_byref(vf_netdev);
1920         if (!ndev)
1921                 return NOTIFY_DONE;
1922
1923         net_device_ctx = netdev_priv(ndev);
1924         cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
1925
1926         netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
1927
1928         netdev_rx_handler_unregister(vf_netdev);
1929         netdev_upper_dev_unlink(vf_netdev, ndev);
1930         RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
1931         dev_put(vf_netdev);
1932
1933         return NOTIFY_OK;
1934 }
1935
1936 static int netvsc_probe(struct hv_device *dev,
1937                         const struct hv_vmbus_device_id *dev_id)
1938 {
1939         struct net_device *net = NULL;
1940         struct net_device_context *net_device_ctx;
1941         struct netvsc_device_info device_info;
1942         struct netvsc_device *nvdev;
1943         int ret = -ENOMEM;
1944
1945         net = alloc_etherdev_mq(sizeof(struct net_device_context),
1946                                 VRSS_CHANNEL_MAX);
1947         if (!net)
1948                 goto no_net;
1949
1950         netif_carrier_off(net);
1951
1952         netvsc_init_settings(net);
1953
1954         net_device_ctx = netdev_priv(net);
1955         net_device_ctx->device_ctx = dev;
1956         net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1957         if (netif_msg_probe(net_device_ctx))
1958                 netdev_dbg(net, "netvsc msg_enable: %d\n",
1959                            net_device_ctx->msg_enable);
1960
1961         hv_set_drvdata(dev, net);
1962
1963         INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
1964
1965         spin_lock_init(&net_device_ctx->lock);
1966         INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1967         INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
1968
1969         net_device_ctx->vf_stats
1970                 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1971         if (!net_device_ctx->vf_stats)
1972                 goto no_stats;
1973
1974         net->netdev_ops = &device_ops;
1975         net->ethtool_ops = &ethtool_ops;
1976         SET_NETDEV_DEV(net, &dev->device);
1977
1978         /* We always need headroom for rndis header */
1979         net->needed_headroom = RNDIS_AND_PPI_SIZE;
1980
1981         /* Initialize the number of queues to be 1, we may change it if more
1982          * channels are offered later.
1983          */
1984         netif_set_real_num_tx_queues(net, 1);
1985         netif_set_real_num_rx_queues(net, 1);
1986
1987         /* Notify the netvsc driver of the new device */
1988         memset(&device_info, 0, sizeof(device_info));
1989         device_info.num_chn = VRSS_CHANNEL_DEFAULT;
1990         device_info.send_sections = NETVSC_DEFAULT_TX;
1991         device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
1992         device_info.recv_sections = NETVSC_DEFAULT_RX;
1993         device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
1994
1995         nvdev = rndis_filter_device_add(dev, &device_info);
1996         if (IS_ERR(nvdev)) {
1997                 ret = PTR_ERR(nvdev);
1998                 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
1999                 goto rndis_failed;
2000         }
2001
2002         memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
2003
2004         /* hw_features computed in rndis_netdev_set_hwcaps() */
2005         net->features = net->hw_features |
2006                 NETIF_F_HIGHDMA | NETIF_F_SG |
2007                 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2008         net->vlan_features = net->features;
2009
2010         netdev_lockdep_set_classes(net);
2011
2012         /* MTU range: 68 - 1500 or 65521 */
2013         net->min_mtu = NETVSC_MTU_MIN;
2014         if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2015                 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2016         else
2017                 net->max_mtu = ETH_DATA_LEN;
2018
2019         ret = register_netdev(net);
2020         if (ret != 0) {
2021                 pr_err("Unable to register netdev.\n");
2022                 goto register_failed;
2023         }
2024
2025         return ret;
2026
2027 register_failed:
2028         rndis_filter_device_remove(dev, nvdev);
2029 rndis_failed:
2030         free_percpu(net_device_ctx->vf_stats);
2031 no_stats:
2032         hv_set_drvdata(dev, NULL);
2033         free_netdev(net);
2034 no_net:
2035         return ret;
2036 }
2037
2038 static int netvsc_remove(struct hv_device *dev)
2039 {
2040         struct net_device_context *ndev_ctx;
2041         struct net_device *vf_netdev;
2042         struct net_device *net;
2043
2044         net = hv_get_drvdata(dev);
2045         if (net == NULL) {
2046                 dev_err(&dev->device, "No net device to remove\n");
2047                 return 0;
2048         }
2049
2050         ndev_ctx = netdev_priv(net);
2051
2052         netif_device_detach(net);
2053
2054         cancel_delayed_work_sync(&ndev_ctx->dwork);
2055
2056         /*
2057          * Call to the vsc driver to let it know that the device is being
2058          * removed. Also blocks mtu and channel changes.
2059          */
2060         rtnl_lock();
2061         vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2062         if (vf_netdev)
2063                 netvsc_unregister_vf(vf_netdev);
2064
2065         unregister_netdevice(net);
2066
2067         rndis_filter_device_remove(dev,
2068                                    rtnl_dereference(ndev_ctx->nvdev));
2069         rtnl_unlock();
2070
2071         hv_set_drvdata(dev, NULL);
2072
2073         free_percpu(ndev_ctx->vf_stats);
2074         free_netdev(net);
2075         return 0;
2076 }
2077
2078 static const struct hv_vmbus_device_id id_table[] = {
2079         /* Network guid */
2080         { HV_NIC_GUID, },
2081         { },
2082 };
2083
2084 MODULE_DEVICE_TABLE(vmbus, id_table);
2085
2086 /* The one and only one */
2087 static struct  hv_driver netvsc_drv = {
2088         .name = KBUILD_MODNAME,
2089         .id_table = id_table,
2090         .probe = netvsc_probe,
2091         .remove = netvsc_remove,
2092 };
2093
2094 /*
2095  * On Hyper-V, every VF interface is matched with a corresponding
2096  * synthetic interface. The synthetic interface is presented first
2097  * to the guest. When the corresponding VF instance is registered,
2098  * we will take care of switching the data path.
2099  */
2100 static int netvsc_netdev_event(struct notifier_block *this,
2101                                unsigned long event, void *ptr)
2102 {
2103         struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2104
2105         /* Skip our own events */
2106         if (event_dev->netdev_ops == &device_ops)
2107                 return NOTIFY_DONE;
2108
2109         /* Avoid non-Ethernet type devices */
2110         if (event_dev->type != ARPHRD_ETHER)
2111                 return NOTIFY_DONE;
2112
2113         /* Avoid Vlan dev with same MAC registering as VF */
2114         if (is_vlan_dev(event_dev))
2115                 return NOTIFY_DONE;
2116
2117         /* Avoid Bonding master dev with same MAC registering as VF */
2118         if ((event_dev->priv_flags & IFF_BONDING) &&
2119             (event_dev->flags & IFF_MASTER))
2120                 return NOTIFY_DONE;
2121
2122         switch (event) {
2123         case NETDEV_REGISTER:
2124                 return netvsc_register_vf(event_dev);
2125         case NETDEV_UNREGISTER:
2126                 return netvsc_unregister_vf(event_dev);
2127         case NETDEV_UP:
2128         case NETDEV_DOWN:
2129                 return netvsc_vf_changed(event_dev);
2130         default:
2131                 return NOTIFY_DONE;
2132         }
2133 }
2134
2135 static struct notifier_block netvsc_netdev_notifier = {
2136         .notifier_call = netvsc_netdev_event,
2137 };
2138
2139 static void __exit netvsc_drv_exit(void)
2140 {
2141         unregister_netdevice_notifier(&netvsc_netdev_notifier);
2142         vmbus_driver_unregister(&netvsc_drv);
2143 }
2144
2145 static int __init netvsc_drv_init(void)
2146 {
2147         int ret;
2148
2149         if (ring_size < RING_SIZE_MIN) {
2150                 ring_size = RING_SIZE_MIN;
2151                 pr_info("Increased ring_size to %u (min allowed)\n",
2152                         ring_size);
2153         }
2154         netvsc_ring_bytes = ring_size * PAGE_SIZE;
2155         netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
2156
2157         ret = vmbus_driver_register(&netvsc_drv);
2158         if (ret)
2159                 return ret;
2160
2161         register_netdevice_notifier(&netvsc_netdev_notifier);
2162         return 0;
2163 }
2164
2165 MODULE_LICENSE("GPL");
2166 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
2167
2168 module_init(netvsc_drv_init);
2169 module_exit(netvsc_drv_exit);