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