xref: /openbmc/linux/drivers/net/virtio_net.c (revision ba275241030cfe87b87d6592345c7e7ebd9b6fba)
148925e37SRusty Russell /* A network driver using virtio.
2296f96fcSRusty Russell  *
3296f96fcSRusty Russell  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4296f96fcSRusty Russell  *
5296f96fcSRusty Russell  * This program is free software; you can redistribute it and/or modify
6296f96fcSRusty Russell  * it under the terms of the GNU General Public License as published by
7296f96fcSRusty Russell  * the Free Software Foundation; either version 2 of the License, or
8296f96fcSRusty Russell  * (at your option) any later version.
9296f96fcSRusty Russell  *
10296f96fcSRusty Russell  * This program is distributed in the hope that it will be useful,
11296f96fcSRusty Russell  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12296f96fcSRusty Russell  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13296f96fcSRusty Russell  * GNU General Public License for more details.
14296f96fcSRusty Russell  *
15296f96fcSRusty Russell  * You should have received a copy of the GNU General Public License
16296f96fcSRusty Russell  * along with this program; if not, write to the Free Software
17296f96fcSRusty Russell  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18296f96fcSRusty Russell  */
19296f96fcSRusty Russell //#define DEBUG
20296f96fcSRusty Russell #include <linux/netdevice.h>
21296f96fcSRusty Russell #include <linux/etherdevice.h>
22a9ea3fc6SHerbert Xu #include <linux/ethtool.h>
23296f96fcSRusty Russell #include <linux/module.h>
24296f96fcSRusty Russell #include <linux/virtio.h>
25296f96fcSRusty Russell #include <linux/virtio_net.h>
26296f96fcSRusty Russell #include <linux/scatterlist.h>
27e918085aSAlex Williamson #include <linux/if_vlan.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
298de4b2f3SWanlong Gao #include <linux/cpu.h>
30296f96fcSRusty Russell 
31d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
326c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
336c0cd7c0SDor Laor 
34eb939922SRusty Russell static bool csum = true, gso = true;
3534a48579SRusty Russell module_param(csum, bool, 0444);
3634a48579SRusty Russell module_param(gso, bool, 0444);
3734a48579SRusty Russell 
38296f96fcSRusty Russell /* FIXME: MTU in config. */
39e918085aSAlex Williamson #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
403f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
41296f96fcSRusty Russell 
4266846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
432a41f71dSAlex Williamson 
443fa2a1dfSstephen hemminger struct virtnet_stats {
4583a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
4683a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
473fa2a1dfSstephen hemminger 	u64 tx_bytes;
483fa2a1dfSstephen hemminger 	u64 tx_packets;
493fa2a1dfSstephen hemminger 
503fa2a1dfSstephen hemminger 	u64 rx_bytes;
513fa2a1dfSstephen hemminger 	u64 rx_packets;
523fa2a1dfSstephen hemminger };
533fa2a1dfSstephen hemminger 
54e9d7417bSJason Wang /* Internal representation of a send virtqueue */
55e9d7417bSJason Wang struct send_queue {
56e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
57e9d7417bSJason Wang 	struct virtqueue *vq;
58e9d7417bSJason Wang 
59e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
60e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
61986a4f4dSJason Wang 
62986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
63986a4f4dSJason Wang 	char name[40];
64e9d7417bSJason Wang };
65e9d7417bSJason Wang 
66e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
67e9d7417bSJason Wang struct receive_queue {
68e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
69e9d7417bSJason Wang 	struct virtqueue *vq;
70e9d7417bSJason Wang 
71296f96fcSRusty Russell 	struct napi_struct napi;
72296f96fcSRusty Russell 
73296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
74296f96fcSRusty Russell 	unsigned int num, max;
75296f96fcSRusty Russell 
76e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
77e9d7417bSJason Wang 	struct page *pages;
78e9d7417bSJason Wang 
79e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
80e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
81986a4f4dSJason Wang 
82986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
83986a4f4dSJason Wang 	char name[40];
84e9d7417bSJason Wang };
85e9d7417bSJason Wang 
86e9d7417bSJason Wang struct virtnet_info {
87e9d7417bSJason Wang 	struct virtio_device *vdev;
88e9d7417bSJason Wang 	struct virtqueue *cvq;
89e9d7417bSJason Wang 	struct net_device *dev;
90986a4f4dSJason Wang 	struct send_queue *sq;
91986a4f4dSJason Wang 	struct receive_queue *rq;
92e9d7417bSJason Wang 	unsigned int status;
93e9d7417bSJason Wang 
94986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
95986a4f4dSJason Wang 	u16 max_queue_pairs;
96986a4f4dSJason Wang 
97986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
98986a4f4dSJason Wang 	u16 curr_queue_pairs;
99986a4f4dSJason Wang 
10097402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
10197402b96SHerbert Xu 	bool big_packets;
10297402b96SHerbert Xu 
1033f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1043f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1053f2c31d9SMark McLoughlin 
106986a4f4dSJason Wang 	/* Has control virtqueue */
107986a4f4dSJason Wang 	bool has_cvq;
108986a4f4dSJason Wang 
109e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
110e7428e95SMichael S. Tsirkin 	bool any_header_sg;
111e7428e95SMichael S. Tsirkin 
112586d17c5SJason Wang 	/* enable config space updates */
113586d17c5SJason Wang 	bool config_enable;
114586d17c5SJason Wang 
1153fa2a1dfSstephen hemminger 	/* Active statistics */
1163fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1173fa2a1dfSstephen hemminger 
1183161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1193161e453SRusty Russell 	struct delayed_work refill;
1203161e453SRusty Russell 
121586d17c5SJason Wang 	/* Work struct for config space updates */
122586d17c5SJason Wang 	struct work_struct config_work;
123586d17c5SJason Wang 
124586d17c5SJason Wang 	/* Lock for config space updates */
125586d17c5SJason Wang 	struct mutex config_lock;
126986a4f4dSJason Wang 
1272613af0eSMichael Dalton 	/* Page_frag for GFP_KERNEL packet buffer allocation when we run
1282613af0eSMichael Dalton 	 * low on memory.
1292613af0eSMichael Dalton 	 */
1302613af0eSMichael Dalton 	struct page_frag alloc_frag;
1312613af0eSMichael Dalton 
132986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
133986a4f4dSJason Wang 	bool affinity_hint_set;
13447be2479SWanlong Gao 
13547be2479SWanlong Gao 	/* Per-cpu variable to show the mapping from CPU to virtqueue */
13647be2479SWanlong Gao 	int __percpu *vq_index;
1378de4b2f3SWanlong 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 
3022613af0eSMichael Dalton static int receive_mergeable(struct receive_queue *rq, struct sk_buff *head_skb)
3039ab86bbcSShirley Ma {
3042613af0eSMichael Dalton 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(head_skb);
3052613af0eSMichael Dalton 	struct sk_buff *curr_skb = head_skb;
3062613af0eSMichael Dalton 	char *buf;
3079ab86bbcSShirley Ma 	struct page *page;
308*ba275241SJason Wang 	int num_buf, len, offset;
3099ab86bbcSShirley Ma 
3109ab86bbcSShirley Ma 	num_buf = hdr->mhdr.num_buffers;
3119ab86bbcSShirley Ma 	while (--num_buf) {
3122613af0eSMichael Dalton 		int num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
3132613af0eSMichael Dalton 		buf = virtqueue_get_buf(rq->vq, &len);
3142613af0eSMichael Dalton 		if (unlikely(!buf)) {
3153f2c31d9SMark McLoughlin 			pr_debug("%s: rx error: %d buffers missing\n",
3162613af0eSMichael Dalton 				 head_skb->dev->name, hdr->mhdr.num_buffers);
3172613af0eSMichael Dalton 			head_skb->dev->stats.rx_length_errors++;
3189ab86bbcSShirley Ma 			return -EINVAL;
3193f2c31d9SMark McLoughlin 		}
3202613af0eSMichael Dalton 		if (unlikely(len > MAX_PACKET_LEN)) {
3212613af0eSMichael Dalton 			pr_debug("%s: rx error: merge buffer too long\n",
3222613af0eSMichael Dalton 				 head_skb->dev->name);
3232613af0eSMichael Dalton 			len = MAX_PACKET_LEN;
3242613af0eSMichael Dalton 		}
3252613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
3262613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
3272613af0eSMichael Dalton 			if (unlikely(!nskb)) {
3282613af0eSMichael Dalton 				head_skb->dev->stats.rx_dropped++;
3292613af0eSMichael Dalton 				return -ENOMEM;
3302613af0eSMichael Dalton 			}
3312613af0eSMichael Dalton 			if (curr_skb == head_skb)
3322613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
3332613af0eSMichael Dalton 			else
3342613af0eSMichael Dalton 				curr_skb->next = nskb;
3352613af0eSMichael Dalton 			curr_skb = nskb;
3362613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
3372613af0eSMichael Dalton 			num_skb_frags = 0;
3382613af0eSMichael Dalton 		}
3392613af0eSMichael Dalton 		if (curr_skb != head_skb) {
3402613af0eSMichael Dalton 			head_skb->data_len += len;
3412613af0eSMichael Dalton 			head_skb->len += len;
3422613af0eSMichael Dalton 			head_skb->truesize += MAX_PACKET_LEN;
3432613af0eSMichael Dalton 		}
3442613af0eSMichael Dalton 		page = virt_to_head_page(buf);
345*ba275241SJason Wang 		offset = buf - (char *)page_address(page);
346*ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
347*ba275241SJason Wang 			put_page(page);
348*ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
349*ba275241SJason Wang 					     len, MAX_PACKET_LEN);
350*ba275241SJason Wang 		} else {
3512613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
352*ba275241SJason Wang 					offset, len,
3532613af0eSMichael Dalton 					MAX_PACKET_LEN);
354*ba275241SJason Wang 		}
355e9d7417bSJason Wang 		--rq->num;
3563f2c31d9SMark McLoughlin 	}
3579ab86bbcSShirley Ma 	return 0;
3589ab86bbcSShirley Ma }
3599ab86bbcSShirley Ma 
360e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
3619ab86bbcSShirley Ma {
362e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
363e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
36458472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
3659ab86bbcSShirley Ma 	struct sk_buff *skb;
3669ab86bbcSShirley Ma 	struct page *page;
3679ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
3689ab86bbcSShirley Ma 
3699ab86bbcSShirley Ma 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
3709ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
3719ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
3722613af0eSMichael Dalton 		if (vi->big_packets)
373e9d7417bSJason Wang 			give_pages(rq, buf);
3742613af0eSMichael Dalton 		else if (vi->mergeable_rx_bufs)
3752613af0eSMichael Dalton 			put_page(virt_to_head_page(buf));
3769ab86bbcSShirley Ma 		else
3779ab86bbcSShirley Ma 			dev_kfree_skb(buf);
3789ab86bbcSShirley Ma 		return;
3799ab86bbcSShirley Ma 	}
3809ab86bbcSShirley Ma 
3819ab86bbcSShirley Ma 	if (!vi->mergeable_rx_bufs && !vi->big_packets) {
3829ab86bbcSShirley Ma 		skb = buf;
3839ab86bbcSShirley Ma 		len -= sizeof(struct virtio_net_hdr);
3849ab86bbcSShirley Ma 		skb_trim(skb, len);
3852613af0eSMichael Dalton 	} else if (vi->mergeable_rx_bufs) {
3862613af0eSMichael Dalton 		struct page *page = virt_to_head_page(buf);
3872613af0eSMichael Dalton 		skb = page_to_skb(rq, page,
3882613af0eSMichael Dalton 				  (char *)buf - (char *)page_address(page),
3892613af0eSMichael Dalton 				  len, MAX_PACKET_LEN);
3902613af0eSMichael Dalton 		if (unlikely(!skb)) {
3912613af0eSMichael Dalton 			dev->stats.rx_dropped++;
3922613af0eSMichael Dalton 			put_page(page);
3932613af0eSMichael Dalton 			return;
3942613af0eSMichael Dalton 		}
3952613af0eSMichael Dalton 		if (receive_mergeable(rq, skb)) {
3962613af0eSMichael Dalton 			dev_kfree_skb(skb);
3972613af0eSMichael Dalton 			return;
3982613af0eSMichael Dalton 		}
3993f2c31d9SMark McLoughlin 	} else {
4009ab86bbcSShirley Ma 		page = buf;
4012613af0eSMichael Dalton 		skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
4029ab86bbcSShirley Ma 		if (unlikely(!skb)) {
40397402b96SHerbert Xu 			dev->stats.rx_dropped++;
404e9d7417bSJason Wang 			give_pages(rq, page);
4059ab86bbcSShirley Ma 			return;
4069ab86bbcSShirley Ma 		}
4073f2c31d9SMark McLoughlin 	}
4083f2c31d9SMark McLoughlin 
4099ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
4103fa2a1dfSstephen hemminger 
41183a27052SEric Dumazet 	u64_stats_update_begin(&stats->rx_syncp);
4123fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
4133fa2a1dfSstephen hemminger 	stats->rx_packets++;
41483a27052SEric Dumazet 	u64_stats_update_end(&stats->rx_syncp);
415296f96fcSRusty Russell 
416b3f24698SRusty Russell 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
417296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
418b3f24698SRusty Russell 		if (!skb_partial_csum_set(skb,
419b3f24698SRusty Russell 					  hdr->hdr.csum_start,
420b3f24698SRusty Russell 					  hdr->hdr.csum_offset))
421296f96fcSRusty Russell 			goto frame_err;
42210a8d94aSJason Wang 	} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
42310a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
424296f96fcSRusty Russell 	}
425296f96fcSRusty Russell 
42623cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
42723cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
42823cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
42923cde76dSMark McLoughlin 
430b3f24698SRusty Russell 	if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
431296f96fcSRusty Russell 		pr_debug("GSO!\n");
432b3f24698SRusty Russell 		switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
433296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
434c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
435296f96fcSRusty Russell 			break;
436296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
437c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
438296f96fcSRusty Russell 			break;
439296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
440c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
441296f96fcSRusty Russell 			break;
442296f96fcSRusty Russell 		default:
443be443899SAmerigo Wang 			net_warn_ratelimited("%s: bad gso type %u.\n",
444b3f24698SRusty Russell 					     dev->name, hdr->hdr.gso_type);
445296f96fcSRusty Russell 			goto frame_err;
446296f96fcSRusty Russell 		}
447296f96fcSRusty Russell 
448b3f24698SRusty Russell 		if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
449c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
45034a48579SRusty Russell 
451b3f24698SRusty Russell 		skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
452296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
453be443899SAmerigo Wang 			net_warn_ratelimited("%s: zero gso size.\n", dev->name);
454296f96fcSRusty Russell 			goto frame_err;
455296f96fcSRusty Russell 		}
456296f96fcSRusty Russell 
457296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
458296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
459296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
460296f96fcSRusty Russell 	}
461296f96fcSRusty Russell 
462296f96fcSRusty Russell 	netif_receive_skb(skb);
463296f96fcSRusty Russell 	return;
464296f96fcSRusty Russell 
465296f96fcSRusty Russell frame_err:
466296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
467296f96fcSRusty Russell 	dev_kfree_skb(skb);
468296f96fcSRusty Russell }
469296f96fcSRusty Russell 
470e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
471296f96fcSRusty Russell {
472e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
473296f96fcSRusty Russell 	struct sk_buff *skb;
474b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr;
4759ab86bbcSShirley Ma 	int err;
4763f2c31d9SMark McLoughlin 
4773464645aSMike Waychison 	skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
4789ab86bbcSShirley Ma 	if (unlikely(!skb))
4799ab86bbcSShirley Ma 		return -ENOMEM;
480296f96fcSRusty Russell 
481296f96fcSRusty Russell 	skb_put(skb, MAX_PACKET_LEN);
4823f2c31d9SMark McLoughlin 
4833f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
484e9d7417bSJason Wang 	sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
48597402b96SHerbert Xu 
486e9d7417bSJason Wang 	skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
48797402b96SHerbert Xu 
4889dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
4899ab86bbcSShirley Ma 	if (err < 0)
4909ab86bbcSShirley Ma 		dev_kfree_skb(skb);
49197402b96SHerbert Xu 
4929ab86bbcSShirley Ma 	return err;
49397402b96SHerbert Xu }
49497402b96SHerbert Xu 
495e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
4969ab86bbcSShirley Ma {
4979ab86bbcSShirley Ma 	struct page *first, *list = NULL;
4989ab86bbcSShirley Ma 	char *p;
4999ab86bbcSShirley Ma 	int i, err, offset;
500296f96fcSRusty Russell 
501e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
5029ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
503e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
5049ab86bbcSShirley Ma 		if (!first) {
5059ab86bbcSShirley Ma 			if (list)
506e9d7417bSJason Wang 				give_pages(rq, list);
5079ab86bbcSShirley Ma 			return -ENOMEM;
508296f96fcSRusty Russell 		}
509e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
5109ab86bbcSShirley Ma 
5119ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
5129ab86bbcSShirley Ma 		first->private = (unsigned long)list;
5139ab86bbcSShirley Ma 		list = first;
5149ab86bbcSShirley Ma 	}
5159ab86bbcSShirley Ma 
516e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
5179ab86bbcSShirley Ma 	if (!first) {
518e9d7417bSJason Wang 		give_pages(rq, list);
5199ab86bbcSShirley Ma 		return -ENOMEM;
5209ab86bbcSShirley Ma 	}
5219ab86bbcSShirley Ma 	p = page_address(first);
5229ab86bbcSShirley Ma 
523e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
524e9d7417bSJason Wang 	/* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
525e9d7417bSJason Wang 	sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
5269ab86bbcSShirley Ma 
527e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
5289ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
529e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
5309ab86bbcSShirley Ma 
5319ab86bbcSShirley Ma 	/* chain first in list head */
5329ab86bbcSShirley Ma 	first->private = (unsigned long)list;
5339dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
534aa989f5eSMichael S. Tsirkin 				  first, gfp);
5359ab86bbcSShirley Ma 	if (err < 0)
536e9d7417bSJason Wang 		give_pages(rq, first);
5379ab86bbcSShirley Ma 
5389ab86bbcSShirley Ma 	return err;
5399ab86bbcSShirley Ma }
5409ab86bbcSShirley Ma 
541e9d7417bSJason Wang static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
5429ab86bbcSShirley Ma {
5432613af0eSMichael Dalton 	struct virtnet_info *vi = rq->vq->vdev->priv;
5442613af0eSMichael Dalton 	char *buf = NULL;
5459ab86bbcSShirley Ma 	int err;
5469ab86bbcSShirley Ma 
5472613af0eSMichael Dalton 	if (gfp & __GFP_WAIT) {
5482613af0eSMichael Dalton 		if (skb_page_frag_refill(MAX_PACKET_LEN, &vi->alloc_frag,
5492613af0eSMichael Dalton 					 gfp)) {
5502613af0eSMichael Dalton 			buf = (char *)page_address(vi->alloc_frag.page) +
5512613af0eSMichael Dalton 			      vi->alloc_frag.offset;
5522613af0eSMichael Dalton 			get_page(vi->alloc_frag.page);
5532613af0eSMichael Dalton 			vi->alloc_frag.offset += MAX_PACKET_LEN;
5542613af0eSMichael Dalton 		}
5552613af0eSMichael Dalton 	} else {
5562613af0eSMichael Dalton 		buf = netdev_alloc_frag(MAX_PACKET_LEN);
5572613af0eSMichael Dalton 	}
5582613af0eSMichael Dalton 	if (!buf)
5599ab86bbcSShirley Ma 		return -ENOMEM;
5609ab86bbcSShirley Ma 
5612613af0eSMichael Dalton 	sg_init_one(rq->sg, buf, MAX_PACKET_LEN);
5622613af0eSMichael Dalton 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
5639ab86bbcSShirley Ma 	if (err < 0)
5642613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
5659ab86bbcSShirley Ma 
5669ab86bbcSShirley Ma 	return err;
567296f96fcSRusty Russell }
568296f96fcSRusty Russell 
569b2baed69SRusty Russell /*
570b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
571b2baed69SRusty Russell  *
572b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
573b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
574b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
575b2baed69SRusty Russell  */
576e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
5773f2c31d9SMark McLoughlin {
578e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
5793f2c31d9SMark McLoughlin 	int err;
5801788f495SMichael S. Tsirkin 	bool oom;
5813f2c31d9SMark McLoughlin 
5820aea51c3SAmit Shah 	do {
5839ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
584e9d7417bSJason Wang 			err = add_recvbuf_mergeable(rq, gfp);
5859ab86bbcSShirley Ma 		else if (vi->big_packets)
586e9d7417bSJason Wang 			err = add_recvbuf_big(rq, gfp);
5879ab86bbcSShirley Ma 		else
588e9d7417bSJason Wang 			err = add_recvbuf_small(rq, gfp);
5893f2c31d9SMark McLoughlin 
5901788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
5919ed4cb07SRusty Russell 		if (err)
5923f2c31d9SMark McLoughlin 			break;
593e9d7417bSJason Wang 		++rq->num;
594b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
595e9d7417bSJason Wang 	if (unlikely(rq->num > rq->max))
596e9d7417bSJason Wang 		rq->max = rq->num;
597e9d7417bSJason Wang 	virtqueue_kick(rq->vq);
5983161e453SRusty Russell 	return !oom;
5993f2c31d9SMark McLoughlin }
6003f2c31d9SMark McLoughlin 
60118445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
602296f96fcSRusty Russell {
603296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
604986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
605e9d7417bSJason Wang 
60618445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
607e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
6081915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
609e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
61018445c4dSRusty Russell 	}
611296f96fcSRusty Russell }
612296f96fcSRusty Russell 
613e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
6143e9d08ecSBruce Rogers {
615e9d7417bSJason Wang 	napi_enable(&rq->napi);
6163e9d08ecSBruce Rogers 
6173e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
6183e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
6193e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
6203e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
621e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
622e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
623ec13ee80SMichael S. Tsirkin 		local_bh_disable();
624e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
625ec13ee80SMichael S. Tsirkin 		local_bh_enable();
6263e9d08ecSBruce Rogers 	}
6273e9d08ecSBruce Rogers }
6283e9d08ecSBruce Rogers 
6293161e453SRusty Russell static void refill_work(struct work_struct *work)
6303161e453SRusty Russell {
631e9d7417bSJason Wang 	struct virtnet_info *vi =
632e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
6333161e453SRusty Russell 	bool still_empty;
634986a4f4dSJason Wang 	int i;
6353161e453SRusty Russell 
63655257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
637986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
638986a4f4dSJason Wang 
639986a4f4dSJason Wang 		napi_disable(&rq->napi);
640986a4f4dSJason Wang 		still_empty = !try_fill_recv(rq, GFP_KERNEL);
641986a4f4dSJason Wang 		virtnet_napi_enable(rq);
6423161e453SRusty Russell 
6433161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
644986a4f4dSJason Wang 		 * we will *never* try to fill again.
645986a4f4dSJason Wang 		 */
6463161e453SRusty Russell 		if (still_empty)
6473b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
6483161e453SRusty Russell 	}
649986a4f4dSJason Wang }
6503161e453SRusty Russell 
651296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
652296f96fcSRusty Russell {
653e9d7417bSJason Wang 	struct receive_queue *rq =
654e9d7417bSJason Wang 		container_of(napi, struct receive_queue, napi);
655e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
6569ab86bbcSShirley Ma 	void *buf;
657cbdadbbfSMichael S. Tsirkin 	unsigned int r, len, received = 0;
658296f96fcSRusty Russell 
659296f96fcSRusty Russell again:
660296f96fcSRusty Russell 	while (received < budget &&
661e9d7417bSJason Wang 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
662e9d7417bSJason Wang 		receive_buf(rq, buf, len);
663e9d7417bSJason Wang 		--rq->num;
664296f96fcSRusty Russell 		received++;
665296f96fcSRusty Russell 	}
666296f96fcSRusty Russell 
667e9d7417bSJason Wang 	if (rq->num < rq->max / 2) {
668e9d7417bSJason Wang 		if (!try_fill_recv(rq, GFP_ATOMIC))
6693b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
6703161e453SRusty Russell 	}
671296f96fcSRusty Russell 
6728329d98eSRusty Russell 	/* Out of packets? */
6738329d98eSRusty Russell 	if (received < budget) {
674cbdadbbfSMichael S. Tsirkin 		r = virtqueue_enable_cb_prepare(rq->vq);
675288379f0SBen Hutchings 		napi_complete(napi);
676cbdadbbfSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(rq->vq, r)) &&
6778e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
678e9d7417bSJason Wang 			virtqueue_disable_cb(rq->vq);
679288379f0SBen Hutchings 			__napi_schedule(napi);
680296f96fcSRusty Russell 			goto again;
681296f96fcSRusty Russell 		}
6824265f161SChristian Borntraeger 	}
683296f96fcSRusty Russell 
684296f96fcSRusty Russell 	return received;
685296f96fcSRusty Russell }
686296f96fcSRusty Russell 
687986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
688986a4f4dSJason Wang {
689986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
690986a4f4dSJason Wang 	int i;
691986a4f4dSJason Wang 
692e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
693e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
694986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
695986a4f4dSJason Wang 			if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
696986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
697986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
698986a4f4dSJason Wang 	}
699986a4f4dSJason Wang 
700986a4f4dSJason Wang 	return 0;
701986a4f4dSJason Wang }
702986a4f4dSJason Wang 
703b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
704296f96fcSRusty Russell {
705296f96fcSRusty Russell 	struct sk_buff *skb;
7066ee57bccSMichael S. Tsirkin 	unsigned int len;
707e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
70858472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
709296f96fcSRusty Russell 
710e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
711296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
7123fa2a1dfSstephen hemminger 
71383a27052SEric Dumazet 		u64_stats_update_begin(&stats->tx_syncp);
7143fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
7153fa2a1dfSstephen hemminger 		stats->tx_packets++;
71683a27052SEric Dumazet 		u64_stats_update_end(&stats->tx_syncp);
7173fa2a1dfSstephen hemminger 
718ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
719296f96fcSRusty Russell 	}
720296f96fcSRusty Russell }
721296f96fcSRusty Russell 
722e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
723296f96fcSRusty Russell {
724e7428e95SMichael S. Tsirkin 	struct skb_vnet_hdr *hdr;
725296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
726e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
7277bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
728e7428e95SMichael S. Tsirkin 	unsigned hdr_len;
729e7428e95SMichael S. Tsirkin 	bool can_push;
730296f96fcSRusty Russell 
731e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
732e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
733e7428e95SMichael S. Tsirkin 		hdr_len = sizeof hdr->mhdr;
734e7428e95SMichael S. Tsirkin 	else
735e7428e95SMichael S. Tsirkin 		hdr_len = sizeof hdr->hdr;
736e7428e95SMichael S. Tsirkin 
737e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
738e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
739e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
740e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
741e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
742e7428e95SMichael S. Tsirkin 	if (can_push)
743e7428e95SMichael S. Tsirkin 		hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
744e7428e95SMichael S. Tsirkin 	else
745e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
746296f96fcSRusty Russell 
747296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
748b3f24698SRusty Russell 		hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
74955508d60SMichał Mirosław 		hdr->hdr.csum_start = skb_checksum_start_offset(skb);
750b3f24698SRusty Russell 		hdr->hdr.csum_offset = skb->csum_offset;
751296f96fcSRusty Russell 	} else {
752b3f24698SRusty Russell 		hdr->hdr.flags = 0;
753b3f24698SRusty Russell 		hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
754296f96fcSRusty Russell 	}
755296f96fcSRusty Russell 
756296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
757b3f24698SRusty Russell 		hdr->hdr.hdr_len = skb_headlen(skb);
758b3f24698SRusty Russell 		hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
75934a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
760b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
761296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
762b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
763296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
764b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
765296f96fcSRusty Russell 		else
766296f96fcSRusty Russell 			BUG();
76734a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
768b3f24698SRusty Russell 			hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
769296f96fcSRusty Russell 	} else {
770b3f24698SRusty Russell 		hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
771b3f24698SRusty Russell 		hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
772296f96fcSRusty Russell 	}
773296f96fcSRusty Russell 
774e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
775b3f24698SRusty Russell 		hdr->mhdr.num_buffers = 0;
7763f2c31d9SMark McLoughlin 
777e7428e95SMichael S. Tsirkin 	if (can_push) {
778e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
779e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
780e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
781e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
782e7428e95SMichael S. Tsirkin 	} else {
783e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
784b7dfde95SLinus Torvalds 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
785e7428e95SMichael S. Tsirkin 	}
7869dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
78711a3a154SRusty Russell }
78811a3a154SRusty Russell 
789424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
79099ffc696SRusty Russell {
79199ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
792986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
793986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
7949ed4cb07SRusty Russell 	int err;
7952cb9c6baSRusty Russell 
7962cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
797e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
79899ffc696SRusty Russell 
79903f191baSMichael S. Tsirkin 	/* Try to transmit */
800b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
80199ffc696SRusty Russell 
8029ed4cb07SRusty Russell 	/* This should not happen! */
8030e3daa64SRusty Russell 	if (unlikely(err)) {
80458eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
8052e57b79cSRick Jones 		if (net_ratelimit())
80658eba97dSRusty Russell 			dev_warn(&dev->dev,
807b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
80858eba97dSRusty Russell 		dev->stats.tx_dropped++;
80958eba97dSRusty Russell 		kfree_skb(skb);
81058eba97dSRusty Russell 		return NETDEV_TX_OK;
811296f96fcSRusty Russell 	}
812e9d7417bSJason Wang 	virtqueue_kick(sq->vq);
81303f191baSMichael S. Tsirkin 
81448925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
81548925e37SRusty Russell 	skb_orphan(skb);
81648925e37SRusty Russell 	nf_reset(skb);
81748925e37SRusty Russell 
81848925e37SRusty Russell 	/* Apparently nice girls don't return TX_BUSY; stop the queue
81948925e37SRusty Russell 	 * before it gets out of hand.  Naturally, this wastes entries. */
820b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
821986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
822e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
82348925e37SRusty Russell 			/* More just got used, free them then recheck. */
824b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
825b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
826986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
827e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
82848925e37SRusty Russell 			}
82948925e37SRusty Russell 		}
83048925e37SRusty Russell 	}
83148925e37SRusty Russell 
83248925e37SRusty Russell 	return NETDEV_TX_OK;
83348925e37SRusty Russell }
83448925e37SRusty Russell 
83540cbfc37SAmos Kong /*
83640cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
83740cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
83840cbfc37SAmos Kong  * never fail unless improperly formated.
83940cbfc37SAmos Kong  */
84040cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
841f7bc9594SRusty Russell 				 struct scatterlist *out,
842f7bc9594SRusty Russell 				 struct scatterlist *in)
84340cbfc37SAmos Kong {
844f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
84540cbfc37SAmos Kong 	struct virtio_net_ctrl_hdr ctrl;
84640cbfc37SAmos Kong 	virtio_net_ctrl_ack status = ~0;
847f7bc9594SRusty Russell 	unsigned out_num = 0, in_num = 0, tmp;
84840cbfc37SAmos Kong 
84940cbfc37SAmos Kong 	/* Caller should know better */
850f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
85140cbfc37SAmos Kong 
85240cbfc37SAmos Kong 	ctrl.class = class;
85340cbfc37SAmos Kong 	ctrl.cmd = cmd;
854f7bc9594SRusty Russell 	/* Add header */
855f7bc9594SRusty Russell 	sg_init_one(&hdr, &ctrl, sizeof(ctrl));
856f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
85740cbfc37SAmos Kong 
858f7bc9594SRusty Russell 	if (out)
859f7bc9594SRusty Russell 		sgs[out_num++] = out;
860f7bc9594SRusty Russell 	if (in)
861f7bc9594SRusty Russell 		sgs[out_num + in_num++] = in;
86240cbfc37SAmos Kong 
863f7bc9594SRusty Russell 	/* Add return status. */
864f7bc9594SRusty Russell 	sg_init_one(&stat, &status, sizeof(status));
865f7bc9594SRusty Russell 	sgs[out_num + in_num++] = &stat;
86640cbfc37SAmos Kong 
867f7bc9594SRusty Russell 	BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
868f7bc9594SRusty Russell 	BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
869f7bc9594SRusty Russell 	       < 0);
87040cbfc37SAmos Kong 
87140cbfc37SAmos Kong 	virtqueue_kick(vi->cvq);
87240cbfc37SAmos Kong 
87340cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
87440cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
87540cbfc37SAmos Kong 	 */
87640cbfc37SAmos Kong 	while (!virtqueue_get_buf(vi->cvq, &tmp))
87740cbfc37SAmos Kong 		cpu_relax();
87840cbfc37SAmos Kong 
87940cbfc37SAmos Kong 	return status == VIRTIO_NET_OK;
88040cbfc37SAmos Kong }
88140cbfc37SAmos Kong 
8829c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
8839c46f6d4SAlex Williamson {
8849c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
8859c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
886f2f2c8b4SJiri Pirko 	int ret;
8877e58d5aeSAmos Kong 	struct sockaddr *addr = p;
8887e58d5aeSAmos Kong 	struct scatterlist sg;
8899c46f6d4SAlex Williamson 
8907e58d5aeSAmos Kong 	ret = eth_prepare_mac_addr_change(dev, p);
891f2f2c8b4SJiri Pirko 	if (ret)
892f2f2c8b4SJiri Pirko 		return ret;
8939c46f6d4SAlex Williamson 
8947e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
8957e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
8967e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
8977e58d5aeSAmos Kong 					  VIRTIO_NET_CTRL_MAC_ADDR_SET,
898f7bc9594SRusty Russell 					  &sg, NULL)) {
8997e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
9007e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
9017e58d5aeSAmos Kong 			return -EINVAL;
9027e58d5aeSAmos Kong 		}
9037e58d5aeSAmos Kong 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
9049c46f6d4SAlex Williamson 		vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
9057e58d5aeSAmos Kong 				  addr->sa_data, dev->addr_len);
9067e58d5aeSAmos Kong 	}
9077e58d5aeSAmos Kong 
9087e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
9099c46f6d4SAlex Williamson 
9109c46f6d4SAlex Williamson 	return 0;
9119c46f6d4SAlex Williamson }
9129c46f6d4SAlex Williamson 
9133fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
9143fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
9153fa2a1dfSstephen hemminger {
9163fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
9173fa2a1dfSstephen hemminger 	int cpu;
9183fa2a1dfSstephen hemminger 	unsigned int start;
9193fa2a1dfSstephen hemminger 
9203fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
92158472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
9223fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
9233fa2a1dfSstephen hemminger 
9243fa2a1dfSstephen hemminger 		do {
925e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
9263fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
9273fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
928e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
92983a27052SEric Dumazet 
93083a27052SEric Dumazet 		do {
931e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
9323fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
9333fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
934e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
9353fa2a1dfSstephen hemminger 
9363fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
9373fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
9383fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
9393fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
9403fa2a1dfSstephen hemminger 	}
9413fa2a1dfSstephen hemminger 
9423fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
943021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
9443fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
9453fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
9463fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
9473fa2a1dfSstephen hemminger 
9483fa2a1dfSstephen hemminger 	return tot;
9493fa2a1dfSstephen hemminger }
9503fa2a1dfSstephen hemminger 
951da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
952da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
953da74e89dSAmit Shah {
954da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
955986a4f4dSJason Wang 	int i;
956da74e89dSAmit Shah 
957986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
958986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
959da74e89dSAmit Shah }
960da74e89dSAmit Shah #endif
961da74e89dSAmit Shah 
962586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
963586d17c5SJason Wang {
964586d17c5SJason Wang 	rtnl_lock();
965586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
966f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
967586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
968586d17c5SJason Wang 	rtnl_unlock();
969586d17c5SJason Wang }
970586d17c5SJason Wang 
971986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
972986a4f4dSJason Wang {
973986a4f4dSJason Wang 	struct scatterlist sg;
974986a4f4dSJason Wang 	struct virtio_net_ctrl_mq s;
975986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
976986a4f4dSJason Wang 
977986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
978986a4f4dSJason Wang 		return 0;
979986a4f4dSJason Wang 
980986a4f4dSJason Wang 	s.virtqueue_pairs = queue_pairs;
981986a4f4dSJason Wang 	sg_init_one(&sg, &s, sizeof(s));
982986a4f4dSJason Wang 
983986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
984f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
985986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
986986a4f4dSJason Wang 			 queue_pairs);
987986a4f4dSJason Wang 		return -EINVAL;
98855257d72SSasha Levin 	} else {
989986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
99035ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
99135ed159bSJason Wang 		if (dev->flags & IFF_UP)
9929b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
99355257d72SSasha Levin 	}
994986a4f4dSJason Wang 
995986a4f4dSJason Wang 	return 0;
996986a4f4dSJason Wang }
997986a4f4dSJason Wang 
998296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
999296f96fcSRusty Russell {
1000296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1001986a4f4dSJason Wang 	int i;
1002296f96fcSRusty Russell 
1003b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1004b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1005986a4f4dSJason Wang 
1006986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
1007986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
1008296f96fcSRusty Russell 
1009296f96fcSRusty Russell 	return 0;
1010296f96fcSRusty Russell }
1011296f96fcSRusty Russell 
10122af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
10132af7698eSAlex Williamson {
10142af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1015f565a7c2SAlex Williamson 	struct scatterlist sg[2];
10162af7698eSAlex Williamson 	u8 promisc, allmulti;
1017f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1018ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
101932e7bfc4SJiri Pirko 	int uc_count;
10204cd24eafSJiri Pirko 	int mc_count;
1021f565a7c2SAlex Williamson 	void *buf;
1022f565a7c2SAlex Williamson 	int i;
10232af7698eSAlex Williamson 
10242af7698eSAlex Williamson 	/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
10252af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
10262af7698eSAlex Williamson 		return;
10272af7698eSAlex Williamson 
1028f565a7c2SAlex Williamson 	promisc = ((dev->flags & IFF_PROMISC) != 0);
1029f565a7c2SAlex Williamson 	allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
10302af7698eSAlex Williamson 
103123e258e1SAlex Williamson 	sg_init_one(sg, &promisc, sizeof(promisc));
10322af7698eSAlex Williamson 
10332af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
10342af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_PROMISC,
1035f7bc9594SRusty Russell 				  sg, NULL))
10362af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
10372af7698eSAlex Williamson 			 promisc ? "en" : "dis");
10382af7698eSAlex Williamson 
103923e258e1SAlex Williamson 	sg_init_one(sg, &allmulti, sizeof(allmulti));
10402af7698eSAlex Williamson 
10412af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
10422af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_ALLMULTI,
1043f7bc9594SRusty Russell 				  sg, NULL))
10442af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
10452af7698eSAlex Williamson 			 allmulti ? "en" : "dis");
1046f565a7c2SAlex Williamson 
104732e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
10484cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1049f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
10504cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1051f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
10524cd24eafSJiri Pirko 	mac_data = buf;
1053e68ed8f0SJoe Perches 	if (!buf)
1054f565a7c2SAlex Williamson 		return;
1055f565a7c2SAlex Williamson 
105623e258e1SAlex Williamson 	sg_init_table(sg, 2);
105723e258e1SAlex Williamson 
1058f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
105932e7bfc4SJiri Pirko 	mac_data->entries = uc_count;
1060ccffad25SJiri Pirko 	i = 0;
106132e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1062ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1063f565a7c2SAlex Williamson 
1064f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
106532e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1066f565a7c2SAlex Williamson 
1067f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
106832e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1069f565a7c2SAlex Williamson 
10704cd24eafSJiri Pirko 	mac_data->entries = mc_count;
1071567ec874SJiri Pirko 	i = 0;
107222bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
107322bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1074f565a7c2SAlex Williamson 
1075f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
10764cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1077f565a7c2SAlex Williamson 
1078f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1079f565a7c2SAlex Williamson 				  VIRTIO_NET_CTRL_MAC_TABLE_SET,
1080f7bc9594SRusty Russell 				  sg, NULL))
1081f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
1082f565a7c2SAlex Williamson 
1083f565a7c2SAlex Williamson 	kfree(buf);
10842af7698eSAlex Williamson }
10852af7698eSAlex Williamson 
108680d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
108780d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
10880bde9569SAlex Williamson {
10890bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
10900bde9569SAlex Williamson 	struct scatterlist sg;
10910bde9569SAlex Williamson 
109223e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
10930bde9569SAlex Williamson 
10940bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1095f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
10960bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
10978e586137SJiri Pirko 	return 0;
10980bde9569SAlex Williamson }
10990bde9569SAlex Williamson 
110080d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
110180d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
11020bde9569SAlex Williamson {
11030bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11040bde9569SAlex Williamson 	struct scatterlist sg;
11050bde9569SAlex Williamson 
110623e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
11070bde9569SAlex Williamson 
11080bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1109f7bc9594SRusty Russell 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
11100bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
11118e586137SJiri Pirko 	return 0;
11120bde9569SAlex Williamson }
11130bde9569SAlex Williamson 
11148898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1115986a4f4dSJason Wang {
1116986a4f4dSJason Wang 	int i;
11178898c21cSWanlong Gao 	int cpu;
11188898c21cSWanlong Gao 
11198898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
11208898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
11218898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
11228898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
11238898c21cSWanlong Gao 		}
11248898c21cSWanlong Gao 
11258898c21cSWanlong Gao 		vi->affinity_hint_set = false;
11268898c21cSWanlong Gao 	}
11278898c21cSWanlong Gao 
11288898c21cSWanlong Gao 	i = 0;
11298898c21cSWanlong Gao 	for_each_online_cpu(cpu) {
11308898c21cSWanlong Gao 		if (cpu == hcpu) {
11318898c21cSWanlong Gao 			*per_cpu_ptr(vi->vq_index, cpu) = -1;
11328898c21cSWanlong Gao 		} else {
11338898c21cSWanlong Gao 			*per_cpu_ptr(vi->vq_index, cpu) =
11348898c21cSWanlong Gao 				++i % vi->curr_queue_pairs;
11358898c21cSWanlong Gao 		}
11368898c21cSWanlong Gao 	}
11378898c21cSWanlong Gao }
11388898c21cSWanlong Gao 
11398898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1140986a4f4dSJason Wang {
1141986a4f4dSJason Wang 	int i;
114247be2479SWanlong Gao 	int cpu;
1143986a4f4dSJason Wang 
1144986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1145986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1146986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1147986a4f4dSJason Wang 	 */
11488898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
11498898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
11508898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1151986a4f4dSJason Wang 		return;
1152986a4f4dSJason Wang 	}
1153986a4f4dSJason Wang 
115447be2479SWanlong Gao 	i = 0;
115547be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1156986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1157986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
115847be2479SWanlong Gao 		*per_cpu_ptr(vi->vq_index, cpu) = i;
115947be2479SWanlong Gao 		i++;
1160986a4f4dSJason Wang 	}
1161986a4f4dSJason Wang 
1162986a4f4dSJason Wang 	vi->affinity_hint_set = true;
116347be2479SWanlong Gao }
1164986a4f4dSJason Wang 
11658de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb,
11668de4b2f3SWanlong Gao 			        unsigned long action, void *hcpu)
11678de4b2f3SWanlong Gao {
11688de4b2f3SWanlong Gao 	struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
11698de4b2f3SWanlong Gao 
11708de4b2f3SWanlong Gao 	switch(action & ~CPU_TASKS_FROZEN) {
11718de4b2f3SWanlong Gao 	case CPU_ONLINE:
11728de4b2f3SWanlong Gao 	case CPU_DOWN_FAILED:
11738de4b2f3SWanlong Gao 	case CPU_DEAD:
11748de4b2f3SWanlong Gao 		virtnet_set_affinity(vi);
11758de4b2f3SWanlong Gao 		break;
11768de4b2f3SWanlong Gao 	case CPU_DOWN_PREPARE:
11778de4b2f3SWanlong Gao 		virtnet_clean_affinity(vi, (long)hcpu);
11788de4b2f3SWanlong Gao 		break;
11798de4b2f3SWanlong Gao 	default:
11808de4b2f3SWanlong Gao 		break;
11818de4b2f3SWanlong Gao 	}
11823ab098dfSJason Wang 
11838de4b2f3SWanlong Gao 	return NOTIFY_OK;
1184a9ea3fc6SHerbert Xu }
1185a9ea3fc6SHerbert Xu 
11868f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
11878f9f4668SRick Jones 				struct ethtool_ringparam *ring)
11888f9f4668SRick Jones {
11898f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
11908f9f4668SRick Jones 
1191986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1192986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
11938f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
11948f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
11958f9f4668SRick Jones }
11968f9f4668SRick Jones 
119766846048SRick Jones 
119866846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
119966846048SRick Jones 				struct ethtool_drvinfo *info)
120066846048SRick Jones {
120166846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
120266846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
120366846048SRick Jones 
120466846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
120566846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
120666846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
120766846048SRick Jones 
120866846048SRick Jones }
120966846048SRick Jones 
1210d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1211d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1212d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1213d73bcd2cSJason Wang {
1214d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1215d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1216d73bcd2cSJason Wang 	int err;
1217d73bcd2cSJason Wang 
1218d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1219d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1220d73bcd2cSJason Wang 	 */
1221d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1222d73bcd2cSJason Wang 		return -EINVAL;
1223d73bcd2cSJason Wang 
1224d73bcd2cSJason Wang 	if (queue_pairs > vi->max_queue_pairs)
1225d73bcd2cSJason Wang 		return -EINVAL;
1226d73bcd2cSJason Wang 
122747be2479SWanlong Gao 	get_online_cpus();
1228d73bcd2cSJason Wang 	err = virtnet_set_queues(vi, queue_pairs);
1229d73bcd2cSJason Wang 	if (!err) {
1230d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1231d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1232d73bcd2cSJason Wang 
12338898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1234d73bcd2cSJason Wang 	}
123547be2479SWanlong Gao 	put_online_cpus();
1236d73bcd2cSJason Wang 
1237d73bcd2cSJason Wang 	return err;
1238d73bcd2cSJason Wang }
1239d73bcd2cSJason Wang 
1240d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1241d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1242d73bcd2cSJason Wang {
1243d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1244d73bcd2cSJason Wang 
1245d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1246d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1247d73bcd2cSJason Wang 	channels->max_other = 0;
1248d73bcd2cSJason Wang 	channels->rx_count = 0;
1249d73bcd2cSJason Wang 	channels->tx_count = 0;
1250d73bcd2cSJason Wang 	channels->other_count = 0;
1251d73bcd2cSJason Wang }
1252d73bcd2cSJason Wang 
12530fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
125466846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
12559f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
12568f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1257d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1258d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1259a9ea3fc6SHerbert Xu };
1260a9ea3fc6SHerbert Xu 
126139da5814SMark McLoughlin #define MIN_MTU 68
126239da5814SMark McLoughlin #define MAX_MTU 65535
126339da5814SMark McLoughlin 
126439da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
126539da5814SMark McLoughlin {
126639da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
126739da5814SMark McLoughlin 		return -EINVAL;
126839da5814SMark McLoughlin 	dev->mtu = new_mtu;
126939da5814SMark McLoughlin 	return 0;
127039da5814SMark McLoughlin }
127139da5814SMark McLoughlin 
1272986a4f4dSJason Wang /* To avoid contending a lock hold by a vcpu who would exit to host, select the
1273986a4f4dSJason Wang  * txq based on the processor id.
1274986a4f4dSJason Wang  */
1275986a4f4dSJason Wang static u16 virtnet_select_queue(struct net_device *dev, struct sk_buff *skb)
1276986a4f4dSJason Wang {
127747be2479SWanlong Gao 	int txq;
127847be2479SWanlong Gao 	struct virtnet_info *vi = netdev_priv(dev);
127947be2479SWanlong Gao 
128047be2479SWanlong Gao 	if (skb_rx_queue_recorded(skb)) {
128147be2479SWanlong Gao 		txq = skb_get_rx_queue(skb);
128247be2479SWanlong Gao 	} else {
128347be2479SWanlong Gao 		txq = *__this_cpu_ptr(vi->vq_index);
128447be2479SWanlong Gao 		if (txq == -1)
128547be2479SWanlong Gao 			txq = 0;
128647be2479SWanlong Gao 	}
1287986a4f4dSJason Wang 
1288986a4f4dSJason Wang 	while (unlikely(txq >= dev->real_num_tx_queues))
1289986a4f4dSJason Wang 		txq -= dev->real_num_tx_queues;
1290986a4f4dSJason Wang 
1291986a4f4dSJason Wang 	return txq;
1292986a4f4dSJason Wang }
1293986a4f4dSJason Wang 
129476288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
129576288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
129676288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
129776288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
129876288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
12999c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
13002af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
130176288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
13023fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
13031824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
13041824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1305986a4f4dSJason Wang 	.ndo_select_queue     = virtnet_select_queue,
130676288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
130776288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
130876288b4eSStephen Hemminger #endif
130976288b4eSStephen Hemminger };
131076288b4eSStephen Hemminger 
1311586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
13129f4d26d0SMark McLoughlin {
1313586d17c5SJason Wang 	struct virtnet_info *vi =
1314586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
13159f4d26d0SMark McLoughlin 	u16 v;
13169f4d26d0SMark McLoughlin 
1317586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1318586d17c5SJason Wang 	if (!vi->config_enable)
1319586d17c5SJason Wang 		goto done;
1320586d17c5SJason Wang 
132177dd7693SSasha Levin 	if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
13229f4d26d0SMark McLoughlin 			      offsetof(struct virtio_net_config, status),
132377dd7693SSasha Levin 			      &v) < 0)
1324586d17c5SJason Wang 		goto done;
1325586d17c5SJason Wang 
1326586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1327ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1328586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1329586d17c5SJason Wang 	}
13309f4d26d0SMark McLoughlin 
13319f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
13329f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
13339f4d26d0SMark McLoughlin 
13349f4d26d0SMark McLoughlin 	if (vi->status == v)
1335586d17c5SJason Wang 		goto done;
13369f4d26d0SMark McLoughlin 
13379f4d26d0SMark McLoughlin 	vi->status = v;
13389f4d26d0SMark McLoughlin 
13399f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
13409f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1341986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
13429f4d26d0SMark McLoughlin 	} else {
13439f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1344986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
13459f4d26d0SMark McLoughlin 	}
1346586d17c5SJason Wang done:
1347586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
13489f4d26d0SMark McLoughlin }
13499f4d26d0SMark McLoughlin 
13509f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
13519f4d26d0SMark McLoughlin {
13529f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
13539f4d26d0SMark McLoughlin 
13543b07e9caSTejun Heo 	schedule_work(&vi->config_work);
13559f4d26d0SMark McLoughlin }
13569f4d26d0SMark McLoughlin 
1357986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1358986a4f4dSJason Wang {
1359986a4f4dSJason Wang 	kfree(vi->rq);
1360986a4f4dSJason Wang 	kfree(vi->sq);
1361986a4f4dSJason Wang }
1362986a4f4dSJason Wang 
1363986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi)
1364986a4f4dSJason Wang {
1365986a4f4dSJason Wang 	int i;
1366986a4f4dSJason Wang 
1367986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1368986a4f4dSJason Wang 		while (vi->rq[i].pages)
1369986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1370986a4f4dSJason Wang 	}
1371986a4f4dSJason Wang }
1372986a4f4dSJason Wang 
1373986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1374986a4f4dSJason Wang {
1375986a4f4dSJason Wang 	void *buf;
1376986a4f4dSJason Wang 	int i;
1377986a4f4dSJason Wang 
1378986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1379986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
1380986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1381986a4f4dSJason Wang 			dev_kfree_skb(buf);
1382986a4f4dSJason Wang 	}
1383986a4f4dSJason Wang 
1384986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1385986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
1386986a4f4dSJason Wang 
1387986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
13882613af0eSMichael Dalton 			if (vi->big_packets)
1389986a4f4dSJason Wang 				give_pages(&vi->rq[i], buf);
13902613af0eSMichael Dalton 			else if (vi->mergeable_rx_bufs)
13912613af0eSMichael Dalton 				put_page(virt_to_head_page(buf));
1392986a4f4dSJason Wang 			else
1393986a4f4dSJason Wang 				dev_kfree_skb(buf);
1394986a4f4dSJason Wang 			--vi->rq[i].num;
1395986a4f4dSJason Wang 		}
1396986a4f4dSJason Wang 		BUG_ON(vi->rq[i].num != 0);
1397986a4f4dSJason Wang 	}
1398986a4f4dSJason Wang }
1399986a4f4dSJason Wang 
1400e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
1401e9d7417bSJason Wang {
1402e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
1403e9d7417bSJason Wang 
14048898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
1405986a4f4dSJason Wang 
1406e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
1407986a4f4dSJason Wang 
1408986a4f4dSJason Wang 	virtnet_free_queues(vi);
1409986a4f4dSJason Wang }
1410986a4f4dSJason Wang 
1411986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
1412986a4f4dSJason Wang {
1413986a4f4dSJason Wang 	vq_callback_t **callbacks;
1414986a4f4dSJason Wang 	struct virtqueue **vqs;
1415986a4f4dSJason Wang 	int ret = -ENOMEM;
1416986a4f4dSJason Wang 	int i, total_vqs;
1417986a4f4dSJason Wang 	const char **names;
1418986a4f4dSJason Wang 
1419986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1420986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1421986a4f4dSJason Wang 	 * possible control vq.
1422986a4f4dSJason Wang 	 */
1423986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
1424986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1425986a4f4dSJason Wang 
1426986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
1427986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1428986a4f4dSJason Wang 	if (!vqs)
1429986a4f4dSJason Wang 		goto err_vq;
1430986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1431986a4f4dSJason Wang 	if (!callbacks)
1432986a4f4dSJason Wang 		goto err_callback;
1433986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1434986a4f4dSJason Wang 	if (!names)
1435986a4f4dSJason Wang 		goto err_names;
1436986a4f4dSJason Wang 
1437986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
1438986a4f4dSJason Wang 	if (vi->has_cvq) {
1439986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
1440986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
1441986a4f4dSJason Wang 	}
1442986a4f4dSJason Wang 
1443986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
1444986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1445986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
1446986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
1447986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
1448986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
1449986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
1450986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
1451986a4f4dSJason Wang 	}
1452986a4f4dSJason Wang 
1453986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1454986a4f4dSJason Wang 					 names);
1455986a4f4dSJason Wang 	if (ret)
1456986a4f4dSJason Wang 		goto err_find;
1457986a4f4dSJason Wang 
1458986a4f4dSJason Wang 	if (vi->has_cvq) {
1459986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
1460986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1461f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1462986a4f4dSJason Wang 	}
1463986a4f4dSJason Wang 
1464986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1465986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
1466986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
1467986a4f4dSJason Wang 	}
1468986a4f4dSJason Wang 
1469986a4f4dSJason Wang 	kfree(names);
1470986a4f4dSJason Wang 	kfree(callbacks);
1471986a4f4dSJason Wang 	kfree(vqs);
1472986a4f4dSJason Wang 
1473986a4f4dSJason Wang 	return 0;
1474986a4f4dSJason Wang 
1475986a4f4dSJason Wang err_find:
1476986a4f4dSJason Wang 	kfree(names);
1477986a4f4dSJason Wang err_names:
1478986a4f4dSJason Wang 	kfree(callbacks);
1479986a4f4dSJason Wang err_callback:
1480986a4f4dSJason Wang 	kfree(vqs);
1481986a4f4dSJason Wang err_vq:
1482986a4f4dSJason Wang 	return ret;
1483986a4f4dSJason Wang }
1484986a4f4dSJason Wang 
1485986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
1486986a4f4dSJason Wang {
1487986a4f4dSJason Wang 	int i;
1488986a4f4dSJason Wang 
1489986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1490986a4f4dSJason Wang 	if (!vi->sq)
1491986a4f4dSJason Wang 		goto err_sq;
1492986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1493008d4278SAmerigo Wang 	if (!vi->rq)
1494986a4f4dSJason Wang 		goto err_rq;
1495986a4f4dSJason Wang 
1496986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
1497986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1498986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
1499986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1500986a4f4dSJason Wang 			       napi_weight);
1501986a4f4dSJason Wang 
1502986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1503986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1504986a4f4dSJason Wang 	}
1505986a4f4dSJason Wang 
1506986a4f4dSJason Wang 	return 0;
1507986a4f4dSJason Wang 
1508986a4f4dSJason Wang err_rq:
1509986a4f4dSJason Wang 	kfree(vi->sq);
1510986a4f4dSJason Wang err_sq:
1511986a4f4dSJason Wang 	return -ENOMEM;
1512e9d7417bSJason Wang }
1513e9d7417bSJason Wang 
15143f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
15153f9c10b0SAmit Shah {
1516986a4f4dSJason Wang 	int ret;
15173f9c10b0SAmit Shah 
1518986a4f4dSJason Wang 	/* Allocate send & receive queues */
1519986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
1520986a4f4dSJason Wang 	if (ret)
1521986a4f4dSJason Wang 		goto err;
15223f9c10b0SAmit Shah 
1523986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
1524986a4f4dSJason Wang 	if (ret)
1525986a4f4dSJason Wang 		goto err_free;
15263f9c10b0SAmit Shah 
152747be2479SWanlong Gao 	get_online_cpus();
15288898c21cSWanlong Gao 	virtnet_set_affinity(vi);
152947be2479SWanlong Gao 	put_online_cpus();
153047be2479SWanlong Gao 
15313f9c10b0SAmit Shah 	return 0;
1532986a4f4dSJason Wang 
1533986a4f4dSJason Wang err_free:
1534986a4f4dSJason Wang 	virtnet_free_queues(vi);
1535986a4f4dSJason Wang err:
1536986a4f4dSJason Wang 	return ret;
15373f9c10b0SAmit Shah }
15383f9c10b0SAmit Shah 
1539296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
1540296f96fcSRusty Russell {
1541986a4f4dSJason Wang 	int i, err;
1542296f96fcSRusty Russell 	struct net_device *dev;
1543296f96fcSRusty Russell 	struct virtnet_info *vi;
1544986a4f4dSJason Wang 	u16 max_queue_pairs;
1545986a4f4dSJason Wang 
1546986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
1547986a4f4dSJason Wang 	err = virtio_config_val(vdev, VIRTIO_NET_F_MQ,
1548986a4f4dSJason Wang 				offsetof(struct virtio_net_config,
1549986a4f4dSJason Wang 				max_virtqueue_pairs), &max_queue_pairs);
1550986a4f4dSJason Wang 
1551986a4f4dSJason Wang 	/* We need at least 2 queue's */
1552986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1553986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1554986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1555986a4f4dSJason Wang 		max_queue_pairs = 1;
1556296f96fcSRusty Russell 
1557296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
1558986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1559296f96fcSRusty Russell 	if (!dev)
1560296f96fcSRusty Russell 		return -ENOMEM;
1561296f96fcSRusty Russell 
1562296f96fcSRusty Russell 	/* Set up network device as normal. */
1563f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
156476288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
1565296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
15663fa2a1dfSstephen hemminger 
1567a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1568296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
1569296f96fcSRusty Russell 
1570296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
157198e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1572296f96fcSRusty Russell 		/* This opens up the world of extra features. */
157398e778c9SMichał Mirosław 		dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
157498e778c9SMichał Mirosław 		if (csum)
1575296f96fcSRusty Russell 			dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
157698e778c9SMichał Mirosław 
157798e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
157898e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
157934a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
158034a48579SRusty Russell 		}
15815539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
158298e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
158398e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
158498e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
158598e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
158698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
158798e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
158898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
158998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_UFO;
159098e778c9SMichał Mirosław 
159198e778c9SMichał Mirosław 		if (gso)
159298e778c9SMichał Mirosław 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
159398e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
1594296f96fcSRusty Russell 	}
15954f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
15964f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
1597296f96fcSRusty Russell 
15984fda8302SJason Wang 	dev->vlan_features = dev->features;
15994fda8302SJason Wang 
1600296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
160177dd7693SSasha Levin 	if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
1602a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
160377dd7693SSasha Levin 				  dev->dev_addr, dev->addr_len) < 0)
1604f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1605296f96fcSRusty Russell 
1606296f96fcSRusty Russell 	/* Set up our device-specific information */
1607296f96fcSRusty Russell 	vi = netdev_priv(dev);
1608296f96fcSRusty Russell 	vi->dev = dev;
1609296f96fcSRusty Russell 	vi->vdev = vdev;
1610d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
16113fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
16123fa2a1dfSstephen hemminger 	err = -ENOMEM;
16133fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
16143fa2a1dfSstephen hemminger 		goto free;
16153fa2a1dfSstephen hemminger 
161647be2479SWanlong Gao 	vi->vq_index = alloc_percpu(int);
161747be2479SWanlong Gao 	if (vi->vq_index == NULL)
161847be2479SWanlong Gao 		goto free_stats;
161947be2479SWanlong Gao 
1620586d17c5SJason Wang 	mutex_init(&vi->config_lock);
1621586d17c5SJason Wang 	vi->config_enable = true;
1622586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1623296f96fcSRusty Russell 
162497402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
16258e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
16268e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
16278e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
162897402b96SHerbert Xu 		vi->big_packets = true;
162997402b96SHerbert Xu 
16303f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
16313f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
16323f2c31d9SMark McLoughlin 
1633e7428e95SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1634e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
1635e7428e95SMichael S. Tsirkin 
1636986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1637986a4f4dSJason Wang 		vi->has_cvq = true;
1638986a4f4dSJason Wang 
1639986a4f4dSJason Wang 	/* Use single tx/rx queue pair as default */
1640986a4f4dSJason Wang 	vi->curr_queue_pairs = 1;
1641986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
1642986a4f4dSJason Wang 
1643986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
16443f9c10b0SAmit Shah 	err = init_vqs(vi);
1645d2a7dddaSMichael S. Tsirkin 	if (err)
164647be2479SWanlong Gao 		goto free_index;
1647d2a7dddaSMichael S. Tsirkin 
1648986a4f4dSJason Wang 	netif_set_real_num_tx_queues(dev, 1);
1649986a4f4dSJason Wang 	netif_set_real_num_rx_queues(dev, 1);
1650986a4f4dSJason Wang 
1651296f96fcSRusty Russell 	err = register_netdev(dev);
1652296f96fcSRusty Russell 	if (err) {
1653296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1654d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1655296f96fcSRusty Russell 	}
1656b3369c1fSRusty Russell 
1657b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
165855257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1659986a4f4dSJason Wang 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
1660b3369c1fSRusty Russell 
1661b3369c1fSRusty Russell 		/* If we didn't even get one input buffer, we're useless. */
1662986a4f4dSJason Wang 		if (vi->rq[i].num == 0) {
1663986a4f4dSJason Wang 			free_unused_bufs(vi);
1664b3369c1fSRusty Russell 			err = -ENOMEM;
1665986a4f4dSJason Wang 			goto free_recv_bufs;
1666986a4f4dSJason Wang 		}
1667b3369c1fSRusty Russell 	}
1668b3369c1fSRusty Russell 
16698de4b2f3SWanlong Gao 	vi->nb.notifier_call = &virtnet_cpu_callback;
16708de4b2f3SWanlong Gao 	err = register_hotcpu_notifier(&vi->nb);
16718de4b2f3SWanlong Gao 	if (err) {
16728de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
16738de4b2f3SWanlong Gao 		goto free_recv_bufs;
16748de4b2f3SWanlong Gao 	}
16758de4b2f3SWanlong Gao 
1676167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1677167c25e4SJason Wang 	   otherwise get link status from config. */
1678167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1679167c25e4SJason Wang 		netif_carrier_off(dev);
16803b07e9caSTejun Heo 		schedule_work(&vi->config_work);
1681167c25e4SJason Wang 	} else {
1682167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
16834783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1684167c25e4SJason Wang 	}
16859f4d26d0SMark McLoughlin 
1686986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1687986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
1688986a4f4dSJason Wang 
1689296f96fcSRusty Russell 	return 0;
1690296f96fcSRusty Russell 
1691986a4f4dSJason Wang free_recv_bufs:
1692986a4f4dSJason Wang 	free_receive_bufs(vi);
1693b3369c1fSRusty Russell 	unregister_netdev(dev);
1694d2a7dddaSMichael S. Tsirkin free_vqs:
1695986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
1696e9d7417bSJason Wang 	virtnet_del_vqs(vi);
16972613af0eSMichael Dalton 	if (vi->alloc_frag.page)
16982613af0eSMichael Dalton 		put_page(vi->alloc_frag.page);
169947be2479SWanlong Gao free_index:
170047be2479SWanlong Gao 	free_percpu(vi->vq_index);
17013fa2a1dfSstephen hemminger free_stats:
17023fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1703296f96fcSRusty Russell free:
1704296f96fcSRusty Russell 	free_netdev(dev);
1705296f96fcSRusty Russell 	return err;
1706296f96fcSRusty Russell }
1707296f96fcSRusty Russell 
170804486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
1709296f96fcSRusty Russell {
171004486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
1711830a8a97SShirley Ma 
1712830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
17139ab86bbcSShirley Ma 	free_unused_bufs(vi);
1714fb6813f4SRusty Russell 
1715986a4f4dSJason Wang 	free_receive_bufs(vi);
1716d2a7dddaSMichael S. Tsirkin 
1717986a4f4dSJason Wang 	virtnet_del_vqs(vi);
171804486ed0SAmit Shah }
171904486ed0SAmit Shah 
17208cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
172104486ed0SAmit Shah {
172204486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
172304486ed0SAmit Shah 
17248de4b2f3SWanlong Gao 	unregister_hotcpu_notifier(&vi->nb);
17258de4b2f3SWanlong Gao 
1726586d17c5SJason Wang 	/* Prevent config work handler from accessing the device. */
1727586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1728586d17c5SJason Wang 	vi->config_enable = false;
1729586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1730586d17c5SJason Wang 
173104486ed0SAmit Shah 	unregister_netdev(vi->dev);
173204486ed0SAmit Shah 
173304486ed0SAmit Shah 	remove_vq_common(vi);
17342613af0eSMichael Dalton 	if (vi->alloc_frag.page)
17352613af0eSMichael Dalton 		put_page(vi->alloc_frag.page);
1736fb6813f4SRusty Russell 
1737586d17c5SJason Wang 	flush_work(&vi->config_work);
1738586d17c5SJason Wang 
173947be2479SWanlong Gao 	free_percpu(vi->vq_index);
17402e66f55bSKrishna Kumar 	free_percpu(vi->stats);
174174b2553fSRusty Russell 	free_netdev(vi->dev);
1742296f96fcSRusty Russell }
1743296f96fcSRusty Russell 
17440741bcb5SAmit Shah #ifdef CONFIG_PM
17450741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
17460741bcb5SAmit Shah {
17470741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1748986a4f4dSJason Wang 	int i;
17490741bcb5SAmit Shah 
1750ec9debbdSJason Wang 	unregister_hotcpu_notifier(&vi->nb);
1751ec9debbdSJason Wang 
1752586d17c5SJason Wang 	/* Prevent config work handler from accessing the device */
1753586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1754586d17c5SJason Wang 	vi->config_enable = false;
1755586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1756586d17c5SJason Wang 
17570741bcb5SAmit Shah 	netif_device_detach(vi->dev);
17580741bcb5SAmit Shah 	cancel_delayed_work_sync(&vi->refill);
17590741bcb5SAmit Shah 
17600741bcb5SAmit Shah 	if (netif_running(vi->dev))
1761986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++) {
1762986a4f4dSJason Wang 			napi_disable(&vi->rq[i].napi);
1763986a4f4dSJason Wang 			netif_napi_del(&vi->rq[i].napi);
1764986a4f4dSJason Wang 		}
17650741bcb5SAmit Shah 
17660741bcb5SAmit Shah 	remove_vq_common(vi);
17670741bcb5SAmit Shah 
1768586d17c5SJason Wang 	flush_work(&vi->config_work);
1769586d17c5SJason Wang 
17700741bcb5SAmit Shah 	return 0;
17710741bcb5SAmit Shah }
17720741bcb5SAmit Shah 
17730741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
17740741bcb5SAmit Shah {
17750741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1776986a4f4dSJason Wang 	int err, i;
17770741bcb5SAmit Shah 
17780741bcb5SAmit Shah 	err = init_vqs(vi);
17790741bcb5SAmit Shah 	if (err)
17800741bcb5SAmit Shah 		return err;
17810741bcb5SAmit Shah 
17820741bcb5SAmit Shah 	if (netif_running(vi->dev))
1783986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
1784986a4f4dSJason Wang 			virtnet_napi_enable(&vi->rq[i]);
17850741bcb5SAmit Shah 
17860741bcb5SAmit Shah 	netif_device_attach(vi->dev);
17870741bcb5SAmit Shah 
178855257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++)
1789986a4f4dSJason Wang 		if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
17903b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
17910741bcb5SAmit Shah 
1792586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1793586d17c5SJason Wang 	vi->config_enable = true;
1794586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1795586d17c5SJason Wang 
179635ed159bSJason Wang 	rtnl_lock();
1797986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
179835ed159bSJason Wang 	rtnl_unlock();
1799986a4f4dSJason Wang 
1800ec9debbdSJason Wang 	err = register_hotcpu_notifier(&vi->nb);
1801ec9debbdSJason Wang 	if (err)
1802ec9debbdSJason Wang 		return err;
1803ec9debbdSJason Wang 
18040741bcb5SAmit Shah 	return 0;
18050741bcb5SAmit Shah }
18060741bcb5SAmit Shah #endif
18070741bcb5SAmit Shah 
1808296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
1809296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1810296f96fcSRusty Russell 	{ 0 },
1811296f96fcSRusty Russell };
1812296f96fcSRusty Russell 
1813c45a6816SRusty Russell static unsigned int features[] = {
18145e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
18155e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1816c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
181797402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
18185c516751SSridhar Samudrala 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
18192a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
18200bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1821986a4f4dSJason Wang 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
18227e58d5aeSAmos Kong 	VIRTIO_NET_F_CTRL_MAC_ADDR,
1823e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
1824c45a6816SRusty Russell };
1825c45a6816SRusty Russell 
182622402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
1827c45a6816SRusty Russell 	.feature_table = features,
1828c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
1829296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
1830296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
1831296f96fcSRusty Russell 	.id_table =	id_table,
1832296f96fcSRusty Russell 	.probe =	virtnet_probe,
18338cc085d6SBill Pemberton 	.remove =	virtnet_remove,
18349f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
18350741bcb5SAmit Shah #ifdef CONFIG_PM
18360741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
18370741bcb5SAmit Shah 	.restore =	virtnet_restore,
18380741bcb5SAmit Shah #endif
1839296f96fcSRusty Russell };
1840296f96fcSRusty Russell 
1841b2a17029SRusty Russell module_virtio_driver(virtio_net_driver);
1842296f96fcSRusty Russell 
1843296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
1844296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
1845296f96fcSRusty Russell MODULE_LICENSE("GPL");
1846