]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/virtio_net.c
virtio_net: fix PAGE_SIZE > 64k
[karo-tx-linux.git] / drivers / net / virtio_net.c
1 /* A network driver using virtio.
2  *
3  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 //#define DEBUG
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/bpf.h>
26 #include <linux/scatterlist.h>
27 #include <linux/if_vlan.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
30 #include <linux/average.h>
31 #include <net/busy_poll.h>
32
33 static int napi_weight = NAPI_POLL_WEIGHT;
34 module_param(napi_weight, int, 0444);
35
36 static bool csum = true, gso = true;
37 module_param(csum, bool, 0444);
38 module_param(gso, bool, 0444);
39
40 /* FIXME: MTU in config. */
41 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
42 #define GOOD_COPY_LEN   128
43
44 /* RX packet size EWMA. The average packet size is used to determine the packet
45  * buffer size when refilling RX rings. As the entire RX ring may be refilled
46  * at once, the weight is chosen so that the EWMA will be insensitive to short-
47  * term, transient changes in packet size.
48  */
49 DECLARE_EWMA(pkt_len, 1, 64)
50
51 /* With mergeable buffers we align buffer address and use the low bits to
52  * encode its true size. Buffer size is up to 1 page so we need to align to
53  * square root of page size to ensure we reserve enough bits to encode the true
54  * size.
55  */
56 #define MERGEABLE_BUFFER_MIN_ALIGN_SHIFT ((PAGE_SHIFT + 1) / 2)
57
58 /* Minimum alignment for mergeable packet buffers. */
59 #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, \
60                                    1 << MERGEABLE_BUFFER_MIN_ALIGN_SHIFT)
61
62 #define VIRTNET_DRIVER_VERSION "1.0.0"
63
64 struct virtnet_stats {
65         struct u64_stats_sync tx_syncp;
66         struct u64_stats_sync rx_syncp;
67         u64 tx_bytes;
68         u64 tx_packets;
69
70         u64 rx_bytes;
71         u64 rx_packets;
72 };
73
74 /* Internal representation of a send virtqueue */
75 struct send_queue {
76         /* Virtqueue associated with this send _queue */
77         struct virtqueue *vq;
78
79         /* TX: fragments + linear part + virtio header */
80         struct scatterlist sg[MAX_SKB_FRAGS + 2];
81
82         /* Name of the send queue: output.$index */
83         char name[40];
84 };
85
86 /* Internal representation of a receive virtqueue */
87 struct receive_queue {
88         /* Virtqueue associated with this receive_queue */
89         struct virtqueue *vq;
90
91         struct napi_struct napi;
92
93         struct bpf_prog __rcu *xdp_prog;
94
95         /* Chain pages by the private ptr. */
96         struct page *pages;
97
98         /* Average packet length for mergeable receive buffers. */
99         struct ewma_pkt_len mrg_avg_pkt_len;
100
101         /* Page frag for packet buffer allocation. */
102         struct page_frag alloc_frag;
103
104         /* RX: fragments + linear part + virtio header */
105         struct scatterlist sg[MAX_SKB_FRAGS + 2];
106
107         /* Name of this receive queue: input.$index */
108         char name[40];
109 };
110
111 struct virtnet_info {
112         struct virtio_device *vdev;
113         struct virtqueue *cvq;
114         struct net_device *dev;
115         struct send_queue *sq;
116         struct receive_queue *rq;
117         unsigned int status;
118
119         /* Max # of queue pairs supported by the device */
120         u16 max_queue_pairs;
121
122         /* # of queue pairs currently used by the driver */
123         u16 curr_queue_pairs;
124
125         /* # of XDP queue pairs currently used by the driver */
126         u16 xdp_queue_pairs;
127
128         /* I like... big packets and I cannot lie! */
129         bool big_packets;
130
131         /* Host will merge rx buffers for big packets (shake it! shake it!) */
132         bool mergeable_rx_bufs;
133
134         /* Has control virtqueue */
135         bool has_cvq;
136
137         /* Host can handle any s/g split between our header and packet data */
138         bool any_header_sg;
139
140         /* Packet virtio header size */
141         u8 hdr_len;
142
143         /* Active statistics */
144         struct virtnet_stats __percpu *stats;
145
146         /* Work struct for refilling if we run low on memory. */
147         struct delayed_work refill;
148
149         /* Work struct for config space updates */
150         struct work_struct config_work;
151
152         /* Does the affinity hint is set for virtqueues? */
153         bool affinity_hint_set;
154
155         /* CPU hotplug instances for online & dead */
156         struct hlist_node node;
157         struct hlist_node node_dead;
158
159         /* Control VQ buffers: protected by the rtnl lock */
160         struct virtio_net_ctrl_hdr ctrl_hdr;
161         virtio_net_ctrl_ack ctrl_status;
162         struct virtio_net_ctrl_mq ctrl_mq;
163         u8 ctrl_promisc;
164         u8 ctrl_allmulti;
165         u16 ctrl_vid;
166
167         /* Ethtool settings */
168         u8 duplex;
169         u32 speed;
170 };
171
172 struct padded_vnet_hdr {
173         struct virtio_net_hdr_mrg_rxbuf hdr;
174         /*
175          * hdr is in a separate sg buffer, and data sg buffer shares same page
176          * with this header sg. This padding makes next sg 16 byte aligned
177          * after the header.
178          */
179         char padding[4];
180 };
181
182 /* Converting between virtqueue no. and kernel tx/rx queue no.
183  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
184  */
185 static int vq2txq(struct virtqueue *vq)
186 {
187         return (vq->index - 1) / 2;
188 }
189
190 static int txq2vq(int txq)
191 {
192         return txq * 2 + 1;
193 }
194
195 static int vq2rxq(struct virtqueue *vq)
196 {
197         return vq->index / 2;
198 }
199
200 static int rxq2vq(int rxq)
201 {
202         return rxq * 2;
203 }
204
205 static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
206 {
207         return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
208 }
209
210 /*
211  * private is used to chain pages for big packets, put the whole
212  * most recent used list in the beginning for reuse
213  */
214 static void give_pages(struct receive_queue *rq, struct page *page)
215 {
216         struct page *end;
217
218         /* Find end of list, sew whole thing into vi->rq.pages. */
219         for (end = page; end->private; end = (struct page *)end->private);
220         end->private = (unsigned long)rq->pages;
221         rq->pages = page;
222 }
223
224 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
225 {
226         struct page *p = rq->pages;
227
228         if (p) {
229                 rq->pages = (struct page *)p->private;
230                 /* clear private here, it is used to chain pages */
231                 p->private = 0;
232         } else
233                 p = alloc_page(gfp_mask);
234         return p;
235 }
236
237 static void skb_xmit_done(struct virtqueue *vq)
238 {
239         struct virtnet_info *vi = vq->vdev->priv;
240
241         /* Suppress further interrupts. */
242         virtqueue_disable_cb(vq);
243
244         /* We were probably waiting for more output buffers. */
245         netif_wake_subqueue(vi->dev, vq2txq(vq));
246 }
247
248 static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
249 {
250         unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
251         return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
252 }
253
254 static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
255 {
256         return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
257
258 }
259
260 static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
261 {
262         unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
263         return (unsigned long)buf | (size - 1);
264 }
265
266 /* Called from bottom half context */
267 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
268                                    struct receive_queue *rq,
269                                    struct page *page, unsigned int offset,
270                                    unsigned int len, unsigned int truesize)
271 {
272         struct sk_buff *skb;
273         struct virtio_net_hdr_mrg_rxbuf *hdr;
274         unsigned int copy, hdr_len, hdr_padded_len;
275         char *p;
276
277         p = page_address(page) + offset;
278
279         /* copy small packet so we can reuse these pages for small data */
280         skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
281         if (unlikely(!skb))
282                 return NULL;
283
284         hdr = skb_vnet_hdr(skb);
285
286         hdr_len = vi->hdr_len;
287         if (vi->mergeable_rx_bufs)
288                 hdr_padded_len = sizeof *hdr;
289         else
290                 hdr_padded_len = sizeof(struct padded_vnet_hdr);
291
292         memcpy(hdr, p, hdr_len);
293
294         len -= hdr_len;
295         offset += hdr_padded_len;
296         p += hdr_padded_len;
297
298         copy = len;
299         if (copy > skb_tailroom(skb))
300                 copy = skb_tailroom(skb);
301         memcpy(skb_put(skb, copy), p, copy);
302
303         len -= copy;
304         offset += copy;
305
306         if (vi->mergeable_rx_bufs) {
307                 if (len)
308                         skb_add_rx_frag(skb, 0, page, offset, len, truesize);
309                 else
310                         put_page(page);
311                 return skb;
312         }
313
314         /*
315          * Verify that we can indeed put this data into a skb.
316          * This is here to handle cases when the device erroneously
317          * tries to receive more than is possible. This is usually
318          * the case of a broken device.
319          */
320         if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
321                 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
322                 dev_kfree_skb(skb);
323                 return NULL;
324         }
325         BUG_ON(offset >= PAGE_SIZE);
326         while (len) {
327                 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
328                 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
329                                 frag_size, truesize);
330                 len -= frag_size;
331                 page = (struct page *)page->private;
332                 offset = 0;
333         }
334
335         if (page)
336                 give_pages(rq, page);
337
338         return skb;
339 }
340
341 static void virtnet_xdp_xmit(struct virtnet_info *vi,
342                              struct receive_queue *rq,
343                              struct send_queue *sq,
344                              struct xdp_buff *xdp,
345                              void *data)
346 {
347         struct virtio_net_hdr_mrg_rxbuf *hdr;
348         unsigned int num_sg, len;
349         void *xdp_sent;
350         int err;
351
352         /* Free up any pending old buffers before queueing new ones. */
353         while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
354                 if (vi->mergeable_rx_bufs) {
355                         struct page *sent_page = virt_to_head_page(xdp_sent);
356
357                         put_page(sent_page);
358                 } else { /* small buffer */
359                         struct sk_buff *skb = xdp_sent;
360
361                         kfree_skb(skb);
362                 }
363         }
364
365         if (vi->mergeable_rx_bufs) {
366                 /* Zero header and leave csum up to XDP layers */
367                 hdr = xdp->data;
368                 memset(hdr, 0, vi->hdr_len);
369
370                 num_sg = 1;
371                 sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
372         } else { /* small buffer */
373                 struct sk_buff *skb = data;
374
375                 /* Zero header and leave csum up to XDP layers */
376                 hdr = skb_vnet_hdr(skb);
377                 memset(hdr, 0, vi->hdr_len);
378
379                 num_sg = 2;
380                 sg_init_table(sq->sg, 2);
381                 sg_set_buf(sq->sg, hdr, vi->hdr_len);
382                 skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
383         }
384         err = virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
385                                    data, GFP_ATOMIC);
386         if (unlikely(err)) {
387                 if (vi->mergeable_rx_bufs) {
388                         struct page *page = virt_to_head_page(xdp->data);
389
390                         put_page(page);
391                 } else /* small buffer */
392                         kfree_skb(data);
393                 return; // On error abort to avoid unnecessary kick
394         }
395
396         virtqueue_kick(sq->vq);
397 }
398
399 static u32 do_xdp_prog(struct virtnet_info *vi,
400                        struct receive_queue *rq,
401                        struct bpf_prog *xdp_prog,
402                        void *data, int len)
403 {
404         int hdr_padded_len;
405         struct xdp_buff xdp;
406         void *buf;
407         unsigned int qp;
408         u32 act;
409
410         if (vi->mergeable_rx_bufs) {
411                 hdr_padded_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
412                 xdp.data = data + hdr_padded_len;
413                 xdp.data_end = xdp.data + (len - vi->hdr_len);
414                 buf = data;
415         } else { /* small buffers */
416                 struct sk_buff *skb = data;
417
418                 xdp.data = skb->data;
419                 xdp.data_end = xdp.data + len;
420                 buf = skb->data;
421         }
422
423         act = bpf_prog_run_xdp(xdp_prog, &xdp);
424         switch (act) {
425         case XDP_PASS:
426                 return XDP_PASS;
427         case XDP_TX:
428                 qp = vi->curr_queue_pairs -
429                         vi->xdp_queue_pairs +
430                         smp_processor_id();
431                 xdp.data = buf;
432                 virtnet_xdp_xmit(vi, rq, &vi->sq[qp], &xdp, data);
433                 return XDP_TX;
434         default:
435                 bpf_warn_invalid_xdp_action(act);
436         case XDP_ABORTED:
437         case XDP_DROP:
438                 return XDP_DROP;
439         }
440 }
441
442 static struct sk_buff *receive_small(struct net_device *dev,
443                                      struct virtnet_info *vi,
444                                      struct receive_queue *rq,
445                                      void *buf, unsigned int len)
446 {
447         struct sk_buff * skb = buf;
448         struct bpf_prog *xdp_prog;
449
450         len -= vi->hdr_len;
451         skb_trim(skb, len);
452
453         rcu_read_lock();
454         xdp_prog = rcu_dereference(rq->xdp_prog);
455         if (xdp_prog) {
456                 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
457                 u32 act;
458
459                 if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
460                         goto err_xdp;
461                 act = do_xdp_prog(vi, rq, xdp_prog, skb, len);
462                 switch (act) {
463                 case XDP_PASS:
464                         break;
465                 case XDP_TX:
466                         rcu_read_unlock();
467                         goto xdp_xmit;
468                 case XDP_DROP:
469                 default:
470                         goto err_xdp;
471                 }
472         }
473         rcu_read_unlock();
474
475         return skb;
476
477 err_xdp:
478         rcu_read_unlock();
479         dev->stats.rx_dropped++;
480         kfree_skb(skb);
481 xdp_xmit:
482         return NULL;
483 }
484
485 static struct sk_buff *receive_big(struct net_device *dev,
486                                    struct virtnet_info *vi,
487                                    struct receive_queue *rq,
488                                    void *buf,
489                                    unsigned int len)
490 {
491         struct page *page = buf;
492         struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
493
494         if (unlikely(!skb))
495                 goto err;
496
497         return skb;
498
499 err:
500         dev->stats.rx_dropped++;
501         give_pages(rq, page);
502         return NULL;
503 }
504
505 /* The conditions to enable XDP should preclude the underlying device from
506  * sending packets across multiple buffers (num_buf > 1). However per spec
507  * it does not appear to be illegal to do so but rather just against convention.
508  * So in order to avoid making a system unresponsive the packets are pushed
509  * into a page and the XDP program is run. This will be extremely slow and we
510  * push a warning to the user to fix this as soon as possible. Fixing this may
511  * require resolving the underlying hardware to determine why multiple buffers
512  * are being received or simply loading the XDP program in the ingress stack
513  * after the skb is built because there is no advantage to running it here
514  * anymore.
515  */
516 static struct page *xdp_linearize_page(struct receive_queue *rq,
517                                        u16 *num_buf,
518                                        struct page *p,
519                                        int offset,
520                                        unsigned int *len)
521 {
522         struct page *page = alloc_page(GFP_ATOMIC);
523         unsigned int page_off = 0;
524
525         if (!page)
526                 return NULL;
527
528         memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
529         page_off += *len;
530
531         while (--*num_buf) {
532                 unsigned int buflen;
533                 unsigned long ctx;
534                 void *buf;
535                 int off;
536
537                 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &buflen);
538                 if (unlikely(!ctx))
539                         goto err_buf;
540
541                 buf = mergeable_ctx_to_buf_address(ctx);
542                 p = virt_to_head_page(buf);
543                 off = buf - page_address(p);
544
545                 /* guard against a misconfigured or uncooperative backend that
546                  * is sending packet larger than the MTU.
547                  */
548                 if ((page_off + buflen) > PAGE_SIZE) {
549                         put_page(p);
550                         goto err_buf;
551                 }
552
553                 memcpy(page_address(page) + page_off,
554                        page_address(p) + off, buflen);
555                 page_off += buflen;
556                 put_page(p);
557         }
558
559         *len = page_off;
560         return page;
561 err_buf:
562         __free_pages(page, 0);
563         return NULL;
564 }
565
566 static struct sk_buff *receive_mergeable(struct net_device *dev,
567                                          struct virtnet_info *vi,
568                                          struct receive_queue *rq,
569                                          unsigned long ctx,
570                                          unsigned int len)
571 {
572         void *buf = mergeable_ctx_to_buf_address(ctx);
573         struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
574         u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
575         struct page *page = virt_to_head_page(buf);
576         int offset = buf - page_address(page);
577         struct sk_buff *head_skb, *curr_skb;
578         struct bpf_prog *xdp_prog;
579         unsigned int truesize;
580
581         head_skb = NULL;
582
583         rcu_read_lock();
584         xdp_prog = rcu_dereference(rq->xdp_prog);
585         if (xdp_prog) {
586                 struct page *xdp_page;
587                 u32 act;
588
589                 /* This happens when rx buffer size is underestimated */
590                 if (unlikely(num_buf > 1)) {
591                         /* linearize data for XDP */
592                         xdp_page = xdp_linearize_page(rq, &num_buf,
593                                                       page, offset, &len);
594                         if (!xdp_page)
595                                 goto err_xdp;
596                         offset = 0;
597                 } else {
598                         xdp_page = page;
599                 }
600
601                 /* Transient failure which in theory could occur if
602                  * in-flight packets from before XDP was enabled reach
603                  * the receive path after XDP is loaded. In practice I
604                  * was not able to create this condition.
605                  */
606                 if (unlikely(hdr->hdr.gso_type))
607                         goto err_xdp;
608
609                 act = do_xdp_prog(vi, rq, xdp_prog,
610                                   page_address(xdp_page) + offset, len);
611                 switch (act) {
612                 case XDP_PASS:
613                         /* We can only create skb based on xdp_page. */
614                         if (unlikely(xdp_page != page)) {
615                                 rcu_read_unlock();
616                                 put_page(page);
617                                 head_skb = page_to_skb(vi, rq, xdp_page,
618                                                        0, len, PAGE_SIZE);
619                                 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
620                                 return head_skb;
621                         }
622                         break;
623                 case XDP_TX:
624                         ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
625                         if (unlikely(xdp_page != page))
626                                 goto err_xdp;
627                         rcu_read_unlock();
628                         goto xdp_xmit;
629                 case XDP_DROP:
630                 default:
631                         if (unlikely(xdp_page != page))
632                                 __free_pages(xdp_page, 0);
633                         ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
634                         goto err_xdp;
635                 }
636         }
637         rcu_read_unlock();
638
639         truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
640         head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
641         curr_skb = head_skb;
642
643         if (unlikely(!curr_skb))
644                 goto err_skb;
645         while (--num_buf) {
646                 int num_skb_frags;
647
648                 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
649                 if (unlikely(!ctx)) {
650                         pr_debug("%s: rx error: %d buffers out of %d missing\n",
651                                  dev->name, num_buf,
652                                  virtio16_to_cpu(vi->vdev,
653                                                  hdr->num_buffers));
654                         dev->stats.rx_length_errors++;
655                         goto err_buf;
656                 }
657
658                 buf = mergeable_ctx_to_buf_address(ctx);
659                 page = virt_to_head_page(buf);
660
661                 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
662                 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
663                         struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
664
665                         if (unlikely(!nskb))
666                                 goto err_skb;
667                         if (curr_skb == head_skb)
668                                 skb_shinfo(curr_skb)->frag_list = nskb;
669                         else
670                                 curr_skb->next = nskb;
671                         curr_skb = nskb;
672                         head_skb->truesize += nskb->truesize;
673                         num_skb_frags = 0;
674                 }
675                 truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
676                 if (curr_skb != head_skb) {
677                         head_skb->data_len += len;
678                         head_skb->len += len;
679                         head_skb->truesize += truesize;
680                 }
681                 offset = buf - page_address(page);
682                 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
683                         put_page(page);
684                         skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
685                                              len, truesize);
686                 } else {
687                         skb_add_rx_frag(curr_skb, num_skb_frags, page,
688                                         offset, len, truesize);
689                 }
690         }
691
692         ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
693         return head_skb;
694
695 err_xdp:
696         rcu_read_unlock();
697 err_skb:
698         put_page(page);
699         while (--num_buf) {
700                 ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
701                 if (unlikely(!ctx)) {
702                         pr_debug("%s: rx error: %d buffers missing\n",
703                                  dev->name, num_buf);
704                         dev->stats.rx_length_errors++;
705                         break;
706                 }
707                 page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
708                 put_page(page);
709         }
710 err_buf:
711         dev->stats.rx_dropped++;
712         dev_kfree_skb(head_skb);
713 xdp_xmit:
714         return NULL;
715 }
716
717 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
718                         void *buf, unsigned int len)
719 {
720         struct net_device *dev = vi->dev;
721         struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
722         struct sk_buff *skb;
723         struct virtio_net_hdr_mrg_rxbuf *hdr;
724
725         if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
726                 pr_debug("%s: short packet %i\n", dev->name, len);
727                 dev->stats.rx_length_errors++;
728                 if (vi->mergeable_rx_bufs) {
729                         unsigned long ctx = (unsigned long)buf;
730                         void *base = mergeable_ctx_to_buf_address(ctx);
731                         put_page(virt_to_head_page(base));
732                 } else if (vi->big_packets) {
733                         give_pages(rq, buf);
734                 } else {
735                         dev_kfree_skb(buf);
736                 }
737                 return;
738         }
739
740         if (vi->mergeable_rx_bufs)
741                 skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
742         else if (vi->big_packets)
743                 skb = receive_big(dev, vi, rq, buf, len);
744         else
745                 skb = receive_small(dev, vi, rq, buf, len);
746
747         if (unlikely(!skb))
748                 return;
749
750         hdr = skb_vnet_hdr(skb);
751
752         u64_stats_update_begin(&stats->rx_syncp);
753         stats->rx_bytes += skb->len;
754         stats->rx_packets++;
755         u64_stats_update_end(&stats->rx_syncp);
756
757         if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
758                 skb->ip_summed = CHECKSUM_UNNECESSARY;
759
760         if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
761                                   virtio_is_little_endian(vi->vdev))) {
762                 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
763                                      dev->name, hdr->hdr.gso_type,
764                                      hdr->hdr.gso_size);
765                 goto frame_err;
766         }
767
768         skb->protocol = eth_type_trans(skb, dev);
769         pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
770                  ntohs(skb->protocol), skb->len, skb->pkt_type);
771
772         napi_gro_receive(&rq->napi, skb);
773         return;
774
775 frame_err:
776         dev->stats.rx_frame_errors++;
777         dev_kfree_skb(skb);
778 }
779
780 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
781                              gfp_t gfp)
782 {
783         struct sk_buff *skb;
784         struct virtio_net_hdr_mrg_rxbuf *hdr;
785         int err;
786
787         skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
788         if (unlikely(!skb))
789                 return -ENOMEM;
790
791         skb_put(skb, GOOD_PACKET_LEN);
792
793         hdr = skb_vnet_hdr(skb);
794         sg_init_table(rq->sg, 2);
795         sg_set_buf(rq->sg, hdr, vi->hdr_len);
796         skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
797
798         err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
799         if (err < 0)
800                 dev_kfree_skb(skb);
801
802         return err;
803 }
804
805 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
806                            gfp_t gfp)
807 {
808         struct page *first, *list = NULL;
809         char *p;
810         int i, err, offset;
811
812         sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
813
814         /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
815         for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
816                 first = get_a_page(rq, gfp);
817                 if (!first) {
818                         if (list)
819                                 give_pages(rq, list);
820                         return -ENOMEM;
821                 }
822                 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
823
824                 /* chain new page in list head to match sg */
825                 first->private = (unsigned long)list;
826                 list = first;
827         }
828
829         first = get_a_page(rq, gfp);
830         if (!first) {
831                 give_pages(rq, list);
832                 return -ENOMEM;
833         }
834         p = page_address(first);
835
836         /* rq->sg[0], rq->sg[1] share the same page */
837         /* a separated rq->sg[0] for header - required in case !any_header_sg */
838         sg_set_buf(&rq->sg[0], p, vi->hdr_len);
839
840         /* rq->sg[1] for data packet, from offset */
841         offset = sizeof(struct padded_vnet_hdr);
842         sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
843
844         /* chain first in list head */
845         first->private = (unsigned long)list;
846         err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
847                                   first, gfp);
848         if (err < 0)
849                 give_pages(rq, first);
850
851         return err;
852 }
853
854 static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
855 {
856         const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
857         unsigned int len;
858
859         len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
860                         GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
861         return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
862 }
863
864 static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
865 {
866         struct page_frag *alloc_frag = &rq->alloc_frag;
867         char *buf;
868         unsigned long ctx;
869         int err;
870         unsigned int len, hole;
871
872         len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
873         if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
874                 return -ENOMEM;
875
876         buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
877         ctx = mergeable_buf_to_ctx(buf, len);
878         get_page(alloc_frag->page);
879         alloc_frag->offset += len;
880         hole = alloc_frag->size - alloc_frag->offset;
881         if (hole < len) {
882                 /* To avoid internal fragmentation, if there is very likely not
883                  * enough space for another buffer, add the remaining space to
884                  * the current buffer. This extra space is not included in
885                  * the truesize stored in ctx.
886                  */
887                 len += hole;
888                 alloc_frag->offset += hole;
889         }
890
891         sg_init_one(rq->sg, buf, len);
892         err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
893         if (err < 0)
894                 put_page(virt_to_head_page(buf));
895
896         return err;
897 }
898
899 /*
900  * Returns false if we couldn't fill entirely (OOM).
901  *
902  * Normally run in the receive path, but can also be run from ndo_open
903  * before we're receiving packets, or from refill_work which is
904  * careful to disable receiving (using napi_disable).
905  */
906 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
907                           gfp_t gfp)
908 {
909         int err;
910         bool oom;
911
912         gfp |= __GFP_COLD;
913         do {
914                 if (vi->mergeable_rx_bufs)
915                         err = add_recvbuf_mergeable(rq, gfp);
916                 else if (vi->big_packets)
917                         err = add_recvbuf_big(vi, rq, gfp);
918                 else
919                         err = add_recvbuf_small(vi, rq, gfp);
920
921                 oom = err == -ENOMEM;
922                 if (err)
923                         break;
924         } while (rq->vq->num_free);
925         virtqueue_kick(rq->vq);
926         return !oom;
927 }
928
929 static void skb_recv_done(struct virtqueue *rvq)
930 {
931         struct virtnet_info *vi = rvq->vdev->priv;
932         struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
933
934         /* Schedule NAPI, Suppress further interrupts if successful. */
935         if (napi_schedule_prep(&rq->napi)) {
936                 virtqueue_disable_cb(rvq);
937                 __napi_schedule(&rq->napi);
938         }
939 }
940
941 static void virtnet_napi_enable(struct receive_queue *rq)
942 {
943         napi_enable(&rq->napi);
944
945         /* If all buffers were filled by other side before we napi_enabled, we
946          * won't get another interrupt, so process any outstanding packets
947          * now.  virtnet_poll wants re-enable the queue, so we disable here.
948          * We synchronize against interrupts via NAPI_STATE_SCHED */
949         if (napi_schedule_prep(&rq->napi)) {
950                 virtqueue_disable_cb(rq->vq);
951                 local_bh_disable();
952                 __napi_schedule(&rq->napi);
953                 local_bh_enable();
954         }
955 }
956
957 static void refill_work(struct work_struct *work)
958 {
959         struct virtnet_info *vi =
960                 container_of(work, struct virtnet_info, refill.work);
961         bool still_empty;
962         int i;
963
964         for (i = 0; i < vi->curr_queue_pairs; i++) {
965                 struct receive_queue *rq = &vi->rq[i];
966
967                 napi_disable(&rq->napi);
968                 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
969                 virtnet_napi_enable(rq);
970
971                 /* In theory, this can happen: if we don't get any buffers in
972                  * we will *never* try to fill again.
973                  */
974                 if (still_empty)
975                         schedule_delayed_work(&vi->refill, HZ/2);
976         }
977 }
978
979 static int virtnet_receive(struct receive_queue *rq, int budget)
980 {
981         struct virtnet_info *vi = rq->vq->vdev->priv;
982         unsigned int len, received = 0;
983         void *buf;
984
985         while (received < budget &&
986                (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
987                 receive_buf(vi, rq, buf, len);
988                 received++;
989         }
990
991         if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
992                 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
993                         schedule_delayed_work(&vi->refill, 0);
994         }
995
996         return received;
997 }
998
999 static int virtnet_poll(struct napi_struct *napi, int budget)
1000 {
1001         struct receive_queue *rq =
1002                 container_of(napi, struct receive_queue, napi);
1003         unsigned int r, received;
1004
1005         received = virtnet_receive(rq, budget);
1006
1007         /* Out of packets? */
1008         if (received < budget) {
1009                 r = virtqueue_enable_cb_prepare(rq->vq);
1010                 napi_complete_done(napi, received);
1011                 if (unlikely(virtqueue_poll(rq->vq, r)) &&
1012                     napi_schedule_prep(napi)) {
1013                         virtqueue_disable_cb(rq->vq);
1014                         __napi_schedule(napi);
1015                 }
1016         }
1017
1018         return received;
1019 }
1020
1021 #ifdef CONFIG_NET_RX_BUSY_POLL
1022 /* must be called with local_bh_disable()d */
1023 static int virtnet_busy_poll(struct napi_struct *napi)
1024 {
1025         struct receive_queue *rq =
1026                 container_of(napi, struct receive_queue, napi);
1027         struct virtnet_info *vi = rq->vq->vdev->priv;
1028         int r, received = 0, budget = 4;
1029
1030         if (!(vi->status & VIRTIO_NET_S_LINK_UP))
1031                 return LL_FLUSH_FAILED;
1032
1033         if (!napi_schedule_prep(napi))
1034                 return LL_FLUSH_BUSY;
1035
1036         virtqueue_disable_cb(rq->vq);
1037
1038 again:
1039         received += virtnet_receive(rq, budget);
1040
1041         r = virtqueue_enable_cb_prepare(rq->vq);
1042         clear_bit(NAPI_STATE_SCHED, &napi->state);
1043         if (unlikely(virtqueue_poll(rq->vq, r)) &&
1044             napi_schedule_prep(napi)) {
1045                 virtqueue_disable_cb(rq->vq);
1046                 if (received < budget) {
1047                         budget -= received;
1048                         goto again;
1049                 } else {
1050                         __napi_schedule(napi);
1051                 }
1052         }
1053
1054         return received;
1055 }
1056 #endif  /* CONFIG_NET_RX_BUSY_POLL */
1057
1058 static int virtnet_open(struct net_device *dev)
1059 {
1060         struct virtnet_info *vi = netdev_priv(dev);
1061         int i;
1062
1063         for (i = 0; i < vi->max_queue_pairs; i++) {
1064                 if (i < vi->curr_queue_pairs)
1065                         /* Make sure we have some buffers: if oom use wq. */
1066                         if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1067                                 schedule_delayed_work(&vi->refill, 0);
1068                 virtnet_napi_enable(&vi->rq[i]);
1069         }
1070
1071         return 0;
1072 }
1073
1074 static void free_old_xmit_skbs(struct send_queue *sq)
1075 {
1076         struct sk_buff *skb;
1077         unsigned int len;
1078         struct virtnet_info *vi = sq->vq->vdev->priv;
1079         struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
1080
1081         while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1082                 pr_debug("Sent skb %p\n", skb);
1083
1084                 u64_stats_update_begin(&stats->tx_syncp);
1085                 stats->tx_bytes += skb->len;
1086                 stats->tx_packets++;
1087                 u64_stats_update_end(&stats->tx_syncp);
1088
1089                 dev_kfree_skb_any(skb);
1090         }
1091 }
1092
1093 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1094 {
1095         struct virtio_net_hdr_mrg_rxbuf *hdr;
1096         const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1097         struct virtnet_info *vi = sq->vq->vdev->priv;
1098         unsigned num_sg;
1099         unsigned hdr_len = vi->hdr_len;
1100         bool can_push;
1101
1102         pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1103
1104         can_push = vi->any_header_sg &&
1105                 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1106                 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1107         /* Even if we can, don't push here yet as this would skew
1108          * csum_start offset below. */
1109         if (can_push)
1110                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1111         else
1112                 hdr = skb_vnet_hdr(skb);
1113
1114         if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1115                                     virtio_is_little_endian(vi->vdev), false))
1116                 BUG();
1117
1118         if (vi->mergeable_rx_bufs)
1119                 hdr->num_buffers = 0;
1120
1121         sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1122         if (can_push) {
1123                 __skb_push(skb, hdr_len);
1124                 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1125                 /* Pull header back to avoid skew in tx bytes calculations. */
1126                 __skb_pull(skb, hdr_len);
1127         } else {
1128                 sg_set_buf(sq->sg, hdr, hdr_len);
1129                 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1130         }
1131         return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
1132 }
1133
1134 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
1135 {
1136         struct virtnet_info *vi = netdev_priv(dev);
1137         int qnum = skb_get_queue_mapping(skb);
1138         struct send_queue *sq = &vi->sq[qnum];
1139         int err;
1140         struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1141         bool kick = !skb->xmit_more;
1142
1143         /* Free up any pending old buffers before queueing new ones. */
1144         free_old_xmit_skbs(sq);
1145
1146         /* timestamp packet in software */
1147         skb_tx_timestamp(skb);
1148
1149         /* Try to transmit */
1150         err = xmit_skb(sq, skb);
1151
1152         /* This should not happen! */
1153         if (unlikely(err)) {
1154                 dev->stats.tx_fifo_errors++;
1155                 if (net_ratelimit())
1156                         dev_warn(&dev->dev,
1157                                  "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
1158                 dev->stats.tx_dropped++;
1159                 dev_kfree_skb_any(skb);
1160                 return NETDEV_TX_OK;
1161         }
1162
1163         /* Don't wait up for transmitted skbs to be freed. */
1164         skb_orphan(skb);
1165         nf_reset(skb);
1166
1167         /* If running out of space, stop queue to avoid getting packets that we
1168          * are then unable to transmit.
1169          * An alternative would be to force queuing layer to requeue the skb by
1170          * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1171          * returned in a normal path of operation: it means that driver is not
1172          * maintaining the TX queue stop/start state properly, and causes
1173          * the stack to do a non-trivial amount of useless work.
1174          * Since most packets only take 1 or 2 ring slots, stopping the queue
1175          * early means 16 slots are typically wasted.
1176          */
1177         if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1178                 netif_stop_subqueue(dev, qnum);
1179                 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1180                         /* More just got used, free them then recheck. */
1181                         free_old_xmit_skbs(sq);
1182                         if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1183                                 netif_start_subqueue(dev, qnum);
1184                                 virtqueue_disable_cb(sq->vq);
1185                         }
1186                 }
1187         }
1188
1189         if (kick || netif_xmit_stopped(txq))
1190                 virtqueue_kick(sq->vq);
1191
1192         return NETDEV_TX_OK;
1193 }
1194
1195 /*
1196  * Send command via the control virtqueue and check status.  Commands
1197  * supported by the hypervisor, as indicated by feature bits, should
1198  * never fail unless improperly formatted.
1199  */
1200 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1201                                  struct scatterlist *out)
1202 {
1203         struct scatterlist *sgs[4], hdr, stat;
1204         unsigned out_num = 0, tmp;
1205
1206         /* Caller should know better */
1207         BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1208
1209         vi->ctrl_status = ~0;
1210         vi->ctrl_hdr.class = class;
1211         vi->ctrl_hdr.cmd = cmd;
1212         /* Add header */
1213         sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
1214         sgs[out_num++] = &hdr;
1215
1216         if (out)
1217                 sgs[out_num++] = out;
1218
1219         /* Add return status. */
1220         sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
1221         sgs[out_num] = &stat;
1222
1223         BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1224         virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1225
1226         if (unlikely(!virtqueue_kick(vi->cvq)))
1227                 return vi->ctrl_status == VIRTIO_NET_OK;
1228
1229         /* Spin for a response, the kick causes an ioport write, trapping
1230          * into the hypervisor, so the request should be handled immediately.
1231          */
1232         while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1233                !virtqueue_is_broken(vi->cvq))
1234                 cpu_relax();
1235
1236         return vi->ctrl_status == VIRTIO_NET_OK;
1237 }
1238
1239 static int virtnet_set_mac_address(struct net_device *dev, void *p)
1240 {
1241         struct virtnet_info *vi = netdev_priv(dev);
1242         struct virtio_device *vdev = vi->vdev;
1243         int ret;
1244         struct sockaddr *addr;
1245         struct scatterlist sg;
1246
1247         addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1248         if (!addr)
1249                 return -ENOMEM;
1250         memcpy(addr, p, sizeof(*addr));
1251
1252         ret = eth_prepare_mac_addr_change(dev, addr);
1253         if (ret)
1254                 goto out;
1255
1256         if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1257                 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1258                 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1259                                           VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
1260                         dev_warn(&vdev->dev,
1261                                  "Failed to set mac address by vq command.\n");
1262                         ret = -EINVAL;
1263                         goto out;
1264                 }
1265         } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1266                    !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1267                 unsigned int i;
1268
1269                 /* Naturally, this has an atomicity problem. */
1270                 for (i = 0; i < dev->addr_len; i++)
1271                         virtio_cwrite8(vdev,
1272                                        offsetof(struct virtio_net_config, mac) +
1273                                        i, addr->sa_data[i]);
1274         }
1275
1276         eth_commit_mac_addr_change(dev, p);
1277         ret = 0;
1278
1279 out:
1280         kfree(addr);
1281         return ret;
1282 }
1283
1284 static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
1285                                                struct rtnl_link_stats64 *tot)
1286 {
1287         struct virtnet_info *vi = netdev_priv(dev);
1288         int cpu;
1289         unsigned int start;
1290
1291         for_each_possible_cpu(cpu) {
1292                 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
1293                 u64 tpackets, tbytes, rpackets, rbytes;
1294
1295                 do {
1296                         start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
1297                         tpackets = stats->tx_packets;
1298                         tbytes   = stats->tx_bytes;
1299                 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
1300
1301                 do {
1302                         start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
1303                         rpackets = stats->rx_packets;
1304                         rbytes   = stats->rx_bytes;
1305                 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
1306
1307                 tot->rx_packets += rpackets;
1308                 tot->tx_packets += tpackets;
1309                 tot->rx_bytes   += rbytes;
1310                 tot->tx_bytes   += tbytes;
1311         }
1312
1313         tot->tx_dropped = dev->stats.tx_dropped;
1314         tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
1315         tot->rx_dropped = dev->stats.rx_dropped;
1316         tot->rx_length_errors = dev->stats.rx_length_errors;
1317         tot->rx_frame_errors = dev->stats.rx_frame_errors;
1318
1319         return tot;
1320 }
1321
1322 #ifdef CONFIG_NET_POLL_CONTROLLER
1323 static void virtnet_netpoll(struct net_device *dev)
1324 {
1325         struct virtnet_info *vi = netdev_priv(dev);
1326         int i;
1327
1328         for (i = 0; i < vi->curr_queue_pairs; i++)
1329                 napi_schedule(&vi->rq[i].napi);
1330 }
1331 #endif
1332
1333 static void virtnet_ack_link_announce(struct virtnet_info *vi)
1334 {
1335         rtnl_lock();
1336         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1337                                   VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1338                 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1339         rtnl_unlock();
1340 }
1341
1342 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1343 {
1344         struct scatterlist sg;
1345         struct net_device *dev = vi->dev;
1346
1347         if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1348                 return 0;
1349
1350         vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1351         sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
1352
1353         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1354                                   VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1355                 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1356                          queue_pairs);
1357                 return -EINVAL;
1358         } else {
1359                 vi->curr_queue_pairs = queue_pairs;
1360                 /* virtnet_open() will refill when device is going to up. */
1361                 if (dev->flags & IFF_UP)
1362                         schedule_delayed_work(&vi->refill, 0);
1363         }
1364
1365         return 0;
1366 }
1367
1368 static int virtnet_close(struct net_device *dev)
1369 {
1370         struct virtnet_info *vi = netdev_priv(dev);
1371         int i;
1372
1373         /* Make sure refill_work doesn't re-enable napi! */
1374         cancel_delayed_work_sync(&vi->refill);
1375
1376         for (i = 0; i < vi->max_queue_pairs; i++)
1377                 napi_disable(&vi->rq[i].napi);
1378
1379         return 0;
1380 }
1381
1382 static void virtnet_set_rx_mode(struct net_device *dev)
1383 {
1384         struct virtnet_info *vi = netdev_priv(dev);
1385         struct scatterlist sg[2];
1386         struct virtio_net_ctrl_mac *mac_data;
1387         struct netdev_hw_addr *ha;
1388         int uc_count;
1389         int mc_count;
1390         void *buf;
1391         int i;
1392
1393         /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
1394         if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1395                 return;
1396
1397         vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1398         vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1399
1400         sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
1401
1402         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1403                                   VIRTIO_NET_CTRL_RX_PROMISC, sg))
1404                 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1405                          vi->ctrl_promisc ? "en" : "dis");
1406
1407         sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
1408
1409         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1410                                   VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
1411                 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1412                          vi->ctrl_allmulti ? "en" : "dis");
1413
1414         uc_count = netdev_uc_count(dev);
1415         mc_count = netdev_mc_count(dev);
1416         /* MAC filter - use one buffer for both lists */
1417         buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1418                       (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1419         mac_data = buf;
1420         if (!buf)
1421                 return;
1422
1423         sg_init_table(sg, 2);
1424
1425         /* Store the unicast list and count in the front of the buffer */
1426         mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1427         i = 0;
1428         netdev_for_each_uc_addr(ha, dev)
1429                 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1430
1431         sg_set_buf(&sg[0], mac_data,
1432                    sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1433
1434         /* multicast list and count fill the end */
1435         mac_data = (void *)&mac_data->macs[uc_count][0];
1436
1437         mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1438         i = 0;
1439         netdev_for_each_mc_addr(ha, dev)
1440                 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1441
1442         sg_set_buf(&sg[1], mac_data,
1443                    sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1444
1445         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1446                                   VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
1447                 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1448
1449         kfree(buf);
1450 }
1451
1452 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1453                                    __be16 proto, u16 vid)
1454 {
1455         struct virtnet_info *vi = netdev_priv(dev);
1456         struct scatterlist sg;
1457
1458         vi->ctrl_vid = vid;
1459         sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
1460
1461         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1462                                   VIRTIO_NET_CTRL_VLAN_ADD, &sg))
1463                 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
1464         return 0;
1465 }
1466
1467 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1468                                     __be16 proto, u16 vid)
1469 {
1470         struct virtnet_info *vi = netdev_priv(dev);
1471         struct scatterlist sg;
1472
1473         vi->ctrl_vid = vid;
1474         sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
1475
1476         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1477                                   VIRTIO_NET_CTRL_VLAN_DEL, &sg))
1478                 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
1479         return 0;
1480 }
1481
1482 static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1483 {
1484         int i;
1485
1486         if (vi->affinity_hint_set) {
1487                 for (i = 0; i < vi->max_queue_pairs; i++) {
1488                         virtqueue_set_affinity(vi->rq[i].vq, -1);
1489                         virtqueue_set_affinity(vi->sq[i].vq, -1);
1490                 }
1491
1492                 vi->affinity_hint_set = false;
1493         }
1494 }
1495
1496 static void virtnet_set_affinity(struct virtnet_info *vi)
1497 {
1498         int i;
1499         int cpu;
1500
1501         /* In multiqueue mode, when the number of cpu is equal to the number of
1502          * queue pairs, we let the queue pairs to be private to one cpu by
1503          * setting the affinity hint to eliminate the contention.
1504          */
1505         if (vi->curr_queue_pairs == 1 ||
1506             vi->max_queue_pairs != num_online_cpus()) {
1507                 virtnet_clean_affinity(vi, -1);
1508                 return;
1509         }
1510
1511         i = 0;
1512         for_each_online_cpu(cpu) {
1513                 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1514                 virtqueue_set_affinity(vi->sq[i].vq, cpu);
1515                 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
1516                 i++;
1517         }
1518
1519         vi->affinity_hint_set = true;
1520 }
1521
1522 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
1523 {
1524         struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1525                                                    node);
1526         virtnet_set_affinity(vi);
1527         return 0;
1528 }
1529
1530 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1531 {
1532         struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1533                                                    node_dead);
1534         virtnet_set_affinity(vi);
1535         return 0;
1536 }
1537
1538 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1539 {
1540         struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1541                                                    node);
1542
1543         virtnet_clean_affinity(vi, cpu);
1544         return 0;
1545 }
1546
1547 static enum cpuhp_state virtionet_online;
1548
1549 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1550 {
1551         int ret;
1552
1553         ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1554         if (ret)
1555                 return ret;
1556         ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1557                                                &vi->node_dead);
1558         if (!ret)
1559                 return ret;
1560         cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1561         return ret;
1562 }
1563
1564 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1565 {
1566         cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1567         cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1568                                             &vi->node_dead);
1569 }
1570
1571 static void virtnet_get_ringparam(struct net_device *dev,
1572                                 struct ethtool_ringparam *ring)
1573 {
1574         struct virtnet_info *vi = netdev_priv(dev);
1575
1576         ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1577         ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
1578         ring->rx_pending = ring->rx_max_pending;
1579         ring->tx_pending = ring->tx_max_pending;
1580 }
1581
1582
1583 static void virtnet_get_drvinfo(struct net_device *dev,
1584                                 struct ethtool_drvinfo *info)
1585 {
1586         struct virtnet_info *vi = netdev_priv(dev);
1587         struct virtio_device *vdev = vi->vdev;
1588
1589         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1590         strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1591         strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1592
1593 }
1594
1595 /* TODO: Eliminate OOO packets during switching */
1596 static int virtnet_set_channels(struct net_device *dev,
1597                                 struct ethtool_channels *channels)
1598 {
1599         struct virtnet_info *vi = netdev_priv(dev);
1600         u16 queue_pairs = channels->combined_count;
1601         int err;
1602
1603         /* We don't support separate rx/tx channels.
1604          * We don't allow setting 'other' channels.
1605          */
1606         if (channels->rx_count || channels->tx_count || channels->other_count)
1607                 return -EINVAL;
1608
1609         if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
1610                 return -EINVAL;
1611
1612         /* For now we don't support modifying channels while XDP is loaded
1613          * also when XDP is loaded all RX queues have XDP programs so we only
1614          * need to check a single RX queue.
1615          */
1616         if (vi->rq[0].xdp_prog)
1617                 return -EINVAL;
1618
1619         get_online_cpus();
1620         err = virtnet_set_queues(vi, queue_pairs);
1621         if (!err) {
1622                 netif_set_real_num_tx_queues(dev, queue_pairs);
1623                 netif_set_real_num_rx_queues(dev, queue_pairs);
1624
1625                 virtnet_set_affinity(vi);
1626         }
1627         put_online_cpus();
1628
1629         return err;
1630 }
1631
1632 static void virtnet_get_channels(struct net_device *dev,
1633                                  struct ethtool_channels *channels)
1634 {
1635         struct virtnet_info *vi = netdev_priv(dev);
1636
1637         channels->combined_count = vi->curr_queue_pairs;
1638         channels->max_combined = vi->max_queue_pairs;
1639         channels->max_other = 0;
1640         channels->rx_count = 0;
1641         channels->tx_count = 0;
1642         channels->other_count = 0;
1643 }
1644
1645 /* Check if the user is trying to change anything besides speed/duplex */
1646 static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1647 {
1648         struct ethtool_cmd diff1 = *cmd;
1649         struct ethtool_cmd diff2 = {};
1650
1651         /* cmd is always set so we need to clear it, validate the port type
1652          * and also without autonegotiation we can ignore advertising
1653          */
1654         ethtool_cmd_speed_set(&diff1, 0);
1655         diff2.port = PORT_OTHER;
1656         diff1.advertising = 0;
1657         diff1.duplex = 0;
1658         diff1.cmd = 0;
1659
1660         return !memcmp(&diff1, &diff2, sizeof(diff1));
1661 }
1662
1663 static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1664 {
1665         struct virtnet_info *vi = netdev_priv(dev);
1666         u32 speed;
1667
1668         speed = ethtool_cmd_speed(cmd);
1669         /* don't allow custom speed and duplex */
1670         if (!ethtool_validate_speed(speed) ||
1671             !ethtool_validate_duplex(cmd->duplex) ||
1672             !virtnet_validate_ethtool_cmd(cmd))
1673                 return -EINVAL;
1674         vi->speed = speed;
1675         vi->duplex = cmd->duplex;
1676
1677         return 0;
1678 }
1679
1680 static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1681 {
1682         struct virtnet_info *vi = netdev_priv(dev);
1683
1684         ethtool_cmd_speed_set(cmd, vi->speed);
1685         cmd->duplex = vi->duplex;
1686         cmd->port = PORT_OTHER;
1687
1688         return 0;
1689 }
1690
1691 static void virtnet_init_settings(struct net_device *dev)
1692 {
1693         struct virtnet_info *vi = netdev_priv(dev);
1694
1695         vi->speed = SPEED_UNKNOWN;
1696         vi->duplex = DUPLEX_UNKNOWN;
1697 }
1698
1699 static const struct ethtool_ops virtnet_ethtool_ops = {
1700         .get_drvinfo = virtnet_get_drvinfo,
1701         .get_link = ethtool_op_get_link,
1702         .get_ringparam = virtnet_get_ringparam,
1703         .set_channels = virtnet_set_channels,
1704         .get_channels = virtnet_get_channels,
1705         .get_ts_info = ethtool_op_get_ts_info,
1706         .get_settings = virtnet_get_settings,
1707         .set_settings = virtnet_set_settings,
1708 };
1709
1710 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1711 {
1712         unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1713         struct virtnet_info *vi = netdev_priv(dev);
1714         struct bpf_prog *old_prog;
1715         u16 xdp_qp = 0, curr_qp;
1716         int i, err;
1717
1718         if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1719             virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1720             virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1721             virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
1722                 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1723                 return -EOPNOTSUPP;
1724         }
1725
1726         if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1727                 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1728                 return -EINVAL;
1729         }
1730
1731         if (dev->mtu > max_sz) {
1732                 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1733                 return -EINVAL;
1734         }
1735
1736         curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1737         if (prog)
1738                 xdp_qp = nr_cpu_ids;
1739
1740         /* XDP requires extra queues for XDP_TX */
1741         if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1742                 netdev_warn(dev, "request %i queues but max is %i\n",
1743                             curr_qp + xdp_qp, vi->max_queue_pairs);
1744                 return -ENOMEM;
1745         }
1746
1747         err = virtnet_set_queues(vi, curr_qp + xdp_qp);
1748         if (err) {
1749                 dev_warn(&dev->dev, "XDP Device queue allocation failure.\n");
1750                 return err;
1751         }
1752
1753         if (prog) {
1754                 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1755                 if (IS_ERR(prog)) {
1756                         virtnet_set_queues(vi, curr_qp);
1757                         return PTR_ERR(prog);
1758                 }
1759         }
1760
1761         vi->xdp_queue_pairs = xdp_qp;
1762         netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1763
1764         for (i = 0; i < vi->max_queue_pairs; i++) {
1765                 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1766                 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1767                 if (old_prog)
1768                         bpf_prog_put(old_prog);
1769         }
1770
1771         return 0;
1772 }
1773
1774 static bool virtnet_xdp_query(struct net_device *dev)
1775 {
1776         struct virtnet_info *vi = netdev_priv(dev);
1777         int i;
1778
1779         for (i = 0; i < vi->max_queue_pairs; i++) {
1780                 if (vi->rq[i].xdp_prog)
1781                         return true;
1782         }
1783         return false;
1784 }
1785
1786 static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1787 {
1788         switch (xdp->command) {
1789         case XDP_SETUP_PROG:
1790                 return virtnet_xdp_set(dev, xdp->prog);
1791         case XDP_QUERY_PROG:
1792                 xdp->prog_attached = virtnet_xdp_query(dev);
1793                 return 0;
1794         default:
1795                 return -EINVAL;
1796         }
1797 }
1798
1799 static const struct net_device_ops virtnet_netdev = {
1800         .ndo_open            = virtnet_open,
1801         .ndo_stop            = virtnet_close,
1802         .ndo_start_xmit      = start_xmit,
1803         .ndo_validate_addr   = eth_validate_addr,
1804         .ndo_set_mac_address = virtnet_set_mac_address,
1805         .ndo_set_rx_mode     = virtnet_set_rx_mode,
1806         .ndo_get_stats64     = virtnet_stats,
1807         .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1808         .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1809 #ifdef CONFIG_NET_POLL_CONTROLLER
1810         .ndo_poll_controller = virtnet_netpoll,
1811 #endif
1812 #ifdef CONFIG_NET_RX_BUSY_POLL
1813         .ndo_busy_poll          = virtnet_busy_poll,
1814 #endif
1815         .ndo_xdp                = virtnet_xdp,
1816 };
1817
1818 static void virtnet_config_changed_work(struct work_struct *work)
1819 {
1820         struct virtnet_info *vi =
1821                 container_of(work, struct virtnet_info, config_work);
1822         u16 v;
1823
1824         if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1825                                  struct virtio_net_config, status, &v) < 0)
1826                 return;
1827
1828         if (v & VIRTIO_NET_S_ANNOUNCE) {
1829                 netdev_notify_peers(vi->dev);
1830                 virtnet_ack_link_announce(vi);
1831         }
1832
1833         /* Ignore unknown (future) status bits */
1834         v &= VIRTIO_NET_S_LINK_UP;
1835
1836         if (vi->status == v)
1837                 return;
1838
1839         vi->status = v;
1840
1841         if (vi->status & VIRTIO_NET_S_LINK_UP) {
1842                 netif_carrier_on(vi->dev);
1843                 netif_tx_wake_all_queues(vi->dev);
1844         } else {
1845                 netif_carrier_off(vi->dev);
1846                 netif_tx_stop_all_queues(vi->dev);
1847         }
1848 }
1849
1850 static void virtnet_config_changed(struct virtio_device *vdev)
1851 {
1852         struct virtnet_info *vi = vdev->priv;
1853
1854         schedule_work(&vi->config_work);
1855 }
1856
1857 static void virtnet_free_queues(struct virtnet_info *vi)
1858 {
1859         int i;
1860
1861         for (i = 0; i < vi->max_queue_pairs; i++) {
1862                 napi_hash_del(&vi->rq[i].napi);
1863                 netif_napi_del(&vi->rq[i].napi);
1864         }
1865
1866         /* We called napi_hash_del() before netif_napi_del(),
1867          * we need to respect an RCU grace period before freeing vi->rq
1868          */
1869         synchronize_net();
1870
1871         kfree(vi->rq);
1872         kfree(vi->sq);
1873 }
1874
1875 static void free_receive_bufs(struct virtnet_info *vi)
1876 {
1877         struct bpf_prog *old_prog;
1878         int i;
1879
1880         rtnl_lock();
1881         for (i = 0; i < vi->max_queue_pairs; i++) {
1882                 while (vi->rq[i].pages)
1883                         __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1884
1885                 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1886                 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
1887                 if (old_prog)
1888                         bpf_prog_put(old_prog);
1889         }
1890         rtnl_unlock();
1891 }
1892
1893 static void free_receive_page_frags(struct virtnet_info *vi)
1894 {
1895         int i;
1896         for (i = 0; i < vi->max_queue_pairs; i++)
1897                 if (vi->rq[i].alloc_frag.page)
1898                         put_page(vi->rq[i].alloc_frag.page);
1899 }
1900
1901 static bool is_xdp_queue(struct virtnet_info *vi, int q)
1902 {
1903         if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1904                 return false;
1905         else if (q < vi->curr_queue_pairs)
1906                 return true;
1907         else
1908                 return false;
1909 }
1910
1911 static void free_unused_bufs(struct virtnet_info *vi)
1912 {
1913         void *buf;
1914         int i;
1915
1916         for (i = 0; i < vi->max_queue_pairs; i++) {
1917                 struct virtqueue *vq = vi->sq[i].vq;
1918                 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1919                         if (!is_xdp_queue(vi, i))
1920                                 dev_kfree_skb(buf);
1921                         else
1922                                 put_page(virt_to_head_page(buf));
1923                 }
1924         }
1925
1926         for (i = 0; i < vi->max_queue_pairs; i++) {
1927                 struct virtqueue *vq = vi->rq[i].vq;
1928
1929                 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1930                         if (vi->mergeable_rx_bufs) {
1931                                 unsigned long ctx = (unsigned long)buf;
1932                                 void *base = mergeable_ctx_to_buf_address(ctx);
1933                                 put_page(virt_to_head_page(base));
1934                         } else if (vi->big_packets) {
1935                                 give_pages(&vi->rq[i], buf);
1936                         } else {
1937                                 dev_kfree_skb(buf);
1938                         }
1939                 }
1940         }
1941 }
1942
1943 static void virtnet_del_vqs(struct virtnet_info *vi)
1944 {
1945         struct virtio_device *vdev = vi->vdev;
1946
1947         virtnet_clean_affinity(vi, -1);
1948
1949         vdev->config->del_vqs(vdev);
1950
1951         virtnet_free_queues(vi);
1952 }
1953
1954 static int virtnet_find_vqs(struct virtnet_info *vi)
1955 {
1956         vq_callback_t **callbacks;
1957         struct virtqueue **vqs;
1958         int ret = -ENOMEM;
1959         int i, total_vqs;
1960         const char **names;
1961
1962         /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1963          * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1964          * possible control vq.
1965          */
1966         total_vqs = vi->max_queue_pairs * 2 +
1967                     virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1968
1969         /* Allocate space for find_vqs parameters */
1970         vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1971         if (!vqs)
1972                 goto err_vq;
1973         callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1974         if (!callbacks)
1975                 goto err_callback;
1976         names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1977         if (!names)
1978                 goto err_names;
1979
1980         /* Parameters for control virtqueue, if any */
1981         if (vi->has_cvq) {
1982                 callbacks[total_vqs - 1] = NULL;
1983                 names[total_vqs - 1] = "control";
1984         }
1985
1986         /* Allocate/initialize parameters for send/receive virtqueues */
1987         for (i = 0; i < vi->max_queue_pairs; i++) {
1988                 callbacks[rxq2vq(i)] = skb_recv_done;
1989                 callbacks[txq2vq(i)] = skb_xmit_done;
1990                 sprintf(vi->rq[i].name, "input.%d", i);
1991                 sprintf(vi->sq[i].name, "output.%d", i);
1992                 names[rxq2vq(i)] = vi->rq[i].name;
1993                 names[txq2vq(i)] = vi->sq[i].name;
1994         }
1995
1996         ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1997                                          names);
1998         if (ret)
1999                 goto err_find;
2000
2001         if (vi->has_cvq) {
2002                 vi->cvq = vqs[total_vqs - 1];
2003                 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2004                         vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2005         }
2006
2007         for (i = 0; i < vi->max_queue_pairs; i++) {
2008                 vi->rq[i].vq = vqs[rxq2vq(i)];
2009                 vi->sq[i].vq = vqs[txq2vq(i)];
2010         }
2011
2012         kfree(names);
2013         kfree(callbacks);
2014         kfree(vqs);
2015
2016         return 0;
2017
2018 err_find:
2019         kfree(names);
2020 err_names:
2021         kfree(callbacks);
2022 err_callback:
2023         kfree(vqs);
2024 err_vq:
2025         return ret;
2026 }
2027
2028 static int virtnet_alloc_queues(struct virtnet_info *vi)
2029 {
2030         int i;
2031
2032         vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2033         if (!vi->sq)
2034                 goto err_sq;
2035         vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
2036         if (!vi->rq)
2037                 goto err_rq;
2038
2039         INIT_DELAYED_WORK(&vi->refill, refill_work);
2040         for (i = 0; i < vi->max_queue_pairs; i++) {
2041                 vi->rq[i].pages = NULL;
2042                 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2043                                napi_weight);
2044
2045                 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
2046                 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2047                 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2048         }
2049
2050         return 0;
2051
2052 err_rq:
2053         kfree(vi->sq);
2054 err_sq:
2055         return -ENOMEM;
2056 }
2057
2058 static int init_vqs(struct virtnet_info *vi)
2059 {
2060         int ret;
2061
2062         /* Allocate send & receive queues */
2063         ret = virtnet_alloc_queues(vi);
2064         if (ret)
2065                 goto err;
2066
2067         ret = virtnet_find_vqs(vi);
2068         if (ret)
2069                 goto err_free;
2070
2071         get_online_cpus();
2072         virtnet_set_affinity(vi);
2073         put_online_cpus();
2074
2075         return 0;
2076
2077 err_free:
2078         virtnet_free_queues(vi);
2079 err:
2080         return ret;
2081 }
2082
2083 #ifdef CONFIG_SYSFS
2084 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2085                 struct rx_queue_attribute *attribute, char *buf)
2086 {
2087         struct virtnet_info *vi = netdev_priv(queue->dev);
2088         unsigned int queue_index = get_netdev_rx_queue_index(queue);
2089         struct ewma_pkt_len *avg;
2090
2091         BUG_ON(queue_index >= vi->max_queue_pairs);
2092         avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2093         return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2094 }
2095
2096 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2097         __ATTR_RO(mergeable_rx_buffer_size);
2098
2099 static struct attribute *virtio_net_mrg_rx_attrs[] = {
2100         &mergeable_rx_buffer_size_attribute.attr,
2101         NULL
2102 };
2103
2104 static const struct attribute_group virtio_net_mrg_rx_group = {
2105         .name = "virtio_net",
2106         .attrs = virtio_net_mrg_rx_attrs
2107 };
2108 #endif
2109
2110 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2111                                     unsigned int fbit,
2112                                     const char *fname, const char *dname)
2113 {
2114         if (!virtio_has_feature(vdev, fbit))
2115                 return false;
2116
2117         dev_err(&vdev->dev, "device advertises feature %s but not %s",
2118                 fname, dname);
2119
2120         return true;
2121 }
2122
2123 #define VIRTNET_FAIL_ON(vdev, fbit, dbit)                       \
2124         virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2125
2126 static bool virtnet_validate_features(struct virtio_device *vdev)
2127 {
2128         if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2129             (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2130                              "VIRTIO_NET_F_CTRL_VQ") ||
2131              VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2132                              "VIRTIO_NET_F_CTRL_VQ") ||
2133              VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2134                              "VIRTIO_NET_F_CTRL_VQ") ||
2135              VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2136              VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2137                              "VIRTIO_NET_F_CTRL_VQ"))) {
2138                 return false;
2139         }
2140
2141         return true;
2142 }
2143
2144 #define MIN_MTU ETH_MIN_MTU
2145 #define MAX_MTU ETH_MAX_MTU
2146
2147 static int virtnet_probe(struct virtio_device *vdev)
2148 {
2149         int i, err;
2150         struct net_device *dev;
2151         struct virtnet_info *vi;
2152         u16 max_queue_pairs;
2153         int mtu;
2154
2155         if (!vdev->config->get) {
2156                 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2157                         __func__);
2158                 return -EINVAL;
2159         }
2160
2161         if (!virtnet_validate_features(vdev))
2162                 return -EINVAL;
2163
2164         /* Find if host supports multiqueue virtio_net device */
2165         err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2166                                    struct virtio_net_config,
2167                                    max_virtqueue_pairs, &max_queue_pairs);
2168
2169         /* We need at least 2 queue's */
2170         if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2171             max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2172             !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2173                 max_queue_pairs = 1;
2174
2175         /* Allocate ourselves a network device with room for our info */
2176         dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
2177         if (!dev)
2178                 return -ENOMEM;
2179
2180         /* Set up network device as normal. */
2181         dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2182         dev->netdev_ops = &virtnet_netdev;
2183         dev->features = NETIF_F_HIGHDMA;
2184
2185         dev->ethtool_ops = &virtnet_ethtool_ops;
2186         SET_NETDEV_DEV(dev, &vdev->dev);
2187
2188         /* Do we support "hardware" checksums? */
2189         if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
2190                 /* This opens up the world of extra features. */
2191                 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
2192                 if (csum)
2193                         dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
2194
2195                 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
2196                         dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
2197                                 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2198                 }
2199                 /* Individual feature bits: what can host handle? */
2200                 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2201                         dev->hw_features |= NETIF_F_TSO;
2202                 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2203                         dev->hw_features |= NETIF_F_TSO6;
2204                 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2205                         dev->hw_features |= NETIF_F_TSO_ECN;
2206                 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2207                         dev->hw_features |= NETIF_F_UFO;
2208
2209                 dev->features |= NETIF_F_GSO_ROBUST;
2210
2211                 if (gso)
2212                         dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
2213                 /* (!csum && gso) case will be fixed by register_netdev() */
2214         }
2215         if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2216                 dev->features |= NETIF_F_RXCSUM;
2217
2218         dev->vlan_features = dev->features;
2219
2220         /* MTU range: 68 - 65535 */
2221         dev->min_mtu = MIN_MTU;
2222         dev->max_mtu = MAX_MTU;
2223
2224         /* Configuration may specify what MAC to use.  Otherwise random. */
2225         if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2226                 virtio_cread_bytes(vdev,
2227                                    offsetof(struct virtio_net_config, mac),
2228                                    dev->dev_addr, dev->addr_len);
2229         else
2230                 eth_hw_addr_random(dev);
2231
2232         /* Set up our device-specific information */
2233         vi = netdev_priv(dev);
2234         vi->dev = dev;
2235         vi->vdev = vdev;
2236         vdev->priv = vi;
2237         vi->stats = alloc_percpu(struct virtnet_stats);
2238         err = -ENOMEM;
2239         if (vi->stats == NULL)
2240                 goto free;
2241
2242         for_each_possible_cpu(i) {
2243                 struct virtnet_stats *virtnet_stats;
2244                 virtnet_stats = per_cpu_ptr(vi->stats, i);
2245                 u64_stats_init(&virtnet_stats->tx_syncp);
2246                 u64_stats_init(&virtnet_stats->rx_syncp);
2247         }
2248
2249         INIT_WORK(&vi->config_work, virtnet_config_changed_work);
2250
2251         /* If we can receive ANY GSO packets, we must allocate large ones. */
2252         if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2253             virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2254             virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2255             virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
2256                 vi->big_packets = true;
2257
2258         if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2259                 vi->mergeable_rx_bufs = true;
2260
2261         if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2262             virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2263                 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2264         else
2265                 vi->hdr_len = sizeof(struct virtio_net_hdr);
2266
2267         if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2268             virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2269                 vi->any_header_sg = true;
2270
2271         if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2272                 vi->has_cvq = true;
2273
2274         if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2275                 mtu = virtio_cread16(vdev,
2276                                      offsetof(struct virtio_net_config,
2277                                               mtu));
2278                 if (mtu < dev->min_mtu) {
2279                         __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2280                 } else {
2281                         dev->mtu = mtu;
2282                         dev->max_mtu = mtu;
2283                 }
2284         }
2285
2286         if (vi->any_header_sg)
2287                 dev->needed_headroom = vi->hdr_len;
2288
2289         /* Enable multiqueue by default */
2290         if (num_online_cpus() >= max_queue_pairs)
2291                 vi->curr_queue_pairs = max_queue_pairs;
2292         else
2293                 vi->curr_queue_pairs = num_online_cpus();
2294         vi->max_queue_pairs = max_queue_pairs;
2295
2296         /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
2297         err = init_vqs(vi);
2298         if (err)
2299                 goto free_stats;
2300
2301 #ifdef CONFIG_SYSFS
2302         if (vi->mergeable_rx_bufs)
2303                 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2304 #endif
2305         netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2306         netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
2307
2308         virtnet_init_settings(dev);
2309
2310         err = register_netdev(dev);
2311         if (err) {
2312                 pr_debug("virtio_net: registering device failed\n");
2313                 goto free_vqs;
2314         }
2315
2316         virtio_device_ready(vdev);
2317
2318         err = virtnet_cpu_notif_add(vi);
2319         if (err) {
2320                 pr_debug("virtio_net: registering cpu notifier failed\n");
2321                 goto free_unregister_netdev;
2322         }
2323
2324         rtnl_lock();
2325         virtnet_set_queues(vi, vi->curr_queue_pairs);
2326         rtnl_unlock();
2327
2328         /* Assume link up if device can't report link status,
2329            otherwise get link status from config. */
2330         if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2331                 netif_carrier_off(dev);
2332                 schedule_work(&vi->config_work);
2333         } else {
2334                 vi->status = VIRTIO_NET_S_LINK_UP;
2335                 netif_carrier_on(dev);
2336         }
2337
2338         pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2339                  dev->name, max_queue_pairs);
2340
2341         return 0;
2342
2343 free_unregister_netdev:
2344         vi->vdev->config->reset(vdev);
2345
2346         unregister_netdev(dev);
2347 free_vqs:
2348         cancel_delayed_work_sync(&vi->refill);
2349         free_receive_page_frags(vi);
2350         virtnet_del_vqs(vi);
2351 free_stats:
2352         free_percpu(vi->stats);
2353 free:
2354         free_netdev(dev);
2355         return err;
2356 }
2357
2358 static void remove_vq_common(struct virtnet_info *vi)
2359 {
2360         vi->vdev->config->reset(vi->vdev);
2361
2362         /* Free unused buffers in both send and recv, if any. */
2363         free_unused_bufs(vi);
2364
2365         free_receive_bufs(vi);
2366
2367         free_receive_page_frags(vi);
2368
2369         virtnet_del_vqs(vi);
2370 }
2371
2372 static void virtnet_remove(struct virtio_device *vdev)
2373 {
2374         struct virtnet_info *vi = vdev->priv;
2375
2376         virtnet_cpu_notif_remove(vi);
2377
2378         /* Make sure no work handler is accessing the device. */
2379         flush_work(&vi->config_work);
2380
2381         unregister_netdev(vi->dev);
2382
2383         remove_vq_common(vi);
2384
2385         free_percpu(vi->stats);
2386         free_netdev(vi->dev);
2387 }
2388
2389 #ifdef CONFIG_PM_SLEEP
2390 static int virtnet_freeze(struct virtio_device *vdev)
2391 {
2392         struct virtnet_info *vi = vdev->priv;
2393         int i;
2394
2395         virtnet_cpu_notif_remove(vi);
2396
2397         /* Make sure no work handler is accessing the device */
2398         flush_work(&vi->config_work);
2399
2400         netif_device_detach(vi->dev);
2401         cancel_delayed_work_sync(&vi->refill);
2402
2403         if (netif_running(vi->dev)) {
2404                 for (i = 0; i < vi->max_queue_pairs; i++)
2405                         napi_disable(&vi->rq[i].napi);
2406         }
2407
2408         remove_vq_common(vi);
2409
2410         return 0;
2411 }
2412
2413 static int virtnet_restore(struct virtio_device *vdev)
2414 {
2415         struct virtnet_info *vi = vdev->priv;
2416         int err, i;
2417
2418         err = init_vqs(vi);
2419         if (err)
2420                 return err;
2421
2422         virtio_device_ready(vdev);
2423
2424         if (netif_running(vi->dev)) {
2425                 for (i = 0; i < vi->curr_queue_pairs; i++)
2426                         if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2427                                 schedule_delayed_work(&vi->refill, 0);
2428
2429                 for (i = 0; i < vi->max_queue_pairs; i++)
2430                         virtnet_napi_enable(&vi->rq[i]);
2431         }
2432
2433         netif_device_attach(vi->dev);
2434
2435         rtnl_lock();
2436         virtnet_set_queues(vi, vi->curr_queue_pairs);
2437         rtnl_unlock();
2438
2439         err = virtnet_cpu_notif_add(vi);
2440         if (err)
2441                 return err;
2442
2443         return 0;
2444 }
2445 #endif
2446
2447 static struct virtio_device_id id_table[] = {
2448         { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2449         { 0 },
2450 };
2451
2452 #define VIRTNET_FEATURES \
2453         VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2454         VIRTIO_NET_F_MAC, \
2455         VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2456         VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2457         VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2458         VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2459         VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2460         VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2461         VIRTIO_NET_F_CTRL_MAC_ADDR, \
2462         VIRTIO_NET_F_MTU
2463
2464 static unsigned int features[] = {
2465         VIRTNET_FEATURES,
2466 };
2467
2468 static unsigned int features_legacy[] = {
2469         VIRTNET_FEATURES,
2470         VIRTIO_NET_F_GSO,
2471         VIRTIO_F_ANY_LAYOUT,
2472 };
2473
2474 static struct virtio_driver virtio_net_driver = {
2475         .feature_table = features,
2476         .feature_table_size = ARRAY_SIZE(features),
2477         .feature_table_legacy = features_legacy,
2478         .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
2479         .driver.name =  KBUILD_MODNAME,
2480         .driver.owner = THIS_MODULE,
2481         .id_table =     id_table,
2482         .probe =        virtnet_probe,
2483         .remove =       virtnet_remove,
2484         .config_changed = virtnet_config_changed,
2485 #ifdef CONFIG_PM_SLEEP
2486         .freeze =       virtnet_freeze,
2487         .restore =      virtnet_restore,
2488 #endif
2489 };
2490
2491 static __init int virtio_net_driver_init(void)
2492 {
2493         int ret;
2494
2495         ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
2496                                       virtnet_cpu_online,
2497                                       virtnet_cpu_down_prep);
2498         if (ret < 0)
2499                 goto out;
2500         virtionet_online = ret;
2501         ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
2502                                       NULL, virtnet_cpu_dead);
2503         if (ret)
2504                 goto err_dead;
2505
2506         ret = register_virtio_driver(&virtio_net_driver);
2507         if (ret)
2508                 goto err_virtio;
2509         return 0;
2510 err_virtio:
2511         cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2512 err_dead:
2513         cpuhp_remove_multi_state(virtionet_online);
2514 out:
2515         return ret;
2516 }
2517 module_init(virtio_net_driver_init);
2518
2519 static __exit void virtio_net_driver_exit(void)
2520 {
2521         cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2522         cpuhp_remove_multi_state(virtionet_online);
2523         unregister_virtio_driver(&virtio_net_driver);
2524 }
2525 module_exit(virtio_net_driver_exit);
2526
2527 MODULE_DEVICE_TABLE(virtio, id_table);
2528 MODULE_DESCRIPTION("Virtio network driver");
2529 MODULE_LICENSE("GPL");