xref: /openbmc/linux/drivers/net/virtio_net.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
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>
2549e47a5bSJakub Kicinski #include <net/netdev_rx_queue.h>
26296f96fcSRusty Russell 
27d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
286c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
296c0cd7c0SDor Laor 
3031c03aefSWillem de Bruijn static bool csum = true, gso = true, napi_tx = true;
3134a48579SRusty Russell module_param(csum, bool, 0444);
3234a48579SRusty Russell module_param(gso, bool, 0444);
33b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644);
3434a48579SRusty Russell 
35296f96fcSRusty Russell /* FIXME: MTU in config. */
365061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
373f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
38296f96fcSRusty Russell 
39f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
40f6b10209SJason Wang 
412de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
422de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256
432de2f7f4SJohn Fastabend 
442471c75eSJesper Dangaard Brouer /* Separating two types of XDP xmit */
452471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_TX		BIT(0)
462471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_REDIR	BIT(1)
472471c75eSJesper Dangaard Brouer 
485050471dSToshiaki Makita #define VIRTIO_XDP_FLAG	BIT(0)
495050471dSToshiaki Makita 
505377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet
515377d758SJohannes Berg  * buffer size when refilling RX rings. As the entire RX ring may be refilled
525377d758SJohannes Berg  * at once, the weight is chosen so that the EWMA will be insensitive to short-
535377d758SJohannes Berg  * term, transient changes in packet size.
54ab7db917SMichael Dalton  */
55eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64)
56ab7db917SMichael Dalton 
5766846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
582a41f71dSAlex Williamson 
597acd4329SColin Ian King static const unsigned long guest_offloads[] = {
607acd4329SColin Ian King 	VIRTIO_NET_F_GUEST_TSO4,
613f93522fSJason Wang 	VIRTIO_NET_F_GUEST_TSO6,
623f93522fSJason Wang 	VIRTIO_NET_F_GUEST_ECN,
63e59ff2c4SJason Wang 	VIRTIO_NET_F_GUEST_UFO,
64418044e1SAndrew Melnychenko 	VIRTIO_NET_F_GUEST_CSUM,
65418044e1SAndrew Melnychenko 	VIRTIO_NET_F_GUEST_USO4,
66be50da3eSJiri Pirko 	VIRTIO_NET_F_GUEST_USO6,
67be50da3eSJiri Pirko 	VIRTIO_NET_F_GUEST_HDRLEN
687acd4329SColin Ian King };
693f93522fSJason Wang 
70dbcf24d1SJason Wang #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
711a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
721a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_ECN)  | \
73418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_UFO)  | \
74418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_USO4) | \
75418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_USO6))
761a03b8a3STonghao Zhang 
77d7dfc5cfSToshiaki Makita struct virtnet_stat_desc {
78d7dfc5cfSToshiaki Makita 	char desc[ETH_GSTRING_LEN];
79d7dfc5cfSToshiaki Makita 	size_t offset;
803fa2a1dfSstephen hemminger };
813fa2a1dfSstephen hemminger 
82d7dfc5cfSToshiaki Makita struct virtnet_sq_stats {
83d7dfc5cfSToshiaki Makita 	struct u64_stats_sync syncp;
8427debe3eSEric Dumazet 	u64_stats_t packets;
8527debe3eSEric Dumazet 	u64_stats_t bytes;
8627debe3eSEric Dumazet 	u64_stats_t xdp_tx;
8727debe3eSEric Dumazet 	u64_stats_t xdp_tx_drops;
8827debe3eSEric Dumazet 	u64_stats_t kicks;
8927debe3eSEric Dumazet 	u64_stats_t tx_timeouts;
90d7dfc5cfSToshiaki Makita };
91d7dfc5cfSToshiaki Makita 
92d46eeeafSJason Wang struct virtnet_rq_stats {
93d46eeeafSJason Wang 	struct u64_stats_sync syncp;
9427debe3eSEric Dumazet 	u64_stats_t packets;
9527debe3eSEric Dumazet 	u64_stats_t bytes;
9627debe3eSEric Dumazet 	u64_stats_t drops;
9727debe3eSEric Dumazet 	u64_stats_t xdp_packets;
9827debe3eSEric Dumazet 	u64_stats_t xdp_tx;
9927debe3eSEric Dumazet 	u64_stats_t xdp_redirects;
10027debe3eSEric Dumazet 	u64_stats_t xdp_drops;
10127debe3eSEric Dumazet 	u64_stats_t kicks;
102d7dfc5cfSToshiaki Makita };
103d7dfc5cfSToshiaki Makita 
104d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STAT(m)	offsetof(struct virtnet_sq_stats, m)
105d46eeeafSJason Wang #define VIRTNET_RQ_STAT(m)	offsetof(struct virtnet_rq_stats, m)
106d7dfc5cfSToshiaki Makita 
107d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
108d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_SQ_STAT(packets) },
109d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_SQ_STAT(bytes) },
1105b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_SQ_STAT(xdp_tx) },
1115b8f3c8dSToshiaki Makita 	{ "xdp_tx_drops",	VIRTNET_SQ_STAT(xdp_tx_drops) },
112461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_SQ_STAT(kicks) },
113a520794bSTony Lu 	{ "tx_timeouts",	VIRTNET_SQ_STAT(tx_timeouts) },
114d7dfc5cfSToshiaki Makita };
115d7dfc5cfSToshiaki Makita 
116d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
117d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_RQ_STAT(packets) },
118d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_RQ_STAT(bytes) },
1192c4a2f7dSToshiaki Makita 	{ "drops",		VIRTNET_RQ_STAT(drops) },
1205b8f3c8dSToshiaki Makita 	{ "xdp_packets",	VIRTNET_RQ_STAT(xdp_packets) },
1215b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_RQ_STAT(xdp_tx) },
1225b8f3c8dSToshiaki Makita 	{ "xdp_redirects",	VIRTNET_RQ_STAT(xdp_redirects) },
1235b8f3c8dSToshiaki Makita 	{ "xdp_drops",		VIRTNET_RQ_STAT(xdp_drops) },
124461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_RQ_STAT(kicks) },
125d7dfc5cfSToshiaki Makita };
126d7dfc5cfSToshiaki Makita 
127d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN	ARRAY_SIZE(virtnet_sq_stats_desc)
128d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN	ARRAY_SIZE(virtnet_rq_stats_desc)
129d7dfc5cfSToshiaki Makita 
130308d7982SGavin Li struct virtnet_interrupt_coalesce {
131308d7982SGavin Li 	u32 max_packets;
132308d7982SGavin Li 	u32 max_usecs;
133308d7982SGavin Li };
134308d7982SGavin Li 
135295525e2SXuan Zhuo /* The dma information of pages allocated at a time. */
136295525e2SXuan Zhuo struct virtnet_rq_dma {
137295525e2SXuan Zhuo 	dma_addr_t addr;
138295525e2SXuan Zhuo 	u32 ref;
139295525e2SXuan Zhuo 	u16 len;
140295525e2SXuan Zhuo 	u16 need_sync;
141295525e2SXuan Zhuo };
142295525e2SXuan Zhuo 
143e9d7417bSJason Wang /* Internal representation of a send virtqueue */
144e9d7417bSJason Wang struct send_queue {
145e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
146e9d7417bSJason Wang 	struct virtqueue *vq;
147e9d7417bSJason Wang 
148e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
149e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
150986a4f4dSJason Wang 
151986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
152d0671115SParav Pandit 	char name[16];
153b92f1e67SWillem de Bruijn 
154d7dfc5cfSToshiaki Makita 	struct virtnet_sq_stats stats;
155d7dfc5cfSToshiaki Makita 
156394bd877SGavin Li 	struct virtnet_interrupt_coalesce intr_coal;
157394bd877SGavin Li 
158b92f1e67SWillem de Bruijn 	struct napi_struct napi;
159ebcce492SXuan Zhuo 
160ebcce492SXuan Zhuo 	/* Record whether sq is in reset state. */
161ebcce492SXuan Zhuo 	bool reset;
162e9d7417bSJason Wang };
163e9d7417bSJason Wang 
164e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
165e9d7417bSJason Wang struct receive_queue {
166e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
167e9d7417bSJason Wang 	struct virtqueue *vq;
168e9d7417bSJason Wang 
169296f96fcSRusty Russell 	struct napi_struct napi;
170296f96fcSRusty Russell 
171f600b690SJohn Fastabend 	struct bpf_prog __rcu *xdp_prog;
172f600b690SJohn Fastabend 
173d7dfc5cfSToshiaki Makita 	struct virtnet_rq_stats stats;
174d7dfc5cfSToshiaki Makita 
175394bd877SGavin Li 	struct virtnet_interrupt_coalesce intr_coal;
176394bd877SGavin Li 
177e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
178e9d7417bSJason Wang 	struct page *pages;
179e9d7417bSJason Wang 
180ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
1815377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
182ab7db917SMichael Dalton 
183fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
184fb51879dSMichael Dalton 	struct page_frag alloc_frag;
185fb51879dSMichael Dalton 
186e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
187e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
188986a4f4dSJason Wang 
189d85b758fSMichael S. Tsirkin 	/* Min single buffer size for mergeable buffers case. */
190d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len;
191d85b758fSMichael S. Tsirkin 
192986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
193d0671115SParav Pandit 	char name[16];
194754b8a21SJesper Dangaard Brouer 
195754b8a21SJesper Dangaard Brouer 	struct xdp_rxq_info xdp_rxq;
196295525e2SXuan Zhuo 
197295525e2SXuan Zhuo 	/* Record the last dma info to free after new pages is allocated. */
198295525e2SXuan Zhuo 	struct virtnet_rq_dma *last_dma;
199295525e2SXuan Zhuo 
200295525e2SXuan Zhuo 	/* Do dma by self */
201295525e2SXuan Zhuo 	bool do_dma;
202e9d7417bSJason Wang };
203e9d7417bSJason Wang 
204c7114b12SAndrew Melnychenko /* This structure can contain rss message with maximum settings for indirection table and keysize
205c7114b12SAndrew Melnychenko  * Note, that default structure that describes RSS configuration virtio_net_rss_config
206c7114b12SAndrew Melnychenko  * contains same info but can't handle table values.
207c7114b12SAndrew Melnychenko  * In any case, structure would be passed to virtio hw through sg_buf split by parts
208c7114b12SAndrew Melnychenko  * because table sizes may be differ according to the device configuration.
209c7114b12SAndrew Melnychenko  */
210c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
211c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_TABLE_LEN    128
212c7114b12SAndrew Melnychenko struct virtio_net_ctrl_rss {
213c7114b12SAndrew Melnychenko 	u32 hash_types;
214c7114b12SAndrew Melnychenko 	u16 indirection_table_mask;
215c7114b12SAndrew Melnychenko 	u16 unclassified_queue;
216c7114b12SAndrew Melnychenko 	u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
217c7114b12SAndrew Melnychenko 	u16 max_tx_vq;
218c7114b12SAndrew Melnychenko 	u8 hash_key_length;
219c7114b12SAndrew Melnychenko 	u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
220c7114b12SAndrew Melnychenko };
221c7114b12SAndrew Melnychenko 
22212e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */
22312e57169SMichael S. Tsirkin struct control_buf {
22412e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr hdr;
22512e57169SMichael S. Tsirkin 	virtio_net_ctrl_ack status;
22612e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_mq mq;
22712e57169SMichael S. Tsirkin 	u8 promisc;
22812e57169SMichael S. Tsirkin 	u8 allmulti;
229d7fad4c8SMichael S. Tsirkin 	__virtio16 vid;
230f4ee703aSMichael S. Tsirkin 	__virtio64 offloads;
231c7114b12SAndrew Melnychenko 	struct virtio_net_ctrl_rss rss;
232accc1bf2SBrett Creeley 	struct virtio_net_ctrl_coal_tx coal_tx;
233accc1bf2SBrett Creeley 	struct virtio_net_ctrl_coal_rx coal_rx;
234394bd877SGavin Li 	struct virtio_net_ctrl_coal_vq coal_vq;
23512e57169SMichael S. Tsirkin };
23612e57169SMichael S. Tsirkin 
237e9d7417bSJason Wang struct virtnet_info {
238e9d7417bSJason Wang 	struct virtio_device *vdev;
239e9d7417bSJason Wang 	struct virtqueue *cvq;
240e9d7417bSJason Wang 	struct net_device *dev;
241986a4f4dSJason Wang 	struct send_queue *sq;
242986a4f4dSJason Wang 	struct receive_queue *rq;
243e9d7417bSJason Wang 	unsigned int status;
244e9d7417bSJason Wang 
245986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
246986a4f4dSJason Wang 	u16 max_queue_pairs;
247986a4f4dSJason Wang 
248986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
249986a4f4dSJason Wang 	u16 curr_queue_pairs;
250986a4f4dSJason Wang 
251672aafd5SJohn Fastabend 	/* # of XDP queue pairs currently used by the driver */
252672aafd5SJohn Fastabend 	u16 xdp_queue_pairs;
253672aafd5SJohn Fastabend 
25497c2c69eSXuan Zhuo 	/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
25597c2c69eSXuan Zhuo 	bool xdp_enabled;
25697c2c69eSXuan Zhuo 
25797402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
25897402b96SHerbert Xu 	bool big_packets;
25997402b96SHerbert Xu 
2604959aebbSGavin Li 	/* number of sg entries allocated for big packets */
2614959aebbSGavin Li 	unsigned int big_packets_num_skbfrags;
2624959aebbSGavin Li 
2633f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
2643f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
2653f2c31d9SMark McLoughlin 
266c7114b12SAndrew Melnychenko 	/* Host supports rss and/or hash report */
267c7114b12SAndrew Melnychenko 	bool has_rss;
26891f41f01SAndrew Melnychenko 	bool has_rss_hash_report;
269c7114b12SAndrew Melnychenko 	u8 rss_key_size;
270c7114b12SAndrew Melnychenko 	u16 rss_indir_table_size;
271c7114b12SAndrew Melnychenko 	u32 rss_hash_types_supported;
272c1170820SAndrew Melnychenko 	u32 rss_hash_types_saved;
273c7114b12SAndrew Melnychenko 
274986a4f4dSJason Wang 	/* Has control virtqueue */
275986a4f4dSJason Wang 	bool has_cvq;
276986a4f4dSJason Wang 
277e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
278e7428e95SMichael S. Tsirkin 	bool any_header_sg;
279e7428e95SMichael S. Tsirkin 
280012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
281012873d0SMichael S. Tsirkin 	u8 hdr_len;
282012873d0SMichael S. Tsirkin 
2835a159128SJason Wang 	/* Work struct for delayed refilling if we run low on memory. */
2843161e453SRusty Russell 	struct delayed_work refill;
2853161e453SRusty Russell 
2865a159128SJason Wang 	/* Is delayed refill enabled? */
2875a159128SJason Wang 	bool refill_enabled;
2885a159128SJason Wang 
2895a159128SJason Wang 	/* The lock to synchronize the access to refill_enabled */
2905a159128SJason Wang 	spinlock_t refill_lock;
2915a159128SJason Wang 
292586d17c5SJason Wang 	/* Work struct for config space updates */
293586d17c5SJason Wang 	struct work_struct config_work;
294586d17c5SJason Wang 
295986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
296986a4f4dSJason Wang 	bool affinity_hint_set;
29747be2479SWanlong Gao 
2988017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
2998017c279SSebastian Andrzej Siewior 	struct hlist_node node;
3008017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
3012ac46030SMichael S. Tsirkin 
30212e57169SMichael S. Tsirkin 	struct control_buf *ctrl;
30316032be5SNikolay Aleksandrov 
30416032be5SNikolay Aleksandrov 	/* Ethtool settings */
30516032be5SNikolay Aleksandrov 	u8 duplex;
30616032be5SNikolay Aleksandrov 	u32 speed;
3073f93522fSJason Wang 
308699b045aSAlvaro Karsz 	/* Interrupt coalescing settings */
309308d7982SGavin Li 	struct virtnet_interrupt_coalesce intr_coal_tx;
310308d7982SGavin Li 	struct virtnet_interrupt_coalesce intr_coal_rx;
311699b045aSAlvaro Karsz 
3123f93522fSJason Wang 	unsigned long guest_offloads;
313a02e8964SWillem de Bruijn 	unsigned long guest_offloads_capable;
314ba5e4426SSridhar Samudrala 
315ba5e4426SSridhar Samudrala 	/* failover when STANDBY feature enabled */
316ba5e4426SSridhar Samudrala 	struct failover *failover;
317296f96fcSRusty Russell };
318296f96fcSRusty Russell 
3199ab86bbcSShirley Ma struct padded_vnet_hdr {
320c1ddc42dSAndrew Melnychenko 	struct virtio_net_hdr_v1_hash hdr;
3219ab86bbcSShirley Ma 	/*
322012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
323012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
324012873d0SMichael S. Tsirkin 	 * after the header.
3259ab86bbcSShirley Ma 	 */
326c1ddc42dSAndrew Melnychenko 	char padding[12];
3279ab86bbcSShirley Ma };
3289ab86bbcSShirley Ma 
329dae64749SFeng Liu struct virtio_net_common_hdr {
330dae64749SFeng Liu 	union {
331dae64749SFeng Liu 		struct virtio_net_hdr hdr;
332dae64749SFeng Liu 		struct virtio_net_hdr_mrg_rxbuf	mrg_hdr;
333dae64749SFeng Liu 		struct virtio_net_hdr_v1_hash hash_v1_hdr;
334dae64749SFeng Liu 	};
335dae64749SFeng Liu };
336dae64749SFeng Liu 
337ebcce492SXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
3386a4763e2SXuan Zhuo 
is_xdp_frame(void * ptr)3395050471dSToshiaki Makita static bool is_xdp_frame(void *ptr)
3405050471dSToshiaki Makita {
3415050471dSToshiaki Makita 	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
3425050471dSToshiaki Makita }
3435050471dSToshiaki Makita 
xdp_to_ptr(struct xdp_frame * ptr)3445050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr)
3455050471dSToshiaki Makita {
3465050471dSToshiaki Makita 	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
3475050471dSToshiaki Makita }
3485050471dSToshiaki Makita 
ptr_to_xdp(void * ptr)3495050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr)
3505050471dSToshiaki Makita {
3515050471dSToshiaki Makita 	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
3525050471dSToshiaki Makita }
3535050471dSToshiaki Makita 
354986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
355986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
356986a4f4dSJason Wang  */
vq2txq(struct virtqueue * vq)357986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
358986a4f4dSJason Wang {
3599d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
360986a4f4dSJason Wang }
361986a4f4dSJason Wang 
txq2vq(int txq)362986a4f4dSJason Wang static int txq2vq(int txq)
363986a4f4dSJason Wang {
364986a4f4dSJason Wang 	return txq * 2 + 1;
365986a4f4dSJason Wang }
366986a4f4dSJason Wang 
vq2rxq(struct virtqueue * vq)367986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
368986a4f4dSJason Wang {
3699d0ca6edSRusty Russell 	return vq->index / 2;
370986a4f4dSJason Wang }
371986a4f4dSJason Wang 
rxq2vq(int rxq)372986a4f4dSJason Wang static int rxq2vq(int rxq)
373986a4f4dSJason Wang {
374986a4f4dSJason Wang 	return rxq * 2;
375986a4f4dSJason Wang }
376986a4f4dSJason Wang 
377dae64749SFeng Liu static inline struct virtio_net_common_hdr *
skb_vnet_common_hdr(struct sk_buff * skb)378dae64749SFeng Liu skb_vnet_common_hdr(struct sk_buff *skb)
379296f96fcSRusty Russell {
380dae64749SFeng Liu 	return (struct virtio_net_common_hdr *)skb->cb;
381296f96fcSRusty Russell }
382296f96fcSRusty Russell 
3839ab86bbcSShirley Ma /*
3849ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
3859ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
3869ab86bbcSShirley Ma  */
give_pages(struct receive_queue * rq,struct page * page)387e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
388fb6813f4SRusty Russell {
3899ab86bbcSShirley Ma 	struct page *end;
3909ab86bbcSShirley Ma 
391e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
3929ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
393e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
394e9d7417bSJason Wang 	rq->pages = page;
395fb6813f4SRusty Russell }
396fb6813f4SRusty Russell 
get_a_page(struct receive_queue * rq,gfp_t gfp_mask)397e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
398fb6813f4SRusty Russell {
399e9d7417bSJason Wang 	struct page *p = rq->pages;
400fb6813f4SRusty Russell 
4019ab86bbcSShirley Ma 	if (p) {
402e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
4039ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
4049ab86bbcSShirley Ma 		p->private = 0;
4059ab86bbcSShirley Ma 	} else
406fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
407fb6813f4SRusty Russell 	return p;
408fb6813f4SRusty Russell }
409fb6813f4SRusty Russell 
virtnet_rq_free_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf)4103ffd05c2SXuan Zhuo static void virtnet_rq_free_buf(struct virtnet_info *vi,
4113ffd05c2SXuan Zhuo 				struct receive_queue *rq, void *buf)
4123ffd05c2SXuan Zhuo {
4133ffd05c2SXuan Zhuo 	if (vi->mergeable_rx_bufs)
4143ffd05c2SXuan Zhuo 		put_page(virt_to_head_page(buf));
4153ffd05c2SXuan Zhuo 	else if (vi->big_packets)
4163ffd05c2SXuan Zhuo 		give_pages(rq, buf);
4173ffd05c2SXuan Zhuo 	else
4183ffd05c2SXuan Zhuo 		put_page(virt_to_head_page(buf));
4193ffd05c2SXuan Zhuo }
4203ffd05c2SXuan Zhuo 
enable_delayed_refill(struct virtnet_info * vi)4215a159128SJason Wang static void enable_delayed_refill(struct virtnet_info *vi)
4225a159128SJason Wang {
4235a159128SJason Wang 	spin_lock_bh(&vi->refill_lock);
4245a159128SJason Wang 	vi->refill_enabled = true;
4255a159128SJason Wang 	spin_unlock_bh(&vi->refill_lock);
4265a159128SJason Wang }
4275a159128SJason Wang 
disable_delayed_refill(struct virtnet_info * vi)4285a159128SJason Wang static void disable_delayed_refill(struct virtnet_info *vi)
4295a159128SJason Wang {
4305a159128SJason Wang 	spin_lock_bh(&vi->refill_lock);
4315a159128SJason Wang 	vi->refill_enabled = false;
4325a159128SJason Wang 	spin_unlock_bh(&vi->refill_lock);
4335a159128SJason Wang }
4345a159128SJason Wang 
virtqueue_napi_schedule(struct napi_struct * napi,struct virtqueue * vq)435e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi,
436e4e8452aSWillem de Bruijn 				    struct virtqueue *vq)
437e4e8452aSWillem de Bruijn {
438e4e8452aSWillem de Bruijn 	if (napi_schedule_prep(napi)) {
439e4e8452aSWillem de Bruijn 		virtqueue_disable_cb(vq);
440e4e8452aSWillem de Bruijn 		__napi_schedule(napi);
441e4e8452aSWillem de Bruijn 	}
442e4e8452aSWillem de Bruijn }
443e4e8452aSWillem de Bruijn 
virtqueue_napi_complete(struct napi_struct * napi,struct virtqueue * vq,int processed)444e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi,
445e4e8452aSWillem de Bruijn 				    struct virtqueue *vq, int processed)
446e4e8452aSWillem de Bruijn {
447e4e8452aSWillem de Bruijn 	int opaque;
448e4e8452aSWillem de Bruijn 
449e4e8452aSWillem de Bruijn 	opaque = virtqueue_enable_cb_prepare(vq);
450fdaa767aSToshiaki Makita 	if (napi_complete_done(napi, processed)) {
451fdaa767aSToshiaki Makita 		if (unlikely(virtqueue_poll(vq, opaque)))
452e4e8452aSWillem de Bruijn 			virtqueue_napi_schedule(napi, vq);
453fdaa767aSToshiaki Makita 	} else {
454fdaa767aSToshiaki Makita 		virtqueue_disable_cb(vq);
455fdaa767aSToshiaki Makita 	}
456e4e8452aSWillem de Bruijn }
457e4e8452aSWillem de Bruijn 
skb_xmit_done(struct virtqueue * vq)458e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
459296f96fcSRusty Russell {
460e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
461b92f1e67SWillem de Bruijn 	struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
462296f96fcSRusty Russell 
4632cb9c6baSRusty Russell 	/* Suppress further interrupts. */
464e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
46511a3a154SRusty Russell 
466b92f1e67SWillem de Bruijn 	if (napi->weight)
467b92f1e67SWillem de Bruijn 		virtqueue_napi_schedule(napi, vq);
468b92f1e67SWillem de Bruijn 	else
469363f1514SRusty Russell 		/* We were probably waiting for more output buffers. */
470986a4f4dSJason Wang 		netif_wake_subqueue(vi->dev, vq2txq(vq));
471296f96fcSRusty Russell }
472296f96fcSRusty Russell 
47328b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22
mergeable_len_to_ctx(unsigned int truesize,unsigned int headroom)47428b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize,
47528b39bc7SJason Wang 				  unsigned int headroom)
47628b39bc7SJason Wang {
47728b39bc7SJason Wang 	return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
47828b39bc7SJason Wang }
47928b39bc7SJason Wang 
mergeable_ctx_to_headroom(void * mrg_ctx)48028b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
48128b39bc7SJason Wang {
48228b39bc7SJason Wang 	return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
48328b39bc7SJason Wang }
48428b39bc7SJason Wang 
mergeable_ctx_to_truesize(void * mrg_ctx)48528b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
48628b39bc7SJason Wang {
48728b39bc7SJason Wang 	return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
48828b39bc7SJason Wang }
48928b39bc7SJason Wang 
virtnet_build_skb(void * buf,unsigned int buflen,unsigned int headroom,unsigned int len)49021e26a71SXuan Zhuo static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
49121e26a71SXuan Zhuo 					 unsigned int headroom,
49221e26a71SXuan Zhuo 					 unsigned int len)
49321e26a71SXuan Zhuo {
49421e26a71SXuan Zhuo 	struct sk_buff *skb;
49521e26a71SXuan Zhuo 
49621e26a71SXuan Zhuo 	skb = build_skb(buf, buflen);
49721e26a71SXuan Zhuo 	if (unlikely(!skb))
49821e26a71SXuan Zhuo 		return NULL;
49921e26a71SXuan Zhuo 
50021e26a71SXuan Zhuo 	skb_reserve(skb, headroom);
50121e26a71SXuan Zhuo 	skb_put(skb, len);
50221e26a71SXuan Zhuo 
50321e26a71SXuan Zhuo 	return skb;
50421e26a71SXuan Zhuo }
50521e26a71SXuan Zhuo 
5063464645aSMike Waychison /* Called from bottom half context */
page_to_skb(struct virtnet_info * vi,struct receive_queue * rq,struct page * page,unsigned int offset,unsigned int len,unsigned int truesize,unsigned int headroom)507946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
508946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
5092613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
510fa0f1ba7SXuan Zhuo 				   unsigned int len, unsigned int truesize,
511fa0f1ba7SXuan Zhuo 				   unsigned int headroom)
5129ab86bbcSShirley Ma {
5139ab86bbcSShirley Ma 	struct sk_buff *skb;
514dae64749SFeng Liu 	struct virtio_net_common_hdr *hdr;
5152613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
516af39c8f7SEric Dumazet 	struct page *page_to_free = NULL;
517fb32856bSXuan Zhuo 	int tailroom, shinfo_size;
518f80bd740SXuan Zhuo 	char *p, *hdr_p, *buf;
5199ab86bbcSShirley Ma 
5202613af0eSMichael Dalton 	p = page_address(page) + offset;
521fb32856bSXuan Zhuo 	hdr_p = p;
5229ab86bbcSShirley Ma 
523012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
524012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
525c1ddc42dSAndrew Melnychenko 		hdr_padded_len = hdr_len;
526012873d0SMichael S. Tsirkin 	else
5272613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
5283f2c31d9SMark McLoughlin 
529fa0f1ba7SXuan Zhuo 	buf = p - headroom;
5309ab86bbcSShirley Ma 	len -= hdr_len;
5312613af0eSMichael Dalton 	offset += hdr_padded_len;
5322613af0eSMichael Dalton 	p += hdr_padded_len;
533fa0f1ba7SXuan Zhuo 	tailroom = truesize - headroom  - hdr_padded_len - len;
5343f2c31d9SMark McLoughlin 
535fb32856bSXuan Zhuo 	shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
536fb32856bSXuan Zhuo 
537f80bd740SXuan Zhuo 	/* copy small packet so we can reuse these pages */
538f5d7872aSEric Dumazet 	if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
53921e26a71SXuan Zhuo 		skb = virtnet_build_skb(buf, truesize, p - buf, len);
540fb32856bSXuan Zhuo 		if (unlikely(!skb))
541fb32856bSXuan Zhuo 			return NULL;
542fb32856bSXuan Zhuo 
543afd92d82SJason Wang 		page = (struct page *)page->private;
544afd92d82SJason Wang 		if (page)
545afd92d82SJason Wang 			give_pages(rq, page);
546fb32856bSXuan Zhuo 		goto ok;
547fb32856bSXuan Zhuo 	}
548fb32856bSXuan Zhuo 
549fb32856bSXuan Zhuo 	/* copy small packet so we can reuse these pages for small data */
550fb32856bSXuan Zhuo 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
551fb32856bSXuan Zhuo 	if (unlikely(!skb))
552fb32856bSXuan Zhuo 		return NULL;
553fb32856bSXuan Zhuo 
5540f6925b3SEric Dumazet 	/* Copy all frame if it fits skb->head, otherwise
5550f6925b3SEric Dumazet 	 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
5560f6925b3SEric Dumazet 	 */
5570f6925b3SEric Dumazet 	if (len <= skb_tailroom(skb))
5583f2c31d9SMark McLoughlin 		copy = len;
5590f6925b3SEric Dumazet 	else
56018117a84SHeng Qi 		copy = ETH_HLEN;
56159ae1d12SJohannes Berg 	skb_put_data(skb, p, copy);
5623f2c31d9SMark McLoughlin 
5633f2c31d9SMark McLoughlin 	len -= copy;
5649ab86bbcSShirley Ma 	offset += copy;
5653f2c31d9SMark McLoughlin 
5662613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
5672613af0eSMichael Dalton 		if (len)
5682613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
5692613af0eSMichael Dalton 		else
570af39c8f7SEric Dumazet 			page_to_free = page;
571fb32856bSXuan Zhuo 		goto ok;
5722613af0eSMichael Dalton 	}
5732613af0eSMichael Dalton 
574e878d78bSSasha Levin 	/*
575e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
576e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
577e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
578e878d78bSSasha Levin 	 * the case of a broken device.
579e878d78bSSasha Levin 	 */
580e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
581be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
582e878d78bSSasha Levin 		dev_kfree_skb(skb);
583e878d78bSSasha Levin 		return NULL;
584e878d78bSSasha Levin 	}
5852613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
5869ab86bbcSShirley Ma 	while (len) {
5872613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
5882613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
5892613af0eSMichael Dalton 				frag_size, truesize);
5902613af0eSMichael Dalton 		len -= frag_size;
5919ab86bbcSShirley Ma 		page = (struct page *)page->private;
5929ab86bbcSShirley Ma 		offset = 0;
5933f2c31d9SMark McLoughlin 	}
5943f2c31d9SMark McLoughlin 
5959ab86bbcSShirley Ma 	if (page)
596e9d7417bSJason Wang 		give_pages(rq, page);
5973f2c31d9SMark McLoughlin 
598fb32856bSXuan Zhuo ok:
599dae64749SFeng Liu 	hdr = skb_vnet_common_hdr(skb);
600fb32856bSXuan Zhuo 	memcpy(hdr, hdr_p, hdr_len);
601af39c8f7SEric Dumazet 	if (page_to_free)
602af39c8f7SEric Dumazet 		put_page(page_to_free);
603fb32856bSXuan Zhuo 
6049ab86bbcSShirley Ma 	return skb;
6059ab86bbcSShirley Ma }
6069ab86bbcSShirley Ma 
virtnet_rq_unmap(struct receive_queue * rq,void * buf,u32 len)607295525e2SXuan Zhuo static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
608295525e2SXuan Zhuo {
609295525e2SXuan Zhuo 	struct page *page = virt_to_head_page(buf);
610295525e2SXuan Zhuo 	struct virtnet_rq_dma *dma;
611295525e2SXuan Zhuo 	void *head;
612295525e2SXuan Zhuo 	int offset;
613295525e2SXuan Zhuo 
614295525e2SXuan Zhuo 	head = page_address(page);
615295525e2SXuan Zhuo 
616295525e2SXuan Zhuo 	dma = head;
617295525e2SXuan Zhuo 
618295525e2SXuan Zhuo 	--dma->ref;
619295525e2SXuan Zhuo 
620295525e2SXuan Zhuo 	if (dma->need_sync && len) {
621295525e2SXuan Zhuo 		offset = buf - (head + sizeof(*dma));
622295525e2SXuan Zhuo 
6235720c43dSXuan Zhuo 		virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
6245720c43dSXuan Zhuo 							offset, len,
6255720c43dSXuan Zhuo 							DMA_FROM_DEVICE);
626295525e2SXuan Zhuo 	}
627295525e2SXuan Zhuo 
6285720c43dSXuan Zhuo 	if (dma->ref)
629295525e2SXuan Zhuo 		return;
630295525e2SXuan Zhuo 
631295525e2SXuan Zhuo 	virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
632295525e2SXuan Zhuo 					 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
633295525e2SXuan Zhuo 	put_page(page);
634295525e2SXuan Zhuo }
635295525e2SXuan Zhuo 
virtnet_rq_get_buf(struct receive_queue * rq,u32 * len,void ** ctx)636295525e2SXuan Zhuo static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
637295525e2SXuan Zhuo {
638295525e2SXuan Zhuo 	void *buf;
639295525e2SXuan Zhuo 
640295525e2SXuan Zhuo 	buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
641295525e2SXuan Zhuo 	if (buf && rq->do_dma)
642295525e2SXuan Zhuo 		virtnet_rq_unmap(rq, buf, *len);
643295525e2SXuan Zhuo 
644295525e2SXuan Zhuo 	return buf;
645295525e2SXuan Zhuo }
646295525e2SXuan Zhuo 
virtnet_rq_init_one_sg(struct receive_queue * rq,void * buf,u32 len)647295525e2SXuan Zhuo static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
648295525e2SXuan Zhuo {
649295525e2SXuan Zhuo 	struct virtnet_rq_dma *dma;
650295525e2SXuan Zhuo 	dma_addr_t addr;
651295525e2SXuan Zhuo 	u32 offset;
652295525e2SXuan Zhuo 	void *head;
653295525e2SXuan Zhuo 
654295525e2SXuan Zhuo 	if (!rq->do_dma) {
655295525e2SXuan Zhuo 		sg_init_one(rq->sg, buf, len);
656295525e2SXuan Zhuo 		return;
657295525e2SXuan Zhuo 	}
658295525e2SXuan Zhuo 
659295525e2SXuan Zhuo 	head = page_address(rq->alloc_frag.page);
660295525e2SXuan Zhuo 
661295525e2SXuan Zhuo 	offset = buf - head;
662295525e2SXuan Zhuo 
663295525e2SXuan Zhuo 	dma = head;
664295525e2SXuan Zhuo 
665295525e2SXuan Zhuo 	addr = dma->addr - sizeof(*dma) + offset;
666295525e2SXuan Zhuo 
667295525e2SXuan Zhuo 	sg_init_table(rq->sg, 1);
668295525e2SXuan Zhuo 	rq->sg[0].dma_address = addr;
669295525e2SXuan Zhuo 	rq->sg[0].length = len;
670295525e2SXuan Zhuo }
671295525e2SXuan Zhuo 
virtnet_rq_alloc(struct receive_queue * rq,u32 size,gfp_t gfp)672295525e2SXuan Zhuo static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
673295525e2SXuan Zhuo {
674295525e2SXuan Zhuo 	struct page_frag *alloc_frag = &rq->alloc_frag;
675295525e2SXuan Zhuo 	struct virtnet_rq_dma *dma;
676295525e2SXuan Zhuo 	void *buf, *head;
677295525e2SXuan Zhuo 	dma_addr_t addr;
678295525e2SXuan Zhuo 
679295525e2SXuan Zhuo 	head = page_address(alloc_frag->page);
680295525e2SXuan Zhuo 
681295525e2SXuan Zhuo 	if (rq->do_dma) {
682295525e2SXuan Zhuo 		dma = head;
683295525e2SXuan Zhuo 
684295525e2SXuan Zhuo 		/* new pages */
685295525e2SXuan Zhuo 		if (!alloc_frag->offset) {
686295525e2SXuan Zhuo 			if (rq->last_dma) {
687295525e2SXuan Zhuo 				/* Now, the new page is allocated, the last dma
688295525e2SXuan Zhuo 				 * will not be used. So the dma can be unmapped
689295525e2SXuan Zhuo 				 * if the ref is 0.
690295525e2SXuan Zhuo 				 */
691295525e2SXuan Zhuo 				virtnet_rq_unmap(rq, rq->last_dma, 0);
692295525e2SXuan Zhuo 				rq->last_dma = NULL;
693295525e2SXuan Zhuo 			}
694295525e2SXuan Zhuo 
695295525e2SXuan Zhuo 			dma->len = alloc_frag->size - sizeof(*dma);
696295525e2SXuan Zhuo 
697295525e2SXuan Zhuo 			addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
698295525e2SXuan Zhuo 							      dma->len, DMA_FROM_DEVICE, 0);
699295525e2SXuan Zhuo 			if (virtqueue_dma_mapping_error(rq->vq, addr))
700295525e2SXuan Zhuo 				return NULL;
701295525e2SXuan Zhuo 
702295525e2SXuan Zhuo 			dma->addr = addr;
703295525e2SXuan Zhuo 			dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
704295525e2SXuan Zhuo 
705295525e2SXuan Zhuo 			/* Add a reference to dma to prevent the entire dma from
706295525e2SXuan Zhuo 			 * being released during error handling. This reference
707295525e2SXuan Zhuo 			 * will be freed after the pages are no longer used.
708295525e2SXuan Zhuo 			 */
709295525e2SXuan Zhuo 			get_page(alloc_frag->page);
710295525e2SXuan Zhuo 			dma->ref = 1;
711295525e2SXuan Zhuo 			alloc_frag->offset = sizeof(*dma);
712295525e2SXuan Zhuo 
713295525e2SXuan Zhuo 			rq->last_dma = dma;
714295525e2SXuan Zhuo 		}
715295525e2SXuan Zhuo 
716295525e2SXuan Zhuo 		++dma->ref;
717295525e2SXuan Zhuo 	}
718295525e2SXuan Zhuo 
719295525e2SXuan Zhuo 	buf = head + alloc_frag->offset;
720295525e2SXuan Zhuo 
721295525e2SXuan Zhuo 	get_page(alloc_frag->page);
722295525e2SXuan Zhuo 	alloc_frag->offset += size;
723295525e2SXuan Zhuo 
724295525e2SXuan Zhuo 	return buf;
725295525e2SXuan Zhuo }
726295525e2SXuan Zhuo 
virtnet_rq_set_premapped(struct virtnet_info * vi)727295525e2SXuan Zhuo static void virtnet_rq_set_premapped(struct virtnet_info *vi)
728295525e2SXuan Zhuo {
729295525e2SXuan Zhuo 	int i;
730295525e2SXuan Zhuo 
731295525e2SXuan Zhuo 	/* disable for big mode */
732295525e2SXuan Zhuo 	if (!vi->mergeable_rx_bufs && vi->big_packets)
733295525e2SXuan Zhuo 		return;
734295525e2SXuan Zhuo 
735295525e2SXuan Zhuo 	for (i = 0; i < vi->max_queue_pairs; i++) {
736295525e2SXuan Zhuo 		if (virtqueue_set_dma_premapped(vi->rq[i].vq))
737295525e2SXuan Zhuo 			continue;
738295525e2SXuan Zhuo 
739295525e2SXuan Zhuo 		vi->rq[i].do_dma = true;
740295525e2SXuan Zhuo 	}
741295525e2SXuan Zhuo }
742295525e2SXuan Zhuo 
virtnet_rq_unmap_free_buf(struct virtqueue * vq,void * buf)7433ffd05c2SXuan Zhuo static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
7443ffd05c2SXuan Zhuo {
7453ffd05c2SXuan Zhuo 	struct virtnet_info *vi = vq->vdev->priv;
7463ffd05c2SXuan Zhuo 	struct receive_queue *rq;
7473ffd05c2SXuan Zhuo 	int i = vq2rxq(vq);
7483ffd05c2SXuan Zhuo 
7493ffd05c2SXuan Zhuo 	rq = &vi->rq[i];
7503ffd05c2SXuan Zhuo 
7513ffd05c2SXuan Zhuo 	if (rq->do_dma)
7523ffd05c2SXuan Zhuo 		virtnet_rq_unmap(rq, buf, 0);
7533ffd05c2SXuan Zhuo 
7543ffd05c2SXuan Zhuo 	virtnet_rq_free_buf(vi, rq, buf);
7553ffd05c2SXuan Zhuo }
7563ffd05c2SXuan Zhuo 
free_old_xmit_skbs(struct send_queue * sq,bool in_napi)75725074a44SXuan Zhuo static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
75825074a44SXuan Zhuo {
75925074a44SXuan Zhuo 	unsigned int len;
76025074a44SXuan Zhuo 	unsigned int packets = 0;
76125074a44SXuan Zhuo 	unsigned int bytes = 0;
76225074a44SXuan Zhuo 	void *ptr;
76325074a44SXuan Zhuo 
76425074a44SXuan Zhuo 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
76525074a44SXuan Zhuo 		if (likely(!is_xdp_frame(ptr))) {
76625074a44SXuan Zhuo 			struct sk_buff *skb = ptr;
76725074a44SXuan Zhuo 
76825074a44SXuan Zhuo 			pr_debug("Sent skb %p\n", skb);
76925074a44SXuan Zhuo 
77025074a44SXuan Zhuo 			bytes += skb->len;
77125074a44SXuan Zhuo 			napi_consume_skb(skb, in_napi);
77225074a44SXuan Zhuo 		} else {
77325074a44SXuan Zhuo 			struct xdp_frame *frame = ptr_to_xdp(ptr);
77425074a44SXuan Zhuo 
77525074a44SXuan Zhuo 			bytes += xdp_get_frame_len(frame);
77625074a44SXuan Zhuo 			xdp_return_frame(frame);
77725074a44SXuan Zhuo 		}
77825074a44SXuan Zhuo 		packets++;
77925074a44SXuan Zhuo 	}
78025074a44SXuan Zhuo 
78125074a44SXuan Zhuo 	/* Avoid overhead when no packets have been processed
78225074a44SXuan Zhuo 	 * happens when called speculatively from start_xmit.
78325074a44SXuan Zhuo 	 */
78425074a44SXuan Zhuo 	if (!packets)
78525074a44SXuan Zhuo 		return;
78625074a44SXuan Zhuo 
78725074a44SXuan Zhuo 	u64_stats_update_begin(&sq->stats.syncp);
78827debe3eSEric Dumazet 	u64_stats_add(&sq->stats.bytes, bytes);
78927debe3eSEric Dumazet 	u64_stats_add(&sq->stats.packets, packets);
79025074a44SXuan Zhuo 	u64_stats_update_end(&sq->stats.syncp);
79125074a44SXuan Zhuo }
79225074a44SXuan Zhuo 
is_xdp_raw_buffer_queue(struct virtnet_info * vi,int q)79325074a44SXuan Zhuo static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
79425074a44SXuan Zhuo {
79525074a44SXuan Zhuo 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
79625074a44SXuan Zhuo 		return false;
79725074a44SXuan Zhuo 	else if (q < vi->curr_queue_pairs)
79825074a44SXuan Zhuo 		return true;
79925074a44SXuan Zhuo 	else
80025074a44SXuan Zhuo 		return false;
80125074a44SXuan Zhuo }
80225074a44SXuan Zhuo 
check_sq_full_and_disable(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)803b8ef4809SXuan Zhuo static void check_sq_full_and_disable(struct virtnet_info *vi,
804b8ef4809SXuan Zhuo 				      struct net_device *dev,
805b8ef4809SXuan Zhuo 				      struct send_queue *sq)
806b8ef4809SXuan Zhuo {
807b8ef4809SXuan Zhuo 	bool use_napi = sq->napi.weight;
808b8ef4809SXuan Zhuo 	int qnum;
809b8ef4809SXuan Zhuo 
810b8ef4809SXuan Zhuo 	qnum = sq - vi->sq;
811b8ef4809SXuan Zhuo 
812b8ef4809SXuan Zhuo 	/* If running out of space, stop queue to avoid getting packets that we
813b8ef4809SXuan Zhuo 	 * are then unable to transmit.
814b8ef4809SXuan Zhuo 	 * An alternative would be to force queuing layer to requeue the skb by
815b8ef4809SXuan Zhuo 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
816b8ef4809SXuan Zhuo 	 * returned in a normal path of operation: it means that driver is not
817b8ef4809SXuan Zhuo 	 * maintaining the TX queue stop/start state properly, and causes
818b8ef4809SXuan Zhuo 	 * the stack to do a non-trivial amount of useless work.
819b8ef4809SXuan Zhuo 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
820b8ef4809SXuan Zhuo 	 * early means 16 slots are typically wasted.
821b8ef4809SXuan Zhuo 	 */
822b8ef4809SXuan Zhuo 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
823b8ef4809SXuan Zhuo 		netif_stop_subqueue(dev, qnum);
824b8ef4809SXuan Zhuo 		if (use_napi) {
825b8ef4809SXuan Zhuo 			if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
826b8ef4809SXuan Zhuo 				virtqueue_napi_schedule(&sq->napi, sq->vq);
827b8ef4809SXuan Zhuo 		} else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
828b8ef4809SXuan Zhuo 			/* More just got used, free them then recheck. */
829b8ef4809SXuan Zhuo 			free_old_xmit_skbs(sq, false);
830b8ef4809SXuan Zhuo 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
831b8ef4809SXuan Zhuo 				netif_start_subqueue(dev, qnum);
832b8ef4809SXuan Zhuo 				virtqueue_disable_cb(sq->vq);
833b8ef4809SXuan Zhuo 			}
834b8ef4809SXuan Zhuo 		}
835b8ef4809SXuan Zhuo 	}
836b8ef4809SXuan Zhuo }
837b8ef4809SXuan Zhuo 
__virtnet_xdp_xmit_one(struct virtnet_info * vi,struct send_queue * sq,struct xdp_frame * xdpf)838735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
839735fc405SJesper Dangaard Brouer 				   struct send_queue *sq,
84044fa2dbdSJesper Dangaard Brouer 				   struct xdp_frame *xdpf)
84156434a01SJohn Fastabend {
84256434a01SJohn Fastabend 	struct virtio_net_hdr_mrg_rxbuf *hdr;
84397717e8dSHeng Qi 	struct skb_shared_info *shinfo;
84497717e8dSHeng Qi 	u8 nr_frags = 0;
84597717e8dSHeng Qi 	int err, i;
84656434a01SJohn Fastabend 
847cac320c8SJesper Dangaard Brouer 	if (unlikely(xdpf->headroom < vi->hdr_len))
848cac320c8SJesper Dangaard Brouer 		return -EOVERFLOW;
849cac320c8SJesper Dangaard Brouer 
85097717e8dSHeng Qi 	if (unlikely(xdp_frame_has_frags(xdpf))) {
85197717e8dSHeng Qi 		shinfo = xdp_get_shared_info_from_frame(xdpf);
85297717e8dSHeng Qi 		nr_frags = shinfo->nr_frags;
85397717e8dSHeng Qi 	}
85497717e8dSHeng Qi 
85597717e8dSHeng Qi 	/* In wrapping function virtnet_xdp_xmit(), we need to free
85697717e8dSHeng Qi 	 * up the pending old buffers, where we need to calculate the
85797717e8dSHeng Qi 	 * position of skb_shared_info in xdp_get_frame_len() and
85897717e8dSHeng Qi 	 * xdp_return_frame(), which will involve to xdpf->data and
85997717e8dSHeng Qi 	 * xdpf->headroom. Therefore, we need to update the value of
86097717e8dSHeng Qi 	 * headroom synchronously here.
86197717e8dSHeng Qi 	 */
86297717e8dSHeng Qi 	xdpf->headroom -= vi->hdr_len;
863cac320c8SJesper Dangaard Brouer 	xdpf->data -= vi->hdr_len;
86456434a01SJohn Fastabend 	/* Zero header and leave csum up to XDP layers */
865cac320c8SJesper Dangaard Brouer 	hdr = xdpf->data;
86656434a01SJohn Fastabend 	memset(hdr, 0, vi->hdr_len);
867cac320c8SJesper Dangaard Brouer 	xdpf->len   += vi->hdr_len;
86856434a01SJohn Fastabend 
86997717e8dSHeng Qi 	sg_init_table(sq->sg, nr_frags + 1);
87097717e8dSHeng Qi 	sg_set_buf(sq->sg, xdpf->data, xdpf->len);
87197717e8dSHeng Qi 	for (i = 0; i < nr_frags; i++) {
87297717e8dSHeng Qi 		skb_frag_t *frag = &shinfo->frags[i];
873bb91accfSJason Wang 
87497717e8dSHeng Qi 		sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
87597717e8dSHeng Qi 			    skb_frag_size(frag), skb_frag_off(frag));
87697717e8dSHeng Qi 	}
87797717e8dSHeng Qi 
87897717e8dSHeng Qi 	err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
87997717e8dSHeng Qi 				   xdp_to_ptr(xdpf), GFP_ATOMIC);
88011b7d897SJesper Dangaard Brouer 	if (unlikely(err))
881cac320c8SJesper Dangaard Brouer 		return -ENOSPC; /* Caller handle free/refcnt */
88256434a01SJohn Fastabend 
883cac320c8SJesper Dangaard Brouer 	return 0;
88456434a01SJohn Fastabend }
88556434a01SJohn Fastabend 
88697c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
88797c2c69eSXuan Zhuo  * the current cpu, so it does not need to be locked.
88897c2c69eSXuan Zhuo  *
88997c2c69eSXuan Zhuo  * Here we use marco instead of inline functions because we have to deal with
89097c2c69eSXuan Zhuo  * three issues at the same time: 1. the choice of sq. 2. judge and execute the
89197c2c69eSXuan Zhuo  * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
89297c2c69eSXuan Zhuo  * functions to perfectly solve these three problems at the same time.
89397c2c69eSXuan Zhuo  */
89497c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({                                       \
8953dcc1edcSLi RongQing 	int cpu = smp_processor_id();                                   \
89697c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
89797c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
89897c2c69eSXuan Zhuo 	unsigned int qp;                                                \
89997c2c69eSXuan Zhuo 									\
90097c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
90197c2c69eSXuan Zhuo 		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
9023dcc1edcSLi RongQing 		qp += cpu;                                              \
90397c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
90497c2c69eSXuan Zhuo 		__netif_tx_acquire(txq);                                \
90597c2c69eSXuan Zhuo 	} else {                                                        \
9063dcc1edcSLi RongQing 		qp = cpu % v->curr_queue_pairs;                         \
90797c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
9083dcc1edcSLi RongQing 		__netif_tx_lock(txq, cpu);                              \
90997c2c69eSXuan Zhuo 	}                                                               \
91097c2c69eSXuan Zhuo 	v->sq + qp;                                                     \
91197c2c69eSXuan Zhuo })
9122a43565cSToshiaki Makita 
91397c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) {                                     \
91497c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
91597c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
91697c2c69eSXuan Zhuo 									\
91797c2c69eSXuan Zhuo 	txq = netdev_get_tx_queue(v->dev, (q) - v->sq);                 \
91897c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids)                           \
91997c2c69eSXuan Zhuo 		__netif_tx_release(txq);                                \
92097c2c69eSXuan Zhuo 	else                                                            \
92197c2c69eSXuan Zhuo 		__netif_tx_unlock(txq);                                 \
9222a43565cSToshiaki Makita }
9232a43565cSToshiaki Makita 
virtnet_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)924735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev,
92542b33468SJesper Dangaard Brouer 			    int n, struct xdp_frame **frames, u32 flags)
926186b3c99SJason Wang {
927186b3c99SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
9288dcc5b0aSJesper Dangaard Brouer 	struct receive_queue *rq = vi->rq;
9298dcc5b0aSJesper Dangaard Brouer 	struct bpf_prog *xdp_prog;
930735fc405SJesper Dangaard Brouer 	struct send_queue *sq;
931735fc405SJesper Dangaard Brouer 	unsigned int len;
932546f2897SToshiaki Makita 	int packets = 0;
933546f2897SToshiaki Makita 	int bytes = 0;
934fdc13979SLorenzo Bianconi 	int nxmit = 0;
935461f03dcSToshiaki Makita 	int kicks = 0;
9365050471dSToshiaki Makita 	void *ptr;
937fdc13979SLorenzo Bianconi 	int ret;
938735fc405SJesper Dangaard Brouer 	int i;
939735fc405SJesper Dangaard Brouer 
9408dcc5b0aSJesper Dangaard Brouer 	/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
9418dcc5b0aSJesper Dangaard Brouer 	 * indicate XDP resources have been successfully allocated.
9428dcc5b0aSJesper Dangaard Brouer 	 */
9439719c6b9SJohn Fastabend 	xdp_prog = rcu_access_pointer(rq->xdp_prog);
9441667c08aSToshiaki Makita 	if (!xdp_prog)
9451667c08aSToshiaki Makita 		return -ENXIO;
9461667c08aSToshiaki Makita 
94797c2c69eSXuan Zhuo 	sq = virtnet_xdp_get_sq(vi);
9489ab86bbcSShirley Ma 
9499ab86bbcSShirley Ma 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
950186b3c99SJason Wang 		ret = -EINVAL;
951186b3c99SJason Wang 		goto out;
952186b3c99SJason Wang 	}
953186b3c99SJason Wang 
954735fc405SJesper Dangaard Brouer 	/* Free up any pending old buffers before queueing new ones. */
9555050471dSToshiaki Makita 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
956546f2897SToshiaki Makita 		if (likely(is_xdp_frame(ptr))) {
957546f2897SToshiaki Makita 			struct xdp_frame *frame = ptr_to_xdp(ptr);
958546f2897SToshiaki Makita 
95950bd14bcSHeng Qi 			bytes += xdp_get_frame_len(frame);
960546f2897SToshiaki Makita 			xdp_return_frame(frame);
961546f2897SToshiaki Makita 		} else {
962546f2897SToshiaki Makita 			struct sk_buff *skb = ptr;
963546f2897SToshiaki Makita 
964546f2897SToshiaki Makita 			bytes += skb->len;
965546f2897SToshiaki Makita 			napi_consume_skb(skb, false);
966546f2897SToshiaki Makita 		}
967546f2897SToshiaki Makita 		packets++;
9685050471dSToshiaki Makita 	}
969735fc405SJesper Dangaard Brouer 
970735fc405SJesper Dangaard Brouer 	for (i = 0; i < n; i++) {
971735fc405SJesper Dangaard Brouer 		struct xdp_frame *xdpf = frames[i];
972735fc405SJesper Dangaard Brouer 
973fdc13979SLorenzo Bianconi 		if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
974fdc13979SLorenzo Bianconi 			break;
975fdc13979SLorenzo Bianconi 		nxmit++;
976735fc405SJesper Dangaard Brouer 	}
977fdc13979SLorenzo Bianconi 	ret = nxmit;
9785d274cb4SJesper Dangaard Brouer 
979cd1c604aSXuan Zhuo 	if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
980cd1c604aSXuan Zhuo 		check_sq_full_and_disable(vi, dev, sq);
981cd1c604aSXuan Zhuo 
982461f03dcSToshiaki Makita 	if (flags & XDP_XMIT_FLUSH) {
983461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
984461f03dcSToshiaki Makita 			kicks = 1;
985461f03dcSToshiaki Makita 	}
9865b8f3c8dSToshiaki Makita out:
9875b8f3c8dSToshiaki Makita 	u64_stats_update_begin(&sq->stats.syncp);
98827debe3eSEric Dumazet 	u64_stats_add(&sq->stats.bytes, bytes);
98927debe3eSEric Dumazet 	u64_stats_add(&sq->stats.packets, packets);
99027debe3eSEric Dumazet 	u64_stats_add(&sq->stats.xdp_tx, n);
99127debe3eSEric Dumazet 	u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
99227debe3eSEric Dumazet 	u64_stats_add(&sq->stats.kicks, kicks);
9935b8f3c8dSToshiaki Makita 	u64_stats_update_end(&sq->stats.syncp);
9945d274cb4SJesper Dangaard Brouer 
99597c2c69eSXuan Zhuo 	virtnet_xdp_put_sq(vi, sq);
9965b8f3c8dSToshiaki Makita 	return ret;
997186b3c99SJason Wang }
998186b3c99SJason Wang 
put_xdp_frags(struct xdp_buff * xdp)999bb2c1e9eSXuan Zhuo static void put_xdp_frags(struct xdp_buff *xdp)
1000bb2c1e9eSXuan Zhuo {
1001bb2c1e9eSXuan Zhuo 	struct skb_shared_info *shinfo;
1002bb2c1e9eSXuan Zhuo 	struct page *xdp_page;
1003bb2c1e9eSXuan Zhuo 	int i;
1004bb2c1e9eSXuan Zhuo 
1005bb2c1e9eSXuan Zhuo 	if (xdp_buff_has_frags(xdp)) {
1006bb2c1e9eSXuan Zhuo 		shinfo = xdp_get_shared_info_from_buff(xdp);
1007bb2c1e9eSXuan Zhuo 		for (i = 0; i < shinfo->nr_frags; i++) {
1008bb2c1e9eSXuan Zhuo 			xdp_page = skb_frag_page(&shinfo->frags[i]);
1009bb2c1e9eSXuan Zhuo 			put_page(xdp_page);
1010bb2c1e9eSXuan Zhuo 		}
1011bb2c1e9eSXuan Zhuo 	}
1012bb2c1e9eSXuan Zhuo }
1013bb2c1e9eSXuan Zhuo 
virtnet_xdp_handler(struct bpf_prog * xdp_prog,struct xdp_buff * xdp,struct net_device * dev,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)101400765f8eSXuan Zhuo static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
101500765f8eSXuan Zhuo 			       struct net_device *dev,
101600765f8eSXuan Zhuo 			       unsigned int *xdp_xmit,
101700765f8eSXuan Zhuo 			       struct virtnet_rq_stats *stats)
101800765f8eSXuan Zhuo {
101900765f8eSXuan Zhuo 	struct xdp_frame *xdpf;
102000765f8eSXuan Zhuo 	int err;
102100765f8eSXuan Zhuo 	u32 act;
102200765f8eSXuan Zhuo 
102300765f8eSXuan Zhuo 	act = bpf_prog_run_xdp(xdp_prog, xdp);
102427debe3eSEric Dumazet 	u64_stats_inc(&stats->xdp_packets);
102500765f8eSXuan Zhuo 
102600765f8eSXuan Zhuo 	switch (act) {
102700765f8eSXuan Zhuo 	case XDP_PASS:
102800765f8eSXuan Zhuo 		return act;
102900765f8eSXuan Zhuo 
103000765f8eSXuan Zhuo 	case XDP_TX:
103127debe3eSEric Dumazet 		u64_stats_inc(&stats->xdp_tx);
103200765f8eSXuan Zhuo 		xdpf = xdp_convert_buff_to_frame(xdp);
103300765f8eSXuan Zhuo 		if (unlikely(!xdpf)) {
103400765f8eSXuan Zhuo 			netdev_dbg(dev, "convert buff to frame failed for xdp\n");
103500765f8eSXuan Zhuo 			return XDP_DROP;
103600765f8eSXuan Zhuo 		}
103700765f8eSXuan Zhuo 
103800765f8eSXuan Zhuo 		err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
103900765f8eSXuan Zhuo 		if (unlikely(!err)) {
104000765f8eSXuan Zhuo 			xdp_return_frame_rx_napi(xdpf);
104100765f8eSXuan Zhuo 		} else if (unlikely(err < 0)) {
104200765f8eSXuan Zhuo 			trace_xdp_exception(dev, xdp_prog, act);
104300765f8eSXuan Zhuo 			return XDP_DROP;
104400765f8eSXuan Zhuo 		}
104500765f8eSXuan Zhuo 		*xdp_xmit |= VIRTIO_XDP_TX;
104600765f8eSXuan Zhuo 		return act;
104700765f8eSXuan Zhuo 
104800765f8eSXuan Zhuo 	case XDP_REDIRECT:
104927debe3eSEric Dumazet 		u64_stats_inc(&stats->xdp_redirects);
105000765f8eSXuan Zhuo 		err = xdp_do_redirect(dev, xdp, xdp_prog);
105100765f8eSXuan Zhuo 		if (err)
105200765f8eSXuan Zhuo 			return XDP_DROP;
105300765f8eSXuan Zhuo 
105400765f8eSXuan Zhuo 		*xdp_xmit |= VIRTIO_XDP_REDIR;
105500765f8eSXuan Zhuo 		return act;
105600765f8eSXuan Zhuo 
105700765f8eSXuan Zhuo 	default:
105800765f8eSXuan Zhuo 		bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
105900765f8eSXuan Zhuo 		fallthrough;
106000765f8eSXuan Zhuo 	case XDP_ABORTED:
106100765f8eSXuan Zhuo 		trace_xdp_exception(dev, xdp_prog, act);
106200765f8eSXuan Zhuo 		fallthrough;
106300765f8eSXuan Zhuo 	case XDP_DROP:
106400765f8eSXuan Zhuo 		return XDP_DROP;
106500765f8eSXuan Zhuo 	}
106600765f8eSXuan Zhuo }
106700765f8eSXuan Zhuo 
virtnet_get_headroom(struct virtnet_info * vi)1068f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1069f6b10209SJason Wang {
107097c2c69eSXuan Zhuo 	return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
1071f6b10209SJason Wang }
1072f6b10209SJason Wang 
10734941d472SJason Wang /* We copy the packet for XDP in the following cases:
10744941d472SJason Wang  *
10754941d472SJason Wang  * 1) Packet is scattered across multiple rx buffers.
10764941d472SJason Wang  * 2) Headroom space is insufficient.
10774941d472SJason Wang  *
10784941d472SJason Wang  * This is inefficient but it's a temporary condition that
10794941d472SJason Wang  * we hit right after XDP is enabled and until queue is refilled
10804941d472SJason Wang  * with large buffers with sufficient headroom - so it should affect
10814941d472SJason Wang  * at most queue size packets.
10824941d472SJason Wang  * Afterwards, the conditions to enable
10834941d472SJason Wang  * XDP should preclude the underlying device from sending packets
10844941d472SJason Wang  * across multiple buffers (num_buf > 1), and we make sure buffers
10854941d472SJason Wang  * have enough headroom.
108672979a6cSJohn Fastabend  */
xdp_linearize_page(struct receive_queue * rq,int * num_buf,struct page * p,int offset,int page_off,unsigned int * len)108772979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq,
1088981f14d4SHeng Qi 				       int *num_buf,
108972979a6cSJohn Fastabend 				       struct page *p,
109072979a6cSJohn Fastabend 				       int offset,
10914941d472SJason Wang 				       int page_off,
109272979a6cSJohn Fastabend 				       unsigned int *len)
109372979a6cSJohn Fastabend {
1094853618d5SXuan Zhuo 	int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1095853618d5SXuan Zhuo 	struct page *page;
109672979a6cSJohn Fastabend 
1097853618d5SXuan Zhuo 	if (page_off + *len + tailroom > PAGE_SIZE)
1098853618d5SXuan Zhuo 		return NULL;
1099853618d5SXuan Zhuo 
1100853618d5SXuan Zhuo 	page = alloc_page(GFP_ATOMIC);
110172979a6cSJohn Fastabend 	if (!page)
110272979a6cSJohn Fastabend 		return NULL;
110372979a6cSJohn Fastabend 
110472979a6cSJohn Fastabend 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
110572979a6cSJohn Fastabend 	page_off += *len;
110672979a6cSJohn Fastabend 
110756a86f84SJason Wang 	while (--*num_buf) {
110872979a6cSJohn Fastabend 		unsigned int buflen;
110972979a6cSJohn Fastabend 		void *buf;
111072979a6cSJohn Fastabend 		int off;
111172979a6cSJohn Fastabend 
1112295525e2SXuan Zhuo 		buf = virtnet_rq_get_buf(rq, &buflen, NULL);
1113680557cfSMichael S. Tsirkin 		if (unlikely(!buf))
111472979a6cSJohn Fastabend 			goto err_buf;
111572979a6cSJohn Fastabend 
111672979a6cSJohn Fastabend 		p = virt_to_head_page(buf);
111772979a6cSJohn Fastabend 		off = buf - page_address(p);
111872979a6cSJohn Fastabend 
111956a86f84SJason Wang 		/* guard against a misconfigured or uncooperative backend that
112056a86f84SJason Wang 		 * is sending packet larger than the MTU.
112156a86f84SJason Wang 		 */
11223cc81a9aSJason Wang 		if ((page_off + buflen + tailroom) > PAGE_SIZE) {
112356a86f84SJason Wang 			put_page(p);
112456a86f84SJason Wang 			goto err_buf;
112556a86f84SJason Wang 		}
112656a86f84SJason Wang 
112772979a6cSJohn Fastabend 		memcpy(page_address(page) + page_off,
112872979a6cSJohn Fastabend 		       page_address(p) + off, buflen);
112972979a6cSJohn Fastabend 		page_off += buflen;
113056a86f84SJason Wang 		put_page(p);
113172979a6cSJohn Fastabend 	}
113272979a6cSJohn Fastabend 
11332de2f7f4SJohn Fastabend 	/* Headroom does not contribute to packet length */
11342de2f7f4SJohn Fastabend 	*len = page_off - VIRTIO_XDP_HEADROOM;
113572979a6cSJohn Fastabend 	return page;
113672979a6cSJohn Fastabend err_buf:
113772979a6cSJohn Fastabend 	__free_pages(page, 0);
113872979a6cSJohn Fastabend 	return NULL;
113972979a6cSJohn Fastabend }
114072979a6cSJohn Fastabend 
receive_small_build_skb(struct virtnet_info * vi,unsigned int xdp_headroom,void * buf,unsigned int len)114119e8c85eSXuan Zhuo static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
114219e8c85eSXuan Zhuo 					       unsigned int xdp_headroom,
114319e8c85eSXuan Zhuo 					       void *buf,
114419e8c85eSXuan Zhuo 					       unsigned int len)
114519e8c85eSXuan Zhuo {
114619e8c85eSXuan Zhuo 	unsigned int header_offset;
114719e8c85eSXuan Zhuo 	unsigned int headroom;
114819e8c85eSXuan Zhuo 	unsigned int buflen;
114919e8c85eSXuan Zhuo 	struct sk_buff *skb;
115019e8c85eSXuan Zhuo 
115119e8c85eSXuan Zhuo 	header_offset = VIRTNET_RX_PAD + xdp_headroom;
115219e8c85eSXuan Zhuo 	headroom = vi->hdr_len + header_offset;
115319e8c85eSXuan Zhuo 	buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
115419e8c85eSXuan Zhuo 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
115519e8c85eSXuan Zhuo 
115621e26a71SXuan Zhuo 	skb = virtnet_build_skb(buf, buflen, headroom, len);
115721e26a71SXuan Zhuo 	if (unlikely(!skb))
115819e8c85eSXuan Zhuo 		return NULL;
115919e8c85eSXuan Zhuo 
116019e8c85eSXuan Zhuo 	buf += header_offset;
1161dae64749SFeng Liu 	memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
116219e8c85eSXuan Zhuo 
116319e8c85eSXuan Zhuo 	return skb;
116419e8c85eSXuan Zhuo }
116519e8c85eSXuan Zhuo 
receive_small_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,unsigned int xdp_headroom,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1166c5f3e72fSXuan Zhuo static struct sk_buff *receive_small_xdp(struct net_device *dev,
11674941d472SJason Wang 					 struct virtnet_info *vi,
11684941d472SJason Wang 					 struct receive_queue *rq,
1169c5f3e72fSXuan Zhuo 					 struct bpf_prog *xdp_prog,
1170c5f3e72fSXuan Zhuo 					 void *buf,
1171c5f3e72fSXuan Zhuo 					 unsigned int xdp_headroom,
1172186b3c99SJason Wang 					 unsigned int len,
11737d9d60fdSToshiaki Makita 					 unsigned int *xdp_xmit,
1174d46eeeafSJason Wang 					 struct virtnet_rq_stats *stats)
11754941d472SJason Wang {
11764941d472SJason Wang 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
11774941d472SJason Wang 	unsigned int headroom = vi->hdr_len + header_offset;
11784941d472SJason Wang 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1179c5f3e72fSXuan Zhuo 	struct page *page = virt_to_head_page(buf);
1180c5f3e72fSXuan Zhuo 	struct page *xdp_page;
1181c5f3e72fSXuan Zhuo 	unsigned int buflen;
11824941d472SJason Wang 	struct xdp_buff xdp;
1183c5f3e72fSXuan Zhuo 	struct sk_buff *skb;
1184c5f3e72fSXuan Zhuo 	unsigned int metasize = 0;
11854941d472SJason Wang 	u32 act;
11864941d472SJason Wang 
118795dbe9e7SJesper Dangaard Brouer 	if (unlikely(hdr->hdr.gso_type))
11884941d472SJason Wang 		goto err_xdp;
11894941d472SJason Wang 
11903ef2a16aSHeng Qi 	/* Partially checksummed packets must be dropped. */
11913ef2a16aSHeng Qi 	if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
11923ef2a16aSHeng Qi 		goto err_xdp;
11933ef2a16aSHeng Qi 
1194c5f3e72fSXuan Zhuo 	buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1195c5f3e72fSXuan Zhuo 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1196c5f3e72fSXuan Zhuo 
11974941d472SJason Wang 	if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
11984941d472SJason Wang 		int offset = buf - page_address(page) + header_offset;
11994941d472SJason Wang 		unsigned int tlen = len + vi->hdr_len;
1200981f14d4SHeng Qi 		int num_buf = 1;
12014941d472SJason Wang 
12024941d472SJason Wang 		xdp_headroom = virtnet_get_headroom(vi);
12034941d472SJason Wang 		header_offset = VIRTNET_RX_PAD + xdp_headroom;
12044941d472SJason Wang 		headroom = vi->hdr_len + header_offset;
12054941d472SJason Wang 		buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
12064941d472SJason Wang 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
12074941d472SJason Wang 		xdp_page = xdp_linearize_page(rq, &num_buf, page,
12084941d472SJason Wang 					      offset, header_offset,
12094941d472SJason Wang 					      &tlen);
12104941d472SJason Wang 		if (!xdp_page)
12114941d472SJason Wang 			goto err_xdp;
12124941d472SJason Wang 
12134941d472SJason Wang 		buf = page_address(xdp_page);
12144941d472SJason Wang 		put_page(page);
12154941d472SJason Wang 		page = xdp_page;
12164941d472SJason Wang 	}
12174941d472SJason Wang 
121843b5169dSLorenzo Bianconi 	xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1219be9df4afSLorenzo Bianconi 	xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1220be9df4afSLorenzo Bianconi 			 xdp_headroom, len, true);
122100765f8eSXuan Zhuo 
122200765f8eSXuan Zhuo 	act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
12234941d472SJason Wang 
12244941d472SJason Wang 	switch (act) {
12254941d472SJason Wang 	case XDP_PASS:
12264941d472SJason Wang 		/* Recalculate length in case bpf program changed it */
12276870de43SNikita V. Shirokov 		len = xdp.data_end - xdp.data;
1228503d539aSYuya Kusakabe 		metasize = xdp.data - xdp.data_meta;
12294941d472SJason Wang 		break;
1230c5f3e72fSXuan Zhuo 
12314941d472SJason Wang 	case XDP_TX:
1232186b3c99SJason Wang 	case XDP_REDIRECT:
12334941d472SJason Wang 		goto xdp_xmit;
1234c5f3e72fSXuan Zhuo 
12354941d472SJason Wang 	default:
12364941d472SJason Wang 		goto err_xdp;
12374941d472SJason Wang 	}
1238c5f3e72fSXuan Zhuo 
123921e26a71SXuan Zhuo 	skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
124021e26a71SXuan Zhuo 	if (unlikely(!skb))
1241c5f3e72fSXuan Zhuo 		goto err;
1242c5f3e72fSXuan Zhuo 
1243c5f3e72fSXuan Zhuo 	if (metasize)
1244c5f3e72fSXuan Zhuo 		skb_metadata_set(skb, metasize);
1245c5f3e72fSXuan Zhuo 
1246c5f3e72fSXuan Zhuo 	return skb;
1247c5f3e72fSXuan Zhuo 
1248c5f3e72fSXuan Zhuo err_xdp:
124927debe3eSEric Dumazet 	u64_stats_inc(&stats->xdp_drops);
1250c5f3e72fSXuan Zhuo err:
125127debe3eSEric Dumazet 	u64_stats_inc(&stats->drops);
1252c5f3e72fSXuan Zhuo 	put_page(page);
1253c5f3e72fSXuan Zhuo xdp_xmit:
1254c5f3e72fSXuan Zhuo 	return NULL;
1255c5f3e72fSXuan Zhuo }
1256c5f3e72fSXuan Zhuo 
receive_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1257c5f3e72fSXuan Zhuo static struct sk_buff *receive_small(struct net_device *dev,
1258c5f3e72fSXuan Zhuo 				     struct virtnet_info *vi,
1259c5f3e72fSXuan Zhuo 				     struct receive_queue *rq,
1260c5f3e72fSXuan Zhuo 				     void *buf, void *ctx,
1261c5f3e72fSXuan Zhuo 				     unsigned int len,
1262c5f3e72fSXuan Zhuo 				     unsigned int *xdp_xmit,
1263c5f3e72fSXuan Zhuo 				     struct virtnet_rq_stats *stats)
1264c5f3e72fSXuan Zhuo {
1265c5f3e72fSXuan Zhuo 	unsigned int xdp_headroom = (unsigned long)ctx;
1266c5f3e72fSXuan Zhuo 	struct page *page = virt_to_head_page(buf);
1267aef76506SXuan Zhuo 	struct sk_buff *skb;
1268c5f3e72fSXuan Zhuo 
1269e9e3424dSWenbo Li 	/* We passed the address of virtnet header to virtio-core,
1270e9e3424dSWenbo Li 	 * so truncate the padding.
1271e9e3424dSWenbo Li 	 */
1272e9e3424dSWenbo Li 	buf -= VIRTNET_RX_PAD + xdp_headroom;
1273e9e3424dSWenbo Li 
1274c5f3e72fSXuan Zhuo 	len -= vi->hdr_len;
127527debe3eSEric Dumazet 	u64_stats_add(&stats->bytes, len);
1276c5f3e72fSXuan Zhuo 
1277c5f3e72fSXuan Zhuo 	if (unlikely(len > GOOD_PACKET_LEN)) {
1278c5f3e72fSXuan Zhuo 		pr_debug("%s: rx error: len %u exceeds max size %d\n",
1279c5f3e72fSXuan Zhuo 			 dev->name, len, GOOD_PACKET_LEN);
1280e2e5c2a3SEric Dumazet 		DEV_STATS_INC(dev, rx_length_errors);
1281c5f3e72fSXuan Zhuo 		goto err;
1282c5f3e72fSXuan Zhuo 	}
1283c5f3e72fSXuan Zhuo 
1284aef76506SXuan Zhuo 	if (unlikely(vi->xdp_enabled)) {
1285aef76506SXuan Zhuo 		struct bpf_prog *xdp_prog;
1286c5f3e72fSXuan Zhuo 
1287c5f3e72fSXuan Zhuo 		rcu_read_lock();
1288c5f3e72fSXuan Zhuo 		xdp_prog = rcu_dereference(rq->xdp_prog);
1289c5f3e72fSXuan Zhuo 		if (xdp_prog) {
1290aef76506SXuan Zhuo 			skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1291aef76506SXuan Zhuo 						xdp_headroom, len, xdp_xmit,
1292aef76506SXuan Zhuo 						stats);
1293c5f3e72fSXuan Zhuo 			rcu_read_unlock();
1294c5f3e72fSXuan Zhuo 			return skb;
12954941d472SJason Wang 		}
12964941d472SJason Wang 		rcu_read_unlock();
1297aef76506SXuan Zhuo 	}
12984941d472SJason Wang 
129919e8c85eSXuan Zhuo 	skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
130019e8c85eSXuan Zhuo 	if (likely(skb))
13014941d472SJason Wang 		return skb;
13024941d472SJason Wang 
1303053c9e18SWenliang Wang err:
130427debe3eSEric Dumazet 	u64_stats_inc(&stats->drops);
13054941d472SJason Wang 	put_page(page);
13064941d472SJason Wang 	return NULL;
13074941d472SJason Wang }
13084941d472SJason Wang 
receive_big(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,struct virtnet_rq_stats * stats)13094941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev,
13104941d472SJason Wang 				   struct virtnet_info *vi,
13114941d472SJason Wang 				   struct receive_queue *rq,
13124941d472SJason Wang 				   void *buf,
13137d9d60fdSToshiaki Makita 				   unsigned int len,
1314d46eeeafSJason Wang 				   struct virtnet_rq_stats *stats)
13154941d472SJason Wang {
13164941d472SJason Wang 	struct page *page = buf;
1317503d539aSYuya Kusakabe 	struct sk_buff *skb =
1318fa0f1ba7SXuan Zhuo 		page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
13194941d472SJason Wang 
132027debe3eSEric Dumazet 	u64_stats_add(&stats->bytes, len - vi->hdr_len);
13214941d472SJason Wang 	if (unlikely(!skb))
13224941d472SJason Wang 		goto err;
13234941d472SJason Wang 
13244941d472SJason Wang 	return skb;
13254941d472SJason Wang 
13264941d472SJason Wang err:
132727debe3eSEric Dumazet 	u64_stats_inc(&stats->drops);
13284941d472SJason Wang 	give_pages(rq, page);
13294941d472SJason Wang 	return NULL;
13304941d472SJason Wang }
13314941d472SJason Wang 
mergeable_buf_free(struct receive_queue * rq,int num_buf,struct net_device * dev,struct virtnet_rq_stats * stats)133280f50f91SXuan Zhuo static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
133380f50f91SXuan Zhuo 			       struct net_device *dev,
133480f50f91SXuan Zhuo 			       struct virtnet_rq_stats *stats)
133580f50f91SXuan Zhuo {
133680f50f91SXuan Zhuo 	struct page *page;
133780f50f91SXuan Zhuo 	void *buf;
133880f50f91SXuan Zhuo 	int len;
133980f50f91SXuan Zhuo 
134080f50f91SXuan Zhuo 	while (num_buf-- > 1) {
1341295525e2SXuan Zhuo 		buf = virtnet_rq_get_buf(rq, &len, NULL);
134280f50f91SXuan Zhuo 		if (unlikely(!buf)) {
134380f50f91SXuan Zhuo 			pr_debug("%s: rx error: %d buffers missing\n",
134480f50f91SXuan Zhuo 				 dev->name, num_buf);
1345e2e5c2a3SEric Dumazet 			DEV_STATS_INC(dev, rx_length_errors);
134680f50f91SXuan Zhuo 			break;
134780f50f91SXuan Zhuo 		}
134827debe3eSEric Dumazet 		u64_stats_add(&stats->bytes, len);
134980f50f91SXuan Zhuo 		page = virt_to_head_page(buf);
135080f50f91SXuan Zhuo 		put_page(page);
135180f50f91SXuan Zhuo 	}
135280f50f91SXuan Zhuo }
135380f50f91SXuan Zhuo 
1354b26aa481SHeng Qi /* Why not use xdp_build_skb_from_frame() ?
1355b26aa481SHeng Qi  * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1356b26aa481SHeng Qi  * virtio-net there are 2 points that do not match its requirements:
1357b26aa481SHeng Qi  *  1. The size of the prefilled buffer is not fixed before xdp is set.
1358b26aa481SHeng Qi  *  2. xdp_build_skb_from_frame() does more checks that we don't need,
1359b26aa481SHeng Qi  *     like eth_type_trans() (which virtio-net does in receive_buf()).
1360b26aa481SHeng Qi  */
build_skb_from_xdp_buff(struct net_device * dev,struct virtnet_info * vi,struct xdp_buff * xdp,unsigned int xdp_frags_truesz)1361b26aa481SHeng Qi static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1362b26aa481SHeng Qi 					       struct virtnet_info *vi,
1363b26aa481SHeng Qi 					       struct xdp_buff *xdp,
1364b26aa481SHeng Qi 					       unsigned int xdp_frags_truesz)
1365b26aa481SHeng Qi {
1366b26aa481SHeng Qi 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1367b26aa481SHeng Qi 	unsigned int headroom, data_len;
1368b26aa481SHeng Qi 	struct sk_buff *skb;
1369b26aa481SHeng Qi 	int metasize;
1370b26aa481SHeng Qi 	u8 nr_frags;
1371b26aa481SHeng Qi 
1372b26aa481SHeng Qi 	if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1373b26aa481SHeng Qi 		pr_debug("Error building skb as missing reserved tailroom for xdp");
1374b26aa481SHeng Qi 		return NULL;
1375b26aa481SHeng Qi 	}
1376b26aa481SHeng Qi 
1377b26aa481SHeng Qi 	if (unlikely(xdp_buff_has_frags(xdp)))
1378b26aa481SHeng Qi 		nr_frags = sinfo->nr_frags;
1379b26aa481SHeng Qi 
1380b26aa481SHeng Qi 	skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1381b26aa481SHeng Qi 	if (unlikely(!skb))
1382b26aa481SHeng Qi 		return NULL;
1383b26aa481SHeng Qi 
1384b26aa481SHeng Qi 	headroom = xdp->data - xdp->data_hard_start;
1385b26aa481SHeng Qi 	data_len = xdp->data_end - xdp->data;
1386b26aa481SHeng Qi 	skb_reserve(skb, headroom);
1387b26aa481SHeng Qi 	__skb_put(skb, data_len);
1388b26aa481SHeng Qi 
1389b26aa481SHeng Qi 	metasize = xdp->data - xdp->data_meta;
1390b26aa481SHeng Qi 	metasize = metasize > 0 ? metasize : 0;
1391b26aa481SHeng Qi 	if (metasize)
1392b26aa481SHeng Qi 		skb_metadata_set(skb, metasize);
1393b26aa481SHeng Qi 
1394b26aa481SHeng Qi 	if (unlikely(xdp_buff_has_frags(xdp)))
1395b26aa481SHeng Qi 		xdp_update_skb_shared_info(skb, nr_frags,
1396b26aa481SHeng Qi 					   sinfo->xdp_frags_size,
1397b26aa481SHeng Qi 					   xdp_frags_truesz,
1398b26aa481SHeng Qi 					   xdp_buff_is_frag_pfmemalloc(xdp));
1399b26aa481SHeng Qi 
1400b26aa481SHeng Qi 	return skb;
1401b26aa481SHeng Qi }
1402b26aa481SHeng Qi 
1403ef75cb51SHeng Qi /* TODO: build xdp in big mode */
virtnet_build_xdp_buff_mrg(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,void * buf,unsigned int len,unsigned int frame_sz,int * num_buf,unsigned int * xdp_frags_truesize,struct virtnet_rq_stats * stats)1404ef75cb51SHeng Qi static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1405ef75cb51SHeng Qi 				      struct virtnet_info *vi,
1406ef75cb51SHeng Qi 				      struct receive_queue *rq,
1407ef75cb51SHeng Qi 				      struct xdp_buff *xdp,
1408ef75cb51SHeng Qi 				      void *buf,
1409ef75cb51SHeng Qi 				      unsigned int len,
1410ef75cb51SHeng Qi 				      unsigned int frame_sz,
1411981f14d4SHeng Qi 				      int *num_buf,
1412ef75cb51SHeng Qi 				      unsigned int *xdp_frags_truesize,
1413ef75cb51SHeng Qi 				      struct virtnet_rq_stats *stats)
1414ef75cb51SHeng Qi {
1415ef75cb51SHeng Qi 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1416ef75cb51SHeng Qi 	unsigned int headroom, tailroom, room;
1417ef75cb51SHeng Qi 	unsigned int truesize, cur_frag_size;
1418ef75cb51SHeng Qi 	struct skb_shared_info *shinfo;
1419ef75cb51SHeng Qi 	unsigned int xdp_frags_truesz = 0;
1420ef75cb51SHeng Qi 	struct page *page;
1421ef75cb51SHeng Qi 	skb_frag_t *frag;
1422ef75cb51SHeng Qi 	int offset;
1423ef75cb51SHeng Qi 	void *ctx;
1424ef75cb51SHeng Qi 
1425ef75cb51SHeng Qi 	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1426ef75cb51SHeng Qi 	xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1427ef75cb51SHeng Qi 			 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1428ef75cb51SHeng Qi 
1429981f14d4SHeng Qi 	if (!*num_buf)
1430981f14d4SHeng Qi 		return 0;
1431981f14d4SHeng Qi 
1432ef75cb51SHeng Qi 	if (*num_buf > 1) {
1433ef75cb51SHeng Qi 		/* If we want to build multi-buffer xdp, we need
1434ef75cb51SHeng Qi 		 * to specify that the flags of xdp_buff have the
1435ef75cb51SHeng Qi 		 * XDP_FLAGS_HAS_FRAG bit.
1436ef75cb51SHeng Qi 		 */
1437ef75cb51SHeng Qi 		if (!xdp_buff_has_frags(xdp))
1438ef75cb51SHeng Qi 			xdp_buff_set_frags_flag(xdp);
1439ef75cb51SHeng Qi 
1440ef75cb51SHeng Qi 		shinfo = xdp_get_shared_info_from_buff(xdp);
1441ef75cb51SHeng Qi 		shinfo->nr_frags = 0;
1442ef75cb51SHeng Qi 		shinfo->xdp_frags_size = 0;
1443ef75cb51SHeng Qi 	}
1444ef75cb51SHeng Qi 
1445981f14d4SHeng Qi 	if (*num_buf > MAX_SKB_FRAGS + 1)
1446ef75cb51SHeng Qi 		return -EINVAL;
1447ef75cb51SHeng Qi 
1448981f14d4SHeng Qi 	while (--*num_buf > 0) {
1449295525e2SXuan Zhuo 		buf = virtnet_rq_get_buf(rq, &len, &ctx);
1450ef75cb51SHeng Qi 		if (unlikely(!buf)) {
1451ef75cb51SHeng Qi 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1452ef75cb51SHeng Qi 				 dev->name, *num_buf,
1453ef75cb51SHeng Qi 				 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1454e2e5c2a3SEric Dumazet 			DEV_STATS_INC(dev, rx_length_errors);
14554cb00b13SXuan Zhuo 			goto err;
1456ef75cb51SHeng Qi 		}
1457ef75cb51SHeng Qi 
145827debe3eSEric Dumazet 		u64_stats_add(&stats->bytes, len);
1459ef75cb51SHeng Qi 		page = virt_to_head_page(buf);
1460ef75cb51SHeng Qi 		offset = buf - page_address(page);
1461ef75cb51SHeng Qi 
1462ef75cb51SHeng Qi 		truesize = mergeable_ctx_to_truesize(ctx);
1463ef75cb51SHeng Qi 		headroom = mergeable_ctx_to_headroom(ctx);
1464ef75cb51SHeng Qi 		tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1465ef75cb51SHeng Qi 		room = SKB_DATA_ALIGN(headroom + tailroom);
1466ef75cb51SHeng Qi 
1467ef75cb51SHeng Qi 		cur_frag_size = truesize;
1468ef75cb51SHeng Qi 		xdp_frags_truesz += cur_frag_size;
1469ef75cb51SHeng Qi 		if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1470ef75cb51SHeng Qi 			put_page(page);
1471ef75cb51SHeng Qi 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1472ef75cb51SHeng Qi 				 dev->name, len, (unsigned long)(truesize - room));
1473e2e5c2a3SEric Dumazet 			DEV_STATS_INC(dev, rx_length_errors);
14744cb00b13SXuan Zhuo 			goto err;
1475ef75cb51SHeng Qi 		}
1476ef75cb51SHeng Qi 
1477ef75cb51SHeng Qi 		frag = &shinfo->frags[shinfo->nr_frags++];
1478b51f4113SYunsheng Lin 		skb_frag_fill_page_desc(frag, page, offset, len);
1479ef75cb51SHeng Qi 		if (page_is_pfmemalloc(page))
1480ef75cb51SHeng Qi 			xdp_buff_set_frag_pfmemalloc(xdp);
1481ef75cb51SHeng Qi 
1482ef75cb51SHeng Qi 		shinfo->xdp_frags_size += len;
1483ef75cb51SHeng Qi 	}
1484ef75cb51SHeng Qi 
1485ef75cb51SHeng Qi 	*xdp_frags_truesize = xdp_frags_truesz;
1486ef75cb51SHeng Qi 	return 0;
14874cb00b13SXuan Zhuo 
14884cb00b13SXuan Zhuo err:
14894cb00b13SXuan Zhuo 	put_xdp_frags(xdp);
14904cb00b13SXuan Zhuo 	return -EINVAL;
1491ef75cb51SHeng Qi }
1492ef75cb51SHeng Qi 
mergeable_xdp_get_buf(struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * ctx,unsigned int * frame_sz,int * num_buf,struct page ** page,int offset,unsigned int * len,struct virtio_net_hdr_mrg_rxbuf * hdr)1493ad4858beSXuan Zhuo static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
1494ad4858beSXuan Zhuo 				   struct receive_queue *rq,
1495ad4858beSXuan Zhuo 				   struct bpf_prog *xdp_prog,
1496ad4858beSXuan Zhuo 				   void *ctx,
1497ad4858beSXuan Zhuo 				   unsigned int *frame_sz,
1498ad4858beSXuan Zhuo 				   int *num_buf,
1499ad4858beSXuan Zhuo 				   struct page **page,
1500ad4858beSXuan Zhuo 				   int offset,
1501ad4858beSXuan Zhuo 				   unsigned int *len,
1502ad4858beSXuan Zhuo 				   struct virtio_net_hdr_mrg_rxbuf *hdr)
1503ad4858beSXuan Zhuo {
1504ad4858beSXuan Zhuo 	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1505ad4858beSXuan Zhuo 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1506ad4858beSXuan Zhuo 	struct page *xdp_page;
1507ad4858beSXuan Zhuo 	unsigned int xdp_room;
1508ad4858beSXuan Zhuo 
1509ad4858beSXuan Zhuo 	/* Transient failure which in theory could occur if
1510ad4858beSXuan Zhuo 	 * in-flight packets from before XDP was enabled reach
1511ad4858beSXuan Zhuo 	 * the receive path after XDP is loaded.
1512ad4858beSXuan Zhuo 	 */
1513ad4858beSXuan Zhuo 	if (unlikely(hdr->hdr.gso_type))
1514ad4858beSXuan Zhuo 		return NULL;
1515ad4858beSXuan Zhuo 
15163ef2a16aSHeng Qi 	/* Partially checksummed packets must be dropped. */
15173ef2a16aSHeng Qi 	if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
15183ef2a16aSHeng Qi 		return NULL;
15193ef2a16aSHeng Qi 
1520ad4858beSXuan Zhuo 	/* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1521ad4858beSXuan Zhuo 	 * with headroom may add hole in truesize, which
1522ad4858beSXuan Zhuo 	 * make their length exceed PAGE_SIZE. So we disabled the
1523ad4858beSXuan Zhuo 	 * hole mechanism for xdp. See add_recvbuf_mergeable().
1524ad4858beSXuan Zhuo 	 */
1525ad4858beSXuan Zhuo 	*frame_sz = truesize;
1526ad4858beSXuan Zhuo 
1527dbe4fec2SXuan Zhuo 	if (likely(headroom >= virtnet_get_headroom(vi) &&
1528dbe4fec2SXuan Zhuo 		   (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
1529dbe4fec2SXuan Zhuo 		return page_address(*page) + offset;
1530dbe4fec2SXuan Zhuo 	}
1531dbe4fec2SXuan Zhuo 
1532ad4858beSXuan Zhuo 	/* This happens when headroom is not enough because
1533ad4858beSXuan Zhuo 	 * of the buffer was prefilled before XDP is set.
1534ad4858beSXuan Zhuo 	 * This should only happen for the first several packets.
1535ad4858beSXuan Zhuo 	 * In fact, vq reset can be used here to help us clean up
1536ad4858beSXuan Zhuo 	 * the prefilled buffers, but many existing devices do not
1537ad4858beSXuan Zhuo 	 * support it, and we don't want to bother users who are
1538ad4858beSXuan Zhuo 	 * using xdp normally.
1539ad4858beSXuan Zhuo 	 */
1540dbe4fec2SXuan Zhuo 	if (!xdp_prog->aux->xdp_has_frags) {
1541ad4858beSXuan Zhuo 		/* linearize data for XDP */
1542ad4858beSXuan Zhuo 		xdp_page = xdp_linearize_page(rq, num_buf,
1543ad4858beSXuan Zhuo 					      *page, offset,
1544ad4858beSXuan Zhuo 					      VIRTIO_XDP_HEADROOM,
1545ad4858beSXuan Zhuo 					      len);
1546ad4858beSXuan Zhuo 		if (!xdp_page)
1547ad4858beSXuan Zhuo 			return NULL;
1548dbe4fec2SXuan Zhuo 	} else {
1549ad4858beSXuan Zhuo 		xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
1550ad4858beSXuan Zhuo 					  sizeof(struct skb_shared_info));
1551ad4858beSXuan Zhuo 		if (*len + xdp_room > PAGE_SIZE)
1552ad4858beSXuan Zhuo 			return NULL;
1553ad4858beSXuan Zhuo 
1554ad4858beSXuan Zhuo 		xdp_page = alloc_page(GFP_ATOMIC);
1555ad4858beSXuan Zhuo 		if (!xdp_page)
1556ad4858beSXuan Zhuo 			return NULL;
1557ad4858beSXuan Zhuo 
1558ad4858beSXuan Zhuo 		memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
1559ad4858beSXuan Zhuo 		       page_address(*page) + offset, *len);
1560ad4858beSXuan Zhuo 	}
1561ad4858beSXuan Zhuo 
1562dbe4fec2SXuan Zhuo 	*frame_sz = PAGE_SIZE;
1563dbe4fec2SXuan Zhuo 
1564dbe4fec2SXuan Zhuo 	put_page(*page);
1565dbe4fec2SXuan Zhuo 
1566dbe4fec2SXuan Zhuo 	*page = xdp_page;
1567dbe4fec2SXuan Zhuo 
1568dbe4fec2SXuan Zhuo 	return page_address(*page) + VIRTIO_XDP_HEADROOM;
1569ad4858beSXuan Zhuo }
1570ad4858beSXuan Zhuo 
receive_mergeable_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1571d8f2835aSXuan Zhuo static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
1572d8f2835aSXuan Zhuo 					     struct virtnet_info *vi,
1573d8f2835aSXuan Zhuo 					     struct receive_queue *rq,
1574d8f2835aSXuan Zhuo 					     struct bpf_prog *xdp_prog,
1575d8f2835aSXuan Zhuo 					     void *buf,
1576d8f2835aSXuan Zhuo 					     void *ctx,
1577d8f2835aSXuan Zhuo 					     unsigned int len,
1578d8f2835aSXuan Zhuo 					     unsigned int *xdp_xmit,
1579d8f2835aSXuan Zhuo 					     struct virtnet_rq_stats *stats)
1580d8f2835aSXuan Zhuo {
1581d8f2835aSXuan Zhuo 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1582d8f2835aSXuan Zhuo 	int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1583d8f2835aSXuan Zhuo 	struct page *page = virt_to_head_page(buf);
1584d8f2835aSXuan Zhuo 	int offset = buf - page_address(page);
1585d8f2835aSXuan Zhuo 	unsigned int xdp_frags_truesz = 0;
1586d8f2835aSXuan Zhuo 	struct sk_buff *head_skb;
1587d8f2835aSXuan Zhuo 	unsigned int frame_sz;
1588d8f2835aSXuan Zhuo 	struct xdp_buff xdp;
1589d8f2835aSXuan Zhuo 	void *data;
1590d8f2835aSXuan Zhuo 	u32 act;
1591d8f2835aSXuan Zhuo 	int err;
1592d8f2835aSXuan Zhuo 
1593d8f2835aSXuan Zhuo 	data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
1594d8f2835aSXuan Zhuo 				     offset, &len, hdr);
1595d8f2835aSXuan Zhuo 	if (unlikely(!data))
1596d8f2835aSXuan Zhuo 		goto err_xdp;
1597d8f2835aSXuan Zhuo 
1598d8f2835aSXuan Zhuo 	err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
1599d8f2835aSXuan Zhuo 					 &num_buf, &xdp_frags_truesz, stats);
1600d8f2835aSXuan Zhuo 	if (unlikely(err))
1601d8f2835aSXuan Zhuo 		goto err_xdp;
1602d8f2835aSXuan Zhuo 
1603d8f2835aSXuan Zhuo 	act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1604d8f2835aSXuan Zhuo 
1605d8f2835aSXuan Zhuo 	switch (act) {
1606d8f2835aSXuan Zhuo 	case XDP_PASS:
1607d8f2835aSXuan Zhuo 		head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
1608d8f2835aSXuan Zhuo 		if (unlikely(!head_skb))
1609d8f2835aSXuan Zhuo 			break;
1610d8f2835aSXuan Zhuo 		return head_skb;
1611d8f2835aSXuan Zhuo 
1612d8f2835aSXuan Zhuo 	case XDP_TX:
1613d8f2835aSXuan Zhuo 	case XDP_REDIRECT:
1614d8f2835aSXuan Zhuo 		return NULL;
1615d8f2835aSXuan Zhuo 
1616d8f2835aSXuan Zhuo 	default:
1617d8f2835aSXuan Zhuo 		break;
1618d8f2835aSXuan Zhuo 	}
1619d8f2835aSXuan Zhuo 
1620d8f2835aSXuan Zhuo 	put_xdp_frags(&xdp);
1621d8f2835aSXuan Zhuo 
1622d8f2835aSXuan Zhuo err_xdp:
1623d8f2835aSXuan Zhuo 	put_page(page);
1624d8f2835aSXuan Zhuo 	mergeable_buf_free(rq, num_buf, dev, stats);
1625d8f2835aSXuan Zhuo 
162627debe3eSEric Dumazet 	u64_stats_inc(&stats->xdp_drops);
162727debe3eSEric Dumazet 	u64_stats_inc(&stats->drops);
1628d8f2835aSXuan Zhuo 	return NULL;
1629d8f2835aSXuan Zhuo }
1630d8f2835aSXuan Zhuo 
receive_mergeable(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)16318fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
1632fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
16338fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
1634680557cfSMichael S. Tsirkin 					 void *buf,
1635680557cfSMichael S. Tsirkin 					 void *ctx,
1636186b3c99SJason Wang 					 unsigned int len,
16377d9d60fdSToshiaki Makita 					 unsigned int *xdp_xmit,
1638d46eeeafSJason Wang 					 struct virtnet_rq_stats *stats)
16399ab86bbcSShirley Ma {
1640012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1641981f14d4SHeng Qi 	int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
16428fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
16438fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
1644f600b690SJohn Fastabend 	struct sk_buff *head_skb, *curr_skb;
16459ce6146eSJesper Dangaard Brouer 	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
16464941d472SJason Wang 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1647ef75cb51SHeng Qi 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1648ef75cb51SHeng Qi 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1649ab7db917SMichael Dalton 
165056434a01SJohn Fastabend 	head_skb = NULL;
165127debe3eSEric Dumazet 	u64_stats_add(&stats->bytes, len - vi->hdr_len);
165256434a01SJohn Fastabend 
1653ef75cb51SHeng Qi 	if (unlikely(len > truesize - room)) {
1654ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1655ef75cb51SHeng Qi 			 dev->name, len, (unsigned long)(truesize - room));
1656e2e5c2a3SEric Dumazet 		DEV_STATS_INC(dev, rx_length_errors);
1657ad993a95SXie Yongji 		goto err_skb;
1658ad993a95SXie Yongji 	}
16596213f07cSLi RongQing 
166059ba3b1aSXuan Zhuo 	if (unlikely(vi->xdp_enabled)) {
166159ba3b1aSXuan Zhuo 		struct bpf_prog *xdp_prog;
16626213f07cSLi RongQing 
1663f600b690SJohn Fastabend 		rcu_read_lock();
1664f600b690SJohn Fastabend 		xdp_prog = rcu_dereference(rq->xdp_prog);
1665f600b690SJohn Fastabend 		if (xdp_prog) {
1666d8f2835aSXuan Zhuo 			head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
1667d8f2835aSXuan Zhuo 							 len, xdp_xmit, stats);
1668fab89bafSHeng Qi 			rcu_read_unlock();
16691830f893SJason Wang 			return head_skb;
167056434a01SJohn Fastabend 		}
1671f600b690SJohn Fastabend 		rcu_read_unlock();
167259ba3b1aSXuan Zhuo 	}
1673f600b690SJohn Fastabend 
1674fa0f1ba7SXuan Zhuo 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
1675f600b690SJohn Fastabend 	curr_skb = head_skb;
16769ab86bbcSShirley Ma 
16778fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
16788fc3b9e9SMichael S. Tsirkin 		goto err_skb;
16799ab86bbcSShirley Ma 	while (--num_buf) {
16808fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
16818fc3b9e9SMichael S. Tsirkin 
1682295525e2SXuan Zhuo 		buf = virtnet_rq_get_buf(rq, &len, &ctx);
168303e9f8a0SYunjian Wang 		if (unlikely(!buf)) {
16848fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1685fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
1686012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
1687012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
1688e2e5c2a3SEric Dumazet 			DEV_STATS_INC(dev, rx_length_errors);
16898fc3b9e9SMichael S. Tsirkin 			goto err_buf;
16903f2c31d9SMark McLoughlin 		}
16918fc3b9e9SMichael S. Tsirkin 
169227debe3eSEric Dumazet 		u64_stats_add(&stats->bytes, len);
16938fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
169428b39bc7SJason Wang 
169528b39bc7SJason Wang 		truesize = mergeable_ctx_to_truesize(ctx);
1696ef75cb51SHeng Qi 		headroom = mergeable_ctx_to_headroom(ctx);
1697ef75cb51SHeng Qi 		tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1698ef75cb51SHeng Qi 		room = SKB_DATA_ALIGN(headroom + tailroom);
1699ef75cb51SHeng Qi 		if (unlikely(len > truesize - room)) {
170056da5fd0SDan Carpenter 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1701ef75cb51SHeng Qi 				 dev->name, len, (unsigned long)(truesize - room));
1702e2e5c2a3SEric Dumazet 			DEV_STATS_INC(dev, rx_length_errors);
1703680557cfSMichael S. Tsirkin 			goto err_skb;
1704680557cfSMichael S. Tsirkin 		}
17058fc3b9e9SMichael S. Tsirkin 
17068fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
17072613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
17082613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
17098fc3b9e9SMichael S. Tsirkin 
17108fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
17118fc3b9e9SMichael S. Tsirkin 				goto err_skb;
17122613af0eSMichael Dalton 			if (curr_skb == head_skb)
17132613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
17142613af0eSMichael Dalton 			else
17152613af0eSMichael Dalton 				curr_skb->next = nskb;
17162613af0eSMichael Dalton 			curr_skb = nskb;
17172613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
17182613af0eSMichael Dalton 			num_skb_frags = 0;
17192613af0eSMichael Dalton 		}
17202613af0eSMichael Dalton 		if (curr_skb != head_skb) {
17212613af0eSMichael Dalton 			head_skb->data_len += len;
17222613af0eSMichael Dalton 			head_skb->len += len;
1723fb51879dSMichael Dalton 			head_skb->truesize += truesize;
17242613af0eSMichael Dalton 		}
17258fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
1726ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1727ba275241SJason Wang 			put_page(page);
1728ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1729fb51879dSMichael Dalton 					     len, truesize);
1730ba275241SJason Wang 		} else {
17312613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
1732fb51879dSMichael Dalton 					offset, len, truesize);
1733ba275241SJason Wang 		}
17348fc3b9e9SMichael S. Tsirkin 	}
17358fc3b9e9SMichael S. Tsirkin 
17365377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
17378fc3b9e9SMichael S. Tsirkin 	return head_skb;
17388fc3b9e9SMichael S. Tsirkin 
17398fc3b9e9SMichael S. Tsirkin err_skb:
17408fc3b9e9SMichael S. Tsirkin 	put_page(page);
174180f50f91SXuan Zhuo 	mergeable_buf_free(rq, num_buf, dev, stats);
174280f50f91SXuan Zhuo 
17438fc3b9e9SMichael S. Tsirkin err_buf:
174427debe3eSEric Dumazet 	u64_stats_inc(&stats->drops);
17458fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
17468fc3b9e9SMichael S. Tsirkin 	return NULL;
17479ab86bbcSShirley Ma }
17489ab86bbcSShirley Ma 
virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash * hdr_hash,struct sk_buff * skb)174991f41f01SAndrew Melnychenko static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
175091f41f01SAndrew Melnychenko 				struct sk_buff *skb)
175191f41f01SAndrew Melnychenko {
175291f41f01SAndrew Melnychenko 	enum pkt_hash_types rss_hash_type;
175391f41f01SAndrew Melnychenko 
175491f41f01SAndrew Melnychenko 	if (!hdr_hash || !skb)
175591f41f01SAndrew Melnychenko 		return;
175691f41f01SAndrew Melnychenko 
175795bb6330SMichael S. Tsirkin 	switch (__le16_to_cpu(hdr_hash->hash_report)) {
175891f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv4:
175991f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv4:
176091f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv6:
176191f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv6:
176291f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
176391f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
176491f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_L4;
176591f41f01SAndrew Melnychenko 		break;
176691f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv4:
176791f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv6:
176891f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv6_EX:
176991f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_L3;
177091f41f01SAndrew Melnychenko 		break;
177191f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_NONE:
177291f41f01SAndrew Melnychenko 	default:
177391f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_NONE;
177491f41f01SAndrew Melnychenko 	}
177595bb6330SMichael S. Tsirkin 	skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
177691f41f01SAndrew Melnychenko }
177791f41f01SAndrew Melnychenko 
receive_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,void ** ctx,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)17787d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
17792471c75eSJesper Dangaard Brouer 			void *buf, unsigned int len, void **ctx,
1780a0929a44SToshiaki Makita 			unsigned int *xdp_xmit,
1781d46eeeafSJason Wang 			struct virtnet_rq_stats *stats)
17829ab86bbcSShirley Ma {
1783e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
17849ab86bbcSShirley Ma 	struct sk_buff *skb;
1785dae64749SFeng Liu 	struct virtio_net_common_hdr *hdr;
17863ef2a16aSHeng Qi 	u8 flags;
17879ab86bbcSShirley Ma 
1788bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
17899ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
1790e2e5c2a3SEric Dumazet 		DEV_STATS_INC(dev, rx_length_errors);
17913ffd05c2SXuan Zhuo 		virtnet_rq_free_buf(vi, rq, buf);
17927d9d60fdSToshiaki Makita 		return;
17939ab86bbcSShirley Ma 	}
17949ab86bbcSShirley Ma 
17953ef2a16aSHeng Qi 	/* 1. Save the flags early, as the XDP program might overwrite them.
17963ef2a16aSHeng Qi 	 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
17973ef2a16aSHeng Qi 	 * stay valid after XDP processing.
17983ef2a16aSHeng Qi 	 * 2. XDP doesn't work with partially checksummed packets (refer to
17993ef2a16aSHeng Qi 	 * virtnet_xdp_set()), so packets marked as
18003ef2a16aSHeng Qi 	 * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
18013ef2a16aSHeng Qi 	 */
18023ef2a16aSHeng Qi 	flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
18033ef2a16aSHeng Qi 
1804f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
18057d9d60fdSToshiaki Makita 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1806a0929a44SToshiaki Makita 					stats);
1807f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
1808a0929a44SToshiaki Makita 		skb = receive_big(dev, vi, rq, buf, len, stats);
1809f121159dSMichael S. Tsirkin 	else
1810a0929a44SToshiaki Makita 		skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1811f121159dSMichael S. Tsirkin 
18128fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
18137d9d60fdSToshiaki Makita 		return;
18143f2c31d9SMark McLoughlin 
1815dae64749SFeng Liu 	hdr = skb_vnet_common_hdr(skb);
181691f41f01SAndrew Melnychenko 	if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1817dae64749SFeng Liu 		virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
18183fa2a1dfSstephen hemminger 
18193ef2a16aSHeng Qi 	if (flags & VIRTIO_NET_HDR_F_DATA_VALID)
182010a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1821296f96fcSRusty Russell 
1822e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1823e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
1824e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1825e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
1826fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
1827296f96fcSRusty Russell 		goto frame_err;
1828296f96fcSRusty Russell 	}
1829296f96fcSRusty Russell 
1830133bbb18SWillem de Bruijn 	skb_record_rx_queue(skb, vq2rxq(rq->vq));
1831d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
1832d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1833d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
1834d1dc06dcSMike Rapoport 
18350fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
18367d9d60fdSToshiaki Makita 	return;
1837296f96fcSRusty Russell 
1838296f96fcSRusty Russell frame_err:
1839e2e5c2a3SEric Dumazet 	DEV_STATS_INC(dev, rx_frame_errors);
1840296f96fcSRusty Russell 	dev_kfree_skb(skb);
1841296f96fcSRusty Russell }
1842296f96fcSRusty Russell 
1843192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the
1844192f68cfSJason Wang  * same size, except for the headroom. For this reason we do
1845192f68cfSJason Wang  * not need to use  mergeable_len_to_ctx here - it is enough
1846192f68cfSJason Wang  * to store the headroom as the context ignoring the truesize.
1847192f68cfSJason Wang  */
add_recvbuf_small(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1848946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1849946fa564SMichael S. Tsirkin 			     gfp_t gfp)
1850296f96fcSRusty Russell {
1851f6b10209SJason Wang 	char *buf;
18522de2f7f4SJohn Fastabend 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
1853192f68cfSJason Wang 	void *ctx = (void *)(unsigned long)xdp_headroom;
1854f6b10209SJason Wang 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
18559ab86bbcSShirley Ma 	int err;
18563f2c31d9SMark McLoughlin 
1857f6b10209SJason Wang 	len = SKB_DATA_ALIGN(len) +
1858f6b10209SJason Wang 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1859295525e2SXuan Zhuo 
1860*a8f7d696SXuan Zhuo 	if (unlikely(!skb_page_frag_refill(len, &rq->alloc_frag, gfp)))
1861*a8f7d696SXuan Zhuo 		return -ENOMEM;
1862*a8f7d696SXuan Zhuo 
1863295525e2SXuan Zhuo 	buf = virtnet_rq_alloc(rq, len, gfp);
1864295525e2SXuan Zhuo 	if (unlikely(!buf))
18659ab86bbcSShirley Ma 		return -ENOMEM;
1866296f96fcSRusty Russell 
1867e9e3424dSWenbo Li 	buf += VIRTNET_RX_PAD + xdp_headroom;
1868e9e3424dSWenbo Li 
1869e9e3424dSWenbo Li 	virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
1870295525e2SXuan Zhuo 
1871192f68cfSJason Wang 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1872295525e2SXuan Zhuo 	if (err < 0) {
1873295525e2SXuan Zhuo 		if (rq->do_dma)
1874295525e2SXuan Zhuo 			virtnet_rq_unmap(rq, buf, 0);
1875f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
1876295525e2SXuan Zhuo 	}
1877295525e2SXuan Zhuo 
18789ab86bbcSShirley Ma 	return err;
187997402b96SHerbert Xu }
188097402b96SHerbert Xu 
add_recvbuf_big(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1881012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1882012873d0SMichael S. Tsirkin 			   gfp_t gfp)
18839ab86bbcSShirley Ma {
18849ab86bbcSShirley Ma 	struct page *first, *list = NULL;
18859ab86bbcSShirley Ma 	char *p;
18869ab86bbcSShirley Ma 	int i, err, offset;
1887296f96fcSRusty Russell 
18884959aebbSGavin Li 	sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1889a5835440SRusty Russell 
18904959aebbSGavin Li 	/* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
18914959aebbSGavin Li 	for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1892e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
18939ab86bbcSShirley Ma 		if (!first) {
18949ab86bbcSShirley Ma 			if (list)
1895e9d7417bSJason Wang 				give_pages(rq, list);
18969ab86bbcSShirley Ma 			return -ENOMEM;
1897296f96fcSRusty Russell 		}
1898e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
18999ab86bbcSShirley Ma 
19009ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
19019ab86bbcSShirley Ma 		first->private = (unsigned long)list;
19029ab86bbcSShirley Ma 		list = first;
19039ab86bbcSShirley Ma 	}
19049ab86bbcSShirley Ma 
1905e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
19069ab86bbcSShirley Ma 	if (!first) {
1907e9d7417bSJason Wang 		give_pages(rq, list);
19089ab86bbcSShirley Ma 		return -ENOMEM;
19099ab86bbcSShirley Ma 	}
19109ab86bbcSShirley Ma 	p = page_address(first);
19119ab86bbcSShirley Ma 
1912e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
1913012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
1914012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
19159ab86bbcSShirley Ma 
1916e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
19179ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
1918e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
19199ab86bbcSShirley Ma 
19209ab86bbcSShirley Ma 	/* chain first in list head */
19219ab86bbcSShirley Ma 	first->private = (unsigned long)list;
19224959aebbSGavin Li 	err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1923aa989f5eSMichael S. Tsirkin 				  first, gfp);
19249ab86bbcSShirley Ma 	if (err < 0)
1925e9d7417bSJason Wang 		give_pages(rq, first);
19269ab86bbcSShirley Ma 
19279ab86bbcSShirley Ma 	return err;
19289ab86bbcSShirley Ma }
19299ab86bbcSShirley Ma 
get_mergeable_buf_len(struct receive_queue * rq,struct ewma_pkt_len * avg_pkt_len,unsigned int room)1930d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
19313cc81a9aSJason Wang 					  struct ewma_pkt_len *avg_pkt_len,
19323cc81a9aSJason Wang 					  unsigned int room)
19339ab86bbcSShirley Ma {
1934c1ddc42dSAndrew Melnychenko 	struct virtnet_info *vi = rq->vq->vdev->priv;
1935c1ddc42dSAndrew Melnychenko 	const size_t hdr_len = vi->hdr_len;
1936fbf28d78SMichael Dalton 	unsigned int len;
1937fbf28d78SMichael Dalton 
19383cc81a9aSJason Wang 	if (room)
19393cc81a9aSJason Wang 		return PAGE_SIZE - room;
19403cc81a9aSJason Wang 
19415377d758SJohannes Berg 	len = hdr_len +	clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1942f0c3192cSMichael S. Tsirkin 				rq->min_buf_len, PAGE_SIZE - hdr_len);
19433cc81a9aSJason Wang 
1944e377fcc8SMichael S. Tsirkin 	return ALIGN(len, L1_CACHE_BYTES);
1945fbf28d78SMichael Dalton }
1946fbf28d78SMichael Dalton 
add_recvbuf_mergeable(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)19472de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi,
19482de2f7f4SJohn Fastabend 				 struct receive_queue *rq, gfp_t gfp)
1949fbf28d78SMichael Dalton {
1950fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
19512de2f7f4SJohn Fastabend 	unsigned int headroom = virtnet_get_headroom(vi);
19523cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
19533cc81a9aSJason Wang 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1954fb51879dSMichael Dalton 	unsigned int len, hole;
1955295525e2SXuan Zhuo 	void *ctx;
1956295525e2SXuan Zhuo 	char *buf;
1957295525e2SXuan Zhuo 	int err;
19589ab86bbcSShirley Ma 
19593cc81a9aSJason Wang 	/* Extra tailroom is needed to satisfy XDP's assumption. This
19603cc81a9aSJason Wang 	 * means rx frags coalescing won't work, but consider we've
19613cc81a9aSJason Wang 	 * disabled GSO for XDP, it won't be a big issue.
19623cc81a9aSJason Wang 	 */
19633cc81a9aSJason Wang 	len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1964295525e2SXuan Zhuo 
1965*a8f7d696SXuan Zhuo 	if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1966*a8f7d696SXuan Zhuo 		return -ENOMEM;
1967*a8f7d696SXuan Zhuo 
1968*a8f7d696SXuan Zhuo 	if (!alloc_frag->offset && len + room + sizeof(struct virtnet_rq_dma) > alloc_frag->size)
1969*a8f7d696SXuan Zhuo 		len -= sizeof(struct virtnet_rq_dma);
1970*a8f7d696SXuan Zhuo 
1971295525e2SXuan Zhuo 	buf = virtnet_rq_alloc(rq, len + room, gfp);
1972295525e2SXuan Zhuo 	if (unlikely(!buf))
19739ab86bbcSShirley Ma 		return -ENOMEM;
1974ab7db917SMichael Dalton 
19752de2f7f4SJohn Fastabend 	buf += headroom; /* advance address leaving hole at front of pkt */
1976fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
19773cc81a9aSJason Wang 	if (hole < len + room) {
1978ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
1979ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
19801daa8790SMichael S. Tsirkin 		 * the current buffer.
1981484beac2SHeng Qi 		 * XDP core assumes that frame_size of xdp_buff and the length
1982484beac2SHeng Qi 		 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
1983ab7db917SMichael Dalton 		 */
1984484beac2SHeng Qi 		if (!headroom)
1985fb51879dSMichael Dalton 			len += hole;
1986fb51879dSMichael Dalton 		alloc_frag->offset += hole;
1987fb51879dSMichael Dalton 	}
19889ab86bbcSShirley Ma 
1989295525e2SXuan Zhuo 	virtnet_rq_init_one_sg(rq, buf, len);
1990295525e2SXuan Zhuo 
1991ef75cb51SHeng Qi 	ctx = mergeable_len_to_ctx(len + room, headroom);
1992680557cfSMichael S. Tsirkin 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1993295525e2SXuan Zhuo 	if (err < 0) {
1994295525e2SXuan Zhuo 		if (rq->do_dma)
1995295525e2SXuan Zhuo 			virtnet_rq_unmap(rq, buf, 0);
19962613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
1997295525e2SXuan Zhuo 	}
19989ab86bbcSShirley Ma 
19999ab86bbcSShirley Ma 	return err;
2000296f96fcSRusty Russell }
2001296f96fcSRusty Russell 
2002b2baed69SRusty Russell /*
2003b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
2004b2baed69SRusty Russell  *
2005b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
2006b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
2007b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
2008b2baed69SRusty Russell  */
try_fill_recv(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2009946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2010946fa564SMichael S. Tsirkin 			  gfp_t gfp)
20113f2c31d9SMark McLoughlin {
20123f2c31d9SMark McLoughlin 	int err;
20131788f495SMichael S. Tsirkin 	bool oom;
20143f2c31d9SMark McLoughlin 
20150aea51c3SAmit Shah 	do {
20169ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
20172de2f7f4SJohn Fastabend 			err = add_recvbuf_mergeable(vi, rq, gfp);
20189ab86bbcSShirley Ma 		else if (vi->big_packets)
2019012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
20209ab86bbcSShirley Ma 		else
2021946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
20223f2c31d9SMark McLoughlin 
20231788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
20249ed4cb07SRusty Russell 		if (err)
20253f2c31d9SMark McLoughlin 			break;
2026b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
2027461f03dcSToshiaki Makita 	if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
202801c32598SMichael S. Tsirkin 		unsigned long flags;
202901c32598SMichael S. Tsirkin 
203001c32598SMichael S. Tsirkin 		flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
203127debe3eSEric Dumazet 		u64_stats_inc(&rq->stats.kicks);
203201c32598SMichael S. Tsirkin 		u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2033461f03dcSToshiaki Makita 	}
2034461f03dcSToshiaki Makita 
20353161e453SRusty Russell 	return !oom;
20363f2c31d9SMark McLoughlin }
20373f2c31d9SMark McLoughlin 
skb_recv_done(struct virtqueue * rvq)203818445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
2039296f96fcSRusty Russell {
2040296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
2041986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2042e9d7417bSJason Wang 
2043e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(&rq->napi, rvq);
2044296f96fcSRusty Russell }
2045296f96fcSRusty Russell 
virtnet_napi_enable(struct virtqueue * vq,struct napi_struct * napi)2046e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
20473e9d08ecSBruce Rogers {
2048e4e8452aSWillem de Bruijn 	napi_enable(napi);
20493e9d08ecSBruce Rogers 
20503e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
2051e4e8452aSWillem de Bruijn 	 * won't get another interrupt, so process any outstanding packets now.
2052e4e8452aSWillem de Bruijn 	 * Call local_bh_enable after to trigger softIRQ processing.
2053e4e8452aSWillem de Bruijn 	 */
2054ec13ee80SMichael S. Tsirkin 	local_bh_disable();
2055e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(napi, vq);
2056ec13ee80SMichael S. Tsirkin 	local_bh_enable();
20573e9d08ecSBruce Rogers }
20583e9d08ecSBruce Rogers 
virtnet_napi_tx_enable(struct virtnet_info * vi,struct virtqueue * vq,struct napi_struct * napi)2059b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi,
2060b92f1e67SWillem de Bruijn 				   struct virtqueue *vq,
2061b92f1e67SWillem de Bruijn 				   struct napi_struct *napi)
2062b92f1e67SWillem de Bruijn {
2063b92f1e67SWillem de Bruijn 	if (!napi->weight)
2064b92f1e67SWillem de Bruijn 		return;
2065b92f1e67SWillem de Bruijn 
2066b92f1e67SWillem de Bruijn 	/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2067b92f1e67SWillem de Bruijn 	 * enable the feature if this is likely affine with the transmit path.
2068b92f1e67SWillem de Bruijn 	 */
2069b92f1e67SWillem de Bruijn 	if (!vi->affinity_hint_set) {
2070b92f1e67SWillem de Bruijn 		napi->weight = 0;
2071b92f1e67SWillem de Bruijn 		return;
2072b92f1e67SWillem de Bruijn 	}
2073b92f1e67SWillem de Bruijn 
2074b92f1e67SWillem de Bruijn 	return virtnet_napi_enable(vq, napi);
2075b92f1e67SWillem de Bruijn }
2076b92f1e67SWillem de Bruijn 
virtnet_napi_tx_disable(struct napi_struct * napi)207778a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi)
207878a57b48SWillem de Bruijn {
207978a57b48SWillem de Bruijn 	if (napi->weight)
208078a57b48SWillem de Bruijn 		napi_disable(napi);
208178a57b48SWillem de Bruijn }
208278a57b48SWillem de Bruijn 
refill_work(struct work_struct * work)20833161e453SRusty Russell static void refill_work(struct work_struct *work)
20843161e453SRusty Russell {
2085e9d7417bSJason Wang 	struct virtnet_info *vi =
2086e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
20873161e453SRusty Russell 	bool still_empty;
2088986a4f4dSJason Wang 	int i;
20893161e453SRusty Russell 
209055257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2091986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
2092986a4f4dSJason Wang 
2093986a4f4dSJason Wang 		napi_disable(&rq->napi);
2094946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2095e4e8452aSWillem de Bruijn 		virtnet_napi_enable(rq->vq, &rq->napi);
20963161e453SRusty Russell 
20973161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
2098986a4f4dSJason Wang 		 * we will *never* try to fill again.
2099986a4f4dSJason Wang 		 */
21003161e453SRusty Russell 		if (still_empty)
21013b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
21023161e453SRusty Russell 	}
2103986a4f4dSJason Wang }
21043161e453SRusty Russell 
virtnet_receive(struct receive_queue * rq,int budget,unsigned int * xdp_xmit)21052471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget,
21062471c75eSJesper Dangaard Brouer 			   unsigned int *xdp_xmit)
2107296f96fcSRusty Russell {
2108e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
2109d46eeeafSJason Wang 	struct virtnet_rq_stats stats = {};
2110a0929a44SToshiaki Makita 	unsigned int len;
211127debe3eSEric Dumazet 	int packets = 0;
21129ab86bbcSShirley Ma 	void *buf;
2113a0929a44SToshiaki Makita 	int i;
2114296f96fcSRusty Russell 
2115192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
2116680557cfSMichael S. Tsirkin 		void *ctx;
2117680557cfSMichael S. Tsirkin 
211827debe3eSEric Dumazet 		while (packets < budget &&
2119295525e2SXuan Zhuo 		       (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2120a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
212127debe3eSEric Dumazet 			packets++;
2122680557cfSMichael S. Tsirkin 		}
2123680557cfSMichael S. Tsirkin 	} else {
212427debe3eSEric Dumazet 		while (packets < budget &&
2125295525e2SXuan Zhuo 		       (buf = virtnet_rq_get_buf(rq, &len, NULL)) != NULL) {
2126a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
212727debe3eSEric Dumazet 			packets++;
2128296f96fcSRusty Russell 		}
2129680557cfSMichael S. Tsirkin 	}
2130296f96fcSRusty Russell 
2131718be6baS? jiang 	if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
21325a159128SJason Wang 		if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
21335a159128SJason Wang 			spin_lock(&vi->refill_lock);
21345a159128SJason Wang 			if (vi->refill_enabled)
21353b07e9caSTejun Heo 				schedule_delayed_work(&vi->refill, 0);
21365a159128SJason Wang 			spin_unlock(&vi->refill_lock);
21375a159128SJason Wang 		}
21383161e453SRusty Russell 	}
2139296f96fcSRusty Russell 
214027debe3eSEric Dumazet 	u64_stats_set(&stats.packets, packets);
2141d7dfc5cfSToshiaki Makita 	u64_stats_update_begin(&rq->stats.syncp);
2142a0929a44SToshiaki Makita 	for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
2143a0929a44SToshiaki Makita 		size_t offset = virtnet_rq_stats_desc[i].offset;
214427debe3eSEric Dumazet 		u64_stats_t *item, *src;
2145a0929a44SToshiaki Makita 
214627debe3eSEric Dumazet 		item = (u64_stats_t *)((u8 *)&rq->stats + offset);
214727debe3eSEric Dumazet 		src = (u64_stats_t *)((u8 *)&stats + offset);
214827debe3eSEric Dumazet 		u64_stats_add(item, u64_stats_read(src));
2149a0929a44SToshiaki Makita 	}
2150d7dfc5cfSToshiaki Makita 	u64_stats_update_end(&rq->stats.syncp);
215161845d20SJason Wang 
215227debe3eSEric Dumazet 	return packets;
21532ffa7598SJason Wang }
21542ffa7598SJason Wang 
virtnet_poll_cleantx(struct receive_queue * rq,int budget)2155f5e9a22dSBreno Leitao static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
21567b0411efSWillem de Bruijn {
21577b0411efSWillem de Bruijn 	struct virtnet_info *vi = rq->vq->vdev->priv;
21587b0411efSWillem de Bruijn 	unsigned int index = vq2rxq(rq->vq);
21597b0411efSWillem de Bruijn 	struct send_queue *sq = &vi->sq[index];
21607b0411efSWillem de Bruijn 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
21617b0411efSWillem de Bruijn 
2162534da5e8SToshiaki Makita 	if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
21637b0411efSWillem de Bruijn 		return;
21647b0411efSWillem de Bruijn 
21657b0411efSWillem de Bruijn 	if (__netif_tx_trylock(txq)) {
2166ebcce492SXuan Zhuo 		if (sq->reset) {
2167ebcce492SXuan Zhuo 			__netif_tx_unlock(txq);
2168ebcce492SXuan Zhuo 			return;
2169ebcce492SXuan Zhuo 		}
2170ebcce492SXuan Zhuo 
2171a7766ef1SMichael S. Tsirkin 		do {
2172a7766ef1SMichael S. Tsirkin 			virtqueue_disable_cb(sq->vq);
2173f5e9a22dSBreno Leitao 			free_old_xmit_skbs(sq, !!budget);
2174a7766ef1SMichael S. Tsirkin 		} while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
21757b0411efSWillem de Bruijn 
21767b0411efSWillem de Bruijn 		if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
21777b0411efSWillem de Bruijn 			netif_tx_wake_queue(txq);
217822bc63c5SMichael S. Tsirkin 
217922bc63c5SMichael S. Tsirkin 		__netif_tx_unlock(txq);
218022bc63c5SMichael S. Tsirkin 	}
21817b0411efSWillem de Bruijn }
21827b0411efSWillem de Bruijn 
virtnet_poll(struct napi_struct * napi,int budget)21832ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
21842ffa7598SJason Wang {
21852ffa7598SJason Wang 	struct receive_queue *rq =
21862ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
21879267c430SJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
21889267c430SJason Wang 	struct send_queue *sq;
21892a43565cSToshiaki Makita 	unsigned int received;
21902471c75eSJesper Dangaard Brouer 	unsigned int xdp_xmit = 0;
21912ffa7598SJason Wang 
2192f5e9a22dSBreno Leitao 	virtnet_poll_cleantx(rq, budget);
21937b0411efSWillem de Bruijn 
2194186b3c99SJason Wang 	received = virtnet_receive(rq, budget, &xdp_xmit);
21952ffa7598SJason Wang 
2196ad7e615fSMagnus Karlsson 	if (xdp_xmit & VIRTIO_XDP_REDIR)
2197ad7e615fSMagnus Karlsson 		xdp_do_flush();
2198ad7e615fSMagnus Karlsson 
21998329d98eSRusty Russell 	/* Out of packets? */
2200e4e8452aSWillem de Bruijn 	if (received < budget)
2201e4e8452aSWillem de Bruijn 		virtqueue_napi_complete(napi, rq->vq, received);
2202296f96fcSRusty Russell 
22032471c75eSJesper Dangaard Brouer 	if (xdp_xmit & VIRTIO_XDP_TX) {
220497c2c69eSXuan Zhuo 		sq = virtnet_xdp_get_sq(vi);
2205461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2206461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
220727debe3eSEric Dumazet 			u64_stats_inc(&sq->stats.kicks);
2208461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
2209461f03dcSToshiaki Makita 		}
221097c2c69eSXuan Zhuo 		virtnet_xdp_put_sq(vi, sq);
22119267c430SJason Wang 	}
2212186b3c99SJason Wang 
2213296f96fcSRusty Russell 	return received;
2214296f96fcSRusty Russell }
2215296f96fcSRusty Russell 
virtnet_disable_queue_pair(struct virtnet_info * vi,int qp_index)22165306623aSFeng Liu static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
22175306623aSFeng Liu {
22185306623aSFeng Liu 	virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
22195306623aSFeng Liu 	napi_disable(&vi->rq[qp_index].napi);
22205306623aSFeng Liu 	xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
22215306623aSFeng Liu }
22225306623aSFeng Liu 
virtnet_enable_queue_pair(struct virtnet_info * vi,int qp_index)22235306623aSFeng Liu static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
22245306623aSFeng Liu {
22255306623aSFeng Liu 	struct net_device *dev = vi->dev;
22265306623aSFeng Liu 	int err;
22275306623aSFeng Liu 
22285306623aSFeng Liu 	err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
22295306623aSFeng Liu 			       vi->rq[qp_index].napi.napi_id);
22305306623aSFeng Liu 	if (err < 0)
22315306623aSFeng Liu 		return err;
22325306623aSFeng Liu 
22335306623aSFeng Liu 	err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
22345306623aSFeng Liu 					 MEM_TYPE_PAGE_SHARED, NULL);
22355306623aSFeng Liu 	if (err < 0)
22365306623aSFeng Liu 		goto err_xdp_reg_mem_model;
22375306623aSFeng Liu 
22385306623aSFeng Liu 	virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
22395306623aSFeng Liu 	virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
22405306623aSFeng Liu 
22415306623aSFeng Liu 	return 0;
22425306623aSFeng Liu 
22435306623aSFeng Liu err_xdp_reg_mem_model:
22445306623aSFeng Liu 	xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
22455306623aSFeng Liu 	return err;
22465306623aSFeng Liu }
22475306623aSFeng Liu 
virtnet_open(struct net_device * dev)2248986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
2249986a4f4dSJason Wang {
2250986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2251754b8a21SJesper Dangaard Brouer 	int i, err;
2252986a4f4dSJason Wang 
22535a159128SJason Wang 	enable_delayed_refill(vi);
22545a159128SJason Wang 
2255e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
2256e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
2257986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
2258946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2259986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
2260754b8a21SJesper Dangaard Brouer 
22615306623aSFeng Liu 		err = virtnet_enable_queue_pair(vi, i);
2262754b8a21SJesper Dangaard Brouer 		if (err < 0)
22635306623aSFeng Liu 			goto err_enable_qp;
2264986a4f4dSJason Wang 	}
2265986a4f4dSJason Wang 
2266986a4f4dSJason Wang 	return 0;
22675306623aSFeng Liu 
22685306623aSFeng Liu err_enable_qp:
22695306623aSFeng Liu 	disable_delayed_refill(vi);
22705306623aSFeng Liu 	cancel_delayed_work_sync(&vi->refill);
22715306623aSFeng Liu 
22725306623aSFeng Liu 	for (i--; i >= 0; i--)
22735306623aSFeng Liu 		virtnet_disable_queue_pair(vi, i);
22745306623aSFeng Liu 	return err;
2275986a4f4dSJason Wang }
2276986a4f4dSJason Wang 
virtnet_poll_tx(struct napi_struct * napi,int budget)2277b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2278b92f1e67SWillem de Bruijn {
2279b92f1e67SWillem de Bruijn 	struct send_queue *sq = container_of(napi, struct send_queue, napi);
2280b92f1e67SWillem de Bruijn 	struct virtnet_info *vi = sq->vq->vdev->priv;
2281534da5e8SToshiaki Makita 	unsigned int index = vq2txq(sq->vq);
2282534da5e8SToshiaki Makita 	struct netdev_queue *txq;
22835a2f966dSMichael S. Tsirkin 	int opaque;
22845a2f966dSMichael S. Tsirkin 	bool done;
2285b92f1e67SWillem de Bruijn 
2286534da5e8SToshiaki Makita 	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
2287534da5e8SToshiaki Makita 		/* We don't need to enable cb for XDP */
2288534da5e8SToshiaki Makita 		napi_complete_done(napi, 0);
2289534da5e8SToshiaki Makita 		return 0;
2290534da5e8SToshiaki Makita 	}
2291534da5e8SToshiaki Makita 
2292534da5e8SToshiaki Makita 	txq = netdev_get_tx_queue(vi->dev, index);
2293b92f1e67SWillem de Bruijn 	__netif_tx_lock(txq, raw_smp_processor_id());
22945a2f966dSMichael S. Tsirkin 	virtqueue_disable_cb(sq->vq);
2295f5e9a22dSBreno Leitao 	free_old_xmit_skbs(sq, !!budget);
22965a2f966dSMichael S. Tsirkin 
229722bc63c5SMichael S. Tsirkin 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
229822bc63c5SMichael S. Tsirkin 		netif_tx_wake_queue(txq);
229922bc63c5SMichael S. Tsirkin 
23005a2f966dSMichael S. Tsirkin 	opaque = virtqueue_enable_cb_prepare(sq->vq);
23015a2f966dSMichael S. Tsirkin 
23025a2f966dSMichael S. Tsirkin 	done = napi_complete_done(napi, 0);
23035a2f966dSMichael S. Tsirkin 
23045a2f966dSMichael S. Tsirkin 	if (!done)
23055a2f966dSMichael S. Tsirkin 		virtqueue_disable_cb(sq->vq);
23065a2f966dSMichael S. Tsirkin 
2307b92f1e67SWillem de Bruijn 	__netif_tx_unlock(txq);
2308b92f1e67SWillem de Bruijn 
23095a2f966dSMichael S. Tsirkin 	if (done) {
23105a2f966dSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(sq->vq, opaque))) {
23115a2f966dSMichael S. Tsirkin 			if (napi_schedule_prep(napi)) {
23125a2f966dSMichael S. Tsirkin 				__netif_tx_lock(txq, raw_smp_processor_id());
23135a2f966dSMichael S. Tsirkin 				virtqueue_disable_cb(sq->vq);
23145a2f966dSMichael S. Tsirkin 				__netif_tx_unlock(txq);
23155a2f966dSMichael S. Tsirkin 				__napi_schedule(napi);
23165a2f966dSMichael S. Tsirkin 			}
23175a2f966dSMichael S. Tsirkin 		}
23185a2f966dSMichael S. Tsirkin 	}
2319b92f1e67SWillem de Bruijn 
2320b92f1e67SWillem de Bruijn 	return 0;
2321b92f1e67SWillem de Bruijn }
2322b92f1e67SWillem de Bruijn 
xmit_skb(struct send_queue * sq,struct sk_buff * skb)2323e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
2324296f96fcSRusty Russell {
2325012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
2326296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
2327e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
2328e2fcad58SJason A. Donenfeld 	int num_sg;
2329012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
2330e7428e95SMichael S. Tsirkin 	bool can_push;
2331296f96fcSRusty Russell 
2332e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
2333e7428e95SMichael S. Tsirkin 
2334e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
2335e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
2336e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
2337e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
2338e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
2339e7428e95SMichael S. Tsirkin 	if (can_push)
2340012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
2341e7428e95SMichael S. Tsirkin 	else
2342dae64749SFeng Liu 		hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
2343296f96fcSRusty Russell 
2344e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2345fd3a8862SWillem de Bruijn 				    virtio_is_little_endian(vi->vdev), false,
2346fd3a8862SWillem de Bruijn 				    0))
234785eb1389SXianting Tian 		return -EPROTO;
2348296f96fcSRusty Russell 
2349e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
2350012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
23513f2c31d9SMark McLoughlin 
2352547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
2353e7428e95SMichael S. Tsirkin 	if (can_push) {
2354e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
2355e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
2356e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
2357e2fcad58SJason A. Donenfeld 			return num_sg;
2358e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
2359e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
2360e7428e95SMichael S. Tsirkin 	} else {
2361e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
2362e2fcad58SJason A. Donenfeld 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
2363e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
2364e2fcad58SJason A. Donenfeld 			return num_sg;
2365e2fcad58SJason A. Donenfeld 		num_sg++;
2366e7428e95SMichael S. Tsirkin 	}
23679dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
236811a3a154SRusty Russell }
236911a3a154SRusty Russell 
start_xmit(struct sk_buff * skb,struct net_device * dev)2370424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
237199ffc696SRusty Russell {
237299ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
2373986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
2374986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
23759ed4cb07SRusty Russell 	int err;
23764b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
23776b16f9eeSFlorian Westphal 	bool kick = !netdev_xmit_more();
2378b92f1e67SWillem de Bruijn 	bool use_napi = sq->napi.weight;
23792cb9c6baSRusty Russell 
23802cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
2381a7766ef1SMichael S. Tsirkin 	do {
2382a7766ef1SMichael S. Tsirkin 		if (use_napi)
2383a7766ef1SMichael S. Tsirkin 			virtqueue_disable_cb(sq->vq);
2384a7766ef1SMichael S. Tsirkin 
2385df133f3fSMichael S. Tsirkin 		free_old_xmit_skbs(sq, false);
238699ffc696SRusty Russell 
2387a7766ef1SMichael S. Tsirkin 	} while (use_napi && kick &&
2388a7766ef1SMichael S. Tsirkin 	       unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2389bdb12e0dSWillem de Bruijn 
2390074c3582SJacob Keller 	/* timestamp packet in software */
2391074c3582SJacob Keller 	skb_tx_timestamp(skb);
2392074c3582SJacob Keller 
239303f191baSMichael S. Tsirkin 	/* Try to transmit */
2394b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
239599ffc696SRusty Russell 
23969ed4cb07SRusty Russell 	/* This should not happen! */
2397681daee2SJason Wang 	if (unlikely(err)) {
2398e2e5c2a3SEric Dumazet 		DEV_STATS_INC(dev, tx_fifo_errors);
23992e57b79cSRick Jones 		if (net_ratelimit())
240058eba97dSRusty Russell 			dev_warn(&dev->dev,
24017934b481SYuval Shaia 				 "Unexpected TXQ (%d) queue failure: %d\n",
24027934b481SYuval Shaia 				 qnum, err);
2403e2e5c2a3SEric Dumazet 		DEV_STATS_INC(dev, tx_dropped);
240485e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
240558eba97dSRusty Russell 		return NETDEV_TX_OK;
2406296f96fcSRusty Russell 	}
240703f191baSMichael S. Tsirkin 
240848925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
2409b92f1e67SWillem de Bruijn 	if (!use_napi) {
241048925e37SRusty Russell 		skb_orphan(skb);
2411895b5c9fSFlorian Westphal 		nf_reset_ct(skb);
2412b92f1e67SWillem de Bruijn 	}
241348925e37SRusty Russell 
2414b8ef4809SXuan Zhuo 	check_sq_full_and_disable(vi, dev, sq);
241548925e37SRusty Russell 
2416461f03dcSToshiaki Makita 	if (kick || netif_xmit_stopped(txq)) {
2417461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2418461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
241927debe3eSEric Dumazet 			u64_stats_inc(&sq->stats.kicks);
2420461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
2421461f03dcSToshiaki Makita 		}
2422461f03dcSToshiaki Makita 	}
24230b725a2cSDavid S. Miller 
24240b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
2425c223a078SDavid S. Miller }
2426c223a078SDavid S. Miller 
virtnet_rx_resize(struct virtnet_info * vi,struct receive_queue * rq,u32 ring_num)24276a4763e2SXuan Zhuo static int virtnet_rx_resize(struct virtnet_info *vi,
24286a4763e2SXuan Zhuo 			     struct receive_queue *rq, u32 ring_num)
24296a4763e2SXuan Zhuo {
24306a4763e2SXuan Zhuo 	bool running = netif_running(vi->dev);
24316a4763e2SXuan Zhuo 	int err, qindex;
24326a4763e2SXuan Zhuo 
24336a4763e2SXuan Zhuo 	qindex = rq - vi->rq;
24346a4763e2SXuan Zhuo 
24356a4763e2SXuan Zhuo 	if (running)
24366a4763e2SXuan Zhuo 		napi_disable(&rq->napi);
24376a4763e2SXuan Zhuo 
24383ffd05c2SXuan Zhuo 	err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf);
24396a4763e2SXuan Zhuo 	if (err)
24406a4763e2SXuan Zhuo 		netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
24416a4763e2SXuan Zhuo 
24426a4763e2SXuan Zhuo 	if (!try_fill_recv(vi, rq, GFP_KERNEL))
24436a4763e2SXuan Zhuo 		schedule_delayed_work(&vi->refill, 0);
24446a4763e2SXuan Zhuo 
24456a4763e2SXuan Zhuo 	if (running)
24466a4763e2SXuan Zhuo 		virtnet_napi_enable(rq->vq, &rq->napi);
24476a4763e2SXuan Zhuo 	return err;
24486a4763e2SXuan Zhuo }
24496a4763e2SXuan Zhuo 
virtnet_tx_resize(struct virtnet_info * vi,struct send_queue * sq,u32 ring_num)2450ebcce492SXuan Zhuo static int virtnet_tx_resize(struct virtnet_info *vi,
2451ebcce492SXuan Zhuo 			     struct send_queue *sq, u32 ring_num)
2452ebcce492SXuan Zhuo {
2453ebcce492SXuan Zhuo 	bool running = netif_running(vi->dev);
2454ebcce492SXuan Zhuo 	struct netdev_queue *txq;
2455ebcce492SXuan Zhuo 	int err, qindex;
2456ebcce492SXuan Zhuo 
2457ebcce492SXuan Zhuo 	qindex = sq - vi->sq;
2458ebcce492SXuan Zhuo 
2459ebcce492SXuan Zhuo 	if (running)
2460ebcce492SXuan Zhuo 		virtnet_napi_tx_disable(&sq->napi);
2461ebcce492SXuan Zhuo 
2462ebcce492SXuan Zhuo 	txq = netdev_get_tx_queue(vi->dev, qindex);
2463ebcce492SXuan Zhuo 
2464ebcce492SXuan Zhuo 	/* 1. wait all ximt complete
2465ebcce492SXuan Zhuo 	 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2466ebcce492SXuan Zhuo 	 */
2467ebcce492SXuan Zhuo 	__netif_tx_lock_bh(txq);
2468ebcce492SXuan Zhuo 
2469ebcce492SXuan Zhuo 	/* Prevent rx poll from accessing sq. */
2470ebcce492SXuan Zhuo 	sq->reset = true;
2471ebcce492SXuan Zhuo 
2472ebcce492SXuan Zhuo 	/* Prevent the upper layer from trying to send packets. */
2473ebcce492SXuan Zhuo 	netif_stop_subqueue(vi->dev, qindex);
2474ebcce492SXuan Zhuo 
2475ebcce492SXuan Zhuo 	__netif_tx_unlock_bh(txq);
2476ebcce492SXuan Zhuo 
2477ebcce492SXuan Zhuo 	err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2478ebcce492SXuan Zhuo 	if (err)
2479ebcce492SXuan Zhuo 		netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2480ebcce492SXuan Zhuo 
2481ebcce492SXuan Zhuo 	__netif_tx_lock_bh(txq);
2482ebcce492SXuan Zhuo 	sq->reset = false;
2483ebcce492SXuan Zhuo 	netif_tx_wake_queue(txq);
2484ebcce492SXuan Zhuo 	__netif_tx_unlock_bh(txq);
2485ebcce492SXuan Zhuo 
2486ebcce492SXuan Zhuo 	if (running)
2487ebcce492SXuan Zhuo 		virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2488ebcce492SXuan Zhuo 	return err;
2489ebcce492SXuan Zhuo }
2490ebcce492SXuan Zhuo 
249140cbfc37SAmos Kong /*
249240cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
249340cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
2494788a8b6dSstephen hemminger  * never fail unless improperly formatted.
249540cbfc37SAmos Kong  */
virtnet_send_command(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out)249640cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2497d24bae32Sstephen hemminger 				 struct scatterlist *out)
249840cbfc37SAmos Kong {
2499f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
2500d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
2501222722bcSYunjian Wang 	int ret;
250240cbfc37SAmos Kong 
250340cbfc37SAmos Kong 	/* Caller should know better */
2504f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
250540cbfc37SAmos Kong 
250612e57169SMichael S. Tsirkin 	vi->ctrl->status = ~0;
250712e57169SMichael S. Tsirkin 	vi->ctrl->hdr.class = class;
250812e57169SMichael S. Tsirkin 	vi->ctrl->hdr.cmd = cmd;
2509f7bc9594SRusty Russell 	/* Add header */
251012e57169SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2511f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
251240cbfc37SAmos Kong 
2513f7bc9594SRusty Russell 	if (out)
2514f7bc9594SRusty Russell 		sgs[out_num++] = out;
251540cbfc37SAmos Kong 
2516f7bc9594SRusty Russell 	/* Add return status. */
251712e57169SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2518d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
251940cbfc37SAmos Kong 
2520d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
2521222722bcSYunjian Wang 	ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
2522222722bcSYunjian Wang 	if (ret < 0) {
2523222722bcSYunjian Wang 		dev_warn(&vi->vdev->dev,
2524222722bcSYunjian Wang 			 "Failed to add sgs for command vq: %d\n.", ret);
2525222722bcSYunjian Wang 		return false;
2526222722bcSYunjian Wang 	}
252740cbfc37SAmos Kong 
252867975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
252912e57169SMichael S. Tsirkin 		return vi->ctrl->status == VIRTIO_NET_OK;
253040cbfc37SAmos Kong 
253140cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
253240cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
253340cbfc37SAmos Kong 	 */
2534047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2535047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
253640cbfc37SAmos Kong 		cpu_relax();
253740cbfc37SAmos Kong 
253812e57169SMichael S. Tsirkin 	return vi->ctrl->status == VIRTIO_NET_OK;
253940cbfc37SAmos Kong }
254040cbfc37SAmos Kong 
virtnet_set_mac_address(struct net_device * dev,void * p)25419c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
25429c46f6d4SAlex Williamson {
25439c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
25449c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
2545f2f2c8b4SJiri Pirko 	int ret;
2546e37e2ff3SAndy Lutomirski 	struct sockaddr *addr;
25477e58d5aeSAmos Kong 	struct scatterlist sg;
25489c46f6d4SAlex Williamson 
2549ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2550ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
2551ba5e4426SSridhar Samudrala 
2552801822d1SShyam Saini 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2553e37e2ff3SAndy Lutomirski 	if (!addr)
2554e37e2ff3SAndy Lutomirski 		return -ENOMEM;
2555e37e2ff3SAndy Lutomirski 
2556e37e2ff3SAndy Lutomirski 	ret = eth_prepare_mac_addr_change(dev, addr);
2557f2f2c8b4SJiri Pirko 	if (ret)
2558e37e2ff3SAndy Lutomirski 		goto out;
25599c46f6d4SAlex Williamson 
25607e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
25617e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
25627e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2563d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
25647e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
25657e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
2566e37e2ff3SAndy Lutomirski 			ret = -EINVAL;
2567e37e2ff3SAndy Lutomirski 			goto out;
25687e58d5aeSAmos Kong 		}
25697e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
25707e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2571855e0c52SRusty Russell 		unsigned int i;
2572855e0c52SRusty Russell 
2573855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
2574855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
2575855e0c52SRusty Russell 			virtio_cwrite8(vdev,
2576855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
2577855e0c52SRusty Russell 				       i, addr->sa_data[i]);
25787e58d5aeSAmos Kong 	}
25797e58d5aeSAmos Kong 
25807e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
2581e37e2ff3SAndy Lutomirski 	ret = 0;
25829c46f6d4SAlex Williamson 
2583e37e2ff3SAndy Lutomirski out:
2584e37e2ff3SAndy Lutomirski 	kfree(addr);
2585e37e2ff3SAndy Lutomirski 	return ret;
25869c46f6d4SAlex Williamson }
25879c46f6d4SAlex Williamson 
virtnet_stats(struct net_device * dev,struct rtnl_link_stats64 * tot)2588bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev,
25893fa2a1dfSstephen hemminger 			  struct rtnl_link_stats64 *tot)
25903fa2a1dfSstephen hemminger {
25913fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
25923fa2a1dfSstephen hemminger 	unsigned int start;
2593d7dfc5cfSToshiaki Makita 	int i;
25943fa2a1dfSstephen hemminger 
2595d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
2596a520794bSTony Lu 		u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2597d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
2598d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
25993fa2a1dfSstephen hemminger 
26003fa2a1dfSstephen hemminger 		do {
2601068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&sq->stats.syncp);
260227debe3eSEric Dumazet 			tpackets = u64_stats_read(&sq->stats.packets);
260327debe3eSEric Dumazet 			tbytes   = u64_stats_read(&sq->stats.bytes);
260427debe3eSEric Dumazet 			terrors  = u64_stats_read(&sq->stats.tx_timeouts);
2605068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
260683a27052SEric Dumazet 
260783a27052SEric Dumazet 		do {
2608068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq->stats.syncp);
260927debe3eSEric Dumazet 			rpackets = u64_stats_read(&rq->stats.packets);
261027debe3eSEric Dumazet 			rbytes   = u64_stats_read(&rq->stats.bytes);
261127debe3eSEric Dumazet 			rdrops   = u64_stats_read(&rq->stats.drops);
2612068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq->stats.syncp, start));
26133fa2a1dfSstephen hemminger 
26143fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
26153fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
26163fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
26173fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
26182c4a2f7dSToshiaki Makita 		tot->rx_dropped += rdrops;
2619a520794bSTony Lu 		tot->tx_errors  += terrors;
26203fa2a1dfSstephen hemminger 	}
26213fa2a1dfSstephen hemminger 
2622e2e5c2a3SEric Dumazet 	tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
2623e2e5c2a3SEric Dumazet 	tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
2624e2e5c2a3SEric Dumazet 	tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
2625e2e5c2a3SEric Dumazet 	tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
26263fa2a1dfSstephen hemminger }
26273fa2a1dfSstephen hemminger 
virtnet_ack_link_announce(struct virtnet_info * vi)2628586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
2629586d17c5SJason Wang {
2630586d17c5SJason Wang 	rtnl_lock();
2631586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2632d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2633586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2634586d17c5SJason Wang 	rtnl_unlock();
2635586d17c5SJason Wang }
2636586d17c5SJason Wang 
_virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)263747315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2638986a4f4dSJason Wang {
2639986a4f4dSJason Wang 	struct scatterlist sg;
2640986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
2641986a4f4dSJason Wang 
2642986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2643986a4f4dSJason Wang 		return 0;
2644986a4f4dSJason Wang 
264512e57169SMichael S. Tsirkin 	vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
264612e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2647986a4f4dSJason Wang 
2648986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2649d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2650986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2651986a4f4dSJason Wang 			 queue_pairs);
2652986a4f4dSJason Wang 		return -EINVAL;
265355257d72SSasha Levin 	} else {
2654986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
265535ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
265635ed159bSJason Wang 		if (dev->flags & IFF_UP)
26579b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
265855257d72SSasha Levin 	}
2659986a4f4dSJason Wang 
2660986a4f4dSJason Wang 	return 0;
2661986a4f4dSJason Wang }
2662986a4f4dSJason Wang 
virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)266347315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
266447315329SJohn Fastabend {
266547315329SJohn Fastabend 	int err;
266647315329SJohn Fastabend 
266747315329SJohn Fastabend 	rtnl_lock();
266847315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
266947315329SJohn Fastabend 	rtnl_unlock();
267047315329SJohn Fastabend 	return err;
267147315329SJohn Fastabend }
267247315329SJohn Fastabend 
virtnet_close(struct net_device * dev)2673296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
2674296f96fcSRusty Russell {
2675296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
2676986a4f4dSJason Wang 	int i;
2677296f96fcSRusty Russell 
26785a159128SJason Wang 	/* Make sure NAPI doesn't schedule refill work */
26795a159128SJason Wang 	disable_delayed_refill(vi);
2680b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
2681b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
2682986a4f4dSJason Wang 
26835306623aSFeng Liu 	for (i = 0; i < vi->max_queue_pairs; i++)
26845306623aSFeng Liu 		virtnet_disable_queue_pair(vi, i);
2685296f96fcSRusty Russell 
2686296f96fcSRusty Russell 	return 0;
2687296f96fcSRusty Russell }
2688296f96fcSRusty Russell 
virtnet_set_rx_mode(struct net_device * dev)26892af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
26902af7698eSAlex Williamson {
26912af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
2692f565a7c2SAlex Williamson 	struct scatterlist sg[2];
2693f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
2694ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
269532e7bfc4SJiri Pirko 	int uc_count;
26964cd24eafSJiri Pirko 	int mc_count;
2697f565a7c2SAlex Williamson 	void *buf;
2698f565a7c2SAlex Williamson 	int i;
26992af7698eSAlex Williamson 
2700788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
27012af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
27022af7698eSAlex Williamson 		return;
27032af7698eSAlex Williamson 
270412e57169SMichael S. Tsirkin 	vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
270512e57169SMichael S. Tsirkin 	vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
27062af7698eSAlex Williamson 
270712e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
27082af7698eSAlex Williamson 
27092af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2710d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
27112af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
271212e57169SMichael S. Tsirkin 			 vi->ctrl->promisc ? "en" : "dis");
27132af7698eSAlex Williamson 
271412e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
27152af7698eSAlex Williamson 
27162af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2717d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
27182af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
271912e57169SMichael S. Tsirkin 			 vi->ctrl->allmulti ? "en" : "dis");
2720f565a7c2SAlex Williamson 
272132e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
27224cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
2723f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
27244cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2725f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
27264cd24eafSJiri Pirko 	mac_data = buf;
2727e68ed8f0SJoe Perches 	if (!buf)
2728f565a7c2SAlex Williamson 		return;
2729f565a7c2SAlex Williamson 
273023e258e1SAlex Williamson 	sg_init_table(sg, 2);
273123e258e1SAlex Williamson 
2732f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
2733fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2734ccffad25SJiri Pirko 	i = 0;
273532e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
2736ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2737f565a7c2SAlex Williamson 
2738f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
273932e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2740f565a7c2SAlex Williamson 
2741f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
274232e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
2743f565a7c2SAlex Williamson 
2744fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2745567ec874SJiri Pirko 	i = 0;
274622bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
274722bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2748f565a7c2SAlex Williamson 
2749f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
27504cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2751f565a7c2SAlex Williamson 
2752f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2753d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
275499e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2755f565a7c2SAlex Williamson 
2756f565a7c2SAlex Williamson 	kfree(buf);
27572af7698eSAlex Williamson }
27582af7698eSAlex Williamson 
virtnet_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)275980d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
276080d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
27610bde9569SAlex Williamson {
27620bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
27630bde9569SAlex Williamson 	struct scatterlist sg;
27640bde9569SAlex Williamson 
2765d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
276612e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
27670bde9569SAlex Williamson 
27680bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2769d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
27700bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
27718e586137SJiri Pirko 	return 0;
27720bde9569SAlex Williamson }
27730bde9569SAlex Williamson 
virtnet_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)277480d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
277580d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
27760bde9569SAlex Williamson {
27770bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
27780bde9569SAlex Williamson 	struct scatterlist sg;
27790bde9569SAlex Williamson 
2780d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
278112e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
27820bde9569SAlex Williamson 
27830bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2784d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
27850bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
27868e586137SJiri Pirko 	return 0;
27870bde9569SAlex Williamson }
27880bde9569SAlex Williamson 
virtnet_clean_affinity(struct virtnet_info * vi)2789310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi)
2790986a4f4dSJason Wang {
2791986a4f4dSJason Wang 	int i;
27928898c21cSWanlong Gao 
27938898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
27948898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
279519e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->rq[i].vq, NULL);
279619e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->sq[i].vq, NULL);
27978898c21cSWanlong Gao 		}
27988898c21cSWanlong Gao 
27998898c21cSWanlong Gao 		vi->affinity_hint_set = false;
28008898c21cSWanlong Gao 	}
28018898c21cSWanlong Gao }
28028898c21cSWanlong Gao 
virtnet_set_affinity(struct virtnet_info * vi)28038898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
2804986a4f4dSJason Wang {
28052ca653d6SCaleb Raitto 	cpumask_var_t mask;
28062ca653d6SCaleb Raitto 	int stragglers;
28072ca653d6SCaleb Raitto 	int group_size;
28082ca653d6SCaleb Raitto 	int i, j, cpu;
28092ca653d6SCaleb Raitto 	int num_cpu;
28102ca653d6SCaleb Raitto 	int stride;
2811986a4f4dSJason Wang 
28122ca653d6SCaleb Raitto 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2813310974faSPeter Xu 		virtnet_clean_affinity(vi);
2814986a4f4dSJason Wang 		return;
2815986a4f4dSJason Wang 	}
2816986a4f4dSJason Wang 
28172ca653d6SCaleb Raitto 	num_cpu = num_online_cpus();
28182ca653d6SCaleb Raitto 	stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
28192ca653d6SCaleb Raitto 	stragglers = num_cpu >= vi->curr_queue_pairs ?
28202ca653d6SCaleb Raitto 			num_cpu % vi->curr_queue_pairs :
28212ca653d6SCaleb Raitto 			0;
28229b51d9d8SYury Norov 	cpu = cpumask_first(cpu_online_mask);
28234d99f660SAndrei Vagin 
28242ca653d6SCaleb Raitto 	for (i = 0; i < vi->curr_queue_pairs; i++) {
28252ca653d6SCaleb Raitto 		group_size = stride + (i < stragglers ? 1 : 0);
28262ca653d6SCaleb Raitto 
28272ca653d6SCaleb Raitto 		for (j = 0; j < group_size; j++) {
28282ca653d6SCaleb Raitto 			cpumask_set_cpu(cpu, mask);
28292ca653d6SCaleb Raitto 			cpu = cpumask_next_wrap(cpu, cpu_online_mask,
28302ca653d6SCaleb Raitto 						nr_cpu_ids, false);
28312ca653d6SCaleb Raitto 		}
28322ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->rq[i].vq, mask);
28332ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->sq[i].vq, mask);
2834044ab86dSAntoine Tenart 		__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
28352ca653d6SCaleb Raitto 		cpumask_clear(mask);
2836986a4f4dSJason Wang 	}
2837986a4f4dSJason Wang 
2838986a4f4dSJason Wang 	vi->affinity_hint_set = true;
28392ca653d6SCaleb Raitto 	free_cpumask_var(mask);
284047be2479SWanlong Gao }
2841986a4f4dSJason Wang 
virtnet_cpu_online(unsigned int cpu,struct hlist_node * node)28428017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
28438de4b2f3SWanlong Gao {
28448017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
28458017c279SSebastian Andrzej Siewior 						   node);
28468de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
28478017c279SSebastian Andrzej Siewior 	return 0;
28488de4b2f3SWanlong Gao }
28493ab098dfSJason Wang 
virtnet_cpu_dead(unsigned int cpu,struct hlist_node * node)28508017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
28518017c279SSebastian Andrzej Siewior {
28528017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
28538017c279SSebastian Andrzej Siewior 						   node_dead);
28548017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
28558017c279SSebastian Andrzej Siewior 	return 0;
28568017c279SSebastian Andrzej Siewior }
28578017c279SSebastian Andrzej Siewior 
virtnet_cpu_down_prep(unsigned int cpu,struct hlist_node * node)28588017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
28598017c279SSebastian Andrzej Siewior {
28608017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
28618017c279SSebastian Andrzej Siewior 						   node);
28628017c279SSebastian Andrzej Siewior 
2863310974faSPeter Xu 	virtnet_clean_affinity(vi);
28648017c279SSebastian Andrzej Siewior 	return 0;
28658017c279SSebastian Andrzej Siewior }
28668017c279SSebastian Andrzej Siewior 
28678017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
28688017c279SSebastian Andrzej Siewior 
virtnet_cpu_notif_add(struct virtnet_info * vi)28698017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
28708017c279SSebastian Andrzej Siewior {
28718017c279SSebastian Andrzej Siewior 	int ret;
28728017c279SSebastian Andrzej Siewior 
28738017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
28748017c279SSebastian Andrzej Siewior 	if (ret)
28758017c279SSebastian Andrzej Siewior 		return ret;
28768017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
28778017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
28788017c279SSebastian Andrzej Siewior 	if (!ret)
28798017c279SSebastian Andrzej Siewior 		return ret;
28808017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
28818017c279SSebastian Andrzej Siewior 	return ret;
28828017c279SSebastian Andrzej Siewior }
28838017c279SSebastian Andrzej Siewior 
virtnet_cpu_notif_remove(struct virtnet_info * vi)28848017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
28858017c279SSebastian Andrzej Siewior {
28868017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
28878017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
28888017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
2889a9ea3fc6SHerbert Xu }
2890a9ea3fc6SHerbert Xu 
virtnet_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)28918f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
289274624944SHao Chen 				  struct ethtool_ringparam *ring,
289374624944SHao Chen 				  struct kernel_ethtool_ringparam *kernel_ring,
289474624944SHao Chen 				  struct netlink_ext_ack *extack)
28958f9f4668SRick Jones {
28968f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
28978f9f4668SRick Jones 
28988597b5ddSXuan Zhuo 	ring->rx_max_pending = vi->rq[0].vq->num_max;
28998597b5ddSXuan Zhuo 	ring->tx_max_pending = vi->sq[0].vq->num_max;
29008597b5ddSXuan Zhuo 	ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
29018597b5ddSXuan Zhuo 	ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
29028f9f4668SRick Jones }
29038f9f4668SRick Jones 
290439d591b0SHeng Qi static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
290539d591b0SHeng Qi 					 u16 vqn, u32 max_usecs, u32 max_packets);
290639d591b0SHeng Qi 
virtnet_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)2907a335b33fSXuan Zhuo static int virtnet_set_ringparam(struct net_device *dev,
2908a335b33fSXuan Zhuo 				 struct ethtool_ringparam *ring,
2909a335b33fSXuan Zhuo 				 struct kernel_ethtool_ringparam *kernel_ring,
2910a335b33fSXuan Zhuo 				 struct netlink_ext_ack *extack)
2911a335b33fSXuan Zhuo {
2912a335b33fSXuan Zhuo 	struct virtnet_info *vi = netdev_priv(dev);
2913a335b33fSXuan Zhuo 	u32 rx_pending, tx_pending;
2914a335b33fSXuan Zhuo 	struct receive_queue *rq;
2915a335b33fSXuan Zhuo 	struct send_queue *sq;
2916a335b33fSXuan Zhuo 	int i, err;
2917a335b33fSXuan Zhuo 
2918a335b33fSXuan Zhuo 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2919a335b33fSXuan Zhuo 		return -EINVAL;
2920a335b33fSXuan Zhuo 
2921a335b33fSXuan Zhuo 	rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2922a335b33fSXuan Zhuo 	tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2923a335b33fSXuan Zhuo 
2924a335b33fSXuan Zhuo 	if (ring->rx_pending == rx_pending &&
2925a335b33fSXuan Zhuo 	    ring->tx_pending == tx_pending)
2926a335b33fSXuan Zhuo 		return 0;
2927a335b33fSXuan Zhuo 
2928a335b33fSXuan Zhuo 	if (ring->rx_pending > vi->rq[0].vq->num_max)
2929a335b33fSXuan Zhuo 		return -EINVAL;
2930a335b33fSXuan Zhuo 
2931a335b33fSXuan Zhuo 	if (ring->tx_pending > vi->sq[0].vq->num_max)
2932a335b33fSXuan Zhuo 		return -EINVAL;
2933a335b33fSXuan Zhuo 
2934a335b33fSXuan Zhuo 	for (i = 0; i < vi->max_queue_pairs; i++) {
2935a335b33fSXuan Zhuo 		rq = vi->rq + i;
2936a335b33fSXuan Zhuo 		sq = vi->sq + i;
2937a335b33fSXuan Zhuo 
2938a335b33fSXuan Zhuo 		if (ring->tx_pending != tx_pending) {
2939a335b33fSXuan Zhuo 			err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2940a335b33fSXuan Zhuo 			if (err)
2941a335b33fSXuan Zhuo 				return err;
294239d591b0SHeng Qi 
294339d591b0SHeng Qi 			/* Upon disabling and re-enabling a transmit virtqueue, the device must
294439d591b0SHeng Qi 			 * set the coalescing parameters of the virtqueue to those configured
294539d591b0SHeng Qi 			 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
294639d591b0SHeng Qi 			 * did not set any TX coalescing parameters, to 0.
294739d591b0SHeng Qi 			 */
294839d591b0SHeng Qi 			err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(i),
294939d591b0SHeng Qi 							    vi->intr_coal_tx.max_usecs,
295039d591b0SHeng Qi 							    vi->intr_coal_tx.max_packets);
295139d591b0SHeng Qi 			if (err)
295239d591b0SHeng Qi 				return err;
295339d591b0SHeng Qi 
295439d591b0SHeng Qi 			vi->sq[i].intr_coal.max_usecs = vi->intr_coal_tx.max_usecs;
295539d591b0SHeng Qi 			vi->sq[i].intr_coal.max_packets = vi->intr_coal_tx.max_packets;
2956a335b33fSXuan Zhuo 		}
2957a335b33fSXuan Zhuo 
2958a335b33fSXuan Zhuo 		if (ring->rx_pending != rx_pending) {
2959a335b33fSXuan Zhuo 			err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2960a335b33fSXuan Zhuo 			if (err)
2961a335b33fSXuan Zhuo 				return err;
296239d591b0SHeng Qi 
296339d591b0SHeng Qi 			/* The reason is same as the transmit virtqueue reset */
296439d591b0SHeng Qi 			err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(i),
296539d591b0SHeng Qi 							    vi->intr_coal_rx.max_usecs,
296639d591b0SHeng Qi 							    vi->intr_coal_rx.max_packets);
296739d591b0SHeng Qi 			if (err)
296839d591b0SHeng Qi 				return err;
296939d591b0SHeng Qi 
297039d591b0SHeng Qi 			vi->rq[i].intr_coal.max_usecs = vi->intr_coal_rx.max_usecs;
297139d591b0SHeng Qi 			vi->rq[i].intr_coal.max_packets = vi->intr_coal_rx.max_packets;
2972a335b33fSXuan Zhuo 		}
2973a335b33fSXuan Zhuo 	}
2974a335b33fSXuan Zhuo 
2975a335b33fSXuan Zhuo 	return 0;
2976a9ea3fc6SHerbert Xu }
2977a9ea3fc6SHerbert Xu 
virtnet_commit_rss_command(struct virtnet_info * vi)2978c7114b12SAndrew Melnychenko static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2979c7114b12SAndrew Melnychenko {
2980c7114b12SAndrew Melnychenko 	struct net_device *dev = vi->dev;
2981c7114b12SAndrew Melnychenko 	struct scatterlist sgs[4];
2982c7114b12SAndrew Melnychenko 	unsigned int sg_buf_size;
2983c7114b12SAndrew Melnychenko 
2984c7114b12SAndrew Melnychenko 	/* prepare sgs */
2985c7114b12SAndrew Melnychenko 	sg_init_table(sgs, 4);
2986c7114b12SAndrew Melnychenko 
2987c7114b12SAndrew Melnychenko 	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2988c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2989c7114b12SAndrew Melnychenko 
2990c7114b12SAndrew Melnychenko 	sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2991c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2992c7114b12SAndrew Melnychenko 
2993c7114b12SAndrew Melnychenko 	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2994c7114b12SAndrew Melnychenko 			- offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2995c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2996c7114b12SAndrew Melnychenko 
2997c7114b12SAndrew Melnychenko 	sg_buf_size = vi->rss_key_size;
2998c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2999c7114b12SAndrew Melnychenko 
3000c7114b12SAndrew Melnychenko 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
300191f41f01SAndrew Melnychenko 				  vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
300291f41f01SAndrew Melnychenko 				  : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
3003c7114b12SAndrew Melnychenko 		dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
3004c7114b12SAndrew Melnychenko 		return false;
3005c7114b12SAndrew Melnychenko 	}
3006c7114b12SAndrew Melnychenko 	return true;
3007c7114b12SAndrew Melnychenko }
3008c7114b12SAndrew Melnychenko 
virtnet_init_default_rss(struct virtnet_info * vi)3009c7114b12SAndrew Melnychenko static void virtnet_init_default_rss(struct virtnet_info *vi)
3010c7114b12SAndrew Melnychenko {
3011c7114b12SAndrew Melnychenko 	u32 indir_val = 0;
3012c7114b12SAndrew Melnychenko 	int i = 0;
3013c7114b12SAndrew Melnychenko 
3014c7114b12SAndrew Melnychenko 	vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
3015c1170820SAndrew Melnychenko 	vi->rss_hash_types_saved = vi->rss_hash_types_supported;
3016c7114b12SAndrew Melnychenko 	vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
3017c7114b12SAndrew Melnychenko 						? vi->rss_indir_table_size - 1 : 0;
3018c7114b12SAndrew Melnychenko 	vi->ctrl->rss.unclassified_queue = 0;
3019c7114b12SAndrew Melnychenko 
3020c7114b12SAndrew Melnychenko 	for (; i < vi->rss_indir_table_size; ++i) {
3021c7114b12SAndrew Melnychenko 		indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
3022c7114b12SAndrew Melnychenko 		vi->ctrl->rss.indirection_table[i] = indir_val;
3023c7114b12SAndrew Melnychenko 	}
3024c7114b12SAndrew Melnychenko 
30252c507ce9SHawkins Jiawei 	vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
3026c7114b12SAndrew Melnychenko 	vi->ctrl->rss.hash_key_length = vi->rss_key_size;
3027c7114b12SAndrew Melnychenko 
3028c7114b12SAndrew Melnychenko 	netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
3029c7114b12SAndrew Melnychenko }
3030c7114b12SAndrew Melnychenko 
virtnet_get_hashflow(const struct virtnet_info * vi,struct ethtool_rxnfc * info)3031c1170820SAndrew Melnychenko static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
3032c1170820SAndrew Melnychenko {
3033c1170820SAndrew Melnychenko 	info->data = 0;
3034c1170820SAndrew Melnychenko 	switch (info->flow_type) {
3035c1170820SAndrew Melnychenko 	case TCP_V4_FLOW:
3036c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
3037c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
3038c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3039c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3040c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
3041c1170820SAndrew Melnychenko 		}
3042c1170820SAndrew Melnychenko 		break;
3043c1170820SAndrew Melnychenko 	case TCP_V6_FLOW:
3044c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
3045c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
3046c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3047c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3048c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
3049c1170820SAndrew Melnychenko 		}
3050c1170820SAndrew Melnychenko 		break;
3051c1170820SAndrew Melnychenko 	case UDP_V4_FLOW:
3052c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
3053c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
3054c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3055c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3056c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
3057c1170820SAndrew Melnychenko 		}
3058c1170820SAndrew Melnychenko 		break;
3059c1170820SAndrew Melnychenko 	case UDP_V6_FLOW:
3060c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
3061c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
3062c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3063c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3064c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
3065c1170820SAndrew Melnychenko 		}
3066c1170820SAndrew Melnychenko 		break;
3067c1170820SAndrew Melnychenko 	case IPV4_FLOW:
3068c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
3069c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
3070c1170820SAndrew Melnychenko 
3071c1170820SAndrew Melnychenko 		break;
3072c1170820SAndrew Melnychenko 	case IPV6_FLOW:
3073c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
3074c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
3075c1170820SAndrew Melnychenko 
3076c1170820SAndrew Melnychenko 		break;
3077c1170820SAndrew Melnychenko 	default:
3078c1170820SAndrew Melnychenko 		info->data = 0;
3079c1170820SAndrew Melnychenko 		break;
3080c1170820SAndrew Melnychenko 	}
3081c1170820SAndrew Melnychenko }
3082c1170820SAndrew Melnychenko 
virtnet_set_hashflow(struct virtnet_info * vi,struct ethtool_rxnfc * info)3083c1170820SAndrew Melnychenko static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
3084c1170820SAndrew Melnychenko {
3085c1170820SAndrew Melnychenko 	u32 new_hashtypes = vi->rss_hash_types_saved;
3086c1170820SAndrew Melnychenko 	bool is_disable = info->data & RXH_DISCARD;
3087c1170820SAndrew Melnychenko 	bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
3088c1170820SAndrew Melnychenko 
3089c1170820SAndrew Melnychenko 	/* supports only 'sd', 'sdfn' and 'r' */
3090c1170820SAndrew Melnychenko 	if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
3091c1170820SAndrew Melnychenko 		return false;
3092c1170820SAndrew Melnychenko 
3093c1170820SAndrew Melnychenko 	switch (info->flow_type) {
3094c1170820SAndrew Melnychenko 	case TCP_V4_FLOW:
3095c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
3096c1170820SAndrew Melnychenko 		if (!is_disable)
3097c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3098c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
3099c1170820SAndrew Melnychenko 		break;
3100c1170820SAndrew Melnychenko 	case UDP_V4_FLOW:
3101c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
3102c1170820SAndrew Melnychenko 		if (!is_disable)
3103c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3104c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
3105c1170820SAndrew Melnychenko 		break;
3106c1170820SAndrew Melnychenko 	case IPV4_FLOW:
3107c1170820SAndrew Melnychenko 		new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3108c1170820SAndrew Melnychenko 		if (!is_disable)
3109c1170820SAndrew Melnychenko 			new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3110c1170820SAndrew Melnychenko 		break;
3111c1170820SAndrew Melnychenko 	case TCP_V6_FLOW:
3112c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
3113c1170820SAndrew Melnychenko 		if (!is_disable)
3114c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3115c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
3116c1170820SAndrew Melnychenko 		break;
3117c1170820SAndrew Melnychenko 	case UDP_V6_FLOW:
3118c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
3119c1170820SAndrew Melnychenko 		if (!is_disable)
3120c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3121c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
3122c1170820SAndrew Melnychenko 		break;
3123c1170820SAndrew Melnychenko 	case IPV6_FLOW:
3124c1170820SAndrew Melnychenko 		new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3125c1170820SAndrew Melnychenko 		if (!is_disable)
3126c1170820SAndrew Melnychenko 			new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3127c1170820SAndrew Melnychenko 		break;
3128c1170820SAndrew Melnychenko 	default:
3129c1170820SAndrew Melnychenko 		/* unsupported flow */
3130c1170820SAndrew Melnychenko 		return false;
3131c1170820SAndrew Melnychenko 	}
3132c1170820SAndrew Melnychenko 
3133c1170820SAndrew Melnychenko 	/* if unsupported hashtype was set */
3134c1170820SAndrew Melnychenko 	if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
3135c1170820SAndrew Melnychenko 		return false;
3136c1170820SAndrew Melnychenko 
3137c1170820SAndrew Melnychenko 	if (new_hashtypes != vi->rss_hash_types_saved) {
3138c1170820SAndrew Melnychenko 		vi->rss_hash_types_saved = new_hashtypes;
3139c1170820SAndrew Melnychenko 		vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3140c1170820SAndrew Melnychenko 		if (vi->dev->features & NETIF_F_RXHASH)
3141c1170820SAndrew Melnychenko 			return virtnet_commit_rss_command(vi);
3142c1170820SAndrew Melnychenko 	}
3143c1170820SAndrew Melnychenko 
3144c1170820SAndrew Melnychenko 	return true;
3145c1170820SAndrew Melnychenko }
314666846048SRick Jones 
virtnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)314766846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
314866846048SRick Jones 				struct ethtool_drvinfo *info)
314966846048SRick Jones {
315066846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
315166846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
315266846048SRick Jones 
3153fb3ceec1SWolfram Sang 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
3154fb3ceec1SWolfram Sang 	strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
3155fb3ceec1SWolfram Sang 	strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
315666846048SRick Jones 
315766846048SRick Jones }
315866846048SRick Jones 
3159d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
virtnet_set_channels(struct net_device * dev,struct ethtool_channels * channels)3160d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
3161d73bcd2cSJason Wang 				struct ethtool_channels *channels)
3162d73bcd2cSJason Wang {
3163d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
3164d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
3165d73bcd2cSJason Wang 	int err;
3166d73bcd2cSJason Wang 
3167d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
3168d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
3169d73bcd2cSJason Wang 	 */
3170d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
3171d73bcd2cSJason Wang 		return -EINVAL;
3172d73bcd2cSJason Wang 
3173c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
3174d73bcd2cSJason Wang 		return -EINVAL;
3175d73bcd2cSJason Wang 
3176f600b690SJohn Fastabend 	/* For now we don't support modifying channels while XDP is loaded
3177f600b690SJohn Fastabend 	 * also when XDP is loaded all RX queues have XDP programs so we only
3178f600b690SJohn Fastabend 	 * need to check a single RX queue.
3179f600b690SJohn Fastabend 	 */
3180f600b690SJohn Fastabend 	if (vi->rq[0].xdp_prog)
3181f600b690SJohn Fastabend 		return -EINVAL;
3182f600b690SJohn Fastabend 
3183a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_lock();
318447315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
3185de33212fSJeff Dike 	if (err) {
3186a0d1d0f4SSebastian Andrzej Siewior 		cpus_read_unlock();
3187de33212fSJeff Dike 		goto err;
3188d73bcd2cSJason Wang 	}
3189de33212fSJeff Dike 	virtnet_set_affinity(vi);
3190a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_unlock();
3191d73bcd2cSJason Wang 
3192de33212fSJeff Dike 	netif_set_real_num_tx_queues(dev, queue_pairs);
3193de33212fSJeff Dike 	netif_set_real_num_rx_queues(dev, queue_pairs);
3194de33212fSJeff Dike  err:
3195d73bcd2cSJason Wang 	return err;
3196d73bcd2cSJason Wang }
3197d73bcd2cSJason Wang 
virtnet_get_strings(struct net_device * dev,u32 stringset,u8 * data)3198d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3199d7dfc5cfSToshiaki Makita {
3200d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
3201d7dfc5cfSToshiaki Makita 	unsigned int i, j;
3202d7a9a01bSAlexander Duyck 	u8 *p = data;
3203d7dfc5cfSToshiaki Makita 
3204d7dfc5cfSToshiaki Makita 	switch (stringset) {
3205d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
3206d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
3207d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
3208d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "rx_queue_%u_%s", i,
3209d7a9a01bSAlexander Duyck 						virtnet_rq_stats_desc[j].desc);
3210d7dfc5cfSToshiaki Makita 		}
3211d7dfc5cfSToshiaki Makita 
3212d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
3213d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
3214d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "tx_queue_%u_%s", i,
3215d7a9a01bSAlexander Duyck 						virtnet_sq_stats_desc[j].desc);
3216d7dfc5cfSToshiaki Makita 		}
3217d7dfc5cfSToshiaki Makita 		break;
3218d7dfc5cfSToshiaki Makita 	}
3219d7dfc5cfSToshiaki Makita }
3220d7dfc5cfSToshiaki Makita 
virtnet_get_sset_count(struct net_device * dev,int sset)3221d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset)
3222d7dfc5cfSToshiaki Makita {
3223d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
3224d7dfc5cfSToshiaki Makita 
3225d7dfc5cfSToshiaki Makita 	switch (sset) {
3226d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
3227d7dfc5cfSToshiaki Makita 		return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
3228d7dfc5cfSToshiaki Makita 					       VIRTNET_SQ_STATS_LEN);
3229d7dfc5cfSToshiaki Makita 	default:
3230d7dfc5cfSToshiaki Makita 		return -EOPNOTSUPP;
3231d7dfc5cfSToshiaki Makita 	}
3232d7dfc5cfSToshiaki Makita }
3233d7dfc5cfSToshiaki Makita 
virtnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)3234d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev,
3235d7dfc5cfSToshiaki Makita 				      struct ethtool_stats *stats, u64 *data)
3236d7dfc5cfSToshiaki Makita {
3237d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
3238d7dfc5cfSToshiaki Makita 	unsigned int idx = 0, start, i, j;
3239d7dfc5cfSToshiaki Makita 	const u8 *stats_base;
324027debe3eSEric Dumazet 	const u64_stats_t *p;
3241d7dfc5cfSToshiaki Makita 	size_t offset;
3242d7dfc5cfSToshiaki Makita 
3243d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
3244d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
3245d7dfc5cfSToshiaki Makita 
324627debe3eSEric Dumazet 		stats_base = (const u8 *)&rq->stats;
3247d7dfc5cfSToshiaki Makita 		do {
3248068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq->stats.syncp);
3249d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
3250d7dfc5cfSToshiaki Makita 				offset = virtnet_rq_stats_desc[j].offset;
325127debe3eSEric Dumazet 				p = (const u64_stats_t *)(stats_base + offset);
325227debe3eSEric Dumazet 				data[idx + j] = u64_stats_read(p);
3253d7dfc5cfSToshiaki Makita 			}
3254068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3255d7dfc5cfSToshiaki Makita 		idx += VIRTNET_RQ_STATS_LEN;
3256d7dfc5cfSToshiaki Makita 	}
3257d7dfc5cfSToshiaki Makita 
3258d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
3259d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
3260d7dfc5cfSToshiaki Makita 
326127debe3eSEric Dumazet 		stats_base = (const u8 *)&sq->stats;
3262d7dfc5cfSToshiaki Makita 		do {
3263068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&sq->stats.syncp);
3264d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
3265d7dfc5cfSToshiaki Makita 				offset = virtnet_sq_stats_desc[j].offset;
326627debe3eSEric Dumazet 				p = (const u64_stats_t *)(stats_base + offset);
326727debe3eSEric Dumazet 				data[idx + j] = u64_stats_read(p);
3268d7dfc5cfSToshiaki Makita 			}
3269068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3270d7dfc5cfSToshiaki Makita 		idx += VIRTNET_SQ_STATS_LEN;
3271d7dfc5cfSToshiaki Makita 	}
3272d7dfc5cfSToshiaki Makita }
3273d7dfc5cfSToshiaki Makita 
virtnet_get_channels(struct net_device * dev,struct ethtool_channels * channels)3274d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
3275d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
3276d73bcd2cSJason Wang {
3277d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
3278d73bcd2cSJason Wang 
3279d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
3280d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
3281d73bcd2cSJason Wang 	channels->max_other = 0;
3282d73bcd2cSJason Wang 	channels->rx_count = 0;
3283d73bcd2cSJason Wang 	channels->tx_count = 0;
3284d73bcd2cSJason Wang 	channels->other_count = 0;
3285d73bcd2cSJason Wang }
3286d73bcd2cSJason Wang 
virtnet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)3287ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev,
3288ebb6b4b1SPhilippe Reynes 				      const struct ethtool_link_ksettings *cmd)
328916032be5SNikolay Aleksandrov {
329016032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
329116032be5SNikolay Aleksandrov 
32929aedc6e2SCris Forno 	return ethtool_virtdev_set_link_ksettings(dev, cmd,
32939aedc6e2SCris Forno 						  &vi->speed, &vi->duplex);
329416032be5SNikolay Aleksandrov }
329516032be5SNikolay Aleksandrov 
virtnet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)3296ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev,
3297ebb6b4b1SPhilippe Reynes 				      struct ethtool_link_ksettings *cmd)
329816032be5SNikolay Aleksandrov {
329916032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
330016032be5SNikolay Aleksandrov 
3301ebb6b4b1SPhilippe Reynes 	cmd->base.speed = vi->speed;
3302ebb6b4b1SPhilippe Reynes 	cmd->base.duplex = vi->duplex;
3303ebb6b4b1SPhilippe Reynes 	cmd->base.port = PORT_OTHER;
330416032be5SNikolay Aleksandrov 
330516032be5SNikolay Aleksandrov 	return 0;
330616032be5SNikolay Aleksandrov }
330716032be5SNikolay Aleksandrov 
virtnet_send_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)3308699b045aSAlvaro Karsz static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
3309699b045aSAlvaro Karsz 				       struct ethtool_coalesce *ec)
3310699b045aSAlvaro Karsz {
3311699b045aSAlvaro Karsz 	struct scatterlist sgs_tx, sgs_rx;
3312829cce76SHeng Qi 	int i;
3313699b045aSAlvaro Karsz 
3314accc1bf2SBrett Creeley 	vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
3315accc1bf2SBrett Creeley 	vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
3316accc1bf2SBrett Creeley 	sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx));
3317699b045aSAlvaro Karsz 
3318699b045aSAlvaro Karsz 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3319699b045aSAlvaro Karsz 				  VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
3320699b045aSAlvaro Karsz 				  &sgs_tx))
3321699b045aSAlvaro Karsz 		return -EINVAL;
3322699b045aSAlvaro Karsz 
3323699b045aSAlvaro Karsz 	/* Save parameters */
3324308d7982SGavin Li 	vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
3325308d7982SGavin Li 	vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
3326829cce76SHeng Qi 	for (i = 0; i < vi->max_queue_pairs; i++) {
3327829cce76SHeng Qi 		vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3328829cce76SHeng Qi 		vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3329829cce76SHeng Qi 	}
3330699b045aSAlvaro Karsz 
3331accc1bf2SBrett Creeley 	vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
3332accc1bf2SBrett Creeley 	vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
3333accc1bf2SBrett Creeley 	sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx));
3334699b045aSAlvaro Karsz 
3335699b045aSAlvaro Karsz 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3336699b045aSAlvaro Karsz 				  VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
3337699b045aSAlvaro Karsz 				  &sgs_rx))
3338699b045aSAlvaro Karsz 		return -EINVAL;
3339699b045aSAlvaro Karsz 
3340699b045aSAlvaro Karsz 	/* Save parameters */
3341308d7982SGavin Li 	vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
3342308d7982SGavin Li 	vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
3343829cce76SHeng Qi 	for (i = 0; i < vi->max_queue_pairs; i++) {
3344829cce76SHeng Qi 		vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3345829cce76SHeng Qi 		vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3346829cce76SHeng Qi 	}
3347699b045aSAlvaro Karsz 
3348699b045aSAlvaro Karsz 	return 0;
3349699b045aSAlvaro Karsz }
3350699b045aSAlvaro Karsz 
virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 vqn,u32 max_usecs,u32 max_packets)3351394bd877SGavin Li static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3352394bd877SGavin Li 					 u16 vqn, u32 max_usecs, u32 max_packets)
3353394bd877SGavin Li {
3354394bd877SGavin Li 	struct scatterlist sgs;
3355394bd877SGavin Li 
3356394bd877SGavin Li 	vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn);
3357394bd877SGavin Li 	vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs);
3358394bd877SGavin Li 	vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets);
3359394bd877SGavin Li 	sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq));
3360394bd877SGavin Li 
3361394bd877SGavin Li 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3362394bd877SGavin Li 				  VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
3363394bd877SGavin Li 				  &sgs))
3364394bd877SGavin Li 		return -EINVAL;
3365394bd877SGavin Li 
3366394bd877SGavin Li 	return 0;
3367394bd877SGavin Li }
3368394bd877SGavin Li 
virtnet_send_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)3369394bd877SGavin Li static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
3370394bd877SGavin Li 					  struct ethtool_coalesce *ec,
3371394bd877SGavin Li 					  u16 queue)
3372394bd877SGavin Li {
3373394bd877SGavin Li 	int err;
3374394bd877SGavin Li 
3375394bd877SGavin Li 	err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
3376394bd877SGavin Li 					    ec->rx_coalesce_usecs,
3377394bd877SGavin Li 					    ec->rx_max_coalesced_frames);
3378394bd877SGavin Li 	if (err)
3379394bd877SGavin Li 		return err;
33807c1453abSHeng Qi 
3381394bd877SGavin Li 	vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3382394bd877SGavin Li 	vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3383394bd877SGavin Li 
3384394bd877SGavin Li 	err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
3385394bd877SGavin Li 					    ec->tx_coalesce_usecs,
3386394bd877SGavin Li 					    ec->tx_max_coalesced_frames);
3387394bd877SGavin Li 	if (err)
3388394bd877SGavin Li 		return err;
33897c1453abSHeng Qi 
3390394bd877SGavin Li 	vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3391394bd877SGavin Li 	vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3392394bd877SGavin Li 
3393394bd877SGavin Li 	return 0;
3394394bd877SGavin Li }
3395394bd877SGavin Li 
virtnet_coal_params_supported(struct ethtool_coalesce * ec)3396699b045aSAlvaro Karsz static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
3397699b045aSAlvaro Karsz {
3398699b045aSAlvaro Karsz 	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
3399699b045aSAlvaro Karsz 	 * feature is negotiated.
3400699b045aSAlvaro Karsz 	 */
3401699b045aSAlvaro Karsz 	if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
3402699b045aSAlvaro Karsz 		return -EOPNOTSUPP;
3403699b045aSAlvaro Karsz 
3404699b045aSAlvaro Karsz 	if (ec->tx_max_coalesced_frames > 1 ||
3405699b045aSAlvaro Karsz 	    ec->rx_max_coalesced_frames != 1)
3406699b045aSAlvaro Karsz 		return -EINVAL;
3407699b045aSAlvaro Karsz 
3408699b045aSAlvaro Karsz 	return 0;
3409699b045aSAlvaro Karsz }
3410699b045aSAlvaro Karsz 
virtnet_should_update_vq_weight(int dev_flags,int weight,int vq_weight,bool * should_update)3411394bd877SGavin Li static int virtnet_should_update_vq_weight(int dev_flags, int weight,
3412394bd877SGavin Li 					   int vq_weight, bool *should_update)
3413394bd877SGavin Li {
3414394bd877SGavin Li 	if (weight ^ vq_weight) {
3415394bd877SGavin Li 		if (dev_flags & IFF_UP)
3416394bd877SGavin Li 			return -EBUSY;
3417394bd877SGavin Li 		*should_update = true;
3418394bd877SGavin Li 	}
3419394bd877SGavin Li 
3420394bd877SGavin Li 	return 0;
3421394bd877SGavin Li }
3422394bd877SGavin Li 
virtnet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)34230c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev,
3424f3ccfda1SYufeng Mo 				struct ethtool_coalesce *ec,
3425f3ccfda1SYufeng Mo 				struct kernel_ethtool_coalesce *kernel_coal,
3426f3ccfda1SYufeng Mo 				struct netlink_ext_ack *extack)
34270c465be1SJason Wang {
34280c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
3429394bd877SGavin Li 	int ret, queue_number, napi_weight;
3430699b045aSAlvaro Karsz 	bool update_napi = false;
34310c465be1SJason Wang 
3432699b045aSAlvaro Karsz 	/* Can't change NAPI weight if the link is up */
34330c465be1SJason Wang 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3434394bd877SGavin Li 	for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
3435394bd877SGavin Li 		ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3436394bd877SGavin Li 						      vi->sq[queue_number].napi.weight,
3437394bd877SGavin Li 						      &update_napi);
3438394bd877SGavin Li 		if (ret)
3439394bd877SGavin Li 			return ret;
3440394bd877SGavin Li 
3441394bd877SGavin Li 		if (update_napi) {
3442394bd877SGavin Li 			/* All queues that belong to [queue_number, vi->max_queue_pairs] will be
3443394bd877SGavin Li 			 * updated for the sake of simplicity, which might not be necessary
3444394bd877SGavin Li 			 */
3445394bd877SGavin Li 			break;
3446394bd877SGavin Li 		}
3447699b045aSAlvaro Karsz 	}
3448699b045aSAlvaro Karsz 
3449699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
3450699b045aSAlvaro Karsz 		ret = virtnet_send_notf_coal_cmds(vi, ec);
3451699b045aSAlvaro Karsz 	else
3452699b045aSAlvaro Karsz 		ret = virtnet_coal_params_supported(ec);
3453699b045aSAlvaro Karsz 
3454699b045aSAlvaro Karsz 	if (ret)
3455699b045aSAlvaro Karsz 		return ret;
3456699b045aSAlvaro Karsz 
3457699b045aSAlvaro Karsz 	if (update_napi) {
3458394bd877SGavin Li 		for (; queue_number < vi->max_queue_pairs; queue_number++)
3459394bd877SGavin Li 			vi->sq[queue_number].napi.weight = napi_weight;
34600c465be1SJason Wang 	}
34610c465be1SJason Wang 
3462699b045aSAlvaro Karsz 	return ret;
34630c465be1SJason Wang }
34640c465be1SJason Wang 
virtnet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)34650c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev,
3466f3ccfda1SYufeng Mo 				struct ethtool_coalesce *ec,
3467f3ccfda1SYufeng Mo 				struct kernel_ethtool_coalesce *kernel_coal,
3468f3ccfda1SYufeng Mo 				struct netlink_ext_ack *extack)
34690c465be1SJason Wang {
34700c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
34710c465be1SJason Wang 
3472699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3473308d7982SGavin Li 		ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
3474308d7982SGavin Li 		ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
3475308d7982SGavin Li 		ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
3476308d7982SGavin Li 		ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
3477699b045aSAlvaro Karsz 	} else {
3478699b045aSAlvaro Karsz 		ec->rx_max_coalesced_frames = 1;
34790c465be1SJason Wang 
34800c465be1SJason Wang 		if (vi->sq[0].napi.weight)
34810c465be1SJason Wang 			ec->tx_max_coalesced_frames = 1;
3482699b045aSAlvaro Karsz 	}
34830c465be1SJason Wang 
34840c465be1SJason Wang 	return 0;
34850c465be1SJason Wang }
34860c465be1SJason Wang 
virtnet_set_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)3487394bd877SGavin Li static int virtnet_set_per_queue_coalesce(struct net_device *dev,
3488394bd877SGavin Li 					  u32 queue,
3489394bd877SGavin Li 					  struct ethtool_coalesce *ec)
3490394bd877SGavin Li {
3491394bd877SGavin Li 	struct virtnet_info *vi = netdev_priv(dev);
3492394bd877SGavin Li 	int ret, napi_weight;
3493394bd877SGavin Li 	bool update_napi = false;
3494394bd877SGavin Li 
3495394bd877SGavin Li 	if (queue >= vi->max_queue_pairs)
3496394bd877SGavin Li 		return -EINVAL;
3497394bd877SGavin Li 
3498394bd877SGavin Li 	/* Can't change NAPI weight if the link is up */
3499394bd877SGavin Li 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3500394bd877SGavin Li 	ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3501394bd877SGavin Li 					      vi->sq[queue].napi.weight,
3502394bd877SGavin Li 					      &update_napi);
3503394bd877SGavin Li 	if (ret)
3504394bd877SGavin Li 		return ret;
3505394bd877SGavin Li 
3506394bd877SGavin Li 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3507394bd877SGavin Li 		ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
3508394bd877SGavin Li 	else
3509394bd877SGavin Li 		ret = virtnet_coal_params_supported(ec);
3510394bd877SGavin Li 
3511394bd877SGavin Li 	if (ret)
3512394bd877SGavin Li 		return ret;
3513394bd877SGavin Li 
3514394bd877SGavin Li 	if (update_napi)
3515394bd877SGavin Li 		vi->sq[queue].napi.weight = napi_weight;
3516394bd877SGavin Li 
3517394bd877SGavin Li 	return 0;
3518394bd877SGavin Li }
3519394bd877SGavin Li 
virtnet_get_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)3520394bd877SGavin Li static int virtnet_get_per_queue_coalesce(struct net_device *dev,
3521394bd877SGavin Li 					  u32 queue,
3522394bd877SGavin Li 					  struct ethtool_coalesce *ec)
3523394bd877SGavin Li {
3524394bd877SGavin Li 	struct virtnet_info *vi = netdev_priv(dev);
3525394bd877SGavin Li 
3526394bd877SGavin Li 	if (queue >= vi->max_queue_pairs)
3527394bd877SGavin Li 		return -EINVAL;
3528394bd877SGavin Li 
3529394bd877SGavin Li 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
3530394bd877SGavin Li 		ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
3531394bd877SGavin Li 		ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
3532394bd877SGavin Li 		ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
3533394bd877SGavin Li 		ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
3534394bd877SGavin Li 	} else {
3535394bd877SGavin Li 		ec->rx_max_coalesced_frames = 1;
3536394bd877SGavin Li 
353784a056f7SHeng Qi 		if (vi->sq[queue].napi.weight)
3538394bd877SGavin Li 			ec->tx_max_coalesced_frames = 1;
3539394bd877SGavin Li 	}
3540394bd877SGavin Li 
3541394bd877SGavin Li 	return 0;
3542394bd877SGavin Li }
3543394bd877SGavin Li 
virtnet_init_settings(struct net_device * dev)354416032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
354516032be5SNikolay Aleksandrov {
354616032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
354716032be5SNikolay Aleksandrov 
354816032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
354916032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
355016032be5SNikolay Aleksandrov }
355116032be5SNikolay Aleksandrov 
virtnet_update_settings(struct virtnet_info * vi)3552faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi)
3553faa9b39fSJason Baron {
3554faa9b39fSJason Baron 	u32 speed;
3555faa9b39fSJason Baron 	u8 duplex;
3556faa9b39fSJason Baron 
3557faa9b39fSJason Baron 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3558faa9b39fSJason Baron 		return;
3559faa9b39fSJason Baron 
356064ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
356164ffa39dSMichael S. Tsirkin 
3562faa9b39fSJason Baron 	if (ethtool_validate_speed(speed))
3563faa9b39fSJason Baron 		vi->speed = speed;
356464ffa39dSMichael S. Tsirkin 
356564ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
356664ffa39dSMichael S. Tsirkin 
3567faa9b39fSJason Baron 	if (ethtool_validate_duplex(duplex))
3568faa9b39fSJason Baron 		vi->duplex = duplex;
3569faa9b39fSJason Baron }
3570faa9b39fSJason Baron 
virtnet_get_rxfh_key_size(struct net_device * dev)3571c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
3572c7114b12SAndrew Melnychenko {
3573c7114b12SAndrew Melnychenko 	return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
3574c7114b12SAndrew Melnychenko }
3575c7114b12SAndrew Melnychenko 
virtnet_get_rxfh_indir_size(struct net_device * dev)3576c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
3577c7114b12SAndrew Melnychenko {
3578c7114b12SAndrew Melnychenko 	return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
3579c7114b12SAndrew Melnychenko }
3580c7114b12SAndrew Melnychenko 
virtnet_get_rxfh(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc)3581c7114b12SAndrew Melnychenko static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
3582c7114b12SAndrew Melnychenko {
3583c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3584c7114b12SAndrew Melnychenko 	int i;
3585c7114b12SAndrew Melnychenko 
3586c7114b12SAndrew Melnychenko 	if (indir) {
3587c7114b12SAndrew Melnychenko 		for (i = 0; i < vi->rss_indir_table_size; ++i)
3588c7114b12SAndrew Melnychenko 			indir[i] = vi->ctrl->rss.indirection_table[i];
3589c7114b12SAndrew Melnychenko 	}
3590c7114b12SAndrew Melnychenko 
3591c7114b12SAndrew Melnychenko 	if (key)
3592c7114b12SAndrew Melnychenko 		memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
3593c7114b12SAndrew Melnychenko 
3594c7114b12SAndrew Melnychenko 	if (hfunc)
3595c7114b12SAndrew Melnychenko 		*hfunc = ETH_RSS_HASH_TOP;
3596c7114b12SAndrew Melnychenko 
3597c7114b12SAndrew Melnychenko 	return 0;
3598c7114b12SAndrew Melnychenko }
3599c7114b12SAndrew Melnychenko 
virtnet_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc)3600c7114b12SAndrew Melnychenko static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
3601c7114b12SAndrew Melnychenko {
3602c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
360343a71c1bSBreno Leitao 	bool update = false;
3604c7114b12SAndrew Melnychenko 	int i;
3605c7114b12SAndrew Melnychenko 
3606c7114b12SAndrew Melnychenko 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3607c7114b12SAndrew Melnychenko 		return -EOPNOTSUPP;
3608c7114b12SAndrew Melnychenko 
3609c7114b12SAndrew Melnychenko 	if (indir) {
361043a71c1bSBreno Leitao 		if (!vi->has_rss)
361143a71c1bSBreno Leitao 			return -EOPNOTSUPP;
361243a71c1bSBreno Leitao 
3613c7114b12SAndrew Melnychenko 		for (i = 0; i < vi->rss_indir_table_size; ++i)
3614c7114b12SAndrew Melnychenko 			vi->ctrl->rss.indirection_table[i] = indir[i];
361543a71c1bSBreno Leitao 		update = true;
3616c7114b12SAndrew Melnychenko 	}
361743a71c1bSBreno Leitao 	if (key) {
361843a71c1bSBreno Leitao 		/* If either _F_HASH_REPORT or _F_RSS are negotiated, the
361943a71c1bSBreno Leitao 		 * device provides hash calculation capabilities, that is,
362043a71c1bSBreno Leitao 		 * hash_key is configured.
362143a71c1bSBreno Leitao 		 */
362243a71c1bSBreno Leitao 		if (!vi->has_rss && !vi->has_rss_hash_report)
362343a71c1bSBreno Leitao 			return -EOPNOTSUPP;
3624c7114b12SAndrew Melnychenko 
362543a71c1bSBreno Leitao 		memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
362643a71c1bSBreno Leitao 		update = true;
362743a71c1bSBreno Leitao 	}
362843a71c1bSBreno Leitao 
362943a71c1bSBreno Leitao 	if (update)
3630c7114b12SAndrew Melnychenko 		virtnet_commit_rss_command(vi);
3631c7114b12SAndrew Melnychenko 
3632c7114b12SAndrew Melnychenko 	return 0;
3633c7114b12SAndrew Melnychenko }
3634c7114b12SAndrew Melnychenko 
virtnet_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rule_locs)3635c7114b12SAndrew Melnychenko static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
3636c7114b12SAndrew Melnychenko {
3637c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3638c7114b12SAndrew Melnychenko 	int rc = 0;
3639c7114b12SAndrew Melnychenko 
3640c7114b12SAndrew Melnychenko 	switch (info->cmd) {
3641c7114b12SAndrew Melnychenko 	case ETHTOOL_GRXRINGS:
3642c7114b12SAndrew Melnychenko 		info->data = vi->curr_queue_pairs;
3643c7114b12SAndrew Melnychenko 		break;
3644c1170820SAndrew Melnychenko 	case ETHTOOL_GRXFH:
3645c1170820SAndrew Melnychenko 		virtnet_get_hashflow(vi, info);
3646c1170820SAndrew Melnychenko 		break;
3647c1170820SAndrew Melnychenko 	default:
3648c1170820SAndrew Melnychenko 		rc = -EOPNOTSUPP;
3649c1170820SAndrew Melnychenko 	}
3650c1170820SAndrew Melnychenko 
3651c1170820SAndrew Melnychenko 	return rc;
3652c1170820SAndrew Melnychenko }
3653c1170820SAndrew Melnychenko 
virtnet_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)3654c1170820SAndrew Melnychenko static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
3655c1170820SAndrew Melnychenko {
3656c1170820SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3657c1170820SAndrew Melnychenko 	int rc = 0;
3658c1170820SAndrew Melnychenko 
3659c1170820SAndrew Melnychenko 	switch (info->cmd) {
3660c1170820SAndrew Melnychenko 	case ETHTOOL_SRXFH:
3661c1170820SAndrew Melnychenko 		if (!virtnet_set_hashflow(vi, info))
3662c1170820SAndrew Melnychenko 			rc = -EINVAL;
3663c1170820SAndrew Melnychenko 
3664c1170820SAndrew Melnychenko 		break;
3665c7114b12SAndrew Melnychenko 	default:
3666c7114b12SAndrew Melnychenko 		rc = -EOPNOTSUPP;
3667c7114b12SAndrew Melnychenko 	}
3668c7114b12SAndrew Melnychenko 
3669c7114b12SAndrew Melnychenko 	return rc;
3670c7114b12SAndrew Melnychenko }
3671c7114b12SAndrew Melnychenko 
36720fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
3673699b045aSAlvaro Karsz 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
3674699b045aSAlvaro Karsz 		ETHTOOL_COALESCE_USECS,
367566846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
36769f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
36778f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
3678a335b33fSXuan Zhuo 	.set_ringparam = virtnet_set_ringparam,
3679d7dfc5cfSToshiaki Makita 	.get_strings = virtnet_get_strings,
3680d7dfc5cfSToshiaki Makita 	.get_sset_count = virtnet_get_sset_count,
3681d7dfc5cfSToshiaki Makita 	.get_ethtool_stats = virtnet_get_ethtool_stats,
3682d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
3683d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
3684074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
3685ebb6b4b1SPhilippe Reynes 	.get_link_ksettings = virtnet_get_link_ksettings,
3686ebb6b4b1SPhilippe Reynes 	.set_link_ksettings = virtnet_set_link_ksettings,
36870c465be1SJason Wang 	.set_coalesce = virtnet_set_coalesce,
36880c465be1SJason Wang 	.get_coalesce = virtnet_get_coalesce,
3689394bd877SGavin Li 	.set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
3690394bd877SGavin Li 	.get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
3691c7114b12SAndrew Melnychenko 	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
3692c7114b12SAndrew Melnychenko 	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
3693c7114b12SAndrew Melnychenko 	.get_rxfh = virtnet_get_rxfh,
3694c7114b12SAndrew Melnychenko 	.set_rxfh = virtnet_set_rxfh,
3695c7114b12SAndrew Melnychenko 	.get_rxnfc = virtnet_get_rxnfc,
3696c1170820SAndrew Melnychenko 	.set_rxnfc = virtnet_set_rxnfc,
3697a9ea3fc6SHerbert Xu };
3698a9ea3fc6SHerbert Xu 
virtnet_freeze_down(struct virtio_device * vdev)36999fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev)
37009fe7bfceSJohn Fastabend {
37019fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
37029fe7bfceSJohn Fastabend 
37039fe7bfceSJohn Fastabend 	/* Make sure no work handler is accessing the device */
37049fe7bfceSJohn Fastabend 	flush_work(&vi->config_work);
37059fe7bfceSJohn Fastabend 
370605c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
37079fe7bfceSJohn Fastabend 	netif_device_detach(vi->dev);
370805c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
37098af52fe9SStephan Gerhold 	if (netif_running(vi->dev))
37108af52fe9SStephan Gerhold 		virtnet_close(vi->dev);
37119fe7bfceSJohn Fastabend }
37129fe7bfceSJohn Fastabend 
37139fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi);
37149fe7bfceSJohn Fastabend 
virtnet_restore_up(struct virtio_device * vdev)37159fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev)
37169fe7bfceSJohn Fastabend {
37179fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
37188af52fe9SStephan Gerhold 	int err;
37199fe7bfceSJohn Fastabend 
37209fe7bfceSJohn Fastabend 	err = init_vqs(vi);
37219fe7bfceSJohn Fastabend 	if (err)
37229fe7bfceSJohn Fastabend 		return err;
37239fe7bfceSJohn Fastabend 
37249fe7bfceSJohn Fastabend 	virtio_device_ready(vdev);
37259fe7bfceSJohn Fastabend 
37265a159128SJason Wang 	enable_delayed_refill(vi);
37275a159128SJason Wang 
37289fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
37298af52fe9SStephan Gerhold 		err = virtnet_open(vi->dev);
37308af52fe9SStephan Gerhold 		if (err)
37318af52fe9SStephan Gerhold 			return err;
37329fe7bfceSJohn Fastabend 	}
37339fe7bfceSJohn Fastabend 
373405c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
37359fe7bfceSJohn Fastabend 	netif_device_attach(vi->dev);
373605c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
37379fe7bfceSJohn Fastabend 	return err;
37389fe7bfceSJohn Fastabend }
37399fe7bfceSJohn Fastabend 
virtnet_set_guest_offloads(struct virtnet_info * vi,u64 offloads)37403f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
37413f93522fSJason Wang {
37423f93522fSJason Wang 	struct scatterlist sg;
374312e57169SMichael S. Tsirkin 	vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
37443f93522fSJason Wang 
374512e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
37463f93522fSJason Wang 
37473f93522fSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
37483f93522fSJason Wang 				  VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
37493f93522fSJason Wang 		dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
37503f93522fSJason Wang 		return -EINVAL;
37513f93522fSJason Wang 	}
37523f93522fSJason Wang 
37533f93522fSJason Wang 	return 0;
37543f93522fSJason Wang }
37553f93522fSJason Wang 
virtnet_clear_guest_offloads(struct virtnet_info * vi)37563f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
37573f93522fSJason Wang {
37583f93522fSJason Wang 	u64 offloads = 0;
37593f93522fSJason Wang 
37603f93522fSJason Wang 	if (!vi->guest_offloads)
37613f93522fSJason Wang 		return 0;
37623f93522fSJason Wang 
37633f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
37643f93522fSJason Wang }
37653f93522fSJason Wang 
virtnet_restore_guest_offloads(struct virtnet_info * vi)37663f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
37673f93522fSJason Wang {
37683f93522fSJason Wang 	u64 offloads = vi->guest_offloads;
37693f93522fSJason Wang 
37703f93522fSJason Wang 	if (!vi->guest_offloads)
37713f93522fSJason Wang 		return 0;
37723f93522fSJason Wang 
37733f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
37743f93522fSJason Wang }
37753f93522fSJason Wang 
virtnet_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)37769861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
37779861ce03SJakub Kicinski 			   struct netlink_ext_ack *extack)
3778f600b690SJohn Fastabend {
3779e814b958SHeng Qi 	unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
3780e814b958SHeng Qi 					   sizeof(struct skb_shared_info));
3781e814b958SHeng Qi 	unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
3782f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
3783f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
3784017b29c3SJason Wang 	u16 xdp_qp = 0, curr_qp;
3785672aafd5SJohn Fastabend 	int i, err;
3786f600b690SJohn Fastabend 
37873f93522fSJason Wang 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
37883f93522fSJason Wang 	    && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
378992502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
379092502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
379118ba58e1SJason Wang 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3792418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3793418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3794418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3795dbcf24d1SJason Wang 		NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3796f600b690SJohn Fastabend 		return -EOPNOTSUPP;
3797f600b690SJohn Fastabend 	}
3798f600b690SJohn Fastabend 
3799f600b690SJohn Fastabend 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
38004d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3801f600b690SJohn Fastabend 		return -EINVAL;
3802f600b690SJohn Fastabend 	}
3803f600b690SJohn Fastabend 
38048d9bc36dSHeng Qi 	if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
38058d9bc36dSHeng Qi 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
38068d9bc36dSHeng Qi 		netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
3807f600b690SJohn Fastabend 		return -EINVAL;
3808f600b690SJohn Fastabend 	}
3809f600b690SJohn Fastabend 
3810672aafd5SJohn Fastabend 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3811672aafd5SJohn Fastabend 	if (prog)
3812672aafd5SJohn Fastabend 		xdp_qp = nr_cpu_ids;
3813672aafd5SJohn Fastabend 
3814672aafd5SJohn Fastabend 	/* XDP requires extra queues for XDP_TX */
3815672aafd5SJohn Fastabend 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
38169ce4e3d6SXuan Zhuo 		netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
3817672aafd5SJohn Fastabend 				 curr_qp + xdp_qp, vi->max_queue_pairs);
381897c2c69eSXuan Zhuo 		xdp_qp = 0;
3819672aafd5SJohn Fastabend 	}
3820672aafd5SJohn Fastabend 
382103aa6d34SToshiaki Makita 	old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
382203aa6d34SToshiaki Makita 	if (!prog && !old_prog)
382303aa6d34SToshiaki Makita 		return 0;
382403aa6d34SToshiaki Makita 
382585192dbfSAndrii Nakryiko 	if (prog)
382685192dbfSAndrii Nakryiko 		bpf_prog_add(prog, vi->max_queue_pairs - 1);
38272de2f7f4SJohn Fastabend 
38284941d472SJason Wang 	/* Make sure NAPI is not using any XDP TX queues for RX. */
3829534da5e8SToshiaki Makita 	if (netif_running(dev)) {
3830534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
38314941d472SJason Wang 			napi_disable(&vi->rq[i].napi);
3832534da5e8SToshiaki Makita 			virtnet_napi_tx_disable(&vi->sq[i].napi);
3833534da5e8SToshiaki Makita 		}
3834534da5e8SToshiaki Makita 	}
38352de2f7f4SJohn Fastabend 
383603aa6d34SToshiaki Makita 	if (!prog) {
383703aa6d34SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
383803aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
383903aa6d34SToshiaki Makita 			if (i == 0)
384003aa6d34SToshiaki Makita 				virtnet_restore_guest_offloads(vi);
384103aa6d34SToshiaki Makita 		}
384203aa6d34SToshiaki Makita 		synchronize_net();
384303aa6d34SToshiaki Makita 	}
384403aa6d34SToshiaki Makita 
38454941d472SJason Wang 	err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
38464941d472SJason Wang 	if (err)
38474941d472SJason Wang 		goto err;
3848188313c1SToshiaki Makita 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
38494941d472SJason Wang 	vi->xdp_queue_pairs = xdp_qp;
3850f600b690SJohn Fastabend 
385103aa6d34SToshiaki Makita 	if (prog) {
385297c2c69eSXuan Zhuo 		vi->xdp_enabled = true;
3853f600b690SJohn Fastabend 		for (i = 0; i < vi->max_queue_pairs; i++) {
3854f600b690SJohn Fastabend 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
385503aa6d34SToshiaki Makita 			if (i == 0 && !old_prog)
38563f93522fSJason Wang 				virtnet_clear_guest_offloads(vi);
38573f93522fSJason Wang 		}
385866c0e13aSMarek Majtyka 		if (!old_prog)
385930bbf891SLorenzo Bianconi 			xdp_features_set_redirect_target(dev, true);
386097c2c69eSXuan Zhuo 	} else {
386166c0e13aSMarek Majtyka 		xdp_features_clear_redirect_target(dev);
386297c2c69eSXuan Zhuo 		vi->xdp_enabled = false;
386303aa6d34SToshiaki Makita 	}
386403aa6d34SToshiaki Makita 
386503aa6d34SToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
3866f600b690SJohn Fastabend 		if (old_prog)
3867f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
3868534da5e8SToshiaki Makita 		if (netif_running(dev)) {
38694941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3870534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3871534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
3872534da5e8SToshiaki Makita 		}
3873f600b690SJohn Fastabend 	}
3874f600b690SJohn Fastabend 
3875f600b690SJohn Fastabend 	return 0;
38762de2f7f4SJohn Fastabend 
38774941d472SJason Wang err:
387803aa6d34SToshiaki Makita 	if (!prog) {
387903aa6d34SToshiaki Makita 		virtnet_clear_guest_offloads(vi);
38804941d472SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
388103aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
388203aa6d34SToshiaki Makita 	}
388303aa6d34SToshiaki Makita 
38848be4d9a4SToshiaki Makita 	if (netif_running(dev)) {
3885534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
38864941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3887534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3888534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
3889534da5e8SToshiaki Makita 		}
38908be4d9a4SToshiaki Makita 	}
38912de2f7f4SJohn Fastabend 	if (prog)
38922de2f7f4SJohn Fastabend 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
38932de2f7f4SJohn Fastabend 	return err;
3894f600b690SJohn Fastabend }
3895f600b690SJohn Fastabend 
virtnet_xdp(struct net_device * dev,struct netdev_bpf * xdp)3896f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3897f600b690SJohn Fastabend {
3898f600b690SJohn Fastabend 	switch (xdp->command) {
3899f600b690SJohn Fastabend 	case XDP_SETUP_PROG:
39009861ce03SJakub Kicinski 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3901f600b690SJohn Fastabend 	default:
3902f600b690SJohn Fastabend 		return -EINVAL;
3903f600b690SJohn Fastabend 	}
3904f600b690SJohn Fastabend }
3905f600b690SJohn Fastabend 
virtnet_get_phys_port_name(struct net_device * dev,char * buf,size_t len)3906ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3907ba5e4426SSridhar Samudrala 				      size_t len)
3908ba5e4426SSridhar Samudrala {
3909ba5e4426SSridhar Samudrala 	struct virtnet_info *vi = netdev_priv(dev);
3910ba5e4426SSridhar Samudrala 	int ret;
3911ba5e4426SSridhar Samudrala 
3912ba5e4426SSridhar Samudrala 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3913ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
3914ba5e4426SSridhar Samudrala 
3915ba5e4426SSridhar Samudrala 	ret = snprintf(buf, len, "sby");
3916ba5e4426SSridhar Samudrala 	if (ret >= len)
3917ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
3918ba5e4426SSridhar Samudrala 
3919ba5e4426SSridhar Samudrala 	return 0;
3920ba5e4426SSridhar Samudrala }
3921ba5e4426SSridhar Samudrala 
virtnet_set_features(struct net_device * dev,netdev_features_t features)3922a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev,
3923a02e8964SWillem de Bruijn 				netdev_features_t features)
3924a02e8964SWillem de Bruijn {
3925a02e8964SWillem de Bruijn 	struct virtnet_info *vi = netdev_priv(dev);
3926cf8691cbSMichael S. Tsirkin 	u64 offloads;
3927a02e8964SWillem de Bruijn 	int err;
3928a02e8964SWillem de Bruijn 
3929dbcf24d1SJason Wang 	if ((dev->features ^ features) & NETIF_F_GRO_HW) {
393097c2c69eSXuan Zhuo 		if (vi->xdp_enabled)
3931a02e8964SWillem de Bruijn 			return -EBUSY;
3932a02e8964SWillem de Bruijn 
3933dbcf24d1SJason Wang 		if (features & NETIF_F_GRO_HW)
3934cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable;
3935a02e8964SWillem de Bruijn 		else
3936cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable &
3937dbcf24d1SJason Wang 				   ~GUEST_OFFLOAD_GRO_HW_MASK;
3938a02e8964SWillem de Bruijn 
3939a02e8964SWillem de Bruijn 		err = virtnet_set_guest_offloads(vi, offloads);
3940a02e8964SWillem de Bruijn 		if (err)
3941a02e8964SWillem de Bruijn 			return err;
39423618ad2aSTonghao Zhang 		vi->guest_offloads = offloads;
3943cf8691cbSMichael S. Tsirkin 	}
3944cf8691cbSMichael S. Tsirkin 
3945c7114b12SAndrew Melnychenko 	if ((dev->features ^ features) & NETIF_F_RXHASH) {
3946c7114b12SAndrew Melnychenko 		if (features & NETIF_F_RXHASH)
3947c1170820SAndrew Melnychenko 			vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3948c7114b12SAndrew Melnychenko 		else
3949c7114b12SAndrew Melnychenko 			vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3950c7114b12SAndrew Melnychenko 
3951c7114b12SAndrew Melnychenko 		if (!virtnet_commit_rss_command(vi))
3952c7114b12SAndrew Melnychenko 			return -EINVAL;
3953c7114b12SAndrew Melnychenko 	}
3954c7114b12SAndrew Melnychenko 
3955a02e8964SWillem de Bruijn 	return 0;
3956a02e8964SWillem de Bruijn }
3957a02e8964SWillem de Bruijn 
virtnet_tx_timeout(struct net_device * dev,unsigned int txqueue)3958a520794bSTony Lu static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3959a520794bSTony Lu {
3960a520794bSTony Lu 	struct virtnet_info *priv = netdev_priv(dev);
3961a520794bSTony Lu 	struct send_queue *sq = &priv->sq[txqueue];
3962a520794bSTony Lu 	struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3963a520794bSTony Lu 
3964a520794bSTony Lu 	u64_stats_update_begin(&sq->stats.syncp);
396527debe3eSEric Dumazet 	u64_stats_inc(&sq->stats.tx_timeouts);
3966a520794bSTony Lu 	u64_stats_update_end(&sq->stats.syncp);
3967a520794bSTony Lu 
3968a520794bSTony Lu 	netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3969a520794bSTony Lu 		   txqueue, sq->name, sq->vq->index, sq->vq->name,
39705337824fSEric Dumazet 		   jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3971a520794bSTony Lu }
3972a520794bSTony Lu 
397376288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
397476288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
397576288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
397676288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
397776288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
39789c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
39792af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
39803fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
39811824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
39821824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3983f4e63525SJakub Kicinski 	.ndo_bpf		= virtnet_xdp,
3984186b3c99SJason Wang 	.ndo_xdp_xmit		= virtnet_xdp_xmit,
39852836b4f2SVlad Yasevich 	.ndo_features_check	= passthru_features_check,
3986ba5e4426SSridhar Samudrala 	.ndo_get_phys_port_name	= virtnet_get_phys_port_name,
3987a02e8964SWillem de Bruijn 	.ndo_set_features	= virtnet_set_features,
3988a520794bSTony Lu 	.ndo_tx_timeout		= virtnet_tx_timeout,
398976288b4eSStephen Hemminger };
399076288b4eSStephen Hemminger 
virtnet_config_changed_work(struct work_struct * work)3991586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
39929f4d26d0SMark McLoughlin {
3993586d17c5SJason Wang 	struct virtnet_info *vi =
3994586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
39959f4d26d0SMark McLoughlin 	u16 v;
39969f4d26d0SMark McLoughlin 
3997855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3998855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
3999507613bfSMichael S. Tsirkin 		return;
4000586d17c5SJason Wang 
4001586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
4002ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
4003586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
4004586d17c5SJason Wang 	}
40059f4d26d0SMark McLoughlin 
40069f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
40079f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
40089f4d26d0SMark McLoughlin 
40099f4d26d0SMark McLoughlin 	if (vi->status == v)
4010507613bfSMichael S. Tsirkin 		return;
40119f4d26d0SMark McLoughlin 
40129f4d26d0SMark McLoughlin 	vi->status = v;
40139f4d26d0SMark McLoughlin 
40149f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
4015faa9b39fSJason Baron 		virtnet_update_settings(vi);
40169f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
4017986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
40189f4d26d0SMark McLoughlin 	} else {
40199f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
4020986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
40219f4d26d0SMark McLoughlin 	}
40229f4d26d0SMark McLoughlin }
40239f4d26d0SMark McLoughlin 
virtnet_config_changed(struct virtio_device * vdev)40249f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
40259f4d26d0SMark McLoughlin {
40269f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
40279f4d26d0SMark McLoughlin 
40283b07e9caSTejun Heo 	schedule_work(&vi->config_work);
40299f4d26d0SMark McLoughlin }
40309f4d26d0SMark McLoughlin 
virtnet_free_queues(struct virtnet_info * vi)4031986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
4032986a4f4dSJason Wang {
4033d4fb84eeSAndrey Vagin 	int i;
4034d4fb84eeSAndrey Vagin 
4035ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
40365198d545SJakub Kicinski 		__netif_napi_del(&vi->rq[i].napi);
40375198d545SJakub Kicinski 		__netif_napi_del(&vi->sq[i].napi);
4038ab3971b1SJason Wang 	}
4039d4fb84eeSAndrey Vagin 
40405198d545SJakub Kicinski 	/* We called __netif_napi_del(),
4041963abe5cSEric Dumazet 	 * we need to respect an RCU grace period before freeing vi->rq
4042963abe5cSEric Dumazet 	 */
4043963abe5cSEric Dumazet 	synchronize_net();
4044963abe5cSEric Dumazet 
4045986a4f4dSJason Wang 	kfree(vi->rq);
4046986a4f4dSJason Wang 	kfree(vi->sq);
404712e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
4048986a4f4dSJason Wang }
4049986a4f4dSJason Wang 
_free_receive_bufs(struct virtnet_info * vi)405047315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi)
4051986a4f4dSJason Wang {
4052f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
4053986a4f4dSJason Wang 	int i;
4054986a4f4dSJason Wang 
4055986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
4056986a4f4dSJason Wang 		while (vi->rq[i].pages)
4057986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
4058f600b690SJohn Fastabend 
4059f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
4060f600b690SJohn Fastabend 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
4061f600b690SJohn Fastabend 		if (old_prog)
4062f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
4063986a4f4dSJason Wang 	}
406447315329SJohn Fastabend }
406547315329SJohn Fastabend 
free_receive_bufs(struct virtnet_info * vi)406647315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi)
406747315329SJohn Fastabend {
406847315329SJohn Fastabend 	rtnl_lock();
406947315329SJohn Fastabend 	_free_receive_bufs(vi);
4070f600b690SJohn Fastabend 	rtnl_unlock();
4071986a4f4dSJason Wang }
4072986a4f4dSJason Wang 
free_receive_page_frags(struct virtnet_info * vi)4073fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
4074fb51879dSMichael Dalton {
4075fb51879dSMichael Dalton 	int i;
4076fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
4077295525e2SXuan Zhuo 		if (vi->rq[i].alloc_frag.page) {
4078295525e2SXuan Zhuo 			if (vi->rq[i].do_dma && vi->rq[i].last_dma)
4079295525e2SXuan Zhuo 				virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
4080fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
4081fb51879dSMichael Dalton 		}
4082295525e2SXuan Zhuo }
4083fb51879dSMichael Dalton 
virtnet_sq_free_unused_buf(struct virtqueue * vq,void * buf)40846e345f8cSXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
40856e345f8cSXuan Zhuo {
40866e345f8cSXuan Zhuo 	if (!is_xdp_frame(buf))
40876e345f8cSXuan Zhuo 		dev_kfree_skb(buf);
40886e345f8cSXuan Zhuo 	else
40896e345f8cSXuan Zhuo 		xdp_return_frame(ptr_to_xdp(buf));
40906e345f8cSXuan Zhuo }
40916e345f8cSXuan Zhuo 
free_unused_bufs(struct virtnet_info * vi)4092986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
4093986a4f4dSJason Wang {
4094986a4f4dSJason Wang 	void *buf;
4095986a4f4dSJason Wang 	int i;
4096986a4f4dSJason Wang 
4097986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
4098986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
40996e345f8cSXuan Zhuo 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
41006e345f8cSXuan Zhuo 			virtnet_sq_free_unused_buf(vq, buf);
4101f8bb5104SWenliang Wang 		cond_resched();
4102986a4f4dSJason Wang 	}
4103986a4f4dSJason Wang 
4104986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
41053ffd05c2SXuan Zhuo 		struct virtqueue *vq = vi->rq[i].vq;
4106295525e2SXuan Zhuo 
41073ffd05c2SXuan Zhuo 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
41083ffd05c2SXuan Zhuo 			virtnet_rq_unmap_free_buf(vq, buf);
4109f8bb5104SWenliang Wang 		cond_resched();
4110986a4f4dSJason Wang 	}
4111ab7db917SMichael Dalton }
4112986a4f4dSJason Wang 
virtnet_del_vqs(struct virtnet_info * vi)4113e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
4114e9d7417bSJason Wang {
4115e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
4116e9d7417bSJason Wang 
4117310974faSPeter Xu 	virtnet_clean_affinity(vi);
4118986a4f4dSJason Wang 
4119e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
4120986a4f4dSJason Wang 
4121986a4f4dSJason Wang 	virtnet_free_queues(vi);
4122986a4f4dSJason Wang }
4123986a4f4dSJason Wang 
4124d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at
4125d85b758fSMichael S. Tsirkin  * least one full packet?
4126d85b758fSMichael S. Tsirkin  * Logic below assumes the mergeable buffer header is used.
4127d85b758fSMichael S. Tsirkin  */
mergeable_min_buf_len(struct virtnet_info * vi,struct virtqueue * vq)4128d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
4129d85b758fSMichael S. Tsirkin {
4130c1ddc42dSAndrew Melnychenko 	const unsigned int hdr_len = vi->hdr_len;
4131d85b758fSMichael S. Tsirkin 	unsigned int rq_size = virtqueue_get_vring_size(vq);
4132d85b758fSMichael S. Tsirkin 	unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
4133d85b758fSMichael S. Tsirkin 	unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
4134d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
4135d85b758fSMichael S. Tsirkin 
4136f0c3192cSMichael S. Tsirkin 	return max(max(min_buf_len, hdr_len) - hdr_len,
4137f0c3192cSMichael S. Tsirkin 		   (unsigned int)GOOD_PACKET_LEN);
4138d85b758fSMichael S. Tsirkin }
4139d85b758fSMichael S. Tsirkin 
virtnet_find_vqs(struct virtnet_info * vi)4140986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
4141986a4f4dSJason Wang {
4142986a4f4dSJason Wang 	vq_callback_t **callbacks;
4143986a4f4dSJason Wang 	struct virtqueue **vqs;
4144986a4f4dSJason Wang 	const char **names;
414520e81d2cSZhu Yanjun 	int ret = -ENOMEM;
414620e81d2cSZhu Yanjun 	int total_vqs;
4147d45b897bSMichael S. Tsirkin 	bool *ctx;
414820e81d2cSZhu Yanjun 	u16 i;
4149986a4f4dSJason Wang 
4150986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
4151986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
4152986a4f4dSJason Wang 	 * possible control vq.
4153986a4f4dSJason Wang 	 */
4154986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
4155986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
4156986a4f4dSJason Wang 
4157986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
41586396bb22SKees Cook 	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
4159986a4f4dSJason Wang 	if (!vqs)
4160986a4f4dSJason Wang 		goto err_vq;
41616da2ec56SKees Cook 	callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
4162986a4f4dSJason Wang 	if (!callbacks)
4163986a4f4dSJason Wang 		goto err_callback;
41646da2ec56SKees Cook 	names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
4165986a4f4dSJason Wang 	if (!names)
4166986a4f4dSJason Wang 		goto err_names;
4167192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
41686396bb22SKees Cook 		ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
4169d45b897bSMichael S. Tsirkin 		if (!ctx)
4170d45b897bSMichael S. Tsirkin 			goto err_ctx;
4171d45b897bSMichael S. Tsirkin 	} else {
4172d45b897bSMichael S. Tsirkin 		ctx = NULL;
4173d45b897bSMichael S. Tsirkin 	}
4174986a4f4dSJason Wang 
4175986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
4176986a4f4dSJason Wang 	if (vi->has_cvq) {
4177986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
4178986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
4179986a4f4dSJason Wang 	}
4180986a4f4dSJason Wang 
4181986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
4182986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
4183986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
4184986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
418520e81d2cSZhu Yanjun 		sprintf(vi->rq[i].name, "input.%u", i);
418620e81d2cSZhu Yanjun 		sprintf(vi->sq[i].name, "output.%u", i);
4187986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
4188986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
4189d45b897bSMichael S. Tsirkin 		if (ctx)
4190d45b897bSMichael S. Tsirkin 			ctx[rxq2vq(i)] = true;
4191986a4f4dSJason Wang 	}
4192986a4f4dSJason Wang 
41932e9ca760SMichael S. Tsirkin 	ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
41942e9ca760SMichael S. Tsirkin 				  names, ctx, NULL);
4195986a4f4dSJason Wang 	if (ret)
4196986a4f4dSJason Wang 		goto err_find;
4197986a4f4dSJason Wang 
4198986a4f4dSJason Wang 	if (vi->has_cvq) {
4199986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
4200986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
4201f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4202986a4f4dSJason Wang 	}
4203986a4f4dSJason Wang 
4204986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
4205986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
4206d85b758fSMichael S. Tsirkin 		vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
4207986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
4208986a4f4dSJason Wang 	}
4209986a4f4dSJason Wang 
42102fa3c8a8STonghao Zhang 	/* run here: ret == 0. */
4211986a4f4dSJason Wang 
4212986a4f4dSJason Wang 
4213986a4f4dSJason Wang err_find:
4214d45b897bSMichael S. Tsirkin 	kfree(ctx);
4215d45b897bSMichael S. Tsirkin err_ctx:
4216986a4f4dSJason Wang 	kfree(names);
4217986a4f4dSJason Wang err_names:
4218986a4f4dSJason Wang 	kfree(callbacks);
4219986a4f4dSJason Wang err_callback:
4220986a4f4dSJason Wang 	kfree(vqs);
4221986a4f4dSJason Wang err_vq:
4222986a4f4dSJason Wang 	return ret;
4223986a4f4dSJason Wang }
4224986a4f4dSJason Wang 
virtnet_alloc_queues(struct virtnet_info * vi)4225986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
4226986a4f4dSJason Wang {
4227986a4f4dSJason Wang 	int i;
4228986a4f4dSJason Wang 
4229122b84a1SMax Gurtovoy 	if (vi->has_cvq) {
423012e57169SMichael S. Tsirkin 		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
423112e57169SMichael S. Tsirkin 		if (!vi->ctrl)
423212e57169SMichael S. Tsirkin 			goto err_ctrl;
4233122b84a1SMax Gurtovoy 	} else {
4234122b84a1SMax Gurtovoy 		vi->ctrl = NULL;
4235122b84a1SMax Gurtovoy 	}
42366396bb22SKees Cook 	vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
4237986a4f4dSJason Wang 	if (!vi->sq)
4238986a4f4dSJason Wang 		goto err_sq;
42396396bb22SKees Cook 	vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
4240008d4278SAmerigo Wang 	if (!vi->rq)
4241986a4f4dSJason Wang 		goto err_rq;
4242986a4f4dSJason Wang 
4243986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
4244986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
4245986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
4246d484735dSJakub Kicinski 		netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
4247986a4f4dSJason Wang 				      napi_weight);
42488d602e1aSJakub Kicinski 		netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
42498d602e1aSJakub Kicinski 					 virtnet_poll_tx,
4250b92f1e67SWillem de Bruijn 					 napi_tx ? napi_weight : 0);
4251986a4f4dSJason Wang 
4252986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
42535377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
4254986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
4255d7dfc5cfSToshiaki Makita 
4256d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->rq[i].stats.syncp);
4257d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->sq[i].stats.syncp);
4258986a4f4dSJason Wang 	}
4259986a4f4dSJason Wang 
4260986a4f4dSJason Wang 	return 0;
4261986a4f4dSJason Wang 
4262986a4f4dSJason Wang err_rq:
4263986a4f4dSJason Wang 	kfree(vi->sq);
4264986a4f4dSJason Wang err_sq:
426512e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
426612e57169SMichael S. Tsirkin err_ctrl:
4267986a4f4dSJason Wang 	return -ENOMEM;
4268e9d7417bSJason Wang }
4269e9d7417bSJason Wang 
init_vqs(struct virtnet_info * vi)42703f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
42713f9c10b0SAmit Shah {
4272986a4f4dSJason Wang 	int ret;
42733f9c10b0SAmit Shah 
4274986a4f4dSJason Wang 	/* Allocate send & receive queues */
4275986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
4276986a4f4dSJason Wang 	if (ret)
4277986a4f4dSJason Wang 		goto err;
42783f9c10b0SAmit Shah 
4279986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
4280986a4f4dSJason Wang 	if (ret)
4281986a4f4dSJason Wang 		goto err_free;
42823f9c10b0SAmit Shah 
4283295525e2SXuan Zhuo 	virtnet_rq_set_premapped(vi);
4284295525e2SXuan Zhuo 
4285a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_lock();
42868898c21cSWanlong Gao 	virtnet_set_affinity(vi);
4287a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_unlock();
428847be2479SWanlong Gao 
42893f9c10b0SAmit Shah 	return 0;
4290986a4f4dSJason Wang 
4291986a4f4dSJason Wang err_free:
4292986a4f4dSJason Wang 	virtnet_free_queues(vi);
4293986a4f4dSJason Wang err:
4294986a4f4dSJason Wang 	return ret;
42953f9c10b0SAmit Shah }
42963f9c10b0SAmit Shah 
4297fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
mergeable_rx_buffer_size_show(struct netdev_rx_queue * queue,char * buf)4298fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
4299718ad681Sstephen hemminger 		char *buf)
4300fbf28d78SMichael Dalton {
4301fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
4302fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
43033cc81a9aSJason Wang 	unsigned int headroom = virtnet_get_headroom(vi);
43043cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
43055377d758SJohannes Berg 	struct ewma_pkt_len *avg;
4306fbf28d78SMichael Dalton 
4307fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
4308fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
4309d85b758fSMichael S. Tsirkin 	return sprintf(buf, "%u\n",
43103cc81a9aSJason Wang 		       get_mergeable_buf_len(&vi->rq[queue_index], avg,
43113cc81a9aSJason Wang 				       SKB_DATA_ALIGN(headroom + tailroom)));
4312fbf28d78SMichael Dalton }
4313fbf28d78SMichael Dalton 
4314fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
4315fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
4316fbf28d78SMichael Dalton 
4317fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
4318fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
4319fbf28d78SMichael Dalton 	NULL
4320fbf28d78SMichael Dalton };
4321fbf28d78SMichael Dalton 
4322fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
4323fbf28d78SMichael Dalton 	.name = "virtio_net",
4324fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
4325fbf28d78SMichael Dalton };
4326fbf28d78SMichael Dalton #endif
4327fbf28d78SMichael Dalton 
virtnet_fail_on_feature(struct virtio_device * vdev,unsigned int fbit,const char * fname,const char * dname)4328892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
4329892d6eb1SJason Wang 				    unsigned int fbit,
4330892d6eb1SJason Wang 				    const char *fname, const char *dname)
4331892d6eb1SJason Wang {
4332892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
4333892d6eb1SJason Wang 		return false;
4334892d6eb1SJason Wang 
4335892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
4336892d6eb1SJason Wang 		fname, dname);
4337892d6eb1SJason Wang 
4338892d6eb1SJason Wang 	return true;
4339892d6eb1SJason Wang }
4340892d6eb1SJason Wang 
4341892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
4342892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
4343892d6eb1SJason Wang 
virtnet_validate_features(struct virtio_device * vdev)4344892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
4345892d6eb1SJason Wang {
4346892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
4347892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
4348892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
4349892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
4350892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
4351892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
4352892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
4353892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
4354892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
4355c7114b12SAndrew Melnychenko 			     "VIRTIO_NET_F_CTRL_VQ") ||
4356c7114b12SAndrew Melnychenko 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
435791f41f01SAndrew Melnychenko 			     "VIRTIO_NET_F_CTRL_VQ") ||
435891f41f01SAndrew Melnychenko 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
4359699b045aSAlvaro Karsz 			     "VIRTIO_NET_F_CTRL_VQ") ||
4360699b045aSAlvaro Karsz 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
43618af3bf66SGavin Li 			     "VIRTIO_NET_F_CTRL_VQ") ||
43628af3bf66SGavin Li 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
4363892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
4364892d6eb1SJason Wang 		return false;
4365892d6eb1SJason Wang 	}
4366892d6eb1SJason Wang 
4367892d6eb1SJason Wang 	return true;
4368892d6eb1SJason Wang }
4369892d6eb1SJason Wang 
4370d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
4371d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
4372d0c2c997SJarod Wilson 
virtnet_validate(struct virtio_device * vdev)4373fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev)
4374296f96fcSRusty Russell {
43756ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
43766ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
43776ba42248SMichael S. Tsirkin 			__func__);
43786ba42248SMichael S. Tsirkin 		return -EINVAL;
43796ba42248SMichael S. Tsirkin 	}
43806ba42248SMichael S. Tsirkin 
4381892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
4382892d6eb1SJason Wang 		return -EINVAL;
4383892d6eb1SJason Wang 
4384fe36cbe0SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4385fe36cbe0SMichael S. Tsirkin 		int mtu = virtio_cread16(vdev,
4386fe36cbe0SMichael S. Tsirkin 					 offsetof(struct virtio_net_config,
4387fe36cbe0SMichael S. Tsirkin 						  mtu));
4388fe36cbe0SMichael S. Tsirkin 		if (mtu < MIN_MTU)
4389fe36cbe0SMichael S. Tsirkin 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
4390fe36cbe0SMichael S. Tsirkin 	}
4391fe36cbe0SMichael S. Tsirkin 
43927c06458cSLaurent Vivier 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
43937c06458cSLaurent Vivier 	    !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
43947c06458cSLaurent Vivier 		dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
43957c06458cSLaurent Vivier 		__virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
43967c06458cSLaurent Vivier 	}
43977c06458cSLaurent Vivier 
4398fe36cbe0SMichael S. Tsirkin 	return 0;
4399fe36cbe0SMichael S. Tsirkin }
4400fe36cbe0SMichael S. Tsirkin 
virtnet_check_guest_gso(const struct virtnet_info * vi)440146cd26f4SGavin Li static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
440246cd26f4SGavin Li {
440346cd26f4SGavin Li 	return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
440446cd26f4SGavin Li 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
440546cd26f4SGavin Li 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
4406418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
4407418044e1SAndrew Melnychenko 		(virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
4408418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
440946cd26f4SGavin Li }
441046cd26f4SGavin Li 
virtnet_set_big_packets(struct virtnet_info * vi,const int mtu)44114959aebbSGavin Li static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
44124959aebbSGavin Li {
44134959aebbSGavin Li 	bool guest_gso = virtnet_check_guest_gso(vi);
44144959aebbSGavin Li 
44154959aebbSGavin Li 	/* If device can receive ANY guest GSO packets, regardless of mtu,
44164959aebbSGavin Li 	 * allocate packets of maximum size, otherwise limit it to only
44174959aebbSGavin Li 	 * mtu size worth only.
44184959aebbSGavin Li 	 */
44194959aebbSGavin Li 	if (mtu > ETH_DATA_LEN || guest_gso) {
44204959aebbSGavin Li 		vi->big_packets = true;
44214959aebbSGavin Li 		vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
44224959aebbSGavin Li 	}
44234959aebbSGavin Li }
44244959aebbSGavin Li 
virtnet_probe(struct virtio_device * vdev)4425fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev)
4426fe36cbe0SMichael S. Tsirkin {
4427d7dfc5cfSToshiaki Makita 	int i, err = -ENOMEM;
4428fe36cbe0SMichael S. Tsirkin 	struct net_device *dev;
4429fe36cbe0SMichael S. Tsirkin 	struct virtnet_info *vi;
4430fe36cbe0SMichael S. Tsirkin 	u16 max_queue_pairs;
44314959aebbSGavin Li 	int mtu = 0;
4432fe36cbe0SMichael S. Tsirkin 
4433c7114b12SAndrew Melnychenko 	/* Find if host supports multiqueue/rss virtio_net device */
4434c7114b12SAndrew Melnychenko 	max_queue_pairs = 1;
4435c7114b12SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
4436c7114b12SAndrew Melnychenko 		max_queue_pairs =
4437c7114b12SAndrew Melnychenko 		     virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
4438986a4f4dSJason Wang 
4439986a4f4dSJason Wang 	/* We need at least 2 queue's */
4440c7114b12SAndrew Melnychenko 	if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
4441986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
4442986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4443986a4f4dSJason Wang 		max_queue_pairs = 1;
4444296f96fcSRusty Russell 
4445296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
4446986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
4447296f96fcSRusty Russell 	if (!dev)
4448296f96fcSRusty Russell 		return -ENOMEM;
4449296f96fcSRusty Russell 
4450296f96fcSRusty Russell 	/* Set up network device as normal. */
4451ab5bd583SXuan Zhuo 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
4452ab5bd583SXuan Zhuo 			   IFF_TX_SKB_NO_LINEAR;
445376288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
4454296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
44553fa2a1dfSstephen hemminger 
44567ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
4457296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
4458296f96fcSRusty Russell 
4459296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
446098e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
4461296f96fcSRusty Russell 		/* This opens up the world of extra features. */
446248900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
446398e778c9SMichał Mirosław 		if (csum)
446448900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
446598e778c9SMichał Mirosław 
446698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
4467e078de03SDavid S. Miller 			dev->hw_features |= NETIF_F_TSO
446834a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
446934a48579SRusty Russell 		}
44705539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
447198e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
447298e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
447398e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
447498e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
447598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
447698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
4477418044e1SAndrew Melnychenko 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
4478418044e1SAndrew Melnychenko 			dev->hw_features |= NETIF_F_GSO_UDP_L4;
447998e778c9SMichał Mirosław 
448041f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
448141f2f127SJason Wang 
448298e778c9SMichał Mirosław 		if (gso)
4483e078de03SDavid S. Miller 			dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
448498e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
4485296f96fcSRusty Russell 	}
4486ef609fd7SHeng Qi 
4487ef609fd7SHeng Qi 	/* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
4488ef609fd7SHeng Qi 	 * need to calculate checksums for partially checksummed packets,
4489ef609fd7SHeng Qi 	 * as they're considered valid by the upper layer.
4490ef609fd7SHeng Qi 	 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
4491ef609fd7SHeng Qi 	 * receives fully checksummed packets. The device may assist in
4492ef609fd7SHeng Qi 	 * validating these packets' checksums, so the driver won't have to.
4493ef609fd7SHeng Qi 	 */
44944f49129bSThomas Huth 	dev->features |= NETIF_F_RXCSUM;
4495ef609fd7SHeng Qi 
4496a02e8964SWillem de Bruijn 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4497a02e8964SWillem de Bruijn 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
4498dbcf24d1SJason Wang 		dev->features |= NETIF_F_GRO_HW;
4499cf8691cbSMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
4500dbcf24d1SJason Wang 		dev->hw_features |= NETIF_F_GRO_HW;
4501296f96fcSRusty Russell 
45024fda8302SJason Wang 	dev->vlan_features = dev->features;
450366c0e13aSMarek Majtyka 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
45044fda8302SJason Wang 
4505d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
4506d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
4507d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
4508d0c2c997SJarod Wilson 
4509296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
4510f2edaa4aSJakub Kicinski 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4511f2edaa4aSJakub Kicinski 		u8 addr[ETH_ALEN];
4512f2edaa4aSJakub Kicinski 
4513855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
4514a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
4515f2edaa4aSJakub Kicinski 				   addr, ETH_ALEN);
4516f2edaa4aSJakub Kicinski 		eth_hw_addr_set(dev, addr);
4517f2edaa4aSJakub Kicinski 	} else {
4518f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
45199f62d221SLaurent Vivier 		dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
45209f62d221SLaurent Vivier 			 dev->dev_addr);
4521f2edaa4aSJakub Kicinski 	}
4522296f96fcSRusty Russell 
4523296f96fcSRusty Russell 	/* Set up our device-specific information */
4524296f96fcSRusty Russell 	vi = netdev_priv(dev);
4525296f96fcSRusty Russell 	vi->dev = dev;
4526296f96fcSRusty Russell 	vi->vdev = vdev;
4527d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
4528827da44cSJohn Stultz 
4529586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
45305a159128SJason Wang 	spin_lock_init(&vi->refill_lock);
4531296f96fcSRusty Russell 
453230bbf891SLorenzo Bianconi 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
45333f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
453430bbf891SLorenzo Bianconi 		dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
453530bbf891SLorenzo Bianconi 	}
45363f2c31d9SMark McLoughlin 
4537699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
4538308d7982SGavin Li 		vi->intr_coal_rx.max_usecs = 0;
4539308d7982SGavin Li 		vi->intr_coal_tx.max_usecs = 0;
4540308d7982SGavin Li 		vi->intr_coal_tx.max_packets = 0;
4541308d7982SGavin Li 		vi->intr_coal_rx.max_packets = 0;
4542699b045aSAlvaro Karsz 	}
4543699b045aSAlvaro Karsz 
454491f41f01SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
454591f41f01SAndrew Melnychenko 		vi->has_rss_hash_report = true;
454691f41f01SAndrew Melnychenko 
454743a71c1bSBreno Leitao 	if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
4548c7114b12SAndrew Melnychenko 		vi->has_rss = true;
454991f41f01SAndrew Melnychenko 
4550c7114b12SAndrew Melnychenko 		vi->rss_indir_table_size =
4551c7114b12SAndrew Melnychenko 			virtio_cread16(vdev, offsetof(struct virtio_net_config,
4552c7114b12SAndrew Melnychenko 				rss_max_indirection_table_length));
455343a71c1bSBreno Leitao 	}
455443a71c1bSBreno Leitao 
455543a71c1bSBreno Leitao 	if (vi->has_rss || vi->has_rss_hash_report) {
4556c7114b12SAndrew Melnychenko 		vi->rss_key_size =
4557c7114b12SAndrew Melnychenko 			virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
4558af0aa8aeSPhilo Lu 		if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
4559af0aa8aeSPhilo Lu 			dev_err(&vdev->dev, "rss_max_key_size=%u exceeds the limit %u.\n",
4560af0aa8aeSPhilo Lu 				vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE);
4561af0aa8aeSPhilo Lu 			err = -EINVAL;
4562af0aa8aeSPhilo Lu 			goto free;
4563af0aa8aeSPhilo Lu 		}
4564c7114b12SAndrew Melnychenko 
4565c7114b12SAndrew Melnychenko 		vi->rss_hash_types_supported =
4566c7114b12SAndrew Melnychenko 		    virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
4567c7114b12SAndrew Melnychenko 		vi->rss_hash_types_supported &=
4568c7114b12SAndrew Melnychenko 				~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
4569c7114b12SAndrew Melnychenko 				  VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
4570c7114b12SAndrew Melnychenko 				  VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
4571c7114b12SAndrew Melnychenko 
4572c7114b12SAndrew Melnychenko 		dev->hw_features |= NETIF_F_RXHASH;
4573c7114b12SAndrew Melnychenko 	}
457491f41f01SAndrew Melnychenko 
457591f41f01SAndrew Melnychenko 	if (vi->has_rss_hash_report)
457691f41f01SAndrew Melnychenko 		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
457791f41f01SAndrew Melnychenko 	else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
4578d04302b3SMichael S. Tsirkin 		 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4579012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
4580012873d0SMichael S. Tsirkin 	else
4581012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
4582012873d0SMichael S. Tsirkin 
458375993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
458475993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4585e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
4586e7428e95SMichael S. Tsirkin 
4587986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4588986a4f4dSJason Wang 		vi->has_cvq = true;
4589986a4f4dSJason Wang 
459014de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
459114de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
459214de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
459314de9d11SAaron Conole 					      mtu));
459493a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
4595fe36cbe0SMichael S. Tsirkin 			/* Should never trigger: MTU was previously validated
4596fe36cbe0SMichael S. Tsirkin 			 * in virtnet_validate.
4597fe36cbe0SMichael S. Tsirkin 			 */
45987934b481SYuval Shaia 			dev_err(&vdev->dev,
45997934b481SYuval Shaia 				"device MTU appears to have changed it is now %d < %d",
46007934b481SYuval Shaia 				mtu, dev->min_mtu);
4601411ea23aSDan Carpenter 			err = -EINVAL;
4602d7dfc5cfSToshiaki Makita 			goto free;
4603fe36cbe0SMichael S. Tsirkin 		}
4604fe36cbe0SMichael S. Tsirkin 
4605d0c2c997SJarod Wilson 		dev->mtu = mtu;
460693a205eeSAaron Conole 		dev->max_mtu = mtu;
460714de9d11SAaron Conole 	}
460814de9d11SAaron Conole 
46094959aebbSGavin Li 	virtnet_set_big_packets(vi, mtu);
46104959aebbSGavin Li 
4611012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
4612012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
46136ebbc1a6SZhangjie \(HZ\) 
461444900010SJason Wang 	/* Enable multiqueue by default */
461544900010SJason Wang 	if (num_online_cpus() >= max_queue_pairs)
461644900010SJason Wang 		vi->curr_queue_pairs = max_queue_pairs;
461744900010SJason Wang 	else
461844900010SJason Wang 		vi->curr_queue_pairs = num_online_cpus();
4619986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
4620986a4f4dSJason Wang 
4621986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
46223f9c10b0SAmit Shah 	err = init_vqs(vi);
4623d2a7dddaSMichael S. Tsirkin 	if (err)
4624d7dfc5cfSToshiaki Makita 		goto free;
4625d2a7dddaSMichael S. Tsirkin 
4626fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
4627fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
4628fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
4629fbf28d78SMichael Dalton #endif
46300f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
46310f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
4632986a4f4dSJason Wang 
46332e9ca760SMichael S. Tsirkin 	virtnet_init_settings(dev);
46342e9ca760SMichael S. Tsirkin 
4635ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4636ba5e4426SSridhar Samudrala 		vi->failover = net_failover_create(vi->dev);
46374b8e6ac4SWei Yongjun 		if (IS_ERR(vi->failover)) {
46384b8e6ac4SWei Yongjun 			err = PTR_ERR(vi->failover);
4639ba5e4426SSridhar Samudrala 			goto free_vqs;
4640ba5e4426SSridhar Samudrala 		}
46414b8e6ac4SWei Yongjun 	}
4642ba5e4426SSridhar Samudrala 
464391f41f01SAndrew Melnychenko 	if (vi->has_rss || vi->has_rss_hash_report)
4644c7114b12SAndrew Melnychenko 		virtnet_init_default_rss(vi);
4645c7114b12SAndrew Melnychenko 
464650c0ada6SJason Wang 	/* serialize netdev register + virtio_device_ready() with ndo_open() */
464750c0ada6SJason Wang 	rtnl_lock();
464850c0ada6SJason Wang 
464950c0ada6SJason Wang 	err = register_netdevice(dev);
4650296f96fcSRusty Russell 	if (err) {
4651296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
465250c0ada6SJason Wang 		rtnl_unlock();
4653ba5e4426SSridhar Samudrala 		goto free_failover;
4654296f96fcSRusty Russell 	}
4655b3369c1fSRusty Russell 
46564baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
46574baf1e33SMichael S. Tsirkin 
465851b81317SJason Wang 	_virtnet_set_queues(vi, vi->curr_queue_pairs);
465951b81317SJason Wang 
46609f62d221SLaurent Vivier 	/* a random MAC address has been assigned, notify the device.
46619f62d221SLaurent Vivier 	 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
46629f62d221SLaurent Vivier 	 * because many devices work fine without getting MAC explicitly
46639f62d221SLaurent Vivier 	 */
46649f62d221SLaurent Vivier 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
46659f62d221SLaurent Vivier 	    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
46669f62d221SLaurent Vivier 		struct scatterlist sg;
46679f62d221SLaurent Vivier 
46689f62d221SLaurent Vivier 		sg_init_one(&sg, dev->dev_addr, dev->addr_len);
46699f62d221SLaurent Vivier 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
46709f62d221SLaurent Vivier 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
46719f62d221SLaurent Vivier 			pr_debug("virtio_net: setting MAC address failed\n");
46729f62d221SLaurent Vivier 			rtnl_unlock();
46739f62d221SLaurent Vivier 			err = -EINVAL;
46749f62d221SLaurent Vivier 			goto free_unregister_netdev;
46759f62d221SLaurent Vivier 		}
46769f62d221SLaurent Vivier 	}
46779f62d221SLaurent Vivier 
467850c0ada6SJason Wang 	rtnl_unlock();
467950c0ada6SJason Wang 
46808017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
46818de4b2f3SWanlong Gao 	if (err) {
46828de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
4683f00e35e2Swangyunjian 		goto free_unregister_netdev;
46848de4b2f3SWanlong Gao 	}
46858de4b2f3SWanlong Gao 
4686167c25e4SJason Wang 	/* Assume link up if device can't report link status,
4687167c25e4SJason Wang 	   otherwise get link status from config. */
4688167c25e4SJason Wang 	netif_carrier_off(dev);
4689bda7fab5SJay Vosburgh 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
46903b07e9caSTejun Heo 		schedule_work(&vi->config_work);
4691167c25e4SJason Wang 	} else {
4692167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
4693faa9b39fSJason Baron 		virtnet_update_settings(vi);
46944783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
4695167c25e4SJason Wang 	}
46969f4d26d0SMark McLoughlin 
46973f93522fSJason Wang 	for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
46983f93522fSJason Wang 		if (virtio_has_feature(vi->vdev, guest_offloads[i]))
46993f93522fSJason Wang 			set_bit(guest_offloads[i], &vi->guest_offloads);
4700a02e8964SWillem de Bruijn 	vi->guest_offloads_capable = vi->guest_offloads;
47013f93522fSJason Wang 
4702986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
4703986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
4704986a4f4dSJason Wang 
4705296f96fcSRusty Russell 	return 0;
4706296f96fcSRusty Russell 
4707f00e35e2Swangyunjian free_unregister_netdev:
4708b3369c1fSRusty Russell 	unregister_netdev(dev);
4709ba5e4426SSridhar Samudrala free_failover:
4710ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
4711d2a7dddaSMichael S. Tsirkin free_vqs:
4712b0686565SLi Zetao 	virtio_reset_device(vdev);
4713986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
4714fb51879dSMichael Dalton 	free_receive_page_frags(vi);
4715e9d7417bSJason Wang 	virtnet_del_vqs(vi);
4716296f96fcSRusty Russell free:
4717296f96fcSRusty Russell 	free_netdev(dev);
4718296f96fcSRusty Russell 	return err;
4719296f96fcSRusty Russell }
4720296f96fcSRusty Russell 
remove_vq_common(struct virtnet_info * vi)472104486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
4722296f96fcSRusty Russell {
4723d9679d00SMichael S. Tsirkin 	virtio_reset_device(vi->vdev);
4724830a8a97SShirley Ma 
4725830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
47269ab86bbcSShirley Ma 	free_unused_bufs(vi);
4727fb6813f4SRusty Russell 
4728986a4f4dSJason Wang 	free_receive_bufs(vi);
4729d2a7dddaSMichael S. Tsirkin 
4730fb51879dSMichael Dalton 	free_receive_page_frags(vi);
4731fb51879dSMichael Dalton 
4732986a4f4dSJason Wang 	virtnet_del_vqs(vi);
473304486ed0SAmit Shah }
473404486ed0SAmit Shah 
virtnet_remove(struct virtio_device * vdev)47358cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
473604486ed0SAmit Shah {
473704486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
473804486ed0SAmit Shah 
47398017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
47408de4b2f3SWanlong Gao 
4741102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
4742102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
4743586d17c5SJason Wang 
474404486ed0SAmit Shah 	unregister_netdev(vi->dev);
474504486ed0SAmit Shah 
4746ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
4747ba5e4426SSridhar Samudrala 
474804486ed0SAmit Shah 	remove_vq_common(vi);
4749fb6813f4SRusty Russell 
475074b2553fSRusty Russell 	free_netdev(vi->dev);
4751296f96fcSRusty Russell }
4752296f96fcSRusty Russell 
virtnet_freeze(struct virtio_device * vdev)475367a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
47540741bcb5SAmit Shah {
47550741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
47560741bcb5SAmit Shah 
47578017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
47589fe7bfceSJohn Fastabend 	virtnet_freeze_down(vdev);
47590741bcb5SAmit Shah 	remove_vq_common(vi);
47600741bcb5SAmit Shah 
47610741bcb5SAmit Shah 	return 0;
47620741bcb5SAmit Shah }
47630741bcb5SAmit Shah 
virtnet_restore(struct virtio_device * vdev)476467a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
47650741bcb5SAmit Shah {
47660741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
47679fe7bfceSJohn Fastabend 	int err;
47680741bcb5SAmit Shah 
47699fe7bfceSJohn Fastabend 	err = virtnet_restore_up(vdev);
47700741bcb5SAmit Shah 	if (err)
47710741bcb5SAmit Shah 		return err;
4772986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
4773986a4f4dSJason Wang 
47748017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
47753f2869caSXie Yongji 	if (err) {
47763f2869caSXie Yongji 		virtnet_freeze_down(vdev);
47773f2869caSXie Yongji 		remove_vq_common(vi);
4778ec9debbdSJason Wang 		return err;
47793f2869caSXie Yongji 	}
4780ec9debbdSJason Wang 
47810741bcb5SAmit Shah 	return 0;
47820741bcb5SAmit Shah }
47830741bcb5SAmit Shah 
4784296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
4785296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4786296f96fcSRusty Russell 	{ 0 },
4787296f96fcSRusty Russell };
4788296f96fcSRusty Russell 
4789f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \
4790f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4791f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MAC, \
4792f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4793f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4794f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4795418044e1SAndrew Melnychenko 	VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4796f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4797f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4798f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4799f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
4800faa9b39fSJason Baron 	VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4801c7114b12SAndrew Melnychenko 	VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4802be50da3eSJiri Pirko 	VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
48038af3bf66SGavin Li 	VIRTIO_NET_F_VQ_NOTF_COAL, \
4804be50da3eSJiri Pirko 	VIRTIO_NET_F_GUEST_HDRLEN
4805f3358507SMichael S. Tsirkin 
4806c45a6816SRusty Russell static unsigned int features[] = {
4807f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
4808f3358507SMichael S. Tsirkin };
4809f3358507SMichael S. Tsirkin 
4810f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = {
4811f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
4812f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GSO,
4813e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
4814c45a6816SRusty Russell };
4815c45a6816SRusty Russell 
481622402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
4817c45a6816SRusty Russell 	.feature_table = features,
4818c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
4819f3358507SMichael S. Tsirkin 	.feature_table_legacy = features_legacy,
4820f3358507SMichael S. Tsirkin 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4821296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
4822296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
4823296f96fcSRusty Russell 	.id_table =	id_table,
4824fe36cbe0SMichael S. Tsirkin 	.validate =	virtnet_validate,
4825296f96fcSRusty Russell 	.probe =	virtnet_probe,
48268cc085d6SBill Pemberton 	.remove =	virtnet_remove,
48279f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
482889107000SAaron Lu #ifdef CONFIG_PM_SLEEP
48290741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
48300741bcb5SAmit Shah 	.restore =	virtnet_restore,
48310741bcb5SAmit Shah #endif
4832296f96fcSRusty Russell };
4833296f96fcSRusty Russell 
virtio_net_driver_init(void)48348017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
48358017c279SSebastian Andrzej Siewior {
48368017c279SSebastian Andrzej Siewior 	int ret;
48378017c279SSebastian Andrzej Siewior 
483873c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
48398017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
48408017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
48418017c279SSebastian Andrzej Siewior 	if (ret < 0)
48428017c279SSebastian Andrzej Siewior 		goto out;
48438017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
484473c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
48458017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
48468017c279SSebastian Andrzej Siewior 	if (ret)
48478017c279SSebastian Andrzej Siewior 		goto err_dead;
48488017c279SSebastian Andrzej Siewior 	ret = register_virtio_driver(&virtio_net_driver);
48498017c279SSebastian Andrzej Siewior 	if (ret)
48508017c279SSebastian Andrzej Siewior 		goto err_virtio;
48518017c279SSebastian Andrzej Siewior 	return 0;
48528017c279SSebastian Andrzej Siewior err_virtio:
48538017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
48548017c279SSebastian Andrzej Siewior err_dead:
48558017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
48568017c279SSebastian Andrzej Siewior out:
48578017c279SSebastian Andrzej Siewior 	return ret;
48588017c279SSebastian Andrzej Siewior }
48598017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
48608017c279SSebastian Andrzej Siewior 
virtio_net_driver_exit(void)48618017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
48628017c279SSebastian Andrzej Siewior {
4863cfa0ebc9SAndrew Jones 	unregister_virtio_driver(&virtio_net_driver);
48648017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
48658017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
48668017c279SSebastian Andrzej Siewior }
48678017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
4868296f96fcSRusty Russell 
4869296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
4870296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
4871296f96fcSRusty Russell MODULE_LICENSE("GPL");
4872