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, see <http://www.gnu.org/licenses/>.
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/scatterlist.h>
26 #include <linux/if_vlan.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
30 static int napi_weight = NAPI_POLL_WEIGHT;
31 module_param(napi_weight, int, 0444);
33 static bool csum = true, gso = true;
34 module_param(csum, bool, 0444);
35 module_param(gso, bool, 0444);
37 /* FIXME: MTU in config. */
38 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
40 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
42 #define GOOD_COPY_LEN 128
44 #define VIRTNET_DRIVER_VERSION "1.0.0"
46 struct virtnet_stats {
47 struct u64_stats_sync tx_syncp;
48 struct u64_stats_sync rx_syncp;
56 /* Internal representation of a send virtqueue */
58 /* Virtqueue associated with this send _queue */
61 /* TX: fragments + linear part + virtio header */
62 struct scatterlist sg[MAX_SKB_FRAGS + 2];
64 /* Name of the send queue: output.$index */
68 /* Internal representation of a receive virtqueue */
69 struct receive_queue {
70 /* Virtqueue associated with this receive_queue */
73 struct napi_struct napi;
75 /* Chain pages by the private ptr. */
78 /* RX: fragments + linear part + virtio header */
79 struct scatterlist sg[MAX_SKB_FRAGS + 2];
81 /* Name of this receive queue: input.$index */
86 struct virtio_device *vdev;
87 struct virtqueue *cvq;
88 struct net_device *dev;
89 struct send_queue *sq;
90 struct receive_queue *rq;
93 /* Max # of queue pairs supported by the device */
96 /* # of queue pairs currently used by the driver */
99 /* I like... big packets and I cannot lie! */
102 /* Host will merge rx buffers for big packets (shake it! shake it!) */
103 bool mergeable_rx_bufs;
105 /* Has control virtqueue */
108 /* Host can handle any s/g split between our header and packet data */
111 /* enable config space updates */
114 /* Active statistics */
115 struct virtnet_stats __percpu *stats;
117 /* Work struct for refilling if we run low on memory. */
118 struct delayed_work refill;
120 /* Work struct for config space updates */
121 struct work_struct config_work;
123 /* Lock for config space updates */
124 struct mutex config_lock;
126 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
129 struct page_frag alloc_frag;
131 /* Does the affinity hint is set for virtqueues? */
132 bool affinity_hint_set;
134 /* CPU hot plug notifier */
135 struct notifier_block nb;
138 struct skb_vnet_hdr {
140 struct virtio_net_hdr hdr;
141 struct virtio_net_hdr_mrg_rxbuf mhdr;
145 struct padded_vnet_hdr {
146 struct virtio_net_hdr hdr;
148 * virtio_net_hdr should be in a separated sg buffer because of a
149 * QEMU bug, and data sg buffer shares same page with this header sg.
150 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
155 /* Converting between virtqueue no. and kernel tx/rx queue no.
156 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
158 static int vq2txq(struct virtqueue *vq)
160 return (vq->index - 1) / 2;
163 static int txq2vq(int txq)
168 static int vq2rxq(struct virtqueue *vq)
170 return vq->index / 2;
173 static int rxq2vq(int rxq)
178 static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
180 return (struct skb_vnet_hdr *)skb->cb;
184 * private is used to chain pages for big packets, put the whole
185 * most recent used list in the beginning for reuse
187 static void give_pages(struct receive_queue *rq, struct page *page)
191 /* Find end of list, sew whole thing into vi->rq.pages. */
192 for (end = page; end->private; end = (struct page *)end->private);
193 end->private = (unsigned long)rq->pages;
197 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
199 struct page *p = rq->pages;
202 rq->pages = (struct page *)p->private;
203 /* clear private here, it is used to chain pages */
206 p = alloc_page(gfp_mask);
210 static void skb_xmit_done(struct virtqueue *vq)
212 struct virtnet_info *vi = vq->vdev->priv;
214 /* Suppress further interrupts. */
215 virtqueue_disable_cb(vq);
217 /* We were probably waiting for more output buffers. */
218 netif_wake_subqueue(vi->dev, vq2txq(vq));
221 /* Called from bottom half context */
222 static struct sk_buff *page_to_skb(struct receive_queue *rq,
223 struct page *page, unsigned int offset,
224 unsigned int len, unsigned int truesize)
226 struct virtnet_info *vi = rq->vq->vdev->priv;
228 struct skb_vnet_hdr *hdr;
229 unsigned int copy, hdr_len, hdr_padded_len;
232 p = page_address(page) + offset;
234 /* copy small packet so we can reuse these pages for small data */
235 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
239 hdr = skb_vnet_hdr(skb);
241 if (vi->mergeable_rx_bufs) {
242 hdr_len = sizeof hdr->mhdr;
243 hdr_padded_len = sizeof hdr->mhdr;
245 hdr_len = sizeof hdr->hdr;
246 hdr_padded_len = sizeof(struct padded_vnet_hdr);
249 memcpy(hdr, p, hdr_len);
252 offset += hdr_padded_len;
256 if (copy > skb_tailroom(skb))
257 copy = skb_tailroom(skb);
258 memcpy(skb_put(skb, copy), p, copy);
263 if (vi->mergeable_rx_bufs) {
265 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
272 * Verify that we can indeed put this data into a skb.
273 * This is here to handle cases when the device erroneously
274 * tries to receive more than is possible. This is usually
275 * the case of a broken device.
277 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
278 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
282 BUG_ON(offset >= PAGE_SIZE);
284 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
285 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
286 frag_size, truesize);
288 page = (struct page *)page->private;
293 give_pages(rq, page);
298 static struct sk_buff *receive_small(void *buf, unsigned int len)
300 struct sk_buff * skb = buf;
302 len -= sizeof(struct virtio_net_hdr);
308 static struct sk_buff *receive_big(struct net_device *dev,
309 struct receive_queue *rq,
313 struct page *page = buf;
314 struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
322 dev->stats.rx_dropped++;
323 give_pages(rq, page);
327 static struct sk_buff *receive_mergeable(struct net_device *dev,
328 struct receive_queue *rq,
332 struct skb_vnet_hdr *hdr = buf;
333 int num_buf = hdr->mhdr.num_buffers;
334 struct page *page = virt_to_head_page(buf);
335 int offset = buf - page_address(page);
336 struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
338 struct sk_buff *curr_skb = head_skb;
340 if (unlikely(!curr_skb))
346 buf = virtqueue_get_buf(rq->vq, &len);
347 if (unlikely(!buf)) {
348 pr_debug("%s: rx error: %d buffers out of %d missing\n",
349 dev->name, num_buf, hdr->mhdr.num_buffers);
350 dev->stats.rx_length_errors++;
353 if (unlikely(len > MERGE_BUFFER_LEN)) {
354 pr_debug("%s: rx error: merge buffer too long\n",
356 len = MERGE_BUFFER_LEN;
359 page = virt_to_head_page(buf);
361 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
362 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
363 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
367 if (curr_skb == head_skb)
368 skb_shinfo(curr_skb)->frag_list = nskb;
370 curr_skb->next = nskb;
372 head_skb->truesize += nskb->truesize;
375 if (curr_skb != head_skb) {
376 head_skb->data_len += len;
377 head_skb->len += len;
378 head_skb->truesize += MERGE_BUFFER_LEN;
380 offset = buf - page_address(page);
381 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
383 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
384 len, MERGE_BUFFER_LEN);
386 skb_add_rx_frag(curr_skb, num_skb_frags, page,
387 offset, len, MERGE_BUFFER_LEN);
396 buf = virtqueue_get_buf(rq->vq, &len);
397 if (unlikely(!buf)) {
398 pr_debug("%s: rx error: %d buffers missing\n",
400 dev->stats.rx_length_errors++;
403 page = virt_to_head_page(buf);
407 dev->stats.rx_dropped++;
408 dev_kfree_skb(head_skb);
412 static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
414 struct virtnet_info *vi = rq->vq->vdev->priv;
415 struct net_device *dev = vi->dev;
416 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
418 struct skb_vnet_hdr *hdr;
420 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
421 pr_debug("%s: short packet %i\n", dev->name, len);
422 dev->stats.rx_length_errors++;
423 if (vi->mergeable_rx_bufs)
424 put_page(virt_to_head_page(buf));
425 else if (vi->big_packets)
432 if (vi->mergeable_rx_bufs)
433 skb = receive_mergeable(dev, rq, buf, len);
434 else if (vi->big_packets)
435 skb = receive_big(dev, rq, buf, len);
437 skb = receive_small(buf, len);
442 hdr = skb_vnet_hdr(skb);
444 u64_stats_update_begin(&stats->rx_syncp);
445 stats->rx_bytes += skb->len;
447 u64_stats_update_end(&stats->rx_syncp);
449 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
450 pr_debug("Needs csum!\n");
451 if (!skb_partial_csum_set(skb,
453 hdr->hdr.csum_offset))
455 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
456 skb->ip_summed = CHECKSUM_UNNECESSARY;
459 skb->protocol = eth_type_trans(skb, dev);
460 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
461 ntohs(skb->protocol), skb->len, skb->pkt_type);
463 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
465 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
466 case VIRTIO_NET_HDR_GSO_TCPV4:
467 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
469 case VIRTIO_NET_HDR_GSO_UDP:
470 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
472 case VIRTIO_NET_HDR_GSO_TCPV6:
473 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
476 net_warn_ratelimited("%s: bad gso type %u.\n",
477 dev->name, hdr->hdr.gso_type);
481 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
482 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
484 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
485 if (skb_shinfo(skb)->gso_size == 0) {
486 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
490 /* Header must be checked, and gso_segs computed. */
491 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
492 skb_shinfo(skb)->gso_segs = 0;
495 netif_receive_skb(skb);
499 dev->stats.rx_frame_errors++;
503 static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
505 struct virtnet_info *vi = rq->vq->vdev->priv;
507 struct skb_vnet_hdr *hdr;
510 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
514 skb_put(skb, GOOD_PACKET_LEN);
516 hdr = skb_vnet_hdr(skb);
517 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
519 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
521 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
528 static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
530 struct page *first, *list = NULL;
534 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
535 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
536 first = get_a_page(rq, gfp);
539 give_pages(rq, list);
542 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
544 /* chain new page in list head to match sg */
545 first->private = (unsigned long)list;
549 first = get_a_page(rq, gfp);
551 give_pages(rq, list);
554 p = page_address(first);
556 /* rq->sg[0], rq->sg[1] share the same page */
557 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
558 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
560 /* rq->sg[1] for data packet, from offset */
561 offset = sizeof(struct padded_vnet_hdr);
562 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
564 /* chain first in list head */
565 first->private = (unsigned long)list;
566 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
569 give_pages(rq, first);
574 static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
576 struct virtnet_info *vi = rq->vq->vdev->priv;
580 if (gfp & __GFP_WAIT) {
581 if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
583 buf = (char *)page_address(vi->alloc_frag.page) +
584 vi->alloc_frag.offset;
585 get_page(vi->alloc_frag.page);
586 vi->alloc_frag.offset += MERGE_BUFFER_LEN;
589 buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
594 sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
595 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
597 put_page(virt_to_head_page(buf));
603 * Returns false if we couldn't fill entirely (OOM).
605 * Normally run in the receive path, but can also be run from ndo_open
606 * before we're receiving packets, or from refill_work which is
607 * careful to disable receiving (using napi_disable).
609 static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
611 struct virtnet_info *vi = rq->vq->vdev->priv;
616 if (vi->mergeable_rx_bufs)
617 err = add_recvbuf_mergeable(rq, gfp);
618 else if (vi->big_packets)
619 err = add_recvbuf_big(rq, gfp);
621 err = add_recvbuf_small(rq, gfp);
623 oom = err == -ENOMEM;
626 } while (rq->vq->num_free);
627 if (unlikely(!virtqueue_kick(rq->vq)))
632 static void skb_recv_done(struct virtqueue *rvq)
634 struct virtnet_info *vi = rvq->vdev->priv;
635 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
637 /* Schedule NAPI, Suppress further interrupts if successful. */
638 if (napi_schedule_prep(&rq->napi)) {
639 virtqueue_disable_cb(rvq);
640 __napi_schedule(&rq->napi);
644 static void virtnet_napi_enable(struct receive_queue *rq)
646 napi_enable(&rq->napi);
648 /* If all buffers were filled by other side before we napi_enabled, we
649 * won't get another interrupt, so process any outstanding packets
650 * now. virtnet_poll wants re-enable the queue, so we disable here.
651 * We synchronize against interrupts via NAPI_STATE_SCHED */
652 if (napi_schedule_prep(&rq->napi)) {
653 virtqueue_disable_cb(rq->vq);
655 __napi_schedule(&rq->napi);
660 static void refill_work(struct work_struct *work)
662 struct virtnet_info *vi =
663 container_of(work, struct virtnet_info, refill.work);
667 for (i = 0; i < vi->curr_queue_pairs; i++) {
668 struct receive_queue *rq = &vi->rq[i];
670 napi_disable(&rq->napi);
671 still_empty = !try_fill_recv(rq, GFP_KERNEL);
672 virtnet_napi_enable(rq);
674 /* In theory, this can happen: if we don't get any buffers in
675 * we will *never* try to fill again.
678 schedule_delayed_work(&vi->refill, HZ/2);
682 static int virtnet_poll(struct napi_struct *napi, int budget)
684 struct receive_queue *rq =
685 container_of(napi, struct receive_queue, napi);
686 struct virtnet_info *vi = rq->vq->vdev->priv;
688 unsigned int r, len, received = 0;
691 while (received < budget &&
692 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
693 receive_buf(rq, buf, len);
697 if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
698 if (!try_fill_recv(rq, GFP_ATOMIC))
699 schedule_delayed_work(&vi->refill, 0);
702 /* Out of packets? */
703 if (received < budget) {
704 r = virtqueue_enable_cb_prepare(rq->vq);
706 if (unlikely(virtqueue_poll(rq->vq, r)) &&
707 napi_schedule_prep(napi)) {
708 virtqueue_disable_cb(rq->vq);
709 __napi_schedule(napi);
717 static int virtnet_open(struct net_device *dev)
719 struct virtnet_info *vi = netdev_priv(dev);
722 for (i = 0; i < vi->max_queue_pairs; i++) {
723 if (i < vi->curr_queue_pairs)
724 /* Make sure we have some buffers: if oom use wq. */
725 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
726 schedule_delayed_work(&vi->refill, 0);
727 virtnet_napi_enable(&vi->rq[i]);
733 static void free_old_xmit_skbs(struct send_queue *sq)
737 struct virtnet_info *vi = sq->vq->vdev->priv;
738 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
740 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
741 pr_debug("Sent skb %p\n", skb);
743 u64_stats_update_begin(&stats->tx_syncp);
744 stats->tx_bytes += skb->len;
746 u64_stats_update_end(&stats->tx_syncp);
748 dev_kfree_skb_any(skb);
752 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
754 struct skb_vnet_hdr *hdr;
755 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
756 struct virtnet_info *vi = sq->vq->vdev->priv;
761 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
762 if (vi->mergeable_rx_bufs)
763 hdr_len = sizeof hdr->mhdr;
765 hdr_len = sizeof hdr->hdr;
767 can_push = vi->any_header_sg &&
768 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
769 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
770 /* Even if we can, don't push here yet as this would skew
771 * csum_start offset below. */
773 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
775 hdr = skb_vnet_hdr(skb);
777 if (skb->ip_summed == CHECKSUM_PARTIAL) {
778 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
779 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
780 hdr->hdr.csum_offset = skb->csum_offset;
783 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
786 if (skb_is_gso(skb)) {
787 hdr->hdr.hdr_len = skb_headlen(skb);
788 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
789 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
790 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
791 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
792 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
793 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
794 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
797 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
798 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
800 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
801 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
804 if (vi->mergeable_rx_bufs)
805 hdr->mhdr.num_buffers = 0;
808 __skb_push(skb, hdr_len);
809 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
810 /* Pull header back to avoid skew in tx bytes calculations. */
811 __skb_pull(skb, hdr_len);
813 sg_set_buf(sq->sg, hdr, hdr_len);
814 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
816 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
819 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
821 struct virtnet_info *vi = netdev_priv(dev);
822 int qnum = skb_get_queue_mapping(skb);
823 struct send_queue *sq = &vi->sq[qnum];
826 /* Free up any pending old buffers before queueing new ones. */
827 free_old_xmit_skbs(sq);
829 /* Try to transmit */
830 err = xmit_skb(sq, skb);
832 /* This should not happen! */
833 if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
834 dev->stats.tx_fifo_errors++;
837 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
838 dev->stats.tx_dropped++;
843 /* Don't wait up for transmitted skbs to be freed. */
847 /* Apparently nice girls don't return TX_BUSY; stop the queue
848 * before it gets out of hand. Naturally, this wastes entries. */
849 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
850 netif_stop_subqueue(dev, qnum);
851 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
852 /* More just got used, free them then recheck. */
853 free_old_xmit_skbs(sq);
854 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
855 netif_start_subqueue(dev, qnum);
856 virtqueue_disable_cb(sq->vq);
865 * Send command via the control virtqueue and check status. Commands
866 * supported by the hypervisor, as indicated by feature bits, should
867 * never fail unless improperly formatted.
869 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
870 struct scatterlist *out)
872 struct scatterlist *sgs[4], hdr, stat;
873 struct virtio_net_ctrl_hdr ctrl;
874 virtio_net_ctrl_ack status = ~0;
875 unsigned out_num = 0, tmp;
877 /* Caller should know better */
878 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
883 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
884 sgs[out_num++] = &hdr;
887 sgs[out_num++] = out;
889 /* Add return status. */
890 sg_init_one(&stat, &status, sizeof(status));
891 sgs[out_num] = &stat;
893 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
894 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC) < 0);
896 if (unlikely(!virtqueue_kick(vi->cvq)))
897 return status == VIRTIO_NET_OK;
899 /* Spin for a response, the kick causes an ioport write, trapping
900 * into the hypervisor, so the request should be handled immediately.
902 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
903 !virtqueue_is_broken(vi->cvq))
906 return status == VIRTIO_NET_OK;
909 static int virtnet_set_mac_address(struct net_device *dev, void *p)
911 struct virtnet_info *vi = netdev_priv(dev);
912 struct virtio_device *vdev = vi->vdev;
914 struct sockaddr *addr = p;
915 struct scatterlist sg;
917 ret = eth_prepare_mac_addr_change(dev, p);
921 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
922 sg_init_one(&sg, addr->sa_data, dev->addr_len);
923 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
924 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
926 "Failed to set mac address by vq command.\n");
929 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
932 /* Naturally, this has an atomicity problem. */
933 for (i = 0; i < dev->addr_len; i++)
935 offsetof(struct virtio_net_config, mac) +
936 i, addr->sa_data[i]);
939 eth_commit_mac_addr_change(dev, p);
944 static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
945 struct rtnl_link_stats64 *tot)
947 struct virtnet_info *vi = netdev_priv(dev);
951 for_each_possible_cpu(cpu) {
952 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
953 u64 tpackets, tbytes, rpackets, rbytes;
956 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
957 tpackets = stats->tx_packets;
958 tbytes = stats->tx_bytes;
959 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
962 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
963 rpackets = stats->rx_packets;
964 rbytes = stats->rx_bytes;
965 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
967 tot->rx_packets += rpackets;
968 tot->tx_packets += tpackets;
969 tot->rx_bytes += rbytes;
970 tot->tx_bytes += tbytes;
973 tot->tx_dropped = dev->stats.tx_dropped;
974 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
975 tot->rx_dropped = dev->stats.rx_dropped;
976 tot->rx_length_errors = dev->stats.rx_length_errors;
977 tot->rx_frame_errors = dev->stats.rx_frame_errors;
982 #ifdef CONFIG_NET_POLL_CONTROLLER
983 static void virtnet_netpoll(struct net_device *dev)
985 struct virtnet_info *vi = netdev_priv(dev);
988 for (i = 0; i < vi->curr_queue_pairs; i++)
989 napi_schedule(&vi->rq[i].napi);
993 static void virtnet_ack_link_announce(struct virtnet_info *vi)
996 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
997 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
998 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1002 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1004 struct scatterlist sg;
1005 struct virtio_net_ctrl_mq s;
1006 struct net_device *dev = vi->dev;
1008 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1011 s.virtqueue_pairs = queue_pairs;
1012 sg_init_one(&sg, &s, sizeof(s));
1014 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1015 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1016 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1020 vi->curr_queue_pairs = queue_pairs;
1021 /* virtnet_open() will refill when device is going to up. */
1022 if (dev->flags & IFF_UP)
1023 schedule_delayed_work(&vi->refill, 0);
1029 static int virtnet_close(struct net_device *dev)
1031 struct virtnet_info *vi = netdev_priv(dev);
1034 /* Make sure refill_work doesn't re-enable napi! */
1035 cancel_delayed_work_sync(&vi->refill);
1037 for (i = 0; i < vi->max_queue_pairs; i++)
1038 napi_disable(&vi->rq[i].napi);
1043 static void virtnet_set_rx_mode(struct net_device *dev)
1045 struct virtnet_info *vi = netdev_priv(dev);
1046 struct scatterlist sg[2];
1047 u8 promisc, allmulti;
1048 struct virtio_net_ctrl_mac *mac_data;
1049 struct netdev_hw_addr *ha;
1055 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
1056 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1059 promisc = ((dev->flags & IFF_PROMISC) != 0);
1060 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1062 sg_init_one(sg, &promisc, sizeof(promisc));
1064 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1065 VIRTIO_NET_CTRL_RX_PROMISC, sg))
1066 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1067 promisc ? "en" : "dis");
1069 sg_init_one(sg, &allmulti, sizeof(allmulti));
1071 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1072 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
1073 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1074 allmulti ? "en" : "dis");
1076 uc_count = netdev_uc_count(dev);
1077 mc_count = netdev_mc_count(dev);
1078 /* MAC filter - use one buffer for both lists */
1079 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1080 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1085 sg_init_table(sg, 2);
1087 /* Store the unicast list and count in the front of the buffer */
1088 mac_data->entries = uc_count;
1090 netdev_for_each_uc_addr(ha, dev)
1091 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1093 sg_set_buf(&sg[0], mac_data,
1094 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1096 /* multicast list and count fill the end */
1097 mac_data = (void *)&mac_data->macs[uc_count][0];
1099 mac_data->entries = mc_count;
1101 netdev_for_each_mc_addr(ha, dev)
1102 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1104 sg_set_buf(&sg[1], mac_data,
1105 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1107 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1108 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
1109 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1114 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1115 __be16 proto, u16 vid)
1117 struct virtnet_info *vi = netdev_priv(dev);
1118 struct scatterlist sg;
1120 sg_init_one(&sg, &vid, sizeof(vid));
1122 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1123 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
1124 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
1128 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1129 __be16 proto, u16 vid)
1131 struct virtnet_info *vi = netdev_priv(dev);
1132 struct scatterlist sg;
1134 sg_init_one(&sg, &vid, sizeof(vid));
1136 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1137 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
1138 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
1142 static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1146 if (vi->affinity_hint_set) {
1147 for (i = 0; i < vi->max_queue_pairs; i++) {
1148 virtqueue_set_affinity(vi->rq[i].vq, -1);
1149 virtqueue_set_affinity(vi->sq[i].vq, -1);
1152 vi->affinity_hint_set = false;
1156 static void virtnet_set_affinity(struct virtnet_info *vi)
1161 /* In multiqueue mode, when the number of cpu is equal to the number of
1162 * queue pairs, we let the queue pairs to be private to one cpu by
1163 * setting the affinity hint to eliminate the contention.
1165 if (vi->curr_queue_pairs == 1 ||
1166 vi->max_queue_pairs != num_online_cpus()) {
1167 virtnet_clean_affinity(vi, -1);
1172 for_each_online_cpu(cpu) {
1173 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1174 virtqueue_set_affinity(vi->sq[i].vq, cpu);
1175 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
1179 vi->affinity_hint_set = true;
1182 static int virtnet_cpu_callback(struct notifier_block *nfb,
1183 unsigned long action, void *hcpu)
1185 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1187 switch(action & ~CPU_TASKS_FROZEN) {
1189 case CPU_DOWN_FAILED:
1191 virtnet_set_affinity(vi);
1193 case CPU_DOWN_PREPARE:
1194 virtnet_clean_affinity(vi, (long)hcpu);
1203 static void virtnet_get_ringparam(struct net_device *dev,
1204 struct ethtool_ringparam *ring)
1206 struct virtnet_info *vi = netdev_priv(dev);
1208 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1209 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
1210 ring->rx_pending = ring->rx_max_pending;
1211 ring->tx_pending = ring->tx_max_pending;
1215 static void virtnet_get_drvinfo(struct net_device *dev,
1216 struct ethtool_drvinfo *info)
1218 struct virtnet_info *vi = netdev_priv(dev);
1219 struct virtio_device *vdev = vi->vdev;
1221 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1222 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1223 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1227 /* TODO: Eliminate OOO packets during switching */
1228 static int virtnet_set_channels(struct net_device *dev,
1229 struct ethtool_channels *channels)
1231 struct virtnet_info *vi = netdev_priv(dev);
1232 u16 queue_pairs = channels->combined_count;
1235 /* We don't support separate rx/tx channels.
1236 * We don't allow setting 'other' channels.
1238 if (channels->rx_count || channels->tx_count || channels->other_count)
1241 if (queue_pairs > vi->max_queue_pairs)
1245 err = virtnet_set_queues(vi, queue_pairs);
1247 netif_set_real_num_tx_queues(dev, queue_pairs);
1248 netif_set_real_num_rx_queues(dev, queue_pairs);
1250 virtnet_set_affinity(vi);
1257 static void virtnet_get_channels(struct net_device *dev,
1258 struct ethtool_channels *channels)
1260 struct virtnet_info *vi = netdev_priv(dev);
1262 channels->combined_count = vi->curr_queue_pairs;
1263 channels->max_combined = vi->max_queue_pairs;
1264 channels->max_other = 0;
1265 channels->rx_count = 0;
1266 channels->tx_count = 0;
1267 channels->other_count = 0;
1270 static const struct ethtool_ops virtnet_ethtool_ops = {
1271 .get_drvinfo = virtnet_get_drvinfo,
1272 .get_link = ethtool_op_get_link,
1273 .get_ringparam = virtnet_get_ringparam,
1274 .set_channels = virtnet_set_channels,
1275 .get_channels = virtnet_get_channels,
1279 #define MAX_MTU 65535
1281 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1283 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1289 static const struct net_device_ops virtnet_netdev = {
1290 .ndo_open = virtnet_open,
1291 .ndo_stop = virtnet_close,
1292 .ndo_start_xmit = start_xmit,
1293 .ndo_validate_addr = eth_validate_addr,
1294 .ndo_set_mac_address = virtnet_set_mac_address,
1295 .ndo_set_rx_mode = virtnet_set_rx_mode,
1296 .ndo_change_mtu = virtnet_change_mtu,
1297 .ndo_get_stats64 = virtnet_stats,
1298 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1299 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1300 #ifdef CONFIG_NET_POLL_CONTROLLER
1301 .ndo_poll_controller = virtnet_netpoll,
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);
1311 mutex_lock(&vi->config_lock);
1312 if (!vi->config_enable)
1315 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1316 struct virtio_net_config, status, &v) < 0)
1319 if (v & VIRTIO_NET_S_ANNOUNCE) {
1320 netdev_notify_peers(vi->dev);
1321 virtnet_ack_link_announce(vi);
1324 /* Ignore unknown (future) status bits */
1325 v &= VIRTIO_NET_S_LINK_UP;
1327 if (vi->status == v)
1332 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1333 netif_carrier_on(vi->dev);
1334 netif_tx_wake_all_queues(vi->dev);
1336 netif_carrier_off(vi->dev);
1337 netif_tx_stop_all_queues(vi->dev);
1340 mutex_unlock(&vi->config_lock);
1343 static void virtnet_config_changed(struct virtio_device *vdev)
1345 struct virtnet_info *vi = vdev->priv;
1347 schedule_work(&vi->config_work);
1350 static void virtnet_free_queues(struct virtnet_info *vi)
1354 for (i = 0; i < vi->max_queue_pairs; i++)
1355 netif_napi_del(&vi->rq[i].napi);
1361 static void free_receive_bufs(struct virtnet_info *vi)
1365 for (i = 0; i < vi->max_queue_pairs; i++) {
1366 while (vi->rq[i].pages)
1367 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1371 static void free_unused_bufs(struct virtnet_info *vi)
1376 for (i = 0; i < vi->max_queue_pairs; i++) {
1377 struct virtqueue *vq = vi->sq[i].vq;
1378 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1382 for (i = 0; i < vi->max_queue_pairs; i++) {
1383 struct virtqueue *vq = vi->rq[i].vq;
1385 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1386 if (vi->mergeable_rx_bufs)
1387 put_page(virt_to_head_page(buf));
1388 else if (vi->big_packets)
1389 give_pages(&vi->rq[i], buf);
1396 static void virtnet_del_vqs(struct virtnet_info *vi)
1398 struct virtio_device *vdev = vi->vdev;
1400 virtnet_clean_affinity(vi, -1);
1402 vdev->config->del_vqs(vdev);
1404 virtnet_free_queues(vi);
1407 static int virtnet_find_vqs(struct virtnet_info *vi)
1409 vq_callback_t **callbacks;
1410 struct virtqueue **vqs;
1415 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1416 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1417 * possible control vq.
1419 total_vqs = vi->max_queue_pairs * 2 +
1420 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1422 /* Allocate space for find_vqs parameters */
1423 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1426 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1429 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1433 /* Parameters for control virtqueue, if any */
1435 callbacks[total_vqs - 1] = NULL;
1436 names[total_vqs - 1] = "control";
1439 /* Allocate/initialize parameters for send/receive virtqueues */
1440 for (i = 0; i < vi->max_queue_pairs; i++) {
1441 callbacks[rxq2vq(i)] = skb_recv_done;
1442 callbacks[txq2vq(i)] = skb_xmit_done;
1443 sprintf(vi->rq[i].name, "input.%d", i);
1444 sprintf(vi->sq[i].name, "output.%d", i);
1445 names[rxq2vq(i)] = vi->rq[i].name;
1446 names[txq2vq(i)] = vi->sq[i].name;
1449 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1455 vi->cvq = vqs[total_vqs - 1];
1456 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1457 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1460 for (i = 0; i < vi->max_queue_pairs; i++) {
1461 vi->rq[i].vq = vqs[rxq2vq(i)];
1462 vi->sq[i].vq = vqs[txq2vq(i)];
1481 static int virtnet_alloc_queues(struct virtnet_info *vi)
1485 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1488 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1492 INIT_DELAYED_WORK(&vi->refill, refill_work);
1493 for (i = 0; i < vi->max_queue_pairs; i++) {
1494 vi->rq[i].pages = NULL;
1495 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1498 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1499 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1510 static int init_vqs(struct virtnet_info *vi)
1514 /* Allocate send & receive queues */
1515 ret = virtnet_alloc_queues(vi);
1519 ret = virtnet_find_vqs(vi);
1524 virtnet_set_affinity(vi);
1530 virtnet_free_queues(vi);
1535 static int virtnet_probe(struct virtio_device *vdev)
1538 struct net_device *dev;
1539 struct virtnet_info *vi;
1540 u16 max_queue_pairs;
1542 /* Find if host supports multiqueue virtio_net device */
1543 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1544 struct virtio_net_config,
1545 max_virtqueue_pairs, &max_queue_pairs);
1547 /* We need at least 2 queue's */
1548 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1549 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1550 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1551 max_queue_pairs = 1;
1553 /* Allocate ourselves a network device with room for our info */
1554 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1558 /* Set up network device as normal. */
1559 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
1560 dev->netdev_ops = &virtnet_netdev;
1561 dev->features = NETIF_F_HIGHDMA;
1563 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1564 SET_NETDEV_DEV(dev, &vdev->dev);
1566 /* Do we support "hardware" checksums? */
1567 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1568 /* This opens up the world of extra features. */
1569 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1571 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1573 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1574 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
1575 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1577 /* Individual feature bits: what can host handle? */
1578 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1579 dev->hw_features |= NETIF_F_TSO;
1580 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1581 dev->hw_features |= NETIF_F_TSO6;
1582 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1583 dev->hw_features |= NETIF_F_TSO_ECN;
1584 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1585 dev->hw_features |= NETIF_F_UFO;
1588 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1589 /* (!csum && gso) case will be fixed by register_netdev() */
1591 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1592 dev->features |= NETIF_F_RXCSUM;
1594 dev->vlan_features = dev->features;
1596 /* Configuration may specify what MAC to use. Otherwise random. */
1597 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1598 virtio_cread_bytes(vdev,
1599 offsetof(struct virtio_net_config, mac),
1600 dev->dev_addr, dev->addr_len);
1602 eth_hw_addr_random(dev);
1604 /* Set up our device-specific information */
1605 vi = netdev_priv(dev);
1609 vi->stats = alloc_percpu(struct virtnet_stats);
1611 if (vi->stats == NULL)
1614 for_each_possible_cpu(i) {
1615 struct virtnet_stats *virtnet_stats;
1616 virtnet_stats = per_cpu_ptr(vi->stats, i);
1617 u64_stats_init(&virtnet_stats->tx_syncp);
1618 u64_stats_init(&virtnet_stats->rx_syncp);
1621 mutex_init(&vi->config_lock);
1622 vi->config_enable = true;
1623 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1625 /* If we can receive ANY GSO packets, we must allocate large ones. */
1626 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1627 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1628 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
1629 vi->big_packets = true;
1631 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1632 vi->mergeable_rx_bufs = true;
1634 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1635 vi->any_header_sg = true;
1637 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1640 /* Use single tx/rx queue pair as default */
1641 vi->curr_queue_pairs = 1;
1642 vi->max_queue_pairs = max_queue_pairs;
1644 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
1649 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1650 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
1652 err = register_netdev(dev);
1654 pr_debug("virtio_net: registering device failed\n");
1658 /* Last of all, set up some receive buffers. */
1659 for (i = 0; i < vi->curr_queue_pairs; i++) {
1660 try_fill_recv(&vi->rq[i], GFP_KERNEL);
1662 /* If we didn't even get one input buffer, we're useless. */
1663 if (vi->rq[i].vq->num_free ==
1664 virtqueue_get_vring_size(vi->rq[i].vq)) {
1665 free_unused_bufs(vi);
1667 goto free_recv_bufs;
1671 vi->nb.notifier_call = &virtnet_cpu_callback;
1672 err = register_hotcpu_notifier(&vi->nb);
1674 pr_debug("virtio_net: registering cpu notifier failed\n");
1675 goto free_recv_bufs;
1678 /* Assume link up if device can't report link status,
1679 otherwise get link status from config. */
1680 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1681 netif_carrier_off(dev);
1682 schedule_work(&vi->config_work);
1684 vi->status = VIRTIO_NET_S_LINK_UP;
1685 netif_carrier_on(dev);
1688 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1689 dev->name, max_queue_pairs);
1694 free_receive_bufs(vi);
1695 unregister_netdev(dev);
1697 cancel_delayed_work_sync(&vi->refill);
1698 virtnet_del_vqs(vi);
1699 if (vi->alloc_frag.page)
1700 put_page(vi->alloc_frag.page);
1702 free_percpu(vi->stats);
1708 static void remove_vq_common(struct virtnet_info *vi)
1710 vi->vdev->config->reset(vi->vdev);
1712 /* Free unused buffers in both send and recv, if any. */
1713 free_unused_bufs(vi);
1715 free_receive_bufs(vi);
1717 virtnet_del_vqs(vi);
1720 static void virtnet_remove(struct virtio_device *vdev)
1722 struct virtnet_info *vi = vdev->priv;
1724 unregister_hotcpu_notifier(&vi->nb);
1726 /* Prevent config work handler from accessing the device. */
1727 mutex_lock(&vi->config_lock);
1728 vi->config_enable = false;
1729 mutex_unlock(&vi->config_lock);
1731 unregister_netdev(vi->dev);
1733 remove_vq_common(vi);
1734 if (vi->alloc_frag.page)
1735 put_page(vi->alloc_frag.page);
1737 flush_work(&vi->config_work);
1739 free_percpu(vi->stats);
1740 free_netdev(vi->dev);
1743 #ifdef CONFIG_PM_SLEEP
1744 static int virtnet_freeze(struct virtio_device *vdev)
1746 struct virtnet_info *vi = vdev->priv;
1749 unregister_hotcpu_notifier(&vi->nb);
1751 /* Prevent config work handler from accessing the device */
1752 mutex_lock(&vi->config_lock);
1753 vi->config_enable = false;
1754 mutex_unlock(&vi->config_lock);
1756 netif_device_detach(vi->dev);
1757 cancel_delayed_work_sync(&vi->refill);
1759 if (netif_running(vi->dev))
1760 for (i = 0; i < vi->max_queue_pairs; i++) {
1761 napi_disable(&vi->rq[i].napi);
1762 netif_napi_del(&vi->rq[i].napi);
1765 remove_vq_common(vi);
1767 flush_work(&vi->config_work);
1772 static int virtnet_restore(struct virtio_device *vdev)
1774 struct virtnet_info *vi = vdev->priv;
1781 if (netif_running(vi->dev)) {
1782 for (i = 0; i < vi->curr_queue_pairs; i++)
1783 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1784 schedule_delayed_work(&vi->refill, 0);
1786 for (i = 0; i < vi->max_queue_pairs; i++)
1787 virtnet_napi_enable(&vi->rq[i]);
1790 netif_device_attach(vi->dev);
1792 mutex_lock(&vi->config_lock);
1793 vi->config_enable = true;
1794 mutex_unlock(&vi->config_lock);
1797 virtnet_set_queues(vi, vi->curr_queue_pairs);
1800 err = register_hotcpu_notifier(&vi->nb);
1808 static struct virtio_device_id id_table[] = {
1809 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1813 static unsigned int features[] = {
1814 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1815 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1816 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
1817 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
1818 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
1819 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
1820 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1821 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
1822 VIRTIO_NET_F_CTRL_MAC_ADDR,
1823 VIRTIO_F_ANY_LAYOUT,
1826 static struct virtio_driver virtio_net_driver = {
1827 .feature_table = features,
1828 .feature_table_size = ARRAY_SIZE(features),
1829 .driver.name = KBUILD_MODNAME,
1830 .driver.owner = THIS_MODULE,
1831 .id_table = id_table,
1832 .probe = virtnet_probe,
1833 .remove = virtnet_remove,
1834 .config_changed = virtnet_config_changed,
1835 #ifdef CONFIG_PM_SLEEP
1836 .freeze = virtnet_freeze,
1837 .restore = virtnet_restore,
1841 module_virtio_driver(virtio_net_driver);
1843 MODULE_DEVICE_TABLE(virtio, id_table);
1844 MODULE_DESCRIPTION("Virtio network driver");
1845 MODULE_LICENSE("GPL");