xref: /openbmc/linux/drivers/net/virtio_net.c (revision f646968f8f7c624587de729115d802372b9063dd)
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 
316c0cd7c0SDor Laor static int napi_weight = 128;
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 
42f565a7c2SAlex Williamson #define VIRTNET_SEND_COMMAND_SG_MAX    2
4366846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
442a41f71dSAlex Williamson 
453fa2a1dfSstephen hemminger struct virtnet_stats {
4683a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
4783a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
483fa2a1dfSstephen hemminger 	u64 tx_bytes;
493fa2a1dfSstephen hemminger 	u64 tx_packets;
503fa2a1dfSstephen hemminger 
513fa2a1dfSstephen hemminger 	u64 rx_bytes;
523fa2a1dfSstephen hemminger 	u64 rx_packets;
533fa2a1dfSstephen hemminger };
543fa2a1dfSstephen hemminger 
55e9d7417bSJason Wang /* Internal representation of a send virtqueue */
56e9d7417bSJason Wang struct send_queue {
57e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
58e9d7417bSJason Wang 	struct virtqueue *vq;
59e9d7417bSJason Wang 
60e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
61e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
62986a4f4dSJason Wang 
63986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
64986a4f4dSJason Wang 	char name[40];
65e9d7417bSJason Wang };
66e9d7417bSJason Wang 
67e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
68e9d7417bSJason Wang struct receive_queue {
69e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
70e9d7417bSJason Wang 	struct virtqueue *vq;
71e9d7417bSJason Wang 
72296f96fcSRusty Russell 	struct napi_struct napi;
73296f96fcSRusty Russell 
74296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
75296f96fcSRusty Russell 	unsigned int num, max;
76296f96fcSRusty Russell 
77e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
78e9d7417bSJason Wang 	struct page *pages;
79e9d7417bSJason Wang 
80e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
81e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
82986a4f4dSJason Wang 
83986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
84986a4f4dSJason Wang 	char name[40];
85e9d7417bSJason Wang };
86e9d7417bSJason Wang 
87e9d7417bSJason Wang struct virtnet_info {
88e9d7417bSJason Wang 	struct virtio_device *vdev;
89e9d7417bSJason Wang 	struct virtqueue *cvq;
90e9d7417bSJason Wang 	struct net_device *dev;
91986a4f4dSJason Wang 	struct send_queue *sq;
92986a4f4dSJason Wang 	struct receive_queue *rq;
93e9d7417bSJason Wang 	unsigned int status;
94e9d7417bSJason Wang 
95986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
96986a4f4dSJason Wang 	u16 max_queue_pairs;
97986a4f4dSJason Wang 
98986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
99986a4f4dSJason Wang 	u16 curr_queue_pairs;
100986a4f4dSJason Wang 
10197402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
10297402b96SHerbert Xu 	bool big_packets;
10397402b96SHerbert Xu 
1043f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1053f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1063f2c31d9SMark McLoughlin 
107986a4f4dSJason Wang 	/* Has control virtqueue */
108986a4f4dSJason Wang 	bool has_cvq;
109986a4f4dSJason Wang 
110586d17c5SJason Wang 	/* enable config space updates */
111586d17c5SJason Wang 	bool config_enable;
112586d17c5SJason Wang 
1133fa2a1dfSstephen hemminger 	/* Active statistics */
1143fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1153fa2a1dfSstephen hemminger 
1163161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1173161e453SRusty Russell 	struct delayed_work refill;
1183161e453SRusty Russell 
119586d17c5SJason Wang 	/* Work struct for config space updates */
120586d17c5SJason Wang 	struct work_struct config_work;
121586d17c5SJason Wang 
122586d17c5SJason Wang 	/* Lock for config space updates */
123586d17c5SJason Wang 	struct mutex config_lock;
124986a4f4dSJason Wang 
125986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
126986a4f4dSJason Wang 	bool affinity_hint_set;
12747be2479SWanlong Gao 
12847be2479SWanlong Gao 	/* Per-cpu variable to show the mapping from CPU to virtqueue */
12947be2479SWanlong Gao 	int __percpu *vq_index;
1308de4b2f3SWanlong Gao 
1318de4b2f3SWanlong Gao 	/* CPU hot plug notifier */
1328de4b2f3SWanlong Gao 	struct notifier_block nb;
133296f96fcSRusty Russell };
134296f96fcSRusty Russell 
135b3f24698SRusty Russell struct skb_vnet_hdr {
136b3f24698SRusty Russell 	union {
137b3f24698SRusty Russell 		struct virtio_net_hdr hdr;
138b3f24698SRusty Russell 		struct virtio_net_hdr_mrg_rxbuf mhdr;
139b3f24698SRusty Russell 	};
140b3f24698SRusty Russell };
141b3f24698SRusty Russell 
1429ab86bbcSShirley Ma struct padded_vnet_hdr {
1439ab86bbcSShirley Ma 	struct virtio_net_hdr hdr;
1449ab86bbcSShirley Ma 	/*
1459ab86bbcSShirley Ma 	 * virtio_net_hdr should be in a separated sg buffer because of a
1469ab86bbcSShirley Ma 	 * QEMU bug, and data sg buffer shares same page with this header sg.
1479ab86bbcSShirley Ma 	 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
1489ab86bbcSShirley Ma 	 */
1499ab86bbcSShirley Ma 	char padding[6];
1509ab86bbcSShirley Ma };
1519ab86bbcSShirley Ma 
152986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
153986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
154986a4f4dSJason Wang  */
155986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
156986a4f4dSJason Wang {
1579d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
158986a4f4dSJason Wang }
159986a4f4dSJason Wang 
160986a4f4dSJason Wang static int txq2vq(int txq)
161986a4f4dSJason Wang {
162986a4f4dSJason Wang 	return txq * 2 + 1;
163986a4f4dSJason Wang }
164986a4f4dSJason Wang 
165986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
166986a4f4dSJason Wang {
1679d0ca6edSRusty Russell 	return vq->index / 2;
168986a4f4dSJason Wang }
169986a4f4dSJason Wang 
170986a4f4dSJason Wang static int rxq2vq(int rxq)
171986a4f4dSJason Wang {
172986a4f4dSJason Wang 	return rxq * 2;
173986a4f4dSJason Wang }
174986a4f4dSJason Wang 
175b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
176296f96fcSRusty Russell {
177b3f24698SRusty Russell 	return (struct skb_vnet_hdr *)skb->cb;
178296f96fcSRusty Russell }
179296f96fcSRusty Russell 
1809ab86bbcSShirley Ma /*
1819ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
1829ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
1839ab86bbcSShirley Ma  */
184e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
185fb6813f4SRusty Russell {
1869ab86bbcSShirley Ma 	struct page *end;
1879ab86bbcSShirley Ma 
188e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
1899ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
190e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
191e9d7417bSJason Wang 	rq->pages = page;
192fb6813f4SRusty Russell }
193fb6813f4SRusty Russell 
194e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
195fb6813f4SRusty Russell {
196e9d7417bSJason Wang 	struct page *p = rq->pages;
197fb6813f4SRusty Russell 
1989ab86bbcSShirley Ma 	if (p) {
199e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2009ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2019ab86bbcSShirley Ma 		p->private = 0;
2029ab86bbcSShirley Ma 	} else
203fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
204fb6813f4SRusty Russell 	return p;
205fb6813f4SRusty Russell }
206fb6813f4SRusty Russell 
207e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
208296f96fcSRusty Russell {
209e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
210296f96fcSRusty Russell 
2112cb9c6baSRusty Russell 	/* Suppress further interrupts. */
212e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
21311a3a154SRusty Russell 
214363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
215986a4f4dSJason Wang 	netif_wake_subqueue(vi->dev, vq2txq(vq));
216296f96fcSRusty Russell }
217296f96fcSRusty Russell 
2189ab86bbcSShirley Ma static void set_skb_frag(struct sk_buff *skb, struct page *page,
2199ab86bbcSShirley Ma 			 unsigned int offset, unsigned int *len)
220296f96fcSRusty Russell {
2218a59a7b9SKrishna Kumar 	int size = min((unsigned)PAGE_SIZE - offset, *len);
2229ab86bbcSShirley Ma 	int i = skb_shinfo(skb)->nr_frags;
223296f96fcSRusty Russell 
2248a59a7b9SKrishna Kumar 	__skb_fill_page_desc(skb, i, page, offset, size);
2259ab86bbcSShirley Ma 
2268a59a7b9SKrishna Kumar 	skb->data_len += size;
2278a59a7b9SKrishna Kumar 	skb->len += size;
2284b727361SEric Dumazet 	skb->truesize += PAGE_SIZE;
2299ab86bbcSShirley Ma 	skb_shinfo(skb)->nr_frags++;
230c9af6db4SPravin B Shelar 	skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
2318a59a7b9SKrishna Kumar 	*len -= size;
232296f96fcSRusty Russell }
2333f2c31d9SMark McLoughlin 
2343464645aSMike Waychison /* Called from bottom half context */
235e9d7417bSJason Wang static struct sk_buff *page_to_skb(struct receive_queue *rq,
2369ab86bbcSShirley Ma 				   struct page *page, unsigned int len)
2379ab86bbcSShirley Ma {
238e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
2399ab86bbcSShirley Ma 	struct sk_buff *skb;
2409ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
2419ab86bbcSShirley Ma 	unsigned int copy, hdr_len, offset;
2429ab86bbcSShirley Ma 	char *p;
2439ab86bbcSShirley Ma 
2449ab86bbcSShirley Ma 	p = page_address(page);
2459ab86bbcSShirley Ma 
2469ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
2479ab86bbcSShirley Ma 	skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
2489ab86bbcSShirley Ma 	if (unlikely(!skb))
2499ab86bbcSShirley Ma 		return NULL;
2509ab86bbcSShirley Ma 
2519ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
2529ab86bbcSShirley Ma 
2533f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs) {
2549ab86bbcSShirley Ma 		hdr_len = sizeof hdr->mhdr;
2559ab86bbcSShirley Ma 		offset = hdr_len;
2569ab86bbcSShirley Ma 	} else {
2579ab86bbcSShirley Ma 		hdr_len = sizeof hdr->hdr;
2589ab86bbcSShirley Ma 		offset = sizeof(struct padded_vnet_hdr);
2599ab86bbcSShirley Ma 	}
2603f2c31d9SMark McLoughlin 
2619ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
2623f2c31d9SMark McLoughlin 
2639ab86bbcSShirley Ma 	len -= hdr_len;
2649ab86bbcSShirley Ma 	p += offset;
2653f2c31d9SMark McLoughlin 
2663f2c31d9SMark McLoughlin 	copy = len;
2673f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
2683f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
2693f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
2703f2c31d9SMark McLoughlin 
2713f2c31d9SMark McLoughlin 	len -= copy;
2729ab86bbcSShirley Ma 	offset += copy;
2733f2c31d9SMark McLoughlin 
274e878d78bSSasha Levin 	/*
275e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
276e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
277e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
278e878d78bSSasha Levin 	 * the case of a broken device.
279e878d78bSSasha Levin 	 */
280e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
281be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
282e878d78bSSasha Levin 		dev_kfree_skb(skb);
283e878d78bSSasha Levin 		return NULL;
284e878d78bSSasha Levin 	}
285e878d78bSSasha Levin 
2869ab86bbcSShirley Ma 	while (len) {
2879ab86bbcSShirley Ma 		set_skb_frag(skb, page, offset, &len);
2889ab86bbcSShirley Ma 		page = (struct page *)page->private;
2899ab86bbcSShirley Ma 		offset = 0;
2903f2c31d9SMark McLoughlin 	}
2913f2c31d9SMark McLoughlin 
2929ab86bbcSShirley Ma 	if (page)
293e9d7417bSJason Wang 		give_pages(rq, page);
2943f2c31d9SMark McLoughlin 
2959ab86bbcSShirley Ma 	return skb;
2969ab86bbcSShirley Ma }
2979ab86bbcSShirley Ma 
298e9d7417bSJason Wang static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb)
2999ab86bbcSShirley Ma {
3009ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
3019ab86bbcSShirley Ma 	struct page *page;
3029ab86bbcSShirley Ma 	int num_buf, i, len;
3039ab86bbcSShirley Ma 
3049ab86bbcSShirley Ma 	num_buf = hdr->mhdr.num_buffers;
3059ab86bbcSShirley Ma 	while (--num_buf) {
3063f2c31d9SMark McLoughlin 		i = skb_shinfo(skb)->nr_frags;
3073f2c31d9SMark McLoughlin 		if (i >= MAX_SKB_FRAGS) {
3089ab86bbcSShirley Ma 			pr_debug("%s: packet too long\n", skb->dev->name);
3099ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
3109ab86bbcSShirley Ma 			return -EINVAL;
3113f2c31d9SMark McLoughlin 		}
312e9d7417bSJason Wang 		page = virtqueue_get_buf(rq->vq, &len);
3139ab86bbcSShirley Ma 		if (!page) {
3143f2c31d9SMark McLoughlin 			pr_debug("%s: rx error: %d buffers missing\n",
3159ab86bbcSShirley Ma 				 skb->dev->name, hdr->mhdr.num_buffers);
3169ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
3179ab86bbcSShirley Ma 			return -EINVAL;
3183f2c31d9SMark McLoughlin 		}
3193fa2a1dfSstephen hemminger 
3203f2c31d9SMark McLoughlin 		if (len > PAGE_SIZE)
3213f2c31d9SMark McLoughlin 			len = PAGE_SIZE;
3223f2c31d9SMark McLoughlin 
3239ab86bbcSShirley Ma 		set_skb_frag(skb, page, 0, &len);
3249ab86bbcSShirley Ma 
325e9d7417bSJason Wang 		--rq->num;
3263f2c31d9SMark McLoughlin 	}
3279ab86bbcSShirley Ma 	return 0;
3289ab86bbcSShirley Ma }
3299ab86bbcSShirley Ma 
330e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
3319ab86bbcSShirley Ma {
332e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
333e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
33458472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
3359ab86bbcSShirley Ma 	struct sk_buff *skb;
3369ab86bbcSShirley Ma 	struct page *page;
3379ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
3389ab86bbcSShirley Ma 
3399ab86bbcSShirley Ma 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
3409ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
3419ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
3429ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs || vi->big_packets)
343e9d7417bSJason Wang 			give_pages(rq, buf);
3449ab86bbcSShirley Ma 		else
3459ab86bbcSShirley Ma 			dev_kfree_skb(buf);
3469ab86bbcSShirley Ma 		return;
3479ab86bbcSShirley Ma 	}
3489ab86bbcSShirley Ma 
3499ab86bbcSShirley Ma 	if (!vi->mergeable_rx_bufs && !vi->big_packets) {
3509ab86bbcSShirley Ma 		skb = buf;
3519ab86bbcSShirley Ma 		len -= sizeof(struct virtio_net_hdr);
3529ab86bbcSShirley Ma 		skb_trim(skb, len);
3533f2c31d9SMark McLoughlin 	} else {
3549ab86bbcSShirley Ma 		page = buf;
355e9d7417bSJason Wang 		skb = page_to_skb(rq, page, len);
3569ab86bbcSShirley Ma 		if (unlikely(!skb)) {
35797402b96SHerbert Xu 			dev->stats.rx_dropped++;
358e9d7417bSJason Wang 			give_pages(rq, page);
3599ab86bbcSShirley Ma 			return;
3609ab86bbcSShirley Ma 		}
3619ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
362e9d7417bSJason Wang 			if (receive_mergeable(rq, skb)) {
3639ab86bbcSShirley Ma 				dev_kfree_skb(skb);
3649ab86bbcSShirley Ma 				return;
36597402b96SHerbert Xu 			}
3663f2c31d9SMark McLoughlin 	}
3673f2c31d9SMark McLoughlin 
3689ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
3693fa2a1dfSstephen hemminger 
37083a27052SEric Dumazet 	u64_stats_update_begin(&stats->rx_syncp);
3713fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
3723fa2a1dfSstephen hemminger 	stats->rx_packets++;
37383a27052SEric Dumazet 	u64_stats_update_end(&stats->rx_syncp);
374296f96fcSRusty Russell 
375b3f24698SRusty Russell 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
376296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
377b3f24698SRusty Russell 		if (!skb_partial_csum_set(skb,
378b3f24698SRusty Russell 					  hdr->hdr.csum_start,
379b3f24698SRusty Russell 					  hdr->hdr.csum_offset))
380296f96fcSRusty Russell 			goto frame_err;
38110a8d94aSJason Wang 	} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
38210a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
383296f96fcSRusty Russell 	}
384296f96fcSRusty Russell 
38523cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
38623cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
38723cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
38823cde76dSMark McLoughlin 
389b3f24698SRusty Russell 	if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
390296f96fcSRusty Russell 		pr_debug("GSO!\n");
391b3f24698SRusty Russell 		switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
392296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
393c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
394296f96fcSRusty Russell 			break;
395296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
396c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
397296f96fcSRusty Russell 			break;
398296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
399c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
400296f96fcSRusty Russell 			break;
401296f96fcSRusty Russell 		default:
402be443899SAmerigo Wang 			net_warn_ratelimited("%s: bad gso type %u.\n",
403b3f24698SRusty Russell 					     dev->name, hdr->hdr.gso_type);
404296f96fcSRusty Russell 			goto frame_err;
405296f96fcSRusty Russell 		}
406296f96fcSRusty Russell 
407b3f24698SRusty Russell 		if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
408c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
40934a48579SRusty Russell 
410b3f24698SRusty Russell 		skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
411296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
412be443899SAmerigo Wang 			net_warn_ratelimited("%s: zero gso size.\n", dev->name);
413296f96fcSRusty Russell 			goto frame_err;
414296f96fcSRusty Russell 		}
415296f96fcSRusty Russell 
416296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
417296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
418296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
419296f96fcSRusty Russell 	}
420296f96fcSRusty Russell 
421296f96fcSRusty Russell 	netif_receive_skb(skb);
422296f96fcSRusty Russell 	return;
423296f96fcSRusty Russell 
424296f96fcSRusty Russell frame_err:
425296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
426296f96fcSRusty Russell 	dev_kfree_skb(skb);
427296f96fcSRusty Russell }
428296f96fcSRusty Russell 
429e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
430296f96fcSRusty Russell {
431e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
432296f96fcSRusty Russell 	struct sk_buff *skb;
433b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr;
4349ab86bbcSShirley Ma 	int err;
4353f2c31d9SMark McLoughlin 
4363464645aSMike Waychison 	skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
4379ab86bbcSShirley Ma 	if (unlikely(!skb))
4389ab86bbcSShirley Ma 		return -ENOMEM;
439296f96fcSRusty Russell 
440296f96fcSRusty Russell 	skb_put(skb, MAX_PACKET_LEN);
4413f2c31d9SMark McLoughlin 
4423f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
443e9d7417bSJason Wang 	sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
44497402b96SHerbert Xu 
445e9d7417bSJason Wang 	skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
44697402b96SHerbert Xu 
447e9d7417bSJason Wang 	err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb, gfp);
4489ab86bbcSShirley Ma 	if (err < 0)
4499ab86bbcSShirley Ma 		dev_kfree_skb(skb);
45097402b96SHerbert Xu 
4519ab86bbcSShirley Ma 	return err;
45297402b96SHerbert Xu }
45397402b96SHerbert Xu 
454e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
4559ab86bbcSShirley Ma {
4569ab86bbcSShirley Ma 	struct page *first, *list = NULL;
4579ab86bbcSShirley Ma 	char *p;
4589ab86bbcSShirley Ma 	int i, err, offset;
459296f96fcSRusty Russell 
460e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
4619ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
462e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
4639ab86bbcSShirley Ma 		if (!first) {
4649ab86bbcSShirley Ma 			if (list)
465e9d7417bSJason Wang 				give_pages(rq, list);
4669ab86bbcSShirley Ma 			return -ENOMEM;
467296f96fcSRusty Russell 		}
468e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
4699ab86bbcSShirley Ma 
4709ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
4719ab86bbcSShirley Ma 		first->private = (unsigned long)list;
4729ab86bbcSShirley Ma 		list = first;
4739ab86bbcSShirley Ma 	}
4749ab86bbcSShirley Ma 
475e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
4769ab86bbcSShirley Ma 	if (!first) {
477e9d7417bSJason Wang 		give_pages(rq, list);
4789ab86bbcSShirley Ma 		return -ENOMEM;
4799ab86bbcSShirley Ma 	}
4809ab86bbcSShirley Ma 	p = page_address(first);
4819ab86bbcSShirley Ma 
482e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
483e9d7417bSJason Wang 	/* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
484e9d7417bSJason Wang 	sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
4859ab86bbcSShirley Ma 
486e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
4879ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
488e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
4899ab86bbcSShirley Ma 
4909ab86bbcSShirley Ma 	/* chain first in list head */
4919ab86bbcSShirley Ma 	first->private = (unsigned long)list;
492e9d7417bSJason Wang 	err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2,
493aa989f5eSMichael S. Tsirkin 				first, gfp);
4949ab86bbcSShirley Ma 	if (err < 0)
495e9d7417bSJason Wang 		give_pages(rq, first);
4969ab86bbcSShirley Ma 
4979ab86bbcSShirley Ma 	return err;
4989ab86bbcSShirley Ma }
4999ab86bbcSShirley Ma 
500e9d7417bSJason Wang static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
5019ab86bbcSShirley Ma {
5029ab86bbcSShirley Ma 	struct page *page;
5039ab86bbcSShirley Ma 	int err;
5049ab86bbcSShirley Ma 
505e9d7417bSJason Wang 	page = get_a_page(rq, gfp);
5069ab86bbcSShirley Ma 	if (!page)
5079ab86bbcSShirley Ma 		return -ENOMEM;
5089ab86bbcSShirley Ma 
509e9d7417bSJason Wang 	sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
5109ab86bbcSShirley Ma 
511e9d7417bSJason Wang 	err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page, gfp);
5129ab86bbcSShirley Ma 	if (err < 0)
513e9d7417bSJason Wang 		give_pages(rq, page);
5149ab86bbcSShirley Ma 
5159ab86bbcSShirley Ma 	return err;
516296f96fcSRusty Russell }
517296f96fcSRusty Russell 
518b2baed69SRusty Russell /*
519b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
520b2baed69SRusty Russell  *
521b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
522b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
523b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
524b2baed69SRusty Russell  */
525e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
5263f2c31d9SMark McLoughlin {
527e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
5283f2c31d9SMark McLoughlin 	int err;
5291788f495SMichael S. Tsirkin 	bool oom;
5303f2c31d9SMark McLoughlin 
5310aea51c3SAmit Shah 	do {
5329ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
533e9d7417bSJason Wang 			err = add_recvbuf_mergeable(rq, gfp);
5349ab86bbcSShirley Ma 		else if (vi->big_packets)
535e9d7417bSJason Wang 			err = add_recvbuf_big(rq, gfp);
5369ab86bbcSShirley Ma 		else
537e9d7417bSJason Wang 			err = add_recvbuf_small(rq, gfp);
5383f2c31d9SMark McLoughlin 
5391788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
5409ed4cb07SRusty Russell 		if (err)
5413f2c31d9SMark McLoughlin 			break;
542e9d7417bSJason Wang 		++rq->num;
543b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
544e9d7417bSJason Wang 	if (unlikely(rq->num > rq->max))
545e9d7417bSJason Wang 		rq->max = rq->num;
546e9d7417bSJason Wang 	virtqueue_kick(rq->vq);
5473161e453SRusty Russell 	return !oom;
5483f2c31d9SMark McLoughlin }
5493f2c31d9SMark McLoughlin 
55018445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
551296f96fcSRusty Russell {
552296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
553986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
554e9d7417bSJason Wang 
55518445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
556e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
5571915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
558e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
55918445c4dSRusty Russell 	}
560296f96fcSRusty Russell }
561296f96fcSRusty Russell 
562e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
5633e9d08ecSBruce Rogers {
564e9d7417bSJason Wang 	napi_enable(&rq->napi);
5653e9d08ecSBruce Rogers 
5663e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
5673e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
5683e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
5693e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
570e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
571e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
572ec13ee80SMichael S. Tsirkin 		local_bh_disable();
573e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
574ec13ee80SMichael S. Tsirkin 		local_bh_enable();
5753e9d08ecSBruce Rogers 	}
5763e9d08ecSBruce Rogers }
5773e9d08ecSBruce Rogers 
5783161e453SRusty Russell static void refill_work(struct work_struct *work)
5793161e453SRusty Russell {
580e9d7417bSJason Wang 	struct virtnet_info *vi =
581e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
5823161e453SRusty Russell 	bool still_empty;
583986a4f4dSJason Wang 	int i;
5843161e453SRusty Russell 
585986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
586986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
587986a4f4dSJason Wang 
588986a4f4dSJason Wang 		napi_disable(&rq->napi);
589986a4f4dSJason Wang 		still_empty = !try_fill_recv(rq, GFP_KERNEL);
590986a4f4dSJason Wang 		virtnet_napi_enable(rq);
5913161e453SRusty Russell 
5923161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
593986a4f4dSJason Wang 		 * we will *never* try to fill again.
594986a4f4dSJason Wang 		 */
5953161e453SRusty Russell 		if (still_empty)
5963b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
5973161e453SRusty Russell 	}
598986a4f4dSJason Wang }
5993161e453SRusty Russell 
600296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
601296f96fcSRusty Russell {
602e9d7417bSJason Wang 	struct receive_queue *rq =
603e9d7417bSJason Wang 		container_of(napi, struct receive_queue, napi);
604e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
6059ab86bbcSShirley Ma 	void *buf;
606296f96fcSRusty Russell 	unsigned int len, received = 0;
607296f96fcSRusty Russell 
608296f96fcSRusty Russell again:
609296f96fcSRusty Russell 	while (received < budget &&
610e9d7417bSJason Wang 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
611e9d7417bSJason Wang 		receive_buf(rq, buf, len);
612e9d7417bSJason Wang 		--rq->num;
613296f96fcSRusty Russell 		received++;
614296f96fcSRusty Russell 	}
615296f96fcSRusty Russell 
616e9d7417bSJason Wang 	if (rq->num < rq->max / 2) {
617e9d7417bSJason Wang 		if (!try_fill_recv(rq, GFP_ATOMIC))
6183b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
6193161e453SRusty Russell 	}
620296f96fcSRusty Russell 
6218329d98eSRusty Russell 	/* Out of packets? */
6228329d98eSRusty Russell 	if (received < budget) {
623288379f0SBen Hutchings 		napi_complete(napi);
624e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb(rq->vq)) &&
6258e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
626e9d7417bSJason Wang 			virtqueue_disable_cb(rq->vq);
627288379f0SBen Hutchings 			__napi_schedule(napi);
628296f96fcSRusty Russell 			goto again;
629296f96fcSRusty Russell 		}
6304265f161SChristian Borntraeger 	}
631296f96fcSRusty Russell 
632296f96fcSRusty Russell 	return received;
633296f96fcSRusty Russell }
634296f96fcSRusty Russell 
635986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
636986a4f4dSJason Wang {
637986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
638986a4f4dSJason Wang 	int i;
639986a4f4dSJason Wang 
640986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
641986a4f4dSJason Wang 		/* Make sure we have some buffers: if oom use wq. */
642986a4f4dSJason Wang 		if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
643986a4f4dSJason Wang 			schedule_delayed_work(&vi->refill, 0);
644986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
645986a4f4dSJason Wang 	}
646986a4f4dSJason Wang 
647986a4f4dSJason Wang 	return 0;
648986a4f4dSJason Wang }
649986a4f4dSJason Wang 
650b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
651296f96fcSRusty Russell {
652296f96fcSRusty Russell 	struct sk_buff *skb;
6536ee57bccSMichael S. Tsirkin 	unsigned int len;
654e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
65558472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
656296f96fcSRusty Russell 
657e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
658296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
6593fa2a1dfSstephen hemminger 
66083a27052SEric Dumazet 		u64_stats_update_begin(&stats->tx_syncp);
6613fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
6623fa2a1dfSstephen hemminger 		stats->tx_packets++;
66383a27052SEric Dumazet 		u64_stats_update_end(&stats->tx_syncp);
6643fa2a1dfSstephen hemminger 
665ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
666296f96fcSRusty Russell 	}
667296f96fcSRusty Russell }
668296f96fcSRusty Russell 
669e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
670296f96fcSRusty Russell {
671b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
672296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
673e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
6747bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
675296f96fcSRusty Russell 
676e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
677296f96fcSRusty Russell 
678296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
679b3f24698SRusty Russell 		hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
68055508d60SMichał Mirosław 		hdr->hdr.csum_start = skb_checksum_start_offset(skb);
681b3f24698SRusty Russell 		hdr->hdr.csum_offset = skb->csum_offset;
682296f96fcSRusty Russell 	} else {
683b3f24698SRusty Russell 		hdr->hdr.flags = 0;
684b3f24698SRusty Russell 		hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
685296f96fcSRusty Russell 	}
686296f96fcSRusty Russell 
687296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
688b3f24698SRusty Russell 		hdr->hdr.hdr_len = skb_headlen(skb);
689b3f24698SRusty Russell 		hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
69034a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
691b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
692296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
693b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
694296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
695b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
696296f96fcSRusty Russell 		else
697296f96fcSRusty Russell 			BUG();
69834a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
699b3f24698SRusty Russell 			hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
700296f96fcSRusty Russell 	} else {
701b3f24698SRusty Russell 		hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
702b3f24698SRusty Russell 		hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
703296f96fcSRusty Russell 	}
704296f96fcSRusty Russell 
705b3f24698SRusty Russell 	hdr->mhdr.num_buffers = 0;
7063f2c31d9SMark McLoughlin 
7073f2c31d9SMark McLoughlin 	/* Encode metadata header at front. */
7083f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs)
709e9d7417bSJason Wang 		sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr);
7103f2c31d9SMark McLoughlin 	else
711e9d7417bSJason Wang 		sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
7123f2c31d9SMark McLoughlin 
713b7dfde95SLinus Torvalds 	num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
714b7dfde95SLinus Torvalds 	return virtqueue_add_buf(sq->vq, sq->sg, num_sg,
715f96fde41SRusty Russell 				 0, skb, GFP_ATOMIC);
71611a3a154SRusty Russell }
71711a3a154SRusty Russell 
718424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
71999ffc696SRusty Russell {
72099ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
721986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
722986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
7239ed4cb07SRusty Russell 	int err;
7242cb9c6baSRusty Russell 
7252cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
726e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
72799ffc696SRusty Russell 
72803f191baSMichael S. Tsirkin 	/* Try to transmit */
729b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
73099ffc696SRusty Russell 
7319ed4cb07SRusty Russell 	/* This should not happen! */
7320e3daa64SRusty Russell 	if (unlikely(err)) {
73358eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
7342e57b79cSRick Jones 		if (net_ratelimit())
73558eba97dSRusty Russell 			dev_warn(&dev->dev,
736b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
73758eba97dSRusty Russell 		dev->stats.tx_dropped++;
73858eba97dSRusty Russell 		kfree_skb(skb);
73958eba97dSRusty Russell 		return NETDEV_TX_OK;
740296f96fcSRusty Russell 	}
741e9d7417bSJason Wang 	virtqueue_kick(sq->vq);
74203f191baSMichael S. Tsirkin 
74348925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
74448925e37SRusty Russell 	skb_orphan(skb);
74548925e37SRusty Russell 	nf_reset(skb);
74648925e37SRusty Russell 
74748925e37SRusty Russell 	/* Apparently nice girls don't return TX_BUSY; stop the queue
74848925e37SRusty Russell 	 * before it gets out of hand.  Naturally, this wastes entries. */
749b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
750986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
751e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
75248925e37SRusty Russell 			/* More just got used, free them then recheck. */
753b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
754b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
755986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
756e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
75748925e37SRusty Russell 			}
75848925e37SRusty Russell 		}
75948925e37SRusty Russell 	}
76048925e37SRusty Russell 
76148925e37SRusty Russell 	return NETDEV_TX_OK;
76248925e37SRusty Russell }
76348925e37SRusty Russell 
76440cbfc37SAmos Kong /*
76540cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
76640cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
76740cbfc37SAmos Kong  * never fail unless improperly formated.
76840cbfc37SAmos Kong  */
76940cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
77040cbfc37SAmos Kong 				 struct scatterlist *data, int out, int in)
77140cbfc37SAmos Kong {
77240cbfc37SAmos Kong 	struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
77340cbfc37SAmos Kong 	struct virtio_net_ctrl_hdr ctrl;
77440cbfc37SAmos Kong 	virtio_net_ctrl_ack status = ~0;
77540cbfc37SAmos Kong 	unsigned int tmp;
77640cbfc37SAmos Kong 	int i;
77740cbfc37SAmos Kong 
77840cbfc37SAmos Kong 	/* Caller should know better */
77940cbfc37SAmos Kong 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
78040cbfc37SAmos Kong 		(out + in > VIRTNET_SEND_COMMAND_SG_MAX));
78140cbfc37SAmos Kong 
78240cbfc37SAmos Kong 	out++; /* Add header */
78340cbfc37SAmos Kong 	in++; /* Add return status */
78440cbfc37SAmos Kong 
78540cbfc37SAmos Kong 	ctrl.class = class;
78640cbfc37SAmos Kong 	ctrl.cmd = cmd;
78740cbfc37SAmos Kong 
78840cbfc37SAmos Kong 	sg_init_table(sg, out + in);
78940cbfc37SAmos Kong 
79040cbfc37SAmos Kong 	sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
79140cbfc37SAmos Kong 	for_each_sg(data, s, out + in - 2, i)
79240cbfc37SAmos Kong 		sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
79340cbfc37SAmos Kong 	sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
79440cbfc37SAmos Kong 
79540cbfc37SAmos Kong 	BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
79640cbfc37SAmos Kong 
79740cbfc37SAmos Kong 	virtqueue_kick(vi->cvq);
79840cbfc37SAmos Kong 
79940cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
80040cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
80140cbfc37SAmos Kong 	 */
80240cbfc37SAmos Kong 	while (!virtqueue_get_buf(vi->cvq, &tmp))
80340cbfc37SAmos Kong 		cpu_relax();
80440cbfc37SAmos Kong 
80540cbfc37SAmos Kong 	return status == VIRTIO_NET_OK;
80640cbfc37SAmos Kong }
80740cbfc37SAmos Kong 
8089c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
8099c46f6d4SAlex Williamson {
8109c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
8119c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
812f2f2c8b4SJiri Pirko 	int ret;
8137e58d5aeSAmos Kong 	struct sockaddr *addr = p;
8147e58d5aeSAmos Kong 	struct scatterlist sg;
8159c46f6d4SAlex Williamson 
8167e58d5aeSAmos Kong 	ret = eth_prepare_mac_addr_change(dev, p);
817f2f2c8b4SJiri Pirko 	if (ret)
818f2f2c8b4SJiri Pirko 		return ret;
8199c46f6d4SAlex Williamson 
8207e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
8217e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
8227e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
8237e58d5aeSAmos Kong 					  VIRTIO_NET_CTRL_MAC_ADDR_SET,
8247e58d5aeSAmos Kong 					  &sg, 1, 0)) {
8257e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
8267e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
8277e58d5aeSAmos Kong 			return -EINVAL;
8287e58d5aeSAmos Kong 		}
8297e58d5aeSAmos Kong 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
8309c46f6d4SAlex Williamson 		vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
8317e58d5aeSAmos Kong 				  addr->sa_data, dev->addr_len);
8327e58d5aeSAmos Kong 	}
8337e58d5aeSAmos Kong 
8347e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
8359c46f6d4SAlex Williamson 
8369c46f6d4SAlex Williamson 	return 0;
8379c46f6d4SAlex Williamson }
8389c46f6d4SAlex Williamson 
8393fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
8403fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
8413fa2a1dfSstephen hemminger {
8423fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
8433fa2a1dfSstephen hemminger 	int cpu;
8443fa2a1dfSstephen hemminger 	unsigned int start;
8453fa2a1dfSstephen hemminger 
8463fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
84758472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
8483fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
8493fa2a1dfSstephen hemminger 
8503fa2a1dfSstephen hemminger 		do {
851e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
8523fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
8533fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
854e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
85583a27052SEric Dumazet 
85683a27052SEric Dumazet 		do {
857e3906486SKevin Groeneveld 			start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
8583fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
8593fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
860e3906486SKevin Groeneveld 		} while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
8613fa2a1dfSstephen hemminger 
8623fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
8633fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
8643fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
8653fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
8663fa2a1dfSstephen hemminger 	}
8673fa2a1dfSstephen hemminger 
8683fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
869021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
8703fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
8713fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
8723fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
8733fa2a1dfSstephen hemminger 
8743fa2a1dfSstephen hemminger 	return tot;
8753fa2a1dfSstephen hemminger }
8763fa2a1dfSstephen hemminger 
877da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
878da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
879da74e89dSAmit Shah {
880da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
881986a4f4dSJason Wang 	int i;
882da74e89dSAmit Shah 
883986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
884986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
885da74e89dSAmit Shah }
886da74e89dSAmit Shah #endif
887da74e89dSAmit Shah 
888586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
889586d17c5SJason Wang {
890586d17c5SJason Wang 	rtnl_lock();
891586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
892586d17c5SJason Wang 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
893586d17c5SJason Wang 				  0, 0))
894586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
895586d17c5SJason Wang 	rtnl_unlock();
896586d17c5SJason Wang }
897586d17c5SJason Wang 
898986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
899986a4f4dSJason Wang {
900986a4f4dSJason Wang 	struct scatterlist sg;
901986a4f4dSJason Wang 	struct virtio_net_ctrl_mq s;
902986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
903986a4f4dSJason Wang 
904986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
905986a4f4dSJason Wang 		return 0;
906986a4f4dSJason Wang 
907986a4f4dSJason Wang 	s.virtqueue_pairs = queue_pairs;
908986a4f4dSJason Wang 	sg_init_one(&sg, &s, sizeof(s));
909986a4f4dSJason Wang 
910986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
911986a4f4dSJason Wang 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, 1, 0)){
912986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
913986a4f4dSJason Wang 			 queue_pairs);
914986a4f4dSJason Wang 		return -EINVAL;
915986a4f4dSJason Wang 	} else
916986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
917986a4f4dSJason Wang 
918986a4f4dSJason Wang 	return 0;
919986a4f4dSJason Wang }
920986a4f4dSJason Wang 
921296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
922296f96fcSRusty Russell {
923296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
924986a4f4dSJason Wang 	int i;
925296f96fcSRusty Russell 
926b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
927b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
928986a4f4dSJason Wang 
929986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
930986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
931296f96fcSRusty Russell 
932296f96fcSRusty Russell 	return 0;
933296f96fcSRusty Russell }
934296f96fcSRusty Russell 
9352af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
9362af7698eSAlex Williamson {
9372af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
938f565a7c2SAlex Williamson 	struct scatterlist sg[2];
9392af7698eSAlex Williamson 	u8 promisc, allmulti;
940f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
941ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
94232e7bfc4SJiri Pirko 	int uc_count;
9434cd24eafSJiri Pirko 	int mc_count;
944f565a7c2SAlex Williamson 	void *buf;
945f565a7c2SAlex Williamson 	int i;
9462af7698eSAlex Williamson 
9472af7698eSAlex Williamson 	/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
9482af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
9492af7698eSAlex Williamson 		return;
9502af7698eSAlex Williamson 
951f565a7c2SAlex Williamson 	promisc = ((dev->flags & IFF_PROMISC) != 0);
952f565a7c2SAlex Williamson 	allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
9532af7698eSAlex Williamson 
95423e258e1SAlex Williamson 	sg_init_one(sg, &promisc, sizeof(promisc));
9552af7698eSAlex Williamson 
9562af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
9572af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_PROMISC,
958f565a7c2SAlex Williamson 				  sg, 1, 0))
9592af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
9602af7698eSAlex Williamson 			 promisc ? "en" : "dis");
9612af7698eSAlex Williamson 
96223e258e1SAlex Williamson 	sg_init_one(sg, &allmulti, sizeof(allmulti));
9632af7698eSAlex Williamson 
9642af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
9652af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_ALLMULTI,
966f565a7c2SAlex Williamson 				  sg, 1, 0))
9672af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
9682af7698eSAlex Williamson 			 allmulti ? "en" : "dis");
969f565a7c2SAlex Williamson 
97032e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
9714cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
972f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
9734cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
974f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
9754cd24eafSJiri Pirko 	mac_data = buf;
976e68ed8f0SJoe Perches 	if (!buf)
977f565a7c2SAlex Williamson 		return;
978f565a7c2SAlex Williamson 
97923e258e1SAlex Williamson 	sg_init_table(sg, 2);
98023e258e1SAlex Williamson 
981f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
98232e7bfc4SJiri Pirko 	mac_data->entries = uc_count;
983ccffad25SJiri Pirko 	i = 0;
98432e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
985ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
986f565a7c2SAlex Williamson 
987f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
98832e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
989f565a7c2SAlex Williamson 
990f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
99132e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
992f565a7c2SAlex Williamson 
9934cd24eafSJiri Pirko 	mac_data->entries = mc_count;
994567ec874SJiri Pirko 	i = 0;
99522bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
99622bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
997f565a7c2SAlex Williamson 
998f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
9994cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1000f565a7c2SAlex Williamson 
1001f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1002f565a7c2SAlex Williamson 				  VIRTIO_NET_CTRL_MAC_TABLE_SET,
1003f565a7c2SAlex Williamson 				  sg, 2, 0))
1004f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
1005f565a7c2SAlex Williamson 
1006f565a7c2SAlex Williamson 	kfree(buf);
10072af7698eSAlex Williamson }
10082af7698eSAlex Williamson 
10098e586137SJiri Pirko static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
10100bde9569SAlex Williamson {
10110bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
10120bde9569SAlex Williamson 	struct scatterlist sg;
10130bde9569SAlex Williamson 
101423e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
10150bde9569SAlex Williamson 
10160bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
10170bde9569SAlex Williamson 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
10180bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
10198e586137SJiri Pirko 	return 0;
10200bde9569SAlex Williamson }
10210bde9569SAlex Williamson 
10228e586137SJiri Pirko static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
10230bde9569SAlex Williamson {
10240bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
10250bde9569SAlex Williamson 	struct scatterlist sg;
10260bde9569SAlex Williamson 
102723e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
10280bde9569SAlex Williamson 
10290bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
10300bde9569SAlex Williamson 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
10310bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
10328e586137SJiri Pirko 	return 0;
10330bde9569SAlex Williamson }
10340bde9569SAlex Williamson 
10358898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1036986a4f4dSJason Wang {
1037986a4f4dSJason Wang 	int i;
10388898c21cSWanlong Gao 	int cpu;
10398898c21cSWanlong Gao 
10408898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
10418898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
10428898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
10438898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
10448898c21cSWanlong Gao 		}
10458898c21cSWanlong Gao 
10468898c21cSWanlong Gao 		vi->affinity_hint_set = false;
10478898c21cSWanlong Gao 	}
10488898c21cSWanlong Gao 
10498898c21cSWanlong Gao 	i = 0;
10508898c21cSWanlong Gao 	for_each_online_cpu(cpu) {
10518898c21cSWanlong Gao 		if (cpu == hcpu) {
10528898c21cSWanlong Gao 			*per_cpu_ptr(vi->vq_index, cpu) = -1;
10538898c21cSWanlong Gao 		} else {
10548898c21cSWanlong Gao 			*per_cpu_ptr(vi->vq_index, cpu) =
10558898c21cSWanlong Gao 				++i % vi->curr_queue_pairs;
10568898c21cSWanlong Gao 		}
10578898c21cSWanlong Gao 	}
10588898c21cSWanlong Gao }
10598898c21cSWanlong Gao 
10608898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1061986a4f4dSJason Wang {
1062986a4f4dSJason Wang 	int i;
106347be2479SWanlong Gao 	int cpu;
1064986a4f4dSJason Wang 
1065986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1066986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1067986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1068986a4f4dSJason Wang 	 */
10698898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
10708898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
10718898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1072986a4f4dSJason Wang 		return;
1073986a4f4dSJason Wang 	}
1074986a4f4dSJason Wang 
107547be2479SWanlong Gao 	i = 0;
107647be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1077986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1078986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
107947be2479SWanlong Gao 		*per_cpu_ptr(vi->vq_index, cpu) = i;
108047be2479SWanlong Gao 		i++;
1081986a4f4dSJason Wang 	}
1082986a4f4dSJason Wang 
1083986a4f4dSJason Wang 	vi->affinity_hint_set = true;
108447be2479SWanlong Gao }
1085986a4f4dSJason Wang 
10868de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb,
10878de4b2f3SWanlong Gao 			        unsigned long action, void *hcpu)
10888de4b2f3SWanlong Gao {
10898de4b2f3SWanlong Gao 	struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
10908de4b2f3SWanlong Gao 
10918de4b2f3SWanlong Gao 	switch(action & ~CPU_TASKS_FROZEN) {
10928de4b2f3SWanlong Gao 	case CPU_ONLINE:
10938de4b2f3SWanlong Gao 	case CPU_DOWN_FAILED:
10948de4b2f3SWanlong Gao 	case CPU_DEAD:
10958de4b2f3SWanlong Gao 		virtnet_set_affinity(vi);
10968de4b2f3SWanlong Gao 		break;
10978de4b2f3SWanlong Gao 	case CPU_DOWN_PREPARE:
10988de4b2f3SWanlong Gao 		virtnet_clean_affinity(vi, (long)hcpu);
10998de4b2f3SWanlong Gao 		break;
11008de4b2f3SWanlong Gao 	default:
11018de4b2f3SWanlong Gao 		break;
11028de4b2f3SWanlong Gao 	}
11038de4b2f3SWanlong Gao 	return NOTIFY_OK;
1104a9ea3fc6SHerbert Xu }
1105a9ea3fc6SHerbert Xu 
11068f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
11078f9f4668SRick Jones 				struct ethtool_ringparam *ring)
11088f9f4668SRick Jones {
11098f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
11108f9f4668SRick Jones 
1111986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1112986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
11138f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
11148f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
11158f9f4668SRick Jones }
11168f9f4668SRick Jones 
111766846048SRick Jones 
111866846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
111966846048SRick Jones 				struct ethtool_drvinfo *info)
112066846048SRick Jones {
112166846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
112266846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
112366846048SRick Jones 
112466846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
112566846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
112666846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
112766846048SRick Jones 
112866846048SRick Jones }
112966846048SRick Jones 
1130d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1131d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1132d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1133d73bcd2cSJason Wang {
1134d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1135d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1136d73bcd2cSJason Wang 	int err;
1137d73bcd2cSJason Wang 
1138d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1139d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1140d73bcd2cSJason Wang 	 */
1141d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1142d73bcd2cSJason Wang 		return -EINVAL;
1143d73bcd2cSJason Wang 
1144d73bcd2cSJason Wang 	if (queue_pairs > vi->max_queue_pairs)
1145d73bcd2cSJason Wang 		return -EINVAL;
1146d73bcd2cSJason Wang 
114747be2479SWanlong Gao 	get_online_cpus();
1148d73bcd2cSJason Wang 	err = virtnet_set_queues(vi, queue_pairs);
1149d73bcd2cSJason Wang 	if (!err) {
1150d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1151d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1152d73bcd2cSJason Wang 
11538898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1154d73bcd2cSJason Wang 	}
115547be2479SWanlong Gao 	put_online_cpus();
1156d73bcd2cSJason Wang 
1157d73bcd2cSJason Wang 	return err;
1158d73bcd2cSJason Wang }
1159d73bcd2cSJason Wang 
1160d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1161d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1162d73bcd2cSJason Wang {
1163d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1164d73bcd2cSJason Wang 
1165d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1166d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1167d73bcd2cSJason Wang 	channels->max_other = 0;
1168d73bcd2cSJason Wang 	channels->rx_count = 0;
1169d73bcd2cSJason Wang 	channels->tx_count = 0;
1170d73bcd2cSJason Wang 	channels->other_count = 0;
1171d73bcd2cSJason Wang }
1172d73bcd2cSJason Wang 
11730fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
117466846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
11759f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
11768f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1177d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1178d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1179a9ea3fc6SHerbert Xu };
1180a9ea3fc6SHerbert Xu 
118139da5814SMark McLoughlin #define MIN_MTU 68
118239da5814SMark McLoughlin #define MAX_MTU 65535
118339da5814SMark McLoughlin 
118439da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
118539da5814SMark McLoughlin {
118639da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
118739da5814SMark McLoughlin 		return -EINVAL;
118839da5814SMark McLoughlin 	dev->mtu = new_mtu;
118939da5814SMark McLoughlin 	return 0;
119039da5814SMark McLoughlin }
119139da5814SMark McLoughlin 
1192986a4f4dSJason Wang /* To avoid contending a lock hold by a vcpu who would exit to host, select the
1193986a4f4dSJason Wang  * txq based on the processor id.
1194986a4f4dSJason Wang  */
1195986a4f4dSJason Wang static u16 virtnet_select_queue(struct net_device *dev, struct sk_buff *skb)
1196986a4f4dSJason Wang {
119747be2479SWanlong Gao 	int txq;
119847be2479SWanlong Gao 	struct virtnet_info *vi = netdev_priv(dev);
119947be2479SWanlong Gao 
120047be2479SWanlong Gao 	if (skb_rx_queue_recorded(skb)) {
120147be2479SWanlong Gao 		txq = skb_get_rx_queue(skb);
120247be2479SWanlong Gao 	} else {
120347be2479SWanlong Gao 		txq = *__this_cpu_ptr(vi->vq_index);
120447be2479SWanlong Gao 		if (txq == -1)
120547be2479SWanlong Gao 			txq = 0;
120647be2479SWanlong Gao 	}
1207986a4f4dSJason Wang 
1208986a4f4dSJason Wang 	while (unlikely(txq >= dev->real_num_tx_queues))
1209986a4f4dSJason Wang 		txq -= dev->real_num_tx_queues;
1210986a4f4dSJason Wang 
1211986a4f4dSJason Wang 	return txq;
1212986a4f4dSJason Wang }
1213986a4f4dSJason Wang 
121476288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
121576288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
121676288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
121776288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
121876288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
12199c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
12202af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
122176288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
12223fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
12231824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
12241824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1225986a4f4dSJason Wang 	.ndo_select_queue     = virtnet_select_queue,
122676288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
122776288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
122876288b4eSStephen Hemminger #endif
122976288b4eSStephen Hemminger };
123076288b4eSStephen Hemminger 
1231586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
12329f4d26d0SMark McLoughlin {
1233586d17c5SJason Wang 	struct virtnet_info *vi =
1234586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
12359f4d26d0SMark McLoughlin 	u16 v;
12369f4d26d0SMark McLoughlin 
1237586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1238586d17c5SJason Wang 	if (!vi->config_enable)
1239586d17c5SJason Wang 		goto done;
1240586d17c5SJason Wang 
124177dd7693SSasha Levin 	if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
12429f4d26d0SMark McLoughlin 			      offsetof(struct virtio_net_config, status),
124377dd7693SSasha Levin 			      &v) < 0)
1244586d17c5SJason Wang 		goto done;
1245586d17c5SJason Wang 
1246586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1247ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1248586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1249586d17c5SJason Wang 	}
12509f4d26d0SMark McLoughlin 
12519f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
12529f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
12539f4d26d0SMark McLoughlin 
12549f4d26d0SMark McLoughlin 	if (vi->status == v)
1255586d17c5SJason Wang 		goto done;
12569f4d26d0SMark McLoughlin 
12579f4d26d0SMark McLoughlin 	vi->status = v;
12589f4d26d0SMark McLoughlin 
12599f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
12609f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1261986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
12629f4d26d0SMark McLoughlin 	} else {
12639f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1264986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
12659f4d26d0SMark McLoughlin 	}
1266586d17c5SJason Wang done:
1267586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
12689f4d26d0SMark McLoughlin }
12699f4d26d0SMark McLoughlin 
12709f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
12719f4d26d0SMark McLoughlin {
12729f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
12739f4d26d0SMark McLoughlin 
12743b07e9caSTejun Heo 	schedule_work(&vi->config_work);
12759f4d26d0SMark McLoughlin }
12769f4d26d0SMark McLoughlin 
1277986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1278986a4f4dSJason Wang {
1279986a4f4dSJason Wang 	kfree(vi->rq);
1280986a4f4dSJason Wang 	kfree(vi->sq);
1281986a4f4dSJason Wang }
1282986a4f4dSJason Wang 
1283986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi)
1284986a4f4dSJason Wang {
1285986a4f4dSJason Wang 	int i;
1286986a4f4dSJason Wang 
1287986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1288986a4f4dSJason Wang 		while (vi->rq[i].pages)
1289986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1290986a4f4dSJason Wang 	}
1291986a4f4dSJason Wang }
1292986a4f4dSJason Wang 
1293986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1294986a4f4dSJason Wang {
1295986a4f4dSJason Wang 	void *buf;
1296986a4f4dSJason Wang 	int i;
1297986a4f4dSJason Wang 
1298986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1299986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
1300986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1301986a4f4dSJason Wang 			dev_kfree_skb(buf);
1302986a4f4dSJason Wang 	}
1303986a4f4dSJason Wang 
1304986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1305986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
1306986a4f4dSJason Wang 
1307986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1308986a4f4dSJason Wang 			if (vi->mergeable_rx_bufs || vi->big_packets)
1309986a4f4dSJason Wang 				give_pages(&vi->rq[i], buf);
1310986a4f4dSJason Wang 			else
1311986a4f4dSJason Wang 				dev_kfree_skb(buf);
1312986a4f4dSJason Wang 			--vi->rq[i].num;
1313986a4f4dSJason Wang 		}
1314986a4f4dSJason Wang 		BUG_ON(vi->rq[i].num != 0);
1315986a4f4dSJason Wang 	}
1316986a4f4dSJason Wang }
1317986a4f4dSJason Wang 
1318e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
1319e9d7417bSJason Wang {
1320e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
1321e9d7417bSJason Wang 
13228898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
1323986a4f4dSJason Wang 
1324e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
1325986a4f4dSJason Wang 
1326986a4f4dSJason Wang 	virtnet_free_queues(vi);
1327986a4f4dSJason Wang }
1328986a4f4dSJason Wang 
1329986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
1330986a4f4dSJason Wang {
1331986a4f4dSJason Wang 	vq_callback_t **callbacks;
1332986a4f4dSJason Wang 	struct virtqueue **vqs;
1333986a4f4dSJason Wang 	int ret = -ENOMEM;
1334986a4f4dSJason Wang 	int i, total_vqs;
1335986a4f4dSJason Wang 	const char **names;
1336986a4f4dSJason Wang 
1337986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1338986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1339986a4f4dSJason Wang 	 * possible control vq.
1340986a4f4dSJason Wang 	 */
1341986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
1342986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1343986a4f4dSJason Wang 
1344986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
1345986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1346986a4f4dSJason Wang 	if (!vqs)
1347986a4f4dSJason Wang 		goto err_vq;
1348986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1349986a4f4dSJason Wang 	if (!callbacks)
1350986a4f4dSJason Wang 		goto err_callback;
1351986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1352986a4f4dSJason Wang 	if (!names)
1353986a4f4dSJason Wang 		goto err_names;
1354986a4f4dSJason Wang 
1355986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
1356986a4f4dSJason Wang 	if (vi->has_cvq) {
1357986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
1358986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
1359986a4f4dSJason Wang 	}
1360986a4f4dSJason Wang 
1361986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
1362986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1363986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
1364986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
1365986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
1366986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
1367986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
1368986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
1369986a4f4dSJason Wang 	}
1370986a4f4dSJason Wang 
1371986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1372986a4f4dSJason Wang 					 names);
1373986a4f4dSJason Wang 	if (ret)
1374986a4f4dSJason Wang 		goto err_find;
1375986a4f4dSJason Wang 
1376986a4f4dSJason Wang 	if (vi->has_cvq) {
1377986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
1378986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1379*f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1380986a4f4dSJason Wang 	}
1381986a4f4dSJason Wang 
1382986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1383986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
1384986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
1385986a4f4dSJason Wang 	}
1386986a4f4dSJason Wang 
1387986a4f4dSJason Wang 	kfree(names);
1388986a4f4dSJason Wang 	kfree(callbacks);
1389986a4f4dSJason Wang 	kfree(vqs);
1390986a4f4dSJason Wang 
1391986a4f4dSJason Wang 	return 0;
1392986a4f4dSJason Wang 
1393986a4f4dSJason Wang err_find:
1394986a4f4dSJason Wang 	kfree(names);
1395986a4f4dSJason Wang err_names:
1396986a4f4dSJason Wang 	kfree(callbacks);
1397986a4f4dSJason Wang err_callback:
1398986a4f4dSJason Wang 	kfree(vqs);
1399986a4f4dSJason Wang err_vq:
1400986a4f4dSJason Wang 	return ret;
1401986a4f4dSJason Wang }
1402986a4f4dSJason Wang 
1403986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
1404986a4f4dSJason Wang {
1405986a4f4dSJason Wang 	int i;
1406986a4f4dSJason Wang 
1407986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1408986a4f4dSJason Wang 	if (!vi->sq)
1409986a4f4dSJason Wang 		goto err_sq;
1410986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1411008d4278SAmerigo Wang 	if (!vi->rq)
1412986a4f4dSJason Wang 		goto err_rq;
1413986a4f4dSJason Wang 
1414986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
1415986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1416986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
1417986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1418986a4f4dSJason Wang 			       napi_weight);
1419986a4f4dSJason Wang 
1420986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1421986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1422986a4f4dSJason Wang 	}
1423986a4f4dSJason Wang 
1424986a4f4dSJason Wang 	return 0;
1425986a4f4dSJason Wang 
1426986a4f4dSJason Wang err_rq:
1427986a4f4dSJason Wang 	kfree(vi->sq);
1428986a4f4dSJason Wang err_sq:
1429986a4f4dSJason Wang 	return -ENOMEM;
1430e9d7417bSJason Wang }
1431e9d7417bSJason Wang 
14323f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
14333f9c10b0SAmit Shah {
1434986a4f4dSJason Wang 	int ret;
14353f9c10b0SAmit Shah 
1436986a4f4dSJason Wang 	/* Allocate send & receive queues */
1437986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
1438986a4f4dSJason Wang 	if (ret)
1439986a4f4dSJason Wang 		goto err;
14403f9c10b0SAmit Shah 
1441986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
1442986a4f4dSJason Wang 	if (ret)
1443986a4f4dSJason Wang 		goto err_free;
14443f9c10b0SAmit Shah 
144547be2479SWanlong Gao 	get_online_cpus();
14468898c21cSWanlong Gao 	virtnet_set_affinity(vi);
144747be2479SWanlong Gao 	put_online_cpus();
144847be2479SWanlong Gao 
14493f9c10b0SAmit Shah 	return 0;
1450986a4f4dSJason Wang 
1451986a4f4dSJason Wang err_free:
1452986a4f4dSJason Wang 	virtnet_free_queues(vi);
1453986a4f4dSJason Wang err:
1454986a4f4dSJason Wang 	return ret;
14553f9c10b0SAmit Shah }
14563f9c10b0SAmit Shah 
1457296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
1458296f96fcSRusty Russell {
1459986a4f4dSJason Wang 	int i, err;
1460296f96fcSRusty Russell 	struct net_device *dev;
1461296f96fcSRusty Russell 	struct virtnet_info *vi;
1462986a4f4dSJason Wang 	u16 max_queue_pairs;
1463986a4f4dSJason Wang 
1464986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
1465986a4f4dSJason Wang 	err = virtio_config_val(vdev, VIRTIO_NET_F_MQ,
1466986a4f4dSJason Wang 				offsetof(struct virtio_net_config,
1467986a4f4dSJason Wang 				max_virtqueue_pairs), &max_queue_pairs);
1468986a4f4dSJason Wang 
1469986a4f4dSJason Wang 	/* We need at least 2 queue's */
1470986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1471986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1472986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1473986a4f4dSJason Wang 		max_queue_pairs = 1;
1474296f96fcSRusty Russell 
1475296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
1476986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1477296f96fcSRusty Russell 	if (!dev)
1478296f96fcSRusty Russell 		return -ENOMEM;
1479296f96fcSRusty Russell 
1480296f96fcSRusty Russell 	/* Set up network device as normal. */
1481f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
148276288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
1483296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
14843fa2a1dfSstephen hemminger 
1485a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1486296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
1487296f96fcSRusty Russell 
1488296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
148998e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1490296f96fcSRusty Russell 		/* This opens up the world of extra features. */
149198e778c9SMichał Mirosław 		dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
149298e778c9SMichał Mirosław 		if (csum)
1493296f96fcSRusty Russell 			dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
149498e778c9SMichał Mirosław 
149598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
149698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
149734a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
149834a48579SRusty Russell 		}
14995539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
150098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
150198e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
150298e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
150398e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
150498e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
150598e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
150698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
150798e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_UFO;
150898e778c9SMichał Mirosław 
150998e778c9SMichał Mirosław 		if (gso)
151098e778c9SMichał Mirosław 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
151198e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
1512296f96fcSRusty Russell 	}
1513296f96fcSRusty Russell 
15144fda8302SJason Wang 	dev->vlan_features = dev->features;
15154fda8302SJason Wang 
1516296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
151777dd7693SSasha Levin 	if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
1518a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
151977dd7693SSasha Levin 				  dev->dev_addr, dev->addr_len) < 0)
1520f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1521296f96fcSRusty Russell 
1522296f96fcSRusty Russell 	/* Set up our device-specific information */
1523296f96fcSRusty Russell 	vi = netdev_priv(dev);
1524296f96fcSRusty Russell 	vi->dev = dev;
1525296f96fcSRusty Russell 	vi->vdev = vdev;
1526d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
15273fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
15283fa2a1dfSstephen hemminger 	err = -ENOMEM;
15293fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
15303fa2a1dfSstephen hemminger 		goto free;
15313fa2a1dfSstephen hemminger 
153247be2479SWanlong Gao 	vi->vq_index = alloc_percpu(int);
153347be2479SWanlong Gao 	if (vi->vq_index == NULL)
153447be2479SWanlong Gao 		goto free_stats;
153547be2479SWanlong Gao 
1536586d17c5SJason Wang 	mutex_init(&vi->config_lock);
1537586d17c5SJason Wang 	vi->config_enable = true;
1538586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1539296f96fcSRusty Russell 
154097402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
15418e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
15428e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
15438e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
154497402b96SHerbert Xu 		vi->big_packets = true;
154597402b96SHerbert Xu 
15463f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
15473f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
15483f2c31d9SMark McLoughlin 
1549986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1550986a4f4dSJason Wang 		vi->has_cvq = true;
1551986a4f4dSJason Wang 
1552986a4f4dSJason Wang 	/* Use single tx/rx queue pair as default */
1553986a4f4dSJason Wang 	vi->curr_queue_pairs = 1;
1554986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
1555986a4f4dSJason Wang 
1556986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
15573f9c10b0SAmit Shah 	err = init_vqs(vi);
1558d2a7dddaSMichael S. Tsirkin 	if (err)
155947be2479SWanlong Gao 		goto free_index;
1560d2a7dddaSMichael S. Tsirkin 
1561986a4f4dSJason Wang 	netif_set_real_num_tx_queues(dev, 1);
1562986a4f4dSJason Wang 	netif_set_real_num_rx_queues(dev, 1);
1563986a4f4dSJason Wang 
1564296f96fcSRusty Russell 	err = register_netdev(dev);
1565296f96fcSRusty Russell 	if (err) {
1566296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1567d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1568296f96fcSRusty Russell 	}
1569b3369c1fSRusty Russell 
1570b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
1571986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1572986a4f4dSJason Wang 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
1573b3369c1fSRusty Russell 
1574b3369c1fSRusty Russell 		/* If we didn't even get one input buffer, we're useless. */
1575986a4f4dSJason Wang 		if (vi->rq[i].num == 0) {
1576986a4f4dSJason Wang 			free_unused_bufs(vi);
1577b3369c1fSRusty Russell 			err = -ENOMEM;
1578986a4f4dSJason Wang 			goto free_recv_bufs;
1579986a4f4dSJason Wang 		}
1580b3369c1fSRusty Russell 	}
1581b3369c1fSRusty Russell 
15828de4b2f3SWanlong Gao 	vi->nb.notifier_call = &virtnet_cpu_callback;
15838de4b2f3SWanlong Gao 	err = register_hotcpu_notifier(&vi->nb);
15848de4b2f3SWanlong Gao 	if (err) {
15858de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
15868de4b2f3SWanlong Gao 		goto free_recv_bufs;
15878de4b2f3SWanlong Gao 	}
15888de4b2f3SWanlong Gao 
1589167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1590167c25e4SJason Wang 	   otherwise get link status from config. */
1591167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1592167c25e4SJason Wang 		netif_carrier_off(dev);
15933b07e9caSTejun Heo 		schedule_work(&vi->config_work);
1594167c25e4SJason Wang 	} else {
1595167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
15964783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1597167c25e4SJason Wang 	}
15989f4d26d0SMark McLoughlin 
1599986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1600986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
1601986a4f4dSJason Wang 
1602296f96fcSRusty Russell 	return 0;
1603296f96fcSRusty Russell 
1604986a4f4dSJason Wang free_recv_bufs:
1605986a4f4dSJason Wang 	free_receive_bufs(vi);
1606b3369c1fSRusty Russell 	unregister_netdev(dev);
1607d2a7dddaSMichael S. Tsirkin free_vqs:
1608986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
1609e9d7417bSJason Wang 	virtnet_del_vqs(vi);
161047be2479SWanlong Gao free_index:
161147be2479SWanlong Gao 	free_percpu(vi->vq_index);
16123fa2a1dfSstephen hemminger free_stats:
16133fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1614296f96fcSRusty Russell free:
1615296f96fcSRusty Russell 	free_netdev(dev);
1616296f96fcSRusty Russell 	return err;
1617296f96fcSRusty Russell }
1618296f96fcSRusty Russell 
161904486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
1620296f96fcSRusty Russell {
162104486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
1622830a8a97SShirley Ma 
1623830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
16249ab86bbcSShirley Ma 	free_unused_bufs(vi);
1625fb6813f4SRusty Russell 
1626986a4f4dSJason Wang 	free_receive_bufs(vi);
1627d2a7dddaSMichael S. Tsirkin 
1628986a4f4dSJason Wang 	virtnet_del_vqs(vi);
162904486ed0SAmit Shah }
163004486ed0SAmit Shah 
16318cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
163204486ed0SAmit Shah {
163304486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
163404486ed0SAmit Shah 
16358de4b2f3SWanlong Gao 	unregister_hotcpu_notifier(&vi->nb);
16368de4b2f3SWanlong Gao 
1637586d17c5SJason Wang 	/* Prevent config work handler from accessing the device. */
1638586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1639586d17c5SJason Wang 	vi->config_enable = false;
1640586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1641586d17c5SJason Wang 
164204486ed0SAmit Shah 	unregister_netdev(vi->dev);
164304486ed0SAmit Shah 
164404486ed0SAmit Shah 	remove_vq_common(vi);
1645fb6813f4SRusty Russell 
1646586d17c5SJason Wang 	flush_work(&vi->config_work);
1647586d17c5SJason Wang 
164847be2479SWanlong Gao 	free_percpu(vi->vq_index);
16492e66f55bSKrishna Kumar 	free_percpu(vi->stats);
165074b2553fSRusty Russell 	free_netdev(vi->dev);
1651296f96fcSRusty Russell }
1652296f96fcSRusty Russell 
16530741bcb5SAmit Shah #ifdef CONFIG_PM
16540741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
16550741bcb5SAmit Shah {
16560741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1657986a4f4dSJason Wang 	int i;
16580741bcb5SAmit Shah 
1659586d17c5SJason Wang 	/* Prevent config work handler from accessing the device */
1660586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1661586d17c5SJason Wang 	vi->config_enable = false;
1662586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1663586d17c5SJason Wang 
16640741bcb5SAmit Shah 	netif_device_detach(vi->dev);
16650741bcb5SAmit Shah 	cancel_delayed_work_sync(&vi->refill);
16660741bcb5SAmit Shah 
16670741bcb5SAmit Shah 	if (netif_running(vi->dev))
1668986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++) {
1669986a4f4dSJason Wang 			napi_disable(&vi->rq[i].napi);
1670986a4f4dSJason Wang 			netif_napi_del(&vi->rq[i].napi);
1671986a4f4dSJason Wang 		}
16720741bcb5SAmit Shah 
16730741bcb5SAmit Shah 	remove_vq_common(vi);
16740741bcb5SAmit Shah 
1675586d17c5SJason Wang 	flush_work(&vi->config_work);
1676586d17c5SJason Wang 
16770741bcb5SAmit Shah 	return 0;
16780741bcb5SAmit Shah }
16790741bcb5SAmit Shah 
16800741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
16810741bcb5SAmit Shah {
16820741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1683986a4f4dSJason Wang 	int err, i;
16840741bcb5SAmit Shah 
16850741bcb5SAmit Shah 	err = init_vqs(vi);
16860741bcb5SAmit Shah 	if (err)
16870741bcb5SAmit Shah 		return err;
16880741bcb5SAmit Shah 
16890741bcb5SAmit Shah 	if (netif_running(vi->dev))
1690986a4f4dSJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
1691986a4f4dSJason Wang 			virtnet_napi_enable(&vi->rq[i]);
16920741bcb5SAmit Shah 
16930741bcb5SAmit Shah 	netif_device_attach(vi->dev);
16940741bcb5SAmit Shah 
1695986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
1696986a4f4dSJason Wang 		if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
16973b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
16980741bcb5SAmit Shah 
1699586d17c5SJason Wang 	mutex_lock(&vi->config_lock);
1700586d17c5SJason Wang 	vi->config_enable = true;
1701586d17c5SJason Wang 	mutex_unlock(&vi->config_lock);
1702586d17c5SJason Wang 
1703986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
1704986a4f4dSJason Wang 
17050741bcb5SAmit Shah 	return 0;
17060741bcb5SAmit Shah }
17070741bcb5SAmit Shah #endif
17080741bcb5SAmit Shah 
1709296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
1710296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1711296f96fcSRusty Russell 	{ 0 },
1712296f96fcSRusty Russell };
1713296f96fcSRusty Russell 
1714c45a6816SRusty Russell static unsigned int features[] = {
17155e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
17165e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1717c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
171897402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
17195c516751SSridhar Samudrala 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
17202a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
17210bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1722986a4f4dSJason Wang 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
17237e58d5aeSAmos Kong 	VIRTIO_NET_F_CTRL_MAC_ADDR,
1724c45a6816SRusty Russell };
1725c45a6816SRusty Russell 
172622402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
1727c45a6816SRusty Russell 	.feature_table = features,
1728c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
1729296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
1730296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
1731296f96fcSRusty Russell 	.id_table =	id_table,
1732296f96fcSRusty Russell 	.probe =	virtnet_probe,
17338cc085d6SBill Pemberton 	.remove =	virtnet_remove,
17349f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
17350741bcb5SAmit Shah #ifdef CONFIG_PM
17360741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
17370741bcb5SAmit Shah 	.restore =	virtnet_restore,
17380741bcb5SAmit Shah #endif
1739296f96fcSRusty Russell };
1740296f96fcSRusty Russell 
1741b2a17029SRusty Russell module_virtio_driver(virtio_net_driver);
1742296f96fcSRusty Russell 
1743296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
1744296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
1745296f96fcSRusty Russell MODULE_LICENSE("GPL");
1746