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