]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/virtio_net.c
virtio-net: determine type of bufs correctly
[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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19//#define DEBUG
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
a9ea3fc6 22#include <linux/ethtool.h>
296f96fc
RR
23#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
e918085a 27#include <linux/if_vlan.h>
5a0e3ad6 28#include <linux/slab.h>
8de4b2f3 29#include <linux/cpu.h>
296f96fc 30
d34710e3 31static int napi_weight = NAPI_POLL_WEIGHT;
6c0cd7c0
DL
32module_param(napi_weight, int, 0444);
33
eb939922 34static bool csum = true, gso = true;
34a48579
RR
35module_param(csum, bool, 0444);
36module_param(gso, bool, 0444);
37
296f96fc 38/* FIXME: MTU in config. */
5061de36
MD
39#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
40#define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
41 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
42 L1_CACHE_BYTES))
3f2c31d9 43#define GOOD_COPY_LEN 128
296f96fc 44
66846048 45#define VIRTNET_DRIVER_VERSION "1.0.0"
2a41f71d 46
3fa2a1df 47struct virtnet_stats {
83a27052
ED
48 struct u64_stats_sync tx_syncp;
49 struct u64_stats_sync rx_syncp;
3fa2a1df 50 u64 tx_bytes;
51 u64 tx_packets;
52
53 u64 rx_bytes;
54 u64 rx_packets;
55};
56
e9d7417b
JW
57/* Internal representation of a send virtqueue */
58struct send_queue {
59 /* Virtqueue associated with this send _queue */
60 struct virtqueue *vq;
61
62 /* TX: fragments + linear part + virtio header */
63 struct scatterlist sg[MAX_SKB_FRAGS + 2];
986a4f4d
JW
64
65 /* Name of the send queue: output.$index */
66 char name[40];
e9d7417b
JW
67};
68
69/* Internal representation of a receive virtqueue */
70struct receive_queue {
71 /* Virtqueue associated with this receive_queue */
72 struct virtqueue *vq;
73
296f96fc
RR
74 struct napi_struct napi;
75
76 /* Number of input buffers, and max we've ever had. */
77 unsigned int num, max;
78
e9d7417b
JW
79 /* Chain pages by the private ptr. */
80 struct page *pages;
81
82 /* RX: fragments + linear part + virtio header */
83 struct scatterlist sg[MAX_SKB_FRAGS + 2];
986a4f4d
JW
84
85 /* Name of this receive queue: input.$index */
86 char name[40];
e9d7417b
JW
87};
88
89struct virtnet_info {
90 struct virtio_device *vdev;
91 struct virtqueue *cvq;
92 struct net_device *dev;
986a4f4d
JW
93 struct send_queue *sq;
94 struct receive_queue *rq;
e9d7417b
JW
95 unsigned int status;
96
986a4f4d
JW
97 /* Max # of queue pairs supported by the device */
98 u16 max_queue_pairs;
99
100 /* # of queue pairs currently used by the driver */
101 u16 curr_queue_pairs;
102
97402b96
HX
103 /* I like... big packets and I cannot lie! */
104 bool big_packets;
105
3f2c31d9
MM
106 /* Host will merge rx buffers for big packets (shake it! shake it!) */
107 bool mergeable_rx_bufs;
108
986a4f4d
JW
109 /* Has control virtqueue */
110 bool has_cvq;
111
e7428e95
MT
112 /* Host can handle any s/g split between our header and packet data */
113 bool any_header_sg;
114
586d17c5
JW
115 /* enable config space updates */
116 bool config_enable;
117
3fa2a1df 118 /* Active statistics */
119 struct virtnet_stats __percpu *stats;
120
3161e453
RR
121 /* Work struct for refilling if we run low on memory. */
122 struct delayed_work refill;
123
586d17c5
JW
124 /* Work struct for config space updates */
125 struct work_struct config_work;
126
127 /* Lock for config space updates */
128 struct mutex config_lock;
986a4f4d 129
2613af0e
MD
130 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
131 * low on memory.
132 */
133 struct page_frag alloc_frag;
134
986a4f4d
JW
135 /* Does the affinity hint is set for virtqueues? */
136 bool affinity_hint_set;
47be2479 137
8de4b2f3
WG
138 /* CPU hot plug notifier */
139 struct notifier_block nb;
296f96fc
RR
140};
141
b3f24698
RR
142struct skb_vnet_hdr {
143 union {
144 struct virtio_net_hdr hdr;
145 struct virtio_net_hdr_mrg_rxbuf mhdr;
146 };
147};
148
9ab86bbc
SM
149struct padded_vnet_hdr {
150 struct virtio_net_hdr hdr;
151 /*
152 * virtio_net_hdr should be in a separated sg buffer because of a
153 * QEMU bug, and data sg buffer shares same page with this header sg.
154 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
155 */
156 char padding[6];
157};
158
986a4f4d
JW
159/* Converting between virtqueue no. and kernel tx/rx queue no.
160 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
161 */
162static int vq2txq(struct virtqueue *vq)
163{
9d0ca6ed 164 return (vq->index - 1) / 2;
986a4f4d
JW
165}
166
167static int txq2vq(int txq)
168{
169 return txq * 2 + 1;
170}
171
172static int vq2rxq(struct virtqueue *vq)
173{
9d0ca6ed 174 return vq->index / 2;
986a4f4d
JW
175}
176
177static int rxq2vq(int rxq)
178{
179 return rxq * 2;
180}
181
b3f24698 182static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
296f96fc 183{
b3f24698 184 return (struct skb_vnet_hdr *)skb->cb;
296f96fc
RR
185}
186
9ab86bbc
SM
187/*
188 * private is used to chain pages for big packets, put the whole
189 * most recent used list in the beginning for reuse
190 */
e9d7417b 191static void give_pages(struct receive_queue *rq, struct page *page)
0a888fd1 192{
9ab86bbc 193 struct page *end;
0a888fd1 194
e9d7417b 195 /* Find end of list, sew whole thing into vi->rq.pages. */
9ab86bbc 196 for (end = page; end->private; end = (struct page *)end->private);
e9d7417b
JW
197 end->private = (unsigned long)rq->pages;
198 rq->pages = page;
0a888fd1
MM
199}
200
e9d7417b 201static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
fb6813f4 202{
e9d7417b 203 struct page *p = rq->pages;
fb6813f4 204
9ab86bbc 205 if (p) {
e9d7417b 206 rq->pages = (struct page *)p->private;
9ab86bbc
SM
207 /* clear private here, it is used to chain pages */
208 p->private = 0;
209 } else
fb6813f4
RR
210 p = alloc_page(gfp_mask);
211 return p;
212}
213
e9d7417b 214static void skb_xmit_done(struct virtqueue *vq)
296f96fc 215{
e9d7417b 216 struct virtnet_info *vi = vq->vdev->priv;
296f96fc 217
2cb9c6ba 218 /* Suppress further interrupts. */
e9d7417b 219 virtqueue_disable_cb(vq);
11a3a154 220
363f1514 221 /* We were probably waiting for more output buffers. */
986a4f4d 222 netif_wake_subqueue(vi->dev, vq2txq(vq));
296f96fc
RR
223}
224
3464645a 225/* Called from bottom half context */
e9d7417b 226static struct sk_buff *page_to_skb(struct receive_queue *rq,
2613af0e
MD
227 struct page *page, unsigned int offset,
228 unsigned int len, unsigned int truesize)
9ab86bbc 229{
e9d7417b 230 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc
SM
231 struct sk_buff *skb;
232 struct skb_vnet_hdr *hdr;
2613af0e 233 unsigned int copy, hdr_len, hdr_padded_len;
9ab86bbc 234 char *p;
fb6813f4 235
2613af0e 236 p = page_address(page) + offset;
3f2c31d9 237
9ab86bbc
SM
238 /* copy small packet so we can reuse these pages for small data */
239 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
240 if (unlikely(!skb))
241 return NULL;
3f2c31d9 242
9ab86bbc 243 hdr = skb_vnet_hdr(skb);
3f2c31d9 244
9ab86bbc
SM
245 if (vi->mergeable_rx_bufs) {
246 hdr_len = sizeof hdr->mhdr;
2613af0e 247 hdr_padded_len = sizeof hdr->mhdr;
9ab86bbc
SM
248 } else {
249 hdr_len = sizeof hdr->hdr;
2613af0e 250 hdr_padded_len = sizeof(struct padded_vnet_hdr);
9ab86bbc 251 }
3f2c31d9 252
9ab86bbc 253 memcpy(hdr, p, hdr_len);
3f2c31d9 254
9ab86bbc 255 len -= hdr_len;
2613af0e
MD
256 offset += hdr_padded_len;
257 p += hdr_padded_len;
3f2c31d9 258
9ab86bbc
SM
259 copy = len;
260 if (copy > skb_tailroom(skb))
261 copy = skb_tailroom(skb);
262 memcpy(skb_put(skb, copy), p, copy);
3f2c31d9 263
9ab86bbc
SM
264 len -= copy;
265 offset += copy;
3f2c31d9 266
2613af0e
MD
267 if (vi->mergeable_rx_bufs) {
268 if (len)
269 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
270 else
271 put_page(page);
272 return skb;
273 }
274
e878d78b
SL
275 /*
276 * Verify that we can indeed put this data into a skb.
277 * This is here to handle cases when the device erroneously
278 * tries to receive more than is possible. This is usually
279 * the case of a broken device.
280 */
281 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
be443899 282 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
e878d78b
SL
283 dev_kfree_skb(skb);
284 return NULL;
285 }
2613af0e 286 BUG_ON(offset >= PAGE_SIZE);
9ab86bbc 287 while (len) {
2613af0e
MD
288 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
289 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
290 frag_size, truesize);
291 len -= frag_size;
9ab86bbc
SM
292 page = (struct page *)page->private;
293 offset = 0;
294 }
3f2c31d9 295
9ab86bbc 296 if (page)
e9d7417b 297 give_pages(rq, page);
3f2c31d9 298
9ab86bbc
SM
299 return skb;
300}
3f2c31d9 301
f121159d
MT
302static struct sk_buff *receive_small(void *buf, unsigned int len)
303{
304 struct sk_buff * skb = buf;
305
306 len -= sizeof(struct virtio_net_hdr);
307 skb_trim(skb, len);
308
309 return skb;
310}
311
312static struct sk_buff *receive_big(struct net_device *dev,
313 struct receive_queue *rq,
314 void *buf,
315 unsigned int len)
316{
317 struct page *page = buf;
318 struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
319
320 if (unlikely(!skb))
321 goto err;
322
323 return skb;
324
325err:
326 dev->stats.rx_dropped++;
327 give_pages(rq, page);
328 return NULL;
329}
330
8fc3b9e9
MT
331static struct sk_buff *receive_mergeable(struct net_device *dev,
332 struct receive_queue *rq,
333 void *buf,
334 unsigned int len)
9ab86bbc 335{
8fc3b9e9
MT
336 struct skb_vnet_hdr *hdr = buf;
337 int num_buf = hdr->mhdr.num_buffers;
338 struct page *page = virt_to_head_page(buf);
339 int offset = buf - page_address(page);
340 struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
341 MERGE_BUFFER_LEN);
2613af0e 342 struct sk_buff *curr_skb = head_skb;
9ab86bbc 343
8fc3b9e9
MT
344 if (unlikely(!curr_skb))
345 goto err_skb;
346
9ab86bbc 347 while (--num_buf) {
8fc3b9e9
MT
348 int num_skb_frags;
349
2613af0e
MD
350 buf = virtqueue_get_buf(rq->vq, &len);
351 if (unlikely(!buf)) {
8fc3b9e9
MT
352 pr_debug("%s: rx error: %d buffers out of %d missing\n",
353 dev->name, num_buf, hdr->mhdr.num_buffers);
354 dev->stats.rx_length_errors++;
355 goto err_buf;
3f2c31d9 356 }
5061de36 357 if (unlikely(len > MERGE_BUFFER_LEN)) {
2613af0e 358 pr_debug("%s: rx error: merge buffer too long\n",
8fc3b9e9 359 dev->name);
5061de36 360 len = MERGE_BUFFER_LEN;
2613af0e 361 }
8fc3b9e9
MT
362
363 page = virt_to_head_page(buf);
364 --rq->num;
365
366 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
2613af0e
MD
367 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
368 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
8fc3b9e9
MT
369
370 if (unlikely(!nskb))
371 goto err_skb;
2613af0e
MD
372 if (curr_skb == head_skb)
373 skb_shinfo(curr_skb)->frag_list = nskb;
374 else
375 curr_skb->next = nskb;
376 curr_skb = nskb;
377 head_skb->truesize += nskb->truesize;
378 num_skb_frags = 0;
379 }
380 if (curr_skb != head_skb) {
381 head_skb->data_len += len;
382 head_skb->len += len;
5061de36 383 head_skb->truesize += MERGE_BUFFER_LEN;
2613af0e 384 }
8fc3b9e9 385 offset = buf - page_address(page);
ba275241
JW
386 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
387 put_page(page);
388 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
5061de36 389 len, MERGE_BUFFER_LEN);
ba275241
JW
390 } else {
391 skb_add_rx_frag(curr_skb, num_skb_frags, page,
5061de36 392 offset, len, MERGE_BUFFER_LEN);
ba275241 393 }
8fc3b9e9
MT
394 }
395
396 return head_skb;
397
398err_skb:
399 put_page(page);
400 while (--num_buf) {
401 buf = virtqueue_get_buf(rq->vq, &len);
402 if (unlikely(!buf)) {
403 pr_debug("%s: rx error: %d buffers missing\n",
404 dev->name, num_buf);
405 dev->stats.rx_length_errors++;
406 break;
407 }
408 page = virt_to_head_page(buf);
409 put_page(page);
e9d7417b 410 --rq->num;
9ab86bbc 411 }
8fc3b9e9
MT
412err_buf:
413 dev->stats.rx_dropped++;
414 dev_kfree_skb(head_skb);
415 return NULL;
9ab86bbc
SM
416}
417
e9d7417b 418static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
9ab86bbc 419{
e9d7417b
JW
420 struct virtnet_info *vi = rq->vq->vdev->priv;
421 struct net_device *dev = vi->dev;
58472a76 422 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
9ab86bbc 423 struct sk_buff *skb;
9ab86bbc 424 struct skb_vnet_hdr *hdr;
3f2c31d9 425
9ab86bbc
SM
426 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
427 pr_debug("%s: short packet %i\n", dev->name, len);
428 dev->stats.rx_length_errors++;
2613af0e 429 if (vi->big_packets)
e9d7417b 430 give_pages(rq, buf);
2613af0e
MD
431 else if (vi->mergeable_rx_bufs)
432 put_page(virt_to_head_page(buf));
9ab86bbc
SM
433 else
434 dev_kfree_skb(buf);
435 return;
436 }
3f2c31d9 437
f121159d 438 if (vi->mergeable_rx_bufs)
8fc3b9e9 439 skb = receive_mergeable(dev, rq, buf, len);
f121159d
MT
440 else if (vi->big_packets)
441 skb = receive_big(dev, rq, buf, len);
442 else
443 skb = receive_small(buf, len);
444
445 if (unlikely(!skb))
446 return;
3f2c31d9 447
9ab86bbc 448 hdr = skb_vnet_hdr(skb);
3fa2a1df 449
83a27052 450 u64_stats_update_begin(&stats->rx_syncp);
3fa2a1df 451 stats->rx_bytes += skb->len;
452 stats->rx_packets++;
83a27052 453 u64_stats_update_end(&stats->rx_syncp);
296f96fc 454
b3f24698 455 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
296f96fc 456 pr_debug("Needs csum!\n");
b3f24698
RR
457 if (!skb_partial_csum_set(skb,
458 hdr->hdr.csum_start,
459 hdr->hdr.csum_offset))
296f96fc 460 goto frame_err;
10a8d94a
JW
461 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
462 skb->ip_summed = CHECKSUM_UNNECESSARY;
296f96fc
RR
463 }
464
23cde76d
MM
465 skb->protocol = eth_type_trans(skb, dev);
466 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
467 ntohs(skb->protocol), skb->len, skb->pkt_type);
468
b3f24698 469 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
296f96fc 470 pr_debug("GSO!\n");
b3f24698 471 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
296f96fc 472 case VIRTIO_NET_HDR_GSO_TCPV4:
c9af6db4 473 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
296f96fc 474 break;
296f96fc 475 case VIRTIO_NET_HDR_GSO_UDP:
c9af6db4 476 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
296f96fc
RR
477 break;
478 case VIRTIO_NET_HDR_GSO_TCPV6:
c9af6db4 479 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
296f96fc
RR
480 break;
481 default:
be443899
AW
482 net_warn_ratelimited("%s: bad gso type %u.\n",
483 dev->name, hdr->hdr.gso_type);
296f96fc
RR
484 goto frame_err;
485 }
486
b3f24698 487 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
c9af6db4 488 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
34a48579 489
b3f24698 490 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
296f96fc 491 if (skb_shinfo(skb)->gso_size == 0) {
be443899 492 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
296f96fc
RR
493 goto frame_err;
494 }
495
496 /* Header must be checked, and gso_segs computed. */
497 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
498 skb_shinfo(skb)->gso_segs = 0;
499 }
500
501 netif_receive_skb(skb);
502 return;
503
504frame_err:
505 dev->stats.rx_frame_errors++;
296f96fc
RR
506 dev_kfree_skb(skb);
507}
508
e9d7417b 509static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
296f96fc 510{
e9d7417b 511 struct virtnet_info *vi = rq->vq->vdev->priv;
296f96fc 512 struct sk_buff *skb;
9ab86bbc 513 struct skb_vnet_hdr *hdr;
9ab86bbc 514 int err;
3f2c31d9 515
5061de36 516 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
9ab86bbc
SM
517 if (unlikely(!skb))
518 return -ENOMEM;
296f96fc 519
5061de36 520 skb_put(skb, GOOD_PACKET_LEN);
3f2c31d9 521
9ab86bbc 522 hdr = skb_vnet_hdr(skb);
e9d7417b 523 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
97402b96 524
e9d7417b 525 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
97402b96 526
9dc7b9e4 527 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
9ab86bbc
SM
528 if (err < 0)
529 dev_kfree_skb(skb);
97402b96 530
9ab86bbc
SM
531 return err;
532}
97402b96 533
e9d7417b 534static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
9ab86bbc 535{
9ab86bbc
SM
536 struct page *first, *list = NULL;
537 char *p;
538 int i, err, offset;
539
e9d7417b 540 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
9ab86bbc 541 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
e9d7417b 542 first = get_a_page(rq, gfp);
9ab86bbc
SM
543 if (!first) {
544 if (list)
e9d7417b 545 give_pages(rq, list);
9ab86bbc 546 return -ENOMEM;
97402b96 547 }
e9d7417b 548 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
97402b96 549
9ab86bbc
SM
550 /* chain new page in list head to match sg */
551 first->private = (unsigned long)list;
552 list = first;
553 }
296f96fc 554
e9d7417b 555 first = get_a_page(rq, gfp);
9ab86bbc 556 if (!first) {
e9d7417b 557 give_pages(rq, list);
9ab86bbc
SM
558 return -ENOMEM;
559 }
560 p = page_address(first);
561
e9d7417b
JW
562 /* rq->sg[0], rq->sg[1] share the same page */
563 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
564 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
9ab86bbc 565
e9d7417b 566 /* rq->sg[1] for data packet, from offset */
9ab86bbc 567 offset = sizeof(struct padded_vnet_hdr);
e9d7417b 568 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
9ab86bbc
SM
569
570 /* chain first in list head */
571 first->private = (unsigned long)list;
9dc7b9e4
RR
572 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
573 first, gfp);
9ab86bbc 574 if (err < 0)
e9d7417b 575 give_pages(rq, first);
9ab86bbc
SM
576
577 return err;
296f96fc
RR
578}
579
e9d7417b 580static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
3f2c31d9 581{
2613af0e
MD
582 struct virtnet_info *vi = rq->vq->vdev->priv;
583 char *buf = NULL;
3f2c31d9 584 int err;
3f2c31d9 585
2613af0e 586 if (gfp & __GFP_WAIT) {
5061de36 587 if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
2613af0e
MD
588 gfp)) {
589 buf = (char *)page_address(vi->alloc_frag.page) +
590 vi->alloc_frag.offset;
591 get_page(vi->alloc_frag.page);
5061de36 592 vi->alloc_frag.offset += MERGE_BUFFER_LEN;
2613af0e
MD
593 }
594 } else {
5061de36 595 buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
2613af0e
MD
596 }
597 if (!buf)
9ab86bbc 598 return -ENOMEM;
3f2c31d9 599
5061de36 600 sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
2613af0e 601 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
9ab86bbc 602 if (err < 0)
2613af0e 603 put_page(virt_to_head_page(buf));
3f2c31d9 604
9ab86bbc
SM
605 return err;
606}
3f2c31d9 607
b2baed69
RR
608/*
609 * Returns false if we couldn't fill entirely (OOM).
610 *
611 * Normally run in the receive path, but can also be run from ndo_open
612 * before we're receiving packets, or from refill_work which is
613 * careful to disable receiving (using napi_disable).
614 */
e9d7417b 615static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
9ab86bbc 616{
e9d7417b 617 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc 618 int err;
1788f495 619 bool oom;
3f2c31d9 620
9ab86bbc
SM
621 do {
622 if (vi->mergeable_rx_bufs)
e9d7417b 623 err = add_recvbuf_mergeable(rq, gfp);
9ab86bbc 624 else if (vi->big_packets)
e9d7417b 625 err = add_recvbuf_big(rq, gfp);
9ab86bbc 626 else
e9d7417b 627 err = add_recvbuf_small(rq, gfp);
3f2c31d9 628
1788f495 629 oom = err == -ENOMEM;
9ed4cb07 630 if (err)
3f2c31d9 631 break;
e9d7417b 632 ++rq->num;
b7dfde95 633 } while (rq->vq->num_free);
e9d7417b
JW
634 if (unlikely(rq->num > rq->max))
635 rq->max = rq->num;
67975901
HG
636 if (unlikely(!virtqueue_kick(rq->vq)))
637 return false;
3161e453 638 return !oom;
3f2c31d9
MM
639}
640
18445c4d 641static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
642{
643 struct virtnet_info *vi = rvq->vdev->priv;
986a4f4d 644 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
e9d7417b 645
18445c4d 646 /* Schedule NAPI, Suppress further interrupts if successful. */
e9d7417b 647 if (napi_schedule_prep(&rq->napi)) {
1915a712 648 virtqueue_disable_cb(rvq);
e9d7417b 649 __napi_schedule(&rq->napi);
18445c4d 650 }
296f96fc
RR
651}
652
e9d7417b 653static void virtnet_napi_enable(struct receive_queue *rq)
3e9d08ec 654{
e9d7417b 655 napi_enable(&rq->napi);
3e9d08ec
BR
656
657 /* If all buffers were filled by other side before we napi_enabled, we
658 * won't get another interrupt, so process any outstanding packets
659 * now. virtnet_poll wants re-enable the queue, so we disable here.
660 * We synchronize against interrupts via NAPI_STATE_SCHED */
e9d7417b
JW
661 if (napi_schedule_prep(&rq->napi)) {
662 virtqueue_disable_cb(rq->vq);
ec13ee80 663 local_bh_disable();
e9d7417b 664 __napi_schedule(&rq->napi);
ec13ee80 665 local_bh_enable();
3e9d08ec
BR
666 }
667}
668
3161e453
RR
669static void refill_work(struct work_struct *work)
670{
e9d7417b
JW
671 struct virtnet_info *vi =
672 container_of(work, struct virtnet_info, refill.work);
3161e453 673 bool still_empty;
986a4f4d
JW
674 int i;
675
55257d72 676 for (i = 0; i < vi->curr_queue_pairs; i++) {
986a4f4d 677 struct receive_queue *rq = &vi->rq[i];
3161e453 678
986a4f4d
JW
679 napi_disable(&rq->napi);
680 still_empty = !try_fill_recv(rq, GFP_KERNEL);
681 virtnet_napi_enable(rq);
3161e453 682
986a4f4d
JW
683 /* In theory, this can happen: if we don't get any buffers in
684 * we will *never* try to fill again.
685 */
686 if (still_empty)
687 schedule_delayed_work(&vi->refill, HZ/2);
688 }
3161e453
RR
689}
690
296f96fc
RR
691static int virtnet_poll(struct napi_struct *napi, int budget)
692{
e9d7417b
JW
693 struct receive_queue *rq =
694 container_of(napi, struct receive_queue, napi);
695 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc 696 void *buf;
cbdadbbf 697 unsigned int r, len, received = 0;
296f96fc
RR
698
699again:
700 while (received < budget &&
e9d7417b
JW
701 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
702 receive_buf(rq, buf, len);
703 --rq->num;
296f96fc
RR
704 received++;
705 }
706
e9d7417b
JW
707 if (rq->num < rq->max / 2) {
708 if (!try_fill_recv(rq, GFP_ATOMIC))
3b07e9ca 709 schedule_delayed_work(&vi->refill, 0);
3161e453 710 }
296f96fc 711
8329d98e
RR
712 /* Out of packets? */
713 if (received < budget) {
cbdadbbf 714 r = virtqueue_enable_cb_prepare(rq->vq);
288379f0 715 napi_complete(napi);
cbdadbbf 716 if (unlikely(virtqueue_poll(rq->vq, r)) &&
8e95a202 717 napi_schedule_prep(napi)) {
e9d7417b 718 virtqueue_disable_cb(rq->vq);
288379f0 719 __napi_schedule(napi);
296f96fc 720 goto again;
4265f161 721 }
296f96fc
RR
722 }
723
724 return received;
725}
726
986a4f4d
JW
727static int virtnet_open(struct net_device *dev)
728{
729 struct virtnet_info *vi = netdev_priv(dev);
730 int i;
731
e4166625
JW
732 for (i = 0; i < vi->max_queue_pairs; i++) {
733 if (i < vi->curr_queue_pairs)
734 /* Make sure we have some buffers: if oom use wq. */
735 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
736 schedule_delayed_work(&vi->refill, 0);
986a4f4d
JW
737 virtnet_napi_enable(&vi->rq[i]);
738 }
739
740 return 0;
741}
742
b7dfde95 743static void free_old_xmit_skbs(struct send_queue *sq)
296f96fc
RR
744{
745 struct sk_buff *skb;
6ee57bcc 746 unsigned int len;
e9d7417b 747 struct virtnet_info *vi = sq->vq->vdev->priv;
58472a76 748 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
296f96fc 749
e9d7417b 750 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
296f96fc 751 pr_debug("Sent skb %p\n", skb);
3fa2a1df 752
83a27052 753 u64_stats_update_begin(&stats->tx_syncp);
3fa2a1df 754 stats->tx_bytes += skb->len;
755 stats->tx_packets++;
83a27052 756 u64_stats_update_end(&stats->tx_syncp);
3fa2a1df 757
ed79bab8 758 dev_kfree_skb_any(skb);
296f96fc
RR
759 }
760}
761
e9d7417b 762static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
296f96fc 763{
e7428e95 764 struct skb_vnet_hdr *hdr;
296f96fc 765 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
e9d7417b 766 struct virtnet_info *vi = sq->vq->vdev->priv;
7bedc7dc 767 unsigned num_sg;
e7428e95
MT
768 unsigned hdr_len;
769 bool can_push;
296f96fc 770
e174961c 771 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
e7428e95
MT
772 if (vi->mergeable_rx_bufs)
773 hdr_len = sizeof hdr->mhdr;
774 else
775 hdr_len = sizeof hdr->hdr;
776
777 can_push = vi->any_header_sg &&
778 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
779 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
780 /* Even if we can, don't push here yet as this would skew
781 * csum_start offset below. */
782 if (can_push)
783 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
784 else
785 hdr = skb_vnet_hdr(skb);
296f96fc 786
296f96fc 787 if (skb->ip_summed == CHECKSUM_PARTIAL) {
b3f24698 788 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
55508d60 789 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
b3f24698 790 hdr->hdr.csum_offset = skb->csum_offset;
296f96fc 791 } else {
b3f24698
RR
792 hdr->hdr.flags = 0;
793 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
296f96fc
RR
794 }
795
796 if (skb_is_gso(skb)) {
b3f24698
RR
797 hdr->hdr.hdr_len = skb_headlen(skb);
798 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
34a48579 799 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
b3f24698 800 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
296f96fc 801 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
b3f24698 802 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
296f96fc 803 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
b3f24698 804 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
296f96fc
RR
805 else
806 BUG();
34a48579 807 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
b3f24698 808 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
296f96fc 809 } else {
b3f24698
RR
810 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
811 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
296f96fc
RR
812 }
813
3f2c31d9 814 if (vi->mergeable_rx_bufs)
e7428e95 815 hdr->mhdr.num_buffers = 0;
3f2c31d9 816
e7428e95
MT
817 if (can_push) {
818 __skb_push(skb, hdr_len);
819 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
820 /* Pull header back to avoid skew in tx bytes calculations. */
821 __skb_pull(skb, hdr_len);
822 } else {
823 sg_set_buf(sq->sg, hdr, hdr_len);
824 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
825 }
9dc7b9e4 826 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
11a3a154
RR
827}
828
424efe9c 829static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
99ffc696
RR
830{
831 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d
JW
832 int qnum = skb_get_queue_mapping(skb);
833 struct send_queue *sq = &vi->sq[qnum];
9ed4cb07 834 int err;
2cb9c6ba 835
2cb9c6ba 836 /* Free up any pending old buffers before queueing new ones. */
e9d7417b 837 free_old_xmit_skbs(sq);
99ffc696 838
03f191ba 839 /* Try to transmit */
b7dfde95 840 err = xmit_skb(sq, skb);
48925e37 841
9ed4cb07 842 /* This should not happen! */
67975901 843 if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
9ed4cb07
RR
844 dev->stats.tx_fifo_errors++;
845 if (net_ratelimit())
846 dev_warn(&dev->dev,
b7dfde95 847 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
58eba97d
RR
848 dev->stats.tx_dropped++;
849 kfree_skb(skb);
850 return NETDEV_TX_OK;
296f96fc 851 }
03f191ba 852
48925e37
RR
853 /* Don't wait up for transmitted skbs to be freed. */
854 skb_orphan(skb);
855 nf_reset(skb);
856
857 /* Apparently nice girls don't return TX_BUSY; stop the queue
858 * before it gets out of hand. Naturally, this wastes entries. */
b7dfde95 859 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
986a4f4d 860 netif_stop_subqueue(dev, qnum);
e9d7417b 861 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
48925e37 862 /* More just got used, free them then recheck. */
b7dfde95
LT
863 free_old_xmit_skbs(sq);
864 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
986a4f4d 865 netif_start_subqueue(dev, qnum);
e9d7417b 866 virtqueue_disable_cb(sq->vq);
48925e37
RR
867 }
868 }
99ffc696 869 }
48925e37
RR
870
871 return NETDEV_TX_OK;
296f96fc
RR
872}
873
40cbfc37
AK
874/*
875 * Send command via the control virtqueue and check status. Commands
876 * supported by the hypervisor, as indicated by feature bits, should
877 * never fail unless improperly formated.
878 */
879static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
f7bc9594
RR
880 struct scatterlist *out,
881 struct scatterlist *in)
40cbfc37 882{
f7bc9594 883 struct scatterlist *sgs[4], hdr, stat;
40cbfc37
AK
884 struct virtio_net_ctrl_hdr ctrl;
885 virtio_net_ctrl_ack status = ~0;
f7bc9594 886 unsigned out_num = 0, in_num = 0, tmp;
40cbfc37
AK
887
888 /* Caller should know better */
f7bc9594 889 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
40cbfc37
AK
890
891 ctrl.class = class;
892 ctrl.cmd = cmd;
f7bc9594
RR
893 /* Add header */
894 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
895 sgs[out_num++] = &hdr;
40cbfc37 896
f7bc9594
RR
897 if (out)
898 sgs[out_num++] = out;
899 if (in)
900 sgs[out_num + in_num++] = in;
40cbfc37 901
f7bc9594
RR
902 /* Add return status. */
903 sg_init_one(&stat, &status, sizeof(status));
904 sgs[out_num + in_num++] = &stat;
40cbfc37 905
f7bc9594
RR
906 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
907 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
908 < 0);
40cbfc37 909
67975901
HG
910 if (unlikely(!virtqueue_kick(vi->cvq)))
911 return status == VIRTIO_NET_OK;
40cbfc37
AK
912
913 /* Spin for a response, the kick causes an ioport write, trapping
914 * into the hypervisor, so the request should be handled immediately.
915 */
047b9b94
HG
916 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
917 !virtqueue_is_broken(vi->cvq))
40cbfc37
AK
918 cpu_relax();
919
920 return status == VIRTIO_NET_OK;
921}
922
9c46f6d4
AW
923static int virtnet_set_mac_address(struct net_device *dev, void *p)
924{
925 struct virtnet_info *vi = netdev_priv(dev);
926 struct virtio_device *vdev = vi->vdev;
f2f2c8b4 927 int ret;
7e58d5ae
AK
928 struct sockaddr *addr = p;
929 struct scatterlist sg;
9c46f6d4 930
7e58d5ae 931 ret = eth_prepare_mac_addr_change(dev, p);
f2f2c8b4
JP
932 if (ret)
933 return ret;
9c46f6d4 934
7e58d5ae
AK
935 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
936 sg_init_one(&sg, addr->sa_data, dev->addr_len);
937 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
938 VIRTIO_NET_CTRL_MAC_ADDR_SET,
f7bc9594 939 &sg, NULL)) {
7e58d5ae
AK
940 dev_warn(&vdev->dev,
941 "Failed to set mac address by vq command.\n");
942 return -EINVAL;
943 }
944 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
855e0c52
RR
945 unsigned int i;
946
947 /* Naturally, this has an atomicity problem. */
948 for (i = 0; i < dev->addr_len; i++)
949 virtio_cwrite8(vdev,
950 offsetof(struct virtio_net_config, mac) +
951 i, addr->sa_data[i]);
7e58d5ae
AK
952 }
953
954 eth_commit_mac_addr_change(dev, p);
9c46f6d4
AW
955
956 return 0;
957}
958
3fa2a1df 959static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
960 struct rtnl_link_stats64 *tot)
961{
962 struct virtnet_info *vi = netdev_priv(dev);
963 int cpu;
964 unsigned int start;
965
966 for_each_possible_cpu(cpu) {
58472a76 967 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
3fa2a1df 968 u64 tpackets, tbytes, rpackets, rbytes;
969
970 do {
e3906486 971 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
3fa2a1df 972 tpackets = stats->tx_packets;
973 tbytes = stats->tx_bytes;
e3906486 974 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
83a27052
ED
975
976 do {
e3906486 977 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
3fa2a1df 978 rpackets = stats->rx_packets;
979 rbytes = stats->rx_bytes;
e3906486 980 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
3fa2a1df 981
982 tot->rx_packets += rpackets;
983 tot->tx_packets += tpackets;
984 tot->rx_bytes += rbytes;
985 tot->tx_bytes += tbytes;
986 }
987
988 tot->tx_dropped = dev->stats.tx_dropped;
021ac8d3 989 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
3fa2a1df 990 tot->rx_dropped = dev->stats.rx_dropped;
991 tot->rx_length_errors = dev->stats.rx_length_errors;
992 tot->rx_frame_errors = dev->stats.rx_frame_errors;
993
994 return tot;
995}
996
da74e89d
AS
997#ifdef CONFIG_NET_POLL_CONTROLLER
998static void virtnet_netpoll(struct net_device *dev)
999{
1000 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1001 int i;
da74e89d 1002
986a4f4d
JW
1003 for (i = 0; i < vi->curr_queue_pairs; i++)
1004 napi_schedule(&vi->rq[i].napi);
da74e89d
AS
1005}
1006#endif
1007
586d17c5
JW
1008static void virtnet_ack_link_announce(struct virtnet_info *vi)
1009{
1010 rtnl_lock();
1011 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
f7bc9594 1012 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
586d17c5
JW
1013 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1014 rtnl_unlock();
1015}
1016
986a4f4d
JW
1017static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1018{
1019 struct scatterlist sg;
1020 struct virtio_net_ctrl_mq s;
1021 struct net_device *dev = vi->dev;
1022
1023 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1024 return 0;
1025
1026 s.virtqueue_pairs = queue_pairs;
1027 sg_init_one(&sg, &s, sizeof(s));
1028
1029 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
f7bc9594 1030 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
986a4f4d
JW
1031 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1032 queue_pairs);
1033 return -EINVAL;
55257d72 1034 } else {
986a4f4d 1035 vi->curr_queue_pairs = queue_pairs;
35ed159b
JW
1036 /* virtnet_open() will refill when device is going to up. */
1037 if (dev->flags & IFF_UP)
1038 schedule_delayed_work(&vi->refill, 0);
55257d72 1039 }
986a4f4d
JW
1040
1041 return 0;
1042}
1043
296f96fc
RR
1044static int virtnet_close(struct net_device *dev)
1045{
1046 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1047 int i;
296f96fc 1048
b2baed69
RR
1049 /* Make sure refill_work doesn't re-enable napi! */
1050 cancel_delayed_work_sync(&vi->refill);
986a4f4d
JW
1051
1052 for (i = 0; i < vi->max_queue_pairs; i++)
1053 napi_disable(&vi->rq[i].napi);
296f96fc 1054
296f96fc
RR
1055 return 0;
1056}
1057
2af7698e
AW
1058static void virtnet_set_rx_mode(struct net_device *dev)
1059{
1060 struct virtnet_info *vi = netdev_priv(dev);
f565a7c2 1061 struct scatterlist sg[2];
2af7698e 1062 u8 promisc, allmulti;
f565a7c2 1063 struct virtio_net_ctrl_mac *mac_data;
ccffad25 1064 struct netdev_hw_addr *ha;
32e7bfc4 1065 int uc_count;
4cd24eaf 1066 int mc_count;
f565a7c2
AW
1067 void *buf;
1068 int i;
2af7698e
AW
1069
1070 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
1071 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1072 return;
1073
f565a7c2
AW
1074 promisc = ((dev->flags & IFF_PROMISC) != 0);
1075 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2af7698e 1076
23e258e1 1077 sg_init_one(sg, &promisc, sizeof(promisc));
2af7698e
AW
1078
1079 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1080 VIRTIO_NET_CTRL_RX_PROMISC,
f7bc9594 1081 sg, NULL))
2af7698e
AW
1082 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1083 promisc ? "en" : "dis");
1084
23e258e1 1085 sg_init_one(sg, &allmulti, sizeof(allmulti));
2af7698e
AW
1086
1087 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1088 VIRTIO_NET_CTRL_RX_ALLMULTI,
f7bc9594 1089 sg, NULL))
2af7698e
AW
1090 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1091 allmulti ? "en" : "dis");
f565a7c2 1092
32e7bfc4 1093 uc_count = netdev_uc_count(dev);
4cd24eaf 1094 mc_count = netdev_mc_count(dev);
f565a7c2 1095 /* MAC filter - use one buffer for both lists */
4cd24eaf
JP
1096 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1097 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1098 mac_data = buf;
e68ed8f0 1099 if (!buf)
f565a7c2 1100 return;
f565a7c2 1101
23e258e1
AW
1102 sg_init_table(sg, 2);
1103
f565a7c2 1104 /* Store the unicast list and count in the front of the buffer */
32e7bfc4 1105 mac_data->entries = uc_count;
ccffad25 1106 i = 0;
32e7bfc4 1107 netdev_for_each_uc_addr(ha, dev)
ccffad25 1108 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1109
1110 sg_set_buf(&sg[0], mac_data,
32e7bfc4 1111 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
f565a7c2
AW
1112
1113 /* multicast list and count fill the end */
32e7bfc4 1114 mac_data = (void *)&mac_data->macs[uc_count][0];
f565a7c2 1115
4cd24eaf 1116 mac_data->entries = mc_count;
567ec874 1117 i = 0;
22bedad3
JP
1118 netdev_for_each_mc_addr(ha, dev)
1119 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1120
1121 sg_set_buf(&sg[1], mac_data,
4cd24eaf 1122 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
f565a7c2
AW
1123
1124 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1125 VIRTIO_NET_CTRL_MAC_TABLE_SET,
f7bc9594 1126 sg, NULL))
99e872ae 1127 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
f565a7c2
AW
1128
1129 kfree(buf);
2af7698e
AW
1130}
1131
80d5c368
PM
1132static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1133 __be16 proto, u16 vid)
0bde9569
AW
1134{
1135 struct virtnet_info *vi = netdev_priv(dev);
1136 struct scatterlist sg;
1137
23e258e1 1138 sg_init_one(&sg, &vid, sizeof(vid));
0bde9569
AW
1139
1140 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
f7bc9594 1141 VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
0bde9569 1142 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8e586137 1143 return 0;
0bde9569
AW
1144}
1145
80d5c368
PM
1146static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1147 __be16 proto, u16 vid)
0bde9569
AW
1148{
1149 struct virtnet_info *vi = netdev_priv(dev);
1150 struct scatterlist sg;
1151
23e258e1 1152 sg_init_one(&sg, &vid, sizeof(vid));
0bde9569
AW
1153
1154 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
f7bc9594 1155 VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
0bde9569 1156 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8e586137 1157 return 0;
0bde9569
AW
1158}
1159
8898c21c 1160static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
986a4f4d
JW
1161{
1162 int i;
1163
8898c21c
WG
1164 if (vi->affinity_hint_set) {
1165 for (i = 0; i < vi->max_queue_pairs; i++) {
47be2479
WG
1166 virtqueue_set_affinity(vi->rq[i].vq, -1);
1167 virtqueue_set_affinity(vi->sq[i].vq, -1);
1168 }
1169
8898c21c
WG
1170 vi->affinity_hint_set = false;
1171 }
8898c21c 1172}
47be2479 1173
8898c21c
WG
1174static void virtnet_set_affinity(struct virtnet_info *vi)
1175{
1176 int i;
1177 int cpu;
986a4f4d
JW
1178
1179 /* In multiqueue mode, when the number of cpu is equal to the number of
1180 * queue pairs, we let the queue pairs to be private to one cpu by
1181 * setting the affinity hint to eliminate the contention.
1182 */
8898c21c
WG
1183 if (vi->curr_queue_pairs == 1 ||
1184 vi->max_queue_pairs != num_online_cpus()) {
1185 virtnet_clean_affinity(vi, -1);
1186 return;
986a4f4d
JW
1187 }
1188
8898c21c
WG
1189 i = 0;
1190 for_each_online_cpu(cpu) {
986a4f4d
JW
1191 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1192 virtqueue_set_affinity(vi->sq[i].vq, cpu);
9bb8ca86 1193 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
8898c21c 1194 i++;
986a4f4d
JW
1195 }
1196
8898c21c 1197 vi->affinity_hint_set = true;
986a4f4d
JW
1198}
1199
8de4b2f3
WG
1200static int virtnet_cpu_callback(struct notifier_block *nfb,
1201 unsigned long action, void *hcpu)
1202{
1203 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1204
1205 switch(action & ~CPU_TASKS_FROZEN) {
1206 case CPU_ONLINE:
1207 case CPU_DOWN_FAILED:
1208 case CPU_DEAD:
1209 virtnet_set_affinity(vi);
1210 break;
1211 case CPU_DOWN_PREPARE:
1212 virtnet_clean_affinity(vi, (long)hcpu);
1213 break;
1214 default:
1215 break;
1216 }
3ab098df 1217
8de4b2f3 1218 return NOTIFY_OK;
986a4f4d
JW
1219}
1220
8f9f4668
RJ
1221static void virtnet_get_ringparam(struct net_device *dev,
1222 struct ethtool_ringparam *ring)
1223{
1224 struct virtnet_info *vi = netdev_priv(dev);
1225
986a4f4d
JW
1226 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1227 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
8f9f4668
RJ
1228 ring->rx_pending = ring->rx_max_pending;
1229 ring->tx_pending = ring->tx_max_pending;
8f9f4668
RJ
1230}
1231
66846048
RJ
1232
1233static void virtnet_get_drvinfo(struct net_device *dev,
1234 struct ethtool_drvinfo *info)
1235{
1236 struct virtnet_info *vi = netdev_priv(dev);
1237 struct virtio_device *vdev = vi->vdev;
1238
1239 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1240 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1241 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1242
1243}
1244
d73bcd2c
JW
1245/* TODO: Eliminate OOO packets during switching */
1246static int virtnet_set_channels(struct net_device *dev,
1247 struct ethtool_channels *channels)
1248{
1249 struct virtnet_info *vi = netdev_priv(dev);
1250 u16 queue_pairs = channels->combined_count;
1251 int err;
1252
1253 /* We don't support separate rx/tx channels.
1254 * We don't allow setting 'other' channels.
1255 */
1256 if (channels->rx_count || channels->tx_count || channels->other_count)
1257 return -EINVAL;
1258
1259 if (queue_pairs > vi->max_queue_pairs)
1260 return -EINVAL;
1261
47be2479 1262 get_online_cpus();
d73bcd2c
JW
1263 err = virtnet_set_queues(vi, queue_pairs);
1264 if (!err) {
1265 netif_set_real_num_tx_queues(dev, queue_pairs);
1266 netif_set_real_num_rx_queues(dev, queue_pairs);
1267
8898c21c 1268 virtnet_set_affinity(vi);
d73bcd2c 1269 }
47be2479 1270 put_online_cpus();
d73bcd2c
JW
1271
1272 return err;
1273}
1274
1275static void virtnet_get_channels(struct net_device *dev,
1276 struct ethtool_channels *channels)
1277{
1278 struct virtnet_info *vi = netdev_priv(dev);
1279
1280 channels->combined_count = vi->curr_queue_pairs;
1281 channels->max_combined = vi->max_queue_pairs;
1282 channels->max_other = 0;
1283 channels->rx_count = 0;
1284 channels->tx_count = 0;
1285 channels->other_count = 0;
1286}
1287
0fc0b732 1288static const struct ethtool_ops virtnet_ethtool_ops = {
66846048 1289 .get_drvinfo = virtnet_get_drvinfo,
9f4d26d0 1290 .get_link = ethtool_op_get_link,
8f9f4668 1291 .get_ringparam = virtnet_get_ringparam,
d73bcd2c
JW
1292 .set_channels = virtnet_set_channels,
1293 .get_channels = virtnet_get_channels,
a9ea3fc6
HX
1294};
1295
39da5814
MM
1296#define MIN_MTU 68
1297#define MAX_MTU 65535
1298
1299static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1300{
1301 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1302 return -EINVAL;
1303 dev->mtu = new_mtu;
1304 return 0;
1305}
1306
76288b4e
SH
1307static const struct net_device_ops virtnet_netdev = {
1308 .ndo_open = virtnet_open,
1309 .ndo_stop = virtnet_close,
1310 .ndo_start_xmit = start_xmit,
1311 .ndo_validate_addr = eth_validate_addr,
9c46f6d4 1312 .ndo_set_mac_address = virtnet_set_mac_address,
2af7698e 1313 .ndo_set_rx_mode = virtnet_set_rx_mode,
76288b4e 1314 .ndo_change_mtu = virtnet_change_mtu,
3fa2a1df 1315 .ndo_get_stats64 = virtnet_stats,
1824a989
AW
1316 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1317 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
76288b4e
SH
1318#ifdef CONFIG_NET_POLL_CONTROLLER
1319 .ndo_poll_controller = virtnet_netpoll,
1320#endif
1321};
1322
586d17c5 1323static void virtnet_config_changed_work(struct work_struct *work)
9f4d26d0 1324{
586d17c5
JW
1325 struct virtnet_info *vi =
1326 container_of(work, struct virtnet_info, config_work);
9f4d26d0
MM
1327 u16 v;
1328
586d17c5
JW
1329 mutex_lock(&vi->config_lock);
1330 if (!vi->config_enable)
1331 goto done;
1332
855e0c52
RR
1333 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1334 struct virtio_net_config, status, &v) < 0)
586d17c5
JW
1335 goto done;
1336
1337 if (v & VIRTIO_NET_S_ANNOUNCE) {
ee89bab1 1338 netdev_notify_peers(vi->dev);
586d17c5
JW
1339 virtnet_ack_link_announce(vi);
1340 }
9f4d26d0
MM
1341
1342 /* Ignore unknown (future) status bits */
1343 v &= VIRTIO_NET_S_LINK_UP;
1344
1345 if (vi->status == v)
586d17c5 1346 goto done;
9f4d26d0
MM
1347
1348 vi->status = v;
1349
1350 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1351 netif_carrier_on(vi->dev);
986a4f4d 1352 netif_tx_wake_all_queues(vi->dev);
9f4d26d0
MM
1353 } else {
1354 netif_carrier_off(vi->dev);
986a4f4d 1355 netif_tx_stop_all_queues(vi->dev);
9f4d26d0 1356 }
586d17c5
JW
1357done:
1358 mutex_unlock(&vi->config_lock);
9f4d26d0
MM
1359}
1360
1361static void virtnet_config_changed(struct virtio_device *vdev)
1362{
1363 struct virtnet_info *vi = vdev->priv;
1364
3b07e9ca 1365 schedule_work(&vi->config_work);
9f4d26d0
MM
1366}
1367
986a4f4d
JW
1368static void virtnet_free_queues(struct virtnet_info *vi)
1369{
1370 kfree(vi->rq);
1371 kfree(vi->sq);
1372}
1373
1374static void free_receive_bufs(struct virtnet_info *vi)
1375{
1376 int i;
1377
1378 for (i = 0; i < vi->max_queue_pairs; i++) {
1379 while (vi->rq[i].pages)
1380 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1381 }
1382}
1383
1384static void free_unused_bufs(struct virtnet_info *vi)
1385{
1386 void *buf;
1387 int i;
1388
1389 for (i = 0; i < vi->max_queue_pairs; i++) {
1390 struct virtqueue *vq = vi->sq[i].vq;
1391 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1392 dev_kfree_skb(buf);
1393 }
1394
1395 for (i = 0; i < vi->max_queue_pairs; i++) {
1396 struct virtqueue *vq = vi->rq[i].vq;
1397
1398 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
fa9fac17 1399 if (vi->mergeable_rx_bufs)
2613af0e 1400 put_page(virt_to_head_page(buf));
fa9fac17
AV
1401 else if (vi->big_packets)
1402 give_pages(&vi->rq[i], buf);
986a4f4d
JW
1403 else
1404 dev_kfree_skb(buf);
1405 --vi->rq[i].num;
1406 }
1407 BUG_ON(vi->rq[i].num != 0);
1408 }
1409}
1410
e9d7417b
JW
1411static void virtnet_del_vqs(struct virtnet_info *vi)
1412{
1413 struct virtio_device *vdev = vi->vdev;
1414
8898c21c 1415 virtnet_clean_affinity(vi, -1);
986a4f4d 1416
e9d7417b 1417 vdev->config->del_vqs(vdev);
986a4f4d
JW
1418
1419 virtnet_free_queues(vi);
e9d7417b
JW
1420}
1421
986a4f4d 1422static int virtnet_find_vqs(struct virtnet_info *vi)
3f9c10b0 1423{
986a4f4d
JW
1424 vq_callback_t **callbacks;
1425 struct virtqueue **vqs;
1426 int ret = -ENOMEM;
1427 int i, total_vqs;
1428 const char **names;
1429
1430 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1431 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1432 * possible control vq.
1433 */
1434 total_vqs = vi->max_queue_pairs * 2 +
1435 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1436
1437 /* Allocate space for find_vqs parameters */
1438 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1439 if (!vqs)
1440 goto err_vq;
1441 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1442 if (!callbacks)
1443 goto err_callback;
1444 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1445 if (!names)
1446 goto err_names;
1447
1448 /* Parameters for control virtqueue, if any */
1449 if (vi->has_cvq) {
1450 callbacks[total_vqs - 1] = NULL;
1451 names[total_vqs - 1] = "control";
1452 }
3f9c10b0 1453
986a4f4d
JW
1454 /* Allocate/initialize parameters for send/receive virtqueues */
1455 for (i = 0; i < vi->max_queue_pairs; i++) {
1456 callbacks[rxq2vq(i)] = skb_recv_done;
1457 callbacks[txq2vq(i)] = skb_xmit_done;
1458 sprintf(vi->rq[i].name, "input.%d", i);
1459 sprintf(vi->sq[i].name, "output.%d", i);
1460 names[rxq2vq(i)] = vi->rq[i].name;
1461 names[txq2vq(i)] = vi->sq[i].name;
1462 }
3f9c10b0 1463
986a4f4d
JW
1464 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1465 names);
1466 if (ret)
1467 goto err_find;
3f9c10b0 1468
986a4f4d
JW
1469 if (vi->has_cvq) {
1470 vi->cvq = vqs[total_vqs - 1];
3f9c10b0 1471 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
f646968f 1472 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3f9c10b0 1473 }
986a4f4d
JW
1474
1475 for (i = 0; i < vi->max_queue_pairs; i++) {
1476 vi->rq[i].vq = vqs[rxq2vq(i)];
1477 vi->sq[i].vq = vqs[txq2vq(i)];
1478 }
1479
1480 kfree(names);
1481 kfree(callbacks);
1482 kfree(vqs);
1483
3f9c10b0 1484 return 0;
986a4f4d
JW
1485
1486err_find:
1487 kfree(names);
1488err_names:
1489 kfree(callbacks);
1490err_callback:
1491 kfree(vqs);
1492err_vq:
1493 return ret;
1494}
1495
1496static int virtnet_alloc_queues(struct virtnet_info *vi)
1497{
1498 int i;
1499
1500 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1501 if (!vi->sq)
1502 goto err_sq;
1503 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
008d4278 1504 if (!vi->rq)
986a4f4d
JW
1505 goto err_rq;
1506
1507 INIT_DELAYED_WORK(&vi->refill, refill_work);
1508 for (i = 0; i < vi->max_queue_pairs; i++) {
1509 vi->rq[i].pages = NULL;
1510 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1511 napi_weight);
1512
1513 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1514 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1515 }
1516
1517 return 0;
1518
1519err_rq:
1520 kfree(vi->sq);
1521err_sq:
1522 return -ENOMEM;
1523}
1524
1525static int init_vqs(struct virtnet_info *vi)
1526{
1527 int ret;
1528
1529 /* Allocate send & receive queues */
1530 ret = virtnet_alloc_queues(vi);
1531 if (ret)
1532 goto err;
1533
1534 ret = virtnet_find_vqs(vi);
1535 if (ret)
1536 goto err_free;
1537
47be2479 1538 get_online_cpus();
8898c21c 1539 virtnet_set_affinity(vi);
47be2479
WG
1540 put_online_cpus();
1541
986a4f4d
JW
1542 return 0;
1543
1544err_free:
1545 virtnet_free_queues(vi);
1546err:
1547 return ret;
3f9c10b0
AS
1548}
1549
296f96fc
RR
1550static int virtnet_probe(struct virtio_device *vdev)
1551{
986a4f4d 1552 int i, err;
296f96fc
RR
1553 struct net_device *dev;
1554 struct virtnet_info *vi;
986a4f4d
JW
1555 u16 max_queue_pairs;
1556
1557 /* Find if host supports multiqueue virtio_net device */
855e0c52
RR
1558 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1559 struct virtio_net_config,
1560 max_virtqueue_pairs, &max_queue_pairs);
986a4f4d
JW
1561
1562 /* We need at least 2 queue's */
1563 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1564 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1565 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1566 max_queue_pairs = 1;
296f96fc
RR
1567
1568 /* Allocate ourselves a network device with room for our info */
986a4f4d 1569 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
296f96fc
RR
1570 if (!dev)
1571 return -ENOMEM;
1572
1573 /* Set up network device as normal. */
f2f2c8b4 1574 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
76288b4e 1575 dev->netdev_ops = &virtnet_netdev;
296f96fc 1576 dev->features = NETIF_F_HIGHDMA;
3fa2a1df 1577
a9ea3fc6 1578 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
296f96fc
RR
1579 SET_NETDEV_DEV(dev, &vdev->dev);
1580
1581 /* Do we support "hardware" checksums? */
98e778c9 1582 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc 1583 /* This opens up the world of extra features. */
98e778c9
MM
1584 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1585 if (csum)
1586 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1587
1588 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1589 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
34a48579
RR
1590 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1591 }
5539ae96 1592 /* Individual feature bits: what can host handle? */
98e778c9
MM
1593 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1594 dev->hw_features |= NETIF_F_TSO;
1595 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1596 dev->hw_features |= NETIF_F_TSO6;
1597 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1598 dev->hw_features |= NETIF_F_TSO_ECN;
1599 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1600 dev->hw_features |= NETIF_F_UFO;
1601
1602 if (gso)
1603 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1604 /* (!csum && gso) case will be fixed by register_netdev() */
296f96fc 1605 }
4f49129b
TH
1606 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1607 dev->features |= NETIF_F_RXCSUM;
296f96fc 1608
4fda8302
JW
1609 dev->vlan_features = dev->features;
1610
296f96fc 1611 /* Configuration may specify what MAC to use. Otherwise random. */
855e0c52
RR
1612 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1613 virtio_cread_bytes(vdev,
1614 offsetof(struct virtio_net_config, mac),
1615 dev->dev_addr, dev->addr_len);
1616 else
f2cedb63 1617 eth_hw_addr_random(dev);
296f96fc
RR
1618
1619 /* Set up our device-specific information */
1620 vi = netdev_priv(dev);
296f96fc
RR
1621 vi->dev = dev;
1622 vi->vdev = vdev;
d9d5dcc8 1623 vdev->priv = vi;
3fa2a1df 1624 vi->stats = alloc_percpu(struct virtnet_stats);
1625 err = -ENOMEM;
1626 if (vi->stats == NULL)
1627 goto free;
1628
827da44c
JS
1629 for_each_possible_cpu(i) {
1630 struct virtnet_stats *virtnet_stats;
1631 virtnet_stats = per_cpu_ptr(vi->stats, i);
1632 u64_stats_init(&virtnet_stats->tx_syncp);
1633 u64_stats_init(&virtnet_stats->rx_syncp);
1634 }
1635
586d17c5
JW
1636 mutex_init(&vi->config_lock);
1637 vi->config_enable = true;
1638 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
296f96fc 1639
97402b96 1640 /* If we can receive ANY GSO packets, we must allocate large ones. */
8e95a202
JP
1641 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1642 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1643 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
97402b96
HX
1644 vi->big_packets = true;
1645
3f2c31d9
MM
1646 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1647 vi->mergeable_rx_bufs = true;
1648
e7428e95
MT
1649 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1650 vi->any_header_sg = true;
1651
986a4f4d
JW
1652 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1653 vi->has_cvq = true;
1654
1655 /* Use single tx/rx queue pair as default */
1656 vi->curr_queue_pairs = 1;
1657 vi->max_queue_pairs = max_queue_pairs;
1658
1659 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3f9c10b0 1660 err = init_vqs(vi);
d2a7ddda 1661 if (err)
9bb8ca86 1662 goto free_stats;
296f96fc 1663
0f13b66b
ZYW
1664 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1665 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
986a4f4d 1666
296f96fc
RR
1667 err = register_netdev(dev);
1668 if (err) {
1669 pr_debug("virtio_net: registering device failed\n");
d2a7ddda 1670 goto free_vqs;
296f96fc 1671 }
b3369c1f
RR
1672
1673 /* Last of all, set up some receive buffers. */
55257d72 1674 for (i = 0; i < vi->curr_queue_pairs; i++) {
986a4f4d
JW
1675 try_fill_recv(&vi->rq[i], GFP_KERNEL);
1676
1677 /* If we didn't even get one input buffer, we're useless. */
1678 if (vi->rq[i].num == 0) {
1679 free_unused_bufs(vi);
1680 err = -ENOMEM;
1681 goto free_recv_bufs;
1682 }
b3369c1f
RR
1683 }
1684
8de4b2f3
WG
1685 vi->nb.notifier_call = &virtnet_cpu_callback;
1686 err = register_hotcpu_notifier(&vi->nb);
1687 if (err) {
1688 pr_debug("virtio_net: registering cpu notifier failed\n");
1689 goto free_recv_bufs;
1690 }
1691
167c25e4
JW
1692 /* Assume link up if device can't report link status,
1693 otherwise get link status from config. */
1694 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1695 netif_carrier_off(dev);
3b07e9ca 1696 schedule_work(&vi->config_work);
167c25e4
JW
1697 } else {
1698 vi->status = VIRTIO_NET_S_LINK_UP;
1699 netif_carrier_on(dev);
1700 }
9f4d26d0 1701
986a4f4d
JW
1702 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1703 dev->name, max_queue_pairs);
1704
296f96fc
RR
1705 return 0;
1706
986a4f4d
JW
1707free_recv_bufs:
1708 free_receive_bufs(vi);
b3369c1f 1709 unregister_netdev(dev);
d2a7ddda 1710free_vqs:
986a4f4d 1711 cancel_delayed_work_sync(&vi->refill);
e9d7417b 1712 virtnet_del_vqs(vi);
2613af0e
MD
1713 if (vi->alloc_frag.page)
1714 put_page(vi->alloc_frag.page);
3fa2a1df 1715free_stats:
1716 free_percpu(vi->stats);
296f96fc
RR
1717free:
1718 free_netdev(dev);
1719 return err;
1720}
1721
04486ed0 1722static void remove_vq_common(struct virtnet_info *vi)
296f96fc 1723{
04486ed0 1724 vi->vdev->config->reset(vi->vdev);
830a8a97
SM
1725
1726 /* Free unused buffers in both send and recv, if any. */
9ab86bbc 1727 free_unused_bufs(vi);
fb6813f4 1728
986a4f4d 1729 free_receive_bufs(vi);
d2a7ddda 1730
986a4f4d 1731 virtnet_del_vqs(vi);
04486ed0
AS
1732}
1733
8cc085d6 1734static void virtnet_remove(struct virtio_device *vdev)
04486ed0
AS
1735{
1736 struct virtnet_info *vi = vdev->priv;
1737
8de4b2f3
WG
1738 unregister_hotcpu_notifier(&vi->nb);
1739
586d17c5
JW
1740 /* Prevent config work handler from accessing the device. */
1741 mutex_lock(&vi->config_lock);
1742 vi->config_enable = false;
1743 mutex_unlock(&vi->config_lock);
1744
04486ed0
AS
1745 unregister_netdev(vi->dev);
1746
1747 remove_vq_common(vi);
2613af0e
MD
1748 if (vi->alloc_frag.page)
1749 put_page(vi->alloc_frag.page);
fb6813f4 1750
586d17c5
JW
1751 flush_work(&vi->config_work);
1752
2e66f55b 1753 free_percpu(vi->stats);
74b2553f 1754 free_netdev(vi->dev);
296f96fc
RR
1755}
1756
89107000 1757#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
1758static int virtnet_freeze(struct virtio_device *vdev)
1759{
1760 struct virtnet_info *vi = vdev->priv;
986a4f4d 1761 int i;
0741bcb5 1762
ec9debbd
JW
1763 unregister_hotcpu_notifier(&vi->nb);
1764
586d17c5
JW
1765 /* Prevent config work handler from accessing the device */
1766 mutex_lock(&vi->config_lock);
1767 vi->config_enable = false;
1768 mutex_unlock(&vi->config_lock);
1769
0741bcb5
AS
1770 netif_device_detach(vi->dev);
1771 cancel_delayed_work_sync(&vi->refill);
1772
1773 if (netif_running(vi->dev))
986a4f4d
JW
1774 for (i = 0; i < vi->max_queue_pairs; i++) {
1775 napi_disable(&vi->rq[i].napi);
1776 netif_napi_del(&vi->rq[i].napi);
1777 }
0741bcb5
AS
1778
1779 remove_vq_common(vi);
1780
586d17c5
JW
1781 flush_work(&vi->config_work);
1782
0741bcb5
AS
1783 return 0;
1784}
1785
1786static int virtnet_restore(struct virtio_device *vdev)
1787{
1788 struct virtnet_info *vi = vdev->priv;
986a4f4d 1789 int err, i;
0741bcb5
AS
1790
1791 err = init_vqs(vi);
1792 if (err)
1793 return err;
1794
1795 if (netif_running(vi->dev))
986a4f4d
JW
1796 for (i = 0; i < vi->max_queue_pairs; i++)
1797 virtnet_napi_enable(&vi->rq[i]);
0741bcb5
AS
1798
1799 netif_device_attach(vi->dev);
1800
55257d72 1801 for (i = 0; i < vi->curr_queue_pairs; i++)
986a4f4d
JW
1802 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1803 schedule_delayed_work(&vi->refill, 0);
0741bcb5 1804
586d17c5
JW
1805 mutex_lock(&vi->config_lock);
1806 vi->config_enable = true;
1807 mutex_unlock(&vi->config_lock);
1808
35ed159b 1809 rtnl_lock();
986a4f4d 1810 virtnet_set_queues(vi, vi->curr_queue_pairs);
35ed159b 1811 rtnl_unlock();
986a4f4d 1812
ec9debbd
JW
1813 err = register_hotcpu_notifier(&vi->nb);
1814 if (err)
1815 return err;
1816
0741bcb5
AS
1817 return 0;
1818}
1819#endif
1820
296f96fc
RR
1821static struct virtio_device_id id_table[] = {
1822 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1823 { 0 },
1824};
1825
c45a6816 1826static unsigned int features[] = {
5e4fe5c4
MM
1827 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1828 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
c45a6816 1829 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
97402b96 1830 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
5c516751 1831 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
2a41f71d 1832 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
0bde9569 1833 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
986a4f4d 1834 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
7e58d5ae 1835 VIRTIO_NET_F_CTRL_MAC_ADDR,
e7428e95 1836 VIRTIO_F_ANY_LAYOUT,
c45a6816
RR
1837};
1838
22402529 1839static struct virtio_driver virtio_net_driver = {
c45a6816
RR
1840 .feature_table = features,
1841 .feature_table_size = ARRAY_SIZE(features),
296f96fc
RR
1842 .driver.name = KBUILD_MODNAME,
1843 .driver.owner = THIS_MODULE,
1844 .id_table = id_table,
1845 .probe = virtnet_probe,
8cc085d6 1846 .remove = virtnet_remove,
9f4d26d0 1847 .config_changed = virtnet_config_changed,
89107000 1848#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
1849 .freeze = virtnet_freeze,
1850 .restore = virtnet_restore,
1851#endif
296f96fc
RR
1852};
1853
b2a17029 1854module_virtio_driver(virtio_net_driver);
296f96fc
RR
1855
1856MODULE_DEVICE_TABLE(virtio, id_table);
1857MODULE_DESCRIPTION("Virtio network driver");
1858MODULE_LICENSE("GPL");