x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / net / virtio_net.c
blob0232156dade3acbeacba5b55bfba63d37e9bd529
1 /* A network driver using virtio.
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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.
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.
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
19 //#define DEBUG
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/ethtool.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
25 #include <linux/virtio_net.h>
26 #include <linux/scatterlist.h>
27 #include <linux/if_vlan.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
31 static int napi_weight = NAPI_POLL_WEIGHT;
32 module_param(napi_weight, int, 0444);
34 static bool csum = true, gso = true;
35 module_param(csum, bool, 0444);
36 module_param(gso, bool, 0444);
38 /* FIXME: MTU in config. */
39 #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
40 #define GOOD_COPY_LEN 128
42 #define VIRTNET_DRIVER_VERSION "1.0.0"
44 struct virtnet_stats {
45 struct u64_stats_sync tx_syncp;
46 struct u64_stats_sync rx_syncp;
47 u64 tx_bytes;
48 u64 tx_packets;
50 u64 rx_bytes;
51 u64 rx_packets;
54 /* Internal representation of a send virtqueue */
55 struct send_queue {
56 /* Virtqueue associated with this send _queue */
57 struct virtqueue *vq;
59 /* TX: fragments + linear part + virtio header */
60 struct scatterlist sg[MAX_SKB_FRAGS + 2];
62 /* Name of the send queue: output.$index */
63 char name[40];
66 /* Internal representation of a receive virtqueue */
67 struct receive_queue {
68 /* Virtqueue associated with this receive_queue */
69 struct virtqueue *vq;
71 struct napi_struct napi;
73 /* Number of input buffers, and max we've ever had. */
74 unsigned int num, max;
76 /* Chain pages by the private ptr. */
77 struct page *pages;
79 /* RX: fragments + linear part + virtio header */
80 struct scatterlist sg[MAX_SKB_FRAGS + 2];
82 /* Name of this receive queue: input.$index */
83 char name[40];
86 struct virtnet_info {
87 struct virtio_device *vdev;
88 struct virtqueue *cvq;
89 struct net_device *dev;
90 struct send_queue *sq;
91 struct receive_queue *rq;
92 unsigned int status;
94 /* Max # of queue pairs supported by the device */
95 u16 max_queue_pairs;
97 /* # of queue pairs currently used by the driver */
98 u16 curr_queue_pairs;
100 /* I like... big packets and I cannot lie! */
101 bool big_packets;
103 /* Host will merge rx buffers for big packets (shake it! shake it!) */
104 bool mergeable_rx_bufs;
106 /* Has control virtqueue */
107 bool has_cvq;
109 /* Host can handle any s/g split between our header and packet data */
110 bool any_header_sg;
112 /* enable config space updates */
113 bool config_enable;
115 /* Active statistics */
116 struct virtnet_stats __percpu *stats;
118 /* Work struct for refilling if we run low on memory. */
119 struct delayed_work refill;
121 /* Work struct for config space updates */
122 struct work_struct config_work;
124 /* Lock for config space updates */
125 struct mutex config_lock;
127 /* Does the affinity hint is set for virtqueues? */
128 bool affinity_hint_set;
130 /* Per-cpu variable to show the mapping from CPU to virtqueue */
131 int __percpu *vq_index;
133 /* CPU hot plug notifier */
134 struct notifier_block nb;
137 struct skb_vnet_hdr {
138 union {
139 struct virtio_net_hdr hdr;
140 struct virtio_net_hdr_mrg_rxbuf mhdr;
144 struct padded_vnet_hdr {
145 struct virtio_net_hdr hdr;
147 * virtio_net_hdr should be in a separated sg buffer because of a
148 * QEMU bug, and data sg buffer shares same page with this header sg.
149 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
151 char padding[6];
154 /* Converting between virtqueue no. and kernel tx/rx queue no.
155 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
157 static int vq2txq(struct virtqueue *vq)
159 return (vq->index - 1) / 2;
162 static int txq2vq(int txq)
164 return txq * 2 + 1;
167 static int vq2rxq(struct virtqueue *vq)
169 return vq->index / 2;
172 static int rxq2vq(int rxq)
174 return rxq * 2;
177 static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
179 return (struct skb_vnet_hdr *)skb->cb;
183 * private is used to chain pages for big packets, put the whole
184 * most recent used list in the beginning for reuse
186 static void give_pages(struct receive_queue *rq, struct page *page)
188 struct page *end;
190 /* Find end of list, sew whole thing into vi->rq.pages. */
191 for (end = page; end->private; end = (struct page *)end->private);
192 end->private = (unsigned long)rq->pages;
193 rq->pages = page;
196 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
198 struct page *p = rq->pages;
200 if (p) {
201 rq->pages = (struct page *)p->private;
202 /* clear private here, it is used to chain pages */
203 p->private = 0;
204 } else
205 p = alloc_page(gfp_mask);
206 return p;
209 static void skb_xmit_done(struct virtqueue *vq)
211 struct virtnet_info *vi = vq->vdev->priv;
213 /* Suppress further interrupts. */
214 virtqueue_disable_cb(vq);
216 /* We were probably waiting for more output buffers. */
217 netif_wake_subqueue(vi->dev, vq2txq(vq));
220 static void set_skb_frag(struct sk_buff *skb, struct page *page,
221 unsigned int offset, unsigned int *len)
223 int size = min((unsigned)PAGE_SIZE - offset, *len);
224 int i = skb_shinfo(skb)->nr_frags;
226 __skb_fill_page_desc(skb, i, page, offset, size);
228 skb->data_len += size;
229 skb->len += size;
230 skb->truesize += PAGE_SIZE;
231 skb_shinfo(skb)->nr_frags++;
232 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
233 *len -= size;
236 /* Called from bottom half context */
237 static struct sk_buff *page_to_skb(struct receive_queue *rq,
238 struct page *page, unsigned int len)
240 struct virtnet_info *vi = rq->vq->vdev->priv;
241 struct sk_buff *skb;
242 struct skb_vnet_hdr *hdr;
243 unsigned int copy, hdr_len, offset;
244 char *p;
246 p = page_address(page);
248 /* copy small packet so we can reuse these pages for small data */
249 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
250 if (unlikely(!skb))
251 return NULL;
253 hdr = skb_vnet_hdr(skb);
255 if (vi->mergeable_rx_bufs) {
256 hdr_len = sizeof hdr->mhdr;
257 offset = hdr_len;
258 } else {
259 hdr_len = sizeof hdr->hdr;
260 offset = sizeof(struct padded_vnet_hdr);
263 memcpy(hdr, p, hdr_len);
265 len -= hdr_len;
266 p += offset;
268 copy = len;
269 if (copy > skb_tailroom(skb))
270 copy = skb_tailroom(skb);
271 memcpy(skb_put(skb, copy), p, copy);
273 len -= copy;
274 offset += copy;
277 * Verify that we can indeed put this data into a skb.
278 * This is here to handle cases when the device erroneously
279 * tries to receive more than is possible. This is usually
280 * the case of a broken device.
282 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
283 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
284 dev_kfree_skb(skb);
285 return NULL;
288 while (len) {
289 set_skb_frag(skb, page, offset, &len);
290 page = (struct page *)page->private;
291 offset = 0;
294 if (page)
295 give_pages(rq, page);
297 return skb;
300 static struct sk_buff *receive_small(void *buf, unsigned int len)
302 struct sk_buff * skb = buf;
304 len -= sizeof(struct virtio_net_hdr);
305 skb_trim(skb, len);
307 return skb;
310 static struct sk_buff *receive_big(struct net_device *dev,
311 struct receive_queue *rq,
312 void *buf)
314 struct page *page = buf;
315 struct sk_buff *skb = page_to_skb(rq, page, 0);
317 if (unlikely(!skb))
318 goto err;
320 return skb;
322 err:
323 dev->stats.rx_dropped++;
324 give_pages(rq, page);
325 return NULL;
328 static struct sk_buff *receive_mergeable(struct net_device *dev,
329 struct receive_queue *rq,
330 void *buf,
331 unsigned int len)
333 struct skb_vnet_hdr *hdr = page_address(buf);
334 int num_buf = hdr->mhdr.num_buffers;
335 struct page *page = buf;
336 struct sk_buff *skb = page_to_skb(rq, page, len);
337 int i;
339 if (unlikely(!skb))
340 goto err_skb;
342 while (--num_buf) {
343 i = skb_shinfo(skb)->nr_frags;
344 if (i >= MAX_SKB_FRAGS) {
345 pr_debug("%s: packet too long\n", skb->dev->name);
346 skb->dev->stats.rx_length_errors++;
347 goto err_frags;
349 page = virtqueue_get_buf(rq->vq, &len);
350 if (!page) {
351 pr_debug("%s: rx error: %d buffers %d missing\n",
352 dev->name, hdr->mhdr.num_buffers, num_buf);
353 dev->stats.rx_length_errors++;
354 goto err_buf;
357 if (len > PAGE_SIZE)
358 len = PAGE_SIZE;
360 set_skb_frag(skb, page, 0, &len);
362 --rq->num;
364 return skb;
365 err_skb:
366 give_pages(rq, page);
367 while (--num_buf) {
368 err_frags:
369 buf = virtqueue_get_buf(rq->vq, &len);
370 if (unlikely(!buf)) {
371 pr_debug("%s: rx error: %d buffers missing\n",
372 dev->name, num_buf);
373 dev->stats.rx_length_errors++;
374 break;
376 page = buf;
377 give_pages(rq, page);
378 --rq->num;
380 err_buf:
381 dev->stats.rx_dropped++;
382 dev_kfree_skb(skb);
383 return NULL;
386 static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
388 struct virtnet_info *vi = rq->vq->vdev->priv;
389 struct net_device *dev = vi->dev;
390 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
391 struct sk_buff *skb;
392 struct skb_vnet_hdr *hdr;
394 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
395 pr_debug("%s: short packet %i\n", dev->name, len);
396 dev->stats.rx_length_errors++;
397 if (vi->mergeable_rx_bufs || vi->big_packets)
398 give_pages(rq, buf);
399 else
400 dev_kfree_skb(buf);
401 return;
403 if (vi->mergeable_rx_bufs)
404 skb = receive_mergeable(dev, rq, buf, len);
405 else if (vi->big_packets)
406 skb = receive_big(dev, rq, buf);
407 else
408 skb = receive_small(buf, len);
410 if (unlikely(!skb))
411 return;
413 hdr = skb_vnet_hdr(skb);
415 u64_stats_update_begin(&stats->rx_syncp);
416 stats->rx_bytes += skb->len;
417 stats->rx_packets++;
418 u64_stats_update_end(&stats->rx_syncp);
420 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
421 pr_debug("Needs csum!\n");
422 if (!skb_partial_csum_set(skb,
423 hdr->hdr.csum_start,
424 hdr->hdr.csum_offset))
425 goto frame_err;
426 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
427 skb->ip_summed = CHECKSUM_UNNECESSARY;
430 skb->protocol = eth_type_trans(skb, dev);
431 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
432 ntohs(skb->protocol), skb->len, skb->pkt_type);
434 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
435 pr_debug("GSO!\n");
436 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
437 case VIRTIO_NET_HDR_GSO_TCPV4:
438 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
439 break;
440 case VIRTIO_NET_HDR_GSO_UDP:
441 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
442 break;
443 case VIRTIO_NET_HDR_GSO_TCPV6:
444 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
445 break;
446 default:
447 net_warn_ratelimited("%s: bad gso type %u.\n",
448 dev->name, hdr->hdr.gso_type);
449 goto frame_err;
452 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
453 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
455 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
456 if (skb_shinfo(skb)->gso_size == 0) {
457 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
458 goto frame_err;
461 /* Header must be checked, and gso_segs computed. */
462 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
463 skb_shinfo(skb)->gso_segs = 0;
466 netif_receive_skb(skb);
467 return;
469 frame_err:
470 dev->stats.rx_frame_errors++;
471 dev_kfree_skb(skb);
474 static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
476 struct virtnet_info *vi = rq->vq->vdev->priv;
477 struct sk_buff *skb;
478 struct skb_vnet_hdr *hdr;
479 int err;
481 skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
482 if (unlikely(!skb))
483 return -ENOMEM;
485 skb_put(skb, MAX_PACKET_LEN);
487 hdr = skb_vnet_hdr(skb);
488 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
490 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
492 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
493 if (err < 0)
494 dev_kfree_skb(skb);
496 return err;
499 static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
501 struct page *first, *list = NULL;
502 char *p;
503 int i, err, offset;
505 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
506 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
507 first = get_a_page(rq, gfp);
508 if (!first) {
509 if (list)
510 give_pages(rq, list);
511 return -ENOMEM;
513 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
515 /* chain new page in list head to match sg */
516 first->private = (unsigned long)list;
517 list = first;
520 first = get_a_page(rq, gfp);
521 if (!first) {
522 give_pages(rq, list);
523 return -ENOMEM;
525 p = page_address(first);
527 /* rq->sg[0], rq->sg[1] share the same page */
528 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
529 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
531 /* rq->sg[1] for data packet, from offset */
532 offset = sizeof(struct padded_vnet_hdr);
533 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
535 /* chain first in list head */
536 first->private = (unsigned long)list;
537 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
538 first, gfp);
539 if (err < 0)
540 give_pages(rq, first);
542 return err;
545 static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
547 struct page *page;
548 int err;
550 page = get_a_page(rq, gfp);
551 if (!page)
552 return -ENOMEM;
554 sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
556 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, page, gfp);
557 if (err < 0)
558 give_pages(rq, page);
560 return err;
564 * Returns false if we couldn't fill entirely (OOM).
566 * Normally run in the receive path, but can also be run from ndo_open
567 * before we're receiving packets, or from refill_work which is
568 * careful to disable receiving (using napi_disable).
570 static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
572 struct virtnet_info *vi = rq->vq->vdev->priv;
573 int err;
574 bool oom;
576 do {
577 if (vi->mergeable_rx_bufs)
578 err = add_recvbuf_mergeable(rq, gfp);
579 else if (vi->big_packets)
580 err = add_recvbuf_big(rq, gfp);
581 else
582 err = add_recvbuf_small(rq, gfp);
584 oom = err == -ENOMEM;
585 if (err)
586 break;
587 ++rq->num;
588 } while (rq->vq->num_free);
589 if (unlikely(rq->num > rq->max))
590 rq->max = rq->num;
591 virtqueue_kick(rq->vq);
592 return !oom;
595 static void skb_recv_done(struct virtqueue *rvq)
597 struct virtnet_info *vi = rvq->vdev->priv;
598 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
600 /* Schedule NAPI, Suppress further interrupts if successful. */
601 if (napi_schedule_prep(&rq->napi)) {
602 virtqueue_disable_cb(rvq);
603 __napi_schedule(&rq->napi);
607 static void virtnet_napi_enable(struct receive_queue *rq)
609 napi_enable(&rq->napi);
611 /* If all buffers were filled by other side before we napi_enabled, we
612 * won't get another interrupt, so process any outstanding packets
613 * now. virtnet_poll wants re-enable the queue, so we disable here.
614 * We synchronize against interrupts via NAPI_STATE_SCHED */
615 if (napi_schedule_prep(&rq->napi)) {
616 virtqueue_disable_cb(rq->vq);
617 local_bh_disable();
618 __napi_schedule(&rq->napi);
619 local_bh_enable();
623 static void refill_work(struct work_struct *work)
625 struct virtnet_info *vi =
626 container_of(work, struct virtnet_info, refill.work);
627 bool still_empty;
628 int i;
630 for (i = 0; i < vi->curr_queue_pairs; i++) {
631 struct receive_queue *rq = &vi->rq[i];
633 napi_disable(&rq->napi);
634 still_empty = !try_fill_recv(rq, GFP_KERNEL);
635 virtnet_napi_enable(rq);
637 /* In theory, this can happen: if we don't get any buffers in
638 * we will *never* try to fill again.
640 if (still_empty)
641 schedule_delayed_work(&vi->refill, HZ/2);
645 static int virtnet_poll(struct napi_struct *napi, int budget)
647 struct receive_queue *rq =
648 container_of(napi, struct receive_queue, napi);
649 struct virtnet_info *vi = rq->vq->vdev->priv;
650 void *buf;
651 unsigned int r, len, received = 0;
653 again:
654 while (received < budget &&
655 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
656 receive_buf(rq, buf, len);
657 --rq->num;
658 received++;
661 if (rq->num < rq->max / 2) {
662 if (!try_fill_recv(rq, GFP_ATOMIC))
663 schedule_delayed_work(&vi->refill, 0);
666 /* Out of packets? */
667 if (received < budget) {
668 r = virtqueue_enable_cb_prepare(rq->vq);
669 napi_complete(napi);
670 if (unlikely(virtqueue_poll(rq->vq, r)) &&
671 napi_schedule_prep(napi)) {
672 virtqueue_disable_cb(rq->vq);
673 __napi_schedule(napi);
674 goto again;
678 return received;
681 static int virtnet_open(struct net_device *dev)
683 struct virtnet_info *vi = netdev_priv(dev);
684 int i;
686 for (i = 0; i < vi->max_queue_pairs; i++) {
687 if (i < vi->curr_queue_pairs)
688 /* Make sure we have some buffers: if oom use wq. */
689 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
690 schedule_delayed_work(&vi->refill, 0);
691 virtnet_napi_enable(&vi->rq[i]);
694 return 0;
697 static void free_old_xmit_skbs(struct send_queue *sq)
699 struct sk_buff *skb;
700 unsigned int len;
701 struct virtnet_info *vi = sq->vq->vdev->priv;
702 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
704 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
705 pr_debug("Sent skb %p\n", skb);
707 u64_stats_update_begin(&stats->tx_syncp);
708 stats->tx_bytes += skb->len;
709 stats->tx_packets++;
710 u64_stats_update_end(&stats->tx_syncp);
712 dev_kfree_skb_any(skb);
716 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
718 struct skb_vnet_hdr *hdr;
719 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
720 struct virtnet_info *vi = sq->vq->vdev->priv;
721 unsigned num_sg;
722 unsigned hdr_len;
723 bool can_push;
725 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
726 if (vi->mergeable_rx_bufs)
727 hdr_len = sizeof hdr->mhdr;
728 else
729 hdr_len = sizeof hdr->hdr;
731 can_push = vi->any_header_sg &&
732 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
733 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
734 /* Even if we can, don't push here yet as this would skew
735 * csum_start offset below. */
736 if (can_push)
737 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
738 else
739 hdr = skb_vnet_hdr(skb);
741 if (skb->ip_summed == CHECKSUM_PARTIAL) {
742 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
743 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
744 hdr->hdr.csum_offset = skb->csum_offset;
745 } else {
746 hdr->hdr.flags = 0;
747 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
750 if (skb_is_gso(skb)) {
751 hdr->hdr.hdr_len = skb_headlen(skb);
752 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
753 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
754 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
755 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
756 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
757 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
758 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
759 else
760 BUG();
761 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
762 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
763 } else {
764 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
765 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
768 if (vi->mergeable_rx_bufs)
769 hdr->mhdr.num_buffers = 0;
771 if (can_push) {
772 __skb_push(skb, hdr_len);
773 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
774 /* Pull header back to avoid skew in tx bytes calculations. */
775 __skb_pull(skb, hdr_len);
776 } else {
777 sg_set_buf(sq->sg, hdr, hdr_len);
778 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
780 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
783 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
785 struct virtnet_info *vi = netdev_priv(dev);
786 int qnum = skb_get_queue_mapping(skb);
787 struct send_queue *sq = &vi->sq[qnum];
788 int err;
790 /* Free up any pending old buffers before queueing new ones. */
791 free_old_xmit_skbs(sq);
793 /* Try to transmit */
794 err = xmit_skb(sq, skb);
796 /* This should not happen! */
797 if (unlikely(err)) {
798 dev->stats.tx_fifo_errors++;
799 if (net_ratelimit())
800 dev_warn(&dev->dev,
801 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
802 dev->stats.tx_dropped++;
803 kfree_skb(skb);
804 return NETDEV_TX_OK;
806 virtqueue_kick(sq->vq);
808 /* Don't wait up for transmitted skbs to be freed. */
809 skb_orphan(skb);
810 nf_reset(skb);
812 /* Apparently nice girls don't return TX_BUSY; stop the queue
813 * before it gets out of hand. Naturally, this wastes entries. */
814 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
815 netif_stop_subqueue(dev, qnum);
816 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
817 /* More just got used, free them then recheck. */
818 free_old_xmit_skbs(sq);
819 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
820 netif_start_subqueue(dev, qnum);
821 virtqueue_disable_cb(sq->vq);
826 return NETDEV_TX_OK;
830 * Send command via the control virtqueue and check status. Commands
831 * supported by the hypervisor, as indicated by feature bits, should
832 * never fail unless improperly formated.
834 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
835 struct scatterlist *out,
836 struct scatterlist *in)
838 struct scatterlist *sgs[4], hdr, stat;
839 struct virtio_net_ctrl_hdr ctrl;
840 virtio_net_ctrl_ack status = ~0;
841 unsigned out_num = 0, in_num = 0, tmp;
843 /* Caller should know better */
844 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
846 ctrl.class = class;
847 ctrl.cmd = cmd;
848 /* Add header */
849 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
850 sgs[out_num++] = &hdr;
852 if (out)
853 sgs[out_num++] = out;
854 if (in)
855 sgs[out_num + in_num++] = in;
857 /* Add return status. */
858 sg_init_one(&stat, &status, sizeof(status));
859 sgs[out_num + in_num++] = &stat;
861 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
862 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
863 < 0);
865 virtqueue_kick(vi->cvq);
867 /* Spin for a response, the kick causes an ioport write, trapping
868 * into the hypervisor, so the request should be handled immediately.
870 while (!virtqueue_get_buf(vi->cvq, &tmp))
871 cpu_relax();
873 return status == VIRTIO_NET_OK;
876 static int virtnet_set_mac_address(struct net_device *dev, void *p)
878 struct virtnet_info *vi = netdev_priv(dev);
879 struct virtio_device *vdev = vi->vdev;
880 int ret;
881 struct sockaddr *addr = p;
882 struct scatterlist sg;
884 ret = eth_prepare_mac_addr_change(dev, p);
885 if (ret)
886 return ret;
888 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
889 sg_init_one(&sg, addr->sa_data, dev->addr_len);
890 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
891 VIRTIO_NET_CTRL_MAC_ADDR_SET,
892 &sg, NULL)) {
893 dev_warn(&vdev->dev,
894 "Failed to set mac address by vq command.\n");
895 return -EINVAL;
897 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
898 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
899 addr->sa_data, dev->addr_len);
902 eth_commit_mac_addr_change(dev, p);
904 return 0;
907 static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
908 struct rtnl_link_stats64 *tot)
910 struct virtnet_info *vi = netdev_priv(dev);
911 int cpu;
912 unsigned int start;
914 for_each_possible_cpu(cpu) {
915 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
916 u64 tpackets, tbytes, rpackets, rbytes;
918 do {
919 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
920 tpackets = stats->tx_packets;
921 tbytes = stats->tx_bytes;
922 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
924 do {
925 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
926 rpackets = stats->rx_packets;
927 rbytes = stats->rx_bytes;
928 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
930 tot->rx_packets += rpackets;
931 tot->tx_packets += tpackets;
932 tot->rx_bytes += rbytes;
933 tot->tx_bytes += tbytes;
936 tot->tx_dropped = dev->stats.tx_dropped;
937 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
938 tot->rx_dropped = dev->stats.rx_dropped;
939 tot->rx_length_errors = dev->stats.rx_length_errors;
940 tot->rx_frame_errors = dev->stats.rx_frame_errors;
942 return tot;
945 #ifdef CONFIG_NET_POLL_CONTROLLER
946 static void virtnet_netpoll(struct net_device *dev)
948 struct virtnet_info *vi = netdev_priv(dev);
949 int i;
951 for (i = 0; i < vi->curr_queue_pairs; i++)
952 napi_schedule(&vi->rq[i].napi);
954 #endif
956 static void virtnet_ack_link_announce(struct virtnet_info *vi)
958 rtnl_lock();
959 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
960 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
961 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
962 rtnl_unlock();
965 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
967 struct scatterlist sg;
968 struct virtio_net_ctrl_mq s;
969 struct net_device *dev = vi->dev;
971 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
972 return 0;
974 s.virtqueue_pairs = queue_pairs;
975 sg_init_one(&sg, &s, sizeof(s));
977 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
978 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
979 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
980 queue_pairs);
981 return -EINVAL;
982 } else {
983 vi->curr_queue_pairs = queue_pairs;
984 /* virtnet_open() will refill when device is going to up. */
985 if (dev->flags & IFF_UP)
986 schedule_delayed_work(&vi->refill, 0);
989 return 0;
992 static int virtnet_close(struct net_device *dev)
994 struct virtnet_info *vi = netdev_priv(dev);
995 int i;
997 /* Make sure refill_work doesn't re-enable napi! */
998 cancel_delayed_work_sync(&vi->refill);
1000 for (i = 0; i < vi->max_queue_pairs; i++)
1001 napi_disable(&vi->rq[i].napi);
1003 return 0;
1006 static void virtnet_set_rx_mode(struct net_device *dev)
1008 struct virtnet_info *vi = netdev_priv(dev);
1009 struct scatterlist sg[2];
1010 u8 promisc, allmulti;
1011 struct virtio_net_ctrl_mac *mac_data;
1012 struct netdev_hw_addr *ha;
1013 int uc_count;
1014 int mc_count;
1015 void *buf;
1016 int i;
1018 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
1019 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1020 return;
1022 promisc = ((dev->flags & IFF_PROMISC) != 0);
1023 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1025 sg_init_one(sg, &promisc, sizeof(promisc));
1027 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1028 VIRTIO_NET_CTRL_RX_PROMISC,
1029 sg, NULL))
1030 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1031 promisc ? "en" : "dis");
1033 sg_init_one(sg, &allmulti, sizeof(allmulti));
1035 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1036 VIRTIO_NET_CTRL_RX_ALLMULTI,
1037 sg, NULL))
1038 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1039 allmulti ? "en" : "dis");
1041 uc_count = netdev_uc_count(dev);
1042 mc_count = netdev_mc_count(dev);
1043 /* MAC filter - use one buffer for both lists */
1044 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1045 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1046 mac_data = buf;
1047 if (!buf)
1048 return;
1050 sg_init_table(sg, 2);
1052 /* Store the unicast list and count in the front of the buffer */
1053 mac_data->entries = uc_count;
1054 i = 0;
1055 netdev_for_each_uc_addr(ha, dev)
1056 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1058 sg_set_buf(&sg[0], mac_data,
1059 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1061 /* multicast list and count fill the end */
1062 mac_data = (void *)&mac_data->macs[uc_count][0];
1064 mac_data->entries = mc_count;
1065 i = 0;
1066 netdev_for_each_mc_addr(ha, dev)
1067 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1069 sg_set_buf(&sg[1], mac_data,
1070 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1072 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1073 VIRTIO_NET_CTRL_MAC_TABLE_SET,
1074 sg, NULL))
1075 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
1077 kfree(buf);
1080 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1081 __be16 proto, u16 vid)
1083 struct virtnet_info *vi = netdev_priv(dev);
1084 struct scatterlist sg;
1086 sg_init_one(&sg, &vid, sizeof(vid));
1088 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1089 VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
1090 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
1091 return 0;
1094 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1095 __be16 proto, u16 vid)
1097 struct virtnet_info *vi = netdev_priv(dev);
1098 struct scatterlist sg;
1100 sg_init_one(&sg, &vid, sizeof(vid));
1102 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1103 VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
1104 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
1105 return 0;
1108 static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1110 int i;
1111 int cpu;
1113 if (vi->affinity_hint_set) {
1114 for (i = 0; i < vi->max_queue_pairs; i++) {
1115 virtqueue_set_affinity(vi->rq[i].vq, -1);
1116 virtqueue_set_affinity(vi->sq[i].vq, -1);
1119 vi->affinity_hint_set = false;
1122 i = 0;
1123 for_each_online_cpu(cpu) {
1124 if (cpu == hcpu) {
1125 *per_cpu_ptr(vi->vq_index, cpu) = -1;
1126 } else {
1127 *per_cpu_ptr(vi->vq_index, cpu) =
1128 ++i % vi->curr_queue_pairs;
1133 static void virtnet_set_affinity(struct virtnet_info *vi)
1135 int i;
1136 int cpu;
1138 /* In multiqueue mode, when the number of cpu is equal to the number of
1139 * queue pairs, we let the queue pairs to be private to one cpu by
1140 * setting the affinity hint to eliminate the contention.
1142 if (vi->curr_queue_pairs == 1 ||
1143 vi->max_queue_pairs != num_online_cpus()) {
1144 virtnet_clean_affinity(vi, -1);
1145 return;
1148 i = 0;
1149 for_each_online_cpu(cpu) {
1150 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1151 virtqueue_set_affinity(vi->sq[i].vq, cpu);
1152 *per_cpu_ptr(vi->vq_index, cpu) = i;
1153 i++;
1156 vi->affinity_hint_set = true;
1159 static int virtnet_cpu_callback(struct notifier_block *nfb,
1160 unsigned long action, void *hcpu)
1162 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1164 switch(action & ~CPU_TASKS_FROZEN) {
1165 case CPU_ONLINE:
1166 case CPU_DOWN_FAILED:
1167 case CPU_DEAD:
1168 virtnet_set_affinity(vi);
1169 break;
1170 case CPU_DOWN_PREPARE:
1171 virtnet_clean_affinity(vi, (long)hcpu);
1172 break;
1173 default:
1174 break;
1177 return NOTIFY_OK;
1180 static void virtnet_get_ringparam(struct net_device *dev,
1181 struct ethtool_ringparam *ring)
1183 struct virtnet_info *vi = netdev_priv(dev);
1185 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1186 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
1187 ring->rx_pending = ring->rx_max_pending;
1188 ring->tx_pending = ring->tx_max_pending;
1192 static void virtnet_get_drvinfo(struct net_device *dev,
1193 struct ethtool_drvinfo *info)
1195 struct virtnet_info *vi = netdev_priv(dev);
1196 struct virtio_device *vdev = vi->vdev;
1198 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1199 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1200 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1204 /* TODO: Eliminate OOO packets during switching */
1205 static int virtnet_set_channels(struct net_device *dev,
1206 struct ethtool_channels *channels)
1208 struct virtnet_info *vi = netdev_priv(dev);
1209 u16 queue_pairs = channels->combined_count;
1210 int err;
1212 /* We don't support separate rx/tx channels.
1213 * We don't allow setting 'other' channels.
1215 if (channels->rx_count || channels->tx_count || channels->other_count)
1216 return -EINVAL;
1218 if (queue_pairs > vi->max_queue_pairs)
1219 return -EINVAL;
1221 get_online_cpus();
1222 err = virtnet_set_queues(vi, queue_pairs);
1223 if (!err) {
1224 netif_set_real_num_tx_queues(dev, queue_pairs);
1225 netif_set_real_num_rx_queues(dev, queue_pairs);
1227 virtnet_set_affinity(vi);
1229 put_online_cpus();
1231 return err;
1234 static void virtnet_get_channels(struct net_device *dev,
1235 struct ethtool_channels *channels)
1237 struct virtnet_info *vi = netdev_priv(dev);
1239 channels->combined_count = vi->curr_queue_pairs;
1240 channels->max_combined = vi->max_queue_pairs;
1241 channels->max_other = 0;
1242 channels->rx_count = 0;
1243 channels->tx_count = 0;
1244 channels->other_count = 0;
1247 static const struct ethtool_ops virtnet_ethtool_ops = {
1248 .get_drvinfo = virtnet_get_drvinfo,
1249 .get_link = ethtool_op_get_link,
1250 .get_ringparam = virtnet_get_ringparam,
1251 .set_channels = virtnet_set_channels,
1252 .get_channels = virtnet_get_channels,
1255 #define MIN_MTU 68
1256 #define MAX_MTU 65535
1258 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1260 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1261 return -EINVAL;
1262 dev->mtu = new_mtu;
1263 return 0;
1266 /* To avoid contending a lock hold by a vcpu who would exit to host, select the
1267 * txq based on the processor id.
1269 static u16 virtnet_select_queue(struct net_device *dev, struct sk_buff *skb)
1271 int txq;
1272 struct virtnet_info *vi = netdev_priv(dev);
1274 if (skb_rx_queue_recorded(skb)) {
1275 txq = skb_get_rx_queue(skb);
1276 } else {
1277 txq = *__this_cpu_ptr(vi->vq_index);
1278 if (txq == -1)
1279 txq = 0;
1282 while (unlikely(txq >= dev->real_num_tx_queues))
1283 txq -= dev->real_num_tx_queues;
1285 return txq;
1288 static const struct net_device_ops virtnet_netdev = {
1289 .ndo_open = virtnet_open,
1290 .ndo_stop = virtnet_close,
1291 .ndo_start_xmit = start_xmit,
1292 .ndo_validate_addr = eth_validate_addr,
1293 .ndo_set_mac_address = virtnet_set_mac_address,
1294 .ndo_set_rx_mode = virtnet_set_rx_mode,
1295 .ndo_change_mtu = virtnet_change_mtu,
1296 .ndo_get_stats64 = virtnet_stats,
1297 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1298 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1299 .ndo_select_queue = virtnet_select_queue,
1300 #ifdef CONFIG_NET_POLL_CONTROLLER
1301 .ndo_poll_controller = virtnet_netpoll,
1302 #endif
1305 static void virtnet_config_changed_work(struct work_struct *work)
1307 struct virtnet_info *vi =
1308 container_of(work, struct virtnet_info, config_work);
1309 u16 v;
1311 mutex_lock(&vi->config_lock);
1312 if (!vi->config_enable)
1313 goto done;
1315 if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
1316 offsetof(struct virtio_net_config, status),
1317 &v) < 0)
1318 goto done;
1320 if (v & VIRTIO_NET_S_ANNOUNCE) {
1321 netdev_notify_peers(vi->dev);
1322 virtnet_ack_link_announce(vi);
1325 /* Ignore unknown (future) status bits */
1326 v &= VIRTIO_NET_S_LINK_UP;
1328 if (vi->status == v)
1329 goto done;
1331 vi->status = v;
1333 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1334 netif_carrier_on(vi->dev);
1335 netif_tx_wake_all_queues(vi->dev);
1336 } else {
1337 netif_carrier_off(vi->dev);
1338 netif_tx_stop_all_queues(vi->dev);
1340 done:
1341 mutex_unlock(&vi->config_lock);
1344 static void virtnet_config_changed(struct virtio_device *vdev)
1346 struct virtnet_info *vi = vdev->priv;
1348 schedule_work(&vi->config_work);
1351 static void virtnet_free_queues(struct virtnet_info *vi)
1353 int i;
1355 for (i = 0; i < vi->max_queue_pairs; i++)
1356 netif_napi_del(&vi->rq[i].napi);
1358 kfree(vi->rq);
1359 kfree(vi->sq);
1362 static void free_receive_bufs(struct virtnet_info *vi)
1364 int i;
1366 for (i = 0; i < vi->max_queue_pairs; i++) {
1367 while (vi->rq[i].pages)
1368 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1372 static void free_unused_bufs(struct virtnet_info *vi)
1374 void *buf;
1375 int i;
1377 for (i = 0; i < vi->max_queue_pairs; i++) {
1378 struct virtqueue *vq = vi->sq[i].vq;
1379 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1380 dev_kfree_skb(buf);
1383 for (i = 0; i < vi->max_queue_pairs; i++) {
1384 struct virtqueue *vq = vi->rq[i].vq;
1386 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1387 if (vi->mergeable_rx_bufs || vi->big_packets)
1388 give_pages(&vi->rq[i], buf);
1389 else
1390 dev_kfree_skb(buf);
1391 --vi->rq[i].num;
1393 BUG_ON(vi->rq[i].num != 0);
1397 static void virtnet_del_vqs(struct virtnet_info *vi)
1399 struct virtio_device *vdev = vi->vdev;
1401 virtnet_clean_affinity(vi, -1);
1403 vdev->config->del_vqs(vdev);
1405 virtnet_free_queues(vi);
1408 static int virtnet_find_vqs(struct virtnet_info *vi)
1410 vq_callback_t **callbacks;
1411 struct virtqueue **vqs;
1412 int ret = -ENOMEM;
1413 int i, total_vqs;
1414 const char **names;
1416 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1417 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1418 * possible control vq.
1420 total_vqs = vi->max_queue_pairs * 2 +
1421 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1423 /* Allocate space for find_vqs parameters */
1424 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1425 if (!vqs)
1426 goto err_vq;
1427 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1428 if (!callbacks)
1429 goto err_callback;
1430 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1431 if (!names)
1432 goto err_names;
1434 /* Parameters for control virtqueue, if any */
1435 if (vi->has_cvq) {
1436 callbacks[total_vqs - 1] = NULL;
1437 names[total_vqs - 1] = "control";
1440 /* Allocate/initialize parameters for send/receive virtqueues */
1441 for (i = 0; i < vi->max_queue_pairs; i++) {
1442 callbacks[rxq2vq(i)] = skb_recv_done;
1443 callbacks[txq2vq(i)] = skb_xmit_done;
1444 sprintf(vi->rq[i].name, "input.%d", i);
1445 sprintf(vi->sq[i].name, "output.%d", i);
1446 names[rxq2vq(i)] = vi->rq[i].name;
1447 names[txq2vq(i)] = vi->sq[i].name;
1450 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1451 names);
1452 if (ret)
1453 goto err_find;
1455 if (vi->has_cvq) {
1456 vi->cvq = vqs[total_vqs - 1];
1457 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1458 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1461 for (i = 0; i < vi->max_queue_pairs; i++) {
1462 vi->rq[i].vq = vqs[rxq2vq(i)];
1463 vi->sq[i].vq = vqs[txq2vq(i)];
1466 kfree(names);
1467 kfree(callbacks);
1468 kfree(vqs);
1470 return 0;
1472 err_find:
1473 kfree(names);
1474 err_names:
1475 kfree(callbacks);
1476 err_callback:
1477 kfree(vqs);
1478 err_vq:
1479 return ret;
1482 static int virtnet_alloc_queues(struct virtnet_info *vi)
1484 int i;
1486 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1487 if (!vi->sq)
1488 goto err_sq;
1489 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1490 if (!vi->rq)
1491 goto err_rq;
1493 INIT_DELAYED_WORK(&vi->refill, refill_work);
1494 for (i = 0; i < vi->max_queue_pairs; i++) {
1495 vi->rq[i].pages = NULL;
1496 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1497 napi_weight);
1499 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1500 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1503 return 0;
1505 err_rq:
1506 kfree(vi->sq);
1507 err_sq:
1508 return -ENOMEM;
1511 static int init_vqs(struct virtnet_info *vi)
1513 int ret;
1515 /* Allocate send & receive queues */
1516 ret = virtnet_alloc_queues(vi);
1517 if (ret)
1518 goto err;
1520 ret = virtnet_find_vqs(vi);
1521 if (ret)
1522 goto err_free;
1524 get_online_cpus();
1525 virtnet_set_affinity(vi);
1526 put_online_cpus();
1528 return 0;
1530 err_free:
1531 virtnet_free_queues(vi);
1532 err:
1533 return ret;
1536 static int virtnet_probe(struct virtio_device *vdev)
1538 int i, err;
1539 struct net_device *dev;
1540 struct virtnet_info *vi;
1541 u16 max_queue_pairs;
1543 /* Find if host supports multiqueue virtio_net device */
1544 err = virtio_config_val(vdev, VIRTIO_NET_F_MQ,
1545 offsetof(struct virtio_net_config,
1546 max_virtqueue_pairs), &max_queue_pairs);
1548 /* We need at least 2 queue's */
1549 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1550 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1551 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1552 max_queue_pairs = 1;
1554 /* Allocate ourselves a network device with room for our info */
1555 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1556 if (!dev)
1557 return -ENOMEM;
1559 /* Set up network device as normal. */
1560 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
1561 dev->netdev_ops = &virtnet_netdev;
1562 dev->features = NETIF_F_HIGHDMA;
1564 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1565 SET_NETDEV_DEV(dev, &vdev->dev);
1567 /* Do we support "hardware" checksums? */
1568 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1569 /* This opens up the world of extra features. */
1570 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1571 if (csum)
1572 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1574 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1575 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
1576 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1578 /* Individual feature bits: what can host handle? */
1579 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1580 dev->hw_features |= NETIF_F_TSO;
1581 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1582 dev->hw_features |= NETIF_F_TSO6;
1583 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1584 dev->hw_features |= NETIF_F_TSO_ECN;
1585 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1586 dev->hw_features |= NETIF_F_UFO;
1588 if (gso)
1589 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1590 /* (!csum && gso) case will be fixed by register_netdev() */
1592 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1593 dev->features |= NETIF_F_RXCSUM;
1595 dev->vlan_features = dev->features;
1597 /* Configuration may specify what MAC to use. Otherwise random. */
1598 if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
1599 offsetof(struct virtio_net_config, mac),
1600 dev->dev_addr, dev->addr_len) < 0)
1601 eth_hw_addr_random(dev);
1603 /* Set up our device-specific information */
1604 vi = netdev_priv(dev);
1605 vi->dev = dev;
1606 vi->vdev = vdev;
1607 vdev->priv = vi;
1608 vi->stats = alloc_percpu(struct virtnet_stats);
1609 err = -ENOMEM;
1610 if (vi->stats == NULL)
1611 goto free;
1613 vi->vq_index = alloc_percpu(int);
1614 if (vi->vq_index == NULL)
1615 goto free_stats;
1617 mutex_init(&vi->config_lock);
1618 vi->config_enable = true;
1619 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1621 /* If we can receive ANY GSO packets, we must allocate large ones. */
1622 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1623 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1624 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
1625 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
1626 vi->big_packets = true;
1628 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1629 vi->mergeable_rx_bufs = true;
1631 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1632 vi->any_header_sg = true;
1634 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1635 vi->has_cvq = true;
1637 /* Use single tx/rx queue pair as default */
1638 vi->curr_queue_pairs = 1;
1639 vi->max_queue_pairs = max_queue_pairs;
1641 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
1642 err = init_vqs(vi);
1643 if (err)
1644 goto free_index;
1646 netif_set_real_num_tx_queues(dev, 1);
1647 netif_set_real_num_rx_queues(dev, 1);
1649 err = register_netdev(dev);
1650 if (err) {
1651 pr_debug("virtio_net: registering device failed\n");
1652 goto free_vqs;
1655 /* Last of all, set up some receive buffers. */
1656 for (i = 0; i < vi->curr_queue_pairs; i++) {
1657 try_fill_recv(&vi->rq[i], GFP_KERNEL);
1659 /* If we didn't even get one input buffer, we're useless. */
1660 if (vi->rq[i].num == 0) {
1661 free_unused_bufs(vi);
1662 err = -ENOMEM;
1663 goto free_recv_bufs;
1667 vi->nb.notifier_call = &virtnet_cpu_callback;
1668 err = register_hotcpu_notifier(&vi->nb);
1669 if (err) {
1670 pr_debug("virtio_net: registering cpu notifier failed\n");
1671 goto free_recv_bufs;
1674 /* Assume link up if device can't report link status,
1675 otherwise get link status from config. */
1676 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1677 netif_carrier_off(dev);
1678 schedule_work(&vi->config_work);
1679 } else {
1680 vi->status = VIRTIO_NET_S_LINK_UP;
1681 netif_carrier_on(dev);
1684 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1685 dev->name, max_queue_pairs);
1687 return 0;
1689 free_recv_bufs:
1690 free_receive_bufs(vi);
1691 unregister_netdev(dev);
1692 free_vqs:
1693 cancel_delayed_work_sync(&vi->refill);
1694 virtnet_del_vqs(vi);
1695 free_index:
1696 free_percpu(vi->vq_index);
1697 free_stats:
1698 free_percpu(vi->stats);
1699 free:
1700 free_netdev(dev);
1701 return err;
1704 static void remove_vq_common(struct virtnet_info *vi)
1706 vi->vdev->config->reset(vi->vdev);
1708 /* Free unused buffers in both send and recv, if any. */
1709 free_unused_bufs(vi);
1711 free_receive_bufs(vi);
1713 virtnet_del_vqs(vi);
1716 static void virtnet_remove(struct virtio_device *vdev)
1718 struct virtnet_info *vi = vdev->priv;
1720 unregister_hotcpu_notifier(&vi->nb);
1722 /* Prevent config work handler from accessing the device. */
1723 mutex_lock(&vi->config_lock);
1724 vi->config_enable = false;
1725 mutex_unlock(&vi->config_lock);
1727 unregister_netdev(vi->dev);
1729 remove_vq_common(vi);
1731 flush_work(&vi->config_work);
1733 free_percpu(vi->vq_index);
1734 free_percpu(vi->stats);
1735 free_netdev(vi->dev);
1738 #ifdef CONFIG_PM
1739 static int virtnet_freeze(struct virtio_device *vdev)
1741 struct virtnet_info *vi = vdev->priv;
1742 int i;
1744 unregister_hotcpu_notifier(&vi->nb);
1746 /* Prevent config work handler from accessing the device */
1747 mutex_lock(&vi->config_lock);
1748 vi->config_enable = false;
1749 mutex_unlock(&vi->config_lock);
1751 netif_device_detach(vi->dev);
1752 cancel_delayed_work_sync(&vi->refill);
1754 if (netif_running(vi->dev))
1755 for (i = 0; i < vi->max_queue_pairs; i++) {
1756 napi_disable(&vi->rq[i].napi);
1757 netif_napi_del(&vi->rq[i].napi);
1760 remove_vq_common(vi);
1762 flush_work(&vi->config_work);
1764 return 0;
1767 static int virtnet_restore(struct virtio_device *vdev)
1769 struct virtnet_info *vi = vdev->priv;
1770 int err, i;
1772 err = init_vqs(vi);
1773 if (err)
1774 return err;
1776 if (netif_running(vi->dev)) {
1777 for (i = 0; i < vi->curr_queue_pairs; i++)
1778 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1779 schedule_delayed_work(&vi->refill, 0);
1781 for (i = 0; i < vi->max_queue_pairs; i++)
1782 virtnet_napi_enable(&vi->rq[i]);
1785 netif_device_attach(vi->dev);
1787 mutex_lock(&vi->config_lock);
1788 vi->config_enable = true;
1789 mutex_unlock(&vi->config_lock);
1791 rtnl_lock();
1792 virtnet_set_queues(vi, vi->curr_queue_pairs);
1793 rtnl_unlock();
1795 err = register_hotcpu_notifier(&vi->nb);
1796 if (err)
1797 return err;
1799 return 0;
1801 #endif
1803 static struct virtio_device_id id_table[] = {
1804 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1805 { 0 },
1808 static unsigned int features[] = {
1809 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1810 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1811 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
1812 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
1813 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
1814 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
1815 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1816 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
1817 VIRTIO_NET_F_CTRL_MAC_ADDR,
1818 VIRTIO_F_ANY_LAYOUT,
1821 static struct virtio_driver virtio_net_driver = {
1822 .feature_table = features,
1823 .feature_table_size = ARRAY_SIZE(features),
1824 .driver.name = KBUILD_MODNAME,
1825 .driver.owner = THIS_MODULE,
1826 .id_table = id_table,
1827 .probe = virtnet_probe,
1828 .remove = virtnet_remove,
1829 .config_changed = virtnet_config_changed,
1830 #ifdef CONFIG_PM
1831 .freeze = virtnet_freeze,
1832 .restore = virtnet_restore,
1833 #endif
1836 module_virtio_driver(virtio_net_driver);
1838 MODULE_DEVICE_TABLE(virtio, id_table);
1839 MODULE_DESCRIPTION("Virtio network driver");
1840 MODULE_LICENSE("GPL");