xref: /openbmc/linux/drivers/net/virtio_net.c (revision 93a205ee98a4881e8bf608e65562c19d45930a93)
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 
1418017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
1428017c279SSebastian Andrzej Siewior 	struct hlist_node node;
1438017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
1442ac46030SMichael S. Tsirkin 
1452ac46030SMichael S. Tsirkin 	/* Control VQ buffers: protected by the rtnl lock */
1462ac46030SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr ctrl_hdr;
1472ac46030SMichael S. Tsirkin 	virtio_net_ctrl_ack ctrl_status;
148a725ee3eSAndy Lutomirski 	struct virtio_net_ctrl_mq ctrl_mq;
1492ac46030SMichael S. Tsirkin 	u8 ctrl_promisc;
1502ac46030SMichael S. Tsirkin 	u8 ctrl_allmulti;
151a725ee3eSAndy Lutomirski 	u16 ctrl_vid;
15216032be5SNikolay Aleksandrov 
15316032be5SNikolay Aleksandrov 	/* Ethtool settings */
15416032be5SNikolay Aleksandrov 	u8 duplex;
15516032be5SNikolay Aleksandrov 	u32 speed;
156296f96fcSRusty Russell };
157296f96fcSRusty Russell 
1589ab86bbcSShirley Ma struct padded_vnet_hdr {
159012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf hdr;
1609ab86bbcSShirley Ma 	/*
161012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
162012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
163012873d0SMichael S. Tsirkin 	 * after the header.
1649ab86bbcSShirley Ma 	 */
165012873d0SMichael S. Tsirkin 	char padding[4];
1669ab86bbcSShirley Ma };
1679ab86bbcSShirley Ma 
168986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
169986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
170986a4f4dSJason Wang  */
171986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
172986a4f4dSJason Wang {
1739d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
174986a4f4dSJason Wang }
175986a4f4dSJason Wang 
176986a4f4dSJason Wang static int txq2vq(int txq)
177986a4f4dSJason Wang {
178986a4f4dSJason Wang 	return txq * 2 + 1;
179986a4f4dSJason Wang }
180986a4f4dSJason Wang 
181986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
182986a4f4dSJason Wang {
1839d0ca6edSRusty Russell 	return vq->index / 2;
184986a4f4dSJason Wang }
185986a4f4dSJason Wang 
186986a4f4dSJason Wang static int rxq2vq(int rxq)
187986a4f4dSJason Wang {
188986a4f4dSJason Wang 	return rxq * 2;
189986a4f4dSJason Wang }
190986a4f4dSJason Wang 
191012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
192296f96fcSRusty Russell {
193012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
194296f96fcSRusty Russell }
195296f96fcSRusty Russell 
1969ab86bbcSShirley Ma /*
1979ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
1989ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
1999ab86bbcSShirley Ma  */
200e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
201fb6813f4SRusty Russell {
2029ab86bbcSShirley Ma 	struct page *end;
2039ab86bbcSShirley Ma 
204e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
2059ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
206e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
207e9d7417bSJason Wang 	rq->pages = page;
208fb6813f4SRusty Russell }
209fb6813f4SRusty Russell 
210e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
211fb6813f4SRusty Russell {
212e9d7417bSJason Wang 	struct page *p = rq->pages;
213fb6813f4SRusty Russell 
2149ab86bbcSShirley Ma 	if (p) {
215e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2169ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2179ab86bbcSShirley Ma 		p->private = 0;
2189ab86bbcSShirley Ma 	} else
219fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
220fb6813f4SRusty Russell 	return p;
221fb6813f4SRusty Russell }
222fb6813f4SRusty Russell 
223e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
224296f96fcSRusty Russell {
225e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
226296f96fcSRusty Russell 
2272cb9c6baSRusty Russell 	/* Suppress further interrupts. */
228e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
22911a3a154SRusty Russell 
230363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
231986a4f4dSJason Wang 	netif_wake_subqueue(vi->dev, vq2txq(vq));
232296f96fcSRusty Russell }
233296f96fcSRusty Russell 
234ab7db917SMichael Dalton static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx)
235ab7db917SMichael Dalton {
236ab7db917SMichael Dalton 	unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1);
237ab7db917SMichael Dalton 	return (truesize + 1) * MERGEABLE_BUFFER_ALIGN;
238ab7db917SMichael Dalton }
239ab7db917SMichael Dalton 
240ab7db917SMichael Dalton static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx)
241ab7db917SMichael Dalton {
242ab7db917SMichael Dalton 	return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN);
243ab7db917SMichael Dalton 
244ab7db917SMichael Dalton }
245ab7db917SMichael Dalton 
246ab7db917SMichael Dalton static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize)
247ab7db917SMichael Dalton {
248ab7db917SMichael Dalton 	unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN;
249ab7db917SMichael Dalton 	return (unsigned long)buf | (size - 1);
250ab7db917SMichael Dalton }
251ab7db917SMichael Dalton 
2523464645aSMike Waychison /* Called from bottom half context */
253946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
254946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
2552613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
2562613af0eSMichael Dalton 				   unsigned int len, unsigned int truesize)
2579ab86bbcSShirley Ma {
2589ab86bbcSShirley Ma 	struct sk_buff *skb;
259012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
2602613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
2619ab86bbcSShirley Ma 	char *p;
2629ab86bbcSShirley Ma 
2632613af0eSMichael Dalton 	p = page_address(page) + offset;
2649ab86bbcSShirley Ma 
2659ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
266c67f5db8SPaolo Abeni 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
2679ab86bbcSShirley Ma 	if (unlikely(!skb))
2689ab86bbcSShirley Ma 		return NULL;
2699ab86bbcSShirley Ma 
2709ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
2719ab86bbcSShirley Ma 
272012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
273012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
274012873d0SMichael S. Tsirkin 		hdr_padded_len = sizeof *hdr;
275012873d0SMichael S. Tsirkin 	else
2762613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
2773f2c31d9SMark McLoughlin 
2789ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
2793f2c31d9SMark McLoughlin 
2809ab86bbcSShirley Ma 	len -= hdr_len;
2812613af0eSMichael Dalton 	offset += hdr_padded_len;
2822613af0eSMichael Dalton 	p += hdr_padded_len;
2833f2c31d9SMark McLoughlin 
2843f2c31d9SMark McLoughlin 	copy = len;
2853f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
2863f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
2873f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
2883f2c31d9SMark McLoughlin 
2893f2c31d9SMark McLoughlin 	len -= copy;
2909ab86bbcSShirley Ma 	offset += copy;
2913f2c31d9SMark McLoughlin 
2922613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
2932613af0eSMichael Dalton 		if (len)
2942613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
2952613af0eSMichael Dalton 		else
2962613af0eSMichael Dalton 			put_page(page);
2972613af0eSMichael Dalton 		return skb;
2982613af0eSMichael Dalton 	}
2992613af0eSMichael Dalton 
300e878d78bSSasha Levin 	/*
301e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
302e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
303e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
304e878d78bSSasha Levin 	 * the case of a broken device.
305e878d78bSSasha Levin 	 */
306e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
307be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
308e878d78bSSasha Levin 		dev_kfree_skb(skb);
309e878d78bSSasha Levin 		return NULL;
310e878d78bSSasha Levin 	}
3112613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
3129ab86bbcSShirley Ma 	while (len) {
3132613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
3142613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
3152613af0eSMichael Dalton 				frag_size, truesize);
3162613af0eSMichael Dalton 		len -= frag_size;
3179ab86bbcSShirley Ma 		page = (struct page *)page->private;
3189ab86bbcSShirley Ma 		offset = 0;
3193f2c31d9SMark McLoughlin 	}
3203f2c31d9SMark McLoughlin 
3219ab86bbcSShirley Ma 	if (page)
322e9d7417bSJason Wang 		give_pages(rq, page);
3233f2c31d9SMark McLoughlin 
3249ab86bbcSShirley Ma 	return skb;
3259ab86bbcSShirley Ma }
3269ab86bbcSShirley Ma 
327012873d0SMichael S. Tsirkin static struct sk_buff *receive_small(struct virtnet_info *vi, void *buf, unsigned int len)
328f121159dSMichael S. Tsirkin {
329f121159dSMichael S. Tsirkin 	struct sk_buff * skb = buf;
330f121159dSMichael S. Tsirkin 
331012873d0SMichael S. Tsirkin 	len -= vi->hdr_len;
332f121159dSMichael S. Tsirkin 	skb_trim(skb, len);
333f121159dSMichael S. Tsirkin 
334f121159dSMichael S. Tsirkin 	return skb;
335f121159dSMichael S. Tsirkin }
336f121159dSMichael S. Tsirkin 
337f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev,
338946fa564SMichael S. Tsirkin 				   struct virtnet_info *vi,
339f121159dSMichael S. Tsirkin 				   struct receive_queue *rq,
340f121159dSMichael S. Tsirkin 				   void *buf,
341f121159dSMichael S. Tsirkin 				   unsigned int len)
342f121159dSMichael S. Tsirkin {
343f121159dSMichael S. Tsirkin 	struct page *page = buf;
344946fa564SMichael S. Tsirkin 	struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
345f121159dSMichael S. Tsirkin 
346f121159dSMichael S. Tsirkin 	if (unlikely(!skb))
347f121159dSMichael S. Tsirkin 		goto err;
348f121159dSMichael S. Tsirkin 
349f121159dSMichael S. Tsirkin 	return skb;
350f121159dSMichael S. Tsirkin 
351f121159dSMichael S. Tsirkin err:
352f121159dSMichael S. Tsirkin 	dev->stats.rx_dropped++;
353f121159dSMichael S. Tsirkin 	give_pages(rq, page);
354f121159dSMichael S. Tsirkin 	return NULL;
355f121159dSMichael S. Tsirkin }
356f121159dSMichael S. Tsirkin 
3578fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
358fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
3598fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
360ab7db917SMichael Dalton 					 unsigned long ctx,
3618fc3b9e9SMichael S. Tsirkin 					 unsigned int len)
3629ab86bbcSShirley Ma {
363ab7db917SMichael Dalton 	void *buf = mergeable_ctx_to_buf_address(ctx);
364012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
365012873d0SMichael S. Tsirkin 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
3668fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
3678fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
368ab7db917SMichael Dalton 	unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
369ab7db917SMichael Dalton 
370946fa564SMichael S. Tsirkin 	struct sk_buff *head_skb = page_to_skb(vi, rq, page, offset, len,
371946fa564SMichael S. Tsirkin 					       truesize);
3722613af0eSMichael Dalton 	struct sk_buff *curr_skb = head_skb;
3739ab86bbcSShirley Ma 
3748fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
3758fc3b9e9SMichael S. Tsirkin 		goto err_skb;
3769ab86bbcSShirley Ma 	while (--num_buf) {
3778fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
3788fc3b9e9SMichael S. Tsirkin 
379ab7db917SMichael Dalton 		ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
380ab7db917SMichael Dalton 		if (unlikely(!ctx)) {
3818fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
382fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
383012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
384012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
3858fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
3868fc3b9e9SMichael S. Tsirkin 			goto err_buf;
3873f2c31d9SMark McLoughlin 		}
3888fc3b9e9SMichael S. Tsirkin 
389ab7db917SMichael Dalton 		buf = mergeable_ctx_to_buf_address(ctx);
3908fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
3918fc3b9e9SMichael S. Tsirkin 
3928fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
3932613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
3942613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
3958fc3b9e9SMichael S. Tsirkin 
3968fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
3978fc3b9e9SMichael S. Tsirkin 				goto err_skb;
3982613af0eSMichael Dalton 			if (curr_skb == head_skb)
3992613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
4002613af0eSMichael Dalton 			else
4012613af0eSMichael Dalton 				curr_skb->next = nskb;
4022613af0eSMichael Dalton 			curr_skb = nskb;
4032613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
4042613af0eSMichael Dalton 			num_skb_frags = 0;
4052613af0eSMichael Dalton 		}
406ab7db917SMichael Dalton 		truesize = max(len, mergeable_ctx_to_buf_truesize(ctx));
4072613af0eSMichael Dalton 		if (curr_skb != head_skb) {
4082613af0eSMichael Dalton 			head_skb->data_len += len;
4092613af0eSMichael Dalton 			head_skb->len += len;
410fb51879dSMichael Dalton 			head_skb->truesize += truesize;
4112613af0eSMichael Dalton 		}
4128fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
413ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
414ba275241SJason Wang 			put_page(page);
415ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
416fb51879dSMichael Dalton 					     len, truesize);
417ba275241SJason Wang 		} else {
4182613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
419fb51879dSMichael Dalton 					offset, len, truesize);
420ba275241SJason Wang 		}
4218fc3b9e9SMichael S. Tsirkin 	}
4228fc3b9e9SMichael S. Tsirkin 
4235377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
4248fc3b9e9SMichael S. Tsirkin 	return head_skb;
4258fc3b9e9SMichael S. Tsirkin 
4268fc3b9e9SMichael S. Tsirkin err_skb:
4278fc3b9e9SMichael S. Tsirkin 	put_page(page);
4288fc3b9e9SMichael S. Tsirkin 	while (--num_buf) {
429ab7db917SMichael Dalton 		ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len);
430ab7db917SMichael Dalton 		if (unlikely(!ctx)) {
4318fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
4328fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
4338fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
4348fc3b9e9SMichael S. Tsirkin 			break;
4358fc3b9e9SMichael S. Tsirkin 		}
436ab7db917SMichael Dalton 		page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx));
4378fc3b9e9SMichael S. Tsirkin 		put_page(page);
4383f2c31d9SMark McLoughlin 	}
4398fc3b9e9SMichael S. Tsirkin err_buf:
4408fc3b9e9SMichael S. Tsirkin 	dev->stats.rx_dropped++;
4418fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
4428fc3b9e9SMichael S. Tsirkin 	return NULL;
4439ab86bbcSShirley Ma }
4449ab86bbcSShirley Ma 
445946fa564SMichael S. Tsirkin static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
446946fa564SMichael S. Tsirkin 			void *buf, unsigned int len)
4479ab86bbcSShirley Ma {
448e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
44958472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
4509ab86bbcSShirley Ma 	struct sk_buff *skb;
451012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
4529ab86bbcSShirley Ma 
453bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
4549ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
4559ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
456ab7db917SMichael Dalton 		if (vi->mergeable_rx_bufs) {
457ab7db917SMichael Dalton 			unsigned long ctx = (unsigned long)buf;
458ab7db917SMichael Dalton 			void *base = mergeable_ctx_to_buf_address(ctx);
459ab7db917SMichael Dalton 			put_page(virt_to_head_page(base));
460ab7db917SMichael Dalton 		} else if (vi->big_packets) {
46198bfd23cSMichael Dalton 			give_pages(rq, buf);
462ab7db917SMichael Dalton 		} else {
4639ab86bbcSShirley Ma 			dev_kfree_skb(buf);
464ab7db917SMichael Dalton 		}
4659ab86bbcSShirley Ma 		return;
4669ab86bbcSShirley Ma 	}
4679ab86bbcSShirley Ma 
468f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
469fdd819b2SMichael S. Tsirkin 		skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len);
470f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
471946fa564SMichael S. Tsirkin 		skb = receive_big(dev, vi, rq, buf, len);
472f121159dSMichael S. Tsirkin 	else
473012873d0SMichael S. Tsirkin 		skb = receive_small(vi, buf, len);
474f121159dSMichael S. Tsirkin 
4758fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
4762613af0eSMichael Dalton 		return;
4773f2c31d9SMark McLoughlin 
4789ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
4793fa2a1dfSstephen hemminger 
48083a27052SEric Dumazet 	u64_stats_update_begin(&stats->rx_syncp);
4813fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
4823fa2a1dfSstephen hemminger 	stats->rx_packets++;
48383a27052SEric Dumazet 	u64_stats_update_end(&stats->rx_syncp);
484296f96fcSRusty Russell 
485e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
48610a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
487296f96fcSRusty Russell 
488e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
489e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
490e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
491e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
492fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
493296f96fcSRusty Russell 		goto frame_err;
494296f96fcSRusty Russell 	}
495296f96fcSRusty Russell 
496d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
497d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
498d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
499d1dc06dcSMike Rapoport 
5000fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
501296f96fcSRusty Russell 	return;
502296f96fcSRusty Russell 
503296f96fcSRusty Russell frame_err:
504296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
505296f96fcSRusty Russell 	dev_kfree_skb(skb);
506296f96fcSRusty Russell }
507296f96fcSRusty Russell 
508946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
509946fa564SMichael S. Tsirkin 			     gfp_t gfp)
510296f96fcSRusty Russell {
511296f96fcSRusty Russell 	struct sk_buff *skb;
512012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
5139ab86bbcSShirley Ma 	int err;
5143f2c31d9SMark McLoughlin 
5155061de36SMichael Dalton 	skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
5169ab86bbcSShirley Ma 	if (unlikely(!skb))
5179ab86bbcSShirley Ma 		return -ENOMEM;
518296f96fcSRusty Russell 
5195061de36SMichael Dalton 	skb_put(skb, GOOD_PACKET_LEN);
5203f2c31d9SMark McLoughlin 
5213f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
522547c890cSJason Wang 	sg_init_table(rq->sg, 2);
523012873d0SMichael S. Tsirkin 	sg_set_buf(rq->sg, hdr, vi->hdr_len);
524e9d7417bSJason Wang 	skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
52597402b96SHerbert Xu 
5269dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
5279ab86bbcSShirley Ma 	if (err < 0)
5289ab86bbcSShirley Ma 		dev_kfree_skb(skb);
52997402b96SHerbert Xu 
5309ab86bbcSShirley Ma 	return err;
53197402b96SHerbert Xu }
53297402b96SHerbert Xu 
533012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
534012873d0SMichael S. Tsirkin 			   gfp_t gfp)
5359ab86bbcSShirley Ma {
5369ab86bbcSShirley Ma 	struct page *first, *list = NULL;
5379ab86bbcSShirley Ma 	char *p;
5389ab86bbcSShirley Ma 	int i, err, offset;
539296f96fcSRusty Russell 
540a5835440SRusty Russell 	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
541a5835440SRusty Russell 
542e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
5439ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
544e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
5459ab86bbcSShirley Ma 		if (!first) {
5469ab86bbcSShirley Ma 			if (list)
547e9d7417bSJason Wang 				give_pages(rq, list);
5489ab86bbcSShirley Ma 			return -ENOMEM;
549296f96fcSRusty Russell 		}
550e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
5519ab86bbcSShirley Ma 
5529ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
5539ab86bbcSShirley Ma 		first->private = (unsigned long)list;
5549ab86bbcSShirley Ma 		list = first;
5559ab86bbcSShirley Ma 	}
5569ab86bbcSShirley Ma 
557e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
5589ab86bbcSShirley Ma 	if (!first) {
559e9d7417bSJason Wang 		give_pages(rq, list);
5609ab86bbcSShirley Ma 		return -ENOMEM;
5619ab86bbcSShirley Ma 	}
5629ab86bbcSShirley Ma 	p = page_address(first);
5639ab86bbcSShirley Ma 
564e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
565012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
566012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
5679ab86bbcSShirley Ma 
568e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
5699ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
570e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
5719ab86bbcSShirley Ma 
5729ab86bbcSShirley Ma 	/* chain first in list head */
5739ab86bbcSShirley Ma 	first->private = (unsigned long)list;
5749dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
575aa989f5eSMichael S. Tsirkin 				  first, gfp);
5769ab86bbcSShirley Ma 	if (err < 0)
577e9d7417bSJason Wang 		give_pages(rq, first);
5789ab86bbcSShirley Ma 
5799ab86bbcSShirley Ma 	return err;
5809ab86bbcSShirley Ma }
5819ab86bbcSShirley Ma 
5825377d758SJohannes Berg static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len)
5839ab86bbcSShirley Ma {
584ab7db917SMichael Dalton 	const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
585fbf28d78SMichael Dalton 	unsigned int len;
586fbf28d78SMichael Dalton 
5875377d758SJohannes Berg 	len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
588fbf28d78SMichael Dalton 			GOOD_PACKET_LEN, PAGE_SIZE - hdr_len);
589fbf28d78SMichael Dalton 	return ALIGN(len, MERGEABLE_BUFFER_ALIGN);
590fbf28d78SMichael Dalton }
591fbf28d78SMichael Dalton 
592fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
593fbf28d78SMichael Dalton {
594fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
595fb51879dSMichael Dalton 	char *buf;
596ab7db917SMichael Dalton 	unsigned long ctx;
5979ab86bbcSShirley Ma 	int err;
598fb51879dSMichael Dalton 	unsigned int len, hole;
5999ab86bbcSShirley Ma 
600fbf28d78SMichael Dalton 	len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len);
601ab7db917SMichael Dalton 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
6029ab86bbcSShirley Ma 		return -ENOMEM;
603ab7db917SMichael Dalton 
604fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
605ab7db917SMichael Dalton 	ctx = mergeable_buf_to_ctx(buf, len);
606fb51879dSMichael Dalton 	get_page(alloc_frag->page);
607fb51879dSMichael Dalton 	alloc_frag->offset += len;
608fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
609ab7db917SMichael Dalton 	if (hole < len) {
610ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
611ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
612ab7db917SMichael Dalton 		 * the current buffer. This extra space is not included in
613ab7db917SMichael Dalton 		 * the truesize stored in ctx.
614ab7db917SMichael Dalton 		 */
615fb51879dSMichael Dalton 		len += hole;
616fb51879dSMichael Dalton 		alloc_frag->offset += hole;
617fb51879dSMichael Dalton 	}
6189ab86bbcSShirley Ma 
619fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
620ab7db917SMichael Dalton 	err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp);
6219ab86bbcSShirley Ma 	if (err < 0)
6222613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
6239ab86bbcSShirley Ma 
6249ab86bbcSShirley Ma 	return err;
625296f96fcSRusty Russell }
626296f96fcSRusty Russell 
627b2baed69SRusty Russell /*
628b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
629b2baed69SRusty Russell  *
630b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
631b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
632b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
633b2baed69SRusty Russell  */
634946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
635946fa564SMichael S. Tsirkin 			  gfp_t gfp)
6363f2c31d9SMark McLoughlin {
6373f2c31d9SMark McLoughlin 	int err;
6381788f495SMichael S. Tsirkin 	bool oom;
6393f2c31d9SMark McLoughlin 
640fb51879dSMichael Dalton 	gfp |= __GFP_COLD;
6410aea51c3SAmit Shah 	do {
6429ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
643e9d7417bSJason Wang 			err = add_recvbuf_mergeable(rq, gfp);
6449ab86bbcSShirley Ma 		else if (vi->big_packets)
645012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
6469ab86bbcSShirley Ma 		else
647946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
6483f2c31d9SMark McLoughlin 
6491788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
6509ed4cb07SRusty Russell 		if (err)
6513f2c31d9SMark McLoughlin 			break;
652b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
653681daee2SJason Wang 	virtqueue_kick(rq->vq);
6543161e453SRusty Russell 	return !oom;
6553f2c31d9SMark McLoughlin }
6563f2c31d9SMark McLoughlin 
65718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
658296f96fcSRusty Russell {
659296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
660986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
661e9d7417bSJason Wang 
66218445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
663e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
6641915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
665e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
66618445c4dSRusty Russell 	}
667296f96fcSRusty Russell }
668296f96fcSRusty Russell 
669e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq)
6703e9d08ecSBruce Rogers {
671e9d7417bSJason Wang 	napi_enable(&rq->napi);
6723e9d08ecSBruce Rogers 
6733e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
6743e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
6753e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
6763e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
677e9d7417bSJason Wang 	if (napi_schedule_prep(&rq->napi)) {
678e9d7417bSJason Wang 		virtqueue_disable_cb(rq->vq);
679ec13ee80SMichael S. Tsirkin 		local_bh_disable();
680e9d7417bSJason Wang 		__napi_schedule(&rq->napi);
681ec13ee80SMichael S. Tsirkin 		local_bh_enable();
6823e9d08ecSBruce Rogers 	}
6833e9d08ecSBruce Rogers }
6843e9d08ecSBruce Rogers 
6853161e453SRusty Russell static void refill_work(struct work_struct *work)
6863161e453SRusty Russell {
687e9d7417bSJason Wang 	struct virtnet_info *vi =
688e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
6893161e453SRusty Russell 	bool still_empty;
690986a4f4dSJason Wang 	int i;
6913161e453SRusty Russell 
69255257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
693986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
694986a4f4dSJason Wang 
695986a4f4dSJason Wang 		napi_disable(&rq->napi);
696946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
697986a4f4dSJason Wang 		virtnet_napi_enable(rq);
6983161e453SRusty Russell 
6993161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
700986a4f4dSJason Wang 		 * we will *never* try to fill again.
701986a4f4dSJason Wang 		 */
7023161e453SRusty Russell 		if (still_empty)
7033b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
7043161e453SRusty Russell 	}
705986a4f4dSJason Wang }
7063161e453SRusty Russell 
7072ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget)
708296f96fcSRusty Russell {
709e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
7102ffa7598SJason Wang 	unsigned int len, received = 0;
7119ab86bbcSShirley Ma 	void *buf;
712296f96fcSRusty Russell 
713296f96fcSRusty Russell 	while (received < budget &&
714e9d7417bSJason Wang 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
715946fa564SMichael S. Tsirkin 		receive_buf(vi, rq, buf, len);
716296f96fcSRusty Russell 		received++;
717296f96fcSRusty Russell 	}
718296f96fcSRusty Russell 
719be121f46SJason Wang 	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
720946fa564SMichael S. Tsirkin 		if (!try_fill_recv(vi, rq, GFP_ATOMIC))
7213b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
7223161e453SRusty Russell 	}
723296f96fcSRusty Russell 
7242ffa7598SJason Wang 	return received;
7252ffa7598SJason Wang }
7262ffa7598SJason Wang 
7272ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
7282ffa7598SJason Wang {
7292ffa7598SJason Wang 	struct receive_queue *rq =
7302ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
731faadb05fSLi RongQing 	unsigned int r, received;
7322ffa7598SJason Wang 
733faadb05fSLi RongQing 	received = virtnet_receive(rq, budget);
7342ffa7598SJason Wang 
7358329d98eSRusty Russell 	/* Out of packets? */
7368329d98eSRusty Russell 	if (received < budget) {
737cbdadbbfSMichael S. Tsirkin 		r = virtqueue_enable_cb_prepare(rq->vq);
7380fbd050aSEric Dumazet 		napi_complete_done(napi, received);
739cbdadbbfSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(rq->vq, r)) &&
7408e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
741e9d7417bSJason Wang 			virtqueue_disable_cb(rq->vq);
742288379f0SBen Hutchings 			__napi_schedule(napi);
743296f96fcSRusty Russell 		}
7444265f161SChristian Borntraeger 	}
745296f96fcSRusty Russell 
746296f96fcSRusty Russell 	return received;
747296f96fcSRusty Russell }
748296f96fcSRusty Russell 
74991815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL
75091815639SJason Wang /* must be called with local_bh_disable()d */
75191815639SJason Wang static int virtnet_busy_poll(struct napi_struct *napi)
75291815639SJason Wang {
75391815639SJason Wang 	struct receive_queue *rq =
75491815639SJason Wang 		container_of(napi, struct receive_queue, napi);
75591815639SJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
75691815639SJason Wang 	int r, received = 0, budget = 4;
75791815639SJason Wang 
75891815639SJason Wang 	if (!(vi->status & VIRTIO_NET_S_LINK_UP))
75991815639SJason Wang 		return LL_FLUSH_FAILED;
76091815639SJason Wang 
76191815639SJason Wang 	if (!napi_schedule_prep(napi))
76291815639SJason Wang 		return LL_FLUSH_BUSY;
76391815639SJason Wang 
76491815639SJason Wang 	virtqueue_disable_cb(rq->vq);
76591815639SJason Wang 
76691815639SJason Wang again:
76791815639SJason Wang 	received += virtnet_receive(rq, budget);
76891815639SJason Wang 
76991815639SJason Wang 	r = virtqueue_enable_cb_prepare(rq->vq);
77091815639SJason Wang 	clear_bit(NAPI_STATE_SCHED, &napi->state);
77191815639SJason Wang 	if (unlikely(virtqueue_poll(rq->vq, r)) &&
77291815639SJason Wang 	    napi_schedule_prep(napi)) {
77391815639SJason Wang 		virtqueue_disable_cb(rq->vq);
77491815639SJason Wang 		if (received < budget) {
77591815639SJason Wang 			budget -= received;
77691815639SJason Wang 			goto again;
77791815639SJason Wang 		} else {
77891815639SJason Wang 			__napi_schedule(napi);
77991815639SJason Wang 		}
78091815639SJason Wang 	}
78191815639SJason Wang 
78291815639SJason Wang 	return received;
78391815639SJason Wang }
78491815639SJason Wang #endif	/* CONFIG_NET_RX_BUSY_POLL */
78591815639SJason Wang 
786986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
787986a4f4dSJason Wang {
788986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
789986a4f4dSJason Wang 	int i;
790986a4f4dSJason Wang 
791e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
792e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
793986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
794946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
795986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
796986a4f4dSJason Wang 		virtnet_napi_enable(&vi->rq[i]);
797986a4f4dSJason Wang 	}
798986a4f4dSJason Wang 
799986a4f4dSJason Wang 	return 0;
800986a4f4dSJason Wang }
801986a4f4dSJason Wang 
802b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq)
803296f96fcSRusty Russell {
804296f96fcSRusty Russell 	struct sk_buff *skb;
8056ee57bccSMichael S. Tsirkin 	unsigned int len;
806e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
80758472a76SEric Dumazet 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
808296f96fcSRusty Russell 
809e9d7417bSJason Wang 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
810296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
8113fa2a1dfSstephen hemminger 
81283a27052SEric Dumazet 		u64_stats_update_begin(&stats->tx_syncp);
8133fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
8143fa2a1dfSstephen hemminger 		stats->tx_packets++;
81583a27052SEric Dumazet 		u64_stats_update_end(&stats->tx_syncp);
8163fa2a1dfSstephen hemminger 
817ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
818296f96fcSRusty Russell 	}
819296f96fcSRusty Russell }
820296f96fcSRusty Russell 
821e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
822296f96fcSRusty Russell {
823012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
824296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
825e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
8267bedc7dcSMichael S. Tsirkin 	unsigned num_sg;
827012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
828e7428e95SMichael S. Tsirkin 	bool can_push;
829296f96fcSRusty Russell 
830e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
831e7428e95SMichael S. Tsirkin 
832e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
833e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
834e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
835e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
836e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
837e7428e95SMichael S. Tsirkin 	if (can_push)
838012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
839e7428e95SMichael S. Tsirkin 	else
840e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
841296f96fcSRusty Russell 
842e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
843e858fae2SMike Rapoport 				    virtio_is_little_endian(vi->vdev)))
844296f96fcSRusty Russell 		BUG();
845296f96fcSRusty Russell 
846e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
847012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
8483f2c31d9SMark McLoughlin 
849547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
850e7428e95SMichael S. Tsirkin 	if (can_push) {
851e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
852e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
853e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
854e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
855e7428e95SMichael S. Tsirkin 	} else {
856e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
857b7dfde95SLinus Torvalds 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
858e7428e95SMichael S. Tsirkin 	}
8599dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
86011a3a154SRusty Russell }
86111a3a154SRusty Russell 
862424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
86399ffc696SRusty Russell {
86499ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
865986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
866986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
8679ed4cb07SRusty Russell 	int err;
8684b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
8694b7fd2e6SMichael S. Tsirkin 	bool kick = !skb->xmit_more;
8702cb9c6baSRusty Russell 
8712cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
872e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
87399ffc696SRusty Russell 
874074c3582SJacob Keller 	/* timestamp packet in software */
875074c3582SJacob Keller 	skb_tx_timestamp(skb);
876074c3582SJacob Keller 
87703f191baSMichael S. Tsirkin 	/* Try to transmit */
878b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
87999ffc696SRusty Russell 
8809ed4cb07SRusty Russell 	/* This should not happen! */
881681daee2SJason Wang 	if (unlikely(err)) {
88258eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
8832e57b79cSRick Jones 		if (net_ratelimit())
88458eba97dSRusty Russell 			dev_warn(&dev->dev,
885b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
88658eba97dSRusty Russell 		dev->stats.tx_dropped++;
88785e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
88858eba97dSRusty Russell 		return NETDEV_TX_OK;
889296f96fcSRusty Russell 	}
89003f191baSMichael S. Tsirkin 
89148925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
89248925e37SRusty Russell 	skb_orphan(skb);
89348925e37SRusty Russell 	nf_reset(skb);
89448925e37SRusty Russell 
89560302ff6SMichael S. Tsirkin 	/* If running out of space, stop queue to avoid getting packets that we
89660302ff6SMichael S. Tsirkin 	 * are then unable to transmit.
89760302ff6SMichael S. Tsirkin 	 * An alternative would be to force queuing layer to requeue the skb by
89860302ff6SMichael S. Tsirkin 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
89960302ff6SMichael S. Tsirkin 	 * returned in a normal path of operation: it means that driver is not
90060302ff6SMichael S. Tsirkin 	 * maintaining the TX queue stop/start state properly, and causes
90160302ff6SMichael S. Tsirkin 	 * the stack to do a non-trivial amount of useless work.
90260302ff6SMichael S. Tsirkin 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
90360302ff6SMichael S. Tsirkin 	 * early means 16 slots are typically wasted.
904d631b94eSstephen hemminger 	 */
905b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
906986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
907e9d7417bSJason Wang 		if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
90848925e37SRusty Russell 			/* More just got used, free them then recheck. */
909b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
910b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
911986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
912e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
91348925e37SRusty Russell 			}
91448925e37SRusty Russell 		}
91548925e37SRusty Russell 	}
91648925e37SRusty Russell 
9174b7fd2e6SMichael S. Tsirkin 	if (kick || netif_xmit_stopped(txq))
918c223a078SDavid S. Miller 		virtqueue_kick(sq->vq);
9190b725a2cSDavid S. Miller 
9200b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
921c223a078SDavid S. Miller }
922c223a078SDavid S. Miller 
92340cbfc37SAmos Kong /*
92440cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
92540cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
926788a8b6dSstephen hemminger  * never fail unless improperly formatted.
92740cbfc37SAmos Kong  */
92840cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
929d24bae32Sstephen hemminger 				 struct scatterlist *out)
93040cbfc37SAmos Kong {
931f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
932d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
93340cbfc37SAmos Kong 
93440cbfc37SAmos Kong 	/* Caller should know better */
935f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
93640cbfc37SAmos Kong 
9372ac46030SMichael S. Tsirkin 	vi->ctrl_status = ~0;
9382ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.class = class;
9392ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.cmd = cmd;
940f7bc9594SRusty Russell 	/* Add header */
9412ac46030SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
942f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
94340cbfc37SAmos Kong 
944f7bc9594SRusty Russell 	if (out)
945f7bc9594SRusty Russell 		sgs[out_num++] = out;
94640cbfc37SAmos Kong 
947f7bc9594SRusty Russell 	/* Add return status. */
9482ac46030SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
949d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
95040cbfc37SAmos Kong 
951d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
952a7c58146SRusty Russell 	virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
95340cbfc37SAmos Kong 
95467975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
9552ac46030SMichael S. Tsirkin 		return vi->ctrl_status == VIRTIO_NET_OK;
95640cbfc37SAmos Kong 
95740cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
95840cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
95940cbfc37SAmos Kong 	 */
960047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
961047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
96240cbfc37SAmos Kong 		cpu_relax();
96340cbfc37SAmos Kong 
9642ac46030SMichael S. Tsirkin 	return vi->ctrl_status == VIRTIO_NET_OK;
96540cbfc37SAmos Kong }
96640cbfc37SAmos Kong 
9679c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
9689c46f6d4SAlex Williamson {
9699c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
9709c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
971f2f2c8b4SJiri Pirko 	int ret;
9727e58d5aeSAmos Kong 	struct sockaddr *addr = p;
9737e58d5aeSAmos Kong 	struct scatterlist sg;
9749c46f6d4SAlex Williamson 
9757e58d5aeSAmos Kong 	ret = eth_prepare_mac_addr_change(dev, p);
976f2f2c8b4SJiri Pirko 	if (ret)
977f2f2c8b4SJiri Pirko 		return ret;
9789c46f6d4SAlex Williamson 
9797e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
9807e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
9817e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
982d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
9837e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
9847e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
9857e58d5aeSAmos Kong 			return -EINVAL;
9867e58d5aeSAmos Kong 		}
9877e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
9887e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
989855e0c52SRusty Russell 		unsigned int i;
990855e0c52SRusty Russell 
991855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
992855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
993855e0c52SRusty Russell 			virtio_cwrite8(vdev,
994855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
995855e0c52SRusty Russell 				       i, addr->sa_data[i]);
9967e58d5aeSAmos Kong 	}
9977e58d5aeSAmos Kong 
9987e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
9999c46f6d4SAlex Williamson 
10009c46f6d4SAlex Williamson 	return 0;
10019c46f6d4SAlex Williamson }
10029c46f6d4SAlex Williamson 
10033fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
10043fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
10053fa2a1dfSstephen hemminger {
10063fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
10073fa2a1dfSstephen hemminger 	int cpu;
10083fa2a1dfSstephen hemminger 	unsigned int start;
10093fa2a1dfSstephen hemminger 
10103fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
101158472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
10123fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
10133fa2a1dfSstephen hemminger 
10143fa2a1dfSstephen hemminger 		do {
101557a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
10163fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
10173fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
101857a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
101983a27052SEric Dumazet 
102083a27052SEric Dumazet 		do {
102157a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
10223fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
10233fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
102457a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
10253fa2a1dfSstephen hemminger 
10263fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
10273fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
10283fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
10293fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
10303fa2a1dfSstephen hemminger 	}
10313fa2a1dfSstephen hemminger 
10323fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
1033021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
10343fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
10353fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
10363fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
10373fa2a1dfSstephen hemminger 
10383fa2a1dfSstephen hemminger 	return tot;
10393fa2a1dfSstephen hemminger }
10403fa2a1dfSstephen hemminger 
1041da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
1042da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
1043da74e89dSAmit Shah {
1044da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
1045986a4f4dSJason Wang 	int i;
1046da74e89dSAmit Shah 
1047986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
1048986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
1049da74e89dSAmit Shah }
1050da74e89dSAmit Shah #endif
1051da74e89dSAmit Shah 
1052586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
1053586d17c5SJason Wang {
1054586d17c5SJason Wang 	rtnl_lock();
1055586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1056d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1057586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1058586d17c5SJason Wang 	rtnl_unlock();
1059586d17c5SJason Wang }
1060586d17c5SJason Wang 
1061986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1062986a4f4dSJason Wang {
1063986a4f4dSJason Wang 	struct scatterlist sg;
1064986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
1065986a4f4dSJason Wang 
1066986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1067986a4f4dSJason Wang 		return 0;
1068986a4f4dSJason Wang 
1069a725ee3eSAndy Lutomirski 	vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1070a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
1071986a4f4dSJason Wang 
1072986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1073d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1074986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1075986a4f4dSJason Wang 			 queue_pairs);
1076986a4f4dSJason Wang 		return -EINVAL;
107755257d72SSasha Levin 	} else {
1078986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
107935ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
108035ed159bSJason Wang 		if (dev->flags & IFF_UP)
10819b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
108255257d72SSasha Levin 	}
1083986a4f4dSJason Wang 
1084986a4f4dSJason Wang 	return 0;
1085986a4f4dSJason Wang }
1086986a4f4dSJason Wang 
1087296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
1088296f96fcSRusty Russell {
1089296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1090986a4f4dSJason Wang 	int i;
1091296f96fcSRusty Russell 
1092b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1093b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1094986a4f4dSJason Wang 
1095986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
1096986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
1097296f96fcSRusty Russell 
1098296f96fcSRusty Russell 	return 0;
1099296f96fcSRusty Russell }
1100296f96fcSRusty Russell 
11012af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
11022af7698eSAlex Williamson {
11032af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1104f565a7c2SAlex Williamson 	struct scatterlist sg[2];
1105f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1106ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
110732e7bfc4SJiri Pirko 	int uc_count;
11084cd24eafSJiri Pirko 	int mc_count;
1109f565a7c2SAlex Williamson 	void *buf;
1110f565a7c2SAlex Williamson 	int i;
11112af7698eSAlex Williamson 
1112788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
11132af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
11142af7698eSAlex Williamson 		return;
11152af7698eSAlex Williamson 
11162ac46030SMichael S. Tsirkin 	vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
11172ac46030SMichael S. Tsirkin 	vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
11182af7698eSAlex Williamson 
11192ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
11202af7698eSAlex Williamson 
11212af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1122d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
11232af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
11242ac46030SMichael S. Tsirkin 			 vi->ctrl_promisc ? "en" : "dis");
11252af7698eSAlex Williamson 
11262ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
11272af7698eSAlex Williamson 
11282af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1129d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
11302af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
11312ac46030SMichael S. Tsirkin 			 vi->ctrl_allmulti ? "en" : "dis");
1132f565a7c2SAlex Williamson 
113332e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
11344cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1135f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
11364cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1137f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
11384cd24eafSJiri Pirko 	mac_data = buf;
1139e68ed8f0SJoe Perches 	if (!buf)
1140f565a7c2SAlex Williamson 		return;
1141f565a7c2SAlex Williamson 
114223e258e1SAlex Williamson 	sg_init_table(sg, 2);
114323e258e1SAlex Williamson 
1144f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
1145fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1146ccffad25SJiri Pirko 	i = 0;
114732e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1148ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1149f565a7c2SAlex Williamson 
1150f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
115132e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1152f565a7c2SAlex Williamson 
1153f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
115432e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1155f565a7c2SAlex Williamson 
1156fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1157567ec874SJiri Pirko 	i = 0;
115822bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
115922bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1160f565a7c2SAlex Williamson 
1161f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
11624cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1163f565a7c2SAlex Williamson 
1164f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1165d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
116699e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1167f565a7c2SAlex Williamson 
1168f565a7c2SAlex Williamson 	kfree(buf);
11692af7698eSAlex Williamson }
11702af7698eSAlex Williamson 
117180d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
117280d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
11730bde9569SAlex Williamson {
11740bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11750bde9569SAlex Williamson 	struct scatterlist sg;
11760bde9569SAlex Williamson 
1177a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1178a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
11790bde9569SAlex Williamson 
11800bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1181d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
11820bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
11838e586137SJiri Pirko 	return 0;
11840bde9569SAlex Williamson }
11850bde9569SAlex Williamson 
118680d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
118780d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
11880bde9569SAlex Williamson {
11890bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
11900bde9569SAlex Williamson 	struct scatterlist sg;
11910bde9569SAlex Williamson 
1192a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1193a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
11940bde9569SAlex Williamson 
11950bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1196d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
11970bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
11988e586137SJiri Pirko 	return 0;
11990bde9569SAlex Williamson }
12000bde9569SAlex Williamson 
12018898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1202986a4f4dSJason Wang {
1203986a4f4dSJason Wang 	int i;
12048898c21cSWanlong Gao 
12058898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
12068898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
12078898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
12088898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
12098898c21cSWanlong Gao 		}
12108898c21cSWanlong Gao 
12118898c21cSWanlong Gao 		vi->affinity_hint_set = false;
12128898c21cSWanlong Gao 	}
12138898c21cSWanlong Gao }
12148898c21cSWanlong Gao 
12158898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1216986a4f4dSJason Wang {
1217986a4f4dSJason Wang 	int i;
121847be2479SWanlong Gao 	int cpu;
1219986a4f4dSJason Wang 
1220986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1221986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1222986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1223986a4f4dSJason Wang 	 */
12248898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
12258898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
12268898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1227986a4f4dSJason Wang 		return;
1228986a4f4dSJason Wang 	}
1229986a4f4dSJason Wang 
123047be2479SWanlong Gao 	i = 0;
123147be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1232986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1233986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
12349bb8ca86SJason Wang 		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
123547be2479SWanlong Gao 		i++;
1236986a4f4dSJason Wang 	}
1237986a4f4dSJason Wang 
1238986a4f4dSJason Wang 	vi->affinity_hint_set = true;
123947be2479SWanlong Gao }
1240986a4f4dSJason Wang 
12418017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
12428de4b2f3SWanlong Gao {
12438017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
12448017c279SSebastian Andrzej Siewior 						   node);
12458de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
12468017c279SSebastian Andrzej Siewior 	return 0;
12478de4b2f3SWanlong Gao }
12483ab098dfSJason Wang 
12498017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
12508017c279SSebastian Andrzej Siewior {
12518017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
12528017c279SSebastian Andrzej Siewior 						   node_dead);
12538017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
12548017c279SSebastian Andrzej Siewior 	return 0;
12558017c279SSebastian Andrzej Siewior }
12568017c279SSebastian Andrzej Siewior 
12578017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
12588017c279SSebastian Andrzej Siewior {
12598017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
12608017c279SSebastian Andrzej Siewior 						   node);
12618017c279SSebastian Andrzej Siewior 
12628017c279SSebastian Andrzej Siewior 	virtnet_clean_affinity(vi, cpu);
12638017c279SSebastian Andrzej Siewior 	return 0;
12648017c279SSebastian Andrzej Siewior }
12658017c279SSebastian Andrzej Siewior 
12668017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
12678017c279SSebastian Andrzej Siewior 
12688017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
12698017c279SSebastian Andrzej Siewior {
12708017c279SSebastian Andrzej Siewior 	int ret;
12718017c279SSebastian Andrzej Siewior 
12728017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
12738017c279SSebastian Andrzej Siewior 	if (ret)
12748017c279SSebastian Andrzej Siewior 		return ret;
12758017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
12768017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
12778017c279SSebastian Andrzej Siewior 	if (!ret)
12788017c279SSebastian Andrzej Siewior 		return ret;
12798017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
12808017c279SSebastian Andrzej Siewior 	return ret;
12818017c279SSebastian Andrzej Siewior }
12828017c279SSebastian Andrzej Siewior 
12838017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
12848017c279SSebastian Andrzej Siewior {
12858017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
12868017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
12878017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
1288a9ea3fc6SHerbert Xu }
1289a9ea3fc6SHerbert Xu 
12908f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
12918f9f4668SRick Jones 				struct ethtool_ringparam *ring)
12928f9f4668SRick Jones {
12938f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
12948f9f4668SRick Jones 
1295986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1296986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
12978f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
12988f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
12998f9f4668SRick Jones }
13008f9f4668SRick Jones 
130166846048SRick Jones 
130266846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
130366846048SRick Jones 				struct ethtool_drvinfo *info)
130466846048SRick Jones {
130566846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
130666846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
130766846048SRick Jones 
130866846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
130966846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
131066846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
131166846048SRick Jones 
131266846048SRick Jones }
131366846048SRick Jones 
1314d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1315d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1316d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1317d73bcd2cSJason Wang {
1318d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1319d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1320d73bcd2cSJason Wang 	int err;
1321d73bcd2cSJason Wang 
1322d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1323d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1324d73bcd2cSJason Wang 	 */
1325d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1326d73bcd2cSJason Wang 		return -EINVAL;
1327d73bcd2cSJason Wang 
1328c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
1329d73bcd2cSJason Wang 		return -EINVAL;
1330d73bcd2cSJason Wang 
133147be2479SWanlong Gao 	get_online_cpus();
1332d73bcd2cSJason Wang 	err = virtnet_set_queues(vi, queue_pairs);
1333d73bcd2cSJason Wang 	if (!err) {
1334d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1335d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1336d73bcd2cSJason Wang 
13378898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1338d73bcd2cSJason Wang 	}
133947be2479SWanlong Gao 	put_online_cpus();
1340d73bcd2cSJason Wang 
1341d73bcd2cSJason Wang 	return err;
1342d73bcd2cSJason Wang }
1343d73bcd2cSJason Wang 
1344d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1345d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1346d73bcd2cSJason Wang {
1347d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1348d73bcd2cSJason Wang 
1349d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1350d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1351d73bcd2cSJason Wang 	channels->max_other = 0;
1352d73bcd2cSJason Wang 	channels->rx_count = 0;
1353d73bcd2cSJason Wang 	channels->tx_count = 0;
1354d73bcd2cSJason Wang 	channels->other_count = 0;
1355d73bcd2cSJason Wang }
1356d73bcd2cSJason Wang 
135716032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */
135816032be5SNikolay Aleksandrov static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
135916032be5SNikolay Aleksandrov {
136016032be5SNikolay Aleksandrov 	struct ethtool_cmd diff1 = *cmd;
136116032be5SNikolay Aleksandrov 	struct ethtool_cmd diff2 = {};
136216032be5SNikolay Aleksandrov 
13630cf3ace9SNikolay Aleksandrov 	/* cmd is always set so we need to clear it, validate the port type
13640cf3ace9SNikolay Aleksandrov 	 * and also without autonegotiation we can ignore advertising
13650cf3ace9SNikolay Aleksandrov 	 */
136616032be5SNikolay Aleksandrov 	ethtool_cmd_speed_set(&diff1, 0);
13670cf3ace9SNikolay Aleksandrov 	diff2.port = PORT_OTHER;
136816032be5SNikolay Aleksandrov 	diff1.advertising = 0;
136916032be5SNikolay Aleksandrov 	diff1.duplex = 0;
137016032be5SNikolay Aleksandrov 	diff1.cmd = 0;
137116032be5SNikolay Aleksandrov 
137216032be5SNikolay Aleksandrov 	return !memcmp(&diff1, &diff2, sizeof(diff1));
137316032be5SNikolay Aleksandrov }
137416032be5SNikolay Aleksandrov 
137516032be5SNikolay Aleksandrov static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
137616032be5SNikolay Aleksandrov {
137716032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
137816032be5SNikolay Aleksandrov 	u32 speed;
137916032be5SNikolay Aleksandrov 
138016032be5SNikolay Aleksandrov 	speed = ethtool_cmd_speed(cmd);
138116032be5SNikolay Aleksandrov 	/* don't allow custom speed and duplex */
138216032be5SNikolay Aleksandrov 	if (!ethtool_validate_speed(speed) ||
138316032be5SNikolay Aleksandrov 	    !ethtool_validate_duplex(cmd->duplex) ||
138416032be5SNikolay Aleksandrov 	    !virtnet_validate_ethtool_cmd(cmd))
138516032be5SNikolay Aleksandrov 		return -EINVAL;
138616032be5SNikolay Aleksandrov 	vi->speed = speed;
138716032be5SNikolay Aleksandrov 	vi->duplex = cmd->duplex;
138816032be5SNikolay Aleksandrov 
138916032be5SNikolay Aleksandrov 	return 0;
139016032be5SNikolay Aleksandrov }
139116032be5SNikolay Aleksandrov 
139216032be5SNikolay Aleksandrov static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
139316032be5SNikolay Aleksandrov {
139416032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
139516032be5SNikolay Aleksandrov 
139616032be5SNikolay Aleksandrov 	ethtool_cmd_speed_set(cmd, vi->speed);
139716032be5SNikolay Aleksandrov 	cmd->duplex = vi->duplex;
139816032be5SNikolay Aleksandrov 	cmd->port = PORT_OTHER;
139916032be5SNikolay Aleksandrov 
140016032be5SNikolay Aleksandrov 	return 0;
140116032be5SNikolay Aleksandrov }
140216032be5SNikolay Aleksandrov 
140316032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
140416032be5SNikolay Aleksandrov {
140516032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
140616032be5SNikolay Aleksandrov 
140716032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
140816032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
140916032be5SNikolay Aleksandrov }
141016032be5SNikolay Aleksandrov 
14110fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
141266846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
14139f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
14148f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1415d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1416d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1417074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
141816032be5SNikolay Aleksandrov 	.get_settings = virtnet_get_settings,
141916032be5SNikolay Aleksandrov 	.set_settings = virtnet_set_settings,
1420a9ea3fc6SHerbert Xu };
1421a9ea3fc6SHerbert Xu 
142276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
142376288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
142476288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
142576288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
142676288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
14279c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
14282af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
14293fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
14301824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
14311824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
143276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
143376288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
143476288b4eSStephen Hemminger #endif
143591815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL
143691815639SJason Wang 	.ndo_busy_poll		= virtnet_busy_poll,
143791815639SJason Wang #endif
143876288b4eSStephen Hemminger };
143976288b4eSStephen Hemminger 
1440586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
14419f4d26d0SMark McLoughlin {
1442586d17c5SJason Wang 	struct virtnet_info *vi =
1443586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
14449f4d26d0SMark McLoughlin 	u16 v;
14459f4d26d0SMark McLoughlin 
1446855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1447855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
1448507613bfSMichael S. Tsirkin 		return;
1449586d17c5SJason Wang 
1450586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
1451ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
1452586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
1453586d17c5SJason Wang 	}
14549f4d26d0SMark McLoughlin 
14559f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
14569f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
14579f4d26d0SMark McLoughlin 
14589f4d26d0SMark McLoughlin 	if (vi->status == v)
1459507613bfSMichael S. Tsirkin 		return;
14609f4d26d0SMark McLoughlin 
14619f4d26d0SMark McLoughlin 	vi->status = v;
14629f4d26d0SMark McLoughlin 
14639f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
14649f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
1465986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
14669f4d26d0SMark McLoughlin 	} else {
14679f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
1468986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
14699f4d26d0SMark McLoughlin 	}
14709f4d26d0SMark McLoughlin }
14719f4d26d0SMark McLoughlin 
14729f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
14739f4d26d0SMark McLoughlin {
14749f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
14759f4d26d0SMark McLoughlin 
14763b07e9caSTejun Heo 	schedule_work(&vi->config_work);
14779f4d26d0SMark McLoughlin }
14789f4d26d0SMark McLoughlin 
1479986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
1480986a4f4dSJason Wang {
1481d4fb84eeSAndrey Vagin 	int i;
1482d4fb84eeSAndrey Vagin 
1483ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1484ab3971b1SJason Wang 		napi_hash_del(&vi->rq[i].napi);
1485d4fb84eeSAndrey Vagin 		netif_napi_del(&vi->rq[i].napi);
1486ab3971b1SJason Wang 	}
1487d4fb84eeSAndrey Vagin 
1488986a4f4dSJason Wang 	kfree(vi->rq);
1489986a4f4dSJason Wang 	kfree(vi->sq);
1490986a4f4dSJason Wang }
1491986a4f4dSJason Wang 
1492986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi)
1493986a4f4dSJason Wang {
1494986a4f4dSJason Wang 	int i;
1495986a4f4dSJason Wang 
1496986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1497986a4f4dSJason Wang 		while (vi->rq[i].pages)
1498986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1499986a4f4dSJason Wang 	}
1500986a4f4dSJason Wang }
1501986a4f4dSJason Wang 
1502fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
1503fb51879dSMichael Dalton {
1504fb51879dSMichael Dalton 	int i;
1505fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
1506fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
1507fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
1508fb51879dSMichael Dalton }
1509fb51879dSMichael Dalton 
1510986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
1511986a4f4dSJason Wang {
1512986a4f4dSJason Wang 	void *buf;
1513986a4f4dSJason Wang 	int i;
1514986a4f4dSJason Wang 
1515986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1516986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
1517986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1518986a4f4dSJason Wang 			dev_kfree_skb(buf);
1519986a4f4dSJason Wang 	}
1520986a4f4dSJason Wang 
1521986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1522986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
1523986a4f4dSJason Wang 
1524986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1525ab7db917SMichael Dalton 			if (vi->mergeable_rx_bufs) {
1526ab7db917SMichael Dalton 				unsigned long ctx = (unsigned long)buf;
1527ab7db917SMichael Dalton 				void *base = mergeable_ctx_to_buf_address(ctx);
1528ab7db917SMichael Dalton 				put_page(virt_to_head_page(base));
1529ab7db917SMichael Dalton 			} else if (vi->big_packets) {
1530fa9fac17SAndrey Vagin 				give_pages(&vi->rq[i], buf);
1531ab7db917SMichael Dalton 			} else {
1532986a4f4dSJason Wang 				dev_kfree_skb(buf);
1533986a4f4dSJason Wang 			}
1534986a4f4dSJason Wang 		}
1535986a4f4dSJason Wang 	}
1536ab7db917SMichael Dalton }
1537986a4f4dSJason Wang 
1538e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
1539e9d7417bSJason Wang {
1540e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
1541e9d7417bSJason Wang 
15428898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
1543986a4f4dSJason Wang 
1544e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
1545986a4f4dSJason Wang 
1546986a4f4dSJason Wang 	virtnet_free_queues(vi);
1547986a4f4dSJason Wang }
1548986a4f4dSJason Wang 
1549986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
1550986a4f4dSJason Wang {
1551986a4f4dSJason Wang 	vq_callback_t **callbacks;
1552986a4f4dSJason Wang 	struct virtqueue **vqs;
1553986a4f4dSJason Wang 	int ret = -ENOMEM;
1554986a4f4dSJason Wang 	int i, total_vqs;
1555986a4f4dSJason Wang 	const char **names;
1556986a4f4dSJason Wang 
1557986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1558986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1559986a4f4dSJason Wang 	 * possible control vq.
1560986a4f4dSJason Wang 	 */
1561986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
1562986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1563986a4f4dSJason Wang 
1564986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
1565986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1566986a4f4dSJason Wang 	if (!vqs)
1567986a4f4dSJason Wang 		goto err_vq;
1568986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1569986a4f4dSJason Wang 	if (!callbacks)
1570986a4f4dSJason Wang 		goto err_callback;
1571986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1572986a4f4dSJason Wang 	if (!names)
1573986a4f4dSJason Wang 		goto err_names;
1574986a4f4dSJason Wang 
1575986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
1576986a4f4dSJason Wang 	if (vi->has_cvq) {
1577986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
1578986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
1579986a4f4dSJason Wang 	}
1580986a4f4dSJason Wang 
1581986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
1582986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1583986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
1584986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
1585986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
1586986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
1587986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
1588986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
1589986a4f4dSJason Wang 	}
1590986a4f4dSJason Wang 
1591986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1592986a4f4dSJason Wang 					 names);
1593986a4f4dSJason Wang 	if (ret)
1594986a4f4dSJason Wang 		goto err_find;
1595986a4f4dSJason Wang 
1596986a4f4dSJason Wang 	if (vi->has_cvq) {
1597986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
1598986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1599f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1600986a4f4dSJason Wang 	}
1601986a4f4dSJason Wang 
1602986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1603986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
1604986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
1605986a4f4dSJason Wang 	}
1606986a4f4dSJason Wang 
1607986a4f4dSJason Wang 	kfree(names);
1608986a4f4dSJason Wang 	kfree(callbacks);
1609986a4f4dSJason Wang 	kfree(vqs);
1610986a4f4dSJason Wang 
1611986a4f4dSJason Wang 	return 0;
1612986a4f4dSJason Wang 
1613986a4f4dSJason Wang err_find:
1614986a4f4dSJason Wang 	kfree(names);
1615986a4f4dSJason Wang err_names:
1616986a4f4dSJason Wang 	kfree(callbacks);
1617986a4f4dSJason Wang err_callback:
1618986a4f4dSJason Wang 	kfree(vqs);
1619986a4f4dSJason Wang err_vq:
1620986a4f4dSJason Wang 	return ret;
1621986a4f4dSJason Wang }
1622986a4f4dSJason Wang 
1623986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
1624986a4f4dSJason Wang {
1625986a4f4dSJason Wang 	int i;
1626986a4f4dSJason Wang 
1627986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1628986a4f4dSJason Wang 	if (!vi->sq)
1629986a4f4dSJason Wang 		goto err_sq;
1630986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1631008d4278SAmerigo Wang 	if (!vi->rq)
1632986a4f4dSJason Wang 		goto err_rq;
1633986a4f4dSJason Wang 
1634986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
1635986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1636986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
1637986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1638986a4f4dSJason Wang 			       napi_weight);
1639986a4f4dSJason Wang 
1640986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
16415377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
1642986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1643986a4f4dSJason Wang 	}
1644986a4f4dSJason Wang 
1645986a4f4dSJason Wang 	return 0;
1646986a4f4dSJason Wang 
1647986a4f4dSJason Wang err_rq:
1648986a4f4dSJason Wang 	kfree(vi->sq);
1649986a4f4dSJason Wang err_sq:
1650986a4f4dSJason Wang 	return -ENOMEM;
1651e9d7417bSJason Wang }
1652e9d7417bSJason Wang 
16533f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
16543f9c10b0SAmit Shah {
1655986a4f4dSJason Wang 	int ret;
16563f9c10b0SAmit Shah 
1657986a4f4dSJason Wang 	/* Allocate send & receive queues */
1658986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
1659986a4f4dSJason Wang 	if (ret)
1660986a4f4dSJason Wang 		goto err;
16613f9c10b0SAmit Shah 
1662986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
1663986a4f4dSJason Wang 	if (ret)
1664986a4f4dSJason Wang 		goto err_free;
16653f9c10b0SAmit Shah 
166647be2479SWanlong Gao 	get_online_cpus();
16678898c21cSWanlong Gao 	virtnet_set_affinity(vi);
166847be2479SWanlong Gao 	put_online_cpus();
166947be2479SWanlong Gao 
16703f9c10b0SAmit Shah 	return 0;
1671986a4f4dSJason Wang 
1672986a4f4dSJason Wang err_free:
1673986a4f4dSJason Wang 	virtnet_free_queues(vi);
1674986a4f4dSJason Wang err:
1675986a4f4dSJason Wang 	return ret;
16763f9c10b0SAmit Shah }
16773f9c10b0SAmit Shah 
1678fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
1679fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
1680fbf28d78SMichael Dalton 		struct rx_queue_attribute *attribute, char *buf)
1681fbf28d78SMichael Dalton {
1682fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
1683fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
16845377d758SJohannes Berg 	struct ewma_pkt_len *avg;
1685fbf28d78SMichael Dalton 
1686fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
1687fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
1688fbf28d78SMichael Dalton 	return sprintf(buf, "%u\n", get_mergeable_buf_len(avg));
1689fbf28d78SMichael Dalton }
1690fbf28d78SMichael Dalton 
1691fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
1692fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
1693fbf28d78SMichael Dalton 
1694fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
1695fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
1696fbf28d78SMichael Dalton 	NULL
1697fbf28d78SMichael Dalton };
1698fbf28d78SMichael Dalton 
1699fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
1700fbf28d78SMichael Dalton 	.name = "virtio_net",
1701fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
1702fbf28d78SMichael Dalton };
1703fbf28d78SMichael Dalton #endif
1704fbf28d78SMichael Dalton 
1705892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
1706892d6eb1SJason Wang 				    unsigned int fbit,
1707892d6eb1SJason Wang 				    const char *fname, const char *dname)
1708892d6eb1SJason Wang {
1709892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
1710892d6eb1SJason Wang 		return false;
1711892d6eb1SJason Wang 
1712892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
1713892d6eb1SJason Wang 		fname, dname);
1714892d6eb1SJason Wang 
1715892d6eb1SJason Wang 	return true;
1716892d6eb1SJason Wang }
1717892d6eb1SJason Wang 
1718892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
1719892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
1720892d6eb1SJason Wang 
1721892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
1722892d6eb1SJason Wang {
1723892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
1724892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
1725892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
1726892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
1727892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
1728892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
1729892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
1730892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
1731892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
1732892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
1733892d6eb1SJason Wang 		return false;
1734892d6eb1SJason Wang 	}
1735892d6eb1SJason Wang 
1736892d6eb1SJason Wang 	return true;
1737892d6eb1SJason Wang }
1738892d6eb1SJason Wang 
1739d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
1740d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
1741d0c2c997SJarod Wilson 
1742296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
1743296f96fcSRusty Russell {
1744986a4f4dSJason Wang 	int i, err;
1745296f96fcSRusty Russell 	struct net_device *dev;
1746296f96fcSRusty Russell 	struct virtnet_info *vi;
1747986a4f4dSJason Wang 	u16 max_queue_pairs;
174814de9d11SAaron Conole 	int mtu;
1749986a4f4dSJason Wang 
17506ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
17516ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
17526ba42248SMichael S. Tsirkin 			__func__);
17536ba42248SMichael S. Tsirkin 		return -EINVAL;
17546ba42248SMichael S. Tsirkin 	}
17556ba42248SMichael S. Tsirkin 
1756892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
1757892d6eb1SJason Wang 		return -EINVAL;
1758892d6eb1SJason Wang 
1759986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
1760855e0c52SRusty Russell 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1761855e0c52SRusty Russell 				   struct virtio_net_config,
1762855e0c52SRusty Russell 				   max_virtqueue_pairs, &max_queue_pairs);
1763986a4f4dSJason Wang 
1764986a4f4dSJason Wang 	/* We need at least 2 queue's */
1765986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1766986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1767986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1768986a4f4dSJason Wang 		max_queue_pairs = 1;
1769296f96fcSRusty Russell 
1770296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
1771986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1772296f96fcSRusty Russell 	if (!dev)
1773296f96fcSRusty Russell 		return -ENOMEM;
1774296f96fcSRusty Russell 
1775296f96fcSRusty Russell 	/* Set up network device as normal. */
1776f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
177776288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
1778296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
17793fa2a1dfSstephen hemminger 
17807ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
1781296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
1782296f96fcSRusty Russell 
1783296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
178498e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1785296f96fcSRusty Russell 		/* This opens up the world of extra features. */
178648900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
178798e778c9SMichał Mirosław 		if (csum)
178848900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
178998e778c9SMichał Mirosław 
179098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1791e3e3c423SVlad Yasevich 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
179234a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
179334a48579SRusty Russell 		}
17945539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
179598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
179698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
179798e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
179898e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
179998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
180098e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
1801e3e3c423SVlad Yasevich 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1802e3e3c423SVlad Yasevich 			dev->hw_features |= NETIF_F_UFO;
180398e778c9SMichał Mirosław 
180441f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
180541f2f127SJason Wang 
180698e778c9SMichał Mirosław 		if (gso)
1807e3e3c423SVlad Yasevich 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
180898e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
1809296f96fcSRusty Russell 	}
18104f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
18114f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
1812296f96fcSRusty Russell 
18134fda8302SJason Wang 	dev->vlan_features = dev->features;
18144fda8302SJason Wang 
1815d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
1816d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
1817d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
1818d0c2c997SJarod Wilson 
1819296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
1820855e0c52SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1821855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
1822a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
1823855e0c52SRusty Russell 				   dev->dev_addr, dev->addr_len);
1824855e0c52SRusty Russell 	else
1825f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1826296f96fcSRusty Russell 
1827296f96fcSRusty Russell 	/* Set up our device-specific information */
1828296f96fcSRusty Russell 	vi = netdev_priv(dev);
1829296f96fcSRusty Russell 	vi->dev = dev;
1830296f96fcSRusty Russell 	vi->vdev = vdev;
1831d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
18323fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
18333fa2a1dfSstephen hemminger 	err = -ENOMEM;
18343fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
18353fa2a1dfSstephen hemminger 		goto free;
18363fa2a1dfSstephen hemminger 
1837827da44cSJohn Stultz 	for_each_possible_cpu(i) {
1838827da44cSJohn Stultz 		struct virtnet_stats *virtnet_stats;
1839827da44cSJohn Stultz 		virtnet_stats = per_cpu_ptr(vi->stats, i);
1840827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->tx_syncp);
1841827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->rx_syncp);
1842827da44cSJohn Stultz 	}
1843827da44cSJohn Stultz 
1844586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1845296f96fcSRusty Russell 
184697402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
18478e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
18488e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1849e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
1850e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
185197402b96SHerbert Xu 		vi->big_packets = true;
185297402b96SHerbert Xu 
18533f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
18543f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
18553f2c31d9SMark McLoughlin 
1856d04302b3SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
1857d04302b3SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
1858012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1859012873d0SMichael S. Tsirkin 	else
1860012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
1861012873d0SMichael S. Tsirkin 
186275993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
186375993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
1864e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
1865e7428e95SMichael S. Tsirkin 
1866986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1867986a4f4dSJason Wang 		vi->has_cvq = true;
1868986a4f4dSJason Wang 
186914de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
187014de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
187114de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
187214de9d11SAaron Conole 					      mtu));
1873*93a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
187414de9d11SAaron Conole 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
1875*93a205eeSAaron Conole 		} else {
1876d0c2c997SJarod Wilson 			dev->mtu = mtu;
1877*93a205eeSAaron Conole 			dev->max_mtu = mtu;
1878*93a205eeSAaron Conole 		}
187914de9d11SAaron Conole 	}
188014de9d11SAaron Conole 
1881012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
1882012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
18836ebbc1a6SZhangjie \(HZ\) 
1884986a4f4dSJason Wang 	/* Use single tx/rx queue pair as default */
1885986a4f4dSJason Wang 	vi->curr_queue_pairs = 1;
1886986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
1887986a4f4dSJason Wang 
1888986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
18893f9c10b0SAmit Shah 	err = init_vqs(vi);
1890d2a7dddaSMichael S. Tsirkin 	if (err)
18919bb8ca86SJason Wang 		goto free_stats;
1892d2a7dddaSMichael S. Tsirkin 
1893fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
1894fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
1895fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
1896fbf28d78SMichael Dalton #endif
18970f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
18980f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
1899986a4f4dSJason Wang 
190016032be5SNikolay Aleksandrov 	virtnet_init_settings(dev);
190116032be5SNikolay Aleksandrov 
1902296f96fcSRusty Russell 	err = register_netdev(dev);
1903296f96fcSRusty Russell 	if (err) {
1904296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1905d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1906296f96fcSRusty Russell 	}
1907b3369c1fSRusty Russell 
19084baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
19094baf1e33SMichael S. Tsirkin 
19108017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
19118de4b2f3SWanlong Gao 	if (err) {
19128de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
1913f00e35e2Swangyunjian 		goto free_unregister_netdev;
19148de4b2f3SWanlong Gao 	}
19158de4b2f3SWanlong Gao 
1916167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1917167c25e4SJason Wang 	   otherwise get link status from config. */
1918167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1919167c25e4SJason Wang 		netif_carrier_off(dev);
19203b07e9caSTejun Heo 		schedule_work(&vi->config_work);
1921167c25e4SJason Wang 	} else {
1922167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
19234783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1924167c25e4SJason Wang 	}
19259f4d26d0SMark McLoughlin 
1926986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1927986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
1928986a4f4dSJason Wang 
1929296f96fcSRusty Russell 	return 0;
1930296f96fcSRusty Russell 
1931f00e35e2Swangyunjian free_unregister_netdev:
193202465555SMichael S. Tsirkin 	vi->vdev->config->reset(vdev);
193302465555SMichael S. Tsirkin 
1934b3369c1fSRusty Russell 	unregister_netdev(dev);
1935d2a7dddaSMichael S. Tsirkin free_vqs:
1936986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
1937fb51879dSMichael Dalton 	free_receive_page_frags(vi);
1938e9d7417bSJason Wang 	virtnet_del_vqs(vi);
19393fa2a1dfSstephen hemminger free_stats:
19403fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1941296f96fcSRusty Russell free:
1942296f96fcSRusty Russell 	free_netdev(dev);
1943296f96fcSRusty Russell 	return err;
1944296f96fcSRusty Russell }
1945296f96fcSRusty Russell 
194604486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
1947296f96fcSRusty Russell {
194804486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
1949830a8a97SShirley Ma 
1950830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
19519ab86bbcSShirley Ma 	free_unused_bufs(vi);
1952fb6813f4SRusty Russell 
1953986a4f4dSJason Wang 	free_receive_bufs(vi);
1954d2a7dddaSMichael S. Tsirkin 
1955fb51879dSMichael Dalton 	free_receive_page_frags(vi);
1956fb51879dSMichael Dalton 
1957986a4f4dSJason Wang 	virtnet_del_vqs(vi);
195804486ed0SAmit Shah }
195904486ed0SAmit Shah 
19608cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
196104486ed0SAmit Shah {
196204486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
196304486ed0SAmit Shah 
19648017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
19658de4b2f3SWanlong Gao 
1966102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
1967102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
1968586d17c5SJason Wang 
196904486ed0SAmit Shah 	unregister_netdev(vi->dev);
197004486ed0SAmit Shah 
197104486ed0SAmit Shah 	remove_vq_common(vi);
1972fb6813f4SRusty Russell 
19732e66f55bSKrishna Kumar 	free_percpu(vi->stats);
197474b2553fSRusty Russell 	free_netdev(vi->dev);
1975296f96fcSRusty Russell }
1976296f96fcSRusty Russell 
197789107000SAaron Lu #ifdef CONFIG_PM_SLEEP
19780741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
19790741bcb5SAmit Shah {
19800741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
1981986a4f4dSJason Wang 	int i;
19820741bcb5SAmit Shah 
19838017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
1984ec9debbdSJason Wang 
1985102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device */
1986102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
1987586d17c5SJason Wang 
19880741bcb5SAmit Shah 	netif_device_detach(vi->dev);
19890741bcb5SAmit Shah 	cancel_delayed_work_sync(&vi->refill);
19900741bcb5SAmit Shah 
199191815639SJason Wang 	if (netif_running(vi->dev)) {
1992ab3971b1SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
1993986a4f4dSJason Wang 			napi_disable(&vi->rq[i].napi);
199491815639SJason Wang 	}
19950741bcb5SAmit Shah 
19960741bcb5SAmit Shah 	remove_vq_common(vi);
19970741bcb5SAmit Shah 
19980741bcb5SAmit Shah 	return 0;
19990741bcb5SAmit Shah }
20000741bcb5SAmit Shah 
20010741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
20020741bcb5SAmit Shah {
20030741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
2004986a4f4dSJason Wang 	int err, i;
20050741bcb5SAmit Shah 
20060741bcb5SAmit Shah 	err = init_vqs(vi);
20070741bcb5SAmit Shah 	if (err)
20080741bcb5SAmit Shah 		return err;
20090741bcb5SAmit Shah 
2010e53fbd11SMichael S. Tsirkin 	virtio_device_ready(vdev);
2011e53fbd11SMichael S. Tsirkin 
20126cd4ce00SJason Wang 	if (netif_running(vi->dev)) {
201355257d72SSasha Levin 		for (i = 0; i < vi->curr_queue_pairs; i++)
2014946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
20153b07e9caSTejun Heo 				schedule_delayed_work(&vi->refill, 0);
20160741bcb5SAmit Shah 
20176cd4ce00SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
20186cd4ce00SJason Wang 			virtnet_napi_enable(&vi->rq[i]);
20196cd4ce00SJason Wang 	}
20206cd4ce00SJason Wang 
20216cd4ce00SJason Wang 	netif_device_attach(vi->dev);
20226cd4ce00SJason Wang 
202335ed159bSJason Wang 	rtnl_lock();
2024986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
202535ed159bSJason Wang 	rtnl_unlock();
2026986a4f4dSJason Wang 
20278017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
2028ec9debbdSJason Wang 	if (err)
2029ec9debbdSJason Wang 		return err;
2030ec9debbdSJason Wang 
20310741bcb5SAmit Shah 	return 0;
20320741bcb5SAmit Shah }
20330741bcb5SAmit Shah #endif
20340741bcb5SAmit Shah 
2035296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
2036296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2037296f96fcSRusty Russell 	{ 0 },
2038296f96fcSRusty Russell };
2039296f96fcSRusty Russell 
2040c45a6816SRusty Russell static unsigned int features[] = {
20415e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
20425e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
2043e3e3c423SVlad Yasevich 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
204497402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
2045e3e3c423SVlad Yasevich 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
20462a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
20470bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
2048986a4f4dSJason Wang 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
20497e58d5aeSAmos Kong 	VIRTIO_NET_F_CTRL_MAC_ADDR,
2050e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
205114de9d11SAaron Conole 	VIRTIO_NET_F_MTU,
2052c45a6816SRusty Russell };
2053c45a6816SRusty Russell 
205422402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
2055c45a6816SRusty Russell 	.feature_table = features,
2056c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
2057296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
2058296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
2059296f96fcSRusty Russell 	.id_table =	id_table,
2060296f96fcSRusty Russell 	.probe =	virtnet_probe,
20618cc085d6SBill Pemberton 	.remove =	virtnet_remove,
20629f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
206389107000SAaron Lu #ifdef CONFIG_PM_SLEEP
20640741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
20650741bcb5SAmit Shah 	.restore =	virtnet_restore,
20660741bcb5SAmit Shah #endif
2067296f96fcSRusty Russell };
2068296f96fcSRusty Russell 
20698017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
20708017c279SSebastian Andrzej Siewior {
20718017c279SSebastian Andrzej Siewior 	int ret;
20728017c279SSebastian Andrzej Siewior 
20738017c279SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "AP_VIRT_NET_ONLINE",
20748017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
20758017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
20768017c279SSebastian Andrzej Siewior 	if (ret < 0)
20778017c279SSebastian Andrzej Siewior 		goto out;
20788017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
20798017c279SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "VIRT_NET_DEAD",
20808017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
20818017c279SSebastian Andrzej Siewior 	if (ret)
20828017c279SSebastian Andrzej Siewior 		goto err_dead;
20838017c279SSebastian Andrzej Siewior 
20848017c279SSebastian Andrzej Siewior         ret = register_virtio_driver(&virtio_net_driver);
20858017c279SSebastian Andrzej Siewior 	if (ret)
20868017c279SSebastian Andrzej Siewior 		goto err_virtio;
20878017c279SSebastian Andrzej Siewior 	return 0;
20888017c279SSebastian Andrzej Siewior err_virtio:
20898017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
20908017c279SSebastian Andrzej Siewior err_dead:
20918017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
20928017c279SSebastian Andrzej Siewior out:
20938017c279SSebastian Andrzej Siewior 	return ret;
20948017c279SSebastian Andrzej Siewior }
20958017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
20968017c279SSebastian Andrzej Siewior 
20978017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
20988017c279SSebastian Andrzej Siewior {
20998017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
21008017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
21018017c279SSebastian Andrzej Siewior 	unregister_virtio_driver(&virtio_net_driver);
21028017c279SSebastian Andrzej Siewior }
21038017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
2104296f96fcSRusty Russell 
2105296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
2106296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
2107296f96fcSRusty Russell MODULE_LICENSE("GPL");
2108