Merge branch 'ibmvnic-Move-sub-crq-init-out-of-interrupt-context'
[linux-block.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
3161e453
RR
1000static void refill_work(struct work_struct *work)
1001{
e9d7417b
JW
1002 struct virtnet_info *vi =
1003 container_of(work, struct virtnet_info, refill.work);
3161e453 1004 bool still_empty;
986a4f4d
JW
1005 int i;
1006
55257d72 1007 for (i = 0; i < vi->curr_queue_pairs; i++) {
986a4f4d 1008 struct receive_queue *rq = &vi->rq[i];
3161e453 1009
986a4f4d 1010 napi_disable(&rq->napi);
946fa564 1011 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
e4e8452a 1012 virtnet_napi_enable(rq->vq, &rq->napi);
3161e453 1013
986a4f4d
JW
1014 /* In theory, this can happen: if we don't get any buffers in
1015 * we will *never* try to fill again.
1016 */
1017 if (still_empty)
1018 schedule_delayed_work(&vi->refill, HZ/2);
1019 }
3161e453
RR
1020}
1021
2ffa7598 1022static int virtnet_receive(struct receive_queue *rq, int budget)
296f96fc 1023{
e9d7417b 1024 struct virtnet_info *vi = rq->vq->vdev->priv;
61845d20 1025 unsigned int len, received = 0, bytes = 0;
9ab86bbc 1026 void *buf;
61845d20 1027 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
296f96fc 1028
296f96fc 1029 while (received < budget &&
e9d7417b 1030 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
61845d20 1031 bytes += receive_buf(vi, rq, buf, len);
296f96fc
RR
1032 received++;
1033 }
1034
be121f46 1035 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
946fa564 1036 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
3b07e9ca 1037 schedule_delayed_work(&vi->refill, 0);
3161e453 1038 }
296f96fc 1039
61845d20
JW
1040 u64_stats_update_begin(&stats->rx_syncp);
1041 stats->rx_bytes += bytes;
1042 stats->rx_packets += received;
1043 u64_stats_update_end(&stats->rx_syncp);
1044
2ffa7598
JW
1045 return received;
1046}
1047
ea7735d9
WB
1048static void free_old_xmit_skbs(struct send_queue *sq)
1049{
1050 struct sk_buff *skb;
1051 unsigned int len;
1052 struct virtnet_info *vi = sq->vq->vdev->priv;
1053 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
1054 unsigned int packets = 0;
1055 unsigned int bytes = 0;
1056
1057 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1058 pr_debug("Sent skb %p\n", skb);
1059
1060 bytes += skb->len;
1061 packets++;
1062
1063 dev_kfree_skb_any(skb);
1064 }
1065
1066 /* Avoid overhead when no packets have been processed
1067 * happens when called speculatively from start_xmit.
1068 */
1069 if (!packets)
1070 return;
1071
1072 u64_stats_update_begin(&stats->tx_syncp);
1073 stats->tx_bytes += bytes;
1074 stats->tx_packets += packets;
1075 u64_stats_update_end(&stats->tx_syncp);
1076}
1077
7b0411ef
WB
1078static void virtnet_poll_cleantx(struct receive_queue *rq)
1079{
1080 struct virtnet_info *vi = rq->vq->vdev->priv;
1081 unsigned int index = vq2rxq(rq->vq);
1082 struct send_queue *sq = &vi->sq[index];
1083 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
1084
1085 if (!sq->napi.weight)
1086 return;
1087
1088 if (__netif_tx_trylock(txq)) {
1089 free_old_xmit_skbs(sq);
1090 __netif_tx_unlock(txq);
1091 }
1092
1093 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1094 netif_tx_wake_queue(txq);
1095}
1096
2ffa7598
JW
1097static int virtnet_poll(struct napi_struct *napi, int budget)
1098{
1099 struct receive_queue *rq =
1100 container_of(napi, struct receive_queue, napi);
e4e8452a 1101 unsigned int received;
2ffa7598 1102
7b0411ef
WB
1103 virtnet_poll_cleantx(rq);
1104
faadb05f 1105 received = virtnet_receive(rq, budget);
2ffa7598 1106
8329d98e 1107 /* Out of packets? */
e4e8452a
WB
1108 if (received < budget)
1109 virtqueue_napi_complete(napi, rq->vq, received);
296f96fc
RR
1110
1111 return received;
1112}
1113
986a4f4d
JW
1114static int virtnet_open(struct net_device *dev)
1115{
1116 struct virtnet_info *vi = netdev_priv(dev);
1117 int i;
1118
e4166625
JW
1119 for (i = 0; i < vi->max_queue_pairs; i++) {
1120 if (i < vi->curr_queue_pairs)
1121 /* Make sure we have some buffers: if oom use wq. */
946fa564 1122 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
e4166625 1123 schedule_delayed_work(&vi->refill, 0);
e4e8452a 1124 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
b92f1e67 1125 virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
986a4f4d
JW
1126 }
1127
1128 return 0;
1129}
1130
b92f1e67
WB
1131static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1132{
1133 struct send_queue *sq = container_of(napi, struct send_queue, napi);
1134 struct virtnet_info *vi = sq->vq->vdev->priv;
1135 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1136
1137 __netif_tx_lock(txq, raw_smp_processor_id());
1138 free_old_xmit_skbs(sq);
1139 __netif_tx_unlock(txq);
1140
1141 virtqueue_napi_complete(napi, sq->vq, 0);
1142
1143 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1144 netif_tx_wake_queue(txq);
1145
1146 return 0;
1147}
1148
e9d7417b 1149static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
296f96fc 1150{
012873d0 1151 struct virtio_net_hdr_mrg_rxbuf *hdr;
296f96fc 1152 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
e9d7417b 1153 struct virtnet_info *vi = sq->vq->vdev->priv;
7bedc7dc 1154 unsigned num_sg;
012873d0 1155 unsigned hdr_len = vi->hdr_len;
e7428e95 1156 bool can_push;
296f96fc 1157
e174961c 1158 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
e7428e95
MT
1159
1160 can_push = vi->any_header_sg &&
1161 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1162 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1163 /* Even if we can, don't push here yet as this would skew
1164 * csum_start offset below. */
1165 if (can_push)
012873d0 1166 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
e7428e95
MT
1167 else
1168 hdr = skb_vnet_hdr(skb);
296f96fc 1169
e858fae2 1170 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
6391a448 1171 virtio_is_little_endian(vi->vdev), false))
e858fae2 1172 BUG();
296f96fc 1173
3f2c31d9 1174 if (vi->mergeable_rx_bufs)
012873d0 1175 hdr->num_buffers = 0;
3f2c31d9 1176
547c890c 1177 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
e7428e95
MT
1178 if (can_push) {
1179 __skb_push(skb, hdr_len);
1180 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1181 /* Pull header back to avoid skew in tx bytes calculations. */
1182 __skb_pull(skb, hdr_len);
1183 } else {
1184 sg_set_buf(sq->sg, hdr, hdr_len);
1185 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
1186 }
9dc7b9e4 1187 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
11a3a154
RR
1188}
1189
424efe9c 1190static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
99ffc696
RR
1191{
1192 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d
JW
1193 int qnum = skb_get_queue_mapping(skb);
1194 struct send_queue *sq = &vi->sq[qnum];
9ed4cb07 1195 int err;
4b7fd2e6
MT
1196 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1197 bool kick = !skb->xmit_more;
b92f1e67 1198 bool use_napi = sq->napi.weight;
2cb9c6ba 1199
2cb9c6ba 1200 /* Free up any pending old buffers before queueing new ones. */
e9d7417b 1201 free_old_xmit_skbs(sq);
99ffc696 1202
bdb12e0d
WB
1203 if (use_napi && kick)
1204 virtqueue_enable_cb_delayed(sq->vq);
1205
074c3582
JK
1206 /* timestamp packet in software */
1207 skb_tx_timestamp(skb);
1208
03f191ba 1209 /* Try to transmit */
b7dfde95 1210 err = xmit_skb(sq, skb);
48925e37 1211
9ed4cb07 1212 /* This should not happen! */
681daee2 1213 if (unlikely(err)) {
9ed4cb07
RR
1214 dev->stats.tx_fifo_errors++;
1215 if (net_ratelimit())
1216 dev_warn(&dev->dev,
b7dfde95 1217 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
58eba97d 1218 dev->stats.tx_dropped++;
85e94525 1219 dev_kfree_skb_any(skb);
58eba97d 1220 return NETDEV_TX_OK;
296f96fc 1221 }
03f191ba 1222
48925e37 1223 /* Don't wait up for transmitted skbs to be freed. */
b92f1e67
WB
1224 if (!use_napi) {
1225 skb_orphan(skb);
1226 nf_reset(skb);
1227 }
48925e37 1228
60302ff6
MT
1229 /* If running out of space, stop queue to avoid getting packets that we
1230 * are then unable to transmit.
1231 * An alternative would be to force queuing layer to requeue the skb by
1232 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
1233 * returned in a normal path of operation: it means that driver is not
1234 * maintaining the TX queue stop/start state properly, and causes
1235 * the stack to do a non-trivial amount of useless work.
1236 * Since most packets only take 1 or 2 ring slots, stopping the queue
1237 * early means 16 slots are typically wasted.
d631b94e 1238 */
b7dfde95 1239 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
986a4f4d 1240 netif_stop_subqueue(dev, qnum);
b92f1e67
WB
1241 if (!use_napi &&
1242 unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
48925e37 1243 /* More just got used, free them then recheck. */
b7dfde95
LT
1244 free_old_xmit_skbs(sq);
1245 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
986a4f4d 1246 netif_start_subqueue(dev, qnum);
e9d7417b 1247 virtqueue_disable_cb(sq->vq);
48925e37
RR
1248 }
1249 }
99ffc696 1250 }
48925e37 1251
4b7fd2e6 1252 if (kick || netif_xmit_stopped(txq))
0b725a2c 1253 virtqueue_kick(sq->vq);
296f96fc 1254
0b725a2c 1255 return NETDEV_TX_OK;
c223a078
DM
1256}
1257
40cbfc37
AK
1258/*
1259 * Send command via the control virtqueue and check status. Commands
1260 * supported by the hypervisor, as indicated by feature bits, should
788a8b6d 1261 * never fail unless improperly formatted.
40cbfc37
AK
1262 */
1263static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
d24bae32 1264 struct scatterlist *out)
40cbfc37 1265{
f7bc9594 1266 struct scatterlist *sgs[4], hdr, stat;
d24bae32 1267 unsigned out_num = 0, tmp;
40cbfc37
AK
1268
1269 /* Caller should know better */
f7bc9594 1270 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
40cbfc37 1271
2ac46030
MT
1272 vi->ctrl_status = ~0;
1273 vi->ctrl_hdr.class = class;
1274 vi->ctrl_hdr.cmd = cmd;
f7bc9594 1275 /* Add header */
2ac46030 1276 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
f7bc9594 1277 sgs[out_num++] = &hdr;
40cbfc37 1278
f7bc9594
RR
1279 if (out)
1280 sgs[out_num++] = out;
40cbfc37 1281
f7bc9594 1282 /* Add return status. */
2ac46030 1283 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
d24bae32 1284 sgs[out_num] = &stat;
40cbfc37 1285
d24bae32 1286 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
a7c58146 1287 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
40cbfc37 1288
67975901 1289 if (unlikely(!virtqueue_kick(vi->cvq)))
2ac46030 1290 return vi->ctrl_status == VIRTIO_NET_OK;
40cbfc37
AK
1291
1292 /* Spin for a response, the kick causes an ioport write, trapping
1293 * into the hypervisor, so the request should be handled immediately.
1294 */
047b9b94
HG
1295 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1296 !virtqueue_is_broken(vi->cvq))
40cbfc37
AK
1297 cpu_relax();
1298
2ac46030 1299 return vi->ctrl_status == VIRTIO_NET_OK;
40cbfc37
AK
1300}
1301
9c46f6d4
AW
1302static int virtnet_set_mac_address(struct net_device *dev, void *p)
1303{
1304 struct virtnet_info *vi = netdev_priv(dev);
1305 struct virtio_device *vdev = vi->vdev;
f2f2c8b4 1306 int ret;
e37e2ff3 1307 struct sockaddr *addr;
7e58d5ae 1308 struct scatterlist sg;
9c46f6d4 1309
801822d1 1310 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
e37e2ff3
AL
1311 if (!addr)
1312 return -ENOMEM;
e37e2ff3
AL
1313
1314 ret = eth_prepare_mac_addr_change(dev, addr);
f2f2c8b4 1315 if (ret)
e37e2ff3 1316 goto out;
9c46f6d4 1317
7e58d5ae
AK
1318 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
1319 sg_init_one(&sg, addr->sa_data, dev->addr_len);
1320 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
d24bae32 1321 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
7e58d5ae
AK
1322 dev_warn(&vdev->dev,
1323 "Failed to set mac address by vq command.\n");
e37e2ff3
AL
1324 ret = -EINVAL;
1325 goto out;
7e58d5ae 1326 }
7e93a02f
MT
1327 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
1328 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
855e0c52
RR
1329 unsigned int i;
1330
1331 /* Naturally, this has an atomicity problem. */
1332 for (i = 0; i < dev->addr_len; i++)
1333 virtio_cwrite8(vdev,
1334 offsetof(struct virtio_net_config, mac) +
1335 i, addr->sa_data[i]);
7e58d5ae
AK
1336 }
1337
1338 eth_commit_mac_addr_change(dev, p);
e37e2ff3 1339 ret = 0;
9c46f6d4 1340
e37e2ff3
AL
1341out:
1342 kfree(addr);
1343 return ret;
9c46f6d4
AW
1344}
1345
bc1f4470 1346static void virtnet_stats(struct net_device *dev,
1347 struct rtnl_link_stats64 *tot)
3fa2a1df 1348{
1349 struct virtnet_info *vi = netdev_priv(dev);
1350 int cpu;
1351 unsigned int start;
1352
1353 for_each_possible_cpu(cpu) {
58472a76 1354 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
3fa2a1df 1355 u64 tpackets, tbytes, rpackets, rbytes;
1356
1357 do {
57a7744e 1358 start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
3fa2a1df 1359 tpackets = stats->tx_packets;
1360 tbytes = stats->tx_bytes;
57a7744e 1361 } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
83a27052
ED
1362
1363 do {
57a7744e 1364 start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
3fa2a1df 1365 rpackets = stats->rx_packets;
1366 rbytes = stats->rx_bytes;
57a7744e 1367 } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
3fa2a1df 1368
1369 tot->rx_packets += rpackets;
1370 tot->tx_packets += tpackets;
1371 tot->rx_bytes += rbytes;
1372 tot->tx_bytes += tbytes;
1373 }
1374
1375 tot->tx_dropped = dev->stats.tx_dropped;
021ac8d3 1376 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
3fa2a1df 1377 tot->rx_dropped = dev->stats.rx_dropped;
1378 tot->rx_length_errors = dev->stats.rx_length_errors;
1379 tot->rx_frame_errors = dev->stats.rx_frame_errors;
3fa2a1df 1380}
1381
da74e89d
AS
1382#ifdef CONFIG_NET_POLL_CONTROLLER
1383static void virtnet_netpoll(struct net_device *dev)
1384{
1385 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1386 int i;
da74e89d 1387
986a4f4d
JW
1388 for (i = 0; i < vi->curr_queue_pairs; i++)
1389 napi_schedule(&vi->rq[i].napi);
da74e89d
AS
1390}
1391#endif
1392
586d17c5
JW
1393static void virtnet_ack_link_announce(struct virtnet_info *vi)
1394{
1395 rtnl_lock();
1396 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
d24bae32 1397 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
586d17c5
JW
1398 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1399 rtnl_unlock();
1400}
1401
47315329 1402static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
986a4f4d
JW
1403{
1404 struct scatterlist sg;
986a4f4d
JW
1405 struct net_device *dev = vi->dev;
1406
1407 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1408 return 0;
1409
a725ee3e
AL
1410 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1411 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
986a4f4d
JW
1412
1413 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
d24bae32 1414 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
986a4f4d
JW
1415 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1416 queue_pairs);
1417 return -EINVAL;
55257d72 1418 } else {
986a4f4d 1419 vi->curr_queue_pairs = queue_pairs;
35ed159b
JW
1420 /* virtnet_open() will refill when device is going to up. */
1421 if (dev->flags & IFF_UP)
1422 schedule_delayed_work(&vi->refill, 0);
55257d72 1423 }
986a4f4d
JW
1424
1425 return 0;
1426}
1427
47315329
JF
1428static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1429{
1430 int err;
1431
1432 rtnl_lock();
1433 err = _virtnet_set_queues(vi, queue_pairs);
1434 rtnl_unlock();
1435 return err;
1436}
1437
296f96fc
RR
1438static int virtnet_close(struct net_device *dev)
1439{
1440 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1441 int i;
296f96fc 1442
b2baed69
RR
1443 /* Make sure refill_work doesn't re-enable napi! */
1444 cancel_delayed_work_sync(&vi->refill);
986a4f4d 1445
b92f1e67 1446 for (i = 0; i < vi->max_queue_pairs; i++) {
986a4f4d 1447 napi_disable(&vi->rq[i].napi);
b92f1e67
WB
1448 napi_disable(&vi->sq[i].napi);
1449 }
296f96fc 1450
296f96fc
RR
1451 return 0;
1452}
1453
2af7698e
AW
1454static void virtnet_set_rx_mode(struct net_device *dev)
1455{
1456 struct virtnet_info *vi = netdev_priv(dev);
f565a7c2 1457 struct scatterlist sg[2];
f565a7c2 1458 struct virtio_net_ctrl_mac *mac_data;
ccffad25 1459 struct netdev_hw_addr *ha;
32e7bfc4 1460 int uc_count;
4cd24eaf 1461 int mc_count;
f565a7c2
AW
1462 void *buf;
1463 int i;
2af7698e 1464
788a8b6d 1465 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2af7698e
AW
1466 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1467 return;
1468
2ac46030
MT
1469 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
1470 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2af7698e 1471
2ac46030 1472 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
2af7698e
AW
1473
1474 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
d24bae32 1475 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2af7698e 1476 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2ac46030 1477 vi->ctrl_promisc ? "en" : "dis");
2af7698e 1478
2ac46030 1479 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
2af7698e
AW
1480
1481 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
d24bae32 1482 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2af7698e 1483 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2ac46030 1484 vi->ctrl_allmulti ? "en" : "dis");
f565a7c2 1485
32e7bfc4 1486 uc_count = netdev_uc_count(dev);
4cd24eaf 1487 mc_count = netdev_mc_count(dev);
f565a7c2 1488 /* MAC filter - use one buffer for both lists */
4cd24eaf
JP
1489 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1490 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1491 mac_data = buf;
e68ed8f0 1492 if (!buf)
f565a7c2 1493 return;
f565a7c2 1494
23e258e1
AW
1495 sg_init_table(sg, 2);
1496
f565a7c2 1497 /* Store the unicast list and count in the front of the buffer */
fdd819b2 1498 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
ccffad25 1499 i = 0;
32e7bfc4 1500 netdev_for_each_uc_addr(ha, dev)
ccffad25 1501 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1502
1503 sg_set_buf(&sg[0], mac_data,
32e7bfc4 1504 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
f565a7c2
AW
1505
1506 /* multicast list and count fill the end */
32e7bfc4 1507 mac_data = (void *)&mac_data->macs[uc_count][0];
f565a7c2 1508
fdd819b2 1509 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
567ec874 1510 i = 0;
22bedad3
JP
1511 netdev_for_each_mc_addr(ha, dev)
1512 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1513
1514 sg_set_buf(&sg[1], mac_data,
4cd24eaf 1515 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
f565a7c2
AW
1516
1517 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
d24bae32 1518 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
99e872ae 1519 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
f565a7c2
AW
1520
1521 kfree(buf);
2af7698e
AW
1522}
1523
80d5c368
PM
1524static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1525 __be16 proto, u16 vid)
0bde9569
AW
1526{
1527 struct virtnet_info *vi = netdev_priv(dev);
1528 struct scatterlist sg;
1529
a725ee3e
AL
1530 vi->ctrl_vid = vid;
1531 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
0bde9569
AW
1532
1533 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
d24bae32 1534 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
0bde9569 1535 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8e586137 1536 return 0;
0bde9569
AW
1537}
1538
80d5c368
PM
1539static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1540 __be16 proto, u16 vid)
0bde9569
AW
1541{
1542 struct virtnet_info *vi = netdev_priv(dev);
1543 struct scatterlist sg;
1544
a725ee3e
AL
1545 vi->ctrl_vid = vid;
1546 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
0bde9569
AW
1547
1548 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
d24bae32 1549 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
0bde9569 1550 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8e586137 1551 return 0;
0bde9569
AW
1552}
1553
8898c21c 1554static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
986a4f4d
JW
1555{
1556 int i;
1557
8898c21c
WG
1558 if (vi->affinity_hint_set) {
1559 for (i = 0; i < vi->max_queue_pairs; i++) {
47be2479
WG
1560 virtqueue_set_affinity(vi->rq[i].vq, -1);
1561 virtqueue_set_affinity(vi->sq[i].vq, -1);
1562 }
1563
8898c21c
WG
1564 vi->affinity_hint_set = false;
1565 }
8898c21c 1566}
47be2479 1567
8898c21c
WG
1568static void virtnet_set_affinity(struct virtnet_info *vi)
1569{
1570 int i;
1571 int cpu;
986a4f4d
JW
1572
1573 /* In multiqueue mode, when the number of cpu is equal to the number of
1574 * queue pairs, we let the queue pairs to be private to one cpu by
1575 * setting the affinity hint to eliminate the contention.
1576 */
8898c21c
WG
1577 if (vi->curr_queue_pairs == 1 ||
1578 vi->max_queue_pairs != num_online_cpus()) {
1579 virtnet_clean_affinity(vi, -1);
1580 return;
986a4f4d
JW
1581 }
1582
8898c21c
WG
1583 i = 0;
1584 for_each_online_cpu(cpu) {
986a4f4d
JW
1585 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1586 virtqueue_set_affinity(vi->sq[i].vq, cpu);
9bb8ca86 1587 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
8898c21c 1588 i++;
986a4f4d
JW
1589 }
1590
8898c21c 1591 vi->affinity_hint_set = true;
986a4f4d
JW
1592}
1593
8017c279 1594static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
8de4b2f3 1595{
8017c279
SAS
1596 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1597 node);
1598 virtnet_set_affinity(vi);
1599 return 0;
1600}
8de4b2f3 1601
8017c279
SAS
1602static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
1603{
1604 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1605 node_dead);
1606 virtnet_set_affinity(vi);
1607 return 0;
1608}
3ab098df 1609
8017c279
SAS
1610static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
1611{
1612 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
1613 node);
1614
1615 virtnet_clean_affinity(vi, cpu);
1616 return 0;
1617}
1618
1619static enum cpuhp_state virtionet_online;
1620
1621static int virtnet_cpu_notif_add(struct virtnet_info *vi)
1622{
1623 int ret;
1624
1625 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
1626 if (ret)
1627 return ret;
1628 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1629 &vi->node_dead);
1630 if (!ret)
1631 return ret;
1632 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1633 return ret;
1634}
1635
1636static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
1637{
1638 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
1639 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
1640 &vi->node_dead);
986a4f4d
JW
1641}
1642
8f9f4668
RJ
1643static void virtnet_get_ringparam(struct net_device *dev,
1644 struct ethtool_ringparam *ring)
1645{
1646 struct virtnet_info *vi = netdev_priv(dev);
1647
986a4f4d
JW
1648 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1649 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
8f9f4668
RJ
1650 ring->rx_pending = ring->rx_max_pending;
1651 ring->tx_pending = ring->tx_max_pending;
8f9f4668
RJ
1652}
1653
66846048
RJ
1654
1655static void virtnet_get_drvinfo(struct net_device *dev,
1656 struct ethtool_drvinfo *info)
1657{
1658 struct virtnet_info *vi = netdev_priv(dev);
1659 struct virtio_device *vdev = vi->vdev;
1660
1661 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1662 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1663 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1664
1665}
1666
d73bcd2c
JW
1667/* TODO: Eliminate OOO packets during switching */
1668static int virtnet_set_channels(struct net_device *dev,
1669 struct ethtool_channels *channels)
1670{
1671 struct virtnet_info *vi = netdev_priv(dev);
1672 u16 queue_pairs = channels->combined_count;
1673 int err;
1674
1675 /* We don't support separate rx/tx channels.
1676 * We don't allow setting 'other' channels.
1677 */
1678 if (channels->rx_count || channels->tx_count || channels->other_count)
1679 return -EINVAL;
1680
c18e9cd6 1681 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
d73bcd2c
JW
1682 return -EINVAL;
1683
f600b690
JF
1684 /* For now we don't support modifying channels while XDP is loaded
1685 * also when XDP is loaded all RX queues have XDP programs so we only
1686 * need to check a single RX queue.
1687 */
1688 if (vi->rq[0].xdp_prog)
1689 return -EINVAL;
1690
47be2479 1691 get_online_cpus();
47315329 1692 err = _virtnet_set_queues(vi, queue_pairs);
d73bcd2c
JW
1693 if (!err) {
1694 netif_set_real_num_tx_queues(dev, queue_pairs);
1695 netif_set_real_num_rx_queues(dev, queue_pairs);
1696
8898c21c 1697 virtnet_set_affinity(vi);
d73bcd2c 1698 }
47be2479 1699 put_online_cpus();
d73bcd2c
JW
1700
1701 return err;
1702}
1703
1704static void virtnet_get_channels(struct net_device *dev,
1705 struct ethtool_channels *channels)
1706{
1707 struct virtnet_info *vi = netdev_priv(dev);
1708
1709 channels->combined_count = vi->curr_queue_pairs;
1710 channels->max_combined = vi->max_queue_pairs;
1711 channels->max_other = 0;
1712 channels->rx_count = 0;
1713 channels->tx_count = 0;
1714 channels->other_count = 0;
1715}
1716
16032be5 1717/* Check if the user is trying to change anything besides speed/duplex */
ebb6b4b1
PR
1718static bool
1719virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
16032be5 1720{
ebb6b4b1
PR
1721 struct ethtool_link_ksettings diff1 = *cmd;
1722 struct ethtool_link_ksettings diff2 = {};
16032be5 1723
0cf3ace9
NA
1724 /* cmd is always set so we need to clear it, validate the port type
1725 * and also without autonegotiation we can ignore advertising
1726 */
ebb6b4b1
PR
1727 diff1.base.speed = 0;
1728 diff2.base.port = PORT_OTHER;
1729 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
1730 diff1.base.duplex = 0;
1731 diff1.base.cmd = 0;
1732 diff1.base.link_mode_masks_nwords = 0;
1733
1734 return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
1735 bitmap_empty(diff1.link_modes.supported,
1736 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1737 bitmap_empty(diff1.link_modes.advertising,
1738 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1739 bitmap_empty(diff1.link_modes.lp_advertising,
1740 __ETHTOOL_LINK_MODE_MASK_NBITS);
16032be5
NA
1741}
1742
ebb6b4b1
PR
1743static int virtnet_set_link_ksettings(struct net_device *dev,
1744 const struct ethtool_link_ksettings *cmd)
16032be5
NA
1745{
1746 struct virtnet_info *vi = netdev_priv(dev);
1747 u32 speed;
1748
ebb6b4b1 1749 speed = cmd->base.speed;
16032be5
NA
1750 /* don't allow custom speed and duplex */
1751 if (!ethtool_validate_speed(speed) ||
ebb6b4b1 1752 !ethtool_validate_duplex(cmd->base.duplex) ||
16032be5
NA
1753 !virtnet_validate_ethtool_cmd(cmd))
1754 return -EINVAL;
1755 vi->speed = speed;
ebb6b4b1 1756 vi->duplex = cmd->base.duplex;
16032be5
NA
1757
1758 return 0;
1759}
1760
ebb6b4b1
PR
1761static int virtnet_get_link_ksettings(struct net_device *dev,
1762 struct ethtool_link_ksettings *cmd)
16032be5
NA
1763{
1764 struct virtnet_info *vi = netdev_priv(dev);
1765
ebb6b4b1
PR
1766 cmd->base.speed = vi->speed;
1767 cmd->base.duplex = vi->duplex;
1768 cmd->base.port = PORT_OTHER;
16032be5
NA
1769
1770 return 0;
1771}
1772
1773static void virtnet_init_settings(struct net_device *dev)
1774{
1775 struct virtnet_info *vi = netdev_priv(dev);
1776
1777 vi->speed = SPEED_UNKNOWN;
1778 vi->duplex = DUPLEX_UNKNOWN;
1779}
1780
0fc0b732 1781static const struct ethtool_ops virtnet_ethtool_ops = {
66846048 1782 .get_drvinfo = virtnet_get_drvinfo,
9f4d26d0 1783 .get_link = ethtool_op_get_link,
8f9f4668 1784 .get_ringparam = virtnet_get_ringparam,
d73bcd2c
JW
1785 .set_channels = virtnet_set_channels,
1786 .get_channels = virtnet_get_channels,
074c3582 1787 .get_ts_info = ethtool_op_get_ts_info,
ebb6b4b1
PR
1788 .get_link_ksettings = virtnet_get_link_ksettings,
1789 .set_link_ksettings = virtnet_set_link_ksettings,
a9ea3fc6
HX
1790};
1791
9fe7bfce
JF
1792static void virtnet_freeze_down(struct virtio_device *vdev)
1793{
1794 struct virtnet_info *vi = vdev->priv;
1795 int i;
1796
1797 /* Make sure no work handler is accessing the device */
1798 flush_work(&vi->config_work);
1799
1800 netif_device_detach(vi->dev);
1801 cancel_delayed_work_sync(&vi->refill);
1802
1803 if (netif_running(vi->dev)) {
b92f1e67 1804 for (i = 0; i < vi->max_queue_pairs; i++) {
9fe7bfce 1805 napi_disable(&vi->rq[i].napi);
b92f1e67
WB
1806 napi_disable(&vi->sq[i].napi);
1807 }
9fe7bfce
JF
1808 }
1809}
1810
1811static int init_vqs(struct virtnet_info *vi);
2de2f7f4 1812static void _remove_vq_common(struct virtnet_info *vi);
9fe7bfce
JF
1813
1814static int virtnet_restore_up(struct virtio_device *vdev)
1815{
1816 struct virtnet_info *vi = vdev->priv;
1817 int err, i;
1818
1819 err = init_vqs(vi);
1820 if (err)
1821 return err;
1822
1823 virtio_device_ready(vdev);
1824
1825 if (netif_running(vi->dev)) {
1826 for (i = 0; i < vi->curr_queue_pairs; i++)
1827 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1828 schedule_delayed_work(&vi->refill, 0);
1829
b92f1e67 1830 for (i = 0; i < vi->max_queue_pairs; i++) {
e4e8452a 1831 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
b92f1e67
WB
1832 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
1833 &vi->sq[i].napi);
1834 }
9fe7bfce
JF
1835 }
1836
1837 netif_device_attach(vi->dev);
1838 return err;
1839}
1840
017b29c3 1841static int virtnet_reset(struct virtnet_info *vi, int curr_qp, int xdp_qp)
2de2f7f4
JF
1842{
1843 struct virtio_device *dev = vi->vdev;
1844 int ret;
1845
1846 virtio_config_disable(dev);
1847 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
1848 virtnet_freeze_down(dev);
1849 _remove_vq_common(vi);
1850
1851 dev->config->reset(dev);
1852 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
1853 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
1854
1855 ret = virtio_finalize_features(dev);
1856 if (ret)
1857 goto err;
1858
017b29c3 1859 vi->xdp_queue_pairs = xdp_qp;
2de2f7f4
JF
1860 ret = virtnet_restore_up(dev);
1861 if (ret)
1862 goto err;
017b29c3 1863 ret = _virtnet_set_queues(vi, curr_qp);
2de2f7f4
JF
1864 if (ret)
1865 goto err;
1866
1867 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1868 virtio_config_enable(dev);
1869 return 0;
1870err:
1871 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
1872 return ret;
1873}
1874
f600b690
JF
1875static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog)
1876{
1877 unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1878 struct virtnet_info *vi = netdev_priv(dev);
1879 struct bpf_prog *old_prog;
017b29c3 1880 u16 xdp_qp = 0, curr_qp;
672aafd5 1881 int i, err;
f600b690
JF
1882
1883 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
92502fe8
JW
1884 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1885 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1886 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO)) {
f600b690
JF
1887 netdev_warn(dev, "can't set XDP while host is implementing LRO, disable LRO first\n");
1888 return -EOPNOTSUPP;
1889 }
1890
1891 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
1892 netdev_warn(dev, "XDP expects header/data in single page, any_header_sg required\n");
1893 return -EINVAL;
1894 }
1895
1896 if (dev->mtu > max_sz) {
1897 netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1898 return -EINVAL;
1899 }
1900
672aafd5
JF
1901 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1902 if (prog)
1903 xdp_qp = nr_cpu_ids;
1904
1905 /* XDP requires extra queues for XDP_TX */
1906 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
1907 netdev_warn(dev, "request %i queues but max is %i\n",
1908 curr_qp + xdp_qp, vi->max_queue_pairs);
1909 return -ENOMEM;
1910 }
1911
2de2f7f4
JF
1912 if (prog) {
1913 prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
1914 if (IS_ERR(prog))
1915 return PTR_ERR(prog);
1916 }
1917
2de2f7f4
JF
1918 /* Changing the headroom in buffers is a disruptive operation because
1919 * existing buffers must be flushed and reallocated. This will happen
1920 * when a xdp program is initially added or xdp is disabled by removing
1921 * the xdp program resulting in number of XDP queues changing.
1922 */
1923 if (vi->xdp_queue_pairs != xdp_qp) {
017b29c3
JW
1924 err = virtnet_reset(vi, curr_qp + xdp_qp, xdp_qp);
1925 if (err) {
1926 dev_warn(&dev->dev, "XDP reset failure.\n");
2de2f7f4 1927 goto virtio_reset_err;
017b29c3 1928 }
f600b690
JF
1929 }
1930
672aafd5
JF
1931 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
1932
f600b690
JF
1933 for (i = 0; i < vi->max_queue_pairs; i++) {
1934 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
1935 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
1936 if (old_prog)
1937 bpf_prog_put(old_prog);
1938 }
1939
1940 return 0;
2de2f7f4
JF
1941
1942virtio_reset_err:
1943 /* On reset error do our best to unwind XDP changes inflight and return
1944 * error up to user space for resolution. The underlying reset hung on
1945 * us so not much we can do here.
1946 */
2de2f7f4
JF
1947 if (prog)
1948 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
1949 return err;
f600b690
JF
1950}
1951
1952static bool virtnet_xdp_query(struct net_device *dev)
1953{
1954 struct virtnet_info *vi = netdev_priv(dev);
1955 int i;
1956
1957 for (i = 0; i < vi->max_queue_pairs; i++) {
1958 if (vi->rq[i].xdp_prog)
1959 return true;
1960 }
1961 return false;
1962}
1963
1964static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1965{
1966 switch (xdp->command) {
1967 case XDP_SETUP_PROG:
1968 return virtnet_xdp_set(dev, xdp->prog);
1969 case XDP_QUERY_PROG:
1970 xdp->prog_attached = virtnet_xdp_query(dev);
1971 return 0;
1972 default:
1973 return -EINVAL;
1974 }
1975}
1976
76288b4e
SH
1977static const struct net_device_ops virtnet_netdev = {
1978 .ndo_open = virtnet_open,
1979 .ndo_stop = virtnet_close,
1980 .ndo_start_xmit = start_xmit,
1981 .ndo_validate_addr = eth_validate_addr,
9c46f6d4 1982 .ndo_set_mac_address = virtnet_set_mac_address,
2af7698e 1983 .ndo_set_rx_mode = virtnet_set_rx_mode,
3fa2a1df 1984 .ndo_get_stats64 = virtnet_stats,
1824a989
AW
1985 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1986 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
76288b4e
SH
1987#ifdef CONFIG_NET_POLL_CONTROLLER
1988 .ndo_poll_controller = virtnet_netpoll,
91815639 1989#endif
f600b690 1990 .ndo_xdp = virtnet_xdp,
76288b4e
SH
1991};
1992
586d17c5 1993static void virtnet_config_changed_work(struct work_struct *work)
9f4d26d0 1994{
586d17c5
JW
1995 struct virtnet_info *vi =
1996 container_of(work, struct virtnet_info, config_work);
9f4d26d0
MM
1997 u16 v;
1998
855e0c52
RR
1999 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2000 struct virtio_net_config, status, &v) < 0)
507613bf 2001 return;
586d17c5
JW
2002
2003 if (v & VIRTIO_NET_S_ANNOUNCE) {
ee89bab1 2004 netdev_notify_peers(vi->dev);
586d17c5
JW
2005 virtnet_ack_link_announce(vi);
2006 }
9f4d26d0
MM
2007
2008 /* Ignore unknown (future) status bits */
2009 v &= VIRTIO_NET_S_LINK_UP;
2010
2011 if (vi->status == v)
507613bf 2012 return;
9f4d26d0
MM
2013
2014 vi->status = v;
2015
2016 if (vi->status & VIRTIO_NET_S_LINK_UP) {
2017 netif_carrier_on(vi->dev);
986a4f4d 2018 netif_tx_wake_all_queues(vi->dev);
9f4d26d0
MM
2019 } else {
2020 netif_carrier_off(vi->dev);
986a4f4d 2021 netif_tx_stop_all_queues(vi->dev);
9f4d26d0
MM
2022 }
2023}
2024
2025static void virtnet_config_changed(struct virtio_device *vdev)
2026{
2027 struct virtnet_info *vi = vdev->priv;
2028
3b07e9ca 2029 schedule_work(&vi->config_work);
9f4d26d0
MM
2030}
2031
986a4f4d
JW
2032static void virtnet_free_queues(struct virtnet_info *vi)
2033{
d4fb84ee
AV
2034 int i;
2035
ab3971b1
JW
2036 for (i = 0; i < vi->max_queue_pairs; i++) {
2037 napi_hash_del(&vi->rq[i].napi);
d4fb84ee 2038 netif_napi_del(&vi->rq[i].napi);
b92f1e67 2039 netif_napi_del(&vi->sq[i].napi);
ab3971b1 2040 }
d4fb84ee 2041
963abe5c
ED
2042 /* We called napi_hash_del() before netif_napi_del(),
2043 * we need to respect an RCU grace period before freeing vi->rq
2044 */
2045 synchronize_net();
2046
986a4f4d
JW
2047 kfree(vi->rq);
2048 kfree(vi->sq);
2049}
2050
47315329 2051static void _free_receive_bufs(struct virtnet_info *vi)
986a4f4d 2052{
f600b690 2053 struct bpf_prog *old_prog;
986a4f4d
JW
2054 int i;
2055
2056 for (i = 0; i < vi->max_queue_pairs; i++) {
2057 while (vi->rq[i].pages)
2058 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
f600b690
JF
2059
2060 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2061 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2062 if (old_prog)
2063 bpf_prog_put(old_prog);
986a4f4d 2064 }
47315329
JF
2065}
2066
2067static void free_receive_bufs(struct virtnet_info *vi)
2068{
2069 rtnl_lock();
2070 _free_receive_bufs(vi);
f600b690 2071 rtnl_unlock();
986a4f4d
JW
2072}
2073
fb51879d
MD
2074static void free_receive_page_frags(struct virtnet_info *vi)
2075{
2076 int i;
2077 for (i = 0; i < vi->max_queue_pairs; i++)
2078 if (vi->rq[i].alloc_frag.page)
2079 put_page(vi->rq[i].alloc_frag.page);
2080}
2081
b68df015 2082static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
56434a01
JF
2083{
2084 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
2085 return false;
2086 else if (q < vi->curr_queue_pairs)
2087 return true;
2088 else
2089 return false;
2090}
2091
986a4f4d
JW
2092static void free_unused_bufs(struct virtnet_info *vi)
2093{
2094 void *buf;
2095 int i;
2096
2097 for (i = 0; i < vi->max_queue_pairs; i++) {
2098 struct virtqueue *vq = vi->sq[i].vq;
56434a01 2099 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
b68df015 2100 if (!is_xdp_raw_buffer_queue(vi, i))
56434a01
JF
2101 dev_kfree_skb(buf);
2102 else
2103 put_page(virt_to_head_page(buf));
2104 }
986a4f4d
JW
2105 }
2106
2107 for (i = 0; i < vi->max_queue_pairs; i++) {
2108 struct virtqueue *vq = vi->rq[i].vq;
2109
2110 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
ab7db917
MD
2111 if (vi->mergeable_rx_bufs) {
2112 unsigned long ctx = (unsigned long)buf;
2113 void *base = mergeable_ctx_to_buf_address(ctx);
2114 put_page(virt_to_head_page(base));
2115 } else if (vi->big_packets) {
fa9fac17 2116 give_pages(&vi->rq[i], buf);
ab7db917 2117 } else {
f6b10209 2118 put_page(virt_to_head_page(buf));
ab7db917 2119 }
986a4f4d 2120 }
986a4f4d
JW
2121 }
2122}
2123
e9d7417b
JW
2124static void virtnet_del_vqs(struct virtnet_info *vi)
2125{
2126 struct virtio_device *vdev = vi->vdev;
2127
8898c21c 2128 virtnet_clean_affinity(vi, -1);
986a4f4d 2129
e9d7417b 2130 vdev->config->del_vqs(vdev);
986a4f4d
JW
2131
2132 virtnet_free_queues(vi);
e9d7417b
JW
2133}
2134
986a4f4d 2135static int virtnet_find_vqs(struct virtnet_info *vi)
3f9c10b0 2136{
986a4f4d
JW
2137 vq_callback_t **callbacks;
2138 struct virtqueue **vqs;
2139 int ret = -ENOMEM;
2140 int i, total_vqs;
2141 const char **names;
2142
2143 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2144 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2145 * possible control vq.
2146 */
2147 total_vqs = vi->max_queue_pairs * 2 +
2148 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2149
2150 /* Allocate space for find_vqs parameters */
2151 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2152 if (!vqs)
2153 goto err_vq;
2154 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2155 if (!callbacks)
2156 goto err_callback;
2157 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2158 if (!names)
2159 goto err_names;
2160
2161 /* Parameters for control virtqueue, if any */
2162 if (vi->has_cvq) {
2163 callbacks[total_vqs - 1] = NULL;
2164 names[total_vqs - 1] = "control";
2165 }
3f9c10b0 2166
986a4f4d
JW
2167 /* Allocate/initialize parameters for send/receive virtqueues */
2168 for (i = 0; i < vi->max_queue_pairs; i++) {
2169 callbacks[rxq2vq(i)] = skb_recv_done;
2170 callbacks[txq2vq(i)] = skb_xmit_done;
2171 sprintf(vi->rq[i].name, "input.%d", i);
2172 sprintf(vi->sq[i].name, "output.%d", i);
2173 names[rxq2vq(i)] = vi->rq[i].name;
2174 names[txq2vq(i)] = vi->sq[i].name;
2175 }
3f9c10b0 2176
986a4f4d 2177 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
fb5e31d9 2178 names, NULL);
986a4f4d
JW
2179 if (ret)
2180 goto err_find;
3f9c10b0 2181
986a4f4d
JW
2182 if (vi->has_cvq) {
2183 vi->cvq = vqs[total_vqs - 1];
3f9c10b0 2184 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
f646968f 2185 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3f9c10b0 2186 }
986a4f4d
JW
2187
2188 for (i = 0; i < vi->max_queue_pairs; i++) {
2189 vi->rq[i].vq = vqs[rxq2vq(i)];
2190 vi->sq[i].vq = vqs[txq2vq(i)];
2191 }
2192
2193 kfree(names);
2194 kfree(callbacks);
2195 kfree(vqs);
2196
3f9c10b0 2197 return 0;
986a4f4d
JW
2198
2199err_find:
2200 kfree(names);
2201err_names:
2202 kfree(callbacks);
2203err_callback:
2204 kfree(vqs);
2205err_vq:
2206 return ret;
2207}
2208
2209static int virtnet_alloc_queues(struct virtnet_info *vi)
2210{
2211 int i;
2212
2213 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2214 if (!vi->sq)
2215 goto err_sq;
2216 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
008d4278 2217 if (!vi->rq)
986a4f4d
JW
2218 goto err_rq;
2219
2220 INIT_DELAYED_WORK(&vi->refill, refill_work);
2221 for (i = 0; i < vi->max_queue_pairs; i++) {
2222 vi->rq[i].pages = NULL;
2223 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2224 napi_weight);
b92f1e67
WB
2225 netif_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2226 napi_tx ? napi_weight : 0);
986a4f4d
JW
2227
2228 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
5377d758 2229 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
986a4f4d
JW
2230 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2231 }
2232
2233 return 0;
2234
2235err_rq:
2236 kfree(vi->sq);
2237err_sq:
2238 return -ENOMEM;
2239}
2240
2241static int init_vqs(struct virtnet_info *vi)
2242{
2243 int ret;
2244
2245 /* Allocate send & receive queues */
2246 ret = virtnet_alloc_queues(vi);
2247 if (ret)
2248 goto err;
2249
2250 ret = virtnet_find_vqs(vi);
2251 if (ret)
2252 goto err_free;
2253
47be2479 2254 get_online_cpus();
8898c21c 2255 virtnet_set_affinity(vi);
47be2479
WG
2256 put_online_cpus();
2257
986a4f4d
JW
2258 return 0;
2259
2260err_free:
2261 virtnet_free_queues(vi);
2262err:
2263 return ret;
3f9c10b0
AS
2264}
2265
fbf28d78
MD
2266#ifdef CONFIG_SYSFS
2267static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2268 struct rx_queue_attribute *attribute, char *buf)
2269{
2270 struct virtnet_info *vi = netdev_priv(queue->dev);
2271 unsigned int queue_index = get_netdev_rx_queue_index(queue);
5377d758 2272 struct ewma_pkt_len *avg;
fbf28d78
MD
2273
2274 BUG_ON(queue_index >= vi->max_queue_pairs);
2275 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2276 return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
2277}
2278
2279static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2280 __ATTR_RO(mergeable_rx_buffer_size);
2281
2282static struct attribute *virtio_net_mrg_rx_attrs[] = {
2283 &mergeable_rx_buffer_size_attribute.attr,
2284 NULL
2285};
2286
2287static const struct attribute_group virtio_net_mrg_rx_group = {
2288 .name = "virtio_net",
2289 .attrs = virtio_net_mrg_rx_attrs
2290};
2291#endif
2292
892d6eb1
JW
2293static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2294 unsigned int fbit,
2295 const char *fname, const char *dname)
2296{
2297 if (!virtio_has_feature(vdev, fbit))
2298 return false;
2299
2300 dev_err(&vdev->dev, "device advertises feature %s but not %s",
2301 fname, dname);
2302
2303 return true;
2304}
2305
2306#define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
2307 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2308
2309static bool virtnet_validate_features(struct virtio_device *vdev)
2310{
2311 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2312 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2313 "VIRTIO_NET_F_CTRL_VQ") ||
2314 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2315 "VIRTIO_NET_F_CTRL_VQ") ||
2316 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2317 "VIRTIO_NET_F_CTRL_VQ") ||
2318 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2319 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2320 "VIRTIO_NET_F_CTRL_VQ"))) {
2321 return false;
2322 }
2323
2324 return true;
2325}
2326
d0c2c997
JW
2327#define MIN_MTU ETH_MIN_MTU
2328#define MAX_MTU ETH_MAX_MTU
2329
fe36cbe0 2330static int virtnet_validate(struct virtio_device *vdev)
296f96fc 2331{
6ba42248
MT
2332 if (!vdev->config->get) {
2333 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2334 __func__);
2335 return -EINVAL;
2336 }
2337
892d6eb1
JW
2338 if (!virtnet_validate_features(vdev))
2339 return -EINVAL;
2340
fe36cbe0
MT
2341 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2342 int mtu = virtio_cread16(vdev,
2343 offsetof(struct virtio_net_config,
2344 mtu));
2345 if (mtu < MIN_MTU)
2346 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2347 }
2348
2349 return 0;
2350}
2351
2352static int virtnet_probe(struct virtio_device *vdev)
2353{
2354 int i, err;
2355 struct net_device *dev;
2356 struct virtnet_info *vi;
2357 u16 max_queue_pairs;
2358 int mtu;
2359
986a4f4d 2360 /* Find if host supports multiqueue virtio_net device */
855e0c52
RR
2361 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2362 struct virtio_net_config,
2363 max_virtqueue_pairs, &max_queue_pairs);
986a4f4d
JW
2364
2365 /* We need at least 2 queue's */
2366 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2367 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2368 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2369 max_queue_pairs = 1;
296f96fc
RR
2370
2371 /* Allocate ourselves a network device with room for our info */
986a4f4d 2372 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
296f96fc
RR
2373 if (!dev)
2374 return -ENOMEM;
2375
2376 /* Set up network device as normal. */
f2f2c8b4 2377 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
76288b4e 2378 dev->netdev_ops = &virtnet_netdev;
296f96fc 2379 dev->features = NETIF_F_HIGHDMA;
3fa2a1df 2380
7ad24ea4 2381 dev->ethtool_ops = &virtnet_ethtool_ops;
296f96fc
RR
2382 SET_NETDEV_DEV(dev, &vdev->dev);
2383
2384 /* Do we support "hardware" checksums? */
98e778c9 2385 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc 2386 /* This opens up the world of extra features. */
48900cb6 2387 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
98e778c9 2388 if (csum)
48900cb6 2389 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
98e778c9
MM
2390
2391 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
e3e3c423 2392 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
34a48579
RR
2393 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
2394 }
5539ae96 2395 /* Individual feature bits: what can host handle? */
98e778c9
MM
2396 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
2397 dev->hw_features |= NETIF_F_TSO;
2398 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
2399 dev->hw_features |= NETIF_F_TSO6;
2400 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
2401 dev->hw_features |= NETIF_F_TSO_ECN;
e3e3c423
VY
2402 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
2403 dev->hw_features |= NETIF_F_UFO;
98e778c9 2404
41f2f127
JW
2405 dev->features |= NETIF_F_GSO_ROBUST;
2406
98e778c9 2407 if (gso)
e3e3c423 2408 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
98e778c9 2409 /* (!csum && gso) case will be fixed by register_netdev() */
296f96fc 2410 }
4f49129b
TH
2411 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
2412 dev->features |= NETIF_F_RXCSUM;
296f96fc 2413
4fda8302
JW
2414 dev->vlan_features = dev->features;
2415
d0c2c997
JW
2416 /* MTU range: 68 - 65535 */
2417 dev->min_mtu = MIN_MTU;
2418 dev->max_mtu = MAX_MTU;
2419
296f96fc 2420 /* Configuration may specify what MAC to use. Otherwise random. */
855e0c52
RR
2421 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2422 virtio_cread_bytes(vdev,
2423 offsetof(struct virtio_net_config, mac),
2424 dev->dev_addr, dev->addr_len);
2425 else
f2cedb63 2426 eth_hw_addr_random(dev);
296f96fc
RR
2427
2428 /* Set up our device-specific information */
2429 vi = netdev_priv(dev);
296f96fc
RR
2430 vi->dev = dev;
2431 vi->vdev = vdev;
d9d5dcc8 2432 vdev->priv = vi;
3fa2a1df 2433 vi->stats = alloc_percpu(struct virtnet_stats);
2434 err = -ENOMEM;
2435 if (vi->stats == NULL)
2436 goto free;
2437
827da44c
JS
2438 for_each_possible_cpu(i) {
2439 struct virtnet_stats *virtnet_stats;
2440 virtnet_stats = per_cpu_ptr(vi->stats, i);
2441 u64_stats_init(&virtnet_stats->tx_syncp);
2442 u64_stats_init(&virtnet_stats->rx_syncp);
2443 }
2444
586d17c5 2445 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
296f96fc 2446
97402b96 2447 /* If we can receive ANY GSO packets, we must allocate large ones. */
8e95a202
JP
2448 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
2449 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
e3e3c423
VY
2450 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2451 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
97402b96
HX
2452 vi->big_packets = true;
2453
3f2c31d9
MM
2454 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
2455 vi->mergeable_rx_bufs = true;
2456
d04302b3
MT
2457 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2458 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
012873d0
MT
2459 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2460 else
2461 vi->hdr_len = sizeof(struct virtio_net_hdr);
2462
75993300
MT
2463 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
2464 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
e7428e95
MT
2465 vi->any_header_sg = true;
2466
986a4f4d
JW
2467 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2468 vi->has_cvq = true;
2469
14de9d11
AC
2470 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2471 mtu = virtio_cread16(vdev,
2472 offsetof(struct virtio_net_config,
2473 mtu));
93a205ee 2474 if (mtu < dev->min_mtu) {
fe36cbe0
MT
2475 /* Should never trigger: MTU was previously validated
2476 * in virtnet_validate.
2477 */
2478 dev_err(&vdev->dev, "device MTU appears to have changed "
2479 "it is now %d < %d", mtu, dev->min_mtu);
2480 goto free_stats;
93a205ee 2481 }
2e123b44 2482
fe36cbe0
MT
2483 dev->mtu = mtu;
2484 dev->max_mtu = mtu;
2485
2e123b44
MT
2486 /* TODO: size buffers correctly in this case. */
2487 if (dev->mtu > ETH_DATA_LEN)
2488 vi->big_packets = true;
14de9d11
AC
2489 }
2490
012873d0
MT
2491 if (vi->any_header_sg)
2492 dev->needed_headroom = vi->hdr_len;
6ebbc1a6 2493
44900010
JW
2494 /* Enable multiqueue by default */
2495 if (num_online_cpus() >= max_queue_pairs)
2496 vi->curr_queue_pairs = max_queue_pairs;
2497 else
2498 vi->curr_queue_pairs = num_online_cpus();
986a4f4d
JW
2499 vi->max_queue_pairs = max_queue_pairs;
2500
2501 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3f9c10b0 2502 err = init_vqs(vi);
d2a7ddda 2503 if (err)
9bb8ca86 2504 goto free_stats;
296f96fc 2505
fbf28d78
MD
2506#ifdef CONFIG_SYSFS
2507 if (vi->mergeable_rx_bufs)
2508 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2509#endif
0f13b66b
ZYW
2510 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
2511 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
986a4f4d 2512
16032be5
NA
2513 virtnet_init_settings(dev);
2514
296f96fc
RR
2515 err = register_netdev(dev);
2516 if (err) {
2517 pr_debug("virtio_net: registering device failed\n");
d2a7ddda 2518 goto free_vqs;
296f96fc 2519 }
b3369c1f 2520
4baf1e33
MT
2521 virtio_device_ready(vdev);
2522
8017c279 2523 err = virtnet_cpu_notif_add(vi);
8de4b2f3
WG
2524 if (err) {
2525 pr_debug("virtio_net: registering cpu notifier failed\n");
f00e35e2 2526 goto free_unregister_netdev;
8de4b2f3
WG
2527 }
2528
a220871b 2529 virtnet_set_queues(vi, vi->curr_queue_pairs);
44900010 2530
167c25e4
JW
2531 /* Assume link up if device can't report link status,
2532 otherwise get link status from config. */
2533 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2534 netif_carrier_off(dev);
3b07e9ca 2535 schedule_work(&vi->config_work);
167c25e4
JW
2536 } else {
2537 vi->status = VIRTIO_NET_S_LINK_UP;
2538 netif_carrier_on(dev);
2539 }
9f4d26d0 2540
986a4f4d
JW
2541 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2542 dev->name, max_queue_pairs);
2543
296f96fc
RR
2544 return 0;
2545
f00e35e2 2546free_unregister_netdev:
02465555
MT
2547 vi->vdev->config->reset(vdev);
2548
b3369c1f 2549 unregister_netdev(dev);
d2a7ddda 2550free_vqs:
986a4f4d 2551 cancel_delayed_work_sync(&vi->refill);
fb51879d 2552 free_receive_page_frags(vi);
e9d7417b 2553 virtnet_del_vqs(vi);
3fa2a1df 2554free_stats:
2555 free_percpu(vi->stats);
296f96fc
RR
2556free:
2557 free_netdev(dev);
2558 return err;
2559}
2560
2de2f7f4
JF
2561static void _remove_vq_common(struct virtnet_info *vi)
2562{
2563 vi->vdev->config->reset(vi->vdev);
2564 free_unused_bufs(vi);
2565 _free_receive_bufs(vi);
2566 free_receive_page_frags(vi);
2567 virtnet_del_vqs(vi);
2568}
2569
04486ed0 2570static void remove_vq_common(struct virtnet_info *vi)
296f96fc 2571{
04486ed0 2572 vi->vdev->config->reset(vi->vdev);
830a8a97
SM
2573
2574 /* Free unused buffers in both send and recv, if any. */
9ab86bbc 2575 free_unused_bufs(vi);
fb6813f4 2576
986a4f4d 2577 free_receive_bufs(vi);
d2a7ddda 2578
fb51879d
MD
2579 free_receive_page_frags(vi);
2580
986a4f4d 2581 virtnet_del_vqs(vi);
04486ed0
AS
2582}
2583
8cc085d6 2584static void virtnet_remove(struct virtio_device *vdev)
04486ed0
AS
2585{
2586 struct virtnet_info *vi = vdev->priv;
2587
8017c279 2588 virtnet_cpu_notif_remove(vi);
8de4b2f3 2589
102a2786
MT
2590 /* Make sure no work handler is accessing the device. */
2591 flush_work(&vi->config_work);
586d17c5 2592
04486ed0
AS
2593 unregister_netdev(vi->dev);
2594
2595 remove_vq_common(vi);
fb6813f4 2596
2e66f55b 2597 free_percpu(vi->stats);
74b2553f 2598 free_netdev(vi->dev);
296f96fc
RR
2599}
2600
89107000 2601#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
2602static int virtnet_freeze(struct virtio_device *vdev)
2603{
2604 struct virtnet_info *vi = vdev->priv;
2605
8017c279 2606 virtnet_cpu_notif_remove(vi);
9fe7bfce 2607 virtnet_freeze_down(vdev);
0741bcb5
AS
2608 remove_vq_common(vi);
2609
2610 return 0;
2611}
2612
2613static int virtnet_restore(struct virtio_device *vdev)
2614{
2615 struct virtnet_info *vi = vdev->priv;
9fe7bfce 2616 int err;
0741bcb5 2617
9fe7bfce 2618 err = virtnet_restore_up(vdev);
0741bcb5
AS
2619 if (err)
2620 return err;
986a4f4d
JW
2621 virtnet_set_queues(vi, vi->curr_queue_pairs);
2622
8017c279 2623 err = virtnet_cpu_notif_add(vi);
ec9debbd
JW
2624 if (err)
2625 return err;
2626
0741bcb5
AS
2627 return 0;
2628}
2629#endif
2630
296f96fc
RR
2631static struct virtio_device_id id_table[] = {
2632 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2633 { 0 },
2634};
2635
f3358507
MT
2636#define VIRTNET_FEATURES \
2637 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2638 VIRTIO_NET_F_MAC, \
2639 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2640 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2641 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2642 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2643 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2644 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2645 VIRTIO_NET_F_CTRL_MAC_ADDR, \
2646 VIRTIO_NET_F_MTU
2647
c45a6816 2648static unsigned int features[] = {
f3358507
MT
2649 VIRTNET_FEATURES,
2650};
2651
2652static unsigned int features_legacy[] = {
2653 VIRTNET_FEATURES,
2654 VIRTIO_NET_F_GSO,
e7428e95 2655 VIRTIO_F_ANY_LAYOUT,
c45a6816
RR
2656};
2657
22402529 2658static struct virtio_driver virtio_net_driver = {
c45a6816
RR
2659 .feature_table = features,
2660 .feature_table_size = ARRAY_SIZE(features),
f3358507
MT
2661 .feature_table_legacy = features_legacy,
2662 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
296f96fc
RR
2663 .driver.name = KBUILD_MODNAME,
2664 .driver.owner = THIS_MODULE,
2665 .id_table = id_table,
fe36cbe0 2666 .validate = virtnet_validate,
296f96fc 2667 .probe = virtnet_probe,
8cc085d6 2668 .remove = virtnet_remove,
9f4d26d0 2669 .config_changed = virtnet_config_changed,
89107000 2670#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
2671 .freeze = virtnet_freeze,
2672 .restore = virtnet_restore,
2673#endif
296f96fc
RR
2674};
2675
8017c279
SAS
2676static __init int virtio_net_driver_init(void)
2677{
2678 int ret;
2679
73c1b41e 2680 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
8017c279
SAS
2681 virtnet_cpu_online,
2682 virtnet_cpu_down_prep);
2683 if (ret < 0)
2684 goto out;
2685 virtionet_online = ret;
73c1b41e 2686 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
8017c279
SAS
2687 NULL, virtnet_cpu_dead);
2688 if (ret)
2689 goto err_dead;
2690
2691 ret = register_virtio_driver(&virtio_net_driver);
2692 if (ret)
2693 goto err_virtio;
2694 return 0;
2695err_virtio:
2696 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2697err_dead:
2698 cpuhp_remove_multi_state(virtionet_online);
2699out:
2700 return ret;
2701}
2702module_init(virtio_net_driver_init);
2703
2704static __exit void virtio_net_driver_exit(void)
2705{
2706 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
2707 cpuhp_remove_multi_state(virtionet_online);
2708 unregister_virtio_driver(&virtio_net_driver);
2709}
2710module_exit(virtio_net_driver_exit);
296f96fc
RR
2711
2712MODULE_DEVICE_TABLE(virtio, id_table);
2713MODULE_DESCRIPTION("Virtio network driver");
2714MODULE_LICENSE("GPL");