xref: /openbmc/linux/drivers/net/virtio_net.c (revision fa9fac1725fd38dad414e3729c56c089a7188383)
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. */
395061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
405061de36SMichael Dalton #define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
415061de36SMichael Dalton                                 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
425061de36SMichael Dalton                                 L1_CACHE_BYTES))
433f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
44296f96fcSRusty Russell 
4566846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
462a41f71dSAlex Williamson 
473fa2a1dfSstephen hemminger struct virtnet_stats {
4883a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
4983a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
503fa2a1dfSstephen hemminger 	u64 tx_bytes;
513fa2a1dfSstephen hemminger 	u64 tx_packets;
523fa2a1dfSstephen hemminger 
533fa2a1dfSstephen hemminger 	u64 rx_bytes;
543fa2a1dfSstephen hemminger 	u64 rx_packets;
553fa2a1dfSstephen hemminger };
563fa2a1dfSstephen hemminger 
57e9d7417bSJason Wang /* Internal representation of a send virtqueue */
58e9d7417bSJason Wang struct send_queue {
59e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
60e9d7417bSJason Wang 	struct virtqueue *vq;
61e9d7417bSJason Wang 
62e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
63e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
64986a4f4dSJason Wang 
65986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
66986a4f4dSJason Wang 	char name[40];
67e9d7417bSJason Wang };
68e9d7417bSJason Wang 
69e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
70e9d7417bSJason Wang struct receive_queue {
71e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
72e9d7417bSJason Wang 	struct virtqueue *vq;
73e9d7417bSJason Wang 
74296f96fcSRusty Russell 	struct napi_struct napi;
75296f96fcSRusty Russell 
76296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
77296f96fcSRusty Russell 	unsigned int num, max;
78296f96fcSRusty Russell 
79e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
80e9d7417bSJason Wang 	struct page *pages;
81e9d7417bSJason Wang 
82e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
83e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
84986a4f4dSJason Wang 
85986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
86986a4f4dSJason Wang 	char name[40];
87e9d7417bSJason Wang };
88e9d7417bSJason Wang 
89e9d7417bSJason Wang struct virtnet_info {
90e9d7417bSJason Wang 	struct virtio_device *vdev;
91e9d7417bSJason Wang 	struct virtqueue *cvq;
92e9d7417bSJason Wang 	struct net_device *dev;
93986a4f4dSJason Wang 	struct send_queue *sq;
94986a4f4dSJason Wang 	struct receive_queue *rq;
95e9d7417bSJason Wang 	unsigned int status;
96e9d7417bSJason Wang 
97986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
98986a4f4dSJason Wang 	u16 max_queue_pairs;
99986a4f4dSJason Wang 
100986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
101986a4f4dSJason Wang 	u16 curr_queue_pairs;
102986a4f4dSJason Wang 
10397402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
10497402b96SHerbert Xu 	bool big_packets;
10597402b96SHerbert Xu 
1063f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1073f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1083f2c31d9SMark McLoughlin 
109986a4f4dSJason Wang 	/* Has control virtqueue */
110986a4f4dSJason Wang 	bool has_cvq;
111986a4f4dSJason Wang 
112e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
113e7428e95SMichael S. Tsirkin 	bool any_header_sg;
114e7428e95SMichael S. Tsirkin 
115586d17c5SJason Wang 	/* enable config space updates */
116586d17c5SJason Wang 	bool config_enable;
117586d17c5SJason Wang 
1183fa2a1dfSstephen hemminger 	/* Active statistics */
1193fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1203fa2a1dfSstephen hemminger 
1213161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1223161e453SRusty Russell 	struct delayed_work refill;
1233161e453SRusty Russell 
124586d17c5SJason Wang 	/* Work struct for config space updates */
125586d17c5SJason Wang 	struct work_struct config_work;
126586d17c5SJason Wang 
127586d17c5SJason Wang 	/* Lock for config space updates */
128586d17c5SJason Wang 	struct mutex config_lock;
129986a4f4dSJason Wang 
1302613af0eSMichael Dalton 	/* Page_frag for GFP_KERNEL packet buffer allocation when we run
1312613af0eSMichael Dalton 	 * low on memory.
1322613af0eSMichael Dalton 	 */
1332613af0eSMichael Dalton 	struct page_frag alloc_frag;
1342613af0eSMichael Dalton 
135986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
136986a4f4dSJason Wang 	bool affinity_hint_set;
13747be2479SWanlong Gao 
1388de4b2f3SWanlong Gao 	/* CPU hot plug notifier */
1398de4b2f3SWanlong Gao 	struct notifier_block nb;
140296f96fcSRusty Russell };
141296f96fcSRusty Russell 
142b3f24698SRusty Russell struct skb_vnet_hdr {
143b3f24698SRusty Russell 	union {
144b3f24698SRusty Russell 		struct virtio_net_hdr hdr;
145b3f24698SRusty Russell 		struct virtio_net_hdr_mrg_rxbuf mhdr;
146b3f24698SRusty Russell 	};
147b3f24698SRusty Russell };
148b3f24698SRusty Russell 
1499ab86bbcSShirley Ma struct padded_vnet_hdr {
1509ab86bbcSShirley Ma 	struct virtio_net_hdr hdr;
1519ab86bbcSShirley Ma 	/*
1529ab86bbcSShirley Ma 	 * virtio_net_hdr should be in a separated sg buffer because of a
1539ab86bbcSShirley Ma 	 * QEMU bug, and data sg buffer shares same page with this header sg.
1549ab86bbcSShirley Ma 	 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
1559ab86bbcSShirley Ma 	 */
1569ab86bbcSShirley Ma 	char padding[6];
1579ab86bbcSShirley Ma };
1589ab86bbcSShirley Ma 
159986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
160986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
161986a4f4dSJason Wang  */
162986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
163986a4f4dSJason Wang {
1649d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
165986a4f4dSJason Wang }
166986a4f4dSJason Wang 
167986a4f4dSJason Wang static int txq2vq(int txq)
168986a4f4dSJason Wang {
169986a4f4dSJason Wang 	return txq * 2 + 1;
170986a4f4dSJason Wang }
171986a4f4dSJason Wang 
172986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
173986a4f4dSJason Wang {
1749d0ca6edSRusty Russell 	return vq->index / 2;
175986a4f4dSJason Wang }
176986a4f4dSJason Wang 
177986a4f4dSJason Wang static int rxq2vq(int rxq)
178986a4f4dSJason Wang {
179986a4f4dSJason Wang 	return rxq * 2;
180986a4f4dSJason Wang }
181986a4f4dSJason Wang 
182b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
183296f96fcSRusty Russell {
184b3f24698SRusty Russell 	return (struct skb_vnet_hdr *)skb->cb;
185296f96fcSRusty Russell }
186296f96fcSRusty Russell 
1879ab86bbcSShirley Ma /*
1889ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
1899ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
1909ab86bbcSShirley Ma  */
191e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
192fb6813f4SRusty Russell {
1939ab86bbcSShirley Ma 	struct page *end;
1949ab86bbcSShirley Ma 
195e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
1969ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
197e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
198e9d7417bSJason Wang 	rq->pages = page;
199fb6813f4SRusty Russell }
200fb6813f4SRusty Russell 
201e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
202fb6813f4SRusty Russell {
203e9d7417bSJason Wang 	struct page *p = rq->pages;
204fb6813f4SRusty Russell 
2059ab86bbcSShirley Ma 	if (p) {
206e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2079ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2089ab86bbcSShirley Ma 		p->private = 0;
2099ab86bbcSShirley Ma 	} else
210fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
211fb6813f4SRusty Russell 	return p;
212fb6813f4SRusty Russell }
213fb6813f4SRusty Russell 
214e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
215296f96fcSRusty Russell {
216e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
217296f96fcSRusty Russell 
2182cb9c6baSRusty Russell 	/* Suppress further interrupts. */
219e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
22011a3a154SRusty Russell 
221363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
222986a4f4dSJason Wang 	netif_wake_subqueue(vi->dev, vq2txq(vq));
223296f96fcSRusty Russell }
224296f96fcSRusty Russell 
2253464645aSMike Waychison /* Called from bottom half context */
226e9d7417bSJason Wang static struct sk_buff *page_to_skb(struct receive_queue *rq,
2272613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
2282613af0eSMichael Dalton 				   unsigned int len, unsigned int truesize)
2299ab86bbcSShirley Ma {
230e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
2319ab86bbcSShirley Ma 	struct sk_buff *skb;
2329ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
2332613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
2349ab86bbcSShirley Ma 	char *p;
2359ab86bbcSShirley Ma 
2362613af0eSMichael Dalton 	p = page_address(page) + offset;
2379ab86bbcSShirley Ma 
2389ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
2399ab86bbcSShirley Ma 	skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
2409ab86bbcSShirley Ma 	if (unlikely(!skb))
2419ab86bbcSShirley Ma 		return NULL;
2429ab86bbcSShirley Ma 
2439ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
2449ab86bbcSShirley Ma 
2453f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs) {
2469ab86bbcSShirley Ma 		hdr_len = sizeof hdr->mhdr;
2472613af0eSMichael Dalton 		hdr_padded_len = sizeof hdr->mhdr;
2489ab86bbcSShirley Ma 	} else {
2499ab86bbcSShirley Ma 		hdr_len = sizeof hdr->hdr;
2502613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
2519ab86bbcSShirley Ma 	}
2523f2c31d9SMark McLoughlin 
2539ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
2543f2c31d9SMark McLoughlin 
2559ab86bbcSShirley Ma 	len -= hdr_len;
2562613af0eSMichael Dalton 	offset += hdr_padded_len;
2572613af0eSMichael Dalton 	p += hdr_padded_len;
2583f2c31d9SMark McLoughlin 
2593f2c31d9SMark McLoughlin 	copy = len;
2603f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
2613f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
2623f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
2633f2c31d9SMark McLoughlin 
2643f2c31d9SMark McLoughlin 	len -= copy;
2659ab86bbcSShirley Ma 	offset += copy;
2663f2c31d9SMark McLoughlin 
2672613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
2682613af0eSMichael Dalton 		if (len)
2692613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
2702613af0eSMichael Dalton 		else
2712613af0eSMichael Dalton 			put_page(page);
2722613af0eSMichael Dalton 		return skb;
2732613af0eSMichael Dalton 	}
2742613af0eSMichael Dalton 
275e878d78bSSasha Levin 	/*
276e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
277e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
278e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
279e878d78bSSasha Levin 	 * the case of a broken device.
280e878d78bSSasha Levin 	 */
281e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
282be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
283e878d78bSSasha Levin 		dev_kfree_skb(skb);
284e878d78bSSasha Levin 		return NULL;
285e878d78bSSasha Levin 	}
2862613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
2879ab86bbcSShirley Ma 	while (len) {
2882613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
2892613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
2902613af0eSMichael Dalton 				frag_size, truesize);
2912613af0eSMichael Dalton 		len -= frag_size;
2929ab86bbcSShirley Ma 		page = (struct page *)page->private;
2939ab86bbcSShirley Ma 		offset = 0;
2943f2c31d9SMark McLoughlin 	}
2953f2c31d9SMark McLoughlin 
2969ab86bbcSShirley Ma 	if (page)
297e9d7417bSJason Wang 		give_pages(rq, page);
2983f2c31d9SMark McLoughlin 
2999ab86bbcSShirley Ma 	return skb;
3009ab86bbcSShirley Ma }
3019ab86bbcSShirley Ma 
302f121159dSMichael S. Tsirkin static struct sk_buff *receive_small(void *buf, unsigned int len)
303f121159dSMichael S. Tsirkin {
304f121159dSMichael S. Tsirkin 	struct sk_buff * skb = buf;
305f121159dSMichael S. Tsirkin 
306f121159dSMichael S. Tsirkin 	len -= sizeof(struct virtio_net_hdr);
307f121159dSMichael S. Tsirkin 	skb_trim(skb, len);
308f121159dSMichael S. Tsirkin 
309f121159dSMichael S. Tsirkin 	return skb;
310f121159dSMichael S. Tsirkin }
311f121159dSMichael S. Tsirkin 
312f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev,
313f121159dSMichael S. Tsirkin 				   struct receive_queue *rq,
314f121159dSMichael S. Tsirkin 				   void *buf,
315f121159dSMichael S. Tsirkin 				   unsigned int len)
316f121159dSMichael S. Tsirkin {
317f121159dSMichael S. Tsirkin 	struct page *page = buf;
318f121159dSMichael S. Tsirkin 	struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
319f121159dSMichael S. Tsirkin 
320f121159dSMichael S. Tsirkin 	if (unlikely(!skb))
321f121159dSMichael S. Tsirkin 		goto err;
322f121159dSMichael S. Tsirkin 
323f121159dSMichael S. Tsirkin 	return skb;
324f121159dSMichael S. Tsirkin 
325f121159dSMichael S. Tsirkin err:
326f121159dSMichael S. Tsirkin 	dev->stats.rx_dropped++;
327f121159dSMichael S. Tsirkin 	give_pages(rq, page);
328f121159dSMichael S. Tsirkin 	return NULL;
329f121159dSMichael S. Tsirkin }
330f121159dSMichael S. Tsirkin 
3318fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
3328fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
3338fc3b9e9SMichael S. Tsirkin 					 void *buf,
3348fc3b9e9SMichael S. Tsirkin 					 unsigned int len)
3359ab86bbcSShirley Ma {
3368fc3b9e9SMichael S. Tsirkin 	struct skb_vnet_hdr *hdr = buf;
3378fc3b9e9SMichael S. Tsirkin 	int num_buf = hdr->mhdr.num_buffers;
3388fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
3398fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
3408fc3b9e9SMichael S. Tsirkin 	struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
3418fc3b9e9SMichael S. Tsirkin 					       MERGE_BUFFER_LEN);
3422613af0eSMichael Dalton 	struct sk_buff *curr_skb = head_skb;
3439ab86bbcSShirley Ma 
3448fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
3458fc3b9e9SMichael S. Tsirkin 		goto err_skb;
3468fc3b9e9SMichael S. Tsirkin 
3479ab86bbcSShirley Ma 	while (--num_buf) {
3488fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
3498fc3b9e9SMichael S. Tsirkin 
3502613af0eSMichael Dalton 		buf = virtqueue_get_buf(rq->vq, &len);
3512613af0eSMichael Dalton 		if (unlikely(!buf)) {
3528fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
3538fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf, hdr->mhdr.num_buffers);
3548fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
3558fc3b9e9SMichael S. Tsirkin 			goto err_buf;
3563f2c31d9SMark McLoughlin 		}
3575061de36SMichael Dalton 		if (unlikely(len > MERGE_BUFFER_LEN)) {
3582613af0eSMichael Dalton 			pr_debug("%s: rx error: merge buffer too long\n",
3598fc3b9e9SMichael S. Tsirkin 				 dev->name);
3605061de36SMichael Dalton 			len = MERGE_BUFFER_LEN;
3612613af0eSMichael Dalton 		}
3628fc3b9e9SMichael S. Tsirkin 
3638fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
3648fc3b9e9SMichael S. Tsirkin 		--rq->num;
3658fc3b9e9SMichael S. Tsirkin 
3668fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
3672613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
3682613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
3698fc3b9e9SMichael S. Tsirkin 
3708fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
3718fc3b9e9SMichael S. Tsirkin 				goto err_skb;
3722613af0eSMichael Dalton 			if (curr_skb == head_skb)
3732613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
3742613af0eSMichael Dalton 			else
3752613af0eSMichael Dalton 				curr_skb->next = nskb;
3762613af0eSMichael Dalton 			curr_skb = nskb;
3772613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
3782613af0eSMichael Dalton 			num_skb_frags = 0;
3792613af0eSMichael Dalton 		}
3802613af0eSMichael Dalton 		if (curr_skb != head_skb) {
3812613af0eSMichael Dalton 			head_skb->data_len += len;
3822613af0eSMichael Dalton 			head_skb->len += len;
3835061de36SMichael Dalton 			head_skb->truesize += MERGE_BUFFER_LEN;
3842613af0eSMichael Dalton 		}
3858fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
386ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
387ba275241SJason Wang 			put_page(page);
388ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
3895061de36SMichael Dalton 					     len, MERGE_BUFFER_LEN);
390ba275241SJason Wang 		} else {
3912613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
3925061de36SMichael Dalton 					offset, len, MERGE_BUFFER_LEN);
393ba275241SJason Wang 		}
3948fc3b9e9SMichael S. Tsirkin 	}
3958fc3b9e9SMichael S. Tsirkin 
3968fc3b9e9SMichael S. Tsirkin 	return head_skb;
3978fc3b9e9SMichael S. Tsirkin 
3988fc3b9e9SMichael S. Tsirkin err_skb:
3998fc3b9e9SMichael S. Tsirkin 	put_page(page);
4008fc3b9e9SMichael S. Tsirkin 	while (--num_buf) {
4018fc3b9e9SMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &len);
4028fc3b9e9SMichael S. Tsirkin 		if (unlikely(!buf)) {
4038fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
4048fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
4058fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
4068fc3b9e9SMichael S. Tsirkin 			break;
4078fc3b9e9SMichael S. Tsirkin 		}
4088fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
4098fc3b9e9SMichael S. Tsirkin 		put_page(page);
410e9d7417bSJason Wang 		--rq->num;
4113f2c31d9SMark McLoughlin 	}
4128fc3b9e9SMichael S. Tsirkin err_buf:
4138fc3b9e9SMichael S. Tsirkin 	dev->stats.rx_dropped++;
4148fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
4158fc3b9e9SMichael S. Tsirkin 	return NULL;
4169ab86bbcSShirley Ma }
4179ab86bbcSShirley Ma 
418e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
4199ab86bbcSShirley Ma {
420e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
421e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
42258472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
4239ab86bbcSShirley Ma 	struct sk_buff *skb;
4249ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
4259ab86bbcSShirley Ma 
4269ab86bbcSShirley Ma 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
4279ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
4289ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
4292613af0eSMichael Dalton 		if (vi->big_packets)
430e9d7417bSJason Wang 			give_pages(rq, buf);
4312613af0eSMichael Dalton 		else if (vi->mergeable_rx_bufs)
4322613af0eSMichael Dalton 			put_page(virt_to_head_page(buf));
4339ab86bbcSShirley Ma 		else
4349ab86bbcSShirley Ma 			dev_kfree_skb(buf);
4359ab86bbcSShirley Ma 		return;
4369ab86bbcSShirley Ma 	}
4379ab86bbcSShirley Ma 
438f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
4398fc3b9e9SMichael S. Tsirkin 		skb = receive_mergeable(dev, rq, buf, len);
440f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
441f121159dSMichael S. Tsirkin 		skb = receive_big(dev, rq, buf, len);
442f121159dSMichael S. Tsirkin 	else
443f121159dSMichael S. Tsirkin 		skb = receive_small(buf, len);
444f121159dSMichael S. Tsirkin 
4458fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
4462613af0eSMichael Dalton 		return;
4473f2c31d9SMark McLoughlin 
4489ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
4493fa2a1dfSstephen hemminger 
45083a27052SEric Dumazet 	u64_stats_update_begin(&stats->rx_syncp);
4513fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
4523fa2a1dfSstephen hemminger 	stats->rx_packets++;
45383a27052SEric Dumazet 	u64_stats_update_end(&stats->rx_syncp);
454296f96fcSRusty Russell 
455b3f24698SRusty Russell 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
456296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
457b3f24698SRusty Russell 		if (!skb_partial_csum_set(skb,
458b3f24698SRusty Russell 					  hdr->hdr.csum_start,
459b3f24698SRusty Russell 					  hdr->hdr.csum_offset))
460296f96fcSRusty Russell 			goto frame_err;
46110a8d94aSJason Wang 	} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
46210a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
463296f96fcSRusty Russell 	}
464296f96fcSRusty Russell 
46523cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
46623cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
46723cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
46823cde76dSMark McLoughlin 
469b3f24698SRusty Russell 	if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
470296f96fcSRusty Russell 		pr_debug("GSO!\n");
471b3f24698SRusty Russell 		switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
472296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
473c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
474296f96fcSRusty Russell 			break;
475296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
476c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
477296f96fcSRusty Russell 			break;
478296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
479c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
480296f96fcSRusty Russell 			break;
481296f96fcSRusty Russell 		default:
482be443899SAmerigo Wang 			net_warn_ratelimited("%s: bad gso type %u.\n",
483b3f24698SRusty Russell 					     dev->name, hdr->hdr.gso_type);
484296f96fcSRusty Russell 			goto frame_err;
485296f96fcSRusty Russell 		}
486296f96fcSRusty Russell 
487b3f24698SRusty Russell 		if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
488c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
48934a48579SRusty Russell 
490b3f24698SRusty Russell 		skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
491296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
492be443899SAmerigo Wang 			net_warn_ratelimited("%s: zero gso size.\n", dev->name);
493296f96fcSRusty Russell 			goto frame_err;
494296f96fcSRusty Russell 		}
495296f96fcSRusty Russell 
496296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
497296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
498296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
499296f96fcSRusty Russell 	}
500296f96fcSRusty Russell 
501296f96fcSRusty Russell 	netif_receive_skb(skb);
502296f96fcSRusty Russell 	return;
503296f96fcSRusty Russell 
504296f96fcSRusty Russell frame_err:
505296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
506296f96fcSRusty Russell 	dev_kfree_skb(skb);
507296f96fcSRusty Russell }
508296f96fcSRusty Russell 
509e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
510296f96fcSRusty Russell {
511e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
512296f96fcSRusty Russell 	struct sk_buff *skb;
513b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr;
5149ab86bbcSShirley Ma 	int err;
5153f2c31d9SMark McLoughlin 
5165061de36SMichael Dalton 	skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
5179ab86bbcSShirley Ma 	if (unlikely(!skb))
5189ab86bbcSShirley Ma 		return -ENOMEM;
519296f96fcSRusty Russell 
5205061de36SMichael Dalton 	skb_put(skb, GOOD_PACKET_LEN);
5213f2c31d9SMark McLoughlin 
5223f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
523e9d7417bSJason Wang 	sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
52497402b96SHerbert Xu 
525e9d7417bSJason Wang 	skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
52697402b96SHerbert Xu 
5279dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
5289ab86bbcSShirley Ma 	if (err < 0)
5299ab86bbcSShirley Ma 		dev_kfree_skb(skb);
53097402b96SHerbert Xu 
5319ab86bbcSShirley Ma 	return err;
53297402b96SHerbert Xu }
53397402b96SHerbert Xu 
534e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
5359ab86bbcSShirley Ma {
5369ab86bbcSShirley Ma 	struct page *first, *list = NULL;
5379ab86bbcSShirley Ma 	char *p;
5389ab86bbcSShirley Ma 	int i, err, offset;
539296f96fcSRusty Russell 
540e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
5419ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
542e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
5439ab86bbcSShirley Ma 		if (!first) {
5449ab86bbcSShirley Ma 			if (list)
545e9d7417bSJason Wang 				give_pages(rq, list);
5469ab86bbcSShirley Ma 			return -ENOMEM;
547296f96fcSRusty Russell 		}
548e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
5499ab86bbcSShirley Ma 
5509ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
5519ab86bbcSShirley Ma 		first->private = (unsigned long)list;
5529ab86bbcSShirley Ma 		list = first;
5539ab86bbcSShirley Ma 	}
5549ab86bbcSShirley Ma 
555e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
5569ab86bbcSShirley Ma 	if (!first) {
557e9d7417bSJason Wang 		give_pages(rq, list);
5589ab86bbcSShirley Ma 		return -ENOMEM;
5599ab86bbcSShirley Ma 	}
5609ab86bbcSShirley Ma 	p = page_address(first);
5619ab86bbcSShirley Ma 
562e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
563e9d7417bSJason Wang 	/* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
564e9d7417bSJason Wang 	sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
5659ab86bbcSShirley Ma 
566e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
5679ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
568e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
5699ab86bbcSShirley Ma 
5709ab86bbcSShirley Ma 	/* chain first in list head */
5719ab86bbcSShirley Ma 	first->private = (unsigned long)list;
5729dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
573aa989f5eSMichael S. Tsirkin 				  first, gfp);
5749ab86bbcSShirley Ma 	if (err < 0)
575e9d7417bSJason Wang 		give_pages(rq, first);
5769ab86bbcSShirley Ma 
5779ab86bbcSShirley Ma 	return err;
5789ab86bbcSShirley Ma }
5799ab86bbcSShirley Ma 
580e9d7417bSJason Wang static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
5819ab86bbcSShirley Ma {
5822613af0eSMichael Dalton 	struct virtnet_info *vi = rq->vq->vdev->priv;
5832613af0eSMichael Dalton 	char *buf = NULL;
5849ab86bbcSShirley Ma 	int err;
5859ab86bbcSShirley Ma 
5862613af0eSMichael Dalton 	if (gfp & __GFP_WAIT) {
5875061de36SMichael Dalton 		if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
5882613af0eSMichael Dalton 					 gfp)) {
5892613af0eSMichael Dalton 			buf = (char *)page_address(vi->alloc_frag.page) +
5902613af0eSMichael Dalton 			      vi->alloc_frag.offset;
5912613af0eSMichael Dalton 			get_page(vi->alloc_frag.page);
5925061de36SMichael Dalton 			vi->alloc_frag.offset += MERGE_BUFFER_LEN;
5932613af0eSMichael Dalton 		}
5942613af0eSMichael Dalton 	} else {
5955061de36SMichael Dalton 		buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
5962613af0eSMichael Dalton 	}
5972613af0eSMichael Dalton 	if (!buf)
5989ab86bbcSShirley Ma 		return -ENOMEM;
5999ab86bbcSShirley Ma 
6005061de36SMichael Dalton 	sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
6012613af0eSMichael Dalton 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
6029ab86bbcSShirley Ma 	if (err < 0)
6032613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
6049ab86bbcSShirley Ma 
6059ab86bbcSShirley Ma 	return err;
606296f96fcSRusty Russell }
607296f96fcSRusty Russell 
608b2baed69SRusty Russell /*
609b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
610b2baed69SRusty Russell  *
611b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
612b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
613b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
614b2baed69SRusty Russell  */
615e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
6163f2c31d9SMark McLoughlin {
617e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
6183f2c31d9SMark McLoughlin 	int err;
6191788f495SMichael S. Tsirkin 	bool oom;
6203f2c31d9SMark McLoughlin 
6210aea51c3SAmit Shah 	do {
6229ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
623e9d7417bSJason Wang 			err = add_recvbuf_mergeable(rq, gfp);
6249ab86bbcSShirley Ma 		else if (vi->big_packets)
625e9d7417bSJason Wang 			err = add_recvbuf_big(rq, gfp);
6269ab86bbcSShirley Ma 		else
627e9d7417bSJason Wang 			err = add_recvbuf_small(rq, gfp);
6283f2c31d9SMark McLoughlin 
6291788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
6309ed4cb07SRusty Russell 		if (err)
6313f2c31d9SMark McLoughlin 			break;
632e9d7417bSJason Wang 		++rq->num;
633b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
634e9d7417bSJason Wang 	if (unlikely(rq->num > rq->max))
635e9d7417bSJason Wang 		rq->max = rq->num;
63667975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(rq->vq)))
63767975901SHeinz Graalfs 		return false;
6383161e453SRusty Russell 	return !oom;
6393f2c31d9SMark McLoughlin }
6403f2c31d9SMark McLoughlin 
64118445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
642296f96fcSRusty Russell {
643296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
644986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
645e9d7417bSJason Wang 
64618445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
647e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
6481915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
649e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
65018445c4dSRusty Russell 	}
651296f96fcSRusty Russell }
652296f96fcSRusty Russell 
653e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
6543e9d08ecSBruce Rogers {
655e9d7417bSJason Wang 	napi_enable(&rq->napi);
6563e9d08ecSBruce Rogers 
6573e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
6583e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
6593e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
6603e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
661e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
662e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
663ec13ee80SMichael S. Tsirkin 		local_bh_disable();
664e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
665ec13ee80SMichael S. Tsirkin 		local_bh_enable();
6663e9d08ecSBruce Rogers 	}
6673e9d08ecSBruce Rogers }
6683e9d08ecSBruce Rogers 
6693161e453SRusty Russell static void refill_work(struct work_struct *work)
6703161e453SRusty Russell {
671e9d7417bSJason Wang 	struct virtnet_info *vi =
672e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
6733161e453SRusty Russell 	bool still_empty;
674986a4f4dSJason Wang 	int i;
6753161e453SRusty Russell 
67655257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
677986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
678986a4f4dSJason Wang 
679986a4f4dSJason Wang 		napi_disable(&rq->napi);
680986a4f4dSJason Wang 		still_empty = !try_fill_recv(rq, GFP_KERNEL);
681986a4f4dSJason Wang 		virtnet_napi_enable(rq);
6823161e453SRusty Russell 
6833161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
684986a4f4dSJason Wang 		 * we will *never* try to fill again.
685986a4f4dSJason Wang 		 */
6863161e453SRusty Russell 		if (still_empty)
6873b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
6883161e453SRusty Russell 	}
689986a4f4dSJason Wang }
6903161e453SRusty Russell 
691296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
692296f96fcSRusty Russell {
693e9d7417bSJason Wang 	struct receive_queue *rq =
694e9d7417bSJason Wang 		container_of(napi, struct receive_queue, napi);
695e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
6969ab86bbcSShirley Ma 	void *buf;
697cbdadbbfSMichael S. Tsirkin 	unsigned int r, len, received = 0;
698296f96fcSRusty Russell 
699296f96fcSRusty Russell again:
700296f96fcSRusty Russell 	while (received < budget &&
701e9d7417bSJason Wang 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
702e9d7417bSJason Wang 		receive_buf(rq, buf, len);
703e9d7417bSJason Wang 		--rq->num;
704296f96fcSRusty Russell 		received++;
705296f96fcSRusty Russell 	}
706296f96fcSRusty Russell 
707e9d7417bSJason Wang 	if (rq->num < rq->max / 2) {
708e9d7417bSJason Wang 		if (!try_fill_recv(rq, GFP_ATOMIC))
7093b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
7103161e453SRusty Russell 	}
711296f96fcSRusty Russell 
7128329d98eSRusty Russell 	/* Out of packets? */
7138329d98eSRusty Russell 	if (received < budget) {
714cbdadbbfSMichael S. Tsirkin 		r = virtqueue_enable_cb_prepare(rq->vq);
715288379f0SBen Hutchings 		napi_complete(napi);
716cbdadbbfSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(rq->vq, r)) &&
7178e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
718e9d7417bSJason Wang 			virtqueue_disable_cb(rq->vq);
719288379f0SBen Hutchings 			__napi_schedule(napi);
720296f96fcSRusty Russell 			goto again;
721296f96fcSRusty Russell 		}
7224265f161SChristian Borntraeger 	}
723296f96fcSRusty Russell 
724296f96fcSRusty Russell 	return received;
725296f96fcSRusty Russell }
726296f96fcSRusty Russell 
727986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
728986a4f4dSJason Wang {
729986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
730986a4f4dSJason Wang 	int i;
731986a4f4dSJason Wang 
732e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
733e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
734986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
735986a4f4dSJason Wang 			if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
736986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
737986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
738986a4f4dSJason Wang 	}
739986a4f4dSJason Wang 
740986a4f4dSJason Wang 	return 0;
741986a4f4dSJason Wang }
742986a4f4dSJason Wang 
743b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
744296f96fcSRusty Russell {
745296f96fcSRusty Russell 	struct sk_buff *skb;
7466ee57bccSMichael S. Tsirkin 	unsigned int len;
747e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
74858472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
749296f96fcSRusty Russell 
750e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
751296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
7523fa2a1dfSstephen hemminger 
75383a27052SEric Dumazet 		u64_stats_update_begin(&stats->tx_syncp);
7543fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
7553fa2a1dfSstephen hemminger 		stats->tx_packets++;
75683a27052SEric Dumazet 		u64_stats_update_end(&stats->tx_syncp);
7573fa2a1dfSstephen hemminger 
758ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
759296f96fcSRusty Russell 	}
760296f96fcSRusty Russell }
761296f96fcSRusty Russell 
762e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
763296f96fcSRusty Russell {
764e7428e95SMichael S. Tsirkin 	struct skb_vnet_hdr *hdr;
765296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
766e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
7677bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
768e7428e95SMichael S. Tsirkin 	unsigned hdr_len;
769e7428e95SMichael S. Tsirkin 	bool can_push;
770296f96fcSRusty Russell 
771e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
772e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
773e7428e95SMichael S. Tsirkin 		hdr_len = sizeof hdr->mhdr;
774e7428e95SMichael S. Tsirkin 	else
775e7428e95SMichael S. Tsirkin 		hdr_len = sizeof hdr->hdr;
776e7428e95SMichael S. Tsirkin 
777e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
778e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
779e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
780e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
781e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
782e7428e95SMichael S. Tsirkin 	if (can_push)
783e7428e95SMichael S. Tsirkin 		hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
784e7428e95SMichael S. Tsirkin 	else
785e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
786296f96fcSRusty Russell 
787296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
788b3f24698SRusty Russell 		hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
78955508d60SMichał Mirosław 		hdr->hdr.csum_start = skb_checksum_start_offset(skb);
790b3f24698SRusty Russell 		hdr->hdr.csum_offset = skb->csum_offset;
791296f96fcSRusty Russell 	} else {
792b3f24698SRusty Russell 		hdr->hdr.flags = 0;
793b3f24698SRusty Russell 		hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
794296f96fcSRusty Russell 	}
795296f96fcSRusty Russell 
796296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
797b3f24698SRusty Russell 		hdr->hdr.hdr_len = skb_headlen(skb);
798b3f24698SRusty Russell 		hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
79934a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
800b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
801296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
802b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
803296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
804b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
805296f96fcSRusty Russell 		else
806296f96fcSRusty Russell 			BUG();
80734a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
808b3f24698SRusty Russell 			hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
809296f96fcSRusty Russell 	} else {
810b3f24698SRusty Russell 		hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
811b3f24698SRusty Russell 		hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
812296f96fcSRusty Russell 	}
813296f96fcSRusty Russell 
814e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
815b3f24698SRusty Russell 		hdr->mhdr.num_buffers = 0;
8163f2c31d9SMark McLoughlin 
817e7428e95SMichael S. Tsirkin 	if (can_push) {
818e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
819e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
820e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
821e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
822e7428e95SMichael S. Tsirkin 	} else {
823e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
824b7dfde95SLinus Torvalds 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
825e7428e95SMichael S. Tsirkin 	}
8269dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
82711a3a154SRusty Russell }
82811a3a154SRusty Russell 
829424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
83099ffc696SRusty Russell {
83199ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
832986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
833986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
8349ed4cb07SRusty Russell 	int err;
8352cb9c6baSRusty Russell 
8362cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
837e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
83899ffc696SRusty Russell 
83903f191baSMichael S. Tsirkin 	/* Try to transmit */
840b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
84199ffc696SRusty Russell 
8429ed4cb07SRusty Russell 	/* This should not happen! */
84367975901SHeinz Graalfs 	if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
84458eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
8452e57b79cSRick Jones 		if (net_ratelimit())
84658eba97dSRusty Russell 			dev_warn(&dev->dev,
847b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
84858eba97dSRusty Russell 		dev->stats.tx_dropped++;
84958eba97dSRusty Russell 		kfree_skb(skb);
85058eba97dSRusty Russell 		return NETDEV_TX_OK;
851296f96fcSRusty Russell 	}
85203f191baSMichael S. Tsirkin 
85348925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
85448925e37SRusty Russell 	skb_orphan(skb);
85548925e37SRusty Russell 	nf_reset(skb);
85648925e37SRusty Russell 
85748925e37SRusty Russell 	/* Apparently nice girls don't return TX_BUSY; stop the queue
85848925e37SRusty Russell 	 * before it gets out of hand.  Naturally, this wastes entries. */
859b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
860986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
861e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
86248925e37SRusty Russell 			/* More just got used, free them then recheck. */
863b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
864b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
865986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
866e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
86748925e37SRusty Russell 			}
86848925e37SRusty Russell 		}
86948925e37SRusty Russell 	}
87048925e37SRusty Russell 
87148925e37SRusty Russell 	return NETDEV_TX_OK;
87248925e37SRusty Russell }
87348925e37SRusty Russell 
87440cbfc37SAmos Kong /*
87540cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
87640cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
87740cbfc37SAmos Kong  * never fail unless improperly formated.
87840cbfc37SAmos Kong  */
87940cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
880f7bc9594SRusty Russell 				 struct scatterlist *out,
881f7bc9594SRusty Russell 				 struct scatterlist *in)
88240cbfc37SAmos Kong {
883f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
88440cbfc37SAmos Kong 	struct virtio_net_ctrl_hdr ctrl;
88540cbfc37SAmos Kong 	virtio_net_ctrl_ack status = ~0;
886f7bc9594SRusty Russell 	unsigned out_num = 0, in_num = 0, tmp;
88740cbfc37SAmos Kong 
88840cbfc37SAmos Kong 	/* Caller should know better */
889f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
89040cbfc37SAmos Kong 
89140cbfc37SAmos Kong 	ctrl.class = class;
89240cbfc37SAmos Kong 	ctrl.cmd = cmd;
893f7bc9594SRusty Russell 	/* Add header */
894f7bc9594SRusty Russell 	sg_init_one(&hdr, &ctrl, sizeof(ctrl));
895f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
89640cbfc37SAmos Kong 
897f7bc9594SRusty Russell 	if (out)
898f7bc9594SRusty Russell 		sgs[out_num++] = out;
899f7bc9594SRusty Russell 	if (in)
900f7bc9594SRusty Russell 		sgs[out_num + in_num++] = in;
90140cbfc37SAmos Kong 
902f7bc9594SRusty Russell 	/* Add return status. */
903f7bc9594SRusty Russell 	sg_init_one(&stat, &status, sizeof(status));
904f7bc9594SRusty Russell 	sgs[out_num + in_num++] = &stat;
90540cbfc37SAmos Kong 
906f7bc9594SRusty Russell 	BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
907f7bc9594SRusty Russell 	BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
908f7bc9594SRusty Russell 	       < 0);
90940cbfc37SAmos Kong 
91067975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
91167975901SHeinz Graalfs 		return status == VIRTIO_NET_OK;
91240cbfc37SAmos Kong 
91340cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
91440cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
91540cbfc37SAmos Kong 	 */
916047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
917047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
91840cbfc37SAmos Kong 		cpu_relax();
91940cbfc37SAmos Kong 
92040cbfc37SAmos Kong 	return status == VIRTIO_NET_OK;
92140cbfc37SAmos Kong }
92240cbfc37SAmos Kong 
9239c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
9249c46f6d4SAlex Williamson {
9259c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
9269c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
927f2f2c8b4SJiri Pirko 	int ret;
9287e58d5aeSAmos Kong 	struct sockaddr *addr = p;
9297e58d5aeSAmos Kong 	struct scatterlist sg;
9309c46f6d4SAlex Williamson 
9317e58d5aeSAmos Kong 	ret = eth_prepare_mac_addr_change(dev, p);
932f2f2c8b4SJiri Pirko 	if (ret)
933f2f2c8b4SJiri Pirko 		return ret;
9349c46f6d4SAlex Williamson 
9357e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
9367e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
9377e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
9387e58d5aeSAmos Kong 					  VIRTIO_NET_CTRL_MAC_ADDR_SET,
939f7bc9594SRusty Russell 					  &sg, NULL)) {
9407e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
9417e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
9427e58d5aeSAmos Kong 			return -EINVAL;
9437e58d5aeSAmos Kong 		}
9447e58d5aeSAmos Kong 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
945855e0c52SRusty Russell 		unsigned int i;
946855e0c52SRusty Russell 
947855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
948855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
949855e0c52SRusty Russell 			virtio_cwrite8(vdev,
950855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
951855e0c52SRusty Russell 				       i, addr->sa_data[i]);
9527e58d5aeSAmos Kong 	}
9537e58d5aeSAmos Kong 
9547e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
9559c46f6d4SAlex Williamson 
9569c46f6d4SAlex Williamson 	return 0;
9579c46f6d4SAlex Williamson }
9589c46f6d4SAlex Williamson 
9593fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
9603fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
9613fa2a1dfSstephen hemminger {
9623fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
9633fa2a1dfSstephen hemminger 	int cpu;
9643fa2a1dfSstephen hemminger 	unsigned int start;
9653fa2a1dfSstephen hemminger 
9663fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
96758472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
9683fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
9693fa2a1dfSstephen hemminger 
9703fa2a1dfSstephen hemminger 		do {
971e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
9723fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
9733fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
974e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
97583a27052SEric Dumazet 
97683a27052SEric Dumazet 		do {
977e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
9783fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
9793fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
980e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
9813fa2a1dfSstephen hemminger 
9823fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
9833fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
9843fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
9853fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
9863fa2a1dfSstephen hemminger 	}
9873fa2a1dfSstephen hemminger 
9883fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
989021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
9903fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
9913fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
9923fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
9933fa2a1dfSstephen hemminger 
9943fa2a1dfSstephen hemminger 	return tot;
9953fa2a1dfSstephen hemminger }
9963fa2a1dfSstephen hemminger 
997da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
998da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
999da74e89dSAmit Shah {
1000da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
1001986a4f4dSJason Wang 	int i;
1002da74e89dSAmit Shah 
1003986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
1004986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
1005da74e89dSAmit Shah }
1006da74e89dSAmit Shah #endif
1007da74e89dSAmit Shah 
1008586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
1009586d17c5SJason Wang {
1010586d17c5SJason Wang 	rtnl_lock();
1011586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1012f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
1013586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1014586d17c5SJason Wang 	rtnl_unlock();
1015586d17c5SJason Wang }
1016586d17c5SJason Wang 
1017986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1018986a4f4dSJason Wang {
1019986a4f4dSJason Wang 	struct scatterlist sg;
1020986a4f4dSJason Wang 	struct virtio_net_ctrl_mq s;
1021986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
1022986a4f4dSJason Wang 
1023986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1024986a4f4dSJason Wang 		return 0;
1025986a4f4dSJason Wang 
1026986a4f4dSJason Wang 	s.virtqueue_pairs = queue_pairs;
1027986a4f4dSJason Wang 	sg_init_one(&sg, &s, sizeof(s));
1028986a4f4dSJason Wang 
1029986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1030f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
1031986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1032986a4f4dSJason Wang 			 queue_pairs);
1033986a4f4dSJason Wang 		return -EINVAL;
103455257d72SSasha Levin 	} else {
1035986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
103635ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
103735ed159bSJason Wang 		if (dev->flags & IFF_UP)
10389b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
103955257d72SSasha Levin 	}
1040986a4f4dSJason Wang 
1041986a4f4dSJason Wang 	return 0;
1042986a4f4dSJason Wang }
1043986a4f4dSJason Wang 
1044296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
1045296f96fcSRusty Russell {
1046296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1047986a4f4dSJason Wang 	int i;
1048296f96fcSRusty Russell 
1049b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1050b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1051986a4f4dSJason Wang 
1052986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
1053986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
1054296f96fcSRusty Russell 
1055296f96fcSRusty Russell 	return 0;
1056296f96fcSRusty Russell }
1057296f96fcSRusty Russell 
10582af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
10592af7698eSAlex Williamson {
10602af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1061f565a7c2SAlex Williamson 	struct scatterlist sg[2];
10622af7698eSAlex Williamson 	u8 promisc, allmulti;
1063f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1064ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
106532e7bfc4SJiri Pirko 	int uc_count;
10664cd24eafSJiri Pirko 	int mc_count;
1067f565a7c2SAlex Williamson 	void *buf;
1068f565a7c2SAlex Williamson 	int i;
10692af7698eSAlex Williamson 
10702af7698eSAlex Williamson 	/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
10712af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
10722af7698eSAlex Williamson 		return;
10732af7698eSAlex Williamson 
1074f565a7c2SAlex Williamson 	promisc = ((dev->flags & IFF_PROMISC) != 0);
1075f565a7c2SAlex Williamson 	allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
10762af7698eSAlex Williamson 
107723e258e1SAlex Williamson 	sg_init_one(sg, &promisc, sizeof(promisc));
10782af7698eSAlex Williamson 
10792af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
10802af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_PROMISC,
1081f7bc9594SRusty Russell 				  sg, NULL))
10822af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
10832af7698eSAlex Williamson 			 promisc ? "en" : "dis");
10842af7698eSAlex Williamson 
108523e258e1SAlex Williamson 	sg_init_one(sg, &allmulti, sizeof(allmulti));
10862af7698eSAlex Williamson 
10872af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
10882af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_ALLMULTI,
1089f7bc9594SRusty Russell 				  sg, NULL))
10902af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
10912af7698eSAlex Williamson 			 allmulti ? "en" : "dis");
1092f565a7c2SAlex Williamson 
109332e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
10944cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1095f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
10964cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1097f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
10984cd24eafSJiri Pirko 	mac_data = buf;
1099e68ed8f0SJoe Perches 	if (!buf)
1100f565a7c2SAlex Williamson 		return;
1101f565a7c2SAlex Williamson 
110223e258e1SAlex Williamson 	sg_init_table(sg, 2);
110323e258e1SAlex Williamson 
1104f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
110532e7bfc4SJiri Pirko 	mac_data->entries = uc_count;
1106ccffad25SJiri Pirko 	i = 0;
110732e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1108ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1109f565a7c2SAlex Williamson 
1110f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
111132e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1112f565a7c2SAlex Williamson 
1113f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
111432e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1115f565a7c2SAlex Williamson 
11164cd24eafSJiri Pirko 	mac_data->entries = mc_count;
1117567ec874SJiri Pirko 	i = 0;
111822bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
111922bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1120f565a7c2SAlex Williamson 
1121f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
11224cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1123f565a7c2SAlex Williamson 
1124f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1125f565a7c2SAlex Williamson 				  VIRTIO_NET_CTRL_MAC_TABLE_SET,
1126f7bc9594SRusty Russell 				  sg, NULL))
112799e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1128f565a7c2SAlex Williamson 
1129f565a7c2SAlex Williamson 	kfree(buf);
11302af7698eSAlex Williamson }
11312af7698eSAlex Williamson 
113280d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
113380d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
11340bde9569SAlex Williamson {
11350bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11360bde9569SAlex Williamson 	struct scatterlist sg;
11370bde9569SAlex Williamson 
113823e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
11390bde9569SAlex Williamson 
11400bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1141f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
11420bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
11438e586137SJiri Pirko 	return 0;
11440bde9569SAlex Williamson }
11450bde9569SAlex Williamson 
114680d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
114780d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
11480bde9569SAlex Williamson {
11490bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11500bde9569SAlex Williamson 	struct scatterlist sg;
11510bde9569SAlex Williamson 
115223e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
11530bde9569SAlex Williamson 
11540bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1155f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
11560bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
11578e586137SJiri Pirko 	return 0;
11580bde9569SAlex Williamson }
11590bde9569SAlex Williamson 
11608898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1161986a4f4dSJason Wang {
1162986a4f4dSJason Wang 	int i;
11638898c21cSWanlong Gao 
11648898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
11658898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
11668898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
11678898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
11688898c21cSWanlong Gao 		}
11698898c21cSWanlong Gao 
11708898c21cSWanlong Gao 		vi->affinity_hint_set = false;
11718898c21cSWanlong Gao 	}
11728898c21cSWanlong Gao }
11738898c21cSWanlong Gao 
11748898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1175986a4f4dSJason Wang {
1176986a4f4dSJason Wang 	int i;
117747be2479SWanlong Gao 	int cpu;
1178986a4f4dSJason Wang 
1179986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1180986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1181986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1182986a4f4dSJason Wang 	 */
11838898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
11848898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
11858898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1186986a4f4dSJason Wang 		return;
1187986a4f4dSJason Wang 	}
1188986a4f4dSJason Wang 
118947be2479SWanlong Gao 	i = 0;
119047be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1191986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1192986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
11939bb8ca86SJason Wang 		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
119447be2479SWanlong Gao 		i++;
1195986a4f4dSJason Wang 	}
1196986a4f4dSJason Wang 
1197986a4f4dSJason Wang 	vi->affinity_hint_set = true;
119847be2479SWanlong Gao }
1199986a4f4dSJason Wang 
12008de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb,
12018de4b2f3SWanlong Gao 			        unsigned long action, void *hcpu)
12028de4b2f3SWanlong Gao {
12038de4b2f3SWanlong Gao 	struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
12048de4b2f3SWanlong Gao 
12058de4b2f3SWanlong Gao 	switch(action & ~CPU_TASKS_FROZEN) {
12068de4b2f3SWanlong Gao 	case CPU_ONLINE:
12078de4b2f3SWanlong Gao 	case CPU_DOWN_FAILED:
12088de4b2f3SWanlong Gao 	case CPU_DEAD:
12098de4b2f3SWanlong Gao 		virtnet_set_affinity(vi);
12108de4b2f3SWanlong Gao 		break;
12118de4b2f3SWanlong Gao 	case CPU_DOWN_PREPARE:
12128de4b2f3SWanlong Gao 		virtnet_clean_affinity(vi, (long)hcpu);
12138de4b2f3SWanlong Gao 		break;
12148de4b2f3SWanlong Gao 	default:
12158de4b2f3SWanlong Gao 		break;
12168de4b2f3SWanlong Gao 	}
12173ab098dfSJason Wang 
12188de4b2f3SWanlong Gao 	return NOTIFY_OK;
1219a9ea3fc6SHerbert Xu }
1220a9ea3fc6SHerbert Xu 
12218f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
12228f9f4668SRick Jones 				struct ethtool_ringparam *ring)
12238f9f4668SRick Jones {
12248f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
12258f9f4668SRick Jones 
1226986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1227986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
12288f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
12298f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
12308f9f4668SRick Jones }
12318f9f4668SRick Jones 
123266846048SRick Jones 
123366846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
123466846048SRick Jones 				struct ethtool_drvinfo *info)
123566846048SRick Jones {
123666846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
123766846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
123866846048SRick Jones 
123966846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
124066846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
124166846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
124266846048SRick Jones 
124366846048SRick Jones }
124466846048SRick Jones 
1245d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1246d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1247d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1248d73bcd2cSJason Wang {
1249d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1250d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1251d73bcd2cSJason Wang 	int err;
1252d73bcd2cSJason Wang 
1253d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1254d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1255d73bcd2cSJason Wang 	 */
1256d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1257d73bcd2cSJason Wang 		return -EINVAL;
1258d73bcd2cSJason Wang 
1259d73bcd2cSJason Wang 	if (queue_pairs > vi->max_queue_pairs)
1260d73bcd2cSJason Wang 		return -EINVAL;
1261d73bcd2cSJason Wang 
126247be2479SWanlong Gao 	get_online_cpus();
1263d73bcd2cSJason Wang 	err = virtnet_set_queues(vi, queue_pairs);
1264d73bcd2cSJason Wang 	if (!err) {
1265d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1266d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1267d73bcd2cSJason Wang 
12688898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1269d73bcd2cSJason Wang 	}
127047be2479SWanlong Gao 	put_online_cpus();
1271d73bcd2cSJason Wang 
1272d73bcd2cSJason Wang 	return err;
1273d73bcd2cSJason Wang }
1274d73bcd2cSJason Wang 
1275d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1276d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1277d73bcd2cSJason Wang {
1278d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1279d73bcd2cSJason Wang 
1280d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1281d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1282d73bcd2cSJason Wang 	channels->max_other = 0;
1283d73bcd2cSJason Wang 	channels->rx_count = 0;
1284d73bcd2cSJason Wang 	channels->tx_count = 0;
1285d73bcd2cSJason Wang 	channels->other_count = 0;
1286d73bcd2cSJason Wang }
1287d73bcd2cSJason Wang 
12880fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
128966846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
12909f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
12918f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1292d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1293d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1294a9ea3fc6SHerbert Xu };
1295a9ea3fc6SHerbert Xu 
129639da5814SMark McLoughlin #define MIN_MTU 68
129739da5814SMark McLoughlin #define MAX_MTU 65535
129839da5814SMark McLoughlin 
129939da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
130039da5814SMark McLoughlin {
130139da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
130239da5814SMark McLoughlin 		return -EINVAL;
130339da5814SMark McLoughlin 	dev->mtu = new_mtu;
130439da5814SMark McLoughlin 	return 0;
130539da5814SMark McLoughlin }
130639da5814SMark McLoughlin 
130776288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
130876288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
130976288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
131076288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
131176288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
13129c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
13132af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
131476288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
13153fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
13161824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
13171824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
131876288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
131976288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
132076288b4eSStephen Hemminger #endif
132176288b4eSStephen Hemminger };
132276288b4eSStephen Hemminger 
1323586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
13249f4d26d0SMark McLoughlin {
1325586d17c5SJason Wang 	struct virtnet_info *vi =
1326586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
13279f4d26d0SMark McLoughlin 	u16 v;
13289f4d26d0SMark McLoughlin 
1329586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1330586d17c5SJason Wang 	if (!vi->config_enable)
1331586d17c5SJason Wang 		goto done;
1332586d17c5SJason Wang 
1333855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1334855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
1335586d17c5SJason Wang 		goto done;
1336586d17c5SJason Wang 
1337586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1338ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1339586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1340586d17c5SJason Wang 	}
13419f4d26d0SMark McLoughlin 
13429f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
13439f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
13449f4d26d0SMark McLoughlin 
13459f4d26d0SMark McLoughlin 	if (vi->status == v)
1346586d17c5SJason Wang 		goto done;
13479f4d26d0SMark McLoughlin 
13489f4d26d0SMark McLoughlin 	vi->status = v;
13499f4d26d0SMark McLoughlin 
13509f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
13519f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1352986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
13539f4d26d0SMark McLoughlin 	} else {
13549f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1355986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
13569f4d26d0SMark McLoughlin 	}
1357586d17c5SJason Wang done:
1358586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
13599f4d26d0SMark McLoughlin }
13609f4d26d0SMark McLoughlin 
13619f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
13629f4d26d0SMark McLoughlin {
13639f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
13649f4d26d0SMark McLoughlin 
13653b07e9caSTejun Heo 	schedule_work(&vi->config_work);
13669f4d26d0SMark McLoughlin }
13679f4d26d0SMark McLoughlin 
1368986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1369986a4f4dSJason Wang {
1370986a4f4dSJason Wang 	kfree(vi->rq);
1371986a4f4dSJason Wang 	kfree(vi->sq);
1372986a4f4dSJason Wang }
1373986a4f4dSJason Wang 
1374986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi)
1375986a4f4dSJason Wang {
1376986a4f4dSJason Wang 	int i;
1377986a4f4dSJason Wang 
1378986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1379986a4f4dSJason Wang 		while (vi->rq[i].pages)
1380986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1381986a4f4dSJason Wang 	}
1382986a4f4dSJason Wang }
1383986a4f4dSJason Wang 
1384986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1385986a4f4dSJason Wang {
1386986a4f4dSJason Wang 	void *buf;
1387986a4f4dSJason Wang 	int i;
1388986a4f4dSJason Wang 
1389986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1390986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
1391986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1392986a4f4dSJason Wang 			dev_kfree_skb(buf);
1393986a4f4dSJason Wang 	}
1394986a4f4dSJason Wang 
1395986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1396986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
1397986a4f4dSJason Wang 
1398986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1399*fa9fac17SAndrey Vagin 			if (vi->mergeable_rx_bufs)
14002613af0eSMichael Dalton 				put_page(virt_to_head_page(buf));
1401*fa9fac17SAndrey Vagin 			else if (vi->big_packets)
1402*fa9fac17SAndrey Vagin 				give_pages(&vi->rq[i], buf);
1403986a4f4dSJason Wang 			else
1404986a4f4dSJason Wang 				dev_kfree_skb(buf);
1405986a4f4dSJason Wang 			--vi->rq[i].num;
1406986a4f4dSJason Wang 		}
1407986a4f4dSJason Wang 		BUG_ON(vi->rq[i].num != 0);
1408986a4f4dSJason Wang 	}
1409986a4f4dSJason Wang }
1410986a4f4dSJason Wang 
1411e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
1412e9d7417bSJason Wang {
1413e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
1414e9d7417bSJason Wang 
14158898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
1416986a4f4dSJason Wang 
1417e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
1418986a4f4dSJason Wang 
1419986a4f4dSJason Wang 	virtnet_free_queues(vi);
1420986a4f4dSJason Wang }
1421986a4f4dSJason Wang 
1422986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
1423986a4f4dSJason Wang {
1424986a4f4dSJason Wang 	vq_callback_t **callbacks;
1425986a4f4dSJason Wang 	struct virtqueue **vqs;
1426986a4f4dSJason Wang 	int ret = -ENOMEM;
1427986a4f4dSJason Wang 	int i, total_vqs;
1428986a4f4dSJason Wang 	const char **names;
1429986a4f4dSJason Wang 
1430986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1431986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1432986a4f4dSJason Wang 	 * possible control vq.
1433986a4f4dSJason Wang 	 */
1434986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
1435986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1436986a4f4dSJason Wang 
1437986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
1438986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1439986a4f4dSJason Wang 	if (!vqs)
1440986a4f4dSJason Wang 		goto err_vq;
1441986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1442986a4f4dSJason Wang 	if (!callbacks)
1443986a4f4dSJason Wang 		goto err_callback;
1444986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1445986a4f4dSJason Wang 	if (!names)
1446986a4f4dSJason Wang 		goto err_names;
1447986a4f4dSJason Wang 
1448986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
1449986a4f4dSJason Wang 	if (vi->has_cvq) {
1450986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
1451986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
1452986a4f4dSJason Wang 	}
1453986a4f4dSJason Wang 
1454986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
1455986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1456986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
1457986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
1458986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
1459986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
1460986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
1461986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
1462986a4f4dSJason Wang 	}
1463986a4f4dSJason Wang 
1464986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1465986a4f4dSJason Wang 					 names);
1466986a4f4dSJason Wang 	if (ret)
1467986a4f4dSJason Wang 		goto err_find;
1468986a4f4dSJason Wang 
1469986a4f4dSJason Wang 	if (vi->has_cvq) {
1470986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
1471986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1472f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1473986a4f4dSJason Wang 	}
1474986a4f4dSJason Wang 
1475986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1476986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
1477986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
1478986a4f4dSJason Wang 	}
1479986a4f4dSJason Wang 
1480986a4f4dSJason Wang 	kfree(names);
1481986a4f4dSJason Wang 	kfree(callbacks);
1482986a4f4dSJason Wang 	kfree(vqs);
1483986a4f4dSJason Wang 
1484986a4f4dSJason Wang 	return 0;
1485986a4f4dSJason Wang 
1486986a4f4dSJason Wang err_find:
1487986a4f4dSJason Wang 	kfree(names);
1488986a4f4dSJason Wang err_names:
1489986a4f4dSJason Wang 	kfree(callbacks);
1490986a4f4dSJason Wang err_callback:
1491986a4f4dSJason Wang 	kfree(vqs);
1492986a4f4dSJason Wang err_vq:
1493986a4f4dSJason Wang 	return ret;
1494986a4f4dSJason Wang }
1495986a4f4dSJason Wang 
1496986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
1497986a4f4dSJason Wang {
1498986a4f4dSJason Wang 	int i;
1499986a4f4dSJason Wang 
1500986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1501986a4f4dSJason Wang 	if (!vi->sq)
1502986a4f4dSJason Wang 		goto err_sq;
1503986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1504008d4278SAmerigo Wang 	if (!vi->rq)
1505986a4f4dSJason Wang 		goto err_rq;
1506986a4f4dSJason Wang 
1507986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
1508986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1509986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
1510986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1511986a4f4dSJason Wang 			       napi_weight);
1512986a4f4dSJason Wang 
1513986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1514986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1515986a4f4dSJason Wang 	}
1516986a4f4dSJason Wang 
1517986a4f4dSJason Wang 	return 0;
1518986a4f4dSJason Wang 
1519986a4f4dSJason Wang err_rq:
1520986a4f4dSJason Wang 	kfree(vi->sq);
1521986a4f4dSJason Wang err_sq:
1522986a4f4dSJason Wang 	return -ENOMEM;
1523e9d7417bSJason Wang }
1524e9d7417bSJason Wang 
15253f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
15263f9c10b0SAmit Shah {
1527986a4f4dSJason Wang 	int ret;
15283f9c10b0SAmit Shah 
1529986a4f4dSJason Wang 	/* Allocate send & receive queues */
1530986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
1531986a4f4dSJason Wang 	if (ret)
1532986a4f4dSJason Wang 		goto err;
15333f9c10b0SAmit Shah 
1534986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
1535986a4f4dSJason Wang 	if (ret)
1536986a4f4dSJason Wang 		goto err_free;
15373f9c10b0SAmit Shah 
153847be2479SWanlong Gao 	get_online_cpus();
15398898c21cSWanlong Gao 	virtnet_set_affinity(vi);
154047be2479SWanlong Gao 	put_online_cpus();
154147be2479SWanlong Gao 
15423f9c10b0SAmit Shah 	return 0;
1543986a4f4dSJason Wang 
1544986a4f4dSJason Wang err_free:
1545986a4f4dSJason Wang 	virtnet_free_queues(vi);
1546986a4f4dSJason Wang err:
1547986a4f4dSJason Wang 	return ret;
15483f9c10b0SAmit Shah }
15493f9c10b0SAmit Shah 
1550296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
1551296f96fcSRusty Russell {
1552986a4f4dSJason Wang 	int i, err;
1553296f96fcSRusty Russell 	struct net_device *dev;
1554296f96fcSRusty Russell 	struct virtnet_info *vi;
1555986a4f4dSJason Wang 	u16 max_queue_pairs;
1556986a4f4dSJason Wang 
1557986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
1558855e0c52SRusty Russell 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1559855e0c52SRusty Russell 				   struct virtio_net_config,
1560855e0c52SRusty Russell 				   max_virtqueue_pairs, &max_queue_pairs);
1561986a4f4dSJason Wang 
1562986a4f4dSJason Wang 	/* We need at least 2 queue's */
1563986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1564986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1565986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1566986a4f4dSJason Wang 		max_queue_pairs = 1;
1567296f96fcSRusty Russell 
1568296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
1569986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1570296f96fcSRusty Russell 	if (!dev)
1571296f96fcSRusty Russell 		return -ENOMEM;
1572296f96fcSRusty Russell 
1573296f96fcSRusty Russell 	/* Set up network device as normal. */
1574f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
157576288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
1576296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
15773fa2a1dfSstephen hemminger 
1578a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1579296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
1580296f96fcSRusty Russell 
1581296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
158298e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1583296f96fcSRusty Russell 		/* This opens up the world of extra features. */
158498e778c9SMichał Mirosław 		dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
158598e778c9SMichał Mirosław 		if (csum)
1586296f96fcSRusty Russell 			dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
158798e778c9SMichał Mirosław 
158898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
158998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
159034a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
159134a48579SRusty Russell 		}
15925539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
159398e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
159498e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
159598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
159698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
159798e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
159898e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
159998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
160098e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_UFO;
160198e778c9SMichał Mirosław 
160298e778c9SMichał Mirosław 		if (gso)
160398e778c9SMichał Mirosław 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
160498e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
1605296f96fcSRusty Russell 	}
16064f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
16074f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
1608296f96fcSRusty Russell 
16094fda8302SJason Wang 	dev->vlan_features = dev->features;
16104fda8302SJason Wang 
1611296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
1612855e0c52SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1613855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
1614a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
1615855e0c52SRusty Russell 				   dev->dev_addr, dev->addr_len);
1616855e0c52SRusty Russell 	else
1617f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1618296f96fcSRusty Russell 
1619296f96fcSRusty Russell 	/* Set up our device-specific information */
1620296f96fcSRusty Russell 	vi = netdev_priv(dev);
1621296f96fcSRusty Russell 	vi->dev = dev;
1622296f96fcSRusty Russell 	vi->vdev = vdev;
1623d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
16243fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
16253fa2a1dfSstephen hemminger 	err = -ENOMEM;
16263fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
16273fa2a1dfSstephen hemminger 		goto free;
16283fa2a1dfSstephen hemminger 
1629827da44cSJohn Stultz 	for_each_possible_cpu(i) {
1630827da44cSJohn Stultz 		struct virtnet_stats *virtnet_stats;
1631827da44cSJohn Stultz 		virtnet_stats = per_cpu_ptr(vi->stats, i);
1632827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->tx_syncp);
1633827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->rx_syncp);
1634827da44cSJohn Stultz 	}
1635827da44cSJohn Stultz 
1636586d17c5SJason Wang 	mutex_init(&vi->config_lock);
1637586d17c5SJason Wang 	vi->config_enable = true;
1638586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1639296f96fcSRusty Russell 
164097402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
16418e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
16428e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
16438e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
164497402b96SHerbert Xu 		vi->big_packets = true;
164597402b96SHerbert Xu 
16463f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
16473f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
16483f2c31d9SMark McLoughlin 
1649e7428e95SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1650e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
1651e7428e95SMichael S. Tsirkin 
1652986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1653986a4f4dSJason Wang 		vi->has_cvq = true;
1654986a4f4dSJason Wang 
1655986a4f4dSJason Wang 	/* Use single tx/rx queue pair as default */
1656986a4f4dSJason Wang 	vi->curr_queue_pairs = 1;
1657986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
1658986a4f4dSJason Wang 
1659986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
16603f9c10b0SAmit Shah 	err = init_vqs(vi);
1661d2a7dddaSMichael S. Tsirkin 	if (err)
16629bb8ca86SJason Wang 		goto free_stats;
1663d2a7dddaSMichael S. Tsirkin 
16640f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
16650f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
1666986a4f4dSJason Wang 
1667296f96fcSRusty Russell 	err = register_netdev(dev);
1668296f96fcSRusty Russell 	if (err) {
1669296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1670d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1671296f96fcSRusty Russell 	}
1672b3369c1fSRusty Russell 
1673b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
167455257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1675986a4f4dSJason Wang 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
1676b3369c1fSRusty Russell 
1677b3369c1fSRusty Russell 		/* If we didn't even get one input buffer, we're useless. */
1678986a4f4dSJason Wang 		if (vi->rq[i].num == 0) {
1679986a4f4dSJason Wang 			free_unused_bufs(vi);
1680b3369c1fSRusty Russell 			err = -ENOMEM;
1681986a4f4dSJason Wang 			goto free_recv_bufs;
1682986a4f4dSJason Wang 		}
1683b3369c1fSRusty Russell 	}
1684b3369c1fSRusty Russell 
16858de4b2f3SWanlong Gao 	vi->nb.notifier_call = &virtnet_cpu_callback;
16868de4b2f3SWanlong Gao 	err = register_hotcpu_notifier(&vi->nb);
16878de4b2f3SWanlong Gao 	if (err) {
16888de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
16898de4b2f3SWanlong Gao 		goto free_recv_bufs;
16908de4b2f3SWanlong Gao 	}
16918de4b2f3SWanlong Gao 
1692167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1693167c25e4SJason Wang 	   otherwise get link status from config. */
1694167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1695167c25e4SJason Wang 		netif_carrier_off(dev);
16963b07e9caSTejun Heo 		schedule_work(&vi->config_work);
1697167c25e4SJason Wang 	} else {
1698167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
16994783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1700167c25e4SJason Wang 	}
17019f4d26d0SMark McLoughlin 
1702986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1703986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
1704986a4f4dSJason Wang 
1705296f96fcSRusty Russell 	return 0;
1706296f96fcSRusty Russell 
1707986a4f4dSJason Wang free_recv_bufs:
1708986a4f4dSJason Wang 	free_receive_bufs(vi);
1709b3369c1fSRusty Russell 	unregister_netdev(dev);
1710d2a7dddaSMichael S. Tsirkin free_vqs:
1711986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
1712e9d7417bSJason Wang 	virtnet_del_vqs(vi);
17132613af0eSMichael Dalton 	if (vi->alloc_frag.page)
17142613af0eSMichael Dalton 		put_page(vi->alloc_frag.page);
17153fa2a1dfSstephen hemminger free_stats:
17163fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1717296f96fcSRusty Russell free:
1718296f96fcSRusty Russell 	free_netdev(dev);
1719296f96fcSRusty Russell 	return err;
1720296f96fcSRusty Russell }
1721296f96fcSRusty Russell 
172204486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
1723296f96fcSRusty Russell {
172404486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
1725830a8a97SShirley Ma 
1726830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
17279ab86bbcSShirley Ma 	free_unused_bufs(vi);
1728fb6813f4SRusty Russell 
1729986a4f4dSJason Wang 	free_receive_bufs(vi);
1730d2a7dddaSMichael S. Tsirkin 
1731986a4f4dSJason Wang 	virtnet_del_vqs(vi);
173204486ed0SAmit Shah }
173304486ed0SAmit Shah 
17348cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
173504486ed0SAmit Shah {
173604486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
173704486ed0SAmit Shah 
17388de4b2f3SWanlong Gao 	unregister_hotcpu_notifier(&vi->nb);
17398de4b2f3SWanlong Gao 
1740586d17c5SJason Wang 	/* Prevent config work handler from accessing the device. */
1741586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1742586d17c5SJason Wang 	vi->config_enable = false;
1743586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1744586d17c5SJason Wang 
174504486ed0SAmit Shah 	unregister_netdev(vi->dev);
174604486ed0SAmit Shah 
174704486ed0SAmit Shah 	remove_vq_common(vi);
17482613af0eSMichael Dalton 	if (vi->alloc_frag.page)
17492613af0eSMichael Dalton 		put_page(vi->alloc_frag.page);
1750fb6813f4SRusty Russell 
1751586d17c5SJason Wang 	flush_work(&vi->config_work);
1752586d17c5SJason Wang 
17532e66f55bSKrishna Kumar 	free_percpu(vi->stats);
175474b2553fSRusty Russell 	free_netdev(vi->dev);
1755296f96fcSRusty Russell }
1756296f96fcSRusty Russell 
175789107000SAaron Lu #ifdef CONFIG_PM_SLEEP
17580741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
17590741bcb5SAmit Shah {
17600741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1761986a4f4dSJason Wang 	int i;
17620741bcb5SAmit Shah 
1763ec9debbdSJason Wang 	unregister_hotcpu_notifier(&vi->nb);
1764ec9debbdSJason Wang 
1765586d17c5SJason Wang 	/* Prevent config work handler from accessing the device */
1766586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1767586d17c5SJason Wang 	vi->config_enable = false;
1768586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1769586d17c5SJason Wang 
17700741bcb5SAmit Shah 	netif_device_detach(vi->dev);
17710741bcb5SAmit Shah 	cancel_delayed_work_sync(&vi->refill);
17720741bcb5SAmit Shah 
17730741bcb5SAmit Shah 	if (netif_running(vi->dev))
1774986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++) {
1775986a4f4dSJason Wang 			napi_disable(&vi->rq[i].napi);
1776986a4f4dSJason Wang 			netif_napi_del(&vi->rq[i].napi);
1777986a4f4dSJason Wang 		}
17780741bcb5SAmit Shah 
17790741bcb5SAmit Shah 	remove_vq_common(vi);
17800741bcb5SAmit Shah 
1781586d17c5SJason Wang 	flush_work(&vi->config_work);
1782586d17c5SJason Wang 
17830741bcb5SAmit Shah 	return 0;
17840741bcb5SAmit Shah }
17850741bcb5SAmit Shah 
17860741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
17870741bcb5SAmit Shah {
17880741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1789986a4f4dSJason Wang 	int err, i;
17900741bcb5SAmit Shah 
17910741bcb5SAmit Shah 	err = init_vqs(vi);
17920741bcb5SAmit Shah 	if (err)
17930741bcb5SAmit Shah 		return err;
17940741bcb5SAmit Shah 
17950741bcb5SAmit Shah 	if (netif_running(vi->dev))
1796986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
1797986a4f4dSJason Wang 			virtnet_napi_enable(&vi->rq[i]);
17980741bcb5SAmit Shah 
17990741bcb5SAmit Shah 	netif_device_attach(vi->dev);
18000741bcb5SAmit Shah 
180155257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++)
1802986a4f4dSJason Wang 		if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
18033b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
18040741bcb5SAmit Shah 
1805586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1806586d17c5SJason Wang 	vi->config_enable = true;
1807586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1808586d17c5SJason Wang 
180935ed159bSJason Wang 	rtnl_lock();
1810986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
181135ed159bSJason Wang 	rtnl_unlock();
1812986a4f4dSJason Wang 
1813ec9debbdSJason Wang 	err = register_hotcpu_notifier(&vi->nb);
1814ec9debbdSJason Wang 	if (err)
1815ec9debbdSJason Wang 		return err;
1816ec9debbdSJason Wang 
18170741bcb5SAmit Shah 	return 0;
18180741bcb5SAmit Shah }
18190741bcb5SAmit Shah #endif
18200741bcb5SAmit Shah 
1821296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
1822296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1823296f96fcSRusty Russell 	{ 0 },
1824296f96fcSRusty Russell };
1825296f96fcSRusty Russell 
1826c45a6816SRusty Russell static unsigned int features[] = {
18275e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
18285e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1829c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
183097402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
18315c516751SSridhar Samudrala 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
18322a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
18330bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1834986a4f4dSJason Wang 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
18357e58d5aeSAmos Kong 	VIRTIO_NET_F_CTRL_MAC_ADDR,
1836e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
1837c45a6816SRusty Russell };
1838c45a6816SRusty Russell 
183922402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
1840c45a6816SRusty Russell 	.feature_table = features,
1841c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
1842296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
1843296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
1844296f96fcSRusty Russell 	.id_table =	id_table,
1845296f96fcSRusty Russell 	.probe =	virtnet_probe,
18468cc085d6SBill Pemberton 	.remove =	virtnet_remove,
18479f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
184889107000SAaron Lu #ifdef CONFIG_PM_SLEEP
18490741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
18500741bcb5SAmit Shah 	.restore =	virtnet_restore,
18510741bcb5SAmit Shah #endif
1852296f96fcSRusty Russell };
1853296f96fcSRusty Russell 
1854b2a17029SRusty Russell module_virtio_driver(virtio_net_driver);
1855296f96fcSRusty Russell 
1856296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
1857296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
1858296f96fcSRusty Russell MODULE_LICENSE("GPL");
1859