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