xref: /openbmc/linux/drivers/net/virtio_net.c (revision a725ee3e)
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
16adf8d3ffSJeff Kirsher  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17296f96fcSRusty Russell  */
18296f96fcSRusty Russell //#define DEBUG
19296f96fcSRusty Russell #include <linux/netdevice.h>
20296f96fcSRusty Russell #include <linux/etherdevice.h>
21a9ea3fc6SHerbert Xu #include <linux/ethtool.h>
22296f96fcSRusty Russell #include <linux/module.h>
23296f96fcSRusty Russell #include <linux/virtio.h>
24296f96fcSRusty Russell #include <linux/virtio_net.h>
25296f96fcSRusty Russell #include <linux/scatterlist.h>
26e918085aSAlex Williamson #include <linux/if_vlan.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
288de4b2f3SWanlong Gao #include <linux/cpu.h>
29ab7db917SMichael Dalton #include <linux/average.h>
3091815639SJason Wang #include <net/busy_poll.h>
31296f96fcSRusty Russell 
32d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
336c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
346c0cd7c0SDor Laor 
35eb939922SRusty Russell static bool csum = true, gso = true;
3634a48579SRusty Russell module_param(csum, bool, 0444);
3734a48579SRusty Russell module_param(gso, bool, 0444);
3834a48579SRusty Russell 
39296f96fcSRusty Russell /* FIXME: MTU in config. */
405061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
413f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
42296f96fcSRusty Russell 
435377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet
445377d758SJohannes Berg  * buffer size when refilling RX rings. As the entire RX ring may be refilled
455377d758SJohannes Berg  * at once, the weight is chosen so that the EWMA will be insensitive to short-
465377d758SJohannes Berg  * term, transient changes in packet size.
47ab7db917SMichael Dalton  */
485377d758SJohannes Berg DECLARE_EWMA(pkt_len, 1, 64)
49ab7db917SMichael Dalton 
50ab7db917SMichael Dalton /* Minimum alignment for mergeable packet buffers. */
51ab7db917SMichael Dalton #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, 256)
52ab7db917SMichael Dalton 
5366846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
542a41f71dSAlex Williamson 
553fa2a1dfSstephen hemminger struct virtnet_stats {
5683a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
5783a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
583fa2a1dfSstephen hemminger 	u64 tx_bytes;
593fa2a1dfSstephen hemminger 	u64 tx_packets;
603fa2a1dfSstephen hemminger 
613fa2a1dfSstephen hemminger 	u64 rx_bytes;
623fa2a1dfSstephen hemminger 	u64 rx_packets;
633fa2a1dfSstephen hemminger };
643fa2a1dfSstephen hemminger 
65e9d7417bSJason Wang /* Internal representation of a send virtqueue */
66e9d7417bSJason Wang struct send_queue {
67e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
68e9d7417bSJason Wang 	struct virtqueue *vq;
69e9d7417bSJason Wang 
70e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
71e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
72986a4f4dSJason Wang 
73986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
74986a4f4dSJason Wang 	char name[40];
75e9d7417bSJason Wang };
76e9d7417bSJason Wang 
77e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
78e9d7417bSJason Wang struct receive_queue {
79e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
80e9d7417bSJason Wang 	struct virtqueue *vq;
81e9d7417bSJason Wang 
82296f96fcSRusty Russell 	struct napi_struct napi;
83296f96fcSRusty Russell 
84e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
85e9d7417bSJason Wang 	struct page *pages;
86e9d7417bSJason Wang 
87ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
885377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
89ab7db917SMichael Dalton 
90fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
91fb51879dSMichael Dalton 	struct page_frag alloc_frag;
92fb51879dSMichael Dalton 
93e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
94e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
95986a4f4dSJason Wang 
96986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
97986a4f4dSJason Wang 	char name[40];
98e9d7417bSJason Wang };
99e9d7417bSJason Wang 
100e9d7417bSJason Wang struct virtnet_info {
101e9d7417bSJason Wang 	struct virtio_device *vdev;
102e9d7417bSJason Wang 	struct virtqueue *cvq;
103e9d7417bSJason Wang 	struct net_device *dev;
104986a4f4dSJason Wang 	struct send_queue *sq;
105986a4f4dSJason Wang 	struct receive_queue *rq;
106e9d7417bSJason Wang 	unsigned int status;
107e9d7417bSJason Wang 
108986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
109986a4f4dSJason Wang 	u16 max_queue_pairs;
110986a4f4dSJason Wang 
111986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
112986a4f4dSJason Wang 	u16 curr_queue_pairs;
113986a4f4dSJason Wang 
11497402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
11597402b96SHerbert Xu 	bool big_packets;
11697402b96SHerbert Xu 
1173f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1183f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1193f2c31d9SMark McLoughlin 
120986a4f4dSJason Wang 	/* Has control virtqueue */
121986a4f4dSJason Wang 	bool has_cvq;
122986a4f4dSJason Wang 
123e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
124e7428e95SMichael S. Tsirkin 	bool any_header_sg;
125e7428e95SMichael S. Tsirkin 
126012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
127012873d0SMichael S. Tsirkin 	u8 hdr_len;
128012873d0SMichael S. Tsirkin 
1293fa2a1dfSstephen hemminger 	/* Active statistics */
1303fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1313fa2a1dfSstephen hemminger 
1323161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1333161e453SRusty Russell 	struct delayed_work refill;
1343161e453SRusty Russell 
135586d17c5SJason Wang 	/* Work struct for config space updates */
136586d17c5SJason Wang 	struct work_struct config_work;
137586d17c5SJason Wang 
138986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
139986a4f4dSJason Wang 	bool affinity_hint_set;
14047be2479SWanlong Gao 
1418de4b2f3SWanlong Gao 	/* CPU hot plug notifier */
1428de4b2f3SWanlong Gao 	struct notifier_block nb;
1432ac46030SMichael S. Tsirkin 
1442ac46030SMichael S. Tsirkin 	/* Control VQ buffers: protected by the rtnl lock */
1452ac46030SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr ctrl_hdr;
1462ac46030SMichael S. Tsirkin 	virtio_net_ctrl_ack ctrl_status;
147a725ee3eSAndy Lutomirski 	struct virtio_net_ctrl_mq ctrl_mq;
1482ac46030SMichael S. Tsirkin 	u8 ctrl_promisc;
1492ac46030SMichael S. Tsirkin 	u8 ctrl_allmulti;
150a725ee3eSAndy Lutomirski 	u16 ctrl_vid;
15116032be5SNikolay Aleksandrov 
15216032be5SNikolay Aleksandrov 	/* Ethtool settings */
15316032be5SNikolay Aleksandrov 	u8 duplex;
15416032be5SNikolay Aleksandrov 	u32 speed;
155296f96fcSRusty Russell };
156296f96fcSRusty Russell 
1579ab86bbcSShirley Ma struct padded_vnet_hdr {
158012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf hdr;
1599ab86bbcSShirley Ma 	/*
160012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
161012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
162012873d0SMichael S. Tsirkin 	 * after the header.
1639ab86bbcSShirley Ma 	 */
164012873d0SMichael S. Tsirkin 	char padding[4];
1659ab86bbcSShirley Ma };
1669ab86bbcSShirley Ma 
167986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
168986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
169986a4f4dSJason Wang  */
170986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
171986a4f4dSJason Wang {
1729d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
173986a4f4dSJason Wang }
174986a4f4dSJason Wang 
175986a4f4dSJason Wang static int txq2vq(int txq)
176986a4f4dSJason Wang {
177986a4f4dSJason Wang 	return txq * 2 + 1;
178986a4f4dSJason Wang }
179986a4f4dSJason Wang 
180986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
181986a4f4dSJason Wang {
1829d0ca6edSRusty Russell 	return vq->index / 2;
183986a4f4dSJason Wang }
184986a4f4dSJason Wang 
185986a4f4dSJason Wang static int rxq2vq(int rxq)
186986a4f4dSJason Wang {
187986a4f4dSJason Wang 	return rxq * 2;
188986a4f4dSJason Wang }
189986a4f4dSJason Wang 
190012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
191296f96fcSRusty Russell {
192012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
193296f96fcSRusty Russell }
194296f96fcSRusty Russell 
1959ab86bbcSShirley Ma /*
1969ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
1979ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
1989ab86bbcSShirley Ma  */
199e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
200fb6813f4SRusty Russell {
2019ab86bbcSShirley Ma 	struct page *end;
2029ab86bbcSShirley Ma 
203e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
2049ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
205e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
206e9d7417bSJason Wang 	rq->pages = page;
207fb6813f4SRusty Russell }
208fb6813f4SRusty Russell 
209e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
210fb6813f4SRusty Russell {
211e9d7417bSJason Wang 	struct page *p = rq->pages;
212fb6813f4SRusty Russell 
2139ab86bbcSShirley Ma 	if (p) {
214e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2159ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2169ab86bbcSShirley Ma 		p->private = 0;
2179ab86bbcSShirley Ma 	} else
218fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
219fb6813f4SRusty Russell 	return p;
220fb6813f4SRusty Russell }
221fb6813f4SRusty Russell 
222e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
223296f96fcSRusty Russell {
224e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
225296f96fcSRusty Russell 
2262cb9c6baSRusty Russell 	/* Suppress further interrupts. */
227e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
22811a3a154SRusty Russell 
229363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
230986a4f4dSJason Wang 	netif_wake_subqueue(vi->dev, vq2txq(vq));
231296f96fcSRusty Russell }
232296f96fcSRusty Russell 
233ab7db917SMichael Dalton static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
234ab7db917SMichael Dalton {
235ab7db917SMichael Dalton 	unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
236ab7db917SMichael Dalton 	return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
237ab7db917SMichael Dalton }
238ab7db917SMichael Dalton 
239ab7db917SMichael Dalton static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
240ab7db917SMichael Dalton {
241ab7db917SMichael Dalton 	return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
242ab7db917SMichael Dalton 
243ab7db917SMichael Dalton }
244ab7db917SMichael Dalton 
245ab7db917SMichael Dalton static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
246ab7db917SMichael Dalton {
247ab7db917SMichael Dalton 	unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
248ab7db917SMichael Dalton 	return (unsigned long)buf | (size - 1);
249ab7db917SMichael Dalton }
250ab7db917SMichael Dalton 
2513464645aSMike Waychison /* Called from bottom half context */
252946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
253946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
2542613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
2552613af0eSMichael Dalton 				   unsigned int len, unsigned int truesize)
2569ab86bbcSShirley Ma {
2579ab86bbcSShirley Ma 	struct sk_buff *skb;
258012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
2592613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
2609ab86bbcSShirley Ma 	char *p;
2619ab86bbcSShirley Ma 
2622613af0eSMichael Dalton 	p = page_address(page) + offset;
2639ab86bbcSShirley Ma 
2649ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
265c67f5db8SPaolo Abeni 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
2669ab86bbcSShirley Ma 	if (unlikely(!skb))
2679ab86bbcSShirley Ma 		return NULL;
2689ab86bbcSShirley Ma 
2699ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
2709ab86bbcSShirley Ma 
271012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
272012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
273012873d0SMichael S. Tsirkin 		hdr_padded_len = sizeof *hdr;
274012873d0SMichael S. Tsirkin 	else
2752613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
2763f2c31d9SMark McLoughlin 
2779ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
2783f2c31d9SMark McLoughlin 
2799ab86bbcSShirley Ma 	len -= hdr_len;
2802613af0eSMichael Dalton 	offset += hdr_padded_len;
2812613af0eSMichael Dalton 	p += hdr_padded_len;
2823f2c31d9SMark McLoughlin 
2833f2c31d9SMark McLoughlin 	copy = len;
2843f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
2853f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
2863f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
2873f2c31d9SMark McLoughlin 
2883f2c31d9SMark McLoughlin 	len -= copy;
2899ab86bbcSShirley Ma 	offset += copy;
2903f2c31d9SMark McLoughlin 
2912613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
2922613af0eSMichael Dalton 		if (len)
2932613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
2942613af0eSMichael Dalton 		else
2952613af0eSMichael Dalton 			put_page(page);
2962613af0eSMichael Dalton 		return skb;
2972613af0eSMichael Dalton 	}
2982613af0eSMichael Dalton 
299e878d78bSSasha Levin 	/*
300e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
301e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
302e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
303e878d78bSSasha Levin 	 * the case of a broken device.
304e878d78bSSasha Levin 	 */
305e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
306be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
307e878d78bSSasha Levin 		dev_kfree_skb(skb);
308e878d78bSSasha Levin 		return NULL;
309e878d78bSSasha Levin 	}
3102613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
3119ab86bbcSShirley Ma 	while (len) {
3122613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
3132613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
3142613af0eSMichael Dalton 				frag_size, truesize);
3152613af0eSMichael Dalton 		len -= frag_size;
3169ab86bbcSShirley Ma 		page = (struct page *)page->private;
3179ab86bbcSShirley Ma 		offset = 0;
3183f2c31d9SMark McLoughlin 	}
3193f2c31d9SMark McLoughlin 
3209ab86bbcSShirley Ma 	if (page)
321e9d7417bSJason Wang 		give_pages(rq, page);
3223f2c31d9SMark McLoughlin 
3239ab86bbcSShirley Ma 	return skb;
3249ab86bbcSShirley Ma }
3259ab86bbcSShirley Ma 
326012873d0SMichael S. Tsirkin static struct sk_buff *receive_small(struct virtnet_info *vi, void *buf, unsigned int len)
327f121159dSMichael S. Tsirkin {
328f121159dSMichael S. Tsirkin 	struct sk_buff * skb = buf;
329f121159dSMichael S. Tsirkin 
330012873d0SMichael S. Tsirkin 	len -= vi->hdr_len;
331f121159dSMichael S. Tsirkin 	skb_trim(skb, len);
332f121159dSMichael S. Tsirkin 
333f121159dSMichael S. Tsirkin 	return skb;
334f121159dSMichael S. Tsirkin }
335f121159dSMichael S. Tsirkin 
336f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev,
337946fa564SMichael S. Tsirkin 				   struct virtnet_info *vi,
338f121159dSMichael S. Tsirkin 				   struct receive_queue *rq,
339f121159dSMichael S. Tsirkin 				   void *buf,
340f121159dSMichael S. Tsirkin 				   unsigned int len)
341f121159dSMichael S. Tsirkin {
342f121159dSMichael S. Tsirkin 	struct page *page = buf;
343946fa564SMichael S. Tsirkin 	struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
344f121159dSMichael S. Tsirkin 
345f121159dSMichael S. Tsirkin 	if (unlikely(!skb))
346f121159dSMichael S. Tsirkin 		goto err;
347f121159dSMichael S. Tsirkin 
348f121159dSMichael S. Tsirkin 	return skb;
349f121159dSMichael S. Tsirkin 
350f121159dSMichael S. Tsirkin err:
351f121159dSMichael S. Tsirkin 	dev->stats.rx_dropped++;
352f121159dSMichael S. Tsirkin 	give_pages(rq, page);
353f121159dSMichael S. Tsirkin 	return NULL;
354f121159dSMichael S. Tsirkin }
355f121159dSMichael S. Tsirkin 
3568fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
357fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
3588fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
359ab7db917SMichael Dalton 					 unsigned long ctx,
3608fc3b9e9SMichael S. Tsirkin 					 unsigned int len)
3619ab86bbcSShirley Ma {
362ab7db917SMichael Dalton 	void *buf = mergeable_ctx_to_buf_address(ctx);
363012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
364012873d0SMichael S. Tsirkin 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
3658fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
3668fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
367ab7db917SMichael Dalton 	unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
368ab7db917SMichael Dalton 
369946fa564SMichael S. Tsirkin 	struct sk_buff *head_skb = page_to_skb(vi, rq, page, offset, len,
370946fa564SMichael S. Tsirkin 					       truesize);
3712613af0eSMichael Dalton 	struct sk_buff *curr_skb = head_skb;
3729ab86bbcSShirley Ma 
3738fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
3748fc3b9e9SMichael S. Tsirkin 		goto err_skb;
3759ab86bbcSShirley Ma 	while (--num_buf) {
3768fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
3778fc3b9e9SMichael S. Tsirkin 
378ab7db917SMichael Dalton 		ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
379ab7db917SMichael Dalton 		if (unlikely(!ctx)) {
3808fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
381fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
382012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
383012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
3848fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
3858fc3b9e9SMichael S. Tsirkin 			goto err_buf;
3863f2c31d9SMark McLoughlin 		}
3878fc3b9e9SMichael S. Tsirkin 
388ab7db917SMichael Dalton 		buf = mergeable_ctx_to_buf_address(ctx);
3898fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
3908fc3b9e9SMichael S. Tsirkin 
3918fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
3922613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
3932613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
3948fc3b9e9SMichael S. Tsirkin 
3958fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
3968fc3b9e9SMichael S. Tsirkin 				goto err_skb;
3972613af0eSMichael Dalton 			if (curr_skb == head_skb)
3982613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
3992613af0eSMichael Dalton 			else
4002613af0eSMichael Dalton 				curr_skb->next = nskb;
4012613af0eSMichael Dalton 			curr_skb = nskb;
4022613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
4032613af0eSMichael Dalton 			num_skb_frags = 0;
4042613af0eSMichael Dalton 		}
405ab7db917SMichael Dalton 		truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
4062613af0eSMichael Dalton 		if (curr_skb != head_skb) {
4072613af0eSMichael Dalton 			head_skb->data_len += len;
4082613af0eSMichael Dalton 			head_skb->len += len;
409fb51879dSMichael Dalton 			head_skb->truesize += truesize;
4102613af0eSMichael Dalton 		}
4118fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
412ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
413ba275241SJason Wang 			put_page(page);
414ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
415fb51879dSMichael Dalton 					     len, truesize);
416ba275241SJason Wang 		} else {
4172613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
418fb51879dSMichael Dalton 					offset, len, truesize);
419ba275241SJason Wang 		}
4208fc3b9e9SMichael S. Tsirkin 	}
4218fc3b9e9SMichael S. Tsirkin 
4225377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
4238fc3b9e9SMichael S. Tsirkin 	return head_skb;
4248fc3b9e9SMichael S. Tsirkin 
4258fc3b9e9SMichael S. Tsirkin err_skb:
4268fc3b9e9SMichael S. Tsirkin 	put_page(page);
4278fc3b9e9SMichael S. Tsirkin 	while (--num_buf) {
428ab7db917SMichael Dalton 		ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
429ab7db917SMichael Dalton 		if (unlikely(!ctx)) {
4308fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
4318fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
4328fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
4338fc3b9e9SMichael S. Tsirkin 			break;
4348fc3b9e9SMichael S. Tsirkin 		}
435ab7db917SMichael Dalton 		page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
4368fc3b9e9SMichael S. Tsirkin 		put_page(page);
4373f2c31d9SMark McLoughlin 	}
4388fc3b9e9SMichael S. Tsirkin err_buf:
4398fc3b9e9SMichael S. Tsirkin 	dev->stats.rx_dropped++;
4408fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
4418fc3b9e9SMichael S. Tsirkin 	return NULL;
4429ab86bbcSShirley Ma }
4439ab86bbcSShirley Ma 
444946fa564SMichael S. Tsirkin static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
445946fa564SMichael S. Tsirkin 			void *buf, unsigned int len)
4469ab86bbcSShirley Ma {
447e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
44858472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
4499ab86bbcSShirley Ma 	struct sk_buff *skb;
450012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
4519ab86bbcSShirley Ma 
452bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
4539ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
4549ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
455ab7db917SMichael Dalton 		if (vi->mergeable_rx_bufs) {
456ab7db917SMichael Dalton 			unsigned long ctx = (unsigned long)buf;
457ab7db917SMichael Dalton 			void *base = mergeable_ctx_to_buf_address(ctx);
458ab7db917SMichael Dalton 			put_page(virt_to_head_page(base));
459ab7db917SMichael Dalton 		} else if (vi->big_packets) {
46098bfd23cSMichael Dalton 			give_pages(rq, buf);
461ab7db917SMichael Dalton 		} else {
4629ab86bbcSShirley Ma 			dev_kfree_skb(buf);
463ab7db917SMichael Dalton 		}
4649ab86bbcSShirley Ma 		return;
4659ab86bbcSShirley Ma 	}
4669ab86bbcSShirley Ma 
467f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
468fdd819b2SMichael S. Tsirkin 		skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
469f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
470946fa564SMichael S. Tsirkin 		skb = receive_big(dev, vi, rq, buf, len);
471f121159dSMichael S. Tsirkin 	else
472012873d0SMichael S. Tsirkin 		skb = receive_small(vi, buf, len);
473f121159dSMichael S. Tsirkin 
4748fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
4752613af0eSMichael Dalton 		return;
4763f2c31d9SMark McLoughlin 
4779ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
4783fa2a1dfSstephen hemminger 
47983a27052SEric Dumazet 	u64_stats_update_begin(&stats->rx_syncp);
4803fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
4813fa2a1dfSstephen hemminger 	stats->rx_packets++;
48283a27052SEric Dumazet 	u64_stats_update_end(&stats->rx_syncp);
483296f96fcSRusty Russell 
484e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
48510a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
486296f96fcSRusty Russell 
487e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
488e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
489e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
490e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
491fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
492296f96fcSRusty Russell 		goto frame_err;
493296f96fcSRusty Russell 	}
494296f96fcSRusty Russell 
495d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
496d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
497d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
498d1dc06dcSMike Rapoport 
4990fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
500296f96fcSRusty Russell 	return;
501296f96fcSRusty Russell 
502296f96fcSRusty Russell frame_err:
503296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
504296f96fcSRusty Russell 	dev_kfree_skb(skb);
505296f96fcSRusty Russell }
506296f96fcSRusty Russell 
507946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
508946fa564SMichael S. Tsirkin 			     gfp_t gfp)
509296f96fcSRusty Russell {
510296f96fcSRusty Russell 	struct sk_buff *skb;
511012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
5129ab86bbcSShirley Ma 	int err;
5133f2c31d9SMark McLoughlin 
5145061de36SMichael Dalton 	skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
5159ab86bbcSShirley Ma 	if (unlikely(!skb))
5169ab86bbcSShirley Ma 		return -ENOMEM;
517296f96fcSRusty Russell 
5185061de36SMichael Dalton 	skb_put(skb, GOOD_PACKET_LEN);
5193f2c31d9SMark McLoughlin 
5203f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
521547c890cSJason Wang 	sg_init_table(rq->sg, 2);
522012873d0SMichael S. Tsirkin 	sg_set_buf(rq->sg, hdr, vi->hdr_len);
523e9d7417bSJason Wang 	skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
52497402b96SHerbert Xu 
5259dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
5269ab86bbcSShirley Ma 	if (err < 0)
5279ab86bbcSShirley Ma 		dev_kfree_skb(skb);
52897402b96SHerbert Xu 
5299ab86bbcSShirley Ma 	return err;
53097402b96SHerbert Xu }
53197402b96SHerbert Xu 
532012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
533012873d0SMichael S. Tsirkin 			   gfp_t gfp)
5349ab86bbcSShirley Ma {
5359ab86bbcSShirley Ma 	struct page *first, *list = NULL;
5369ab86bbcSShirley Ma 	char *p;
5379ab86bbcSShirley Ma 	int i, err, offset;
538296f96fcSRusty Russell 
539a5835440SRusty Russell 	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
540a5835440SRusty Russell 
541e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
5429ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
543e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
5449ab86bbcSShirley Ma 		if (!first) {
5459ab86bbcSShirley Ma 			if (list)
546e9d7417bSJason Wang 				give_pages(rq, list);
5479ab86bbcSShirley Ma 			return -ENOMEM;
548296f96fcSRusty Russell 		}
549e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
5509ab86bbcSShirley Ma 
5519ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
5529ab86bbcSShirley Ma 		first->private = (unsigned long)list;
5539ab86bbcSShirley Ma 		list = first;
5549ab86bbcSShirley Ma 	}
5559ab86bbcSShirley Ma 
556e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
5579ab86bbcSShirley Ma 	if (!first) {
558e9d7417bSJason Wang 		give_pages(rq, list);
5599ab86bbcSShirley Ma 		return -ENOMEM;
5609ab86bbcSShirley Ma 	}
5619ab86bbcSShirley Ma 	p = page_address(first);
5629ab86bbcSShirley Ma 
563e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
564012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
565012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
5669ab86bbcSShirley Ma 
567e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
5689ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
569e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
5709ab86bbcSShirley Ma 
5719ab86bbcSShirley Ma 	/* chain first in list head */
5729ab86bbcSShirley Ma 	first->private = (unsigned long)list;
5739dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
574aa989f5eSMichael S. Tsirkin 				  first, gfp);
5759ab86bbcSShirley Ma 	if (err < 0)
576e9d7417bSJason Wang 		give_pages(rq, first);
5779ab86bbcSShirley Ma 
5789ab86bbcSShirley Ma 	return err;
5799ab86bbcSShirley Ma }
5809ab86bbcSShirley Ma 
5815377d758SJohannes Berg static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
5829ab86bbcSShirley Ma {
583ab7db917SMichael Dalton 	const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
584fbf28d78SMichael Dalton 	unsigned int len;
585fbf28d78SMichael Dalton 
5865377d758SJohannes Berg 	len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
587fbf28d78SMichael Dalton 			GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
588fbf28d78SMichael Dalton 	return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
589fbf28d78SMichael Dalton }
590fbf28d78SMichael Dalton 
591fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
592fbf28d78SMichael Dalton {
593fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
594fb51879dSMichael Dalton 	char *buf;
595ab7db917SMichael Dalton 	unsigned long ctx;
5969ab86bbcSShirley Ma 	int err;
597fb51879dSMichael Dalton 	unsigned int len, hole;
5989ab86bbcSShirley Ma 
599fbf28d78SMichael Dalton 	len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
600ab7db917SMichael Dalton 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
6019ab86bbcSShirley Ma 		return -ENOMEM;
602ab7db917SMichael Dalton 
603fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
604ab7db917SMichael Dalton 	ctx = mergeable_buf_to_ctx(buf, len);
605fb51879dSMichael Dalton 	get_page(alloc_frag->page);
606fb51879dSMichael Dalton 	alloc_frag->offset += len;
607fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
608ab7db917SMichael Dalton 	if (hole < len) {
609ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
610ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
611ab7db917SMichael Dalton 		 * the current buffer. This extra space is not included in
612ab7db917SMichael Dalton 		 * the truesize stored in ctx.
613ab7db917SMichael Dalton 		 */
614fb51879dSMichael Dalton 		len += hole;
615fb51879dSMichael Dalton 		alloc_frag->offset += hole;
616fb51879dSMichael Dalton 	}
6179ab86bbcSShirley Ma 
618fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
619ab7db917SMichael Dalton 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
6209ab86bbcSShirley Ma 	if (err < 0)
6212613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
6229ab86bbcSShirley Ma 
6239ab86bbcSShirley Ma 	return err;
624296f96fcSRusty Russell }
625296f96fcSRusty Russell 
626b2baed69SRusty Russell /*
627b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
628b2baed69SRusty Russell  *
629b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
630b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
631b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
632b2baed69SRusty Russell  */
633946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
634946fa564SMichael S. Tsirkin 			  gfp_t gfp)
6353f2c31d9SMark McLoughlin {
6363f2c31d9SMark McLoughlin 	int err;
6371788f495SMichael S. Tsirkin 	bool oom;
6383f2c31d9SMark McLoughlin 
639fb51879dSMichael Dalton 	gfp |= __GFP_COLD;
6400aea51c3SAmit Shah 	do {
6419ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
642e9d7417bSJason Wang 			err = add_recvbuf_mergeable(rq, gfp);
6439ab86bbcSShirley Ma 		else if (vi->big_packets)
644012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
6459ab86bbcSShirley Ma 		else
646946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
6473f2c31d9SMark McLoughlin 
6481788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
6499ed4cb07SRusty Russell 		if (err)
6503f2c31d9SMark McLoughlin 			break;
651b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
652681daee2SJason Wang 	virtqueue_kick(rq->vq);
6533161e453SRusty Russell 	return !oom;
6543f2c31d9SMark McLoughlin }
6553f2c31d9SMark McLoughlin 
65618445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
657296f96fcSRusty Russell {
658296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
659986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
660e9d7417bSJason Wang 
66118445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
662e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
6631915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
664e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
66518445c4dSRusty Russell 	}
666296f96fcSRusty Russell }
667296f96fcSRusty Russell 
668e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
6693e9d08ecSBruce Rogers {
670e9d7417bSJason Wang 	napi_enable(&rq->napi);
6713e9d08ecSBruce Rogers 
6723e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
6733e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
6743e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
6753e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
676e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
677e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
678ec13ee80SMichael S. Tsirkin 		local_bh_disable();
679e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
680ec13ee80SMichael S. Tsirkin 		local_bh_enable();
6813e9d08ecSBruce Rogers 	}
6823e9d08ecSBruce Rogers }
6833e9d08ecSBruce Rogers 
6843161e453SRusty Russell static void refill_work(struct work_struct *work)
6853161e453SRusty Russell {
686e9d7417bSJason Wang 	struct virtnet_info *vi =
687e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
6883161e453SRusty Russell 	bool still_empty;
689986a4f4dSJason Wang 	int i;
6903161e453SRusty Russell 
69155257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
692986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
693986a4f4dSJason Wang 
694986a4f4dSJason Wang 		napi_disable(&rq->napi);
695946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
696986a4f4dSJason Wang 		virtnet_napi_enable(rq);
6973161e453SRusty Russell 
6983161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
699986a4f4dSJason Wang 		 * we will *never* try to fill again.
700986a4f4dSJason Wang 		 */
7013161e453SRusty Russell 		if (still_empty)
7023b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
7033161e453SRusty Russell 	}
704986a4f4dSJason Wang }
7053161e453SRusty Russell 
7062ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget)
707296f96fcSRusty Russell {
708e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
7092ffa7598SJason Wang 	unsigned int len, received = 0;
7109ab86bbcSShirley Ma 	void *buf;
711296f96fcSRusty Russell 
712296f96fcSRusty Russell 	while (received < budget &&
713e9d7417bSJason Wang 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
714946fa564SMichael S. Tsirkin 		receive_buf(vi, rq, buf, len);
715296f96fcSRusty Russell 		received++;
716296f96fcSRusty Russell 	}
717296f96fcSRusty Russell 
718be121f46SJason Wang 	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
719946fa564SMichael S. Tsirkin 		if (!try_fill_recv(vi, rq, GFP_ATOMIC))
7203b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
7213161e453SRusty Russell 	}
722296f96fcSRusty Russell 
7232ffa7598SJason Wang 	return received;
7242ffa7598SJason Wang }
7252ffa7598SJason Wang 
7262ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
7272ffa7598SJason Wang {
7282ffa7598SJason Wang 	struct receive_queue *rq =
7292ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
730faadb05fSLi RongQing 	unsigned int r, received;
7312ffa7598SJason Wang 
732faadb05fSLi RongQing 	received = virtnet_receive(rq, budget);
7332ffa7598SJason Wang 
7348329d98eSRusty Russell 	/* Out of packets? */
7358329d98eSRusty Russell 	if (received < budget) {
736cbdadbbfSMichael S. Tsirkin 		r = virtqueue_enable_cb_prepare(rq->vq);
7370fbd050aSEric Dumazet 		napi_complete_done(napi, received);
738cbdadbbfSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(rq->vq, r)) &&
7398e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
740e9d7417bSJason Wang 			virtqueue_disable_cb(rq->vq);
741288379f0SBen Hutchings 			__napi_schedule(napi);
742296f96fcSRusty Russell 		}
7434265f161SChristian Borntraeger 	}
744296f96fcSRusty Russell 
745296f96fcSRusty Russell 	return received;
746296f96fcSRusty Russell }
747296f96fcSRusty Russell 
74891815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL
74991815639SJason Wang /* must be called with local_bh_disable()d */
75091815639SJason Wang static int virtnet_busy_poll(struct napi_struct *napi)
75191815639SJason Wang {
75291815639SJason Wang 	struct receive_queue *rq =
75391815639SJason Wang 		container_of(napi, struct receive_queue, napi);
75491815639SJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
75591815639SJason Wang 	int r, received = 0, budget = 4;
75691815639SJason Wang 
75791815639SJason Wang 	if (!(vi->status & VIRTIO_NET_S_LINK_UP))
75891815639SJason Wang 		return LL_FLUSH_FAILED;
75991815639SJason Wang 
76091815639SJason Wang 	if (!napi_schedule_prep(napi))
76191815639SJason Wang 		return LL_FLUSH_BUSY;
76291815639SJason Wang 
76391815639SJason Wang 	virtqueue_disable_cb(rq->vq);
76491815639SJason Wang 
76591815639SJason Wang again:
76691815639SJason Wang 	received += virtnet_receive(rq, budget);
76791815639SJason Wang 
76891815639SJason Wang 	r = virtqueue_enable_cb_prepare(rq->vq);
76991815639SJason Wang 	clear_bit(NAPI_STATE_SCHED, &napi->state);
77091815639SJason Wang 	if (unlikely(virtqueue_poll(rq->vq, r)) &&
77191815639SJason Wang 	    napi_schedule_prep(napi)) {
77291815639SJason Wang 		virtqueue_disable_cb(rq->vq);
77391815639SJason Wang 		if (received < budget) {
77491815639SJason Wang 			budget -= received;
77591815639SJason Wang 			goto again;
77691815639SJason Wang 		} else {
77791815639SJason Wang 			__napi_schedule(napi);
77891815639SJason Wang 		}
77991815639SJason Wang 	}
78091815639SJason Wang 
78191815639SJason Wang 	return received;
78291815639SJason Wang }
78391815639SJason Wang #endif	/* CONFIG_NET_RX_BUSY_POLL */
78491815639SJason Wang 
785986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
786986a4f4dSJason Wang {
787986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
788986a4f4dSJason Wang 	int i;
789986a4f4dSJason Wang 
790e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
791e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
792986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
793946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
794986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
795986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
796986a4f4dSJason Wang 	}
797986a4f4dSJason Wang 
798986a4f4dSJason Wang 	return 0;
799986a4f4dSJason Wang }
800986a4f4dSJason Wang 
801b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
802296f96fcSRusty Russell {
803296f96fcSRusty Russell 	struct sk_buff *skb;
8046ee57bccSMichael S. Tsirkin 	unsigned int len;
805e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
80658472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
807296f96fcSRusty Russell 
808e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
809296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
8103fa2a1dfSstephen hemminger 
81183a27052SEric Dumazet 		u64_stats_update_begin(&stats->tx_syncp);
8123fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
8133fa2a1dfSstephen hemminger 		stats->tx_packets++;
81483a27052SEric Dumazet 		u64_stats_update_end(&stats->tx_syncp);
8153fa2a1dfSstephen hemminger 
816ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
817296f96fcSRusty Russell 	}
818296f96fcSRusty Russell }
819296f96fcSRusty Russell 
820e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
821296f96fcSRusty Russell {
822012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
823296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
824e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
8257bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
826012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
827e7428e95SMichael S. Tsirkin 	bool can_push;
828296f96fcSRusty Russell 
829e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
830e7428e95SMichael S. Tsirkin 
831e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
832e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
833e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
834e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
835e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
836e7428e95SMichael S. Tsirkin 	if (can_push)
837012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
838e7428e95SMichael S. Tsirkin 	else
839e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
840296f96fcSRusty Russell 
841e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
842e858fae2SMike Rapoport 				    virtio_is_little_endian(vi->vdev)))
843296f96fcSRusty Russell 		BUG();
844296f96fcSRusty Russell 
845e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
846012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
8473f2c31d9SMark McLoughlin 
848547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
849e7428e95SMichael S. Tsirkin 	if (can_push) {
850e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
851e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
852e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
853e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
854e7428e95SMichael S. Tsirkin 	} else {
855e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
856b7dfde95SLinus Torvalds 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
857e7428e95SMichael S. Tsirkin 	}
8589dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
85911a3a154SRusty Russell }
86011a3a154SRusty Russell 
861424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
86299ffc696SRusty Russell {
86399ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
864986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
865986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
8669ed4cb07SRusty Russell 	int err;
8674b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
8684b7fd2e6SMichael S. Tsirkin 	bool kick = !skb->xmit_more;
8692cb9c6baSRusty Russell 
8702cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
871e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
87299ffc696SRusty Russell 
873074c3582SJacob Keller 	/* timestamp packet in software */
874074c3582SJacob Keller 	skb_tx_timestamp(skb);
875074c3582SJacob Keller 
87603f191baSMichael S. Tsirkin 	/* Try to transmit */
877b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
87899ffc696SRusty Russell 
8799ed4cb07SRusty Russell 	/* This should not happen! */
880681daee2SJason Wang 	if (unlikely(err)) {
88158eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
8822e57b79cSRick Jones 		if (net_ratelimit())
88358eba97dSRusty Russell 			dev_warn(&dev->dev,
884b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
88558eba97dSRusty Russell 		dev->stats.tx_dropped++;
88685e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
88758eba97dSRusty Russell 		return NETDEV_TX_OK;
888296f96fcSRusty Russell 	}
88903f191baSMichael S. Tsirkin 
89048925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
89148925e37SRusty Russell 	skb_orphan(skb);
89248925e37SRusty Russell 	nf_reset(skb);
89348925e37SRusty Russell 
89460302ff6SMichael S. Tsirkin 	/* If running out of space, stop queue to avoid getting packets that we
89560302ff6SMichael S. Tsirkin 	 * are then unable to transmit.
89660302ff6SMichael S. Tsirkin 	 * An alternative would be to force queuing layer to requeue the skb by
89760302ff6SMichael S. Tsirkin 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
89860302ff6SMichael S. Tsirkin 	 * returned in a normal path of operation: it means that driver is not
89960302ff6SMichael S. Tsirkin 	 * maintaining the TX queue stop/start state properly, and causes
90060302ff6SMichael S. Tsirkin 	 * the stack to do a non-trivial amount of useless work.
90160302ff6SMichael S. Tsirkin 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
90260302ff6SMichael S. Tsirkin 	 * early means 16 slots are typically wasted.
903d631b94eSstephen hemminger 	 */
904b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
905986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
906e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
90748925e37SRusty Russell 			/* More just got used, free them then recheck. */
908b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
909b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
910986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
911e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
91248925e37SRusty Russell 			}
91348925e37SRusty Russell 		}
91448925e37SRusty Russell 	}
91548925e37SRusty Russell 
9164b7fd2e6SMichael S. Tsirkin 	if (kick || netif_xmit_stopped(txq))
917c223a078SDavid S. Miller 		virtqueue_kick(sq->vq);
9180b725a2cSDavid S. Miller 
9190b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
920c223a078SDavid S. Miller }
921c223a078SDavid S. Miller 
92240cbfc37SAmos Kong /*
92340cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
92440cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
925788a8b6dSstephen hemminger  * never fail unless improperly formatted.
92640cbfc37SAmos Kong  */
92740cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
928d24bae32Sstephen hemminger 				 struct scatterlist *out)
92940cbfc37SAmos Kong {
930f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
931d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
93240cbfc37SAmos Kong 
93340cbfc37SAmos Kong 	/* Caller should know better */
934f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
93540cbfc37SAmos Kong 
9362ac46030SMichael S. Tsirkin 	vi->ctrl_status = ~0;
9372ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.class = class;
9382ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.cmd = cmd;
939f7bc9594SRusty Russell 	/* Add header */
9402ac46030SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
941f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
94240cbfc37SAmos Kong 
943f7bc9594SRusty Russell 	if (out)
944f7bc9594SRusty Russell 		sgs[out_num++] = out;
94540cbfc37SAmos Kong 
946f7bc9594SRusty Russell 	/* Add return status. */
9472ac46030SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
948d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
94940cbfc37SAmos Kong 
950d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
951a7c58146SRusty Russell 	virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
95240cbfc37SAmos Kong 
95367975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
9542ac46030SMichael S. Tsirkin 		return vi->ctrl_status == VIRTIO_NET_OK;
95540cbfc37SAmos Kong 
95640cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
95740cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
95840cbfc37SAmos Kong 	 */
959047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
960047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
96140cbfc37SAmos Kong 		cpu_relax();
96240cbfc37SAmos Kong 
9632ac46030SMichael S. Tsirkin 	return vi->ctrl_status == VIRTIO_NET_OK;
96440cbfc37SAmos Kong }
96540cbfc37SAmos Kong 
9669c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
9679c46f6d4SAlex Williamson {
9689c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
9699c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
970f2f2c8b4SJiri Pirko 	int ret;
9717e58d5aeSAmos Kong 	struct sockaddr *addr = p;
9727e58d5aeSAmos Kong 	struct scatterlist sg;
9739c46f6d4SAlex Williamson 
9747e58d5aeSAmos Kong 	ret = eth_prepare_mac_addr_change(dev, p);
975f2f2c8b4SJiri Pirko 	if (ret)
976f2f2c8b4SJiri Pirko 		return ret;
9779c46f6d4SAlex Williamson 
9787e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
9797e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
9807e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
981d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
9827e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
9837e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
9847e58d5aeSAmos Kong 			return -EINVAL;
9857e58d5aeSAmos Kong 		}
9867e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
9877e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
988855e0c52SRusty Russell 		unsigned int i;
989855e0c52SRusty Russell 
990855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
991855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
992855e0c52SRusty Russell 			virtio_cwrite8(vdev,
993855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
994855e0c52SRusty Russell 				       i, addr->sa_data[i]);
9957e58d5aeSAmos Kong 	}
9967e58d5aeSAmos Kong 
9977e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
9989c46f6d4SAlex Williamson 
9999c46f6d4SAlex Williamson 	return 0;
10009c46f6d4SAlex Williamson }
10019c46f6d4SAlex Williamson 
10023fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
10033fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
10043fa2a1dfSstephen hemminger {
10053fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
10063fa2a1dfSstephen hemminger 	int cpu;
10073fa2a1dfSstephen hemminger 	unsigned int start;
10083fa2a1dfSstephen hemminger 
10093fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
101058472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
10113fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
10123fa2a1dfSstephen hemminger 
10133fa2a1dfSstephen hemminger 		do {
101457a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
10153fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
10163fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
101757a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
101883a27052SEric Dumazet 
101983a27052SEric Dumazet 		do {
102057a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
10213fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
10223fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
102357a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
10243fa2a1dfSstephen hemminger 
10253fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
10263fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
10273fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
10283fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
10293fa2a1dfSstephen hemminger 	}
10303fa2a1dfSstephen hemminger 
10313fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
1032021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
10333fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
10343fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
10353fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
10363fa2a1dfSstephen hemminger 
10373fa2a1dfSstephen hemminger 	return tot;
10383fa2a1dfSstephen hemminger }
10393fa2a1dfSstephen hemminger 
1040da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
1041da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
1042da74e89dSAmit Shah {
1043da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
1044986a4f4dSJason Wang 	int i;
1045da74e89dSAmit Shah 
1046986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
1047986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
1048da74e89dSAmit Shah }
1049da74e89dSAmit Shah #endif
1050da74e89dSAmit Shah 
1051586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
1052586d17c5SJason Wang {
1053586d17c5SJason Wang 	rtnl_lock();
1054586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1055d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1056586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1057586d17c5SJason Wang 	rtnl_unlock();
1058586d17c5SJason Wang }
1059586d17c5SJason Wang 
1060986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1061986a4f4dSJason Wang {
1062986a4f4dSJason Wang 	struct scatterlist sg;
1063986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
1064986a4f4dSJason Wang 
1065986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1066986a4f4dSJason Wang 		return 0;
1067986a4f4dSJason Wang 
1068a725ee3eSAndy Lutomirski 	vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1069a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
1070986a4f4dSJason Wang 
1071986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1072d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1073986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1074986a4f4dSJason Wang 			 queue_pairs);
1075986a4f4dSJason Wang 		return -EINVAL;
107655257d72SSasha Levin 	} else {
1077986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
107835ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
107935ed159bSJason Wang 		if (dev->flags & IFF_UP)
10809b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
108155257d72SSasha Levin 	}
1082986a4f4dSJason Wang 
1083986a4f4dSJason Wang 	return 0;
1084986a4f4dSJason Wang }
1085986a4f4dSJason Wang 
1086296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
1087296f96fcSRusty Russell {
1088296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1089986a4f4dSJason Wang 	int i;
1090296f96fcSRusty Russell 
1091b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1092b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1093986a4f4dSJason Wang 
1094986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
1095986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
1096296f96fcSRusty Russell 
1097296f96fcSRusty Russell 	return 0;
1098296f96fcSRusty Russell }
1099296f96fcSRusty Russell 
11002af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
11012af7698eSAlex Williamson {
11022af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1103f565a7c2SAlex Williamson 	struct scatterlist sg[2];
1104f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1105ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
110632e7bfc4SJiri Pirko 	int uc_count;
11074cd24eafSJiri Pirko 	int mc_count;
1108f565a7c2SAlex Williamson 	void *buf;
1109f565a7c2SAlex Williamson 	int i;
11102af7698eSAlex Williamson 
1111788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
11122af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
11132af7698eSAlex Williamson 		return;
11142af7698eSAlex Williamson 
11152ac46030SMichael S. Tsirkin 	vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
11162ac46030SMichael S. Tsirkin 	vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
11172af7698eSAlex Williamson 
11182ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
11192af7698eSAlex Williamson 
11202af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1121d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
11222af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
11232ac46030SMichael S. Tsirkin 			 vi->ctrl_promisc ? "en" : "dis");
11242af7698eSAlex Williamson 
11252ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
11262af7698eSAlex Williamson 
11272af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1128d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
11292af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
11302ac46030SMichael S. Tsirkin 			 vi->ctrl_allmulti ? "en" : "dis");
1131f565a7c2SAlex Williamson 
113232e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
11334cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1134f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
11354cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1136f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
11374cd24eafSJiri Pirko 	mac_data = buf;
1138e68ed8f0SJoe Perches 	if (!buf)
1139f565a7c2SAlex Williamson 		return;
1140f565a7c2SAlex Williamson 
114123e258e1SAlex Williamson 	sg_init_table(sg, 2);
114223e258e1SAlex Williamson 
1143f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
1144fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1145ccffad25SJiri Pirko 	i = 0;
114632e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1147ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1148f565a7c2SAlex Williamson 
1149f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
115032e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1151f565a7c2SAlex Williamson 
1152f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
115332e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1154f565a7c2SAlex Williamson 
1155fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1156567ec874SJiri Pirko 	i = 0;
115722bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
115822bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1159f565a7c2SAlex Williamson 
1160f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
11614cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1162f565a7c2SAlex Williamson 
1163f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1164d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
116599e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1166f565a7c2SAlex Williamson 
1167f565a7c2SAlex Williamson 	kfree(buf);
11682af7698eSAlex Williamson }
11692af7698eSAlex Williamson 
117080d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
117180d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
11720bde9569SAlex Williamson {
11730bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11740bde9569SAlex Williamson 	struct scatterlist sg;
11750bde9569SAlex Williamson 
1176a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1177a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
11780bde9569SAlex Williamson 
11790bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1180d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
11810bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
11828e586137SJiri Pirko 	return 0;
11830bde9569SAlex Williamson }
11840bde9569SAlex Williamson 
118580d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
118680d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
11870bde9569SAlex Williamson {
11880bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11890bde9569SAlex Williamson 	struct scatterlist sg;
11900bde9569SAlex Williamson 
1191a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1192a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
11930bde9569SAlex Williamson 
11940bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1195d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
11960bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
11978e586137SJiri Pirko 	return 0;
11980bde9569SAlex Williamson }
11990bde9569SAlex Williamson 
12008898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1201986a4f4dSJason Wang {
1202986a4f4dSJason Wang 	int i;
12038898c21cSWanlong Gao 
12048898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
12058898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
12068898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
12078898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
12088898c21cSWanlong Gao 		}
12098898c21cSWanlong Gao 
12108898c21cSWanlong Gao 		vi->affinity_hint_set = false;
12118898c21cSWanlong Gao 	}
12128898c21cSWanlong Gao }
12138898c21cSWanlong Gao 
12148898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1215986a4f4dSJason Wang {
1216986a4f4dSJason Wang 	int i;
121747be2479SWanlong Gao 	int cpu;
1218986a4f4dSJason Wang 
1219986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1220986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1221986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1222986a4f4dSJason Wang 	 */
12238898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
12248898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
12258898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1226986a4f4dSJason Wang 		return;
1227986a4f4dSJason Wang 	}
1228986a4f4dSJason Wang 
122947be2479SWanlong Gao 	i = 0;
123047be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1231986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1232986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
12339bb8ca86SJason Wang 		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
123447be2479SWanlong Gao 		i++;
1235986a4f4dSJason Wang 	}
1236986a4f4dSJason Wang 
1237986a4f4dSJason Wang 	vi->affinity_hint_set = true;
123847be2479SWanlong Gao }
1239986a4f4dSJason Wang 
12408de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb,
12418de4b2f3SWanlong Gao 			        unsigned long action, void *hcpu)
12428de4b2f3SWanlong Gao {
12438de4b2f3SWanlong Gao 	struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
12448de4b2f3SWanlong Gao 
12458de4b2f3SWanlong Gao 	switch(action & ~CPU_TASKS_FROZEN) {
12468de4b2f3SWanlong Gao 	case CPU_ONLINE:
12478de4b2f3SWanlong Gao 	case CPU_DOWN_FAILED:
12488de4b2f3SWanlong Gao 	case CPU_DEAD:
12498de4b2f3SWanlong Gao 		virtnet_set_affinity(vi);
12508de4b2f3SWanlong Gao 		break;
12518de4b2f3SWanlong Gao 	case CPU_DOWN_PREPARE:
12528de4b2f3SWanlong Gao 		virtnet_clean_affinity(vi, (long)hcpu);
12538de4b2f3SWanlong Gao 		break;
12548de4b2f3SWanlong Gao 	default:
12558de4b2f3SWanlong Gao 		break;
12568de4b2f3SWanlong Gao 	}
12573ab098dfSJason Wang 
12588de4b2f3SWanlong Gao 	return NOTIFY_OK;
1259a9ea3fc6SHerbert Xu }
1260a9ea3fc6SHerbert Xu 
12618f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
12628f9f4668SRick Jones 				struct ethtool_ringparam *ring)
12638f9f4668SRick Jones {
12648f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
12658f9f4668SRick Jones 
1266986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1267986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
12688f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
12698f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
12708f9f4668SRick Jones }
12718f9f4668SRick Jones 
127266846048SRick Jones 
127366846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
127466846048SRick Jones 				struct ethtool_drvinfo *info)
127566846048SRick Jones {
127666846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
127766846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
127866846048SRick Jones 
127966846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
128066846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
128166846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
128266846048SRick Jones 
128366846048SRick Jones }
128466846048SRick Jones 
1285d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1286d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1287d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1288d73bcd2cSJason Wang {
1289d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1290d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1291d73bcd2cSJason Wang 	int err;
1292d73bcd2cSJason Wang 
1293d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1294d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1295d73bcd2cSJason Wang 	 */
1296d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1297d73bcd2cSJason Wang 		return -EINVAL;
1298d73bcd2cSJason Wang 
1299c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
1300d73bcd2cSJason Wang 		return -EINVAL;
1301d73bcd2cSJason Wang 
130247be2479SWanlong Gao 	get_online_cpus();
1303d73bcd2cSJason Wang 	err = virtnet_set_queues(vi, queue_pairs);
1304d73bcd2cSJason Wang 	if (!err) {
1305d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1306d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1307d73bcd2cSJason Wang 
13088898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1309d73bcd2cSJason Wang 	}
131047be2479SWanlong Gao 	put_online_cpus();
1311d73bcd2cSJason Wang 
1312d73bcd2cSJason Wang 	return err;
1313d73bcd2cSJason Wang }
1314d73bcd2cSJason Wang 
1315d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1316d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1317d73bcd2cSJason Wang {
1318d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1319d73bcd2cSJason Wang 
1320d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1321d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1322d73bcd2cSJason Wang 	channels->max_other = 0;
1323d73bcd2cSJason Wang 	channels->rx_count = 0;
1324d73bcd2cSJason Wang 	channels->tx_count = 0;
1325d73bcd2cSJason Wang 	channels->other_count = 0;
1326d73bcd2cSJason Wang }
1327d73bcd2cSJason Wang 
132816032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */
132916032be5SNikolay Aleksandrov static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
133016032be5SNikolay Aleksandrov {
133116032be5SNikolay Aleksandrov 	struct ethtool_cmd diff1 = *cmd;
133216032be5SNikolay Aleksandrov 	struct ethtool_cmd diff2 = {};
133316032be5SNikolay Aleksandrov 
13340cf3ace9SNikolay Aleksandrov 	/* cmd is always set so we need to clear it, validate the port type
13350cf3ace9SNikolay Aleksandrov 	 * and also without autonegotiation we can ignore advertising
13360cf3ace9SNikolay Aleksandrov 	 */
133716032be5SNikolay Aleksandrov 	ethtool_cmd_speed_set(&diff1, 0);
13380cf3ace9SNikolay Aleksandrov 	diff2.port = PORT_OTHER;
133916032be5SNikolay Aleksandrov 	diff1.advertising = 0;
134016032be5SNikolay Aleksandrov 	diff1.duplex = 0;
134116032be5SNikolay Aleksandrov 	diff1.cmd = 0;
134216032be5SNikolay Aleksandrov 
134316032be5SNikolay Aleksandrov 	return !memcmp(&diff1, &diff2, sizeof(diff1));
134416032be5SNikolay Aleksandrov }
134516032be5SNikolay Aleksandrov 
134616032be5SNikolay Aleksandrov static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
134716032be5SNikolay Aleksandrov {
134816032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
134916032be5SNikolay Aleksandrov 	u32 speed;
135016032be5SNikolay Aleksandrov 
135116032be5SNikolay Aleksandrov 	speed = ethtool_cmd_speed(cmd);
135216032be5SNikolay Aleksandrov 	/* don't allow custom speed and duplex */
135316032be5SNikolay Aleksandrov 	if (!ethtool_validate_speed(speed) ||
135416032be5SNikolay Aleksandrov 	    !ethtool_validate_duplex(cmd->duplex) ||
135516032be5SNikolay Aleksandrov 	    !virtnet_validate_ethtool_cmd(cmd))
135616032be5SNikolay Aleksandrov 		return -EINVAL;
135716032be5SNikolay Aleksandrov 	vi->speed = speed;
135816032be5SNikolay Aleksandrov 	vi->duplex = cmd->duplex;
135916032be5SNikolay Aleksandrov 
136016032be5SNikolay Aleksandrov 	return 0;
136116032be5SNikolay Aleksandrov }
136216032be5SNikolay Aleksandrov 
136316032be5SNikolay Aleksandrov static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
136416032be5SNikolay Aleksandrov {
136516032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
136616032be5SNikolay Aleksandrov 
136716032be5SNikolay Aleksandrov 	ethtool_cmd_speed_set(cmd, vi->speed);
136816032be5SNikolay Aleksandrov 	cmd->duplex = vi->duplex;
136916032be5SNikolay Aleksandrov 	cmd->port = PORT_OTHER;
137016032be5SNikolay Aleksandrov 
137116032be5SNikolay Aleksandrov 	return 0;
137216032be5SNikolay Aleksandrov }
137316032be5SNikolay Aleksandrov 
137416032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
137516032be5SNikolay Aleksandrov {
137616032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
137716032be5SNikolay Aleksandrov 
137816032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
137916032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
138016032be5SNikolay Aleksandrov }
138116032be5SNikolay Aleksandrov 
13820fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
138366846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
13849f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
13858f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1386d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1387d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1388074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
138916032be5SNikolay Aleksandrov 	.get_settings = virtnet_get_settings,
139016032be5SNikolay Aleksandrov 	.set_settings = virtnet_set_settings,
1391a9ea3fc6SHerbert Xu };
1392a9ea3fc6SHerbert Xu 
139339da5814SMark McLoughlin #define MIN_MTU 68
139439da5814SMark McLoughlin #define MAX_MTU 65535
139539da5814SMark McLoughlin 
139639da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
139739da5814SMark McLoughlin {
139839da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
139939da5814SMark McLoughlin 		return -EINVAL;
140039da5814SMark McLoughlin 	dev->mtu = new_mtu;
140139da5814SMark McLoughlin 	return 0;
140239da5814SMark McLoughlin }
140339da5814SMark McLoughlin 
140476288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
140576288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
140676288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
140776288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
140876288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
14099c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
14102af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
141176288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
14123fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
14131824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
14141824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
141576288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
141676288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
141776288b4eSStephen Hemminger #endif
141891815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL
141991815639SJason Wang 	.ndo_busy_poll		= virtnet_busy_poll,
142091815639SJason Wang #endif
142176288b4eSStephen Hemminger };
142276288b4eSStephen Hemminger 
1423586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
14249f4d26d0SMark McLoughlin {
1425586d17c5SJason Wang 	struct virtnet_info *vi =
1426586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
14279f4d26d0SMark McLoughlin 	u16 v;
14289f4d26d0SMark McLoughlin 
1429855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1430855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
1431507613bfSMichael S. Tsirkin 		return;
1432586d17c5SJason Wang 
1433586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1434ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1435586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1436586d17c5SJason Wang 	}
14379f4d26d0SMark McLoughlin 
14389f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
14399f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
14409f4d26d0SMark McLoughlin 
14419f4d26d0SMark McLoughlin 	if (vi->status == v)
1442507613bfSMichael S. Tsirkin 		return;
14439f4d26d0SMark McLoughlin 
14449f4d26d0SMark McLoughlin 	vi->status = v;
14459f4d26d0SMark McLoughlin 
14469f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
14479f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1448986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
14499f4d26d0SMark McLoughlin 	} else {
14509f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1451986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
14529f4d26d0SMark McLoughlin 	}
14539f4d26d0SMark McLoughlin }
14549f4d26d0SMark McLoughlin 
14559f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
14569f4d26d0SMark McLoughlin {
14579f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
14589f4d26d0SMark McLoughlin 
14593b07e9caSTejun Heo 	schedule_work(&vi->config_work);
14609f4d26d0SMark McLoughlin }
14619f4d26d0SMark McLoughlin 
1462986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1463986a4f4dSJason Wang {
1464d4fb84eeSAndrey Vagin 	int i;
1465d4fb84eeSAndrey Vagin 
1466ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1467ab3971b1SJason Wang 		napi_hash_del(&vi->rq[i].napi);
1468d4fb84eeSAndrey Vagin 		netif_napi_del(&vi->rq[i].napi);
1469ab3971b1SJason Wang 	}
1470d4fb84eeSAndrey Vagin 
1471986a4f4dSJason Wang 	kfree(vi->rq);
1472986a4f4dSJason Wang 	kfree(vi->sq);
1473986a4f4dSJason Wang }
1474986a4f4dSJason Wang 
1475986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi)
1476986a4f4dSJason Wang {
1477986a4f4dSJason Wang 	int i;
1478986a4f4dSJason Wang 
1479986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1480986a4f4dSJason Wang 		while (vi->rq[i].pages)
1481986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1482986a4f4dSJason Wang 	}
1483986a4f4dSJason Wang }
1484986a4f4dSJason Wang 
1485fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
1486fb51879dSMichael Dalton {
1487fb51879dSMichael Dalton 	int i;
1488fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
1489fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
1490fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
1491fb51879dSMichael Dalton }
1492fb51879dSMichael Dalton 
1493986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1494986a4f4dSJason Wang {
1495986a4f4dSJason Wang 	void *buf;
1496986a4f4dSJason Wang 	int i;
1497986a4f4dSJason Wang 
1498986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1499986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
1500986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1501986a4f4dSJason Wang 			dev_kfree_skb(buf);
1502986a4f4dSJason Wang 	}
1503986a4f4dSJason Wang 
1504986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1505986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
1506986a4f4dSJason Wang 
1507986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1508ab7db917SMichael Dalton 			if (vi->mergeable_rx_bufs) {
1509ab7db917SMichael Dalton 				unsigned long ctx = (unsigned long)buf;
1510ab7db917SMichael Dalton 				void *base = mergeable_ctx_to_buf_address(ctx);
1511ab7db917SMichael Dalton 				put_page(virt_to_head_page(base));
1512ab7db917SMichael Dalton 			} else if (vi->big_packets) {
1513fa9fac17SAndrey Vagin 				give_pages(&vi->rq[i], buf);
1514ab7db917SMichael Dalton 			} else {
1515986a4f4dSJason Wang 				dev_kfree_skb(buf);
1516986a4f4dSJason Wang 			}
1517986a4f4dSJason Wang 		}
1518986a4f4dSJason Wang 	}
1519ab7db917SMichael Dalton }
1520986a4f4dSJason Wang 
1521e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
1522e9d7417bSJason Wang {
1523e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
1524e9d7417bSJason Wang 
15258898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
1526986a4f4dSJason Wang 
1527e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
1528986a4f4dSJason Wang 
1529986a4f4dSJason Wang 	virtnet_free_queues(vi);
1530986a4f4dSJason Wang }
1531986a4f4dSJason Wang 
1532986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
1533986a4f4dSJason Wang {
1534986a4f4dSJason Wang 	vq_callback_t **callbacks;
1535986a4f4dSJason Wang 	struct virtqueue **vqs;
1536986a4f4dSJason Wang 	int ret = -ENOMEM;
1537986a4f4dSJason Wang 	int i, total_vqs;
1538986a4f4dSJason Wang 	const char **names;
1539986a4f4dSJason Wang 
1540986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1541986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1542986a4f4dSJason Wang 	 * possible control vq.
1543986a4f4dSJason Wang 	 */
1544986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
1545986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1546986a4f4dSJason Wang 
1547986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
1548986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1549986a4f4dSJason Wang 	if (!vqs)
1550986a4f4dSJason Wang 		goto err_vq;
1551986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1552986a4f4dSJason Wang 	if (!callbacks)
1553986a4f4dSJason Wang 		goto err_callback;
1554986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1555986a4f4dSJason Wang 	if (!names)
1556986a4f4dSJason Wang 		goto err_names;
1557986a4f4dSJason Wang 
1558986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
1559986a4f4dSJason Wang 	if (vi->has_cvq) {
1560986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
1561986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
1562986a4f4dSJason Wang 	}
1563986a4f4dSJason Wang 
1564986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
1565986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1566986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
1567986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
1568986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
1569986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
1570986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
1571986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
1572986a4f4dSJason Wang 	}
1573986a4f4dSJason Wang 
1574986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1575986a4f4dSJason Wang 					 names);
1576986a4f4dSJason Wang 	if (ret)
1577986a4f4dSJason Wang 		goto err_find;
1578986a4f4dSJason Wang 
1579986a4f4dSJason Wang 	if (vi->has_cvq) {
1580986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
1581986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1582f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1583986a4f4dSJason Wang 	}
1584986a4f4dSJason Wang 
1585986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1586986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
1587986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
1588986a4f4dSJason Wang 	}
1589986a4f4dSJason Wang 
1590986a4f4dSJason Wang 	kfree(names);
1591986a4f4dSJason Wang 	kfree(callbacks);
1592986a4f4dSJason Wang 	kfree(vqs);
1593986a4f4dSJason Wang 
1594986a4f4dSJason Wang 	return 0;
1595986a4f4dSJason Wang 
1596986a4f4dSJason Wang err_find:
1597986a4f4dSJason Wang 	kfree(names);
1598986a4f4dSJason Wang err_names:
1599986a4f4dSJason Wang 	kfree(callbacks);
1600986a4f4dSJason Wang err_callback:
1601986a4f4dSJason Wang 	kfree(vqs);
1602986a4f4dSJason Wang err_vq:
1603986a4f4dSJason Wang 	return ret;
1604986a4f4dSJason Wang }
1605986a4f4dSJason Wang 
1606986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
1607986a4f4dSJason Wang {
1608986a4f4dSJason Wang 	int i;
1609986a4f4dSJason Wang 
1610986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1611986a4f4dSJason Wang 	if (!vi->sq)
1612986a4f4dSJason Wang 		goto err_sq;
1613986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1614008d4278SAmerigo Wang 	if (!vi->rq)
1615986a4f4dSJason Wang 		goto err_rq;
1616986a4f4dSJason Wang 
1617986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
1618986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1619986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
1620986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1621986a4f4dSJason Wang 			       napi_weight);
1622986a4f4dSJason Wang 
1623986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
16245377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
1625986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1626986a4f4dSJason Wang 	}
1627986a4f4dSJason Wang 
1628986a4f4dSJason Wang 	return 0;
1629986a4f4dSJason Wang 
1630986a4f4dSJason Wang err_rq:
1631986a4f4dSJason Wang 	kfree(vi->sq);
1632986a4f4dSJason Wang err_sq:
1633986a4f4dSJason Wang 	return -ENOMEM;
1634e9d7417bSJason Wang }
1635e9d7417bSJason Wang 
16363f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
16373f9c10b0SAmit Shah {
1638986a4f4dSJason Wang 	int ret;
16393f9c10b0SAmit Shah 
1640986a4f4dSJason Wang 	/* Allocate send & receive queues */
1641986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
1642986a4f4dSJason Wang 	if (ret)
1643986a4f4dSJason Wang 		goto err;
16443f9c10b0SAmit Shah 
1645986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
1646986a4f4dSJason Wang 	if (ret)
1647986a4f4dSJason Wang 		goto err_free;
16483f9c10b0SAmit Shah 
164947be2479SWanlong Gao 	get_online_cpus();
16508898c21cSWanlong Gao 	virtnet_set_affinity(vi);
165147be2479SWanlong Gao 	put_online_cpus();
165247be2479SWanlong Gao 
16533f9c10b0SAmit Shah 	return 0;
1654986a4f4dSJason Wang 
1655986a4f4dSJason Wang err_free:
1656986a4f4dSJason Wang 	virtnet_free_queues(vi);
1657986a4f4dSJason Wang err:
1658986a4f4dSJason Wang 	return ret;
16593f9c10b0SAmit Shah }
16603f9c10b0SAmit Shah 
1661fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
1662fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
1663fbf28d78SMichael Dalton 		struct rx_queue_attribute *attribute, char *buf)
1664fbf28d78SMichael Dalton {
1665fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
1666fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
16675377d758SJohannes Berg 	struct ewma_pkt_len *avg;
1668fbf28d78SMichael Dalton 
1669fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
1670fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
1671fbf28d78SMichael Dalton 	return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
1672fbf28d78SMichael Dalton }
1673fbf28d78SMichael Dalton 
1674fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
1675fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
1676fbf28d78SMichael Dalton 
1677fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
1678fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
1679fbf28d78SMichael Dalton 	NULL
1680fbf28d78SMichael Dalton };
1681fbf28d78SMichael Dalton 
1682fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
1683fbf28d78SMichael Dalton 	.name = "virtio_net",
1684fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
1685fbf28d78SMichael Dalton };
1686fbf28d78SMichael Dalton #endif
1687fbf28d78SMichael Dalton 
1688892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
1689892d6eb1SJason Wang 				    unsigned int fbit,
1690892d6eb1SJason Wang 				    const char *fname, const char *dname)
1691892d6eb1SJason Wang {
1692892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
1693892d6eb1SJason Wang 		return false;
1694892d6eb1SJason Wang 
1695892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
1696892d6eb1SJason Wang 		fname, dname);
1697892d6eb1SJason Wang 
1698892d6eb1SJason Wang 	return true;
1699892d6eb1SJason Wang }
1700892d6eb1SJason Wang 
1701892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
1702892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
1703892d6eb1SJason Wang 
1704892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
1705892d6eb1SJason Wang {
1706892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
1707892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
1708892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
1709892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
1710892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
1711892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
1712892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
1713892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
1714892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
1715892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
1716892d6eb1SJason Wang 		return false;
1717892d6eb1SJason Wang 	}
1718892d6eb1SJason Wang 
1719892d6eb1SJason Wang 	return true;
1720892d6eb1SJason Wang }
1721892d6eb1SJason Wang 
1722296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
1723296f96fcSRusty Russell {
1724986a4f4dSJason Wang 	int i, err;
1725296f96fcSRusty Russell 	struct net_device *dev;
1726296f96fcSRusty Russell 	struct virtnet_info *vi;
1727986a4f4dSJason Wang 	u16 max_queue_pairs;
172814de9d11SAaron Conole 	int mtu;
1729986a4f4dSJason Wang 
17306ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
17316ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
17326ba42248SMichael S. Tsirkin 			__func__);
17336ba42248SMichael S. Tsirkin 		return -EINVAL;
17346ba42248SMichael S. Tsirkin 	}
17356ba42248SMichael S. Tsirkin 
1736892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
1737892d6eb1SJason Wang 		return -EINVAL;
1738892d6eb1SJason Wang 
1739986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
1740855e0c52SRusty Russell 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1741855e0c52SRusty Russell 				   struct virtio_net_config,
1742855e0c52SRusty Russell 				   max_virtqueue_pairs, &max_queue_pairs);
1743986a4f4dSJason Wang 
1744986a4f4dSJason Wang 	/* We need at least 2 queue's */
1745986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1746986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1747986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1748986a4f4dSJason Wang 		max_queue_pairs = 1;
1749296f96fcSRusty Russell 
1750296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
1751986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1752296f96fcSRusty Russell 	if (!dev)
1753296f96fcSRusty Russell 		return -ENOMEM;
1754296f96fcSRusty Russell 
1755296f96fcSRusty Russell 	/* Set up network device as normal. */
1756f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
175776288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
1758296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
17593fa2a1dfSstephen hemminger 
17607ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
1761296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
1762296f96fcSRusty Russell 
1763296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
176498e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1765296f96fcSRusty Russell 		/* This opens up the world of extra features. */
176648900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
176798e778c9SMichał Mirosław 		if (csum)
176848900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
176998e778c9SMichał Mirosław 
177098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1771e3e3c423SVlad Yasevich 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
177234a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
177334a48579SRusty Russell 		}
17745539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
177598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
177698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
177798e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
177898e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
177998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
178098e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
1781e3e3c423SVlad Yasevich 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1782e3e3c423SVlad Yasevich 			dev->hw_features |= NETIF_F_UFO;
178398e778c9SMichał Mirosław 
178441f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
178541f2f127SJason Wang 
178698e778c9SMichał Mirosław 		if (gso)
1787e3e3c423SVlad Yasevich 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
178898e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
1789296f96fcSRusty Russell 	}
17904f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
17914f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
1792296f96fcSRusty Russell 
17934fda8302SJason Wang 	dev->vlan_features = dev->features;
17944fda8302SJason Wang 
1795296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
1796855e0c52SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1797855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
1798a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
1799855e0c52SRusty Russell 				   dev->dev_addr, dev->addr_len);
1800855e0c52SRusty Russell 	else
1801f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1802296f96fcSRusty Russell 
1803296f96fcSRusty Russell 	/* Set up our device-specific information */
1804296f96fcSRusty Russell 	vi = netdev_priv(dev);
1805296f96fcSRusty Russell 	vi->dev = dev;
1806296f96fcSRusty Russell 	vi->vdev = vdev;
1807d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
18083fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
18093fa2a1dfSstephen hemminger 	err = -ENOMEM;
18103fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
18113fa2a1dfSstephen hemminger 		goto free;
18123fa2a1dfSstephen hemminger 
1813827da44cSJohn Stultz 	for_each_possible_cpu(i) {
1814827da44cSJohn Stultz 		struct virtnet_stats *virtnet_stats;
1815827da44cSJohn Stultz 		virtnet_stats = per_cpu_ptr(vi->stats, i);
1816827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->tx_syncp);
1817827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->rx_syncp);
1818827da44cSJohn Stultz 	}
1819827da44cSJohn Stultz 
1820586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1821296f96fcSRusty Russell 
182297402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
18238e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
18248e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1825e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
1826e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
182797402b96SHerbert Xu 		vi->big_packets = true;
182897402b96SHerbert Xu 
18293f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
18303f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
18313f2c31d9SMark McLoughlin 
1832d04302b3SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
1833d04302b3SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
1834012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1835012873d0SMichael S. Tsirkin 	else
1836012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
1837012873d0SMichael S. Tsirkin 
183875993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
183975993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
1840e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
1841e7428e95SMichael S. Tsirkin 
1842986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1843986a4f4dSJason Wang 		vi->has_cvq = true;
1844986a4f4dSJason Wang 
184514de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
184614de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
184714de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
184814de9d11SAaron Conole 					      mtu));
184914de9d11SAaron Conole 		if (virtnet_change_mtu(dev, mtu))
185014de9d11SAaron Conole 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
185114de9d11SAaron Conole 	}
185214de9d11SAaron Conole 
1853012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
1854012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
18556ebbc1a6SZhangjie \(HZ\) 
1856986a4f4dSJason Wang 	/* Use single tx/rx queue pair as default */
1857986a4f4dSJason Wang 	vi->curr_queue_pairs = 1;
1858986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
1859986a4f4dSJason Wang 
1860986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
18613f9c10b0SAmit Shah 	err = init_vqs(vi);
1862d2a7dddaSMichael S. Tsirkin 	if (err)
18639bb8ca86SJason Wang 		goto free_stats;
1864d2a7dddaSMichael S. Tsirkin 
1865fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
1866fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
1867fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
1868fbf28d78SMichael Dalton #endif
18690f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
18700f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
1871986a4f4dSJason Wang 
187216032be5SNikolay Aleksandrov 	virtnet_init_settings(dev);
187316032be5SNikolay Aleksandrov 
1874296f96fcSRusty Russell 	err = register_netdev(dev);
1875296f96fcSRusty Russell 	if (err) {
1876296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1877d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1878296f96fcSRusty Russell 	}
1879b3369c1fSRusty Russell 
18804baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
18814baf1e33SMichael S. Tsirkin 
18828de4b2f3SWanlong Gao 	vi->nb.notifier_call = &virtnet_cpu_callback;
18838de4b2f3SWanlong Gao 	err = register_hotcpu_notifier(&vi->nb);
18848de4b2f3SWanlong Gao 	if (err) {
18858de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
1886f00e35e2Swangyunjian 		goto free_unregister_netdev;
18878de4b2f3SWanlong Gao 	}
18888de4b2f3SWanlong Gao 
1889167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1890167c25e4SJason Wang 	   otherwise get link status from config. */
1891167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1892167c25e4SJason Wang 		netif_carrier_off(dev);
18933b07e9caSTejun Heo 		schedule_work(&vi->config_work);
1894167c25e4SJason Wang 	} else {
1895167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
18964783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1897167c25e4SJason Wang 	}
18989f4d26d0SMark McLoughlin 
1899986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1900986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
1901986a4f4dSJason Wang 
1902296f96fcSRusty Russell 	return 0;
1903296f96fcSRusty Russell 
1904f00e35e2Swangyunjian free_unregister_netdev:
190502465555SMichael S. Tsirkin 	vi->vdev->config->reset(vdev);
190602465555SMichael S. Tsirkin 
1907b3369c1fSRusty Russell 	unregister_netdev(dev);
1908d2a7dddaSMichael S. Tsirkin free_vqs:
1909986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
1910fb51879dSMichael Dalton 	free_receive_page_frags(vi);
1911e9d7417bSJason Wang 	virtnet_del_vqs(vi);
19123fa2a1dfSstephen hemminger free_stats:
19133fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1914296f96fcSRusty Russell free:
1915296f96fcSRusty Russell 	free_netdev(dev);
1916296f96fcSRusty Russell 	return err;
1917296f96fcSRusty Russell }
1918296f96fcSRusty Russell 
191904486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
1920296f96fcSRusty Russell {
192104486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
1922830a8a97SShirley Ma 
1923830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
19249ab86bbcSShirley Ma 	free_unused_bufs(vi);
1925fb6813f4SRusty Russell 
1926986a4f4dSJason Wang 	free_receive_bufs(vi);
1927d2a7dddaSMichael S. Tsirkin 
1928fb51879dSMichael Dalton 	free_receive_page_frags(vi);
1929fb51879dSMichael Dalton 
1930986a4f4dSJason Wang 	virtnet_del_vqs(vi);
193104486ed0SAmit Shah }
193204486ed0SAmit Shah 
19338cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
193404486ed0SAmit Shah {
193504486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
193604486ed0SAmit Shah 
19378de4b2f3SWanlong Gao 	unregister_hotcpu_notifier(&vi->nb);
19388de4b2f3SWanlong Gao 
1939102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
1940102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
1941586d17c5SJason Wang 
194204486ed0SAmit Shah 	unregister_netdev(vi->dev);
194304486ed0SAmit Shah 
194404486ed0SAmit Shah 	remove_vq_common(vi);
1945fb6813f4SRusty Russell 
19462e66f55bSKrishna Kumar 	free_percpu(vi->stats);
194774b2553fSRusty Russell 	free_netdev(vi->dev);
1948296f96fcSRusty Russell }
1949296f96fcSRusty Russell 
195089107000SAaron Lu #ifdef CONFIG_PM_SLEEP
19510741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
19520741bcb5SAmit Shah {
19530741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1954986a4f4dSJason Wang 	int i;
19550741bcb5SAmit Shah 
1956ec9debbdSJason Wang 	unregister_hotcpu_notifier(&vi->nb);
1957ec9debbdSJason Wang 
1958102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device */
1959102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
1960586d17c5SJason Wang 
19610741bcb5SAmit Shah 	netif_device_detach(vi->dev);
19620741bcb5SAmit Shah 	cancel_delayed_work_sync(&vi->refill);
19630741bcb5SAmit Shah 
196491815639SJason Wang 	if (netif_running(vi->dev)) {
1965ab3971b1SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
1966986a4f4dSJason Wang 			napi_disable(&vi->rq[i].napi);
196791815639SJason Wang 	}
19680741bcb5SAmit Shah 
19690741bcb5SAmit Shah 	remove_vq_common(vi);
19700741bcb5SAmit Shah 
19710741bcb5SAmit Shah 	return 0;
19720741bcb5SAmit Shah }
19730741bcb5SAmit Shah 
19740741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
19750741bcb5SAmit Shah {
19760741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1977986a4f4dSJason Wang 	int err, i;
19780741bcb5SAmit Shah 
19790741bcb5SAmit Shah 	err = init_vqs(vi);
19800741bcb5SAmit Shah 	if (err)
19810741bcb5SAmit Shah 		return err;
19820741bcb5SAmit Shah 
1983e53fbd11SMichael S. Tsirkin 	virtio_device_ready(vdev);
1984e53fbd11SMichael S. Tsirkin 
19856cd4ce00SJason Wang 	if (netif_running(vi->dev)) {
198655257d72SSasha Levin 		for (i = 0; i < vi->curr_queue_pairs; i++)
1987946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
19883b07e9caSTejun Heo 				schedule_delayed_work(&vi->refill, 0);
19890741bcb5SAmit Shah 
19906cd4ce00SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
19916cd4ce00SJason Wang 			virtnet_napi_enable(&vi->rq[i]);
19926cd4ce00SJason Wang 	}
19936cd4ce00SJason Wang 
19946cd4ce00SJason Wang 	netif_device_attach(vi->dev);
19956cd4ce00SJason Wang 
199635ed159bSJason Wang 	rtnl_lock();
1997986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
199835ed159bSJason Wang 	rtnl_unlock();
1999986a4f4dSJason Wang 
2000ec9debbdSJason Wang 	err = register_hotcpu_notifier(&vi->nb);
2001ec9debbdSJason Wang 	if (err)
2002ec9debbdSJason Wang 		return err;
2003ec9debbdSJason Wang 
20040741bcb5SAmit Shah 	return 0;
20050741bcb5SAmit Shah }
20060741bcb5SAmit Shah #endif
20070741bcb5SAmit Shah 
2008296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
2009296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2010296f96fcSRusty Russell 	{ 0 },
2011296f96fcSRusty Russell };
2012296f96fcSRusty Russell 
2013c45a6816SRusty Russell static unsigned int features[] = {
20145e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
20155e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
2016e3e3c423SVlad Yasevich 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
201797402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
2018e3e3c423SVlad Yasevich 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
20192a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
20200bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
2021986a4f4dSJason Wang 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
20227e58d5aeSAmos Kong 	VIRTIO_NET_F_CTRL_MAC_ADDR,
2023e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
202414de9d11SAaron Conole 	VIRTIO_NET_F_MTU,
2025c45a6816SRusty Russell };
2026c45a6816SRusty Russell 
202722402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
2028c45a6816SRusty Russell 	.feature_table = features,
2029c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
2030296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
2031296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
2032296f96fcSRusty Russell 	.id_table =	id_table,
2033296f96fcSRusty Russell 	.probe =	virtnet_probe,
20348cc085d6SBill Pemberton 	.remove =	virtnet_remove,
20359f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
203689107000SAaron Lu #ifdef CONFIG_PM_SLEEP
20370741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
20380741bcb5SAmit Shah 	.restore =	virtnet_restore,
20390741bcb5SAmit Shah #endif
2040296f96fcSRusty Russell };
2041296f96fcSRusty Russell 
2042b2a17029SRusty Russell module_virtio_driver(virtio_net_driver);
2043296f96fcSRusty Russell 
2044296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
2045296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
2046296f96fcSRusty Russell MODULE_LICENSE("GPL");
2047