xref: /openbmc/linux/drivers/net/virtio_net.c (revision 3ab098df35f8b98b6553edc2e40234af512ba877)
148925e37SRusty Russell /* A network driver using virtio.
2296f96fcSRusty Russell  *
3296f96fcSRusty Russell  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4296f96fcSRusty Russell  *
5296f96fcSRusty Russell  * This program is free software; you can redistribute it and/or modify
6296f96fcSRusty Russell  * it under the terms of the GNU General Public License as published by
7296f96fcSRusty Russell  * the Free Software Foundation; either version 2 of the License, or
8296f96fcSRusty Russell  * (at your option) any later version.
9296f96fcSRusty Russell  *
10296f96fcSRusty Russell  * This program is distributed in the hope that it will be useful,
11296f96fcSRusty Russell  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12296f96fcSRusty Russell  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13296f96fcSRusty Russell  * GNU General Public License for more details.
14296f96fcSRusty Russell  *
15296f96fcSRusty Russell  * You should have received a copy of the GNU General Public License
16296f96fcSRusty Russell  * along with this program; if not, write to the Free Software
17296f96fcSRusty Russell  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18296f96fcSRusty Russell  */
19296f96fcSRusty Russell //#define DEBUG
20296f96fcSRusty Russell #include <linux/netdevice.h>
21296f96fcSRusty Russell #include <linux/etherdevice.h>
22a9ea3fc6SHerbert Xu #include <linux/ethtool.h>
23296f96fcSRusty Russell #include <linux/module.h>
24296f96fcSRusty Russell #include <linux/virtio.h>
25296f96fcSRusty Russell #include <linux/virtio_net.h>
26296f96fcSRusty Russell #include <linux/scatterlist.h>
27e918085aSAlex Williamson #include <linux/if_vlan.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
298de4b2f3SWanlong Gao #include <linux/cpu.h>
30296f96fcSRusty Russell 
31d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
326c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
336c0cd7c0SDor Laor 
34eb939922SRusty Russell static bool csum = true, gso = true;
3534a48579SRusty Russell module_param(csum, bool, 0444);
3634a48579SRusty Russell module_param(gso, bool, 0444);
3734a48579SRusty Russell 
38296f96fcSRusty Russell /* FIXME: MTU in config. */
39e918085aSAlex Williamson #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
403f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
41296f96fcSRusty Russell 
4266846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
432a41f71dSAlex Williamson 
443fa2a1dfSstephen hemminger struct virtnet_stats {
4583a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
4683a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
473fa2a1dfSstephen hemminger 	u64 tx_bytes;
483fa2a1dfSstephen hemminger 	u64 tx_packets;
493fa2a1dfSstephen hemminger 
503fa2a1dfSstephen hemminger 	u64 rx_bytes;
513fa2a1dfSstephen hemminger 	u64 rx_packets;
523fa2a1dfSstephen hemminger };
533fa2a1dfSstephen hemminger 
54e9d7417bSJason Wang /* Internal representation of a send virtqueue */
55e9d7417bSJason Wang struct send_queue {
56e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
57e9d7417bSJason Wang 	struct virtqueue *vq;
58e9d7417bSJason Wang 
59e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
60e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
61986a4f4dSJason Wang 
62986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
63986a4f4dSJason Wang 	char name[40];
64e9d7417bSJason Wang };
65e9d7417bSJason Wang 
66e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
67e9d7417bSJason Wang struct receive_queue {
68e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
69e9d7417bSJason Wang 	struct virtqueue *vq;
70e9d7417bSJason Wang 
71296f96fcSRusty Russell 	struct napi_struct napi;
72296f96fcSRusty Russell 
73296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
74296f96fcSRusty Russell 	unsigned int num, max;
75296f96fcSRusty Russell 
76e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
77e9d7417bSJason Wang 	struct page *pages;
78e9d7417bSJason Wang 
79e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
80e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
81986a4f4dSJason Wang 
82986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
83986a4f4dSJason Wang 	char name[40];
84e9d7417bSJason Wang };
85e9d7417bSJason Wang 
86e9d7417bSJason Wang struct virtnet_info {
87e9d7417bSJason Wang 	struct virtio_device *vdev;
88e9d7417bSJason Wang 	struct virtqueue *cvq;
89e9d7417bSJason Wang 	struct net_device *dev;
90986a4f4dSJason Wang 	struct send_queue *sq;
91986a4f4dSJason Wang 	struct receive_queue *rq;
92e9d7417bSJason Wang 	unsigned int status;
93e9d7417bSJason Wang 
94986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
95986a4f4dSJason Wang 	u16 max_queue_pairs;
96986a4f4dSJason Wang 
97986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
98986a4f4dSJason Wang 	u16 curr_queue_pairs;
99986a4f4dSJason Wang 
10097402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
10197402b96SHerbert Xu 	bool big_packets;
10297402b96SHerbert Xu 
1033f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1043f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1053f2c31d9SMark McLoughlin 
106986a4f4dSJason Wang 	/* Has control virtqueue */
107986a4f4dSJason Wang 	bool has_cvq;
108986a4f4dSJason Wang 
109e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
110e7428e95SMichael S. Tsirkin 	bool any_header_sg;
111e7428e95SMichael S. Tsirkin 
112586d17c5SJason Wang 	/* enable config space updates */
113586d17c5SJason Wang 	bool config_enable;
114586d17c5SJason Wang 
1153fa2a1dfSstephen hemminger 	/* Active statistics */
1163fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1173fa2a1dfSstephen hemminger 
1183161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1193161e453SRusty Russell 	struct delayed_work refill;
1203161e453SRusty Russell 
121586d17c5SJason Wang 	/* Work struct for config space updates */
122586d17c5SJason Wang 	struct work_struct config_work;
123586d17c5SJason Wang 
124586d17c5SJason Wang 	/* Lock for config space updates */
125586d17c5SJason Wang 	struct mutex config_lock;
126986a4f4dSJason Wang 
127986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
128986a4f4dSJason Wang 	bool affinity_hint_set;
12947be2479SWanlong Gao 
13047be2479SWanlong Gao 	/* Per-cpu variable to show the mapping from CPU to virtqueue */
13147be2479SWanlong Gao 	int __percpu *vq_index;
1328de4b2f3SWanlong Gao 
1338de4b2f3SWanlong Gao 	/* CPU hot plug notifier */
1348de4b2f3SWanlong Gao 	struct notifier_block nb;
135296f96fcSRusty Russell };
136296f96fcSRusty Russell 
137b3f24698SRusty Russell struct skb_vnet_hdr {
138b3f24698SRusty Russell 	union {
139b3f24698SRusty Russell 		struct virtio_net_hdr hdr;
140b3f24698SRusty Russell 		struct virtio_net_hdr_mrg_rxbuf mhdr;
141b3f24698SRusty Russell 	};
142b3f24698SRusty Russell };
143b3f24698SRusty Russell 
1449ab86bbcSShirley Ma struct padded_vnet_hdr {
1459ab86bbcSShirley Ma 	struct virtio_net_hdr hdr;
1469ab86bbcSShirley Ma 	/*
1479ab86bbcSShirley Ma 	 * virtio_net_hdr should be in a separated sg buffer because of a
1489ab86bbcSShirley Ma 	 * QEMU bug, and data sg buffer shares same page with this header sg.
1499ab86bbcSShirley Ma 	 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
1509ab86bbcSShirley Ma 	 */
1519ab86bbcSShirley Ma 	char padding[6];
1529ab86bbcSShirley Ma };
1539ab86bbcSShirley Ma 
154986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
155986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
156986a4f4dSJason Wang  */
157986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
158986a4f4dSJason Wang {
1599d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
160986a4f4dSJason Wang }
161986a4f4dSJason Wang 
162986a4f4dSJason Wang static int txq2vq(int txq)
163986a4f4dSJason Wang {
164986a4f4dSJason Wang 	return txq * 2 + 1;
165986a4f4dSJason Wang }
166986a4f4dSJason Wang 
167986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
168986a4f4dSJason Wang {
1699d0ca6edSRusty Russell 	return vq->index / 2;
170986a4f4dSJason Wang }
171986a4f4dSJason Wang 
172986a4f4dSJason Wang static int rxq2vq(int rxq)
173986a4f4dSJason Wang {
174986a4f4dSJason Wang 	return rxq * 2;
175986a4f4dSJason Wang }
176986a4f4dSJason Wang 
177b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
178296f96fcSRusty Russell {
179b3f24698SRusty Russell 	return (struct skb_vnet_hdr *)skb->cb;
180296f96fcSRusty Russell }
181296f96fcSRusty Russell 
1829ab86bbcSShirley Ma /*
1839ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
1849ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
1859ab86bbcSShirley Ma  */
186e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
187fb6813f4SRusty Russell {
1889ab86bbcSShirley Ma 	struct page *end;
1899ab86bbcSShirley Ma 
190e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
1919ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
192e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
193e9d7417bSJason Wang 	rq->pages = page;
194fb6813f4SRusty Russell }
195fb6813f4SRusty Russell 
196e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
197fb6813f4SRusty Russell {
198e9d7417bSJason Wang 	struct page *p = rq->pages;
199fb6813f4SRusty Russell 
2009ab86bbcSShirley Ma 	if (p) {
201e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2029ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2039ab86bbcSShirley Ma 		p->private = 0;
2049ab86bbcSShirley Ma 	} else
205fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
206fb6813f4SRusty Russell 	return p;
207fb6813f4SRusty Russell }
208fb6813f4SRusty Russell 
209e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
210296f96fcSRusty Russell {
211e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
212296f96fcSRusty Russell 
2132cb9c6baSRusty Russell 	/* Suppress further interrupts. */
214e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
21511a3a154SRusty Russell 
216363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
217986a4f4dSJason Wang 	netif_wake_subqueue(vi->dev, vq2txq(vq));
218296f96fcSRusty Russell }
219296f96fcSRusty Russell 
2209ab86bbcSShirley Ma static void set_skb_frag(struct sk_buff *skb, struct page *page,
2219ab86bbcSShirley Ma 			 unsigned int offset, unsigned int *len)
222296f96fcSRusty Russell {
2238a59a7b9SKrishna Kumar 	int size = min((unsigned)PAGE_SIZE - offset, *len);
2249ab86bbcSShirley Ma 	int i = skb_shinfo(skb)->nr_frags;
225296f96fcSRusty Russell 
2268a59a7b9SKrishna Kumar 	__skb_fill_page_desc(skb, i, page, offset, size);
2279ab86bbcSShirley Ma 
2288a59a7b9SKrishna Kumar 	skb->data_len += size;
2298a59a7b9SKrishna Kumar 	skb->len += size;
2304b727361SEric Dumazet 	skb->truesize += PAGE_SIZE;
2319ab86bbcSShirley Ma 	skb_shinfo(skb)->nr_frags++;
232c9af6db4SPravin B Shelar 	skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
2338a59a7b9SKrishna Kumar 	*len -= size;
234296f96fcSRusty Russell }
2353f2c31d9SMark McLoughlin 
2363464645aSMike Waychison /* Called from bottom half context */
237e9d7417bSJason Wang static struct sk_buff *page_to_skb(struct receive_queue *rq,
2389ab86bbcSShirley Ma 				   struct page *page, unsigned int len)
2399ab86bbcSShirley Ma {
240e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
2419ab86bbcSShirley Ma 	struct sk_buff *skb;
2429ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
2439ab86bbcSShirley Ma 	unsigned int copy, hdr_len, offset;
2449ab86bbcSShirley Ma 	char *p;
2459ab86bbcSShirley Ma 
2469ab86bbcSShirley Ma 	p = page_address(page);
2479ab86bbcSShirley Ma 
2489ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
2499ab86bbcSShirley Ma 	skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
2509ab86bbcSShirley Ma 	if (unlikely(!skb))
2519ab86bbcSShirley Ma 		return NULL;
2529ab86bbcSShirley Ma 
2539ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
2549ab86bbcSShirley Ma 
2553f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs) {
2569ab86bbcSShirley Ma 		hdr_len = sizeof hdr->mhdr;
2579ab86bbcSShirley Ma 		offset = hdr_len;
2589ab86bbcSShirley Ma 	} else {
2599ab86bbcSShirley Ma 		hdr_len = sizeof hdr->hdr;
2609ab86bbcSShirley Ma 		offset = sizeof(struct padded_vnet_hdr);
2619ab86bbcSShirley Ma 	}
2623f2c31d9SMark McLoughlin 
2639ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
2643f2c31d9SMark McLoughlin 
2659ab86bbcSShirley Ma 	len -= hdr_len;
2669ab86bbcSShirley Ma 	p += offset;
2673f2c31d9SMark McLoughlin 
2683f2c31d9SMark McLoughlin 	copy = len;
2693f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
2703f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
2713f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
2723f2c31d9SMark McLoughlin 
2733f2c31d9SMark McLoughlin 	len -= copy;
2749ab86bbcSShirley Ma 	offset += copy;
2753f2c31d9SMark McLoughlin 
276e878d78bSSasha Levin 	/*
277e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
278e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
279e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
280e878d78bSSasha Levin 	 * the case of a broken device.
281e878d78bSSasha Levin 	 */
282e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
283be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
284e878d78bSSasha Levin 		dev_kfree_skb(skb);
285e878d78bSSasha Levin 		return NULL;
286e878d78bSSasha Levin 	}
287e878d78bSSasha Levin 
2889ab86bbcSShirley Ma 	while (len) {
2899ab86bbcSShirley Ma 		set_skb_frag(skb, page, offset, &len);
2909ab86bbcSShirley Ma 		page = (struct page *)page->private;
2919ab86bbcSShirley Ma 		offset = 0;
2923f2c31d9SMark McLoughlin 	}
2933f2c31d9SMark McLoughlin 
2949ab86bbcSShirley Ma 	if (page)
295e9d7417bSJason Wang 		give_pages(rq, page);
2963f2c31d9SMark McLoughlin 
2979ab86bbcSShirley Ma 	return skb;
2989ab86bbcSShirley Ma }
2999ab86bbcSShirley Ma 
300e9d7417bSJason Wang static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb)
3019ab86bbcSShirley Ma {
3029ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
3039ab86bbcSShirley Ma 	struct page *page;
3049ab86bbcSShirley Ma 	int num_buf, i, len;
3059ab86bbcSShirley Ma 
3069ab86bbcSShirley Ma 	num_buf = hdr->mhdr.num_buffers;
3079ab86bbcSShirley Ma 	while (--num_buf) {
3083f2c31d9SMark McLoughlin 		i = skb_shinfo(skb)->nr_frags;
3093f2c31d9SMark McLoughlin 		if (i >= MAX_SKB_FRAGS) {
3109ab86bbcSShirley Ma 			pr_debug("%s: packet too long\n", skb->dev->name);
3119ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
3129ab86bbcSShirley Ma 			return -EINVAL;
3133f2c31d9SMark McLoughlin 		}
314e9d7417bSJason Wang 		page = virtqueue_get_buf(rq->vq, &len);
3159ab86bbcSShirley Ma 		if (!page) {
3163f2c31d9SMark McLoughlin 			pr_debug("%s: rx error: %d buffers missing\n",
3179ab86bbcSShirley Ma 				 skb->dev->name, hdr->mhdr.num_buffers);
3189ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
3199ab86bbcSShirley Ma 			return -EINVAL;
3203f2c31d9SMark McLoughlin 		}
3213fa2a1dfSstephen hemminger 
3223f2c31d9SMark McLoughlin 		if (len > PAGE_SIZE)
3233f2c31d9SMark McLoughlin 			len = PAGE_SIZE;
3243f2c31d9SMark McLoughlin 
3259ab86bbcSShirley Ma 		set_skb_frag(skb, page, 0, &len);
3269ab86bbcSShirley Ma 
327e9d7417bSJason Wang 		--rq->num;
3283f2c31d9SMark McLoughlin 	}
3299ab86bbcSShirley Ma 	return 0;
3309ab86bbcSShirley Ma }
3319ab86bbcSShirley Ma 
332e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
3339ab86bbcSShirley Ma {
334e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
335e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
33658472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
3379ab86bbcSShirley Ma 	struct sk_buff *skb;
3389ab86bbcSShirley Ma 	struct page *page;
3399ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
3409ab86bbcSShirley Ma 
3419ab86bbcSShirley Ma 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
3429ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
3439ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
3449ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs || vi->big_packets)
345e9d7417bSJason Wang 			give_pages(rq, buf);
3469ab86bbcSShirley Ma 		else
3479ab86bbcSShirley Ma 			dev_kfree_skb(buf);
3489ab86bbcSShirley Ma 		return;
3499ab86bbcSShirley Ma 	}
3509ab86bbcSShirley Ma 
3519ab86bbcSShirley Ma 	if (!vi->mergeable_rx_bufs && !vi->big_packets) {
3529ab86bbcSShirley Ma 		skb = buf;
3539ab86bbcSShirley Ma 		len -= sizeof(struct virtio_net_hdr);
3549ab86bbcSShirley Ma 		skb_trim(skb, len);
3553f2c31d9SMark McLoughlin 	} else {
3569ab86bbcSShirley Ma 		page = buf;
357e9d7417bSJason Wang 		skb = page_to_skb(rq, page, len);
3589ab86bbcSShirley Ma 		if (unlikely(!skb)) {
35997402b96SHerbert Xu 			dev->stats.rx_dropped++;
360e9d7417bSJason Wang 			give_pages(rq, page);
3619ab86bbcSShirley Ma 			return;
3629ab86bbcSShirley Ma 		}
3639ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
364e9d7417bSJason Wang 			if (receive_mergeable(rq, skb)) {
3659ab86bbcSShirley Ma 				dev_kfree_skb(skb);
3669ab86bbcSShirley Ma 				return;
36797402b96SHerbert Xu 			}
3683f2c31d9SMark McLoughlin 	}
3693f2c31d9SMark McLoughlin 
3709ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
3713fa2a1dfSstephen hemminger 
37283a27052SEric Dumazet 	u64_stats_update_begin(&stats->rx_syncp);
3733fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
3743fa2a1dfSstephen hemminger 	stats->rx_packets++;
37583a27052SEric Dumazet 	u64_stats_update_end(&stats->rx_syncp);
376296f96fcSRusty Russell 
377b3f24698SRusty Russell 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
378296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
379b3f24698SRusty Russell 		if (!skb_partial_csum_set(skb,
380b3f24698SRusty Russell 					  hdr->hdr.csum_start,
381b3f24698SRusty Russell 					  hdr->hdr.csum_offset))
382296f96fcSRusty Russell 			goto frame_err;
38310a8d94aSJason Wang 	} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
38410a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
385296f96fcSRusty Russell 	}
386296f96fcSRusty Russell 
38723cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
38823cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
38923cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
39023cde76dSMark McLoughlin 
391b3f24698SRusty Russell 	if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
392296f96fcSRusty Russell 		pr_debug("GSO!\n");
393b3f24698SRusty Russell 		switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
394296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
395c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
396296f96fcSRusty Russell 			break;
397296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
398c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
399296f96fcSRusty Russell 			break;
400296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
401c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
402296f96fcSRusty Russell 			break;
403296f96fcSRusty Russell 		default:
404be443899SAmerigo Wang 			net_warn_ratelimited("%s: bad gso type %u.\n",
405b3f24698SRusty Russell 					     dev->name, hdr->hdr.gso_type);
406296f96fcSRusty Russell 			goto frame_err;
407296f96fcSRusty Russell 		}
408296f96fcSRusty Russell 
409b3f24698SRusty Russell 		if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
410c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
41134a48579SRusty Russell 
412b3f24698SRusty Russell 		skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
413296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
414be443899SAmerigo Wang 			net_warn_ratelimited("%s: zero gso size.\n", dev->name);
415296f96fcSRusty Russell 			goto frame_err;
416296f96fcSRusty Russell 		}
417296f96fcSRusty Russell 
418296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
419296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
420296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
421296f96fcSRusty Russell 	}
422296f96fcSRusty Russell 
423296f96fcSRusty Russell 	netif_receive_skb(skb);
424296f96fcSRusty Russell 	return;
425296f96fcSRusty Russell 
426296f96fcSRusty Russell frame_err:
427296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
428296f96fcSRusty Russell 	dev_kfree_skb(skb);
429296f96fcSRusty Russell }
430296f96fcSRusty Russell 
431e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
432296f96fcSRusty Russell {
433e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
434296f96fcSRusty Russell 	struct sk_buff *skb;
435b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr;
4369ab86bbcSShirley Ma 	int err;
4373f2c31d9SMark McLoughlin 
4383464645aSMike Waychison 	skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
4399ab86bbcSShirley Ma 	if (unlikely(!skb))
4409ab86bbcSShirley Ma 		return -ENOMEM;
441296f96fcSRusty Russell 
442296f96fcSRusty Russell 	skb_put(skb, MAX_PACKET_LEN);
4433f2c31d9SMark McLoughlin 
4443f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
445e9d7417bSJason Wang 	sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
44697402b96SHerbert Xu 
447e9d7417bSJason Wang 	skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
44897402b96SHerbert Xu 
4499dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
4509ab86bbcSShirley Ma 	if (err < 0)
4519ab86bbcSShirley Ma 		dev_kfree_skb(skb);
45297402b96SHerbert Xu 
4539ab86bbcSShirley Ma 	return err;
45497402b96SHerbert Xu }
45597402b96SHerbert Xu 
456e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
4579ab86bbcSShirley Ma {
4589ab86bbcSShirley Ma 	struct page *first, *list = NULL;
4599ab86bbcSShirley Ma 	char *p;
4609ab86bbcSShirley Ma 	int i, err, offset;
461296f96fcSRusty Russell 
462e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
4639ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
464e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
4659ab86bbcSShirley Ma 		if (!first) {
4669ab86bbcSShirley Ma 			if (list)
467e9d7417bSJason Wang 				give_pages(rq, list);
4689ab86bbcSShirley Ma 			return -ENOMEM;
469296f96fcSRusty Russell 		}
470e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
4719ab86bbcSShirley Ma 
4729ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
4739ab86bbcSShirley Ma 		first->private = (unsigned long)list;
4749ab86bbcSShirley Ma 		list = first;
4759ab86bbcSShirley Ma 	}
4769ab86bbcSShirley Ma 
477e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
4789ab86bbcSShirley Ma 	if (!first) {
479e9d7417bSJason Wang 		give_pages(rq, list);
4809ab86bbcSShirley Ma 		return -ENOMEM;
4819ab86bbcSShirley Ma 	}
4829ab86bbcSShirley Ma 	p = page_address(first);
4839ab86bbcSShirley Ma 
484e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
485e9d7417bSJason Wang 	/* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
486e9d7417bSJason Wang 	sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
4879ab86bbcSShirley Ma 
488e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
4899ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
490e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
4919ab86bbcSShirley Ma 
4929ab86bbcSShirley Ma 	/* chain first in list head */
4939ab86bbcSShirley Ma 	first->private = (unsigned long)list;
4949dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
495aa989f5eSMichael S. Tsirkin 				  first, gfp);
4969ab86bbcSShirley Ma 	if (err < 0)
497e9d7417bSJason Wang 		give_pages(rq, first);
4989ab86bbcSShirley Ma 
4999ab86bbcSShirley Ma 	return err;
5009ab86bbcSShirley Ma }
5019ab86bbcSShirley Ma 
502e9d7417bSJason Wang static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
5039ab86bbcSShirley Ma {
5049ab86bbcSShirley Ma 	struct page *page;
5059ab86bbcSShirley Ma 	int err;
5069ab86bbcSShirley Ma 
507e9d7417bSJason Wang 	page = get_a_page(rq, gfp);
5089ab86bbcSShirley Ma 	if (!page)
5099ab86bbcSShirley Ma 		return -ENOMEM;
5109ab86bbcSShirley Ma 
511e9d7417bSJason Wang 	sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
5129ab86bbcSShirley Ma 
5139dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, page, gfp);
5149ab86bbcSShirley Ma 	if (err < 0)
515e9d7417bSJason Wang 		give_pages(rq, page);
5169ab86bbcSShirley Ma 
5179ab86bbcSShirley Ma 	return err;
518296f96fcSRusty Russell }
519296f96fcSRusty Russell 
520b2baed69SRusty Russell /*
521b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
522b2baed69SRusty Russell  *
523b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
524b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
525b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
526b2baed69SRusty Russell  */
527e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
5283f2c31d9SMark McLoughlin {
529e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
5303f2c31d9SMark McLoughlin 	int err;
5311788f495SMichael S. Tsirkin 	bool oom;
5323f2c31d9SMark McLoughlin 
5330aea51c3SAmit Shah 	do {
5349ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
535e9d7417bSJason Wang 			err = add_recvbuf_mergeable(rq, gfp);
5369ab86bbcSShirley Ma 		else if (vi->big_packets)
537e9d7417bSJason Wang 			err = add_recvbuf_big(rq, gfp);
5389ab86bbcSShirley Ma 		else
539e9d7417bSJason Wang 			err = add_recvbuf_small(rq, gfp);
5403f2c31d9SMark McLoughlin 
5411788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
5429ed4cb07SRusty Russell 		if (err)
5433f2c31d9SMark McLoughlin 			break;
544e9d7417bSJason Wang 		++rq->num;
545b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
546e9d7417bSJason Wang 	if (unlikely(rq->num > rq->max))
547e9d7417bSJason Wang 		rq->max = rq->num;
548e9d7417bSJason Wang 	virtqueue_kick(rq->vq);
5493161e453SRusty Russell 	return !oom;
5503f2c31d9SMark McLoughlin }
5513f2c31d9SMark McLoughlin 
55218445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
553296f96fcSRusty Russell {
554296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
555986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
556e9d7417bSJason Wang 
55718445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
558e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
5591915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
560e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
56118445c4dSRusty Russell 	}
562296f96fcSRusty Russell }
563296f96fcSRusty Russell 
564e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
5653e9d08ecSBruce Rogers {
566e9d7417bSJason Wang 	napi_enable(&rq->napi);
5673e9d08ecSBruce Rogers 
5683e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
5693e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
5703e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
5713e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
572e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
573e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
574ec13ee80SMichael S. Tsirkin 		local_bh_disable();
575e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
576ec13ee80SMichael S. Tsirkin 		local_bh_enable();
5773e9d08ecSBruce Rogers 	}
5783e9d08ecSBruce Rogers }
5793e9d08ecSBruce Rogers 
5803161e453SRusty Russell static void refill_work(struct work_struct *work)
5813161e453SRusty Russell {
582e9d7417bSJason Wang 	struct virtnet_info *vi =
583e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
5843161e453SRusty Russell 	bool still_empty;
585986a4f4dSJason Wang 	int i;
5863161e453SRusty Russell 
58755257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
588986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
589986a4f4dSJason Wang 
590986a4f4dSJason Wang 		napi_disable(&rq->napi);
591986a4f4dSJason Wang 		still_empty = !try_fill_recv(rq, GFP_KERNEL);
592986a4f4dSJason Wang 		virtnet_napi_enable(rq);
5933161e453SRusty Russell 
5943161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
595986a4f4dSJason Wang 		 * we will *never* try to fill again.
596986a4f4dSJason Wang 		 */
5973161e453SRusty Russell 		if (still_empty)
5983b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
5993161e453SRusty Russell 	}
600986a4f4dSJason Wang }
6013161e453SRusty Russell 
602296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
603296f96fcSRusty Russell {
604e9d7417bSJason Wang 	struct receive_queue *rq =
605e9d7417bSJason Wang 		container_of(napi, struct receive_queue, napi);
606e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
6079ab86bbcSShirley Ma 	void *buf;
608cbdadbbfSMichael S. Tsirkin 	unsigned int r, len, received = 0;
609296f96fcSRusty Russell 
610296f96fcSRusty Russell again:
611296f96fcSRusty Russell 	while (received < budget &&
612e9d7417bSJason Wang 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
613e9d7417bSJason Wang 		receive_buf(rq, buf, len);
614e9d7417bSJason Wang 		--rq->num;
615296f96fcSRusty Russell 		received++;
616296f96fcSRusty Russell 	}
617296f96fcSRusty Russell 
618e9d7417bSJason Wang 	if (rq->num < rq->max / 2) {
619e9d7417bSJason Wang 		if (!try_fill_recv(rq, GFP_ATOMIC))
6203b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
6213161e453SRusty Russell 	}
622296f96fcSRusty Russell 
6238329d98eSRusty Russell 	/* Out of packets? */
6248329d98eSRusty Russell 	if (received < budget) {
625cbdadbbfSMichael S. Tsirkin 		r = virtqueue_enable_cb_prepare(rq->vq);
626288379f0SBen Hutchings 		napi_complete(napi);
627cbdadbbfSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(rq->vq, r)) &&
6288e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
629e9d7417bSJason Wang 			virtqueue_disable_cb(rq->vq);
630288379f0SBen Hutchings 			__napi_schedule(napi);
631296f96fcSRusty Russell 			goto again;
632296f96fcSRusty Russell 		}
6334265f161SChristian Borntraeger 	}
634296f96fcSRusty Russell 
635296f96fcSRusty Russell 	return received;
636296f96fcSRusty Russell }
637296f96fcSRusty Russell 
638986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
639986a4f4dSJason Wang {
640986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
641986a4f4dSJason Wang 	int i;
642986a4f4dSJason Wang 
643e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
644e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
645986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
646986a4f4dSJason Wang 			if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
647986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
648986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
649986a4f4dSJason Wang 	}
650986a4f4dSJason Wang 
651986a4f4dSJason Wang 	return 0;
652986a4f4dSJason Wang }
653986a4f4dSJason Wang 
654b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
655296f96fcSRusty Russell {
656296f96fcSRusty Russell 	struct sk_buff *skb;
6576ee57bccSMichael S. Tsirkin 	unsigned int len;
658e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
65958472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
660296f96fcSRusty Russell 
661e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
662296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
6633fa2a1dfSstephen hemminger 
66483a27052SEric Dumazet 		u64_stats_update_begin(&stats->tx_syncp);
6653fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
6663fa2a1dfSstephen hemminger 		stats->tx_packets++;
66783a27052SEric Dumazet 		u64_stats_update_end(&stats->tx_syncp);
6683fa2a1dfSstephen hemminger 
669ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
670296f96fcSRusty Russell 	}
671296f96fcSRusty Russell }
672296f96fcSRusty Russell 
673e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
674296f96fcSRusty Russell {
675e7428e95SMichael S. Tsirkin 	struct skb_vnet_hdr *hdr;
676296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
677e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
6787bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
679e7428e95SMichael S. Tsirkin 	unsigned hdr_len;
680e7428e95SMichael S. Tsirkin 	bool can_push;
681296f96fcSRusty Russell 
682e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
683e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
684e7428e95SMichael S. Tsirkin 		hdr_len = sizeof hdr->mhdr;
685e7428e95SMichael S. Tsirkin 	else
686e7428e95SMichael S. Tsirkin 		hdr_len = sizeof hdr->hdr;
687e7428e95SMichael S. Tsirkin 
688e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
689e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
690e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
691e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
692e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
693e7428e95SMichael S. Tsirkin 	if (can_push)
694e7428e95SMichael S. Tsirkin 		hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
695e7428e95SMichael S. Tsirkin 	else
696e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
697296f96fcSRusty Russell 
698296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
699b3f24698SRusty Russell 		hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
70055508d60SMichał Mirosław 		hdr->hdr.csum_start = skb_checksum_start_offset(skb);
701b3f24698SRusty Russell 		hdr->hdr.csum_offset = skb->csum_offset;
702296f96fcSRusty Russell 	} else {
703b3f24698SRusty Russell 		hdr->hdr.flags = 0;
704b3f24698SRusty Russell 		hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
705296f96fcSRusty Russell 	}
706296f96fcSRusty Russell 
707296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
708b3f24698SRusty Russell 		hdr->hdr.hdr_len = skb_headlen(skb);
709b3f24698SRusty Russell 		hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
71034a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
711b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
712296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
713b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
714296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
715b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
716296f96fcSRusty Russell 		else
717296f96fcSRusty Russell 			BUG();
71834a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
719b3f24698SRusty Russell 			hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
720296f96fcSRusty Russell 	} else {
721b3f24698SRusty Russell 		hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
722b3f24698SRusty Russell 		hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
723296f96fcSRusty Russell 	}
724296f96fcSRusty Russell 
725e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
726b3f24698SRusty Russell 		hdr->mhdr.num_buffers = 0;
7273f2c31d9SMark McLoughlin 
728e7428e95SMichael S. Tsirkin 	if (can_push) {
729e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
730e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
731e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
732e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
733e7428e95SMichael S. Tsirkin 	} else {
734e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
735b7dfde95SLinus Torvalds 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
736e7428e95SMichael S. Tsirkin 	}
7379dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
73811a3a154SRusty Russell }
73911a3a154SRusty Russell 
740424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
74199ffc696SRusty Russell {
74299ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
743986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
744986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
7459ed4cb07SRusty Russell 	int err;
7462cb9c6baSRusty Russell 
7472cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
748e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
74999ffc696SRusty Russell 
75003f191baSMichael S. Tsirkin 	/* Try to transmit */
751b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
75299ffc696SRusty Russell 
7539ed4cb07SRusty Russell 	/* This should not happen! */
7540e3daa64SRusty Russell 	if (unlikely(err)) {
75558eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
7562e57b79cSRick Jones 		if (net_ratelimit())
75758eba97dSRusty Russell 			dev_warn(&dev->dev,
758b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
75958eba97dSRusty Russell 		dev->stats.tx_dropped++;
76058eba97dSRusty Russell 		kfree_skb(skb);
76158eba97dSRusty Russell 		return NETDEV_TX_OK;
762296f96fcSRusty Russell 	}
763e9d7417bSJason Wang 	virtqueue_kick(sq->vq);
76403f191baSMichael S. Tsirkin 
76548925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
76648925e37SRusty Russell 	skb_orphan(skb);
76748925e37SRusty Russell 	nf_reset(skb);
76848925e37SRusty Russell 
76948925e37SRusty Russell 	/* Apparently nice girls don't return TX_BUSY; stop the queue
77048925e37SRusty Russell 	 * before it gets out of hand.  Naturally, this wastes entries. */
771b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
772986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
773e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
77448925e37SRusty Russell 			/* More just got used, free them then recheck. */
775b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
776b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
777986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
778e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
77948925e37SRusty Russell 			}
78048925e37SRusty Russell 		}
78148925e37SRusty Russell 	}
78248925e37SRusty Russell 
78348925e37SRusty Russell 	return NETDEV_TX_OK;
78448925e37SRusty Russell }
78548925e37SRusty Russell 
78640cbfc37SAmos Kong /*
78740cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
78840cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
78940cbfc37SAmos Kong  * never fail unless improperly formated.
79040cbfc37SAmos Kong  */
79140cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
792f7bc9594SRusty Russell 				 struct scatterlist *out,
793f7bc9594SRusty Russell 				 struct scatterlist *in)
79440cbfc37SAmos Kong {
795f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
79640cbfc37SAmos Kong 	struct virtio_net_ctrl_hdr ctrl;
79740cbfc37SAmos Kong 	virtio_net_ctrl_ack status = ~0;
798f7bc9594SRusty Russell 	unsigned out_num = 0, in_num = 0, tmp;
79940cbfc37SAmos Kong 
80040cbfc37SAmos Kong 	/* Caller should know better */
801f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
80240cbfc37SAmos Kong 
80340cbfc37SAmos Kong 	ctrl.class = class;
80440cbfc37SAmos Kong 	ctrl.cmd = cmd;
805f7bc9594SRusty Russell 	/* Add header */
806f7bc9594SRusty Russell 	sg_init_one(&hdr, &ctrl, sizeof(ctrl));
807f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
80840cbfc37SAmos Kong 
809f7bc9594SRusty Russell 	if (out)
810f7bc9594SRusty Russell 		sgs[out_num++] = out;
811f7bc9594SRusty Russell 	if (in)
812f7bc9594SRusty Russell 		sgs[out_num + in_num++] = in;
81340cbfc37SAmos Kong 
814f7bc9594SRusty Russell 	/* Add return status. */
815f7bc9594SRusty Russell 	sg_init_one(&stat, &status, sizeof(status));
816f7bc9594SRusty Russell 	sgs[out_num + in_num++] = &stat;
81740cbfc37SAmos Kong 
818f7bc9594SRusty Russell 	BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
819f7bc9594SRusty Russell 	BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
820f7bc9594SRusty Russell 	       < 0);
82140cbfc37SAmos Kong 
82240cbfc37SAmos Kong 	virtqueue_kick(vi->cvq);
82340cbfc37SAmos Kong 
82440cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
82540cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
82640cbfc37SAmos Kong 	 */
82740cbfc37SAmos Kong 	while (!virtqueue_get_buf(vi->cvq, &tmp))
82840cbfc37SAmos Kong 		cpu_relax();
82940cbfc37SAmos Kong 
83040cbfc37SAmos Kong 	return status == VIRTIO_NET_OK;
83140cbfc37SAmos Kong }
83240cbfc37SAmos Kong 
8339c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
8349c46f6d4SAlex Williamson {
8359c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
8369c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
837f2f2c8b4SJiri Pirko 	int ret;
8387e58d5aeSAmos Kong 	struct sockaddr *addr = p;
8397e58d5aeSAmos Kong 	struct scatterlist sg;
8409c46f6d4SAlex Williamson 
8417e58d5aeSAmos Kong 	ret = eth_prepare_mac_addr_change(dev, p);
842f2f2c8b4SJiri Pirko 	if (ret)
843f2f2c8b4SJiri Pirko 		return ret;
8449c46f6d4SAlex Williamson 
8457e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
8467e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
8477e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
8487e58d5aeSAmos Kong 					  VIRTIO_NET_CTRL_MAC_ADDR_SET,
849f7bc9594SRusty Russell 					  &sg, NULL)) {
8507e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
8517e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
8527e58d5aeSAmos Kong 			return -EINVAL;
8537e58d5aeSAmos Kong 		}
8547e58d5aeSAmos Kong 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
8559c46f6d4SAlex Williamson 		vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
8567e58d5aeSAmos Kong 				  addr->sa_data, dev->addr_len);
8577e58d5aeSAmos Kong 	}
8587e58d5aeSAmos Kong 
8597e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
8609c46f6d4SAlex Williamson 
8619c46f6d4SAlex Williamson 	return 0;
8629c46f6d4SAlex Williamson }
8639c46f6d4SAlex Williamson 
8643fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
8653fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
8663fa2a1dfSstephen hemminger {
8673fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
8683fa2a1dfSstephen hemminger 	int cpu;
8693fa2a1dfSstephen hemminger 	unsigned int start;
8703fa2a1dfSstephen hemminger 
8713fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
87258472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
8733fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
8743fa2a1dfSstephen hemminger 
8753fa2a1dfSstephen hemminger 		do {
876e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
8773fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
8783fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
879e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
88083a27052SEric Dumazet 
88183a27052SEric Dumazet 		do {
882e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
8833fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
8843fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
885e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
8863fa2a1dfSstephen hemminger 
8873fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
8883fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
8893fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
8903fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
8913fa2a1dfSstephen hemminger 	}
8923fa2a1dfSstephen hemminger 
8933fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
894021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
8953fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
8963fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
8973fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
8983fa2a1dfSstephen hemminger 
8993fa2a1dfSstephen hemminger 	return tot;
9003fa2a1dfSstephen hemminger }
9013fa2a1dfSstephen hemminger 
902da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
903da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
904da74e89dSAmit Shah {
905da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
906986a4f4dSJason Wang 	int i;
907da74e89dSAmit Shah 
908986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
909986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
910da74e89dSAmit Shah }
911da74e89dSAmit Shah #endif
912da74e89dSAmit Shah 
913586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
914586d17c5SJason Wang {
915586d17c5SJason Wang 	rtnl_lock();
916586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
917f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
918586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
919586d17c5SJason Wang 	rtnl_unlock();
920586d17c5SJason Wang }
921586d17c5SJason Wang 
922986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
923986a4f4dSJason Wang {
924986a4f4dSJason Wang 	struct scatterlist sg;
925986a4f4dSJason Wang 	struct virtio_net_ctrl_mq s;
926986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
927986a4f4dSJason Wang 
928986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
929986a4f4dSJason Wang 		return 0;
930986a4f4dSJason Wang 
931986a4f4dSJason Wang 	s.virtqueue_pairs = queue_pairs;
932986a4f4dSJason Wang 	sg_init_one(&sg, &s, sizeof(s));
933986a4f4dSJason Wang 
934986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
935f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
936986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
937986a4f4dSJason Wang 			 queue_pairs);
938986a4f4dSJason Wang 		return -EINVAL;
93955257d72SSasha Levin 	} else {
940986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
9419b9cd802SJason Wang 		schedule_delayed_work(&vi->refill, 0);
94255257d72SSasha Levin 	}
943986a4f4dSJason Wang 
944986a4f4dSJason Wang 	return 0;
945986a4f4dSJason Wang }
946986a4f4dSJason Wang 
947296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
948296f96fcSRusty Russell {
949296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
950986a4f4dSJason Wang 	int i;
951296f96fcSRusty Russell 
952b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
953b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
954986a4f4dSJason Wang 
955986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
956986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
957296f96fcSRusty Russell 
958296f96fcSRusty Russell 	return 0;
959296f96fcSRusty Russell }
960296f96fcSRusty Russell 
9612af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
9622af7698eSAlex Williamson {
9632af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
964f565a7c2SAlex Williamson 	struct scatterlist sg[2];
9652af7698eSAlex Williamson 	u8 promisc, allmulti;
966f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
967ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
96832e7bfc4SJiri Pirko 	int uc_count;
9694cd24eafSJiri Pirko 	int mc_count;
970f565a7c2SAlex Williamson 	void *buf;
971f565a7c2SAlex Williamson 	int i;
9722af7698eSAlex Williamson 
9732af7698eSAlex Williamson 	/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
9742af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
9752af7698eSAlex Williamson 		return;
9762af7698eSAlex Williamson 
977f565a7c2SAlex Williamson 	promisc = ((dev->flags & IFF_PROMISC) != 0);
978f565a7c2SAlex Williamson 	allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
9792af7698eSAlex Williamson 
98023e258e1SAlex Williamson 	sg_init_one(sg, &promisc, sizeof(promisc));
9812af7698eSAlex Williamson 
9822af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
9832af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_PROMISC,
984f7bc9594SRusty Russell 				  sg, NULL))
9852af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
9862af7698eSAlex Williamson 			 promisc ? "en" : "dis");
9872af7698eSAlex Williamson 
98823e258e1SAlex Williamson 	sg_init_one(sg, &allmulti, sizeof(allmulti));
9892af7698eSAlex Williamson 
9902af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
9912af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_ALLMULTI,
992f7bc9594SRusty Russell 				  sg, NULL))
9932af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
9942af7698eSAlex Williamson 			 allmulti ? "en" : "dis");
995f565a7c2SAlex Williamson 
99632e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
9974cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
998f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
9994cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1000f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
10014cd24eafSJiri Pirko 	mac_data = buf;
1002e68ed8f0SJoe Perches 	if (!buf)
1003f565a7c2SAlex Williamson 		return;
1004f565a7c2SAlex Williamson 
100523e258e1SAlex Williamson 	sg_init_table(sg, 2);
100623e258e1SAlex Williamson 
1007f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
100832e7bfc4SJiri Pirko 	mac_data->entries = uc_count;
1009ccffad25SJiri Pirko 	i = 0;
101032e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1011ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1012f565a7c2SAlex Williamson 
1013f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
101432e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1015f565a7c2SAlex Williamson 
1016f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
101732e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1018f565a7c2SAlex Williamson 
10194cd24eafSJiri Pirko 	mac_data->entries = mc_count;
1020567ec874SJiri Pirko 	i = 0;
102122bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
102222bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1023f565a7c2SAlex Williamson 
1024f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
10254cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1026f565a7c2SAlex Williamson 
1027f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1028f565a7c2SAlex Williamson 				  VIRTIO_NET_CTRL_MAC_TABLE_SET,
1029f7bc9594SRusty Russell 				  sg, NULL))
1030f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
1031f565a7c2SAlex Williamson 
1032f565a7c2SAlex Williamson 	kfree(buf);
10332af7698eSAlex Williamson }
10342af7698eSAlex Williamson 
103580d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
103680d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
10370bde9569SAlex Williamson {
10380bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
10390bde9569SAlex Williamson 	struct scatterlist sg;
10400bde9569SAlex Williamson 
104123e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
10420bde9569SAlex Williamson 
10430bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1044f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
10450bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
10468e586137SJiri Pirko 	return 0;
10470bde9569SAlex Williamson }
10480bde9569SAlex Williamson 
104980d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
105080d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
10510bde9569SAlex Williamson {
10520bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
10530bde9569SAlex Williamson 	struct scatterlist sg;
10540bde9569SAlex Williamson 
105523e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
10560bde9569SAlex Williamson 
10570bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1058f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
10590bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
10608e586137SJiri Pirko 	return 0;
10610bde9569SAlex Williamson }
10620bde9569SAlex Williamson 
10638898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1064986a4f4dSJason Wang {
1065986a4f4dSJason Wang 	int i;
10668898c21cSWanlong Gao 	int cpu;
10678898c21cSWanlong Gao 
10688898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
10698898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
10708898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
10718898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
10728898c21cSWanlong Gao 		}
10738898c21cSWanlong Gao 
10748898c21cSWanlong Gao 		vi->affinity_hint_set = false;
10758898c21cSWanlong Gao 	}
10768898c21cSWanlong Gao 
10778898c21cSWanlong Gao 	i = 0;
10788898c21cSWanlong Gao 	for_each_online_cpu(cpu) {
10798898c21cSWanlong Gao 		if (cpu == hcpu) {
10808898c21cSWanlong Gao 			*per_cpu_ptr(vi->vq_index, cpu) = -1;
10818898c21cSWanlong Gao 		} else {
10828898c21cSWanlong Gao 			*per_cpu_ptr(vi->vq_index, cpu) =
10838898c21cSWanlong Gao 				++i % vi->curr_queue_pairs;
10848898c21cSWanlong Gao 		}
10858898c21cSWanlong Gao 	}
10868898c21cSWanlong Gao }
10878898c21cSWanlong Gao 
10888898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1089986a4f4dSJason Wang {
1090986a4f4dSJason Wang 	int i;
109147be2479SWanlong Gao 	int cpu;
1092986a4f4dSJason Wang 
1093986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1094986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1095986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1096986a4f4dSJason Wang 	 */
10978898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
10988898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
10998898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1100986a4f4dSJason Wang 		return;
1101986a4f4dSJason Wang 	}
1102986a4f4dSJason Wang 
110347be2479SWanlong Gao 	i = 0;
110447be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1105986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1106986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
110747be2479SWanlong Gao 		*per_cpu_ptr(vi->vq_index, cpu) = i;
110847be2479SWanlong Gao 		i++;
1109986a4f4dSJason Wang 	}
1110986a4f4dSJason Wang 
1111986a4f4dSJason Wang 	vi->affinity_hint_set = true;
111247be2479SWanlong Gao }
1113986a4f4dSJason Wang 
11148de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb,
11158de4b2f3SWanlong Gao 			        unsigned long action, void *hcpu)
11168de4b2f3SWanlong Gao {
11178de4b2f3SWanlong Gao 	struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
11188de4b2f3SWanlong Gao 
1119*3ab098dfSJason Wang 	mutex_lock(&vi->config_lock);
1120*3ab098dfSJason Wang 
1121*3ab098dfSJason Wang 	if (!vi->config_enable)
1122*3ab098dfSJason Wang 		goto done;
1123*3ab098dfSJason Wang 
11248de4b2f3SWanlong Gao 	switch(action & ~CPU_TASKS_FROZEN) {
11258de4b2f3SWanlong Gao 	case CPU_ONLINE:
11268de4b2f3SWanlong Gao 	case CPU_DOWN_FAILED:
11278de4b2f3SWanlong Gao 	case CPU_DEAD:
11288de4b2f3SWanlong Gao 		virtnet_set_affinity(vi);
11298de4b2f3SWanlong Gao 		break;
11308de4b2f3SWanlong Gao 	case CPU_DOWN_PREPARE:
11318de4b2f3SWanlong Gao 		virtnet_clean_affinity(vi, (long)hcpu);
11328de4b2f3SWanlong Gao 		break;
11338de4b2f3SWanlong Gao 	default:
11348de4b2f3SWanlong Gao 		break;
11358de4b2f3SWanlong Gao 	}
1136*3ab098dfSJason Wang 
1137*3ab098dfSJason Wang done:
1138*3ab098dfSJason Wang 	mutex_unlock(&vi->config_lock);
11398de4b2f3SWanlong Gao 	return NOTIFY_OK;
1140a9ea3fc6SHerbert Xu }
1141a9ea3fc6SHerbert Xu 
11428f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
11438f9f4668SRick Jones 				struct ethtool_ringparam *ring)
11448f9f4668SRick Jones {
11458f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
11468f9f4668SRick Jones 
1147986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1148986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
11498f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
11508f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
11518f9f4668SRick Jones }
11528f9f4668SRick Jones 
115366846048SRick Jones 
115466846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
115566846048SRick Jones 				struct ethtool_drvinfo *info)
115666846048SRick Jones {
115766846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
115866846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
115966846048SRick Jones 
116066846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
116166846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
116266846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
116366846048SRick Jones 
116466846048SRick Jones }
116566846048SRick Jones 
1166d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1167d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1168d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1169d73bcd2cSJason Wang {
1170d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1171d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1172d73bcd2cSJason Wang 	int err;
1173d73bcd2cSJason Wang 
1174d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1175d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1176d73bcd2cSJason Wang 	 */
1177d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1178d73bcd2cSJason Wang 		return -EINVAL;
1179d73bcd2cSJason Wang 
1180d73bcd2cSJason Wang 	if (queue_pairs > vi->max_queue_pairs)
1181d73bcd2cSJason Wang 		return -EINVAL;
1182d73bcd2cSJason Wang 
118347be2479SWanlong Gao 	get_online_cpus();
1184d73bcd2cSJason Wang 	err = virtnet_set_queues(vi, queue_pairs);
1185d73bcd2cSJason Wang 	if (!err) {
1186d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1187d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1188d73bcd2cSJason Wang 
11898898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1190d73bcd2cSJason Wang 	}
119147be2479SWanlong Gao 	put_online_cpus();
1192d73bcd2cSJason Wang 
1193d73bcd2cSJason Wang 	return err;
1194d73bcd2cSJason Wang }
1195d73bcd2cSJason Wang 
1196d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1197d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1198d73bcd2cSJason Wang {
1199d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1200d73bcd2cSJason Wang 
1201d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1202d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1203d73bcd2cSJason Wang 	channels->max_other = 0;
1204d73bcd2cSJason Wang 	channels->rx_count = 0;
1205d73bcd2cSJason Wang 	channels->tx_count = 0;
1206d73bcd2cSJason Wang 	channels->other_count = 0;
1207d73bcd2cSJason Wang }
1208d73bcd2cSJason Wang 
12090fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
121066846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
12119f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
12128f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1213d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1214d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1215a9ea3fc6SHerbert Xu };
1216a9ea3fc6SHerbert Xu 
121739da5814SMark McLoughlin #define MIN_MTU 68
121839da5814SMark McLoughlin #define MAX_MTU 65535
121939da5814SMark McLoughlin 
122039da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
122139da5814SMark McLoughlin {
122239da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
122339da5814SMark McLoughlin 		return -EINVAL;
122439da5814SMark McLoughlin 	dev->mtu = new_mtu;
122539da5814SMark McLoughlin 	return 0;
122639da5814SMark McLoughlin }
122739da5814SMark McLoughlin 
1228986a4f4dSJason Wang /* To avoid contending a lock hold by a vcpu who would exit to host, select the
1229986a4f4dSJason Wang  * txq based on the processor id.
1230986a4f4dSJason Wang  */
1231986a4f4dSJason Wang static u16 virtnet_select_queue(struct net_device *dev, struct sk_buff *skb)
1232986a4f4dSJason Wang {
123347be2479SWanlong Gao 	int txq;
123447be2479SWanlong Gao 	struct virtnet_info *vi = netdev_priv(dev);
123547be2479SWanlong Gao 
123647be2479SWanlong Gao 	if (skb_rx_queue_recorded(skb)) {
123747be2479SWanlong Gao 		txq = skb_get_rx_queue(skb);
123847be2479SWanlong Gao 	} else {
123947be2479SWanlong Gao 		txq = *__this_cpu_ptr(vi->vq_index);
124047be2479SWanlong Gao 		if (txq == -1)
124147be2479SWanlong Gao 			txq = 0;
124247be2479SWanlong Gao 	}
1243986a4f4dSJason Wang 
1244986a4f4dSJason Wang 	while (unlikely(txq >= dev->real_num_tx_queues))
1245986a4f4dSJason Wang 		txq -= dev->real_num_tx_queues;
1246986a4f4dSJason Wang 
1247986a4f4dSJason Wang 	return txq;
1248986a4f4dSJason Wang }
1249986a4f4dSJason Wang 
125076288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
125176288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
125276288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
125376288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
125476288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
12559c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
12562af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
125776288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
12583fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
12591824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
12601824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1261986a4f4dSJason Wang 	.ndo_select_queue     = virtnet_select_queue,
126276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
126376288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
126476288b4eSStephen Hemminger #endif
126576288b4eSStephen Hemminger };
126676288b4eSStephen Hemminger 
1267586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
12689f4d26d0SMark McLoughlin {
1269586d17c5SJason Wang 	struct virtnet_info *vi =
1270586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
12719f4d26d0SMark McLoughlin 	u16 v;
12729f4d26d0SMark McLoughlin 
1273586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1274586d17c5SJason Wang 	if (!vi->config_enable)
1275586d17c5SJason Wang 		goto done;
1276586d17c5SJason Wang 
127777dd7693SSasha Levin 	if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
12789f4d26d0SMark McLoughlin 			      offsetof(struct virtio_net_config, status),
127977dd7693SSasha Levin 			      &v) < 0)
1280586d17c5SJason Wang 		goto done;
1281586d17c5SJason Wang 
1282586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1283ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1284586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1285586d17c5SJason Wang 	}
12869f4d26d0SMark McLoughlin 
12879f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
12889f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
12899f4d26d0SMark McLoughlin 
12909f4d26d0SMark McLoughlin 	if (vi->status == v)
1291586d17c5SJason Wang 		goto done;
12929f4d26d0SMark McLoughlin 
12939f4d26d0SMark McLoughlin 	vi->status = v;
12949f4d26d0SMark McLoughlin 
12959f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
12969f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1297986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
12989f4d26d0SMark McLoughlin 	} else {
12999f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1300986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
13019f4d26d0SMark McLoughlin 	}
1302586d17c5SJason Wang done:
1303586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
13049f4d26d0SMark McLoughlin }
13059f4d26d0SMark McLoughlin 
13069f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
13079f4d26d0SMark McLoughlin {
13089f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
13099f4d26d0SMark McLoughlin 
13103b07e9caSTejun Heo 	schedule_work(&vi->config_work);
13119f4d26d0SMark McLoughlin }
13129f4d26d0SMark McLoughlin 
1313986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1314986a4f4dSJason Wang {
1315986a4f4dSJason Wang 	kfree(vi->rq);
1316986a4f4dSJason Wang 	kfree(vi->sq);
1317986a4f4dSJason Wang }
1318986a4f4dSJason Wang 
1319986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi)
1320986a4f4dSJason Wang {
1321986a4f4dSJason Wang 	int i;
1322986a4f4dSJason Wang 
1323986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1324986a4f4dSJason Wang 		while (vi->rq[i].pages)
1325986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1326986a4f4dSJason Wang 	}
1327986a4f4dSJason Wang }
1328986a4f4dSJason Wang 
1329986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1330986a4f4dSJason Wang {
1331986a4f4dSJason Wang 	void *buf;
1332986a4f4dSJason Wang 	int i;
1333986a4f4dSJason Wang 
1334986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1335986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
1336986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1337986a4f4dSJason Wang 			dev_kfree_skb(buf);
1338986a4f4dSJason Wang 	}
1339986a4f4dSJason Wang 
1340986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1341986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
1342986a4f4dSJason Wang 
1343986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1344986a4f4dSJason Wang 			if (vi->mergeable_rx_bufs || vi->big_packets)
1345986a4f4dSJason Wang 				give_pages(&vi->rq[i], buf);
1346986a4f4dSJason Wang 			else
1347986a4f4dSJason Wang 				dev_kfree_skb(buf);
1348986a4f4dSJason Wang 			--vi->rq[i].num;
1349986a4f4dSJason Wang 		}
1350986a4f4dSJason Wang 		BUG_ON(vi->rq[i].num != 0);
1351986a4f4dSJason Wang 	}
1352986a4f4dSJason Wang }
1353986a4f4dSJason Wang 
1354e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
1355e9d7417bSJason Wang {
1356e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
1357e9d7417bSJason Wang 
13588898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
1359986a4f4dSJason Wang 
1360e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
1361986a4f4dSJason Wang 
1362986a4f4dSJason Wang 	virtnet_free_queues(vi);
1363986a4f4dSJason Wang }
1364986a4f4dSJason Wang 
1365986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
1366986a4f4dSJason Wang {
1367986a4f4dSJason Wang 	vq_callback_t **callbacks;
1368986a4f4dSJason Wang 	struct virtqueue **vqs;
1369986a4f4dSJason Wang 	int ret = -ENOMEM;
1370986a4f4dSJason Wang 	int i, total_vqs;
1371986a4f4dSJason Wang 	const char **names;
1372986a4f4dSJason Wang 
1373986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1374986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1375986a4f4dSJason Wang 	 * possible control vq.
1376986a4f4dSJason Wang 	 */
1377986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
1378986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1379986a4f4dSJason Wang 
1380986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
1381986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1382986a4f4dSJason Wang 	if (!vqs)
1383986a4f4dSJason Wang 		goto err_vq;
1384986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1385986a4f4dSJason Wang 	if (!callbacks)
1386986a4f4dSJason Wang 		goto err_callback;
1387986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1388986a4f4dSJason Wang 	if (!names)
1389986a4f4dSJason Wang 		goto err_names;
1390986a4f4dSJason Wang 
1391986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
1392986a4f4dSJason Wang 	if (vi->has_cvq) {
1393986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
1394986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
1395986a4f4dSJason Wang 	}
1396986a4f4dSJason Wang 
1397986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
1398986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1399986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
1400986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
1401986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
1402986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
1403986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
1404986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
1405986a4f4dSJason Wang 	}
1406986a4f4dSJason Wang 
1407986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1408986a4f4dSJason Wang 					 names);
1409986a4f4dSJason Wang 	if (ret)
1410986a4f4dSJason Wang 		goto err_find;
1411986a4f4dSJason Wang 
1412986a4f4dSJason Wang 	if (vi->has_cvq) {
1413986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
1414986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1415f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1416986a4f4dSJason Wang 	}
1417986a4f4dSJason Wang 
1418986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1419986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
1420986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
1421986a4f4dSJason Wang 	}
1422986a4f4dSJason Wang 
1423986a4f4dSJason Wang 	kfree(names);
1424986a4f4dSJason Wang 	kfree(callbacks);
1425986a4f4dSJason Wang 	kfree(vqs);
1426986a4f4dSJason Wang 
1427986a4f4dSJason Wang 	return 0;
1428986a4f4dSJason Wang 
1429986a4f4dSJason Wang err_find:
1430986a4f4dSJason Wang 	kfree(names);
1431986a4f4dSJason Wang err_names:
1432986a4f4dSJason Wang 	kfree(callbacks);
1433986a4f4dSJason Wang err_callback:
1434986a4f4dSJason Wang 	kfree(vqs);
1435986a4f4dSJason Wang err_vq:
1436986a4f4dSJason Wang 	return ret;
1437986a4f4dSJason Wang }
1438986a4f4dSJason Wang 
1439986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
1440986a4f4dSJason Wang {
1441986a4f4dSJason Wang 	int i;
1442986a4f4dSJason Wang 
1443986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1444986a4f4dSJason Wang 	if (!vi->sq)
1445986a4f4dSJason Wang 		goto err_sq;
1446986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1447008d4278SAmerigo Wang 	if (!vi->rq)
1448986a4f4dSJason Wang 		goto err_rq;
1449986a4f4dSJason Wang 
1450986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
1451986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1452986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
1453986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1454986a4f4dSJason Wang 			       napi_weight);
1455986a4f4dSJason Wang 
1456986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1457986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1458986a4f4dSJason Wang 	}
1459986a4f4dSJason Wang 
1460986a4f4dSJason Wang 	return 0;
1461986a4f4dSJason Wang 
1462986a4f4dSJason Wang err_rq:
1463986a4f4dSJason Wang 	kfree(vi->sq);
1464986a4f4dSJason Wang err_sq:
1465986a4f4dSJason Wang 	return -ENOMEM;
1466e9d7417bSJason Wang }
1467e9d7417bSJason Wang 
14683f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
14693f9c10b0SAmit Shah {
1470986a4f4dSJason Wang 	int ret;
14713f9c10b0SAmit Shah 
1472986a4f4dSJason Wang 	/* Allocate send & receive queues */
1473986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
1474986a4f4dSJason Wang 	if (ret)
1475986a4f4dSJason Wang 		goto err;
14763f9c10b0SAmit Shah 
1477986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
1478986a4f4dSJason Wang 	if (ret)
1479986a4f4dSJason Wang 		goto err_free;
14803f9c10b0SAmit Shah 
148147be2479SWanlong Gao 	get_online_cpus();
14828898c21cSWanlong Gao 	virtnet_set_affinity(vi);
148347be2479SWanlong Gao 	put_online_cpus();
148447be2479SWanlong Gao 
14853f9c10b0SAmit Shah 	return 0;
1486986a4f4dSJason Wang 
1487986a4f4dSJason Wang err_free:
1488986a4f4dSJason Wang 	virtnet_free_queues(vi);
1489986a4f4dSJason Wang err:
1490986a4f4dSJason Wang 	return ret;
14913f9c10b0SAmit Shah }
14923f9c10b0SAmit Shah 
1493296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
1494296f96fcSRusty Russell {
1495986a4f4dSJason Wang 	int i, err;
1496296f96fcSRusty Russell 	struct net_device *dev;
1497296f96fcSRusty Russell 	struct virtnet_info *vi;
1498986a4f4dSJason Wang 	u16 max_queue_pairs;
1499986a4f4dSJason Wang 
1500986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
1501986a4f4dSJason Wang 	err = virtio_config_val(vdev, VIRTIO_NET_F_MQ,
1502986a4f4dSJason Wang 				offsetof(struct virtio_net_config,
1503986a4f4dSJason Wang 				max_virtqueue_pairs), &max_queue_pairs);
1504986a4f4dSJason Wang 
1505986a4f4dSJason Wang 	/* We need at least 2 queue's */
1506986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1507986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1508986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1509986a4f4dSJason Wang 		max_queue_pairs = 1;
1510296f96fcSRusty Russell 
1511296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
1512986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1513296f96fcSRusty Russell 	if (!dev)
1514296f96fcSRusty Russell 		return -ENOMEM;
1515296f96fcSRusty Russell 
1516296f96fcSRusty Russell 	/* Set up network device as normal. */
1517f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
151876288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
1519296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
15203fa2a1dfSstephen hemminger 
1521a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1522296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
1523296f96fcSRusty Russell 
1524296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
152598e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1526296f96fcSRusty Russell 		/* This opens up the world of extra features. */
152798e778c9SMichał Mirosław 		dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
152898e778c9SMichał Mirosław 		if (csum)
1529296f96fcSRusty Russell 			dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
153098e778c9SMichał Mirosław 
153198e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
153298e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
153334a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
153434a48579SRusty Russell 		}
15355539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
153698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
153798e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
153898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
153998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
154098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
154198e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
154298e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
154398e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_UFO;
154498e778c9SMichał Mirosław 
154598e778c9SMichał Mirosław 		if (gso)
154698e778c9SMichał Mirosław 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
154798e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
1548296f96fcSRusty Russell 	}
15494f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
15504f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
1551296f96fcSRusty Russell 
15524fda8302SJason Wang 	dev->vlan_features = dev->features;
15534fda8302SJason Wang 
1554296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
155577dd7693SSasha Levin 	if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
1556a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
155777dd7693SSasha Levin 				  dev->dev_addr, dev->addr_len) < 0)
1558f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1559296f96fcSRusty Russell 
1560296f96fcSRusty Russell 	/* Set up our device-specific information */
1561296f96fcSRusty Russell 	vi = netdev_priv(dev);
1562296f96fcSRusty Russell 	vi->dev = dev;
1563296f96fcSRusty Russell 	vi->vdev = vdev;
1564d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
15653fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
15663fa2a1dfSstephen hemminger 	err = -ENOMEM;
15673fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
15683fa2a1dfSstephen hemminger 		goto free;
15693fa2a1dfSstephen hemminger 
157047be2479SWanlong Gao 	vi->vq_index = alloc_percpu(int);
157147be2479SWanlong Gao 	if (vi->vq_index == NULL)
157247be2479SWanlong Gao 		goto free_stats;
157347be2479SWanlong Gao 
1574586d17c5SJason Wang 	mutex_init(&vi->config_lock);
1575586d17c5SJason Wang 	vi->config_enable = true;
1576586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1577296f96fcSRusty Russell 
157897402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
15798e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
15808e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
15818e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
158297402b96SHerbert Xu 		vi->big_packets = true;
158397402b96SHerbert Xu 
15843f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
15853f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
15863f2c31d9SMark McLoughlin 
1587e7428e95SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1588e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
1589e7428e95SMichael S. Tsirkin 
1590986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1591986a4f4dSJason Wang 		vi->has_cvq = true;
1592986a4f4dSJason Wang 
1593986a4f4dSJason Wang 	/* Use single tx/rx queue pair as default */
1594986a4f4dSJason Wang 	vi->curr_queue_pairs = 1;
1595986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
1596986a4f4dSJason Wang 
1597986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
15983f9c10b0SAmit Shah 	err = init_vqs(vi);
1599d2a7dddaSMichael S. Tsirkin 	if (err)
160047be2479SWanlong Gao 		goto free_index;
1601d2a7dddaSMichael S. Tsirkin 
1602986a4f4dSJason Wang 	netif_set_real_num_tx_queues(dev, 1);
1603986a4f4dSJason Wang 	netif_set_real_num_rx_queues(dev, 1);
1604986a4f4dSJason Wang 
1605296f96fcSRusty Russell 	err = register_netdev(dev);
1606296f96fcSRusty Russell 	if (err) {
1607296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1608d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1609296f96fcSRusty Russell 	}
1610b3369c1fSRusty Russell 
1611b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
161255257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1613986a4f4dSJason Wang 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
1614b3369c1fSRusty Russell 
1615b3369c1fSRusty Russell 		/* If we didn't even get one input buffer, we're useless. */
1616986a4f4dSJason Wang 		if (vi->rq[i].num == 0) {
1617986a4f4dSJason Wang 			free_unused_bufs(vi);
1618b3369c1fSRusty Russell 			err = -ENOMEM;
1619986a4f4dSJason Wang 			goto free_recv_bufs;
1620986a4f4dSJason Wang 		}
1621b3369c1fSRusty Russell 	}
1622b3369c1fSRusty Russell 
16238de4b2f3SWanlong Gao 	vi->nb.notifier_call = &virtnet_cpu_callback;
16248de4b2f3SWanlong Gao 	err = register_hotcpu_notifier(&vi->nb);
16258de4b2f3SWanlong Gao 	if (err) {
16268de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
16278de4b2f3SWanlong Gao 		goto free_recv_bufs;
16288de4b2f3SWanlong Gao 	}
16298de4b2f3SWanlong Gao 
1630167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1631167c25e4SJason Wang 	   otherwise get link status from config. */
1632167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1633167c25e4SJason Wang 		netif_carrier_off(dev);
16343b07e9caSTejun Heo 		schedule_work(&vi->config_work);
1635167c25e4SJason Wang 	} else {
1636167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
16374783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1638167c25e4SJason Wang 	}
16399f4d26d0SMark McLoughlin 
1640986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1641986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
1642986a4f4dSJason Wang 
1643296f96fcSRusty Russell 	return 0;
1644296f96fcSRusty Russell 
1645986a4f4dSJason Wang free_recv_bufs:
1646986a4f4dSJason Wang 	free_receive_bufs(vi);
1647b3369c1fSRusty Russell 	unregister_netdev(dev);
1648d2a7dddaSMichael S. Tsirkin free_vqs:
1649986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
1650e9d7417bSJason Wang 	virtnet_del_vqs(vi);
165147be2479SWanlong Gao free_index:
165247be2479SWanlong Gao 	free_percpu(vi->vq_index);
16533fa2a1dfSstephen hemminger free_stats:
16543fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1655296f96fcSRusty Russell free:
1656296f96fcSRusty Russell 	free_netdev(dev);
1657296f96fcSRusty Russell 	return err;
1658296f96fcSRusty Russell }
1659296f96fcSRusty Russell 
166004486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
1661296f96fcSRusty Russell {
166204486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
1663830a8a97SShirley Ma 
1664830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
16659ab86bbcSShirley Ma 	free_unused_bufs(vi);
1666fb6813f4SRusty Russell 
1667986a4f4dSJason Wang 	free_receive_bufs(vi);
1668d2a7dddaSMichael S. Tsirkin 
1669986a4f4dSJason Wang 	virtnet_del_vqs(vi);
167004486ed0SAmit Shah }
167104486ed0SAmit Shah 
16728cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
167304486ed0SAmit Shah {
167404486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
167504486ed0SAmit Shah 
16768de4b2f3SWanlong Gao 	unregister_hotcpu_notifier(&vi->nb);
16778de4b2f3SWanlong Gao 
1678586d17c5SJason Wang 	/* Prevent config work handler from accessing the device. */
1679586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1680586d17c5SJason Wang 	vi->config_enable = false;
1681586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1682586d17c5SJason Wang 
168304486ed0SAmit Shah 	unregister_netdev(vi->dev);
168404486ed0SAmit Shah 
168504486ed0SAmit Shah 	remove_vq_common(vi);
1686fb6813f4SRusty Russell 
1687586d17c5SJason Wang 	flush_work(&vi->config_work);
1688586d17c5SJason Wang 
168947be2479SWanlong Gao 	free_percpu(vi->vq_index);
16902e66f55bSKrishna Kumar 	free_percpu(vi->stats);
169174b2553fSRusty Russell 	free_netdev(vi->dev);
1692296f96fcSRusty Russell }
1693296f96fcSRusty Russell 
16940741bcb5SAmit Shah #ifdef CONFIG_PM
16950741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
16960741bcb5SAmit Shah {
16970741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1698986a4f4dSJason Wang 	int i;
16990741bcb5SAmit Shah 
1700586d17c5SJason Wang 	/* Prevent config work handler from accessing the device */
1701586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1702586d17c5SJason Wang 	vi->config_enable = false;
1703586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1704586d17c5SJason Wang 
17050741bcb5SAmit Shah 	netif_device_detach(vi->dev);
17060741bcb5SAmit Shah 	cancel_delayed_work_sync(&vi->refill);
17070741bcb5SAmit Shah 
17080741bcb5SAmit Shah 	if (netif_running(vi->dev))
1709986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++) {
1710986a4f4dSJason Wang 			napi_disable(&vi->rq[i].napi);
1711986a4f4dSJason Wang 			netif_napi_del(&vi->rq[i].napi);
1712986a4f4dSJason Wang 		}
17130741bcb5SAmit Shah 
17140741bcb5SAmit Shah 	remove_vq_common(vi);
17150741bcb5SAmit Shah 
1716586d17c5SJason Wang 	flush_work(&vi->config_work);
1717586d17c5SJason Wang 
17180741bcb5SAmit Shah 	return 0;
17190741bcb5SAmit Shah }
17200741bcb5SAmit Shah 
17210741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
17220741bcb5SAmit Shah {
17230741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1724986a4f4dSJason Wang 	int err, i;
17250741bcb5SAmit Shah 
17260741bcb5SAmit Shah 	err = init_vqs(vi);
17270741bcb5SAmit Shah 	if (err)
17280741bcb5SAmit Shah 		return err;
17290741bcb5SAmit Shah 
17300741bcb5SAmit Shah 	if (netif_running(vi->dev))
1731986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
1732986a4f4dSJason Wang 			virtnet_napi_enable(&vi->rq[i]);
17330741bcb5SAmit Shah 
17340741bcb5SAmit Shah 	netif_device_attach(vi->dev);
17350741bcb5SAmit Shah 
173655257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++)
1737986a4f4dSJason Wang 		if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
17383b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
17390741bcb5SAmit Shah 
1740586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1741586d17c5SJason Wang 	vi->config_enable = true;
1742586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1743586d17c5SJason Wang 
1744986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
1745986a4f4dSJason Wang 
17460741bcb5SAmit Shah 	return 0;
17470741bcb5SAmit Shah }
17480741bcb5SAmit Shah #endif
17490741bcb5SAmit Shah 
1750296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
1751296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1752296f96fcSRusty Russell 	{ 0 },
1753296f96fcSRusty Russell };
1754296f96fcSRusty Russell 
1755c45a6816SRusty Russell static unsigned int features[] = {
17565e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
17575e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1758c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
175997402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
17605c516751SSridhar Samudrala 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
17612a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
17620bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1763986a4f4dSJason Wang 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
17647e58d5aeSAmos Kong 	VIRTIO_NET_F_CTRL_MAC_ADDR,
1765e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
1766c45a6816SRusty Russell };
1767c45a6816SRusty Russell 
176822402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
1769c45a6816SRusty Russell 	.feature_table = features,
1770c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
1771296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
1772296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
1773296f96fcSRusty Russell 	.id_table =	id_table,
1774296f96fcSRusty Russell 	.probe =	virtnet_probe,
17758cc085d6SBill Pemberton 	.remove =	virtnet_remove,
17769f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
17770741bcb5SAmit Shah #ifdef CONFIG_PM
17780741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
17790741bcb5SAmit Shah 	.restore =	virtnet_restore,
17800741bcb5SAmit Shah #endif
1781296f96fcSRusty Russell };
1782296f96fcSRusty Russell 
1783b2a17029SRusty Russell module_virtio_driver(virtio_net_driver);
1784296f96fcSRusty Russell 
1785296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
1786296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
1787296f96fcSRusty Russell MODULE_LICENSE("GPL");
1788