]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/virtio_net.c
virtio-net: separate fields of sending/receiving queue from virtnet_info
[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>
296f96fc 29
6c0cd7c0
DL
30static int napi_weight = 128;
31module_param(napi_weight, int, 0444);
32
eb939922 33static bool csum = true, gso = true;
34a48579
RR
34module_param(csum, bool, 0444);
35module_param(gso, bool, 0444);
36
296f96fc 37/* FIXME: MTU in config. */
e918085a 38#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
3f2c31d9 39#define GOOD_COPY_LEN 128
296f96fc 40
f565a7c2 41#define VIRTNET_SEND_COMMAND_SG_MAX 2
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];
61};
62
63/* Internal representation of a receive virtqueue */
64struct receive_queue {
65 /* Virtqueue associated with this receive_queue */
66 struct virtqueue *vq;
67
296f96fc
RR
68 struct napi_struct napi;
69
70 /* Number of input buffers, and max we've ever had. */
71 unsigned int num, max;
72
e9d7417b
JW
73 /* Chain pages by the private ptr. */
74 struct page *pages;
75
76 /* RX: fragments + linear part + virtio header */
77 struct scatterlist sg[MAX_SKB_FRAGS + 2];
78};
79
80struct virtnet_info {
81 struct virtio_device *vdev;
82 struct virtqueue *cvq;
83 struct net_device *dev;
84 struct send_queue sq;
85 struct receive_queue rq;
86 unsigned int status;
87
97402b96
HX
88 /* I like... big packets and I cannot lie! */
89 bool big_packets;
90
3f2c31d9
MM
91 /* Host will merge rx buffers for big packets (shake it! shake it!) */
92 bool mergeable_rx_bufs;
93
586d17c5
JW
94 /* enable config space updates */
95 bool config_enable;
96
3fa2a1df 97 /* Active statistics */
98 struct virtnet_stats __percpu *stats;
99
3161e453
RR
100 /* Work struct for refilling if we run low on memory. */
101 struct delayed_work refill;
102
586d17c5
JW
103 /* Work struct for config space updates */
104 struct work_struct config_work;
105
106 /* Lock for config space updates */
107 struct mutex config_lock;
296f96fc
RR
108};
109
b3f24698
RR
110struct skb_vnet_hdr {
111 union {
112 struct virtio_net_hdr hdr;
113 struct virtio_net_hdr_mrg_rxbuf mhdr;
114 };
48925e37 115 unsigned int num_sg;
b3f24698
RR
116};
117
9ab86bbc
SM
118struct padded_vnet_hdr {
119 struct virtio_net_hdr hdr;
120 /*
121 * virtio_net_hdr should be in a separated sg buffer because of a
122 * QEMU bug, and data sg buffer shares same page with this header sg.
123 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
124 */
125 char padding[6];
126};
127
b3f24698 128static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
296f96fc 129{
b3f24698 130 return (struct skb_vnet_hdr *)skb->cb;
296f96fc
RR
131}
132
9ab86bbc
SM
133/*
134 * private is used to chain pages for big packets, put the whole
135 * most recent used list in the beginning for reuse
136 */
e9d7417b 137static void give_pages(struct receive_queue *rq, struct page *page)
0a888fd1 138{
9ab86bbc 139 struct page *end;
0a888fd1 140
e9d7417b 141 /* Find end of list, sew whole thing into vi->rq.pages. */
9ab86bbc 142 for (end = page; end->private; end = (struct page *)end->private);
e9d7417b
JW
143 end->private = (unsigned long)rq->pages;
144 rq->pages = page;
0a888fd1
MM
145}
146
e9d7417b 147static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
fb6813f4 148{
e9d7417b 149 struct page *p = rq->pages;
fb6813f4 150
9ab86bbc 151 if (p) {
e9d7417b 152 rq->pages = (struct page *)p->private;
9ab86bbc
SM
153 /* clear private here, it is used to chain pages */
154 p->private = 0;
155 } else
fb6813f4
RR
156 p = alloc_page(gfp_mask);
157 return p;
158}
159
e9d7417b 160static void skb_xmit_done(struct virtqueue *vq)
296f96fc 161{
e9d7417b 162 struct virtnet_info *vi = vq->vdev->priv;
296f96fc 163
2cb9c6ba 164 /* Suppress further interrupts. */
e9d7417b 165 virtqueue_disable_cb(vq);
11a3a154 166
363f1514 167 /* We were probably waiting for more output buffers. */
296f96fc 168 netif_wake_queue(vi->dev);
296f96fc
RR
169}
170
9ab86bbc
SM
171static void set_skb_frag(struct sk_buff *skb, struct page *page,
172 unsigned int offset, unsigned int *len)
296f96fc 173{
8a59a7b9 174 int size = min((unsigned)PAGE_SIZE - offset, *len);
9ab86bbc 175 int i = skb_shinfo(skb)->nr_frags;
9ab86bbc 176
8a59a7b9 177 __skb_fill_page_desc(skb, i, page, offset, size);
9ab86bbc 178
8a59a7b9
KK
179 skb->data_len += size;
180 skb->len += size;
4b727361 181 skb->truesize += PAGE_SIZE;
9ab86bbc 182 skb_shinfo(skb)->nr_frags++;
8a59a7b9 183 *len -= size;
9ab86bbc 184}
23cde76d 185
3464645a 186/* Called from bottom half context */
e9d7417b 187static struct sk_buff *page_to_skb(struct receive_queue *rq,
9ab86bbc
SM
188 struct page *page, unsigned int len)
189{
e9d7417b 190 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc
SM
191 struct sk_buff *skb;
192 struct skb_vnet_hdr *hdr;
193 unsigned int copy, hdr_len, offset;
194 char *p;
fb6813f4 195
9ab86bbc 196 p = page_address(page);
3f2c31d9 197
9ab86bbc
SM
198 /* copy small packet so we can reuse these pages for small data */
199 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
200 if (unlikely(!skb))
201 return NULL;
3f2c31d9 202
9ab86bbc 203 hdr = skb_vnet_hdr(skb);
3f2c31d9 204
9ab86bbc
SM
205 if (vi->mergeable_rx_bufs) {
206 hdr_len = sizeof hdr->mhdr;
207 offset = hdr_len;
208 } else {
209 hdr_len = sizeof hdr->hdr;
210 offset = sizeof(struct padded_vnet_hdr);
211 }
3f2c31d9 212
9ab86bbc 213 memcpy(hdr, p, hdr_len);
3f2c31d9 214
9ab86bbc
SM
215 len -= hdr_len;
216 p += offset;
3f2c31d9 217
9ab86bbc
SM
218 copy = len;
219 if (copy > skb_tailroom(skb))
220 copy = skb_tailroom(skb);
221 memcpy(skb_put(skb, copy), p, copy);
3f2c31d9 222
9ab86bbc
SM
223 len -= copy;
224 offset += copy;
3f2c31d9 225
e878d78b
SL
226 /*
227 * Verify that we can indeed put this data into a skb.
228 * This is here to handle cases when the device erroneously
229 * tries to receive more than is possible. This is usually
230 * the case of a broken device.
231 */
232 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
be443899 233 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
e878d78b
SL
234 dev_kfree_skb(skb);
235 return NULL;
236 }
237
9ab86bbc
SM
238 while (len) {
239 set_skb_frag(skb, page, offset, &len);
240 page = (struct page *)page->private;
241 offset = 0;
242 }
3f2c31d9 243
9ab86bbc 244 if (page)
e9d7417b 245 give_pages(rq, page);
3f2c31d9 246
9ab86bbc
SM
247 return skb;
248}
3f2c31d9 249
e9d7417b 250static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb)
9ab86bbc
SM
251{
252 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
253 struct page *page;
254 int num_buf, i, len;
255
256 num_buf = hdr->mhdr.num_buffers;
257 while (--num_buf) {
258 i = skb_shinfo(skb)->nr_frags;
259 if (i >= MAX_SKB_FRAGS) {
260 pr_debug("%s: packet too long\n", skb->dev->name);
261 skb->dev->stats.rx_length_errors++;
262 return -EINVAL;
263 }
e9d7417b 264 page = virtqueue_get_buf(rq->vq, &len);
9ab86bbc
SM
265 if (!page) {
266 pr_debug("%s: rx error: %d buffers missing\n",
267 skb->dev->name, hdr->mhdr.num_buffers);
268 skb->dev->stats.rx_length_errors++;
269 return -EINVAL;
3f2c31d9 270 }
3fa2a1df 271
9ab86bbc
SM
272 if (len > PAGE_SIZE)
273 len = PAGE_SIZE;
274
275 set_skb_frag(skb, page, 0, &len);
276
e9d7417b 277 --rq->num;
9ab86bbc
SM
278 }
279 return 0;
280}
281
e9d7417b 282static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
9ab86bbc 283{
e9d7417b
JW
284 struct virtnet_info *vi = rq->vq->vdev->priv;
285 struct net_device *dev = vi->dev;
58472a76 286 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
9ab86bbc
SM
287 struct sk_buff *skb;
288 struct page *page;
289 struct skb_vnet_hdr *hdr;
3f2c31d9 290
9ab86bbc
SM
291 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
292 pr_debug("%s: short packet %i\n", dev->name, len);
293 dev->stats.rx_length_errors++;
294 if (vi->mergeable_rx_bufs || vi->big_packets)
e9d7417b 295 give_pages(rq, buf);
9ab86bbc
SM
296 else
297 dev_kfree_skb(buf);
298 return;
299 }
3f2c31d9 300
9ab86bbc
SM
301 if (!vi->mergeable_rx_bufs && !vi->big_packets) {
302 skb = buf;
303 len -= sizeof(struct virtio_net_hdr);
304 skb_trim(skb, len);
305 } else {
306 page = buf;
e9d7417b 307 skb = page_to_skb(rq, page, len);
9ab86bbc 308 if (unlikely(!skb)) {
3f2c31d9 309 dev->stats.rx_dropped++;
e9d7417b 310 give_pages(rq, page);
9ab86bbc 311 return;
3f2c31d9 312 }
9ab86bbc 313 if (vi->mergeable_rx_bufs)
e9d7417b 314 if (receive_mergeable(rq, skb)) {
9ab86bbc
SM
315 dev_kfree_skb(skb);
316 return;
317 }
97402b96 318 }
3f2c31d9 319
9ab86bbc 320 hdr = skb_vnet_hdr(skb);
3fa2a1df 321
83a27052 322 u64_stats_update_begin(&stats->rx_syncp);
3fa2a1df 323 stats->rx_bytes += skb->len;
324 stats->rx_packets++;
83a27052 325 u64_stats_update_end(&stats->rx_syncp);
296f96fc 326
b3f24698 327 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
296f96fc 328 pr_debug("Needs csum!\n");
b3f24698
RR
329 if (!skb_partial_csum_set(skb,
330 hdr->hdr.csum_start,
331 hdr->hdr.csum_offset))
296f96fc 332 goto frame_err;
10a8d94a
JW
333 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
334 skb->ip_summed = CHECKSUM_UNNECESSARY;
296f96fc
RR
335 }
336
23cde76d
MM
337 skb->protocol = eth_type_trans(skb, dev);
338 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
339 ntohs(skb->protocol), skb->len, skb->pkt_type);
340
b3f24698 341 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
296f96fc 342 pr_debug("GSO!\n");
b3f24698 343 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
296f96fc
RR
344 case VIRTIO_NET_HDR_GSO_TCPV4:
345 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
346 break;
296f96fc
RR
347 case VIRTIO_NET_HDR_GSO_UDP:
348 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
349 break;
350 case VIRTIO_NET_HDR_GSO_TCPV6:
351 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
352 break;
353 default:
be443899
AW
354 net_warn_ratelimited("%s: bad gso type %u.\n",
355 dev->name, hdr->hdr.gso_type);
296f96fc
RR
356 goto frame_err;
357 }
358
b3f24698 359 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
34a48579
RR
360 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
361
b3f24698 362 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
296f96fc 363 if (skb_shinfo(skb)->gso_size == 0) {
be443899 364 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
296f96fc
RR
365 goto frame_err;
366 }
367
368 /* Header must be checked, and gso_segs computed. */
369 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
370 skb_shinfo(skb)->gso_segs = 0;
371 }
372
373 netif_receive_skb(skb);
374 return;
375
376frame_err:
377 dev->stats.rx_frame_errors++;
296f96fc
RR
378 dev_kfree_skb(skb);
379}
380
e9d7417b 381static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
296f96fc 382{
e9d7417b 383 struct virtnet_info *vi = rq->vq->vdev->priv;
296f96fc 384 struct sk_buff *skb;
9ab86bbc 385 struct skb_vnet_hdr *hdr;
9ab86bbc 386 int err;
3f2c31d9 387
3464645a 388 skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
9ab86bbc
SM
389 if (unlikely(!skb))
390 return -ENOMEM;
296f96fc 391
9ab86bbc 392 skb_put(skb, MAX_PACKET_LEN);
3f2c31d9 393
9ab86bbc 394 hdr = skb_vnet_hdr(skb);
e9d7417b 395 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
97402b96 396
e9d7417b 397 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
97402b96 398
e9d7417b 399 err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb, gfp);
9ab86bbc
SM
400 if (err < 0)
401 dev_kfree_skb(skb);
97402b96 402
9ab86bbc
SM
403 return err;
404}
97402b96 405
e9d7417b 406static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
9ab86bbc 407{
9ab86bbc
SM
408 struct page *first, *list = NULL;
409 char *p;
410 int i, err, offset;
411
e9d7417b 412 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
9ab86bbc 413 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
e9d7417b 414 first = get_a_page(rq, gfp);
9ab86bbc
SM
415 if (!first) {
416 if (list)
e9d7417b 417 give_pages(rq, list);
9ab86bbc 418 return -ENOMEM;
97402b96 419 }
e9d7417b 420 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
97402b96 421
9ab86bbc
SM
422 /* chain new page in list head to match sg */
423 first->private = (unsigned long)list;
424 list = first;
425 }
296f96fc 426
e9d7417b 427 first = get_a_page(rq, gfp);
9ab86bbc 428 if (!first) {
e9d7417b 429 give_pages(rq, list);
9ab86bbc
SM
430 return -ENOMEM;
431 }
432 p = page_address(first);
433
e9d7417b
JW
434 /* rq->sg[0], rq->sg[1] share the same page */
435 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
436 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
9ab86bbc 437
e9d7417b 438 /* rq->sg[1] for data packet, from offset */
9ab86bbc 439 offset = sizeof(struct padded_vnet_hdr);
e9d7417b 440 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
9ab86bbc
SM
441
442 /* chain first in list head */
443 first->private = (unsigned long)list;
e9d7417b 444 err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2,
f96fde41 445 first, gfp);
9ab86bbc 446 if (err < 0)
e9d7417b 447 give_pages(rq, first);
9ab86bbc
SM
448
449 return err;
296f96fc
RR
450}
451
e9d7417b 452static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
3f2c31d9 453{
9ab86bbc 454 struct page *page;
3f2c31d9 455 int err;
3f2c31d9 456
e9d7417b 457 page = get_a_page(rq, gfp);
9ab86bbc
SM
458 if (!page)
459 return -ENOMEM;
3f2c31d9 460
e9d7417b 461 sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
3f2c31d9 462
e9d7417b 463 err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page, gfp);
9ab86bbc 464 if (err < 0)
e9d7417b 465 give_pages(rq, page);
3f2c31d9 466
9ab86bbc
SM
467 return err;
468}
3f2c31d9 469
b2baed69
RR
470/*
471 * Returns false if we couldn't fill entirely (OOM).
472 *
473 * Normally run in the receive path, but can also be run from ndo_open
474 * before we're receiving packets, or from refill_work which is
475 * careful to disable receiving (using napi_disable).
476 */
e9d7417b 477static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
9ab86bbc 478{
e9d7417b 479 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc 480 int err;
1788f495 481 bool oom;
3f2c31d9 482
9ab86bbc
SM
483 do {
484 if (vi->mergeable_rx_bufs)
e9d7417b 485 err = add_recvbuf_mergeable(rq, gfp);
9ab86bbc 486 else if (vi->big_packets)
e9d7417b 487 err = add_recvbuf_big(rq, gfp);
9ab86bbc 488 else
e9d7417b 489 err = add_recvbuf_small(rq, gfp);
3f2c31d9 490
1788f495
MT
491 oom = err == -ENOMEM;
492 if (err < 0)
3f2c31d9 493 break;
e9d7417b 494 ++rq->num;
0aea51c3 495 } while (err > 0);
e9d7417b
JW
496 if (unlikely(rq->num > rq->max))
497 rq->max = rq->num;
498 virtqueue_kick(rq->vq);
3161e453 499 return !oom;
3f2c31d9
MM
500}
501
18445c4d 502static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
503{
504 struct virtnet_info *vi = rvq->vdev->priv;
e9d7417b
JW
505 struct receive_queue *rq = &vi->rq;
506
18445c4d 507 /* Schedule NAPI, Suppress further interrupts if successful. */
e9d7417b 508 if (napi_schedule_prep(&rq->napi)) {
1915a712 509 virtqueue_disable_cb(rvq);
e9d7417b 510 __napi_schedule(&rq->napi);
18445c4d 511 }
296f96fc
RR
512}
513
e9d7417b 514static void virtnet_napi_enable(struct receive_queue *rq)
3e9d08ec 515{
e9d7417b 516 napi_enable(&rq->napi);
3e9d08ec
BR
517
518 /* If all buffers were filled by other side before we napi_enabled, we
519 * won't get another interrupt, so process any outstanding packets
520 * now. virtnet_poll wants re-enable the queue, so we disable here.
521 * We synchronize against interrupts via NAPI_STATE_SCHED */
e9d7417b
JW
522 if (napi_schedule_prep(&rq->napi)) {
523 virtqueue_disable_cb(rq->vq);
ec13ee80 524 local_bh_disable();
e9d7417b 525 __napi_schedule(&rq->napi);
ec13ee80 526 local_bh_enable();
3e9d08ec
BR
527 }
528}
529
3161e453
RR
530static void refill_work(struct work_struct *work)
531{
e9d7417b
JW
532 struct virtnet_info *vi =
533 container_of(work, struct virtnet_info, refill.work);
3161e453
RR
534 bool still_empty;
535
e9d7417b
JW
536 napi_disable(&vi->rq.napi);
537 still_empty = !try_fill_recv(&vi->rq, GFP_KERNEL);
538 virtnet_napi_enable(&vi->rq);
3161e453
RR
539
540 /* In theory, this can happen: if we don't get any buffers in
541 * we will *never* try to fill again. */
542 if (still_empty)
3b07e9ca 543 schedule_delayed_work(&vi->refill, HZ/2);
3161e453
RR
544}
545
296f96fc
RR
546static int virtnet_poll(struct napi_struct *napi, int budget)
547{
e9d7417b
JW
548 struct receive_queue *rq =
549 container_of(napi, struct receive_queue, napi);
550 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc 551 void *buf;
296f96fc
RR
552 unsigned int len, received = 0;
553
554again:
555 while (received < budget &&
e9d7417b
JW
556 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
557 receive_buf(rq, buf, len);
558 --rq->num;
296f96fc
RR
559 received++;
560 }
561
e9d7417b
JW
562 if (rq->num < rq->max / 2) {
563 if (!try_fill_recv(rq, GFP_ATOMIC))
3b07e9ca 564 schedule_delayed_work(&vi->refill, 0);
3161e453 565 }
296f96fc 566
8329d98e
RR
567 /* Out of packets? */
568 if (received < budget) {
288379f0 569 napi_complete(napi);
e9d7417b 570 if (unlikely(!virtqueue_enable_cb(rq->vq)) &&
8e95a202 571 napi_schedule_prep(napi)) {
e9d7417b 572 virtqueue_disable_cb(rq->vq);
288379f0 573 __napi_schedule(napi);
296f96fc 574 goto again;
4265f161 575 }
296f96fc
RR
576 }
577
578 return received;
579}
580
e9d7417b 581static unsigned int free_old_xmit_skbs(struct send_queue *sq)
296f96fc
RR
582{
583 struct sk_buff *skb;
48925e37 584 unsigned int len, tot_sgs = 0;
e9d7417b 585 struct virtnet_info *vi = sq->vq->vdev->priv;
58472a76 586 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
296f96fc 587
e9d7417b 588 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
296f96fc 589 pr_debug("Sent skb %p\n", skb);
3fa2a1df 590
83a27052 591 u64_stats_update_begin(&stats->tx_syncp);
3fa2a1df 592 stats->tx_bytes += skb->len;
593 stats->tx_packets++;
83a27052 594 u64_stats_update_end(&stats->tx_syncp);
3fa2a1df 595
48925e37 596 tot_sgs += skb_vnet_hdr(skb)->num_sg;
ed79bab8 597 dev_kfree_skb_any(skb);
296f96fc 598 }
48925e37 599 return tot_sgs;
296f96fc
RR
600}
601
e9d7417b 602static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
296f96fc 603{
b3f24698 604 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
296f96fc 605 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
e9d7417b 606 struct virtnet_info *vi = sq->vq->vdev->priv;
296f96fc 607
e174961c 608 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
296f96fc 609
296f96fc 610 if (skb->ip_summed == CHECKSUM_PARTIAL) {
b3f24698 611 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
55508d60 612 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
b3f24698 613 hdr->hdr.csum_offset = skb->csum_offset;
296f96fc 614 } else {
b3f24698
RR
615 hdr->hdr.flags = 0;
616 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
296f96fc
RR
617 }
618
619 if (skb_is_gso(skb)) {
b3f24698
RR
620 hdr->hdr.hdr_len = skb_headlen(skb);
621 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
34a48579 622 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
b3f24698 623 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
296f96fc 624 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
b3f24698 625 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
296f96fc 626 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
b3f24698 627 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
296f96fc
RR
628 else
629 BUG();
34a48579 630 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
b3f24698 631 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
296f96fc 632 } else {
b3f24698
RR
633 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
634 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
296f96fc
RR
635 }
636
b3f24698 637 hdr->mhdr.num_buffers = 0;
3f2c31d9
MM
638
639 /* Encode metadata header at front. */
640 if (vi->mergeable_rx_bufs)
e9d7417b 641 sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr);
3f2c31d9 642 else
e9d7417b 643 sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
3f2c31d9 644
e9d7417b
JW
645 hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
646 return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
f96fde41 647 0, skb, GFP_ATOMIC);
11a3a154
RR
648}
649
424efe9c 650static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
99ffc696
RR
651{
652 struct virtnet_info *vi = netdev_priv(dev);
e9d7417b 653 struct send_queue *sq = &vi->sq;
48925e37 654 int capacity;
2cb9c6ba 655
2cb9c6ba 656 /* Free up any pending old buffers before queueing new ones. */
e9d7417b 657 free_old_xmit_skbs(sq);
99ffc696 658
03f191ba 659 /* Try to transmit */
e9d7417b 660 capacity = xmit_skb(sq, skb);
48925e37
RR
661
662 /* This can happen with OOM and indirect buffers. */
663 if (unlikely(capacity < 0)) {
2e57b79c 664 if (likely(capacity == -ENOMEM)) {
31304165 665 if (net_ratelimit())
58eba97d
RR
666 dev_warn(&dev->dev,
667 "TX queue failure: out of memory\n");
31304165 668 } else {
2e57b79c
RJ
669 dev->stats.tx_fifo_errors++;
670 if (net_ratelimit())
58eba97d
RR
671 dev_warn(&dev->dev,
672 "Unexpected TX queue failure: %d\n",
673 capacity);
48925e37 674 }
58eba97d
RR
675 dev->stats.tx_dropped++;
676 kfree_skb(skb);
677 return NETDEV_TX_OK;
296f96fc 678 }
e9d7417b 679 virtqueue_kick(sq->vq);
03f191ba 680
48925e37
RR
681 /* Don't wait up for transmitted skbs to be freed. */
682 skb_orphan(skb);
683 nf_reset(skb);
684
685 /* Apparently nice girls don't return TX_BUSY; stop the queue
686 * before it gets out of hand. Naturally, this wastes entries. */
687 if (capacity < 2+MAX_SKB_FRAGS) {
688 netif_stop_queue(dev);
e9d7417b 689 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
48925e37 690 /* More just got used, free them then recheck. */
e9d7417b 691 capacity += free_old_xmit_skbs(sq);
48925e37
RR
692 if (capacity >= 2+MAX_SKB_FRAGS) {
693 netif_start_queue(dev);
e9d7417b 694 virtqueue_disable_cb(sq->vq);
48925e37
RR
695 }
696 }
99ffc696 697 }
48925e37
RR
698
699 return NETDEV_TX_OK;
296f96fc
RR
700}
701
9c46f6d4
AW
702static int virtnet_set_mac_address(struct net_device *dev, void *p)
703{
704 struct virtnet_info *vi = netdev_priv(dev);
705 struct virtio_device *vdev = vi->vdev;
f2f2c8b4 706 int ret;
9c46f6d4 707
f2f2c8b4
JP
708 ret = eth_mac_addr(dev, p);
709 if (ret)
710 return ret;
9c46f6d4 711
62994b2d
AW
712 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
713 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
714 dev->dev_addr, dev->addr_len);
9c46f6d4
AW
715
716 return 0;
717}
718
3fa2a1df 719static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
720 struct rtnl_link_stats64 *tot)
721{
722 struct virtnet_info *vi = netdev_priv(dev);
723 int cpu;
724 unsigned int start;
725
726 for_each_possible_cpu(cpu) {
58472a76 727 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
3fa2a1df 728 u64 tpackets, tbytes, rpackets, rbytes;
729
730 do {
e3906486 731 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
3fa2a1df 732 tpackets = stats->tx_packets;
733 tbytes = stats->tx_bytes;
e3906486 734 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
83a27052
ED
735
736 do {
e3906486 737 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
3fa2a1df 738 rpackets = stats->rx_packets;
739 rbytes = stats->rx_bytes;
e3906486 740 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
3fa2a1df 741
742 tot->rx_packets += rpackets;
743 tot->tx_packets += tpackets;
744 tot->rx_bytes += rbytes;
745 tot->tx_bytes += tbytes;
746 }
747
748 tot->tx_dropped = dev->stats.tx_dropped;
021ac8d3 749 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
3fa2a1df 750 tot->rx_dropped = dev->stats.rx_dropped;
751 tot->rx_length_errors = dev->stats.rx_length_errors;
752 tot->rx_frame_errors = dev->stats.rx_frame_errors;
753
754 return tot;
755}
756
da74e89d
AS
757#ifdef CONFIG_NET_POLL_CONTROLLER
758static void virtnet_netpoll(struct net_device *dev)
759{
760 struct virtnet_info *vi = netdev_priv(dev);
761
e9d7417b 762 napi_schedule(&vi->rq.napi);
da74e89d
AS
763}
764#endif
765
296f96fc
RR
766static int virtnet_open(struct net_device *dev)
767{
768 struct virtnet_info *vi = netdev_priv(dev);
769
b2baed69 770 /* Make sure we have some buffers: if oom use wq. */
e9d7417b 771 if (!try_fill_recv(&vi->rq, GFP_KERNEL))
3b07e9ca 772 schedule_delayed_work(&vi->refill, 0);
b2baed69 773
e9d7417b 774 virtnet_napi_enable(&vi->rq);
296f96fc
RR
775 return 0;
776}
777
2a41f71d
AW
778/*
779 * Send command via the control virtqueue and check status. Commands
780 * supported by the hypervisor, as indicated by feature bits, should
781 * never fail unless improperly formated.
782 */
783static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
784 struct scatterlist *data, int out, int in)
785{
23e258e1 786 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
2a41f71d
AW
787 struct virtio_net_ctrl_hdr ctrl;
788 virtio_net_ctrl_ack status = ~0;
789 unsigned int tmp;
23e258e1 790 int i;
2a41f71d 791
0ee904c3
AB
792 /* Caller should know better */
793 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
794 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
2a41f71d
AW
795
796 out++; /* Add header */
797 in++; /* Add return status */
798
799 ctrl.class = class;
800 ctrl.cmd = cmd;
801
802 sg_init_table(sg, out + in);
803
804 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
23e258e1
AW
805 for_each_sg(data, s, out + in - 2, i)
806 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
2a41f71d
AW
807 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
808
f96fde41 809 BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
2a41f71d 810
1915a712 811 virtqueue_kick(vi->cvq);
2a41f71d
AW
812
813 /*
814 * Spin for a response, the kick causes an ioport write, trapping
815 * into the hypervisor, so the request should be handled immediately.
816 */
1915a712 817 while (!virtqueue_get_buf(vi->cvq, &tmp))
2a41f71d
AW
818 cpu_relax();
819
820 return status == VIRTIO_NET_OK;
821}
822
586d17c5
JW
823static void virtnet_ack_link_announce(struct virtnet_info *vi)
824{
825 rtnl_lock();
826 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
827 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
828 0, 0))
829 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
830 rtnl_unlock();
831}
832
296f96fc
RR
833static int virtnet_close(struct net_device *dev)
834{
835 struct virtnet_info *vi = netdev_priv(dev);
296f96fc 836
b2baed69
RR
837 /* Make sure refill_work doesn't re-enable napi! */
838 cancel_delayed_work_sync(&vi->refill);
e9d7417b 839 napi_disable(&vi->rq.napi);
296f96fc 840
296f96fc
RR
841 return 0;
842}
843
2af7698e
AW
844static void virtnet_set_rx_mode(struct net_device *dev)
845{
846 struct virtnet_info *vi = netdev_priv(dev);
f565a7c2 847 struct scatterlist sg[2];
2af7698e 848 u8 promisc, allmulti;
f565a7c2 849 struct virtio_net_ctrl_mac *mac_data;
ccffad25 850 struct netdev_hw_addr *ha;
32e7bfc4 851 int uc_count;
4cd24eaf 852 int mc_count;
f565a7c2
AW
853 void *buf;
854 int i;
2af7698e
AW
855
856 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
857 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
858 return;
859
f565a7c2
AW
860 promisc = ((dev->flags & IFF_PROMISC) != 0);
861 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2af7698e 862
23e258e1 863 sg_init_one(sg, &promisc, sizeof(promisc));
2af7698e
AW
864
865 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
866 VIRTIO_NET_CTRL_RX_PROMISC,
f565a7c2 867 sg, 1, 0))
2af7698e
AW
868 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
869 promisc ? "en" : "dis");
870
23e258e1 871 sg_init_one(sg, &allmulti, sizeof(allmulti));
2af7698e
AW
872
873 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
874 VIRTIO_NET_CTRL_RX_ALLMULTI,
f565a7c2 875 sg, 1, 0))
2af7698e
AW
876 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
877 allmulti ? "en" : "dis");
f565a7c2 878
32e7bfc4 879 uc_count = netdev_uc_count(dev);
4cd24eaf 880 mc_count = netdev_mc_count(dev);
f565a7c2 881 /* MAC filter - use one buffer for both lists */
4cd24eaf
JP
882 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
883 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
884 mac_data = buf;
f565a7c2
AW
885 if (!buf) {
886 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
887 return;
888 }
889
23e258e1
AW
890 sg_init_table(sg, 2);
891
f565a7c2 892 /* Store the unicast list and count in the front of the buffer */
32e7bfc4 893 mac_data->entries = uc_count;
ccffad25 894 i = 0;
32e7bfc4 895 netdev_for_each_uc_addr(ha, dev)
ccffad25 896 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
897
898 sg_set_buf(&sg[0], mac_data,
32e7bfc4 899 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
f565a7c2
AW
900
901 /* multicast list and count fill the end */
32e7bfc4 902 mac_data = (void *)&mac_data->macs[uc_count][0];
f565a7c2 903
4cd24eaf 904 mac_data->entries = mc_count;
567ec874 905 i = 0;
22bedad3
JP
906 netdev_for_each_mc_addr(ha, dev)
907 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
908
909 sg_set_buf(&sg[1], mac_data,
4cd24eaf 910 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
f565a7c2
AW
911
912 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
913 VIRTIO_NET_CTRL_MAC_TABLE_SET,
914 sg, 2, 0))
915 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
916
917 kfree(buf);
2af7698e
AW
918}
919
8e586137 920static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
0bde9569
AW
921{
922 struct virtnet_info *vi = netdev_priv(dev);
923 struct scatterlist sg;
924
23e258e1 925 sg_init_one(&sg, &vid, sizeof(vid));
0bde9569
AW
926
927 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
928 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
929 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8e586137 930 return 0;
0bde9569
AW
931}
932
8e586137 933static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
0bde9569
AW
934{
935 struct virtnet_info *vi = netdev_priv(dev);
936 struct scatterlist sg;
937
23e258e1 938 sg_init_one(&sg, &vid, sizeof(vid));
0bde9569
AW
939
940 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
941 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
942 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8e586137 943 return 0;
0bde9569
AW
944}
945
8f9f4668
RJ
946static void virtnet_get_ringparam(struct net_device *dev,
947 struct ethtool_ringparam *ring)
948{
949 struct virtnet_info *vi = netdev_priv(dev);
950
e9d7417b
JW
951 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq.vq);
952 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq.vq);
8f9f4668
RJ
953 ring->rx_pending = ring->rx_max_pending;
954 ring->tx_pending = ring->tx_max_pending;
8f9f4668
RJ
955}
956
66846048
RJ
957
958static void virtnet_get_drvinfo(struct net_device *dev,
959 struct ethtool_drvinfo *info)
960{
961 struct virtnet_info *vi = netdev_priv(dev);
962 struct virtio_device *vdev = vi->vdev;
963
964 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
965 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
966 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
967
968}
969
0fc0b732 970static const struct ethtool_ops virtnet_ethtool_ops = {
66846048 971 .get_drvinfo = virtnet_get_drvinfo,
9f4d26d0 972 .get_link = ethtool_op_get_link,
8f9f4668 973 .get_ringparam = virtnet_get_ringparam,
a9ea3fc6
HX
974};
975
39da5814
MM
976#define MIN_MTU 68
977#define MAX_MTU 65535
978
979static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
980{
981 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
982 return -EINVAL;
983 dev->mtu = new_mtu;
984 return 0;
985}
986
76288b4e
SH
987static const struct net_device_ops virtnet_netdev = {
988 .ndo_open = virtnet_open,
989 .ndo_stop = virtnet_close,
990 .ndo_start_xmit = start_xmit,
991 .ndo_validate_addr = eth_validate_addr,
9c46f6d4 992 .ndo_set_mac_address = virtnet_set_mac_address,
2af7698e 993 .ndo_set_rx_mode = virtnet_set_rx_mode,
76288b4e 994 .ndo_change_mtu = virtnet_change_mtu,
3fa2a1df 995 .ndo_get_stats64 = virtnet_stats,
1824a989
AW
996 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
997 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
76288b4e
SH
998#ifdef CONFIG_NET_POLL_CONTROLLER
999 .ndo_poll_controller = virtnet_netpoll,
1000#endif
1001};
1002
586d17c5 1003static void virtnet_config_changed_work(struct work_struct *work)
9f4d26d0 1004{
586d17c5
JW
1005 struct virtnet_info *vi =
1006 container_of(work, struct virtnet_info, config_work);
9f4d26d0
MM
1007 u16 v;
1008
586d17c5
JW
1009 mutex_lock(&vi->config_lock);
1010 if (!vi->config_enable)
1011 goto done;
1012
77dd7693 1013 if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
9f4d26d0 1014 offsetof(struct virtio_net_config, status),
77dd7693 1015 &v) < 0)
586d17c5
JW
1016 goto done;
1017
1018 if (v & VIRTIO_NET_S_ANNOUNCE) {
ee89bab1 1019 netdev_notify_peers(vi->dev);
586d17c5
JW
1020 virtnet_ack_link_announce(vi);
1021 }
9f4d26d0
MM
1022
1023 /* Ignore unknown (future) status bits */
1024 v &= VIRTIO_NET_S_LINK_UP;
1025
1026 if (vi->status == v)
586d17c5 1027 goto done;
9f4d26d0
MM
1028
1029 vi->status = v;
1030
1031 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1032 netif_carrier_on(vi->dev);
1033 netif_wake_queue(vi->dev);
1034 } else {
1035 netif_carrier_off(vi->dev);
1036 netif_stop_queue(vi->dev);
1037 }
586d17c5
JW
1038done:
1039 mutex_unlock(&vi->config_lock);
9f4d26d0
MM
1040}
1041
1042static void virtnet_config_changed(struct virtio_device *vdev)
1043{
1044 struct virtnet_info *vi = vdev->priv;
1045
3b07e9ca 1046 schedule_work(&vi->config_work);
9f4d26d0
MM
1047}
1048
e9d7417b
JW
1049static void virtnet_del_vqs(struct virtnet_info *vi)
1050{
1051 struct virtio_device *vdev = vi->vdev;
1052
1053 vdev->config->del_vqs(vdev);
1054}
1055
3f9c10b0
AS
1056static int init_vqs(struct virtnet_info *vi)
1057{
1058 struct virtqueue *vqs[3];
1059 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
1060 const char *names[] = { "input", "output", "control" };
1061 int nvqs, err;
1062
1063 /* We expect two virtqueues, receive then send,
1064 * and optionally control. */
1065 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
1066
1067 err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
1068 if (err)
1069 return err;
1070
e9d7417b
JW
1071 vi->rq.vq = vqs[0];
1072 vi->sq.vq = vqs[1];
3f9c10b0
AS
1073
1074 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
1075 vi->cvq = vqs[2];
1076
1077 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1078 vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
1079 }
1080 return 0;
1081}
1082
296f96fc
RR
1083static int virtnet_probe(struct virtio_device *vdev)
1084{
1085 int err;
296f96fc
RR
1086 struct net_device *dev;
1087 struct virtnet_info *vi;
296f96fc
RR
1088
1089 /* Allocate ourselves a network device with room for our info */
1090 dev = alloc_etherdev(sizeof(struct virtnet_info));
1091 if (!dev)
1092 return -ENOMEM;
1093
1094 /* Set up network device as normal. */
f2f2c8b4 1095 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
76288b4e 1096 dev->netdev_ops = &virtnet_netdev;
296f96fc 1097 dev->features = NETIF_F_HIGHDMA;
3fa2a1df 1098
a9ea3fc6 1099 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
296f96fc
RR
1100 SET_NETDEV_DEV(dev, &vdev->dev);
1101
1102 /* Do we support "hardware" checksums? */
98e778c9 1103 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc 1104 /* This opens up the world of extra features. */
98e778c9
MM
1105 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1106 if (csum)
1107 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1108
1109 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1110 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
34a48579
RR
1111 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1112 }
5539ae96 1113 /* Individual feature bits: what can host handle? */
98e778c9
MM
1114 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1115 dev->hw_features |= NETIF_F_TSO;
1116 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1117 dev->hw_features |= NETIF_F_TSO6;
1118 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1119 dev->hw_features |= NETIF_F_TSO_ECN;
1120 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1121 dev->hw_features |= NETIF_F_UFO;
1122
1123 if (gso)
1124 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1125 /* (!csum && gso) case will be fixed by register_netdev() */
296f96fc
RR
1126 }
1127
1128 /* Configuration may specify what MAC to use. Otherwise random. */
77dd7693 1129 if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
a586d4f6 1130 offsetof(struct virtio_net_config, mac),
77dd7693 1131 dev->dev_addr, dev->addr_len) < 0)
f2cedb63 1132 eth_hw_addr_random(dev);
296f96fc
RR
1133
1134 /* Set up our device-specific information */
1135 vi = netdev_priv(dev);
e9d7417b 1136 netif_napi_add(dev, &vi->rq.napi, virtnet_poll, napi_weight);
296f96fc
RR
1137 vi->dev = dev;
1138 vi->vdev = vdev;
d9d5dcc8 1139 vdev->priv = vi;
e9d7417b 1140 vi->rq.pages = NULL;
3fa2a1df 1141 vi->stats = alloc_percpu(struct virtnet_stats);
1142 err = -ENOMEM;
1143 if (vi->stats == NULL)
1144 goto free;
1145
3161e453 1146 INIT_DELAYED_WORK(&vi->refill, refill_work);
586d17c5
JW
1147 mutex_init(&vi->config_lock);
1148 vi->config_enable = true;
1149 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
e9d7417b
JW
1150 sg_init_table(vi->rq.sg, ARRAY_SIZE(vi->rq.sg));
1151 sg_init_table(vi->sq.sg, ARRAY_SIZE(vi->sq.sg));
296f96fc 1152
97402b96 1153 /* If we can receive ANY GSO packets, we must allocate large ones. */
8e95a202
JP
1154 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1155 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1156 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
97402b96
HX
1157 vi->big_packets = true;
1158
3f2c31d9
MM
1159 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1160 vi->mergeable_rx_bufs = true;
1161
3f9c10b0 1162 err = init_vqs(vi);
d2a7ddda 1163 if (err)
3fa2a1df 1164 goto free_stats;
296f96fc 1165
296f96fc
RR
1166 err = register_netdev(dev);
1167 if (err) {
1168 pr_debug("virtio_net: registering device failed\n");
d2a7ddda 1169 goto free_vqs;
296f96fc 1170 }
b3369c1f
RR
1171
1172 /* Last of all, set up some receive buffers. */
e9d7417b 1173 try_fill_recv(&vi->rq, GFP_KERNEL);
b3369c1f
RR
1174
1175 /* If we didn't even get one input buffer, we're useless. */
e9d7417b 1176 if (vi->rq.num == 0) {
b3369c1f
RR
1177 err = -ENOMEM;
1178 goto unregister;
1179 }
1180
167c25e4
JW
1181 /* Assume link up if device can't report link status,
1182 otherwise get link status from config. */
1183 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1184 netif_carrier_off(dev);
3b07e9ca 1185 schedule_work(&vi->config_work);
167c25e4
JW
1186 } else {
1187 vi->status = VIRTIO_NET_S_LINK_UP;
1188 netif_carrier_on(dev);
1189 }
9f4d26d0 1190
296f96fc 1191 pr_debug("virtnet: registered device %s\n", dev->name);
296f96fc
RR
1192 return 0;
1193
b3369c1f
RR
1194unregister:
1195 unregister_netdev(dev);
d2a7ddda 1196free_vqs:
e9d7417b 1197 virtnet_del_vqs(vi);
3fa2a1df 1198free_stats:
1199 free_percpu(vi->stats);
296f96fc
RR
1200free:
1201 free_netdev(dev);
1202 return err;
1203}
1204
9ab86bbc
SM
1205static void free_unused_bufs(struct virtnet_info *vi)
1206{
1207 void *buf;
830a8a97 1208 while (1) {
e9d7417b 1209 buf = virtqueue_detach_unused_buf(vi->sq.vq);
830a8a97
SM
1210 if (!buf)
1211 break;
1212 dev_kfree_skb(buf);
1213 }
9ab86bbc 1214 while (1) {
e9d7417b 1215 buf = virtqueue_detach_unused_buf(vi->rq.vq);
9ab86bbc
SM
1216 if (!buf)
1217 break;
1218 if (vi->mergeable_rx_bufs || vi->big_packets)
e9d7417b 1219 give_pages(&vi->rq, buf);
9ab86bbc
SM
1220 else
1221 dev_kfree_skb(buf);
e9d7417b 1222 --vi->rq.num;
9ab86bbc 1223 }
e9d7417b 1224 BUG_ON(vi->rq.num != 0);
9ab86bbc
SM
1225}
1226
04486ed0 1227static void remove_vq_common(struct virtnet_info *vi)
296f96fc 1228{
04486ed0 1229 vi->vdev->config->reset(vi->vdev);
830a8a97
SM
1230
1231 /* Free unused buffers in both send and recv, if any. */
9ab86bbc 1232 free_unused_bufs(vi);
fb6813f4 1233
e9d7417b 1234 virtnet_del_vqs(vi);
d2a7ddda 1235
e9d7417b
JW
1236 while (vi->rq.pages)
1237 __free_pages(get_a_page(&vi->rq, GFP_KERNEL), 0);
04486ed0
AS
1238}
1239
8cc085d6 1240static void virtnet_remove(struct virtio_device *vdev)
04486ed0
AS
1241{
1242 struct virtnet_info *vi = vdev->priv;
1243
586d17c5
JW
1244 /* Prevent config work handler from accessing the device. */
1245 mutex_lock(&vi->config_lock);
1246 vi->config_enable = false;
1247 mutex_unlock(&vi->config_lock);
1248
04486ed0
AS
1249 unregister_netdev(vi->dev);
1250
1251 remove_vq_common(vi);
fb6813f4 1252
586d17c5
JW
1253 flush_work(&vi->config_work);
1254
2e66f55b 1255 free_percpu(vi->stats);
74b2553f 1256 free_netdev(vi->dev);
296f96fc
RR
1257}
1258
0741bcb5
AS
1259#ifdef CONFIG_PM
1260static int virtnet_freeze(struct virtio_device *vdev)
1261{
1262 struct virtnet_info *vi = vdev->priv;
1263
586d17c5
JW
1264 /* Prevent config work handler from accessing the device */
1265 mutex_lock(&vi->config_lock);
1266 vi->config_enable = false;
1267 mutex_unlock(&vi->config_lock);
1268
0741bcb5
AS
1269 netif_device_detach(vi->dev);
1270 cancel_delayed_work_sync(&vi->refill);
1271
1272 if (netif_running(vi->dev))
e9d7417b 1273 napi_disable(&vi->rq.napi);
0741bcb5
AS
1274
1275 remove_vq_common(vi);
1276
586d17c5
JW
1277 flush_work(&vi->config_work);
1278
0741bcb5
AS
1279 return 0;
1280}
1281
1282static int virtnet_restore(struct virtio_device *vdev)
1283{
1284 struct virtnet_info *vi = vdev->priv;
1285 int err;
1286
1287 err = init_vqs(vi);
1288 if (err)
1289 return err;
1290
1291 if (netif_running(vi->dev))
e9d7417b 1292 virtnet_napi_enable(&vi->rq);
0741bcb5
AS
1293
1294 netif_device_attach(vi->dev);
1295
e9d7417b 1296 if (!try_fill_recv(&vi->rq, GFP_KERNEL))
3b07e9ca 1297 schedule_delayed_work(&vi->refill, 0);
0741bcb5 1298
586d17c5
JW
1299 mutex_lock(&vi->config_lock);
1300 vi->config_enable = true;
1301 mutex_unlock(&vi->config_lock);
1302
0741bcb5
AS
1303 return 0;
1304}
1305#endif
1306
296f96fc
RR
1307static struct virtio_device_id id_table[] = {
1308 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1309 { 0 },
1310};
1311
c45a6816 1312static unsigned int features[] = {
5e4fe5c4
MM
1313 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1314 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
c45a6816 1315 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
97402b96 1316 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
5c516751 1317 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
2a41f71d 1318 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
0bde9569 1319 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
586d17c5 1320 VIRTIO_NET_F_GUEST_ANNOUNCE,
c45a6816
RR
1321};
1322
22402529 1323static struct virtio_driver virtio_net_driver = {
c45a6816
RR
1324 .feature_table = features,
1325 .feature_table_size = ARRAY_SIZE(features),
296f96fc
RR
1326 .driver.name = KBUILD_MODNAME,
1327 .driver.owner = THIS_MODULE,
1328 .id_table = id_table,
1329 .probe = virtnet_probe,
8cc085d6 1330 .remove = virtnet_remove,
9f4d26d0 1331 .config_changed = virtnet_config_changed,
0741bcb5
AS
1332#ifdef CONFIG_PM
1333 .freeze = virtnet_freeze,
1334 .restore = virtnet_restore,
1335#endif
296f96fc
RR
1336};
1337
1338static int __init init(void)
1339{
22402529 1340 return register_virtio_driver(&virtio_net_driver);
296f96fc
RR
1341}
1342
1343static void __exit fini(void)
1344{
22402529 1345 unregister_virtio_driver(&virtio_net_driver);
296f96fc
RR
1346}
1347module_init(init);
1348module_exit(fini);
1349
1350MODULE_DEVICE_TABLE(virtio, id_table);
1351MODULE_DESCRIPTION("Virtio network driver");
1352MODULE_LICENSE("GPL");