xref: /openbmc/linux/drivers/net/virtio_net.c (revision 3f2869cace829fb4b80fc53b3ddaa7f4ba9acbf1)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
248925e37SRusty Russell /* A network driver using virtio.
3296f96fcSRusty Russell  *
4296f96fcSRusty Russell  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5296f96fcSRusty Russell  */
6296f96fcSRusty Russell //#define DEBUG
7296f96fcSRusty Russell #include <linux/netdevice.h>
8296f96fcSRusty Russell #include <linux/etherdevice.h>
9a9ea3fc6SHerbert Xu #include <linux/ethtool.h>
10296f96fcSRusty Russell #include <linux/module.h>
11296f96fcSRusty Russell #include <linux/virtio.h>
12296f96fcSRusty Russell #include <linux/virtio_net.h>
13f600b690SJohn Fastabend #include <linux/bpf.h>
14a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h>
15296f96fcSRusty Russell #include <linux/scatterlist.h>
16e918085aSAlex Williamson #include <linux/if_vlan.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
188de4b2f3SWanlong Gao #include <linux/cpu.h>
19ab7db917SMichael Dalton #include <linux/average.h>
20186b3c99SJason Wang #include <linux/filter.h>
212ca653d6SCaleb Raitto #include <linux/kernel.h>
22d85b758fSMichael S. Tsirkin #include <net/route.h>
23754b8a21SJesper Dangaard Brouer #include <net/xdp.h>
24ba5e4426SSridhar Samudrala #include <net/net_failover.h>
25296f96fcSRusty Russell 
26d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
276c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
286c0cd7c0SDor Laor 
2931c03aefSWillem de Bruijn static bool csum = true, gso = true, napi_tx = true;
3034a48579SRusty Russell module_param(csum, bool, 0444);
3134a48579SRusty Russell module_param(gso, bool, 0444);
32b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644);
3334a48579SRusty Russell 
34296f96fcSRusty Russell /* FIXME: MTU in config. */
355061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
363f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
37296f96fcSRusty Russell 
38f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
39f6b10209SJason Wang 
402de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
412de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256
422de2f7f4SJohn Fastabend 
432471c75eSJesper Dangaard Brouer /* Separating two types of XDP xmit */
442471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_TX		BIT(0)
452471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_REDIR	BIT(1)
462471c75eSJesper Dangaard Brouer 
475050471dSToshiaki Makita #define VIRTIO_XDP_FLAG	BIT(0)
485050471dSToshiaki Makita 
495377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet
505377d758SJohannes Berg  * buffer size when refilling RX rings. As the entire RX ring may be refilled
515377d758SJohannes Berg  * at once, the weight is chosen so that the EWMA will be insensitive to short-
525377d758SJohannes Berg  * term, transient changes in packet size.
53ab7db917SMichael Dalton  */
54eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64)
55ab7db917SMichael Dalton 
5666846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
572a41f71dSAlex Williamson 
587acd4329SColin Ian King static const unsigned long guest_offloads[] = {
597acd4329SColin Ian King 	VIRTIO_NET_F_GUEST_TSO4,
603f93522fSJason Wang 	VIRTIO_NET_F_GUEST_TSO6,
613f93522fSJason Wang 	VIRTIO_NET_F_GUEST_ECN,
62e59ff2c4SJason Wang 	VIRTIO_NET_F_GUEST_UFO,
63e59ff2c4SJason Wang 	VIRTIO_NET_F_GUEST_CSUM
647acd4329SColin Ian King };
653f93522fSJason Wang 
661a03b8a3STonghao Zhang #define GUEST_OFFLOAD_LRO_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
671a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
681a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_ECN)  | \
691a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_UFO))
701a03b8a3STonghao Zhang 
71d7dfc5cfSToshiaki Makita struct virtnet_stat_desc {
72d7dfc5cfSToshiaki Makita 	char desc[ETH_GSTRING_LEN];
73d7dfc5cfSToshiaki Makita 	size_t offset;
743fa2a1dfSstephen hemminger };
753fa2a1dfSstephen hemminger 
76d7dfc5cfSToshiaki Makita struct virtnet_sq_stats {
77d7dfc5cfSToshiaki Makita 	struct u64_stats_sync syncp;
78d7dfc5cfSToshiaki Makita 	u64 packets;
79d7dfc5cfSToshiaki Makita 	u64 bytes;
805b8f3c8dSToshiaki Makita 	u64 xdp_tx;
815b8f3c8dSToshiaki Makita 	u64 xdp_tx_drops;
82461f03dcSToshiaki Makita 	u64 kicks;
83d7dfc5cfSToshiaki Makita };
84d7dfc5cfSToshiaki Makita 
85d46eeeafSJason Wang struct virtnet_rq_stats {
86d46eeeafSJason Wang 	struct u64_stats_sync syncp;
87d7dfc5cfSToshiaki Makita 	u64 packets;
88d7dfc5cfSToshiaki Makita 	u64 bytes;
892c4a2f7dSToshiaki Makita 	u64 drops;
905b8f3c8dSToshiaki Makita 	u64 xdp_packets;
915b8f3c8dSToshiaki Makita 	u64 xdp_tx;
925b8f3c8dSToshiaki Makita 	u64 xdp_redirects;
935b8f3c8dSToshiaki Makita 	u64 xdp_drops;
94461f03dcSToshiaki Makita 	u64 kicks;
95d7dfc5cfSToshiaki Makita };
96d7dfc5cfSToshiaki Makita 
97d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STAT(m)	offsetof(struct virtnet_sq_stats, m)
98d46eeeafSJason Wang #define VIRTNET_RQ_STAT(m)	offsetof(struct virtnet_rq_stats, m)
99d7dfc5cfSToshiaki Makita 
100d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
101d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_SQ_STAT(packets) },
102d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_SQ_STAT(bytes) },
1035b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_SQ_STAT(xdp_tx) },
1045b8f3c8dSToshiaki Makita 	{ "xdp_tx_drops",	VIRTNET_SQ_STAT(xdp_tx_drops) },
105461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_SQ_STAT(kicks) },
106d7dfc5cfSToshiaki Makita };
107d7dfc5cfSToshiaki Makita 
108d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
109d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_RQ_STAT(packets) },
110d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_RQ_STAT(bytes) },
1112c4a2f7dSToshiaki Makita 	{ "drops",		VIRTNET_RQ_STAT(drops) },
1125b8f3c8dSToshiaki Makita 	{ "xdp_packets",	VIRTNET_RQ_STAT(xdp_packets) },
1135b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_RQ_STAT(xdp_tx) },
1145b8f3c8dSToshiaki Makita 	{ "xdp_redirects",	VIRTNET_RQ_STAT(xdp_redirects) },
1155b8f3c8dSToshiaki Makita 	{ "xdp_drops",		VIRTNET_RQ_STAT(xdp_drops) },
116461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_RQ_STAT(kicks) },
117d7dfc5cfSToshiaki Makita };
118d7dfc5cfSToshiaki Makita 
119d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN	ARRAY_SIZE(virtnet_sq_stats_desc)
120d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN	ARRAY_SIZE(virtnet_rq_stats_desc)
121d7dfc5cfSToshiaki Makita 
122e9d7417bSJason Wang /* Internal representation of a send virtqueue */
123e9d7417bSJason Wang struct send_queue {
124e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
125e9d7417bSJason Wang 	struct virtqueue *vq;
126e9d7417bSJason Wang 
127e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
128e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
129986a4f4dSJason Wang 
130986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
131986a4f4dSJason Wang 	char name[40];
132b92f1e67SWillem de Bruijn 
133d7dfc5cfSToshiaki Makita 	struct virtnet_sq_stats stats;
134d7dfc5cfSToshiaki Makita 
135b92f1e67SWillem de Bruijn 	struct napi_struct napi;
136e9d7417bSJason Wang };
137e9d7417bSJason Wang 
138e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
139e9d7417bSJason Wang struct receive_queue {
140e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
141e9d7417bSJason Wang 	struct virtqueue *vq;
142e9d7417bSJason Wang 
143296f96fcSRusty Russell 	struct napi_struct napi;
144296f96fcSRusty Russell 
145f600b690SJohn Fastabend 	struct bpf_prog __rcu *xdp_prog;
146f600b690SJohn Fastabend 
147d7dfc5cfSToshiaki Makita 	struct virtnet_rq_stats stats;
148d7dfc5cfSToshiaki Makita 
149e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
150e9d7417bSJason Wang 	struct page *pages;
151e9d7417bSJason Wang 
152ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
1535377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
154ab7db917SMichael Dalton 
155fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
156fb51879dSMichael Dalton 	struct page_frag alloc_frag;
157fb51879dSMichael Dalton 
158e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
159e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
160986a4f4dSJason Wang 
161d85b758fSMichael S. Tsirkin 	/* Min single buffer size for mergeable buffers case. */
162d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len;
163d85b758fSMichael S. Tsirkin 
164986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
165986a4f4dSJason Wang 	char name[40];
166754b8a21SJesper Dangaard Brouer 
167754b8a21SJesper Dangaard Brouer 	struct xdp_rxq_info xdp_rxq;
168e9d7417bSJason Wang };
169e9d7417bSJason Wang 
17012e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */
17112e57169SMichael S. Tsirkin struct control_buf {
17212e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr hdr;
17312e57169SMichael S. Tsirkin 	virtio_net_ctrl_ack status;
17412e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_mq mq;
17512e57169SMichael S. Tsirkin 	u8 promisc;
17612e57169SMichael S. Tsirkin 	u8 allmulti;
177d7fad4c8SMichael S. Tsirkin 	__virtio16 vid;
178f4ee703aSMichael S. Tsirkin 	__virtio64 offloads;
17912e57169SMichael S. Tsirkin };
18012e57169SMichael S. Tsirkin 
181e9d7417bSJason Wang struct virtnet_info {
182e9d7417bSJason Wang 	struct virtio_device *vdev;
183e9d7417bSJason Wang 	struct virtqueue *cvq;
184e9d7417bSJason Wang 	struct net_device *dev;
185986a4f4dSJason Wang 	struct send_queue *sq;
186986a4f4dSJason Wang 	struct receive_queue *rq;
187e9d7417bSJason Wang 	unsigned int status;
188e9d7417bSJason Wang 
189986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
190986a4f4dSJason Wang 	u16 max_queue_pairs;
191986a4f4dSJason Wang 
192986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
193986a4f4dSJason Wang 	u16 curr_queue_pairs;
194986a4f4dSJason Wang 
195672aafd5SJohn Fastabend 	/* # of XDP queue pairs currently used by the driver */
196672aafd5SJohn Fastabend 	u16 xdp_queue_pairs;
197672aafd5SJohn Fastabend 
19897c2c69eSXuan Zhuo 	/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
19997c2c69eSXuan Zhuo 	bool xdp_enabled;
20097c2c69eSXuan Zhuo 
20197402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
20297402b96SHerbert Xu 	bool big_packets;
20397402b96SHerbert Xu 
2043f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
2053f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
2063f2c31d9SMark McLoughlin 
207986a4f4dSJason Wang 	/* Has control virtqueue */
208986a4f4dSJason Wang 	bool has_cvq;
209986a4f4dSJason Wang 
210e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
211e7428e95SMichael S. Tsirkin 	bool any_header_sg;
212e7428e95SMichael S. Tsirkin 
213012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
214012873d0SMichael S. Tsirkin 	u8 hdr_len;
215012873d0SMichael S. Tsirkin 
2163161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
2173161e453SRusty Russell 	struct delayed_work refill;
2183161e453SRusty Russell 
219586d17c5SJason Wang 	/* Work struct for config space updates */
220586d17c5SJason Wang 	struct work_struct config_work;
221586d17c5SJason Wang 
222986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
223986a4f4dSJason Wang 	bool affinity_hint_set;
22447be2479SWanlong Gao 
2258017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
2268017c279SSebastian Andrzej Siewior 	struct hlist_node node;
2278017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
2282ac46030SMichael S. Tsirkin 
22912e57169SMichael S. Tsirkin 	struct control_buf *ctrl;
23016032be5SNikolay Aleksandrov 
23116032be5SNikolay Aleksandrov 	/* Ethtool settings */
23216032be5SNikolay Aleksandrov 	u8 duplex;
23316032be5SNikolay Aleksandrov 	u32 speed;
2343f93522fSJason Wang 
2353f93522fSJason Wang 	unsigned long guest_offloads;
236a02e8964SWillem de Bruijn 	unsigned long guest_offloads_capable;
237ba5e4426SSridhar Samudrala 
238ba5e4426SSridhar Samudrala 	/* failover when STANDBY feature enabled */
239ba5e4426SSridhar Samudrala 	struct failover *failover;
240296f96fcSRusty Russell };
241296f96fcSRusty Russell 
2429ab86bbcSShirley Ma struct padded_vnet_hdr {
243012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf hdr;
2449ab86bbcSShirley Ma 	/*
245012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
246012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
247012873d0SMichael S. Tsirkin 	 * after the header.
2489ab86bbcSShirley Ma 	 */
249012873d0SMichael S. Tsirkin 	char padding[4];
2509ab86bbcSShirley Ma };
2519ab86bbcSShirley Ma 
2525050471dSToshiaki Makita static bool is_xdp_frame(void *ptr)
2535050471dSToshiaki Makita {
2545050471dSToshiaki Makita 	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
2555050471dSToshiaki Makita }
2565050471dSToshiaki Makita 
2575050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr)
2585050471dSToshiaki Makita {
2595050471dSToshiaki Makita 	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
2605050471dSToshiaki Makita }
2615050471dSToshiaki Makita 
2625050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr)
2635050471dSToshiaki Makita {
2645050471dSToshiaki Makita 	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
2655050471dSToshiaki Makita }
2665050471dSToshiaki Makita 
267986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
268986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
269986a4f4dSJason Wang  */
270986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
271986a4f4dSJason Wang {
2729d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
273986a4f4dSJason Wang }
274986a4f4dSJason Wang 
275986a4f4dSJason Wang static int txq2vq(int txq)
276986a4f4dSJason Wang {
277986a4f4dSJason Wang 	return txq * 2 + 1;
278986a4f4dSJason Wang }
279986a4f4dSJason Wang 
280986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
281986a4f4dSJason Wang {
2829d0ca6edSRusty Russell 	return vq->index / 2;
283986a4f4dSJason Wang }
284986a4f4dSJason Wang 
285986a4f4dSJason Wang static int rxq2vq(int rxq)
286986a4f4dSJason Wang {
287986a4f4dSJason Wang 	return rxq * 2;
288986a4f4dSJason Wang }
289986a4f4dSJason Wang 
290012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
291296f96fcSRusty Russell {
292012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
293296f96fcSRusty Russell }
294296f96fcSRusty Russell 
2959ab86bbcSShirley Ma /*
2969ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
2979ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
2989ab86bbcSShirley Ma  */
299e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
300fb6813f4SRusty Russell {
3019ab86bbcSShirley Ma 	struct page *end;
3029ab86bbcSShirley Ma 
303e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
3049ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
305e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
306e9d7417bSJason Wang 	rq->pages = page;
307fb6813f4SRusty Russell }
308fb6813f4SRusty Russell 
309e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
310fb6813f4SRusty Russell {
311e9d7417bSJason Wang 	struct page *p = rq->pages;
312fb6813f4SRusty Russell 
3139ab86bbcSShirley Ma 	if (p) {
314e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
3159ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
3169ab86bbcSShirley Ma 		p->private = 0;
3179ab86bbcSShirley Ma 	} else
318fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
319fb6813f4SRusty Russell 	return p;
320fb6813f4SRusty Russell }
321fb6813f4SRusty Russell 
322e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi,
323e4e8452aSWillem de Bruijn 				    struct virtqueue *vq)
324e4e8452aSWillem de Bruijn {
325e4e8452aSWillem de Bruijn 	if (napi_schedule_prep(napi)) {
326e4e8452aSWillem de Bruijn 		virtqueue_disable_cb(vq);
327e4e8452aSWillem de Bruijn 		__napi_schedule(napi);
328e4e8452aSWillem de Bruijn 	}
329e4e8452aSWillem de Bruijn }
330e4e8452aSWillem de Bruijn 
331e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi,
332e4e8452aSWillem de Bruijn 				    struct virtqueue *vq, int processed)
333e4e8452aSWillem de Bruijn {
334e4e8452aSWillem de Bruijn 	int opaque;
335e4e8452aSWillem de Bruijn 
336e4e8452aSWillem de Bruijn 	opaque = virtqueue_enable_cb_prepare(vq);
337fdaa767aSToshiaki Makita 	if (napi_complete_done(napi, processed)) {
338fdaa767aSToshiaki Makita 		if (unlikely(virtqueue_poll(vq, opaque)))
339e4e8452aSWillem de Bruijn 			virtqueue_napi_schedule(napi, vq);
340fdaa767aSToshiaki Makita 	} else {
341fdaa767aSToshiaki Makita 		virtqueue_disable_cb(vq);
342fdaa767aSToshiaki Makita 	}
343e4e8452aSWillem de Bruijn }
344e4e8452aSWillem de Bruijn 
345e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
346296f96fcSRusty Russell {
347e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
348b92f1e67SWillem de Bruijn 	struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
349296f96fcSRusty Russell 
3502cb9c6baSRusty Russell 	/* Suppress further interrupts. */
351e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
35211a3a154SRusty Russell 
353b92f1e67SWillem de Bruijn 	if (napi->weight)
354b92f1e67SWillem de Bruijn 		virtqueue_napi_schedule(napi, vq);
355b92f1e67SWillem de Bruijn 	else
356363f1514SRusty Russell 		/* We were probably waiting for more output buffers. */
357986a4f4dSJason Wang 		netif_wake_subqueue(vi->dev, vq2txq(vq));
358296f96fcSRusty Russell }
359296f96fcSRusty Russell 
36028b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22
36128b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize,
36228b39bc7SJason Wang 				  unsigned int headroom)
36328b39bc7SJason Wang {
36428b39bc7SJason Wang 	return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
36528b39bc7SJason Wang }
36628b39bc7SJason Wang 
36728b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
36828b39bc7SJason Wang {
36928b39bc7SJason Wang 	return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
37028b39bc7SJason Wang }
37128b39bc7SJason Wang 
37228b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
37328b39bc7SJason Wang {
37428b39bc7SJason Wang 	return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
37528b39bc7SJason Wang }
37628b39bc7SJason Wang 
3773464645aSMike Waychison /* Called from bottom half context */
378946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
379946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
3802613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
381436c9453SJason Wang 				   unsigned int len, unsigned int truesize,
382fb32856bSXuan Zhuo 				   bool hdr_valid, unsigned int metasize,
3836c66c147SXuan Zhuo 				   bool whole_page)
3849ab86bbcSShirley Ma {
3859ab86bbcSShirley Ma 	struct sk_buff *skb;
386012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
3872613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
388af39c8f7SEric Dumazet 	struct page *page_to_free = NULL;
389fb32856bSXuan Zhuo 	int tailroom, shinfo_size;
390f80bd740SXuan Zhuo 	char *p, *hdr_p, *buf;
3919ab86bbcSShirley Ma 
3922613af0eSMichael Dalton 	p = page_address(page) + offset;
393fb32856bSXuan Zhuo 	hdr_p = p;
3949ab86bbcSShirley Ma 
395012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
396012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
397a4a76503Sstephen hemminger 		hdr_padded_len = sizeof(*hdr);
398012873d0SMichael S. Tsirkin 	else
3992613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
4003f2c31d9SMark McLoughlin 
4016c66c147SXuan Zhuo 	/* If whole_page, there is an offset between the beginning of the
402fb32856bSXuan Zhuo 	 * data and the allocated space, otherwise the data and the allocated
403fb32856bSXuan Zhuo 	 * space are aligned.
4048fb7da9eSXuan Zhuo 	 *
4058fb7da9eSXuan Zhuo 	 * Buffers with headroom use PAGE_SIZE as alloc size, see
4068fb7da9eSXuan Zhuo 	 * add_recvbuf_mergeable() + get_mergeable_buf_len()
407fb32856bSXuan Zhuo 	 */
4086c66c147SXuan Zhuo 	if (whole_page) {
4096c66c147SXuan Zhuo 		/* Buffers with whole_page use PAGE_SIZE as alloc size,
410f80bd740SXuan Zhuo 		 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
411f80bd740SXuan Zhuo 		 */
412fb32856bSXuan Zhuo 		truesize = PAGE_SIZE;
4137bf64460SXuan Zhuo 
4147bf64460SXuan Zhuo 		/* page maybe head page, so we should get the buf by p, not the
4157bf64460SXuan Zhuo 		 * page
4167bf64460SXuan Zhuo 		 */
4177bf64460SXuan Zhuo 		tailroom = truesize - len - offset_in_page(p);
4187bf64460SXuan Zhuo 		buf = (char *)((unsigned long)p & PAGE_MASK);
419fb32856bSXuan Zhuo 	} else {
420fb32856bSXuan Zhuo 		tailroom = truesize - len;
421f80bd740SXuan Zhuo 		buf = p;
422fb32856bSXuan Zhuo 	}
4233f2c31d9SMark McLoughlin 
4249ab86bbcSShirley Ma 	len -= hdr_len;
4252613af0eSMichael Dalton 	offset += hdr_padded_len;
4262613af0eSMichael Dalton 	p += hdr_padded_len;
4273f2c31d9SMark McLoughlin 
428fb32856bSXuan Zhuo 	shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
429fb32856bSXuan Zhuo 
430f80bd740SXuan Zhuo 	/* copy small packet so we can reuse these pages */
431f5d7872aSEric Dumazet 	if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
432f80bd740SXuan Zhuo 		skb = build_skb(buf, truesize);
433fb32856bSXuan Zhuo 		if (unlikely(!skb))
434fb32856bSXuan Zhuo 			return NULL;
435fb32856bSXuan Zhuo 
436f80bd740SXuan Zhuo 		skb_reserve(skb, p - buf);
437fb32856bSXuan Zhuo 		skb_put(skb, len);
438fb32856bSXuan Zhuo 		goto ok;
439fb32856bSXuan Zhuo 	}
440fb32856bSXuan Zhuo 
441fb32856bSXuan Zhuo 	/* copy small packet so we can reuse these pages for small data */
442fb32856bSXuan Zhuo 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
443fb32856bSXuan Zhuo 	if (unlikely(!skb))
444fb32856bSXuan Zhuo 		return NULL;
445fb32856bSXuan Zhuo 
4460f6925b3SEric Dumazet 	/* Copy all frame if it fits skb->head, otherwise
4470f6925b3SEric Dumazet 	 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
4480f6925b3SEric Dumazet 	 */
4490f6925b3SEric Dumazet 	if (len <= skb_tailroom(skb))
4503f2c31d9SMark McLoughlin 		copy = len;
4510f6925b3SEric Dumazet 	else
4520f6925b3SEric Dumazet 		copy = ETH_HLEN + metasize;
45359ae1d12SJohannes Berg 	skb_put_data(skb, p, copy);
4543f2c31d9SMark McLoughlin 
4553f2c31d9SMark McLoughlin 	len -= copy;
4569ab86bbcSShirley Ma 	offset += copy;
4573f2c31d9SMark McLoughlin 
4582613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
4592613af0eSMichael Dalton 		if (len)
4602613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
4612613af0eSMichael Dalton 		else
462af39c8f7SEric Dumazet 			page_to_free = page;
463fb32856bSXuan Zhuo 		goto ok;
4642613af0eSMichael Dalton 	}
4652613af0eSMichael Dalton 
466e878d78bSSasha Levin 	/*
467e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
468e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
469e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
470e878d78bSSasha Levin 	 * the case of a broken device.
471e878d78bSSasha Levin 	 */
472e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
473be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
474e878d78bSSasha Levin 		dev_kfree_skb(skb);
475e878d78bSSasha Levin 		return NULL;
476e878d78bSSasha Levin 	}
4772613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
4789ab86bbcSShirley Ma 	while (len) {
4792613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
4802613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
4812613af0eSMichael Dalton 				frag_size, truesize);
4822613af0eSMichael Dalton 		len -= frag_size;
4839ab86bbcSShirley Ma 		page = (struct page *)page->private;
4849ab86bbcSShirley Ma 		offset = 0;
4853f2c31d9SMark McLoughlin 	}
4863f2c31d9SMark McLoughlin 
4879ab86bbcSShirley Ma 	if (page)
488e9d7417bSJason Wang 		give_pages(rq, page);
4893f2c31d9SMark McLoughlin 
490fb32856bSXuan Zhuo ok:
491fb32856bSXuan Zhuo 	/* hdr_valid means no XDP, so we can copy the vnet header */
492fb32856bSXuan Zhuo 	if (hdr_valid) {
493fb32856bSXuan Zhuo 		hdr = skb_vnet_hdr(skb);
494fb32856bSXuan Zhuo 		memcpy(hdr, hdr_p, hdr_len);
495fb32856bSXuan Zhuo 	}
496af39c8f7SEric Dumazet 	if (page_to_free)
497af39c8f7SEric Dumazet 		put_page(page_to_free);
498fb32856bSXuan Zhuo 
499fb32856bSXuan Zhuo 	if (metasize) {
500fb32856bSXuan Zhuo 		__skb_pull(skb, metasize);
501fb32856bSXuan Zhuo 		skb_metadata_set(skb, metasize);
502fb32856bSXuan Zhuo 	}
503fb32856bSXuan Zhuo 
5049ab86bbcSShirley Ma 	return skb;
5059ab86bbcSShirley Ma }
5069ab86bbcSShirley Ma 
507735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
508735fc405SJesper Dangaard Brouer 				   struct send_queue *sq,
50944fa2dbdSJesper Dangaard Brouer 				   struct xdp_frame *xdpf)
51056434a01SJohn Fastabend {
51156434a01SJohn Fastabend 	struct virtio_net_hdr_mrg_rxbuf *hdr;
51256434a01SJohn Fastabend 	int err;
51356434a01SJohn Fastabend 
514cac320c8SJesper Dangaard Brouer 	if (unlikely(xdpf->headroom < vi->hdr_len))
515cac320c8SJesper Dangaard Brouer 		return -EOVERFLOW;
516cac320c8SJesper Dangaard Brouer 
517cac320c8SJesper Dangaard Brouer 	/* Make room for virtqueue hdr (also change xdpf->headroom?) */
518cac320c8SJesper Dangaard Brouer 	xdpf->data -= vi->hdr_len;
51956434a01SJohn Fastabend 	/* Zero header and leave csum up to XDP layers */
520cac320c8SJesper Dangaard Brouer 	hdr = xdpf->data;
52156434a01SJohn Fastabend 	memset(hdr, 0, vi->hdr_len);
522cac320c8SJesper Dangaard Brouer 	xdpf->len   += vi->hdr_len;
52356434a01SJohn Fastabend 
524cac320c8SJesper Dangaard Brouer 	sg_init_one(sq->sg, xdpf->data, xdpf->len);
525bb91accfSJason Wang 
5265050471dSToshiaki Makita 	err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
5275050471dSToshiaki Makita 				   GFP_ATOMIC);
52811b7d897SJesper Dangaard Brouer 	if (unlikely(err))
529cac320c8SJesper Dangaard Brouer 		return -ENOSPC; /* Caller handle free/refcnt */
53056434a01SJohn Fastabend 
531cac320c8SJesper Dangaard Brouer 	return 0;
53256434a01SJohn Fastabend }
53356434a01SJohn Fastabend 
53497c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
53597c2c69eSXuan Zhuo  * the current cpu, so it does not need to be locked.
53697c2c69eSXuan Zhuo  *
53797c2c69eSXuan Zhuo  * Here we use marco instead of inline functions because we have to deal with
53897c2c69eSXuan Zhuo  * three issues at the same time: 1. the choice of sq. 2. judge and execute the
53997c2c69eSXuan Zhuo  * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
54097c2c69eSXuan Zhuo  * functions to perfectly solve these three problems at the same time.
54197c2c69eSXuan Zhuo  */
54297c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({                                       \
54397c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
54497c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
54597c2c69eSXuan Zhuo 	unsigned int qp;                                                \
54697c2c69eSXuan Zhuo 									\
54797c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
54897c2c69eSXuan Zhuo 		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
54997c2c69eSXuan Zhuo 		qp += smp_processor_id();                               \
55097c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
55197c2c69eSXuan Zhuo 		__netif_tx_acquire(txq);                                \
55297c2c69eSXuan Zhuo 	} else {                                                        \
55397c2c69eSXuan Zhuo 		qp = smp_processor_id() % v->curr_queue_pairs;          \
55497c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
55597c2c69eSXuan Zhuo 		__netif_tx_lock(txq, raw_smp_processor_id());           \
55697c2c69eSXuan Zhuo 	}                                                               \
55797c2c69eSXuan Zhuo 	v->sq + qp;                                                     \
55897c2c69eSXuan Zhuo })
5592a43565cSToshiaki Makita 
56097c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) {                                     \
56197c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
56297c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
56397c2c69eSXuan Zhuo 									\
56497c2c69eSXuan Zhuo 	txq = netdev_get_tx_queue(v->dev, (q) - v->sq);                 \
56597c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids)                           \
56697c2c69eSXuan Zhuo 		__netif_tx_release(txq);                                \
56797c2c69eSXuan Zhuo 	else                                                            \
56897c2c69eSXuan Zhuo 		__netif_tx_unlock(txq);                                 \
5692a43565cSToshiaki Makita }
5702a43565cSToshiaki Makita 
571735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev,
57242b33468SJesper Dangaard Brouer 			    int n, struct xdp_frame **frames, u32 flags)
573186b3c99SJason Wang {
574186b3c99SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
5758dcc5b0aSJesper Dangaard Brouer 	struct receive_queue *rq = vi->rq;
5768dcc5b0aSJesper Dangaard Brouer 	struct bpf_prog *xdp_prog;
577735fc405SJesper Dangaard Brouer 	struct send_queue *sq;
578735fc405SJesper Dangaard Brouer 	unsigned int len;
579546f2897SToshiaki Makita 	int packets = 0;
580546f2897SToshiaki Makita 	int bytes = 0;
581fdc13979SLorenzo Bianconi 	int nxmit = 0;
582461f03dcSToshiaki Makita 	int kicks = 0;
5835050471dSToshiaki Makita 	void *ptr;
584fdc13979SLorenzo Bianconi 	int ret;
585735fc405SJesper Dangaard Brouer 	int i;
586735fc405SJesper Dangaard Brouer 
5878dcc5b0aSJesper Dangaard Brouer 	/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
5888dcc5b0aSJesper Dangaard Brouer 	 * indicate XDP resources have been successfully allocated.
5898dcc5b0aSJesper Dangaard Brouer 	 */
5909719c6b9SJohn Fastabend 	xdp_prog = rcu_access_pointer(rq->xdp_prog);
5911667c08aSToshiaki Makita 	if (!xdp_prog)
5921667c08aSToshiaki Makita 		return -ENXIO;
5931667c08aSToshiaki Makita 
59497c2c69eSXuan Zhuo 	sq = virtnet_xdp_get_sq(vi);
5959ab86bbcSShirley Ma 
5969ab86bbcSShirley Ma 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
597186b3c99SJason Wang 		ret = -EINVAL;
598186b3c99SJason Wang 		goto out;
599186b3c99SJason Wang 	}
600186b3c99SJason Wang 
601735fc405SJesper Dangaard Brouer 	/* Free up any pending old buffers before queueing new ones. */
6025050471dSToshiaki Makita 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
603546f2897SToshiaki Makita 		if (likely(is_xdp_frame(ptr))) {
604546f2897SToshiaki Makita 			struct xdp_frame *frame = ptr_to_xdp(ptr);
605546f2897SToshiaki Makita 
606546f2897SToshiaki Makita 			bytes += frame->len;
607546f2897SToshiaki Makita 			xdp_return_frame(frame);
608546f2897SToshiaki Makita 		} else {
609546f2897SToshiaki Makita 			struct sk_buff *skb = ptr;
610546f2897SToshiaki Makita 
611546f2897SToshiaki Makita 			bytes += skb->len;
612546f2897SToshiaki Makita 			napi_consume_skb(skb, false);
613546f2897SToshiaki Makita 		}
614546f2897SToshiaki Makita 		packets++;
6155050471dSToshiaki Makita 	}
616735fc405SJesper Dangaard Brouer 
617735fc405SJesper Dangaard Brouer 	for (i = 0; i < n; i++) {
618735fc405SJesper Dangaard Brouer 		struct xdp_frame *xdpf = frames[i];
619735fc405SJesper Dangaard Brouer 
620fdc13979SLorenzo Bianconi 		if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
621fdc13979SLorenzo Bianconi 			break;
622fdc13979SLorenzo Bianconi 		nxmit++;
623735fc405SJesper Dangaard Brouer 	}
624fdc13979SLorenzo Bianconi 	ret = nxmit;
6255d274cb4SJesper Dangaard Brouer 
626461f03dcSToshiaki Makita 	if (flags & XDP_XMIT_FLUSH) {
627461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
628461f03dcSToshiaki Makita 			kicks = 1;
629461f03dcSToshiaki Makita 	}
6305b8f3c8dSToshiaki Makita out:
6315b8f3c8dSToshiaki Makita 	u64_stats_update_begin(&sq->stats.syncp);
632546f2897SToshiaki Makita 	sq->stats.bytes += bytes;
633546f2897SToshiaki Makita 	sq->stats.packets += packets;
6345b8f3c8dSToshiaki Makita 	sq->stats.xdp_tx += n;
635fdc13979SLorenzo Bianconi 	sq->stats.xdp_tx_drops += n - nxmit;
636461f03dcSToshiaki Makita 	sq->stats.kicks += kicks;
6375b8f3c8dSToshiaki Makita 	u64_stats_update_end(&sq->stats.syncp);
6385d274cb4SJesper Dangaard Brouer 
63997c2c69eSXuan Zhuo 	virtnet_xdp_put_sq(vi, sq);
6405b8f3c8dSToshiaki Makita 	return ret;
641186b3c99SJason Wang }
642186b3c99SJason Wang 
643f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
644f6b10209SJason Wang {
64597c2c69eSXuan Zhuo 	return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
646f6b10209SJason Wang }
647f6b10209SJason Wang 
6484941d472SJason Wang /* We copy the packet for XDP in the following cases:
6494941d472SJason Wang  *
6504941d472SJason Wang  * 1) Packet is scattered across multiple rx buffers.
6514941d472SJason Wang  * 2) Headroom space is insufficient.
6524941d472SJason Wang  *
6534941d472SJason Wang  * This is inefficient but it's a temporary condition that
6544941d472SJason Wang  * we hit right after XDP is enabled and until queue is refilled
6554941d472SJason Wang  * with large buffers with sufficient headroom - so it should affect
6564941d472SJason Wang  * at most queue size packets.
6574941d472SJason Wang  * Afterwards, the conditions to enable
6584941d472SJason Wang  * XDP should preclude the underlying device from sending packets
6594941d472SJason Wang  * across multiple buffers (num_buf > 1), and we make sure buffers
6604941d472SJason Wang  * have enough headroom.
66172979a6cSJohn Fastabend  */
66272979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq,
66356a86f84SJason Wang 				       u16 *num_buf,
66472979a6cSJohn Fastabend 				       struct page *p,
66572979a6cSJohn Fastabend 				       int offset,
6664941d472SJason Wang 				       int page_off,
66772979a6cSJohn Fastabend 				       unsigned int *len)
66872979a6cSJohn Fastabend {
66972979a6cSJohn Fastabend 	struct page *page = alloc_page(GFP_ATOMIC);
67072979a6cSJohn Fastabend 
67172979a6cSJohn Fastabend 	if (!page)
67272979a6cSJohn Fastabend 		return NULL;
67372979a6cSJohn Fastabend 
67472979a6cSJohn Fastabend 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
67572979a6cSJohn Fastabend 	page_off += *len;
67672979a6cSJohn Fastabend 
67756a86f84SJason Wang 	while (--*num_buf) {
6783cc81a9aSJason Wang 		int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
67972979a6cSJohn Fastabend 		unsigned int buflen;
68072979a6cSJohn Fastabend 		void *buf;
68172979a6cSJohn Fastabend 		int off;
68272979a6cSJohn Fastabend 
683680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &buflen);
684680557cfSMichael S. Tsirkin 		if (unlikely(!buf))
68572979a6cSJohn Fastabend 			goto err_buf;
68672979a6cSJohn Fastabend 
68772979a6cSJohn Fastabend 		p = virt_to_head_page(buf);
68872979a6cSJohn Fastabend 		off = buf - page_address(p);
68972979a6cSJohn Fastabend 
69056a86f84SJason Wang 		/* guard against a misconfigured or uncooperative backend that
69156a86f84SJason Wang 		 * is sending packet larger than the MTU.
69256a86f84SJason Wang 		 */
6933cc81a9aSJason Wang 		if ((page_off + buflen + tailroom) > PAGE_SIZE) {
69456a86f84SJason Wang 			put_page(p);
69556a86f84SJason Wang 			goto err_buf;
69656a86f84SJason Wang 		}
69756a86f84SJason Wang 
69872979a6cSJohn Fastabend 		memcpy(page_address(page) + page_off,
69972979a6cSJohn Fastabend 		       page_address(p) + off, buflen);
70072979a6cSJohn Fastabend 		page_off += buflen;
70156a86f84SJason Wang 		put_page(p);
70272979a6cSJohn Fastabend 	}
70372979a6cSJohn Fastabend 
7042de2f7f4SJohn Fastabend 	/* Headroom does not contribute to packet length */
7052de2f7f4SJohn Fastabend 	*len = page_off - VIRTIO_XDP_HEADROOM;
70672979a6cSJohn Fastabend 	return page;
70772979a6cSJohn Fastabend err_buf:
70872979a6cSJohn Fastabend 	__free_pages(page, 0);
70972979a6cSJohn Fastabend 	return NULL;
71072979a6cSJohn Fastabend }
71172979a6cSJohn Fastabend 
7124941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev,
7134941d472SJason Wang 				     struct virtnet_info *vi,
7144941d472SJason Wang 				     struct receive_queue *rq,
7154941d472SJason Wang 				     void *buf, void *ctx,
716186b3c99SJason Wang 				     unsigned int len,
7177d9d60fdSToshiaki Makita 				     unsigned int *xdp_xmit,
718d46eeeafSJason Wang 				     struct virtnet_rq_stats *stats)
7194941d472SJason Wang {
7204941d472SJason Wang 	struct sk_buff *skb;
7214941d472SJason Wang 	struct bpf_prog *xdp_prog;
7224941d472SJason Wang 	unsigned int xdp_headroom = (unsigned long)ctx;
7234941d472SJason Wang 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
7244941d472SJason Wang 	unsigned int headroom = vi->hdr_len + header_offset;
7254941d472SJason Wang 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
7264941d472SJason Wang 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
7274941d472SJason Wang 	struct page *page = virt_to_head_page(buf);
72811b7d897SJesper Dangaard Brouer 	unsigned int delta = 0;
7294941d472SJason Wang 	struct page *xdp_page;
73011b7d897SJesper Dangaard Brouer 	int err;
731503d539aSYuya Kusakabe 	unsigned int metasize = 0;
73211b7d897SJesper Dangaard Brouer 
7334941d472SJason Wang 	len -= vi->hdr_len;
734d46eeeafSJason Wang 	stats->bytes += len;
7354941d472SJason Wang 
736ad993a95SXie Yongji 	if (unlikely(len > GOOD_PACKET_LEN)) {
737ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds max size %d\n",
738ad993a95SXie Yongji 			 dev->name, len, GOOD_PACKET_LEN);
739ad993a95SXie Yongji 		dev->stats.rx_length_errors++;
740ad993a95SXie Yongji 		goto err_len;
741ad993a95SXie Yongji 	}
7424941d472SJason Wang 	rcu_read_lock();
7434941d472SJason Wang 	xdp_prog = rcu_dereference(rq->xdp_prog);
7444941d472SJason Wang 	if (xdp_prog) {
7454941d472SJason Wang 		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
74644fa2dbdSJesper Dangaard Brouer 		struct xdp_frame *xdpf;
7474941d472SJason Wang 		struct xdp_buff xdp;
7484941d472SJason Wang 		void *orig_data;
7494941d472SJason Wang 		u32 act;
7504941d472SJason Wang 
75195dbe9e7SJesper Dangaard Brouer 		if (unlikely(hdr->hdr.gso_type))
7524941d472SJason Wang 			goto err_xdp;
7534941d472SJason Wang 
7544941d472SJason Wang 		if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
7554941d472SJason Wang 			int offset = buf - page_address(page) + header_offset;
7564941d472SJason Wang 			unsigned int tlen = len + vi->hdr_len;
7574941d472SJason Wang 			u16 num_buf = 1;
7584941d472SJason Wang 
7594941d472SJason Wang 			xdp_headroom = virtnet_get_headroom(vi);
7604941d472SJason Wang 			header_offset = VIRTNET_RX_PAD + xdp_headroom;
7614941d472SJason Wang 			headroom = vi->hdr_len + header_offset;
7624941d472SJason Wang 			buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
7634941d472SJason Wang 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
7644941d472SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf, page,
7654941d472SJason Wang 						      offset, header_offset,
7664941d472SJason Wang 						      &tlen);
7674941d472SJason Wang 			if (!xdp_page)
7684941d472SJason Wang 				goto err_xdp;
7694941d472SJason Wang 
7704941d472SJason Wang 			buf = page_address(xdp_page);
7714941d472SJason Wang 			put_page(page);
7724941d472SJason Wang 			page = xdp_page;
7734941d472SJason Wang 		}
7744941d472SJason Wang 
77543b5169dSLorenzo Bianconi 		xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
776be9df4afSLorenzo Bianconi 		xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
777be9df4afSLorenzo Bianconi 				 xdp_headroom, len, true);
7784941d472SJason Wang 		orig_data = xdp.data;
7794941d472SJason Wang 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
780d46eeeafSJason Wang 		stats->xdp_packets++;
7814941d472SJason Wang 
7824941d472SJason Wang 		switch (act) {
7834941d472SJason Wang 		case XDP_PASS:
7844941d472SJason Wang 			/* Recalculate length in case bpf program changed it */
7854941d472SJason Wang 			delta = orig_data - xdp.data;
7866870de43SNikita V. Shirokov 			len = xdp.data_end - xdp.data;
787503d539aSYuya Kusakabe 			metasize = xdp.data - xdp.data_meta;
7884941d472SJason Wang 			break;
7894941d472SJason Wang 		case XDP_TX:
790d46eeeafSJason Wang 			stats->xdp_tx++;
7911b698fa5SLorenzo Bianconi 			xdpf = xdp_convert_buff_to_frame(&xdp);
79244fa2dbdSJesper Dangaard Brouer 			if (unlikely(!xdpf))
79344fa2dbdSJesper Dangaard Brouer 				goto err_xdp;
794ca9e83b4SJason Wang 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
795fdc13979SLorenzo Bianconi 			if (unlikely(!err)) {
796fdc13979SLorenzo Bianconi 				xdp_return_frame_rx_napi(xdpf);
797fdc13979SLorenzo Bianconi 			} else if (unlikely(err < 0)) {
7984941d472SJason Wang 				trace_xdp_exception(vi->dev, xdp_prog, act);
79911b7d897SJesper Dangaard Brouer 				goto err_xdp;
80011b7d897SJesper Dangaard Brouer 			}
8012471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_TX;
802186b3c99SJason Wang 			rcu_read_unlock();
803186b3c99SJason Wang 			goto xdp_xmit;
804186b3c99SJason Wang 		case XDP_REDIRECT:
805d46eeeafSJason Wang 			stats->xdp_redirects++;
806186b3c99SJason Wang 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
80711b7d897SJesper Dangaard Brouer 			if (err)
80811b7d897SJesper Dangaard Brouer 				goto err_xdp;
8092471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_REDIR;
8104941d472SJason Wang 			rcu_read_unlock();
8114941d472SJason Wang 			goto xdp_xmit;
8124941d472SJason Wang 		default:
8134941d472SJason Wang 			bpf_warn_invalid_xdp_action(act);
814df561f66SGustavo A. R. Silva 			fallthrough;
8154941d472SJason Wang 		case XDP_ABORTED:
8164941d472SJason Wang 			trace_xdp_exception(vi->dev, xdp_prog, act);
81795efabf0SGustavo A. R. Silva 			goto err_xdp;
8184941d472SJason Wang 		case XDP_DROP:
8194941d472SJason Wang 			goto err_xdp;
8204941d472SJason Wang 		}
8214941d472SJason Wang 	}
8224941d472SJason Wang 	rcu_read_unlock();
8234941d472SJason Wang 
8244941d472SJason Wang 	skb = build_skb(buf, buflen);
8254941d472SJason Wang 	if (!skb) {
8264941d472SJason Wang 		put_page(page);
8274941d472SJason Wang 		goto err;
8284941d472SJason Wang 	}
8294941d472SJason Wang 	skb_reserve(skb, headroom - delta);
8306870de43SNikita V. Shirokov 	skb_put(skb, len);
831f1d4884dSYuya Kusakabe 	if (!xdp_prog) {
8324941d472SJason Wang 		buf += header_offset;
8334941d472SJason Wang 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
834f1d4884dSYuya Kusakabe 	} /* keep zeroed vnet hdr since XDP is loaded */
8354941d472SJason Wang 
836503d539aSYuya Kusakabe 	if (metasize)
837503d539aSYuya Kusakabe 		skb_metadata_set(skb, metasize);
838503d539aSYuya Kusakabe 
8394941d472SJason Wang err:
8404941d472SJason Wang 	return skb;
8414941d472SJason Wang 
8424941d472SJason Wang err_xdp:
8434941d472SJason Wang 	rcu_read_unlock();
844d46eeeafSJason Wang 	stats->xdp_drops++;
845ad993a95SXie Yongji err_len:
846d46eeeafSJason Wang 	stats->drops++;
8474941d472SJason Wang 	put_page(page);
8484941d472SJason Wang xdp_xmit:
8494941d472SJason Wang 	return NULL;
8504941d472SJason Wang }
8514941d472SJason Wang 
8524941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev,
8534941d472SJason Wang 				   struct virtnet_info *vi,
8544941d472SJason Wang 				   struct receive_queue *rq,
8554941d472SJason Wang 				   void *buf,
8567d9d60fdSToshiaki Makita 				   unsigned int len,
857d46eeeafSJason Wang 				   struct virtnet_rq_stats *stats)
8584941d472SJason Wang {
8594941d472SJason Wang 	struct page *page = buf;
860503d539aSYuya Kusakabe 	struct sk_buff *skb =
861fb32856bSXuan Zhuo 		page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
8624941d472SJason Wang 
863d46eeeafSJason Wang 	stats->bytes += len - vi->hdr_len;
8644941d472SJason Wang 	if (unlikely(!skb))
8654941d472SJason Wang 		goto err;
8664941d472SJason Wang 
8674941d472SJason Wang 	return skb;
8684941d472SJason Wang 
8694941d472SJason Wang err:
870d46eeeafSJason Wang 	stats->drops++;
8714941d472SJason Wang 	give_pages(rq, page);
8724941d472SJason Wang 	return NULL;
8734941d472SJason Wang }
8744941d472SJason Wang 
8758fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
876fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
8778fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
878680557cfSMichael S. Tsirkin 					 void *buf,
879680557cfSMichael S. Tsirkin 					 void *ctx,
880186b3c99SJason Wang 					 unsigned int len,
8817d9d60fdSToshiaki Makita 					 unsigned int *xdp_xmit,
882d46eeeafSJason Wang 					 struct virtnet_rq_stats *stats)
8839ab86bbcSShirley Ma {
884012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
885012873d0SMichael S. Tsirkin 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
8868fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
8878fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
888f600b690SJohn Fastabend 	struct sk_buff *head_skb, *curr_skb;
889f600b690SJohn Fastabend 	struct bpf_prog *xdp_prog;
8909ce6146eSJesper Dangaard Brouer 	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
8914941d472SJason Wang 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
892503d539aSYuya Kusakabe 	unsigned int metasize = 0;
8939ce6146eSJesper Dangaard Brouer 	unsigned int frame_sz;
8949ce6146eSJesper Dangaard Brouer 	int err;
895ab7db917SMichael Dalton 
89656434a01SJohn Fastabend 	head_skb = NULL;
897d46eeeafSJason Wang 	stats->bytes += len - vi->hdr_len;
89856434a01SJohn Fastabend 
899ad993a95SXie Yongji 	if (unlikely(len > truesize)) {
900ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
901ad993a95SXie Yongji 			 dev->name, len, (unsigned long)ctx);
902ad993a95SXie Yongji 		dev->stats.rx_length_errors++;
903ad993a95SXie Yongji 		goto err_skb;
904ad993a95SXie Yongji 	}
905f600b690SJohn Fastabend 	rcu_read_lock();
906f600b690SJohn Fastabend 	xdp_prog = rcu_dereference(rq->xdp_prog);
907f600b690SJohn Fastabend 	if (xdp_prog) {
90844fa2dbdSJesper Dangaard Brouer 		struct xdp_frame *xdpf;
90972979a6cSJohn Fastabend 		struct page *xdp_page;
9100354e4d1SJohn Fastabend 		struct xdp_buff xdp;
9110354e4d1SJohn Fastabend 		void *data;
912f600b690SJohn Fastabend 		u32 act;
913f600b690SJohn Fastabend 
9143d62b2a0SJason Wang 		/* Transient failure which in theory could occur if
9153d62b2a0SJason Wang 		 * in-flight packets from before XDP was enabled reach
9163d62b2a0SJason Wang 		 * the receive path after XDP is loaded.
9173d62b2a0SJason Wang 		 */
9183d62b2a0SJason Wang 		if (unlikely(hdr->hdr.gso_type))
9193d62b2a0SJason Wang 			goto err_xdp;
9203d62b2a0SJason Wang 
9219ce6146eSJesper Dangaard Brouer 		/* Buffers with headroom use PAGE_SIZE as alloc size,
9229ce6146eSJesper Dangaard Brouer 		 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
9239ce6146eSJesper Dangaard Brouer 		 */
9249ce6146eSJesper Dangaard Brouer 		frame_sz = headroom ? PAGE_SIZE : truesize;
9259ce6146eSJesper Dangaard Brouer 
9263cc81a9aSJason Wang 		/* This happens when rx buffer size is underestimated
9273cc81a9aSJason Wang 		 * or headroom is not enough because of the buffer
9283cc81a9aSJason Wang 		 * was refilled before XDP is set. This should only
9293cc81a9aSJason Wang 		 * happen for the first several packets, so we don't
9303cc81a9aSJason Wang 		 * care much about its performance.
9313cc81a9aSJason Wang 		 */
9324941d472SJason Wang 		if (unlikely(num_buf > 1 ||
9334941d472SJason Wang 			     headroom < virtnet_get_headroom(vi))) {
93472979a6cSJohn Fastabend 			/* linearize data for XDP */
93556a86f84SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf,
9364941d472SJason Wang 						      page, offset,
9374941d472SJason Wang 						      VIRTIO_XDP_HEADROOM,
9384941d472SJason Wang 						      &len);
9399ce6146eSJesper Dangaard Brouer 			frame_sz = PAGE_SIZE;
9409ce6146eSJesper Dangaard Brouer 
94172979a6cSJohn Fastabend 			if (!xdp_page)
942f600b690SJohn Fastabend 				goto err_xdp;
9432de2f7f4SJohn Fastabend 			offset = VIRTIO_XDP_HEADROOM;
94472979a6cSJohn Fastabend 		} else {
94572979a6cSJohn Fastabend 			xdp_page = page;
946f600b690SJohn Fastabend 		}
947f600b690SJohn Fastabend 
9482de2f7f4SJohn Fastabend 		/* Allow consuming headroom but reserve enough space to push
9492de2f7f4SJohn Fastabend 		 * the descriptor on if we get an XDP_TX return code.
9502de2f7f4SJohn Fastabend 		 */
9510354e4d1SJohn Fastabend 		data = page_address(xdp_page) + offset;
95243b5169dSLorenzo Bianconi 		xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
953be9df4afSLorenzo Bianconi 		xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
954be9df4afSLorenzo Bianconi 				 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
955754b8a21SJesper Dangaard Brouer 
9560354e4d1SJohn Fastabend 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
957d46eeeafSJason Wang 		stats->xdp_packets++;
9580354e4d1SJohn Fastabend 
95956434a01SJohn Fastabend 		switch (act) {
96056434a01SJohn Fastabend 		case XDP_PASS:
961503d539aSYuya Kusakabe 			metasize = xdp.data - xdp.data_meta;
9622de2f7f4SJohn Fastabend 
963503d539aSYuya Kusakabe 			/* recalculate offset to account for any header
964503d539aSYuya Kusakabe 			 * adjustments and minus the metasize to copy the
965503d539aSYuya Kusakabe 			 * metadata in page_to_skb(). Note other cases do not
966503d539aSYuya Kusakabe 			 * build an skb and avoid using offset
9676870de43SNikita V. Shirokov 			 */
968503d539aSYuya Kusakabe 			offset = xdp.data - page_address(xdp_page) -
969503d539aSYuya Kusakabe 				 vi->hdr_len - metasize;
970503d539aSYuya Kusakabe 
971503d539aSYuya Kusakabe 			/* recalculate len if xdp.data, xdp.data_end or
972503d539aSYuya Kusakabe 			 * xdp.data_meta were adjusted
973503d539aSYuya Kusakabe 			 */
974503d539aSYuya Kusakabe 			len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
9751830f893SJason Wang 			/* We can only create skb based on xdp_page. */
9761830f893SJason Wang 			if (unlikely(xdp_page != page)) {
9771830f893SJason Wang 				rcu_read_unlock();
9781830f893SJason Wang 				put_page(page);
979503d539aSYuya Kusakabe 				head_skb = page_to_skb(vi, rq, xdp_page, offset,
980503d539aSYuya Kusakabe 						       len, PAGE_SIZE, false,
9816c66c147SXuan Zhuo 						       metasize, true);
9821830f893SJason Wang 				return head_skb;
9831830f893SJason Wang 			}
98456434a01SJohn Fastabend 			break;
98556434a01SJohn Fastabend 		case XDP_TX:
986d46eeeafSJason Wang 			stats->xdp_tx++;
9871b698fa5SLorenzo Bianconi 			xdpf = xdp_convert_buff_to_frame(&xdp);
98844fa2dbdSJesper Dangaard Brouer 			if (unlikely(!xdpf))
98944fa2dbdSJesper Dangaard Brouer 				goto err_xdp;
990ca9e83b4SJason Wang 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
991fdc13979SLorenzo Bianconi 			if (unlikely(!err)) {
992fdc13979SLorenzo Bianconi 				xdp_return_frame_rx_napi(xdpf);
993fdc13979SLorenzo Bianconi 			} else if (unlikely(err < 0)) {
9940354e4d1SJohn Fastabend 				trace_xdp_exception(vi->dev, xdp_prog, act);
99511b7d897SJesper Dangaard Brouer 				if (unlikely(xdp_page != page))
99611b7d897SJesper Dangaard Brouer 					put_page(xdp_page);
99711b7d897SJesper Dangaard Brouer 				goto err_xdp;
99811b7d897SJesper Dangaard Brouer 			}
9992471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_TX;
100072979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
10015d458a13SJason Wang 				put_page(page);
100256434a01SJohn Fastabend 			rcu_read_unlock();
100356434a01SJohn Fastabend 			goto xdp_xmit;
10043cc81a9aSJason Wang 		case XDP_REDIRECT:
1005d46eeeafSJason Wang 			stats->xdp_redirects++;
10063cc81a9aSJason Wang 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
10073cc81a9aSJason Wang 			if (err) {
10083cc81a9aSJason Wang 				if (unlikely(xdp_page != page))
10093cc81a9aSJason Wang 					put_page(xdp_page);
10103cc81a9aSJason Wang 				goto err_xdp;
10113cc81a9aSJason Wang 			}
10122471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_REDIR;
10133cc81a9aSJason Wang 			if (unlikely(xdp_page != page))
10146890418bSJason Wang 				put_page(page);
10153cc81a9aSJason Wang 			rcu_read_unlock();
10163cc81a9aSJason Wang 			goto xdp_xmit;
101756434a01SJohn Fastabend 		default:
10180354e4d1SJohn Fastabend 			bpf_warn_invalid_xdp_action(act);
1019df561f66SGustavo A. R. Silva 			fallthrough;
10200354e4d1SJohn Fastabend 		case XDP_ABORTED:
10210354e4d1SJohn Fastabend 			trace_xdp_exception(vi->dev, xdp_prog, act);
1022df561f66SGustavo A. R. Silva 			fallthrough;
10230354e4d1SJohn Fastabend 		case XDP_DROP:
102472979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
102572979a6cSJohn Fastabend 				__free_pages(xdp_page, 0);
1026f600b690SJohn Fastabend 			goto err_xdp;
1027f600b690SJohn Fastabend 		}
102856434a01SJohn Fastabend 	}
1029f600b690SJohn Fastabend 	rcu_read_unlock();
1030f600b690SJohn Fastabend 
1031503d539aSYuya Kusakabe 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
10326c66c147SXuan Zhuo 			       metasize, !!headroom);
1033f600b690SJohn Fastabend 	curr_skb = head_skb;
10349ab86bbcSShirley Ma 
10358fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
10368fc3b9e9SMichael S. Tsirkin 		goto err_skb;
10379ab86bbcSShirley Ma 	while (--num_buf) {
10388fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
10398fc3b9e9SMichael S. Tsirkin 
1040680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
104103e9f8a0SYunjian Wang 		if (unlikely(!buf)) {
10428fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1043fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
1044012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
1045012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
10468fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
10478fc3b9e9SMichael S. Tsirkin 			goto err_buf;
10483f2c31d9SMark McLoughlin 		}
10498fc3b9e9SMichael S. Tsirkin 
1050d46eeeafSJason Wang 		stats->bytes += len;
10518fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
105228b39bc7SJason Wang 
105328b39bc7SJason Wang 		truesize = mergeable_ctx_to_truesize(ctx);
105428b39bc7SJason Wang 		if (unlikely(len > truesize)) {
105556da5fd0SDan Carpenter 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1056680557cfSMichael S. Tsirkin 				 dev->name, len, (unsigned long)ctx);
1057680557cfSMichael S. Tsirkin 			dev->stats.rx_length_errors++;
1058680557cfSMichael S. Tsirkin 			goto err_skb;
1059680557cfSMichael S. Tsirkin 		}
10608fc3b9e9SMichael S. Tsirkin 
10618fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
10622613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
10632613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
10648fc3b9e9SMichael S. Tsirkin 
10658fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
10668fc3b9e9SMichael S. Tsirkin 				goto err_skb;
10672613af0eSMichael Dalton 			if (curr_skb == head_skb)
10682613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
10692613af0eSMichael Dalton 			else
10702613af0eSMichael Dalton 				curr_skb->next = nskb;
10712613af0eSMichael Dalton 			curr_skb = nskb;
10722613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
10732613af0eSMichael Dalton 			num_skb_frags = 0;
10742613af0eSMichael Dalton 		}
10752613af0eSMichael Dalton 		if (curr_skb != head_skb) {
10762613af0eSMichael Dalton 			head_skb->data_len += len;
10772613af0eSMichael Dalton 			head_skb->len += len;
1078fb51879dSMichael Dalton 			head_skb->truesize += truesize;
10792613af0eSMichael Dalton 		}
10808fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
1081ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1082ba275241SJason Wang 			put_page(page);
1083ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1084fb51879dSMichael Dalton 					     len, truesize);
1085ba275241SJason Wang 		} else {
10862613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
1087fb51879dSMichael Dalton 					offset, len, truesize);
1088ba275241SJason Wang 		}
10898fc3b9e9SMichael S. Tsirkin 	}
10908fc3b9e9SMichael S. Tsirkin 
10915377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
10928fc3b9e9SMichael S. Tsirkin 	return head_skb;
10938fc3b9e9SMichael S. Tsirkin 
1094f600b690SJohn Fastabend err_xdp:
1095f600b690SJohn Fastabend 	rcu_read_unlock();
1096d46eeeafSJason Wang 	stats->xdp_drops++;
10978fc3b9e9SMichael S. Tsirkin err_skb:
10988fc3b9e9SMichael S. Tsirkin 	put_page(page);
1099850e088dSJason Wang 	while (num_buf-- > 1) {
1100680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &len);
1101680557cfSMichael S. Tsirkin 		if (unlikely(!buf)) {
11028fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
11038fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
11048fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
11058fc3b9e9SMichael S. Tsirkin 			break;
11068fc3b9e9SMichael S. Tsirkin 		}
1107d46eeeafSJason Wang 		stats->bytes += len;
1108680557cfSMichael S. Tsirkin 		page = virt_to_head_page(buf);
11098fc3b9e9SMichael S. Tsirkin 		put_page(page);
11103f2c31d9SMark McLoughlin 	}
11118fc3b9e9SMichael S. Tsirkin err_buf:
1112d46eeeafSJason Wang 	stats->drops++;
11138fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
111456434a01SJohn Fastabend xdp_xmit:
11158fc3b9e9SMichael S. Tsirkin 	return NULL;
11169ab86bbcSShirley Ma }
11179ab86bbcSShirley Ma 
11187d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
11192471c75eSJesper Dangaard Brouer 			void *buf, unsigned int len, void **ctx,
1120a0929a44SToshiaki Makita 			unsigned int *xdp_xmit,
1121d46eeeafSJason Wang 			struct virtnet_rq_stats *stats)
11229ab86bbcSShirley Ma {
1123e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
11249ab86bbcSShirley Ma 	struct sk_buff *skb;
1125012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
11269ab86bbcSShirley Ma 
1127bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
11289ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
11299ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
1130ab7db917SMichael Dalton 		if (vi->mergeable_rx_bufs) {
1131680557cfSMichael S. Tsirkin 			put_page(virt_to_head_page(buf));
1132ab7db917SMichael Dalton 		} else if (vi->big_packets) {
113398bfd23cSMichael Dalton 			give_pages(rq, buf);
1134ab7db917SMichael Dalton 		} else {
1135f6b10209SJason Wang 			put_page(virt_to_head_page(buf));
1136ab7db917SMichael Dalton 		}
11377d9d60fdSToshiaki Makita 		return;
11389ab86bbcSShirley Ma 	}
11399ab86bbcSShirley Ma 
1140f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
11417d9d60fdSToshiaki Makita 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1142a0929a44SToshiaki Makita 					stats);
1143f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
1144a0929a44SToshiaki Makita 		skb = receive_big(dev, vi, rq, buf, len, stats);
1145f121159dSMichael S. Tsirkin 	else
1146a0929a44SToshiaki Makita 		skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1147f121159dSMichael S. Tsirkin 
11488fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
11497d9d60fdSToshiaki Makita 		return;
11503f2c31d9SMark McLoughlin 
11519ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
11523fa2a1dfSstephen hemminger 
1153e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
115410a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1155296f96fcSRusty Russell 
1156e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1157e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
1158e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1159e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
1160fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
1161296f96fcSRusty Russell 		goto frame_err;
1162296f96fcSRusty Russell 	}
1163296f96fcSRusty Russell 
1164133bbb18SWillem de Bruijn 	skb_record_rx_queue(skb, vq2rxq(rq->vq));
1165d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
1166d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1167d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
1168d1dc06dcSMike Rapoport 
11690fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
11707d9d60fdSToshiaki Makita 	return;
1171296f96fcSRusty Russell 
1172296f96fcSRusty Russell frame_err:
1173296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
1174296f96fcSRusty Russell 	dev_kfree_skb(skb);
1175296f96fcSRusty Russell }
1176296f96fcSRusty Russell 
1177192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the
1178192f68cfSJason Wang  * same size, except for the headroom. For this reason we do
1179192f68cfSJason Wang  * not need to use  mergeable_len_to_ctx here - it is enough
1180192f68cfSJason Wang  * to store the headroom as the context ignoring the truesize.
1181192f68cfSJason Wang  */
1182946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1183946fa564SMichael S. Tsirkin 			     gfp_t gfp)
1184296f96fcSRusty Russell {
1185f6b10209SJason Wang 	struct page_frag *alloc_frag = &rq->alloc_frag;
1186f6b10209SJason Wang 	char *buf;
11872de2f7f4SJohn Fastabend 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
1188192f68cfSJason Wang 	void *ctx = (void *)(unsigned long)xdp_headroom;
1189f6b10209SJason Wang 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
11909ab86bbcSShirley Ma 	int err;
11913f2c31d9SMark McLoughlin 
1192f6b10209SJason Wang 	len = SKB_DATA_ALIGN(len) +
1193f6b10209SJason Wang 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1194f6b10209SJason Wang 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
11959ab86bbcSShirley Ma 		return -ENOMEM;
1196296f96fcSRusty Russell 
1197f6b10209SJason Wang 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1198f6b10209SJason Wang 	get_page(alloc_frag->page);
1199f6b10209SJason Wang 	alloc_frag->offset += len;
1200f6b10209SJason Wang 	sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1201f6b10209SJason Wang 		    vi->hdr_len + GOOD_PACKET_LEN);
1202192f68cfSJason Wang 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
12039ab86bbcSShirley Ma 	if (err < 0)
1204f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
12059ab86bbcSShirley Ma 	return err;
120697402b96SHerbert Xu }
120797402b96SHerbert Xu 
1208012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1209012873d0SMichael S. Tsirkin 			   gfp_t gfp)
12109ab86bbcSShirley Ma {
12119ab86bbcSShirley Ma 	struct page *first, *list = NULL;
12129ab86bbcSShirley Ma 	char *p;
12139ab86bbcSShirley Ma 	int i, err, offset;
1214296f96fcSRusty Russell 
1215a5835440SRusty Russell 	sg_init_table(rq->sg, MAX_SKB_FRAGS + 2);
1216a5835440SRusty Russell 
1217e9d7417bSJason Wang 	/* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
12189ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
1219e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
12209ab86bbcSShirley Ma 		if (!first) {
12219ab86bbcSShirley Ma 			if (list)
1222e9d7417bSJason Wang 				give_pages(rq, list);
12239ab86bbcSShirley Ma 			return -ENOMEM;
1224296f96fcSRusty Russell 		}
1225e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
12269ab86bbcSShirley Ma 
12279ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
12289ab86bbcSShirley Ma 		first->private = (unsigned long)list;
12299ab86bbcSShirley Ma 		list = first;
12309ab86bbcSShirley Ma 	}
12319ab86bbcSShirley Ma 
1232e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
12339ab86bbcSShirley Ma 	if (!first) {
1234e9d7417bSJason Wang 		give_pages(rq, list);
12359ab86bbcSShirley Ma 		return -ENOMEM;
12369ab86bbcSShirley Ma 	}
12379ab86bbcSShirley Ma 	p = page_address(first);
12389ab86bbcSShirley Ma 
1239e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
1240012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
1241012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
12429ab86bbcSShirley Ma 
1243e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
12449ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
1245e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
12469ab86bbcSShirley Ma 
12479ab86bbcSShirley Ma 	/* chain first in list head */
12489ab86bbcSShirley Ma 	first->private = (unsigned long)list;
12499dc7b9e4SRusty Russell 	err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
1250aa989f5eSMichael S. Tsirkin 				  first, gfp);
12519ab86bbcSShirley Ma 	if (err < 0)
1252e9d7417bSJason Wang 		give_pages(rq, first);
12539ab86bbcSShirley Ma 
12549ab86bbcSShirley Ma 	return err;
12559ab86bbcSShirley Ma }
12569ab86bbcSShirley Ma 
1257d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
12583cc81a9aSJason Wang 					  struct ewma_pkt_len *avg_pkt_len,
12593cc81a9aSJason Wang 					  unsigned int room)
12609ab86bbcSShirley Ma {
1261ab7db917SMichael Dalton 	const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1262fbf28d78SMichael Dalton 	unsigned int len;
1263fbf28d78SMichael Dalton 
12643cc81a9aSJason Wang 	if (room)
12653cc81a9aSJason Wang 		return PAGE_SIZE - room;
12663cc81a9aSJason Wang 
12675377d758SJohannes Berg 	len = hdr_len +	clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1268f0c3192cSMichael S. Tsirkin 				rq->min_buf_len, PAGE_SIZE - hdr_len);
12693cc81a9aSJason Wang 
1270e377fcc8SMichael S. Tsirkin 	return ALIGN(len, L1_CACHE_BYTES);
1271fbf28d78SMichael Dalton }
1272fbf28d78SMichael Dalton 
12732de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi,
12742de2f7f4SJohn Fastabend 				 struct receive_queue *rq, gfp_t gfp)
1275fbf28d78SMichael Dalton {
1276fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
12772de2f7f4SJohn Fastabend 	unsigned int headroom = virtnet_get_headroom(vi);
12783cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
12793cc81a9aSJason Wang 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1280fb51879dSMichael Dalton 	char *buf;
1281680557cfSMichael S. Tsirkin 	void *ctx;
12829ab86bbcSShirley Ma 	int err;
1283fb51879dSMichael Dalton 	unsigned int len, hole;
12849ab86bbcSShirley Ma 
12853cc81a9aSJason Wang 	/* Extra tailroom is needed to satisfy XDP's assumption. This
12863cc81a9aSJason Wang 	 * means rx frags coalescing won't work, but consider we've
12873cc81a9aSJason Wang 	 * disabled GSO for XDP, it won't be a big issue.
12883cc81a9aSJason Wang 	 */
12893cc81a9aSJason Wang 	len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
12903cc81a9aSJason Wang 	if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
12919ab86bbcSShirley Ma 		return -ENOMEM;
1292ab7db917SMichael Dalton 
1293fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
12942de2f7f4SJohn Fastabend 	buf += headroom; /* advance address leaving hole at front of pkt */
1295fb51879dSMichael Dalton 	get_page(alloc_frag->page);
12963cc81a9aSJason Wang 	alloc_frag->offset += len + room;
1297fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
12983cc81a9aSJason Wang 	if (hole < len + room) {
1299ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
1300ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
13011daa8790SMichael S. Tsirkin 		 * the current buffer.
1302ab7db917SMichael Dalton 		 */
1303fb51879dSMichael Dalton 		len += hole;
1304fb51879dSMichael Dalton 		alloc_frag->offset += hole;
1305fb51879dSMichael Dalton 	}
13069ab86bbcSShirley Ma 
1307fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
130829fda25aSDavid S. Miller 	ctx = mergeable_len_to_ctx(len, headroom);
1309680557cfSMichael S. Tsirkin 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
13109ab86bbcSShirley Ma 	if (err < 0)
13112613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
13129ab86bbcSShirley Ma 
13139ab86bbcSShirley Ma 	return err;
1314296f96fcSRusty Russell }
1315296f96fcSRusty Russell 
1316b2baed69SRusty Russell /*
1317b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
1318b2baed69SRusty Russell  *
1319b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
1320b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
1321b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
1322b2baed69SRusty Russell  */
1323946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1324946fa564SMichael S. Tsirkin 			  gfp_t gfp)
13253f2c31d9SMark McLoughlin {
13263f2c31d9SMark McLoughlin 	int err;
13271788f495SMichael S. Tsirkin 	bool oom;
13283f2c31d9SMark McLoughlin 
13290aea51c3SAmit Shah 	do {
13309ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
13312de2f7f4SJohn Fastabend 			err = add_recvbuf_mergeable(vi, rq, gfp);
13329ab86bbcSShirley Ma 		else if (vi->big_packets)
1333012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
13349ab86bbcSShirley Ma 		else
1335946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
13363f2c31d9SMark McLoughlin 
13371788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
13389ed4cb07SRusty Russell 		if (err)
13393f2c31d9SMark McLoughlin 			break;
1340b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
1341461f03dcSToshiaki Makita 	if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
134201c32598SMichael S. Tsirkin 		unsigned long flags;
134301c32598SMichael S. Tsirkin 
134401c32598SMichael S. Tsirkin 		flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1345d46eeeafSJason Wang 		rq->stats.kicks++;
134601c32598SMichael S. Tsirkin 		u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1347461f03dcSToshiaki Makita 	}
1348461f03dcSToshiaki Makita 
13493161e453SRusty Russell 	return !oom;
13503f2c31d9SMark McLoughlin }
13513f2c31d9SMark McLoughlin 
135218445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
1353296f96fcSRusty Russell {
1354296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
1355986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1356e9d7417bSJason Wang 
1357e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(&rq->napi, rvq);
1358296f96fcSRusty Russell }
1359296f96fcSRusty Russell 
1360e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
13613e9d08ecSBruce Rogers {
1362e4e8452aSWillem de Bruijn 	napi_enable(napi);
13633e9d08ecSBruce Rogers 
13643e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
1365e4e8452aSWillem de Bruijn 	 * won't get another interrupt, so process any outstanding packets now.
1366e4e8452aSWillem de Bruijn 	 * Call local_bh_enable after to trigger softIRQ processing.
1367e4e8452aSWillem de Bruijn 	 */
1368ec13ee80SMichael S. Tsirkin 	local_bh_disable();
1369e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(napi, vq);
1370ec13ee80SMichael S. Tsirkin 	local_bh_enable();
13713e9d08ecSBruce Rogers }
13723e9d08ecSBruce Rogers 
1373b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1374b92f1e67SWillem de Bruijn 				   struct virtqueue *vq,
1375b92f1e67SWillem de Bruijn 				   struct napi_struct *napi)
1376b92f1e67SWillem de Bruijn {
1377b92f1e67SWillem de Bruijn 	if (!napi->weight)
1378b92f1e67SWillem de Bruijn 		return;
1379b92f1e67SWillem de Bruijn 
1380b92f1e67SWillem de Bruijn 	/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1381b92f1e67SWillem de Bruijn 	 * enable the feature if this is likely affine with the transmit path.
1382b92f1e67SWillem de Bruijn 	 */
1383b92f1e67SWillem de Bruijn 	if (!vi->affinity_hint_set) {
1384b92f1e67SWillem de Bruijn 		napi->weight = 0;
1385b92f1e67SWillem de Bruijn 		return;
1386b92f1e67SWillem de Bruijn 	}
1387b92f1e67SWillem de Bruijn 
1388b92f1e67SWillem de Bruijn 	return virtnet_napi_enable(vq, napi);
1389b92f1e67SWillem de Bruijn }
1390b92f1e67SWillem de Bruijn 
139178a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi)
139278a57b48SWillem de Bruijn {
139378a57b48SWillem de Bruijn 	if (napi->weight)
139478a57b48SWillem de Bruijn 		napi_disable(napi);
139578a57b48SWillem de Bruijn }
139678a57b48SWillem de Bruijn 
13973161e453SRusty Russell static void refill_work(struct work_struct *work)
13983161e453SRusty Russell {
1399e9d7417bSJason Wang 	struct virtnet_info *vi =
1400e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
14013161e453SRusty Russell 	bool still_empty;
1402986a4f4dSJason Wang 	int i;
14033161e453SRusty Russell 
140455257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1405986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
1406986a4f4dSJason Wang 
1407986a4f4dSJason Wang 		napi_disable(&rq->napi);
1408946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1409e4e8452aSWillem de Bruijn 		virtnet_napi_enable(rq->vq, &rq->napi);
14103161e453SRusty Russell 
14113161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
1412986a4f4dSJason Wang 		 * we will *never* try to fill again.
1413986a4f4dSJason Wang 		 */
14143161e453SRusty Russell 		if (still_empty)
14153b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
14163161e453SRusty Russell 	}
1417986a4f4dSJason Wang }
14183161e453SRusty Russell 
14192471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget,
14202471c75eSJesper Dangaard Brouer 			   unsigned int *xdp_xmit)
1421296f96fcSRusty Russell {
1422e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
1423d46eeeafSJason Wang 	struct virtnet_rq_stats stats = {};
1424a0929a44SToshiaki Makita 	unsigned int len;
14259ab86bbcSShirley Ma 	void *buf;
1426a0929a44SToshiaki Makita 	int i;
1427296f96fcSRusty Russell 
1428192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
1429680557cfSMichael S. Tsirkin 		void *ctx;
1430680557cfSMichael S. Tsirkin 
1431d46eeeafSJason Wang 		while (stats.packets < budget &&
1432680557cfSMichael S. Tsirkin 		       (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1433a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1434d46eeeafSJason Wang 			stats.packets++;
1435680557cfSMichael S. Tsirkin 		}
1436680557cfSMichael S. Tsirkin 	} else {
1437d46eeeafSJason Wang 		while (stats.packets < budget &&
1438e9d7417bSJason Wang 		       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1439a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1440d46eeeafSJason Wang 			stats.packets++;
1441296f96fcSRusty Russell 		}
1442680557cfSMichael S. Tsirkin 	}
1443296f96fcSRusty Russell 
1444718be6baS? jiang 	if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
1445946fa564SMichael S. Tsirkin 		if (!try_fill_recv(vi, rq, GFP_ATOMIC))
14463b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, 0);
14473161e453SRusty Russell 	}
1448296f96fcSRusty Russell 
1449d7dfc5cfSToshiaki Makita 	u64_stats_update_begin(&rq->stats.syncp);
1450a0929a44SToshiaki Makita 	for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1451a0929a44SToshiaki Makita 		size_t offset = virtnet_rq_stats_desc[i].offset;
1452a0929a44SToshiaki Makita 		u64 *item;
1453a0929a44SToshiaki Makita 
1454d46eeeafSJason Wang 		item = (u64 *)((u8 *)&rq->stats + offset);
1455d46eeeafSJason Wang 		*item += *(u64 *)((u8 *)&stats + offset);
1456a0929a44SToshiaki Makita 	}
1457d7dfc5cfSToshiaki Makita 	u64_stats_update_end(&rq->stats.syncp);
145861845d20SJason Wang 
1459d46eeeafSJason Wang 	return stats.packets;
14602ffa7598SJason Wang }
14612ffa7598SJason Wang 
1462df133f3fSMichael S. Tsirkin static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1463ea7735d9SWillem de Bruijn {
1464ea7735d9SWillem de Bruijn 	unsigned int len;
1465ea7735d9SWillem de Bruijn 	unsigned int packets = 0;
1466ea7735d9SWillem de Bruijn 	unsigned int bytes = 0;
14675050471dSToshiaki Makita 	void *ptr;
1468ea7735d9SWillem de Bruijn 
14695050471dSToshiaki Makita 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
14705050471dSToshiaki Makita 		if (likely(!is_xdp_frame(ptr))) {
14715050471dSToshiaki Makita 			struct sk_buff *skb = ptr;
14725050471dSToshiaki Makita 
1473ea7735d9SWillem de Bruijn 			pr_debug("Sent skb %p\n", skb);
1474ea7735d9SWillem de Bruijn 
1475ea7735d9SWillem de Bruijn 			bytes += skb->len;
1476df133f3fSMichael S. Tsirkin 			napi_consume_skb(skb, in_napi);
14775050471dSToshiaki Makita 		} else {
14785050471dSToshiaki Makita 			struct xdp_frame *frame = ptr_to_xdp(ptr);
14795050471dSToshiaki Makita 
14805050471dSToshiaki Makita 			bytes += frame->len;
14815050471dSToshiaki Makita 			xdp_return_frame(frame);
14825050471dSToshiaki Makita 		}
14835050471dSToshiaki Makita 		packets++;
1484ea7735d9SWillem de Bruijn 	}
1485ea7735d9SWillem de Bruijn 
1486ea7735d9SWillem de Bruijn 	/* Avoid overhead when no packets have been processed
1487ea7735d9SWillem de Bruijn 	 * happens when called speculatively from start_xmit.
1488ea7735d9SWillem de Bruijn 	 */
1489ea7735d9SWillem de Bruijn 	if (!packets)
1490ea7735d9SWillem de Bruijn 		return;
1491ea7735d9SWillem de Bruijn 
1492d7dfc5cfSToshiaki Makita 	u64_stats_update_begin(&sq->stats.syncp);
1493d7dfc5cfSToshiaki Makita 	sq->stats.bytes += bytes;
1494d7dfc5cfSToshiaki Makita 	sq->stats.packets += packets;
1495d7dfc5cfSToshiaki Makita 	u64_stats_update_end(&sq->stats.syncp);
1496ea7735d9SWillem de Bruijn }
1497ea7735d9SWillem de Bruijn 
1498534da5e8SToshiaki Makita static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1499534da5e8SToshiaki Makita {
1500534da5e8SToshiaki Makita 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1501534da5e8SToshiaki Makita 		return false;
1502534da5e8SToshiaki Makita 	else if (q < vi->curr_queue_pairs)
1503534da5e8SToshiaki Makita 		return true;
1504534da5e8SToshiaki Makita 	else
1505534da5e8SToshiaki Makita 		return false;
1506534da5e8SToshiaki Makita }
1507534da5e8SToshiaki Makita 
15087b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq)
15097b0411efSWillem de Bruijn {
15107b0411efSWillem de Bruijn 	struct virtnet_info *vi = rq->vq->vdev->priv;
15117b0411efSWillem de Bruijn 	unsigned int index = vq2rxq(rq->vq);
15127b0411efSWillem de Bruijn 	struct send_queue *sq = &vi->sq[index];
15137b0411efSWillem de Bruijn 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
15147b0411efSWillem de Bruijn 
1515534da5e8SToshiaki Makita 	if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
15167b0411efSWillem de Bruijn 		return;
15177b0411efSWillem de Bruijn 
15187b0411efSWillem de Bruijn 	if (__netif_tx_trylock(txq)) {
1519df133f3fSMichael S. Tsirkin 		free_old_xmit_skbs(sq, true);
15207b0411efSWillem de Bruijn 		__netif_tx_unlock(txq);
15217b0411efSWillem de Bruijn 	}
15227b0411efSWillem de Bruijn 
15237b0411efSWillem de Bruijn 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
15247b0411efSWillem de Bruijn 		netif_tx_wake_queue(txq);
15257b0411efSWillem de Bruijn }
15267b0411efSWillem de Bruijn 
15272ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
15282ffa7598SJason Wang {
15292ffa7598SJason Wang 	struct receive_queue *rq =
15302ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
15319267c430SJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
15329267c430SJason Wang 	struct send_queue *sq;
15332a43565cSToshiaki Makita 	unsigned int received;
15342471c75eSJesper Dangaard Brouer 	unsigned int xdp_xmit = 0;
15352ffa7598SJason Wang 
15367b0411efSWillem de Bruijn 	virtnet_poll_cleantx(rq);
15377b0411efSWillem de Bruijn 
1538186b3c99SJason Wang 	received = virtnet_receive(rq, budget, &xdp_xmit);
15392ffa7598SJason Wang 
15408329d98eSRusty Russell 	/* Out of packets? */
1541e4e8452aSWillem de Bruijn 	if (received < budget)
1542e4e8452aSWillem de Bruijn 		virtqueue_napi_complete(napi, rq->vq, received);
1543296f96fcSRusty Russell 
15442471c75eSJesper Dangaard Brouer 	if (xdp_xmit & VIRTIO_XDP_REDIR)
15451d233886SToke Høiland-Jørgensen 		xdp_do_flush();
15462471c75eSJesper Dangaard Brouer 
15472471c75eSJesper Dangaard Brouer 	if (xdp_xmit & VIRTIO_XDP_TX) {
154897c2c69eSXuan Zhuo 		sq = virtnet_xdp_get_sq(vi);
1549461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1550461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
1551461f03dcSToshiaki Makita 			sq->stats.kicks++;
1552461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
1553461f03dcSToshiaki Makita 		}
155497c2c69eSXuan Zhuo 		virtnet_xdp_put_sq(vi, sq);
15559267c430SJason Wang 	}
1556186b3c99SJason Wang 
1557296f96fcSRusty Russell 	return received;
1558296f96fcSRusty Russell }
1559296f96fcSRusty Russell 
1560986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
1561986a4f4dSJason Wang {
1562986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1563754b8a21SJesper Dangaard Brouer 	int i, err;
1564986a4f4dSJason Wang 
1565e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1566e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
1567986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
1568946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1569986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
1570754b8a21SJesper Dangaard Brouer 
1571b02e5a0eSBjörn Töpel 		err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1572754b8a21SJesper Dangaard Brouer 		if (err < 0)
1573754b8a21SJesper Dangaard Brouer 			return err;
1574754b8a21SJesper Dangaard Brouer 
15758d5d8852SJesper Dangaard Brouer 		err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
15768d5d8852SJesper Dangaard Brouer 						 MEM_TYPE_PAGE_SHARED, NULL);
15778d5d8852SJesper Dangaard Brouer 		if (err < 0) {
15788d5d8852SJesper Dangaard Brouer 			xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
15798d5d8852SJesper Dangaard Brouer 			return err;
15808d5d8852SJesper Dangaard Brouer 		}
15818d5d8852SJesper Dangaard Brouer 
1582e4e8452aSWillem de Bruijn 		virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1583b92f1e67SWillem de Bruijn 		virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1584986a4f4dSJason Wang 	}
1585986a4f4dSJason Wang 
1586986a4f4dSJason Wang 	return 0;
1587986a4f4dSJason Wang }
1588986a4f4dSJason Wang 
1589b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1590b92f1e67SWillem de Bruijn {
1591b92f1e67SWillem de Bruijn 	struct send_queue *sq = container_of(napi, struct send_queue, napi);
1592b92f1e67SWillem de Bruijn 	struct virtnet_info *vi = sq->vq->vdev->priv;
1593534da5e8SToshiaki Makita 	unsigned int index = vq2txq(sq->vq);
1594534da5e8SToshiaki Makita 	struct netdev_queue *txq;
1595b92f1e67SWillem de Bruijn 
1596534da5e8SToshiaki Makita 	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1597534da5e8SToshiaki Makita 		/* We don't need to enable cb for XDP */
1598534da5e8SToshiaki Makita 		napi_complete_done(napi, 0);
1599534da5e8SToshiaki Makita 		return 0;
1600534da5e8SToshiaki Makita 	}
1601534da5e8SToshiaki Makita 
1602534da5e8SToshiaki Makita 	txq = netdev_get_tx_queue(vi->dev, index);
1603b92f1e67SWillem de Bruijn 	__netif_tx_lock(txq, raw_smp_processor_id());
1604df133f3fSMichael S. Tsirkin 	free_old_xmit_skbs(sq, true);
1605b92f1e67SWillem de Bruijn 	__netif_tx_unlock(txq);
1606b92f1e67SWillem de Bruijn 
1607b92f1e67SWillem de Bruijn 	virtqueue_napi_complete(napi, sq->vq, 0);
1608b92f1e67SWillem de Bruijn 
1609b92f1e67SWillem de Bruijn 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
1610b92f1e67SWillem de Bruijn 		netif_tx_wake_queue(txq);
1611b92f1e67SWillem de Bruijn 
1612b92f1e67SWillem de Bruijn 	return 0;
1613b92f1e67SWillem de Bruijn }
1614b92f1e67SWillem de Bruijn 
1615e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1616296f96fcSRusty Russell {
1617012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1618296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1619e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
1620e2fcad58SJason A. Donenfeld 	int num_sg;
1621012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
1622e7428e95SMichael S. Tsirkin 	bool can_push;
1623296f96fcSRusty Russell 
1624e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1625e7428e95SMichael S. Tsirkin 
1626e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
1627e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1628e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1629e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
1630e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
1631e7428e95SMichael S. Tsirkin 	if (can_push)
1632012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1633e7428e95SMichael S. Tsirkin 	else
1634e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
1635296f96fcSRusty Russell 
1636e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1637fd3a8862SWillem de Bruijn 				    virtio_is_little_endian(vi->vdev), false,
1638fd3a8862SWillem de Bruijn 				    0))
163985eb1389SXianting Tian 		return -EPROTO;
1640296f96fcSRusty Russell 
1641e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
1642012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
16433f2c31d9SMark McLoughlin 
1644547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1645e7428e95SMichael S. Tsirkin 	if (can_push) {
1646e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
1647e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1648e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
1649e2fcad58SJason A. Donenfeld 			return num_sg;
1650e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
1651e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
1652e7428e95SMichael S. Tsirkin 	} else {
1653e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
1654e2fcad58SJason A. Donenfeld 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1655e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
1656e2fcad58SJason A. Donenfeld 			return num_sg;
1657e2fcad58SJason A. Donenfeld 		num_sg++;
1658e7428e95SMichael S. Tsirkin 	}
16599dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
166011a3a154SRusty Russell }
166111a3a154SRusty Russell 
1662424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
166399ffc696SRusty Russell {
166499ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1665986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
1666986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
16679ed4cb07SRusty Russell 	int err;
16684b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
16696b16f9eeSFlorian Westphal 	bool kick = !netdev_xmit_more();
1670b92f1e67SWillem de Bruijn 	bool use_napi = sq->napi.weight;
16712cb9c6baSRusty Russell 
16722cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
1673df133f3fSMichael S. Tsirkin 	free_old_xmit_skbs(sq, false);
167499ffc696SRusty Russell 
1675bdb12e0dSWillem de Bruijn 	if (use_napi && kick)
1676bdb12e0dSWillem de Bruijn 		virtqueue_enable_cb_delayed(sq->vq);
1677bdb12e0dSWillem de Bruijn 
1678074c3582SJacob Keller 	/* timestamp packet in software */
1679074c3582SJacob Keller 	skb_tx_timestamp(skb);
1680074c3582SJacob Keller 
168103f191baSMichael S. Tsirkin 	/* Try to transmit */
1682b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
168399ffc696SRusty Russell 
16849ed4cb07SRusty Russell 	/* This should not happen! */
1685681daee2SJason Wang 	if (unlikely(err)) {
168658eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
16872e57b79cSRick Jones 		if (net_ratelimit())
168858eba97dSRusty Russell 			dev_warn(&dev->dev,
16897934b481SYuval Shaia 				 "Unexpected TXQ (%d) queue failure: %d\n",
16907934b481SYuval Shaia 				 qnum, err);
169158eba97dSRusty Russell 		dev->stats.tx_dropped++;
169285e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
169358eba97dSRusty Russell 		return NETDEV_TX_OK;
1694296f96fcSRusty Russell 	}
169503f191baSMichael S. Tsirkin 
169648925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
1697b92f1e67SWillem de Bruijn 	if (!use_napi) {
169848925e37SRusty Russell 		skb_orphan(skb);
1699895b5c9fSFlorian Westphal 		nf_reset_ct(skb);
1700b92f1e67SWillem de Bruijn 	}
170148925e37SRusty Russell 
170260302ff6SMichael S. Tsirkin 	/* If running out of space, stop queue to avoid getting packets that we
170360302ff6SMichael S. Tsirkin 	 * are then unable to transmit.
170460302ff6SMichael S. Tsirkin 	 * An alternative would be to force queuing layer to requeue the skb by
170560302ff6SMichael S. Tsirkin 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
170660302ff6SMichael S. Tsirkin 	 * returned in a normal path of operation: it means that driver is not
170760302ff6SMichael S. Tsirkin 	 * maintaining the TX queue stop/start state properly, and causes
170860302ff6SMichael S. Tsirkin 	 * the stack to do a non-trivial amount of useless work.
170960302ff6SMichael S. Tsirkin 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
171060302ff6SMichael S. Tsirkin 	 * early means 16 slots are typically wasted.
1711d631b94eSstephen hemminger 	 */
1712b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1713986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
1714b92f1e67SWillem de Bruijn 		if (!use_napi &&
1715b92f1e67SWillem de Bruijn 		    unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
171648925e37SRusty Russell 			/* More just got used, free them then recheck. */
1717df133f3fSMichael S. Tsirkin 			free_old_xmit_skbs(sq, false);
1718b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1719986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
1720e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
172148925e37SRusty Russell 			}
172248925e37SRusty Russell 		}
172348925e37SRusty Russell 	}
172448925e37SRusty Russell 
1725461f03dcSToshiaki Makita 	if (kick || netif_xmit_stopped(txq)) {
1726461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1727461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
1728461f03dcSToshiaki Makita 			sq->stats.kicks++;
1729461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
1730461f03dcSToshiaki Makita 		}
1731461f03dcSToshiaki Makita 	}
17320b725a2cSDavid S. Miller 
17330b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
1734c223a078SDavid S. Miller }
1735c223a078SDavid S. Miller 
173640cbfc37SAmos Kong /*
173740cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
173840cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
1739788a8b6dSstephen hemminger  * never fail unless improperly formatted.
174040cbfc37SAmos Kong  */
174140cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1742d24bae32Sstephen hemminger 				 struct scatterlist *out)
174340cbfc37SAmos Kong {
1744f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
1745d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
174640cbfc37SAmos Kong 
174740cbfc37SAmos Kong 	/* Caller should know better */
1748f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
174940cbfc37SAmos Kong 
175012e57169SMichael S. Tsirkin 	vi->ctrl->status = ~0;
175112e57169SMichael S. Tsirkin 	vi->ctrl->hdr.class = class;
175212e57169SMichael S. Tsirkin 	vi->ctrl->hdr.cmd = cmd;
1753f7bc9594SRusty Russell 	/* Add header */
175412e57169SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1755f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
175640cbfc37SAmos Kong 
1757f7bc9594SRusty Russell 	if (out)
1758f7bc9594SRusty Russell 		sgs[out_num++] = out;
175940cbfc37SAmos Kong 
1760f7bc9594SRusty Russell 	/* Add return status. */
176112e57169SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1762d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
176340cbfc37SAmos Kong 
1764d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1765a7c58146SRusty Russell 	virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
176640cbfc37SAmos Kong 
176767975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
176812e57169SMichael S. Tsirkin 		return vi->ctrl->status == VIRTIO_NET_OK;
176940cbfc37SAmos Kong 
177040cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
177140cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
177240cbfc37SAmos Kong 	 */
1773047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
1774047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
177540cbfc37SAmos Kong 		cpu_relax();
177640cbfc37SAmos Kong 
177712e57169SMichael S. Tsirkin 	return vi->ctrl->status == VIRTIO_NET_OK;
177840cbfc37SAmos Kong }
177940cbfc37SAmos Kong 
17809c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
17819c46f6d4SAlex Williamson {
17829c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
17839c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
1784f2f2c8b4SJiri Pirko 	int ret;
1785e37e2ff3SAndy Lutomirski 	struct sockaddr *addr;
17867e58d5aeSAmos Kong 	struct scatterlist sg;
17879c46f6d4SAlex Williamson 
1788ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
1789ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
1790ba5e4426SSridhar Samudrala 
1791801822d1SShyam Saini 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
1792e37e2ff3SAndy Lutomirski 	if (!addr)
1793e37e2ff3SAndy Lutomirski 		return -ENOMEM;
1794e37e2ff3SAndy Lutomirski 
1795e37e2ff3SAndy Lutomirski 	ret = eth_prepare_mac_addr_change(dev, addr);
1796f2f2c8b4SJiri Pirko 	if (ret)
1797e37e2ff3SAndy Lutomirski 		goto out;
17989c46f6d4SAlex Williamson 
17997e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
18007e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
18017e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1802d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
18037e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
18047e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
1805e37e2ff3SAndy Lutomirski 			ret = -EINVAL;
1806e37e2ff3SAndy Lutomirski 			goto out;
18077e58d5aeSAmos Kong 		}
18087e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
18097e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1810855e0c52SRusty Russell 		unsigned int i;
1811855e0c52SRusty Russell 
1812855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
1813855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
1814855e0c52SRusty Russell 			virtio_cwrite8(vdev,
1815855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
1816855e0c52SRusty Russell 				       i, addr->sa_data[i]);
18177e58d5aeSAmos Kong 	}
18187e58d5aeSAmos Kong 
18197e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
1820e37e2ff3SAndy Lutomirski 	ret = 0;
18219c46f6d4SAlex Williamson 
1822e37e2ff3SAndy Lutomirski out:
1823e37e2ff3SAndy Lutomirski 	kfree(addr);
1824e37e2ff3SAndy Lutomirski 	return ret;
18259c46f6d4SAlex Williamson }
18269c46f6d4SAlex Williamson 
1827bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev,
18283fa2a1dfSstephen hemminger 			  struct rtnl_link_stats64 *tot)
18293fa2a1dfSstephen hemminger {
18303fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
18313fa2a1dfSstephen hemminger 	unsigned int start;
1832d7dfc5cfSToshiaki Makita 	int i;
18333fa2a1dfSstephen hemminger 
1834d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
18352c4a2f7dSToshiaki Makita 		u64 tpackets, tbytes, rpackets, rbytes, rdrops;
1836d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
1837d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
18383fa2a1dfSstephen hemminger 
18393fa2a1dfSstephen hemminger 		do {
1840d7dfc5cfSToshiaki Makita 			start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
1841d7dfc5cfSToshiaki Makita 			tpackets = sq->stats.packets;
1842d7dfc5cfSToshiaki Makita 			tbytes   = sq->stats.bytes;
1843d7dfc5cfSToshiaki Makita 		} while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
184483a27052SEric Dumazet 
184583a27052SEric Dumazet 		do {
1846d7dfc5cfSToshiaki Makita 			start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
1847d46eeeafSJason Wang 			rpackets = rq->stats.packets;
1848d46eeeafSJason Wang 			rbytes   = rq->stats.bytes;
1849d46eeeafSJason Wang 			rdrops   = rq->stats.drops;
1850d7dfc5cfSToshiaki Makita 		} while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
18513fa2a1dfSstephen hemminger 
18523fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
18533fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
18543fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
18553fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
18562c4a2f7dSToshiaki Makita 		tot->rx_dropped += rdrops;
18573fa2a1dfSstephen hemminger 	}
18583fa2a1dfSstephen hemminger 
18593fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
1860021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
18613fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
18623fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
18633fa2a1dfSstephen hemminger }
18643fa2a1dfSstephen hemminger 
1865586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
1866586d17c5SJason Wang {
1867586d17c5SJason Wang 	rtnl_lock();
1868586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1869d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1870586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1871586d17c5SJason Wang 	rtnl_unlock();
1872586d17c5SJason Wang }
1873586d17c5SJason Wang 
187447315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1875986a4f4dSJason Wang {
1876986a4f4dSJason Wang 	struct scatterlist sg;
1877986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
1878986a4f4dSJason Wang 
1879986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1880986a4f4dSJason Wang 		return 0;
1881986a4f4dSJason Wang 
188212e57169SMichael S. Tsirkin 	vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
188312e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
1884986a4f4dSJason Wang 
1885986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1886d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1887986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1888986a4f4dSJason Wang 			 queue_pairs);
1889986a4f4dSJason Wang 		return -EINVAL;
189055257d72SSasha Levin 	} else {
1891986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
189235ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
189335ed159bSJason Wang 		if (dev->flags & IFF_UP)
18949b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
189555257d72SSasha Levin 	}
1896986a4f4dSJason Wang 
1897986a4f4dSJason Wang 	return 0;
1898986a4f4dSJason Wang }
1899986a4f4dSJason Wang 
190047315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
190147315329SJohn Fastabend {
190247315329SJohn Fastabend 	int err;
190347315329SJohn Fastabend 
190447315329SJohn Fastabend 	rtnl_lock();
190547315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
190647315329SJohn Fastabend 	rtnl_unlock();
190747315329SJohn Fastabend 	return err;
190847315329SJohn Fastabend }
190947315329SJohn Fastabend 
1910296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
1911296f96fcSRusty Russell {
1912296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1913986a4f4dSJason Wang 	int i;
1914296f96fcSRusty Russell 
1915b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
1916b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1917986a4f4dSJason Wang 
1918b92f1e67SWillem de Bruijn 	for (i = 0; i < vi->max_queue_pairs; i++) {
1919754b8a21SJesper Dangaard Brouer 		xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1920986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
192178a57b48SWillem de Bruijn 		virtnet_napi_tx_disable(&vi->sq[i].napi);
1922b92f1e67SWillem de Bruijn 	}
1923296f96fcSRusty Russell 
1924296f96fcSRusty Russell 	return 0;
1925296f96fcSRusty Russell }
1926296f96fcSRusty Russell 
19272af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
19282af7698eSAlex Williamson {
19292af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
1930f565a7c2SAlex Williamson 	struct scatterlist sg[2];
1931f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
1932ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
193332e7bfc4SJiri Pirko 	int uc_count;
19344cd24eafSJiri Pirko 	int mc_count;
1935f565a7c2SAlex Williamson 	void *buf;
1936f565a7c2SAlex Williamson 	int i;
19372af7698eSAlex Williamson 
1938788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
19392af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
19402af7698eSAlex Williamson 		return;
19412af7698eSAlex Williamson 
194212e57169SMichael S. Tsirkin 	vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
194312e57169SMichael S. Tsirkin 	vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
19442af7698eSAlex Williamson 
194512e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
19462af7698eSAlex Williamson 
19472af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1948d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
19492af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
195012e57169SMichael S. Tsirkin 			 vi->ctrl->promisc ? "en" : "dis");
19512af7698eSAlex Williamson 
195212e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
19532af7698eSAlex Williamson 
19542af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1955d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
19562af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
195712e57169SMichael S. Tsirkin 			 vi->ctrl->allmulti ? "en" : "dis");
1958f565a7c2SAlex Williamson 
195932e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
19604cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
1961f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
19624cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1963f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
19644cd24eafSJiri Pirko 	mac_data = buf;
1965e68ed8f0SJoe Perches 	if (!buf)
1966f565a7c2SAlex Williamson 		return;
1967f565a7c2SAlex Williamson 
196823e258e1SAlex Williamson 	sg_init_table(sg, 2);
196923e258e1SAlex Williamson 
1970f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
1971fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
1972ccffad25SJiri Pirko 	i = 0;
197332e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
1974ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1975f565a7c2SAlex Williamson 
1976f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
197732e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1978f565a7c2SAlex Williamson 
1979f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
198032e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
1981f565a7c2SAlex Williamson 
1982fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
1983567ec874SJiri Pirko 	i = 0;
198422bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
198522bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1986f565a7c2SAlex Williamson 
1987f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
19884cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1989f565a7c2SAlex Williamson 
1990f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1991d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
199299e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1993f565a7c2SAlex Williamson 
1994f565a7c2SAlex Williamson 	kfree(buf);
19952af7698eSAlex Williamson }
19962af7698eSAlex Williamson 
199780d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
199880d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
19990bde9569SAlex Williamson {
20000bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
20010bde9569SAlex Williamson 	struct scatterlist sg;
20020bde9569SAlex Williamson 
2003d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
200412e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
20050bde9569SAlex Williamson 
20060bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2007d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
20080bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
20098e586137SJiri Pirko 	return 0;
20100bde9569SAlex Williamson }
20110bde9569SAlex Williamson 
201280d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
201380d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
20140bde9569SAlex Williamson {
20150bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
20160bde9569SAlex Williamson 	struct scatterlist sg;
20170bde9569SAlex Williamson 
2018d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
201912e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
20200bde9569SAlex Williamson 
20210bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2022d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
20230bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
20248e586137SJiri Pirko 	return 0;
20250bde9569SAlex Williamson }
20260bde9569SAlex Williamson 
2027310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi)
2028986a4f4dSJason Wang {
2029986a4f4dSJason Wang 	int i;
20308898c21cSWanlong Gao 
20318898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
20328898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
203319e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->rq[i].vq, NULL);
203419e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->sq[i].vq, NULL);
20358898c21cSWanlong Gao 		}
20368898c21cSWanlong Gao 
20378898c21cSWanlong Gao 		vi->affinity_hint_set = false;
20388898c21cSWanlong Gao 	}
20398898c21cSWanlong Gao }
20408898c21cSWanlong Gao 
20418898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
2042986a4f4dSJason Wang {
20432ca653d6SCaleb Raitto 	cpumask_var_t mask;
20442ca653d6SCaleb Raitto 	int stragglers;
20452ca653d6SCaleb Raitto 	int group_size;
20462ca653d6SCaleb Raitto 	int i, j, cpu;
20472ca653d6SCaleb Raitto 	int num_cpu;
20482ca653d6SCaleb Raitto 	int stride;
2049986a4f4dSJason Wang 
20502ca653d6SCaleb Raitto 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2051310974faSPeter Xu 		virtnet_clean_affinity(vi);
2052986a4f4dSJason Wang 		return;
2053986a4f4dSJason Wang 	}
2054986a4f4dSJason Wang 
20552ca653d6SCaleb Raitto 	num_cpu = num_online_cpus();
20562ca653d6SCaleb Raitto 	stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
20572ca653d6SCaleb Raitto 	stragglers = num_cpu >= vi->curr_queue_pairs ?
20582ca653d6SCaleb Raitto 			num_cpu % vi->curr_queue_pairs :
20592ca653d6SCaleb Raitto 			0;
20602ca653d6SCaleb Raitto 	cpu = cpumask_next(-1, cpu_online_mask);
20614d99f660SAndrei Vagin 
20622ca653d6SCaleb Raitto 	for (i = 0; i < vi->curr_queue_pairs; i++) {
20632ca653d6SCaleb Raitto 		group_size = stride + (i < stragglers ? 1 : 0);
20642ca653d6SCaleb Raitto 
20652ca653d6SCaleb Raitto 		for (j = 0; j < group_size; j++) {
20662ca653d6SCaleb Raitto 			cpumask_set_cpu(cpu, mask);
20672ca653d6SCaleb Raitto 			cpu = cpumask_next_wrap(cpu, cpu_online_mask,
20682ca653d6SCaleb Raitto 						nr_cpu_ids, false);
20692ca653d6SCaleb Raitto 		}
20702ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->rq[i].vq, mask);
20712ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->sq[i].vq, mask);
2072044ab86dSAntoine Tenart 		__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
20732ca653d6SCaleb Raitto 		cpumask_clear(mask);
2074986a4f4dSJason Wang 	}
2075986a4f4dSJason Wang 
2076986a4f4dSJason Wang 	vi->affinity_hint_set = true;
20772ca653d6SCaleb Raitto 	free_cpumask_var(mask);
207847be2479SWanlong Gao }
2079986a4f4dSJason Wang 
20808017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
20818de4b2f3SWanlong Gao {
20828017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
20838017c279SSebastian Andrzej Siewior 						   node);
20848de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
20858017c279SSebastian Andrzej Siewior 	return 0;
20868de4b2f3SWanlong Gao }
20873ab098dfSJason Wang 
20888017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
20898017c279SSebastian Andrzej Siewior {
20908017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
20918017c279SSebastian Andrzej Siewior 						   node_dead);
20928017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
20938017c279SSebastian Andrzej Siewior 	return 0;
20948017c279SSebastian Andrzej Siewior }
20958017c279SSebastian Andrzej Siewior 
20968017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
20978017c279SSebastian Andrzej Siewior {
20988017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
20998017c279SSebastian Andrzej Siewior 						   node);
21008017c279SSebastian Andrzej Siewior 
2101310974faSPeter Xu 	virtnet_clean_affinity(vi);
21028017c279SSebastian Andrzej Siewior 	return 0;
21038017c279SSebastian Andrzej Siewior }
21048017c279SSebastian Andrzej Siewior 
21058017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
21068017c279SSebastian Andrzej Siewior 
21078017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
21088017c279SSebastian Andrzej Siewior {
21098017c279SSebastian Andrzej Siewior 	int ret;
21108017c279SSebastian Andrzej Siewior 
21118017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
21128017c279SSebastian Andrzej Siewior 	if (ret)
21138017c279SSebastian Andrzej Siewior 		return ret;
21148017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
21158017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
21168017c279SSebastian Andrzej Siewior 	if (!ret)
21178017c279SSebastian Andrzej Siewior 		return ret;
21188017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
21198017c279SSebastian Andrzej Siewior 	return ret;
21208017c279SSebastian Andrzej Siewior }
21218017c279SSebastian Andrzej Siewior 
21228017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
21238017c279SSebastian Andrzej Siewior {
21248017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
21258017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
21268017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
2127a9ea3fc6SHerbert Xu }
2128a9ea3fc6SHerbert Xu 
21298f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
21308f9f4668SRick Jones 				struct ethtool_ringparam *ring)
21318f9f4668SRick Jones {
21328f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
21338f9f4668SRick Jones 
2134986a4f4dSJason Wang 	ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2135986a4f4dSJason Wang 	ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
21368f9f4668SRick Jones 	ring->rx_pending = ring->rx_max_pending;
21378f9f4668SRick Jones 	ring->tx_pending = ring->tx_max_pending;
21388f9f4668SRick Jones }
21398f9f4668SRick Jones 
214066846048SRick Jones 
214166846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
214266846048SRick Jones 				struct ethtool_drvinfo *info)
214366846048SRick Jones {
214466846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
214566846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
214666846048SRick Jones 
214766846048SRick Jones 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
214866846048SRick Jones 	strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
214966846048SRick Jones 	strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
215066846048SRick Jones 
215166846048SRick Jones }
215266846048SRick Jones 
2153d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
2154d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
2155d73bcd2cSJason Wang 				struct ethtool_channels *channels)
2156d73bcd2cSJason Wang {
2157d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2158d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
2159d73bcd2cSJason Wang 	int err;
2160d73bcd2cSJason Wang 
2161d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
2162d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
2163d73bcd2cSJason Wang 	 */
2164d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
2165d73bcd2cSJason Wang 		return -EINVAL;
2166d73bcd2cSJason Wang 
2167c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2168d73bcd2cSJason Wang 		return -EINVAL;
2169d73bcd2cSJason Wang 
2170f600b690SJohn Fastabend 	/* For now we don't support modifying channels while XDP is loaded
2171f600b690SJohn Fastabend 	 * also when XDP is loaded all RX queues have XDP programs so we only
2172f600b690SJohn Fastabend 	 * need to check a single RX queue.
2173f600b690SJohn Fastabend 	 */
2174f600b690SJohn Fastabend 	if (vi->rq[0].xdp_prog)
2175f600b690SJohn Fastabend 		return -EINVAL;
2176f600b690SJohn Fastabend 
217747be2479SWanlong Gao 	get_online_cpus();
217847315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
2179de33212fSJeff Dike 	if (err) {
2180de33212fSJeff Dike 		put_online_cpus();
2181de33212fSJeff Dike 		goto err;
2182d73bcd2cSJason Wang 	}
2183de33212fSJeff Dike 	virtnet_set_affinity(vi);
218447be2479SWanlong Gao 	put_online_cpus();
2185d73bcd2cSJason Wang 
2186de33212fSJeff Dike 	netif_set_real_num_tx_queues(dev, queue_pairs);
2187de33212fSJeff Dike 	netif_set_real_num_rx_queues(dev, queue_pairs);
2188de33212fSJeff Dike  err:
2189d73bcd2cSJason Wang 	return err;
2190d73bcd2cSJason Wang }
2191d73bcd2cSJason Wang 
2192d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2193d7dfc5cfSToshiaki Makita {
2194d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2195d7dfc5cfSToshiaki Makita 	unsigned int i, j;
2196d7a9a01bSAlexander Duyck 	u8 *p = data;
2197d7dfc5cfSToshiaki Makita 
2198d7dfc5cfSToshiaki Makita 	switch (stringset) {
2199d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
2200d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2201d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2202d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2203d7a9a01bSAlexander Duyck 						virtnet_rq_stats_desc[j].desc);
2204d7dfc5cfSToshiaki Makita 		}
2205d7dfc5cfSToshiaki Makita 
2206d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2207d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2208d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2209d7a9a01bSAlexander Duyck 						virtnet_sq_stats_desc[j].desc);
2210d7dfc5cfSToshiaki Makita 		}
2211d7dfc5cfSToshiaki Makita 		break;
2212d7dfc5cfSToshiaki Makita 	}
2213d7dfc5cfSToshiaki Makita }
2214d7dfc5cfSToshiaki Makita 
2215d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset)
2216d7dfc5cfSToshiaki Makita {
2217d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2218d7dfc5cfSToshiaki Makita 
2219d7dfc5cfSToshiaki Makita 	switch (sset) {
2220d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
2221d7dfc5cfSToshiaki Makita 		return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2222d7dfc5cfSToshiaki Makita 					       VIRTNET_SQ_STATS_LEN);
2223d7dfc5cfSToshiaki Makita 	default:
2224d7dfc5cfSToshiaki Makita 		return -EOPNOTSUPP;
2225d7dfc5cfSToshiaki Makita 	}
2226d7dfc5cfSToshiaki Makita }
2227d7dfc5cfSToshiaki Makita 
2228d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev,
2229d7dfc5cfSToshiaki Makita 				      struct ethtool_stats *stats, u64 *data)
2230d7dfc5cfSToshiaki Makita {
2231d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2232d7dfc5cfSToshiaki Makita 	unsigned int idx = 0, start, i, j;
2233d7dfc5cfSToshiaki Makita 	const u8 *stats_base;
2234d7dfc5cfSToshiaki Makita 	size_t offset;
2235d7dfc5cfSToshiaki Makita 
2236d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2237d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
2238d7dfc5cfSToshiaki Makita 
2239d46eeeafSJason Wang 		stats_base = (u8 *)&rq->stats;
2240d7dfc5cfSToshiaki Makita 		do {
2241d7dfc5cfSToshiaki Makita 			start = u64_stats_fetch_begin_irq(&rq->stats.syncp);
2242d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2243d7dfc5cfSToshiaki Makita 				offset = virtnet_rq_stats_desc[j].offset;
2244d7dfc5cfSToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
2245d7dfc5cfSToshiaki Makita 			}
2246d7dfc5cfSToshiaki Makita 		} while (u64_stats_fetch_retry_irq(&rq->stats.syncp, start));
2247d7dfc5cfSToshiaki Makita 		idx += VIRTNET_RQ_STATS_LEN;
2248d7dfc5cfSToshiaki Makita 	}
2249d7dfc5cfSToshiaki Makita 
2250d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2251d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
2252d7dfc5cfSToshiaki Makita 
2253d7dfc5cfSToshiaki Makita 		stats_base = (u8 *)&sq->stats;
2254d7dfc5cfSToshiaki Makita 		do {
2255d7dfc5cfSToshiaki Makita 			start = u64_stats_fetch_begin_irq(&sq->stats.syncp);
2256d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2257d7dfc5cfSToshiaki Makita 				offset = virtnet_sq_stats_desc[j].offset;
2258d7dfc5cfSToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
2259d7dfc5cfSToshiaki Makita 			}
2260d7dfc5cfSToshiaki Makita 		} while (u64_stats_fetch_retry_irq(&sq->stats.syncp, start));
2261d7dfc5cfSToshiaki Makita 		idx += VIRTNET_SQ_STATS_LEN;
2262d7dfc5cfSToshiaki Makita 	}
2263d7dfc5cfSToshiaki Makita }
2264d7dfc5cfSToshiaki Makita 
2265d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
2266d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
2267d73bcd2cSJason Wang {
2268d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2269d73bcd2cSJason Wang 
2270d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
2271d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
2272d73bcd2cSJason Wang 	channels->max_other = 0;
2273d73bcd2cSJason Wang 	channels->rx_count = 0;
2274d73bcd2cSJason Wang 	channels->tx_count = 0;
2275d73bcd2cSJason Wang 	channels->other_count = 0;
2276d73bcd2cSJason Wang }
2277d73bcd2cSJason Wang 
2278ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev,
2279ebb6b4b1SPhilippe Reynes 				      const struct ethtool_link_ksettings *cmd)
228016032be5SNikolay Aleksandrov {
228116032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
228216032be5SNikolay Aleksandrov 
22839aedc6e2SCris Forno 	return ethtool_virtdev_set_link_ksettings(dev, cmd,
22849aedc6e2SCris Forno 						  &vi->speed, &vi->duplex);
228516032be5SNikolay Aleksandrov }
228616032be5SNikolay Aleksandrov 
2287ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev,
2288ebb6b4b1SPhilippe Reynes 				      struct ethtool_link_ksettings *cmd)
228916032be5SNikolay Aleksandrov {
229016032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
229116032be5SNikolay Aleksandrov 
2292ebb6b4b1SPhilippe Reynes 	cmd->base.speed = vi->speed;
2293ebb6b4b1SPhilippe Reynes 	cmd->base.duplex = vi->duplex;
2294ebb6b4b1SPhilippe Reynes 	cmd->base.port = PORT_OTHER;
229516032be5SNikolay Aleksandrov 
229616032be5SNikolay Aleksandrov 	return 0;
229716032be5SNikolay Aleksandrov }
229816032be5SNikolay Aleksandrov 
22990c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev,
23000c465be1SJason Wang 				struct ethtool_coalesce *ec)
23010c465be1SJason Wang {
23020c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
23030c465be1SJason Wang 	int i, napi_weight;
23040c465be1SJason Wang 
2305a51e5206SJakub Kicinski 	if (ec->tx_max_coalesced_frames > 1 ||
2306a51e5206SJakub Kicinski 	    ec->rx_max_coalesced_frames != 1)
23070c465be1SJason Wang 		return -EINVAL;
23080c465be1SJason Wang 
23090c465be1SJason Wang 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
23100c465be1SJason Wang 	if (napi_weight ^ vi->sq[0].napi.weight) {
23110c465be1SJason Wang 		if (dev->flags & IFF_UP)
23120c465be1SJason Wang 			return -EBUSY;
23130c465be1SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
23140c465be1SJason Wang 			vi->sq[i].napi.weight = napi_weight;
23150c465be1SJason Wang 	}
23160c465be1SJason Wang 
23170c465be1SJason Wang 	return 0;
23180c465be1SJason Wang }
23190c465be1SJason Wang 
23200c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev,
23210c465be1SJason Wang 				struct ethtool_coalesce *ec)
23220c465be1SJason Wang {
23230c465be1SJason Wang 	struct ethtool_coalesce ec_default = {
23240c465be1SJason Wang 		.cmd = ETHTOOL_GCOALESCE,
23250c465be1SJason Wang 		.rx_max_coalesced_frames = 1,
23260c465be1SJason Wang 	};
23270c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
23280c465be1SJason Wang 
23290c465be1SJason Wang 	memcpy(ec, &ec_default, sizeof(ec_default));
23300c465be1SJason Wang 
23310c465be1SJason Wang 	if (vi->sq[0].napi.weight)
23320c465be1SJason Wang 		ec->tx_max_coalesced_frames = 1;
23330c465be1SJason Wang 
23340c465be1SJason Wang 	return 0;
23350c465be1SJason Wang }
23360c465be1SJason Wang 
233716032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
233816032be5SNikolay Aleksandrov {
233916032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
234016032be5SNikolay Aleksandrov 
234116032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
234216032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
234316032be5SNikolay Aleksandrov }
234416032be5SNikolay Aleksandrov 
2345faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi)
2346faa9b39fSJason Baron {
2347faa9b39fSJason Baron 	u32 speed;
2348faa9b39fSJason Baron 	u8 duplex;
2349faa9b39fSJason Baron 
2350faa9b39fSJason Baron 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2351faa9b39fSJason Baron 		return;
2352faa9b39fSJason Baron 
235364ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
235464ffa39dSMichael S. Tsirkin 
2355faa9b39fSJason Baron 	if (ethtool_validate_speed(speed))
2356faa9b39fSJason Baron 		vi->speed = speed;
235764ffa39dSMichael S. Tsirkin 
235864ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
235964ffa39dSMichael S. Tsirkin 
2360faa9b39fSJason Baron 	if (ethtool_validate_duplex(duplex))
2361faa9b39fSJason Baron 		vi->duplex = duplex;
2362faa9b39fSJason Baron }
2363faa9b39fSJason Baron 
23640fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
2365a51e5206SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
236666846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
23679f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
23688f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
2369d7dfc5cfSToshiaki Makita 	.get_strings = virtnet_get_strings,
2370d7dfc5cfSToshiaki Makita 	.get_sset_count = virtnet_get_sset_count,
2371d7dfc5cfSToshiaki Makita 	.get_ethtool_stats = virtnet_get_ethtool_stats,
2372d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
2373d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
2374074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
2375ebb6b4b1SPhilippe Reynes 	.get_link_ksettings = virtnet_get_link_ksettings,
2376ebb6b4b1SPhilippe Reynes 	.set_link_ksettings = virtnet_set_link_ksettings,
23770c465be1SJason Wang 	.set_coalesce = virtnet_set_coalesce,
23780c465be1SJason Wang 	.get_coalesce = virtnet_get_coalesce,
2379a9ea3fc6SHerbert Xu };
2380a9ea3fc6SHerbert Xu 
23819fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev)
23829fe7bfceSJohn Fastabend {
23839fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
23849fe7bfceSJohn Fastabend 	int i;
23859fe7bfceSJohn Fastabend 
23869fe7bfceSJohn Fastabend 	/* Make sure no work handler is accessing the device */
23879fe7bfceSJohn Fastabend 	flush_work(&vi->config_work);
23889fe7bfceSJohn Fastabend 
238905c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
23909fe7bfceSJohn Fastabend 	netif_device_detach(vi->dev);
239105c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
23929fe7bfceSJohn Fastabend 	cancel_delayed_work_sync(&vi->refill);
23939fe7bfceSJohn Fastabend 
23949fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
2395b92f1e67SWillem de Bruijn 		for (i = 0; i < vi->max_queue_pairs; i++) {
23969fe7bfceSJohn Fastabend 			napi_disable(&vi->rq[i].napi);
239778a57b48SWillem de Bruijn 			virtnet_napi_tx_disable(&vi->sq[i].napi);
2398b92f1e67SWillem de Bruijn 		}
23999fe7bfceSJohn Fastabend 	}
24009fe7bfceSJohn Fastabend }
24019fe7bfceSJohn Fastabend 
24029fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi);
24039fe7bfceSJohn Fastabend 
24049fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev)
24059fe7bfceSJohn Fastabend {
24069fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
24079fe7bfceSJohn Fastabend 	int err, i;
24089fe7bfceSJohn Fastabend 
24099fe7bfceSJohn Fastabend 	err = init_vqs(vi);
24109fe7bfceSJohn Fastabend 	if (err)
24119fe7bfceSJohn Fastabend 		return err;
24129fe7bfceSJohn Fastabend 
24139fe7bfceSJohn Fastabend 	virtio_device_ready(vdev);
24149fe7bfceSJohn Fastabend 
24159fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
24169fe7bfceSJohn Fastabend 		for (i = 0; i < vi->curr_queue_pairs; i++)
24179fe7bfceSJohn Fastabend 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
24189fe7bfceSJohn Fastabend 				schedule_delayed_work(&vi->refill, 0);
24199fe7bfceSJohn Fastabend 
2420b92f1e67SWillem de Bruijn 		for (i = 0; i < vi->max_queue_pairs; i++) {
2421e4e8452aSWillem de Bruijn 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2422b92f1e67SWillem de Bruijn 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2423b92f1e67SWillem de Bruijn 					       &vi->sq[i].napi);
2424b92f1e67SWillem de Bruijn 		}
24259fe7bfceSJohn Fastabend 	}
24269fe7bfceSJohn Fastabend 
242705c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
24289fe7bfceSJohn Fastabend 	netif_device_attach(vi->dev);
242905c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
24309fe7bfceSJohn Fastabend 	return err;
24319fe7bfceSJohn Fastabend }
24329fe7bfceSJohn Fastabend 
24333f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
24343f93522fSJason Wang {
24353f93522fSJason Wang 	struct scatterlist sg;
243612e57169SMichael S. Tsirkin 	vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
24373f93522fSJason Wang 
243812e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
24393f93522fSJason Wang 
24403f93522fSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
24413f93522fSJason Wang 				  VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
24423f93522fSJason Wang 		dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
24433f93522fSJason Wang 		return -EINVAL;
24443f93522fSJason Wang 	}
24453f93522fSJason Wang 
24463f93522fSJason Wang 	return 0;
24473f93522fSJason Wang }
24483f93522fSJason Wang 
24493f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
24503f93522fSJason Wang {
24513f93522fSJason Wang 	u64 offloads = 0;
24523f93522fSJason Wang 
24533f93522fSJason Wang 	if (!vi->guest_offloads)
24543f93522fSJason Wang 		return 0;
24553f93522fSJason Wang 
24563f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
24573f93522fSJason Wang }
24583f93522fSJason Wang 
24593f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
24603f93522fSJason Wang {
24613f93522fSJason Wang 	u64 offloads = vi->guest_offloads;
24623f93522fSJason Wang 
24633f93522fSJason Wang 	if (!vi->guest_offloads)
24643f93522fSJason Wang 		return 0;
24653f93522fSJason Wang 
24663f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
24673f93522fSJason Wang }
24683f93522fSJason Wang 
24699861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
24709861ce03SJakub Kicinski 			   struct netlink_ext_ack *extack)
2471f600b690SJohn Fastabend {
2472f600b690SJohn Fastabend 	unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
2473f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
2474f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
2475017b29c3SJason Wang 	u16 xdp_qp = 0, curr_qp;
2476672aafd5SJohn Fastabend 	int i, err;
2477f600b690SJohn Fastabend 
24783f93522fSJason Wang 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
24793f93522fSJason Wang 	    && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
248092502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
248192502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
248218ba58e1SJason Wang 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
248318ba58e1SJason Wang 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM))) {
248418ba58e1SJason Wang 		NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing LRO/CSUM, disable LRO/CSUM first");
2485f600b690SJohn Fastabend 		return -EOPNOTSUPP;
2486f600b690SJohn Fastabend 	}
2487f600b690SJohn Fastabend 
2488f600b690SJohn Fastabend 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
24894d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
2490f600b690SJohn Fastabend 		return -EINVAL;
2491f600b690SJohn Fastabend 	}
2492f600b690SJohn Fastabend 
2493f600b690SJohn Fastabend 	if (dev->mtu > max_sz) {
24944d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
2495f600b690SJohn Fastabend 		netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
2496f600b690SJohn Fastabend 		return -EINVAL;
2497f600b690SJohn Fastabend 	}
2498f600b690SJohn Fastabend 
2499672aafd5SJohn Fastabend 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
2500672aafd5SJohn Fastabend 	if (prog)
2501672aafd5SJohn Fastabend 		xdp_qp = nr_cpu_ids;
2502672aafd5SJohn Fastabend 
2503672aafd5SJohn Fastabend 	/* XDP requires extra queues for XDP_TX */
2504672aafd5SJohn Fastabend 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
250597c2c69eSXuan Zhuo 		netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
2506672aafd5SJohn Fastabend 			    curr_qp + xdp_qp, vi->max_queue_pairs);
250797c2c69eSXuan Zhuo 		xdp_qp = 0;
2508672aafd5SJohn Fastabend 	}
2509672aafd5SJohn Fastabend 
251003aa6d34SToshiaki Makita 	old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
251103aa6d34SToshiaki Makita 	if (!prog && !old_prog)
251203aa6d34SToshiaki Makita 		return 0;
251303aa6d34SToshiaki Makita 
251485192dbfSAndrii Nakryiko 	if (prog)
251585192dbfSAndrii Nakryiko 		bpf_prog_add(prog, vi->max_queue_pairs - 1);
25162de2f7f4SJohn Fastabend 
25174941d472SJason Wang 	/* Make sure NAPI is not using any XDP TX queues for RX. */
2518534da5e8SToshiaki Makita 	if (netif_running(dev)) {
2519534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
25204941d472SJason Wang 			napi_disable(&vi->rq[i].napi);
2521534da5e8SToshiaki Makita 			virtnet_napi_tx_disable(&vi->sq[i].napi);
2522534da5e8SToshiaki Makita 		}
2523534da5e8SToshiaki Makita 	}
25242de2f7f4SJohn Fastabend 
252503aa6d34SToshiaki Makita 	if (!prog) {
252603aa6d34SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
252703aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
252803aa6d34SToshiaki Makita 			if (i == 0)
252903aa6d34SToshiaki Makita 				virtnet_restore_guest_offloads(vi);
253003aa6d34SToshiaki Makita 		}
253103aa6d34SToshiaki Makita 		synchronize_net();
253203aa6d34SToshiaki Makita 	}
253303aa6d34SToshiaki Makita 
25344941d472SJason Wang 	err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
25354941d472SJason Wang 	if (err)
25364941d472SJason Wang 		goto err;
2537188313c1SToshiaki Makita 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
25384941d472SJason Wang 	vi->xdp_queue_pairs = xdp_qp;
2539f600b690SJohn Fastabend 
254003aa6d34SToshiaki Makita 	if (prog) {
254197c2c69eSXuan Zhuo 		vi->xdp_enabled = true;
2542f600b690SJohn Fastabend 		for (i = 0; i < vi->max_queue_pairs; i++) {
2543f600b690SJohn Fastabend 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
254403aa6d34SToshiaki Makita 			if (i == 0 && !old_prog)
25453f93522fSJason Wang 				virtnet_clear_guest_offloads(vi);
25463f93522fSJason Wang 		}
254797c2c69eSXuan Zhuo 	} else {
254897c2c69eSXuan Zhuo 		vi->xdp_enabled = false;
254903aa6d34SToshiaki Makita 	}
255003aa6d34SToshiaki Makita 
255103aa6d34SToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
2552f600b690SJohn Fastabend 		if (old_prog)
2553f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
2554534da5e8SToshiaki Makita 		if (netif_running(dev)) {
25554941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2556534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2557534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
2558534da5e8SToshiaki Makita 		}
2559f600b690SJohn Fastabend 	}
2560f600b690SJohn Fastabend 
2561f600b690SJohn Fastabend 	return 0;
25622de2f7f4SJohn Fastabend 
25634941d472SJason Wang err:
256403aa6d34SToshiaki Makita 	if (!prog) {
256503aa6d34SToshiaki Makita 		virtnet_clear_guest_offloads(vi);
25664941d472SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
256703aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
256803aa6d34SToshiaki Makita 	}
256903aa6d34SToshiaki Makita 
25708be4d9a4SToshiaki Makita 	if (netif_running(dev)) {
2571534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
25724941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
2573534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
2574534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
2575534da5e8SToshiaki Makita 		}
25768be4d9a4SToshiaki Makita 	}
25772de2f7f4SJohn Fastabend 	if (prog)
25782de2f7f4SJohn Fastabend 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
25792de2f7f4SJohn Fastabend 	return err;
2580f600b690SJohn Fastabend }
2581f600b690SJohn Fastabend 
2582f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2583f600b690SJohn Fastabend {
2584f600b690SJohn Fastabend 	switch (xdp->command) {
2585f600b690SJohn Fastabend 	case XDP_SETUP_PROG:
25869861ce03SJakub Kicinski 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
2587f600b690SJohn Fastabend 	default:
2588f600b690SJohn Fastabend 		return -EINVAL;
2589f600b690SJohn Fastabend 	}
2590f600b690SJohn Fastabend }
2591f600b690SJohn Fastabend 
2592ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
2593ba5e4426SSridhar Samudrala 				      size_t len)
2594ba5e4426SSridhar Samudrala {
2595ba5e4426SSridhar Samudrala 	struct virtnet_info *vi = netdev_priv(dev);
2596ba5e4426SSridhar Samudrala 	int ret;
2597ba5e4426SSridhar Samudrala 
2598ba5e4426SSridhar Samudrala 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2599ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
2600ba5e4426SSridhar Samudrala 
2601ba5e4426SSridhar Samudrala 	ret = snprintf(buf, len, "sby");
2602ba5e4426SSridhar Samudrala 	if (ret >= len)
2603ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
2604ba5e4426SSridhar Samudrala 
2605ba5e4426SSridhar Samudrala 	return 0;
2606ba5e4426SSridhar Samudrala }
2607ba5e4426SSridhar Samudrala 
2608a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev,
2609a02e8964SWillem de Bruijn 				netdev_features_t features)
2610a02e8964SWillem de Bruijn {
2611a02e8964SWillem de Bruijn 	struct virtnet_info *vi = netdev_priv(dev);
2612cf8691cbSMichael S. Tsirkin 	u64 offloads;
2613a02e8964SWillem de Bruijn 	int err;
2614a02e8964SWillem de Bruijn 
2615cf8691cbSMichael S. Tsirkin 	if ((dev->features ^ features) & NETIF_F_LRO) {
261697c2c69eSXuan Zhuo 		if (vi->xdp_enabled)
2617a02e8964SWillem de Bruijn 			return -EBUSY;
2618a02e8964SWillem de Bruijn 
2619a02e8964SWillem de Bruijn 		if (features & NETIF_F_LRO)
2620cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable;
2621a02e8964SWillem de Bruijn 		else
2622cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable &
2623cf8691cbSMichael S. Tsirkin 				   ~GUEST_OFFLOAD_LRO_MASK;
2624a02e8964SWillem de Bruijn 
2625a02e8964SWillem de Bruijn 		err = virtnet_set_guest_offloads(vi, offloads);
2626a02e8964SWillem de Bruijn 		if (err)
2627a02e8964SWillem de Bruijn 			return err;
26283618ad2aSTonghao Zhang 		vi->guest_offloads = offloads;
2629cf8691cbSMichael S. Tsirkin 	}
2630cf8691cbSMichael S. Tsirkin 
2631a02e8964SWillem de Bruijn 	return 0;
2632a02e8964SWillem de Bruijn }
2633a02e8964SWillem de Bruijn 
263476288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
263576288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
263676288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
263776288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
263876288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
26399c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
26402af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
26413fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
26421824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
26431824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
2644f4e63525SJakub Kicinski 	.ndo_bpf		= virtnet_xdp,
2645186b3c99SJason Wang 	.ndo_xdp_xmit		= virtnet_xdp_xmit,
26462836b4f2SVlad Yasevich 	.ndo_features_check	= passthru_features_check,
2647ba5e4426SSridhar Samudrala 	.ndo_get_phys_port_name	= virtnet_get_phys_port_name,
2648a02e8964SWillem de Bruijn 	.ndo_set_features	= virtnet_set_features,
264976288b4eSStephen Hemminger };
265076288b4eSStephen Hemminger 
2651586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
26529f4d26d0SMark McLoughlin {
2653586d17c5SJason Wang 	struct virtnet_info *vi =
2654586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
26559f4d26d0SMark McLoughlin 	u16 v;
26569f4d26d0SMark McLoughlin 
2657855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
2658855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
2659507613bfSMichael S. Tsirkin 		return;
2660586d17c5SJason Wang 
2661586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
2662ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
2663586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
2664586d17c5SJason Wang 	}
26659f4d26d0SMark McLoughlin 
26669f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
26679f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
26689f4d26d0SMark McLoughlin 
26699f4d26d0SMark McLoughlin 	if (vi->status == v)
2670507613bfSMichael S. Tsirkin 		return;
26719f4d26d0SMark McLoughlin 
26729f4d26d0SMark McLoughlin 	vi->status = v;
26739f4d26d0SMark McLoughlin 
26749f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
2675faa9b39fSJason Baron 		virtnet_update_settings(vi);
26769f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
2677986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
26789f4d26d0SMark McLoughlin 	} else {
26799f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
2680986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
26819f4d26d0SMark McLoughlin 	}
26829f4d26d0SMark McLoughlin }
26839f4d26d0SMark McLoughlin 
26849f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
26859f4d26d0SMark McLoughlin {
26869f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
26879f4d26d0SMark McLoughlin 
26883b07e9caSTejun Heo 	schedule_work(&vi->config_work);
26899f4d26d0SMark McLoughlin }
26909f4d26d0SMark McLoughlin 
2691986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
2692986a4f4dSJason Wang {
2693d4fb84eeSAndrey Vagin 	int i;
2694d4fb84eeSAndrey Vagin 
2695ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
26965198d545SJakub Kicinski 		__netif_napi_del(&vi->rq[i].napi);
26975198d545SJakub Kicinski 		__netif_napi_del(&vi->sq[i].napi);
2698ab3971b1SJason Wang 	}
2699d4fb84eeSAndrey Vagin 
27005198d545SJakub Kicinski 	/* We called __netif_napi_del(),
2701963abe5cSEric Dumazet 	 * we need to respect an RCU grace period before freeing vi->rq
2702963abe5cSEric Dumazet 	 */
2703963abe5cSEric Dumazet 	synchronize_net();
2704963abe5cSEric Dumazet 
2705986a4f4dSJason Wang 	kfree(vi->rq);
2706986a4f4dSJason Wang 	kfree(vi->sq);
270712e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
2708986a4f4dSJason Wang }
2709986a4f4dSJason Wang 
271047315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi)
2711986a4f4dSJason Wang {
2712f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
2713986a4f4dSJason Wang 	int i;
2714986a4f4dSJason Wang 
2715986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2716986a4f4dSJason Wang 		while (vi->rq[i].pages)
2717986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
2718f600b690SJohn Fastabend 
2719f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
2720f600b690SJohn Fastabend 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
2721f600b690SJohn Fastabend 		if (old_prog)
2722f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
2723986a4f4dSJason Wang 	}
272447315329SJohn Fastabend }
272547315329SJohn Fastabend 
272647315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi)
272747315329SJohn Fastabend {
272847315329SJohn Fastabend 	rtnl_lock();
272947315329SJohn Fastabend 	_free_receive_bufs(vi);
2730f600b690SJohn Fastabend 	rtnl_unlock();
2731986a4f4dSJason Wang }
2732986a4f4dSJason Wang 
2733fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
2734fb51879dSMichael Dalton {
2735fb51879dSMichael Dalton 	int i;
2736fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
2737fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
2738fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
2739fb51879dSMichael Dalton }
2740fb51879dSMichael Dalton 
2741986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
2742986a4f4dSJason Wang {
2743986a4f4dSJason Wang 	void *buf;
2744986a4f4dSJason Wang 	int i;
2745986a4f4dSJason Wang 
2746986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2747986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
274856434a01SJohn Fastabend 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
27495050471dSToshiaki Makita 			if (!is_xdp_frame(buf))
2750986a4f4dSJason Wang 				dev_kfree_skb(buf);
275156434a01SJohn Fastabend 			else
27525050471dSToshiaki Makita 				xdp_return_frame(ptr_to_xdp(buf));
275356434a01SJohn Fastabend 		}
2754986a4f4dSJason Wang 	}
2755986a4f4dSJason Wang 
2756986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2757986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
2758986a4f4dSJason Wang 
2759986a4f4dSJason Wang 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
2760ab7db917SMichael Dalton 			if (vi->mergeable_rx_bufs) {
2761680557cfSMichael S. Tsirkin 				put_page(virt_to_head_page(buf));
2762ab7db917SMichael Dalton 			} else if (vi->big_packets) {
2763fa9fac17SAndrey Vagin 				give_pages(&vi->rq[i], buf);
2764ab7db917SMichael Dalton 			} else {
2765f6b10209SJason Wang 				put_page(virt_to_head_page(buf));
2766986a4f4dSJason Wang 			}
2767986a4f4dSJason Wang 		}
2768986a4f4dSJason Wang 	}
2769ab7db917SMichael Dalton }
2770986a4f4dSJason Wang 
2771e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
2772e9d7417bSJason Wang {
2773e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
2774e9d7417bSJason Wang 
2775310974faSPeter Xu 	virtnet_clean_affinity(vi);
2776986a4f4dSJason Wang 
2777e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
2778986a4f4dSJason Wang 
2779986a4f4dSJason Wang 	virtnet_free_queues(vi);
2780986a4f4dSJason Wang }
2781986a4f4dSJason Wang 
2782d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at
2783d85b758fSMichael S. Tsirkin  * least one full packet?
2784d85b758fSMichael S. Tsirkin  * Logic below assumes the mergeable buffer header is used.
2785d85b758fSMichael S. Tsirkin  */
2786d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
2787d85b758fSMichael S. Tsirkin {
2788d85b758fSMichael S. Tsirkin 	const unsigned int hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2789d85b758fSMichael S. Tsirkin 	unsigned int rq_size = virtqueue_get_vring_size(vq);
2790d85b758fSMichael S. Tsirkin 	unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
2791d85b758fSMichael S. Tsirkin 	unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
2792d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
2793d85b758fSMichael S. Tsirkin 
2794f0c3192cSMichael S. Tsirkin 	return max(max(min_buf_len, hdr_len) - hdr_len,
2795f0c3192cSMichael S. Tsirkin 		   (unsigned int)GOOD_PACKET_LEN);
2796d85b758fSMichael S. Tsirkin }
2797d85b758fSMichael S. Tsirkin 
2798986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
2799986a4f4dSJason Wang {
2800986a4f4dSJason Wang 	vq_callback_t **callbacks;
2801986a4f4dSJason Wang 	struct virtqueue **vqs;
2802986a4f4dSJason Wang 	int ret = -ENOMEM;
2803986a4f4dSJason Wang 	int i, total_vqs;
2804986a4f4dSJason Wang 	const char **names;
2805d45b897bSMichael S. Tsirkin 	bool *ctx;
2806986a4f4dSJason Wang 
2807986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
2808986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
2809986a4f4dSJason Wang 	 * possible control vq.
2810986a4f4dSJason Wang 	 */
2811986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
2812986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
2813986a4f4dSJason Wang 
2814986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
28156396bb22SKees Cook 	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
2816986a4f4dSJason Wang 	if (!vqs)
2817986a4f4dSJason Wang 		goto err_vq;
28186da2ec56SKees Cook 	callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
2819986a4f4dSJason Wang 	if (!callbacks)
2820986a4f4dSJason Wang 		goto err_callback;
28216da2ec56SKees Cook 	names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
2822986a4f4dSJason Wang 	if (!names)
2823986a4f4dSJason Wang 		goto err_names;
2824192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
28256396bb22SKees Cook 		ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
2826d45b897bSMichael S. Tsirkin 		if (!ctx)
2827d45b897bSMichael S. Tsirkin 			goto err_ctx;
2828d45b897bSMichael S. Tsirkin 	} else {
2829d45b897bSMichael S. Tsirkin 		ctx = NULL;
2830d45b897bSMichael S. Tsirkin 	}
2831986a4f4dSJason Wang 
2832986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
2833986a4f4dSJason Wang 	if (vi->has_cvq) {
2834986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
2835986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
2836986a4f4dSJason Wang 	}
2837986a4f4dSJason Wang 
2838986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
2839986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2840986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
2841986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
2842986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
2843986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
2844986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
2845986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
2846d45b897bSMichael S. Tsirkin 		if (ctx)
2847d45b897bSMichael S. Tsirkin 			ctx[rxq2vq(i)] = true;
2848986a4f4dSJason Wang 	}
2849986a4f4dSJason Wang 
2850a2f7dc00SXianting Tian 	ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
2851d45b897bSMichael S. Tsirkin 				  names, ctx, NULL);
2852986a4f4dSJason Wang 	if (ret)
2853986a4f4dSJason Wang 		goto err_find;
2854986a4f4dSJason Wang 
2855986a4f4dSJason Wang 	if (vi->has_cvq) {
2856986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
2857986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
2858f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2859986a4f4dSJason Wang 	}
2860986a4f4dSJason Wang 
2861986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2862986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
2863d85b758fSMichael S. Tsirkin 		vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
2864986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
2865986a4f4dSJason Wang 	}
2866986a4f4dSJason Wang 
28672fa3c8a8STonghao Zhang 	/* run here: ret == 0. */
2868986a4f4dSJason Wang 
2869986a4f4dSJason Wang 
2870986a4f4dSJason Wang err_find:
2871d45b897bSMichael S. Tsirkin 	kfree(ctx);
2872d45b897bSMichael S. Tsirkin err_ctx:
2873986a4f4dSJason Wang 	kfree(names);
2874986a4f4dSJason Wang err_names:
2875986a4f4dSJason Wang 	kfree(callbacks);
2876986a4f4dSJason Wang err_callback:
2877986a4f4dSJason Wang 	kfree(vqs);
2878986a4f4dSJason Wang err_vq:
2879986a4f4dSJason Wang 	return ret;
2880986a4f4dSJason Wang }
2881986a4f4dSJason Wang 
2882986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
2883986a4f4dSJason Wang {
2884986a4f4dSJason Wang 	int i;
2885986a4f4dSJason Wang 
2886122b84a1SMax Gurtovoy 	if (vi->has_cvq) {
288712e57169SMichael S. Tsirkin 		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
288812e57169SMichael S. Tsirkin 		if (!vi->ctrl)
288912e57169SMichael S. Tsirkin 			goto err_ctrl;
2890122b84a1SMax Gurtovoy 	} else {
2891122b84a1SMax Gurtovoy 		vi->ctrl = NULL;
2892122b84a1SMax Gurtovoy 	}
28936396bb22SKees Cook 	vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
2894986a4f4dSJason Wang 	if (!vi->sq)
2895986a4f4dSJason Wang 		goto err_sq;
28966396bb22SKees Cook 	vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
2897008d4278SAmerigo Wang 	if (!vi->rq)
2898986a4f4dSJason Wang 		goto err_rq;
2899986a4f4dSJason Wang 
2900986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
2901986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2902986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
2903986a4f4dSJason Wang 		netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
2904986a4f4dSJason Wang 			       napi_weight);
29051d11e732SWillem de Bruijn 		netif_tx_napi_add(vi->dev, &vi->sq[i].napi, virtnet_poll_tx,
2906b92f1e67SWillem de Bruijn 				  napi_tx ? napi_weight : 0);
2907986a4f4dSJason Wang 
2908986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
29095377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
2910986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
2911d7dfc5cfSToshiaki Makita 
2912d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->rq[i].stats.syncp);
2913d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->sq[i].stats.syncp);
2914986a4f4dSJason Wang 	}
2915986a4f4dSJason Wang 
2916986a4f4dSJason Wang 	return 0;
2917986a4f4dSJason Wang 
2918986a4f4dSJason Wang err_rq:
2919986a4f4dSJason Wang 	kfree(vi->sq);
2920986a4f4dSJason Wang err_sq:
292112e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
292212e57169SMichael S. Tsirkin err_ctrl:
2923986a4f4dSJason Wang 	return -ENOMEM;
2924e9d7417bSJason Wang }
2925e9d7417bSJason Wang 
29263f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
29273f9c10b0SAmit Shah {
2928986a4f4dSJason Wang 	int ret;
29293f9c10b0SAmit Shah 
2930986a4f4dSJason Wang 	/* Allocate send & receive queues */
2931986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
2932986a4f4dSJason Wang 	if (ret)
2933986a4f4dSJason Wang 		goto err;
29343f9c10b0SAmit Shah 
2935986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
2936986a4f4dSJason Wang 	if (ret)
2937986a4f4dSJason Wang 		goto err_free;
29383f9c10b0SAmit Shah 
293947be2479SWanlong Gao 	get_online_cpus();
29408898c21cSWanlong Gao 	virtnet_set_affinity(vi);
294147be2479SWanlong Gao 	put_online_cpus();
294247be2479SWanlong Gao 
29433f9c10b0SAmit Shah 	return 0;
2944986a4f4dSJason Wang 
2945986a4f4dSJason Wang err_free:
2946986a4f4dSJason Wang 	virtnet_free_queues(vi);
2947986a4f4dSJason Wang err:
2948986a4f4dSJason Wang 	return ret;
29493f9c10b0SAmit Shah }
29503f9c10b0SAmit Shah 
2951fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
2952fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
2953718ad681Sstephen hemminger 		char *buf)
2954fbf28d78SMichael Dalton {
2955fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
2956fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
29573cc81a9aSJason Wang 	unsigned int headroom = virtnet_get_headroom(vi);
29583cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
29595377d758SJohannes Berg 	struct ewma_pkt_len *avg;
2960fbf28d78SMichael Dalton 
2961fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
2962fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
2963d85b758fSMichael S. Tsirkin 	return sprintf(buf, "%u\n",
29643cc81a9aSJason Wang 		       get_mergeable_buf_len(&vi->rq[queue_index], avg,
29653cc81a9aSJason Wang 				       SKB_DATA_ALIGN(headroom + tailroom)));
2966fbf28d78SMichael Dalton }
2967fbf28d78SMichael Dalton 
2968fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
2969fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
2970fbf28d78SMichael Dalton 
2971fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
2972fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
2973fbf28d78SMichael Dalton 	NULL
2974fbf28d78SMichael Dalton };
2975fbf28d78SMichael Dalton 
2976fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
2977fbf28d78SMichael Dalton 	.name = "virtio_net",
2978fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
2979fbf28d78SMichael Dalton };
2980fbf28d78SMichael Dalton #endif
2981fbf28d78SMichael Dalton 
2982892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
2983892d6eb1SJason Wang 				    unsigned int fbit,
2984892d6eb1SJason Wang 				    const char *fname, const char *dname)
2985892d6eb1SJason Wang {
2986892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
2987892d6eb1SJason Wang 		return false;
2988892d6eb1SJason Wang 
2989892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
2990892d6eb1SJason Wang 		fname, dname);
2991892d6eb1SJason Wang 
2992892d6eb1SJason Wang 	return true;
2993892d6eb1SJason Wang }
2994892d6eb1SJason Wang 
2995892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
2996892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
2997892d6eb1SJason Wang 
2998892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
2999892d6eb1SJason Wang {
3000892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3001892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3002892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3003892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3004892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3005892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3006892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3007892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3008892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3009892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
3010892d6eb1SJason Wang 		return false;
3011892d6eb1SJason Wang 	}
3012892d6eb1SJason Wang 
3013892d6eb1SJason Wang 	return true;
3014892d6eb1SJason Wang }
3015892d6eb1SJason Wang 
3016d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
3017d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
3018d0c2c997SJarod Wilson 
3019fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev)
3020296f96fcSRusty Russell {
30216ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
30226ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
30236ba42248SMichael S. Tsirkin 			__func__);
30246ba42248SMichael S. Tsirkin 		return -EINVAL;
30256ba42248SMichael S. Tsirkin 	}
30266ba42248SMichael S. Tsirkin 
3027892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
3028892d6eb1SJason Wang 		return -EINVAL;
3029892d6eb1SJason Wang 
3030fe36cbe0SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3031fe36cbe0SMichael S. Tsirkin 		int mtu = virtio_cread16(vdev,
3032fe36cbe0SMichael S. Tsirkin 					 offsetof(struct virtio_net_config,
3033fe36cbe0SMichael S. Tsirkin 						  mtu));
3034fe36cbe0SMichael S. Tsirkin 		if (mtu < MIN_MTU)
3035fe36cbe0SMichael S. Tsirkin 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3036fe36cbe0SMichael S. Tsirkin 	}
3037fe36cbe0SMichael S. Tsirkin 
3038fe36cbe0SMichael S. Tsirkin 	return 0;
3039fe36cbe0SMichael S. Tsirkin }
3040fe36cbe0SMichael S. Tsirkin 
3041fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev)
3042fe36cbe0SMichael S. Tsirkin {
3043d7dfc5cfSToshiaki Makita 	int i, err = -ENOMEM;
3044fe36cbe0SMichael S. Tsirkin 	struct net_device *dev;
3045fe36cbe0SMichael S. Tsirkin 	struct virtnet_info *vi;
3046fe36cbe0SMichael S. Tsirkin 	u16 max_queue_pairs;
3047fe36cbe0SMichael S. Tsirkin 	int mtu;
3048fe36cbe0SMichael S. Tsirkin 
3049986a4f4dSJason Wang 	/* Find if host supports multiqueue virtio_net device */
3050855e0c52SRusty Russell 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
3051855e0c52SRusty Russell 				   struct virtio_net_config,
3052855e0c52SRusty Russell 				   max_virtqueue_pairs, &max_queue_pairs);
3053986a4f4dSJason Wang 
3054986a4f4dSJason Wang 	/* We need at least 2 queue's */
3055986a4f4dSJason Wang 	if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3056986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3057986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3058986a4f4dSJason Wang 		max_queue_pairs = 1;
3059296f96fcSRusty Russell 
3060296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
3061986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3062296f96fcSRusty Russell 	if (!dev)
3063296f96fcSRusty Russell 		return -ENOMEM;
3064296f96fcSRusty Russell 
3065296f96fcSRusty Russell 	/* Set up network device as normal. */
3066ab5bd583SXuan Zhuo 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3067ab5bd583SXuan Zhuo 			   IFF_TX_SKB_NO_LINEAR;
306876288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
3069296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
30703fa2a1dfSstephen hemminger 
30717ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
3072296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
3073296f96fcSRusty Russell 
3074296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
307598e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3076296f96fcSRusty Russell 		/* This opens up the world of extra features. */
307748900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
307898e778c9SMichał Mirosław 		if (csum)
307948900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
308098e778c9SMichał Mirosław 
308198e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3082e078de03SDavid S. Miller 			dev->hw_features |= NETIF_F_TSO
308334a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
308434a48579SRusty Russell 		}
30855539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
308698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
308798e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
308898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
308998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
309098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
309198e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
309298e778c9SMichał Mirosław 
309341f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
309441f2f127SJason Wang 
309598e778c9SMichał Mirosław 		if (gso)
3096e078de03SDavid S. Miller 			dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
309798e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
3098296f96fcSRusty Russell 	}
30994f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
31004f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
3101a02e8964SWillem de Bruijn 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3102a02e8964SWillem de Bruijn 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3103a02e8964SWillem de Bruijn 		dev->features |= NETIF_F_LRO;
3104cf8691cbSMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3105a02e8964SWillem de Bruijn 		dev->hw_features |= NETIF_F_LRO;
3106296f96fcSRusty Russell 
31074fda8302SJason Wang 	dev->vlan_features = dev->features;
31084fda8302SJason Wang 
3109d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
3110d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
3111d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
3112d0c2c997SJarod Wilson 
3113296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
3114855e0c52SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
3115855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
3116a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
3117855e0c52SRusty Russell 				   dev->dev_addr, dev->addr_len);
3118855e0c52SRusty Russell 	else
3119f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
3120296f96fcSRusty Russell 
3121296f96fcSRusty Russell 	/* Set up our device-specific information */
3122296f96fcSRusty Russell 	vi = netdev_priv(dev);
3123296f96fcSRusty Russell 	vi->dev = dev;
3124296f96fcSRusty Russell 	vi->vdev = vdev;
3125d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
3126827da44cSJohn Stultz 
3127586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
3128296f96fcSRusty Russell 
312997402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
31308e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
31318e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3132e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) ||
3133e3e3c423SVlad Yasevich 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO))
313497402b96SHerbert Xu 		vi->big_packets = true;
313597402b96SHerbert Xu 
31363f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
31373f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
31383f2c31d9SMark McLoughlin 
3139d04302b3SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3140d04302b3SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3141012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3142012873d0SMichael S. Tsirkin 	else
3143012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
3144012873d0SMichael S. Tsirkin 
314575993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
314675993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3147e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
3148e7428e95SMichael S. Tsirkin 
3149986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3150986a4f4dSJason Wang 		vi->has_cvq = true;
3151986a4f4dSJason Wang 
315214de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
315314de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
315414de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
315514de9d11SAaron Conole 					      mtu));
315693a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
3157fe36cbe0SMichael S. Tsirkin 			/* Should never trigger: MTU was previously validated
3158fe36cbe0SMichael S. Tsirkin 			 * in virtnet_validate.
3159fe36cbe0SMichael S. Tsirkin 			 */
31607934b481SYuval Shaia 			dev_err(&vdev->dev,
31617934b481SYuval Shaia 				"device MTU appears to have changed it is now %d < %d",
31627934b481SYuval Shaia 				mtu, dev->min_mtu);
3163411ea23aSDan Carpenter 			err = -EINVAL;
3164d7dfc5cfSToshiaki Makita 			goto free;
3165fe36cbe0SMichael S. Tsirkin 		}
3166fe36cbe0SMichael S. Tsirkin 
3167d0c2c997SJarod Wilson 		dev->mtu = mtu;
316893a205eeSAaron Conole 		dev->max_mtu = mtu;
31692e123b44SMichael S. Tsirkin 
31702e123b44SMichael S. Tsirkin 		/* TODO: size buffers correctly in this case. */
31712e123b44SMichael S. Tsirkin 		if (dev->mtu > ETH_DATA_LEN)
31722e123b44SMichael S. Tsirkin 			vi->big_packets = true;
317314de9d11SAaron Conole 	}
317414de9d11SAaron Conole 
3175012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
3176012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
31776ebbc1a6SZhangjie \(HZ\) 
317844900010SJason Wang 	/* Enable multiqueue by default */
317944900010SJason Wang 	if (num_online_cpus() >= max_queue_pairs)
318044900010SJason Wang 		vi->curr_queue_pairs = max_queue_pairs;
318144900010SJason Wang 	else
318244900010SJason Wang 		vi->curr_queue_pairs = num_online_cpus();
3183986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
3184986a4f4dSJason Wang 
3185986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
31863f9c10b0SAmit Shah 	err = init_vqs(vi);
3187d2a7dddaSMichael S. Tsirkin 	if (err)
3188d7dfc5cfSToshiaki Makita 		goto free;
3189d2a7dddaSMichael S. Tsirkin 
3190fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
3191fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
3192fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3193fbf28d78SMichael Dalton #endif
31940f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
31950f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3196986a4f4dSJason Wang 
319716032be5SNikolay Aleksandrov 	virtnet_init_settings(dev);
319816032be5SNikolay Aleksandrov 
3199ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3200ba5e4426SSridhar Samudrala 		vi->failover = net_failover_create(vi->dev);
32014b8e6ac4SWei Yongjun 		if (IS_ERR(vi->failover)) {
32024b8e6ac4SWei Yongjun 			err = PTR_ERR(vi->failover);
3203ba5e4426SSridhar Samudrala 			goto free_vqs;
3204ba5e4426SSridhar Samudrala 		}
32054b8e6ac4SWei Yongjun 	}
3206ba5e4426SSridhar Samudrala 
3207296f96fcSRusty Russell 	err = register_netdev(dev);
3208296f96fcSRusty Russell 	if (err) {
3209296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
3210ba5e4426SSridhar Samudrala 		goto free_failover;
3211296f96fcSRusty Russell 	}
3212b3369c1fSRusty Russell 
32134baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
32144baf1e33SMichael S. Tsirkin 
32158017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
32168de4b2f3SWanlong Gao 	if (err) {
32178de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
3218f00e35e2Swangyunjian 		goto free_unregister_netdev;
32198de4b2f3SWanlong Gao 	}
32208de4b2f3SWanlong Gao 
3221a220871bSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
322244900010SJason Wang 
3223167c25e4SJason Wang 	/* Assume link up if device can't report link status,
3224167c25e4SJason Wang 	   otherwise get link status from config. */
3225167c25e4SJason Wang 	netif_carrier_off(dev);
3226bda7fab5SJay Vosburgh 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
32273b07e9caSTejun Heo 		schedule_work(&vi->config_work);
3228167c25e4SJason Wang 	} else {
3229167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
3230faa9b39fSJason Baron 		virtnet_update_settings(vi);
32314783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
3232167c25e4SJason Wang 	}
32339f4d26d0SMark McLoughlin 
32343f93522fSJason Wang 	for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
32353f93522fSJason Wang 		if (virtio_has_feature(vi->vdev, guest_offloads[i]))
32363f93522fSJason Wang 			set_bit(guest_offloads[i], &vi->guest_offloads);
3237a02e8964SWillem de Bruijn 	vi->guest_offloads_capable = vi->guest_offloads;
32383f93522fSJason Wang 
3239986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3240986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
3241986a4f4dSJason Wang 
3242296f96fcSRusty Russell 	return 0;
3243296f96fcSRusty Russell 
3244f00e35e2Swangyunjian free_unregister_netdev:
324502465555SMichael S. Tsirkin 	vi->vdev->config->reset(vdev);
324602465555SMichael S. Tsirkin 
3247b3369c1fSRusty Russell 	unregister_netdev(dev);
3248ba5e4426SSridhar Samudrala free_failover:
3249ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
3250d2a7dddaSMichael S. Tsirkin free_vqs:
3251986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
3252fb51879dSMichael Dalton 	free_receive_page_frags(vi);
3253e9d7417bSJason Wang 	virtnet_del_vqs(vi);
3254296f96fcSRusty Russell free:
3255296f96fcSRusty Russell 	free_netdev(dev);
3256296f96fcSRusty Russell 	return err;
3257296f96fcSRusty Russell }
3258296f96fcSRusty Russell 
325904486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
3260296f96fcSRusty Russell {
326104486ed0SAmit Shah 	vi->vdev->config->reset(vi->vdev);
3262830a8a97SShirley Ma 
3263830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
32649ab86bbcSShirley Ma 	free_unused_bufs(vi);
3265fb6813f4SRusty Russell 
3266986a4f4dSJason Wang 	free_receive_bufs(vi);
3267d2a7dddaSMichael S. Tsirkin 
3268fb51879dSMichael Dalton 	free_receive_page_frags(vi);
3269fb51879dSMichael Dalton 
3270986a4f4dSJason Wang 	virtnet_del_vqs(vi);
327104486ed0SAmit Shah }
327204486ed0SAmit Shah 
32738cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
327404486ed0SAmit Shah {
327504486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
327604486ed0SAmit Shah 
32778017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
32788de4b2f3SWanlong Gao 
3279102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
3280102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
3281586d17c5SJason Wang 
328204486ed0SAmit Shah 	unregister_netdev(vi->dev);
328304486ed0SAmit Shah 
3284ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
3285ba5e4426SSridhar Samudrala 
328604486ed0SAmit Shah 	remove_vq_common(vi);
3287fb6813f4SRusty Russell 
328874b2553fSRusty Russell 	free_netdev(vi->dev);
3289296f96fcSRusty Russell }
3290296f96fcSRusty Russell 
329167a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
32920741bcb5SAmit Shah {
32930741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
32940741bcb5SAmit Shah 
32958017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
32969fe7bfceSJohn Fastabend 	virtnet_freeze_down(vdev);
32970741bcb5SAmit Shah 	remove_vq_common(vi);
32980741bcb5SAmit Shah 
32990741bcb5SAmit Shah 	return 0;
33000741bcb5SAmit Shah }
33010741bcb5SAmit Shah 
330267a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
33030741bcb5SAmit Shah {
33040741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
33059fe7bfceSJohn Fastabend 	int err;
33060741bcb5SAmit Shah 
33079fe7bfceSJohn Fastabend 	err = virtnet_restore_up(vdev);
33080741bcb5SAmit Shah 	if (err)
33090741bcb5SAmit Shah 		return err;
3310986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
3311986a4f4dSJason Wang 
33128017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
3313*3f2869caSXie Yongji 	if (err) {
3314*3f2869caSXie Yongji 		virtnet_freeze_down(vdev);
3315*3f2869caSXie Yongji 		remove_vq_common(vi);
3316ec9debbdSJason Wang 		return err;
3317*3f2869caSXie Yongji 	}
3318ec9debbdSJason Wang 
33190741bcb5SAmit Shah 	return 0;
33200741bcb5SAmit Shah }
33210741bcb5SAmit Shah 
3322296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
3323296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
3324296f96fcSRusty Russell 	{ 0 },
3325296f96fcSRusty Russell };
3326296f96fcSRusty Russell 
3327f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \
3328f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
3329f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MAC, \
3330f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
3331f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
3332f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
3333f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
3334f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
3335f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
3336f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
3337faa9b39fSJason Baron 	VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
33389805069dSSridhar Samudrala 	VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY
3339f3358507SMichael S. Tsirkin 
3340c45a6816SRusty Russell static unsigned int features[] = {
3341f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
3342f3358507SMichael S. Tsirkin };
3343f3358507SMichael S. Tsirkin 
3344f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = {
3345f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
3346f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GSO,
3347e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
3348c45a6816SRusty Russell };
3349c45a6816SRusty Russell 
335022402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
3351c45a6816SRusty Russell 	.feature_table = features,
3352c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
3353f3358507SMichael S. Tsirkin 	.feature_table_legacy = features_legacy,
3354f3358507SMichael S. Tsirkin 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
3355296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
3356296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
3357296f96fcSRusty Russell 	.id_table =	id_table,
3358fe36cbe0SMichael S. Tsirkin 	.validate =	virtnet_validate,
3359296f96fcSRusty Russell 	.probe =	virtnet_probe,
33608cc085d6SBill Pemberton 	.remove =	virtnet_remove,
33619f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
336289107000SAaron Lu #ifdef CONFIG_PM_SLEEP
33630741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
33640741bcb5SAmit Shah 	.restore =	virtnet_restore,
33650741bcb5SAmit Shah #endif
3366296f96fcSRusty Russell };
3367296f96fcSRusty Russell 
33688017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
33698017c279SSebastian Andrzej Siewior {
33708017c279SSebastian Andrzej Siewior 	int ret;
33718017c279SSebastian Andrzej Siewior 
337273c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
33738017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
33748017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
33758017c279SSebastian Andrzej Siewior 	if (ret < 0)
33768017c279SSebastian Andrzej Siewior 		goto out;
33778017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
337873c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
33798017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
33808017c279SSebastian Andrzej Siewior 	if (ret)
33818017c279SSebastian Andrzej Siewior 		goto err_dead;
33828017c279SSebastian Andrzej Siewior 
33838017c279SSebastian Andrzej Siewior         ret = register_virtio_driver(&virtio_net_driver);
33848017c279SSebastian Andrzej Siewior 	if (ret)
33858017c279SSebastian Andrzej Siewior 		goto err_virtio;
33868017c279SSebastian Andrzej Siewior 	return 0;
33878017c279SSebastian Andrzej Siewior err_virtio:
33888017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
33898017c279SSebastian Andrzej Siewior err_dead:
33908017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
33918017c279SSebastian Andrzej Siewior out:
33928017c279SSebastian Andrzej Siewior 	return ret;
33938017c279SSebastian Andrzej Siewior }
33948017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
33958017c279SSebastian Andrzej Siewior 
33968017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
33978017c279SSebastian Andrzej Siewior {
3398cfa0ebc9SAndrew Jones 	unregister_virtio_driver(&virtio_net_driver);
33998017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
34008017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
34018017c279SSebastian Andrzej Siewior }
34028017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
3403296f96fcSRusty Russell 
3404296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
3405296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
3406296f96fcSRusty Russell MODULE_LICENSE("GPL");
3407