xref: /openbmc/linux/drivers/net/virtio_net.c (revision 3f93522ffab2d46a36b57adf324a54e674fc9536)
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>
25f600b690SJohn Fastabend #include <linux/bpf.h>
26a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h>
27296f96fcSRusty Russell #include <linux/scatterlist.h>
28e918085aSAlex Williamson #include <linux/if_vlan.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
308de4b2f3SWanlong Gao #include <linux/cpu.h>
31ab7db917SMichael Dalton #include <linux/average.h>
32d85b758fSMichael S. Tsirkin #include <net/route.h>
33296f96fcSRusty Russell 
34d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
356c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
366c0cd7c0SDor Laor 
37b92f1e67SWillem de Bruijn static bool csum = true, gso = true, napi_tx;
3834a48579SRusty Russell module_param(csum, bool, 0444);
3934a48579SRusty Russell module_param(gso, bool, 0444);
40b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644);
4134a48579SRusty Russell 
42296f96fcSRusty Russell /* FIXME: MTU in config. */
435061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
443f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
45296f96fcSRusty Russell 
46f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
47f6b10209SJason Wang 
482de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
492de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256
502de2f7f4SJohn Fastabend 
515377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet
525377d758SJohannes Berg  * buffer size when refilling RX rings. As the entire RX ring may be refilled
535377d758SJohannes Berg  * at once, the weight is chosen so that the EWMA will be insensitive to short-
545377d758SJohannes Berg  * term, transient changes in packet size.
55ab7db917SMichael Dalton  */
56eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64)
57ab7db917SMichael Dalton 
5866846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
592a41f71dSAlex Williamson 
60*3f93522fSJason Wang const unsigned long guest_offloads[] = { VIRTIO_NET_F_GUEST_TSO4,
61*3f93522fSJason Wang 					 VIRTIO_NET_F_GUEST_TSO6,
62*3f93522fSJason Wang 					 VIRTIO_NET_F_GUEST_ECN,
63*3f93522fSJason Wang 					 VIRTIO_NET_F_GUEST_UFO };
64*3f93522fSJason Wang 
653fa2a1dfSstephen hemminger struct virtnet_stats {
6683a27052SEric Dumazet 	struct u64_stats_sync tx_syncp;
6783a27052SEric Dumazet 	struct u64_stats_sync rx_syncp;
683fa2a1dfSstephen hemminger 	u64 tx_bytes;
693fa2a1dfSstephen hemminger 	u64 tx_packets;
703fa2a1dfSstephen hemminger 
713fa2a1dfSstephen hemminger 	u64 rx_bytes;
723fa2a1dfSstephen hemminger 	u64 rx_packets;
733fa2a1dfSstephen hemminger };
743fa2a1dfSstephen hemminger 
75e9d7417bSJason Wang /* Internal representation of a send virtqueue */
76e9d7417bSJason Wang struct send_queue {
77e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
78e9d7417bSJason Wang 	struct virtqueue *vq;
79e9d7417bSJason Wang 
80e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
81e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
82986a4f4dSJason Wang 
83986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
84986a4f4dSJason Wang 	char name[40];
85b92f1e67SWillem de Bruijn 
86b92f1e67SWillem de Bruijn 	struct napi_struct napi;
87e9d7417bSJason Wang };
88e9d7417bSJason Wang 
89e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
90e9d7417bSJason Wang struct receive_queue {
91e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
92e9d7417bSJason Wang 	struct virtqueue *vq;
93e9d7417bSJason Wang 
94296f96fcSRusty Russell 	struct napi_struct napi;
95296f96fcSRusty Russell 
96f600b690SJohn Fastabend 	struct bpf_prog __rcu *xdp_prog;
97f600b690SJohn Fastabend 
98e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
99e9d7417bSJason Wang 	struct page *pages;
100e9d7417bSJason Wang 
101ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
1025377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
103ab7db917SMichael Dalton 
104fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
105fb51879dSMichael Dalton 	struct page_frag alloc_frag;
106fb51879dSMichael Dalton 
107e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
108e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
109986a4f4dSJason Wang 
110d85b758fSMichael S. Tsirkin 	/* Min single buffer size for mergeable buffers case. */
111d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len;
112d85b758fSMichael S. Tsirkin 
113986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
114986a4f4dSJason Wang 	char name[40];
115e9d7417bSJason Wang };
116e9d7417bSJason Wang 
117e9d7417bSJason Wang struct virtnet_info {
118e9d7417bSJason Wang 	struct virtio_device *vdev;
119e9d7417bSJason Wang 	struct virtqueue *cvq;
120e9d7417bSJason Wang 	struct net_device *dev;
121986a4f4dSJason Wang 	struct send_queue *sq;
122986a4f4dSJason Wang 	struct receive_queue *rq;
123e9d7417bSJason Wang 	unsigned int status;
124e9d7417bSJason Wang 
125986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
126986a4f4dSJason Wang 	u16 max_queue_pairs;
127986a4f4dSJason Wang 
128986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
129986a4f4dSJason Wang 	u16 curr_queue_pairs;
130986a4f4dSJason Wang 
131672aafd5SJohn Fastabend 	/* # of XDP queue pairs currently used by the driver */
132672aafd5SJohn Fastabend 	u16 xdp_queue_pairs;
133672aafd5SJohn Fastabend 
13497402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
13597402b96SHerbert Xu 	bool big_packets;
13697402b96SHerbert Xu 
1373f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
1383f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
1393f2c31d9SMark McLoughlin 
140986a4f4dSJason Wang 	/* Has control virtqueue */
141986a4f4dSJason Wang 	bool has_cvq;
142986a4f4dSJason Wang 
143e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
144e7428e95SMichael S. Tsirkin 	bool any_header_sg;
145e7428e95SMichael S. Tsirkin 
146012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
147012873d0SMichael S. Tsirkin 	u8 hdr_len;
148012873d0SMichael S. Tsirkin 
1493fa2a1dfSstephen hemminger 	/* Active statistics */
1503fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
1513fa2a1dfSstephen hemminger 
1523161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
1533161e453SRusty Russell 	struct delayed_work refill;
1543161e453SRusty Russell 
155586d17c5SJason Wang 	/* Work struct for config space updates */
156586d17c5SJason Wang 	struct work_struct config_work;
157586d17c5SJason Wang 
158986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
159986a4f4dSJason Wang 	bool affinity_hint_set;
16047be2479SWanlong Gao 
1618017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
1628017c279SSebastian Andrzej Siewior 	struct hlist_node node;
1638017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
1642ac46030SMichael S. Tsirkin 
1652ac46030SMichael S. Tsirkin 	/* Control VQ buffers: protected by the rtnl lock */
1662ac46030SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr ctrl_hdr;
1672ac46030SMichael S. Tsirkin 	virtio_net_ctrl_ack ctrl_status;
168a725ee3eSAndy Lutomirski 	struct virtio_net_ctrl_mq ctrl_mq;
1692ac46030SMichael S. Tsirkin 	u8 ctrl_promisc;
1702ac46030SMichael S. Tsirkin 	u8 ctrl_allmulti;
171a725ee3eSAndy Lutomirski 	u16 ctrl_vid;
172*3f93522fSJason Wang 	u64 ctrl_offloads;
17316032be5SNikolay Aleksandrov 
17416032be5SNikolay Aleksandrov 	/* Ethtool settings */
17516032be5SNikolay Aleksandrov 	u8 duplex;
17616032be5SNikolay Aleksandrov 	u32 speed;
177*3f93522fSJason Wang 
178*3f93522fSJason Wang 	unsigned long guest_offloads;
179296f96fcSRusty Russell };
180296f96fcSRusty Russell 
1819ab86bbcSShirley Ma struct padded_vnet_hdr {
182012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf hdr;
1839ab86bbcSShirley Ma 	/*
184012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
185012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
186012873d0SMichael S. Tsirkin 	 * after the header.
1879ab86bbcSShirley Ma 	 */
188012873d0SMichael S. Tsirkin 	char padding[4];
1899ab86bbcSShirley Ma };
1909ab86bbcSShirley Ma 
191986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
192986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
193986a4f4dSJason Wang  */
194986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
195986a4f4dSJason Wang {
1969d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
197986a4f4dSJason Wang }
198986a4f4dSJason Wang 
199986a4f4dSJason Wang static int txq2vq(int txq)
200986a4f4dSJason Wang {
201986a4f4dSJason Wang 	return txq * 2 + 1;
202986a4f4dSJason Wang }
203986a4f4dSJason Wang 
204986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
205986a4f4dSJason Wang {
2069d0ca6edSRusty Russell 	return vq->index / 2;
207986a4f4dSJason Wang }
208986a4f4dSJason Wang 
209986a4f4dSJason Wang static int rxq2vq(int rxq)
210986a4f4dSJason Wang {
211986a4f4dSJason Wang 	return rxq * 2;
212986a4f4dSJason Wang }
213986a4f4dSJason Wang 
214012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
215296f96fcSRusty Russell {
216012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
217296f96fcSRusty Russell }
218296f96fcSRusty Russell 
2199ab86bbcSShirley Ma /*
2209ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
2219ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
2229ab86bbcSShirley Ma  */
223e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
224fb6813f4SRusty Russell {
2259ab86bbcSShirley Ma 	struct page *end;
2269ab86bbcSShirley Ma 
227e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
2289ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
229e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
230e9d7417bSJason Wang 	rq->pages = page;
231fb6813f4SRusty Russell }
232fb6813f4SRusty Russell 
233e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
234fb6813f4SRusty Russell {
235e9d7417bSJason Wang 	struct page *p = rq->pages;
236fb6813f4SRusty Russell 
2379ab86bbcSShirley Ma 	if (p) {
238e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
2399ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
2409ab86bbcSShirley Ma 		p->private = 0;
2419ab86bbcSShirley Ma 	} else
242fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
243fb6813f4SRusty Russell 	return p;
244fb6813f4SRusty Russell }
245fb6813f4SRusty Russell 
246e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi,
247e4e8452aSWillem de Bruijn 				    struct virtqueue *vq)
248e4e8452aSWillem de Bruijn {
249e4e8452aSWillem de Bruijn 	if (napi_schedule_prep(napi)) {
250e4e8452aSWillem de Bruijn 		virtqueue_disable_cb(vq);
251e4e8452aSWillem de Bruijn 		__napi_schedule(napi);
252e4e8452aSWillem de Bruijn 	}
253e4e8452aSWillem de Bruijn }
254e4e8452aSWillem de Bruijn 
255e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi,
256e4e8452aSWillem de Bruijn 				    struct virtqueue *vq, int processed)
257e4e8452aSWillem de Bruijn {
258e4e8452aSWillem de Bruijn 	int opaque;
259e4e8452aSWillem de Bruijn 
260e4e8452aSWillem de Bruijn 	opaque = virtqueue_enable_cb_prepare(vq);
261e4e8452aSWillem de Bruijn 	if (napi_complete_done(napi, processed) &&
262e4e8452aSWillem de Bruijn 	    unlikely(virtqueue_poll(vq, opaque)))
263e4e8452aSWillem de Bruijn 		virtqueue_napi_schedule(napi, vq);
264e4e8452aSWillem de Bruijn }
265e4e8452aSWillem de Bruijn 
266e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
267296f96fcSRusty Russell {
268e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
269b92f1e67SWillem de Bruijn 	struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
270296f96fcSRusty Russell 
2712cb9c6baSRusty Russell 	/* Suppress further interrupts. */
272e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
27311a3a154SRusty Russell 
274b92f1e67SWillem de Bruijn 	if (napi->weight)
275b92f1e67SWillem de Bruijn 		virtqueue_napi_schedule(napi, vq);
276b92f1e67SWillem de Bruijn 	else
277363f1514SRusty Russell 		/* We were probably waiting for more output buffers. */
278986a4f4dSJason Wang 		netif_wake_subqueue(vi->dev, vq2txq(vq));
279296f96fcSRusty Russell }
280296f96fcSRusty Russell 
28128b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22
28228b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize,
28328b39bc7SJason Wang 				  unsigned int headroom)
28428b39bc7SJason Wang {
28528b39bc7SJason Wang 	return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
28628b39bc7SJason Wang }
28728b39bc7SJason Wang 
28828b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
28928b39bc7SJason Wang {
29028b39bc7SJason Wang 	return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
29128b39bc7SJason Wang }
29228b39bc7SJason Wang 
29328b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
29428b39bc7SJason Wang {
29528b39bc7SJason Wang 	return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
29628b39bc7SJason Wang }
29728b39bc7SJason Wang 
2983464645aSMike Waychison /* Called from bottom half context */
299946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
300946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
3012613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
3022613af0eSMichael Dalton 				   unsigned int len, unsigned int truesize)
3039ab86bbcSShirley Ma {
3049ab86bbcSShirley Ma 	struct sk_buff *skb;
305012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
3062613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
3079ab86bbcSShirley Ma 	char *p;
3089ab86bbcSShirley Ma 
3092613af0eSMichael Dalton 	p = page_address(page) + offset;
3109ab86bbcSShirley Ma 
3119ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
312c67f5db8SPaolo Abeni 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
3139ab86bbcSShirley Ma 	if (unlikely(!skb))
3149ab86bbcSShirley Ma 		return NULL;
3159ab86bbcSShirley Ma 
3169ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
3179ab86bbcSShirley Ma 
318012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
319012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
320012873d0SMichael S. Tsirkin 		hdr_padded_len = sizeof *hdr;
321012873d0SMichael S. Tsirkin 	else
3222613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
3233f2c31d9SMark McLoughlin 
3249ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
3253f2c31d9SMark McLoughlin 
3269ab86bbcSShirley Ma 	len -= hdr_len;
3272613af0eSMichael Dalton 	offset += hdr_padded_len;
3282613af0eSMichael Dalton 	p += hdr_padded_len;
3293f2c31d9SMark McLoughlin 
3303f2c31d9SMark McLoughlin 	copy = len;
3313f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
3323f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
33359ae1d12SJohannes Berg 	skb_put_data(skb, p, copy);
3343f2c31d9SMark McLoughlin 
3353f2c31d9SMark McLoughlin 	len -= copy;
3369ab86bbcSShirley Ma 	offset += copy;
3373f2c31d9SMark McLoughlin 
3382613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
3392613af0eSMichael Dalton 		if (len)
3402613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
3412613af0eSMichael Dalton 		else
3422613af0eSMichael Dalton 			put_page(page);
3432613af0eSMichael Dalton 		return skb;
3442613af0eSMichael Dalton 	}
3452613af0eSMichael Dalton 
346e878d78bSSasha Levin 	/*
347e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
348e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
349e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
350e878d78bSSasha Levin 	 * the case of a broken device.
351e878d78bSSasha Levin 	 */
352e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
353be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
354e878d78bSSasha Levin 		dev_kfree_skb(skb);
355e878d78bSSasha Levin 		return NULL;
356e878d78bSSasha Levin 	}
3572613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
3589ab86bbcSShirley Ma 	while (len) {
3592613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
3602613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
3612613af0eSMichael Dalton 				frag_size, truesize);
3622613af0eSMichael Dalton 		len -= frag_size;
3639ab86bbcSShirley Ma 		page = (struct page *)page->private;
3649ab86bbcSShirley Ma 		offset = 0;
3653f2c31d9SMark McLoughlin 	}
3663f2c31d9SMark McLoughlin 
3679ab86bbcSShirley Ma 	if (page)
368e9d7417bSJason Wang 		give_pages(rq, page);
3693f2c31d9SMark McLoughlin 
3709ab86bbcSShirley Ma 	return skb;
3719ab86bbcSShirley Ma }
3729ab86bbcSShirley Ma 
373a67edbf4SDaniel Borkmann static bool virtnet_xdp_xmit(struct virtnet_info *vi,
37456434a01SJohn Fastabend 			     struct receive_queue *rq,
375f6b10209SJason Wang 			     struct xdp_buff *xdp)
37656434a01SJohn Fastabend {
37756434a01SJohn Fastabend 	struct virtio_net_hdr_mrg_rxbuf *hdr;
378f6b10209SJason Wang 	unsigned int len;
379722d8283SJohn Fastabend 	struct send_queue *sq;
380722d8283SJohn Fastabend 	unsigned int qp;
38156434a01SJohn Fastabend 	void *xdp_sent;
38256434a01SJohn Fastabend 	int err;
38356434a01SJohn Fastabend 
384722d8283SJohn Fastabend 	qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id();
385722d8283SJohn Fastabend 	sq = &vi->sq[qp];
386722d8283SJohn Fastabend 
38756434a01SJohn Fastabend 	/* Free up any pending old buffers before queueing new ones. */
38856434a01SJohn Fastabend 	while ((xdp_sent = virtqueue_get_buf(sq->vq, &len)) != NULL) {
38956434a01SJohn Fastabend 		struct page *sent_page = virt_to_head_page(xdp_sent);
390bb91accfSJason Wang 
39156434a01SJohn Fastabend 		put_page(sent_page);
39256434a01SJohn Fastabend 	}
39356434a01SJohn Fastabend 
394f6b10209SJason Wang 	xdp->data -= vi->hdr_len;
39556434a01SJohn Fastabend 	/* Zero header and leave csum up to XDP layers */
39656434a01SJohn Fastabend 	hdr = xdp->data;
39756434a01SJohn Fastabend 	memset(hdr, 0, vi->hdr_len);
39856434a01SJohn Fastabend 
39956434a01SJohn Fastabend 	sg_init_one(sq->sg, xdp->data, xdp->data_end - xdp->data);
400bb91accfSJason Wang 
401f6b10209SJason Wang 	err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp->data, GFP_ATOMIC);
40256434a01SJohn Fastabend 	if (unlikely(err)) {
403bb91accfSJason Wang 		struct page *page = virt_to_head_page(xdp->data);
404bb91accfSJason Wang 
40556434a01SJohn Fastabend 		put_page(page);
406a67edbf4SDaniel Borkmann 		return false;
40756434a01SJohn Fastabend 	}
40856434a01SJohn Fastabend 
40956434a01SJohn Fastabend 	virtqueue_kick(sq->vq);
410a67edbf4SDaniel Borkmann 	return true;
41156434a01SJohn Fastabend }
41256434a01SJohn Fastabend 
413f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
414f6b10209SJason Wang {
415f6b10209SJason Wang 	return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
416f6b10209SJason Wang }
417f6b10209SJason Wang 
4184941d472SJason Wang /* We copy the packet for XDP in the following cases:
4194941d472SJason Wang  *
4204941d472SJason Wang  * 1) Packet is scattered across multiple rx buffers.
4214941d472SJason Wang  * 2) Headroom space is insufficient.
4224941d472SJason Wang  *
4234941d472SJason Wang  * This is inefficient but it's a temporary condition that
4244941d472SJason Wang  * we hit right after XDP is enabled and until queue is refilled
4254941d472SJason Wang  * with large buffers with sufficient headroom - so it should affect
4264941d472SJason Wang  * at most queue size packets.
4274941d472SJason Wang  * Afterwards, the conditions to enable
4284941d472SJason Wang  * XDP should preclude the underlying device from sending packets
4294941d472SJason Wang  * across multiple buffers (num_buf > 1), and we make sure buffers
4304941d472SJason Wang  * have enough headroom.
43172979a6cSJohn Fastabend  */
43272979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq,
43356a86f84SJason Wang 				       u16 *num_buf,
43472979a6cSJohn Fastabend 				       struct page *p,
43572979a6cSJohn Fastabend 				       int offset,
4364941d472SJason Wang 				       int page_off,
43772979a6cSJohn Fastabend 				       unsigned int *len)
43872979a6cSJohn Fastabend {
43972979a6cSJohn Fastabend 	struct page *page = alloc_page(GFP_ATOMIC);
44072979a6cSJohn Fastabend 
44172979a6cSJohn Fastabend 	if (!page)
44272979a6cSJohn Fastabend 		return NULL;
44372979a6cSJohn Fastabend 
44472979a6cSJohn Fastabend 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
44572979a6cSJohn Fastabend 	page_off += *len;
44672979a6cSJohn Fastabend 
44756a86f84SJason Wang 	while (--*num_buf) {
44872979a6cSJohn Fastabend 		unsigned int buflen;
44972979a6cSJohn Fastabend 		void *buf;
45072979a6cSJohn Fastabend 		int off;
45172979a6cSJohn Fastabend 
452680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &buflen);
453680557cfSMichael S. Tsirkin 		if (unlikely(!buf))
45472979a6cSJohn Fastabend 			goto err_buf;
45572979a6cSJohn Fastabend 
45672979a6cSJohn Fastabend 		p = virt_to_head_page(buf);
45772979a6cSJohn Fastabend 		off = buf - page_address(p);
45872979a6cSJohn Fastabend 
45956a86f84SJason Wang 		/* guard against a misconfigured or uncooperative backend that
46056a86f84SJason Wang 		 * is sending packet larger than the MTU.
46156a86f84SJason Wang 		 */
46256a86f84SJason Wang 		if ((page_off + buflen) > PAGE_SIZE) {
46356a86f84SJason Wang 			put_page(p);
46456a86f84SJason Wang 			goto err_buf;
46556a86f84SJason Wang 		}
46656a86f84SJason Wang 
46772979a6cSJohn Fastabend 		memcpy(page_address(page) + page_off,
46872979a6cSJohn Fastabend 		       page_address(p) + off, buflen);
46972979a6cSJohn Fastabend 		page_off += buflen;
47056a86f84SJason Wang 		put_page(p);
47172979a6cSJohn Fastabend 	}
47272979a6cSJohn Fastabend 
4732de2f7f4SJohn Fastabend 	/* Headroom does not contribute to packet length */
4742de2f7f4SJohn Fastabend 	*len = page_off - VIRTIO_XDP_HEADROOM;
47572979a6cSJohn Fastabend 	return page;
47672979a6cSJohn Fastabend err_buf:
47772979a6cSJohn Fastabend 	__free_pages(page, 0);
47872979a6cSJohn Fastabend 	return NULL;
47972979a6cSJohn Fastabend }
48072979a6cSJohn Fastabend 
4814941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev,
4824941d472SJason Wang 				     struct virtnet_info *vi,
4834941d472SJason Wang 				     struct receive_queue *rq,
4844941d472SJason Wang 				     void *buf, void *ctx,
4854941d472SJason Wang 				     unsigned int len)
4864941d472SJason Wang {
4874941d472SJason Wang 	struct sk_buff *skb;
4884941d472SJason Wang 	struct bpf_prog *xdp_prog;
4894941d472SJason Wang 	unsigned int xdp_headroom = (unsigned long)ctx;
4904941d472SJason Wang 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
4914941d472SJason Wang 	unsigned int headroom = vi->hdr_len + header_offset;
4924941d472SJason Wang 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
4934941d472SJason Wang 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4944941d472SJason Wang 	struct page *page = virt_to_head_page(buf);
4954941d472SJason Wang 	unsigned int delta = 0;
4964941d472SJason Wang 	struct page *xdp_page;
4974941d472SJason Wang 	len -= vi->hdr_len;
4984941d472SJason Wang 
4994941d472SJason Wang 	rcu_read_lock();
5004941d472SJason Wang 	xdp_prog = rcu_dereference(rq->xdp_prog);
5014941d472SJason Wang 	if (xdp_prog) {
5024941d472SJason Wang 		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
5034941d472SJason Wang 		struct xdp_buff xdp;
5044941d472SJason Wang 		void *orig_data;
5054941d472SJason Wang 		u32 act;
5064941d472SJason Wang 
5074941d472SJason Wang 		if (unlikely(hdr->hdr.gso_type || hdr->hdr.flags))
5084941d472SJason Wang 			goto err_xdp;
5094941d472SJason Wang 
5104941d472SJason Wang 		if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
5114941d472SJason Wang 			int offset = buf - page_address(page) + header_offset;
5124941d472SJason Wang 			unsigned int tlen = len + vi->hdr_len;
5134941d472SJason Wang 			u16 num_buf = 1;
5144941d472SJason Wang 
5154941d472SJason Wang 			xdp_headroom = virtnet_get_headroom(vi);
5164941d472SJason Wang 			header_offset = VIRTNET_RX_PAD + xdp_headroom;
5174941d472SJason Wang 			headroom = vi->hdr_len + header_offset;
5184941d472SJason Wang 			buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
5194941d472SJason Wang 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5204941d472SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf, page,
5214941d472SJason Wang 						      offset, header_offset,
5224941d472SJason Wang 						      &tlen);
5234941d472SJason Wang 			if (!xdp_page)
5244941d472SJason Wang 				goto err_xdp;
5254941d472SJason Wang 
5264941d472SJason Wang 			buf = page_address(xdp_page);
5274941d472SJason Wang 			put_page(page);
5284941d472SJason Wang 			page = xdp_page;
5294941d472SJason Wang 		}
5304941d472SJason Wang 
5314941d472SJason Wang 		xdp.data_hard_start = buf + VIRTNET_RX_PAD + vi->hdr_len;
5324941d472SJason Wang 		xdp.data = xdp.data_hard_start + xdp_headroom;
5334941d472SJason Wang 		xdp.data_end = xdp.data + len;
5344941d472SJason Wang 		orig_data = xdp.data;
5354941d472SJason Wang 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
5364941d472SJason Wang 
5374941d472SJason Wang 		switch (act) {
5384941d472SJason Wang 		case XDP_PASS:
5394941d472SJason Wang 			/* Recalculate length in case bpf program changed it */
5404941d472SJason Wang 			delta = orig_data - xdp.data;
5414941d472SJason Wang 			break;
5424941d472SJason Wang 		case XDP_TX:
5434941d472SJason Wang 			if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
5444941d472SJason Wang 				trace_xdp_exception(vi->dev, xdp_prog, act);
5454941d472SJason Wang 			rcu_read_unlock();
5464941d472SJason Wang 			goto xdp_xmit;
5474941d472SJason Wang 		default:
5484941d472SJason Wang 			bpf_warn_invalid_xdp_action(act);
5494941d472SJason Wang 		case XDP_ABORTED:
5504941d472SJason Wang 			trace_xdp_exception(vi->dev, xdp_prog, act);
5514941d472SJason Wang 		case XDP_DROP:
5524941d472SJason Wang 			goto err_xdp;
5534941d472SJason Wang 		}
5544941d472SJason Wang 	}
5554941d472SJason Wang 	rcu_read_unlock();
5564941d472SJason Wang 
5574941d472SJason Wang 	skb = build_skb(buf, buflen);
5584941d472SJason Wang 	if (!skb) {
5594941d472SJason Wang 		put_page(page);
5604941d472SJason Wang 		goto err;
5614941d472SJason Wang 	}
5624941d472SJason Wang 	skb_reserve(skb, headroom - delta);
5634941d472SJason Wang 	skb_put(skb, len + delta);
5644941d472SJason Wang 	if (!delta) {
5654941d472SJason Wang 		buf += header_offset;
5664941d472SJason Wang 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
5674941d472SJason Wang 	} /* keep zeroed vnet hdr since packet was changed by bpf */
5684941d472SJason Wang 
5694941d472SJason Wang err:
5704941d472SJason Wang 	return skb;
5714941d472SJason Wang 
5724941d472SJason Wang err_xdp:
5734941d472SJason Wang 	rcu_read_unlock();
5744941d472SJason Wang 	dev->stats.rx_dropped++;
5754941d472SJason Wang 	put_page(page);
5764941d472SJason Wang xdp_xmit:
5774941d472SJason Wang 	return NULL;
5784941d472SJason Wang }
5794941d472SJason Wang 
5804941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev,
5814941d472SJason Wang 				   struct virtnet_info *vi,
5824941d472SJason Wang 				   struct receive_queue *rq,
5834941d472SJason Wang 				   void *buf,
5844941d472SJason Wang 				   unsigned int len)
5854941d472SJason Wang {
5864941d472SJason Wang 	struct page *page = buf;
5874941d472SJason Wang 	struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE);
5884941d472SJason Wang 
5894941d472SJason Wang 	if (unlikely(!skb))
5904941d472SJason Wang 		goto err;
5914941d472SJason Wang 
5924941d472SJason Wang 	return skb;
5934941d472SJason Wang 
5944941d472SJason Wang err:
5954941d472SJason Wang 	dev->stats.rx_dropped++;
5964941d472SJason Wang 	give_pages(rq, page);
5974941d472SJason Wang 	return NULL;
5984941d472SJason Wang }
5994941d472SJason Wang 
6008fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
601fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
6028fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
603680557cfSMichael S. Tsirkin 					 void *buf,
604680557cfSMichael S. Tsirkin 					 void *ctx,
6058fc3b9e9SMichael S. Tsirkin 					 unsigned int len)
6069ab86bbcSShirley Ma {
607012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
608012873d0SMichael S. Tsirkin 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
6098fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
6108fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
611f600b690SJohn Fastabend 	struct sk_buff *head_skb, *curr_skb;
612f600b690SJohn Fastabend 	struct bpf_prog *xdp_prog;
613f600b690SJohn Fastabend 	unsigned int truesize;
6144941d472SJason Wang 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
615ab7db917SMichael Dalton 
61656434a01SJohn Fastabend 	head_skb = NULL;
61756434a01SJohn Fastabend 
618f600b690SJohn Fastabend 	rcu_read_lock();
619f600b690SJohn Fastabend 	xdp_prog = rcu_dereference(rq->xdp_prog);
620f600b690SJohn Fastabend 	if (xdp_prog) {
62172979a6cSJohn Fastabend 		struct page *xdp_page;
6220354e4d1SJohn Fastabend 		struct xdp_buff xdp;
6230354e4d1SJohn Fastabend 		void *data;
624f600b690SJohn Fastabend 		u32 act;
625f600b690SJohn Fastabend 
62673b62bd0SJason Wang 		/* This happens when rx buffer size is underestimated */
6274941d472SJason Wang 		if (unlikely(num_buf > 1 ||
6284941d472SJason Wang 			     headroom < virtnet_get_headroom(vi))) {
62972979a6cSJohn Fastabend 			/* linearize data for XDP */
63056a86f84SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf,
6314941d472SJason Wang 						      page, offset,
6324941d472SJason Wang 						      VIRTIO_XDP_HEADROOM,
6334941d472SJason Wang 						      &len);
63472979a6cSJohn Fastabend 			if (!xdp_page)
635f600b690SJohn Fastabend 				goto err_xdp;
6362de2f7f4SJohn Fastabend 			offset = VIRTIO_XDP_HEADROOM;
63772979a6cSJohn Fastabend 		} else {
63872979a6cSJohn Fastabend 			xdp_page = page;
639f600b690SJohn Fastabend 		}
640f600b690SJohn Fastabend 
641f600b690SJohn Fastabend 		/* Transient failure which in theory could occur if
642f600b690SJohn Fastabend 		 * in-flight packets from before XDP was enabled reach
643f600b690SJohn Fastabend 		 * the receive path after XDP is loaded. In practice I
644f600b690SJohn Fastabend 		 * was not able to create this condition.
645f600b690SJohn Fastabend 		 */
646b00f70b0SJason Wang 		if (unlikely(hdr->hdr.gso_type))
647f600b690SJohn Fastabend 			goto err_xdp;
648f600b690SJohn Fastabend 
6492de2f7f4SJohn Fastabend 		/* Allow consuming headroom but reserve enough space to push
6502de2f7f4SJohn Fastabend 		 * the descriptor on if we get an XDP_TX return code.
6512de2f7f4SJohn Fastabend 		 */
6520354e4d1SJohn Fastabend 		data = page_address(xdp_page) + offset;
6532de2f7f4SJohn Fastabend 		xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;
6540354e4d1SJohn Fastabend 		xdp.data = data + vi->hdr_len;
6550354e4d1SJohn Fastabend 		xdp.data_end = xdp.data + (len - vi->hdr_len);
6560354e4d1SJohn Fastabend 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
6570354e4d1SJohn Fastabend 
65856434a01SJohn Fastabend 		switch (act) {
65956434a01SJohn Fastabend 		case XDP_PASS:
6602de2f7f4SJohn Fastabend 			/* recalculate offset to account for any header
6612de2f7f4SJohn Fastabend 			 * adjustments. Note other cases do not build an
6622de2f7f4SJohn Fastabend 			 * skb and avoid using offset
6632de2f7f4SJohn Fastabend 			 */
6642de2f7f4SJohn Fastabend 			offset = xdp.data -
6652de2f7f4SJohn Fastabend 					page_address(xdp_page) - vi->hdr_len;
6662de2f7f4SJohn Fastabend 
6671830f893SJason Wang 			/* We can only create skb based on xdp_page. */
6681830f893SJason Wang 			if (unlikely(xdp_page != page)) {
6691830f893SJason Wang 				rcu_read_unlock();
6701830f893SJason Wang 				put_page(page);
6711830f893SJason Wang 				head_skb = page_to_skb(vi, rq, xdp_page,
6722de2f7f4SJohn Fastabend 						       offset, len, PAGE_SIZE);
6735c33474dSJason Wang 				ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
6741830f893SJason Wang 				return head_skb;
6751830f893SJason Wang 			}
67656434a01SJohn Fastabend 			break;
67756434a01SJohn Fastabend 		case XDP_TX:
678f6b10209SJason Wang 			if (unlikely(!virtnet_xdp_xmit(vi, rq, &xdp)))
6790354e4d1SJohn Fastabend 				trace_xdp_exception(vi->dev, xdp_prog, act);
6805c33474dSJason Wang 			ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
68172979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
68272979a6cSJohn Fastabend 				goto err_xdp;
68356434a01SJohn Fastabend 			rcu_read_unlock();
68456434a01SJohn Fastabend 			goto xdp_xmit;
68556434a01SJohn Fastabend 		default:
6860354e4d1SJohn Fastabend 			bpf_warn_invalid_xdp_action(act);
6870354e4d1SJohn Fastabend 		case XDP_ABORTED:
6880354e4d1SJohn Fastabend 			trace_xdp_exception(vi->dev, xdp_prog, act);
6890354e4d1SJohn Fastabend 		case XDP_DROP:
69072979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
69172979a6cSJohn Fastabend 				__free_pages(xdp_page, 0);
6925c33474dSJason Wang 			ewma_pkt_len_add(&rq->mrg_avg_pkt_len, len);
693f600b690SJohn Fastabend 			goto err_xdp;
694f600b690SJohn Fastabend 		}
69556434a01SJohn Fastabend 	}
696f600b690SJohn Fastabend 	rcu_read_unlock();
697f600b690SJohn Fastabend 
69828b39bc7SJason Wang 	truesize = mergeable_ctx_to_truesize(ctx);
69928b39bc7SJason Wang 	if (unlikely(len > truesize)) {
70056da5fd0SDan Carpenter 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
701680557cfSMichael S. Tsirkin 			 dev->name, len, (unsigned long)ctx);
702680557cfSMichael S. Tsirkin 		dev->stats.rx_length_errors++;
703680557cfSMichael S. Tsirkin 		goto err_skb;
704680557cfSMichael S. Tsirkin 	}
70528b39bc7SJason Wang 
706f600b690SJohn Fastabend 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize);
707f600b690SJohn Fastabend 	curr_skb = head_skb;
7089ab86bbcSShirley Ma 
7098fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
7108fc3b9e9SMichael S. Tsirkin 		goto err_skb;
7119ab86bbcSShirley Ma 	while (--num_buf) {
7128fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
7138fc3b9e9SMichael S. Tsirkin 
714680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
715ab7db917SMichael Dalton 		if (unlikely(!ctx)) {
7168fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
717fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
718012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
719012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
7208fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
7218fc3b9e9SMichael S. Tsirkin 			goto err_buf;
7223f2c31d9SMark McLoughlin 		}
7238fc3b9e9SMichael S. Tsirkin 
7248fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
72528b39bc7SJason Wang 
72628b39bc7SJason Wang 		truesize = mergeable_ctx_to_truesize(ctx);
72728b39bc7SJason Wang 		if (unlikely(len > truesize)) {
72856da5fd0SDan Carpenter 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
729680557cfSMichael S. Tsirkin 				 dev->name, len, (unsigned long)ctx);
730680557cfSMichael S. Tsirkin 			dev->stats.rx_length_errors++;
731680557cfSMichael S. Tsirkin 			goto err_skb;
732680557cfSMichael S. Tsirkin 		}
7338fc3b9e9SMichael S. Tsirkin 
7348fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
7352613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
7362613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
7378fc3b9e9SMichael S. Tsirkin 
7388fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
7398fc3b9e9SMichael S. Tsirkin 				goto err_skb;
7402613af0eSMichael Dalton 			if (curr_skb == head_skb)
7412613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
7422613af0eSMichael Dalton 			else
7432613af0eSMichael Dalton 				curr_skb->next = nskb;
7442613af0eSMichael Dalton 			curr_skb = nskb;
7452613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
7462613af0eSMichael Dalton 			num_skb_frags = 0;
7472613af0eSMichael Dalton 		}
7482613af0eSMichael Dalton 		if (curr_skb != head_skb) {
7492613af0eSMichael Dalton 			head_skb->data_len += len;
7502613af0eSMichael Dalton 			head_skb->len += len;
751fb51879dSMichael Dalton 			head_skb->truesize += truesize;
7522613af0eSMichael Dalton 		}
7538fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
754ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
755ba275241SJason Wang 			put_page(page);
756ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
757fb51879dSMichael Dalton 					     len, truesize);
758ba275241SJason Wang 		} else {
7592613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
760fb51879dSMichael Dalton 					offset, len, truesize);
761ba275241SJason Wang 		}
7628fc3b9e9SMichael S. Tsirkin 	}
7638fc3b9e9SMichael S. Tsirkin 
7645377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
7658fc3b9e9SMichael S. Tsirkin 	return head_skb;
7668fc3b9e9SMichael S. Tsirkin 
767f600b690SJohn Fastabend err_xdp:
768f600b690SJohn Fastabend 	rcu_read_unlock();
7698fc3b9e9SMichael S. Tsirkin err_skb:
7708fc3b9e9SMichael S. Tsirkin 	put_page(page);
7718fc3b9e9SMichael S. Tsirkin 	while (--num_buf) {
772680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &len);
773680557cfSMichael S. Tsirkin 		if (unlikely(!buf)) {
7748fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
7758fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
7768fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
7778fc3b9e9SMichael S. Tsirkin 			break;
7788fc3b9e9SMichael S. Tsirkin 		}
779680557cfSMichael S. Tsirkin 		page = virt_to_head_page(buf);
7808fc3b9e9SMichael S. Tsirkin 		put_page(page);
7813f2c31d9SMark McLoughlin 	}
7828fc3b9e9SMichael S. Tsirkin err_buf:
7838fc3b9e9SMichael S. Tsirkin 	dev->stats.rx_dropped++;
7848fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
78556434a01SJohn Fastabend xdp_xmit:
7868fc3b9e9SMichael S. Tsirkin 	return NULL;
7879ab86bbcSShirley Ma }
7889ab86bbcSShirley Ma 
78961845d20SJason Wang static int receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
790680557cfSMichael S. Tsirkin 		       void *buf, unsigned int len, void **ctx)
7919ab86bbcSShirley Ma {
792e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
7939ab86bbcSShirley Ma 	struct sk_buff *skb;
794012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
79561845d20SJason Wang 	int ret;
7969ab86bbcSShirley Ma 
797bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
7989ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
7999ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
800ab7db917SMichael Dalton 		if (vi->mergeable_rx_bufs) {
801680557cfSMichael S. Tsirkin 			put_page(virt_to_head_page(buf));
802ab7db917SMichael Dalton 		} else if (vi->big_packets) {
80398bfd23cSMichael Dalton 			give_pages(rq, buf);
804ab7db917SMichael Dalton 		} else {
805f6b10209SJason Wang 			put_page(virt_to_head_page(buf));
806ab7db917SMichael Dalton 		}
80761845d20SJason Wang 		return 0;
8089ab86bbcSShirley Ma 	}
8099ab86bbcSShirley Ma 
810f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
811680557cfSMichael S. Tsirkin 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len);
812f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
813946fa564SMichael S. Tsirkin 		skb = receive_big(dev, vi, rq, buf, len);
814f121159dSMichael S. Tsirkin 	else
815192f68cfSJason Wang 		skb = receive_small(dev, vi, rq, buf, ctx, len);
816f121159dSMichael S. Tsirkin 
8178fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
81861845d20SJason Wang 		return 0;
8193f2c31d9SMark McLoughlin 
8209ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
8213fa2a1dfSstephen hemminger 
82261845d20SJason Wang 	ret = skb->len;
823296f96fcSRusty Russell 
824e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
82510a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
826296f96fcSRusty Russell 
827e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
828e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
829e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
830e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
831fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
832296f96fcSRusty Russell 		goto frame_err;
833296f96fcSRusty Russell 	}
834296f96fcSRusty Russell 
835d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
836d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
837d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
838d1dc06dcSMike Rapoport 
8390fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
84061845d20SJason Wang 	return ret;
841296f96fcSRusty Russell 
842296f96fcSRusty Russell frame_err:
843296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
844296f96fcSRusty Russell 	dev_kfree_skb(skb);
84561845d20SJason Wang 	return 0;
846296f96fcSRusty Russell }
847296f96fcSRusty Russell 
848192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the
849192f68cfSJason Wang  * same size, except for the headroom. For this reason we do
850192f68cfSJason Wang  * not need to use  mergeable_len_to_ctx here - it is enough
851192f68cfSJason Wang  * to store the headroom as the context ignoring the truesize.
852192f68cfSJason Wang  */
853946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
854946fa564SMichael S. Tsirkin 			     gfp_t gfp)
855296f96fcSRusty Russell {
856f6b10209SJason Wang 	struct page_frag *alloc_frag = &rq->alloc_frag;
857f6b10209SJason Wang 	char *buf;
8582de2f7f4SJohn Fastabend 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
859192f68cfSJason Wang 	void *ctx = (void *)(unsigned long)xdp_headroom;
860f6b10209SJason Wang 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
8619ab86bbcSShirley Ma 	int err;
8623f2c31d9SMark McLoughlin 
863f6b10209SJason Wang 	len = SKB_DATA_ALIGN(len) +
864f6b10209SJason Wang 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
865f6b10209SJason Wang 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
8669ab86bbcSShirley Ma 		return -ENOMEM;
867296f96fcSRusty Russell 
868f6b10209SJason Wang 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
869f6b10209SJason Wang 	get_page(alloc_frag->page);
870f6b10209SJason Wang 	alloc_frag->offset += len;
871f6b10209SJason Wang 	sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
872f6b10209SJason Wang 		    vi->hdr_len + GOOD_PACKET_LEN);
873192f68cfSJason Wang 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
8749ab86bbcSShirley Ma 	if (err < 0)
875f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
8769ab86bbcSShirley Ma 	return err;
87797402b96SHerbert Xu }
87897402b96SHerbert Xu 
879012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
880012873d0SMichael S. Tsirkin 			   gfp_t gfp)
8819ab86bbcSShirley Ma {
8829ab86bbcSShirley Ma 	struct page *first, *list = NULL;
8839ab86bbcSShirley Ma 	char *p;
8849ab86bbcSShirley Ma 	int i, err, offset;
885296f96fcSRusty Russell 
886a5835440SRusty Russell 	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
887a5835440SRusty Russell 
888e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
8899ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
890e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
8919ab86bbcSShirley Ma 		if (!first) {
8929ab86bbcSShirley Ma 			if (list)
893e9d7417bSJason Wang 				give_pages(rq, list);
8949ab86bbcSShirley Ma 			return -ENOMEM;
895296f96fcSRusty Russell 		}
896e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
8979ab86bbcSShirley Ma 
8989ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
8999ab86bbcSShirley Ma 		first->private = (unsigned long)list;
9009ab86bbcSShirley Ma 		list = first;
9019ab86bbcSShirley Ma 	}
9029ab86bbcSShirley Ma 
903e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
9049ab86bbcSShirley Ma 	if (!first) {
905e9d7417bSJason Wang 		give_pages(rq, list);
9069ab86bbcSShirley Ma 		return -ENOMEM;
9079ab86bbcSShirley Ma 	}
9089ab86bbcSShirley Ma 	p = page_address(first);
9099ab86bbcSShirley Ma 
910e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
911012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
912012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
9139ab86bbcSShirley Ma 
914e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
9159ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
916e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
9179ab86bbcSShirley Ma 
9189ab86bbcSShirley Ma 	/* chain first in list head */
9199ab86bbcSShirley Ma 	first->private = (unsigned long)list;
9209dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
921aa989f5eSMichael S. Tsirkin 				  first, gfp);
9229ab86bbcSShirley Ma 	if (err < 0)
923e9d7417bSJason Wang 		give_pages(rq, first);
9249ab86bbcSShirley Ma 
9259ab86bbcSShirley Ma 	return err;
9269ab86bbcSShirley Ma }
9279ab86bbcSShirley Ma 
928d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
929d85b758fSMichael S. Tsirkin 					  struct ewma_pkt_len *avg_pkt_len)
9309ab86bbcSShirley Ma {
931ab7db917SMichael Dalton 	const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
932fbf28d78SMichael Dalton 	unsigned int len;
933fbf28d78SMichael Dalton 
9345377d758SJohannes Berg 	len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
935f0c3192cSMichael S. Tsirkin 				rq->min_buf_len, PAGE_SIZE - hdr_len);
936e377fcc8SMichael S. Tsirkin 	return ALIGN(len, L1_CACHE_BYTES);
937fbf28d78SMichael Dalton }
938fbf28d78SMichael Dalton 
9392de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi,
9402de2f7f4SJohn Fastabend 				 struct receive_queue *rq, gfp_t gfp)
941fbf28d78SMichael Dalton {
942fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
9432de2f7f4SJohn Fastabend 	unsigned int headroom = virtnet_get_headroom(vi);
944fb51879dSMichael Dalton 	char *buf;
945680557cfSMichael S. Tsirkin 	void *ctx;
9469ab86bbcSShirley Ma 	int err;
947fb51879dSMichael Dalton 	unsigned int len, hole;
9489ab86bbcSShirley Ma 
949d85b758fSMichael S. Tsirkin 	len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len);
9502de2f7f4SJohn Fastabend 	if (unlikely(!skb_page_frag_refill(len + headroom, alloc_frag, gfp)))
9519ab86bbcSShirley Ma 		return -ENOMEM;
952ab7db917SMichael Dalton 
953fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
9542de2f7f4SJohn Fastabend 	buf += headroom; /* advance address leaving hole at front of pkt */
95528b39bc7SJason Wang 	ctx = mergeable_len_to_ctx(len, headroom);
956fb51879dSMichael Dalton 	get_page(alloc_frag->page);
9572de2f7f4SJohn Fastabend 	alloc_frag->offset += len + headroom;
958fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
9592de2f7f4SJohn Fastabend 	if (hole < len + headroom) {
960ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
961ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
962ab7db917SMichael Dalton 		 * the current buffer. This extra space is not included in
963ab7db917SMichael Dalton 		 * the truesize stored in ctx.
964ab7db917SMichael Dalton 		 */
965fb51879dSMichael Dalton 		len += hole;
966fb51879dSMichael Dalton 		alloc_frag->offset += hole;
967fb51879dSMichael Dalton 	}
9689ab86bbcSShirley Ma 
969fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
970680557cfSMichael S. Tsirkin 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
9719ab86bbcSShirley Ma 	if (err < 0)
9722613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
9739ab86bbcSShirley Ma 
9749ab86bbcSShirley Ma 	return err;
975296f96fcSRusty Russell }
976296f96fcSRusty Russell 
977b2baed69SRusty Russell /*
978b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
979b2baed69SRusty Russell  *
980b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
981b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
982b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
983b2baed69SRusty Russell  */
984946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
985946fa564SMichael S. Tsirkin 			  gfp_t gfp)
9863f2c31d9SMark McLoughlin {
9873f2c31d9SMark McLoughlin 	int err;
9881788f495SMichael S. Tsirkin 	bool oom;
9893f2c31d9SMark McLoughlin 
990fb51879dSMichael Dalton 	gfp |= __GFP_COLD;
9910aea51c3SAmit Shah 	do {
9929ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
9932de2f7f4SJohn Fastabend 			err = add_recvbuf_mergeable(vi, rq, gfp);
9949ab86bbcSShirley Ma 		else if (vi->big_packets)
995012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
9969ab86bbcSShirley Ma 		else
997946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
9983f2c31d9SMark McLoughlin 
9991788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
10009ed4cb07SRusty Russell 		if (err)
10013f2c31d9SMark McLoughlin 			break;
1002b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
1003681daee2SJason Wang 	virtqueue_kick(rq->vq);
10043161e453SRusty Russell 	return !oom;
10053f2c31d9SMark McLoughlin }
10063f2c31d9SMark McLoughlin 
100718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
1008296f96fcSRusty Russell {
1009296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
1010986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1011e9d7417bSJason Wang 
1012e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(&rq->napi, rvq);
1013296f96fcSRusty Russell }
1014296f96fcSRusty Russell 
1015e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
10163e9d08ecSBruce Rogers {
1017e4e8452aSWillem de Bruijn 	napi_enable(napi);
10183e9d08ecSBruce Rogers 
10193e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
1020e4e8452aSWillem de Bruijn 	 * won't get another interrupt, so process any outstanding packets now.
1021e4e8452aSWillem de Bruijn 	 * Call local_bh_enable after to trigger softIRQ processing.
1022e4e8452aSWillem de Bruijn 	 */
1023ec13ee80SMichael S. Tsirkin 	local_bh_disable();
1024e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(napi, vq);
1025ec13ee80SMichael S. Tsirkin 	local_bh_enable();
10263e9d08ecSBruce Rogers }
10273e9d08ecSBruce Rogers 
1028b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1029b92f1e67SWillem de Bruijn 				   struct virtqueue *vq,
1030b92f1e67SWillem de Bruijn 				   struct napi_struct *napi)
1031b92f1e67SWillem de Bruijn {
1032b92f1e67SWillem de Bruijn 	if (!napi->weight)
1033b92f1e67SWillem de Bruijn 		return;
1034b92f1e67SWillem de Bruijn 
1035b92f1e67SWillem de Bruijn 	/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1036b92f1e67SWillem de Bruijn 	 * enable the feature if this is likely affine with the transmit path.
1037b92f1e67SWillem de Bruijn 	 */
1038b92f1e67SWillem de Bruijn 	if (!vi->affinity_hint_set) {
1039b92f1e67SWillem de Bruijn 		napi->weight = 0;
1040b92f1e67SWillem de Bruijn 		return;
1041b92f1e67SWillem de Bruijn 	}
1042b92f1e67SWillem de Bruijn 
1043b92f1e67SWillem de Bruijn 	return virtnet_napi_enable(vq, napi);
1044b92f1e67SWillem de Bruijn }
1045b92f1e67SWillem de Bruijn 
104678a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi)
104778a57b48SWillem de Bruijn {
104878a57b48SWillem de Bruijn 	if (napi->weight)
104978a57b48SWillem de Bruijn 		napi_disable(napi);
105078a57b48SWillem de Bruijn }
105178a57b48SWillem de Bruijn 
10523161e453SRusty Russell static void refill_work(struct work_struct *work)
10533161e453SRusty Russell {
1054e9d7417bSJason Wang 	struct virtnet_info *vi =
1055e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
10563161e453SRusty Russell 	bool still_empty;
1057986a4f4dSJason Wang 	int i;
10583161e453SRusty Russell 
105955257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1060986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
1061986a4f4dSJason Wang 
1062986a4f4dSJason Wang 		napi_disable(&rq->napi);
1063946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1064e4e8452aSWillem de Bruijn 		virtnet_napi_enable(rq->vq, &rq->napi);
10653161e453SRusty Russell 
10663161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
1067986a4f4dSJason Wang 		 * we will *never* try to fill again.
1068986a4f4dSJason Wang 		 */
10693161e453SRusty Russell 		if (still_empty)
10703b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
10713161e453SRusty Russell 	}
1072986a4f4dSJason Wang }
10733161e453SRusty Russell 
10742ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget)
1075296f96fcSRusty Russell {
1076e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
107761845d20SJason Wang 	unsigned int len, received = 0, bytes = 0;
10789ab86bbcSShirley Ma 	void *buf;
107961845d20SJason Wang 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
1080296f96fcSRusty Russell 
1081192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
1082680557cfSMichael S. Tsirkin 		void *ctx;
1083680557cfSMichael S. Tsirkin 
1084680557cfSMichael S. Tsirkin 		while (received < budget &&
1085680557cfSMichael S. Tsirkin 		       (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1086680557cfSMichael S. Tsirkin 			bytes += receive_buf(vi, rq, buf, len, ctx);
1087680557cfSMichael S. Tsirkin 			received++;
1088680557cfSMichael S. Tsirkin 		}
1089680557cfSMichael S. Tsirkin 	} else {
1090296f96fcSRusty Russell 		while (received < budget &&
1091e9d7417bSJason Wang 		       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1092680557cfSMichael S. Tsirkin 			bytes += receive_buf(vi, rq, buf, len, NULL);
1093296f96fcSRusty Russell 			received++;
1094296f96fcSRusty Russell 		}
1095680557cfSMichael S. Tsirkin 	}
1096296f96fcSRusty Russell 
1097be121f46SJason Wang 	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
1098946fa564SMichael S. Tsirkin 		if (!try_fill_recv(vi, rq, GFP_ATOMIC))
10993b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
11003161e453SRusty Russell 	}
1101296f96fcSRusty Russell 
110261845d20SJason Wang 	u64_stats_update_begin(&stats->rx_syncp);
110361845d20SJason Wang 	stats->rx_bytes += bytes;
110461845d20SJason Wang 	stats->rx_packets += received;
110561845d20SJason Wang 	u64_stats_update_end(&stats->rx_syncp);
110661845d20SJason Wang 
11072ffa7598SJason Wang 	return received;
11082ffa7598SJason Wang }
11092ffa7598SJason Wang 
1110ea7735d9SWillem de Bruijn static void free_old_xmit_skbs(struct send_queue *sq)
1111ea7735d9SWillem de Bruijn {
1112ea7735d9SWillem de Bruijn 	struct sk_buff *skb;
1113ea7735d9SWillem de Bruijn 	unsigned int len;
1114ea7735d9SWillem de Bruijn 	struct virtnet_info *vi = sq->vq->vdev->priv;
1115ea7735d9SWillem de Bruijn 	struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
1116ea7735d9SWillem de Bruijn 	unsigned int packets = 0;
1117ea7735d9SWillem de Bruijn 	unsigned int bytes = 0;
1118ea7735d9SWillem de Bruijn 
1119ea7735d9SWillem de Bruijn 	while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
1120ea7735d9SWillem de Bruijn 		pr_debug("Sent skb %p\n", skb);
1121ea7735d9SWillem de Bruijn 
1122ea7735d9SWillem de Bruijn 		bytes += skb->len;
1123ea7735d9SWillem de Bruijn 		packets++;
1124ea7735d9SWillem de Bruijn 
1125ea7735d9SWillem de Bruijn 		dev_kfree_skb_any(skb);
1126ea7735d9SWillem de Bruijn 	}
1127ea7735d9SWillem de Bruijn 
1128ea7735d9SWillem de Bruijn 	/* Avoid overhead when no packets have been processed
1129ea7735d9SWillem de Bruijn 	 * happens when called speculatively from start_xmit.
1130ea7735d9SWillem de Bruijn 	 */
1131ea7735d9SWillem de Bruijn 	if (!packets)
1132ea7735d9SWillem de Bruijn 		return;
1133ea7735d9SWillem de Bruijn 
1134ea7735d9SWillem de Bruijn 	u64_stats_update_begin(&stats->tx_syncp);
1135ea7735d9SWillem de Bruijn 	stats->tx_bytes += bytes;
1136ea7735d9SWillem de Bruijn 	stats->tx_packets += packets;
1137ea7735d9SWillem de Bruijn 	u64_stats_update_end(&stats->tx_syncp);
1138ea7735d9SWillem de Bruijn }
1139ea7735d9SWillem de Bruijn 
11407b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq)
11417b0411efSWillem de Bruijn {
11427b0411efSWillem de Bruijn 	struct virtnet_info *vi = rq->vq->vdev->priv;
11437b0411efSWillem de Bruijn 	unsigned int index = vq2rxq(rq->vq);
11447b0411efSWillem de Bruijn 	struct send_queue *sq = &vi->sq[index];
11457b0411efSWillem de Bruijn 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
11467b0411efSWillem de Bruijn 
11477b0411efSWillem de Bruijn 	if (!sq->napi.weight)
11487b0411efSWillem de Bruijn 		return;
11497b0411efSWillem de Bruijn 
11507b0411efSWillem de Bruijn 	if (__netif_tx_trylock(txq)) {
11517b0411efSWillem de Bruijn 		free_old_xmit_skbs(sq);
11527b0411efSWillem de Bruijn 		__netif_tx_unlock(txq);
11537b0411efSWillem de Bruijn 	}
11547b0411efSWillem de Bruijn 
11557b0411efSWillem de Bruijn 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
11567b0411efSWillem de Bruijn 		netif_tx_wake_queue(txq);
11577b0411efSWillem de Bruijn }
11587b0411efSWillem de Bruijn 
11592ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
11602ffa7598SJason Wang {
11612ffa7598SJason Wang 	struct receive_queue *rq =
11622ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
1163e4e8452aSWillem de Bruijn 	unsigned int received;
11642ffa7598SJason Wang 
11657b0411efSWillem de Bruijn 	virtnet_poll_cleantx(rq);
11667b0411efSWillem de Bruijn 
1167faadb05fSLi RongQing 	received = virtnet_receive(rq, budget);
11682ffa7598SJason Wang 
11698329d98eSRusty Russell 	/* Out of packets? */
1170e4e8452aSWillem de Bruijn 	if (received < budget)
1171e4e8452aSWillem de Bruijn 		virtqueue_napi_complete(napi, rq->vq, received);
1172296f96fcSRusty Russell 
1173296f96fcSRusty Russell 	return received;
1174296f96fcSRusty Russell }
1175296f96fcSRusty Russell 
1176986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
1177986a4f4dSJason Wang {
1178986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1179986a4f4dSJason Wang 	int i;
1180986a4f4dSJason Wang 
1181e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1182e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
1183986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
1184946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1185986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
1186e4e8452aSWillem de Bruijn 		virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1187b92f1e67SWillem de Bruijn 		virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1188986a4f4dSJason Wang 	}
1189986a4f4dSJason Wang 
1190986a4f4dSJason Wang 	return 0;
1191986a4f4dSJason Wang }
1192986a4f4dSJason Wang 
1193b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1194b92f1e67SWillem de Bruijn {
1195b92f1e67SWillem de Bruijn 	struct send_queue *sq = container_of(napi, struct send_queue, napi);
1196b92f1e67SWillem de Bruijn 	struct virtnet_info *vi = sq->vq->vdev->priv;
1197b92f1e67SWillem de Bruijn 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, vq2txq(sq->vq));
1198b92f1e67SWillem de Bruijn 
1199b92f1e67SWillem de Bruijn 	__netif_tx_lock(txq, raw_smp_processor_id());
1200b92f1e67SWillem de Bruijn 	free_old_xmit_skbs(sq);
1201b92f1e67SWillem de Bruijn 	__netif_tx_unlock(txq);
1202b92f1e67SWillem de Bruijn 
1203b92f1e67SWillem de Bruijn 	virtqueue_napi_complete(napi, sq->vq, 0);
1204b92f1e67SWillem de Bruijn 
1205b92f1e67SWillem de Bruijn 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1206b92f1e67SWillem de Bruijn 		netif_tx_wake_queue(txq);
1207b92f1e67SWillem de Bruijn 
1208b92f1e67SWillem de Bruijn 	return 0;
1209b92f1e67SWillem de Bruijn }
1210b92f1e67SWillem de Bruijn 
1211e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1212296f96fcSRusty Russell {
1213012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1214296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1215e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
1216e2fcad58SJason A. Donenfeld 	int num_sg;
1217012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
1218e7428e95SMichael S. Tsirkin 	bool can_push;
1219296f96fcSRusty Russell 
1220e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1221e7428e95SMichael S. Tsirkin 
1222e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
1223e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1224e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1225e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
1226e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
1227e7428e95SMichael S. Tsirkin 	if (can_push)
1228012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1229e7428e95SMichael S. Tsirkin 	else
1230e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
1231296f96fcSRusty Russell 
1232e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
12336391a448SJason Wang 				    virtio_is_little_endian(vi->vdev), false))
1234296f96fcSRusty Russell 		BUG();
1235296f96fcSRusty Russell 
1236e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
1237012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
12383f2c31d9SMark McLoughlin 
1239547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1240e7428e95SMichael S. Tsirkin 	if (can_push) {
1241e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
1242e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1243e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
1244e2fcad58SJason A. Donenfeld 			return num_sg;
1245e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
1246e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
1247e7428e95SMichael S. Tsirkin 	} else {
1248e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
1249e2fcad58SJason A. Donenfeld 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1250e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
1251e2fcad58SJason A. Donenfeld 			return num_sg;
1252e2fcad58SJason A. Donenfeld 		num_sg++;
1253e7428e95SMichael S. Tsirkin 	}
12549dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
125511a3a154SRusty Russell }
125611a3a154SRusty Russell 
1257424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
125899ffc696SRusty Russell {
125999ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1260986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
1261986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
12629ed4cb07SRusty Russell 	int err;
12634b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
12644b7fd2e6SMichael S. Tsirkin 	bool kick = !skb->xmit_more;
1265b92f1e67SWillem de Bruijn 	bool use_napi = sq->napi.weight;
12662cb9c6baSRusty Russell 
12672cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
1268e9d7417bSJason Wang 	free_old_xmit_skbs(sq);
126999ffc696SRusty Russell 
1270bdb12e0dSWillem de Bruijn 	if (use_napi && kick)
1271bdb12e0dSWillem de Bruijn 		virtqueue_enable_cb_delayed(sq->vq);
1272bdb12e0dSWillem de Bruijn 
1273074c3582SJacob Keller 	/* timestamp packet in software */
1274074c3582SJacob Keller 	skb_tx_timestamp(skb);
1275074c3582SJacob Keller 
127603f191baSMichael S. Tsirkin 	/* Try to transmit */
1277b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
127899ffc696SRusty Russell 
12799ed4cb07SRusty Russell 	/* This should not happen! */
1280681daee2SJason Wang 	if (unlikely(err)) {
128158eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
12822e57b79cSRick Jones 		if (net_ratelimit())
128358eba97dSRusty Russell 			dev_warn(&dev->dev,
1284b7dfde95SLinus Torvalds 				 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
128558eba97dSRusty Russell 		dev->stats.tx_dropped++;
128685e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
128758eba97dSRusty Russell 		return NETDEV_TX_OK;
1288296f96fcSRusty Russell 	}
128903f191baSMichael S. Tsirkin 
129048925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
1291b92f1e67SWillem de Bruijn 	if (!use_napi) {
129248925e37SRusty Russell 		skb_orphan(skb);
129348925e37SRusty Russell 		nf_reset(skb);
1294b92f1e67SWillem de Bruijn 	}
129548925e37SRusty Russell 
129660302ff6SMichael S. Tsirkin 	/* If running out of space, stop queue to avoid getting packets that we
129760302ff6SMichael S. Tsirkin 	 * are then unable to transmit.
129860302ff6SMichael S. Tsirkin 	 * An alternative would be to force queuing layer to requeue the skb by
129960302ff6SMichael S. Tsirkin 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
130060302ff6SMichael S. Tsirkin 	 * returned in a normal path of operation: it means that driver is not
130160302ff6SMichael S. Tsirkin 	 * maintaining the TX queue stop/start state properly, and causes
130260302ff6SMichael S. Tsirkin 	 * the stack to do a non-trivial amount of useless work.
130360302ff6SMichael S. Tsirkin 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
130460302ff6SMichael S. Tsirkin 	 * early means 16 slots are typically wasted.
1305d631b94eSstephen hemminger 	 */
1306b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1307986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
1308b92f1e67SWillem de Bruijn 		if (!use_napi &&
1309b92f1e67SWillem de Bruijn 		    unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
131048925e37SRusty Russell 			/* More just got used, free them then recheck. */
1311b7dfde95SLinus Torvalds 			free_old_xmit_skbs(sq);
1312b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1313986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
1314e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
131548925e37SRusty Russell 			}
131648925e37SRusty Russell 		}
131748925e37SRusty Russell 	}
131848925e37SRusty Russell 
13194b7fd2e6SMichael S. Tsirkin 	if (kick || netif_xmit_stopped(txq))
1320c223a078SDavid S. Miller 		virtqueue_kick(sq->vq);
13210b725a2cSDavid S. Miller 
13220b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
1323c223a078SDavid S. Miller }
1324c223a078SDavid S. Miller 
132540cbfc37SAmos Kong /*
132640cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
132740cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
1328788a8b6dSstephen hemminger  * never fail unless improperly formatted.
132940cbfc37SAmos Kong  */
133040cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1331d24bae32Sstephen hemminger 				 struct scatterlist *out)
133240cbfc37SAmos Kong {
1333f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
1334d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
133540cbfc37SAmos Kong 
133640cbfc37SAmos Kong 	/* Caller should know better */
1337f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
133840cbfc37SAmos Kong 
13392ac46030SMichael S. Tsirkin 	vi->ctrl_status = ~0;
13402ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.class = class;
13412ac46030SMichael S. Tsirkin 	vi->ctrl_hdr.cmd = cmd;
1342f7bc9594SRusty Russell 	/* Add header */
13432ac46030SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr));
1344f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
134540cbfc37SAmos Kong 
1346f7bc9594SRusty Russell 	if (out)
1347f7bc9594SRusty Russell 		sgs[out_num++] = out;
134840cbfc37SAmos Kong 
1349f7bc9594SRusty Russell 	/* Add return status. */
13502ac46030SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status));
1351d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
135240cbfc37SAmos Kong 
1353d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1354a7c58146SRusty Russell 	virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
135540cbfc37SAmos Kong 
135667975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
13572ac46030SMichael S. Tsirkin 		return vi->ctrl_status == VIRTIO_NET_OK;
135840cbfc37SAmos Kong 
135940cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
136040cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
136140cbfc37SAmos Kong 	 */
1362047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1363047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
136440cbfc37SAmos Kong 		cpu_relax();
136540cbfc37SAmos Kong 
13662ac46030SMichael S. Tsirkin 	return vi->ctrl_status == VIRTIO_NET_OK;
136740cbfc37SAmos Kong }
136840cbfc37SAmos Kong 
13699c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
13709c46f6d4SAlex Williamson {
13719c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
13729c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
1373f2f2c8b4SJiri Pirko 	int ret;
1374e37e2ff3SAndy Lutomirski 	struct sockaddr *addr;
13757e58d5aeSAmos Kong 	struct scatterlist sg;
13769c46f6d4SAlex Williamson 
1377801822d1SShyam Saini 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1378e37e2ff3SAndy Lutomirski 	if (!addr)
1379e37e2ff3SAndy Lutomirski 		return -ENOMEM;
1380e37e2ff3SAndy Lutomirski 
1381e37e2ff3SAndy Lutomirski 	ret = eth_prepare_mac_addr_change(dev, addr);
1382f2f2c8b4SJiri Pirko 	if (ret)
1383e37e2ff3SAndy Lutomirski 		goto out;
13849c46f6d4SAlex Williamson 
13857e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
13867e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
13877e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1388d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
13897e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
13907e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
1391e37e2ff3SAndy Lutomirski 			ret = -EINVAL;
1392e37e2ff3SAndy Lutomirski 			goto out;
13937e58d5aeSAmos Kong 		}
13947e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
13957e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1396855e0c52SRusty Russell 		unsigned int i;
1397855e0c52SRusty Russell 
1398855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
1399855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
1400855e0c52SRusty Russell 			virtio_cwrite8(vdev,
1401855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
1402855e0c52SRusty Russell 				       i, addr->sa_data[i]);
14037e58d5aeSAmos Kong 	}
14047e58d5aeSAmos Kong 
14057e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
1406e37e2ff3SAndy Lutomirski 	ret = 0;
14079c46f6d4SAlex Williamson 
1408e37e2ff3SAndy Lutomirski out:
1409e37e2ff3SAndy Lutomirski 	kfree(addr);
1410e37e2ff3SAndy Lutomirski 	return ret;
14119c46f6d4SAlex Williamson }
14129c46f6d4SAlex Williamson 
1413bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev,
14143fa2a1dfSstephen hemminger 			  struct rtnl_link_stats64 *tot)
14153fa2a1dfSstephen hemminger {
14163fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
14173fa2a1dfSstephen hemminger 	int cpu;
14183fa2a1dfSstephen hemminger 	unsigned int start;
14193fa2a1dfSstephen hemminger 
14203fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
142158472a76SEric Dumazet 		struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
14223fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
14233fa2a1dfSstephen hemminger 
14243fa2a1dfSstephen hemminger 		do {
142557a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->tx_syncp);
14263fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
14273fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
142857a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start));
142983a27052SEric Dumazet 
143083a27052SEric Dumazet 		do {
143157a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&stats->rx_syncp);
14323fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
14333fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
143457a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start));
14353fa2a1dfSstephen hemminger 
14363fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
14373fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
14383fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
14393fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
14403fa2a1dfSstephen hemminger 	}
14413fa2a1dfSstephen hemminger 
14423fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
1443021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
14443fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
14453fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
14463fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
14473fa2a1dfSstephen hemminger }
14483fa2a1dfSstephen hemminger 
1449da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
1450da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
1451da74e89dSAmit Shah {
1452da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
1453986a4f4dSJason Wang 	int i;
1454da74e89dSAmit Shah 
1455986a4f4dSJason Wang 	for (i = 0; i < vi->curr_queue_pairs; i++)
1456986a4f4dSJason Wang 		napi_schedule(&vi->rq[i].napi);
1457da74e89dSAmit Shah }
1458da74e89dSAmit Shah #endif
1459da74e89dSAmit Shah 
1460586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
1461586d17c5SJason Wang {
1462586d17c5SJason Wang 	rtnl_lock();
1463586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1464d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1465586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1466586d17c5SJason Wang 	rtnl_unlock();
1467586d17c5SJason Wang }
1468586d17c5SJason Wang 
146947315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1470986a4f4dSJason Wang {
1471986a4f4dSJason Wang 	struct scatterlist sg;
1472986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
1473986a4f4dSJason Wang 
1474986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1475986a4f4dSJason Wang 		return 0;
1476986a4f4dSJason Wang 
1477a725ee3eSAndy Lutomirski 	vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1478a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq));
1479986a4f4dSJason Wang 
1480986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1481d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1482986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1483986a4f4dSJason Wang 			 queue_pairs);
1484986a4f4dSJason Wang 		return -EINVAL;
148555257d72SSasha Levin 	} else {
1486986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
148735ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
148835ed159bSJason Wang 		if (dev->flags & IFF_UP)
14899b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
149055257d72SSasha Levin 	}
1491986a4f4dSJason Wang 
1492986a4f4dSJason Wang 	return 0;
1493986a4f4dSJason Wang }
1494986a4f4dSJason Wang 
149547315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
149647315329SJohn Fastabend {
149747315329SJohn Fastabend 	int err;
149847315329SJohn Fastabend 
149947315329SJohn Fastabend 	rtnl_lock();
150047315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
150147315329SJohn Fastabend 	rtnl_unlock();
150247315329SJohn Fastabend 	return err;
150347315329SJohn Fastabend }
150447315329SJohn Fastabend 
1505296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
1506296f96fcSRusty Russell {
1507296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1508986a4f4dSJason Wang 	int i;
1509296f96fcSRusty Russell 
1510b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1511b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1512986a4f4dSJason Wang 
1513b92f1e67SWillem de Bruijn 	for (i = 0; i < vi->max_queue_pairs; i++) {
1514986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
151578a57b48SWillem de Bruijn 		virtnet_napi_tx_disable(&vi->sq[i].napi);
1516b92f1e67SWillem de Bruijn 	}
1517296f96fcSRusty Russell 
1518296f96fcSRusty Russell 	return 0;
1519296f96fcSRusty Russell }
1520296f96fcSRusty Russell 
15212af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
15222af7698eSAlex Williamson {
15232af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1524f565a7c2SAlex Williamson 	struct scatterlist sg[2];
1525f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1526ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
152732e7bfc4SJiri Pirko 	int uc_count;
15284cd24eafSJiri Pirko 	int mc_count;
1529f565a7c2SAlex Williamson 	void *buf;
1530f565a7c2SAlex Williamson 	int i;
15312af7698eSAlex Williamson 
1532788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
15332af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
15342af7698eSAlex Williamson 		return;
15352af7698eSAlex Williamson 
15362ac46030SMichael S. Tsirkin 	vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0);
15372ac46030SMichael S. Tsirkin 	vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
15382af7698eSAlex Williamson 
15392ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc));
15402af7698eSAlex Williamson 
15412af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1542d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
15432af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
15442ac46030SMichael S. Tsirkin 			 vi->ctrl_promisc ? "en" : "dis");
15452af7698eSAlex Williamson 
15462ac46030SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti));
15472af7698eSAlex Williamson 
15482af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1549d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
15502af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
15512ac46030SMichael S. Tsirkin 			 vi->ctrl_allmulti ? "en" : "dis");
1552f565a7c2SAlex Williamson 
155332e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
15544cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1555f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
15564cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1557f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
15584cd24eafSJiri Pirko 	mac_data = buf;
1559e68ed8f0SJoe Perches 	if (!buf)
1560f565a7c2SAlex Williamson 		return;
1561f565a7c2SAlex Williamson 
156223e258e1SAlex Williamson 	sg_init_table(sg, 2);
156323e258e1SAlex Williamson 
1564f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
1565fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1566ccffad25SJiri Pirko 	i = 0;
156732e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1568ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1569f565a7c2SAlex Williamson 
1570f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
157132e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1572f565a7c2SAlex Williamson 
1573f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
157432e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1575f565a7c2SAlex Williamson 
1576fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1577567ec874SJiri Pirko 	i = 0;
157822bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
157922bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1580f565a7c2SAlex Williamson 
1581f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
15824cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1583f565a7c2SAlex Williamson 
1584f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1585d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
158699e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1587f565a7c2SAlex Williamson 
1588f565a7c2SAlex Williamson 	kfree(buf);
15892af7698eSAlex Williamson }
15902af7698eSAlex Williamson 
159180d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
159280d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
15930bde9569SAlex Williamson {
15940bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
15950bde9569SAlex Williamson 	struct scatterlist sg;
15960bde9569SAlex Williamson 
1597a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1598a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
15990bde9569SAlex Williamson 
16000bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1601d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
16020bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
16038e586137SJiri Pirko 	return 0;
16040bde9569SAlex Williamson }
16050bde9569SAlex Williamson 
160680d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
160780d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
16080bde9569SAlex Williamson {
16090bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
16100bde9569SAlex Williamson 	struct scatterlist sg;
16110bde9569SAlex Williamson 
1612a725ee3eSAndy Lutomirski 	vi->ctrl_vid = vid;
1613a725ee3eSAndy Lutomirski 	sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid));
16140bde9569SAlex Williamson 
16150bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1616d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
16170bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
16188e586137SJiri Pirko 	return 0;
16190bde9569SAlex Williamson }
16200bde9569SAlex Williamson 
16218898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1622986a4f4dSJason Wang {
1623986a4f4dSJason Wang 	int i;
16248898c21cSWanlong Gao 
16258898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
16268898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
16278898c21cSWanlong Gao 			virtqueue_set_affinity(vi->rq[i].vq, -1);
16288898c21cSWanlong Gao 			virtqueue_set_affinity(vi->sq[i].vq, -1);
16298898c21cSWanlong Gao 		}
16308898c21cSWanlong Gao 
16318898c21cSWanlong Gao 		vi->affinity_hint_set = false;
16328898c21cSWanlong Gao 	}
16338898c21cSWanlong Gao }
16348898c21cSWanlong Gao 
16358898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
1636986a4f4dSJason Wang {
1637986a4f4dSJason Wang 	int i;
163847be2479SWanlong Gao 	int cpu;
1639986a4f4dSJason Wang 
1640986a4f4dSJason Wang 	/* In multiqueue mode, when the number of cpu is equal to the number of
1641986a4f4dSJason Wang 	 * queue pairs, we let the queue pairs to be private to one cpu by
1642986a4f4dSJason Wang 	 * setting the affinity hint to eliminate the contention.
1643986a4f4dSJason Wang 	 */
16448898c21cSWanlong Gao 	if (vi->curr_queue_pairs == 1 ||
16458898c21cSWanlong Gao 	    vi->max_queue_pairs != num_online_cpus()) {
16468898c21cSWanlong Gao 		virtnet_clean_affinity(vi, -1);
1647986a4f4dSJason Wang 		return;
1648986a4f4dSJason Wang 	}
1649986a4f4dSJason Wang 
165047be2479SWanlong Gao 	i = 0;
165147be2479SWanlong Gao 	for_each_online_cpu(cpu) {
1652986a4f4dSJason Wang 		virtqueue_set_affinity(vi->rq[i].vq, cpu);
1653986a4f4dSJason Wang 		virtqueue_set_affinity(vi->sq[i].vq, cpu);
16549bb8ca86SJason Wang 		netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
165547be2479SWanlong Gao 		i++;
1656986a4f4dSJason Wang 	}
1657986a4f4dSJason Wang 
1658986a4f4dSJason Wang 	vi->affinity_hint_set = true;
165947be2479SWanlong Gao }
1660986a4f4dSJason Wang 
16618017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
16628de4b2f3SWanlong Gao {
16638017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
16648017c279SSebastian Andrzej Siewior 						   node);
16658de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
16668017c279SSebastian Andrzej Siewior 	return 0;
16678de4b2f3SWanlong Gao }
16683ab098dfSJason Wang 
16698017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
16708017c279SSebastian Andrzej Siewior {
16718017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
16728017c279SSebastian Andrzej Siewior 						   node_dead);
16738017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
16748017c279SSebastian Andrzej Siewior 	return 0;
16758017c279SSebastian Andrzej Siewior }
16768017c279SSebastian Andrzej Siewior 
16778017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
16788017c279SSebastian Andrzej Siewior {
16798017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
16808017c279SSebastian Andrzej Siewior 						   node);
16818017c279SSebastian Andrzej Siewior 
16828017c279SSebastian Andrzej Siewior 	virtnet_clean_affinity(vi, cpu);
16838017c279SSebastian Andrzej Siewior 	return 0;
16848017c279SSebastian Andrzej Siewior }
16858017c279SSebastian Andrzej Siewior 
16868017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
16878017c279SSebastian Andrzej Siewior 
16888017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
16898017c279SSebastian Andrzej Siewior {
16908017c279SSebastian Andrzej Siewior 	int ret;
16918017c279SSebastian Andrzej Siewior 
16928017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
16938017c279SSebastian Andrzej Siewior 	if (ret)
16948017c279SSebastian Andrzej Siewior 		return ret;
16958017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
16968017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
16978017c279SSebastian Andrzej Siewior 	if (!ret)
16988017c279SSebastian Andrzej Siewior 		return ret;
16998017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
17008017c279SSebastian Andrzej Siewior 	return ret;
17018017c279SSebastian Andrzej Siewior }
17028017c279SSebastian Andrzej Siewior 
17038017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
17048017c279SSebastian Andrzej Siewior {
17058017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
17068017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
17078017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
1708a9ea3fc6SHerbert Xu }
1709a9ea3fc6SHerbert Xu 
17108f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
17118f9f4668SRick Jones 				struct ethtool_ringparam *ring)
17128f9f4668SRick Jones {
17138f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
17148f9f4668SRick Jones 
1715986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1716986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
17178f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
17188f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
17198f9f4668SRick Jones }
17208f9f4668SRick Jones 
172166846048SRick Jones 
172266846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
172366846048SRick Jones 				struct ethtool_drvinfo *info)
172466846048SRick Jones {
172566846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
172666846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
172766846048SRick Jones 
172866846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
172966846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
173066846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
173166846048SRick Jones 
173266846048SRick Jones }
173366846048SRick Jones 
1734d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
1735d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
1736d73bcd2cSJason Wang 				struct ethtool_channels *channels)
1737d73bcd2cSJason Wang {
1738d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1739d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
1740d73bcd2cSJason Wang 	int err;
1741d73bcd2cSJason Wang 
1742d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
1743d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
1744d73bcd2cSJason Wang 	 */
1745d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
1746d73bcd2cSJason Wang 		return -EINVAL;
1747d73bcd2cSJason Wang 
1748c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
1749d73bcd2cSJason Wang 		return -EINVAL;
1750d73bcd2cSJason Wang 
1751f600b690SJohn Fastabend 	/* For now we don't support modifying channels while XDP is loaded
1752f600b690SJohn Fastabend 	 * also when XDP is loaded all RX queues have XDP programs so we only
1753f600b690SJohn Fastabend 	 * need to check a single RX queue.
1754f600b690SJohn Fastabend 	 */
1755f600b690SJohn Fastabend 	if (vi->rq[0].xdp_prog)
1756f600b690SJohn Fastabend 		return -EINVAL;
1757f600b690SJohn Fastabend 
175847be2479SWanlong Gao 	get_online_cpus();
175947315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
1760d73bcd2cSJason Wang 	if (!err) {
1761d73bcd2cSJason Wang 		netif_set_real_num_tx_queues(dev, queue_pairs);
1762d73bcd2cSJason Wang 		netif_set_real_num_rx_queues(dev, queue_pairs);
1763d73bcd2cSJason Wang 
17648898c21cSWanlong Gao 		virtnet_set_affinity(vi);
1765d73bcd2cSJason Wang 	}
176647be2479SWanlong Gao 	put_online_cpus();
1767d73bcd2cSJason Wang 
1768d73bcd2cSJason Wang 	return err;
1769d73bcd2cSJason Wang }
1770d73bcd2cSJason Wang 
1771d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
1772d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
1773d73bcd2cSJason Wang {
1774d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1775d73bcd2cSJason Wang 
1776d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
1777d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
1778d73bcd2cSJason Wang 	channels->max_other = 0;
1779d73bcd2cSJason Wang 	channels->rx_count = 0;
1780d73bcd2cSJason Wang 	channels->tx_count = 0;
1781d73bcd2cSJason Wang 	channels->other_count = 0;
1782d73bcd2cSJason Wang }
1783d73bcd2cSJason Wang 
178416032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */
1785ebb6b4b1SPhilippe Reynes static bool
1786ebb6b4b1SPhilippe Reynes virtnet_validate_ethtool_cmd(const struct ethtool_link_ksettings *cmd)
178716032be5SNikolay Aleksandrov {
1788ebb6b4b1SPhilippe Reynes 	struct ethtool_link_ksettings diff1 = *cmd;
1789ebb6b4b1SPhilippe Reynes 	struct ethtool_link_ksettings diff2 = {};
179016032be5SNikolay Aleksandrov 
17910cf3ace9SNikolay Aleksandrov 	/* cmd is always set so we need to clear it, validate the port type
17920cf3ace9SNikolay Aleksandrov 	 * and also without autonegotiation we can ignore advertising
17930cf3ace9SNikolay Aleksandrov 	 */
1794ebb6b4b1SPhilippe Reynes 	diff1.base.speed = 0;
1795ebb6b4b1SPhilippe Reynes 	diff2.base.port = PORT_OTHER;
1796ebb6b4b1SPhilippe Reynes 	ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
1797ebb6b4b1SPhilippe Reynes 	diff1.base.duplex = 0;
1798ebb6b4b1SPhilippe Reynes 	diff1.base.cmd = 0;
1799ebb6b4b1SPhilippe Reynes 	diff1.base.link_mode_masks_nwords = 0;
180016032be5SNikolay Aleksandrov 
1801ebb6b4b1SPhilippe Reynes 	return !memcmp(&diff1.base, &diff2.base, sizeof(diff1.base)) &&
1802ebb6b4b1SPhilippe Reynes 		bitmap_empty(diff1.link_modes.supported,
1803ebb6b4b1SPhilippe Reynes 			     __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1804ebb6b4b1SPhilippe Reynes 		bitmap_empty(diff1.link_modes.advertising,
1805ebb6b4b1SPhilippe Reynes 			     __ETHTOOL_LINK_MODE_MASK_NBITS) &&
1806ebb6b4b1SPhilippe Reynes 		bitmap_empty(diff1.link_modes.lp_advertising,
1807ebb6b4b1SPhilippe Reynes 			     __ETHTOOL_LINK_MODE_MASK_NBITS);
180816032be5SNikolay Aleksandrov }
180916032be5SNikolay Aleksandrov 
1810ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev,
1811ebb6b4b1SPhilippe Reynes 				      const struct ethtool_link_ksettings *cmd)
181216032be5SNikolay Aleksandrov {
181316032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
181416032be5SNikolay Aleksandrov 	u32 speed;
181516032be5SNikolay Aleksandrov 
1816ebb6b4b1SPhilippe Reynes 	speed = cmd->base.speed;
181716032be5SNikolay Aleksandrov 	/* don't allow custom speed and duplex */
181816032be5SNikolay Aleksandrov 	if (!ethtool_validate_speed(speed) ||
1819ebb6b4b1SPhilippe Reynes 	    !ethtool_validate_duplex(cmd->base.duplex) ||
182016032be5SNikolay Aleksandrov 	    !virtnet_validate_ethtool_cmd(cmd))
182116032be5SNikolay Aleksandrov 		return -EINVAL;
182216032be5SNikolay Aleksandrov 	vi->speed = speed;
1823ebb6b4b1SPhilippe Reynes 	vi->duplex = cmd->base.duplex;
182416032be5SNikolay Aleksandrov 
182516032be5SNikolay Aleksandrov 	return 0;
182616032be5SNikolay Aleksandrov }
182716032be5SNikolay Aleksandrov 
1828ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev,
1829ebb6b4b1SPhilippe Reynes 				      struct ethtool_link_ksettings *cmd)
183016032be5SNikolay Aleksandrov {
183116032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
183216032be5SNikolay Aleksandrov 
1833ebb6b4b1SPhilippe Reynes 	cmd->base.speed = vi->speed;
1834ebb6b4b1SPhilippe Reynes 	cmd->base.duplex = vi->duplex;
1835ebb6b4b1SPhilippe Reynes 	cmd->base.port = PORT_OTHER;
183616032be5SNikolay Aleksandrov 
183716032be5SNikolay Aleksandrov 	return 0;
183816032be5SNikolay Aleksandrov }
183916032be5SNikolay Aleksandrov 
184016032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
184116032be5SNikolay Aleksandrov {
184216032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
184316032be5SNikolay Aleksandrov 
184416032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
184516032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
184616032be5SNikolay Aleksandrov }
184716032be5SNikolay Aleksandrov 
18480fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
184966846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
18509f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
18518f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
1852d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
1853d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
1854074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
1855ebb6b4b1SPhilippe Reynes 	.get_link_ksettings = virtnet_get_link_ksettings,
1856ebb6b4b1SPhilippe Reynes 	.set_link_ksettings = virtnet_set_link_ksettings,
1857a9ea3fc6SHerbert Xu };
1858a9ea3fc6SHerbert Xu 
18599fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev)
18609fe7bfceSJohn Fastabend {
18619fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
18629fe7bfceSJohn Fastabend 	int i;
18639fe7bfceSJohn Fastabend 
18649fe7bfceSJohn Fastabend 	/* Make sure no work handler is accessing the device */
18659fe7bfceSJohn Fastabend 	flush_work(&vi->config_work);
18669fe7bfceSJohn Fastabend 
18679fe7bfceSJohn Fastabend 	netif_device_detach(vi->dev);
1868713a98d9SJason Wang 	netif_tx_disable(vi->dev);
18699fe7bfceSJohn Fastabend 	cancel_delayed_work_sync(&vi->refill);
18709fe7bfceSJohn Fastabend 
18719fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
1872b92f1e67SWillem de Bruijn 		for (i = 0; i < vi->max_queue_pairs; i++) {
18739fe7bfceSJohn Fastabend 			napi_disable(&vi->rq[i].napi);
187478a57b48SWillem de Bruijn 			virtnet_napi_tx_disable(&vi->sq[i].napi);
1875b92f1e67SWillem de Bruijn 		}
18769fe7bfceSJohn Fastabend 	}
18779fe7bfceSJohn Fastabend }
18789fe7bfceSJohn Fastabend 
18799fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi);
18809fe7bfceSJohn Fastabend 
18819fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev)
18829fe7bfceSJohn Fastabend {
18839fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
18849fe7bfceSJohn Fastabend 	int err, i;
18859fe7bfceSJohn Fastabend 
18869fe7bfceSJohn Fastabend 	err = init_vqs(vi);
18879fe7bfceSJohn Fastabend 	if (err)
18889fe7bfceSJohn Fastabend 		return err;
18899fe7bfceSJohn Fastabend 
18909fe7bfceSJohn Fastabend 	virtio_device_ready(vdev);
18919fe7bfceSJohn Fastabend 
18929fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
18939fe7bfceSJohn Fastabend 		for (i = 0; i < vi->curr_queue_pairs; i++)
18949fe7bfceSJohn Fastabend 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
18959fe7bfceSJohn Fastabend 				schedule_delayed_work(&vi->refill, 0);
18969fe7bfceSJohn Fastabend 
1897b92f1e67SWillem de Bruijn 		for (i = 0; i < vi->max_queue_pairs; i++) {
1898e4e8452aSWillem de Bruijn 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1899b92f1e67SWillem de Bruijn 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
1900b92f1e67SWillem de Bruijn 					       &vi->sq[i].napi);
1901b92f1e67SWillem de Bruijn 		}
19029fe7bfceSJohn Fastabend 	}
19039fe7bfceSJohn Fastabend 
19049fe7bfceSJohn Fastabend 	netif_device_attach(vi->dev);
19059fe7bfceSJohn Fastabend 	return err;
19069fe7bfceSJohn Fastabend }
19079fe7bfceSJohn Fastabend 
1908*3f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
1909*3f93522fSJason Wang {
1910*3f93522fSJason Wang 	struct scatterlist sg;
1911*3f93522fSJason Wang 	vi->ctrl_offloads = cpu_to_virtio64(vi->vdev, offloads);
1912*3f93522fSJason Wang 
1913*3f93522fSJason Wang 	sg_init_one(&sg, &vi->ctrl_offloads, sizeof(vi->ctrl_offloads));
1914*3f93522fSJason Wang 
1915*3f93522fSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
1916*3f93522fSJason Wang 				  VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
1917*3f93522fSJason Wang 		dev_warn(&vi->dev->dev, "Fail to set guest offload. \n");
1918*3f93522fSJason Wang 		return -EINVAL;
1919*3f93522fSJason Wang 	}
1920*3f93522fSJason Wang 
1921*3f93522fSJason Wang 	return 0;
1922*3f93522fSJason Wang }
1923*3f93522fSJason Wang 
1924*3f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
1925*3f93522fSJason Wang {
1926*3f93522fSJason Wang 	u64 offloads = 0;
1927*3f93522fSJason Wang 
1928*3f93522fSJason Wang 	if (!vi->guest_offloads)
1929*3f93522fSJason Wang 		return 0;
1930*3f93522fSJason Wang 
1931*3f93522fSJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
1932*3f93522fSJason Wang 		offloads = 1ULL << VIRTIO_NET_F_GUEST_CSUM;
1933*3f93522fSJason Wang 
1934*3f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
1935*3f93522fSJason Wang }
1936*3f93522fSJason Wang 
1937*3f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
1938*3f93522fSJason Wang {
1939*3f93522fSJason Wang 	u64 offloads = vi->guest_offloads;
1940*3f93522fSJason Wang 
1941*3f93522fSJason Wang 	if (!vi->guest_offloads)
1942*3f93522fSJason Wang 		return 0;
1943*3f93522fSJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))
1944*3f93522fSJason Wang 		offloads |= 1ULL << VIRTIO_NET_F_GUEST_CSUM;
1945*3f93522fSJason Wang 
1946*3f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
1947*3f93522fSJason Wang }
1948*3f93522fSJason Wang 
19499861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
19509861ce03SJakub Kicinski 			   struct netlink_ext_ack *extack)
1951f600b690SJohn Fastabend {
1952f600b690SJohn Fastabend 	unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
1953f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
1954f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
1955017b29c3SJason Wang 	u16 xdp_qp = 0, curr_qp;
1956672aafd5SJohn Fastabend 	int i, err;
1957f600b690SJohn Fastabend 
1958*3f93522fSJason Wang 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
1959*3f93522fSJason Wang 	    && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
196092502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
196192502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
1962*3f93522fSJason Wang 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO))) {
19634d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO, disable LRO first");
1964f600b690SJohn Fastabend 		return -EOPNOTSUPP;
1965f600b690SJohn Fastabend 	}
1966f600b690SJohn Fastabend 
1967f600b690SJohn Fastabend 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
19684d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
1969f600b690SJohn Fastabend 		return -EINVAL;
1970f600b690SJohn Fastabend 	}
1971f600b690SJohn Fastabend 
1972f600b690SJohn Fastabend 	if (dev->mtu > max_sz) {
19734d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
1974f600b690SJohn Fastabend 		netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
1975f600b690SJohn Fastabend 		return -EINVAL;
1976f600b690SJohn Fastabend 	}
1977f600b690SJohn Fastabend 
1978672aafd5SJohn Fastabend 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
1979672aafd5SJohn Fastabend 	if (prog)
1980672aafd5SJohn Fastabend 		xdp_qp = nr_cpu_ids;
1981672aafd5SJohn Fastabend 
1982672aafd5SJohn Fastabend 	/* XDP requires extra queues for XDP_TX */
1983672aafd5SJohn Fastabend 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
19844d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available");
1985672aafd5SJohn Fastabend 		netdev_warn(dev, "request %i queues but max is %i\n",
1986672aafd5SJohn Fastabend 			    curr_qp + xdp_qp, vi->max_queue_pairs);
1987672aafd5SJohn Fastabend 		return -ENOMEM;
1988672aafd5SJohn Fastabend 	}
1989672aafd5SJohn Fastabend 
19902de2f7f4SJohn Fastabend 	if (prog) {
19912de2f7f4SJohn Fastabend 		prog = bpf_prog_add(prog, vi->max_queue_pairs - 1);
19922de2f7f4SJohn Fastabend 		if (IS_ERR(prog))
19932de2f7f4SJohn Fastabend 			return PTR_ERR(prog);
19942de2f7f4SJohn Fastabend 	}
19952de2f7f4SJohn Fastabend 
19964941d472SJason Wang 	/* Make sure NAPI is not using any XDP TX queues for RX. */
19974941d472SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
19984941d472SJason Wang 		napi_disable(&vi->rq[i].napi);
19992de2f7f4SJohn Fastabend 
2000672aafd5SJohn Fastabend 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
20014941d472SJason Wang 	err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
20024941d472SJason Wang 	if (err)
20034941d472SJason Wang 		goto err;
20044941d472SJason Wang 	vi->xdp_queue_pairs = xdp_qp;
2005f600b690SJohn Fastabend 
2006f600b690SJohn Fastabend 	for (i = 0; i < vi->max_queue_pairs; i++) {
2007f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2008f600b690SJohn Fastabend 		rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
2009*3f93522fSJason Wang 		if (i == 0) {
2010*3f93522fSJason Wang 			if (!old_prog)
2011*3f93522fSJason Wang 				virtnet_clear_guest_offloads(vi);
2012*3f93522fSJason Wang 			if (!prog)
2013*3f93522fSJason Wang 				virtnet_restore_guest_offloads(vi);
2014*3f93522fSJason Wang 		}
2015f600b690SJohn Fastabend 		if (old_prog)
2016f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
20174941d472SJason Wang 		virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2018f600b690SJohn Fastabend 	}
2019f600b690SJohn Fastabend 
2020f600b690SJohn Fastabend 	return 0;
20212de2f7f4SJohn Fastabend 
20224941d472SJason Wang err:
20234941d472SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++)
20244941d472SJason Wang 		virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
20252de2f7f4SJohn Fastabend 	if (prog)
20262de2f7f4SJohn Fastabend 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
20272de2f7f4SJohn Fastabend 	return err;
2028f600b690SJohn Fastabend }
2029f600b690SJohn Fastabend 
20305b0e6629SMartin KaFai Lau static u32 virtnet_xdp_query(struct net_device *dev)
2031f600b690SJohn Fastabend {
2032f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
20335b0e6629SMartin KaFai Lau 	const struct bpf_prog *xdp_prog;
2034f600b690SJohn Fastabend 	int i;
2035f600b690SJohn Fastabend 
2036f600b690SJohn Fastabend 	for (i = 0; i < vi->max_queue_pairs; i++) {
20375b0e6629SMartin KaFai Lau 		xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
20385b0e6629SMartin KaFai Lau 		if (xdp_prog)
20395b0e6629SMartin KaFai Lau 			return xdp_prog->aux->id;
2040f600b690SJohn Fastabend 	}
20415b0e6629SMartin KaFai Lau 	return 0;
2042f600b690SJohn Fastabend }
2043f600b690SJohn Fastabend 
2044f600b690SJohn Fastabend static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
2045f600b690SJohn Fastabend {
2046f600b690SJohn Fastabend 	switch (xdp->command) {
2047f600b690SJohn Fastabend 	case XDP_SETUP_PROG:
20489861ce03SJakub Kicinski 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2049f600b690SJohn Fastabend 	case XDP_QUERY_PROG:
20505b0e6629SMartin KaFai Lau 		xdp->prog_id = virtnet_xdp_query(dev);
20515b0e6629SMartin KaFai Lau 		xdp->prog_attached = !!xdp->prog_id;
2052f600b690SJohn Fastabend 		return 0;
2053f600b690SJohn Fastabend 	default:
2054f600b690SJohn Fastabend 		return -EINVAL;
2055f600b690SJohn Fastabend 	}
2056f600b690SJohn Fastabend }
2057f600b690SJohn Fastabend 
205876288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
205976288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
206076288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
206176288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
206276288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
20639c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
20642af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
20653fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
20661824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
20671824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
206876288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
206976288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
207076288b4eSStephen Hemminger #endif
2071f600b690SJohn Fastabend 	.ndo_xdp		= virtnet_xdp,
20722836b4f2SVlad Yasevich 	.ndo_features_check	= passthru_features_check,
207376288b4eSStephen Hemminger };
207476288b4eSStephen Hemminger 
2075586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
20769f4d26d0SMark McLoughlin {
2077586d17c5SJason Wang 	struct virtnet_info *vi =
2078586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
20799f4d26d0SMark McLoughlin 	u16 v;
20809f4d26d0SMark McLoughlin 
2081855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2082855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
2083507613bfSMichael S. Tsirkin 		return;
2084586d17c5SJason Wang 
2085586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
2086ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
2087586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
2088586d17c5SJason Wang 	}
20899f4d26d0SMark McLoughlin 
20909f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
20919f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
20929f4d26d0SMark McLoughlin 
20939f4d26d0SMark McLoughlin 	if (vi->status == v)
2094507613bfSMichael S. Tsirkin 		return;
20959f4d26d0SMark McLoughlin 
20969f4d26d0SMark McLoughlin 	vi->status = v;
20979f4d26d0SMark McLoughlin 
20989f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
20999f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
2100986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
21019f4d26d0SMark McLoughlin 	} else {
21029f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
2103986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
21049f4d26d0SMark McLoughlin 	}
21059f4d26d0SMark McLoughlin }
21069f4d26d0SMark McLoughlin 
21079f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
21089f4d26d0SMark McLoughlin {
21099f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
21109f4d26d0SMark McLoughlin 
21113b07e9caSTejun Heo 	schedule_work(&vi->config_work);
21129f4d26d0SMark McLoughlin }
21139f4d26d0SMark McLoughlin 
2114986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
2115986a4f4dSJason Wang {
2116d4fb84eeSAndrey Vagin 	int i;
2117d4fb84eeSAndrey Vagin 
2118ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2119ab3971b1SJason Wang 		napi_hash_del(&vi->rq[i].napi);
2120d4fb84eeSAndrey Vagin 		netif_napi_del(&vi->rq[i].napi);
2121b92f1e67SWillem de Bruijn 		netif_napi_del(&vi->sq[i].napi);
2122ab3971b1SJason Wang 	}
2123d4fb84eeSAndrey Vagin 
2124963abe5cSEric Dumazet 	/* We called napi_hash_del() before netif_napi_del(),
2125963abe5cSEric Dumazet 	 * we need to respect an RCU grace period before freeing vi->rq
2126963abe5cSEric Dumazet 	 */
2127963abe5cSEric Dumazet 	synchronize_net();
2128963abe5cSEric Dumazet 
2129986a4f4dSJason Wang 	kfree(vi->rq);
2130986a4f4dSJason Wang 	kfree(vi->sq);
2131986a4f4dSJason Wang }
2132986a4f4dSJason Wang 
213347315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi)
2134986a4f4dSJason Wang {
2135f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
2136986a4f4dSJason Wang 	int i;
2137986a4f4dSJason Wang 
2138986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2139986a4f4dSJason Wang 		while (vi->rq[i].pages)
2140986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
2141f600b690SJohn Fastabend 
2142f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2143f600b690SJohn Fastabend 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2144f600b690SJohn Fastabend 		if (old_prog)
2145f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
2146986a4f4dSJason Wang 	}
214747315329SJohn Fastabend }
214847315329SJohn Fastabend 
214947315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi)
215047315329SJohn Fastabend {
215147315329SJohn Fastabend 	rtnl_lock();
215247315329SJohn Fastabend 	_free_receive_bufs(vi);
2153f600b690SJohn Fastabend 	rtnl_unlock();
2154986a4f4dSJason Wang }
2155986a4f4dSJason Wang 
2156fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
2157fb51879dSMichael Dalton {
2158fb51879dSMichael Dalton 	int i;
2159fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
2160fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
2161fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
2162fb51879dSMichael Dalton }
2163fb51879dSMichael Dalton 
2164b68df015SJohn Fastabend static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
216556434a01SJohn Fastabend {
216656434a01SJohn Fastabend 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
216756434a01SJohn Fastabend 		return false;
216856434a01SJohn Fastabend 	else if (q < vi->curr_queue_pairs)
216956434a01SJohn Fastabend 		return true;
217056434a01SJohn Fastabend 	else
217156434a01SJohn Fastabend 		return false;
217256434a01SJohn Fastabend }
217356434a01SJohn Fastabend 
2174986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
2175986a4f4dSJason Wang {
2176986a4f4dSJason Wang 	void *buf;
2177986a4f4dSJason Wang 	int i;
2178986a4f4dSJason Wang 
2179986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2180986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
218156434a01SJohn Fastabend 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2182b68df015SJohn Fastabend 			if (!is_xdp_raw_buffer_queue(vi, i))
2183986a4f4dSJason Wang 				dev_kfree_skb(buf);
218456434a01SJohn Fastabend 			else
218556434a01SJohn Fastabend 				put_page(virt_to_head_page(buf));
218656434a01SJohn Fastabend 		}
2187986a4f4dSJason Wang 	}
2188986a4f4dSJason Wang 
2189986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2190986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
2191986a4f4dSJason Wang 
2192986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2193ab7db917SMichael Dalton 			if (vi->mergeable_rx_bufs) {
2194680557cfSMichael S. Tsirkin 				put_page(virt_to_head_page(buf));
2195ab7db917SMichael Dalton 			} else if (vi->big_packets) {
2196fa9fac17SAndrey Vagin 				give_pages(&vi->rq[i], buf);
2197ab7db917SMichael Dalton 			} else {
2198f6b10209SJason Wang 				put_page(virt_to_head_page(buf));
2199986a4f4dSJason Wang 			}
2200986a4f4dSJason Wang 		}
2201986a4f4dSJason Wang 	}
2202ab7db917SMichael Dalton }
2203986a4f4dSJason Wang 
2204e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
2205e9d7417bSJason Wang {
2206e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
2207e9d7417bSJason Wang 
22088898c21cSWanlong Gao 	virtnet_clean_affinity(vi, -1);
2209986a4f4dSJason Wang 
2210e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
2211986a4f4dSJason Wang 
2212986a4f4dSJason Wang 	virtnet_free_queues(vi);
2213986a4f4dSJason Wang }
2214986a4f4dSJason Wang 
2215d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at
2216d85b758fSMichael S. Tsirkin  * least one full packet?
2217d85b758fSMichael S. Tsirkin  * Logic below assumes the mergeable buffer header is used.
2218d85b758fSMichael S. Tsirkin  */
2219d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2220d85b758fSMichael S. Tsirkin {
2221d85b758fSMichael S. Tsirkin 	const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2222d85b758fSMichael S. Tsirkin 	unsigned int rq_size = virtqueue_get_vring_size(vq);
2223d85b758fSMichael S. Tsirkin 	unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2224d85b758fSMichael S. Tsirkin 	unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2225d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2226d85b758fSMichael S. Tsirkin 
2227f0c3192cSMichael S. Tsirkin 	return max(max(min_buf_len, hdr_len) - hdr_len,
2228f0c3192cSMichael S. Tsirkin 		   (unsigned int)GOOD_PACKET_LEN);
2229d85b758fSMichael S. Tsirkin }
2230d85b758fSMichael S. Tsirkin 
2231986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
2232986a4f4dSJason Wang {
2233986a4f4dSJason Wang 	vq_callback_t **callbacks;
2234986a4f4dSJason Wang 	struct virtqueue **vqs;
2235986a4f4dSJason Wang 	int ret = -ENOMEM;
2236986a4f4dSJason Wang 	int i, total_vqs;
2237986a4f4dSJason Wang 	const char **names;
2238d45b897bSMichael S. Tsirkin 	bool *ctx;
2239986a4f4dSJason Wang 
2240986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2241986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2242986a4f4dSJason Wang 	 * possible control vq.
2243986a4f4dSJason Wang 	 */
2244986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
2245986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2246986a4f4dSJason Wang 
2247986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
2248986a4f4dSJason Wang 	vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
2249986a4f4dSJason Wang 	if (!vqs)
2250986a4f4dSJason Wang 		goto err_vq;
2251986a4f4dSJason Wang 	callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
2252986a4f4dSJason Wang 	if (!callbacks)
2253986a4f4dSJason Wang 		goto err_callback;
2254986a4f4dSJason Wang 	names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
2255986a4f4dSJason Wang 	if (!names)
2256986a4f4dSJason Wang 		goto err_names;
2257192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
2258d45b897bSMichael S. Tsirkin 		ctx = kzalloc(total_vqs * sizeof(*ctx), GFP_KERNEL);
2259d45b897bSMichael S. Tsirkin 		if (!ctx)
2260d45b897bSMichael S. Tsirkin 			goto err_ctx;
2261d45b897bSMichael S. Tsirkin 	} else {
2262d45b897bSMichael S. Tsirkin 		ctx = NULL;
2263d45b897bSMichael S. Tsirkin 	}
2264986a4f4dSJason Wang 
2265986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
2266986a4f4dSJason Wang 	if (vi->has_cvq) {
2267986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
2268986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
2269986a4f4dSJason Wang 	}
2270986a4f4dSJason Wang 
2271986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
2272986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2273986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
2274986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
2275986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
2276986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
2277986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
2278986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
2279d45b897bSMichael S. Tsirkin 		if (ctx)
2280d45b897bSMichael S. Tsirkin 			ctx[rxq2vq(i)] = true;
2281986a4f4dSJason Wang 	}
2282986a4f4dSJason Wang 
2283986a4f4dSJason Wang 	ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
2284d45b897bSMichael S. Tsirkin 					 names, ctx, NULL);
2285986a4f4dSJason Wang 	if (ret)
2286986a4f4dSJason Wang 		goto err_find;
2287986a4f4dSJason Wang 
2288986a4f4dSJason Wang 	if (vi->has_cvq) {
2289986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
2290986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2291f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2292986a4f4dSJason Wang 	}
2293986a4f4dSJason Wang 
2294986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2295986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
2296d85b758fSMichael S. Tsirkin 		vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
2297986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
2298986a4f4dSJason Wang 	}
2299986a4f4dSJason Wang 
2300986a4f4dSJason Wang 	kfree(names);
2301986a4f4dSJason Wang 	kfree(callbacks);
2302986a4f4dSJason Wang 	kfree(vqs);
230355281621SJason Wang 	kfree(ctx);
2304986a4f4dSJason Wang 
2305986a4f4dSJason Wang 	return 0;
2306986a4f4dSJason Wang 
2307986a4f4dSJason Wang err_find:
2308d45b897bSMichael S. Tsirkin 	kfree(ctx);
2309d45b897bSMichael S. Tsirkin err_ctx:
2310986a4f4dSJason Wang 	kfree(names);
2311986a4f4dSJason Wang err_names:
2312986a4f4dSJason Wang 	kfree(callbacks);
2313986a4f4dSJason Wang err_callback:
2314986a4f4dSJason Wang 	kfree(vqs);
2315986a4f4dSJason Wang err_vq:
2316986a4f4dSJason Wang 	return ret;
2317986a4f4dSJason Wang }
2318986a4f4dSJason Wang 
2319986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
2320986a4f4dSJason Wang {
2321986a4f4dSJason Wang 	int i;
2322986a4f4dSJason Wang 
2323986a4f4dSJason Wang 	vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2324986a4f4dSJason Wang 	if (!vi->sq)
2325986a4f4dSJason Wang 		goto err_sq;
2326986a4f4dSJason Wang 	vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
2327008d4278SAmerigo Wang 	if (!vi->rq)
2328986a4f4dSJason Wang 		goto err_rq;
2329986a4f4dSJason Wang 
2330986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
2331986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2332986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
2333986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2334986a4f4dSJason Wang 			       napi_weight);
23351d11e732SWillem de Bruijn 		netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2336b92f1e67SWillem de Bruijn 				  napi_tx ? napi_weight : 0);
2337986a4f4dSJason Wang 
2338986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
23395377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2340986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2341986a4f4dSJason Wang 	}
2342986a4f4dSJason Wang 
2343986a4f4dSJason Wang 	return 0;
2344986a4f4dSJason Wang 
2345986a4f4dSJason Wang err_rq:
2346986a4f4dSJason Wang 	kfree(vi->sq);
2347986a4f4dSJason Wang err_sq:
2348986a4f4dSJason Wang 	return -ENOMEM;
2349e9d7417bSJason Wang }
2350e9d7417bSJason Wang 
23513f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
23523f9c10b0SAmit Shah {
2353986a4f4dSJason Wang 	int ret;
23543f9c10b0SAmit Shah 
2355986a4f4dSJason Wang 	/* Allocate send & receive queues */
2356986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
2357986a4f4dSJason Wang 	if (ret)
2358986a4f4dSJason Wang 		goto err;
23593f9c10b0SAmit Shah 
2360986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
2361986a4f4dSJason Wang 	if (ret)
2362986a4f4dSJason Wang 		goto err_free;
23633f9c10b0SAmit Shah 
236447be2479SWanlong Gao 	get_online_cpus();
23658898c21cSWanlong Gao 	virtnet_set_affinity(vi);
236647be2479SWanlong Gao 	put_online_cpus();
236747be2479SWanlong Gao 
23683f9c10b0SAmit Shah 	return 0;
2369986a4f4dSJason Wang 
2370986a4f4dSJason Wang err_free:
2371986a4f4dSJason Wang 	virtnet_free_queues(vi);
2372986a4f4dSJason Wang err:
2373986a4f4dSJason Wang 	return ret;
23743f9c10b0SAmit Shah }
23753f9c10b0SAmit Shah 
2376fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
2377fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2378fbf28d78SMichael Dalton 		struct rx_queue_attribute *attribute, char *buf)
2379fbf28d78SMichael Dalton {
2380fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
2381fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
23825377d758SJohannes Berg 	struct ewma_pkt_len *avg;
2383fbf28d78SMichael Dalton 
2384fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
2385fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2386d85b758fSMichael S. Tsirkin 	return sprintf(buf, "%u\n",
2387d85b758fSMichael S. Tsirkin 		       get_mergeable_buf_len(&vi->rq[queue_index], avg));
2388fbf28d78SMichael Dalton }
2389fbf28d78SMichael Dalton 
2390fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2391fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
2392fbf28d78SMichael Dalton 
2393fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
2394fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
2395fbf28d78SMichael Dalton 	NULL
2396fbf28d78SMichael Dalton };
2397fbf28d78SMichael Dalton 
2398fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
2399fbf28d78SMichael Dalton 	.name = "virtio_net",
2400fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
2401fbf28d78SMichael Dalton };
2402fbf28d78SMichael Dalton #endif
2403fbf28d78SMichael Dalton 
2404892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2405892d6eb1SJason Wang 				    unsigned int fbit,
2406892d6eb1SJason Wang 				    const char *fname, const char *dname)
2407892d6eb1SJason Wang {
2408892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
2409892d6eb1SJason Wang 		return false;
2410892d6eb1SJason Wang 
2411892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
2412892d6eb1SJason Wang 		fname, dname);
2413892d6eb1SJason Wang 
2414892d6eb1SJason Wang 	return true;
2415892d6eb1SJason Wang }
2416892d6eb1SJason Wang 
2417892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
2418892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2419892d6eb1SJason Wang 
2420892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
2421892d6eb1SJason Wang {
2422892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
2423892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
2424892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
2425892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
2426892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
2427892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
2428892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
2429892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
2430892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
2431892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
2432892d6eb1SJason Wang 		return false;
2433892d6eb1SJason Wang 	}
2434892d6eb1SJason Wang 
2435892d6eb1SJason Wang 	return true;
2436892d6eb1SJason Wang }
2437892d6eb1SJason Wang 
2438d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
2439d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
2440d0c2c997SJarod Wilson 
2441fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev)
2442296f96fcSRusty Russell {
24436ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
24446ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
24456ba42248SMichael S. Tsirkin 			__func__);
24466ba42248SMichael S. Tsirkin 		return -EINVAL;
24476ba42248SMichael S. Tsirkin 	}
24486ba42248SMichael S. Tsirkin 
2449892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
2450892d6eb1SJason Wang 		return -EINVAL;
2451892d6eb1SJason Wang 
2452fe36cbe0SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
2453fe36cbe0SMichael S. Tsirkin 		int mtu = virtio_cread16(vdev,
2454fe36cbe0SMichael S. Tsirkin 					 offsetof(struct virtio_net_config,
2455fe36cbe0SMichael S. Tsirkin 						  mtu));
2456fe36cbe0SMichael S. Tsirkin 		if (mtu < MIN_MTU)
2457fe36cbe0SMichael S. Tsirkin 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
2458fe36cbe0SMichael S. Tsirkin 	}
2459fe36cbe0SMichael S. Tsirkin 
2460fe36cbe0SMichael S. Tsirkin 	return 0;
2461fe36cbe0SMichael S. Tsirkin }
2462fe36cbe0SMichael S. Tsirkin 
2463fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev)
2464fe36cbe0SMichael S. Tsirkin {
2465fe36cbe0SMichael S. Tsirkin 	int i, err;
2466fe36cbe0SMichael S. Tsirkin 	struct net_device *dev;
2467fe36cbe0SMichael S. Tsirkin 	struct virtnet_info *vi;
2468fe36cbe0SMichael S. Tsirkin 	u16 max_queue_pairs;
2469fe36cbe0SMichael S. Tsirkin 	int mtu;
2470fe36cbe0SMichael S. Tsirkin 
2471986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
2472855e0c52SRusty Russell 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
2473855e0c52SRusty Russell 				   struct virtio_net_config,
2474855e0c52SRusty Russell 				   max_virtqueue_pairs, &max_queue_pairs);
2475986a4f4dSJason Wang 
2476986a4f4dSJason Wang 	/* We need at least 2 queue's */
2477986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
2478986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
2479986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2480986a4f4dSJason Wang 		max_queue_pairs = 1;
2481296f96fcSRusty Russell 
2482296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
2483986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
2484296f96fcSRusty Russell 	if (!dev)
2485296f96fcSRusty Russell 		return -ENOMEM;
2486296f96fcSRusty Russell 
2487296f96fcSRusty Russell 	/* Set up network device as normal. */
2488f2f2c8b4SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
248976288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
2490296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
24913fa2a1dfSstephen hemminger 
24927ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
2493296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
2494296f96fcSRusty Russell 
2495296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
249698e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
2497296f96fcSRusty Russell 		/* This opens up the world of extra features. */
249848900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
249998e778c9SMichał Mirosław 		if (csum)
250048900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
250198e778c9SMichał Mirosław 
250298e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
2503e078de03SDavid S. Miller 			dev->hw_features |= NETIF_F_TSO
250434a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
250534a48579SRusty Russell 		}
25065539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
250798e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
250898e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
250998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
251098e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
251198e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
251298e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
251398e778c9SMichał Mirosław 
251441f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
251541f2f127SJason Wang 
251698e778c9SMichał Mirosław 		if (gso)
2517e078de03SDavid S. Miller 			dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
251898e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
2519296f96fcSRusty Russell 	}
25204f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
25214f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
2522296f96fcSRusty Russell 
25234fda8302SJason Wang 	dev->vlan_features = dev->features;
25244fda8302SJason Wang 
2525d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
2526d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
2527d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
2528d0c2c997SJarod Wilson 
2529296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
2530855e0c52SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
2531855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
2532a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
2533855e0c52SRusty Russell 				   dev->dev_addr, dev->addr_len);
2534855e0c52SRusty Russell 	else
2535f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
2536296f96fcSRusty Russell 
2537296f96fcSRusty Russell 	/* Set up our device-specific information */
2538296f96fcSRusty Russell 	vi = netdev_priv(dev);
2539296f96fcSRusty Russell 	vi->dev = dev;
2540296f96fcSRusty Russell 	vi->vdev = vdev;
2541d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
25423fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
25433fa2a1dfSstephen hemminger 	err = -ENOMEM;
25443fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
25453fa2a1dfSstephen hemminger 		goto free;
25463fa2a1dfSstephen hemminger 
2547827da44cSJohn Stultz 	for_each_possible_cpu(i) {
2548827da44cSJohn Stultz 		struct virtnet_stats *virtnet_stats;
2549827da44cSJohn Stultz 		virtnet_stats = per_cpu_ptr(vi->stats, i);
2550827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->tx_syncp);
2551827da44cSJohn Stultz 		u64_stats_init(&virtnet_stats->rx_syncp);
2552827da44cSJohn Stultz 	}
2553827da44cSJohn Stultz 
2554586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
2555296f96fcSRusty Russell 
255697402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
25578e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
25588e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
2559e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
2560e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
256197402b96SHerbert Xu 		vi->big_packets = true;
256297402b96SHerbert Xu 
25633f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
25643f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
25653f2c31d9SMark McLoughlin 
2566d04302b3SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
2567d04302b3SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2568012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2569012873d0SMichael S. Tsirkin 	else
2570012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
2571012873d0SMichael S. Tsirkin 
257275993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
257375993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
2574e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
2575e7428e95SMichael S. Tsirkin 
2576986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
2577986a4f4dSJason Wang 		vi->has_cvq = true;
2578986a4f4dSJason Wang 
257914de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
258014de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
258114de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
258214de9d11SAaron Conole 					      mtu));
258393a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
2584fe36cbe0SMichael S. Tsirkin 			/* Should never trigger: MTU was previously validated
2585fe36cbe0SMichael S. Tsirkin 			 * in virtnet_validate.
2586fe36cbe0SMichael S. Tsirkin 			 */
2587fe36cbe0SMichael S. Tsirkin 			dev_err(&vdev->dev, "device MTU appears to have changed "
2588fe36cbe0SMichael S. Tsirkin 				"it is now %d < %d", mtu, dev->min_mtu);
2589fe36cbe0SMichael S. Tsirkin 			goto free_stats;
2590fe36cbe0SMichael S. Tsirkin 		}
2591fe36cbe0SMichael S. Tsirkin 
2592d0c2c997SJarod Wilson 		dev->mtu = mtu;
259393a205eeSAaron Conole 		dev->max_mtu = mtu;
25942e123b44SMichael S. Tsirkin 
25952e123b44SMichael S. Tsirkin 		/* TODO: size buffers correctly in this case. */
25962e123b44SMichael S. Tsirkin 		if (dev->mtu > ETH_DATA_LEN)
25972e123b44SMichael S. Tsirkin 			vi->big_packets = true;
259814de9d11SAaron Conole 	}
259914de9d11SAaron Conole 
2600012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
2601012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
26026ebbc1a6SZhangjie \(HZ\) 
260344900010SJason Wang 	/* Enable multiqueue by default */
260444900010SJason Wang 	if (num_online_cpus() >= max_queue_pairs)
260544900010SJason Wang 		vi->curr_queue_pairs = max_queue_pairs;
260644900010SJason Wang 	else
260744900010SJason Wang 		vi->curr_queue_pairs = num_online_cpus();
2608986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
2609986a4f4dSJason Wang 
2610986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
26113f9c10b0SAmit Shah 	err = init_vqs(vi);
2612d2a7dddaSMichael S. Tsirkin 	if (err)
26139bb8ca86SJason Wang 		goto free_stats;
2614d2a7dddaSMichael S. Tsirkin 
2615fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
2616fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
2617fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
2618fbf28d78SMichael Dalton #endif
26190f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
26200f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
2621986a4f4dSJason Wang 
262216032be5SNikolay Aleksandrov 	virtnet_init_settings(dev);
262316032be5SNikolay Aleksandrov 
2624296f96fcSRusty Russell 	err = register_netdev(dev);
2625296f96fcSRusty Russell 	if (err) {
2626296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
2627d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
2628296f96fcSRusty Russell 	}
2629b3369c1fSRusty Russell 
26304baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
26314baf1e33SMichael S. Tsirkin 
26328017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
26338de4b2f3SWanlong Gao 	if (err) {
26348de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
2635f00e35e2Swangyunjian 		goto free_unregister_netdev;
26368de4b2f3SWanlong Gao 	}
26378de4b2f3SWanlong Gao 
2638a220871bSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
263944900010SJason Wang 
2640167c25e4SJason Wang 	/* Assume link up if device can't report link status,
2641167c25e4SJason Wang 	   otherwise get link status from config. */
2642167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
2643167c25e4SJason Wang 		netif_carrier_off(dev);
26443b07e9caSTejun Heo 		schedule_work(&vi->config_work);
2645167c25e4SJason Wang 	} else {
2646167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
26474783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
2648167c25e4SJason Wang 	}
26499f4d26d0SMark McLoughlin 
2650*3f93522fSJason Wang 	for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
2651*3f93522fSJason Wang 		if (virtio_has_feature(vi->vdev, guest_offloads[i]))
2652*3f93522fSJason Wang 			set_bit(guest_offloads[i], &vi->guest_offloads);
2653*3f93522fSJason Wang 
2654986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
2655986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
2656986a4f4dSJason Wang 
2657296f96fcSRusty Russell 	return 0;
2658296f96fcSRusty Russell 
2659f00e35e2Swangyunjian free_unregister_netdev:
266002465555SMichael S. Tsirkin 	vi->vdev->config->reset(vdev);
266102465555SMichael S. Tsirkin 
2662b3369c1fSRusty Russell 	unregister_netdev(dev);
2663d2a7dddaSMichael S. Tsirkin free_vqs:
2664986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
2665fb51879dSMichael Dalton 	free_receive_page_frags(vi);
2666e9d7417bSJason Wang 	virtnet_del_vqs(vi);
26673fa2a1dfSstephen hemminger free_stats:
26683fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
2669296f96fcSRusty Russell free:
2670296f96fcSRusty Russell 	free_netdev(dev);
2671296f96fcSRusty Russell 	return err;
2672296f96fcSRusty Russell }
2673296f96fcSRusty Russell 
267404486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
2675296f96fcSRusty Russell {
267604486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
2677830a8a97SShirley Ma 
2678830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
26799ab86bbcSShirley Ma 	free_unused_bufs(vi);
2680fb6813f4SRusty Russell 
2681986a4f4dSJason Wang 	free_receive_bufs(vi);
2682d2a7dddaSMichael S. Tsirkin 
2683fb51879dSMichael Dalton 	free_receive_page_frags(vi);
2684fb51879dSMichael Dalton 
2685986a4f4dSJason Wang 	virtnet_del_vqs(vi);
268604486ed0SAmit Shah }
268704486ed0SAmit Shah 
26888cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
268904486ed0SAmit Shah {
269004486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
269104486ed0SAmit Shah 
26928017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
26938de4b2f3SWanlong Gao 
2694102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
2695102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
2696586d17c5SJason Wang 
269704486ed0SAmit Shah 	unregister_netdev(vi->dev);
269804486ed0SAmit Shah 
269904486ed0SAmit Shah 	remove_vq_common(vi);
2700fb6813f4SRusty Russell 
27012e66f55bSKrishna Kumar 	free_percpu(vi->stats);
270274b2553fSRusty Russell 	free_netdev(vi->dev);
2703296f96fcSRusty Russell }
2704296f96fcSRusty Russell 
270589107000SAaron Lu #ifdef CONFIG_PM_SLEEP
27060741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev)
27070741bcb5SAmit Shah {
27080741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
27090741bcb5SAmit Shah 
27108017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
27119fe7bfceSJohn Fastabend 	virtnet_freeze_down(vdev);
27120741bcb5SAmit Shah 	remove_vq_common(vi);
27130741bcb5SAmit Shah 
27140741bcb5SAmit Shah 	return 0;
27150741bcb5SAmit Shah }
27160741bcb5SAmit Shah 
27170741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev)
27180741bcb5SAmit Shah {
27190741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
27209fe7bfceSJohn Fastabend 	int err;
27210741bcb5SAmit Shah 
27229fe7bfceSJohn Fastabend 	err = virtnet_restore_up(vdev);
27230741bcb5SAmit Shah 	if (err)
27240741bcb5SAmit Shah 		return err;
2725986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
2726986a4f4dSJason Wang 
27278017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
2728ec9debbdSJason Wang 	if (err)
2729ec9debbdSJason Wang 		return err;
2730ec9debbdSJason Wang 
27310741bcb5SAmit Shah 	return 0;
27320741bcb5SAmit Shah }
27330741bcb5SAmit Shah #endif
27340741bcb5SAmit Shah 
2735296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
2736296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
2737296f96fcSRusty Russell 	{ 0 },
2738296f96fcSRusty Russell };
2739296f96fcSRusty Russell 
2740f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \
2741f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
2742f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MAC, \
2743f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
2744f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
2745f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
2746f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
2747f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
2748f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
2749f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
2750*3f93522fSJason Wang 	VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
2751f3358507SMichael S. Tsirkin 
2752c45a6816SRusty Russell static unsigned int features[] = {
2753f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
2754f3358507SMichael S. Tsirkin };
2755f3358507SMichael S. Tsirkin 
2756f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = {
2757f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
2758f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GSO,
2759e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
2760c45a6816SRusty Russell };
2761c45a6816SRusty Russell 
276222402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
2763c45a6816SRusty Russell 	.feature_table = features,
2764c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
2765f3358507SMichael S. Tsirkin 	.feature_table_legacy = features_legacy,
2766f3358507SMichael S. Tsirkin 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
2767296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
2768296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
2769296f96fcSRusty Russell 	.id_table =	id_table,
2770fe36cbe0SMichael S. Tsirkin 	.validate =	virtnet_validate,
2771296f96fcSRusty Russell 	.probe =	virtnet_probe,
27728cc085d6SBill Pemberton 	.remove =	virtnet_remove,
27739f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
277489107000SAaron Lu #ifdef CONFIG_PM_SLEEP
27750741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
27760741bcb5SAmit Shah 	.restore =	virtnet_restore,
27770741bcb5SAmit Shah #endif
2778296f96fcSRusty Russell };
2779296f96fcSRusty Russell 
27808017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
27818017c279SSebastian Andrzej Siewior {
27828017c279SSebastian Andrzej Siewior 	int ret;
27838017c279SSebastian Andrzej Siewior 
278473c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
27858017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
27868017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
27878017c279SSebastian Andrzej Siewior 	if (ret < 0)
27888017c279SSebastian Andrzej Siewior 		goto out;
27898017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
279073c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
27918017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
27928017c279SSebastian Andrzej Siewior 	if (ret)
27938017c279SSebastian Andrzej Siewior 		goto err_dead;
27948017c279SSebastian Andrzej Siewior 
27958017c279SSebastian Andrzej Siewior         ret = register_virtio_driver(&virtio_net_driver);
27968017c279SSebastian Andrzej Siewior 	if (ret)
27978017c279SSebastian Andrzej Siewior 		goto err_virtio;
27988017c279SSebastian Andrzej Siewior 	return 0;
27998017c279SSebastian Andrzej Siewior err_virtio:
28008017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
28018017c279SSebastian Andrzej Siewior err_dead:
28028017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
28038017c279SSebastian Andrzej Siewior out:
28048017c279SSebastian Andrzej Siewior 	return ret;
28058017c279SSebastian Andrzej Siewior }
28068017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
28078017c279SSebastian Andrzej Siewior 
28088017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
28098017c279SSebastian Andrzej Siewior {
28108017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
28118017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
28128017c279SSebastian Andrzej Siewior 	unregister_virtio_driver(&virtio_net_driver);
28138017c279SSebastian Andrzej Siewior }
28148017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
2815296f96fcSRusty Russell 
2816296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
2817296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
2818296f96fcSRusty Russell MODULE_LICENSE("GPL");
2819